repeat_key.c 11 KB

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