aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/host.c95
-rw-r--r--common/host.h24
-rw-r--r--common/keyboard.c136
-rw-r--r--common/mousekey.c95
4 files changed, 194 insertions, 156 deletions
diff --git a/common/host.c b/common/host.c
index a671c97d3..261ec6472 100644
--- a/common/host.c
+++ b/common/host.c
@@ -27,9 +27,13 @@ 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
30static host_driver_t *driver;
31report_keyboard_t *keyboard_report = &(report_keyboard_t){}; 30report_keyboard_t *keyboard_report = &(report_keyboard_t){};
31report_mouse_t mouse_report = {};
32
32 33
34static host_driver_t *driver;
35static uint16_t last_system_report = 0;
36static uint16_t last_consumer_report = 0;
33 37
34static inline void add_key_byte(uint8_t code); 38static inline void add_key_byte(uint8_t code);
35static inline void del_key_byte(uint8_t code); 39static inline void del_key_byte(uint8_t code);
@@ -52,8 +56,48 @@ uint8_t host_keyboard_leds(void)
52 if (!driver) return 0; 56 if (!driver) return 0;
53 return (*driver->keyboard_leds)(); 57 return (*driver->keyboard_leds)();
54} 58}
59/* send report */
60void host_keyboard_send(report_keyboard_t *report)
61{
62 if (!driver) return;
63 (*driver->send_keyboard)(report);
64
65 if (debug_keyboard) {
66 print("keys: ");
67 for (int i = 0; i < REPORT_KEYS; i++) {
68 phex(keyboard_report->keys[i]); print(" ");
69 }
70 print(" mods: "); phex(keyboard_report->mods); print("\n");
71 }
72}
73
74void host_mouse_send(report_mouse_t *report)
75{
76 if (!driver) return;
77 (*driver->send_mouse)(report);
78}
79
80void host_system_send(uint16_t report)
81{
82 if (report == last_system_report) return;
83 last_system_report = report;
84
85 if (!driver) return;
86 (*driver->send_system)(report);
87}
88
89void host_consumer_send(uint16_t report)
90{
91 if (report == last_consumer_report) return;
92 last_consumer_report = report;
93
94 if (!driver) return;
95 (*driver->send_consumer)(report);
96}
97
98
55 99
56/* keyboard report operations */ 100/* keyboard report utils */
57void host_add_key(uint8_t key) 101void host_add_key(uint8_t key)
58{ 102{
59#ifdef NKRO_ENABLE 103#ifdef NKRO_ENABLE
@@ -113,6 +157,11 @@ uint8_t host_has_anykey(void)
113 return cnt; 157 return cnt;
114} 158}
115 159
160uint8_t host_has_anymod(void)
161{
162 return bitpop(keyboard_report->mods);
163}
164
116uint8_t host_get_first_key(void) 165uint8_t host_get_first_key(void)
117{ 166{
118#ifdef NKRO_ENABLE 167#ifdef NKRO_ENABLE
@@ -129,52 +178,24 @@ uint8_t host_get_first_key(void)
129void host_send_keyboard_report(void) 178void host_send_keyboard_report(void)
130{ 179{
131 if (!driver) return; 180 if (!driver) return;
132 (*driver->send_keyboard)(keyboard_report); 181 host_keyboard_send(keyboard_report);
133
134 if (debug_keyboard) {
135 print("keys: ");
136 for (int i = 0; i < REPORT_KEYS; i++) {
137 phex(keyboard_report->keys[i]); print(" ");
138 }
139 print(" mods: "); phex(keyboard_report->mods); print("\n");
140 }
141} 182}
142 183
143 184uint8_t host_mouse_in_use(void)
144/* send report */
145void host_keyboard_send(report_keyboard_t *report)
146{ 185{
147 if (!driver) return; 186 return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
148 (*driver->send_keyboard)(report);
149} 187}
150 188
151void host_mouse_send(report_mouse_t *report) 189uint16_t host_last_sysytem_report(void)
152{ 190{
153 if (!driver) return; 191 return last_system_report;
154 (*driver->send_mouse)(report);
155} 192}
156 193
157void host_system_send(uint16_t data) 194uint16_t host_last_consumer_report(void)
158{ 195{
159 static uint16_t last_data = 0; 196 return last_consumer_report;
160 if (data == last_data) return;
161 last_data = data;
162
163 if (!driver) return;
164 (*driver->send_system)(data);
165} 197}
166 198
167void host_consumer_send(uint16_t data)
168{
169 static uint16_t last_data = 0;
170 if (data == last_data) return;
171 last_data = data;
172
173 if (!driver) return;
174 (*driver->send_consumer)(data);
175}
176
177
178static inline void add_key_byte(uint8_t code) 199static inline void add_key_byte(uint8_t code)
179{ 200{
180 int8_t i = 0; 201 int8_t i = 0;
diff --git a/common/host.h b/common/host.h
index a0a661af5..207b68310 100644
--- a/common/host.h
+++ b/common/host.h
@@ -31,38 +31,40 @@ extern "C" {
31extern bool keyboard_nkro; 31extern bool keyboard_nkro;
32#endif 32#endif
33 33
34/* report */
34extern report_keyboard_t *keyboard_report; 35extern report_keyboard_t *keyboard_report;
35extern report_keyboard_t *keyboard_report_prev; 36extern report_mouse_t mouse_report;
36 37
37 38
38/* host driver */ 39/* host driver */
39void host_set_driver(host_driver_t *driver); 40void host_set_driver(host_driver_t *driver);
40host_driver_t *host_get_driver(void); 41host_driver_t *host_get_driver(void);
41 42
43/* host driver interface */
42uint8_t host_keyboard_leds(void); 44uint8_t host_keyboard_leds(void);
45void host_keyboard_send(report_keyboard_t *report);
46void host_mouse_send(report_mouse_t *report);
47void host_system_send(uint16_t data);
48void host_consumer_send(uint16_t data);
43 49
44 50/* keyboard report utils */
45/* keyboard report operations */
46/* key */
47void host_add_key(uint8_t key); 51void host_add_key(uint8_t key);
48void host_del_key(uint8_t key); 52void host_del_key(uint8_t key);
49void host_clear_keys(void); 53void host_clear_keys(void);
50/* modifier */
51void host_add_mod_bit(uint8_t mod); 54void host_add_mod_bit(uint8_t mod);
52void host_del_mod_bit(uint8_t mod); 55void host_del_mod_bit(uint8_t mod);
53void host_set_mods(uint8_t mods); 56void host_set_mods(uint8_t mods);
54void host_clear_mods(void); 57void host_clear_mods(void);
55/* query */
56uint8_t host_has_anykey(void); 58uint8_t host_has_anykey(void);
59uint8_t host_has_anymod(void);
57uint8_t host_get_first_key(void); 60uint8_t host_get_first_key(void);
58/* send report */
59void host_send_keyboard_report(void); 61void host_send_keyboard_report(void);
60 62
63/* mouse report utils */
64uint8_t host_mouse_in_use(void);
61 65
62/* send report: mouse, system contorl and consumer page */ 66uint16_t host_last_sysytem_report(void);
63void host_mouse_send(report_mouse_t *report); 67uint16_t host_last_consumer_report(void);
64void host_system_send(uint16_t data);
65void host_consumer_send(uint16_t data);
66 68
67#ifdef __cplusplus 69#ifdef __cplusplus
68} 70}
diff --git a/common/keyboard.c b/common/keyboard.c
index 43abf4236..7a17a9e38 100644
--- a/common/keyboard.c
+++ b/common/keyboard.c
@@ -110,6 +110,12 @@ static void clear_keyboard_but_mods(void)
110#endif 110#endif
111} 111}
112 112
113static bool anykey_sent_to_host(void)
114{
115 return (host_has_anykey() || host_mouse_in_use() ||
116 host_last_sysytem_report() || host_last_consumer_report());
117}
118
113static void layer_switch_on(uint8_t code) 119static void layer_switch_on(uint8_t code)
114{ 120{
115 if (!IS_FN(code)) return; 121 if (!IS_FN(code)) return;
@@ -123,9 +129,9 @@ static void layer_switch_on(uint8_t code)
123 } 129 }
124} 130}
125 131
126static void layer_switch_off(uint8_t code) 132static bool layer_switch_off(uint8_t code)
127{ 133{
128 if (!IS_FN(code)) return; 134 if (!IS_FN(code)) return false;
129 fn_state_bits &= ~FN_BIT(code); 135 fn_state_bits &= ~FN_BIT(code);
130 if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { 136 if (current_layer != keymap_fn_layer(biton(fn_state_bits))) {
131 clear_keyboard_but_mods(); 137 clear_keyboard_but_mods();
@@ -133,21 +139,7 @@ static void layer_switch_off(uint8_t code)
133 debug("Layer Switch(off): "); debug_hex(current_layer); 139 debug("Layer Switch(off): "); debug_hex(current_layer);
134 current_layer = keymap_fn_layer(biton(fn_state_bits)); 140 current_layer = keymap_fn_layer(biton(fn_state_bits));
135 debug(" -> "); debug_hex(current_layer); debug("\n"); 141 debug(" -> "); debug_hex(current_layer); debug("\n");
136 } 142 return true;
137}
138
139// whether any key except modifier is down or not
140static inline bool is_anykey_down(void)
141{
142 for (int r = 0; r < MATRIX_ROWS; r++) {
143 matrix_row_t matrix_row = matrix_get_row(r);
144 for (int c = 0; c < MATRIX_COLS; c++) {
145 if (matrix_row && (1<<c)) {
146 if (IS_KEY(keymap_get_keycode(current_layer, r, c))) {
147 return true;
148 }
149 }
150 }
151 } 143 }
152 return false; 144 return false;
153} 145}
@@ -162,6 +154,10 @@ static void register_code(uint8_t code)
162 host_add_mod_bit(MOD_BIT(code)); 154 host_add_mod_bit(MOD_BIT(code));
163 host_send_keyboard_report(); 155 host_send_keyboard_report();
164 } 156 }
157 else if IS_FN(code) {
158 host_add_key(keymap_fn_keycode(FN_INDEX(code)));
159 host_send_keyboard_report();
160 }
165 else if IS_MOUSEKEY(code) { 161 else if IS_MOUSEKEY(code) {
166#ifdef MOUSEKEY_ENABLE 162#ifdef MOUSEKEY_ENABLE
167 mousekey_on(code); 163 mousekey_on(code);
@@ -256,6 +252,10 @@ static void unregister_code(uint8_t code)
256 host_del_mod_bit(MOD_BIT(code)); 252 host_del_mod_bit(MOD_BIT(code));
257 host_send_keyboard_report(); 253 host_send_keyboard_report();
258 } 254 }
255 else if IS_FN(code) {
256 host_del_key(keymap_fn_keycode(FN_INDEX(code)));
257 host_send_keyboard_report();
258 }
259 else if IS_MOUSEKEY(code) { 259 else if IS_MOUSEKEY(code) {
260#ifdef MOUSEKEY_ENABLE 260#ifdef MOUSEKEY_ENABLE
261 mousekey_off(code); 261 mousekey_off(code);
@@ -272,24 +272,31 @@ static void unregister_code(uint8_t code)
272 272
273/* 273/*
274 * 274 *
275 * Event/State|IDLE DELAYING[f] WAITING[f,k] PRESSING 275 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
276 * -----------+------------------------------------------------------------------ 276 * -----------+------------------------------------------------------------------
277 * Fn Down |IDLE(L+) WAITING(Sk) WAITING(Sk) - 277 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
278 * Up |IDLE(L-) IDLE(L-) IDLE(L-) IDLE(L-) 278 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
279 * Fnk Down |DELAYING(Sf) WAITING(Sk) WAINTING(Sk) PRESSING(Rf) 279 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
280 * Up |IDLE(L-) IDLE(Rf,Uf) IDLE(Rf,Ps,Uf)*3 PRESSING(Uf) 280 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
281 * Key Down |PRESSING(Rk) WAITING(Sk) WAITING(Sk) PRESSING(Rk) 281 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
282 * Up |IDLE(Uk) DELAYING(Uk) IDLE(L+,Ps,Uk) IDLE(Uk)*4 282 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
283 * Delay |- IDLE(L+) IDLE(L+,Ps) -
284 * | 283 * |
285 * No key Down|IDLE(Ld) IDLE(Ld) IDLE(Ld) IDLE(Ld) 284 * Delay |- - IDLE(L+) IDLE(L+,Ps)
285 * Magic Key |COMMAND*5
286 * 286 *
287 * *1: ignore Fn if other key is down.
287 * *2: register Fnk if any key is pressing 288 * *2: register Fnk if any key is pressing
288 * *3: when Fnk == Stored Fnk, if not ignore. 289 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
289 * *4: when no registered key any more 290 * *4: if no keys registered to host
291 * *5: unregister all keys
292 * *6: only if no keys down
293 * *7: ignore Fn because Fnk key and stored key are down.
294 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
295 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
296 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
290 * 297 *
291 * States: 298 * States:
292 * IDLE: 299 * IDLE: No key is down except modifiers
293 * DELAYING: delay layer switch after pressing Fn with alt keycode 300 * DELAYING: delay layer switch after pressing Fn with alt keycode
294 * WAITING: key is pressed during DELAYING 301 * WAITING: key is pressed during DELAYING
295 * 302 *
@@ -297,17 +304,20 @@ static void unregister_code(uint8_t code)
297 * Fn: Fn key without alternative keycode 304 * Fn: Fn key without alternative keycode
298 * Fnk: Fn key with alternative keycode 305 * Fnk: Fn key with alternative keycode
299 * -: ignore 306 * -: ignore
307 * Delay: layer switch delay term is elapsed
300 * 308 *
301 * Actions: 309 * Actions:
302 * Rk: register key 310 * Rk: register key
303 * Uk: unregister key 311 * Uk: unregister key
304 * Rf: register stored Fn(alt keycode) 312 * Rf: register Fn(alt keycode)
305 * Uf: unregister stored Fn(alt keycode) 313 * Uf: unregister Fn(alt keycode)
306 * Rs: register stored key 314 * Rs: register stored key
307 * Us: unregister stored key 315 * Us: unregister stored key
308 * Sk: store key 316 * Sk: Store key(waiting Key)
309 * Sf: store Fn 317 * Sf: Store Fn(delayed Fn)
310 * Ps: play stored key(Interpret stored key and transit state) 318 * Ps: Process stored key
319 * Ps: Process key
320 * Is: Interpret stored keys in current layer
311 * L+: Switch to new layer(*unregister* all keys but modifiers) 321 * L+: Switch to new layer(*unregister* all keys but modifiers)
312 * L-: Switch back to last layer(*unregister* all keys but modifiers) 322 * L-: Switch back to last layer(*unregister* all keys but modifiers)
313 * Ld: Switch back to default layer(*unregister* all keys but modifiers) 323 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
@@ -344,7 +354,7 @@ static inline void process_key(keyevent_t event)
344 // repeat Fn alt key when press Fn key down, up then down again quickly 354 // repeat Fn alt key when press Fn key down, up then down again quickly
345 if (KEYEQ(delayed_fn.event.key, event.key) && 355 if (KEYEQ(delayed_fn.event.key, event.key) &&
346 timer_elapsed(delayed_fn.time) < LAYER_DELAY) { 356 timer_elapsed(delayed_fn.time) < LAYER_DELAY) {
347 register_code(keymap_fn_keycode(FN_INDEX(code))); 357 register_code(code);
348 NEXT(PRESSING); 358 NEXT(PRESSING);
349 } else { 359 } else {
350 delayed_fn = (keyrecord_t) { 360 delayed_fn = (keyrecord_t) {
@@ -380,16 +390,20 @@ static inline void process_key(keyevent_t event)
380 // ignored when any key is pressed 390 // ignored when any key is pressed
381 break; 391 break;
382 case FN_UP: 392 case FN_UP:
383 layer_switch_off(code); 393 if (layer_switch_off(code))
384 NEXT(IDLE); 394 NEXT(IDLE);
385 break; 395 break;
386 case FNK_DOWN: 396 case FNK_DOWN:
387 register_code(keymap_fn_keycode(FN_INDEX(code))); 397 register_code(code);
388 break; 398 break;
389 case FNK_UP: 399 case FNK_UP:
390 // can't know whether layer switched or not 400 if (layer_switch_off(code)) {
391 layer_switch_off(code); 401 NEXT(IDLE);
392 unregister_code(keymap_fn_keycode(FN_INDEX(code))); 402 } else {
403 unregister_code(code);
404 if (!anykey_sent_to_host())
405 NEXT(IDLE);
406 }
393 break; 407 break;
394 case KEY_DOWN: 408 case KEY_DOWN:
395 case MOD_DOWN: 409 case MOD_DOWN:
@@ -398,8 +412,7 @@ static inline void process_key(keyevent_t event)
398 case KEY_UP: 412 case KEY_UP:
399 case MOD_UP: 413 case MOD_UP:
400 unregister_code(code); 414 unregister_code(code);
401 // TODO: no key registered? mousekey, mediakey, systemkey 415 if (!anykey_sent_to_host())
402 if (!host_has_anykey())
403 NEXT(IDLE); 416 NEXT(IDLE);
404 break; 417 break;
405 default: 418 default:
@@ -423,8 +436,8 @@ static inline void process_key(keyevent_t event)
423 register_code(code); 436 register_code(code);
424 break; 437 break;
425 case FN_UP: 438 case FN_UP:
426 layer_switch_off(code); 439 if (layer_switch_off(code))
427 NEXT(IDLE); 440 NEXT(IDLE);
428 break; 441 break;
429 case FNK_UP: 442 case FNK_UP:
430 if (code == delayed_fn.code) { 443 if (code == delayed_fn.code) {
@@ -432,19 +445,16 @@ static inline void process_key(keyevent_t event)
432 // restore the mod status at the time of pressing Fn key 445 // restore the mod status at the time of pressing Fn key
433 tmp_mods = keyboard_report->mods; 446 tmp_mods = keyboard_report->mods;
434 host_set_mods(delayed_fn.mods); 447 host_set_mods(delayed_fn.mods);
435 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); 448 register_code(delayed_fn.code);
436 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); 449 unregister_code(delayed_fn.code);
437 host_set_mods(tmp_mods); 450 host_set_mods(tmp_mods);
438 NEXT(IDLE); 451 NEXT(IDLE);
439 } else { 452 } else {
440 layer_switch_off(code); 453 if (layer_switch_off(code))
441 NEXT(IDLE); 454 NEXT(IDLE);
442 } 455 }
443 break; 456 break;
444 case KEY_UP: 457 case KEY_UP:
445 unregister_code(code);
446 NEXT(IDLE);
447 break;
448 case MOD_UP: 458 case MOD_UP:
449 unregister_code(code); 459 unregister_code(code);
450 break; 460 break;
@@ -459,34 +469,40 @@ static inline void process_key(keyevent_t event)
459 case KEY_DOWN: 469 case KEY_DOWN:
460 tmp_mods = keyboard_report->mods; 470 tmp_mods = keyboard_report->mods;
461 host_set_mods(delayed_fn.mods); 471 host_set_mods(delayed_fn.mods);
462 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); 472 register_code(delayed_fn.code);
463 host_set_mods(waiting_key.mods); 473 host_set_mods(waiting_key.mods);
464 register_code(waiting_key.code); 474 register_code(waiting_key.code);
465 host_set_mods(tmp_mods); 475 host_set_mods(tmp_mods);
466 register_code(code); 476 if (kind == FN_DOWN) {
477 // ignore Fn
478 } else if (kind == FNK_DOWN) {
479 register_code(code);
480 } else if (kind == KEY_DOWN) {
481 register_code(code);
482 }
467 NEXT(IDLE); 483 NEXT(IDLE);
468 break; 484 break;
469 case MOD_DOWN: 485 case MOD_DOWN:
470 register_code(code); 486 register_code(code);
471 break; 487 break;
472 case FN_UP: 488 case FN_UP:
473 layer_switch_off(code); 489 if (layer_switch_off(code))
474 NEXT(IDLE); 490 NEXT(IDLE);
475 break; 491 break;
476 case FNK_UP: 492 case FNK_UP:
477 if (code == delayed_fn.code) { 493 if (code == delayed_fn.code) {
478 // alt down, key down, alt up 494 // alt down, key down, alt up
479 tmp_mods = keyboard_report->mods; 495 tmp_mods = keyboard_report->mods;
480 host_set_mods(delayed_fn.mods); 496 host_set_mods(delayed_fn.mods);
481 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); 497 register_code(delayed_fn.code);
482 host_set_mods(waiting_key.mods); 498 host_set_mods(waiting_key.mods);
483 register_code(waiting_key.code); 499 register_code(waiting_key.code);
484 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); 500 unregister_code(delayed_fn.code);
485 host_set_mods(tmp_mods); 501 host_set_mods(tmp_mods);
486 NEXT(IDLE); 502 NEXT(IDLE);
487 } else { 503 } else {
488 layer_switch_off(code); 504 if (layer_switch_off(code))
489 NEXT(IDLE); 505 NEXT(IDLE);
490 } 506 }
491 break; 507 break;
492 case KEY_UP: 508 case KEY_UP:
diff --git a/common/mousekey.c b/common/mousekey.c
index 4b1fe1740..353890a16 100644
--- a/common/mousekey.c
+++ b/common/mousekey.c
@@ -25,7 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
25#include "mousekey.h" 25#include "mousekey.h"
26 26
27 27
28static report_mouse_t report;
29 28
30static uint8_t mousekey_repeat = 0; 29static uint8_t mousekey_repeat = 0;
31 30
@@ -115,89 +114,89 @@ void mousekey_task(void)
115 if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10)) 114 if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10))
116 return; 115 return;
117 116
118 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0) 117 if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
119 return; 118 return;
120 119
121 if (mousekey_repeat != UINT8_MAX) 120 if (mousekey_repeat != UINT8_MAX)
122 mousekey_repeat++; 121 mousekey_repeat++;
123 122
124 123
125 if (report.x > 0) report.x = move_unit(); 124 if (mouse_report.x > 0) mouse_report.x = move_unit();
126 if (report.x < 0) report.x = move_unit() * -1; 125 if (mouse_report.x < 0) mouse_report.x = move_unit() * -1;
127 if (report.y > 0) report.y = move_unit(); 126 if (mouse_report.y > 0) mouse_report.y = move_unit();
128 if (report.y < 0) report.y = move_unit() * -1; 127 if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;
129 128
130 if (report.x && report.y) { 129 if (mouse_report.x && mouse_report.y) {
131 report.x *= 0.7; 130 mouse_report.x *= 0.7;
132 report.y *= 0.7; 131 mouse_report.y *= 0.7;
133 } 132 }
134 133
135 if (report.v > 0) report.v = wheel_unit(); 134 if (mouse_report.v > 0) mouse_report.v = wheel_unit();
136 if (report.v < 0) report.v = wheel_unit() * -1; 135 if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1;
137 if (report.h > 0) report.h = wheel_unit(); 136 if (mouse_report.h > 0) mouse_report.h = wheel_unit();
138 if (report.h < 0) report.h = wheel_unit() * -1; 137 if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1;
139 138
140 mousekey_send(); 139 mousekey_send();
141} 140}
142 141
143void mousekey_on(uint8_t code) 142void mousekey_on(uint8_t code)
144{ 143{
145 if (code == KC_MS_UP) report.y = MOUSEKEY_MOVE_DELTA * -1; 144 if (code == KC_MS_UP) mouse_report.y = MOUSEKEY_MOVE_DELTA * -1;
146 else if (code == KC_MS_DOWN) report.y = MOUSEKEY_MOVE_DELTA; 145 else if (code == KC_MS_DOWN) mouse_report.y = MOUSEKEY_MOVE_DELTA;
147 else if (code == KC_MS_LEFT) report.x = MOUSEKEY_MOVE_DELTA * -1; 146 else if (code == KC_MS_LEFT) mouse_report.x = MOUSEKEY_MOVE_DELTA * -1;
148 else if (code == KC_MS_RIGHT) report.x = MOUSEKEY_MOVE_DELTA; 147 else if (code == KC_MS_RIGHT) mouse_report.x = MOUSEKEY_MOVE_DELTA;
149 else if (code == KC_MS_WH_UP) report.v = MOUSEKEY_WHEEL_DELTA; 148 else if (code == KC_MS_WH_UP) mouse_report.v = MOUSEKEY_WHEEL_DELTA;
150 else if (code == KC_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_DELTA * -1; 149 else if (code == KC_MS_WH_DOWN) mouse_report.v = MOUSEKEY_WHEEL_DELTA * -1;
151 else if (code == KC_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_DELTA * -1; 150 else if (code == KC_MS_WH_LEFT) mouse_report.h = MOUSEKEY_WHEEL_DELTA * -1;
152 else if (code == KC_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_DELTA; 151 else if (code == KC_MS_WH_RIGHT) mouse_report.h = MOUSEKEY_WHEEL_DELTA;
153 else if (code == KC_MS_BTN1) report.buttons |= MOUSE_BTN1; 152 else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1;
154 else if (code == KC_MS_BTN2) report.buttons |= MOUSE_BTN2; 153 else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2;
155 else if (code == KC_MS_BTN3) report.buttons |= MOUSE_BTN3; 154 else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3;
156 else if (code == KC_MS_BTN4) report.buttons |= MOUSE_BTN4; 155 else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4;
157 else if (code == KC_MS_BTN5) report.buttons |= MOUSE_BTN5; 156 else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5;
158} 157}
159 158
160void mousekey_off(uint8_t code) 159void mousekey_off(uint8_t code)
161{ 160{
162 if (code == KC_MS_UP && report.y < 0) report.y = 0; 161 if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0;
163 else if (code == KC_MS_DOWN && report.y > 0) report.y = 0; 162 else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0;
164 else if (code == KC_MS_LEFT && report.x < 0) report.x = 0; 163 else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0;
165 else if (code == KC_MS_RIGHT && report.x > 0) report.x = 0; 164 else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0;
166 else if (code == KC_MS_WH_UP && report.v > 0) report.v = 0; 165 else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0;
167 else if (code == KC_MS_WH_DOWN && report.v < 0) report.v = 0; 166 else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0;
168 else if (code == KC_MS_WH_LEFT && report.h < 0) report.h = 0; 167 else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0;
169 else if (code == KC_MS_WH_RIGHT && report.h > 0) report.h = 0; 168 else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0;
170 else if (code == KC_MS_BTN1) report.buttons &= ~MOUSE_BTN1; 169 else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1;
171 else if (code == KC_MS_BTN2) report.buttons &= ~MOUSE_BTN2; 170 else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2;
172 else if (code == KC_MS_BTN3) report.buttons &= ~MOUSE_BTN3; 171 else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3;
173 else if (code == KC_MS_BTN4) report.buttons &= ~MOUSE_BTN4; 172 else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4;
174 else if (code == KC_MS_BTN5) report.buttons &= ~MOUSE_BTN5; 173 else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5;
175 174
176 if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0) 175 if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
177 mousekey_repeat = 0; 176 mousekey_repeat = 0;
178} 177}
179 178
180void mousekey_send(void) 179void mousekey_send(void)
181{ 180{
182 mousekey_debug(); 181 mousekey_debug();
183 host_mouse_send(&report); 182 host_mouse_send(&mouse_report);
184 last_timer = timer_read(); 183 last_timer = timer_read();
185} 184}
186 185
187void mousekey_clear(void) 186void mousekey_clear(void)
188{ 187{
189 report = (report_mouse_t){}; 188 mouse_report = (report_mouse_t){};
190} 189}
191 190
192static void mousekey_debug(void) 191static void mousekey_debug(void)
193{ 192{
194 if (!debug_mouse) return; 193 if (!debug_mouse) return;
195 print("mousekey [btn|x y v h]rep: ["); 194 print("mousekey [btn|x y v h]rep: [");
196 phex(report.buttons); print("|"); 195 phex(mouse_report.buttons); print("|");
197 phex(report.x); print(" "); 196 phex(mouse_report.x); print(" ");
198 phex(report.y); print(" "); 197 phex(mouse_report.y); print(" ");
199 phex(report.v); print(" "); 198 phex(mouse_report.v); print(" ");
200 phex(report.h); print("]"); 199 phex(mouse_report.h); print("]");
201 phex(mousekey_repeat); 200 phex(mousekey_repeat);
202 print("\n"); 201 print("\n");
203} 202}