summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChaser Huang <huangkangjing@gmail.com>2025-11-11 07:30:42 -0500
committerGitHub <noreply@github.com>2025-11-11 23:30:42 +1100
commit1ddcf57382f94548aba23871c57c7ce835f203b9 (patch)
tree0e4917817d9a1e9e1d8423c53903347e3e39840e
parente06d79e9c66016402329b26f2f12670175d50c66 (diff)
[Feature Improvement]add option to keep layer state when recording dynamic macros (#24418)
* feat: add option to keep layer state when recording dynamic macros * Better option macro name and lint changes
-rw-r--r--docs/features/dynamic_macros.md13
-rw-r--r--quantum/process_keycode/process_dynamic_macro.c23
2 files changed, 29 insertions, 7 deletions
diff --git a/docs/features/dynamic_macros.md b/docs/features/dynamic_macros.md
index a642ced43e..fa7373a4bd 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
32 32
33There are a number of options added that should allow some additional degree of customization 33There are a number of options added that should allow some additional degree of customization
34 34
35|Define |Default |Description | 35|Define |Default |Description |
36|----------------------------|----------------|-----------------------------------------------------------------------------------------------------------------| 36|------------------------------------------|----------------|-----------------------------------------------------------------------------------------------------------------|
37|`DYNAMIC_MACRO_SIZE` |128 |Sets the amount of memory that Dynamic Macros can use. This is a limited resource, dependent on the controller. | 37|`DYNAMIC_MACRO_SIZE` |128 |Sets the amount of memory that Dynamic Macros can use. This is a limited resource, dependent on the controller. |
38|`DYNAMIC_MACRO_USER_CALL` |*Not defined* |Defining this falls back to using the user `keymap.c` file to trigger the macro behavior. | 38|`DYNAMIC_MACRO_USER_CALL` |*Not defined* |Defining this falls back to using the user `keymap.c` file to trigger the macro behavior. |
39|`DYNAMIC_MACRO_NO_NESTING` |*Not Defined* |Defining this disables the ability to call a macro from another macro (nested macros). | 39|`DYNAMIC_MACRO_NO_NESTING` |*Not Defined* |Defining this disables the ability to call a macro from another macro (nested macros). |
40|`DYNAMIC_MACRO_DELAY` |*Not Defined* |Sets the waiting time (ms unit) when sending each key. | 40|`DYNAMIC_MACRO_DELAY` |*Not Defined* |Sets the waiting time (ms unit) when sending each key. |
41|`DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE` |*Not Defined* |Defining this keeps the layer state when starting to record a macro |
41 42
42 43
43If 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). 44If 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 f8e8256ac8..20e90c6e14 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
89#define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN)))) 89#define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN))))
90#define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1)) 90#define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1))
91 91
92#ifdef DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE
93static layer_state_t dm1_layer_state;
94static layer_state_t dm2_layer_state;
95#endif
96
92/** 97/**
93 * Start recording of the dynamic macro. 98 * Start recording of the dynamic macro.
94 * 99 *
@@ -100,8 +105,16 @@ void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_
100 105
101 dynamic_macro_record_start_kb(direction); 106 dynamic_macro_record_start_kb(direction);
102 107
103 clear_keyboard(); 108#ifdef DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE
109 if (direction == 1) {
110 dm1_layer_state = layer_state;
111 } else if (direction == -1) {
112 dm2_layer_state = layer_state;
113 }
114#else
104 layer_clear(); 115 layer_clear();
116#endif
117 clear_keyboard();
105 *macro_pointer = macro_buffer; 118 *macro_pointer = macro_buffer;
106} 119}
107 120
@@ -118,7 +131,15 @@ void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_
118 layer_state_t saved_layer_state = layer_state; 131 layer_state_t saved_layer_state = layer_state;
119 132
120 clear_keyboard(); 133 clear_keyboard();
134#ifdef DYNAMIC_MACRO_KEEP_ORIGINAL_LAYER_STATE
135 if (direction == 1) {
136 layer_state_set(dm1_layer_state);
137 } else if (direction == -1) {
138 layer_state_set(dm2_layer_state);
139 }
140#else
121 layer_clear(); 141 layer_clear();
142#endif
122 143
123 while (macro_buffer != macro_end) { 144 while (macro_buffer != macro_end) {
124 process_record(macro_buffer); 145 process_record(macro_buffer);