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