]>
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
);
134 if (record
->tap
.interrupted
) {
135 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
136 // ad hoc: set 0 to cancel tap
137 record
->tap
.count
= 0;
140 dprint("MODS_TAP: Tap: register_code\n");
141 register_code(action
.key
.code
);
144 dprint("MODS_TAP: No tap: add_mods\n");
149 dprint("MODS_TAP: Tap: unregister_code\n");
150 unregister_code(action
.key
.code
);
152 dprint("MODS_TAP: No tap: add_mods\n");
153 unregister_mods(mods
);
161 #ifdef EXTRAKEY_ENABLE
162 /* other HID usage */
164 switch (action
.usage
.page
) {
167 host_system_send(action
.usage
.code
);
174 host_consumer_send(action
.usage
.code
);
176 host_consumer_send(0);
182 #ifdef MOUSEKEY_ENABLE
186 mousekey_on(action
.key
.code
);
189 mousekey_off(action
.key
.code
);
194 #ifndef NO_ACTION_LAYER
196 if (action
.layer_bitop
.on
== 0) {
197 /* Default Layer Bitwise Operation */
198 if (!event
.pressed
) {
199 uint8_t shift
= action
.layer_bitop
.part
*4;
200 uint32_t bits
= ((uint32_t)action
.layer_bitop
.bits
)<<shift
;
201 uint32_t mask
= (action
.layer_bitop
.xbit
) ? ~(((uint32_t)0xf)<<shift
) : 0;
202 switch (action
.layer_bitop
.op
) {
203 case OP_BIT_AND
: default_layer_and(bits
| mask
); break;
204 case OP_BIT_OR
: default_layer_or(bits
| mask
); break;
205 case OP_BIT_XOR
: default_layer_xor(bits
| mask
); break;
206 case OP_BIT_SET
: default_layer_and(mask
); default_layer_or(bits
); break;
210 /* Layer Bitwise Operation */
211 if (event
.pressed
? (action
.layer_bitop
.on
& ON_PRESS
) :
212 (action
.layer_bitop
.on
& ON_RELEASE
)) {
213 uint8_t shift
= action
.layer_bitop
.part
*4;
214 uint32_t bits
= ((uint32_t)action
.layer_bitop
.bits
)<<shift
;
215 uint32_t mask
= (action
.layer_bitop
.xbit
) ? ~(((uint32_t)0xf)<<shift
) : 0;
216 switch (action
.layer_bitop
.op
) {
217 case OP_BIT_AND
: layer_and(bits
| mask
); break;
218 case OP_BIT_OR
: layer_or(bits
| mask
); break;
219 case OP_BIT_XOR
: layer_xor(bits
| mask
); break;
220 case OP_BIT_SET
: layer_and(mask
); layer_or(bits
); break;
225 #ifndef NO_ACTION_TAPPING
227 case ACT_LAYER_TAP_EXT
:
228 switch (action
.layer_tap
.code
) {
232 if (tap_count
< TAPPING_TOGGLE
) {
233 layer_invert(action
.layer_tap
.val
);
236 if (tap_count
<= TAPPING_TOGGLE
) {
237 layer_invert(action
.layer_tap
.val
);
242 event
.pressed
? layer_on(action
.layer_tap
.val
) :
243 layer_off(action
.layer_tap
.val
);
246 event
.pressed
? layer_off(action
.layer_tap
.val
) :
247 layer_on(action
.layer_tap
.val
);
250 event
.pressed
? layer_move(action
.layer_tap
.val
) :
257 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
258 register_code(action
.layer_tap
.code
);
260 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
261 layer_on(action
.layer_tap
.val
);
265 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
266 unregister_code(action
.layer_tap
.code
);
268 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
269 layer_off(action
.layer_tap
.val
);
278 #ifndef NO_ACTION_MACRO
280 action_macro_play(action_get_macro(record
, action
.func
.id
, action
.func
.opt
));
283 #ifdef BACKLIGHT_ENABLE
285 if (!event
.pressed
) {
286 switch (action
.backlight
.id
) {
287 case BACKLIGHT_INCREASE
:
288 backlight_increase();
290 case BACKLIGHT_DECREASE
:
291 backlight_decrease();
293 case BACKLIGHT_TOGGLE
:
305 #ifndef NO_ACTION_FUNCTION
307 action_function(record
, action
.func
.id
, action
.func
.opt
);
319 * Utilities for actions.
321 void register_code(uint8_t code
)
327 #ifdef LOCKING_SUPPORT_ENABLE
328 else if (KC_LOCKING_CAPS
== code
) {
329 #ifdef LOCKING_RESYNC_ENABLE
330 // Resync: ignore if caps lock already is on
331 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
)) return;
333 add_key(KC_CAPSLOCK
);
334 send_keyboard_report();
335 del_key(KC_CAPSLOCK
);
336 send_keyboard_report();
339 else if (KC_LOCKING_NUM
== code
) {
340 #ifdef LOCKING_RESYNC_ENABLE
341 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
)) return;
344 send_keyboard_report();
346 send_keyboard_report();
349 else if (KC_LOCKING_SCROLL
== code
) {
350 #ifdef LOCKING_RESYNC_ENABLE
351 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK
)) return;
353 add_key(KC_SCROLLLOCK
);
354 send_keyboard_report();
355 del_key(KC_SCROLLLOCK
);
356 send_keyboard_report();
360 else if IS_KEY(code
) {
361 // TODO: should push command_proc out of this block?
362 if (command_proc(code
)) return;
364 #ifndef NO_ACTION_ONESHOT
366 if (oneshot_state.mods && !oneshot_state.disabled) {
367 uint8_t tmp_mods = get_mods();
368 add_mods(oneshot_state.mods);
371 send_keyboard_report();
374 send_keyboard_report();
381 send_keyboard_report();
384 else if IS_MOD(code
) {
385 add_mods(MOD_BIT(code
));
386 send_keyboard_report();
388 else if IS_SYSTEM(code
) {
389 host_system_send(KEYCODE2SYSTEM(code
));
391 else if IS_CONSUMER(code
) {
392 host_consumer_send(KEYCODE2CONSUMER(code
));
396 void unregister_code(uint8_t code
)
402 #ifdef LOCKING_SUPPORT_ENABLE
403 else if (KC_LOCKING_CAPS
== code
) {
404 #ifdef LOCKING_RESYNC_ENABLE
405 // Resync: ignore if caps lock already is off
406 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
))) return;
408 add_key(KC_CAPSLOCK
);
409 send_keyboard_report();
410 del_key(KC_CAPSLOCK
);
411 send_keyboard_report();
414 else if (KC_LOCKING_NUM
== code
) {
415 #ifdef LOCKING_RESYNC_ENABLE
416 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
))) return;
419 send_keyboard_report();
421 send_keyboard_report();
424 else if (KC_LOCKING_SCROLL
== code
) {
425 #ifdef LOCKING_RESYNC_ENABLE
426 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK
))) return;
428 add_key(KC_SCROLLLOCK
);
429 send_keyboard_report();
430 del_key(KC_SCROLLLOCK
);
431 send_keyboard_report();
435 else if IS_KEY(code
) {
437 send_keyboard_report();
439 else if IS_MOD(code
) {
440 del_mods(MOD_BIT(code
));
441 send_keyboard_report();
443 else if IS_SYSTEM(code
) {
446 else if IS_CONSUMER(code
) {
447 host_consumer_send(0);
451 void register_mods(uint8_t mods
)
455 send_keyboard_report();
459 void unregister_mods(uint8_t mods
)
463 send_keyboard_report();
467 void clear_keyboard(void)
470 clear_keyboard_but_mods();
473 void clear_keyboard_but_mods(void)
477 send_keyboard_report();
478 #ifdef MOUSEKEY_ENABLE
482 #ifdef EXTRAKEY_ENABLE
484 host_consumer_send(0);
488 bool is_tap_key(key_t key
)
490 action_t action
= layer_switch_get_action(key
);
492 switch (action
.kind
.id
) {
496 case ACT_LAYER_TAP_EXT
:
500 if (action
.func
.opt
& FUNC_TAP
) { return true; }
510 void debug_event(keyevent_t event
)
512 dprintf("%04X%c(%u)", (event
.key
.row
<<8 | event
.key
.col
), (event
.pressed
? 'd' : 'u'), event
.time
);
515 void debug_record(keyrecord_t record
)
517 debug_event(record
.event
);
518 #ifndef NO_ACTION_TAPPING
519 dprintf(":%u%c", record
.tap
.count
, (record
.tap
.interrupted
? '-' : ' '));
523 void debug_action(action_t action
)
525 switch (action
.kind
.id
) {
526 case ACT_LMODS
: dprint("ACT_LMODS"); break;
527 case ACT_RMODS
: dprint("ACT_RMODS"); break;
528 case ACT_LMODS_TAP
: dprint("ACT_LMODS_TAP"); break;
529 case ACT_RMODS_TAP
: dprint("ACT_RMODS_TAP"); break;
530 case ACT_USAGE
: dprint("ACT_USAGE"); break;
531 case ACT_MOUSEKEY
: dprint("ACT_MOUSEKEY"); break;
532 case ACT_LAYER
: dprint("ACT_LAYER"); break;
533 case ACT_LAYER_TAP
: dprint("ACT_LAYER_TAP"); break;
534 case ACT_LAYER_TAP_EXT
: dprint("ACT_LAYER_TAP_EXT"); break;
535 case ACT_MACRO
: dprint("ACT_MACRO"); break;
536 case ACT_COMMAND
: dprint("ACT_COMMAND"); break;
537 case ACT_FUNCTION
: dprint("ACT_FUNCTION"); break;
538 default: dprint("UNKNOWN"); break;
540 dprintf("[%X:%02X]", action
.kind
.param
>>8, action
.kind
.param
&0xff);