]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/dynamic_macro.h
fix the clueboard layout json
[qmk_firmware.git] / quantum / dynamic_macro.h
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
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
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  */
33 #define DYNAMIC_MACRO_SIZE 128
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  */
40 enum dynamic_macro_keycodes {
41     DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
42     DYN_REC_START2,
43     DYN_REC_STOP,
44     DYN_MACRO_PLAY1,
45     DYN_MACRO_PLAY2,
46 };
47
48 /* Blink the LEDs to notify the user about some event. */
49 void dynamic_macro_led_blink(void)
50 {
51 #ifdef BACKLIGHT_ENABLE
52     backlight_toggle();
53     wait_ms(100);
54     backlight_toggle();
55 #endif
56 }
57
58 /* Convenience macros used for retrieving the debug info. All of them
59  * need a `direction` variable accessible at the call site.
60  */
61 #define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2)
62 #define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) \
63     ((int)(direction * ((POINTER) - (BEGIN))))
64 #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) \
65     ((int)(direction * ((END2) - (BEGIN)) + 1))
66
67 /**
68  * Start recording of the dynamic macro.
69  *
70  * @param[out] macro_pointer The new macro buffer iterator.
71  * @param[in]  macro_buffer  The macro buffer used to initialize macro_pointer.
72  */
73 void dynamic_macro_record_start(
74     keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
75 {
76     dprintln("dynamic macro recording: started");
77
78     dynamic_macro_led_blink();
79
80     clear_keyboard();
81     layer_clear();
82     *macro_pointer = macro_buffer;
83 }
84
85 /**
86  * Play the dynamic macro.
87  *
88  * @param macro_buffer[in] The beginning of the macro buffer being played.
89  * @param macro_end[in]    The element after the last macro buffer element.
90  * @param direction[in]    Either +1 or -1, which way to iterate the buffer.
91  */
92 void dynamic_macro_play(
93     keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
94 {
95     dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
96
97     uint32_t saved_layer_state = layer_state;
98
99     clear_keyboard();
100     layer_clear();
101
102     while (macro_buffer != macro_end) {
103         process_record(macro_buffer);
104         macro_buffer += direction;
105     }
106
107     clear_keyboard();
108
109     layer_state = saved_layer_state;
110 }
111
112 /**
113  * Record a single key in a dynamic macro.
114  *
115  * @param macro_buffer[in] The start of the used macro buffer.
116  * @param macro_pointer[in,out] The current buffer position.
117  * @param macro2_end[in] The end of the other macro.
118  * @param direction[in]  Either +1 or -1, which way to iterate the buffer.
119  * @param record[in]     The current keypress.
120  */
121 void dynamic_macro_record_key(
122     keyrecord_t *macro_buffer,
123     keyrecord_t **macro_pointer,
124     keyrecord_t *macro2_end,
125     int8_t direction,
126     keyrecord_t *record)
127 {
128     /* If we've just started recording, ignore all the key releases. */
129     if (!record->event.pressed && *macro_pointer == macro_buffer) {
130         dprintln("dynamic macro: ignoring a leading key-up event");
131         return;
132     }
133
134     /* The other end of the other macro is the last buffer element it
135      * is safe to use before overwriting the other macro.
136      */
137     if (*macro_pointer - direction != macro2_end) {
138         **macro_pointer = *record;
139         *macro_pointer += direction;
140     } else {
141         dynamic_macro_led_blink();
142     }
143
144     dprintf(
145         "dynamic macro: slot %d length: %d/%d\n",
146         DYNAMIC_MACRO_CURRENT_SLOT(),
147         DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer),
148         DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end));
149 }
150
151 /**
152  * End recording of the dynamic macro. Essentially just update the
153  * pointer to the end of the macro.
154  */
155 void dynamic_macro_record_end(
156     keyrecord_t *macro_buffer,
157     keyrecord_t *macro_pointer,
158     int8_t direction,
159     keyrecord_t **macro_end)
160 {
161     dynamic_macro_led_blink();
162
163     /* Do not save the keys being held when stopping the recording,
164      * i.e. the keys used to access the layer DYN_REC_STOP is on.
165      */
166     while (macro_pointer != macro_buffer &&
167            (macro_pointer - direction)->event.pressed) {
168         dprintln("dynamic macro: trimming a trailing key-down event");
169         macro_pointer -= direction;
170     }
171
172     dprintf(
173         "dynamic macro: slot %d saved, length: %d\n",
174         DYNAMIC_MACRO_CURRENT_SLOT(),
175         DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
176
177     *macro_end = macro_pointer;
178 }
179
180 /* Handle the key events related to the dynamic macros. Should be
181  * called from process_record_user() like this:
182  *
183  *   bool process_record_user(uint16_t keycode, keyrecord_t *record) {
184  *       if (!process_record_dynamic_macro(keycode, record)) {
185  *           return false;
186  *       }
187  *       <...THE REST OF THE FUNCTION...>
188  *   }
189  */
190 bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
191 {
192     /* Both macros use the same buffer but read/write on different
193      * ends of it.
194      *
195      * Macro1 is written left-to-right starting from the beginning of
196      * the buffer.
197      *
198      * Macro2 is written right-to-left starting from the end of the
199      * buffer.
200      *
201      * &macro_buffer   macro_end
202      *  v                   v
203      * +------------------------------------------------------------+
204      * |>>>>>> MACRO1 >>>>>>      <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
205      * +------------------------------------------------------------+
206      *                           ^                                 ^
207      *                         r_macro_end                  r_macro_buffer
208      *
209      * During the recording when one macro encounters the end of the
210      * other macro, the recording is stopped. Apart from this, there
211      * are no arbitrary limits for the macros' length in relation to
212      * each other: for example one can either have two medium sized
213      * macros or one long macro and one short macro. Or even one empty
214      * and one using the whole buffer.
215      */
216     static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
217
218     /* Pointer to the first buffer element after the first macro.
219      * Initially points to the very beginning of the buffer since the
220      * macro is empty. */
221     static keyrecord_t *macro_end = macro_buffer;
222
223     /* The other end of the macro buffer. Serves as the beginning of
224      * the second macro. */
225     static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
226
227     /* Like macro_end but for the second macro. */
228     static keyrecord_t *r_macro_end = r_macro_buffer;
229
230     /* A persistent pointer to the current macro position (iterator)
231      * used during the recording. */
232     static keyrecord_t *macro_pointer = NULL;
233
234     /* 0   - no macro is being recorded right now
235      * 1,2 - either macro 1 or 2 is being recorded */
236     static uint8_t macro_id = 0;
237
238     if (macro_id == 0) {
239         /* No macro recording in progress. */
240         if (!record->event.pressed) {
241             switch (keycode) {
242             case DYN_REC_START1:
243                 dynamic_macro_record_start(&macro_pointer, macro_buffer);
244                 macro_id = 1;
245                 return false;
246             case DYN_REC_START2:
247                 dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
248                 macro_id = 2;
249                 return false;
250             case DYN_MACRO_PLAY1:
251                 dynamic_macro_play(macro_buffer, macro_end, +1);
252                 return false;
253             case DYN_MACRO_PLAY2:
254                 dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
255                 return false;
256             }
257         }
258     } else {
259         /* A macro is being recorded right now. */
260         switch (keycode) {
261         case DYN_REC_STOP:
262             /* Stop the macro recording. */
263             if (record->event.pressed) { /* Ignore the initial release
264                                           * just after the recoding
265                                           * starts. */
266                 switch (macro_id) {
267                 case 1:
268                     dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
269                     break;
270                 case 2:
271                     dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
272                     break;
273                 }
274                 macro_id = 0;
275             }
276             return false;
277         case DYN_MACRO_PLAY1:
278         case DYN_MACRO_PLAY2:
279             dprintln("dynamic macro: ignoring macro play key while recording");
280             return false;
281         default:
282             /* Store the key in the macro buffer and process it normally. */
283             switch (macro_id) {
284             case 1:
285                 dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
286                 break;
287             case 2:
288                 dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
289                 break;
290             }
291             return true;
292             break;
293         }
294     }
295
296     return true;
297 }
298
299 #undef DYNAMIC_MACRO_CURRENT_SLOT
300 #undef DYNAMIC_MACRO_CURRENT_LENGTH
301 #undef DYNAMIC_MACRO_CURRENT_CAPACITY
302
303 #endif