repeat_key.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright 2022-2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "repeat_key.h"
  15. // Variables saving the state of the last key press.
  16. static keyrecord_t last_record = {0};
  17. static uint8_t last_mods = 0;
  18. // Signed count of the number of times the last key has been repeated or
  19. // alternate repeated: it is 0 when a key is pressed normally, positive when
  20. // repeated, and negative when alternate repeated.
  21. static int8_t last_repeat_count = 0;
  22. // The repeat_count, but set to 0 outside of repeat_key_invoke() so that it is
  23. // nonzero only while a repeated key is being processed.
  24. static int8_t processing_repeat_count = 0;
  25. uint16_t get_last_keycode(void) {
  26. return last_record.keycode;
  27. }
  28. uint8_t get_last_mods(void) {
  29. return last_mods;
  30. }
  31. void set_last_keycode(uint16_t keycode) {
  32. set_last_record(keycode, &(keyrecord_t){
  33. #ifndef NO_ACTION_TAPPING
  34. .tap.interrupted = false,
  35. .tap.count = 1,
  36. #endif
  37. });
  38. }
  39. void set_last_mods(uint8_t mods) {
  40. last_mods = mods;
  41. }
  42. void set_last_record(uint16_t keycode, keyrecord_t* record) {
  43. last_record = *record;
  44. last_record.keycode = keycode;
  45. last_repeat_count = 0;
  46. }
  47. /** @brief Updates `last_repeat_count` in direction `dir`. */
  48. static void update_last_repeat_count(int8_t dir) {
  49. if (dir * last_repeat_count < 0) {
  50. last_repeat_count = dir;
  51. } else if (dir * last_repeat_count < 127) {
  52. last_repeat_count += dir;
  53. }
  54. }
  55. int8_t get_repeat_key_count(void) {
  56. return processing_repeat_count;
  57. }
  58. void repeat_key_invoke(const keyevent_t* event) {
  59. // It is possible (e.g. in rolled presses) that the last key changes while
  60. // the Repeat Key is pressed. To prevent stuck keys, it is important to
  61. // remember separately what key record was processed on press so that the
  62. // the corresponding record is generated on release.
  63. static keyrecord_t registered_record = {0};
  64. static int8_t registered_repeat_count = 0;
  65. // Since this function calls process_record(), it may recursively call
  66. // itself. We return early if `processing_repeat_count` is nonzero to
  67. // prevent infinite recursion.
  68. if (processing_repeat_count || !last_record.keycode) {
  69. return;
  70. }
  71. if (event->pressed) {
  72. update_last_repeat_count(1);
  73. // On press, apply the last mods state, stacking on top of current mods.
  74. register_weak_mods(last_mods);
  75. registered_record = last_record;
  76. registered_repeat_count = last_repeat_count;
  77. }
  78. // Generate a keyrecord and plumb it into the event pipeline.
  79. registered_record.event = *event;
  80. processing_repeat_count = registered_repeat_count;
  81. process_record(&registered_record);
  82. processing_repeat_count = 0;
  83. // On release, restore the mods state.
  84. if (!event->pressed) {
  85. unregister_weak_mods(last_mods);
  86. }
  87. }
  88. #ifndef NO_ALT_REPEAT_KEY
  89. /**
  90. * @brief Find alternate keycode from a table of opposing keycode pairs.
  91. * @param table Array of pairs of basic keycodes, declared as PROGMEM.
  92. * @param table_size_bytes The size of the table in bytes.
  93. * @param target The basic keycode to find.
  94. * @return The alternate basic keycode, or KC_NO if none was found.
  95. *
  96. * @note The table keycodes and target must be basic keycodes.
  97. *
  98. * This helper is used several times below to define alternate keys. Given a
  99. * table of pairs of basic keycodes, the function finds the pair containing
  100. * `target` and returns the other keycode in the pair.
  101. */
  102. static uint8_t find_alt_keycode(const uint8_t (*table)[2], uint8_t table_size_bytes, uint8_t target) {
  103. const uint8_t* keycodes = (const uint8_t*)table;
  104. for (uint8_t i = 0; i < table_size_bytes; ++i) {
  105. if (target == pgm_read_byte(keycodes + i)) {
  106. // Xor (i ^ 1) the index to get the other element in the pair.
  107. return pgm_read_byte(keycodes + (i ^ 1));
  108. }
  109. }
  110. return KC_NO;
  111. }
  112. uint16_t get_alt_repeat_key_keycode(void) {
  113. uint16_t keycode = last_record.keycode;
  114. uint8_t mods = last_mods;
  115. // Call the user callback first to give it a chance to override the default
  116. // alternate key definitions that follow.
  117. uint16_t alt_keycode = get_alt_repeat_key_keycode_user(keycode, mods);
  118. if (alt_keycode != KC_TRANSPARENT) {
  119. return alt_keycode;
  120. }
  121. // Convert 8-bit mods to the 5-bit format used in keycodes. This is lossy:
  122. // if left and right handed mods were mixed, they all become right handed.
  123. mods = ((mods & 0xf0) ? /* set right hand bit */ 0x10 : 0)
  124. // Combine right and left hand mods.
  125. | (((mods >> 4) | mods) & 0xf);
  126. switch (keycode) {
  127. case QK_MODS ... QK_MODS_MAX: // Unpack modifier + basic key.
  128. mods |= QK_MODS_GET_MODS(keycode);
  129. keycode = QK_MODS_GET_BASIC_KEYCODE(keycode);
  130. break;
  131. # ifndef NO_ACTION_TAPPING
  132. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  133. keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode);
  134. break;
  135. # ifndef NO_ACTION_LAYER
  136. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  137. keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode);
  138. break;
  139. # endif // NO_ACTION_LAYER
  140. # endif // NO_ACTION_TAPPING
  141. # ifdef SWAP_HANDS_ENABLE
  142. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  143. if (IS_SWAP_HANDS_KEYCODE(keycode)) {
  144. return KC_NO;
  145. }
  146. keycode = QK_SWAP_HANDS_GET_TAP_KEYCODE(keycode);
  147. break;
  148. # endif // SWAP_HANDS_ENABLE
  149. }
  150. if (IS_QK_BASIC(keycode)) {
  151. if ((mods & (MOD_LCTL | MOD_LALT | MOD_LGUI))) {
  152. // The last key was pressed with a modifier other than Shift.
  153. // The following maps
  154. // mod + F <-> mod + B
  155. // and a few others, supporting several core hotkeys used in
  156. // Emacs, Vim, less, and other programs.
  157. // clang-format off
  158. static const uint8_t pairs[][2] PROGMEM = {
  159. {KC_F , KC_B }, // Forward / Backward.
  160. {KC_D , KC_U }, // Down / Up.
  161. {KC_N , KC_P }, // Next / Previous.
  162. {KC_A , KC_E }, // Home / End.
  163. {KC_O , KC_I }, // Older / Newer in Vim jump list.
  164. };
  165. // clang-format on
  166. alt_keycode = find_alt_keycode(pairs, sizeof(pairs), keycode);
  167. } else {
  168. // The last key was pressed with no mods or only Shift. The
  169. // following map a few more Vim hotkeys.
  170. // clang-format off
  171. static const uint8_t pairs[][2] PROGMEM = {
  172. {KC_J , KC_K }, // Down / Up.
  173. {KC_H , KC_L }, // Left / Right.
  174. // These two lines map W and E to B, and B to W.
  175. {KC_W , KC_B }, // Forward / Backward by word.
  176. {KC_E , KC_B }, // Forward / Backward by word.
  177. };
  178. // clang-format on
  179. alt_keycode = find_alt_keycode(pairs, sizeof(pairs), keycode);
  180. }
  181. if (!alt_keycode) {
  182. // The following key pairs are considered with any mods.
  183. // clang-format off
  184. static const uint8_t pairs[][2] PROGMEM = {
  185. {KC_LEFT, KC_RGHT}, // Left / Right Arrow.
  186. {KC_UP , KC_DOWN}, // Up / Down Arrow.
  187. {KC_HOME, KC_END }, // Home / End.
  188. {KC_PGUP, KC_PGDN}, // Page Up / Page Down.
  189. {KC_BSPC, KC_DEL }, // Backspace / Delete.
  190. {KC_LBRC, KC_RBRC}, // Brackets [ ] and { }.
  191. #ifdef EXTRAKEY_ENABLE
  192. {KC_WBAK, KC_WFWD}, // Browser Back / Forward.
  193. {KC_MNXT, KC_MPRV}, // Next / Previous Media Track.
  194. {KC_MFFD, KC_MRWD}, // Fast Forward / Rewind Media.
  195. {KC_VOLU, KC_VOLD}, // Volume Up / Down.
  196. {KC_BRIU, KC_BRID}, // Brightness Up / Down.
  197. #endif // EXTRAKEY_ENABLE
  198. #ifdef MOUSEKEY_ENABLE
  199. {KC_MS_L, KC_MS_R}, // Mouse Cursor Left / Right.
  200. {KC_MS_U, KC_MS_D}, // Mouse Cursor Up / Down.
  201. {KC_WH_L, KC_WH_R}, // Mouse Wheel Left / Right.
  202. {KC_WH_U, KC_WH_D}, // Mouse Wheel Up / Down.
  203. #endif // MOUSEKEY_ENABLE
  204. };
  205. // clang-format on
  206. alt_keycode = find_alt_keycode(pairs, sizeof(pairs), keycode);
  207. }
  208. if (alt_keycode) {
  209. // Combine basic keycode with mods.
  210. return (mods << 8) | alt_keycode;
  211. }
  212. }
  213. return KC_NO; // No alternate key found.
  214. }
  215. void alt_repeat_key_invoke(const keyevent_t* event) {
  216. static keyrecord_t registered_record = {0};
  217. static int8_t registered_repeat_count = 0;
  218. // Since this function calls process_record(), it may recursively call
  219. // itself. We return early if `processing_repeat_count` is nonzero to
  220. // prevent infinite recursion.
  221. if (processing_repeat_count) {
  222. return;
  223. }
  224. if (event->pressed) {
  225. registered_record = (keyrecord_t){
  226. # ifndef NO_ACTION_TAPPING
  227. .tap.interrupted = false,
  228. .tap.count = 0,
  229. # endif
  230. .keycode = get_alt_repeat_key_keycode(),
  231. };
  232. }
  233. // Early return if there is no alternate key defined.
  234. if (!registered_record.keycode) {
  235. return;
  236. }
  237. if (event->pressed) {
  238. update_last_repeat_count(-1);
  239. registered_repeat_count = last_repeat_count;
  240. }
  241. // Generate a keyrecord and plumb it into the event pipeline.
  242. registered_record.event = *event;
  243. processing_repeat_count = registered_repeat_count;
  244. process_record(&registered_record);
  245. processing_repeat_count = 0;
  246. }
  247. // Default implementation of get_alt_repeat_key_keycode_user().
  248. __attribute__((weak)) uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) {
  249. return KC_TRANSPARENT;
  250. }
  251. #endif // NO_ALT_REPEAT_KEY