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.

264 lines
10 KiB

  1. /* Copyright 2016 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
  17. #pragma once
  18. /* Warn users that this is now deprecated and they should use the core feature instead. */
  19. #pragma message "Dynamic Macros is now a core feature. See updated documentation to see how to configure it: https://docs.qmk.fm/#/feature_dynamic_macros"
  20. #include "action_layer.h"
  21. #ifndef DYNAMIC_MACRO_SIZE
  22. /* May be overridden with a custom value. Be aware that the effective
  23. * macro length is half of this value: each keypress is recorded twice
  24. * because of the down-event and up-event. This is not a bug, it's the
  25. * intended behavior.
  26. *
  27. * Usually it should be fine to set the macro size to at least 256 but
  28. * there have been reports of it being too much in some users' cases,
  29. * so 128 is considered a safe default.
  30. */
  31. # define DYNAMIC_MACRO_SIZE 128
  32. #endif
  33. /* Blink the LEDs to notify the user about some event. */
  34. void dynamic_macro_led_blink(void) {
  35. #ifdef BACKLIGHT_ENABLE
  36. backlight_toggle();
  37. wait_ms(100);
  38. backlight_toggle();
  39. #endif
  40. }
  41. /* Convenience macros used for retrieving the debug info. All of them
  42. * need a `direction` variable accessible at the call site.
  43. */
  44. #define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2)
  45. #define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) ((int)(direction * ((POINTER) - (BEGIN))))
  46. #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) ((int)(direction * ((END2) - (BEGIN)) + 1))
  47. /**
  48. * Start recording of the dynamic macro.
  49. *
  50. * @param[out] macro_pointer The new macro buffer iterator.
  51. * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
  52. */
  53. void dynamic_macro_record_start(keyrecord_t **macro_pointer, keyrecord_t *macro_buffer) {
  54. dprintln("dynamic macro recording: started");
  55. dynamic_macro_led_blink();
  56. clear_keyboard();
  57. layer_clear();
  58. *macro_pointer = macro_buffer;
  59. }
  60. /**
  61. * Play the dynamic macro.
  62. *
  63. * @param macro_buffer[in] The beginning of the macro buffer being played.
  64. * @param macro_end[in] The element after the last macro buffer element.
  65. * @param direction[in] Either +1 or -1, which way to iterate the buffer.
  66. */
  67. void dynamic_macro_play(keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction) {
  68. dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
  69. uint32_t saved_layer_state = layer_state;
  70. clear_keyboard();
  71. layer_clear();
  72. while (macro_buffer != macro_end) {
  73. process_record(macro_buffer);
  74. macro_buffer += direction;
  75. }
  76. clear_keyboard();
  77. layer_state = saved_layer_state;
  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_led_blink();
  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_led_blink();
  111. /* Do not save the keys being held when stopping the recording,
  112. * i.e. the keys used to access the layer DM_RSTP 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_record_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 QK_DYNAMIC_MACRO_RECORD_START_1:
  177. dynamic_macro_record_start(&macro_pointer, macro_buffer);
  178. macro_id = 1;
  179. return false;
  180. case QK_DYNAMIC_MACRO_RECORD_START_2:
  181. dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
  182. macro_id = 2;
  183. return false;
  184. case QK_DYNAMIC_MACRO_PLAY_1:
  185. dynamic_macro_play(macro_buffer, macro_end, +1);
  186. return false;
  187. case QK_DYNAMIC_MACRO_PLAY_2:
  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 QK_DYNAMIC_MACRO_RECORD_STOP:
  196. /* Stop the macro recording. */
  197. if (record->event.pressed) { /* Ignore the initial release
  198. * just after the recoding
  199. * starts. */
  200. switch (macro_id) {
  201. case 1:
  202. dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
  203. break;
  204. case 2:
  205. dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
  206. break;
  207. }
  208. macro_id = 0;
  209. }
  210. return false;
  211. case QK_DYNAMIC_MACRO_PLAY_1:
  212. case QK_DYNAMIC_MACRO_PLAY_2:
  213. dprintln("dynamic macro: ignoring macro play key while recording");
  214. return false;
  215. default:
  216. /* Store the key in the macro buffer and process it normally. */
  217. switch (macro_id) {
  218. case 1:
  219. dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
  220. break;
  221. case 2:
  222. dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
  223. break;
  224. }
  225. return true;
  226. break;
  227. }
  228. }
  229. return true;
  230. }
  231. #undef DYNAMIC_MACRO_CURRENT_SLOT
  232. #undef DYNAMIC_MACRO_CURRENT_LENGTH
  233. #undef DYNAMIC_MACRO_CURRENT_CAPACITY