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"
39 void action_exec(keyevent_t event)
41 if (!IS_NOEVENT(event)) {
42 dprint("\n---- action_exec: start -----\n");
43 dprint("EVENT: "); debug_event(event); dprintln();
44 hook_matrix_change(event);
47 keyrecord_t record = { .event = event };
49 #ifndef NO_ACTION_TAPPING
50 action_tapping_process(record);
52 process_action(&record);
53 if (!IS_NOEVENT(record.event)) {
54 dprint("processed: "); debug_record(record); dprintln();
59 void process_action(keyrecord_t *record)
61 if (hook_process_action(record)) return;
63 keyevent_t event = record->event;
64 #ifndef NO_ACTION_TAPPING
65 uint8_t tap_count = record->tap.count;
68 if (IS_NOEVENT(event)) { return; }
70 action_t action = layer_switch_get_action(event);
71 dprint("ACTION: "); debug_action(action);
72 #ifndef NO_ACTION_LAYER
73 dprint(" layer_state: "); layer_debug();
74 dprint(" default_layer_state: "); default_layer_debug();
78 switch (action.kind.id) {
83 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
88 send_keyboard_report();
90 register_code(action.key.code);
92 unregister_code(action.key.code);
95 send_keyboard_report();
100 #ifndef NO_ACTION_TAPPING
104 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
106 switch (action.key.code) {
107 #ifndef NO_ACTION_ONESHOT
111 if (tap_count == 0) {
114 else if (tap_count == 1) {
115 dprint("MODS_TAP: Oneshot: start\n");
116 set_oneshot_mods(mods);
122 if (tap_count == 0) {
123 clear_oneshot_mods();
124 unregister_mods(mods);
126 else if (tap_count == 1) {
127 // Retain Oneshot mods
130 clear_oneshot_mods();
131 unregister_mods(mods);
136 case MODS_TAP_TOGGLE:
138 if (tap_count <= TAPPING_TOGGLE) {
139 if (mods & get_mods()) {
140 dprint("MODS_TAP_TOGGLE: toggle mods off\n");
141 unregister_mods(mods);
143 dprint("MODS_TAP_TOGGLE: toggle mods on\n");
148 if (tap_count < TAPPING_TOGGLE) {
149 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
150 unregister_mods(mods);
157 if (record->tap.interrupted) {
158 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
159 // ad hoc: set 0 to cancel tap
160 record->tap.count = 0;
163 dprint("MODS_TAP: Tap: register_code\n");
164 register_code(action.key.code);
167 dprint("MODS_TAP: No tap: add_mods\n");
172 dprint("MODS_TAP: Tap: unregister_code\n");
173 unregister_code(action.key.code);
175 dprint("MODS_TAP: No tap: add_mods\n");
176 unregister_mods(mods);
184 #ifdef EXTRAKEY_ENABLE
185 /* other HID usage */
187 switch (action.usage.page) {
190 host_system_send(action.usage.code);
197 host_consumer_send(action.usage.code);
199 host_consumer_send(0);
205 #ifdef MOUSEKEY_ENABLE
209 mousekey_on(action.key.code);
212 mousekey_off(action.key.code);
217 #ifndef NO_ACTION_LAYER
219 if (action.layer_bitop.on == 0) {
220 /* Default Layer Bitwise Operation */
221 if (!event.pressed) {
222 uint8_t shift = action.layer_bitop.part*4;
223 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
224 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
225 switch (action.layer_bitop.op) {
226 case OP_BIT_AND: default_layer_and(bits | mask); break;
227 case OP_BIT_OR: default_layer_or(bits | mask); break;
228 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
229 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
233 /* Layer Bitwise Operation */
234 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
235 (action.layer_bitop.on & ON_RELEASE)) {
236 uint8_t shift = action.layer_bitop.part*4;
237 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
238 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
239 switch (action.layer_bitop.op) {
240 case OP_BIT_AND: layer_and(bits | mask); break;
241 case OP_BIT_OR: layer_or(bits | mask); break;
242 case OP_BIT_XOR: layer_xor(bits | mask); break;
243 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
248 #ifndef NO_ACTION_TAPPING
250 case ACT_LAYER_TAP_EXT:
251 switch (action.layer_tap.code) {
253 /* layer On/Off with modifiers */
255 layer_on(action.layer_tap.val);
256 register_mods((action.layer_tap.code & 0x10) ?
257 (action.layer_tap.code & 0x0f) << 4 :
258 (action.layer_tap.code & 0x0f));
260 layer_off(action.layer_tap.val);
261 unregister_mods((action.layer_tap.code & 0x10) ?
262 (action.layer_tap.code & 0x0f) << 4 :
263 (action.layer_tap.code & 0x0f));
269 if (tap_count < TAPPING_TOGGLE) {
270 layer_invert(action.layer_tap.val);
273 if (tap_count <= TAPPING_TOGGLE) {
274 layer_invert(action.layer_tap.val);
279 event.pressed ? layer_on(action.layer_tap.val) :
280 layer_off(action.layer_tap.val);
283 event.pressed ? layer_off(action.layer_tap.val) :
284 layer_on(action.layer_tap.val);
287 event.pressed ? layer_move(action.layer_tap.val) :
294 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
295 register_code(action.layer_tap.code);
297 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
298 layer_on(action.layer_tap.val);
302 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
303 unregister_code(action.layer_tap.code);
305 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
306 layer_off(action.layer_tap.val);
315 #ifndef NO_ACTION_MACRO
317 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
320 #ifdef BACKLIGHT_ENABLE
322 if (!event.pressed) {
323 switch (action.backlight.opt) {
324 case BACKLIGHT_INCREASE:
325 backlight_increase();
327 case BACKLIGHT_DECREASE:
328 backlight_decrease();
330 case BACKLIGHT_TOGGLE:
336 case BACKLIGHT_LEVEL:
337 backlight_level(action.backlight.level);
345 #ifndef NO_ACTION_FUNCTION
347 action_function(record, action.func.id, action.func.opt);
359 * Utilities for actions.
361 void register_code(uint8_t code)
367 #ifdef LOCKING_SUPPORT_ENABLE
368 else if (KC_LOCKING_CAPS == code) {
369 #ifdef LOCKING_RESYNC_ENABLE
370 // Resync: ignore if caps lock already is on
371 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
373 add_key(KC_CAPSLOCK);
374 send_keyboard_report();
376 del_key(KC_CAPSLOCK);
377 send_keyboard_report();
380 else if (KC_LOCKING_NUM == code) {
381 #ifdef LOCKING_RESYNC_ENABLE
382 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
385 send_keyboard_report();
388 send_keyboard_report();
391 else if (KC_LOCKING_SCROLL == code) {
392 #ifdef LOCKING_RESYNC_ENABLE
393 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
395 add_key(KC_SCROLLLOCK);
396 send_keyboard_report();
398 del_key(KC_SCROLLLOCK);
399 send_keyboard_report();
403 else if IS_KEY(code) {
404 // TODO: should push command_proc out of this block?
405 if (command_proc(code)) return;
407 #ifndef NO_ACTION_ONESHOT
409 if (oneshot_state.mods && !oneshot_state.disabled) {
410 uint8_t tmp_mods = get_mods();
411 add_mods(oneshot_state.mods);
414 send_keyboard_report();
417 send_keyboard_report();
424 send_keyboard_report();
427 else if IS_MOD(code) {
428 add_mods(MOD_BIT(code));
429 send_keyboard_report();
431 else if IS_SYSTEM(code) {
432 host_system_send(KEYCODE2SYSTEM(code));
434 else if IS_CONSUMER(code) {
435 host_consumer_send(KEYCODE2CONSUMER(code));
439 void unregister_code(uint8_t code)
445 #ifdef LOCKING_SUPPORT_ENABLE
446 else if (KC_LOCKING_CAPS == code) {
447 #ifdef LOCKING_RESYNC_ENABLE
448 // Resync: ignore if caps lock already is off
449 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
451 add_key(KC_CAPSLOCK);
452 send_keyboard_report();
454 del_key(KC_CAPSLOCK);
455 send_keyboard_report();
458 else if (KC_LOCKING_NUM == code) {
459 #ifdef LOCKING_RESYNC_ENABLE
460 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
463 send_keyboard_report();
466 send_keyboard_report();
469 else if (KC_LOCKING_SCROLL == code) {
470 #ifdef LOCKING_RESYNC_ENABLE
471 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
473 add_key(KC_SCROLLLOCK);
474 send_keyboard_report();
476 del_key(KC_SCROLLLOCK);
477 send_keyboard_report();
481 else if IS_KEY(code) {
483 send_keyboard_report();
485 else if IS_MOD(code) {
486 del_mods(MOD_BIT(code));
487 send_keyboard_report();
489 else if IS_SYSTEM(code) {
492 else if IS_CONSUMER(code) {
493 host_consumer_send(0);
497 void type_code(uint8_t code)
500 unregister_code(code);
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)
529 send_keyboard_report();
530 #ifdef MOUSEKEY_ENABLE
534 #ifdef EXTRAKEY_ENABLE
536 host_consumer_send(0);
540 bool is_tap_key(keyevent_t event)
542 if (IS_NOEVENT(event)) { return false; }
544 action_t action = layer_switch_get_action(event);
546 switch (action.kind.id) {
549 switch (action.key.code) {
551 case MODS_TAP_TOGGLE:
552 case KC_A ... KC_EXSEL: // tap key
553 case KC_LCTRL ... KC_RGUI: // tap key
557 case ACT_LAYER_TAP_EXT:
558 switch (action.layer_tap.code) {
559 case 0xc0 ... 0xdf: // with modifiers
561 case KC_A ... KC_EXSEL: // tap key
562 case KC_LCTRL ... KC_RGUI: // tap key
569 if (action.func.opt & FUNC_TAP) { return true; }
579 void debug_event(keyevent_t event)
581 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
584 void debug_record(keyrecord_t record)
586 debug_event(record.event);
587 #ifndef NO_ACTION_TAPPING
588 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
592 void debug_action(action_t action)
594 switch (action.kind.id) {
595 case ACT_LMODS: dprint("ACT_LMODS"); break;
596 case ACT_RMODS: dprint("ACT_RMODS"); break;
597 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
598 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
599 case ACT_USAGE: dprint("ACT_USAGE"); break;
600 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
601 case ACT_LAYER: dprint("ACT_LAYER"); break;
602 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
603 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
604 case ACT_MACRO: dprint("ACT_MACRO"); break;
605 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
606 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
607 default: dprint("UNKNOWN"); break;
609 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);