diff options
Diffstat (limited to 'tmk_core')
-rw-r--r-- | tmk_core/common.mk | 1 | ||||
-rw-r--r-- | tmk_core/common/action_util.c | 176 | ||||
-rw-r--r-- | tmk_core/common/action_util.h | 2 | ||||
-rw-r--r-- | tmk_core/common/report.c | 177 | ||||
-rw-r--r-- | tmk_core/common/report.h | 10 |
5 files changed, 193 insertions, 173 deletions
diff --git a/tmk_core/common.mk b/tmk_core/common.mk index 4f80e0fa9..75b810d98 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk | |||
@@ -18,6 +18,7 @@ TMK_COMMON_SRC += $(COMMON_DIR)/host.c \ | |||
18 | $(COMMON_DIR)/debug.c \ | 18 | $(COMMON_DIR)/debug.c \ |
19 | $(COMMON_DIR)/util.c \ | 19 | $(COMMON_DIR)/util.c \ |
20 | $(COMMON_DIR)/eeconfig.c \ | 20 | $(COMMON_DIR)/eeconfig.c \ |
21 | $(COMMON_DIR)/report.c \ | ||
21 | $(PLATFORM_COMMON_DIR)/suspend.c \ | 22 | $(PLATFORM_COMMON_DIR)/suspend.c \ |
22 | $(PLATFORM_COMMON_DIR)/timer.c \ | 23 | $(PLATFORM_COMMON_DIR)/timer.c \ |
23 | $(PLATFORM_COMMON_DIR)/bootloader.c \ | 24 | $(PLATFORM_COMMON_DIR)/bootloader.c \ |
diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c index 77848c092..511649676 100644 --- a/tmk_core/common/action_util.c +++ b/tmk_core/common/action_util.c | |||
@@ -25,13 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
25 | extern keymap_config_t keymap_config; | 25 | extern keymap_config_t keymap_config; |
26 | 26 | ||
27 | 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; | 28 | static uint8_t real_mods = 0; |
36 | static uint8_t weak_mods = 0; | 29 | static uint8_t weak_mods = 0; |
37 | static uint8_t macro_mods = 0; | 30 | static uint8_t macro_mods = 0; |
@@ -134,7 +127,7 @@ void send_keyboard_report(void) { | |||
134 | } | 127 | } |
135 | #endif | 128 | #endif |
136 | keyboard_report->mods |= oneshot_mods; | 129 | keyboard_report->mods |= oneshot_mods; |
137 | if (has_anykey()) { | 130 | if (has_anykey(keyboard_report)) { |
138 | clear_oneshot_mods(); | 131 | clear_oneshot_mods(); |
139 | } | 132 | } |
140 | } | 133 | } |
@@ -148,22 +141,22 @@ void add_key(uint8_t key) | |||
148 | { | 141 | { |
149 | #ifdef NKRO_ENABLE | 142 | #ifdef NKRO_ENABLE |
150 | if (keyboard_protocol && keymap_config.nkro) { | 143 | if (keyboard_protocol && keymap_config.nkro) { |
151 | add_key_bit(key); | 144 | add_key_bit(keyboard_report, key); |
152 | return; | 145 | return; |
153 | } | 146 | } |
154 | #endif | 147 | #endif |
155 | add_key_byte(key); | 148 | add_key_byte(keyboard_report, key); |
156 | } | 149 | } |
157 | 150 | ||
158 | void del_key(uint8_t key) | 151 | void del_key(uint8_t key) |
159 | { | 152 | { |
160 | #ifdef NKRO_ENABLE | 153 | #ifdef NKRO_ENABLE |
161 | if (keyboard_protocol && keymap_config.nkro) { | 154 | if (keyboard_protocol && keymap_config.nkro) { |
162 | del_key_bit(key); | 155 | del_key_bit(keyboard_report, key); |
163 | return; | 156 | return; |
164 | } | 157 | } |
165 | #endif | 158 | #endif |
166 | del_key_byte(key); | 159 | del_key_byte(keyboard_report, key); |
167 | } | 160 | } |
168 | 161 | ||
169 | void clear_keys(void) | 162 | void clear_keys(void) |
@@ -221,166 +214,7 @@ uint8_t get_oneshot_mods(void) | |||
221 | /* | 214 | /* |
222 | * inspect keyboard state | 215 | * inspect keyboard state |
223 | */ | 216 | */ |
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) | 217 | uint8_t has_anymod(void) |
235 | { | 218 | { |
236 | return bitpop(real_mods); | 219 | return bitpop(real_mods); |
237 | } | 220 | } |
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 | ||
diff --git a/tmk_core/common/action_util.h b/tmk_core/common/action_util.h index dd0c4c2bf..8fb88c0f6 100644 --- a/tmk_core/common/action_util.h +++ b/tmk_core/common/action_util.h | |||
@@ -82,9 +82,7 @@ uint8_t get_oneshot_layer_state(void); | |||
82 | bool has_oneshot_layer_timed_out(void); | 82 | bool has_oneshot_layer_timed_out(void); |
83 | 83 | ||
84 | /* inspect */ | 84 | /* inspect */ |
85 | uint8_t has_anykey(void); | ||
86 | uint8_t has_anymod(void); | 85 | uint8_t has_anymod(void); |
87 | uint8_t get_first_key(void); | ||
88 | 86 | ||
89 | #ifdef __cplusplus | 87 | #ifdef __cplusplus |
90 | } | 88 | } |
diff --git a/tmk_core/common/report.c b/tmk_core/common/report.c new file mode 100644 index 000000000..0a67b2534 --- /dev/null +++ b/tmk_core/common/report.c | |||
@@ -0,0 +1,177 @@ | |||
1 | /* Copyright 2017 Fred Sundvik | ||
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 | #include "report.h" | ||
18 | #include "host.h" | ||
19 | #include "keycode_config.h" | ||
20 | #include "debug.h" | ||
21 | #include "util.h" | ||
22 | |||
23 | uint8_t has_anykey(report_keyboard_t* keyboard_report) | ||
24 | { | ||
25 | uint8_t cnt = 0; | ||
26 | for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) { | ||
27 | if (keyboard_report->raw[i]) | ||
28 | cnt++; | ||
29 | } | ||
30 | return cnt; | ||
31 | } | ||
32 | |||
33 | uint8_t get_first_key(report_keyboard_t* keyboard_report) | ||
34 | { | ||
35 | #ifdef NKRO_ENABLE | ||
36 | if (keyboard_protocol && keymap_config.nkro) { | ||
37 | uint8_t i = 0; | ||
38 | for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) | ||
39 | ; | ||
40 | return i<<3 | biton(keyboard_report->nkro.bits[i]); | ||
41 | } | ||
42 | #endif | ||
43 | #ifdef USB_6KRO_ENABLE | ||
44 | uint8_t i = cb_head; | ||
45 | do { | ||
46 | if (keyboard_report->keys[i] != 0) { | ||
47 | break; | ||
48 | } | ||
49 | i = RO_INC(i); | ||
50 | } while (i != cb_tail); | ||
51 | return keyboard_report->keys[i]; | ||
52 | #else | ||
53 | return keyboard_report->keys[0]; | ||
54 | #endif | ||
55 | } | ||
56 | |||
57 | void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) | ||
58 | { | ||
59 | #ifdef USB_6KRO_ENABLE | ||
60 | int8_t i = cb_head; | ||
61 | int8_t empty = -1; | ||
62 | if (cb_count) { | ||
63 | do { | ||
64 | if (keyboard_report->keys[i] == code) { | ||
65 | return; | ||
66 | } | ||
67 | if (empty == -1 && keyboard_report->keys[i] == 0) { | ||
68 | empty = i; | ||
69 | } | ||
70 | i = RO_INC(i); | ||
71 | } while (i != cb_tail); | ||
72 | if (i == cb_tail) { | ||
73 | if (cb_tail == cb_head) { | ||
74 | // buffer is full | ||
75 | if (empty == -1) { | ||
76 | // pop head when has no empty space | ||
77 | cb_head = RO_INC(cb_head); | ||
78 | cb_count--; | ||
79 | } | ||
80 | else { | ||
81 | // left shift when has empty space | ||
82 | uint8_t offset = 1; | ||
83 | i = RO_INC(empty); | ||
84 | do { | ||
85 | if (keyboard_report->keys[i] != 0) { | ||
86 | keyboard_report->keys[empty] = keyboard_report->keys[i]; | ||
87 | keyboard_report->keys[i] = 0; | ||
88 | empty = RO_INC(empty); | ||
89 | } | ||
90 | else { | ||
91 | offset++; | ||
92 | } | ||
93 | i = RO_INC(i); | ||
94 | } while (i != cb_tail); | ||
95 | cb_tail = RO_SUB(cb_tail, offset); | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | // add to tail | ||
101 | keyboard_report->keys[cb_tail] = code; | ||
102 | cb_tail = RO_INC(cb_tail); | ||
103 | cb_count++; | ||
104 | #else | ||
105 | int8_t i = 0; | ||
106 | int8_t empty = -1; | ||
107 | for (; i < KEYBOARD_REPORT_KEYS; i++) { | ||
108 | if (keyboard_report->keys[i] == code) { | ||
109 | break; | ||
110 | } | ||
111 | if (empty == -1 && keyboard_report->keys[i] == 0) { | ||
112 | empty = i; | ||
113 | } | ||
114 | } | ||
115 | if (i == KEYBOARD_REPORT_KEYS) { | ||
116 | if (empty != -1) { | ||
117 | keyboard_report->keys[empty] = code; | ||
118 | } | ||
119 | } | ||
120 | #endif | ||
121 | } | ||
122 | |||
123 | void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) | ||
124 | { | ||
125 | #ifdef USB_6KRO_ENABLE | ||
126 | uint8_t i = cb_head; | ||
127 | if (cb_count) { | ||
128 | do { | ||
129 | if (keyboard_report->keys[i] == code) { | ||
130 | keyboard_report->keys[i] = 0; | ||
131 | cb_count--; | ||
132 | if (cb_count == 0) { | ||
133 | // reset head and tail | ||
134 | cb_tail = cb_head = 0; | ||
135 | } | ||
136 | if (i == RO_DEC(cb_tail)) { | ||
137 | // left shift when next to tail | ||
138 | do { | ||
139 | cb_tail = RO_DEC(cb_tail); | ||
140 | if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) { | ||
141 | break; | ||
142 | } | ||
143 | } while (cb_tail != cb_head); | ||
144 | } | ||
145 | break; | ||
146 | } | ||
147 | i = RO_INC(i); | ||
148 | } while (i != cb_tail); | ||
149 | } | ||
150 | #else | ||
151 | for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { | ||
152 | if (keyboard_report->keys[i] == code) { | ||
153 | keyboard_report->keys[i] = 0; | ||
154 | } | ||
155 | } | ||
156 | #endif | ||
157 | } | ||
158 | |||
159 | #ifdef NKRO_ENABLE | ||
160 | void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) | ||
161 | { | ||
162 | if ((code>>3) < KEYBOARD_REPORT_BITS) { | ||
163 | keyboard_report->nkro.bits[code>>3] |= 1<<(code&7); | ||
164 | } else { | ||
165 | dprintf("add_key_bit: can't add: %02X\n", code); | ||
166 | } | ||
167 | } | ||
168 | |||
169 | void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) | ||
170 | { | ||
171 | if ((code>>3) < KEYBOARD_REPORT_BITS) { | ||
172 | keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7)); | ||
173 | } else { | ||
174 | dprintf("del_key_bit: can't del: %02X\n", code); | ||
175 | } | ||
176 | } | ||
177 | #endif | ||
diff --git a/tmk_core/common/report.h b/tmk_core/common/report.h index 8fb28b6ce..30ce80542 100644 --- a/tmk_core/common/report.h +++ b/tmk_core/common/report.h | |||
@@ -174,6 +174,16 @@ typedef struct { | |||
174 | (key == KC_WWW_REFRESH ? AC_REFRESH : \ | 174 | (key == KC_WWW_REFRESH ? AC_REFRESH : \ |
175 | (key == KC_WWW_FAVORITES ? AC_BOOKMARKS : 0))))))))))))))))))))) | 175 | (key == KC_WWW_FAVORITES ? AC_BOOKMARKS : 0))))))))))))))))))))) |
176 | 176 | ||
177 | uint8_t has_anykey(report_keyboard_t* keyboard_report); | ||
178 | uint8_t get_first_key(report_keyboard_t* keyboard_report); | ||
179 | |||
180 | void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code); | ||
181 | void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code); | ||
182 | #ifdef NKRO_ENABLE | ||
183 | void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code); | ||
184 | void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code); | ||
185 | #endif | ||
186 | |||
177 | #ifdef __cplusplus | 187 | #ifdef __cplusplus |
178 | } | 188 | } |
179 | #endif | 189 | #endif |