]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_util.c
Merge branch 'master' of https://github.com/jackhumbert/tmk_keyboard
[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 "timer.h"
22
23 static inline void add_key_byte(uint8_t code);
24 static inline void del_key_byte(uint8_t code);
25 #ifdef NKRO_ENABLE
26 static inline void add_key_bit(uint8_t code);
27 static inline void del_key_bit(uint8_t code);
28 #endif
29
30 static uint8_t real_mods = 0;
31 static uint8_t weak_mods = 0;
32 static uint8_t macro_mods = 0;
33
34 #ifdef USB_6KRO_ENABLE
35 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
36 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
37 #define RO_INC(a) RO_ADD(a, 1)
38 #define RO_DEC(a) RO_SUB(a, 1)
39 static int8_t cb_head = 0;
40 static int8_t cb_tail = 0;
41 static int8_t cb_count = 0;
42 #endif
43
44 // TODO: pointer variable is not needed
45 //report_keyboard_t keyboard_report = {};
46 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
47
48 #ifndef NO_ACTION_ONESHOT
49 static int8_t oneshot_mods = 0;
50 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
51 static int16_t oneshot_time = 0;
52 #endif
53 #endif
54
55
56 void send_keyboard_report(void) {
57     keyboard_report->mods  = real_mods;
58     keyboard_report->mods |= weak_mods;
59     keyboard_report->mods |= macro_mods;
60 #ifndef NO_ACTION_ONESHOT
61     if (oneshot_mods) {
62 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
63         if (TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT) {
64             dprintf("Oneshot: timeout\n");
65             clear_oneshot_mods();
66         }
67 #endif
68         keyboard_report->mods |= oneshot_mods;
69         if (has_anykey()) {
70             clear_oneshot_mods();
71         }
72     }
73 #endif
74     host_keyboard_send(keyboard_report);
75 }
76
77 /* key */
78 void add_key(uint8_t key)
79 {
80 #ifdef NKRO_ENABLE
81     if (keyboard_protocol && keyboard_nkro) {
82         add_key_bit(key);
83         return;
84     }
85 #endif
86     add_key_byte(key);
87 }
88
89 void del_key(uint8_t key)
90 {
91 #ifdef NKRO_ENABLE
92     if (keyboard_protocol && keyboard_nkro) {
93         del_key_bit(key);
94         return;
95     }
96 #endif
97     del_key_byte(key);
98 }
99
100 void clear_keys(void)
101 {
102     // not clear mods
103     for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
104         keyboard_report->raw[i] = 0;
105     }
106 }
107
108
109 /* modifier */
110 uint8_t get_mods(void) { return real_mods; }
111 void add_mods(uint8_t mods) { real_mods |= mods; }
112 void del_mods(uint8_t mods) { real_mods &= ~mods; }
113 void set_mods(uint8_t mods) { real_mods = mods; }
114 void clear_mods(void) { real_mods = 0; }
115
116 /* weak modifier */
117 uint8_t get_weak_mods(void) { return weak_mods; }
118 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
119 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
120 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
121 void clear_weak_mods(void) { weak_mods = 0; }
122
123 /* macro modifier */
124 uint8_t get_macro_mods(void) { return macro_mods; }
125 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
126 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
127 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
128 void clear_macro_mods(void) { macro_mods = 0; }
129
130 /* Oneshot modifier */
131 #ifndef NO_ACTION_ONESHOT
132 void set_oneshot_mods(uint8_t mods)
133 {
134     oneshot_mods = mods;
135 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
136     oneshot_time = timer_read();
137 #endif
138 }
139 void clear_oneshot_mods(void)
140 {
141     oneshot_mods = 0;
142 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
143     oneshot_time = 0;
144 #endif
145 }
146 #endif
147
148
149
150
151 /*
152  * inspect keyboard state
153  */
154 uint8_t has_anykey(void)
155 {
156     uint8_t cnt = 0;
157     for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
158         if (keyboard_report->raw[i])
159             cnt++;
160     }
161     return cnt;
162 }
163
164 uint8_t has_anymod(void)
165 {
166     return bitpop(real_mods);
167 }
168
169 uint8_t get_first_key(void)
170 {
171 #ifdef NKRO_ENABLE
172     if (keyboard_protocol && keyboard_nkro) {
173         uint8_t i = 0;
174         for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
175             ;
176         return i<<3 | biton(keyboard_report->nkro.bits[i]);
177     }
178 #endif
179 #ifdef USB_6KRO_ENABLE
180     uint8_t i = cb_head;
181     do {
182         if (keyboard_report->keys[i] != 0) {
183             break;
184         }
185         i = RO_INC(i);
186     } while (i != cb_tail);
187     return keyboard_report->keys[i];
188 #else
189     return keyboard_report->keys[0];
190 #endif
191 }
192
193
194
195 /* local functions */
196 static inline void add_key_byte(uint8_t code)
197 {
198 #ifdef USB_6KRO_ENABLE
199     int8_t i = cb_head;
200     int8_t empty = -1;
201     if (cb_count) {
202         do {
203             if (keyboard_report->keys[i] == code) {
204                 return;
205             }
206             if (empty == -1 && keyboard_report->keys[i] == 0) {
207                 empty = i;
208             }
209             i = RO_INC(i);
210         } while (i != cb_tail);
211         if (i == cb_tail) {
212             if (cb_tail == cb_head) {
213                 // buffer is full
214                 if (empty == -1) {
215                     // pop head when has no empty space
216                     cb_head = RO_INC(cb_head);
217                     cb_count--;
218                 }
219                 else {
220                     // left shift when has empty space
221                     uint8_t offset = 1;
222                     i = RO_INC(empty);
223                     do {
224                         if (keyboard_report->keys[i] != 0) {
225                             keyboard_report->keys[empty] = keyboard_report->keys[i];
226                             keyboard_report->keys[i] = 0;
227                             empty = RO_INC(empty);
228                         }
229                         else {
230                             offset++;
231                         }
232                         i = RO_INC(i);
233                     } while (i != cb_tail);
234                     cb_tail = RO_SUB(cb_tail, offset);
235                 }
236             }
237         }
238     }
239     // add to tail
240     keyboard_report->keys[cb_tail] = code;
241     cb_tail = RO_INC(cb_tail);
242     cb_count++;
243 #else
244     int8_t i = 0;
245     int8_t empty = -1;
246     for (; i < KEYBOARD_REPORT_KEYS; i++) {
247         if (keyboard_report->keys[i] == code) {
248             break;
249         }
250         if (empty == -1 && keyboard_report->keys[i] == 0) {
251             empty = i;
252         }
253     }
254     if (i == KEYBOARD_REPORT_KEYS) {
255         if (empty != -1) {
256             keyboard_report->keys[empty] = code;
257         }
258     }
259 #endif
260 }
261
262 static inline void del_key_byte(uint8_t code)
263 {
264 #ifdef USB_6KRO_ENABLE
265     uint8_t i = cb_head;
266     if (cb_count) {
267         do {
268             if (keyboard_report->keys[i] == code) {
269                 keyboard_report->keys[i] = 0;
270                 cb_count--;
271                 if (cb_count == 0) {
272                     // reset head and tail
273                     cb_tail = cb_head = 0;
274                 }
275                 if (i == RO_DEC(cb_tail)) {
276                     // left shift when next to tail
277                     do {
278                         cb_tail = RO_DEC(cb_tail);
279                         if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
280                             break;
281                         }
282                     } while (cb_tail != cb_head);
283                 }
284                 break;
285             }
286             i = RO_INC(i);
287         } while (i != cb_tail);
288     }
289 #else
290     for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
291         if (keyboard_report->keys[i] == code) {
292             keyboard_report->keys[i] = 0;
293         }
294     }
295 #endif
296 }
297
298 #ifdef NKRO_ENABLE
299 static inline void add_key_bit(uint8_t code)
300 {
301     if ((code>>3) < KEYBOARD_REPORT_BITS) {
302         keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
303     } else {
304         dprintf("add_key_bit: can't add: %02X\n", code);
305     }
306 }
307
308 static inline void del_key_bit(uint8_t code)
309 {
310     if ((code>>3) < KEYBOARD_REPORT_BITS) {
311         keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
312     } else {
313         dprintf("del_key_bit: can't del: %02X\n", code);
314     }
315 }
316 #endif