2 Copyright 2012,2013 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"
37 void action_exec(keyevent_t event)
39 if (!IS_NOEVENT(event)) {
40 dprint("\n---- action_exec: start -----\n");
41 dprint("EVENT: "); debug_event(event); dprintln();
44 keyrecord_t record = { .event = event };
46 #ifndef NO_ACTION_TAPPING
47 action_tapping_process(record);
49 process_action(&record);
50 if (!IS_NOEVENT(record.event)) {
51 dprint("processed: "); debug_record(record); dprintln();
56 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
57 bool disable_action_cache = false;
59 void process_action_nocache(keyrecord_t *record)
61 disable_action_cache = true;
62 process_action(record);
63 disable_action_cache = false;
66 void process_action_nocache(keyrecord_t *record)
68 process_action(record);
73 * Make sure the action triggered when the key is released is the same
74 * one as the one triggered on press. It's important for the mod keys
75 * when the layer is switched after the down event but before the up
76 * event as they may get stuck otherwise.
78 action_t store_or_get_action(bool pressed, keypos_t key)
80 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
81 static action_t pressed_actions[MATRIX_ROWS][MATRIX_COLS];
83 if (disable_action_cache) {
84 return layer_switch_get_action(key);
88 pressed_actions[key.row][key.col] = layer_switch_get_action(key);
90 return pressed_actions[key.row][key.col];
92 return layer_switch_get_action(key);
96 void process_action(keyrecord_t *record)
98 keyevent_t event = record->event;
99 #ifndef NO_ACTION_TAPPING
100 uint8_t tap_count = record->tap.count;
103 if (IS_NOEVENT(event)) { return; }
105 action_t action = store_or_get_action(event.pressed, event.key);
106 dprint("ACTION: "); debug_action(action);
107 #ifndef NO_ACTION_LAYER
108 dprint(" layer_state: "); layer_debug();
109 dprint(" default_layer_state: "); default_layer_debug();
113 switch (action.kind.id) {
118 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
123 send_keyboard_report();
125 register_code(action.key.code);
127 unregister_code(action.key.code);
130 send_keyboard_report();
135 #ifndef NO_ACTION_TAPPING
139 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
141 switch (action.layer_tap.code) {
142 #ifndef NO_ACTION_ONESHOT
146 if (tap_count == 0) {
149 else if (tap_count == 1) {
150 dprint("MODS_TAP: Oneshot: start\n");
151 set_oneshot_mods(mods);
157 if (tap_count == 0) {
158 clear_oneshot_mods();
159 unregister_mods(mods);
161 else if (tap_count == 1) {
162 // Retain Oneshot mods
165 clear_oneshot_mods();
166 unregister_mods(mods);
171 case MODS_TAP_TOGGLE:
173 if (tap_count <= TAPPING_TOGGLE) {
177 if (tap_count < TAPPING_TOGGLE) {
178 unregister_mods(mods);
185 #ifndef IGNORE_MOD_TAP_INTERRUPT
186 if (record->tap.interrupted) {
187 dprint("mods_tap: tap: cancel: add_mods\n");
188 // ad hoc: set 0 to cancel tap
189 record->tap.count = 0;
194 dprint("MODS_TAP: Tap: register_code\n");
195 register_code(action.key.code);
198 dprint("MODS_TAP: No tap: add_mods\n");
203 dprint("MODS_TAP: Tap: unregister_code\n");
204 unregister_code(action.key.code);
206 dprint("MODS_TAP: No tap: add_mods\n");
207 unregister_mods(mods);
215 #ifdef EXTRAKEY_ENABLE
216 /* other HID usage */
218 switch (action.usage.page) {
221 host_system_send(action.usage.code);
228 host_consumer_send(action.usage.code);
230 host_consumer_send(0);
236 #ifdef MOUSEKEY_ENABLE
240 mousekey_on(action.key.code);
243 mousekey_off(action.key.code);
248 #ifndef NO_ACTION_LAYER
250 if (action.layer_bitop.on == 0) {
251 /* Default Layer Bitwise Operation */
252 if (!event.pressed) {
253 uint8_t shift = action.layer_bitop.part*4;
254 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
255 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
256 switch (action.layer_bitop.op) {
257 case OP_BIT_AND: default_layer_and(bits | mask); break;
258 case OP_BIT_OR: default_layer_or(bits | mask); break;
259 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
260 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
264 /* Layer Bitwise Operation */
265 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
266 (action.layer_bitop.on & ON_RELEASE)) {
267 uint8_t shift = action.layer_bitop.part*4;
268 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
269 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
270 switch (action.layer_bitop.op) {
271 case OP_BIT_AND: layer_and(bits | mask); break;
272 case OP_BIT_OR: layer_or(bits | mask); break;
273 case OP_BIT_XOR: layer_xor(bits | mask); break;
274 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
279 #ifndef NO_ACTION_TAPPING
281 case ACT_LAYER_TAP_EXT:
282 switch (action.layer_tap.code) {
284 /* layer On/Off with modifiers(left only) */
286 layer_on(action.layer_tap.val);
287 register_mods(action.layer_tap.code & 0x0f);
289 layer_off(action.layer_tap.val);
290 unregister_mods(action.layer_tap.code & 0x0f);
296 if (tap_count < TAPPING_TOGGLE) {
297 layer_invert(action.layer_tap.val);
300 if (tap_count <= TAPPING_TOGGLE) {
301 layer_invert(action.layer_tap.val);
306 event.pressed ? layer_on(action.layer_tap.val) :
307 layer_off(action.layer_tap.val);
310 event.pressed ? layer_off(action.layer_tap.val) :
311 layer_on(action.layer_tap.val);
314 event.pressed ? layer_move(action.layer_tap.val) :
321 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
322 register_code(action.layer_tap.code);
324 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
325 layer_on(action.layer_tap.val);
329 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
330 unregister_code(action.layer_tap.code);
332 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
333 layer_off(action.layer_tap.val);
342 #ifndef NO_ACTION_MACRO
344 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
347 #ifdef BACKLIGHT_ENABLE
349 if (!event.pressed) {
350 switch (action.backlight.opt) {
351 case BACKLIGHT_INCREASE:
352 backlight_increase();
354 case BACKLIGHT_DECREASE:
355 backlight_decrease();
357 case BACKLIGHT_TOGGLE:
363 case BACKLIGHT_LEVEL:
364 backlight_level(action.backlight.level);
372 #ifndef NO_ACTION_FUNCTION
374 action_function(record, action.func.id, action.func.opt);
386 * Utilities for actions.
388 void register_code(uint8_t code)
394 #ifdef LOCKING_SUPPORT_ENABLE
395 else if (KC_LOCKING_CAPS == code) {
396 #ifdef LOCKING_RESYNC_ENABLE
397 // Resync: ignore if caps lock already is on
398 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
400 add_key(KC_CAPSLOCK);
401 send_keyboard_report();
402 del_key(KC_CAPSLOCK);
403 send_keyboard_report();
406 else if (KC_LOCKING_NUM == code) {
407 #ifdef LOCKING_RESYNC_ENABLE
408 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
411 send_keyboard_report();
413 send_keyboard_report();
416 else if (KC_LOCKING_SCROLL == code) {
417 #ifdef LOCKING_RESYNC_ENABLE
418 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
420 add_key(KC_SCROLLLOCK);
421 send_keyboard_report();
422 del_key(KC_SCROLLLOCK);
423 send_keyboard_report();
427 else if IS_KEY(code) {
428 // TODO: should push command_proc out of this block?
429 if (command_proc(code)) return;
431 #ifndef NO_ACTION_ONESHOT
433 if (oneshot_state.mods && !oneshot_state.disabled) {
434 uint8_t tmp_mods = get_mods();
435 add_mods(oneshot_state.mods);
438 send_keyboard_report();
441 send_keyboard_report();
448 send_keyboard_report();
451 else if IS_MOD(code) {
452 add_mods(MOD_BIT(code));
453 send_keyboard_report();
455 else if IS_SYSTEM(code) {
456 host_system_send(KEYCODE2SYSTEM(code));
458 else if IS_CONSUMER(code) {
459 host_consumer_send(KEYCODE2CONSUMER(code));
463 void unregister_code(uint8_t code)
469 #ifdef LOCKING_SUPPORT_ENABLE
470 else if (KC_LOCKING_CAPS == code) {
471 #ifdef LOCKING_RESYNC_ENABLE
472 // Resync: ignore if caps lock already is off
473 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
475 add_key(KC_CAPSLOCK);
476 send_keyboard_report();
477 del_key(KC_CAPSLOCK);
478 send_keyboard_report();
481 else if (KC_LOCKING_NUM == code) {
482 #ifdef LOCKING_RESYNC_ENABLE
483 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
486 send_keyboard_report();
488 send_keyboard_report();
491 else if (KC_LOCKING_SCROLL == code) {
492 #ifdef LOCKING_RESYNC_ENABLE
493 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
495 add_key(KC_SCROLLLOCK);
496 send_keyboard_report();
497 del_key(KC_SCROLLLOCK);
498 send_keyboard_report();
502 else if IS_KEY(code) {
504 send_keyboard_report();
506 else if IS_MOD(code) {
507 del_mods(MOD_BIT(code));
508 send_keyboard_report();
510 else if IS_SYSTEM(code) {
513 else if IS_CONSUMER(code) {
514 host_consumer_send(0);
518 void register_mods(uint8_t mods)
522 send_keyboard_report();
526 void unregister_mods(uint8_t mods)
530 send_keyboard_report();
534 void clear_keyboard(void)
537 clear_keyboard_but_mods();
540 void clear_keyboard_but_mods(void)
544 send_keyboard_report();
545 #ifdef MOUSEKEY_ENABLE
549 #ifdef EXTRAKEY_ENABLE
551 host_consumer_send(0);
555 bool is_tap_key(keypos_t key)
557 action_t action = layer_switch_get_action(key);
559 switch (action.kind.id) {
563 case ACT_LAYER_TAP_EXT:
564 switch (action.layer_tap.code) {
572 if (action.func.opt & FUNC_TAP) { return true; }
582 void debug_event(keyevent_t event)
584 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
587 void debug_record(keyrecord_t record)
589 debug_event(record.event);
590 #ifndef NO_ACTION_TAPPING
591 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
595 void debug_action(action_t action)
597 switch (action.kind.id) {
598 case ACT_LMODS: dprint("ACT_LMODS"); break;
599 case ACT_RMODS: dprint("ACT_RMODS"); break;
600 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
601 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
602 case ACT_USAGE: dprint("ACT_USAGE"); break;
603 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
604 case ACT_LAYER: dprint("ACT_LAYER"); break;
605 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
606 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
607 case ACT_MACRO: dprint("ACT_MACRO"); break;
608 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
609 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
610 default: dprint("UNKNOWN"); break;
612 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);