core: Modifiers can be used as tap key Fix #422
[tmk_keyboard.git] / tmk_core / common / action.c
CommitLineData
a074364c 1/*
2Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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"
71381457 29#include "hook.h"
88c95615 30#include "wait.h"
a074364c 31
32#ifdef DEBUG_ACTION
33#include "debug.h"
34#else
35#include "nodebug.h"
36#endif
37
38
39void action_exec(keyevent_t event)
40{
41 if (!IS_NOEVENT(event)) {
42 dprint("\n---- action_exec: start -----\n");
43 dprint("EVENT: "); debug_event(event); dprintln();
71381457 44 hook_matrix_change(event);
a074364c 45 }
46
47 keyrecord_t record = { .event = event };
48
49#ifndef NO_ACTION_TAPPING
50 action_tapping_process(record);
51#else
52 process_action(&record);
53 if (!IS_NOEVENT(record.event)) {
54 dprint("processed: "); debug_record(record); dprintln();
55 }
56#endif
57}
58
59void process_action(keyrecord_t *record)
60{
61 keyevent_t event = record->event;
62#ifndef NO_ACTION_TAPPING
63 uint8_t tap_count = record->tap.count;
64#endif
65
66 if (IS_NOEVENT(event)) { return; }
67
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();
73#endif
74 dprintln();
75
76 switch (action.kind.id) {
77 /* Key and Mods */
78 case ACT_LMODS:
79 case ACT_RMODS:
80 {
81 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
82 action.key.mods<<4;
83 if (event.pressed) {
84 if (mods) {
85 add_weak_mods(mods);
86 send_keyboard_report();
87 }
88 register_code(action.key.code);
89 } else {
90 unregister_code(action.key.code);
91 if (mods) {
92 del_weak_mods(mods);
93 send_keyboard_report();
94 }
95 }
96 }
97 break;
98#ifndef NO_ACTION_TAPPING
99 case ACT_LMODS_TAP:
100 case ACT_RMODS_TAP:
101 {
102 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
103 action.key.mods<<4;
104 switch (action.layer_tap.code) {
105 #ifndef NO_ACTION_ONESHOT
106 case MODS_ONESHOT:
107 // Oneshot modifier
108 if (event.pressed) {
109 if (tap_count == 0) {
110 register_mods(mods);
111 }
112 else if (tap_count == 1) {
113 dprint("MODS_TAP: Oneshot: start\n");
114 set_oneshot_mods(mods);
115 }
116 else {
117 register_mods(mods);
118 }
119 } else {
120 if (tap_count == 0) {
121 clear_oneshot_mods();
122 unregister_mods(mods);
123 }
124 else if (tap_count == 1) {
125 // Retain Oneshot mods
126 }
127 else {
128 clear_oneshot_mods();
129 unregister_mods(mods);
130 }
131 }
132 break;
133 #endif
134 case MODS_TAP_TOGGLE:
135 if (event.pressed) {
136 if (tap_count <= TAPPING_TOGGLE) {
28203e90 137 if (mods & get_mods()) {
138 dprint("MODS_TAP_TOGGLE: toggle mods off\n");
139 unregister_mods(mods);
140 } else {
141 dprint("MODS_TAP_TOGGLE: toggle mods on\n");
142 register_mods(mods);
143 }
a074364c 144 }
145 } else {
146 if (tap_count < TAPPING_TOGGLE) {
28203e90 147 dprint("MODS_TAP_TOGGLE: release : unregister_mods\n");
a074364c 148 unregister_mods(mods);
149 }
150 }
151 break;
152 default:
153 if (event.pressed) {
154 if (tap_count > 0) {
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;
159 register_mods(mods);
160 } else {
161 dprint("MODS_TAP: Tap: register_code\n");
162 register_code(action.key.code);
163 }
164 } else {
165 dprint("MODS_TAP: No tap: add_mods\n");
166 register_mods(mods);
167 }
168 } else {
169 if (tap_count > 0) {
170 dprint("MODS_TAP: Tap: unregister_code\n");
171 unregister_code(action.key.code);
172 } else {
173 dprint("MODS_TAP: No tap: add_mods\n");
174 unregister_mods(mods);
175 }
176 }
177 break;
178 }
179 }
180 break;
181#endif
182#ifdef EXTRAKEY_ENABLE
183 /* other HID usage */
184 case ACT_USAGE:
185 switch (action.usage.page) {
186 case PAGE_SYSTEM:
187 if (event.pressed) {
188 host_system_send(action.usage.code);
189 } else {
190 host_system_send(0);
191 }
192 break;
193 case PAGE_CONSUMER:
194 if (event.pressed) {
195 host_consumer_send(action.usage.code);
196 } else {
197 host_consumer_send(0);
198 }
199 break;
200 }
201 break;
202#endif
203#ifdef MOUSEKEY_ENABLE
204 /* Mouse key */
205 case ACT_MOUSEKEY:
206 if (event.pressed) {
207 mousekey_on(action.key.code);
208 mousekey_send();
209 } else {
210 mousekey_off(action.key.code);
211 mousekey_send();
212 }
213 break;
214#endif
215#ifndef NO_ACTION_LAYER
216 case ACT_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;
228 }
229 }
230 } else {
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;
242 }
243 }
244 }
245 break;
246 #ifndef NO_ACTION_TAPPING
247 case ACT_LAYER_TAP:
248 case ACT_LAYER_TAP_EXT:
249 switch (action.layer_tap.code) {
250 case 0xe0 ... 0xef:
251 /* layer On/Off with modifiers(left only) */
252 if (event.pressed) {
253 layer_on(action.layer_tap.val);
254 register_mods(action.layer_tap.code & 0x0f);
255 } else {
256 layer_off(action.layer_tap.val);
257 unregister_mods(action.layer_tap.code & 0x0f);
258 }
259 break;
260 case OP_TAP_TOGGLE:
261 /* tap toggle */
262 if (event.pressed) {
263 if (tap_count < TAPPING_TOGGLE) {
264 layer_invert(action.layer_tap.val);
265 }
266 } else {
267 if (tap_count <= TAPPING_TOGGLE) {
268 layer_invert(action.layer_tap.val);
269 }
270 }
271 break;
272 case OP_ON_OFF:
273 event.pressed ? layer_on(action.layer_tap.val) :
274 layer_off(action.layer_tap.val);
275 break;
276 case OP_OFF_ON:
277 event.pressed ? layer_off(action.layer_tap.val) :
278 layer_on(action.layer_tap.val);
279 break;
280 case OP_SET_CLEAR:
281 event.pressed ? layer_move(action.layer_tap.val) :
282 layer_clear();
283 break;
284 default:
285 /* tap key */
286 if (event.pressed) {
287 if (tap_count > 0) {
288 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
289 register_code(action.layer_tap.code);
290 } else {
291 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
292 layer_on(action.layer_tap.val);
293 }
294 } else {
295 if (tap_count > 0) {
296 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
297 unregister_code(action.layer_tap.code);
298 } else {
299 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
300 layer_off(action.layer_tap.val);
301 }
302 }
303 break;
304 }
305 break;
306 #endif
307#endif
308 /* Extentions */
309#ifndef NO_ACTION_MACRO
310 case ACT_MACRO:
311 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
312 break;
313#endif
314#ifdef BACKLIGHT_ENABLE
315 case ACT_BACKLIGHT:
316 if (!event.pressed) {
317 switch (action.backlight.opt) {
318 case BACKLIGHT_INCREASE:
319 backlight_increase();
320 break;
321 case BACKLIGHT_DECREASE:
322 backlight_decrease();
323 break;
324 case BACKLIGHT_TOGGLE:
325 backlight_toggle();
326 break;
327 case BACKLIGHT_STEP:
328 backlight_step();
329 break;
330 case BACKLIGHT_LEVEL:
331 backlight_level(action.backlight.level);
332 break;
333 }
334 }
335 break;
336#endif
337 case ACT_COMMAND:
338 break;
339#ifndef NO_ACTION_FUNCTION
340 case ACT_FUNCTION:
341 action_function(record, action.func.id, action.func.opt);
342 break;
343#endif
344 default:
345 break;
346 }
347}
348
349
350
351
352/*
353 * Utilities for actions.
354 */
355void register_code(uint8_t code)
356{
357 if (code == KC_NO) {
358 return;
359 }
360
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;
366#endif
367 add_key(KC_CAPSLOCK);
368 send_keyboard_report();
88c95615 369 wait_ms(100);
a074364c 370 del_key(KC_CAPSLOCK);
371 send_keyboard_report();
372 }
373
374 else if (KC_LOCKING_NUM == code) {
375#ifdef LOCKING_RESYNC_ENABLE
376 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
377#endif
378 add_key(KC_NUMLOCK);
379 send_keyboard_report();
88c95615 380 wait_ms(100);
a074364c 381 del_key(KC_NUMLOCK);
382 send_keyboard_report();
383 }
384
385 else if (KC_LOCKING_SCROLL == code) {
386#ifdef LOCKING_RESYNC_ENABLE
387 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
388#endif
389 add_key(KC_SCROLLLOCK);
390 send_keyboard_report();
88c95615 391 wait_ms(100);
a074364c 392 del_key(KC_SCROLLLOCK);
393 send_keyboard_report();
394 }
395#endif
396
397 else if IS_KEY(code) {
398 // TODO: should push command_proc out of this block?
399 if (command_proc(code)) return;
400
401#ifndef NO_ACTION_ONESHOT
402/* TODO: remove
403 if (oneshot_state.mods && !oneshot_state.disabled) {
404 uint8_t tmp_mods = get_mods();
405 add_mods(oneshot_state.mods);
406
407 add_key(code);
408 send_keyboard_report();
409
410 set_mods(tmp_mods);
411 send_keyboard_report();
412 oneshot_cancel();
413 } else
414*/
415#endif
416 {
417 add_key(code);
418 send_keyboard_report();
419 }
420 }
421 else if IS_MOD(code) {
422 add_mods(MOD_BIT(code));
423 send_keyboard_report();
424 }
425 else if IS_SYSTEM(code) {
426 host_system_send(KEYCODE2SYSTEM(code));
427 }
428 else if IS_CONSUMER(code) {
429 host_consumer_send(KEYCODE2CONSUMER(code));
430 }
431}
432
433void unregister_code(uint8_t code)
434{
435 if (code == KC_NO) {
436 return;
437 }
438
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;
444#endif
445 add_key(KC_CAPSLOCK);
446 send_keyboard_report();
88c95615 447 wait_ms(100);
a074364c 448 del_key(KC_CAPSLOCK);
449 send_keyboard_report();
450 }
451
452 else if (KC_LOCKING_NUM == code) {
453#ifdef LOCKING_RESYNC_ENABLE
454 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
455#endif
456 add_key(KC_NUMLOCK);
457 send_keyboard_report();
88c95615 458 wait_ms(100);
a074364c 459 del_key(KC_NUMLOCK);
460 send_keyboard_report();
461 }
462
463 else if (KC_LOCKING_SCROLL == code) {
464#ifdef LOCKING_RESYNC_ENABLE
465 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
466#endif
467 add_key(KC_SCROLLLOCK);
468 send_keyboard_report();
88c95615 469 wait_ms(100);
a074364c 470 del_key(KC_SCROLLLOCK);
471 send_keyboard_report();
472 }
473#endif
474
475 else if IS_KEY(code) {
476 del_key(code);
477 send_keyboard_report();
478 }
479 else if IS_MOD(code) {
480 del_mods(MOD_BIT(code));
481 send_keyboard_report();
482 }
483 else if IS_SYSTEM(code) {
484 host_system_send(0);
485 }
486 else if IS_CONSUMER(code) {
487 host_consumer_send(0);
488 }
489}
490
491void register_mods(uint8_t mods)
492{
493 if (mods) {
494 add_mods(mods);
495 send_keyboard_report();
496 }
497}
498
499void unregister_mods(uint8_t mods)
500{
501 if (mods) {
502 del_mods(mods);
503 send_keyboard_report();
504 }
505}
506
507void clear_keyboard(void)
508{
509 clear_mods();
510 clear_keyboard_but_mods();
511}
512
513void clear_keyboard_but_mods(void)
514{
515 clear_weak_mods();
516 clear_keys();
517 send_keyboard_report();
518#ifdef MOUSEKEY_ENABLE
519 mousekey_clear();
520 mousekey_send();
521#endif
522#ifdef EXTRAKEY_ENABLE
523 host_system_send(0);
524 host_consumer_send(0);
525#endif
526}
527
528bool is_tap_key(keypos_t key)
529{
530 action_t action = layer_switch_get_action(key);
531
532 switch (action.kind.id) {
533 case ACT_LMODS_TAP:
534 case ACT_RMODS_TAP:
535 case ACT_LAYER_TAP:
536 case ACT_LAYER_TAP_EXT:
4d116a04 537 switch (action.layer_tap.code) {
c98e89f2 538 case KC_A ... KC_EXSEL:
539 case KC_KP_00 ... KC_KP_HEXADECIMAL:
540 case KC_LCTRL ... KC_RGUI:
4d116a04 541 case OP_TAP_TOGGLE:
542 return true;
543 }
544 return false;
a074364c 545 case ACT_MACRO:
546 case ACT_FUNCTION:
547 if (action.func.opt & FUNC_TAP) { return true; }
548 return false;
549 }
550 return false;
551}
552
553
554/*
555 * debug print
556 */
557void debug_event(keyevent_t event)
558{
559 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
560}
561
562void debug_record(keyrecord_t record)
563{
564 debug_event(record.event);
565#ifndef NO_ACTION_TAPPING
566 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
567#endif
568}
569
570void debug_action(action_t action)
571{
572 switch (action.kind.id) {
573 case ACT_LMODS: dprint("ACT_LMODS"); break;
574 case ACT_RMODS: dprint("ACT_RMODS"); break;
575 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
576 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
577 case ACT_USAGE: dprint("ACT_USAGE"); break;
578 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
579 case ACT_LAYER: dprint("ACT_LAYER"); break;
580 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
581 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
582 case ACT_MACRO: dprint("ACT_MACRO"); break;
583 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
584 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
585 default: dprint("UNKNOWN"); break;
586 }
587 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
588}
Imprint / Impressum