1 /* Copyright 2016 Jack Humbert
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.
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.
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/>.
17 /* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
18 #ifndef DYNAMIC_MACROS_H
19 #define DYNAMIC_MACROS_H
21 #include "action_layer.h"
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
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.
33 #define DYNAMIC_MACRO_SIZE 128
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
40 enum dynamic_macro_keycodes {
41 DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
48 /* Blink the LEDs to notify the user about some event. */
49 void dynamic_macro_led_blink(void)
57 * Start recording of the dynamic macro.
59 * @param[out] macro_pointer The new macro buffer iterator.
60 * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
62 void dynamic_macro_record_start(
63 keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
65 dynamic_macro_led_blink();
69 *macro_pointer = macro_buffer;
73 * Play the dynamic macro.
75 * @param macro_buffer[in] The beginning of the macro buffer being played.
76 * @param macro_end[in] The element after the last macro buffer element.
77 * @param direction[in] Either +1 or -1, which way to iterate the buffer.
79 void dynamic_macro_play(
80 keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
82 uint32_t saved_layer_state = layer_state;
87 while (macro_buffer != macro_end) {
88 process_record(macro_buffer);
89 macro_buffer += direction;
94 layer_state = saved_layer_state;
98 * Record a single key in a dynamic macro.
100 * @param macro_buffer[in] The start of the used macro buffer.
101 * @param macro_pointer[in,out] The current buffer position.
102 * @param macro2_end[in] The last buffer element it is safe to use before overwriting the other macro.
103 * @param direction[in] Either +1 or -1, which way to iterate the buffer.
104 * @param record[in] The current keypress.
106 void dynamic_macro_record_key(
107 keyrecord_t *macro_buffer,
108 keyrecord_t **macro_pointer,
109 keyrecord_t *macro2_end,
113 /* If we've just started recording, ignore all the key releases. */
114 if (!record->event.pressed && *macro_pointer == macro_buffer) {
118 if (*macro_pointer - direction != macro2_end) {
119 **macro_pointer = *record;
120 *macro_pointer += direction;
122 dynamic_macro_led_blink();
127 * End recording of the dynamic macro. Essentially just update the
128 * pointer to the end of the macro.
130 void dynamic_macro_record_end(
131 keyrecord_t *macro_buffer,
132 keyrecord_t *macro_pointer,
134 keyrecord_t **macro_end)
136 dynamic_macro_led_blink();
138 /* Do not save the keys being held when stopping the recording,
139 * i.e. the keys used to access the layer DYN_REC_STOP is on.
141 while (macro_pointer != macro_buffer &&
142 (macro_pointer - direction)->event.pressed) {
143 macro_pointer -= direction;
146 *macro_end = macro_pointer;
149 /* Handle the key events related to the dynamic macros. Should be
150 * called from process_record_user() like this:
152 * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
153 * if (!process_record_dynamic_macro(keycode, record)) {
156 * <...THE REST OF THE FUNCTION...>
159 bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
161 /* Both macros use the same buffer but read/write on different
164 * Macro1 is written left-to-right starting from the beginning of
167 * Macro2 is written right-to-left starting from the end of the
170 * ¯o_buffer macro_end
172 * +------------------------------------------------------------+
173 * |>>>>>> MACRO1 >>>>>>| |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
174 * +------------------------------------------------------------+
176 * r_macro_end r_macro_buffer
178 * During the recording when one macro encounters the end of the
179 * other macro, the recording is stopped. Apart from this, there
180 * are no arbitrary limits for the macros' length in relation to
181 * each other: for example one can either have two medium sized
182 * macros or one long macro and one short macro. Or even one empty
183 * and one using the whole buffer.
185 static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
187 /* Pointer to the first buffer element after the first macro.
188 * Initially points to the very beginning of the buffer since the
190 static keyrecord_t *macro_end = macro_buffer;
192 /* The other end of the macro buffer. Serves as the beginning of
193 * the second macro. */
194 static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
196 /* Like macro_end but for the second macro. */
197 static keyrecord_t *r_macro_end = r_macro_buffer;
199 /* A persistent pointer to the current macro position (iterator)
200 * used during the recording. */
201 static keyrecord_t *macro_pointer = NULL;
203 /* 0 - no macro is being recorded right now
204 * 1,2 - either macro 1 or 2 is being recorded */
205 static uint8_t macro_id = 0;
208 /* No macro recording in progress. */
209 if (!record->event.pressed) {
212 dynamic_macro_record_start(¯o_pointer, macro_buffer);
216 dynamic_macro_record_start(¯o_pointer, r_macro_buffer);
219 case DYN_MACRO_PLAY1:
220 dynamic_macro_play(macro_buffer, macro_end, +1);
222 case DYN_MACRO_PLAY2:
223 dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
228 /* A macro is being recorded right now. */
231 /* Stop the macro recording. */
232 if (record->event.pressed) { /* Ignore the initial release
233 * just after the recoding
237 dynamic_macro_record_end(macro_buffer, macro_pointer, +1, ¯o_end);
240 dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
247 /* Store the key in the macro buffer and process it normally. */
250 dynamic_macro_record_key(macro_buffer, ¯o_pointer, r_macro_end, +1, record);
253 dynamic_macro_record_key(r_macro_buffer, ¯o_pointer, macro_end, -1, record);