aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-10-04 03:30:09 +0900
committertmk <nobody@nowhere>2013-10-04 16:04:31 +0900
commitd52d554360d3bf06189bfd4f386fa99348d8a0a8 (patch)
treeb05d27552402f371e61aa0f29906a60c87e213ad /common
parentcb434cfebc607db15b52a42adae6423bd40f1f98 (diff)
downloadqmk_firmware-d52d554360d3bf06189bfd4f386fa99348d8a0a8.tar.gz
qmk_firmware-d52d554360d3bf06189bfd4f386fa99348d8a0a8.zip
Fix mod stuck of MODS_KEY when leaving layer #62
- Add action_util.c and remove action_oneshot.c - Add oneshot_mods for MODS_ONESHOT - Add weak_mods for MODS_KEY and MACRO - weak_mods is cleared when layer switching
Diffstat (limited to 'common')
-rw-r--r--common/action.c138
-rw-r--r--common/action.h6
-rw-r--r--common/action_code.h6
-rw-r--r--common/action_oneshot.c21
-rw-r--r--common/action_oneshot.h52
-rw-r--r--common/action_util.c192
-rw-r--r--common/action_util.h56
-rw-r--r--common/command.c1
-rw-r--r--common/host.c150
-rw-r--r--common/host.h17
10 files changed, 323 insertions, 316 deletions
diff --git a/common/action.c b/common/action.c
index 59c6f252d..ecd5a7e94 100644
--- a/common/action.c
+++ b/common/action.c
@@ -23,8 +23,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
23#include "backlight.h" 23#include "backlight.h"
24#include "action_layer.h" 24#include "action_layer.h"
25#include "action_tapping.h" 25#include "action_tapping.h"
26#include "action_oneshot.h"
27#include "action_macro.h" 26#include "action_macro.h"
27#include "action_util.h"
28#include "action.h" 28#include "action.h"
29 29
30#ifdef DEBUG_ACTION 30#ifdef DEBUG_ACTION
@@ -79,15 +79,15 @@ void process_action(keyrecord_t *record)
79 action.key.mods<<4; 79 action.key.mods<<4;
80 if (event.pressed) { 80 if (event.pressed) {
81 if (mods) { 81 if (mods) {
82 host_add_mods(mods); 82 add_weak_mods(mods);
83 host_send_keyboard_report(); 83 send_keyboard_report();
84 } 84 }
85 register_code(action.key.code); 85 register_code(action.key.code);
86 } else { 86 } else {
87 unregister_code(action.key.code); 87 unregister_code(action.key.code);
88 if (mods) { 88 if (mods) {
89 host_del_mods(mods); 89 del_weak_mods(mods);
90 host_send_keyboard_report(); 90 send_keyboard_report();
91 } 91 }
92 } 92 }
93 } 93 }
@@ -105,11 +105,11 @@ void process_action(keyrecord_t *record)
105 if (event.pressed) { 105 if (event.pressed) {
106 if (tap_count == 0) { 106 if (tap_count == 0) {
107 dprint("MODS_TAP: Oneshot: add_mods\n"); 107 dprint("MODS_TAP: Oneshot: add_mods\n");
108 add_mods(mods); 108 register_mods(mods);
109 } 109 }
110 else if (tap_count == 1) { 110 else if (tap_count == 1) {
111 dprint("MODS_TAP: Oneshot: start\n"); 111 dprint("MODS_TAP: Oneshot: start\n");
112 oneshot_start(mods); 112 set_oneshot_mods(mods);
113 } 113 }
114 else if (tap_count == TAPPING_TOGGLE) { 114 else if (tap_count == TAPPING_TOGGLE) {
115 dprint("MODS_TAP: Oneshot: toggle\n"); 115 dprint("MODS_TAP: Oneshot: toggle\n");
@@ -118,25 +118,23 @@ void process_action(keyrecord_t *record)
118 else { 118 else {
119 dprint("MODS_TAP: Oneshot: cancel&add_mods\n"); 119 dprint("MODS_TAP: Oneshot: cancel&add_mods\n");
120 // double tap cancels oneshot and works as normal modifier. 120 // double tap cancels oneshot and works as normal modifier.
121 oneshot_cancel(); 121 clear_oneshot_mods();
122 add_mods(mods); 122 register_mods(mods);
123 } 123 }
124 } else { 124 } else {
125 if (tap_count == 0) { 125 if (tap_count == 0) {
126 dprint("MODS_TAP: Oneshot: cancel/del_mods\n"); 126 dprint("MODS_TAP: Oneshot: cancel/del_mods\n");
127 // cancel oneshot on hold 127 // cancel oneshot on hold
128 oneshot_cancel(); 128 clear_oneshot_mods();
129 del_mods(mods); 129 unregister_mods(mods);
130 } 130 }
131 else if (tap_count == 1) { 131 else if (tap_count == 1) {
132 dprint("MODS_TAP: Oneshot: del_mods\n"); 132 // Oneshot
133 // retain Oneshot
134 del_mods(mods);
135 } 133 }
136 else { 134 else {
137 dprint("MODS_TAP: Oneshot: del_mods\n"); 135 dprint("MODS_TAP: Oneshot: del_mods\n");
138 // cancel Mods 136 // cancel Mods
139 del_mods(mods); 137 unregister_mods(mods);
140 } 138 }
141 } 139 }
142 break; 140 break;
@@ -148,14 +146,14 @@ void process_action(keyrecord_t *record)
148 dprint("MODS_TAP: Tap: Cancel: add_mods\n"); 146 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
149 // ad hoc: set 0 to cancel tap 147 // ad hoc: set 0 to cancel tap
150 record->tap.count = 0; 148 record->tap.count = 0;
151 add_mods(mods); 149 register_mods(mods);
152 } else { 150 } else {
153 dprint("MODS_TAP: Tap: register_code\n"); 151 dprint("MODS_TAP: Tap: register_code\n");
154 register_code(action.key.code); 152 register_code(action.key.code);
155 } 153 }
156 } else { 154 } else {
157 dprint("MODS_TAP: No tap: add_mods\n"); 155 dprint("MODS_TAP: No tap: add_mods\n");
158 add_mods(mods); 156 register_mods(mods);
159 } 157 }
160 } else { 158 } else {
161 if (tap_count > 0) { 159 if (tap_count > 0) {
@@ -163,7 +161,7 @@ void process_action(keyrecord_t *record)
163 unregister_code(action.key.code); 161 unregister_code(action.key.code);
164 } else { 162 } else {
165 dprint("MODS_TAP: No tap: add_mods\n"); 163 dprint("MODS_TAP: No tap: add_mods\n");
166 del_mods(mods); 164 unregister_mods(mods);
167 } 165 }
168 } 166 }
169 break; 167 break;
@@ -343,30 +341,30 @@ void register_code(uint8_t code)
343 // Resync: ignore if caps lock already is on 341 // Resync: ignore if caps lock already is on
344 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return; 342 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
345#endif 343#endif
346 host_add_key(KC_CAPSLOCK); 344 add_key(KC_CAPSLOCK);
347 host_send_keyboard_report(); 345 send_keyboard_report();
348 host_del_key(KC_CAPSLOCK); 346 del_key(KC_CAPSLOCK);
349 host_send_keyboard_report(); 347 send_keyboard_report();
350 } 348 }
351 349
352 else if (KC_LOCKING_NUM == code) { 350 else if (KC_LOCKING_NUM == code) {
353#ifdef LOCKING_RESYNC_ENABLE 351#ifdef LOCKING_RESYNC_ENABLE
354 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return; 352 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
355#endif 353#endif
356 host_add_key(KC_NUMLOCK); 354 add_key(KC_NUMLOCK);
357 host_send_keyboard_report(); 355 send_keyboard_report();
358 host_del_key(KC_NUMLOCK); 356 del_key(KC_NUMLOCK);
359 host_send_keyboard_report(); 357 send_keyboard_report();
360 } 358 }
361 359
362 else if (KC_LOCKING_SCROLL == code) { 360 else if (KC_LOCKING_SCROLL == code) {
363#ifdef LOCKING_RESYNC_ENABLE 361#ifdef LOCKING_RESYNC_ENABLE
364 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return; 362 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
365#endif 363#endif
366 host_add_key(KC_SCROLLLOCK); 364 add_key(KC_SCROLLLOCK);
367 host_send_keyboard_report(); 365 send_keyboard_report();
368 host_del_key(KC_SCROLLLOCK); 366 del_key(KC_SCROLLLOCK);
369 host_send_keyboard_report(); 367 send_keyboard_report();
370 } 368 }
371#endif 369#endif
372 370
@@ -375,25 +373,28 @@ void register_code(uint8_t code)
375 if (command_proc(code)) return; 373 if (command_proc(code)) return;
376 374
377#ifndef NO_ACTION_ONESHOT 375#ifndef NO_ACTION_ONESHOT
376/* TODO: remove
378 if (oneshot_state.mods && !oneshot_state.disabled) { 377 if (oneshot_state.mods && !oneshot_state.disabled) {
379 uint8_t tmp_mods = host_get_mods(); 378 uint8_t tmp_mods = get_mods();
380 host_add_mods(oneshot_state.mods); 379 add_mods(oneshot_state.mods);
381 380
382 host_add_key(code); 381 add_key(code);
383 host_send_keyboard_report(); 382 send_keyboard_report();
384 383
385 host_set_mods(tmp_mods); 384 set_mods(tmp_mods);
385 send_keyboard_report();
386 oneshot_cancel(); 386 oneshot_cancel();
387 } else 387 } else
388*/
388#endif 389#endif
389 { 390 {
390 host_add_key(code); 391 add_key(code);
391 host_send_keyboard_report(); 392 send_keyboard_report();
392 } 393 }
393 } 394 }
394 else if IS_MOD(code) { 395 else if IS_MOD(code) {
395 host_add_mods(MOD_BIT(code)); 396 add_mods(MOD_BIT(code));
396 host_send_keyboard_report(); 397 send_keyboard_report();
397 } 398 }
398 else if IS_SYSTEM(code) { 399 else if IS_SYSTEM(code) {
399 host_system_send(KEYCODE2SYSTEM(code)); 400 host_system_send(KEYCODE2SYSTEM(code));
@@ -415,40 +416,40 @@ void unregister_code(uint8_t code)
415 // Resync: ignore if caps lock already is off 416 // Resync: ignore if caps lock already is off
416 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return; 417 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
417#endif 418#endif
418 host_add_key(KC_CAPSLOCK); 419 add_key(KC_CAPSLOCK);
419 host_send_keyboard_report(); 420 send_keyboard_report();
420 host_del_key(KC_CAPSLOCK); 421 del_key(KC_CAPSLOCK);
421 host_send_keyboard_report(); 422 send_keyboard_report();
422 } 423 }
423 424
424 else if (KC_LOCKING_NUM == code) { 425 else if (KC_LOCKING_NUM == code) {
425#ifdef LOCKING_RESYNC_ENABLE 426#ifdef LOCKING_RESYNC_ENABLE
426 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return; 427 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
427#endif 428#endif
428 host_add_key(KC_NUMLOCK); 429 add_key(KC_NUMLOCK);
429 host_send_keyboard_report(); 430 send_keyboard_report();
430 host_del_key(KC_NUMLOCK); 431 del_key(KC_NUMLOCK);
431 host_send_keyboard_report(); 432 send_keyboard_report();
432 } 433 }
433 434
434 else if (KC_LOCKING_SCROLL == code) { 435 else if (KC_LOCKING_SCROLL == code) {
435#ifdef LOCKING_RESYNC_ENABLE 436#ifdef LOCKING_RESYNC_ENABLE
436 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return; 437 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
437#endif 438#endif
438 host_add_key(KC_SCROLLLOCK); 439 add_key(KC_SCROLLLOCK);
439 host_send_keyboard_report(); 440 send_keyboard_report();
440 host_del_key(KC_SCROLLLOCK); 441 del_key(KC_SCROLLLOCK);
441 host_send_keyboard_report(); 442 send_keyboard_report();
442 } 443 }
443#endif 444#endif
444 445
445 else if IS_KEY(code) { 446 else if IS_KEY(code) {
446 host_del_key(code); 447 del_key(code);
447 host_send_keyboard_report(); 448 send_keyboard_report();
448 } 449 }
449 else if IS_MOD(code) { 450 else if IS_MOD(code) {
450 host_del_mods(MOD_BIT(code)); 451 del_mods(MOD_BIT(code));
451 host_send_keyboard_report(); 452 send_keyboard_report();
452 } 453 }
453 else if IS_SYSTEM(code) { 454 else if IS_SYSTEM(code) {
454 host_system_send(0); 455 host_system_send(0);
@@ -458,38 +459,33 @@ void unregister_code(uint8_t code)
458 } 459 }
459} 460}
460 461
461void add_mods(uint8_t mods) 462void register_mods(uint8_t mods)
462{ 463{
463 if (mods) { 464 if (mods) {
464 host_add_mods(mods); 465 add_mods(mods);
465 host_send_keyboard_report(); 466 send_keyboard_report();
466 } 467 }
467} 468}
468 469
469void del_mods(uint8_t mods) 470void unregister_mods(uint8_t mods)
470{ 471{
471 if (mods) { 472 if (mods) {
472 host_del_mods(mods); 473 del_mods(mods);
473 host_send_keyboard_report(); 474 send_keyboard_report();
474 } 475 }
475} 476}
476 477
477void set_mods(uint8_t mods)
478{
479 host_set_mods(mods);
480 host_send_keyboard_report();
481}
482
483void clear_keyboard(void) 478void clear_keyboard(void)
484{ 479{
485 host_clear_mods(); 480 clear_mods();
486 clear_keyboard_but_mods(); 481 clear_keyboard_but_mods();
487} 482}
488 483
489void clear_keyboard_but_mods(void) 484void clear_keyboard_but_mods(void)
490{ 485{
491 host_clear_keys(); 486 clear_weak_mods();
492 host_send_keyboard_report(); 487 clear_keys();
488 send_keyboard_report();
493#ifdef MOUSEKEY_ENABLE 489#ifdef MOUSEKEY_ENABLE
494 mousekey_clear(); 490 mousekey_clear();
495 mousekey_send(); 491 mousekey_send();
@@ -502,7 +498,7 @@ void clear_keyboard_but_mods(void)
502 498
503bool sending_anykey(void) 499bool sending_anykey(void)
504{ 500{
505 return (host_has_anykey() || host_mouse_in_use() || 501 return (has_anykey() || host_mouse_in_use() ||
506 host_last_sysytem_report() || host_last_consumer_report()); 502 host_last_sysytem_report() || host_last_consumer_report());
507} 503}
508 504
diff --git a/common/action.h b/common/action.h
index 8f1f5b798..d57f4a86f 100644
--- a/common/action.h
+++ b/common/action.h
@@ -59,9 +59,9 @@ void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
59void process_action(keyrecord_t *record); 59void process_action(keyrecord_t *record);
60void register_code(uint8_t code); 60void register_code(uint8_t code);
61void unregister_code(uint8_t code); 61void unregister_code(uint8_t code);
62void add_mods(uint8_t mods); 62void register_mods(uint8_t mods);
63void del_mods(uint8_t mods); 63void unregister_mods(uint8_t mods);
64void set_mods(uint8_t mods); 64//void set_mods(uint8_t mods);
65void clear_keyboard(void); 65void clear_keyboard(void);
66void clear_keyboard_but_mods(void); 66void clear_keyboard_but_mods(void);
67bool sending_anykey(void); 67bool sending_anykey(void);
diff --git a/common/action_code.h b/common/action_code.h
index b2a7f2b4f..c153838f2 100644
--- a/common/action_code.h
+++ b/common/action_code.h
@@ -29,13 +29,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * 000r|0000|0000 0001 Transparent code 29 * 000r|0000|0000 0001 Transparent code
30 * 000r|0000| keycode Key 30 * 000r|0000| keycode Key
31 * 000r|mods|0000 0000 Modifiers 31 * 000r|mods|0000 0000 Modifiers
32 * 000r|mods| keycode Key and Modifiers 32 * 000r|mods| keycode Modifiers+Key(Modified key)
33 * r: Left/Right flag(Left:0, Right:1) 33 * r: Left/Right flag(Left:0, Right:1)
34 * 34 *
35 * ACT_MODS_TAP(001r): 35 * ACT_MODS_TAP(001r):
36 * 001r|mods|0000 0000 Modifiers with OneShot 36 * 001r|mods|0000 0000 Modifiers with OneShot
37 * 001r|mods|0000 00xx (reserved) 37 * 001r|mods|0000 00xx (reserved)
38 * 001r|mods| keycode Modifiers with Tap Key 38 * 001r|mods| keycode Modifiers with Tap Key(Dual role)
39 * 39 *
40 * 40 *
41 * Other Keys(01xx) 41 * Other Keys(01xx)
@@ -69,7 +69,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
69 * 1001|oopp|BBBB BBBB 8-bit Bitwise Operation??? 69 * 1001|oopp|BBBB BBBB 8-bit Bitwise Operation???
70 * 70 *
71 * ACT_LAYER_TAP(101x): 71 * ACT_LAYER_TAP(101x):
72 * 101E|LLLL| keycode Invert with tap key 72 * 101E|LLLL| keycode On/Off with tap key
73 * 101E|LLLL|1110 xxxx Reserved(0xE0-EF) 73 * 101E|LLLL|1110 xxxx Reserved(0xE0-EF)
74 * 101E|LLLL|1111 0000 Invert with tap toggle(0xF0) 74 * 101E|LLLL|1111 0000 Invert with tap toggle(0xF0)
75 * 101E|LLLL|1111 0001 On/Off 75 * 101E|LLLL|1111 0001 On/Off
diff --git a/common/action_oneshot.c b/common/action_oneshot.c
deleted file mode 100644
index d34f44b5a..000000000
--- a/common/action_oneshot.c
+++ /dev/null
@@ -1,21 +0,0 @@
1#include "action_oneshot.h"
2
3
4#ifndef NO_ACTION_ONESHOT
5oneshot_state_t oneshot_state;
6
7void oneshot_start(uint8_t mods)
8{
9 oneshot_state.mods = mods;
10}
11
12void oneshot_cancel(void)
13{
14 oneshot_state.mods = 0;
15}
16
17void oneshot_toggle(void)
18{
19 oneshot_state.disabled = !oneshot_state.disabled;
20}
21#endif
diff --git a/common/action_oneshot.h b/common/action_oneshot.h
deleted file mode 100644
index 36ef9e9bc..000000000
--- a/common/action_oneshot.h
+++ /dev/null
@@ -1,52 +0,0 @@
1/*
2Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#ifndef ACTION_ONESHOT_H
18#define ACTION_ONESHOT_H
19
20#include <stdint.h>
21#include <stdbool.h>
22
23#ifdef NO_ACTION_TAPPING
24 #define NO_ACTION_ONESHOT
25#endif
26
27#ifndef NO_ACTION_ONESHOT
28/* Oneshot modifier
29 *
30 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
31 * Solution: Oneshot modifier have its effect on only one key coming next.
32 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
33 *
34 * Hold: works as normal modifier.
35 * Tap: one shot modifier.
36 * 2 Tap: cancel one shot modifier.
37 * 5-Tap: toggles enable/disable oneshot feature.
38 */
39typedef struct {
40 uint8_t mods;
41 bool disabled;
42} oneshot_state_t;
43
44
45oneshot_state_t oneshot_state;
46
47void oneshot_start(uint8_t mods);
48void oneshot_cancel(void);
49void oneshot_toggle(void);
50#endif
51
52#endif
diff --git a/common/action_util.c b/common/action_util.c
new file mode 100644
index 000000000..50d686a07
--- /dev/null
+++ b/common/action_util.c
@@ -0,0 +1,192 @@
1/*
2Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
22static inline void add_key_byte(uint8_t code);
23static inline void del_key_byte(uint8_t code);
24#ifdef NKRO_ENABLE
25static inline void add_key_bit(uint8_t code);
26static inline void del_key_bit(uint8_t code);
27#endif
28
29static uint8_t real_mods = 0;
30static uint8_t weak_mods = 0;
31
32
33// TODO: pointer variable is not needed
34//report_keyboard_t keyboard_report = {};
35report_keyboard_t *keyboard_report = &(report_keyboard_t){};
36
37#ifndef NO_ACTION_ONESHOT
38static bool oneshot_enabled = true;
39static int8_t oneshot_mods = 0;
40#endif
41
42void send_keyboard_report(void) {
43 keyboard_report->mods = real_mods;
44 keyboard_report->mods |= weak_mods;
45#ifndef NO_ACTION_ONESHOT
46 keyboard_report->mods |= oneshot_mods;
47 if (has_anykey()) {
48 clear_oneshot_mods();
49 }
50#endif
51 host_keyboard_send(keyboard_report);
52}
53
54/* key */
55void add_key(uint8_t key)
56{
57#ifdef NKRO_ENABLE
58 if (keyboard_nkro) {
59 add_key_bit(key);
60 return;
61 }
62#endif
63 add_key_byte(key);
64}
65
66void del_key(uint8_t key)
67{
68#ifdef NKRO_ENABLE
69 if (keyboard_nkro) {
70 del_key_bit(key);
71 return;
72 }
73#endif
74 del_key_byte(key);
75}
76
77void clear_keys(void)
78{
79 // not clear mods
80 for (int8_t i = 1; i < REPORT_SIZE; i++) {
81 keyboard_report->raw[i] = 0;
82 }
83}
84
85
86/* modifier */
87uint8_t get_mods(void) { return real_mods; }
88void add_mods(uint8_t mods) { real_mods |= mods; }
89void del_mods(uint8_t mods) { real_mods &= ~mods; }
90void set_mods(uint8_t mods) { real_mods = mods; }
91void clear_mods(void) { real_mods = 0; }
92
93/* weak modifier */
94uint8_t get_weak_mods(void) { return weak_mods; }
95void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
96void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
97void set_weak_mods(uint8_t mods) { weak_mods = mods; }
98void clear_weak_mods(void) { weak_mods = 0; }
99
100/* Oneshot modifier */
101#ifndef NO_ACTION_ONESHOT
102void set_oneshot_mods(uint8_t mods) { oneshot_mods = mods; }
103void clear_oneshot_mods(void) { oneshot_mods = 0; }
104void oneshot_toggle(void) { oneshot_enabled = !oneshot_enabled; }
105void oneshot_enable(void) { oneshot_enabled = true; }
106void oneshot_disable(void) { oneshot_enabled = false; }
107#endif
108
109
110
111
112/*
113 * inspect keyboard state
114 */
115uint8_t has_anykey(void)
116{
117 uint8_t cnt = 0;
118 for (uint8_t i = 1; i < REPORT_SIZE; i++) {
119 if (keyboard_report->raw[i])
120 cnt++;
121 }
122 return cnt;
123}
124
125uint8_t has_anymod(void)
126{
127 return bitpop(real_mods);
128}
129
130uint8_t get_first_key(void)
131{
132#ifdef NKRO_ENABLE
133 if (keyboard_nkro) {
134 uint8_t i = 0;
135 for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
136 ;
137 return i<<3 | biton(keyboard_report->nkro.bits[i]);
138 }
139#endif
140 return keyboard_report->keys[0];
141}
142
143
144
145/* local functions */
146static inline void add_key_byte(uint8_t code)
147{
148 int8_t i = 0;
149 int8_t empty = -1;
150 for (; i < REPORT_KEYS; i++) {
151 if (keyboard_report->keys[i] == code) {
152 break;
153 }
154 if (empty == -1 && keyboard_report->keys[i] == 0) {
155 empty = i;
156 }
157 }
158 if (i == REPORT_KEYS) {
159 if (empty != -1) {
160 keyboard_report->keys[empty] = code;
161 }
162 }
163}
164
165static inline void del_key_byte(uint8_t code)
166{
167 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
168 if (keyboard_report->keys[i] == code) {
169 keyboard_report->keys[i] = 0;
170 }
171 }
172}
173
174#ifdef NKRO_ENABLE
175static inline void add_key_bit(uint8_t code)
176{
177 if ((code>>3) < REPORT_BITS) {
178 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
179 } else {
180 dprintf("add_key_bit: can't add: %02X\n", code);
181 }
182}
183
184static inline void del_key_bit(uint8_t code)
185{
186 if ((code>>3) < REPORT_BITS) {
187 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
188 } else {
189 dprintf("del_key_bit: can't del: %02X\n", code);
190 }
191}
192#endif
diff --git a/common/action_util.h b/common/action_util.h
new file mode 100644
index 000000000..939bc2b66
--- /dev/null
+++ b/common/action_util.h
@@ -0,0 +1,56 @@
1/*
2Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#ifndef ACTION_UTIL_H
18#define ACTION_UTIL_H
19
20#include <stdint.h>
21
22extern report_keyboard_t *keyboard_report;
23
24void send_keyboard_report(void);
25
26/* key */
27void add_key(uint8_t key);
28void del_key(uint8_t key);
29void clear_keys(void);
30
31/* modifier */
32uint8_t get_mods(void);
33void add_mods(uint8_t mods);
34void del_mods(uint8_t mods);
35void set_mods(uint8_t mods);
36void clear_mods(void);
37
38/* weak modifier */
39uint8_t get_weak_mods(void);
40void add_weak_mods(uint8_t mods);
41void del_weak_mods(uint8_t mods);
42void set_weak_mods(uint8_t mods);
43void clear_weak_mods(void);
44
45/* oneshot modifier */
46void set_oneshot_mods(uint8_t mods);
47void clear_oneshot_mods(void);
48void oneshot_toggle(void);
49void oneshot_enable(void);
50void oneshot_disable(void);
51
52/* inspect */
53uint8_t has_anykey(void);
54uint8_t has_anymod(void);
55uint8_t get_first_key(void);
56#endif
diff --git a/common/command.c b/common/command.c
index 8a8a81d66..f6f276951 100644
--- a/common/command.c
+++ b/common/command.c
@@ -27,6 +27,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27#include "keyboard.h" 27#include "keyboard.h"
28#include "bootloader.h" 28#include "bootloader.h"
29#include "action_layer.h" 29#include "action_layer.h"
30#include "action_util.h"
30#include "eeconfig.h" 31#include "eeconfig.h"
31#include "sleep_led.h" 32#include "sleep_led.h"
32#include "led.h" 33#include "led.h"
diff --git a/common/host.c b/common/host.c
index 569451652..0703dba01 100644
--- a/common/host.c
+++ b/common/host.c
@@ -27,7 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27bool keyboard_nkro = false; 27bool keyboard_nkro = false;
28#endif 28#endif
29 29
30report_keyboard_t *keyboard_report = &(report_keyboard_t){};
31report_mouse_t mouse_report = {}; 30report_mouse_t mouse_report = {};
32 31
33 32
@@ -35,13 +34,6 @@ static host_driver_t *driver;
35static uint16_t last_system_report = 0; 34static uint16_t last_system_report = 0;
36static uint16_t last_consumer_report = 0; 35static uint16_t last_consumer_report = 0;
37 36
38static inline void add_key_byte(uint8_t code);
39static inline void del_key_byte(uint8_t code);
40#ifdef NKRO_ENABLE
41static inline void add_key_bit(uint8_t code);
42static inline void del_key_bit(uint8_t code);
43#endif
44
45 37
46void host_set_driver(host_driver_t *d) 38void host_set_driver(host_driver_t *d)
47{ 39{
@@ -67,7 +59,7 @@ void host_keyboard_send(report_keyboard_t *report)
67 if (debug_keyboard) { 59 if (debug_keyboard) {
68 dprint("keyboard_report: "); 60 dprint("keyboard_report: ");
69 for (uint8_t i = 0; i < REPORT_SIZE; i++) { 61 for (uint8_t i = 0; i < REPORT_SIZE; i++) {
70 dprintf("%02X ", keyboard_report->raw[i]); 62 dprintf("%02X ", report->raw[i]);
71 } 63 }
72 dprint("\n"); 64 dprint("\n");
73 } 65 }
@@ -97,98 +89,6 @@ void host_consumer_send(uint16_t report)
97 (*driver->send_consumer)(report); 89 (*driver->send_consumer)(report);
98} 90}
99 91
100
101
102/* keyboard report utils */
103void host_add_key(uint8_t key)
104{
105#ifdef NKRO_ENABLE
106 if (keyboard_nkro) {
107 add_key_bit(key);
108 return;
109 }
110#endif
111 add_key_byte(key);
112}
113
114void host_del_key(uint8_t key)
115{
116#ifdef NKRO_ENABLE
117 if (keyboard_nkro) {
118 del_key_bit(key);
119 return;
120 }
121#endif
122 del_key_byte(key);
123}
124
125void host_clear_keys(void)
126{
127 // not clea mods
128 for (int8_t i = 1; i < REPORT_SIZE; i++) {
129 keyboard_report->raw[i] = 0;
130 }
131}
132
133uint8_t host_get_mods(void)
134{
135 return keyboard_report->mods;
136}
137
138void host_add_mods(uint8_t mods)
139{
140 keyboard_report->mods |= mods;
141}
142
143void host_del_mods(uint8_t mods)
144{
145 keyboard_report->mods &= ~mods;
146}
147
148void host_set_mods(uint8_t mods)
149{
150 keyboard_report->mods = mods;
151}
152
153void host_clear_mods(void)
154{
155 keyboard_report->mods = 0;
156}
157
158uint8_t host_has_anykey(void)
159{
160 uint8_t cnt = 0;
161 for (uint8_t i = 1; i < REPORT_SIZE; i++) {
162 if (keyboard_report->raw[i])
163 cnt++;
164 }
165 return cnt;
166}
167
168uint8_t host_has_anymod(void)
169{
170 return bitpop(keyboard_report->mods);
171}
172
173uint8_t host_get_first_key(void)
174{
175#ifdef NKRO_ENABLE
176 if (keyboard_nkro) {
177 uint8_t i = 0;
178 for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
179 ;
180 return i<<3 | biton(keyboard_report->nkro.bits[i]);
181 }
182#endif
183 return keyboard_report->keys[0];
184}
185
186void host_send_keyboard_report(void)
187{
188 if (!driver) return;
189 host_keyboard_send(keyboard_report);
190}
191
192uint8_t host_mouse_in_use(void) 92uint8_t host_mouse_in_use(void)
193{ 93{
194 return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h); 94 return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
@@ -203,51 +103,3 @@ uint16_t host_last_consumer_report(void)
203{ 103{
204 return last_consumer_report; 104 return last_consumer_report;
205} 105}
206
207static inline void add_key_byte(uint8_t code)
208{
209 int8_t i = 0;
210 int8_t empty = -1;
211 for (; i < REPORT_KEYS; i++) {
212 if (keyboard_report->keys[i] == code) {
213 break;
214 }
215 if (empty == -1 && keyboard_report->keys[i] == 0) {
216 empty = i;
217 }
218 }
219 if (i == REPORT_KEYS) {
220 if (empty != -1) {
221 keyboard_report->keys[empty] = code;
222 }
223 }
224}
225
226static inline void del_key_byte(uint8_t code)
227{
228 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
229 if (keyboard_report->keys[i] == code) {
230 keyboard_report->keys[i] = 0;
231 }
232 }
233}
234
235#ifdef NKRO_ENABLE
236static inline void add_key_bit(uint8_t code)
237{
238 if ((code>>3) < REPORT_BITS) {
239 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
240 } else {
241 dprintf("add_key_bit: can't add: %02X\n", code);
242 }
243}
244
245static inline void del_key_bit(uint8_t code)
246{
247 if ((code>>3) < REPORT_BITS) {
248 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
249 } else {
250 dprintf("del_key_bit: can't del: %02X\n", code);
251 }
252}
253#endif
diff --git a/common/host.h b/common/host.h
index 7c4f06601..c1a0fbac4 100644
--- a/common/host.h
+++ b/common/host.h
@@ -33,7 +33,6 @@ extern bool keyboard_nkro;
33#endif 33#endif
34 34
35/* report */ 35/* report */
36extern report_keyboard_t *keyboard_report;
37extern report_mouse_t mouse_report; 36extern report_mouse_t mouse_report;
38 37
39 38
@@ -48,22 +47,6 @@ void host_mouse_send(report_mouse_t *report);
48void host_system_send(uint16_t data); 47void host_system_send(uint16_t data);
49void host_consumer_send(uint16_t data); 48void host_consumer_send(uint16_t data);
50 49
51/* keyboard report utils */
52void host_add_key(uint8_t key);
53void host_del_key(uint8_t key);
54void host_clear_keys(void);
55
56uint8_t host_get_mods(void);
57void host_add_mods(uint8_t mods);
58void host_del_mods(uint8_t mods);
59void host_set_mods(uint8_t mods);
60void host_clear_mods(void);
61
62uint8_t host_has_anykey(void);
63uint8_t host_has_anymod(void);
64uint8_t host_get_first_key(void);
65void host_send_keyboard_report(void);
66
67/* mouse report utils */ 50/* mouse report utils */
68uint8_t host_mouse_in_use(void); 51uint8_t host_mouse_in_use(void);
69 52