]> git.gir.st - tmk_keyboard.git/blob - common/action.c
b9040f5b7c6cbf0b45820625193f80af2ee270f7
[tmk_keyboard.git] / common / action.c
1 /*
2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17 #include "host.h"
18 #include "keycode.h"
19 #include "keyboard.h"
20 #include "mousekey.h"
21 #include "command.h"
22 #include "led.h"
23 #include "backlight.h"
24 #include "action_layer.h"
25 #include "action_tapping.h"
26 #include "action_macro.h"
27 #include "action_util.h"
28 #include "action.h"
29 #include "hook.h"
30
31 #ifdef DEBUG_ACTION
32 #include "debug.h"
33 #else
34 #include "nodebug.h"
35 #endif
36
37
38 void action_exec(keyevent_t event)
39 {
40 if (!IS_NOEVENT(event)) {
41 dprint("\n---- action_exec: start -----\n");
42 dprint("EVENT: "); debug_event(event); dprintln();
43 hook_matrix_change(event);
44 }
45
46 keyrecord_t record = { .event = event };
47
48 #ifndef NO_ACTION_TAPPING
49 action_tapping_process(record);
50 #else
51 process_action(&record);
52 if (!IS_NOEVENT(record.event)) {
53 dprint("processed: "); debug_record(record); dprintln();
54 }
55 #endif
56 }
57
58 void process_action(keyrecord_t *record)
59 {
60 keyevent_t event = record->event;
61 #ifndef NO_ACTION_TAPPING
62 uint8_t tap_count = record->tap.count;
63 #endif
64
65 if (IS_NOEVENT(event)) { return; }
66
67 action_t action = layer_switch_get_action(event.key);
68 dprint("ACTION: "); debug_action(action);
69 #ifndef NO_ACTION_LAYER
70 dprint(" layer_state: "); layer_debug();
71 dprint(" default_layer_state: "); default_layer_debug();
72 #endif
73 dprintln();
74
75 switch (action.kind.id) {
76 /* Key and Mods */
77 case ACT_LMODS:
78 case ACT_RMODS:
79 {
80 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
81 action.key.mods<<4;
82 if (event.pressed) {
83 if (mods) {
84 add_weak_mods(mods);
85 send_keyboard_report();
86 }
87 register_code(action.key.code);
88 } else {
89 unregister_code(action.key.code);
90 if (mods) {
91 del_weak_mods(mods);
92 send_keyboard_report();
93 }
94 }
95 }
96 break;
97 #ifndef NO_ACTION_TAPPING
98 case ACT_LMODS_TAP:
99 case ACT_RMODS_TAP:
100 {
101 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
102 action.key.mods<<4;
103 switch (action.layer_tap.code) {
104 #ifndef NO_ACTION_ONESHOT
105 case MODS_ONESHOT:
106 // Oneshot modifier
107 if (event.pressed) {
108 if (tap_count == 0) {
109 register_mods(mods);
110 }
111 else if (tap_count == 1) {
112 dprint("MODS_TAP: Oneshot: start\n");
113 set_oneshot_mods(mods);
114 }
115 else {
116 register_mods(mods);
117 }
118 } else {
119 if (tap_count == 0) {
120 clear_oneshot_mods();
121 unregister_mods(mods);
122 }
123 else if (tap_count == 1) {
124 // Retain Oneshot mods
125 }
126 else {
127 clear_oneshot_mods();
128 unregister_mods(mods);
129 }
130 }
131 break;
132 #endif
133 case MODS_TAP_TOGGLE:
134 if (event.pressed) {
135 if (tap_count <= TAPPING_TOGGLE) {
136 if (mods & get_mods()) {
137 dprint("MODS_TAP_TOGGLE: toggle mods off\n");
138 unregister_mods(mods);
139 } else {
140 dprint("MODS_TAP_TOGGLE: toggle mods on\n");
141 register_mods(mods);
142 }
143 }
144 } else {
145 if (tap_count < TAPPING_TOGGLE) {
146 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
147 unregister_mods(mods);
148 }
149 }
150 break;
151 default:
152 if (event.pressed) {
153 if (tap_count > 0) {
154 if (record->tap.interrupted) {
155 dprint("MODS_TAP: Tap: Cancel: add_mods\n");
156 // ad hoc: set 0 to cancel tap
157 record->tap.count = 0;
158 register_mods(mods);
159 } else {
160 dprint("MODS_TAP: Tap: register_code\n");
161 register_code(action.key.code);
162 }
163 } else {
164 dprint("MODS_TAP: No tap: add_mods\n");
165 register_mods(mods);
166 }
167 } else {
168 if (tap_count > 0) {
169 dprint("MODS_TAP: Tap: unregister_code\n");
170 unregister_code(action.key.code);
171 } else {
172 dprint("MODS_TAP: No tap: add_mods\n");
173 unregister_mods(mods);
174 }
175 }
176 break;
177 }
178 }
179 break;
180 #endif
181 #ifdef EXTRAKEY_ENABLE
182 /* other HID usage */
183 case ACT_USAGE:
184 switch (action.usage.page) {
185 case PAGE_SYSTEM:
186 if (event.pressed) {
187 host_system_send(action.usage.code);
188 } else {
189 host_system_send(0);
190 }
191 break;
192 case PAGE_CONSUMER:
193 if (event.pressed) {
194 host_consumer_send(action.usage.code);
195 } else {
196 host_consumer_send(0);
197 }
198 break;
199 }
200 break;
201 #endif
202 #ifdef MOUSEKEY_ENABLE
203 /* Mouse key */
204 case ACT_MOUSEKEY:
205 if (event.pressed) {
206 mousekey_on(action.key.code);
207 mousekey_send();
208 } else {
209 mousekey_off(action.key.code);
210 mousekey_send();
211 }
212 break;
213 #endif
214 #ifndef NO_ACTION_LAYER
215 case ACT_LAYER:
216 if (action.layer_bitop.on == 0) {
217 /* Default Layer Bitwise Operation */
218 if (!event.pressed) {
219 uint8_t shift = action.layer_bitop.part*4;
220 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
221 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
222 switch (action.layer_bitop.op) {
223 case OP_BIT_AND: default_layer_and(bits | mask); break;
224 case OP_BIT_OR: default_layer_or(bits | mask); break;
225 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
226 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
227 }
228 }
229 } else {
230 /* Layer Bitwise Operation */
231 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
232 (action.layer_bitop.on & ON_RELEASE)) {
233 uint8_t shift = action.layer_bitop.part*4;
234 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
235 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
236 switch (action.layer_bitop.op) {
237 case OP_BIT_AND: layer_and(bits | mask); break;
238 case OP_BIT_OR: layer_or(bits | mask); break;
239 case OP_BIT_XOR: layer_xor(bits | mask); break;
240 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
241 }
242 }
243 }
244 break;
245 #ifndef NO_ACTION_TAPPING
246 case ACT_LAYER_TAP:
247 case ACT_LAYER_TAP_EXT:
248 switch (action.layer_tap.code) {
249 case 0xe0 ... 0xef:
250 /* layer On/Off with modifiers(left only) */
251 if (event.pressed) {
252 layer_on(action.layer_tap.val);
253 register_mods(action.layer_tap.code & 0x0f);
254 } else {
255 layer_off(action.layer_tap.val);
256 unregister_mods(action.layer_tap.code & 0x0f);
257 }
258 break;
259 case OP_TAP_TOGGLE:
260 /* tap toggle */
261 if (event.pressed) {
262 if (tap_count < TAPPING_TOGGLE) {
263 layer_invert(action.layer_tap.val);
264 }
265 } else {
266 if (tap_count <= TAPPING_TOGGLE) {
267 layer_invert(action.layer_tap.val);
268 }
269 }
270 break;
271 case OP_ON_OFF:
272 event.pressed ? layer_on(action.layer_tap.val) :
273 layer_off(action.layer_tap.val);
274 break;
275 case OP_OFF_ON:
276 event.pressed ? layer_off(action.layer_tap.val) :
277 layer_on(action.layer_tap.val);
278 break;
279 case OP_SET_CLEAR:
280 event.pressed ? layer_move(action.layer_tap.val) :
281 layer_clear();
282 break;
283 default:
284 /* tap key */
285 if (event.pressed) {
286 if (tap_count > 0) {
287 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
288 register_code(action.layer_tap.code);
289 } else {
290 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
291 layer_on(action.layer_tap.val);
292 }
293 } else {
294 if (tap_count > 0) {
295 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
296 unregister_code(action.layer_tap.code);
297 } else {
298 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
299 layer_off(action.layer_tap.val);
300 }
301 }
302 break;
303 }
304 break;
305 #endif
306 #endif
307 /* Extentions */
308 #ifndef NO_ACTION_MACRO
309 case ACT_MACRO:
310 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
311 break;
312 #endif
313 #ifdef BACKLIGHT_ENABLE
314 case ACT_BACKLIGHT:
315 if (!event.pressed) {
316 switch (action.backlight.opt) {
317 case BACKLIGHT_INCREASE:
318 backlight_increase();
319 break;
320 case BACKLIGHT_DECREASE:
321 backlight_decrease();
322 break;
323 case BACKLIGHT_TOGGLE:
324 backlight_toggle();
325 break;
326 case BACKLIGHT_STEP:
327 backlight_step();
328 break;
329 case BACKLIGHT_LEVEL:
330 backlight_level(action.backlight.level);
331 break;
332 }
333 }
334 break;
335 #endif
336 case ACT_COMMAND:
337 break;
338 #ifndef NO_ACTION_FUNCTION
339 case ACT_FUNCTION:
340 action_function(record, action.func.id, action.func.opt);
341 break;
342 #endif
343 default:
344 break;
345 }
346 }
347
348
349
350
351 /*
352 * Utilities for actions.
353 */
354 void register_code(uint8_t code)
355 {
356 if (code == KC_NO) {
357 return;
358 }
359
360 #ifdef LOCKING_SUPPORT_ENABLE
361 else if (KC_LOCKING_CAPS == code) {
362 #ifdef LOCKING_RESYNC_ENABLE
363 // Resync: ignore if caps lock already is on
364 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
365 #endif
366 add_key(KC_CAPSLOCK);
367 send_keyboard_report();
368 del_key(KC_CAPSLOCK);
369 send_keyboard_report();
370 }
371
372 else if (KC_LOCKING_NUM == code) {
373 #ifdef LOCKING_RESYNC_ENABLE
374 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
375 #endif
376 add_key(KC_NUMLOCK);
377 send_keyboard_report();
378 del_key(KC_NUMLOCK);
379 send_keyboard_report();
380 }
381
382 else if (KC_LOCKING_SCROLL == code) {
383 #ifdef LOCKING_RESYNC_ENABLE
384 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
385 #endif
386 add_key(KC_SCROLLLOCK);
387 send_keyboard_report();
388 del_key(KC_SCROLLLOCK);
389 send_keyboard_report();
390 }
391 #endif
392
393 else if IS_KEY(code) {
394 // TODO: should push command_proc out of this block?
395 if (command_proc(code)) return;
396
397 #ifndef NO_ACTION_ONESHOT
398 /* TODO: remove
399 if (oneshot_state.mods && !oneshot_state.disabled) {
400 uint8_t tmp_mods = get_mods();
401 add_mods(oneshot_state.mods);
402
403 add_key(code);
404 send_keyboard_report();
405
406 set_mods(tmp_mods);
407 send_keyboard_report();
408 oneshot_cancel();
409 } else
410 */
411 #endif
412 {
413 add_key(code);
414 send_keyboard_report();
415 }
416 }
417 else if IS_MOD(code) {
418 add_mods(MOD_BIT(code));
419 send_keyboard_report();
420 }
421 else if IS_SYSTEM(code) {
422 host_system_send(KEYCODE2SYSTEM(code));
423 }
424 else if IS_CONSUMER(code) {
425 host_consumer_send(KEYCODE2CONSUMER(code));
426 }
427 }
428
429 void unregister_code(uint8_t code)
430 {
431 if (code == KC_NO) {
432 return;
433 }
434
435 #ifdef LOCKING_SUPPORT_ENABLE
436 else if (KC_LOCKING_CAPS == code) {
437 #ifdef LOCKING_RESYNC_ENABLE
438 // Resync: ignore if caps lock already is off
439 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
440 #endif
441 add_key(KC_CAPSLOCK);
442 send_keyboard_report();
443 del_key(KC_CAPSLOCK);
444 send_keyboard_report();
445 }
446
447 else if (KC_LOCKING_NUM == code) {
448 #ifdef LOCKING_RESYNC_ENABLE
449 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
450 #endif
451 add_key(KC_NUMLOCK);
452 send_keyboard_report();
453 del_key(KC_NUMLOCK);
454 send_keyboard_report();
455 }
456
457 else if (KC_LOCKING_SCROLL == code) {
458 #ifdef LOCKING_RESYNC_ENABLE
459 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
460 #endif
461 add_key(KC_SCROLLLOCK);
462 send_keyboard_report();
463 del_key(KC_SCROLLLOCK);
464 send_keyboard_report();
465 }
466 #endif
467
468 else if IS_KEY(code) {
469 del_key(code);
470 send_keyboard_report();
471 }
472 else if IS_MOD(code) {
473 del_mods(MOD_BIT(code));
474 send_keyboard_report();
475 }
476 else if IS_SYSTEM(code) {
477 host_system_send(0);
478 }
479 else if IS_CONSUMER(code) {
480 host_consumer_send(0);
481 }
482 }
483
484 void register_mods(uint8_t mods)
485 {
486 if (mods) {
487 add_mods(mods);
488 send_keyboard_report();
489 }
490 }
491
492 void unregister_mods(uint8_t mods)
493 {
494 if (mods) {
495 del_mods(mods);
496 send_keyboard_report();
497 }
498 }
499
500 void clear_keyboard(void)
501 {
502 clear_mods();
503 clear_keyboard_but_mods();
504 }
505
506 void clear_keyboard_but_mods(void)
507 {
508 clear_weak_mods();
509 clear_keys();
510 send_keyboard_report();
511 #ifdef MOUSEKEY_ENABLE
512 mousekey_clear();
513 mousekey_send();
514 #endif
515 #ifdef EXTRAKEY_ENABLE
516 host_system_send(0);
517 host_consumer_send(0);
518 #endif
519 }
520
521 bool is_tap_key(keypos_t key)
522 {
523 action_t action = layer_switch_get_action(key);
524
525 switch (action.kind.id) {
526 case ACT_LMODS_TAP:
527 case ACT_RMODS_TAP:
528 case ACT_LAYER_TAP:
529 case ACT_LAYER_TAP_EXT:
530 switch (action.layer_tap.code) {
531 case 0x00 ... 0xdf:
532 case OP_TAP_TOGGLE:
533 return true;
534 }
535 return false;
536 case ACT_MACRO:
537 case ACT_FUNCTION:
538 if (action.func.opt & FUNC_TAP) { return true; }
539 return false;
540 }
541 return false;
542 }
543
544
545 /*
546 * debug print
547 */
548 void debug_event(keyevent_t event)
549 {
550 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
551 }
552
553 void debug_record(keyrecord_t record)
554 {
555 debug_event(record.event);
556 #ifndef NO_ACTION_TAPPING
557 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
558 #endif
559 }
560
561 void debug_action(action_t action)
562 {
563 switch (action.kind.id) {
564 case ACT_LMODS: dprint("ACT_LMODS"); break;
565 case ACT_RMODS: dprint("ACT_RMODS"); break;
566 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
567 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
568 case ACT_USAGE: dprint("ACT_USAGE"); break;
569 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
570 case ACT_LAYER: dprint("ACT_LAYER"); break;
571 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
572 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
573 case ACT_MACRO: dprint("ACT_MACRO"); break;
574 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
575 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
576 default: dprint("UNKNOWN"); break;
577 }
578 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
579 }
Imprint / Impressum