logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_auto_shift.c (17619B)


  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 key is an 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.time = 0;
  170. // clang-format off
  171. #if defined(AUTO_SHIFT_REPEAT) || defined(AUTO_SHIFT_REPEAT_PER_KEY)
  172. if (keycode == autoshift_lastkey &&
  173. # ifdef AUTO_SHIFT_REPEAT_PER_KEY
  174. get_auto_shift_repeat(autoshift_lastkey, record) &&
  175. # endif
  176. # if !defined(AUTO_SHIFT_NO_AUTO_REPEAT) || defined(AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY)
  177. (
  178. !autoshift_flags.lastshifted
  179. # ifdef AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY
  180. || get_auto_shift_no_auto_repeat(autoshift_lastkey, record)
  181. # endif
  182. ) &&
  183. # endif
  184. TIMER_DIFF_16(now, autoshift_time) < GET_TAPPING_TERM(autoshift_lastkey, record)
  185. ) {
  186. // clang-format on
  187. // Allow a tap-then-hold for keyrepeat.
  188. if (get_mods() & MOD_BIT(KC_LSFT)) {
  189. autoshift_flags.cancelling_lshift = true;
  190. del_mods(MOD_BIT(KC_LSFT));
  191. }
  192. if (get_mods() & MOD_BIT(KC_RSFT)) {
  193. autoshift_flags.cancelling_rshift = true;
  194. del_mods(MOD_BIT(KC_RSFT));
  195. }
  196. // autoshift_shift_state doesn't need to be changed.
  197. autoshift_press_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
  198. return false;
  199. }
  200. #endif
  201. // Use physical shift state of press event to be more like normal typing.
  202. #if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  203. autoshift_flags.lastshifted = (get_mods() | get_oneshot_mods()) & MOD_BIT(KC_LSFT);
  204. set_oneshot_mods(get_oneshot_mods() & (~MOD_BIT(KC_LSFT)));
  205. #else
  206. autoshift_flags.lastshifted = get_mods() & MOD_BIT(KC_LSFT);
  207. #endif
  208. // Record the keycode so we can simulate it later.
  209. autoshift_lastkey = keycode;
  210. autoshift_time = now;
  211. autoshift_flags.in_progress = true;
  212. #if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  213. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  214. #endif
  215. return false;
  216. }
  217. /** \brief Registers an autoshiftable key under the right conditions
  218. *
  219. * If autoshift_timeout has elapsed, register a shift and the key.
  220. *
  221. * If the Auto Shift key is released before the delay has elapsed, register the
  222. * key without a shift.
  223. *
  224. * Called on key down with keycode=KC_NO, auto-shifted key up, and timeout.
  225. */
  226. static void autoshift_end(uint16_t keycode, uint16_t now, bool matrix_trigger, keyrecord_t *record) {
  227. if (autoshift_flags.in_progress && (keycode == autoshift_lastkey || keycode == KC_NO)) {
  228. // Process the auto-shiftable key.
  229. autoshift_flags.in_progress = false;
  230. // clang-format off
  231. autoshift_flags.lastshifted =
  232. autoshift_flags.lastshifted
  233. || TIMER_DIFF_16(now, autoshift_time) >=
  234. #ifdef AUTO_SHIFT_TIMEOUT_PER_KEY
  235. get_autoshift_timeout(autoshift_lastkey, record)
  236. #else
  237. autoshift_timeout
  238. #endif
  239. ;
  240. // clang-format on
  241. set_autoshift_shift_state(autoshift_lastkey, autoshift_flags.lastshifted);
  242. if (get_mods() & MOD_BIT(KC_LSFT)) {
  243. autoshift_flags.cancelling_lshift = true;
  244. del_mods(MOD_BIT(KC_LSFT));
  245. }
  246. if (get_mods() & MOD_BIT(KC_RSFT)) {
  247. autoshift_flags.cancelling_rshift = true;
  248. del_mods(MOD_BIT(KC_RSFT));
  249. }
  250. autoshift_press_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
  251. // clang-format off
  252. #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))
  253. if (matrix_trigger
  254. # ifdef AUTO_SHIFT_REPEAT_PER_KEY
  255. && get_auto_shift_repeat(autoshift_lastkey, record)
  256. # endif
  257. # ifdef AUTO_SHIFT_NO_AUTO_REPEAT_PER_KEY
  258. && !get_auto_shift_no_auto_repeat(autoshift_lastkey, record)
  259. # endif
  260. ) {
  261. // Prevents release.
  262. return;
  263. }
  264. #endif
  265. // clang-format on
  266. #if TAP_CODE_DELAY > 0
  267. wait_ms(TAP_CODE_DELAY);
  268. #endif
  269. autoshift_release_user(autoshift_lastkey, autoshift_flags.lastshifted, record);
  270. autoshift_flush_shift();
  271. } else {
  272. // Release after keyrepeat.
  273. autoshift_release_user(keycode, get_autoshift_shift_state(keycode), record);
  274. if (keycode == autoshift_lastkey) {
  275. // This will only fire when the key was the last auto-shiftable
  276. // pressed. That prevents 'aaaaBBBB' then releasing a from unshifting
  277. // later 'B's (if 'B' wasn't auto-shiftable).
  278. autoshift_flush_shift();
  279. }
  280. }
  281. // Roll the autoshift_time forward for detecting tap-and-hold.
  282. autoshift_time = now;
  283. }
  284. /** \brief Simulates auto-shifted key releases when timeout is hit
  285. *
  286. * Can be called from \c matrix_scan_user so that auto-shifted keys are sent
  287. * immediately after the timeout has expired, rather than waiting for the key
  288. * to be released.
  289. */
  290. void autoshift_matrix_scan(void) {
  291. if (autoshift_flags.in_progress) {
  292. const uint16_t now = timer_read();
  293. if (TIMER_DIFF_16(now, autoshift_time) >=
  294. #ifdef AUTO_SHIFT_TIMEOUT_PER_KEY
  295. get_autoshift_timeout(autoshift_lastkey, &autoshift_lastrecord)
  296. #else
  297. autoshift_timeout
  298. #endif
  299. ) {
  300. autoshift_end(autoshift_lastkey, now, true, &autoshift_lastrecord);
  301. }
  302. }
  303. }
  304. void autoshift_toggle(void) {
  305. autoshift_flags.enabled = !autoshift_flags.enabled;
  306. autoshift_flush_shift();
  307. }
  308. void autoshift_enable(void) {
  309. autoshift_flags.enabled = true;
  310. }
  311. void autoshift_disable(void) {
  312. autoshift_flags.enabled = false;
  313. autoshift_flush_shift();
  314. }
  315. #ifndef AUTO_SHIFT_NO_SETUP
  316. void autoshift_timer_report(void) {
  317. # ifdef SEND_STRING_ENABLE
  318. const char *autoshift_timeout_str = get_u16_str(autoshift_timeout, ' ');
  319. // Skip padding spaces
  320. while (*autoshift_timeout_str == ' ') {
  321. autoshift_timeout_str++;
  322. }
  323. send_string(autoshift_timeout_str);
  324. # endif
  325. }
  326. #endif
  327. bool get_autoshift_state(void) {
  328. return autoshift_flags.enabled;
  329. }
  330. uint16_t get_generic_autoshift_timeout(void) {
  331. return autoshift_timeout;
  332. }
  333. __attribute__((weak)) uint16_t get_autoshift_timeout(uint16_t keycode, keyrecord_t *record) {
  334. return autoshift_timeout;
  335. }
  336. void set_autoshift_timeout(uint16_t timeout) {
  337. autoshift_timeout = timeout;
  338. }
  339. bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
  340. // Note that record->event.time isn't reliable, see:
  341. // https://github.com/qmk/qmk_firmware/pull/9826#issuecomment-733559550
  342. // clang-format off
  343. const uint16_t now =
  344. #if !defined(RETRO_SHIFT) || defined(NO_ACTION_TAPPING)
  345. timer_read()
  346. #else
  347. (record->event.pressed) ? retroshift_time : timer_read()
  348. #endif
  349. ;
  350. // clang-format on
  351. if (record->event.pressed) {
  352. if (autoshift_flags.in_progress) {
  353. // Evaluate previous key if there is one.
  354. autoshift_end(KC_NO, now, false, &autoshift_lastrecord);
  355. }
  356. switch (keycode) {
  357. case AS_TOGG:
  358. autoshift_toggle();
  359. break;
  360. case AS_ON:
  361. autoshift_enable();
  362. break;
  363. case AS_OFF:
  364. autoshift_disable();
  365. break;
  366. #ifndef AUTO_SHIFT_NO_SETUP
  367. case AS_UP:
  368. autoshift_timeout += 5;
  369. break;
  370. case AS_DOWN:
  371. autoshift_timeout -= 5;
  372. break;
  373. case AS_RPT:
  374. autoshift_timer_report();
  375. break;
  376. #endif
  377. }
  378. // If Retro Shift is disabled, possible custom actions shouldn't happen.
  379. // clang-format off
  380. #if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  381. # ifdef HOLD_ON_OTHER_KEY_PRESS
  382. const bool is_hold_on_interrupt = (IS_QK_MOD_TAP(keycode)
  383. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  384. && get_hold_on_other_key_press(keycode, record)
  385. # endif
  386. );
  387. # else
  388. const bool is_hold_on_interrupt = false;
  389. # endif
  390. #endif
  391. if (IS_RETRO(keycode)
  392. #if defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  393. // Not tapped or #defines mean that rolls should use hold action.
  394. && (
  395. record->tap.count == 0
  396. # ifdef RETRO_TAPPING_PER_KEY
  397. || !get_retro_tapping(keycode, record)
  398. # endif
  399. || (record->tap.interrupted && is_hold_on_interrupt))
  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. #ifdef HOLD_ON_OTHER_KEY_PRESS
  427. if (autoshift_flags.in_progress
  428. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  429. && get_hold_on_other_key_press(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(void) {
  465. if (autoshift_flags.in_progress) {
  466. autoshift_time = last_retroshift_time;
  467. }
  468. }
  469. #endif