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 int8_t key_number = key.col + (key.row * MATRIX_COLS);
86 int8_t storage_row = key_number / 8;
87 int8_t storage_bit = key_number % 8;
90 layer = layer_switch_get_layer(key);
91 for (int8_t bit_number = 0; bit_number <= 4; bit_number++) {
92 source_layers_cache[bit_number][storage_row] ^= (-(!!(layer & (1 << bit_number)) ^ source_layers_cache[bit_number][storage_row])) & (1 << storage_bit);
96 for (int8_t bit_number = 0; bit_number <= 4; bit_number++) {
97 layer |= (!!(source_layers_cache[bit_number][storage_row] & (1 << storage_bit))) << bit_number;
100 return action_for_key(layer, key);
102 return layer_switch_get_action(key);
106 void process_action(keyrecord_t *record)
108 keyevent_t event = record->event;
109 #ifndef NO_ACTION_TAPPING
110 uint8_t tap_count = record->tap.count;
113 if (IS_NOEVENT(event)) { return; }
115 action_t action = store_or_get_action(event.pressed, event.key);
116 dprint("ACTION: "); debug_action(action);
117 #ifndef NO_ACTION_LAYER
118 dprint(" layer_state: "); layer_debug();
119 dprint(" default_layer_state: "); default_layer_debug();
123 switch (action.kind.id) {
128 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
133 send_keyboard_report();
135 register_code(action.key.code);
137 unregister_code(action.key.code);
140 send_keyboard_report();
145 #ifndef NO_ACTION_TAPPING
149 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
151 switch (action.layer_tap.code) {
152 #ifndef NO_ACTION_ONESHOT
156 if (tap_count == 0) {
159 else if (tap_count == 1) {
160 dprint("MODS_TAP: Oneshot: start\n");
161 set_oneshot_mods(mods);
167 if (tap_count == 0) {
168 clear_oneshot_mods();
169 unregister_mods(mods);
171 else if (tap_count == 1) {
172 // Retain Oneshot mods
175 clear_oneshot_mods();
176 unregister_mods(mods);
181 case MODS_TAP_TOGGLE:
183 if (tap_count <= TAPPING_TOGGLE) {
187 if (tap_count < TAPPING_TOGGLE) {
188 unregister_mods(mods);
195 #ifndef IGNORE_MOD_TAP_INTERRUPT
196 if (record->tap.interrupted) {
197 dprint("mods_tap: tap: cancel: add_mods\n");
198 // ad hoc: set 0 to cancel tap
199 record->tap.count = 0;
204 dprint("MODS_TAP: Tap: register_code\n");
205 register_code(action.key.code);
208 dprint("MODS_TAP: No tap: add_mods\n");
213 dprint("MODS_TAP: Tap: unregister_code\n");
214 unregister_code(action.key.code);
216 dprint("MODS_TAP: No tap: add_mods\n");
217 unregister_mods(mods);
225 #ifdef EXTRAKEY_ENABLE
226 /* other HID usage */
228 switch (action.usage.page) {
231 host_system_send(action.usage.code);
238 host_consumer_send(action.usage.code);
240 host_consumer_send(0);
246 #ifdef MOUSEKEY_ENABLE
250 mousekey_on(action.key.code);
253 mousekey_off(action.key.code);
258 #ifndef NO_ACTION_LAYER
260 if (action.layer_bitop.on == 0) {
261 /* Default Layer Bitwise Operation */
262 if (!event.pressed) {
263 uint8_t shift = action.layer_bitop.part*4;
264 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
265 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
266 switch (action.layer_bitop.op) {
267 case OP_BIT_AND: default_layer_and(bits | mask); break;
268 case OP_BIT_OR: default_layer_or(bits | mask); break;
269 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
270 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
274 /* Layer Bitwise Operation */
275 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
276 (action.layer_bitop.on & ON_RELEASE)) {
277 uint8_t shift = action.layer_bitop.part*4;
278 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
279 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
280 switch (action.layer_bitop.op) {
281 case OP_BIT_AND: layer_and(bits | mask); break;
282 case OP_BIT_OR: layer_or(bits | mask); break;
283 case OP_BIT_XOR: layer_xor(bits | mask); break;
284 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
289 #ifndef NO_ACTION_TAPPING
291 case ACT_LAYER_TAP_EXT:
292 switch (action.layer_tap.code) {
294 /* layer On/Off with modifiers(left only) */
296 layer_on(action.layer_tap.val);
297 register_mods(action.layer_tap.code & 0x0f);
299 layer_off(action.layer_tap.val);
300 unregister_mods(action.layer_tap.code & 0x0f);
306 if (tap_count < TAPPING_TOGGLE) {
307 layer_invert(action.layer_tap.val);
310 if (tap_count <= TAPPING_TOGGLE) {
311 layer_invert(action.layer_tap.val);
316 event.pressed ? layer_on(action.layer_tap.val) :
317 layer_off(action.layer_tap.val);
320 event.pressed ? layer_off(action.layer_tap.val) :
321 layer_on(action.layer_tap.val);
324 event.pressed ? layer_move(action.layer_tap.val) :
331 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
332 register_code(action.layer_tap.code);
334 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
335 layer_on(action.layer_tap.val);
339 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
340 unregister_code(action.layer_tap.code);
342 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
343 layer_off(action.layer_tap.val);
352 #ifndef NO_ACTION_MACRO
354 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
357 #ifdef BACKLIGHT_ENABLE
359 if (!event.pressed) {
360 switch (action.backlight.opt) {
361 case BACKLIGHT_INCREASE:
362 backlight_increase();
364 case BACKLIGHT_DECREASE:
365 backlight_decrease();
367 case BACKLIGHT_TOGGLE:
373 case BACKLIGHT_LEVEL:
374 backlight_level(action.backlight.level);
382 #ifndef NO_ACTION_FUNCTION
384 action_function(record, action.func.id, action.func.opt);
396 * Utilities for actions.
398 void register_code(uint8_t code)
404 #ifdef LOCKING_SUPPORT_ENABLE
405 else if (KC_LOCKING_CAPS == code) {
406 #ifdef LOCKING_RESYNC_ENABLE
407 // Resync: ignore if caps lock already is on
408 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
410 add_key(KC_CAPSLOCK);
411 send_keyboard_report();
412 del_key(KC_CAPSLOCK);
413 send_keyboard_report();
416 else if (KC_LOCKING_NUM == code) {
417 #ifdef LOCKING_RESYNC_ENABLE
418 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
421 send_keyboard_report();
423 send_keyboard_report();
426 else if (KC_LOCKING_SCROLL == code) {
427 #ifdef LOCKING_RESYNC_ENABLE
428 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
430 add_key(KC_SCROLLLOCK);
431 send_keyboard_report();
432 del_key(KC_SCROLLLOCK);
433 send_keyboard_report();
437 else if IS_KEY(code) {
438 // TODO: should push command_proc out of this block?
439 if (command_proc(code)) return;
441 #ifndef NO_ACTION_ONESHOT
443 if (oneshot_state.mods && !oneshot_state.disabled) {
444 uint8_t tmp_mods = get_mods();
445 add_mods(oneshot_state.mods);
448 send_keyboard_report();
451 send_keyboard_report();
458 send_keyboard_report();
461 else if IS_MOD(code) {
462 add_mods(MOD_BIT(code));
463 send_keyboard_report();
465 else if IS_SYSTEM(code) {
466 host_system_send(KEYCODE2SYSTEM(code));
468 else if IS_CONSUMER(code) {
469 host_consumer_send(KEYCODE2CONSUMER(code));
473 void unregister_code(uint8_t code)
479 #ifdef LOCKING_SUPPORT_ENABLE
480 else if (KC_LOCKING_CAPS == code) {
481 #ifdef LOCKING_RESYNC_ENABLE
482 // Resync: ignore if caps lock already is off
483 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
485 add_key(KC_CAPSLOCK);
486 send_keyboard_report();
487 del_key(KC_CAPSLOCK);
488 send_keyboard_report();
491 else if (KC_LOCKING_NUM == code) {
492 #ifdef LOCKING_RESYNC_ENABLE
493 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
496 send_keyboard_report();
498 send_keyboard_report();
501 else if (KC_LOCKING_SCROLL == code) {
502 #ifdef LOCKING_RESYNC_ENABLE
503 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
505 add_key(KC_SCROLLLOCK);
506 send_keyboard_report();
507 del_key(KC_SCROLLLOCK);
508 send_keyboard_report();
512 else if IS_KEY(code) {
514 send_keyboard_report();
516 else if IS_MOD(code) {
517 del_mods(MOD_BIT(code));
518 send_keyboard_report();
520 else if IS_SYSTEM(code) {
523 else if IS_CONSUMER(code) {
524 host_consumer_send(0);
528 void register_mods(uint8_t mods)
532 send_keyboard_report();
536 void unregister_mods(uint8_t mods)
540 send_keyboard_report();
544 void clear_keyboard(void)
547 clear_keyboard_but_mods();
550 void clear_keyboard_but_mods(void)
554 send_keyboard_report();
555 #ifdef MOUSEKEY_ENABLE
559 #ifdef EXTRAKEY_ENABLE
561 host_consumer_send(0);
565 bool is_tap_key(keypos_t key)
567 action_t action = layer_switch_get_action(key);
569 switch (action.kind.id) {
573 case ACT_LAYER_TAP_EXT:
574 switch (action.layer_tap.code) {
582 if (action.func.opt & FUNC_TAP) { return true; }
592 void debug_event(keyevent_t event)
594 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
597 void debug_record(keyrecord_t record)
599 debug_event(record.event);
600 #ifndef NO_ACTION_TAPPING
601 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
605 void debug_action(action_t action)
607 switch (action.kind.id) {
608 case ACT_LMODS: dprint("ACT_LMODS"); break;
609 case ACT_RMODS: dprint("ACT_RMODS"); break;
610 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
611 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
612 case ACT_USAGE: dprint("ACT_USAGE"); break;
613 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
614 case ACT_LAYER: dprint("ACT_LAYER"); break;
615 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
616 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
617 case ACT_MACRO: dprint("ACT_MACRO"); break;
618 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
619 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
620 default: dprint("UNKNOWN"); break;
622 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);