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 void process_action(keyrecord_t *record)
58 keyevent_t event = record->event;
59 #ifndef NO_ACTION_TAPPING
60 uint8_t tap_count = record->tap.count;
63 if (IS_NOEVENT(event)) { return; }
65 action_t action = layer_switch_get_action(event.key);
66 dprint("ACTION: "); debug_action(action);
67 #ifndef NO_ACTION_LAYER
68 dprint(" layer_state: "); layer_debug();
69 dprint(" default_layer_state: "); default_layer_debug();
73 switch (action.kind.id) {
78 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
83 send_keyboard_report();
85 register_code(action.key.code);
87 unregister_code(action.key.code);
90 send_keyboard_report();
95 #ifndef NO_ACTION_TAPPING
99 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
101 switch (action.layer_tap.code) {
102 #ifndef NO_ACTION_ONESHOT
106 if (tap_count == 0) {
109 else if (tap_count == 1) {
110 dprint("MODS_TAP: Oneshot: start\n");
111 set_oneshot_mods(mods);
117 if (tap_count == 0) {
118 clear_oneshot_mods();
119 unregister_mods(mods);
121 else if (tap_count == 1) {
122 // Retain Oneshot mods
125 clear_oneshot_mods();
126 unregister_mods(mods);
131 case MODS_TAP_TOGGLE:
133 if (tap_count <= TAPPING_TOGGLE) {
134 if (mods & get_mods()) {
135 dprint("MODS_TAP_TOGGLE: toggle mods off\n");
136 unregister_mods(mods);
138 dprint("MODS_TAP_TOGGLE: toggle mods on\n");
143 if (tap_count < TAPPING_TOGGLE) {
144 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
145 unregister_mods(mods);
152 if (record->tap.interrupted) {
153 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
154 // ad hoc: set 0 to cancel tap
155 record->tap.count = 0;
158 dprint("MODS_TAP: Tap: register_code\n");
159 register_code(action.key.code);
162 dprint("MODS_TAP: No tap: add_mods\n");
167 dprint("MODS_TAP: Tap: unregister_code\n");
168 unregister_code(action.key.code);
170 dprint("MODS_TAP: No tap: add_mods\n");
171 unregister_mods(mods);
179 #ifdef EXTRAKEY_ENABLE
180 /* other HID usage */
182 switch (action.usage.page) {
185 host_system_send(action.usage.code);
192 host_consumer_send(action.usage.code);
194 host_consumer_send(0);
200 #ifdef MOUSEKEY_ENABLE
204 mousekey_on(action.key.code);
207 mousekey_off(action.key.code);
212 #ifndef NO_ACTION_LAYER
214 if (action.layer_bitop.on == 0) {
215 /* Default Layer Bitwise Operation */
216 if (!event.pressed) {
217 uint8_t shift = action.layer_bitop.part*4;
218 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
219 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
220 switch (action.layer_bitop.op) {
221 case OP_BIT_AND: default_layer_and(bits | mask); break;
222 case OP_BIT_OR: default_layer_or(bits | mask); break;
223 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
224 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
228 /* Layer Bitwise Operation */
229 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
230 (action.layer_bitop.on & ON_RELEASE)) {
231 uint8_t shift = action.layer_bitop.part*4;
232 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
233 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
234 switch (action.layer_bitop.op) {
235 case OP_BIT_AND: layer_and(bits | mask); break;
236 case OP_BIT_OR: layer_or(bits | mask); break;
237 case OP_BIT_XOR: layer_xor(bits | mask); break;
238 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
243 #ifndef NO_ACTION_TAPPING
245 case ACT_LAYER_TAP_EXT:
246 switch (action.layer_tap.code) {
248 /* layer On/Off with modifiers(left only) */
250 layer_on(action.layer_tap.val);
251 register_mods(action.layer_tap.code & 0x0f);
253 layer_off(action.layer_tap.val);
254 unregister_mods(action.layer_tap.code & 0x0f);
260 if (tap_count < TAPPING_TOGGLE) {
261 layer_invert(action.layer_tap.val);
264 if (tap_count <= TAPPING_TOGGLE) {
265 layer_invert(action.layer_tap.val);
270 event.pressed ? layer_on(action.layer_tap.val) :
271 layer_off(action.layer_tap.val);
274 event.pressed ? layer_off(action.layer_tap.val) :
275 layer_on(action.layer_tap.val);
278 event.pressed ? layer_move(action.layer_tap.val) :
285 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
286 register_code(action.layer_tap.code);
288 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
289 layer_on(action.layer_tap.val);
293 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
294 unregister_code(action.layer_tap.code);
296 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
297 layer_off(action.layer_tap.val);
306 #ifndef NO_ACTION_MACRO
308 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
311 #ifdef BACKLIGHT_ENABLE
313 if (!event.pressed) {
314 switch (action.backlight.opt) {
315 case BACKLIGHT_INCREASE:
316 backlight_increase();
318 case BACKLIGHT_DECREASE:
319 backlight_decrease();
321 case BACKLIGHT_TOGGLE:
327 case BACKLIGHT_LEVEL:
328 backlight_level(action.backlight.level);
336 #ifndef NO_ACTION_FUNCTION
338 action_function(record, action.func.id, action.func.opt);
350 * Utilities for actions.
352 void register_code(uint8_t code)
358 #ifdef LOCKING_SUPPORT_ENABLE
359 else if (KC_LOCKING_CAPS == code) {
360 #ifdef LOCKING_RESYNC_ENABLE
361 // Resync: ignore if caps lock already is on
362 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
364 add_key(KC_CAPSLOCK);
365 send_keyboard_report();
366 del_key(KC_CAPSLOCK);
367 send_keyboard_report();
370 else if (KC_LOCKING_NUM == code) {
371 #ifdef LOCKING_RESYNC_ENABLE
372 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
375 send_keyboard_report();
377 send_keyboard_report();
380 else if (KC_LOCKING_SCROLL == code) {
381 #ifdef LOCKING_RESYNC_ENABLE
382 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
384 add_key(KC_SCROLLLOCK);
385 send_keyboard_report();
386 del_key(KC_SCROLLLOCK);
387 send_keyboard_report();
391 else if IS_KEY(code) {
392 // TODO: should push command_proc out of this block?
393 if (command_proc(code)) return;
395 #ifndef NO_ACTION_ONESHOT
397 if (oneshot_state.mods && !oneshot_state.disabled) {
398 uint8_t tmp_mods = get_mods();
399 add_mods(oneshot_state.mods);
402 send_keyboard_report();
405 send_keyboard_report();
412 send_keyboard_report();
415 else if IS_MOD(code) {
416 add_mods(MOD_BIT(code));
417 send_keyboard_report();
419 else if IS_SYSTEM(code) {
420 host_system_send(KEYCODE2SYSTEM(code));
422 else if IS_CONSUMER(code) {
423 host_consumer_send(KEYCODE2CONSUMER(code));
427 void unregister_code(uint8_t code)
433 #ifdef LOCKING_SUPPORT_ENABLE
434 else if (KC_LOCKING_CAPS == code) {
435 #ifdef LOCKING_RESYNC_ENABLE
436 // Resync: ignore if caps lock already is off
437 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
439 add_key(KC_CAPSLOCK);
440 send_keyboard_report();
441 del_key(KC_CAPSLOCK);
442 send_keyboard_report();
445 else if (KC_LOCKING_NUM == code) {
446 #ifdef LOCKING_RESYNC_ENABLE
447 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
450 send_keyboard_report();
452 send_keyboard_report();
455 else if (KC_LOCKING_SCROLL == code) {
456 #ifdef LOCKING_RESYNC_ENABLE
457 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
459 add_key(KC_SCROLLLOCK);
460 send_keyboard_report();
461 del_key(KC_SCROLLLOCK);
462 send_keyboard_report();
466 else if IS_KEY(code) {
468 send_keyboard_report();
470 else if IS_MOD(code) {
471 del_mods(MOD_BIT(code));
472 send_keyboard_report();
474 else if IS_SYSTEM(code) {
477 else if IS_CONSUMER(code) {
478 host_consumer_send(0);
482 void register_mods(uint8_t mods)
486 send_keyboard_report();
490 void unregister_mods(uint8_t mods)
494 send_keyboard_report();
498 void clear_keyboard(void)
501 clear_keyboard_but_mods();
504 void clear_keyboard_but_mods(void)
508 send_keyboard_report();
509 #ifdef MOUSEKEY_ENABLE
513 #ifdef EXTRAKEY_ENABLE
515 host_consumer_send(0);
519 bool is_tap_key(keypos_t key)
521 action_t action = layer_switch_get_action(key);
523 switch (action.kind.id) {
527 case ACT_LAYER_TAP_EXT:
528 switch (action.layer_tap.code) {
536 if (action.func.opt & FUNC_TAP) { return true; }
546 void debug_event(keyevent_t event)
548 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
551 void debug_record(keyrecord_t record)
553 debug_event(record.event);
554 #ifndef NO_ACTION_TAPPING
555 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
559 void debug_action(action_t action)
561 switch (action.kind.id) {
562 case ACT_LMODS: dprint("ACT_LMODS"); break;
563 case ACT_RMODS: dprint("ACT_RMODS"); break;
564 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
565 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
566 case ACT_USAGE: dprint("ACT_USAGE"); break;
567 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
568 case ACT_LAYER: dprint("ACT_LAYER"); break;
569 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
570 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
571 case ACT_MACRO: dprint("ACT_MACRO"); break;
572 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
573 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
574 default: dprint("UNKNOWN"); break;
576 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);