diff options
Diffstat (limited to 'tmk_core/common/action_util.c')
-rw-r--r-- | tmk_core/common/action_util.c | 176 |
1 files changed, 5 insertions, 171 deletions
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 | ||