Commit | Line | Data |
---|---|---|
23839b8c | 1 | /* Copyright 2016 Jack Humbert |
2 | * | |
3 | * This program is free software: you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation, either version 2 of the License, or | |
6 | * (at your option) any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | */ | |
16 | ||
39e8e612 | 17 | /* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */ |
542cb0a8 DJ |
18 | #pragma once |
19 | ||
20 | /* Warn users that this is now deprecated and they should use the core feature instead. */ | |
21 | #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" | |
39e8e612 WS |
22 | |
23 | #include "action_layer.h" | |
24 | ||
25 | #ifndef DYNAMIC_MACRO_SIZE | |
26 | /* May be overridden with a custom value. Be aware that the effective | |
27 | * macro length is half of this value: each keypress is recorded twice | |
28 | * because of the down-event and up-event. This is not a bug, it's the | |
70f32842 WS |
29 | * intended behavior. |
30 | * | |
31 | * Usually it should be fine to set the macro size to at least 256 but | |
32 | * there have been reports of it being too much in some users' cases, | |
33 | * so 128 is considered a safe default. | |
34 | */ | |
b624f32f | 35 | # define DYNAMIC_MACRO_SIZE 128 |
39e8e612 WS |
36 | #endif |
37 | ||
39e8e612 | 38 | /* Blink the LEDs to notify the user about some event. */ |
b624f32f | 39 | void dynamic_macro_led_blink(void) { |
a1e156a3 | 40 | #ifdef BACKLIGHT_ENABLE |
39e8e612 | 41 | backlight_toggle(); |
67eeb889 | 42 | wait_ms(100); |
39e8e612 | 43 | backlight_toggle(); |
a1e156a3 | 44 | #endif |
39e8e612 WS |
45 | } |
46 | ||
10a7cd7e WS |
47 | /* Convenience macros used for retrieving the debug info. All of them |
48 | * need a `direction` variable accessible at the call site. | |
49 | */ | |
50 | #define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2) | |
b624f32f | 51 | #define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN)))) |
52 | #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1)) | |
10a7cd7e | 53 | |
39e8e612 WS |
54 | /** |
55 | * Start recording of the dynamic macro. | |
56 | * | |
57 | * @param[out] macro_pointer The new macro buffer iterator. | |
58 | * @param[in] macro_buffer The macro buffer used to initialize macro_pointer. | |
59 | */ | |
b624f32f | 60 | void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_buffer) { |
10a7cd7e WS |
61 | dprintln("dynamic macro recording: started"); |
62 | ||
39e8e612 WS |
63 | dynamic_macro_led_blink(); |
64 | ||
65 | clear_keyboard(); | |
66 | layer_clear(); | |
67 | *macro_pointer = macro_buffer; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Play the dynamic macro. | |
72 | * | |
73 | * @param macro_buffer[in] The beginning of the macro buffer being played. | |
74 | * @param macro_end[in] The element after the last macro buffer element. | |
75 | * @param direction[in] Either +1 or -1, which way to iterate the buffer. | |
76 | */ | |
b624f32f | 77 | void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction) { |
10a7cd7e WS |
78 | dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT()); |
79 | ||
39e8e612 WS |
80 | uint32_t saved_layer_state = layer_state; |
81 | ||
82 | clear_keyboard(); | |
83 | layer_clear(); | |
84 | ||
85 | while (macro_buffer != macro_end) { | |
86 | process_record(macro_buffer); | |
87 | macro_buffer += direction; | |
88 | } | |
89 | ||
90 | clear_keyboard(); | |
91 | ||
92 | layer_state = saved_layer_state; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Record a single key in a dynamic macro. | |
97 | * | |
40fe30e4 | 98 | * @param macro_buffer[in] The start of the used macro buffer. |
39e8e612 | 99 | * @param macro_pointer[in,out] The current buffer position. |
8e94c9b4 | 100 | * @param macro2_end[in] The end of the other macro. |
39e8e612 WS |
101 | * @param direction[in] Either +1 or -1, which way to iterate the buffer. |
102 | * @param record[in] The current keypress. | |
103 | */ | |
b624f32f | 104 | void dynamic_macro_record_key(keyrecord_t *macro_buffer, keyrecord_t **macro_pointer, keyrecord_t *macro2_end, int8_t direction, keyrecord_t *record) { |
40fe30e4 WS |
105 | /* If we've just started recording, ignore all the key releases. */ |
106 | if (!record->event.pressed && *macro_pointer == macro_buffer) { | |
10a7cd7e | 107 | dprintln("dynamic macro: ignoring a leading key-up event"); |
40fe30e4 WS |
108 | return; |
109 | } | |
110 | ||
8e94c9b4 WS |
111 | /* The other end of the other macro is the last buffer element it |
112 | * is safe to use before overwriting the other macro. | |
113 | */ | |
436d6617 | 114 | if (*macro_pointer - direction != macro2_end) { |
39e8e612 WS |
115 | **macro_pointer = *record; |
116 | *macro_pointer += direction; | |
117 | } else { | |
5e2a9992 | 118 | dynamic_macro_led_blink(); |
39e8e612 | 119 | } |
10a7cd7e | 120 | |
b624f32f | 121 | 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)); |
39e8e612 WS |
122 | } |
123 | ||
124 | /** | |
125 | * End recording of the dynamic macro. Essentially just update the | |
126 | * pointer to the end of the macro. | |
127 | */ | |
b624f32f | 128 | void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_pointer, int8_t direction, keyrecord_t **macro_end) { |
39e8e612 WS |
129 | dynamic_macro_led_blink(); |
130 | ||
4b50ea15 WS |
131 | /* Do not save the keys being held when stopping the recording, |
132 | * i.e. the keys used to access the layer DYN_REC_STOP is on. | |
133 | */ | |
b624f32f | 134 | while (macro_pointer != macro_buffer && (macro_pointer - direction)->event.pressed) { |
10a7cd7e | 135 | dprintln("dynamic macro: trimming a trailing key-down event"); |
4b50ea15 WS |
136 | macro_pointer -= direction; |
137 | } | |
138 | ||
b624f32f | 139 | dprintf("dynamic macro: slot %d saved, length: %d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer)); |
10a7cd7e | 140 | |
39e8e612 WS |
141 | *macro_end = macro_pointer; |
142 | } | |
143 | ||
144 | /* Handle the key events related to the dynamic macros. Should be | |
145 | * called from process_record_user() like this: | |
146 | * | |
147 | * bool process_record_user(uint16_t keycode, keyrecord_t *record) { | |
148 | * if (!process_record_dynamic_macro(keycode, record)) { | |
149 | * return false; | |
150 | * } | |
151 | * <...THE REST OF THE FUNCTION...> | |
152 | * } | |
153 | */ | |
b624f32f | 154 | bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record) { |
39e8e612 WS |
155 | /* Both macros use the same buffer but read/write on different |
156 | * ends of it. | |
157 | * | |
158 | * Macro1 is written left-to-right starting from the beginning of | |
159 | * the buffer. | |
160 | * | |
161 | * Macro2 is written right-to-left starting from the end of the | |
162 | * buffer. | |
163 | * | |
164 | * ¯o_buffer macro_end | |
165 | * v v | |
166 | * +------------------------------------------------------------+ | |
8e94c9b4 | 167 | * |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<| |
39e8e612 WS |
168 | * +------------------------------------------------------------+ |
169 | * ^ ^ | |
170 | * r_macro_end r_macro_buffer | |
171 | * | |
172 | * During the recording when one macro encounters the end of the | |
173 | * other macro, the recording is stopped. Apart from this, there | |
174 | * are no arbitrary limits for the macros' length in relation to | |
175 | * each other: for example one can either have two medium sized | |
176 | * macros or one long macro and one short macro. Or even one empty | |
177 | * and one using the whole buffer. | |
178 | */ | |
179 | static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE]; | |
180 | ||
181 | /* Pointer to the first buffer element after the first macro. | |
182 | * Initially points to the very beginning of the buffer since the | |
183 | * macro is empty. */ | |
184 | static keyrecord_t *macro_end = macro_buffer; | |
185 | ||
186 | /* The other end of the macro buffer. Serves as the beginning of | |
187 | * the second macro. */ | |
188 | static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1; | |
189 | ||
190 | /* Like macro_end but for the second macro. */ | |
191 | static keyrecord_t *r_macro_end = r_macro_buffer; | |
192 | ||
193 | /* A persistent pointer to the current macro position (iterator) | |
194 | * used during the recording. */ | |
195 | static keyrecord_t *macro_pointer = NULL; | |
196 | ||
197 | /* 0 - no macro is being recorded right now | |
198 | * 1,2 - either macro 1 or 2 is being recorded */ | |
199 | static uint8_t macro_id = 0; | |
200 | ||
201 | if (macro_id == 0) { | |
202 | /* No macro recording in progress. */ | |
203 | if (!record->event.pressed) { | |
204 | switch (keycode) { | |
b624f32f | 205 | case DYN_REC_START1: |
206 | dynamic_macro_record_start(¯o_pointer, macro_buffer); | |
207 | macro_id = 1; | |
208 | return false; | |
209 | case DYN_REC_START2: | |
210 | dynamic_macro_record_start(¯o_pointer, r_macro_buffer); | |
211 | macro_id = 2; | |
212 | return false; | |
213 | case DYN_MACRO_PLAY1: | |
214 | dynamic_macro_play(macro_buffer, macro_end, +1); | |
215 | return false; | |
216 | case DYN_MACRO_PLAY2: | |
217 | dynamic_macro_play(r_macro_buffer, r_macro_end, -1); | |
218 | return false; | |
39e8e612 WS |
219 | } |
220 | } | |
221 | } else { | |
222 | /* A macro is being recorded right now. */ | |
223 | switch (keycode) { | |
b624f32f | 224 | case DYN_REC_STOP: |
225 | /* Stop the macro recording. */ | |
226 | if (record->event.pressed) { /* Ignore the initial release | |
227 | * just after the recoding | |
228 | * starts. */ | |
229 | switch (macro_id) { | |
230 | case 1: | |
231 | dynamic_macro_record_end(macro_buffer, macro_pointer, +1, ¯o_end); | |
232 | break; | |
233 | case 2: | |
234 | dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end); | |
235 | break; | |
236 | } | |
237 | macro_id = 0; | |
238 | } | |
239 | return false; | |
240 | case DYN_MACRO_PLAY1: | |
241 | case DYN_MACRO_PLAY2: | |
242 | dprintln("dynamic macro: ignoring macro play key while recording"); | |
243 | return false; | |
244 | default: | |
245 | /* Store the key in the macro buffer and process it normally. */ | |
39e8e612 | 246 | switch (macro_id) { |
b624f32f | 247 | case 1: |
248 | dynamic_macro_record_key(macro_buffer, ¯o_pointer, r_macro_end, +1, record); | |
249 | break; | |
250 | case 2: | |
251 | dynamic_macro_record_key(r_macro_buffer, ¯o_pointer, macro_end, -1, record); | |
252 | break; | |
39e8e612 | 253 | } |
b624f32f | 254 | return true; |
39e8e612 | 255 | break; |
39e8e612 WS |
256 | } |
257 | } | |
258 | ||
259 | return true; | |
260 | } | |
261 | ||
10a7cd7e WS |
262 | #undef DYNAMIC_MACRO_CURRENT_SLOT |
263 | #undef DYNAMIC_MACRO_CURRENT_LENGTH | |
264 | #undef DYNAMIC_MACRO_CURRENT_CAPACITY |