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 __attribute__ ((weak))
57 void process_action_kb(keyrecord_t *record) {}
59 void process_action(keyrecord_t *record)
61 keyevent_t event = record->event;
62 #ifndef NO_ACTION_TAPPING
63 uint8_t tap_count = record->tap.count;
66 if (IS_NOEVENT(event)) { return; }
68 process_action_kb(record);
70 action_t action = layer_switch_get_action(event.key);
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();
79 // clear the potential weak mods left by previously pressed keys
82 switch (action.kind.id) {
87 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
92 send_keyboard_report();
94 register_code(action.key.code);
96 unregister_code(action.key.code);
99 send_keyboard_report();
104 #ifndef NO_ACTION_TAPPING
108 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
110 switch (action.layer_tap.code) {
111 #ifndef NO_ACTION_ONESHOT
115 if (tap_count == 0) {
118 else if (tap_count == 1) {
119 dprint("MODS_TAP: Oneshot: start\n");
120 set_oneshot_mods(mods);
126 if (tap_count == 0) {
127 clear_oneshot_mods();
128 unregister_mods(mods);
130 else if (tap_count == 1) {
131 // Retain Oneshot mods
134 clear_oneshot_mods();
135 unregister_mods(mods);
140 case MODS_TAP_TOGGLE:
142 if (tap_count <= TAPPING_TOGGLE) {
146 if (tap_count < TAPPING_TOGGLE) {
147 unregister_mods(mods);
154 #ifndef IGNORE_MOD_TAP_INTERRUPT
155 if (record->tap.interrupted) {
156 dprint("mods_tap: tap: cancel: add_mods\n");
157 // ad hoc: set 0 to cancel tap
158 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(left only) */
255 layer_on(action.layer_tap.val);
256 register_mods(action.layer_tap.code & 0x0f);
258 layer_off(action.layer_tap.val);
259 unregister_mods(action.layer_tap.code & 0x0f);
265 if (tap_count < TAPPING_TOGGLE) {
266 layer_invert(action.layer_tap.val);
269 if (tap_count <= TAPPING_TOGGLE) {
270 layer_invert(action.layer_tap.val);
275 event.pressed ? layer_on(action.layer_tap.val) :
276 layer_off(action.layer_tap.val);
279 event.pressed ? layer_off(action.layer_tap.val) :
280 layer_on(action.layer_tap.val);
283 event.pressed ? layer_move(action.layer_tap.val) :
290 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
291 register_code(action.layer_tap.code);
293 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
294 layer_on(action.layer_tap.val);
298 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
299 unregister_code(action.layer_tap.code);
301 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
302 layer_off(action.layer_tap.val);
311 #ifndef NO_ACTION_MACRO
313 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
316 #ifdef BACKLIGHT_ENABLE
318 if (!event.pressed) {
319 switch (action.backlight.opt) {
320 case BACKLIGHT_INCREASE:
321 backlight_increase();
323 case BACKLIGHT_DECREASE:
324 backlight_decrease();
326 case BACKLIGHT_TOGGLE:
332 case BACKLIGHT_LEVEL:
333 backlight_level(action.backlight.level);
341 #ifndef NO_ACTION_FUNCTION
343 action_function(record, action.func.id, action.func.opt);
355 * Utilities for actions.
357 void register_code(uint8_t code)
363 #ifdef LOCKING_SUPPORT_ENABLE
364 else if (KC_LOCKING_CAPS == code) {
365 #ifdef LOCKING_RESYNC_ENABLE
366 // Resync: ignore if caps lock already is on
367 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
369 add_key(KC_CAPSLOCK);
370 send_keyboard_report();
371 del_key(KC_CAPSLOCK);
372 send_keyboard_report();
375 else if (KC_LOCKING_NUM == code) {
376 #ifdef LOCKING_RESYNC_ENABLE
377 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
380 send_keyboard_report();
382 send_keyboard_report();
385 else if (KC_LOCKING_SCROLL == code) {
386 #ifdef LOCKING_RESYNC_ENABLE
387 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
389 add_key(KC_SCROLLLOCK);
390 send_keyboard_report();
391 del_key(KC_SCROLLLOCK);
392 send_keyboard_report();
396 else if IS_KEY(code) {
397 // TODO: should push command_proc out of this block?
398 if (command_proc(code)) return;
400 #ifndef NO_ACTION_ONESHOT
402 if (oneshot_state.mods && !oneshot_state.disabled) {
403 uint8_t tmp_mods = get_mods();
404 add_mods(oneshot_state.mods);
407 send_keyboard_report();
410 send_keyboard_report();
417 send_keyboard_report();
420 else if IS_MOD(code) {
421 add_mods(MOD_BIT(code));
422 send_keyboard_report();
424 else if IS_SYSTEM(code) {
425 host_system_send(KEYCODE2SYSTEM(code));
427 else if IS_CONSUMER(code) {
428 host_consumer_send(KEYCODE2CONSUMER(code));
432 void unregister_code(uint8_t code)
438 #ifdef LOCKING_SUPPORT_ENABLE
439 else if (KC_LOCKING_CAPS == code) {
440 #ifdef LOCKING_RESYNC_ENABLE
441 // Resync: ignore if caps lock already is off
442 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
444 add_key(KC_CAPSLOCK);
445 send_keyboard_report();
446 del_key(KC_CAPSLOCK);
447 send_keyboard_report();
450 else if (KC_LOCKING_NUM == code) {
451 #ifdef LOCKING_RESYNC_ENABLE
452 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
455 send_keyboard_report();
457 send_keyboard_report();
460 else if (KC_LOCKING_SCROLL == code) {
461 #ifdef LOCKING_RESYNC_ENABLE
462 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
464 add_key(KC_SCROLLLOCK);
465 send_keyboard_report();
466 del_key(KC_SCROLLLOCK);
467 send_keyboard_report();
471 else if IS_KEY(code) {
473 send_keyboard_report();
475 else if IS_MOD(code) {
476 del_mods(MOD_BIT(code));
477 send_keyboard_report();
479 else if IS_SYSTEM(code) {
482 else if IS_CONSUMER(code) {
483 host_consumer_send(0);
487 void register_mods(uint8_t mods)
491 send_keyboard_report();
495 void unregister_mods(uint8_t mods)
499 send_keyboard_report();
503 void clear_keyboard(void)
506 clear_keyboard_but_mods();
509 void clear_keyboard_but_mods(void)
514 send_keyboard_report();
515 #ifdef MOUSEKEY_ENABLE
519 #ifdef EXTRAKEY_ENABLE
521 host_consumer_send(0);
525 bool is_tap_key(keypos_t key)
527 action_t action = layer_switch_get_action(key);
529 switch (action.kind.id) {
533 case ACT_LAYER_TAP_EXT:
534 switch (action.layer_tap.code) {
542 if (action.func.opt & FUNC_TAP) { return true; }
552 void debug_event(keyevent_t event)
554 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
557 void debug_record(keyrecord_t record)
559 debug_event(record.event);
560 #ifndef NO_ACTION_TAPPING
561 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
565 void debug_action(action_t action)
567 switch (action.kind.id) {
568 case ACT_LMODS: dprint("ACT_LMODS"); break;
569 case ACT_RMODS: dprint("ACT_RMODS"); break;
570 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
571 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
572 case ACT_USAGE: dprint("ACT_USAGE"); break;
573 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
574 case ACT_LAYER: dprint("ACT_LAYER"); break;
575 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
576 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
577 case ACT_MACRO: dprint("ACT_MACRO"); break;
578 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
579 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
580 default: dprint("UNKNOWN"); break;
582 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);