You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.7 KiB

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