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