logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_dynamic_macro.c (11241B)


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