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.

283 lines
10 KiB

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