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 + 7) / 8] = {0};
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 const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
86 const uint8_t storage_row = key_number / 8;
87 const uint8_t storage_bit = key_number % 8;
90 layer = layer_switch_get_layer(key);
91 for (uint8_t bit_number = 0; bit_number < 5; bit_number++) {
92 source_layers_cache[bit_number][storage_row] ^=
93 (-((layer & (1U << bit_number)) != 0)
94 ^ source_layers_cache[bit_number][storage_row])
95 & (1U << storage_bit);
100 for (uint8_t bit_number = 0; bit_number < 5; bit_number++) {
102 ((source_layers_cache[bit_number][storage_row]
103 & (1U << storage_bit)) != 0)
107 return action_for_key(layer, key);
109 return layer_switch_get_action(key);
113 __attribute__ ((weak))
114 void process_action_kb(keyrecord_t *record) {}
116 void process_action(keyrecord_t *record)
118 keyevent_t event = record->event;
119 #ifndef NO_ACTION_TAPPING
120 uint8_t tap_count = record->tap.count;
123 if (IS_NOEVENT(event)) { return; }
125 process_action_kb(record);
127 action_t action = store_or_get_action(event.pressed, event.key);
128 dprint("ACTION: "); debug_action(action);
129 #ifndef NO_ACTION_LAYER
130 dprint(" layer_state: "); layer_debug();
131 dprint(" default_layer_state: "); default_layer_debug();
136 // clear the potential weak mods left by previously pressed keys
139 switch (action.kind.id) {
144 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
149 send_keyboard_report();
151 register_code(action.key.code);
153 unregister_code(action.key.code);
156 send_keyboard_report();
161 #ifndef NO_ACTION_TAPPING
165 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
167 switch (action.layer_tap.code) {
168 #ifndef NO_ACTION_ONESHOT
172 if (tap_count == 0) {
175 else if (tap_count == 1) {
176 dprint("MODS_TAP: Oneshot: start\n");
177 set_oneshot_mods(mods);
183 if (tap_count == 0) {
184 clear_oneshot_mods();
185 unregister_mods(mods);
187 else if (tap_count == 1) {
188 // Retain Oneshot mods
191 clear_oneshot_mods();
192 unregister_mods(mods);
197 case MODS_TAP_TOGGLE:
199 if (tap_count <= TAPPING_TOGGLE) {
203 if (tap_count < TAPPING_TOGGLE) {
204 unregister_mods(mods);
211 #ifndef IGNORE_MOD_TAP_INTERRUPT
212 if (record->tap.interrupted) {
213 dprint("mods_tap: tap: cancel: add_mods\n");
214 // ad hoc: set 0 to cancel tap
215 record->tap.count = 0;
220 dprint("MODS_TAP: Tap: register_code\n");
221 register_code(action.key.code);
224 dprint("MODS_TAP: No tap: add_mods\n");
229 dprint("MODS_TAP: Tap: unregister_code\n");
230 unregister_code(action.key.code);
232 dprint("MODS_TAP: No tap: add_mods\n");
233 unregister_mods(mods);
241 #ifdef EXTRAKEY_ENABLE
242 /* other HID usage */
244 switch (action.usage.page) {
247 host_system_send(action.usage.code);
254 host_consumer_send(action.usage.code);
256 host_consumer_send(0);
262 #ifdef MOUSEKEY_ENABLE
266 mousekey_on(action.key.code);
269 mousekey_off(action.key.code);
274 #ifndef NO_ACTION_LAYER
276 if (action.layer_bitop.on == 0) {
277 /* Default Layer Bitwise Operation */
278 if (!event.pressed) {
279 uint8_t shift = action.layer_bitop.part*4;
280 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
281 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
282 switch (action.layer_bitop.op) {
283 case OP_BIT_AND: default_layer_and(bits | mask); break;
284 case OP_BIT_OR: default_layer_or(bits | mask); break;
285 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
286 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
290 /* Layer Bitwise Operation */
291 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
292 (action.layer_bitop.on & ON_RELEASE)) {
293 uint8_t shift = action.layer_bitop.part*4;
294 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
295 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
296 switch (action.layer_bitop.op) {
297 case OP_BIT_AND: layer_and(bits | mask); break;
298 case OP_BIT_OR: layer_or(bits | mask); break;
299 case OP_BIT_XOR: layer_xor(bits | mask); break;
300 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
305 #ifndef NO_ACTION_TAPPING
307 case ACT_LAYER_TAP_EXT:
308 switch (action.layer_tap.code) {
310 /* layer On/Off with modifiers(left only) */
312 layer_on(action.layer_tap.val);
313 register_mods(action.layer_tap.code & 0x0f);
315 layer_off(action.layer_tap.val);
316 unregister_mods(action.layer_tap.code & 0x0f);
322 if (tap_count < TAPPING_TOGGLE) {
323 layer_invert(action.layer_tap.val);
326 if (tap_count <= TAPPING_TOGGLE) {
327 layer_invert(action.layer_tap.val);
332 event.pressed ? layer_on(action.layer_tap.val) :
333 layer_off(action.layer_tap.val);
336 event.pressed ? layer_off(action.layer_tap.val) :
337 layer_on(action.layer_tap.val);
340 event.pressed ? layer_move(action.layer_tap.val) :
347 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
348 register_code(action.layer_tap.code);
350 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
351 layer_on(action.layer_tap.val);
355 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
356 unregister_code(action.layer_tap.code);
358 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
359 layer_off(action.layer_tap.val);
368 #ifndef NO_ACTION_MACRO
370 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
373 #ifdef BACKLIGHT_ENABLE
375 if (!event.pressed) {
376 switch (action.backlight.opt) {
377 case BACKLIGHT_INCREASE:
378 backlight_increase();
380 case BACKLIGHT_DECREASE:
381 backlight_decrease();
383 case BACKLIGHT_TOGGLE:
389 case BACKLIGHT_LEVEL:
390 backlight_level(action.backlight.level);
398 #ifndef NO_ACTION_FUNCTION
400 action_function(record, action.func.id, action.func.opt);
412 * Utilities for actions.
414 void register_code(uint8_t code)
420 #ifdef LOCKING_SUPPORT_ENABLE
421 else if (KC_LOCKING_CAPS == code) {
422 #ifdef LOCKING_RESYNC_ENABLE
423 // Resync: ignore if caps lock already is on
424 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
426 add_key(KC_CAPSLOCK);
427 send_keyboard_report();
428 del_key(KC_CAPSLOCK);
429 send_keyboard_report();
432 else if (KC_LOCKING_NUM == code) {
433 #ifdef LOCKING_RESYNC_ENABLE
434 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
437 send_keyboard_report();
439 send_keyboard_report();
442 else if (KC_LOCKING_SCROLL == code) {
443 #ifdef LOCKING_RESYNC_ENABLE
444 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
446 add_key(KC_SCROLLLOCK);
447 send_keyboard_report();
448 del_key(KC_SCROLLLOCK);
449 send_keyboard_report();
453 else if IS_KEY(code) {
454 // TODO: should push command_proc out of this block?
455 if (command_proc(code)) return;
457 #ifndef NO_ACTION_ONESHOT
459 if (oneshot_state.mods && !oneshot_state.disabled) {
460 uint8_t tmp_mods = get_mods();
461 add_mods(oneshot_state.mods);
464 send_keyboard_report();
467 send_keyboard_report();
474 send_keyboard_report();
477 else if IS_MOD(code) {
478 add_mods(MOD_BIT(code));
479 send_keyboard_report();
481 else if IS_SYSTEM(code) {
482 host_system_send(KEYCODE2SYSTEM(code));
484 else if IS_CONSUMER(code) {
485 host_consumer_send(KEYCODE2CONSUMER(code));
489 void unregister_code(uint8_t code)
495 #ifdef LOCKING_SUPPORT_ENABLE
496 else if (KC_LOCKING_CAPS == code) {
497 #ifdef LOCKING_RESYNC_ENABLE
498 // Resync: ignore if caps lock already is off
499 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
501 add_key(KC_CAPSLOCK);
502 send_keyboard_report();
503 del_key(KC_CAPSLOCK);
504 send_keyboard_report();
507 else if (KC_LOCKING_NUM == code) {
508 #ifdef LOCKING_RESYNC_ENABLE
509 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
512 send_keyboard_report();
514 send_keyboard_report();
517 else if (KC_LOCKING_SCROLL == code) {
518 #ifdef LOCKING_RESYNC_ENABLE
519 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
521 add_key(KC_SCROLLLOCK);
522 send_keyboard_report();
523 del_key(KC_SCROLLLOCK);
524 send_keyboard_report();
528 else if IS_KEY(code) {
530 send_keyboard_report();
532 else if IS_MOD(code) {
533 del_mods(MOD_BIT(code));
534 send_keyboard_report();
536 else if IS_SYSTEM(code) {
539 else if IS_CONSUMER(code) {
540 host_consumer_send(0);
544 void register_mods(uint8_t mods)
548 send_keyboard_report();
552 void unregister_mods(uint8_t mods)
556 send_keyboard_report();
560 void clear_keyboard(void)
563 clear_keyboard_but_mods();
566 void clear_keyboard_but_mods(void)
571 send_keyboard_report();
572 #ifdef MOUSEKEY_ENABLE
576 #ifdef EXTRAKEY_ENABLE
578 host_consumer_send(0);
582 bool is_tap_key(keypos_t key)
584 action_t action = layer_switch_get_action(key);
586 switch (action.kind.id) {
590 case ACT_LAYER_TAP_EXT:
591 switch (action.layer_tap.code) {
599 if (action.func.opt & FUNC_TAP) { return true; }
609 void debug_event(keyevent_t event)
611 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
614 void debug_record(keyrecord_t record)
616 debug_event(record.event);
617 #ifndef NO_ACTION_TAPPING
618 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
622 void debug_action(action_t action)
624 switch (action.kind.id) {
625 case ACT_LMODS: dprint("ACT_LMODS"); break;
626 case ACT_RMODS: dprint("ACT_RMODS"); break;
627 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
628 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
629 case ACT_USAGE: dprint("ACT_USAGE"); break;
630 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
631 case ACT_LAYER: dprint("ACT_LAYER"); break;
632 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
633 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
634 case ACT_MACRO: dprint("ACT_MACRO"); break;
635 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
636 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
637 default: dprint("UNKNOWN"); break;
639 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);