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.

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