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