process_auto_shift.c 3.8 KB

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