process_dynamic_macro.c 11 KB

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