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);
72 __attribute__ ((weak))
73 void process_action_kb(keyrecord_t *record) {}
75 void process_action(keyrecord_t *record)
77 keyevent_t event = record->event;
78 #ifndef NO_ACTION_TAPPING
79 uint8_t tap_count = record->tap.count;
82 if (IS_NOEVENT(event)) { return; }
84 process_action_kb(record);
86 action_t action = store_or_get_action(event.pressed, event.key);
87 dprint("ACTION: "); debug_action(action);
88 #ifndef NO_ACTION_LAYER
89 dprint(" layer_state: "); layer_debug();
90 dprint(" default_layer_state: "); default_layer_debug();
95 // clear the potential weak mods left by previously pressed keys
98 switch (action.kind.id) {
103 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
108 send_keyboard_report();
110 register_code(action.key.code);
112 unregister_code(action.key.code);
115 send_keyboard_report();
120 #ifndef NO_ACTION_TAPPING
124 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
126 switch (action.layer_tap.code) {
127 #ifndef NO_ACTION_ONESHOT
131 if (tap_count == 0) {
134 else if (tap_count == 1) {
135 dprint("MODS_TAP: Oneshot: start\n");
136 set_oneshot_mods(mods);
142 if (tap_count == 0) {
143 clear_oneshot_mods();
144 unregister_mods(mods);
146 else if (tap_count == 1) {
147 // Retain Oneshot mods
150 clear_oneshot_mods();
151 unregister_mods(mods);
156 case MODS_TAP_TOGGLE:
158 if (tap_count <= TAPPING_TOGGLE) {
162 if (tap_count < TAPPING_TOGGLE) {
163 unregister_mods(mods);
170 #ifndef IGNORE_MOD_TAP_INTERRUPT
171 if (record->tap.interrupted) {
172 dprint("mods_tap: tap: cancel: add_mods\n");
173 // ad hoc: set 0 to cancel tap
174 record->tap.count = 0;
179 dprint("MODS_TAP: Tap: register_code\n");
180 register_code(action.key.code);
183 dprint("MODS_TAP: No tap: add_mods\n");
188 dprint("MODS_TAP: Tap: unregister_code\n");
189 unregister_code(action.key.code);
191 dprint("MODS_TAP: No tap: add_mods\n");
192 unregister_mods(mods);
200 #ifdef EXTRAKEY_ENABLE
201 /* other HID usage */
203 switch (action.usage.page) {
206 host_system_send(action.usage.code);
213 host_consumer_send(action.usage.code);
215 host_consumer_send(0);
221 #ifdef MOUSEKEY_ENABLE
225 mousekey_on(action.key.code);
228 mousekey_off(action.key.code);
233 #ifndef NO_ACTION_LAYER
235 if (action.layer_bitop.on == 0) {
236 /* Default Layer Bitwise Operation */
237 if (!event.pressed) {
238 uint8_t shift = action.layer_bitop.part*4;
239 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
240 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
241 switch (action.layer_bitop.op) {
242 case OP_BIT_AND: default_layer_and(bits | mask); break;
243 case OP_BIT_OR: default_layer_or(bits | mask); break;
244 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
245 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
249 /* Layer Bitwise Operation */
250 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
251 (action.layer_bitop.on & ON_RELEASE)) {
252 uint8_t shift = action.layer_bitop.part*4;
253 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
254 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
255 switch (action.layer_bitop.op) {
256 case OP_BIT_AND: layer_and(bits | mask); break;
257 case OP_BIT_OR: layer_or(bits | mask); break;
258 case OP_BIT_XOR: layer_xor(bits | mask); break;
259 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
264 #ifndef NO_ACTION_TAPPING
266 case ACT_LAYER_TAP_EXT:
267 switch (action.layer_tap.code) {
269 /* layer On/Off with modifiers(left only) */
271 layer_on(action.layer_tap.val);
272 register_mods(action.layer_tap.code & 0x0f);
274 layer_off(action.layer_tap.val);
275 unregister_mods(action.layer_tap.code & 0x0f);
281 if (tap_count < TAPPING_TOGGLE) {
282 layer_invert(action.layer_tap.val);
285 if (tap_count <= TAPPING_TOGGLE) {
286 layer_invert(action.layer_tap.val);
291 event.pressed ? layer_on(action.layer_tap.val) :
292 layer_off(action.layer_tap.val);
295 event.pressed ? layer_off(action.layer_tap.val) :
296 layer_on(action.layer_tap.val);
299 event.pressed ? layer_move(action.layer_tap.val) :
306 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
307 register_code(action.layer_tap.code);
309 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
310 layer_on(action.layer_tap.val);
314 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
315 unregister_code(action.layer_tap.code);
317 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
318 layer_off(action.layer_tap.val);
327 #ifndef NO_ACTION_MACRO
329 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
332 #ifdef BACKLIGHT_ENABLE
334 if (!event.pressed) {
335 switch (action.backlight.opt) {
336 case BACKLIGHT_INCREASE:
337 backlight_increase();
339 case BACKLIGHT_DECREASE:
340 backlight_decrease();
342 case BACKLIGHT_TOGGLE:
348 case BACKLIGHT_LEVEL:
349 backlight_level(action.backlight.level);
357 #ifndef NO_ACTION_FUNCTION
359 action_function(record, action.func.id, action.func.opt);
371 * Utilities for actions.
373 void register_code(uint8_t code)
379 #ifdef LOCKING_SUPPORT_ENABLE
380 else if (KC_LOCKING_CAPS == code) {
381 #ifdef LOCKING_RESYNC_ENABLE
382 // Resync: ignore if caps lock already is on
383 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
385 add_key(KC_CAPSLOCK);
386 send_keyboard_report();
387 del_key(KC_CAPSLOCK);
388 send_keyboard_report();
391 else if (KC_LOCKING_NUM == code) {
392 #ifdef LOCKING_RESYNC_ENABLE
393 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
396 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();
407 del_key(KC_SCROLLLOCK);
408 send_keyboard_report();
412 else if IS_KEY(code) {
413 // TODO: should push command_proc out of this block?
414 if (command_proc(code)) return;
416 #ifndef NO_ACTION_ONESHOT
418 if (oneshot_state.mods && !oneshot_state.disabled) {
419 uint8_t tmp_mods = get_mods();
420 add_mods(oneshot_state.mods);
423 send_keyboard_report();
426 send_keyboard_report();
433 send_keyboard_report();
436 else if IS_MOD(code) {
437 add_mods(MOD_BIT(code));
438 send_keyboard_report();
440 else if IS_SYSTEM(code) {
441 host_system_send(KEYCODE2SYSTEM(code));
443 else if IS_CONSUMER(code) {
444 host_consumer_send(KEYCODE2CONSUMER(code));
448 void unregister_code(uint8_t code)
454 #ifdef LOCKING_SUPPORT_ENABLE
455 else if (KC_LOCKING_CAPS == code) {
456 #ifdef LOCKING_RESYNC_ENABLE
457 // Resync: ignore if caps lock already is off
458 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
460 add_key(KC_CAPSLOCK);
461 send_keyboard_report();
462 del_key(KC_CAPSLOCK);
463 send_keyboard_report();
466 else if (KC_LOCKING_NUM == code) {
467 #ifdef LOCKING_RESYNC_ENABLE
468 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
471 send_keyboard_report();
473 send_keyboard_report();
476 else if (KC_LOCKING_SCROLL == code) {
477 #ifdef LOCKING_RESYNC_ENABLE
478 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
480 add_key(KC_SCROLLLOCK);
481 send_keyboard_report();
482 del_key(KC_SCROLLLOCK);
483 send_keyboard_report();
487 else if IS_KEY(code) {
489 send_keyboard_report();
491 else if IS_MOD(code) {
492 del_mods(MOD_BIT(code));
493 send_keyboard_report();
495 else if IS_SYSTEM(code) {
498 else if IS_CONSUMER(code) {
499 host_consumer_send(0);
503 void register_mods(uint8_t mods)
507 send_keyboard_report();
511 void unregister_mods(uint8_t mods)
515 send_keyboard_report();
519 void clear_keyboard(void)
522 clear_keyboard_but_mods();
525 void clear_keyboard_but_mods(void)
530 send_keyboard_report();
531 #ifdef MOUSEKEY_ENABLE
535 #ifdef EXTRAKEY_ENABLE
537 host_consumer_send(0);
541 bool is_tap_key(keypos_t key)
543 action_t action = layer_switch_get_action(key);
545 switch (action.kind.id) {
549 case ACT_LAYER_TAP_EXT:
550 switch (action.layer_tap.code) {
558 if (action.func.opt & FUNC_TAP) { return true; }
568 void debug_event(keyevent_t event)
570 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
573 void debug_record(keyrecord_t record)
575 debug_event(record.event);
576 #ifndef NO_ACTION_TAPPING
577 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
581 void debug_action(action_t action)
583 switch (action.kind.id) {
584 case ACT_LMODS: dprint("ACT_LMODS"); break;
585 case ACT_RMODS: dprint("ACT_RMODS"); break;
586 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
587 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
588 case ACT_USAGE: dprint("ACT_USAGE"); break;
589 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
590 case ACT_LAYER: dprint("ACT_LAYER"); break;
591 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
592 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
593 case ACT_MACRO: dprint("ACT_MACRO"); break;
594 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
595 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
596 default: dprint("UNKNOWN"); break;
598 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);