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