You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
10 KiB

  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. }
  115. dynamic_macro_record_key_user(direction, record);
  116. 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));
  117. }
  118. /**
  119. * End recording of the dynamic macro. Essentially just update the
  120. * pointer to the end of the macro.
  121. */
  122. void dynamic_macro_record_end(keyrecord_t *macro_buffer, keyrecord_t *macro_pointer, int8_t direction, keyrecord_t **macro_end) {
  123. dynamic_macro_record_end_user(direction);
  124. /* Do not save the keys being held when stopping the recording,
  125. * i.e. the keys used to access the layer DM_RSTP is on.
  126. */
  127. while (macro_pointer != macro_buffer && (macro_pointer - direction)->event.pressed) {
  128. dprintln("dynamic macro: trimming a trailing key-down event");
  129. macro_pointer -= direction;
  130. }
  131. dprintf("dynamic macro: slot %d saved, length: %d\n", DYNAMIC_MACRO_CURRENT_SLOT(), DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
  132. *macro_end = macro_pointer;
  133. }
  134. /* Both macros use the same buffer but read/write on different
  135. * ends of it.
  136. *
  137. * Macro1 is written left-to-right starting from the beginning of
  138. * the buffer.
  139. *
  140. * Macro2 is written right-to-left starting from the end of the
  141. * buffer.
  142. *
  143. * &macro_buffer macro_end
  144. * v v
  145. * +------------------------------------------------------------+
  146. * |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
  147. * +------------------------------------------------------------+
  148. * ^ ^
  149. * r_macro_end r_macro_buffer
  150. *
  151. * During the recording when one macro encounters the end of the
  152. * other macro, the recording is stopped. Apart from this, there
  153. * are no arbitrary limits for the macros' length in relation to
  154. * each other: for example one can either have two medium sized
  155. * macros or one long macro and one short macro. Or even one empty
  156. * and one using the whole buffer.
  157. */
  158. static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
  159. /* Pointer to the first buffer element after the first macro.
  160. * Initially points to the very beginning of the buffer since the
  161. * macro is empty. */
  162. static keyrecord_t *macro_end = macro_buffer;
  163. /* The other end of the macro buffer. Serves as the beginning of
  164. * the second macro. */
  165. static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
  166. /* Like macro_end but for the second macro. */
  167. static keyrecord_t *r_macro_end = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
  168. /* A persistent pointer to the current macro position (iterator)
  169. * used during the recording. */
  170. static keyrecord_t *macro_pointer = NULL;
  171. /* 0 - no macro is being recorded right now
  172. * 1,2 - either macro 1 or 2 is being recorded */
  173. static uint8_t macro_id = 0;
  174. /**
  175. * If a dynamic macro is currently being recorded, stop recording.
  176. */
  177. void dynamic_macro_stop_recording(void) {
  178. switch (macro_id) {
  179. case 1:
  180. dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
  181. break;
  182. case 2:
  183. dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
  184. break;
  185. }
  186. macro_id = 0;
  187. }
  188. /* Handle the key events related to the dynamic macros. Should be
  189. * called from process_record_user() like this:
  190. *
  191. * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  192. * if (!process_record_dynamic_macro(keycode, record)) {
  193. * return false;
  194. * }
  195. * <...THE REST OF THE FUNCTION...>
  196. * }
  197. */
  198. bool process_dynamic_macro(uint16_t keycode, keyrecord_t *record) {
  199. if (macro_id == 0) {
  200. /* No macro recording in progress. */
  201. if (!record->event.pressed) {
  202. switch (keycode) {
  203. case QK_DYNAMIC_MACRO_RECORD_START_1:
  204. dynamic_macro_record_start(&macro_pointer, macro_buffer, +1);
  205. macro_id = 1;
  206. return false;
  207. case QK_DYNAMIC_MACRO_RECORD_START_2:
  208. dynamic_macro_record_start(&macro_pointer, r_macro_buffer, -1);
  209. macro_id = 2;
  210. return false;
  211. case QK_DYNAMIC_MACRO_PLAY_1:
  212. dynamic_macro_play(macro_buffer, macro_end, +1);
  213. return false;
  214. case QK_DYNAMIC_MACRO_PLAY_2:
  215. dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
  216. return false;
  217. }
  218. }
  219. } else {
  220. /* A macro is being recorded right now. */
  221. switch (keycode) {
  222. case QK_DYNAMIC_MACRO_RECORD_START_1:
  223. case QK_DYNAMIC_MACRO_RECORD_START_2:
  224. case QK_DYNAMIC_MACRO_RECORD_STOP:
  225. /* Stop the macro recording. */
  226. if (record->event.pressed ^ (keycode != QK_DYNAMIC_MACRO_RECORD_STOP)) { /* Ignore the initial release
  227. * just after the recording
  228. * starts for DM_RSTP. */
  229. dynamic_macro_stop_recording();
  230. }
  231. return false;
  232. #ifdef DYNAMIC_MACRO_NO_NESTING
  233. case QK_DYNAMIC_MACRO_PLAY_1:
  234. case QK_DYNAMIC_MACRO_PLAY_2:
  235. dprintln("dynamic macro: ignoring macro play key while recording");
  236. return false;
  237. #endif
  238. default:
  239. if (dynamic_macro_valid_key_user(keycode, record)) {
  240. /* Store the key in the macro buffer and process it normally. */
  241. switch (macro_id) {
  242. case 1:
  243. dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
  244. break;
  245. case 2:
  246. dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
  247. break;
  248. }
  249. }
  250. return true;
  251. break;
  252. }
  253. }
  254. return true;
  255. }