process_auto_shift.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* Copyright 2017 Jeremy Cowgar
  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. #include "process_auto_shift.h"
  17. #include "quantum.h"
  18. #include "action_util.h"
  19. #include "timer.h"
  20. #include "keycodes.h"
  21. #ifndef AUTO_SHIFT_DISABLED_AT_STARTUP
  22. # define AUTO_SHIFT_STARTUP_STATE true /* enabled */
  23. #else
  24. # define AUTO_SHIFT_STARTUP_STATE false /* disabled */
  25. #endif
  26. // Stores the last Auto Shift key's up or down time, for evaluation or keyrepeat.
  27. static uint16_t autoshift_time = 0;
  28. #if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  29. // Stores the last key's up or down time, to replace autoshift_time so that Tap Hold times are accurate.
  30. static uint16_t retroshift_time = 0;
  31. // Stores a possibly Retro Shift key's up or down time, as retroshift_time needs
  32. // to be set before the Retro Shift key is evaluated if it is interrupted by an
  33. // Auto Shifted key.
  34. static uint16_t last_retroshift_time;
  35. #endif
  36. static uint16_t autoshift_timeout = AUTO_SHIFT_TIMEOUT;
  37. static uint16_t autoshift_lastkey = KC_NO;
  38. static keyrecord_t autoshift_lastrecord;
  39. // Keys take 8 bits if modifiers are excluded. This records the shift state
  40. // when pressed for each key, so that can be passed to the release function
  41. // and it knows which key needs to be released (if shifted is different base).
  42. static uint16_t autoshift_shift_states[((1 << 8) + 15) / 16];
  43. static struct {
  44. // Whether Auto Shift is enabled.
  45. bool enabled : 1;
  46. // Whether the last auto-shifted key was released after the timeout. This
  47. // is used to replicate the last key for a tap-then-hold.
  48. bool lastshifted : 1;
  49. // Whether an auto-shiftable key has been pressed but not processed.
  50. bool in_progress : 1;
  51. // Whether the auto-shifted keypress has been registered.
  52. bool holding_shift : 1;
  53. // Whether the user is holding a shift and we removed it.
  54. bool cancelling_lshift : 1;
  55. bool cancelling_rshift : 1;
  56. // clang-format wants to remove the true for some reason.
  57. // clang-format off
  58. } autoshift_flags = {AUTO_SHIFT_STARTUP_STATE, false, false, false, false, false};
  59. // clang-format on
  60. /** \brief Called on physical press, returns whether key should be added to Auto Shift */
  61. __attribute__((weak)) bool get_custom_auto_shifted_key(uint16_t keycode, keyrecord_t *record) {
  62. return false;
  63. }
  64. /** \brief Called on physical press, returns whether is Auto Shift key */
  65. __attribute__((weak)) bool get_auto_shifted_key(uint16_t keycode, keyrecord_t *record) {
  66. switch (keycode) {
  67. #ifndef NO_AUTO_SHIFT_ALPHA
  68. case AUTO_SHIFT_ALPHA:
  69. #endif
  70. #ifndef NO_AUTO_SHIFT_NUMERIC
  71. case AUTO_SHIFT_NUMERIC:
  72. #endif
  73. #ifndef NO_AUTO_SHIFT_SPECIAL
  74. # ifndef NO_AUTO_SHIFT_TAB
  75. case KC_TAB:
  76. # endif
  77. # ifndef NO_AUTO_SHIFT_SYMBOLS
  78. case AUTO_SHIFT_SYMBOLS:
  79. # endif
  80. #endif
  81. #ifdef AUTO_SHIFT_ENTER
  82. case KC_ENT:
  83. #endif
  84. return true;
  85. }
  86. return get_custom_auto_shifted_key(keycode, record);
  87. }
  88. /** \brief Called to check whether defines should apply if PER_KEY is set for it */
  89. __attribute__((weak)) bool get_auto_shift_repeat(uint16_t keycode, keyrecord_t *record) {
  90. return true;
  91. }
  92. __attribute__((weak)) bool get_auto_shift_no_auto_repeat(uint16_t keycode, keyrecord_t *record) {
  93. return true;
  94. }
  95. /** \brief Called when an Auto Shift key needs to be pressed */
  96. __attribute__((weak)) void autoshift_press_user(uint16_t keycode, bool shifted, keyrecord_t *record) {
  97. if (shifted) {
  98. add_weak_mods(MOD_BIT(KC_LSFT));
  99. }
  100. register_code16((IS_RETRO(keycode)) ? keycode & 0xFF : keycode);
  101. }
  102. /** \brief Called when an Auto Shift key needs to be released */
  103. __attribute__((weak)) void autoshift_release_user(uint16_t keycode, bool shifted, keyrecord_t *record) {
  104. unregister_code16((IS_RETRO(keycode)) ? keycode & 0xFF : keycode);
  105. }
  106. /** \brief Sets the shift state to use when keyrepeating, required by custom shifts */
  107. void set_autoshift_shift_state(uint16_t keycode, bool shifted) {
  108. keycode = keycode & 0xFF;
  109. if (shifted) {
  110. autoshift_shift_states[keycode / 16] |= (uint16_t)1 << keycode % 16;
  111. } else {
  112. autoshift_shift_states[keycode / 16] &= ~((uint16_t)1 << keycode % 16);
  113. }
  114. }
  115. /** \brief Gets the shift state to use when keyrepeating, required by custom shifts */
  116. bool get_autoshift_shift_state(uint16_t keycode) {
  117. keycode = keycode & 0xFF;
  118. return (autoshift_shift_states[keycode / 16] & (uint16_t)1 << keycode % 16) != (uint16_t)0;
  119. }
  120. /** \brief Restores the shift key if it was cancelled by Auto Shift */
  121. static void autoshift_flush_shift(void) {
  122. autoshift_flags.holding_shift = false;
  123. #ifdef CAPS_WORD_ENABLE
  124. if (!is_caps_word_on())
  125. #endif // CAPS_WORD_ENABLE
  126. {
  127. del_weak_mods(MOD_BIT(KC_LSFT));
  128. }
  129. if (autoshift_flags.cancelling_lshift) {
  130. autoshift_flags.cancelling_lshift = false;
  131. add_mods(MOD_BIT(KC_LSFT));
  132. }
  133. if (autoshift_flags.cancelling_rshift) {
  134. autoshift_flags.cancelling_rshift = false;
  135. add_mods(MOD_BIT(KC_RSFT));
  136. }
  137. send_keyboard_report();
  138. }
  139. /** \brief Record the press of an autoshiftable key
  140. *
  141. * \return Whether the record should be further processed.
  142. */
  143. static bool autoshift_press(uint16_t keycode, uint16_t now, keyrecord_t *record) {
  144. // clang-format off
  145. if ((get_mods()
  146. #if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  147. | get_oneshot_mods()
  148. #endif
  149. ) & (~MOD_BIT(KC_LSFT))
  150. ) {
  151. // clang-format on
  152. // Prevents keyrepeating unshifted value of key after using it in a key combo.
  153. autoshift_lastkey = KC_NO;
  154. #ifndef AUTO_SHIFT_MODIFIERS
  155. // We can't return true here anymore because custom unshifted values are
  156. // possible and there's no good way to tell whether the press returned
  157. // true upon release.
  158. set_autoshift_shift_state(keycode, false);
  159. autoshift_press_user(keycode, false, record);
  160. # if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  161. set_oneshot_mods(get_oneshot_mods() & (~MOD_BIT(KC_LSFT)));
  162. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  163. # endif
  164. return false;
  165. #endif
  166. }
  167. // Store record to be sent to user functions if there's no release record then.
  168. autoshift_lastrecord = *record;
  169. autoshift_lastrecord.event.pressed = false;
  170. autoshift_lastrecord.event.time = 0;
  171. // clang-format off
  172. #if defined(AUTO_SHIFT_REPEAT) || defined(AUTO_SHIFT_REPEAT_PER_KEY)
  173. if (keycode == autoshift_lastkey &&
  174. # ifdef AUTO_SHIFT_REPEAT_PER_KEY
  175. get_auto_shift_repeat(autoshift_lastkey, record) &&
  176. # endif
  177. # if !defined(AUTO_SHIFT_NO_AUTO_REPEAT) || defined(AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY)
  178. (
  179. !autoshift_flags.lastshifted
  180. # ifdef AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY
  181. || get_auto_shift_no_auto_repeat(autoshift_lastkey, record)
  182. # endif
  183. ) &&
  184. # endif
  185. TIMER_DIFF_16(now, autoshift_time) < GET_TAPPING_TERM(autoshift_lastkey, record)
  186. ) {
  187. // clang-format on
  188. // Allow a tap-then-hold for keyrepeat.
  189. if (get_mods() & MOD_BIT(KC_LSFT)) {
  190. autoshift_flags.cancelling_lshift = true;
  191. del_mods(MOD_BIT(KC_LSFT));
  192. }
  193. if (get_mods() & MOD_BIT(KC_RSFT)) {
  194. autoshift_flags.cancelling_rshift = true;
  195. del_mods(MOD_BIT(KC_RSFT));
  196. }
  197. // autoshift_shift_state doesn't need to be changed.
  198. autoshift_press_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
  199. return false;
  200. }
  201. #endif
  202. // Use physical shift state of press event to be more like normal typing.
  203. #if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  204. autoshift_flags.lastshifted = (get_mods() | get_oneshot_mods()) & MOD_BIT(KC_LSFT);
  205. set_oneshot_mods(get_oneshot_mods() & (~MOD_BIT(KC_LSFT)));
  206. #else
  207. autoshift_flags.lastshifted = get_mods() & MOD_BIT(KC_LSFT);
  208. #endif
  209. // Record the keycode so we can simulate it later.
  210. autoshift_lastkey = keycode;
  211. autoshift_time = now;
  212. autoshift_flags.in_progress = true;
  213. #if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  214. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  215. #endif
  216. return false;
  217. }
  218. /** \brief Registers an autoshiftable key under the right conditions
  219. *
  220. * If autoshift_timeout has elapsed, register a shift and the key.
  221. *
  222. * If the Auto Shift key is released before the delay has elapsed, register the
  223. * key without a shift.
  224. *
  225. * Called on key down with keycode=KC_NO, auto-shifted key up, and timeout.
  226. */
  227. static void autoshift_end(uint16_t keycode, uint16_t now, bool matrix_trigger, keyrecord_t *record) {
  228. if (autoshift_flags.in_progress && (keycode == autoshift_lastkey || keycode == KC_NO)) {
  229. // Process the auto-shiftable key.
  230. autoshift_flags.in_progress = false;
  231. // clang-format off
  232. autoshift_flags.lastshifted =
  233. autoshift_flags.lastshifted
  234. || TIMER_DIFF_16(now, autoshift_time) >=
  235. #ifdef AUTO_SHIFT_TIMEOUT_PER_KEY
  236. get_autoshift_timeout(autoshift_lastkey, record)
  237. #else
  238. autoshift_timeout
  239. #endif
  240. ;
  241. // clang-format on
  242. set_autoshift_shift_state(autoshift_lastkey, autoshift_flags.lastshifted);
  243. if (get_mods() & MOD_BIT(KC_LSFT)) {
  244. autoshift_flags.cancelling_lshift = true;
  245. del_mods(MOD_BIT(KC_LSFT));
  246. }
  247. if (get_mods() & MOD_BIT(KC_RSFT)) {
  248. autoshift_flags.cancelling_rshift = true;
  249. del_mods(MOD_BIT(KC_RSFT));
  250. }
  251. autoshift_press_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
  252. // clang-format off
  253. #if (defined(AUTO_SHIFT_REPEAT) || defined(AUTO_SHIFT_REPEAT_PER_KEY)) && (!defined(AUTO_SHIFT_NO_AUTO_REPEAT) || defined(AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY))
  254. if (matrix_trigger
  255. # ifdef AUTO_SHIFT_REPEAT_PER_KEY
  256. && get_auto_shift_repeat(autoshift_lastkey, record)
  257. # endif
  258. # ifdef AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY
  259. && !get_auto_shift_no_auto_repeat(autoshift_lastkey, record)
  260. # endif
  261. ) {
  262. // Prevents release.
  263. return;
  264. }
  265. #endif
  266. // clang-format on
  267. #if TAP_CODE_DELAY > 0
  268. wait_ms(TAP_CODE_DELAY);
  269. #endif
  270. autoshift_release_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
  271. autoshift_flush_shift();
  272. } else {
  273. // Release after keyrepeat.
  274. autoshift_release_user(keycode, get_autoshift_shift_state(keycode), record);
  275. if (keycode == autoshift_lastkey) {
  276. // This will only fire when the key was the last auto-shiftable
  277. // pressed. That prevents 'aaaaBBBB' then releasing a from unshifting
  278. // later 'B's (if 'B' wasn't auto-shiftable).
  279. autoshift_flush_shift();
  280. }
  281. }
  282. // Roll the autoshift_time forward for detecting tap-and-hold.
  283. autoshift_time = now;
  284. }
  285. /** \brief Simulates auto-shifted key releases when timeout is hit
  286. *
  287. * Can be called from \c matrix_scan_user so that auto-shifted keys are sent
  288. * immediately after the timeout has expired, rather than waiting for the key
  289. * to be released.
  290. */
  291. void autoshift_matrix_scan(void) {
  292. if (autoshift_flags.in_progress) {
  293. const uint16_t now = timer_read();
  294. if (TIMER_DIFF_16(now, autoshift_time) >=
  295. #ifdef AUTO_SHIFT_TIMEOUT_PER_KEY
  296. get_autoshift_timeout(autoshift_lastkey, &autoshift_lastrecord)
  297. #else
  298. autoshift_timeout
  299. #endif
  300. ) {
  301. autoshift_end(autoshift_lastkey, now, true, &autoshift_lastrecord);
  302. }
  303. }
  304. }
  305. void autoshift_toggle(void) {
  306. autoshift_flags.enabled = !autoshift_flags.enabled;
  307. autoshift_flush_shift();
  308. }
  309. void autoshift_enable(void) {
  310. autoshift_flags.enabled = true;
  311. }
  312. void autoshift_disable(void) {
  313. autoshift_flags.enabled = false;
  314. autoshift_flush_shift();
  315. }
  316. #ifndef AUTO_SHIFT_NO_SETUP
  317. void autoshift_timer_report(void) {
  318. # ifdef SEND_STRING_ENABLE
  319. const char *autoshift_timeout_str = get_u16_str(autoshift_timeout, ' ');
  320. // Skip padding spaces
  321. while (*autoshift_timeout_str == ' ') {
  322. autoshift_timeout_str++;
  323. }
  324. send_string(autoshift_timeout_str);
  325. # endif
  326. }
  327. #endif
  328. bool get_autoshift_state(void) {
  329. return autoshift_flags.enabled;
  330. }
  331. uint16_t get_generic_autoshift_timeout(void) {
  332. return autoshift_timeout;
  333. }
  334. __attribute__((weak)) uint16_t get_autoshift_timeout(uint16_t keycode, keyrecord_t *record) {
  335. return autoshift_timeout;
  336. }
  337. void set_autoshift_timeout(uint16_t timeout) {
  338. autoshift_timeout = timeout;
  339. }
  340. bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
  341. // Note that record->event.time isn't reliable, see:
  342. // https://github.com/qmk/qmk_firmware/pull/9826#issuecomment-733559550
  343. // clang-format off
  344. const uint16_t now =
  345. #if !defined(RETRO_SHIFT) || defined(NO_ACTION_TAPPING)
  346. timer_read()
  347. #else
  348. (record->event.pressed) ? retroshift_time : timer_read()
  349. #endif
  350. ;
  351. // clang-format on
  352. if (record->event.pressed) {
  353. if (autoshift_flags.in_progress) {
  354. // Evaluate previous key if there is one.
  355. autoshift_end(KC_NO, now, false, &autoshift_lastrecord);
  356. }
  357. switch (keycode) {
  358. case AS_TOGG:
  359. autoshift_toggle();
  360. break;
  361. case AS_ON:
  362. autoshift_enable();
  363. break;
  364. case AS_OFF:
  365. autoshift_disable();
  366. break;
  367. #ifndef AUTO_SHIFT_NO_SETUP
  368. case AS_UP:
  369. autoshift_timeout += 5;
  370. break;
  371. case AS_DOWN:
  372. autoshift_timeout -= 5;
  373. break;
  374. case AS_RPT:
  375. autoshift_timer_report();
  376. break;
  377. #endif
  378. }
  379. // If Retro Shift is disabled, possible custom actions shouldn't happen.
  380. // clang-format off
  381. #if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  382. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  383. const bool is_hold_on_interrupt = get_hold_on_other_key_press(keycode, record);
  384. # else
  385. const bool is_hold_on_interrupt = false;
  386. # endif
  387. #endif
  388. if (IS_RETRO(keycode)
  389. #if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  390. // Not tapped or #defines mean that rolls should use hold action.
  391. && (
  392. record->tap.count == 0
  393. # ifdef RETRO_TAPPING_PER_KEY
  394. || !get_retro_tapping(keycode, record)
  395. # endif
  396. || (record->tap.interrupted && is_hold_on_interrupt))
  397. #endif
  398. ) {
  399. // clang-format on
  400. autoshift_lastkey = KC_NO;
  401. return true;
  402. }
  403. } else {
  404. if (keycode == KC_LSFT) {
  405. autoshift_flags.cancelling_lshift = false;
  406. } else if (keycode == KC_RSFT) {
  407. autoshift_flags.cancelling_rshift = false;
  408. }
  409. // Same as above (for pressed), additional checks are not needed because
  410. // tap.count gets set to 0 in process_action
  411. // clang-format off
  412. else if (IS_RETRO(keycode)
  413. #if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  414. && (
  415. record->tap.count == 0
  416. # ifdef RETRO_TAPPING_PER_KEY
  417. || !get_retro_tapping(keycode, record)
  418. # endif
  419. )
  420. #endif
  421. ) {
  422. // Fixes modifiers not being applied to rolls with AUTO_SHIFT_MODIFIERS set.
  423. #ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  424. if (autoshift_flags.in_progress && get_hold_on_other_key_press(keycode, record)) {
  425. autoshift_end(KC_NO, now, false, &autoshift_lastrecord);
  426. }
  427. #endif
  428. // clang-format on
  429. return true;
  430. }
  431. }
  432. if (!autoshift_flags.enabled) {
  433. return true;
  434. }
  435. if (get_auto_shifted_key(keycode, record)) {
  436. if (record->event.pressed) {
  437. return autoshift_press(keycode, now, record);
  438. } else {
  439. autoshift_end(keycode, now, false, record);
  440. return false;
  441. }
  442. }
  443. // Prevent keyrepeating of older keys upon non-AS key event.
  444. // Not commented at above returns but they serve the same function.
  445. if (record->event.pressed) {
  446. autoshift_lastkey = KC_NO;
  447. }
  448. return true;
  449. }
  450. #if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  451. // Called to record time before possible delays by action_tapping_process.
  452. void retroshift_poll_time(keyevent_t *event) {
  453. last_retroshift_time = retroshift_time;
  454. retroshift_time = timer_read();
  455. }
  456. // Used to swap the times of Retro Shifted key and Auto Shift key that interrupted it.
  457. void retroshift_swap_times(void) {
  458. if (last_retroshift_time != 0 && autoshift_flags.in_progress) {
  459. uint16_t temp = retroshift_time;
  460. retroshift_time = last_retroshift_time;
  461. last_retroshift_time = temp;
  462. }
  463. }
  464. #endif