2 Copyright 2012,2013,2020 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
23 #include "backlight.h"
24 #include "action_layer.h"
25 #include "action_tapping.h"
26 #include "action_macro.h"
27 #include "action_util.h"
31 #include "bootloader.h"
40 void action_exec(keyevent_t event)
42 if (!IS_NOEVENT(event)) {
43 dprint("\n---- action_exec: start -----\n");
44 dprint("EVENT: "); debug_event(event); dprintln();
45 hook_matrix_change(event);
48 keyrecord_t record = { .event = event };
50 #ifndef NO_ACTION_TAPPING
51 action_tapping_process(record);
53 process_action(&record);
54 if (!IS_NOEVENT(record.event)) {
55 dprint("processed: "); debug_record(record); dprintln();
60 void process_action(keyrecord_t *record)
62 if (hook_process_action(record)) return;
64 keyevent_t event = record->event;
65 #ifndef NO_ACTION_TAPPING
66 uint8_t tap_count = record->tap.count;
69 if (IS_NOEVENT(event)) { return; }
71 action_t action = layer_switch_get_action(event);
72 dprint("ACTION: "); debug_action(action);
73 #ifndef NO_ACTION_LAYER
74 dprint(" layer_state: "); layer_debug();
75 dprint(" default_layer_state: "); default_layer_debug();
79 switch (action.kind.id) {
84 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
89 send_keyboard_report();
91 register_code(action.key.code);
93 unregister_code(action.key.code);
96 send_keyboard_report();
101 #ifndef NO_ACTION_TAPPING
105 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
107 switch (action.key.code) {
108 #ifndef NO_ACTION_ONESHOT
112 if (tap_count == 0) {
115 else if (tap_count == 1) {
116 dprint("MODS_TAP: Oneshot: start\n");
117 set_oneshot_mods(mods);
123 if (tap_count == 0) {
124 clear_oneshot_mods();
125 unregister_mods(mods);
127 else if (tap_count == 1) {
128 // Retain Oneshot mods
131 clear_oneshot_mods();
132 unregister_mods(mods);
137 case MODS_TAP_TOGGLE:
139 if (tap_count <= TAPPING_TOGGLE) {
140 if (mods & get_mods()) {
141 dprint("MODS_TAP_TOGGLE: toggle mods off\n");
142 unregister_mods(mods);
144 dprint("MODS_TAP_TOGGLE: toggle mods on\n");
149 if (tap_count < TAPPING_TOGGLE) {
150 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
151 unregister_mods(mods);
158 if (record->tap.interrupted) {
159 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
160 // ad hoc: set 0 to cancel tap
161 record->tap.count = 0;
164 dprint("MODS_TAP: Tap: register_code\n");
165 register_code(action.key.code);
167 // Delay for MacOS #659
168 if (action.key.code == KC_CAPSLOCK ||
169 action.key.code == KC_NUMLOCK ||
170 action.key.code == KC_SCROLLLOCK) {
175 dprint("MODS_TAP: No tap: add_mods\n");
180 dprint("MODS_TAP: Tap: unregister_code\n");
181 unregister_code(action.key.code);
183 dprint("MODS_TAP: No tap: add_mods\n");
184 unregister_mods(mods);
192 #ifdef EXTRAKEY_ENABLE
193 /* other HID usage */
195 switch (action.usage.page) {
198 host_system_send(action.usage.code);
205 host_consumer_send(action.usage.code);
207 host_consumer_send(0);
213 #ifdef MOUSEKEY_ENABLE
217 mousekey_on(action.key.code);
220 mousekey_off(action.key.code);
225 #ifndef NO_ACTION_LAYER
227 if (action.layer_bitop.on == 0) {
228 /* Default Layer Bitwise Operation */
229 if (!event.pressed) {
230 uint8_t shift = action.layer_bitop.part*4;
231 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
232 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
233 switch (action.layer_bitop.op) {
234 case OP_BIT_AND: default_layer_and(bits | mask); break;
235 case OP_BIT_OR: default_layer_or(bits | mask); break;
236 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
237 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
241 /* Layer Bitwise Operation */
242 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
243 (action.layer_bitop.on & ON_RELEASE)) {
244 uint8_t shift = action.layer_bitop.part*4;
245 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
246 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
247 switch (action.layer_bitop.op) {
248 case OP_BIT_AND: layer_and(bits | mask); break;
249 case OP_BIT_OR: layer_or(bits | mask); break;
250 case OP_BIT_XOR: layer_xor(bits | mask); break;
251 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
256 #ifndef NO_ACTION_TAPPING
258 case ACT_LAYER_TAP_EXT:
259 switch (action.layer_tap.code) {
261 /* layer On/Off with modifiers */
263 layer_on(action.layer_tap.val);
264 register_mods((action.layer_tap.code & 0x10) ?
265 (action.layer_tap.code & 0x0f) << 4 :
266 (action.layer_tap.code & 0x0f));
268 layer_off(action.layer_tap.val);
269 unregister_mods((action.layer_tap.code & 0x10) ?
270 (action.layer_tap.code & 0x0f) << 4 :
271 (action.layer_tap.code & 0x0f));
277 if (tap_count < TAPPING_TOGGLE) {
278 layer_invert(action.layer_tap.val);
281 if (tap_count <= TAPPING_TOGGLE) {
282 layer_invert(action.layer_tap.val);
287 event.pressed ? layer_on(action.layer_tap.val) :
288 layer_off(action.layer_tap.val);
291 event.pressed ? layer_off(action.layer_tap.val) :
292 layer_on(action.layer_tap.val);
295 event.pressed ? layer_move(action.layer_tap.val) :
302 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
303 register_code(action.layer_tap.code);
305 // Delay for MacOS #659
306 if (action.layer_tap.code == KC_CAPSLOCK ||
307 action.layer_tap.code == KC_NUMLOCK ||
308 action.layer_tap.code == KC_SCROLLLOCK) {
312 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
313 layer_on(action.layer_tap.val);
317 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
318 unregister_code(action.layer_tap.code);
320 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
321 layer_off(action.layer_tap.val);
330 #ifndef NO_ACTION_MACRO
332 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
335 #ifdef BACKLIGHT_ENABLE
337 if (!event.pressed) {
338 switch (action.backlight.opt) {
339 case BACKLIGHT_INCREASE:
340 backlight_increase();
342 case BACKLIGHT_DECREASE:
343 backlight_decrease();
345 case BACKLIGHT_TOGGLE:
351 case BACKLIGHT_LEVEL:
352 backlight_level(action.backlight.level);
359 switch (action.command.id) {
360 case COMMAND_BOOTLOADER:
369 #ifndef NO_ACTION_FUNCTION
371 action_function(record, action.func.id, action.func.opt);
383 * Utilities for actions.
385 void register_code(uint8_t code)
391 #ifdef LOCKING_SUPPORT_ENABLE
392 else if (code == KC_LOCKING_CAPS ||
393 code == KC_LOCKING_NUM ||
394 code == KC_LOCKING_SCROLL) {
396 if (code == KC_LOCKING_CAPS) {
398 l = 1<<USB_LED_CAPS_LOCK;
399 } else if (code == KC_LOCKING_NUM) {
401 l = 1<<USB_LED_NUM_LOCK;
402 } else if (code == KC_LOCKING_SCROLL) {
404 l = 1<<USB_LED_SCROLL_LOCK;
406 #ifdef LOCKING_RESYNC_ENABLE
407 // Resync: ignore if lock indicator is already on
408 if (host_keyboard_leds() & l) return;
411 send_keyboard_report();
412 wait_ms(100); // Delay for MacOS #390
414 send_keyboard_report();
418 else if IS_KEY(code) {
419 // TODO: should push command_proc out of this block?
420 if (command_proc(code)) return;
422 #ifndef NO_ACTION_ONESHOT
424 if (oneshot_state.mods && !oneshot_state.disabled) {
425 uint8_t tmp_mods = get_mods();
426 add_mods(oneshot_state.mods);
429 send_keyboard_report();
432 send_keyboard_report();
439 send_keyboard_report();
442 else if IS_MOD(code) {
443 add_mods(MOD_BIT(code));
444 send_keyboard_report();
446 else if IS_SYSTEM(code) {
447 host_system_send(KEYCODE2SYSTEM(code));
449 else if IS_CONSUMER(code) {
450 host_consumer_send(KEYCODE2CONSUMER(code));
454 void unregister_code(uint8_t code)
460 #ifdef LOCKING_SUPPORT_ENABLE
461 else if (code == KC_LOCKING_CAPS ||
462 code == KC_LOCKING_NUM ||
463 code == KC_LOCKING_SCROLL) {
465 if (code == KC_LOCKING_CAPS) {
467 l = 1<<USB_LED_CAPS_LOCK;
468 } else if (code == KC_LOCKING_NUM) {
470 l = 1<<USB_LED_NUM_LOCK;
471 } else if (code == KC_LOCKING_SCROLL) {
473 l = 1<<USB_LED_SCROLL_LOCK;
475 #ifdef LOCKING_RESYNC_ENABLE
476 // Resync: ignore if lock indicator is already off
477 if (!(host_keyboard_leds() & l)) return;
480 send_keyboard_report();
481 wait_ms(100); // Delay for MacOS #390
483 send_keyboard_report();
487 else if IS_KEY(code) {
489 send_keyboard_report();
491 else if IS_MOD(code) {
492 del_mods(MOD_BIT(code));
493 send_keyboard_report();
495 else if IS_SYSTEM(code) {
498 else if IS_CONSUMER(code) {
499 host_consumer_send(0);
503 void type_code(uint8_t code)
506 unregister_code(code);
509 void register_mods(uint8_t mods)
513 send_keyboard_report();
517 void unregister_mods(uint8_t mods)
521 send_keyboard_report();
525 void clear_keyboard(void)
528 clear_keyboard_but_mods();
531 void clear_keyboard_but_mods(void)
535 send_keyboard_report();
536 #ifdef MOUSEKEY_ENABLE
540 #ifdef EXTRAKEY_ENABLE
542 host_consumer_send(0);
546 bool is_tap_key(keyevent_t event)
548 if (IS_NOEVENT(event)) { return false; }
550 action_t action = layer_switch_get_action(event);
552 switch (action.kind.id) {
555 switch (action.key.code) {
557 case MODS_TAP_TOGGLE:
558 case KC_A ... KC_EXSEL: // tap key
559 case KC_LCTRL ... KC_RGUI: // tap key
563 case ACT_LAYER_TAP_EXT:
564 switch (action.layer_tap.code) {
565 case 0xc0 ... 0xdf: // with modifiers
567 case KC_A ... KC_EXSEL: // tap key
568 case KC_LCTRL ... KC_RGUI: // tap key
575 if (action.func.opt & FUNC_TAP) { return true; }
585 void debug_event(keyevent_t event)
587 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
590 void debug_record(keyrecord_t record)
592 debug_event(record.event);
593 #ifndef NO_ACTION_TAPPING
594 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
598 void debug_action(action_t action)
600 switch (action.kind.id) {
601 case ACT_LMODS: dprint("ACT_LMODS"); break;
602 case ACT_RMODS: dprint("ACT_RMODS"); break;
603 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
604 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
605 case ACT_USAGE: dprint("ACT_USAGE"); break;
606 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
607 case ACT_LAYER: dprint("ACT_LAYER"); break;
608 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
609 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
610 case ACT_MACRO: dprint("ACT_MACRO"); break;
611 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
612 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
613 default: dprint("UNKNOWN"); break;
615 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);