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