]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_util.c
77848c0923848ffa69896221c62c461d79b858f6
[qmk_firmware.git] / tmk_core / common / action_util.c
1 /*
2 Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "host.h"
18 #include "report.h"
19 #include "debug.h"
20 #include "action_util.h"
21 #include "action_layer.h"
22 #include "timer.h"
23 #include "keycode_config.h"
24
25 extern keymap_config_t keymap_config;
26
27
28 static inline void add_key_byte(uint8_t code);
29 static inline void del_key_byte(uint8_t code);
30 #ifdef NKRO_ENABLE
31 static inline void add_key_bit(uint8_t code);
32 static inline void del_key_bit(uint8_t code);
33 #endif
34
35 static uint8_t real_mods = 0;
36 static uint8_t weak_mods = 0;
37 static uint8_t macro_mods = 0;
38
39 #ifdef USB_6KRO_ENABLE
40 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
41 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
42 #define RO_INC(a) RO_ADD(a, 1)
43 #define RO_DEC(a) RO_SUB(a, 1)
44 static int8_t cb_head = 0;
45 static int8_t cb_tail = 0;
46 static int8_t cb_count = 0;
47 #endif
48
49 // TODO: pointer variable is not needed
50 //report_keyboard_t keyboard_report = {};
51 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
52
53 #ifndef NO_ACTION_ONESHOT
54 static int8_t oneshot_mods = 0;
55 static int8_t oneshot_locked_mods = 0;
56 int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
57 void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
58 void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
59 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
60 static int16_t oneshot_time = 0;
61 bool has_oneshot_mods_timed_out(void) {
62   return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
63 }
64 #else
65 bool has_oneshot_mods_timed_out(void) {
66     return false;
67 }
68 #endif
69 #endif
70
71 /* oneshot layer */
72 #ifndef NO_ACTION_ONESHOT
73 /* oneshot_layer_data bits
74 * LLLL LSSS
75 * where:
76 *   L => are layer bits
77 *   S => oneshot state bits
78 */
79 static int8_t oneshot_layer_data = 0;
80
81 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
82 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
83
84 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
85 static int16_t oneshot_layer_time = 0;
86 inline bool has_oneshot_layer_timed_out() {
87     return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
88         !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
89 }
90 #endif
91
92 /* Oneshot layer */
93 void set_oneshot_layer(uint8_t layer, uint8_t state)
94 {
95     oneshot_layer_data = layer << 3 | state;
96     layer_on(layer);
97 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
98     oneshot_layer_time = timer_read();
99 #endif
100 }
101 void reset_oneshot_layer(void) {
102     oneshot_layer_data = 0;
103 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
104     oneshot_layer_time = 0;
105 #endif
106 }
107 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
108 {
109     uint8_t start_state = oneshot_layer_data;
110     oneshot_layer_data &= ~state;
111     if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
112         layer_off(get_oneshot_layer());
113 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
114     oneshot_layer_time = 0;
115 #endif
116     }
117 }
118 bool is_oneshot_layer_active(void)
119 {
120     return get_oneshot_layer_state();
121 }
122 #endif
123
124 void send_keyboard_report(void) {
125     keyboard_report->mods  = real_mods;
126     keyboard_report->mods |= weak_mods;
127     keyboard_report->mods |= macro_mods;
128 #ifndef NO_ACTION_ONESHOT
129     if (oneshot_mods) {
130 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
131         if (has_oneshot_mods_timed_out()) {
132             dprintf("Oneshot: timeout\n");
133             clear_oneshot_mods();
134         }
135 #endif
136         keyboard_report->mods |= oneshot_mods;
137         if (has_anykey()) {
138             clear_oneshot_mods();
139         }
140     }
141
142 #endif
143     host_keyboard_send(keyboard_report);
144 }
145
146 /* key */
147 void add_key(uint8_t key)
148 {
149 #ifdef NKRO_ENABLE
150     if (keyboard_protocol && keymap_config.nkro) {
151         add_key_bit(key);
152         return;
153     }
154 #endif
155     add_key_byte(key);
156 }
157
158 void del_key(uint8_t key)
159 {
160 #ifdef NKRO_ENABLE
161     if (keyboard_protocol && keymap_config.nkro) {
162         del_key_bit(key);
163         return;
164     }
165 #endif
166     del_key_byte(key);
167 }
168
169 void clear_keys(void)
170 {
171     // not clear mods
172     for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
173         keyboard_report->raw[i] = 0;
174     }
175 }
176
177
178 /* modifier */
179 uint8_t get_mods(void) { return real_mods; }
180 void add_mods(uint8_t mods) { real_mods |= mods; }
181 void del_mods(uint8_t mods) { real_mods &= ~mods; }
182 void set_mods(uint8_t mods) { real_mods = mods; }
183 void clear_mods(void) { real_mods = 0; }
184
185 /* weak modifier */
186 uint8_t get_weak_mods(void) { return weak_mods; }
187 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
188 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
189 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
190 void clear_weak_mods(void) { weak_mods = 0; }
191
192 /* macro modifier */
193 uint8_t get_macro_mods(void) { return macro_mods; }
194 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
195 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
196 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
197 void clear_macro_mods(void) { macro_mods = 0; }
198
199 /* Oneshot modifier */
200 #ifndef NO_ACTION_ONESHOT
201 void set_oneshot_mods(uint8_t mods)
202 {
203     oneshot_mods = mods;
204 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
205     oneshot_time = timer_read();
206 #endif
207 }
208 void clear_oneshot_mods(void)
209 {
210     oneshot_mods = 0;
211 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
212     oneshot_time = 0;
213 #endif
214 }
215 uint8_t get_oneshot_mods(void)
216 {
217     return oneshot_mods;
218 }
219 #endif
220
221 /*
222  * inspect keyboard state
223  */
224 uint8_t has_anykey(void)
225 {
226     uint8_t cnt = 0;
227     for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
228         if (keyboard_report->raw[i])
229             cnt++;
230     }
231     return cnt;
232 }
233
234 uint8_t has_anymod(void)
235 {
236     return bitpop(real_mods);
237 }
238
239 uint8_t get_first_key(void)
240 {
241 #ifdef NKRO_ENABLE
242     if (keyboard_protocol && keymap_config.nkro) {
243         uint8_t i = 0;
244         for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
245             ;
246         return i<<3 | biton(keyboard_report->nkro.bits[i]);
247     }
248 #endif
249 #ifdef USB_6KRO_ENABLE
250     uint8_t i = cb_head;
251     do {
252         if (keyboard_report->keys[i] != 0) {
253             break;
254         }
255         i = RO_INC(i);
256     } while (i != cb_tail);
257     return keyboard_report->keys[i];
258 #else
259     return keyboard_report->keys[0];
260 #endif
261 }
262
263
264
265 /* local functions */
266 static inline void add_key_byte(uint8_t code)
267 {
268 #ifdef USB_6KRO_ENABLE
269     int8_t i = cb_head;
270     int8_t empty = -1;
271     if (cb_count) {
272         do {
273             if (keyboard_report->keys[i] == code) {
274                 return;
275             }
276             if (empty == -1 && keyboard_report->keys[i] == 0) {
277                 empty = i;
278             }
279             i = RO_INC(i);
280         } while (i != cb_tail);
281         if (i == cb_tail) {
282             if (cb_tail == cb_head) {
283                 // buffer is full
284                 if (empty == -1) {
285                     // pop head when has no empty space
286                     cb_head = RO_INC(cb_head);
287                     cb_count--;
288                 }
289                 else {
290                     // left shift when has empty space
291                     uint8_t offset = 1;
292                     i = RO_INC(empty);
293                     do {
294                         if (keyboard_report->keys[i] != 0) {
295                             keyboard_report->keys[empty] = keyboard_report->keys[i];
296                             keyboard_report->keys[i] = 0;
297                             empty = RO_INC(empty);
298                         }
299                         else {
300                             offset++;
301                         }
302                         i = RO_INC(i);
303                     } while (i != cb_tail);
304                     cb_tail = RO_SUB(cb_tail, offset);
305                 }
306             }
307         }
308     }
309     // add to tail
310     keyboard_report->keys[cb_tail] = code;
311     cb_tail = RO_INC(cb_tail);
312     cb_count++;
313 #else
314     int8_t i = 0;
315     int8_t empty = -1;
316     for (; i < KEYBOARD_REPORT_KEYS; i++) {
317         if (keyboard_report->keys[i] == code) {
318             break;
319         }
320         if (empty == -1 && keyboard_report->keys[i] == 0) {
321             empty = i;
322         }
323     }
324     if (i == KEYBOARD_REPORT_KEYS) {
325         if (empty != -1) {
326             keyboard_report->keys[empty] = code;
327         }
328     }
329 #endif
330 }
331
332 static inline void del_key_byte(uint8_t code)
333 {
334 #ifdef USB_6KRO_ENABLE
335     uint8_t i = cb_head;
336     if (cb_count) {
337         do {
338             if (keyboard_report->keys[i] == code) {
339                 keyboard_report->keys[i] = 0;
340                 cb_count--;
341                 if (cb_count == 0) {
342                     // reset head and tail
343                     cb_tail = cb_head = 0;
344                 }
345                 if (i == RO_DEC(cb_tail)) {
346                     // left shift when next to tail
347                     do {
348                         cb_tail = RO_DEC(cb_tail);
349                         if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
350                             break;
351                         }
352                     } while (cb_tail != cb_head);
353                 }
354                 break;
355             }
356             i = RO_INC(i);
357         } while (i != cb_tail);
358     }
359 #else
360     for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
361         if (keyboard_report->keys[i] == code) {
362             keyboard_report->keys[i] = 0;
363         }
364     }
365 #endif
366 }
367
368 #ifdef NKRO_ENABLE
369 static inline void add_key_bit(uint8_t code)
370 {
371     if ((code>>3) < KEYBOARD_REPORT_BITS) {
372         keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
373     } else {
374         dprintf("add_key_bit: can't add: %02X\n", code);
375     }
376 }
377
378 static inline void del_key_bit(uint8_t code)
379 {
380     if ((code>>3) < KEYBOARD_REPORT_BITS) {
381         keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
382     } else {
383         dprintf("del_key_bit: can't del: %02X\n", code);
384     }
385 }
386 #endif