process_auto_shift.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <stdio.h>
  18. #include "process_auto_shift.h"
  19. #define TAP(key) \
  20. register_code(key); \
  21. unregister_code(key)
  22. #define TAP_WITH_MOD(mod, key) \
  23. register_code(mod); \
  24. register_code(key); \
  25. unregister_code(key); \
  26. unregister_code(mod)
  27. uint16_t autoshift_time = 0;
  28. uint16_t autoshift_timeout = AUTO_SHIFT_TIMEOUT;
  29. uint16_t autoshift_lastkey = KC_NO;
  30. void autoshift_timer_report(void) {
  31. char display[8];
  32. snprintf(display, 8, "\n%d\n", autoshift_timeout);
  33. send_string((const char *)display);
  34. }
  35. void autoshift_on(uint16_t keycode) {
  36. autoshift_time = timer_read();
  37. autoshift_lastkey = keycode;
  38. }
  39. void autoshift_flush(void) {
  40. if (autoshift_lastkey != KC_NO) {
  41. uint16_t elapsed = timer_elapsed(autoshift_time);
  42. if (elapsed > autoshift_timeout) {
  43. register_code(KC_LSFT);
  44. }
  45. register_code(autoshift_lastkey);
  46. unregister_code(autoshift_lastkey);
  47. if (elapsed > autoshift_timeout) {
  48. unregister_code(KC_LSFT);
  49. }
  50. autoshift_time = 0;
  51. autoshift_lastkey = KC_NO;
  52. }
  53. }
  54. bool autoshift_enabled = true;
  55. void autoshift_enable(void) {
  56. autoshift_enabled = true;
  57. }
  58. void autoshift_disable(void) {
  59. autoshift_enabled = false;
  60. autoshift_flush();
  61. }
  62. void autoshift_toggle(void) {
  63. if (autoshift_enabled) {
  64. autoshift_enabled = false;
  65. autoshift_flush();
  66. }
  67. else {
  68. autoshift_enabled = true;
  69. }
  70. }
  71. bool autoshift_state(void) {
  72. return autoshift_enabled;
  73. }
  74. bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
  75. #ifndef AUTO_SHIFT_MODIFIERS
  76. static uint8_t any_mod_pressed;
  77. #endif
  78. if (record->event.pressed) {
  79. switch (keycode) {
  80. case KC_ASUP:
  81. autoshift_timeout += 5;
  82. return false;
  83. case KC_ASDN:
  84. autoshift_timeout -= 5;
  85. return false;
  86. case KC_ASRP:
  87. autoshift_timer_report();
  88. return false;
  89. case KC_ASTG:
  90. autoshift_toggle();
  91. return false;
  92. case KC_ASON:
  93. autoshift_enable();
  94. return false;
  95. case KC_ASOFF:
  96. autoshift_disable();
  97. return false;
  98. #ifndef NO_AUTO_SHIFT_ALPHA
  99. case KC_A:
  100. case KC_B:
  101. case KC_C:
  102. case KC_D:
  103. case KC_E:
  104. case KC_F:
  105. case KC_G:
  106. case KC_H:
  107. case KC_I:
  108. case KC_J:
  109. case KC_K:
  110. case KC_L:
  111. case KC_M:
  112. case KC_N:
  113. case KC_O:
  114. case KC_P:
  115. case KC_Q:
  116. case KC_R:
  117. case KC_S:
  118. case KC_T:
  119. case KC_U:
  120. case KC_V:
  121. case KC_W:
  122. case KC_X:
  123. case KC_Y:
  124. case KC_Z:
  125. #endif
  126. #ifndef NO_AUTO_SHIFT_NUMERIC
  127. case KC_1:
  128. case KC_2:
  129. case KC_3:
  130. case KC_4:
  131. case KC_5:
  132. case KC_6:
  133. case KC_7:
  134. case KC_8:
  135. case KC_9:
  136. case KC_0:
  137. #endif
  138. #ifndef NO_AUTO_SHIFT_SPECIAL
  139. case KC_MINUS:
  140. case KC_EQL:
  141. case KC_TAB:
  142. case KC_LBRC:
  143. case KC_RBRC:
  144. case KC_BSLS:
  145. case KC_SCLN:
  146. case KC_QUOT:
  147. case KC_COMM:
  148. case KC_DOT:
  149. case KC_SLSH:
  150. case KC_GRAVE:
  151. #endif
  152. autoshift_flush();
  153. if (!autoshift_enabled) return true;
  154. #ifndef AUTO_SHIFT_MODIFIERS
  155. any_mod_pressed = get_mods() & (
  156. MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)|
  157. MOD_BIT(KC_LALT)|MOD_BIT(KC_RALT)|
  158. MOD_BIT(KC_LCTL)|MOD_BIT(KC_RCTL)|
  159. MOD_BIT(KC_LSFT)|MOD_BIT(KC_RSFT)
  160. );
  161. if (any_mod_pressed) {
  162. return true;
  163. }
  164. #endif
  165. autoshift_on(keycode);
  166. return false;
  167. default:
  168. autoshift_flush();
  169. return true;
  170. }
  171. } else {
  172. autoshift_flush();
  173. }
  174. return true;
  175. }
  176. #endif