process_auto_shift.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 process_auto_shift(uint16_t keycode, keyrecord_t *record) {
  55. static uint8_t any_mod_pressed;
  56. if (record->event.pressed) {
  57. switch (keycode) {
  58. case KC_ASUP:
  59. autoshift_timeout += 5;
  60. return false;
  61. case KC_ASDN:
  62. autoshift_timeout -= 5;
  63. return false;
  64. case KC_ASRP:
  65. autoshift_timer_report();
  66. return false;
  67. #ifndef NO_AUTO_SHIFT_ALPHA
  68. case KC_A:
  69. case KC_B:
  70. case KC_C:
  71. case KC_D:
  72. case KC_E:
  73. case KC_F:
  74. case KC_G:
  75. case KC_H:
  76. case KC_I:
  77. case KC_J:
  78. case KC_K:
  79. case KC_L:
  80. case KC_M:
  81. case KC_N:
  82. case KC_O:
  83. case KC_P:
  84. case KC_Q:
  85. case KC_R:
  86. case KC_S:
  87. case KC_T:
  88. case KC_U:
  89. case KC_V:
  90. case KC_W:
  91. case KC_X:
  92. case KC_Y:
  93. case KC_Z:
  94. #endif
  95. #ifndef NO_AUTO_SHIFT_NUMERIC
  96. case KC_1:
  97. case KC_2:
  98. case KC_3:
  99. case KC_4:
  100. case KC_5:
  101. case KC_6:
  102. case KC_7:
  103. case KC_8:
  104. case KC_9:
  105. case KC_0:
  106. #endif
  107. #ifndef NO_AUTO_SHIFT_SPECIAL
  108. case KC_TILD:
  109. case KC_MINUS:
  110. case KC_EQL:
  111. case KC_TAB:
  112. case KC_LBRC:
  113. case KC_RBRC:
  114. case KC_BSLS:
  115. case KC_SCLN:
  116. case KC_QUOT:
  117. case KC_COMM:
  118. case KC_DOT:
  119. case KC_SLSH:
  120. #endif
  121. autoshift_flush();
  122. any_mod_pressed = get_mods() & (
  123. MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)|
  124. MOD_BIT(KC_LALT)|MOD_BIT(KC_RALT)|
  125. MOD_BIT(KC_LCTL)|MOD_BIT(KC_RCTL)|
  126. MOD_BIT(KC_LSFT)|MOD_BIT(KC_RSFT)
  127. );
  128. if (any_mod_pressed) {
  129. return true;
  130. }
  131. autoshift_on(keycode);
  132. return false;
  133. default:
  134. autoshift_flush();
  135. return true;
  136. }
  137. } else {
  138. autoshift_flush();
  139. }
  140. return true;
  141. }
  142. #endif