repeat_key.c 11 KB

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