process_dynamic_macro.c 10 KB

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