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;
58 uint8_t source_layers_cache[5][((MATRIX_ROWS * MATRIX_COLS) / 8) ? ((MATRIX_ROWS * MATRIX_COLS) / 8) : 1];
60 void process_action_nocache(keyrecord_t *record)
62 disable_action_cache = true;
63 process_action(record);
64 disable_action_cache = false;
67 void process_action_nocache(keyrecord_t *record)
69 process_action(record);
74 * Make sure the action triggered when the key is released is the same
75 * one as the one triggered on press. It's important for the mod keys
76 * when the layer is switched after the down event but before the up
77 * event as they may get stuck otherwise.
79 action_t store_or_get_action(bool pressed, keypos_t key)
81 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
82 if (disable_action_cache) {
83 return layer_switch_get_action(key);
85 uint8_t key_number = key.col + (key.row * MATRIX_COLS);
86 uint8_t storage_row = key_number / 8;
87 uint8_t storage_bit = key_number % 8;
90 layer = layer_switch_get_layer(key);
91 for (uint8_t bit_number = 0; bit_number <= 4; bit_number++) {
92 source_layers_cache[bit_number][storage_row] ^= (-(bool)((layer & (1U << bit_number)) != 0) ^ source_layers_cache[bit_number][storage_row])) & (1U << storage_bit);
97 for (uint8_t bit_number = 0; bit_number <= 4; bit_number++) {
98 layer |= (uint8_t)(!!(source_layers_cache[bit_number][storage_row] & (1U << storage_bit))) << bit_number;
101 return action_for_key(layer, key);
103 return layer_switch_get_action(key);
107 void process_action(keyrecord_t *record)
109 keyevent_t event = record->event;
110 #ifndef NO_ACTION_TAPPING
111 uint8_t tap_count = record->tap.count;
114 if (IS_NOEVENT(event)) { return; }
116 action_t action = store_or_get_action(event.pressed, event.key);
117 dprint("ACTION: "); debug_action(action);
118 #ifndef NO_ACTION_LAYER
119 dprint(" layer_state: "); layer_debug();
120 dprint(" default_layer_state: "); default_layer_debug();
124 switch (action.kind.id) {
129 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
134 send_keyboard_report();
136 register_code(action.key.code);
138 unregister_code(action.key.code);
141 send_keyboard_report();
146 #ifndef NO_ACTION_TAPPING
150 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
152 switch (action.layer_tap.code) {
153 #ifndef NO_ACTION_ONESHOT
157 if (tap_count == 0) {
160 else if (tap_count == 1) {
161 dprint("MODS_TAP: Oneshot: start\n");
162 set_oneshot_mods(mods);
168 if (tap_count == 0) {
169 clear_oneshot_mods();
170 unregister_mods(mods);
172 else if (tap_count == 1) {
173 // Retain Oneshot mods
176 clear_oneshot_mods();
177 unregister_mods(mods);
182 case MODS_TAP_TOGGLE:
184 if (tap_count <= TAPPING_TOGGLE) {
188 if (tap_count < TAPPING_TOGGLE) {
189 unregister_mods(mods);
196 #ifndef IGNORE_MOD_TAP_INTERRUPT
197 if (record->tap.interrupted) {
198 dprint("mods_tap: tap: cancel: add_mods\n");
199 // ad hoc: set 0 to cancel tap
200 record->tap.count = 0;
205 dprint("MODS_TAP: Tap: register_code\n");
206 register_code(action.key.code);
209 dprint("MODS_TAP: No tap: add_mods\n");
214 dprint("MODS_TAP: Tap: unregister_code\n");
215 unregister_code(action.key.code);
217 dprint("MODS_TAP: No tap: add_mods\n");
218 unregister_mods(mods);
226 #ifdef EXTRAKEY_ENABLE
227 /* other HID usage */
229 switch (action.usage.page) {
232 host_system_send(action.usage.code);
239 host_consumer_send(action.usage.code);
241 host_consumer_send(0);
247 #ifdef MOUSEKEY_ENABLE
251 mousekey_on(action.key.code);
254 mousekey_off(action.key.code);
259 #ifndef NO_ACTION_LAYER
261 if (action.layer_bitop.on == 0) {
262 /* Default Layer Bitwise Operation */
263 if (!event.pressed) {
264 uint8_t shift = action.layer_bitop.part*4;
265 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
266 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
267 switch (action.layer_bitop.op) {
268 case OP_BIT_AND: default_layer_and(bits | mask); break;
269 case OP_BIT_OR: default_layer_or(bits | mask); break;
270 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
271 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
275 /* Layer Bitwise Operation */
276 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
277 (action.layer_bitop.on & ON_RELEASE)) {
278 uint8_t shift = action.layer_bitop.part*4;
279 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
280 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
281 switch (action.layer_bitop.op) {
282 case OP_BIT_AND: layer_and(bits | mask); break;
283 case OP_BIT_OR: layer_or(bits | mask); break;
284 case OP_BIT_XOR: layer_xor(bits | mask); break;
285 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
290 #ifndef NO_ACTION_TAPPING
292 case ACT_LAYER_TAP_EXT:
293 switch (action.layer_tap.code) {
295 /* layer On/Off with modifiers(left only) */
297 layer_on(action.layer_tap.val);
298 register_mods(action.layer_tap.code & 0x0f);
300 layer_off(action.layer_tap.val);
301 unregister_mods(action.layer_tap.code & 0x0f);
307 if (tap_count < TAPPING_TOGGLE) {
308 layer_invert(action.layer_tap.val);
311 if (tap_count <= TAPPING_TOGGLE) {
312 layer_invert(action.layer_tap.val);
317 event.pressed ? layer_on(action.layer_tap.val) :
318 layer_off(action.layer_tap.val);
321 event.pressed ? layer_off(action.layer_tap.val) :
322 layer_on(action.layer_tap.val);
325 event.pressed ? layer_move(action.layer_tap.val) :
332 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
333 register_code(action.layer_tap.code);
335 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
336 layer_on(action.layer_tap.val);
340 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
341 unregister_code(action.layer_tap.code);
343 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
344 layer_off(action.layer_tap.val);
353 #ifndef NO_ACTION_MACRO
355 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
358 #ifdef BACKLIGHT_ENABLE
360 if (!event.pressed) {
361 switch (action.backlight.opt) {
362 case BACKLIGHT_INCREASE:
363 backlight_increase();
365 case BACKLIGHT_DECREASE:
366 backlight_decrease();
368 case BACKLIGHT_TOGGLE:
374 case BACKLIGHT_LEVEL:
375 backlight_level(action.backlight.level);
383 #ifndef NO_ACTION_FUNCTION
385 action_function(record, action.func.id, action.func.opt);
397 * Utilities for actions.
399 void register_code(uint8_t code)
405 #ifdef LOCKING_SUPPORT_ENABLE
406 else if (KC_LOCKING_CAPS == code) {
407 #ifdef LOCKING_RESYNC_ENABLE
408 // Resync: ignore if caps lock already is on
409 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
411 add_key(KC_CAPSLOCK);
412 send_keyboard_report();
413 del_key(KC_CAPSLOCK);
414 send_keyboard_report();
417 else if (KC_LOCKING_NUM == code) {
418 #ifdef LOCKING_RESYNC_ENABLE
419 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
422 send_keyboard_report();
424 send_keyboard_report();
427 else if (KC_LOCKING_SCROLL == code) {
428 #ifdef LOCKING_RESYNC_ENABLE
429 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
431 add_key(KC_SCROLLLOCK);
432 send_keyboard_report();
433 del_key(KC_SCROLLLOCK);
434 send_keyboard_report();
438 else if IS_KEY(code) {
439 // TODO: should push command_proc out of this block?
440 if (command_proc(code)) return;
442 #ifndef NO_ACTION_ONESHOT
444 if (oneshot_state.mods && !oneshot_state.disabled) {
445 uint8_t tmp_mods = get_mods();
446 add_mods(oneshot_state.mods);
449 send_keyboard_report();
452 send_keyboard_report();
459 send_keyboard_report();
462 else if IS_MOD(code) {
463 add_mods(MOD_BIT(code));
464 send_keyboard_report();
466 else if IS_SYSTEM(code) {
467 host_system_send(KEYCODE2SYSTEM(code));
469 else if IS_CONSUMER(code) {
470 host_consumer_send(KEYCODE2CONSUMER(code));
474 void unregister_code(uint8_t code)
480 #ifdef LOCKING_SUPPORT_ENABLE
481 else if (KC_LOCKING_CAPS == code) {
482 #ifdef LOCKING_RESYNC_ENABLE
483 // Resync: ignore if caps lock already is off
484 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
486 add_key(KC_CAPSLOCK);
487 send_keyboard_report();
488 del_key(KC_CAPSLOCK);
489 send_keyboard_report();
492 else if (KC_LOCKING_NUM == code) {
493 #ifdef LOCKING_RESYNC_ENABLE
494 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
497 send_keyboard_report();
499 send_keyboard_report();
502 else if (KC_LOCKING_SCROLL == code) {
503 #ifdef LOCKING_RESYNC_ENABLE
504 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
506 add_key(KC_SCROLLLOCK);
507 send_keyboard_report();
508 del_key(KC_SCROLLLOCK);
509 send_keyboard_report();
513 else if IS_KEY(code) {
515 send_keyboard_report();
517 else if IS_MOD(code) {
518 del_mods(MOD_BIT(code));
519 send_keyboard_report();
521 else if IS_SYSTEM(code) {
524 else if IS_CONSUMER(code) {
525 host_consumer_send(0);
529 void register_mods(uint8_t mods)
533 send_keyboard_report();
537 void unregister_mods(uint8_t mods)
541 send_keyboard_report();
545 void clear_keyboard(void)
548 clear_keyboard_but_mods();
551 void clear_keyboard_but_mods(void)
555 send_keyboard_report();
556 #ifdef MOUSEKEY_ENABLE
560 #ifdef EXTRAKEY_ENABLE
562 host_consumer_send(0);
566 bool is_tap_key(keypos_t key)
568 action_t action = layer_switch_get_action(key);
570 switch (action.kind.id) {
574 case ACT_LAYER_TAP_EXT:
575 switch (action.layer_tap.code) {
583 if (action.func.opt & FUNC_TAP) { return true; }
593 void debug_event(keyevent_t event)
595 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
598 void debug_record(keyrecord_t record)
600 debug_event(record.event);
601 #ifndef NO_ACTION_TAPPING
602 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
606 void debug_action(action_t action)
608 switch (action.kind.id) {
609 case ACT_LMODS: dprint("ACT_LMODS"); break;
610 case ACT_RMODS: dprint("ACT_RMODS"); break;
611 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
612 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
613 case ACT_USAGE: dprint("ACT_USAGE"); break;
614 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
615 case ACT_LAYER: dprint("ACT_LAYER"); break;
616 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
617 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
618 case ACT_MACRO: dprint("ACT_MACRO"); break;
619 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
620 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
621 default: dprint("UNKNOWN"); break;
623 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);