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