process_dynamic_macro.c 9.8 KB

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