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);
168 dprint("MODS_TAP: No tap: add_mods\n");
173 dprint("MODS_TAP: Tap: unregister_code\n");
174 unregister_code(action.key.code);
176 dprint("MODS_TAP: No tap: add_mods\n");
177 unregister_mods(mods);
185 #ifdef EXTRAKEY_ENABLE
186 /* other HID usage */
188 switch (action.usage.page) {
191 host_system_send(action.usage.code);
198 host_consumer_send(action.usage.code);
200 host_consumer_send(0);
206 #ifdef MOUSEKEY_ENABLE
210 mousekey_on(action.key.code);
213 mousekey_off(action.key.code);
218 #ifndef NO_ACTION_LAYER
220 if (action.layer_bitop.on == 0) {
221 /* Default Layer Bitwise Operation */
222 if (!event.pressed) {
223 uint8_t shift = action.layer_bitop.part*4;
224 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
225 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
226 switch (action.layer_bitop.op) {
227 case OP_BIT_AND: default_layer_and(bits | mask); break;
228 case OP_BIT_OR: default_layer_or(bits | mask); break;
229 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
230 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
234 /* Layer Bitwise Operation */
235 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
236 (action.layer_bitop.on & ON_RELEASE)) {
237 uint8_t shift = action.layer_bitop.part*4;
238 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
239 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
240 switch (action.layer_bitop.op) {
241 case OP_BIT_AND: layer_and(bits | mask); break;
242 case OP_BIT_OR: layer_or(bits | mask); break;
243 case OP_BIT_XOR: layer_xor(bits | mask); break;
244 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
249 #ifndef NO_ACTION_TAPPING
251 case ACT_LAYER_TAP_EXT:
252 switch (action.layer_tap.code) {
254 /* layer On/Off with modifiers */
256 layer_on(action.layer_tap.val);
257 register_mods((action.layer_tap.code & 0x10) ?
258 (action.layer_tap.code & 0x0f) << 4 :
259 (action.layer_tap.code & 0x0f));
261 layer_off(action.layer_tap.val);
262 unregister_mods((action.layer_tap.code & 0x10) ?
263 (action.layer_tap.code & 0x0f) << 4 :
264 (action.layer_tap.code & 0x0f));
270 if (tap_count < TAPPING_TOGGLE) {
271 layer_invert(action.layer_tap.val);
274 if (tap_count <= TAPPING_TOGGLE) {
275 layer_invert(action.layer_tap.val);
280 event.pressed ? layer_on(action.layer_tap.val) :
281 layer_off(action.layer_tap.val);
284 event.pressed ? layer_off(action.layer_tap.val) :
285 layer_on(action.layer_tap.val);
288 event.pressed ? layer_move(action.layer_tap.val) :
295 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
296 register_code(action.layer_tap.code);
298 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
299 layer_on(action.layer_tap.val);
303 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
304 unregister_code(action.layer_tap.code);
306 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
307 layer_off(action.layer_tap.val);
316 #ifndef NO_ACTION_MACRO
318 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
321 #ifdef BACKLIGHT_ENABLE
323 if (!event.pressed) {
324 switch (action.backlight.opt) {
325 case BACKLIGHT_INCREASE:
326 backlight_increase();
328 case BACKLIGHT_DECREASE:
329 backlight_decrease();
331 case BACKLIGHT_TOGGLE:
337 case BACKLIGHT_LEVEL:
338 backlight_level(action.backlight.level);
345 switch (action.command.id) {
346 case COMMAND_BOOTLOADER:
355 #ifndef NO_ACTION_FUNCTION
357 action_function(record, action.func.id, action.func.opt);
369 * Utilities for actions.
371 void register_code(uint8_t code)
377 #ifdef LOCKING_SUPPORT_ENABLE
378 else if (KC_LOCKING_CAPS == code) {
379 #ifdef LOCKING_RESYNC_ENABLE
380 // Resync: ignore if caps lock already is on
381 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
383 add_key(KC_CAPSLOCK);
384 send_keyboard_report();
386 del_key(KC_CAPSLOCK);
387 send_keyboard_report();
390 else if (KC_LOCKING_NUM == code) {
391 #ifdef LOCKING_RESYNC_ENABLE
392 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
395 send_keyboard_report();
398 send_keyboard_report();
401 else if (KC_LOCKING_SCROLL == code) {
402 #ifdef LOCKING_RESYNC_ENABLE
403 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
405 add_key(KC_SCROLLLOCK);
406 send_keyboard_report();
408 del_key(KC_SCROLLLOCK);
409 send_keyboard_report();
413 else if IS_KEY(code) {
414 // TODO: should push command_proc out of this block?
415 if (command_proc(code)) return;
417 #ifndef NO_ACTION_ONESHOT
419 if (oneshot_state.mods && !oneshot_state.disabled) {
420 uint8_t tmp_mods = get_mods();
421 add_mods(oneshot_state.mods);
424 send_keyboard_report();
427 send_keyboard_report();
434 send_keyboard_report();
437 else if IS_MOD(code) {
438 add_mods(MOD_BIT(code));
439 send_keyboard_report();
441 else if IS_SYSTEM(code) {
442 host_system_send(KEYCODE2SYSTEM(code));
444 else if IS_CONSUMER(code) {
445 host_consumer_send(KEYCODE2CONSUMER(code));
449 void unregister_code(uint8_t code)
455 #ifdef LOCKING_SUPPORT_ENABLE
456 else if (KC_LOCKING_CAPS == code) {
457 #ifdef LOCKING_RESYNC_ENABLE
458 // Resync: ignore if caps lock already is off
459 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
461 add_key(KC_CAPSLOCK);
462 send_keyboard_report();
464 del_key(KC_CAPSLOCK);
465 send_keyboard_report();
468 else if (KC_LOCKING_NUM == code) {
469 #ifdef LOCKING_RESYNC_ENABLE
470 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
473 send_keyboard_report();
476 send_keyboard_report();
479 else if (KC_LOCKING_SCROLL == code) {
480 #ifdef LOCKING_RESYNC_ENABLE
481 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
483 add_key(KC_SCROLLLOCK);
484 send_keyboard_report();
486 del_key(KC_SCROLLLOCK);
487 send_keyboard_report();
491 else if IS_KEY(code) {
493 send_keyboard_report();
495 else if IS_MOD(code) {
496 del_mods(MOD_BIT(code));
497 send_keyboard_report();
499 else if IS_SYSTEM(code) {
502 else if IS_CONSUMER(code) {
503 host_consumer_send(0);
507 void type_code(uint8_t code)
510 unregister_code(code);
513 void register_mods(uint8_t mods)
517 send_keyboard_report();
521 void unregister_mods(uint8_t mods)
525 send_keyboard_report();
529 void clear_keyboard(void)
532 clear_keyboard_but_mods();
535 void clear_keyboard_but_mods(void)
539 send_keyboard_report();
540 #ifdef MOUSEKEY_ENABLE
544 #ifdef EXTRAKEY_ENABLE
546 host_consumer_send(0);
550 bool is_tap_key(keyevent_t event)
552 if (IS_NOEVENT(event)) { return false; }
554 action_t action = layer_switch_get_action(event);
556 switch (action.kind.id) {
559 switch (action.key.code) {
561 case MODS_TAP_TOGGLE:
562 case KC_A ... KC_EXSEL: // tap key
563 case KC_LCTRL ... KC_RGUI: // tap key
567 case ACT_LAYER_TAP_EXT:
568 switch (action.layer_tap.code) {
569 case 0xc0 ... 0xdf: // with modifiers
571 case KC_A ... KC_EXSEL: // tap key
572 case KC_LCTRL ... KC_RGUI: // tap key
579 if (action.func.opt & FUNC_TAP) { return true; }
589 void debug_event(keyevent_t event)
591 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
594 void debug_record(keyrecord_t record)
596 debug_event(record.event);
597 #ifndef NO_ACTION_TAPPING
598 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
602 void debug_action(action_t action)
604 switch (action.kind.id) {
605 case ACT_LMODS: dprint("ACT_LMODS"); break;
606 case ACT_RMODS: dprint("ACT_RMODS"); break;
607 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
608 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
609 case ACT_USAGE: dprint("ACT_USAGE"); break;
610 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
611 case ACT_LAYER: dprint("ACT_LAYER"); break;
612 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
613 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
614 case ACT_MACRO: dprint("ACT_MACRO"); break;
615 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
616 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
617 default: dprint("UNKNOWN"); break;
619 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);