]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/common/action.c
core:adb_usb: Add Extended Mouse Protocol support #274
[max/tmk_keyboard.git] / tmk_core / common / action.c
1 /*
2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "host.h"
18 #include "keycode.h"
19 #include "keyboard.h"
20 #include "mousekey.h"
21 #include "command.h"
22 #include "led.h"
23 #include "backlight.h"
24 #include "action_layer.h"
25 #include "action_tapping.h"
26 #include "action_macro.h"
27 #include "action_util.h"
28 #include "action.h"
29 #include "hook.h"
30 #include "wait.h"
31
32 #ifdef DEBUG_ACTION
33 #include "debug.h"
34 #else
35 #include "nodebug.h"
36 #endif
37
38
39 void action_exec(keyevent_t event)
40 {
41     if (!IS_NOEVENT(event)) {
42         dprint("\n---- action_exec: start -----\n");
43         dprint("EVENT: "); debug_event(event); dprintln();
44         hook_matrix_change(event);
45     }
46
47     keyrecord_t record = { .event = event };
48
49 #ifndef NO_ACTION_TAPPING
50     action_tapping_process(record);
51 #else
52     process_action(&record);
53     if (!IS_NOEVENT(record.event)) {
54         dprint("processed: "); debug_record(record); dprintln();
55     }
56 #endif
57 }
58
59 void process_action(keyrecord_t *record)
60 {
61     if (hook_process_action(record)) return;
62
63     keyevent_t event = record->event;
64 #ifndef NO_ACTION_TAPPING
65     uint8_t tap_count = record->tap.count;
66 #endif
67
68     if (IS_NOEVENT(event)) { return; }
69
70     action_t action = layer_switch_get_action(event);
71     dprint("ACTION: "); debug_action(action);
72 #ifndef NO_ACTION_LAYER
73     dprint(" layer_state: "); layer_debug();
74     dprint(" default_layer_state: "); default_layer_debug();
75 #endif
76     dprintln();
77
78     switch (action.kind.id) {
79         /* Key and Mods */
80         case ACT_LMODS:
81         case ACT_RMODS:
82             {
83                 uint8_t mods = (action.kind.id == ACT_LMODS) ?  action.key.mods :
84                                                                 action.key.mods<<4;
85                 if (event.pressed) {
86                     if (mods) {
87                         add_weak_mods(mods);
88                         send_keyboard_report();
89                     }
90                     register_code(action.key.code);
91                 } else {
92                     unregister_code(action.key.code);
93                     if (mods) {
94                         del_weak_mods(mods);
95                         send_keyboard_report();
96                     }
97                 }
98             }
99             break;
100 #ifndef NO_ACTION_TAPPING
101         case ACT_LMODS_TAP:
102         case ACT_RMODS_TAP:
103             {
104                 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ?  action.key.mods :
105                                                                     action.key.mods<<4;
106                 switch (action.key.code) {
107     #ifndef NO_ACTION_ONESHOT
108                     case MODS_ONESHOT:
109                         // Oneshot modifier
110                         if (event.pressed) {
111                             if (tap_count == 0) {
112                                 register_mods(mods);
113                             }
114                             else if (tap_count == 1) {
115                                 dprint("MODS_TAP: Oneshot: start\n");
116                                 set_oneshot_mods(mods);
117                             }
118                             else {
119                                 register_mods(mods);
120                             }
121                         } else {
122                             if (tap_count == 0) {
123                                 clear_oneshot_mods();
124                                 unregister_mods(mods);
125                             }
126                             else if (tap_count == 1) {
127                                 // Retain Oneshot mods
128                             }
129                             else {
130                                 clear_oneshot_mods();
131                                 unregister_mods(mods);
132                             }
133                         }
134                         break;
135     #endif
136                     case MODS_TAP_TOGGLE:
137                         if (event.pressed) {
138                             if (tap_count <= TAPPING_TOGGLE) {
139                                 if (mods & get_mods()) {
140                                     dprint("MODS_TAP_TOGGLE: toggle mods off\n");
141                                     unregister_mods(mods);
142                                 } else {
143                                     dprint("MODS_TAP_TOGGLE: toggle mods on\n");
144                                     register_mods(mods);
145                                 }
146                             }
147                         } else {
148                             if (tap_count < TAPPING_TOGGLE) {
149                                 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
150                                 unregister_mods(mods);
151                             }
152                         }
153                         break;
154                     default:
155                         if (event.pressed) {
156                             if (tap_count > 0) {
157                                 if (record->tap.interrupted) {
158                                     dprint("MODS_TAP: Tap: Cancel: add_mods\n");
159                                     // ad hoc: set 0 to cancel tap
160                                     record->tap.count = 0;
161                                     register_mods(mods);
162                                 } else {
163                                     dprint("MODS_TAP: Tap: register_code\n");
164                                     register_code(action.key.code);
165                                 }
166                             } else {
167                                 dprint("MODS_TAP: No tap: add_mods\n");
168                                 register_mods(mods);
169                             }
170                         } else {
171                             if (tap_count > 0) {
172                                 dprint("MODS_TAP: Tap: unregister_code\n");
173                                 unregister_code(action.key.code);
174                             } else {
175                                 dprint("MODS_TAP: No tap: add_mods\n");
176                                 unregister_mods(mods);
177                             }
178                         }
179                         break;
180                 }
181             }
182             break;
183 #endif
184 #ifdef EXTRAKEY_ENABLE
185         /* other HID usage */
186         case ACT_USAGE:
187             switch (action.usage.page) {
188                 case PAGE_SYSTEM:
189                     if (event.pressed) {
190                         host_system_send(action.usage.code);
191                     } else {
192                         host_system_send(0);
193                     }
194                     break;
195                 case PAGE_CONSUMER:
196                     if (event.pressed) {
197                         host_consumer_send(action.usage.code);
198                     } else {
199                         host_consumer_send(0);
200                     }
201                     break;
202             }
203             break;
204 #endif
205 #ifdef MOUSEKEY_ENABLE
206         /* Mouse key */
207         case ACT_MOUSEKEY:
208             if (event.pressed) {
209                 mousekey_on(action.key.code);
210                 mousekey_send();
211             } else {
212                 mousekey_off(action.key.code);
213                 mousekey_send();
214             }
215             break;
216 #endif
217 #ifndef NO_ACTION_LAYER
218         case ACT_LAYER:
219             if (action.layer_bitop.on == 0) {
220                 /* Default Layer Bitwise Operation */
221                 if (!event.pressed) {
222                     uint8_t shift = action.layer_bitop.part*4;
223                     uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
224                     uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
225                     switch (action.layer_bitop.op) {
226                         case OP_BIT_AND: default_layer_and(bits | mask); break;
227                         case OP_BIT_OR:  default_layer_or(bits | mask);  break;
228                         case OP_BIT_XOR: default_layer_xor(bits | mask); break;
229                         case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
230                     }
231                 }
232             } else {
233                 /* Layer Bitwise Operation */
234                 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
235                                     (action.layer_bitop.on & ON_RELEASE)) {
236                     uint8_t shift = action.layer_bitop.part*4;
237                     uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
238                     uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
239                     switch (action.layer_bitop.op) {
240                         case OP_BIT_AND: layer_and(bits | mask); break;
241                         case OP_BIT_OR:  layer_or(bits | mask);  break;
242                         case OP_BIT_XOR: layer_xor(bits | mask); break;
243                         case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
244                     }
245                 }
246             }
247             break;
248     #ifndef NO_ACTION_TAPPING
249         case ACT_LAYER_TAP:
250         case ACT_LAYER_TAP_EXT:
251             switch (action.layer_tap.code) {
252                 case 0xc0 ... 0xdf:
253                     /* layer On/Off with modifiers */
254                     if (event.pressed) {
255                         layer_on(action.layer_tap.val);
256                         register_mods((action.layer_tap.code & 0x10) ?
257                                 (action.layer_tap.code & 0x0f) << 4 :
258                                 (action.layer_tap.code & 0x0f));
259                     } else {
260                         layer_off(action.layer_tap.val);
261                         unregister_mods((action.layer_tap.code & 0x10) ?
262                                 (action.layer_tap.code & 0x0f) << 4 :
263                                 (action.layer_tap.code & 0x0f));
264                     }
265                     break;
266                 case OP_TAP_TOGGLE:
267                     /* tap toggle */
268                     if (event.pressed) {
269                         if (tap_count < TAPPING_TOGGLE) {
270                             layer_invert(action.layer_tap.val);
271                         }
272                     } else {
273                         if (tap_count <= TAPPING_TOGGLE) {
274                             layer_invert(action.layer_tap.val);
275                         }
276                     }
277                     break;
278                 case OP_ON_OFF:
279                     event.pressed ? layer_on(action.layer_tap.val) :
280                                     layer_off(action.layer_tap.val);
281                     break;
282                 case OP_OFF_ON:
283                     event.pressed ? layer_off(action.layer_tap.val) :
284                                     layer_on(action.layer_tap.val);
285                     break;
286                 case OP_SET_CLEAR:
287                     event.pressed ? layer_move(action.layer_tap.val) :
288                                     layer_clear();
289                     break;
290                 default:
291                     /* tap key */
292                     if (event.pressed) {
293                         if (tap_count > 0) {
294                             dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
295                             register_code(action.layer_tap.code);
296                         } else {
297                             dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
298                             layer_on(action.layer_tap.val);
299                         }
300                     } else {
301                         if (tap_count > 0) {
302                             dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
303                             unregister_code(action.layer_tap.code);
304                         } else {
305                             dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
306                             layer_off(action.layer_tap.val);
307                         }
308                     }
309                     break;
310             }
311             break;
312     #endif
313 #endif
314         /* Extentions */
315 #ifndef NO_ACTION_MACRO
316         case ACT_MACRO:
317             action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
318             break;
319 #endif
320 #ifdef BACKLIGHT_ENABLE
321         case ACT_BACKLIGHT:
322             if (!event.pressed) {
323                 switch (action.backlight.opt) {
324                     case BACKLIGHT_INCREASE:
325                         backlight_increase();
326                         break;
327                     case BACKLIGHT_DECREASE:
328                         backlight_decrease();
329                         break;
330                     case BACKLIGHT_TOGGLE:
331                         backlight_toggle();
332                         break;
333                     case BACKLIGHT_STEP:
334                         backlight_step();
335                         break;
336                     case BACKLIGHT_LEVEL:
337                         backlight_level(action.backlight.level);
338                         break;
339                 }
340             }
341             break;
342 #endif
343         case ACT_COMMAND:
344             break;
345 #ifndef NO_ACTION_FUNCTION
346         case ACT_FUNCTION:
347             action_function(record, action.func.id, action.func.opt);
348             break;
349 #endif
350         default:
351             break;
352     }
353 }
354
355
356
357
358 /*
359  * Utilities for actions.
360  */
361 void register_code(uint8_t code)
362 {
363     if (code == KC_NO) {
364         return;
365     }
366
367 #ifdef LOCKING_SUPPORT_ENABLE
368     else if (KC_LOCKING_CAPS == code) {
369 #ifdef LOCKING_RESYNC_ENABLE
370         // Resync: ignore if caps lock already is on
371         if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
372 #endif
373         add_key(KC_CAPSLOCK);
374         send_keyboard_report();
375         wait_ms(100);
376         del_key(KC_CAPSLOCK);
377         send_keyboard_report();
378     }
379
380     else if (KC_LOCKING_NUM == code) {
381 #ifdef LOCKING_RESYNC_ENABLE
382         if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
383 #endif
384         add_key(KC_NUMLOCK);
385         send_keyboard_report();
386         wait_ms(100);
387         del_key(KC_NUMLOCK);
388         send_keyboard_report();
389     }
390
391     else if (KC_LOCKING_SCROLL == code) {
392 #ifdef LOCKING_RESYNC_ENABLE
393         if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
394 #endif
395         add_key(KC_SCROLLLOCK);
396         send_keyboard_report();
397         wait_ms(100);
398         del_key(KC_SCROLLLOCK);
399         send_keyboard_report();
400     }
401 #endif
402
403     else if IS_KEY(code) {
404         // TODO: should push command_proc out of this block?
405         if (command_proc(code)) return;
406
407 #ifndef NO_ACTION_ONESHOT
408 /* TODO: remove
409         if (oneshot_state.mods && !oneshot_state.disabled) {
410             uint8_t tmp_mods = get_mods();
411             add_mods(oneshot_state.mods);
412
413             add_key(code);
414             send_keyboard_report();
415
416             set_mods(tmp_mods);
417             send_keyboard_report();
418             oneshot_cancel();
419         } else 
420 */
421 #endif
422         {
423             add_key(code);
424             send_keyboard_report();
425         }
426     }
427     else if IS_MOD(code) {
428         add_mods(MOD_BIT(code));
429         send_keyboard_report();
430     }
431     else if IS_SYSTEM(code) {
432         host_system_send(KEYCODE2SYSTEM(code));
433     }
434     else if IS_CONSUMER(code) {
435         host_consumer_send(KEYCODE2CONSUMER(code));
436     }
437 }
438
439 void unregister_code(uint8_t code)
440 {
441     if (code == KC_NO) {
442         return;
443     }
444
445 #ifdef LOCKING_SUPPORT_ENABLE
446     else if (KC_LOCKING_CAPS == code) {
447 #ifdef LOCKING_RESYNC_ENABLE
448         // Resync: ignore if caps lock already is off
449         if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
450 #endif
451         add_key(KC_CAPSLOCK);
452         send_keyboard_report();
453         wait_ms(100);
454         del_key(KC_CAPSLOCK);
455         send_keyboard_report();
456     }
457
458     else if (KC_LOCKING_NUM == code) {
459 #ifdef LOCKING_RESYNC_ENABLE
460         if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
461 #endif
462         add_key(KC_NUMLOCK);
463         send_keyboard_report();
464         wait_ms(100);
465         del_key(KC_NUMLOCK);
466         send_keyboard_report();
467     }
468
469     else if (KC_LOCKING_SCROLL == code) {
470 #ifdef LOCKING_RESYNC_ENABLE
471         if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
472 #endif
473         add_key(KC_SCROLLLOCK);
474         send_keyboard_report();
475         wait_ms(100);
476         del_key(KC_SCROLLLOCK);
477         send_keyboard_report();
478     }
479 #endif
480
481     else if IS_KEY(code) {
482         del_key(code);
483         send_keyboard_report();
484     }
485     else if IS_MOD(code) {
486         del_mods(MOD_BIT(code));
487         send_keyboard_report();
488     }
489     else if IS_SYSTEM(code) {
490         host_system_send(0);
491     }
492     else if IS_CONSUMER(code) {
493         host_consumer_send(0);
494     }
495 }
496
497 void type_code(uint8_t code)
498 {
499     register_code(code);
500     unregister_code(code);
501 }
502
503 void register_mods(uint8_t mods)
504 {
505     if (mods) {
506         add_mods(mods);
507         send_keyboard_report();
508     }
509 }
510
511 void unregister_mods(uint8_t mods)
512 {
513     if (mods) {
514         del_mods(mods);
515         send_keyboard_report();
516     }
517 }
518
519 void clear_keyboard(void)
520 {
521     clear_mods();
522     clear_keyboard_but_mods();
523 }
524
525 void clear_keyboard_but_mods(void)
526 {
527     clear_weak_mods();
528     clear_keys();
529     send_keyboard_report();
530 #ifdef MOUSEKEY_ENABLE
531     mousekey_clear();
532     mousekey_send();
533 #endif
534 #ifdef EXTRAKEY_ENABLE
535     host_system_send(0);
536     host_consumer_send(0);
537 #endif
538 }
539
540 bool is_tap_key(keyevent_t event)
541 {
542     if (IS_NOEVENT(event)) { return false; }
543
544     action_t action = layer_switch_get_action(event);
545
546     switch (action.kind.id) {
547         case ACT_LMODS_TAP:
548         case ACT_RMODS_TAP:
549             switch (action.key.code) {
550                 case MODS_ONESHOT:
551                 case MODS_TAP_TOGGLE:
552                 case KC_A ... KC_EXSEL:                 // tap key
553                 case KC_LCTRL ... KC_RGUI:              // tap key
554                     return true;
555             }
556         case ACT_LAYER_TAP:
557         case ACT_LAYER_TAP_EXT:
558             switch (action.layer_tap.code) {
559                 case 0xc0 ... 0xdf:         // with modifiers
560                     return false;
561                 case KC_A ... KC_EXSEL:     // tap key
562                 case KC_LCTRL ... KC_RGUI:  // tap key
563                 case OP_TAP_TOGGLE:
564                     return true;
565             }
566             return false;
567         case ACT_MACRO:
568         case ACT_FUNCTION:
569             if (action.func.opt & FUNC_TAP) { return true; }
570             return false;
571     }
572     return false;
573 }
574
575
576 /*
577  * debug print
578  */
579 void debug_event(keyevent_t event)
580 {
581     dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
582 }
583
584 void debug_record(keyrecord_t record)
585 {
586     debug_event(record.event);
587 #ifndef NO_ACTION_TAPPING
588     dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
589 #endif
590 }
591
592 void debug_action(action_t action)
593 {
594     switch (action.kind.id) {
595         case ACT_LMODS:             dprint("ACT_LMODS");             break;
596         case ACT_RMODS:             dprint("ACT_RMODS");             break;
597         case ACT_LMODS_TAP:         dprint("ACT_LMODS_TAP");         break;
598         case ACT_RMODS_TAP:         dprint("ACT_RMODS_TAP");         break;
599         case ACT_USAGE:             dprint("ACT_USAGE");             break;
600         case ACT_MOUSEKEY:          dprint("ACT_MOUSEKEY");          break;
601         case ACT_LAYER:             dprint("ACT_LAYER");             break;
602         case ACT_LAYER_TAP:         dprint("ACT_LAYER_TAP");         break;
603         case ACT_LAYER_TAP_EXT:     dprint("ACT_LAYER_TAP_EXT");     break;
604         case ACT_MACRO:             dprint("ACT_MACRO");             break;
605         case ACT_COMMAND:           dprint("ACT_COMMAND");           break;
606         case ACT_FUNCTION:          dprint("ACT_FUNCTION");          break;
607         default:                    dprint("UNKNOWN");               break;
608     }
609     dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
610 }