process_auto_shift.c 18 KB

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