]>
git.gir.st - tmk_keyboard.git/blob - common/action.c
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
) {
137 if (tap_count
< TAPPING_TOGGLE
) {
138 unregister_mods(mods
);
145 if (record
->tap
.interrupted
) {
146 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
147 // ad hoc: set 0 to cancel tap
148 record
->tap
.count
= 0;
151 dprint("MODS_TAP: Tap: register_code\n");
152 register_code(action
.key
.code
);
155 dprint("MODS_TAP: No tap: add_mods\n");
160 dprint("MODS_TAP: Tap: unregister_code\n");
161 unregister_code(action
.key
.code
);
163 dprint("MODS_TAP: No tap: add_mods\n");
164 unregister_mods(mods
);
172 #ifdef EXTRAKEY_ENABLE
173 /* other HID usage */
175 switch (action
.usage
.page
) {
178 host_system_send(action
.usage
.code
);
185 host_consumer_send(action
.usage
.code
);
187 host_consumer_send(0);
193 #ifdef MOUSEKEY_ENABLE
197 mousekey_on(action
.key
.code
);
200 mousekey_off(action
.key
.code
);
205 #ifndef NO_ACTION_LAYER
207 if (action
.layer_bitop
.on
== 0) {
208 /* Default Layer Bitwise Operation */
209 if (!event
.pressed
) {
210 uint8_t shift
= action
.layer_bitop
.part
*4;
211 uint32_t bits
= ((uint32_t)action
.layer_bitop
.bits
)<<shift
;
212 uint32_t mask
= (action
.layer_bitop
.xbit
) ? ~(((uint32_t)0xf)<<shift
) : 0;
213 switch (action
.layer_bitop
.op
) {
214 case OP_BIT_AND
: default_layer_and(bits
| mask
); break;
215 case OP_BIT_OR
: default_layer_or(bits
| mask
); break;
216 case OP_BIT_XOR
: default_layer_xor(bits
| mask
); break;
217 case OP_BIT_SET
: default_layer_and(mask
); default_layer_or(bits
); break;
221 /* Layer Bitwise Operation */
222 if (event
.pressed
? (action
.layer_bitop
.on
& ON_PRESS
) :
223 (action
.layer_bitop
.on
& ON_RELEASE
)) {
224 uint8_t shift
= action
.layer_bitop
.part
*4;
225 uint32_t bits
= ((uint32_t)action
.layer_bitop
.bits
)<<shift
;
226 uint32_t mask
= (action
.layer_bitop
.xbit
) ? ~(((uint32_t)0xf)<<shift
) : 0;
227 switch (action
.layer_bitop
.op
) {
228 case OP_BIT_AND
: layer_and(bits
| mask
); break;
229 case OP_BIT_OR
: layer_or(bits
| mask
); break;
230 case OP_BIT_XOR
: layer_xor(bits
| mask
); break;
231 case OP_BIT_SET
: layer_and(mask
); layer_or(bits
); break;
236 #ifndef NO_ACTION_TAPPING
238 case ACT_LAYER_TAP_EXT
:
239 switch (action
.layer_tap
.code
) {
243 if (tap_count
< TAPPING_TOGGLE
) {
244 layer_invert(action
.layer_tap
.val
);
247 if (tap_count
<= TAPPING_TOGGLE
) {
248 layer_invert(action
.layer_tap
.val
);
253 event
.pressed
? layer_on(action
.layer_tap
.val
) :
254 layer_off(action
.layer_tap
.val
);
257 event
.pressed
? layer_off(action
.layer_tap
.val
) :
258 layer_on(action
.layer_tap
.val
);
261 event
.pressed
? layer_move(action
.layer_tap
.val
) :
268 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
269 register_code(action
.layer_tap
.code
);
271 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
272 layer_on(action
.layer_tap
.val
);
276 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
277 unregister_code(action
.layer_tap
.code
);
279 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
280 layer_off(action
.layer_tap
.val
);
289 #ifndef NO_ACTION_MACRO
291 action_macro_play(action_get_macro(record
, action
.func
.id
, action
.func
.opt
));
294 #ifdef BACKLIGHT_ENABLE
296 if (!event
.pressed
) {
297 switch (action
.backlight
.id
) {
298 case BACKLIGHT_INCREASE
:
299 backlight_increase();
301 case BACKLIGHT_DECREASE
:
302 backlight_decrease();
304 case BACKLIGHT_TOGGLE
:
316 #ifndef NO_ACTION_FUNCTION
318 action_function(record
, action
.func
.id
, action
.func
.opt
);
330 * Utilities for actions.
332 void register_code(uint8_t code
)
338 #ifdef LOCKING_SUPPORT_ENABLE
339 else if (KC_LOCKING_CAPS
== code
) {
340 #ifdef LOCKING_RESYNC_ENABLE
341 // Resync: ignore if caps lock already is on
342 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
)) return;
344 add_key(KC_CAPSLOCK
);
345 send_keyboard_report();
346 del_key(KC_CAPSLOCK
);
347 send_keyboard_report();
350 else if (KC_LOCKING_NUM
== code
) {
351 #ifdef LOCKING_RESYNC_ENABLE
352 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
)) return;
355 send_keyboard_report();
357 send_keyboard_report();
360 else if (KC_LOCKING_SCROLL
== code
) {
361 #ifdef LOCKING_RESYNC_ENABLE
362 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK
)) return;
364 add_key(KC_SCROLLLOCK
);
365 send_keyboard_report();
366 del_key(KC_SCROLLLOCK
);
367 send_keyboard_report();
371 else if IS_KEY(code
) {
372 // TODO: should push command_proc out of this block?
373 if (command_proc(code
)) return;
375 #ifndef NO_ACTION_ONESHOT
377 if (oneshot_state.mods && !oneshot_state.disabled) {
378 uint8_t tmp_mods = get_mods();
379 add_mods(oneshot_state.mods);
382 send_keyboard_report();
385 send_keyboard_report();
392 send_keyboard_report();
395 else if IS_MOD(code
) {
396 add_mods(MOD_BIT(code
));
397 send_keyboard_report();
399 else if IS_SYSTEM(code
) {
400 host_system_send(KEYCODE2SYSTEM(code
));
402 else if IS_CONSUMER(code
) {
403 host_consumer_send(KEYCODE2CONSUMER(code
));
407 void unregister_code(uint8_t code
)
413 #ifdef LOCKING_SUPPORT_ENABLE
414 else if (KC_LOCKING_CAPS
== code
) {
415 #ifdef LOCKING_RESYNC_ENABLE
416 // Resync: ignore if caps lock already is off
417 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
))) return;
419 add_key(KC_CAPSLOCK
);
420 send_keyboard_report();
421 del_key(KC_CAPSLOCK
);
422 send_keyboard_report();
425 else if (KC_LOCKING_NUM
== code
) {
426 #ifdef LOCKING_RESYNC_ENABLE
427 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
))) return;
430 send_keyboard_report();
432 send_keyboard_report();
435 else if (KC_LOCKING_SCROLL
== code
) {
436 #ifdef LOCKING_RESYNC_ENABLE
437 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK
))) return;
439 add_key(KC_SCROLLLOCK
);
440 send_keyboard_report();
441 del_key(KC_SCROLLLOCK
);
442 send_keyboard_report();
446 else if IS_KEY(code
) {
448 send_keyboard_report();
450 else if IS_MOD(code
) {
451 del_mods(MOD_BIT(code
));
452 send_keyboard_report();
454 else if IS_SYSTEM(code
) {
457 else if IS_CONSUMER(code
) {
458 host_consumer_send(0);
462 void register_mods(uint8_t mods
)
466 send_keyboard_report();
470 void unregister_mods(uint8_t mods
)
474 send_keyboard_report();
478 void clear_keyboard(void)
481 clear_keyboard_but_mods();
484 void clear_keyboard_but_mods(void)
488 send_keyboard_report();
489 #ifdef MOUSEKEY_ENABLE
493 #ifdef EXTRAKEY_ENABLE
495 host_consumer_send(0);
499 bool is_tap_key(key_t key
)
501 action_t action
= layer_switch_get_action(key
);
503 switch (action
.kind
.id
) {
507 case ACT_LAYER_TAP_EXT
:
511 if (action
.func
.opt
& FUNC_TAP
) { return true; }
521 void debug_event(keyevent_t event
)
523 dprintf("%04X%c(%u)", (event
.key
.row
<<8 | event
.key
.col
), (event
.pressed
? 'd' : 'u'), event
.time
);
526 void debug_record(keyrecord_t record
)
528 debug_event(record
.event
);
529 #ifndef NO_ACTION_TAPPING
530 dprintf(":%u%c", record
.tap
.count
, (record
.tap
.interrupted
? '-' : ' '));
534 void debug_action(action_t action
)
536 switch (action
.kind
.id
) {
537 case ACT_LMODS
: dprint("ACT_LMODS"); break;
538 case ACT_RMODS
: dprint("ACT_RMODS"); break;
539 case ACT_LMODS_TAP
: dprint("ACT_LMODS_TAP"); break;
540 case ACT_RMODS_TAP
: dprint("ACT_RMODS_TAP"); break;
541 case ACT_USAGE
: dprint("ACT_USAGE"); break;
542 case ACT_MOUSEKEY
: dprint("ACT_MOUSEKEY"); break;
543 case ACT_LAYER
: dprint("ACT_LAYER"); break;
544 case ACT_LAYER_TAP
: dprint("ACT_LAYER_TAP"); break;
545 case ACT_LAYER_TAP_EXT
: dprint("ACT_LAYER_TAP_EXT"); break;
546 case ACT_MACRO
: dprint("ACT_MACRO"); break;
547 case ACT_COMMAND
: dprint("ACT_COMMAND"); break;
548 case ACT_FUNCTION
: dprint("ACT_FUNCTION"); break;
549 default: dprint("UNKNOWN"); break;
551 dprintf("[%X:%02X]", action
.kind
.param
>>8, action
.kind
.param
&0xff);