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