]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_util.c
Merge branch 'master' into coderkun_neo2
[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 inline bool has_oneshot_mods_timed_out() {
62   return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
63 }
64 #endif
65 #endif
66
67 /* oneshot layer */
68 #ifndef NO_ACTION_ONESHOT
69 /* oneshot_layer_data bits
70 * LLLL LSSS
71 * where:
72 *   L => are layer bits
73 *   S => oneshot state bits
74 */
75 static int8_t oneshot_layer_data = 0;
76
77 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
78 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
79
80 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
81 static int16_t oneshot_layer_time = 0;
82 inline bool has_oneshot_layer_timed_out() {
83     return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
84         !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
85 }
86 #endif
87
88 /* Oneshot layer */
89 void set_oneshot_layer(uint8_t layer, uint8_t state)
90 {
91     oneshot_layer_data = layer << 3 | state;
92     layer_on(layer);
93 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
94     oneshot_layer_time = timer_read();
95 #endif
96 }
97 void reset_oneshot_layer(void) {
98     oneshot_layer_data = 0;
99 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
100     oneshot_layer_time = 0;
101 #endif
102 }
103 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
104 {
105     uint8_t start_state = oneshot_layer_data;
106     oneshot_layer_data &= ~state;
107     if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
108         layer_off(get_oneshot_layer());
109 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
110     oneshot_layer_time = 0;
111 #endif
112     }
113 }
114 bool is_oneshot_layer_active(void)
115 {
116     return get_oneshot_layer_state();
117 }
118 #endif
119
120 void send_keyboard_report(void) {
121     keyboard_report->mods  = real_mods;
122     keyboard_report->mods |= weak_mods;
123     keyboard_report->mods |= macro_mods;
124 #ifndef NO_ACTION_ONESHOT
125     if (oneshot_mods) {
126 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
127         if (has_oneshot_mods_timed_out()) {
128             dprintf("Oneshot: timeout\n");
129             clear_oneshot_mods();
130         }
131 #endif
132         keyboard_report->mods |= oneshot_mods;
133         if (has_anykey()) {
134             clear_oneshot_mods();
135         }
136     }
137
138 #endif
139     host_keyboard_send(keyboard_report);
140 }
141
142 /* key */
143 void add_key(uint8_t key)
144 {
145 #ifdef NKRO_ENABLE
146     if (keyboard_protocol && keymap_config.nkro) {
147         add_key_bit(key);
148         return;
149     }
150 #endif
151     add_key_byte(key);
152 }
153
154 void del_key(uint8_t key)
155 {
156 #ifdef NKRO_ENABLE
157     if (keyboard_protocol && keymap_config.nkro) {
158         del_key_bit(key);
159         return;
160     }
161 #endif
162     del_key_byte(key);
163 }
164
165 void clear_keys(void)
166 {
167     // not clear mods
168     for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
169         keyboard_report->raw[i] = 0;
170     }
171 }
172
173
174 /* modifier */
175 uint8_t get_mods(void) { return real_mods; }
176 void add_mods(uint8_t mods) { real_mods |= mods; }
177 void del_mods(uint8_t mods) { real_mods &= ~mods; }
178 void set_mods(uint8_t mods) { real_mods = mods; }
179 void clear_mods(void) { real_mods = 0; }
180
181 /* weak modifier */
182 uint8_t get_weak_mods(void) { return weak_mods; }
183 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
184 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
185 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
186 void clear_weak_mods(void) { weak_mods = 0; }
187
188 /* macro modifier */
189 uint8_t get_macro_mods(void) { return macro_mods; }
190 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
191 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
192 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
193 void clear_macro_mods(void) { macro_mods = 0; }
194
195 /* Oneshot modifier */
196 #ifndef NO_ACTION_ONESHOT
197 void set_oneshot_mods(uint8_t mods)
198 {
199     oneshot_mods = mods;
200 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
201     oneshot_time = timer_read();
202 #endif
203 }
204 void clear_oneshot_mods(void)
205 {
206     oneshot_mods = 0;
207 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
208     oneshot_time = 0;
209 #endif
210 }
211 uint8_t get_oneshot_mods(void)
212 {
213     return oneshot_mods;
214 }
215 #endif
216
217 /*
218  * inspect keyboard state
219  */
220 uint8_t has_anykey(void)
221 {
222     uint8_t cnt = 0;
223     for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
224         if (keyboard_report->raw[i])
225             cnt++;
226     }
227     return cnt;
228 }
229
230 uint8_t has_anymod(void)
231 {
232     return bitpop(real_mods);
233 }
234
235 uint8_t get_first_key(void)
236 {
237 #ifdef NKRO_ENABLE
238     if (keyboard_protocol && keymap_config.nkro) {
239         uint8_t i = 0;
240         for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
241             ;
242         return i<<3 | biton(keyboard_report->nkro.bits[i]);
243     }
244 #endif
245 #ifdef USB_6KRO_ENABLE
246     uint8_t i = cb_head;
247     do {
248         if (keyboard_report->keys[i] != 0) {
249             break;
250         }
251         i = RO_INC(i);
252     } while (i != cb_tail);
253     return keyboard_report->keys[i];
254 #else
255     return keyboard_report->keys[0];
256 #endif
257 }
258
259
260
261 /* local functions */
262 static inline void add_key_byte(uint8_t code)
263 {
264 #ifdef USB_6KRO_ENABLE
265     int8_t i = cb_head;
266     int8_t empty = -1;
267     if (cb_count) {
268         do {
269             if (keyboard_report->keys[i] == code) {
270                 return;
271             }
272             if (empty == -1 && keyboard_report->keys[i] == 0) {
273                 empty = i;
274             }
275             i = RO_INC(i);
276         } while (i != cb_tail);
277         if (i == cb_tail) {
278             if (cb_tail == cb_head) {
279                 // buffer is full
280                 if (empty == -1) {
281                     // pop head when has no empty space
282                     cb_head = RO_INC(cb_head);
283                     cb_count--;
284                 }
285                 else {
286                     // left shift when has empty space
287                     uint8_t offset = 1;
288                     i = RO_INC(empty);
289                     do {
290                         if (keyboard_report->keys[i] != 0) {
291                             keyboard_report->keys[empty] = keyboard_report->keys[i];
292                             keyboard_report->keys[i] = 0;
293                             empty = RO_INC(empty);
294                         }
295                         else {
296                             offset++;
297                         }
298                         i = RO_INC(i);
299                     } while (i != cb_tail);
300                     cb_tail = RO_SUB(cb_tail, offset);
301                 }
302             }
303         }
304     }
305     // add to tail
306     keyboard_report->keys[cb_tail] = code;
307     cb_tail = RO_INC(cb_tail);
308     cb_count++;
309 #else
310     int8_t i = 0;
311     int8_t empty = -1;
312     for (; i < KEYBOARD_REPORT_KEYS; i++) {
313         if (keyboard_report->keys[i] == code) {
314             break;
315         }
316         if (empty == -1 && keyboard_report->keys[i] == 0) {
317             empty = i;
318         }
319     }
320     if (i == KEYBOARD_REPORT_KEYS) {
321         if (empty != -1) {
322             keyboard_report->keys[empty] = code;
323         }
324     }
325 #endif
326 }
327
328 static inline void del_key_byte(uint8_t code)
329 {
330 #ifdef USB_6KRO_ENABLE
331     uint8_t i = cb_head;
332     if (cb_count) {
333         do {
334             if (keyboard_report->keys[i] == code) {
335                 keyboard_report->keys[i] = 0;
336                 cb_count--;
337                 if (cb_count == 0) {
338                     // reset head and tail
339                     cb_tail = cb_head = 0;
340                 }
341                 if (i == RO_DEC(cb_tail)) {
342                     // left shift when next to tail
343                     do {
344                         cb_tail = RO_DEC(cb_tail);
345                         if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
346                             break;
347                         }
348                     } while (cb_tail != cb_head);
349                 }
350                 break;
351             }
352             i = RO_INC(i);
353         } while (i != cb_tail);
354     }
355 #else
356     for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
357         if (keyboard_report->keys[i] == code) {
358             keyboard_report->keys[i] = 0;
359         }
360     }
361 #endif
362 }
363
364 #ifdef NKRO_ENABLE
365 static inline void add_key_bit(uint8_t code)
366 {
367     if ((code>>3) < KEYBOARD_REPORT_BITS) {
368         keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
369     } else {
370         dprintf("add_key_bit: can't add: %02X\n", code);
371     }
372 }
373
374 static inline void del_key_bit(uint8_t code)
375 {
376     if ((code>>3) < KEYBOARD_REPORT_BITS) {
377         keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
378     } else {
379         dprintf("del_key_bit: can't del: %02X\n", code);
380     }
381 }
382 #endif