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