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 "action_layer.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 #ifndef NO_ACTION_TAPPING
54 uint8_t tap_count = record->tap.count;
57 if (IS_NOEVENT(event)) { return; }
59 action_t action = layer_switch_get_action(event.key);
60 debug("ACTION: "); debug_action(action);
61 #ifndef NO_ACTION_LAYER
62 debug(" layer_state: "); layer_debug();
63 debug(" default_layer_state: "); default_layer_debug();
67 switch (action.kind.id) {
72 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
75 uint8_t tmp_mods = host_get_mods();
78 host_send_keyboard_report();
80 register_code(action.key.code);
81 if (mods && action.key.code) {
82 host_set_mods(tmp_mods);
83 host_send_keyboard_report();
86 if (mods && !action.key.code) {
88 host_send_keyboard_report();
90 unregister_code(action.key.code);
94 #ifndef NO_ACTION_TAPPING
98 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
100 switch (action.layer_tap.code) {
101 #ifndef NO_ACTION_ONESHOT
105 if (tap_count == 0) {
106 debug("MODS_TAP: Oneshot: add_mods\n");
109 else if (tap_count == 1) {
110 debug("MODS_TAP: Oneshot: start\n");
113 else if (tap_count == TAPPING_TOGGLE) {
114 debug("MODS_TAP: Oneshot: toggle\n");
118 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
119 // double tap cancels oneshot and works as normal modifier.
124 if (tap_count == 0) {
125 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
126 // cancel oneshot on hold
130 else if (tap_count == 1) {
131 debug("MODS_TAP: Oneshot: del_mods\n");
136 debug("MODS_TAP: Oneshot: del_mods\n");
146 if (record->tap.interrupted) {
147 debug("MODS_TAP: Tap: Cancel: add_mods\n");
148 // ad hoc: set 0 to cancel tap
149 record->tap.count = 0;
152 debug("MODS_TAP: Tap: register_code\n");
153 register_code(action.key.code);
156 debug("MODS_TAP: No tap: add_mods\n");
161 debug("MODS_TAP: Tap: unregister_code\n");
162 unregister_code(action.key.code);
164 debug("MODS_TAP: No tap: add_mods\n");
173 #ifdef EXTRAKEY_ENABLE
174 /* other HID usage */
176 switch (action.usage.page) {
179 host_system_send(action.usage.code);
186 host_consumer_send(action.usage.code);
188 host_consumer_send(0);
194 #ifdef MOUSEKEY_ENABLE
198 mousekey_on(action.key.code);
201 mousekey_off(action.key.code);
206 #ifndef NO_ACTION_LAYER
208 if (action.layer_bitop.on == 0) {
209 /* Default Layer Bitwise Operation */
210 if (!event.pressed) {
211 uint8_t shift = action.layer_bitop.part*4;
212 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
213 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
214 switch (action.layer_bitop.op) {
215 case OP_BIT_AND: default_layer_and(bits | mask); break;
216 case OP_BIT_OR: default_layer_or(bits | mask); break;
217 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
218 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
222 /* Layer Bitwise Operation */
223 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
224 (action.layer_bitop.on & ON_RELEASE)) {
225 uint8_t shift = action.layer_bitop.part*4;
226 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
227 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
228 switch (action.layer_bitop.op) {
229 case OP_BIT_AND: layer_and(bits | mask); break;
230 case OP_BIT_OR: layer_or(bits | mask); break;
231 case OP_BIT_XOR: layer_xor(bits | mask); break;
232 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
237 #ifndef NO_ACTION_TAPPING
240 switch (action.layer_tap.code) {
244 if (tap_count < TAPPING_TOGGLE) {
245 layer_invert(action.layer_tap.val);
248 if (tap_count <= TAPPING_TOGGLE) {
249 layer_invert(action.layer_tap.val);
254 event.pressed ? layer_on(action.layer_tap.val) :
255 layer_off(action.layer_tap.val);
258 event.pressed ? layer_off(action.layer_tap.val) :
259 layer_on(action.layer_tap.val);
262 event.pressed ? layer_move(action.layer_tap.val) :
269 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
270 register_code(action.layer_tap.code);
272 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
273 layer_on(action.layer_tap.val);
277 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
278 unregister_code(action.layer_tap.code);
280 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
281 layer_off(action.layer_tap.val);
290 #ifndef NO_ACTION_MACRO
292 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
297 #ifndef NO_ACTION_FUNCTION
299 action_function(record, action.func.id, action.func.opt);
311 * Utilities for actions.
313 void register_code(uint8_t code)
318 #ifdef CAPSLOCK_LOCKING_ENABLE
319 else if (KC_LOCKING_CAPS == code) {
320 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
321 // Resync: ignore if caps lock already is on
322 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
324 host_add_key(KC_CAPSLOCK);
325 host_send_keyboard_report();
326 host_del_key(KC_CAPSLOCK);
327 host_send_keyboard_report();
330 else if IS_KEY(code) {
331 // TODO: should push command_proc out of this block?
332 if (command_proc(code)) return;
334 #ifndef NO_ACTION_ONESHOT
335 if (oneshot_state.mods && !oneshot_state.disabled) {
336 uint8_t tmp_mods = host_get_mods();
337 host_add_mods(oneshot_state.mods);
340 host_send_keyboard_report();
342 host_set_mods(tmp_mods);
348 host_send_keyboard_report();
351 else if IS_MOD(code) {
352 host_add_mods(MOD_BIT(code));
353 host_send_keyboard_report();
357 void unregister_code(uint8_t code)
362 #ifdef CAPSLOCK_LOCKING_ENABLE
363 else if (KC_LOCKING_CAPS == code) {
364 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
365 // Resync: ignore if caps lock already is off
366 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
368 host_add_key(KC_CAPSLOCK);
369 host_send_keyboard_report();
370 host_del_key(KC_CAPSLOCK);
371 host_send_keyboard_report();
374 else if IS_KEY(code) {
376 host_send_keyboard_report();
378 else if IS_MOD(code) {
379 host_del_mods(MOD_BIT(code));
380 host_send_keyboard_report();
384 void add_mods(uint8_t mods)
388 host_send_keyboard_report();
392 void del_mods(uint8_t mods)
396 host_send_keyboard_report();
400 void set_mods(uint8_t mods)
403 host_send_keyboard_report();
406 void clear_keyboard(void)
409 clear_keyboard_but_mods();
412 void clear_keyboard_but_mods(void)
415 host_send_keyboard_report();
416 #ifdef MOUSEKEY_ENABLE
420 #ifdef EXTRAKEY_ENABLE
422 host_consumer_send(0);
426 bool sending_anykey(void)
428 return (host_has_anykey() || host_mouse_in_use() ||
429 host_last_sysytem_report() || host_last_consumer_report());
432 bool is_tap_key(key_t key)
434 action_t action = layer_switch_get_action(key);
436 switch (action.kind.id) {
444 if (action.func.opt & FUNC_TAP) { return true; }
454 void debug_event(keyevent_t event)
456 debug_hex16((event.key.row<<8) | event.key.col);
457 if (event.pressed) debug("d("); else debug("u(");
458 debug_dec(event.time); debug(")");
461 void debug_record(keyrecord_t record)
463 debug_event(record.event);
464 #ifndef NO_ACTION_TAPPING
465 debug(":"); debug_dec(record.tap.count);
466 if (record.tap.interrupted) debug("-");
470 void debug_action(action_t action)
472 switch (action.kind.id) {
473 case ACT_LMODS: debug("ACT_LMODS"); break;
474 case ACT_RMODS: debug("ACT_RMODS"); break;
475 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
476 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
477 case ACT_USAGE: debug("ACT_USAGE"); break;
478 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
479 case ACT_LAYER: debug("ACT_LAYER"); break;
480 case ACT_LAYER_TAP: debug("ACT_LAYER_TAP"); break;
481 case ACT_LAYER_TAP1: debug("ACT_LAYER_TAP1"); break;
482 case ACT_MACRO: debug("ACT_MACRO"); break;
483 case ACT_COMMAND: debug("ACT_COMMAND"); break;
484 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
485 default: debug("UNKNOWN"); break;
488 debug_hex4(action.kind.param>>8);
490 debug_hex8(action.kind.param & 0xff);