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.

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