aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2012-10-09 16:50:14 +0900
committertmk <nobody@nowhere>2012-10-17 15:55:37 +0900
commit71ac82337f803e8ec0c081b3221ac0ccf61035b0 (patch)
tree5692c88c0b16d7f4992321c19a82f5193ebe4dc2 /common
parent373ab0e7192811944786c095facb80938c33f1d5 (diff)
downloadqmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.tar.gz
qmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.zip
Clean host.h interface.
Diffstat (limited to 'common')
-rw-r--r--common/command.c7
-rw-r--r--common/host.c85
-rw-r--r--common/host.h19
-rw-r--r--common/keyboard.c84
4 files changed, 77 insertions, 118 deletions
diff --git a/common/command.c b/common/command.c
index f9cdaf57d..16c6cfb88 100644
--- a/common/command.c
+++ b/common/command.c
@@ -78,8 +78,6 @@ static uint8_t command_common(void)
78 help(); 78 help();
79 break; 79 break;
80 case KC_B: 80 case KC_B:
81 host_clear_keyboard_report();
82 host_send_keyboard_report();
83 print("jump to bootloader... "); 81 print("jump to bootloader... ");
84 _delay_ms(1000); 82 _delay_ms(1000);
85 bootloader_jump(); // not return 83 bootloader_jump(); // not return
@@ -157,9 +155,6 @@ static uint8_t command_common(void)
157 break; 155 break;
158#ifdef NKRO_ENABLE 156#ifdef NKRO_ENABLE
159 case KC_N: 157 case KC_N:
160 // send empty report before change
161 host_clear_keyboard_report();
162 host_send_keyboard_report();
163 keyboard_nkro = !keyboard_nkro; 158 keyboard_nkro = !keyboard_nkro;
164 if (keyboard_nkro) 159 if (keyboard_nkro)
165 print("NKRO: enabled\n"); 160 print("NKRO: enabled\n");
@@ -169,8 +164,6 @@ static uint8_t command_common(void)
169#endif 164#endif
170#ifdef EXTRAKEY_ENABLE 165#ifdef EXTRAKEY_ENABLE
171 case KC_ESC: 166 case KC_ESC:
172 host_clear_keyboard_report();
173 host_send_keyboard_report();
174#ifdef HOST_PJRC 167#ifdef HOST_PJRC
175 if (suspend && remote_wakeup) { 168 if (suspend && remote_wakeup) {
176 usb_remote_wakeup(); 169 usb_remote_wakeup();
diff --git a/common/host.c b/common/host.c
index 37f707d0b..a671c97d3 100644
--- a/common/host.c
+++ b/common/host.c
@@ -1,5 +1,5 @@
1/* 1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com> 2Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
3 3
4This program is free software: you can redistribute it and/or modify 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 5it under the terms of the GNU General Public License as published by
@@ -28,10 +28,7 @@ bool keyboard_nkro = false;
28#endif 28#endif
29 29
30static host_driver_t *driver; 30static host_driver_t *driver;
31static report_keyboard_t report0; 31report_keyboard_t *keyboard_report = &(report_keyboard_t){};
32static report_keyboard_t report1;
33report_keyboard_t *keyboard_report = &report0;
34report_keyboard_t *keyboard_report_prev = &report1;
35 32
36 33
37static inline void add_key_byte(uint8_t code); 34static inline void add_key_byte(uint8_t code);
@@ -56,27 +53,6 @@ uint8_t host_keyboard_leds(void)
56 return (*driver->keyboard_leds)(); 53 return (*driver->keyboard_leds)();
57} 54}
58 55
59/* new interface */
60void host_register_key(uint8_t key)
61{
62 host_add_key(key);
63 host_send_keyboard_report();
64}
65
66void host_unregister_key(uint8_t key)
67{
68 host_del_key(key);
69 host_send_keyboard_report();
70}
71
72void host_clear_all_keys_but_mods(void)
73{
74 for (int8_t i = 0; i < REPORT_KEYS; i++) {
75 keyboard_report->keys[i] = 0;
76 }
77 host_send_keyboard_report();
78}
79
80/* keyboard report operations */ 56/* keyboard report operations */
81void host_add_key(uint8_t key) 57void host_add_key(uint8_t key)
82{ 58{
@@ -100,6 +76,13 @@ void host_del_key(uint8_t key)
100 del_key_byte(key); 76 del_key_byte(key);
101} 77}
102 78
79void host_clear_keys(void)
80{
81 for (int8_t i = 0; i < REPORT_KEYS; i++) {
82 keyboard_report->keys[i] = 0;
83 }
84}
85
103void host_add_mod_bit(uint8_t mod) 86void host_add_mod_bit(uint8_t mod)
104{ 87{
105 keyboard_report->mods |= mod; 88 keyboard_report->mods |= mod;
@@ -115,40 +98,9 @@ void host_set_mods(uint8_t mods)
115 keyboard_report->mods = mods; 98 keyboard_report->mods = mods;
116} 99}
117 100
118void host_add_code(uint8_t code) 101void host_clear_mods(void)
119{
120 if (IS_MOD(code)) {
121 host_add_mod_bit(MOD_BIT(code));
122 } else {
123 host_add_key(code);
124 }
125}
126
127void host_del_code(uint8_t code)
128{
129 if (IS_MOD(code)) {
130 host_del_mod_bit(MOD_BIT(code));
131 } else {
132 host_del_key(code);
133 }
134}
135
136void host_swap_keyboard_report(void)
137{
138 uint8_t sreg = SREG;
139 cli();
140 report_keyboard_t *tmp = keyboard_report_prev;
141 keyboard_report_prev = keyboard_report;
142 keyboard_report = tmp;
143 SREG = sreg;
144}
145
146void host_clear_keyboard_report(void)
147{ 102{
148 keyboard_report->mods = 0; 103 keyboard_report->mods = 0;
149 for (int8_t i = 0; i < REPORT_KEYS; i++) {
150 keyboard_report->keys[i] = 0;
151 }
152} 104}
153 105
154uint8_t host_has_anykey(void) 106uint8_t host_has_anykey(void)
@@ -174,7 +126,6 @@ uint8_t host_get_first_key(void)
174 return keyboard_report->keys[0]; 126 return keyboard_report->keys[0];
175} 127}
176 128
177
178void host_send_keyboard_report(void) 129void host_send_keyboard_report(void)
179{ 130{
180 if (!driver) return; 131 if (!driver) return;
@@ -189,6 +140,14 @@ void host_send_keyboard_report(void)
189 } 140 }
190} 141}
191 142
143
144/* send report */
145void host_keyboard_send(report_keyboard_t *report)
146{
147 if (!driver) return;
148 (*driver->send_keyboard)(report);
149}
150
192void host_mouse_send(report_mouse_t *report) 151void host_mouse_send(report_mouse_t *report)
193{ 152{
194 if (!driver) return; 153 if (!driver) return;
@@ -218,17 +177,13 @@ void host_consumer_send(uint16_t data)
218 177
219static inline void add_key_byte(uint8_t code) 178static inline void add_key_byte(uint8_t code)
220{ 179{
221 // TODO: fix ugly code
222 int8_t i = 0; 180 int8_t i = 0;
223 int8_t empty = -1; 181 int8_t empty = -1;
224 for (; i < REPORT_KEYS; i++) { 182 for (; i < REPORT_KEYS; i++) {
225 if (keyboard_report_prev->keys[i] == code) { 183 if (keyboard_report->keys[i] == code) {
226 keyboard_report->keys[i] = code;
227 break; 184 break;
228 } 185 }
229 if (empty == -1 && 186 if (empty == -1 && keyboard_report->keys[i] == 0) {
230 keyboard_report_prev->keys[i] == 0 &&
231 keyboard_report->keys[i] == 0) {
232 empty = i; 187 empty = i;
233 } 188 }
234 } 189 }
diff --git a/common/host.h b/common/host.h
index a6dff8de0..a0a661af5 100644
--- a/common/host.h
+++ b/common/host.h
@@ -35,30 +35,31 @@ extern report_keyboard_t *keyboard_report;
35extern report_keyboard_t *keyboard_report_prev; 35extern report_keyboard_t *keyboard_report_prev;
36 36
37 37
38/* host driver */
38void host_set_driver(host_driver_t *driver); 39void host_set_driver(host_driver_t *driver);
39host_driver_t *host_get_driver(void); 40host_driver_t *host_get_driver(void);
41
40uint8_t host_keyboard_leds(void); 42uint8_t host_keyboard_leds(void);
41 43
42/* new interface */
43void host_register_key(uint8_t key);
44void host_unregister_key(uint8_t key);
45void host_clear_all_keys_but_mods(void);
46 44
47/* keyboard report operations */ 45/* keyboard report operations */
46/* key */
48void host_add_key(uint8_t key); 47void host_add_key(uint8_t key);
49void host_del_key(uint8_t key); 48void host_del_key(uint8_t key);
49void host_clear_keys(void);
50/* modifier */
50void host_add_mod_bit(uint8_t mod); 51void host_add_mod_bit(uint8_t mod);
51void host_del_mod_bit(uint8_t mod); 52void host_del_mod_bit(uint8_t mod);
52void host_set_mods(uint8_t mods); 53void host_set_mods(uint8_t mods);
53void host_add_code(uint8_t code); 54void host_clear_mods(void);
54void host_del_code(uint8_t code); 55/* query */
55void host_swap_keyboard_report(void);
56void host_clear_keyboard_report(void);
57uint8_t host_has_anykey(void); 56uint8_t host_has_anykey(void);
58uint8_t host_get_first_key(void); 57uint8_t host_get_first_key(void);
58/* send report */
59void host_send_keyboard_report(void);
59 60
60 61
61void host_send_keyboard_report(void); 62/* send report: mouse, system contorl and consumer page */
62void host_mouse_send(report_mouse_t *report); 63void host_mouse_send(report_mouse_t *report);
63void host_system_send(uint16_t data); 64void host_system_send(uint16_t data);
64void host_consumer_send(uint16_t data); 65void host_consumer_send(uint16_t data);
diff --git a/common/keyboard.c b/common/keyboard.c
index 6adad8882..37d3b06ba 100644
--- a/common/keyboard.c
+++ b/common/keyboard.c
@@ -81,17 +81,37 @@ static inline keykind_t get_keykind(uint8_t code, bool pressed)
81 return NONE; 81 return NONE;
82} 82}
83 83
84static void clear_keyboard(void)
85{
86 host_clear_keys();
87 host_clear_mods();
88 host_send_keyboard_report();
89
90 host_system_send(0);
91 host_consumer_send(0);
92
93 mousekey_clear();
94 mousekey_send();
95}
96
97static void clear_keyboard_but_mods(void)
98{
99 host_clear_keys();
100 host_send_keyboard_report();
101
102 host_system_send(0);
103 host_consumer_send(0);
104
105 mousekey_clear();
106 mousekey_send();
107}
108
84static void layer_switch_on(uint8_t code) 109static void layer_switch_on(uint8_t code)
85{ 110{
86 if (!IS_FN(code)) return; 111 if (!IS_FN(code)) return;
87 fn_state_bits |= FN_BIT(code); 112 fn_state_bits |= FN_BIT(code);
88 if (current_layer != keymap_fn_layer(FN_INDEX(code))) { 113 if (current_layer != keymap_fn_layer(FN_INDEX(code))) {
89 // clear all key execpt Mod key 114 clear_keyboard_but_mods();
90 host_clear_all_keys_but_mods();
91 host_system_send(0);
92 host_consumer_send(0);
93 mousekey_clear();
94 mousekey_send();
95 115
96 debug("Layer Switch(on): "); debug_hex(current_layer); 116 debug("Layer Switch(on): "); debug_hex(current_layer);
97 current_layer = keymap_fn_layer(FN_INDEX(code)); 117 current_layer = keymap_fn_layer(FN_INDEX(code));
@@ -104,12 +124,7 @@ static void layer_switch_off(uint8_t code)
104 if (!IS_FN(code)) return; 124 if (!IS_FN(code)) return;
105 fn_state_bits &= ~FN_BIT(code); 125 fn_state_bits &= ~FN_BIT(code);
106 if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { 126 if (current_layer != keymap_fn_layer(biton(fn_state_bits))) {
107 // clear all key execpt Mod key 127 clear_keyboard_but_mods();
108 host_clear_all_keys_but_mods();
109 host_system_send(0);
110 host_consumer_send(0);
111 mousekey_clear();
112 mousekey_send();
113 128
114 debug("Layer Switch(off): "); debug_hex(current_layer); 129 debug("Layer Switch(off): "); debug_hex(current_layer);
115 current_layer = keymap_fn_layer(biton(fn_state_bits)); 130 current_layer = keymap_fn_layer(biton(fn_state_bits));
@@ -117,11 +132,6 @@ static void layer_switch_off(uint8_t code)
117 } 132 }
118} 133}
119 134
120static inline uint8_t get_keycode(key_t key)
121{
122 return keymap_get_keycode(current_layer, key.row, key.col);
123}
124
125// whether any key except modifier is down or not 135// whether any key except modifier is down or not
126static inline bool is_anykey_down(void) 136static inline bool is_anykey_down(void)
127{ 137{
@@ -129,7 +139,7 @@ static inline bool is_anykey_down(void)
129 matrix_row_t matrix_row = matrix_get_row(r); 139 matrix_row_t matrix_row = matrix_get_row(r);
130 for (int c = 0; c < MATRIX_COLS; c++) { 140 for (int c = 0; c < MATRIX_COLS; c++) {
131 if (matrix_row && (1<<c)) { 141 if (matrix_row && (1<<c)) {
132 if (IS_KEY(get_keycode((key_t){ .row = r, .col = c }))) { 142 if (IS_KEY(keymap_get_keycode(current_layer, r, c))) {
133 return true; 143 return true;
134 } 144 }
135 } 145 }
@@ -140,7 +150,6 @@ static inline bool is_anykey_down(void)
140 150
141static void register_code(uint8_t code) 151static void register_code(uint8_t code)
142{ 152{
143debug("register_code\n");
144 if IS_KEY(code) { 153 if IS_KEY(code) {
145 host_add_key(code); 154 host_add_key(code);
146 host_send_keyboard_report(); 155 host_send_keyboard_report();
@@ -154,7 +163,6 @@ debug("register_code\n");
154 mousekey_send(); 163 mousekey_send();
155 } 164 }
156 else if IS_CONSUMER(code) { 165 else if IS_CONSUMER(code) {
157debug("consumer\n");
158 uint16_t usage = 0; 166 uint16_t usage = 0;
159 switch (code) { 167 switch (code) {
160 case KC_AUDIO_MUTE: 168 case KC_AUDIO_MUTE:
@@ -212,7 +220,6 @@ debug("consumer\n");
212 usage = AC_BOOKMARKS; 220 usage = AC_BOOKMARKS;
213 break; 221 break;
214 } 222 }
215debug("usage: "); phex16(usage); debug("\n");
216 host_consumer_send(usage); 223 host_consumer_send(usage);
217 } 224 }
218 else if IS_SYSTEM(code) { 225 else if IS_SYSTEM(code) {
@@ -293,9 +300,9 @@ static void unregister_code(uint8_t code)
293 * Sk: store key 300 * Sk: store key
294 * Sf: store Fn 301 * Sf: store Fn
295 * Ps: play stored key(Interpret stored key and transit state) 302 * Ps: play stored key(Interpret stored key and transit state)
296 * L+: Switch to new layer(*retain* Modifiers only) 303 * L+: Switch to new layer(*unregister* all keys but modifiers)
297 * L-: Switch back to last layer(*clear* stored key/Fn, *unregister* all Modifier/key) 304 * L-: Switch back to last layer(*unregister* all keys but modifiers)
298 * Ld: Switch back to default layer(*clear* stored key/Fn, *unregister* all Modifier/key) 305 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
299 */ 306 */
300#define NEXT(state) do { \ 307#define NEXT(state) do { \
301 debug("NEXT: "); print_P(state_str(kbdstate)); \ 308 debug("NEXT: "); print_P(state_str(kbdstate)); \
@@ -305,13 +312,7 @@ static void unregister_code(uint8_t code)
305 312
306static inline void process_key(keyevent_t event) 313static inline void process_key(keyevent_t event)
307{ 314{
308 /* TODO: ring buffer 315 uint8_t code = keymap_get_keycode(current_layer, event.key.row, event.key.col);
309 static keyrecord_t waiting_keys[5];
310 static uint8_t waiting_keys_head = 0;
311 static uint8_t waiting_keys_tail = 0;
312 */
313
314 uint8_t code = get_keycode(event.key);
315 keykind_t kind = get_keykind(code, event.pressed); 316 keykind_t kind = get_keykind(code, event.pressed);
316 317
317 uint8_t tmp_mods; 318 uint8_t tmp_mods;
@@ -502,8 +503,6 @@ static inline void process_key(keyevent_t event)
502 } 503 }
503 break; 504 break;
504 } 505 }
505
506 // TODO: FAIL SAFE: unregister all keys when no key down
507} 506}
508 507
509void keyboard_init(void) 508void keyboard_init(void)
@@ -526,11 +525,11 @@ void keyboard_task(void)
526 matrix_scan(); 525 matrix_scan();
527 if (command_proc()) { 526 if (command_proc()) {
528 debug("COMMAND\n"); 527 debug("COMMAND\n");
529 // TODO: clear all keys 528 // TODO: COMMAND state?
530 host_clear_keyboard_report(); 529 clear_keyboard();
531 host_send_keyboard_report();
532 return; 530 return;
533 } 531 }
532
534 for (int r = 0; r < MATRIX_ROWS; r++) { 533 for (int r = 0; r < MATRIX_ROWS; r++) {
535 matrix_row = matrix_get_row(r); 534 matrix_row = matrix_get_row(r);
536 matrix_change = matrix_row ^ matrix_prev[r]; 535 matrix_change = matrix_row ^ matrix_prev[r];
@@ -552,7 +551,6 @@ void keyboard_task(void)
552 } 551 }
553 } 552 }
554 MATRIX_LOOP_END: 553 MATRIX_LOOP_END:
555 // TODO: FAIL SAFE: clear all key if no key down
556 554
557 // layer switch when delay term elapses 555 // layer switch when delay term elapses
558 if (kbdstate == DELAYING || kbdstate == WAITING) { 556 if (kbdstate == DELAYING || kbdstate == WAITING) {
@@ -575,6 +573,18 @@ void keyboard_task(void)
575 // mousekey repeat & acceleration 573 // mousekey repeat & acceleration
576 mousekey_task(); 574 mousekey_task();
577 575
576 // FAIL SAFE: clear all key if no key down
577 if (matrix_change) {
578 matrix_row_t is_matrix_on = 0;
579 for (int r = 0; r < MATRIX_ROWS; r++) {
580 is_matrix_on |= matrix_get_row(r);
581 }
582 if (!is_matrix_on) {
583 debug("FAIL SAFE: clear all keys.\n");
584 clear_keyboard();
585 }
586 }
587
578 return; 588 return;
579} 589}
580 590