process_auto_shift.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. static uint16_t autoshift_time = 0;
  26. static uint16_t autoshift_timeout = AUTO_SHIFT_TIMEOUT;
  27. static uint16_t autoshift_lastkey = KC_NO;
  28. static struct {
  29. // Whether autoshift is enabled.
  30. bool enabled : 1;
  31. // Whether the last auto-shifted key was released after the timeout. This
  32. // is used to replicate the last key for a tap-then-hold.
  33. bool lastshifted : 1;
  34. // Whether an auto-shiftable key has been pressed but not processed.
  35. bool in_progress : 1;
  36. // Whether the auto-shifted keypress has been registered.
  37. bool holding_shift : 1;
  38. } autoshift_flags = {AUTO_SHIFT_STARTUP_STATE, false, false, false};
  39. /** \brief Record the press of an autoshiftable key
  40. *
  41. * \return Whether the record should be further processed.
  42. */
  43. static bool autoshift_press(uint16_t keycode, uint16_t now, keyrecord_t *record) {
  44. if (!autoshift_flags.enabled) {
  45. return true;
  46. }
  47. # ifndef AUTO_SHIFT_MODIFIERS
  48. if (get_mods()) {
  49. return true;
  50. }
  51. # endif
  52. # ifdef AUTO_SHIFT_REPEAT
  53. const uint16_t elapsed = TIMER_DIFF_16(now, autoshift_time);
  54. # ifndef AUTO_SHIFT_NO_AUTO_REPEAT
  55. if (!autoshift_flags.lastshifted) {
  56. # endif
  57. if (elapsed < TAPPING_TERM && keycode == autoshift_lastkey) {
  58. // Allow a tap-then-hold for keyrepeat.
  59. if (!autoshift_flags.lastshifted) {
  60. register_code(autoshift_lastkey);
  61. } else {
  62. // Simulate pressing the shift key.
  63. add_weak_mods(MOD_BIT(KC_LEFT_SHIFT));
  64. register_code(autoshift_lastkey);
  65. }
  66. return false;
  67. }
  68. # ifndef AUTO_SHIFT_NO_AUTO_REPEAT
  69. }
  70. # endif
  71. # endif
  72. // Record the keycode so we can simulate it later.
  73. autoshift_lastkey = keycode;
  74. autoshift_time = now;
  75. autoshift_flags.in_progress = true;
  76. # if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  77. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  78. # endif
  79. return false;
  80. }
  81. /** \brief Registers an autoshiftable key under the right conditions
  82. *
  83. * If the autoshift delay has elapsed, register a shift and the key.
  84. *
  85. * If the autoshift key is released before the delay has elapsed, register the
  86. * key without a shift.
  87. */
  88. static void autoshift_end(uint16_t keycode, uint16_t now, bool matrix_trigger) {
  89. // Called on key down with KC_NO, auto-shifted key up, and timeout.
  90. if (autoshift_flags.in_progress) {
  91. // Process the auto-shiftable key.
  92. autoshift_flags.in_progress = false;
  93. // Time since the initial press was recorded.
  94. const uint16_t elapsed = TIMER_DIFF_16(now, autoshift_time);
  95. if (elapsed < autoshift_timeout) {
  96. register_code(autoshift_lastkey);
  97. autoshift_flags.lastshifted = false;
  98. } else {
  99. // Simulate pressing the shift key.
  100. add_weak_mods(MOD_BIT(KC_LEFT_SHIFT));
  101. register_code(autoshift_lastkey);
  102. autoshift_flags.lastshifted = true;
  103. # if defined(AUTO_SHIFT_REPEAT) && !defined(AUTO_SHIFT_NO_AUTO_REPEAT)
  104. if (matrix_trigger) {
  105. // Prevents release.
  106. return;
  107. }
  108. # endif
  109. }
  110. # if TAP_CODE_DELAY > 0
  111. wait_ms(TAP_CODE_DELAY);
  112. # endif
  113. unregister_code(autoshift_lastkey);
  114. del_weak_mods(MOD_BIT(KC_LEFT_SHIFT));
  115. } else {
  116. // Release after keyrepeat.
  117. unregister_code(keycode);
  118. if (keycode == autoshift_lastkey) {
  119. // This will only fire when the key was the last auto-shiftable
  120. // pressed. That prevents aaaaBBBB then releasing a from unshifting
  121. // later Bs (if B wasn't auto-shiftable).
  122. del_weak_mods(MOD_BIT(KC_LEFT_SHIFT));
  123. }
  124. }
  125. send_keyboard_report(); // del_weak_mods doesn't send one.
  126. // Roll the autoshift_time forward for detecting tap-and-hold.
  127. autoshift_time = now;
  128. }
  129. /** \brief Simulates auto-shifted key releases when timeout is hit
  130. *
  131. * Can be called from \c matrix_scan_user so that auto-shifted keys are sent
  132. * immediately after the timeout has expired, rather than waiting for the key
  133. * to be released.
  134. */
  135. void autoshift_matrix_scan(void) {
  136. if (autoshift_flags.in_progress) {
  137. const uint16_t now = timer_read();
  138. const uint16_t elapsed = TIMER_DIFF_16(now, autoshift_time);
  139. if (elapsed >= autoshift_timeout) {
  140. autoshift_end(autoshift_lastkey, now, true);
  141. }
  142. }
  143. }
  144. void autoshift_toggle(void) {
  145. autoshift_flags.enabled = !autoshift_flags.enabled;
  146. del_weak_mods(MOD_BIT(KC_LEFT_SHIFT));
  147. }
  148. void autoshift_enable(void) { autoshift_flags.enabled = true; }
  149. void autoshift_disable(void) {
  150. autoshift_flags.enabled = false;
  151. del_weak_mods(MOD_BIT(KC_LEFT_SHIFT));
  152. }
  153. # ifndef AUTO_SHIFT_NO_SETUP
  154. void autoshift_timer_report(void) {
  155. char display[8];
  156. snprintf(display, 8, "\n%d\n", autoshift_timeout);
  157. send_string((const char *)display);
  158. }
  159. # endif
  160. bool get_autoshift_state(void) { return autoshift_flags.enabled; }
  161. uint16_t get_autoshift_timeout(void) { return autoshift_timeout; }
  162. void set_autoshift_timeout(uint16_t timeout) { autoshift_timeout = timeout; }
  163. bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
  164. // Note that record->event.time isn't reliable, see:
  165. // https://github.com/qmk/qmk_firmware/pull/9826#issuecomment-733559550
  166. const uint16_t now = timer_read();
  167. if (record->event.pressed) {
  168. if (autoshift_flags.in_progress) {
  169. // Evaluate previous key if there is one. Doing this elsewhere is
  170. // more complicated and easier to break.
  171. autoshift_end(KC_NO, now, false);
  172. }
  173. // For pressing another key while keyrepeating shifted autoshift.
  174. del_weak_mods(MOD_BIT(KC_LEFT_SHIFT));
  175. switch (keycode) {
  176. case KC_ASTG:
  177. autoshift_toggle();
  178. return true;
  179. case KC_ASON:
  180. autoshift_enable();
  181. return true;
  182. case KC_ASOFF:
  183. autoshift_disable();
  184. return true;
  185. # ifndef AUTO_SHIFT_NO_SETUP
  186. case KC_ASUP:
  187. autoshift_timeout += 5;
  188. return true;
  189. case KC_ASDN:
  190. autoshift_timeout -= 5;
  191. return true;
  192. case KC_ASRP:
  193. autoshift_timer_report();
  194. return true;
  195. # endif
  196. }
  197. }
  198. if (get_auto_shifted_key(keycode, record)) {
  199. if (record->event.pressed) {
  200. return autoshift_press(keycode, now, record);
  201. } else {
  202. autoshift_end(keycode, now, false);
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. __attribute__((weak)) bool get_auto_shifted_key(uint16_t keycode, keyrecord_t *record) {
  209. switch (keycode) {
  210. # ifndef NO_AUTO_SHIFT_ALPHA
  211. case KC_A ... KC_Z:
  212. # endif
  213. # ifndef NO_AUTO_SHIFT_NUMERIC
  214. case KC_1 ... KC_0:
  215. # endif
  216. # ifndef NO_AUTO_SHIFT_SPECIAL
  217. case KC_TAB:
  218. case KC_MINUS ... KC_SLASH:
  219. case KC_NONUS_BACKSLASH:
  220. # endif
  221. return true;
  222. }
  223. return false;
  224. }
  225. #endif