2024-02-18 18:17:15 +08:00
|
|
|
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-10-27 04:19:23 +08:00
|
|
|
|
2024-02-18 18:17:15 +08:00
|
|
|
#include <string.h>
|
2023-07-16 21:42:56 +08:00
|
|
|
#include "action.h"
|
2024-02-18 18:17:15 +08:00
|
|
|
#include "encoder.h"
|
2023-07-16 21:42:56 +08:00
|
|
|
#include "wait.h"
|
|
|
|
|
2022-03-09 16:29:00 +08:00
|
|
|
#ifndef ENCODER_MAP_KEY_DELAY
|
2022-08-29 12:53:08 +08:00
|
|
|
# define ENCODER_MAP_KEY_DELAY TAP_CODE_DELAY
|
2022-03-09 16:29:00 +08:00
|
|
|
#endif
|
|
|
|
|
2024-02-18 18:17:15 +08:00
|
|
|
__attribute__((weak)) bool should_process_encoder(void) {
|
|
|
|
return is_keyboard_master();
|
|
|
|
}
|
2018-10-27 04:19:23 +08:00
|
|
|
|
2024-02-18 18:17:15 +08:00
|
|
|
static encoder_events_t encoder_events;
|
2024-03-10 19:24:17 +08:00
|
|
|
static bool signal_queue_drain = false;
|
2024-02-18 18:17:15 +08:00
|
|
|
|
|
|
|
void encoder_init(void) {
|
|
|
|
memset(&encoder_events, 0, sizeof(encoder_events));
|
|
|
|
encoder_driver_init();
|
|
|
|
}
|
2018-10-27 04:19:23 +08:00
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
static void encoder_queue_drain(void) {
|
|
|
|
encoder_events.tail = encoder_events.head;
|
|
|
|
encoder_events.dequeued = encoder_events.enqueued;
|
|
|
|
}
|
2022-03-08 13:58:05 +08:00
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
static bool encoder_handle_queue(void) {
|
|
|
|
bool changed = false;
|
|
|
|
uint8_t index;
|
|
|
|
bool clockwise;
|
|
|
|
while (encoder_dequeue_event(&index, &clockwise)) {
|
2024-02-18 18:17:15 +08:00
|
|
|
#ifdef ENCODER_MAP_ENABLE
|
2022-03-08 13:58:05 +08:00
|
|
|
|
2024-02-18 18:17:15 +08:00
|
|
|
// The delays below cater for Windows and its wonderful requirements.
|
2024-03-10 19:24:17 +08:00
|
|
|
action_exec(clockwise ? MAKE_ENCODER_CW_EVENT(index, true) : MAKE_ENCODER_CCW_EVENT(index, true));
|
2024-02-18 18:17:15 +08:00
|
|
|
# if ENCODER_MAP_KEY_DELAY > 0
|
|
|
|
wait_ms(ENCODER_MAP_KEY_DELAY);
|
|
|
|
# endif // ENCODER_MAP_KEY_DELAY > 0
|
2018-10-27 04:19:23 +08:00
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
action_exec(clockwise ? MAKE_ENCODER_CW_EVENT(index, false) : MAKE_ENCODER_CCW_EVENT(index, false));
|
2024-02-18 18:17:15 +08:00
|
|
|
# if ENCODER_MAP_KEY_DELAY > 0
|
|
|
|
wait_ms(ENCODER_MAP_KEY_DELAY);
|
|
|
|
# endif // ENCODER_MAP_KEY_DELAY > 0
|
|
|
|
|
|
|
|
#else // ENCODER_MAP_ENABLE
|
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
encoder_update_kb(index, clockwise);
|
2024-02-18 18:17:15 +08:00
|
|
|
|
|
|
|
#endif // ENCODER_MAP_ENABLE
|
|
|
|
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
2018-10-27 04:19:23 +08:00
|
|
|
|
2024-02-18 18:17:15 +08:00
|
|
|
bool encoder_task(void) {
|
|
|
|
bool changed = false;
|
2019-04-04 07:01:17 +08:00
|
|
|
|
2021-11-27 11:40:09 +08:00
|
|
|
#ifdef SPLIT_KEYBOARD
|
2024-02-18 18:17:15 +08:00
|
|
|
// Attempt to process existing encoder events in case split handling has already enqueued events
|
|
|
|
if (should_process_encoder()) {
|
|
|
|
changed |= encoder_handle_queue();
|
|
|
|
}
|
|
|
|
#endif // SPLIT_KEYBOARD
|
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
if (signal_queue_drain) {
|
|
|
|
signal_queue_drain = false;
|
|
|
|
encoder_queue_drain();
|
|
|
|
}
|
|
|
|
|
2024-02-18 18:17:15 +08:00
|
|
|
// Let the encoder driver produce events
|
|
|
|
encoder_driver_task();
|
|
|
|
|
|
|
|
// Process any events that were enqueued
|
|
|
|
if (should_process_encoder()) {
|
|
|
|
changed |= encoder_handle_queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
bool encoder_queue_full_advanced(encoder_events_t *events) {
|
2024-04-27 07:28:29 +08:00
|
|
|
return events->tail == (events->head + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
2024-03-10 19:24:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool encoder_queue_full(void) {
|
|
|
|
return encoder_queue_full_advanced(&encoder_events);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool encoder_queue_empty_advanced(encoder_events_t *events) {
|
|
|
|
return events->head == events->tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool encoder_queue_empty(void) {
|
|
|
|
return encoder_queue_empty_advanced(&encoder_events);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool encoder_queue_event_advanced(encoder_events_t *events, uint8_t index, bool clockwise) {
|
2024-02-18 18:17:15 +08:00
|
|
|
// Drop out if we're full
|
2024-03-10 19:24:17 +08:00
|
|
|
if (encoder_queue_full_advanced(events)) {
|
2024-02-18 18:17:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the event
|
2024-03-10 19:24:17 +08:00
|
|
|
encoder_event_t new_event = {.index = index, .clockwise = clockwise ? 1 : 0};
|
|
|
|
events->queue[events->head] = new_event;
|
2024-02-18 18:17:15 +08:00
|
|
|
|
|
|
|
// Increment the head index
|
2024-03-10 19:24:17 +08:00
|
|
|
events->head = (events->head + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
|
|
|
events->enqueued++;
|
2024-02-18 18:17:15 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
bool encoder_dequeue_event_advanced(encoder_events_t *events, uint8_t *index, bool *clockwise) {
|
|
|
|
if (encoder_queue_empty_advanced(events)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the event
|
|
|
|
encoder_event_t event = events->queue[events->tail];
|
|
|
|
*index = event.index;
|
|
|
|
*clockwise = event.clockwise;
|
|
|
|
|
|
|
|
// Increment the tail index
|
|
|
|
events->tail = (events->tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
|
|
|
events->dequeued++;
|
|
|
|
|
|
|
|
return true;
|
2024-02-18 18:17:15 +08:00
|
|
|
}
|
2018-10-27 04:19:23 +08:00
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
bool encoder_queue_event(uint8_t index, bool clockwise) {
|
|
|
|
return encoder_queue_event_advanced(&encoder_events, index, clockwise);
|
2024-02-18 18:17:15 +08:00
|
|
|
}
|
2022-03-08 13:58:05 +08:00
|
|
|
|
2024-03-10 19:24:17 +08:00
|
|
|
bool encoder_dequeue_event(uint8_t *index, bool *clockwise) {
|
|
|
|
return encoder_dequeue_event_advanced(&encoder_events, index, clockwise);
|
|
|
|
}
|
|
|
|
|
|
|
|
void encoder_retrieve_events(encoder_events_t *events) {
|
|
|
|
memcpy(events, &encoder_events, sizeof(encoder_events));
|
|
|
|
}
|
|
|
|
|
|
|
|
void encoder_signal_queue_drain(void) {
|
|
|
|
signal_queue_drain = true;
|
2022-02-22 00:04:41 +08:00
|
|
|
}
|
2022-02-21 22:13:06 +08:00
|
|
|
|
2022-02-13 02:29:31 +08:00
|
|
|
__attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-27 04:19:23 +08:00
|
|
|
|
2022-02-13 02:29:31 +08:00
|
|
|
__attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) {
|
2023-04-04 00:18:17 +08:00
|
|
|
bool res = encoder_update_user(index, clockwise);
|
|
|
|
#if !defined(ENCODER_TESTS)
|
|
|
|
if (res) {
|
|
|
|
if (clockwise) {
|
|
|
|
# if defined(EXTRAKEY_ENABLE)
|
|
|
|
tap_code_delay(KC_VOLU, 10);
|
|
|
|
# elif defined(MOUSEKEY_ENABLE)
|
2024-07-03 16:35:54 +08:00
|
|
|
tap_code_delay(QK_MOUSE_WHEEL_UP, 10);
|
2023-04-04 00:18:17 +08:00
|
|
|
# else
|
|
|
|
tap_code_delay(KC_PGDN, 10);
|
|
|
|
# endif
|
|
|
|
} else {
|
|
|
|
# if defined(EXTRAKEY_ENABLE)
|
|
|
|
tap_code_delay(KC_VOLD, 10);
|
|
|
|
# elif defined(MOUSEKEY_ENABLE)
|
2024-07-03 16:35:54 +08:00
|
|
|
tap_code_delay(QK_MOUSE_WHEEL_DOWN, 10);
|
2023-04-04 00:18:17 +08:00
|
|
|
# else
|
|
|
|
tap_code_delay(KC_PGUP, 10);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // ENCODER_TESTS
|
|
|
|
return res;
|
2022-02-13 02:29:31 +08:00
|
|
|
}
|