process_auto_shift.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. static bool autoshift_enabled = true;
  20. static uint16_t autoshift_time = 0;
  21. static uint16_t autoshift_timeout = AUTO_SHIFT_TIMEOUT;
  22. static uint16_t autoshift_lastkey = KC_NO;
  23. void autoshift_flush(void) {
  24. if (autoshift_lastkey != KC_NO) {
  25. uint16_t elapsed = timer_elapsed(autoshift_time);
  26. if (elapsed > autoshift_timeout) {
  27. tap_code16(LSFT(autoshift_lastkey));
  28. } else {
  29. tap_code(autoshift_lastkey);
  30. }
  31. autoshift_time = 0;
  32. autoshift_lastkey = KC_NO;
  33. }
  34. }
  35. void autoshift_on(uint16_t keycode) {
  36. autoshift_time = timer_read();
  37. autoshift_lastkey = keycode;
  38. }
  39. void autoshift_toggle(void) {
  40. if (autoshift_enabled) {
  41. autoshift_enabled = false;
  42. autoshift_flush();
  43. } else {
  44. autoshift_enabled = true;
  45. }
  46. }
  47. void autoshift_enable(void) { autoshift_enabled = true; }
  48. void autoshift_disable(void) {
  49. autoshift_enabled = false;
  50. autoshift_flush();
  51. }
  52. # ifndef AUTO_SHIFT_NO_SETUP
  53. void autoshift_timer_report(void) {
  54. char display[8];
  55. snprintf(display, 8, "\n%d\n", autoshift_timeout);
  56. send_string((const char *)display);
  57. }
  58. # endif
  59. bool get_autoshift_state(void) { return autoshift_enabled; }
  60. uint16_t get_autoshift_timeout(void) { return autoshift_timeout; }
  61. void set_autoshift_timeout(uint16_t timeout) { autoshift_timeout = timeout; }
  62. bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
  63. if (record->event.pressed) {
  64. switch (keycode) {
  65. case KC_ASTG:
  66. autoshift_toggle();
  67. return true;
  68. case KC_ASON:
  69. autoshift_enable();
  70. return true;
  71. case KC_ASOFF:
  72. autoshift_disable();
  73. return true;
  74. # ifndef AUTO_SHIFT_NO_SETUP
  75. case KC_ASUP:
  76. autoshift_timeout += 5;
  77. return true;
  78. case KC_ASDN:
  79. autoshift_timeout -= 5;
  80. return true;
  81. case KC_ASRP:
  82. autoshift_timer_report();
  83. return true;
  84. # endif
  85. # ifndef NO_AUTO_SHIFT_ALPHA
  86. case KC_A ... KC_Z:
  87. # endif
  88. # ifndef NO_AUTO_SHIFT_NUMERIC
  89. case KC_1 ... KC_0:
  90. # endif
  91. # ifndef NO_AUTO_SHIFT_SPECIAL
  92. case KC_TAB:
  93. case KC_MINUS ... KC_SLASH:
  94. case KC_NONUS_BSLASH:
  95. # endif
  96. autoshift_flush();
  97. if (!autoshift_enabled) return true;
  98. # ifndef AUTO_SHIFT_MODIFIERS
  99. if (get_mods()) {
  100. return true;
  101. }
  102. # endif
  103. autoshift_on(keycode);
  104. // We need some extra handling here for OSL edge cases
  105. # if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
  106. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  107. # endif
  108. return false;
  109. default:
  110. autoshift_flush();
  111. return true;
  112. }
  113. } else {
  114. autoshift_flush();
  115. }
  116. return true;
  117. }
  118. #endif