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"
38 void action_exec(keyevent_t event)
40 if (!IS_NOEVENT(event)) {
41 dprint("\n---- action_exec: start -----\n");
42 dprint("EVENT: "); debug_event(event); dprintln();
43 hook_matrix_change(event);
46 keyrecord_t record = { .event = event };
48 #ifndef NO_ACTION_TAPPING
49 action_tapping_process(record);
51 process_action(&record);
52 if (!IS_NOEVENT(record.event)) {
53 dprint("processed: "); debug_record(record); dprintln();
58 void process_action(keyrecord_t *record)
60 keyevent_t event = record->event;
61 #ifndef NO_ACTION_TAPPING
62 uint8_t tap_count = record->tap.count;
65 if (IS_NOEVENT(event)) { return; }
67 action_t action = layer_switch_get_action(event.key);
68 dprint("ACTION: "); debug_action(action);
69 #ifndef NO_ACTION_LAYER
70 dprint(" layer_state: "); layer_debug();
71 dprint(" default_layer_state: "); default_layer_debug();
75 switch (action.kind.id) {
80 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
85 send_keyboard_report();
87 register_code(action.key.code);
89 unregister_code(action.key.code);
92 send_keyboard_report();
97 #ifndef NO_ACTION_TAPPING
101 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
103 switch (action.layer_tap.code) {
104 #ifndef NO_ACTION_ONESHOT
108 if (tap_count == 0) {
111 else if (tap_count == 1) {
112 dprint("MODS_TAP: Oneshot: start\n");
113 set_oneshot_mods(mods);
119 if (tap_count == 0) {
120 clear_oneshot_mods();
121 unregister_mods(mods);
123 else if (tap_count == 1) {
124 // Retain Oneshot mods
127 clear_oneshot_mods();
128 unregister_mods(mods);
133 case MODS_TAP_TOGGLE:
135 if (tap_count <= TAPPING_TOGGLE) {
136 if (mods & get_mods()) {
137 dprint("MODS_TAP_TOGGLE: toggle mods off\n");
138 unregister_mods(mods);
140 dprint("MODS_TAP_TOGGLE: toggle mods on\n");
145 if (tap_count < TAPPING_TOGGLE) {
146 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
147 unregister_mods(mods);
154 if (record->tap.interrupted) {
155 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
156 // ad hoc: set 0 to cancel tap
157 record->tap.count = 0;
160 dprint("MODS_TAP: Tap: register_code\n");
161 register_code(action.key.code);
164 dprint("MODS_TAP: No tap: add_mods\n");
169 dprint("MODS_TAP: Tap: unregister_code\n");
170 unregister_code(action.key.code);
172 dprint("MODS_TAP: No tap: add_mods\n");
173 unregister_mods(mods);
181 #ifdef EXTRAKEY_ENABLE
182 /* other HID usage */
184 switch (action.usage.page) {
187 host_system_send(action.usage.code);
194 host_consumer_send(action.usage.code);
196 host_consumer_send(0);
202 #ifdef MOUSEKEY_ENABLE
206 mousekey_on(action.key.code);
209 mousekey_off(action.key.code);
214 #ifndef NO_ACTION_LAYER
216 if (action.layer_bitop.on == 0) {
217 /* Default Layer Bitwise Operation */
218 if (!event.pressed) {
219 uint8_t shift = action.layer_bitop.part*4;
220 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
221 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
222 switch (action.layer_bitop.op) {
223 case OP_BIT_AND: default_layer_and(bits | mask); break;
224 case OP_BIT_OR: default_layer_or(bits | mask); break;
225 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
226 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
230 /* Layer Bitwise Operation */
231 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
232 (action.layer_bitop.on & ON_RELEASE)) {
233 uint8_t shift = action.layer_bitop.part*4;
234 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
235 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
236 switch (action.layer_bitop.op) {
237 case OP_BIT_AND: layer_and(bits | mask); break;
238 case OP_BIT_OR: layer_or(bits | mask); break;
239 case OP_BIT_XOR: layer_xor(bits | mask); break;
240 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
245 #ifndef NO_ACTION_TAPPING
247 case ACT_LAYER_TAP_EXT:
248 switch (action.layer_tap.code) {
250 /* layer On/Off with modifiers(left only) */
252 layer_on(action.layer_tap.val);
253 register_mods(action.layer_tap.code & 0x0f);
255 layer_off(action.layer_tap.val);
256 unregister_mods(action.layer_tap.code & 0x0f);
262 if (tap_count < TAPPING_TOGGLE) {
263 layer_invert(action.layer_tap.val);
266 if (tap_count <= TAPPING_TOGGLE) {
267 layer_invert(action.layer_tap.val);
272 event.pressed ? layer_on(action.layer_tap.val) :
273 layer_off(action.layer_tap.val);
276 event.pressed ? layer_off(action.layer_tap.val) :
277 layer_on(action.layer_tap.val);
280 event.pressed ? layer_move(action.layer_tap.val) :
287 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
288 register_code(action.layer_tap.code);
290 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
291 layer_on(action.layer_tap.val);
295 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
296 unregister_code(action.layer_tap.code);
298 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
299 layer_off(action.layer_tap.val);
308 #ifndef NO_ACTION_MACRO
310 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
313 #ifdef BACKLIGHT_ENABLE
315 if (!event.pressed) {
316 switch (action.backlight.opt) {
317 case BACKLIGHT_INCREASE:
318 backlight_increase();
320 case BACKLIGHT_DECREASE:
321 backlight_decrease();
323 case BACKLIGHT_TOGGLE:
329 case BACKLIGHT_LEVEL:
330 backlight_level(action.backlight.level);
338 #ifndef NO_ACTION_FUNCTION
340 action_function(record, action.func.id, action.func.opt);
352 * Utilities for actions.
354 void register_code(uint8_t code)
360 #ifdef LOCKING_SUPPORT_ENABLE
361 else if (KC_LOCKING_CAPS == code) {
362 #ifdef LOCKING_RESYNC_ENABLE
363 // Resync: ignore if caps lock already is on
364 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
366 add_key(KC_CAPSLOCK);
367 send_keyboard_report();
368 del_key(KC_CAPSLOCK);
369 send_keyboard_report();
372 else if (KC_LOCKING_NUM == code) {
373 #ifdef LOCKING_RESYNC_ENABLE
374 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
377 send_keyboard_report();
379 send_keyboard_report();
382 else if (KC_LOCKING_SCROLL == code) {
383 #ifdef LOCKING_RESYNC_ENABLE
384 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
386 add_key(KC_SCROLLLOCK);
387 send_keyboard_report();
388 del_key(KC_SCROLLLOCK);
389 send_keyboard_report();
393 else if IS_KEY(code) {
394 // TODO: should push command_proc out of this block?
395 if (command_proc(code)) return;
397 #ifndef NO_ACTION_ONESHOT
399 if (oneshot_state.mods && !oneshot_state.disabled) {
400 uint8_t tmp_mods = get_mods();
401 add_mods(oneshot_state.mods);
404 send_keyboard_report();
407 send_keyboard_report();
414 send_keyboard_report();
417 else if IS_MOD(code) {
418 add_mods(MOD_BIT(code));
419 send_keyboard_report();
421 else if IS_SYSTEM(code) {
422 host_system_send(KEYCODE2SYSTEM(code));
424 else if IS_CONSUMER(code) {
425 host_consumer_send(KEYCODE2CONSUMER(code));
429 void unregister_code(uint8_t code)
435 #ifdef LOCKING_SUPPORT_ENABLE
436 else if (KC_LOCKING_CAPS == code) {
437 #ifdef LOCKING_RESYNC_ENABLE
438 // Resync: ignore if caps lock already is off
439 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
441 add_key(KC_CAPSLOCK);
442 send_keyboard_report();
443 del_key(KC_CAPSLOCK);
444 send_keyboard_report();
447 else if (KC_LOCKING_NUM == code) {
448 #ifdef LOCKING_RESYNC_ENABLE
449 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
452 send_keyboard_report();
454 send_keyboard_report();
457 else if (KC_LOCKING_SCROLL == code) {
458 #ifdef LOCKING_RESYNC_ENABLE
459 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
461 add_key(KC_SCROLLLOCK);
462 send_keyboard_report();
463 del_key(KC_SCROLLLOCK);
464 send_keyboard_report();
468 else if IS_KEY(code) {
470 send_keyboard_report();
472 else if IS_MOD(code) {
473 del_mods(MOD_BIT(code));
474 send_keyboard_report();
476 else if IS_SYSTEM(code) {
479 else if IS_CONSUMER(code) {
480 host_consumer_send(0);
484 void register_mods(uint8_t mods)
488 send_keyboard_report();
492 void unregister_mods(uint8_t mods)
496 send_keyboard_report();
500 void clear_keyboard(void)
503 clear_keyboard_but_mods();
506 void clear_keyboard_but_mods(void)
510 send_keyboard_report();
511 #ifdef MOUSEKEY_ENABLE
515 #ifdef EXTRAKEY_ENABLE
517 host_consumer_send(0);
521 bool is_tap_key(keypos_t key)
523 action_t action = layer_switch_get_action(key);
525 switch (action.kind.id) {
529 case ACT_LAYER_TAP_EXT:
530 switch (action.layer_tap.code) {
538 if (action.func.opt & FUNC_TAP) { return true; }
548 void debug_event(keyevent_t event)
550 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
553 void debug_record(keyrecord_t record)
555 debug_event(record.event);
556 #ifndef NO_ACTION_TAPPING
557 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
561 void debug_action(action_t action)
563 switch (action.kind.id) {
564 case ACT_LMODS: dprint("ACT_LMODS"); break;
565 case ACT_RMODS: dprint("ACT_RMODS"); break;
566 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
567 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
568 case ACT_USAGE: dprint("ACT_USAGE"); break;
569 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
570 case ACT_LAYER: dprint("ACT_LAYER"); break;
571 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
572 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
573 case ACT_MACRO: dprint("ACT_MACRO"); break;
574 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
575 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
576 default: dprint("UNKNOWN"); break;
578 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);