process_dynamic_macro.c 11 KB

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