From 3f42efb60dc229ab5cef4c833feb845253007875 Mon Sep 17 00:00:00 2001 From: Chaser Huang Date: Fri, 20 Sep 2024 17:29:07 -0400 Subject: [PATCH 1/2] feat: add option to keep layer state when recording dynamic macros --- docs/features/dynamic_macros.md | 13 ++++++----- .../process_keycode/process_dynamic_macro.c | 23 ++++++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/docs/features/dynamic_macros.md b/docs/features/dynamic_macros.md index a642ced43e3..467d4deb805 100644 --- a/docs/features/dynamic_macros.md +++ b/docs/features/dynamic_macros.md @@ -32,12 +32,13 @@ For the details about the internals of the dynamic macros, please read the comme There are a number of options added that should allow some additional degree of customization -|Define |Default |Description | -|----------------------------|----------------|-----------------------------------------------------------------------------------------------------------------| -|`DYNAMIC_MACRO_SIZE` |128 |Sets the amount of memory that Dynamic Macros can use. This is a limited resource, dependent on the controller. | -|`DYNAMIC_MACRO_USER_CALL` |*Not defined* |Defining this falls back to using the user `keymap.c` file to trigger the macro behavior. | -|`DYNAMIC_MACRO_NO_NESTING` |*Not Defined* |Defining this disables the ability to call a macro from another macro (nested macros). | -|`DYNAMIC_MACRO_DELAY` |*Not Defined* |Sets the waiting time (ms unit) when sending each key. | +|Define |Default |Description | +|---------------------------------|----------------|-----------------------------------------------------------------------------------------------------------------| +|`DYNAMIC_MACRO_SIZE` |128 |Sets the amount of memory that Dynamic Macros can use. This is a limited resource, dependent on the controller. | +|`DYNAMIC_MACRO_USER_CALL` |*Not defined* |Defining this falls back to using the user `keymap.c` file to trigger the macro behavior. | +|`DYNAMIC_MACRO_NO_NESTING` |*Not Defined* |Defining this disables the ability to call a macro from another macro (nested macros). | +|`DYNAMIC_MACRO_DELAY` |*Not Defined* |Sets the waiting time (ms unit) when sending each key. | +|`DYNAMIC_MACRO_KEEP_LAYER_STATE` |*Not Defined* |Defining this keeps the layer state when starting to record a macro | If the LEDs start blinking during the recording with each keypress, it means there is no more space for the macro in the macro buffer. To fit the macro in, either make the other macro shorter (they share the same buffer) or increase the buffer size by adding the `DYNAMIC_MACRO_SIZE` define in your `config.h` (default value: 128; please read the comments for it in the header). diff --git a/quantum/process_keycode/process_dynamic_macro.c b/quantum/process_keycode/process_dynamic_macro.c index f8e8256ac8d..d584d20de8c 100644 --- a/quantum/process_keycode/process_dynamic_macro.c +++ b/quantum/process_keycode/process_dynamic_macro.c @@ -89,6 +89,11 @@ __attribute__((weak)) bool dynamic_macro_valid_key_user(uint16_t keycode, keyrec #define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN)))) #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1)) +#ifdef DYNAMIC_MACRO_KEEP_LAYER_STATE + static layer_state_t dm1_layer_state; + static layer_state_t dm2_layer_state; +#endif + /** * Start recording of the dynamic macro. * @@ -100,8 +105,16 @@ void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_ dynamic_macro_record_start_kb(direction); - clear_keyboard(); +#ifdef DYNAMIC_MACRO_KEEP_LAYER_STATE + if (direction == 1) { + dm1_layer_state = layer_state; + } else if (direction == -1) { + dm2_layer_state = layer_state; + } +#else layer_clear(); +#endif + clear_keyboard(); *macro_pointer = macro_buffer; } @@ -118,7 +131,15 @@ void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_ layer_state_t saved_layer_state = layer_state; clear_keyboard(); +#ifdef DYNAMIC_MACRO_KEEP_LAYER_STATE + if (direction == 1) { + layer_state_set(dm1_layer_state); + } else if (direction == -1) { + layer_state_set(dm2_layer_state); + } +#else layer_clear(); +#endif while (macro_buffer != macro_end) { process_record(macro_buffer); From b0fbc8a5a3fb75152a4096018357bb9d2e88688c Mon Sep 17 00:00:00 2001 From: Chaser Huang Date: Sat, 21 Sep 2024 16:42:22 -0400 Subject: [PATCH 2/2] Better option macro name and lint changes --- docs/features/dynamic_macros.md | 14 +++++++------- quantum/process_keycode/process_dynamic_macro.c | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/features/dynamic_macros.md b/docs/features/dynamic_macros.md index 467d4deb805..fa7373a4bd8 100644 --- a/docs/features/dynamic_macros.md +++ b/docs/features/dynamic_macros.md @@ -32,13 +32,13 @@ For the details about the internals of the dynamic macros, please read the comme There are a number of options added that should allow some additional degree of customization -|Define |Default |Description | -|---------------------------------|----------------|-----------------------------------------------------------------------------------------------------------------| -|`DYNAMIC_MACRO_SIZE` |128 |Sets the amount of memory that Dynamic Macros can use. This is a limited resource, dependent on the controller. | -|`DYNAMIC_MACRO_USER_CALL` |*Not defined* |Defining this falls back to using the user `keymap.c` file to trigger the macro behavior. | -|`DYNAMIC_MACRO_NO_NESTING` |*Not Defined* |Defining this disables the ability to call a macro from another macro (nested macros). | -|`DYNAMIC_MACRO_DELAY` |*Not Defined* |Sets the waiting time (ms unit) when sending each key. | -|`DYNAMIC_MACRO_KEEP_LAYER_STATE` |*Not Defined* |Defining this keeps the layer state when starting to record a macro | +|Define |Default |Description | +|------------------------------------------|----------------|-----------------------------------------------------------------------------------------------------------------| +|`DYNAMIC_MACRO_SIZE` |128 |Sets the amount of memory that Dynamic Macros can use. This is a limited resource, dependent on the controller. | +|`DYNAMIC_MACRO_USER_CALL` |*Not defined* |Defining this falls back to using the user `keymap.c` file to trigger the macro behavior. | +|`DYNAMIC_MACRO_NO_NESTING` |*Not Defined* |Defining this disables the ability to call a macro from another macro (nested macros). | +|`DYNAMIC_MACRO_DELAY` |*Not Defined* |Sets the waiting time (ms unit) when sending each key. | +|`DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE` |*Not Defined* |Defining this keeps the layer state when starting to record a macro | If the LEDs start blinking during the recording with each keypress, it means there is no more space for the macro in the macro buffer. To fit the macro in, either make the other macro shorter (they share the same buffer) or increase the buffer size by adding the `DYNAMIC_MACRO_SIZE` define in your `config.h` (default value: 128; please read the comments for it in the header). diff --git a/quantum/process_keycode/process_dynamic_macro.c b/quantum/process_keycode/process_dynamic_macro.c index d584d20de8c..20e90c6e14d 100644 --- a/quantum/process_keycode/process_dynamic_macro.c +++ b/quantum/process_keycode/process_dynamic_macro.c @@ -89,9 +89,9 @@ __attribute__((weak)) bool dynamic_macro_valid_key_user(uint16_t keycode, keyrec #define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN)))) #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1)) -#ifdef DYNAMIC_MACRO_KEEP_LAYER_STATE - static layer_state_t dm1_layer_state; - static layer_state_t dm2_layer_state; +#ifdef DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE +static layer_state_t dm1_layer_state; +static layer_state_t dm2_layer_state; #endif /** @@ -105,7 +105,7 @@ void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_ dynamic_macro_record_start_kb(direction); -#ifdef DYNAMIC_MACRO_KEEP_LAYER_STATE +#ifdef DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE if (direction == 1) { dm1_layer_state = layer_state; } else if (direction == -1) { @@ -131,7 +131,7 @@ void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_ layer_state_t saved_layer_state = layer_state; clear_keyboard(); -#ifdef DYNAMIC_MACRO_KEEP_LAYER_STATE +#ifdef DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE if (direction == 1) { layer_state_set(dm1_layer_state); } else if (direction == -1) {