From c4a74be7f02ec64033638e93a49924df20fb2e57 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 13 Jun 2024 21:59:46 +1000 Subject: Add process_keycode handlers for new RGB Matrix and Underglow keycodes (#23896) --- quantum/process_keycode/process_rgb_matrix.c | 101 +++++++++++++++++++++++++++ quantum/process_keycode/process_rgb_matrix.h | 10 +++ quantum/process_keycode/process_underglow.c | 91 ++++++++++++++++++++++++ quantum/process_keycode/process_underglow.h | 10 +++ 4 files changed, 212 insertions(+) create mode 100644 quantum/process_keycode/process_rgb_matrix.c create mode 100644 quantum/process_keycode/process_rgb_matrix.h create mode 100644 quantum/process_keycode/process_underglow.c create mode 100644 quantum/process_keycode/process_underglow.h (limited to 'quantum/process_keycode') diff --git a/quantum/process_keycode/process_rgb_matrix.c b/quantum/process_keycode/process_rgb_matrix.c new file mode 100644 index 0000000000..fd2aa1a0c7 --- /dev/null +++ b/quantum/process_keycode/process_rgb_matrix.c @@ -0,0 +1,101 @@ +// Copyright 2024 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "process_rgb_matrix.h" +#include "rgb_matrix.h" +#include "action_util.h" +#include "keycodes.h" +#include "modifiers.h" + +bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { +#ifdef RGB_TRIGGER_ON_KEYDOWN + if (record->event.pressed) { +#else + if (!record->event.pressed) { +#endif + bool shifted = get_mods() & MOD_MASK_SHIFT; + switch (keycode) { + case QK_RGB_MATRIX_ON: + rgb_matrix_enable(); + return false; + case QK_RGB_MATRIX_OFF: + rgb_matrix_disable(); + return false; + case QK_RGB_MATRIX_TOGGLE: + rgb_matrix_toggle(); + return false; + case QK_RGB_MATRIX_MODE_NEXT: + if (shifted) { + rgb_matrix_step_reverse(); + } else { + rgb_matrix_step(); + } + return false; + case QK_RGB_MATRIX_MODE_PREVIOUS: + if (shifted) { + rgb_matrix_step(); + } else { + rgb_matrix_step_reverse(); + } + return false; + case QK_RGB_MATRIX_HUE_UP: + if (shifted) { + rgb_matrix_decrease_hue(); + } else { + rgb_matrix_increase_hue(); + } + return false; + case QK_RGB_MATRIX_HUE_DOWN: + if (shifted) { + rgb_matrix_increase_hue(); + } else { + rgb_matrix_decrease_hue(); + } + return false; + case QK_RGB_MATRIX_SATURATION_UP: + if (shifted) { + rgb_matrix_decrease_sat(); + } else { + rgb_matrix_increase_sat(); + } + return false; + case QK_RGB_MATRIX_SATURATION_DOWN: + if (shifted) { + rgb_matrix_increase_sat(); + } else { + rgb_matrix_decrease_sat(); + } + return false; + case QK_RGB_MATRIX_VALUE_UP: + if (shifted) { + rgb_matrix_decrease_val(); + } else { + rgb_matrix_increase_val(); + } + return false; + case QK_RGB_MATRIX_VALUE_DOWN: + if (shifted) { + rgb_matrix_increase_val(); + } else { + rgb_matrix_decrease_val(); + } + return false; + case QK_RGB_MATRIX_SPEED_UP: + if (shifted) { + rgb_matrix_decrease_speed(); + } else { + rgb_matrix_increase_speed(); + } + return false; + case QK_RGB_MATRIX_SPEED_DOWN: + if (shifted) { + rgb_matrix_increase_speed(); + } else { + rgb_matrix_decrease_speed(); + } + return false; + } + } + + return true; +} diff --git a/quantum/process_keycode/process_rgb_matrix.h b/quantum/process_keycode/process_rgb_matrix.h new file mode 100644 index 0000000000..a02bf57b5f --- /dev/null +++ b/quantum/process_keycode/process_rgb_matrix.h @@ -0,0 +1,10 @@ +// Copyright 2024 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include "action.h" + +bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record); diff --git a/quantum/process_keycode/process_underglow.c b/quantum/process_keycode/process_underglow.c new file mode 100644 index 0000000000..779672ac07 --- /dev/null +++ b/quantum/process_keycode/process_underglow.c @@ -0,0 +1,91 @@ +// Copyright 2024 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "process_underglow.h" +#include "rgblight.h" +#include "action_util.h" +#include "keycodes.h" +#include "modifiers.h" + +bool process_underglow(uint16_t keycode, keyrecord_t *record) { + if (record->event.pressed) { + uint8_t shifted = get_mods() & MOD_MASK_SHIFT; + switch (keycode) { + case QK_UNDERGLOW_TOGGLE: + rgblight_toggle(); + return false; + case QK_UNDERGLOW_MODE_NEXT: + if (shifted) { + rgblight_step_reverse(); + } else { + rgblight_step(); + } + return false; + case QK_UNDERGLOW_MODE_PREVIOUS: + if (shifted) { + rgblight_step(); + } else { + rgblight_step_reverse(); + } + return false; + case QK_UNDERGLOW_HUE_UP: + if (shifted) { + rgblight_decrease_hue(); + } else { + rgblight_increase_hue(); + } + return false; + case QK_UNDERGLOW_HUE_DOWN: + if (shifted) { + rgblight_increase_hue(); + } else { + rgblight_decrease_hue(); + } + return false; + case QK_UNDERGLOW_SATURATION_UP: + if (shifted) { + rgblight_decrease_sat(); + } else { + rgblight_increase_sat(); + } + return false; + case QK_UNDERGLOW_SATURATION_DOWN: + if (shifted) { + rgblight_increase_sat(); + } else { + rgblight_decrease_sat(); + } + return false; + case QK_UNDERGLOW_VALUE_UP: + if (shifted) { + rgblight_decrease_val(); + } else { + rgblight_increase_val(); + } + return false; + case QK_UNDERGLOW_VALUE_DOWN: + if (shifted) { + rgblight_increase_hue(); + } else { + rgblight_decrease_hue(); + } + return false; + case QK_UNDERGLOW_SPEED_UP: + if (shifted) { + rgblight_decrease_speed(); + } else { + rgblight_increase_speed(); + } + return false; + case QK_UNDERGLOW_SPEED_DOWN: + if (shifted) { + rgblight_increase_speed(); + } else { + rgblight_decrease_speed(); + } + return false; + } + } + + return true; +} diff --git a/quantum/process_keycode/process_underglow.h b/quantum/process_keycode/process_underglow.h new file mode 100644 index 0000000000..b409cafbb8 --- /dev/null +++ b/quantum/process_keycode/process_underglow.h @@ -0,0 +1,10 @@ +// Copyright 2024 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include "action.h" + +bool process_underglow(uint16_t keycode, keyrecord_t *record); -- cgit v1.2.3 From 4ae0ca5a11c475ee49bbce13d29edbf9bd6fc636 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Sat, 6 Jul 2024 09:57:54 +1000 Subject: Tap dance introspection (#24049) --- quantum/keymap_introspection.c | 23 +++++++++++++++++++++++ quantum/keymap_introspection.h | 25 +++++++++++++++++++++++-- quantum/process_keycode/process_tap_dance.c | 12 +++++++++--- quantum/process_keycode/process_tap_dance.h | 4 +--- tests/tap_dance/examples.c | 3 ++- tests/tap_dance/tap_dance_layers/test.mk | 2 +- tests/tap_dance/test.mk | 3 +-- 7 files changed, 60 insertions(+), 12 deletions(-) (limited to 'quantum/process_keycode') diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c index 71e3b429ea..4e95125335 100644 --- a/quantum/keymap_introspection.c +++ b/quantum/keymap_introspection.c @@ -109,3 +109,26 @@ __attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) { } #endif // defined(COMBO_ENABLE) + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Tap Dance + +#if defined(TAP_DANCE_ENABLE) + +uint16_t tap_dance_count_raw(void) { + return sizeof(tap_dance_actions) / sizeof(tap_dance_action_t); +} + +uint16_t tap_dance_count(void) { + return tap_dance_count_raw(); +} + +tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx) { + return &tap_dance_actions[tap_dance_idx]; +} + +tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) { + return tap_dance_get_raw(tap_dance_idx); +} + +#endif // defined(TAP_DANCE_ENABLE) diff --git a/quantum/keymap_introspection.h b/quantum/keymap_introspection.h index f7516bf42a..bc4dd93b4c 100644 --- a/quantum/keymap_introspection.h +++ b/quantum/keymap_introspection.h @@ -61,9 +61,30 @@ uint16_t combo_count_raw(void); // Get the number of combos defined in the user's keymap, potentially stored dynamically uint16_t combo_count(void); -// Get the keycode for the encoder mapping location, stored in firmware rather than any other persistent storage +// Get the combo definition, stored in firmware rather than any other persistent storage combo_t* combo_get_raw(uint16_t combo_idx); -// Get the keycode for the encoder mapping location, potentially stored dynamically +// Get the combo definition, potentially stored dynamically combo_t* combo_get(uint16_t combo_idx); #endif // defined(COMBO_ENABLE) + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Tap Dance + +#if defined(TAP_DANCE_ENABLE) + +// Forward declaration of tap_dance_action_t so we don't need to deal with header reordering +struct tap_dance_action_t; +typedef struct tap_dance_action_t tap_dance_action_t; + +// Get the number of tap dances defined in the user's keymap, stored in firmware rather than any other persistent storage +uint16_t tap_dance_count_raw(void); +// Get the number of tap dances defined in the user's keymap, potentially stored dynamically +uint16_t tap_dance_count(void); + +// Get the tap dance definitions, stored in firmware rather than any other persistent storage +tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx); +// Get the tap dance definitions, potentially stored dynamically +tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx); + +#endif // defined(TAP_DANCE_ENABLE) diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c index ce3b8fc81f..11df62763d 100644 --- a/quantum/process_keycode/process_tap_dance.c +++ b/quantum/process_keycode/process_tap_dance.c @@ -21,6 +21,7 @@ #include "action_util.h" #include "timer.h" #include "wait.h" +#include "keymap_introspection.h" static uint16_t active_td; static uint16_t last_tap_time; @@ -133,7 +134,7 @@ bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) { if (!active_td || keycode == active_td) return false; - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(active_td)]; + action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(active_td)); action->state.interrupted = true; action->state.interrupting_keycode = keycode; process_tap_dance_action_on_dance_finished(action); @@ -150,11 +151,16 @@ bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) { } bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { + int td_index; tap_dance_action_t *action; switch (keycode) { case QK_TAP_DANCE ... QK_TAP_DANCE_MAX: - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(keycode)]; + td_index = QK_TAP_DANCE_GET_INDEX(keycode); + if (td_index >= tap_dance_count()) { + return false; + } + action = tap_dance_get(td_index); action->state.pressed = record->event.pressed; if (record->event.pressed) { @@ -182,7 +188,7 @@ void tap_dance_task(void) { if (!active_td || timer_elapsed(last_tap_time) <= GET_TAPPING_TERM(active_td, &(keyrecord_t){})) return; - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(active_td)]; + action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(active_td)); if (!action->state.interrupted) { process_tap_dance_action_on_dance_finished(action); } diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index c0137c14a3..5cccbdf439 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -35,7 +35,7 @@ typedef struct { typedef void (*tap_dance_user_fn_t)(tap_dance_state_t *state, void *user_data); -typedef struct { +typedef struct tap_dance_action_t { tap_dance_state_t state; struct { tap_dance_user_fn_t on_each_tap; @@ -78,8 +78,6 @@ typedef struct { #define TD_INDEX(code) QK_TAP_DANCE_GET_INDEX(code) #define TAP_DANCE_KEYCODE(state) TD(((tap_dance_action_t *)state) - tap_dance_actions) -extern tap_dance_action_t tap_dance_actions[]; - void reset_tap_dance(tap_dance_state_t *state); /* To be used internally */ diff --git a/tests/tap_dance/examples.c b/tests/tap_dance/examples.c index 5377b397d3..4b6bdb2090 100644 --- a/tests/tap_dance/examples.c +++ b/tests/tap_dance/examples.c @@ -16,6 +16,7 @@ #include "quantum.h" #include "examples.h" +#include "keymap_introspection.h" // Example code from the tap dance documentation, adapted for testing @@ -83,7 +84,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { switch (keycode) { case TD(CT_CLN): - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(keycode)]; + action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(keycode)); if (!record->event.pressed && action->state.count && !action->state.finished) { tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)action->user_data; tap_code16(tap_hold->tap); diff --git a/tests/tap_dance/tap_dance_layers/test.mk b/tests/tap_dance/tap_dance_layers/test.mk index b4cdc9b088..a677fda648 100644 --- a/tests/tap_dance/tap_dance_layers/test.mk +++ b/tests/tap_dance/tap_dance_layers/test.mk @@ -7,4 +7,4 @@ TAP_DANCE_ENABLE = yes -SRC += tap_dance_defs.c +INTROSPECTION_KEYMAP_C = tap_dance_defs.c diff --git a/tests/tap_dance/test.mk b/tests/tap_dance/test.mk index 041d9b4dc9..0e727da9e8 100644 --- a/tests/tap_dance/test.mk +++ b/tests/tap_dance/test.mk @@ -18,5 +18,4 @@ # -------------------------------------------------------------------------------- TAP_DANCE_ENABLE = yes - -SRC += examples.c +INTROSPECTION_KEYMAP_C = examples.c -- cgit v1.2.3 From 8abaa3bc2df4fda13a96d0c5c4ce74b70c974fb7 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Tue, 16 Jul 2024 09:22:17 +1000 Subject: Add support for key override introspection. (#24120) --- docs/ChangeLog/PR24120.md | 19 ++++++++++++ docs/features/key_overrides.md | 17 +++++----- quantum/keymap_introspection.c | 43 +++++++++++++++++++++++--- quantum/keymap_introspection.h | 21 +++++++++++++ quantum/process_keycode/process_key_override.c | 10 +++--- quantum/process_keycode/process_key_override.h | 5 +-- 6 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 docs/ChangeLog/PR24120.md (limited to 'quantum/process_keycode') diff --git a/docs/ChangeLog/PR24120.md b/docs/ChangeLog/PR24120.md new file mode 100644 index 0000000000..5c635349ea --- /dev/null +++ b/docs/ChangeLog/PR24120.md @@ -0,0 +1,19 @@ +## Changes requiring user action + +### Key Override Introspection + +Changes were made to key overrides in order to hook them into the keymap introspection system. + +Key override signature changed from: + +```c +const key_override_t **key_overrides = (const key_override_t *[]){ +``` + +to: + +```c +const key_override_t *key_overrides[] = { +``` + +The list of key overrides now does not need to be `NULL`-terminated. diff --git a/docs/features/key_overrides.md b/docs/features/key_overrides.md index 4c568f1679..9b6015175c 100644 --- a/docs/features/key_overrides.md +++ b/docs/features/key_overrides.md @@ -14,7 +14,7 @@ You can use key overrides in a similar way to momentary layer/fn keys to activat To enable this feature, you need to add `KEY_OVERRIDE_ENABLE = yes` to your `rules.mk`. -Then, in your `keymap.c` file, you'll need to define the array `key_overrides`, which defines all key overrides to be used. Each override is a value of type `key_override_t`. The array `key_overrides` is `NULL`-terminated and contains pointers to `key_override_t` values (`const key_override_t **`). +Then, in your `keymap.c` file, you'll need to define the array `key_overrides`, which defines all key overrides to be used. Each override is a value of type `key_override_t`. The array `key_overrides`contains pointers to `key_override_t` values (`const key_override_t **`). ## Creating Key Overrides {#creating-key-overrides} @@ -42,9 +42,8 @@ This shows how the mentioned example of sending `delete` when `shift` + `backspa const key_override_t delete_key_override = ko_make_basic(MOD_MASK_SHIFT, KC_BSPC, KC_DEL); // This globally defines all key overrides to be used -const key_override_t **key_overrides = (const key_override_t *[]){ - &delete_key_override, - NULL // Null terminate the array of overrides! +const key_override_t *key_overrides[] = { + &delete_key_override }; ``` @@ -91,14 +90,13 @@ const key_override_t brightness_up_override = ko_make_with_layers_negmods_and_op const key_override_t brightness_down_override = ko_make_basic(MOD_MASK_CSA, KC_MPLY, KC_BRID); // This globally defines all key overrides to be used -const key_override_t **key_overrides = (const key_override_t *[]){ +const key_override_t *key_overrides[] = { &next_track_override, &prev_track_override, &vol_up_override, &vol_down_override, &brightness_up_override, - &brightness_down_override, - NULL + &brightness_down_override }; ``` @@ -112,10 +110,9 @@ const key_override_t tilde_esc_override = ko_make_basic(MOD_MASK_SHIFT, KC_ESC, // GUI + esc = ` const key_override_t grave_esc_override = ko_make_basic(MOD_MASK_GUI, KC_ESC, KC_GRV); -const key_override_t **key_overrides = (const key_override_t *[]){ +const key_override_t *key_overrides[] = { &tilde_esc_override, - &grave_esc_override, - NULL + &grave_esc_override }; ``` diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c index 4e95125335..327df6e277 100644 --- a/quantum/keymap_introspection.c +++ b/quantum/keymap_introspection.c @@ -10,6 +10,7 @@ #endif // INTROSPECTION_KEYMAP_C #include "keymap_introspection.h" +#include "util.h" //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Key mapping @@ -83,7 +84,7 @@ uint16_t keycode_at_dip_switch_map_location_raw(uint8_t switch_idx, bool on) { return KC_TRNS; } -uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) { +__attribute__((weak)) uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) { return keycode_at_dip_switch_map_location_raw(switch_idx, on); } @@ -95,13 +96,16 @@ uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) { #if defined(COMBO_ENABLE) uint16_t combo_count_raw(void) { - return sizeof(key_combos) / sizeof(combo_t); + return ARRAY_SIZE(key_combos); } __attribute__((weak)) uint16_t combo_count(void) { return combo_count_raw(); } combo_t* combo_get_raw(uint16_t combo_idx) { + if (combo_idx >= combo_count_raw()) { + return NULL; + } return &key_combos[combo_idx]; } __attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) { @@ -116,19 +120,48 @@ __attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) { #if defined(TAP_DANCE_ENABLE) uint16_t tap_dance_count_raw(void) { - return sizeof(tap_dance_actions) / sizeof(tap_dance_action_t); + return ARRAY_SIZE(tap_dance_actions); } -uint16_t tap_dance_count(void) { +__attribute__((weak)) uint16_t tap_dance_count(void) { return tap_dance_count_raw(); } tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx) { + if (tap_dance_idx >= tap_dance_count_raw()) { + return NULL; + } return &tap_dance_actions[tap_dance_idx]; } -tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) { +__attribute__((weak)) tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) { return tap_dance_get_raw(tap_dance_idx); } #endif // defined(TAP_DANCE_ENABLE) + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Key Overrides + +#if defined(KEY_OVERRIDE_ENABLE) + +uint16_t key_override_count_raw(void) { + return ARRAY_SIZE(key_overrides); +} + +__attribute__((weak)) uint16_t key_override_count(void) { + return key_override_count_raw(); +} + +const key_override_t* key_override_get_raw(uint16_t key_override_idx) { + if (key_override_idx >= key_override_count_raw()) { + return NULL; + } + return key_overrides[key_override_idx]; +} + +__attribute__((weak)) const key_override_t* key_override_get(uint16_t key_override_idx) { + return key_override_get_raw(key_override_idx); +} + +#endif // defined(KEY_OVERRIDE_ENABLE) diff --git a/quantum/keymap_introspection.h b/quantum/keymap_introspection.h index bc4dd93b4c..719825c674 100644 --- a/quantum/keymap_introspection.h +++ b/quantum/keymap_introspection.h @@ -88,3 +88,24 @@ tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx); tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx); #endif // defined(TAP_DANCE_ENABLE) + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Key Overrides + +#if defined(KEY_OVERRIDE_ENABLE) + +// Forward declaration of key_override_t so we don't need to deal with header reordering +struct key_override_t; +typedef struct key_override_t key_override_t; + +// Get the number of key overrides defined in the user's keymap, stored in firmware rather than any other persistent storage +uint16_t key_override_count_raw(void); +// Get the number of key overrides defined in the user's keymap, potentially stored dynamically +uint16_t key_override_count(void); + +// Get the key override definitions, stored in firmware rather than any other persistent storage +const key_override_t* key_override_get_raw(uint16_t key_override_idx); +// Get the key override definitions, potentially stored dynamically +const key_override_t* key_override_get(uint16_t key_override_idx); + +#endif // defined(KEY_OVERRIDE_ENABLE) diff --git a/quantum/process_keycode/process_key_override.c b/quantum/process_keycode/process_key_override.c index 264e2562b8..ce30477ee8 100644 --- a/quantum/process_keycode/process_key_override.c +++ b/quantum/process_keycode/process_key_override.c @@ -23,6 +23,7 @@ #include "action_util.h" #include "quantum.h" #include "quantum_keycodes.h" +#include "keymap_introspection.h" #ifndef KEY_OVERRIDE_REPEAT_DELAY # define KEY_OVERRIDE_REPEAT_DELAY 500 @@ -83,9 +84,6 @@ static uint16_t deferred_register = 0; // TODO: in future maybe save in EEPROM? static bool enabled = true; -// Public variables -__attribute__((weak)) const key_override_t **key_overrides = NULL; - // Forward decls static const key_override_t *clear_active_override(const bool allow_reregister); @@ -247,12 +245,12 @@ static bool check_activation_event(const key_override_t *override, const bool ke /** Iterates through the list of key overrides and tries activating each, until it finds one that activates or reaches the end of overrides. Returns true if the key action for `keycode` should be sent */ static bool try_activating_override(const uint16_t keycode, const uint8_t layer, const bool key_down, const bool is_mod, const uint8_t active_mods, bool *activated) { - if (key_overrides == NULL) { + if (key_override_count() == 0) { return true; } - for (uint8_t i = 0;; i++) { - const key_override_t *const override = key_overrides[i]; + for (uint8_t i = 0; i < key_override_count(); i++) { + const key_override_t *const override = key_override_get(i); // End of array if (override == NULL) { diff --git a/quantum/process_keycode/process_key_override.h b/quantum/process_keycode/process_key_override.h index 3e37c7e63a..9ec84dbe6e 100644 --- a/quantum/process_keycode/process_key_override.h +++ b/quantum/process_keycode/process_key_override.h @@ -55,7 +55,7 @@ typedef enum { } ko_option_t; /** Defines a single key override */ -typedef struct { +typedef struct key_override_t { // The non-modifier keycode that triggers the override. This keycode, and the necessary modifiers (trigger_mods) must be pressed to activate this override. Set this to the keycode of the key that should activate the override. Set to KC_NO to require only the necessary modifiers to be pressed and no non-modifier. uint16_t trigger; @@ -87,9 +87,6 @@ typedef struct { bool *enabled; } key_override_t; -/** Define this as a null-terminated array of pointers to key overrides. These key overrides will be used by qmk. */ -extern const key_override_t **key_overrides; - /** Turns key overrides on */ void key_override_on(void); -- cgit v1.2.3 From ab4c13e835060f6fb921a7d206e28f9525d34178 Mon Sep 17 00:00:00 2001 From: Drashna Jaelre Date: Sat, 17 Aug 2024 17:35:10 -0700 Subject: Add dynamic macro keyboard callbacks (#24142) --- keyboards/ergodox_ez/ergodox_ez.c | 12 +- keyboards/ibm/model_m/mschwingen/mschwingen.c | 12 +- keyboards/zsa/moonlander/moonlander.c | 14 +- keyboards/zsa/voyager/voyager.c | 12 +- quantum/dynamic_macro.h | 264 ------------------------ quantum/process_keycode/process_dynamic_macro.c | 52 +++-- quantum/process_keycode/process_dynamic_macro.h | 14 +- 7 files changed, 86 insertions(+), 294 deletions(-) delete mode 100644 quantum/dynamic_macro.h (limited to 'quantum/process_keycode') diff --git a/keyboards/ergodox_ez/ergodox_ez.c b/keyboards/ergodox_ez/ergodox_ez.c index 5fc173917f..e23231ecb8 100644 --- a/keyboards/ergodox_ez/ergodox_ez.c +++ b/keyboards/ergodox_ez/ergodox_ez.c @@ -382,15 +382,23 @@ static bool is_on = false; static bool is_dynamic_recording = false; static uint16_t dynamic_loop_timer; -void dynamic_macro_record_start_user(int8_t direction) { +bool dynamic_macro_record_start_kb(int8_t direction) { + if (!dynamic_macro_record_start_user(direction)) { + return false; + } is_dynamic_recording = true; dynamic_loop_timer = timer_read(); ergodox_right_led_1_on(); + return true; } -void dynamic_macro_record_end_user(int8_t direction) { +bool dynamic_macro_record_end_kb(int8_t direction) { + if (!dynamic_macro_record_end_user(direction)) { + return false; + } is_dynamic_recording = false; layer_state_set_user(layer_state); + return true; } #endif diff --git a/keyboards/ibm/model_m/mschwingen/mschwingen.c b/keyboards/ibm/model_m/mschwingen/mschwingen.c index 11407d1206..7c0f1b2565 100644 --- a/keyboards/ibm/model_m/mschwingen/mschwingen.c +++ b/keyboards/ibm/model_m/mschwingen/mschwingen.c @@ -206,11 +206,19 @@ void update_layer_leds(void) { #endif -void dynamic_macro_record_start_user(int8_t direction) { +bool dynamic_macro_record_start_kb(int8_t direction) { + if (!dynamic_macro_record_start_user(direction)) { + return false; + } isRecording++; blink_cycle_timer = timer_read(); + return true; } -void dynamic_macro_record_end_user(int8_t direction) { +bool dynamic_macro_record_end_kb(int8_t direction) { + if (!dynamic_macro_record_end_user(direction)) { + return false; + } if (isRecording) isRecording--; + return true; } diff --git a/keyboards/zsa/moonlander/moonlander.c b/keyboards/zsa/moonlander/moonlander.c index 02c64f4b96..0584e8cd31 100644 --- a/keyboards/zsa/moonlander/moonlander.c +++ b/keyboards/zsa/moonlander/moonlander.c @@ -26,11 +26,21 @@ bool is_launching = false; #ifdef DYNAMIC_MACRO_ENABLE static bool is_dynamic_recording = false; -void dynamic_macro_record_start_user(int8_t direction) { is_dynamic_recording = true; } +bool dynamic_macro_record_start_kb(int8_t direction) { + if (!dynamic_macro_record_start_user(direction)) { + return false; + } + is_dynamic_recording = true; + return true; +} -void dynamic_macro_record_end_user(int8_t direction) { +bool dynamic_macro_record_end_kb(int8_t direction) { + if (!dynamic_macro_record_end_user(direction)) { + return false; + } is_dynamic_recording = false; ML_LED_3(false); + return false; } #endif diff --git a/keyboards/zsa/voyager/voyager.c b/keyboards/zsa/voyager/voyager.c index 3255f25a97..69d42bba1a 100644 --- a/keyboards/zsa/voyager/voyager.c +++ b/keyboards/zsa/voyager/voyager.c @@ -21,18 +21,26 @@ static uint32_t dynamic_macro_led(uint32_t trigger_time, void *cb_arg) { return 100; } -void dynamic_macro_record_start_user(int8_t direction) { +bool dynamic_macro_record_start_kb(int8_t direction) { + if (!dynamic_macro_record_start_user(direction)) { + return false; + } if (dynamic_macro_token == INVALID_DEFERRED_TOKEN) { STATUS_LED_3(true); dynamic_macro_token = defer_exec(100, dynamic_macro_led, NULL); } + return true; } -void dynamic_macro_record_end_user(int8_t direction) { +bool dynamic_macro_record_end_kb(int8_t direction) { + if (!dynamic_macro_record_end_user(direction)) { + return false; + } if (cancel_deferred_exec(dynamic_macro_token)) { dynamic_macro_token = INVALID_DEFERRED_TOKEN; STATUS_LED_3(false); } + return true; } # endif diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h deleted file mode 100644 index 64c532e6ce..0000000000 --- a/quantum/dynamic_macro.h +++ /dev/null @@ -1,264 +0,0 @@ -/* Copyright 2016 Jack Humbert - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */ -#pragma once - -/* Warn users that this is now deprecated and they should use the core feature instead. */ -#pragma message "Dynamic Macros is now a core feature. See updated documentation to see how to configure it: https://docs.qmk.fm/#/feature_dynamic_macros" - -#include "action_layer.h" - -#ifndef DYNAMIC_MACRO_SIZE -/* May be overridden with a custom value. Be aware that the effective - * macro length is half of this value: each keypress is recorded twice - * because of the down-event and up-event. This is not a bug, it's the - * intended behavior. - * - * Usually it should be fine to set the macro size to at least 256 but - * there have been reports of it being too much in some users' cases, - * so 128 is considered a safe default. - */ -# define DYNAMIC_MACRO_SIZE 128 -#endif - -/* Blink the LEDs to notify the user about some event. */ -void dynamic_macro_led_blink(void) { -#ifdef BACKLIGHT_ENABLE - backlight_toggle(); - wait_ms(100); - backlight_toggle(); -#endif -} - -/* Convenience macros used for retrieving the debug info. All of them - * need a `direction` variable accessible at the call site. - */ -#define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2) -#define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN)))) -#define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1)) - -/** - * Start recording of the dynamic macro. - * - * @param[out] macro_pointer The new macro buffer iterator. - * @param[in] macro_buffer The macro buffer used to initialize macro_pointer. - */ -void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_buffer) { - dprintln("dynamic macro recording: started"); - - dynamic_macro_led_blink(); - - clear_keyboard(); - layer_clear(); - *macro_pointer = macro_buffer; -} - -/** - * Play the dynamic macro. - * - * @param macro_buffer[in] The beginning of the macro buffer being played. - * @param macro_end[in] The element after the last macro buffer element. - * @param direction[in] Either +1 or -1, which way to iterate the buffer. - */ -void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction) { - dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT()); - - uint32_t saved_layer_state = layer_state; - - clear_keyboard(); - layer_clear(); - - while (macro_buffer != macro_end) { - process_record(macro_buffer); - macro_buffer += direction; - } - - clear_keyboard(); - - layer_state = saved_layer_state; -} - -/** - * Record a single key in a dynamic macro. - * - * @param macro_buffer[in] The start of the used macro buffer. - * @param macro_pointer[in,out] The current buffer position. - * @param macro2_end[in] The end of the other macro. - * @param direction[in] Either +1 or -1, which way to iterate the buffer. - * @param record[in] The current keypress. - */ -void dynamic_macro_record_key(keyrecord_t *macro_buffer, keyrecord_t **macro_pointer, keyrecord_t *macro2_end, int8_t direction, keyrecord_t *record) { - /* If we've just started recording, ignore all the key releases. */ - if (!record->event.pressed && *macro_pointer == macro_buffer) { - dprintln("dynamic macro: ignoring a leading key-up event"); - return; - } - - /* The other end of the other macro is the last buffer element it - * is safe to use before overwriting the other macro. - */ - if (*macro_pointer - direction != macro2_end) { - **macro_pointer = *record; - *macro_pointer += direction; - } else { - dynamic_macro_led_blink(); - } - - dprintf("dynamic macro: slot %d length: %d/%d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer), DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end)); -} - -/** - * End recording of the dynamic macro. Essentially just update the - * pointer to the end of the macro. - */ -void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_pointer, int8_t direction, keyrecord_t **macro_end) { - dynamic_macro_led_blink(); - - /* Do not save the keys being held when stopping the recording, - * i.e. the keys used to access the layer DM_RSTP is on. - */ - while (macro_pointer != macro_buffer && (macro_pointer - direction)->event.pressed) { - dprintln("dynamic macro: trimming a trailing key-down event"); - macro_pointer -= direction; - } - - dprintf("dynamic macro: slot %d saved, length: %d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer)); - - *macro_end = macro_pointer; -} - -/* Handle the key events related to the dynamic macros. Should be - * called from process_record_user() like this: - * - * bool process_record_user(uint16_t keycode, keyrecord_t *record) { - * if (!process_record_dynamic_macro(keycode, record)) { - * return false; - * } - * <...THE REST OF THE FUNCTION...> - * } - */ -bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record) { - /* Both macros use the same buffer but read/write on different - * ends of it. - * - * Macro1 is written left-to-right starting from the beginning of - * the buffer. - * - * Macro2 is written right-to-left starting from the end of the - * buffer. - * - * ¯o_buffer macro_end - * v v - * +------------------------------------------------------------+ - * |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<| - * +------------------------------------------------------------+ - * ^ ^ - * r_macro_end r_macro_buffer - * - * During the recording when one macro encounters the end of the - * other macro, the recording is stopped. Apart from this, there - * are no arbitrary limits for the macros' length in relation to - * each other: for example one can either have two medium sized - * macros or one long macro and one short macro. Or even one empty - * and one using the whole buffer. - */ - static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE]; - - /* Pointer to the first buffer element after the first macro. - * Initially points to the very beginning of the buffer since the - * macro is empty. */ - static keyrecord_t *macro_end = macro_buffer; - - /* The other end of the macro buffer. Serves as the beginning of - * the second macro. */ - static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1; - - /* Like macro_end but for the second macro. */ - static keyrecord_t *r_macro_end = r_macro_buffer; - - /* A persistent pointer to the current macro position (iterator) - * used during the recording. */ - static keyrecord_t *macro_pointer = NULL; - - /* 0 - no macro is being recorded right now - * 1,2 - either macro 1 or 2 is being recorded */ - static uint8_t macro_id = 0; - - if (macro_id == 0) { - /* No macro recording in progress. */ - if (!record->event.pressed) { - switch (keycode) { - case QK_DYNAMIC_MACRO_RECORD_START_1: - dynamic_macro_record_start(¯o_pointer, macro_buffer); - macro_id = 1; - return false; - case QK_DYNAMIC_MACRO_RECORD_START_2: - dynamic_macro_record_start(¯o_pointer, r_macro_buffer); - macro_id = 2; - return false; - case QK_DYNAMIC_MACRO_PLAY_1: - dynamic_macro_play(macro_buffer, macro_end, +1); - return false; - case QK_DYNAMIC_MACRO_PLAY_2: - dynamic_macro_play(r_macro_buffer, r_macro_end, -1); - return false; - } - } - } else { - /* A macro is being recorded right now. */ - switch (keycode) { - case QK_DYNAMIC_MACRO_RECORD_STOP: - /* Stop the macro recording. */ - if (record->event.pressed) { /* Ignore the initial release - * just after the recoding - * starts. */ - switch (macro_id) { - case 1: - dynamic_macro_record_end(macro_buffer, macro_pointer, +1, ¯o_end); - break; - case 2: - dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end); - break; - } - macro_id = 0; - } - return false; - case QK_DYNAMIC_MACRO_PLAY_1: - case QK_DYNAMIC_MACRO_PLAY_2: - dprintln("dynamic macro: ignoring macro play key while recording"); - return false; - default: - /* Store the key in the macro buffer and process it normally. */ - switch (macro_id) { - case 1: - dynamic_macro_record_key(macro_buffer, ¯o_pointer, r_macro_end, +1, record); - break; - case 2: - dynamic_macro_record_key(r_macro_buffer, ¯o_pointer, macro_end, -1, record); - break; - } - return true; - break; - } - } - - return true; -} - -#undef DYNAMIC_MACRO_CURRENT_SLOT -#undef DYNAMIC_MACRO_CURRENT_LENGTH -#undef DYNAMIC_MACRO_CURRENT_CAPACITY diff --git a/quantum/process_keycode/process_dynamic_macro.c b/quantum/process_keycode/process_dynamic_macro.c index 214cd80a87..f8e8256ac8 100644 --- a/quantum/process_keycode/process_dynamic_macro.c +++ b/quantum/process_keycode/process_dynamic_macro.c @@ -38,20 +38,44 @@ void dynamic_macro_led_blink(void) { /* User hooks for Dynamic Macros */ -__attribute__((weak)) void dynamic_macro_record_start_user(int8_t direction) { +__attribute__((weak)) bool dynamic_macro_record_start_kb(int8_t direction) { + return dynamic_macro_record_start_user(direction); +} + +__attribute__((weak)) bool dynamic_macro_record_start_user(int8_t direction) { dynamic_macro_led_blink(); + return true; +} + +__attribute__((weak)) bool dynamic_macro_play_kb(int8_t direction) { + return dynamic_macro_play_user(direction); } -__attribute__((weak)) void dynamic_macro_play_user(int8_t direction) { +__attribute__((weak)) bool dynamic_macro_play_user(int8_t direction) { dynamic_macro_led_blink(); + return true; +} + +__attribute__((weak)) bool dynamic_macro_record_key_kb(int8_t direction, keyrecord_t *record) { + return dynamic_macro_record_key_user(direction, record); } -__attribute__((weak)) void dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record) { +__attribute__((weak)) bool dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record) { dynamic_macro_led_blink(); + return true; +} + +__attribute__((weak)) bool dynamic_macro_record_end_kb(int8_t direction) { + return dynamic_macro_record_end_user(direction); } -__attribute__((weak)) void dynamic_macro_record_end_user(int8_t direction) { +__attribute__((weak)) bool dynamic_macro_record_end_user(int8_t direction) { dynamic_macro_led_blink(); + return true; +} + +__attribute__((weak)) bool dynamic_macro_valid_key_kb(uint16_t keycode, keyrecord_t *record) { + return dynamic_macro_valid_key_user(keycode, record); } __attribute__((weak)) bool dynamic_macro_valid_key_user(uint16_t keycode, keyrecord_t *record) { @@ -74,7 +98,7 @@ __attribute__((weak)) bool dynamic_macro_valid_key_user(uint16_t keycode, keyrec void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_buffer, int8_t direction) { dprintln("dynamic macro recording: started"); - dynamic_macro_record_start_user(direction); + dynamic_macro_record_start_kb(direction); clear_keyboard(); layer_clear(); @@ -108,7 +132,7 @@ void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_ layer_state_set(saved_layer_state); - dynamic_macro_play_user(direction); + dynamic_macro_play_kb(direction); } /** @@ -134,7 +158,7 @@ void dynamic_macro_record_key(keyrecord_t *macro_buffer, keyrecord_t **macro_poi **macro_pointer = *record; *macro_pointer += direction; } - dynamic_macro_record_key_user(direction, record); + dynamic_macro_record_key_kb(direction, record); dprintf("dynamic macro: slot %d length: %d/%d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer), DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end)); } @@ -144,7 +168,7 @@ void dynamic_macro_record_key(keyrecord_t *macro_buffer, keyrecord_t **macro_poi * pointer to the end of the macro. */ void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_pointer, int8_t direction, keyrecord_t **macro_end) { - dynamic_macro_record_end_user(direction); + dynamic_macro_record_end_kb(direction); /* Do not save the keys being held when stopping the recording, * i.e. the keys used to access the layer DM_RSTP is on. @@ -220,15 +244,7 @@ void dynamic_macro_stop_recording(void) { macro_id = 0; } -/* Handle the key events related to the dynamic macros. Should be - * called from process_record_user() like this: - * - * bool process_record_user(uint16_t keycode, keyrecord_t *record) { - * if (!process_record_dynamic_macro(keycode, record)) { - * return false; - * } - * <...THE REST OF THE FUNCTION...> - * } +/* Handle the key events related to the dynamic macros. */ bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) { if (macro_id == 0) { @@ -271,7 +287,7 @@ bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) { return false; #endif default: - if (dynamic_macro_valid_key_user(keycode, record)) { + if (dynamic_macro_valid_key_kb(keycode, record)) { /* Store the key in the macro buffer and process it normally. */ switch (macro_id) { case 1: diff --git a/quantum/process_keycode/process_dynamic_macro.h b/quantum/process_keycode/process_dynamic_macro.h index 2f10733cae..984fc0cd41 100644 --- a/quantum/process_keycode/process_dynamic_macro.h +++ b/quantum/process_keycode/process_dynamic_macro.h @@ -37,8 +37,14 @@ void dynamic_macro_led_blink(void); bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record); -void dynamic_macro_record_start_user(int8_t direction); -void dynamic_macro_play_user(int8_t direction); -void dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record); -void dynamic_macro_record_end_user(int8_t direction); +bool dynamic_macro_record_start_kb(int8_t direction); +bool dynamic_macro_record_start_user(int8_t direction); +bool dynamic_macro_play_kb(int8_t direction); +bool dynamic_macro_play_user(int8_t direction); +bool dynamic_macro_record_key_kb(int8_t direction, keyrecord_t *record); +bool dynamic_macro_record_key_user(int8_t direction, keyrecord_t *record); +bool dynamic_macro_record_end_kb(int8_t direction); +bool dynamic_macro_record_end_user(int8_t direction); +bool dynamic_macro_valid_key_kb(uint16_t keycode, keyrecord_t *record); +bool dynamic_macro_valid_key_user(uint16_t keycode, keyrecord_t *record); void dynamic_macro_stop_recording(void); -- cgit v1.2.3