dynamic_macro.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include "action_layer.h"
  19. #ifndef DYNAMIC_MACRO_SIZE
  20. /* May be overridden with a custom value. Be aware that the effective
  21. * macro length is half of this value: each keypress is recorded twice
  22. * because of the down-event and up-event. This is not a bug, it's the
  23. * intended behavior.
  24. *
  25. * Usually it should be fine to set the macro size to at least 256 but
  26. * there have been reports of it being too much in some users' cases,
  27. * so 128 is considered a safe default.
  28. */
  29. #define DYNAMIC_MACRO_SIZE 128
  30. #endif
  31. /* DYNAMIC_MACRO_RANGE must be set as the last element of user's
  32. * "planck_keycodes" enum prior to including this header. This allows
  33. * us to 'extend' it.
  34. */
  35. enum dynamic_macro_keycodes {
  36. DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
  37. DYN_REC_START2,
  38. DYN_REC_STOP,
  39. DYN_MACRO_PLAY1,
  40. DYN_MACRO_PLAY2,
  41. };
  42. /* Blink the LEDs to notify the user about some event. */
  43. void dynamic_macro_led_blink(void)
  44. {
  45. #ifdef BACKLIGHT_ENABLE
  46. backlight_toggle();
  47. wait_ms(100);
  48. backlight_toggle();
  49. #endif
  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) \
  56. ((int)(direction * ((POINTER) - (BEGIN))))
  57. #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) \
  58. ((int)(direction * ((END2) - (BEGIN)) + 1))
  59. /**
  60. * Start recording of the dynamic macro.
  61. *
  62. * @param[out] macro_pointer The new macro buffer iterator.
  63. * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
  64. */
  65. void dynamic_macro_record_start(
  66. keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
  67. {
  68. dprintln("dynamic macro recording: started");
  69. dynamic_macro_led_blink();
  70. clear_keyboard();
  71. layer_clear();
  72. *macro_pointer = macro_buffer;
  73. }
  74. /**
  75. * Play the dynamic macro.
  76. *
  77. * @param macro_buffer[in] The beginning of the macro buffer being played.
  78. * @param macro_end[in] The element after the last macro buffer element.
  79. * @param direction[in] Either +1 or -1, which way to iterate the buffer.
  80. */
  81. void dynamic_macro_play(
  82. keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
  83. {
  84. dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
  85. uint32_t saved_layer_state = layer_state;
  86. clear_keyboard();
  87. layer_clear();
  88. while (macro_buffer != macro_end) {
  89. process_record(macro_buffer);
  90. macro_buffer += direction;
  91. }
  92. clear_keyboard();
  93. layer_state = saved_layer_state;
  94. }
  95. /**
  96. * Record a single key in a dynamic macro.
  97. *
  98. * @param macro_buffer[in] The start of the used macro buffer.
  99. * @param macro_pointer[in,out] The current buffer position.
  100. * @param macro2_end[in] The end of the other macro.
  101. * @param direction[in] Either +1 or -1, which way to iterate the buffer.
  102. * @param record[in] The current keypress.
  103. */
  104. void dynamic_macro_record_key(
  105. keyrecord_t *macro_buffer,
  106. keyrecord_t **macro_pointer,
  107. keyrecord_t *macro2_end,
  108. int8_t direction,
  109. keyrecord_t *record)
  110. {
  111. /* If we've just started recording, ignore all the key releases. */
  112. if (!record->event.pressed && *macro_pointer == macro_buffer) {
  113. dprintln("dynamic macro: ignoring a leading key-up event");
  114. return;
  115. }
  116. /* The other end of the other macro is the last buffer element it
  117. * is safe to use before overwriting the other macro.
  118. */
  119. if (*macro_pointer - direction != macro2_end) {
  120. **macro_pointer = *record;
  121. *macro_pointer += direction;
  122. } else {
  123. dynamic_macro_led_blink();
  124. }
  125. dprintf(
  126. "dynamic macro: slot %d length: %d/%d\n",
  127. DYNAMIC_MACRO_CURRENT_SLOT(),
  128. DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer),
  129. DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end));
  130. }
  131. /**
  132. * End recording of the dynamic macro. Essentially just update the
  133. * pointer to the end of the macro.
  134. */
  135. void dynamic_macro_record_end(
  136. keyrecord_t *macro_buffer,
  137. keyrecord_t *macro_pointer,
  138. int8_t direction,
  139. keyrecord_t **macro_end)
  140. {
  141. dynamic_macro_led_blink();
  142. /* Do not save the keys being held when stopping the recording,
  143. * i.e. the keys used to access the layer DYN_REC_STOP is on.
  144. */
  145. while (macro_pointer != macro_buffer &&
  146. (macro_pointer - direction)->event.pressed) {
  147. dprintln("dynamic macro: trimming a trailing key-down event");
  148. macro_pointer -= direction;
  149. }
  150. dprintf(
  151. "dynamic macro: slot %d saved, length: %d\n",
  152. DYNAMIC_MACRO_CURRENT_SLOT(),
  153. DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
  154. *macro_end = macro_pointer;
  155. }
  156. /* Handle the key events related to the dynamic macros. Should be
  157. * called from process_record_user() like this:
  158. *
  159. * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  160. * if (!process_record_dynamic_macro(keycode, record)) {
  161. * return false;
  162. * }
  163. * <...THE REST OF THE FUNCTION...>
  164. * }
  165. */
  166. bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
  167. {
  168. /* Both macros use the same buffer but read/write on different
  169. * ends of it.
  170. *
  171. * Macro1 is written left-to-right starting from the beginning of
  172. * the buffer.
  173. *
  174. * Macro2 is written right-to-left starting from the end of the
  175. * buffer.
  176. *
  177. * &macro_buffer macro_end
  178. * v v
  179. * +------------------------------------------------------------+
  180. * |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
  181. * +------------------------------------------------------------+
  182. * ^ ^
  183. * r_macro_end r_macro_buffer
  184. *
  185. * During the recording when one macro encounters the end of the
  186. * other macro, the recording is stopped. Apart from this, there
  187. * are no arbitrary limits for the macros' length in relation to
  188. * each other: for example one can either have two medium sized
  189. * macros or one long macro and one short macro. Or even one empty
  190. * and one using the whole buffer.
  191. */
  192. static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
  193. /* Pointer to the first buffer element after the first macro.
  194. * Initially points to the very beginning of the buffer since the
  195. * macro is empty. */
  196. static keyrecord_t *macro_end = macro_buffer;
  197. /* The other end of the macro buffer. Serves as the beginning of
  198. * the second macro. */
  199. static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
  200. /* Like macro_end but for the second macro. */
  201. static keyrecord_t *r_macro_end = r_macro_buffer;
  202. /* A persistent pointer to the current macro position (iterator)
  203. * used during the recording. */
  204. static keyrecord_t *macro_pointer = NULL;
  205. /* 0 - no macro is being recorded right now
  206. * 1,2 - either macro 1 or 2 is being recorded */
  207. static uint8_t macro_id = 0;
  208. if (macro_id == 0) {
  209. /* No macro recording in progress. */
  210. if (!record->event.pressed) {
  211. switch (keycode) {
  212. case DYN_REC_START1:
  213. dynamic_macro_record_start(&macro_pointer, macro_buffer);
  214. macro_id = 1;
  215. return false;
  216. case DYN_REC_START2:
  217. dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
  218. macro_id = 2;
  219. return false;
  220. case DYN_MACRO_PLAY1:
  221. dynamic_macro_play(macro_buffer, macro_end, +1);
  222. return false;
  223. case DYN_MACRO_PLAY2:
  224. dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
  225. return false;
  226. }
  227. }
  228. } else {
  229. /* A macro is being recorded right now. */
  230. switch (keycode) {
  231. case DYN_REC_STOP:
  232. /* Stop the macro recording. */
  233. if (record->event.pressed) { /* Ignore the initial release
  234. * just after the recoding
  235. * starts. */
  236. switch (macro_id) {
  237. case 1:
  238. dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
  239. break;
  240. case 2:
  241. dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
  242. break;
  243. }
  244. macro_id = 0;
  245. }
  246. return false;
  247. case DYN_MACRO_PLAY1:
  248. case DYN_MACRO_PLAY2:
  249. dprintln("dynamic macro: ignoring macro play key while recording");
  250. return false;
  251. default:
  252. /* Store the key in the macro buffer and process it normally. */
  253. switch (macro_id) {
  254. case 1:
  255. dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
  256. break;
  257. case 2:
  258. dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
  259. break;
  260. }
  261. return true;
  262. break;
  263. }
  264. }
  265. return true;
  266. }
  267. #undef DYNAMIC_MACRO_CURRENT_SLOT
  268. #undef DYNAMIC_MACRO_CURRENT_LENGTH
  269. #undef DYNAMIC_MACRO_CURRENT_CAPACITY