]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/common/action.c
ab6e9d895807898fde8b7112eadfb0eb8053b9e1
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
.key
.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 */
253 layer_on(action
.layer_tap
.val
);
254 register_mods((action
.layer_tap
.code
& 0x10) ?
255 (action
.layer_tap
.code
& 0x0f) << 4 :
256 (action
.layer_tap
.code
& 0x0f));
258 layer_off(action
.layer_tap
.val
);
259 unregister_mods((action
.layer_tap
.code
& 0x10) ?
260 (action
.layer_tap
.code
& 0x0f) << 4 :
261 (action
.layer_tap
.code
& 0x0f));
267 if (tap_count
< TAPPING_TOGGLE
) {
268 layer_invert(action
.layer_tap
.val
);
271 if (tap_count
<= TAPPING_TOGGLE
) {
272 layer_invert(action
.layer_tap
.val
);
277 event
.pressed
? layer_on(action
.layer_tap
.val
) :
278 layer_off(action
.layer_tap
.val
);
281 event
.pressed
? layer_off(action
.layer_tap
.val
) :
282 layer_on(action
.layer_tap
.val
);
285 event
.pressed
? layer_move(action
.layer_tap
.val
) :
292 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
293 register_code(action
.layer_tap
.code
);
295 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
296 layer_on(action
.layer_tap
.val
);
300 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
301 unregister_code(action
.layer_tap
.code
);
303 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
304 layer_off(action
.layer_tap
.val
);
313 #ifndef NO_ACTION_MACRO
315 action_macro_play(action_get_macro(record
, action
.func
.id
, action
.func
.opt
));
318 #ifdef BACKLIGHT_ENABLE
320 if (!event
.pressed
) {
321 switch (action
.backlight
.opt
) {
322 case BACKLIGHT_INCREASE
:
323 backlight_increase();
325 case BACKLIGHT_DECREASE
:
326 backlight_decrease();
328 case BACKLIGHT_TOGGLE
:
334 case BACKLIGHT_LEVEL
:
335 backlight_level(action
.backlight
.level
);
343 #ifndef NO_ACTION_FUNCTION
345 action_function(record
, action
.func
.id
, action
.func
.opt
);
357 * Utilities for actions.
359 void register_code(uint8_t code
)
365 #ifdef LOCKING_SUPPORT_ENABLE
366 else if (KC_LOCKING_CAPS
== code
) {
367 #ifdef LOCKING_RESYNC_ENABLE
368 // Resync: ignore if caps lock already is on
369 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
)) return;
371 add_key(KC_CAPSLOCK
);
372 send_keyboard_report();
374 del_key(KC_CAPSLOCK
);
375 send_keyboard_report();
378 else if (KC_LOCKING_NUM
== code
) {
379 #ifdef LOCKING_RESYNC_ENABLE
380 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
)) return;
383 send_keyboard_report();
386 send_keyboard_report();
389 else if (KC_LOCKING_SCROLL
== code
) {
390 #ifdef LOCKING_RESYNC_ENABLE
391 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK
)) return;
393 add_key(KC_SCROLLLOCK
);
394 send_keyboard_report();
396 del_key(KC_SCROLLLOCK
);
397 send_keyboard_report();
401 else if IS_KEY(code
) {
402 // TODO: should push command_proc out of this block?
403 if (command_proc(code
)) return;
405 #ifndef NO_ACTION_ONESHOT
407 if (oneshot_state.mods && !oneshot_state.disabled) {
408 uint8_t tmp_mods = get_mods();
409 add_mods(oneshot_state.mods);
412 send_keyboard_report();
415 send_keyboard_report();
422 send_keyboard_report();
425 else if IS_MOD(code
) {
426 add_mods(MOD_BIT(code
));
427 send_keyboard_report();
429 else if IS_SYSTEM(code
) {
430 host_system_send(KEYCODE2SYSTEM(code
));
432 else if IS_CONSUMER(code
) {
433 host_consumer_send(KEYCODE2CONSUMER(code
));
437 void unregister_code(uint8_t code
)
443 #ifdef LOCKING_SUPPORT_ENABLE
444 else if (KC_LOCKING_CAPS
== code
) {
445 #ifdef LOCKING_RESYNC_ENABLE
446 // Resync: ignore if caps lock already is off
447 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
))) return;
449 add_key(KC_CAPSLOCK
);
450 send_keyboard_report();
452 del_key(KC_CAPSLOCK
);
453 send_keyboard_report();
456 else if (KC_LOCKING_NUM
== code
) {
457 #ifdef LOCKING_RESYNC_ENABLE
458 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK
))) return;
461 send_keyboard_report();
464 send_keyboard_report();
467 else if (KC_LOCKING_SCROLL
== code
) {
468 #ifdef LOCKING_RESYNC_ENABLE
469 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK
))) return;
471 add_key(KC_SCROLLLOCK
);
472 send_keyboard_report();
474 del_key(KC_SCROLLLOCK
);
475 send_keyboard_report();
479 else if IS_KEY(code
) {
481 send_keyboard_report();
483 else if IS_MOD(code
) {
484 del_mods(MOD_BIT(code
));
485 send_keyboard_report();
487 else if IS_SYSTEM(code
) {
490 else if IS_CONSUMER(code
) {
491 host_consumer_send(0);
495 void register_mods(uint8_t mods
)
499 send_keyboard_report();
503 void unregister_mods(uint8_t mods
)
507 send_keyboard_report();
511 void clear_keyboard(void)
514 clear_keyboard_but_mods();
517 void clear_keyboard_but_mods(void)
521 send_keyboard_report();
522 #ifdef MOUSEKEY_ENABLE
526 #ifdef EXTRAKEY_ENABLE
528 host_consumer_send(0);
532 bool is_tap_key(keypos_t key
)
534 action_t action
= layer_switch_get_action(key
);
536 switch (action
.kind
.id
) {
539 switch (action
.key
.code
) {
541 case MODS_TAP_TOGGLE
:
542 case KC_A
... KC_EXSEL
: // tap key
543 case KC_LCTRL
... KC_RGUI
: // tap key
547 case ACT_LAYER_TAP_EXT
:
548 switch (action
.layer_tap
.code
) {
549 case 0xc0 ... 0xdf: // with modifiers
551 case KC_A
... KC_EXSEL
: // tap key
552 case KC_LCTRL
... KC_RGUI
: // tap key
559 if (action
.func
.opt
& FUNC_TAP
) { return true; }
569 void debug_event(keyevent_t event
)
571 dprintf("%04X%c(%u)", (event
.key
.row
<<8 | event
.key
.col
), (event
.pressed
? 'd' : 'u'), event
.time
);
574 void debug_record(keyrecord_t record
)
576 debug_event(record
.event
);
577 #ifndef NO_ACTION_TAPPING
578 dprintf(":%u%c", record
.tap
.count
, (record
.tap
.interrupted
? '-' : ' '));
582 void debug_action(action_t action
)
584 switch (action
.kind
.id
) {
585 case ACT_LMODS
: dprint("ACT_LMODS"); break;
586 case ACT_RMODS
: dprint("ACT_RMODS"); break;
587 case ACT_LMODS_TAP
: dprint("ACT_LMODS_TAP"); break;
588 case ACT_RMODS_TAP
: dprint("ACT_RMODS_TAP"); break;
589 case ACT_USAGE
: dprint("ACT_USAGE"); break;
590 case ACT_MOUSEKEY
: dprint("ACT_MOUSEKEY"); break;
591 case ACT_LAYER
: dprint("ACT_LAYER"); break;
592 case ACT_LAYER_TAP
: dprint("ACT_LAYER_TAP"); break;
593 case ACT_LAYER_TAP_EXT
: dprint("ACT_LAYER_TAP_EXT"); break;
594 case ACT_MACRO
: dprint("ACT_MACRO"); break;
595 case ACT_COMMAND
: dprint("ACT_COMMAND"); break;
596 case ACT_FUNCTION
: dprint("ACT_FUNCTION"); break;
597 default: dprint("UNKNOWN"); break;
599 dprintf("[%X:%02X]", action
.kind
.param
>>8, action
.kind
.param
&0xff);