process_dynamic_macro.c 10.0 KB

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