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/>.
24 #include "layer_switch.h"
25 #include "action_tapping.h"
26 #include "action_oneshot.h"
27 #include "action_macro.h"
31 void action_exec(keyevent_t event)
33 if (!IS_NOEVENT(event)) {
34 debug("\n---- action_exec: start -----\n");
35 debug("EVENT: "); debug_event(event); debug("\n");
38 keyrecord_t record = { .event = event };
40 #ifndef NO_ACTION_TAPPING
41 action_tapping_process(record);
43 process_action(&record);
44 if (!IS_NOEVENT(record.event)) {
45 debug("processed: "); debug_record(record); debug("\n");
50 void process_action(keyrecord_t *record)
52 keyevent_t event = record->event;
53 uint8_t tap_count = record->tap.count;
55 if (IS_NOEVENT(event)) { return; }
57 action_t action = layer_switch_get_action(event.key);
58 debug("ACTION: "); debug_action(action);
59 debug(" keymaps: "); keymap_debug();
60 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
62 switch (action.kind.id) {
67 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
70 uint8_t tmp_mods = host_get_mods();
73 host_send_keyboard_report();
75 register_code(action.key.code);
76 if (mods && action.key.code) {
77 host_set_mods(tmp_mods);
78 host_send_keyboard_report();
81 if (mods && !action.key.code) {
83 host_send_keyboard_report();
85 unregister_code(action.key.code);
89 #ifndef NO_ACTION_TAPPING
93 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
95 switch (action.layer.code) {
96 #ifndef NO_ACTION_ONESHOT
100 if (tap_count == 0) {
101 debug("MODS_TAP: Oneshot: add_mods\n");
104 else if (tap_count == 1) {
105 debug("MODS_TAP: Oneshot: start\n");
108 else if (tap_count == TAPPING_TOGGLE) {
109 debug("MODS_TAP: Oneshot: toggle\n");
113 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
114 // double tap cancels oneshot and works as normal modifier.
119 if (tap_count == 0) {
120 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
121 // cancel oneshot on hold
125 else if (tap_count == 1) {
126 debug("MODS_TAP: Oneshot: del_mods\n");
131 debug("MODS_TAP: Oneshot: del_mods\n");
141 if (record->tap.interrupted) {
142 debug("MODS_TAP: Tap: Cancel: add_mods\n");
143 // ad hoc: set 0 to cancel tap
144 record->tap.count = 0;
147 debug("MODS_TAP: Tap: register_code\n");
148 register_code(action.key.code);
151 debug("MODS_TAP: No tap: add_mods\n");
156 debug("MODS_TAP: Tap: unregister_code\n");
157 unregister_code(action.key.code);
159 debug("MODS_TAP: No tap: add_mods\n");
168 #ifdef EXTRAKEY_ENABLE
169 /* other HID usage */
171 switch (action.usage.page) {
174 host_system_send(action.usage.code);
181 host_consumer_send(action.usage.code);
183 host_consumer_send(0);
189 #ifdef MOUSEKEY_ENABLE
193 mousekey_on(action.key.code);
196 mousekey_off(action.key.code);
201 #ifndef NO_ACTION_LAYER
204 switch (action.layer.code) {
207 switch (action.layer.val & 0x03) {
218 if (!event.pressed) {
225 /* NOTE: 4-7 rserved */
228 /* Keymap Reset default layer */
229 case (OP_RESET | ON_PRESS):
231 default_layer_set(action.layer.val);
234 case (OP_RESET | ON_RELEASE):
235 if (!event.pressed) {
236 default_layer_set(action.layer.val);
239 case (OP_RESET | ON_BOTH):
240 default_layer_set(action.layer.val);
243 /* Keymap Bit invert */
245 /* with tap toggle */
247 if (tap_count < TAPPING_TOGGLE) {
248 debug("KEYMAP_INV: tap toggle(press).\n");
249 keymap_invert(action.layer.val);
252 if (tap_count <= TAPPING_TOGGLE) {
253 debug("KEYMAP_INV: tap toggle(release).\n");
254 keymap_invert(action.layer.val);
258 case (OP_INV | ON_PRESS):
260 keymap_invert(action.layer.val);
263 case (OP_INV | ON_RELEASE):
264 if (!event.pressed) {
265 keymap_invert(action.layer.val);
268 case (OP_INV | ON_BOTH):
269 keymap_invert(action.layer.val);
275 keymap_on(action.layer.val);
277 keymap_off(action.layer.val);
280 case (OP_ON | ON_PRESS):
282 keymap_on(action.layer.val);
285 case (OP_ON | ON_RELEASE):
286 if (!event.pressed) {
287 keymap_on(action.layer.val);
290 case (OP_ON | ON_BOTH):
291 keymap_on(action.layer.val);
297 keymap_off(action.layer.val);
299 keymap_on(action.layer.val);
302 case (OP_OFF | ON_PRESS):
304 keymap_off(action.layer.val);
307 case (OP_OFF | ON_RELEASE):
308 if (!event.pressed) {
309 keymap_off(action.layer.val);
312 case (OP_OFF | ON_BOTH):
313 keymap_off(action.layer.val);
319 keymap_set(action.layer.val);
324 case (OP_SET | ON_PRESS):
326 keymap_set(action.layer.val);
329 case (OP_SET | ON_RELEASE):
330 if (!event.pressed) {
331 keymap_set(action.layer.val);
334 case (OP_SET | ON_BOTH):
335 keymap_set(action.layer.val);
338 /* Keymap Bit invert with tap key */
342 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
343 register_code(action.layer.code);
345 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
346 keymap_on(action.layer.val);
350 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
351 unregister_code(action.layer.code);
353 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
354 keymap_off(action.layer.val);
362 #ifndef NO_ACTION_MACRO
364 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
369 #ifndef NO_ACTION_FUNCTION
371 action_function(record, action.func.id, action.func.opt);
383 * Utilities for actions.
385 void register_code(uint8_t code)
390 #ifdef CAPSLOCK_LOCKING_ENABLE
391 else if (KC_LOCKING_CAPS == code) {
392 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
393 // Resync: ignore if caps lock already is on
394 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
396 host_add_key(KC_CAPSLOCK);
397 host_send_keyboard_report();
398 host_del_key(KC_CAPSLOCK);
399 host_send_keyboard_report();
402 else if IS_KEY(code) {
403 // TODO: should push command_proc out of this block?
404 if (command_proc(code)) return;
406 #ifndef NO_ACTION_ONESHOT
407 if (oneshot_state.mods && !oneshot_state.disabled) {
408 uint8_t tmp_mods = host_get_mods();
409 host_add_mods(oneshot_state.mods);
412 host_send_keyboard_report();
414 host_set_mods(tmp_mods);
420 host_send_keyboard_report();
423 else if IS_MOD(code) {
424 host_add_mods(MOD_BIT(code));
425 host_send_keyboard_report();
429 void unregister_code(uint8_t code)
434 #ifdef CAPSLOCK_LOCKING_ENABLE
435 else if (KC_LOCKING_CAPS == code) {
436 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
437 // Resync: ignore if caps lock already is off
438 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
440 host_add_key(KC_CAPSLOCK);
441 host_send_keyboard_report();
442 host_del_key(KC_CAPSLOCK);
443 host_send_keyboard_report();
446 else if IS_KEY(code) {
448 host_send_keyboard_report();
450 else if IS_MOD(code) {
451 host_del_mods(MOD_BIT(code));
452 host_send_keyboard_report();
456 void add_mods(uint8_t mods)
460 host_send_keyboard_report();
464 void del_mods(uint8_t mods)
468 host_send_keyboard_report();
472 void set_mods(uint8_t mods)
475 host_send_keyboard_report();
478 void clear_keyboard(void)
481 clear_keyboard_but_mods();
484 void clear_keyboard_but_mods(void)
487 host_send_keyboard_report();
488 #ifdef MOUSEKEY_ENABLE
492 #ifdef EXTRAKEY_ENABLE
494 host_consumer_send(0);
498 bool sending_anykey(void)
500 return (host_has_anykey() || host_mouse_in_use() ||
501 host_last_sysytem_report() || host_last_consumer_report());
504 bool is_tap_key(key_t key)
506 action_t action = layer_switch_get_action(key);
508 switch (action.kind.id) {
513 switch (action.layer.code) {
514 case 0x04 ... 0xEF: /* tap key */
522 if (action.func.opt & FUNC_TAP) { return true; }
532 void debug_event(keyevent_t event)
534 debug_hex16((event.key.row<<8) | event.key.col);
535 if (event.pressed) debug("d("); else debug("u(");
536 debug_dec(event.time); debug(")");
539 void debug_record(keyrecord_t record)
541 debug_event(record.event);
542 #ifndef NO_ACTION_TAPPING
543 debug(":"); debug_dec(record.tap.count);
544 if (record.tap.interrupted) debug("-");
548 void debug_action(action_t action)
550 switch (action.kind.id) {
551 case ACT_LMODS: debug("ACT_LMODS"); break;
552 case ACT_RMODS: debug("ACT_RMODS"); break;
553 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
554 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
555 case ACT_USAGE: debug("ACT_USAGE"); break;
556 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
557 case ACT_LAYER: debug("ACT_LAYER"); break;
558 case ACT_LAYER_BITOP: debug("ACT_LAYER_BITOP"); break;
559 case ACT_MACRO: debug("ACT_MACRO"); break;
560 case ACT_COMMAND: debug("ACT_COMMAND"); break;
561 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
562 default: debug("UNKNOWN"); break;
565 debug_hex4(action.kind.param>>8);
567 debug_hex8(action.kind.param & 0xff);