]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/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"
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 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 action_t action
= layer_switch_get_action(event
.key
);
69 dprint("ACTION: "); debug_action(action
);
70 #ifndef NO_ACTION_LAYER
71 dprint(" layer_state: "); layer_debug();
72 dprint(" default_layer_state: "); default_layer_debug();
76 switch (action
.kind
.id
) {
81 uint8_t mods
= (action
.kind
.id
== ACT_LMODS
) ? action
.key
.mods
:
86 send_keyboard_report();
88 register_code(action
.key
.code
);
90 unregister_code(action
.key
.code
);
93 send_keyboard_report();
98 #ifndef NO_ACTION_TAPPING
102 uint8_t mods
= (action
.kind
.id
== ACT_LMODS_TAP
) ? action
.key
.mods
:
104 switch (action
.layer_tap
.code
) {
105 #ifndef NO_ACTION_ONESHOT
109 if (tap_count
== 0) {
112 else if (tap_count
== 1) {
113 dprint("MODS_TAP: Oneshot: start\n");
114 set_oneshot_mods(mods
);
120 if (tap_count
== 0) {
121 clear_oneshot_mods();
122 unregister_mods(mods
);
124 else if (tap_count
== 1) {
125 // Retain Oneshot mods
128 clear_oneshot_mods();
129 unregister_mods(mods
);
134 case MODS_TAP_TOGGLE
:
136 if (tap_count
<= TAPPING_TOGGLE
) {
137 if (mods
& get_mods()) {
138 dprint("MODS_TAP_TOGGLE: toggle mods off\n");
139 unregister_mods(mods
);
141 dprint("MODS_TAP_TOGGLE: toggle mods on\n");
146 if (tap_count
< TAPPING_TOGGLE
) {
147 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
148 unregister_mods(mods
);
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;
161 dprint("MODS_TAP: Tap: register_code\n");
162 register_code(action
.key
.code
);
165 dprint("MODS_TAP: No tap: add_mods\n");
170 dprint("MODS_TAP: Tap: unregister_code\n");
171 unregister_code(action
.key
.code
);
173 dprint("MODS_TAP: No tap: add_mods\n");
174 unregister_mods(mods
);
182 #ifdef EXTRAKEY_ENABLE
183 /* other HID usage */
185 switch (action
.usage
.page
) {
188 host_system_send(action
.usage
.code
);
195 host_consumer_send(action
.usage
.code
);
197 host_consumer_send(0);
203 #ifdef MOUSEKEY_ENABLE
207 mousekey_on(action
.key
.code
);
210 mousekey_off(action
.key
.code
);
215 #ifndef NO_ACTION_LAYER
217 if (action
.layer_bitop
.on
== 0) {
218 /* Default Layer Bitwise Operation */
219 if (!event
.pressed
) {
220 uint8_t shift
= action
.layer_bitop
.part
*4;
221 uint32_t bits
= ((uint32_t)action
.layer_bitop
.bits
)<<shift
;
222 uint32_t mask
= (action
.layer_bitop
.xbit
) ? ~(((uint32_t)0xf)<<shift
) : 0;
223 switch (action
.layer_bitop
.op
) {
224 case OP_BIT_AND
: default_layer_and(bits
| mask
); break;
225 case OP_BIT_OR
: default_layer_or(bits
| mask
); break;
226 case OP_BIT_XOR
: default_layer_xor(bits
| mask
); break;
227 case OP_BIT_SET
: default_layer_and(mask
); default_layer_or(bits
); break;
231 /* Layer Bitwise Operation */
232 if (event
.pressed
? (action
.layer_bitop
.on
& ON_PRESS
) :
233 (action
.layer_bitop
.on
& ON_RELEASE
)) {
234 uint8_t shift
= action
.layer_bitop
.part
*4;
235 uint32_t bits
= ((uint32_t)action
.layer_bitop
.bits
)<<shift
;
236 uint32_t mask
= (action
.layer_bitop
.xbit
) ? ~(((uint32_t)0xf)<<shift
) : 0;
237 switch (action
.layer_bitop
.op
) {
238 case OP_BIT_AND
: layer_and(bits
| mask
); break;
239 case OP_BIT_OR
: layer_or(bits
| mask
); break;
240 case OP_BIT_XOR
: layer_xor(bits
| mask
); break;
241 case OP_BIT_SET
: layer_and(mask
); layer_or(bits
); break;
246 #ifndef NO_ACTION_TAPPING
248 case ACT_LAYER_TAP_EXT
:
249 switch (action
.layer_tap
.code
) {
251 /* layer On/Off with modifiers(left only) */
253 layer_on(action
.layer_tap
.val
);
254 register_mods(action
.layer_tap
.code
& 0x0f);
256 layer_off(action
.layer_tap
.val
);
257 unregister_mods(action
.layer_tap
.code
& 0x0f);
263 if (tap_count
< TAPPING_TOGGLE
) {
264 layer_invert(action
.layer_tap
.val
);
267 if (tap_count
<= TAPPING_TOGGLE
) {
268 layer_invert(action
.layer_tap
.val
);
273 event
.pressed
? layer_on(action
.layer_tap
.val
) :
274 layer_off(action
.layer_tap
.val
);
277 event
.pressed
? layer_off(action
.layer_tap
.val
) :
278 layer_on(action
.layer_tap
.val
);
281 event
.pressed
? layer_move(action
.layer_tap
.val
) :
288 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
289 register_code(action
.layer_tap
.code
);
291 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
292 layer_on(action
.layer_tap
.val
);
296 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
297 unregister_code(action
.layer_tap
.code
);
299 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
300 layer_off(action
.layer_tap
.val
);
309 #ifndef NO_ACTION_MACRO
311 action_macro_play(action_get_macro(record
, action
.func
.id
, action
.func
.opt
));
314 #ifdef BACKLIGHT_ENABLE
316 if (!event
.pressed
) {
317 switch (action
.backlight
.opt
) {
318 case BACKLIGHT_INCREASE
:
319 backlight_increase();
321 case BACKLIGHT_DECREASE
:
322 backlight_decrease();
324 case BACKLIGHT_TOGGLE
:
330 case BACKLIGHT_LEVEL
:
331 backlight_level(action
.backlight
.level
);
339 #ifndef NO_ACTION_FUNCTION
341 action_function(record
, action
.func
.id
, action
.func
.opt
);
353 * Utilities for actions.
355 void register_code(uint8_t code
)
361 #ifdef LOCKING_SUPPORT_ENABLE
362 else if (KC_LOCKING_CAPS
== code
) {
363 #ifdef LOCKING_RESYNC_ENABLE
364 // Resync: ignore if caps lock already is on
365 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
)) return;
367 add_key(KC_CAPSLOCK
);
368 send_keyboard_report();
370 del_key(KC_CAPSLOCK
);
371 send_keyboard_report();
374 else if (KC_LOCKING_NUM
== code
) {
375 #ifdef LOCKING_RESYNC_ENABLE
376 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
)) return;
379 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();
392 del_key(KC_SCROLLLOCK
);
393 send_keyboard_report();
397 else if IS_KEY(code
) {
398 // TODO: should push command_proc out of this block?
399 if (command_proc(code
)) return;
401 #ifndef NO_ACTION_ONESHOT
403 if (oneshot_state.mods && !oneshot_state.disabled) {
404 uint8_t tmp_mods = get_mods();
405 add_mods(oneshot_state.mods);
408 send_keyboard_report();
411 send_keyboard_report();
418 send_keyboard_report();
421 else if IS_MOD(code
) {
422 add_mods(MOD_BIT(code
));
423 send_keyboard_report();
425 else if IS_SYSTEM(code
) {
426 host_system_send(KEYCODE2SYSTEM(code
));
428 else if IS_CONSUMER(code
) {
429 host_consumer_send(KEYCODE2CONSUMER(code
));
433 void unregister_code(uint8_t code
)
439 #ifdef LOCKING_SUPPORT_ENABLE
440 else if (KC_LOCKING_CAPS
== code
) {
441 #ifdef LOCKING_RESYNC_ENABLE
442 // Resync: ignore if caps lock already is off
443 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
))) return;
445 add_key(KC_CAPSLOCK
);
446 send_keyboard_report();
448 del_key(KC_CAPSLOCK
);
449 send_keyboard_report();
452 else if (KC_LOCKING_NUM
== code
) {
453 #ifdef LOCKING_RESYNC_ENABLE
454 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
))) return;
457 send_keyboard_report();
460 send_keyboard_report();
463 else if (KC_LOCKING_SCROLL
== code
) {
464 #ifdef LOCKING_RESYNC_ENABLE
465 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK
))) return;
467 add_key(KC_SCROLLLOCK
);
468 send_keyboard_report();
470 del_key(KC_SCROLLLOCK
);
471 send_keyboard_report();
475 else if IS_KEY(code
) {
477 send_keyboard_report();
479 else if IS_MOD(code
) {
480 del_mods(MOD_BIT(code
));
481 send_keyboard_report();
483 else if IS_SYSTEM(code
) {
486 else if IS_CONSUMER(code
) {
487 host_consumer_send(0);
491 void register_mods(uint8_t mods
)
495 send_keyboard_report();
499 void unregister_mods(uint8_t mods
)
503 send_keyboard_report();
507 void clear_keyboard(void)
510 clear_keyboard_but_mods();
513 void clear_keyboard_but_mods(void)
517 send_keyboard_report();
518 #ifdef MOUSEKEY_ENABLE
522 #ifdef EXTRAKEY_ENABLE
524 host_consumer_send(0);
528 bool is_tap_key(keypos_t key
)
530 action_t action
= layer_switch_get_action(key
);
532 switch (action
.kind
.id
) {
536 case ACT_LAYER_TAP_EXT
:
537 switch (action
.layer_tap
.code
) {
545 if (action
.func
.opt
& FUNC_TAP
) { return true; }
555 void debug_event(keyevent_t event
)
557 dprintf("%04X%c(%u)", (event
.key
.row
<<8 | event
.key
.col
), (event
.pressed
? 'd' : 'u'), event
.time
);
560 void debug_record(keyrecord_t record
)
562 debug_event(record
.event
);
563 #ifndef NO_ACTION_TAPPING
564 dprintf(":%u%c", record
.tap
.count
, (record
.tap
.interrupted
? '-' : ' '));
568 void debug_action(action_t action
)
570 switch (action
.kind
.id
) {
571 case ACT_LMODS
: dprint("ACT_LMODS"); break;
572 case ACT_RMODS
: dprint("ACT_RMODS"); break;
573 case ACT_LMODS_TAP
: dprint("ACT_LMODS_TAP"); break;
574 case ACT_RMODS_TAP
: dprint("ACT_RMODS_TAP"); break;
575 case ACT_USAGE
: dprint("ACT_USAGE"); break;
576 case ACT_MOUSEKEY
: dprint("ACT_MOUSEKEY"); break;
577 case ACT_LAYER
: dprint("ACT_LAYER"); break;
578 case ACT_LAYER_TAP
: dprint("ACT_LAYER_TAP"); break;
579 case ACT_LAYER_TAP_EXT
: dprint("ACT_LAYER_TAP_EXT"); break;
580 case ACT_MACRO
: dprint("ACT_MACRO"); break;
581 case ACT_COMMAND
: dprint("ACT_COMMAND"); break;
582 case ACT_FUNCTION
: dprint("ACT_FUNCTION"); break;
583 default: dprint("UNKNOWN"); break;
585 dprintf("[%X:%02X]", action
.kind
.param
>>8, action
.kind
.param
&0xff);