]> git.gir.st - tmk_keyboard.git/blob - common/action.c
Merge pull request #81 from bgould/master
[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
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 add_weak_mods(mods);
83 send_keyboard_report();
84 }
85 register_code(action.key.code);
86 } else {
87 unregister_code(action.key.code);
88 if (mods) {
89 del_weak_mods(mods);
90 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 MODS_ONESHOT:
104 // Oneshot modifier
105 if (event.pressed) {
106 if (tap_count == 0) {
107 register_mods(mods);
108 }
109 else if (tap_count == 1) {
110 dprint("MODS_TAP: Oneshot: start\n");
111 set_oneshot_mods(mods);
112 }
113 else {
114 register_mods(mods);
115 }
116 } else {
117 if (tap_count == 0) {
118 clear_oneshot_mods();
119 unregister_mods(mods);
120 }
121 else if (tap_count == 1) {
122 // Retain Oneshot mods
123 }
124 else {
125 clear_oneshot_mods();
126 unregister_mods(mods);
127 }
128 }
129 break;
130 #endif
131 default:
132 if (event.pressed) {
133 if (tap_count > 0) {
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;
138 register_mods(mods);
139 } else {
140 dprint("MODS_TAP: Tap: register_code\n");
141 register_code(action.key.code);
142 }
143 } else {
144 dprint("MODS_TAP: No tap: add_mods\n");
145 register_mods(mods);
146 }
147 } else {
148 if (tap_count > 0) {
149 dprint("MODS_TAP: Tap: unregister_code\n");
150 unregister_code(action.key.code);
151 } else {
152 dprint("MODS_TAP: No tap: add_mods\n");
153 unregister_mods(mods);
154 }
155 }
156 break;
157 }
158 }
159 break;
160 #endif
161 #ifdef EXTRAKEY_ENABLE
162 /* other HID usage */
163 case ACT_USAGE:
164 switch (action.usage.page) {
165 case PAGE_SYSTEM:
166 if (event.pressed) {
167 host_system_send(action.usage.code);
168 } else {
169 host_system_send(0);
170 }
171 break;
172 case PAGE_CONSUMER:
173 if (event.pressed) {
174 host_consumer_send(action.usage.code);
175 } else {
176 host_consumer_send(0);
177 }
178 break;
179 }
180 break;
181 #endif
182 #ifdef MOUSEKEY_ENABLE
183 /* Mouse key */
184 case ACT_MOUSEKEY:
185 if (event.pressed) {
186 mousekey_on(action.key.code);
187 mousekey_send();
188 } else {
189 mousekey_off(action.key.code);
190 mousekey_send();
191 }
192 break;
193 #endif
194 #ifndef NO_ACTION_LAYER
195 case ACT_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;
207 }
208 }
209 } else {
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;
221 }
222 }
223 }
224 break;
225 #ifndef NO_ACTION_TAPPING
226 case ACT_LAYER_TAP:
227 case ACT_LAYER_TAP_EXT:
228 switch (action.layer_tap.code) {
229 case OP_TAP_TOGGLE:
230 /* tap toggle */
231 if (event.pressed) {
232 if (tap_count < TAPPING_TOGGLE) {
233 layer_invert(action.layer_tap.val);
234 }
235 } else {
236 if (tap_count <= TAPPING_TOGGLE) {
237 layer_invert(action.layer_tap.val);
238 }
239 }
240 break;
241 case OP_ON_OFF:
242 event.pressed ? layer_on(action.layer_tap.val) :
243 layer_off(action.layer_tap.val);
244 break;
245 case OP_OFF_ON:
246 event.pressed ? layer_off(action.layer_tap.val) :
247 layer_on(action.layer_tap.val);
248 break;
249 case OP_SET_CLEAR:
250 event.pressed ? layer_move(action.layer_tap.val) :
251 layer_clear();
252 break;
253 default:
254 /* tap key */
255 if (event.pressed) {
256 if (tap_count > 0) {
257 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
258 register_code(action.layer_tap.code);
259 } else {
260 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
261 layer_on(action.layer_tap.val);
262 }
263 } else {
264 if (tap_count > 0) {
265 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
266 unregister_code(action.layer_tap.code);
267 } else {
268 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
269 layer_off(action.layer_tap.val);
270 }
271 }
272 break;
273 }
274 break;
275 #endif
276 #endif
277 /* Extentions */
278 #ifndef NO_ACTION_MACRO
279 case ACT_MACRO:
280 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
281 break;
282 #endif
283 #ifdef BACKLIGHT_ENABLE
284 case ACT_BACKLIGHT:
285 if (!event.pressed) {
286 switch (action.backlight.id) {
287 case BACKLIGHT_INCREASE:
288 backlight_increase();
289 break;
290 case BACKLIGHT_DECREASE:
291 backlight_decrease();
292 break;
293 case BACKLIGHT_TOGGLE:
294 backlight_toggle();
295 break;
296 case BACKLIGHT_STEP:
297 backlight_step();
298 break;
299 }
300 }
301 break;
302 #endif
303 case ACT_COMMAND:
304 break;
305 #ifndef NO_ACTION_FUNCTION
306 case ACT_FUNCTION:
307 action_function(record, action.func.id, action.func.opt);
308 break;
309 #endif
310 default:
311 break;
312 }
313 }
314
315
316
317
318 /*
319 * Utilities for actions.
320 */
321 void register_code(uint8_t code)
322 {
323 if (code == KC_NO) {
324 return;
325 }
326
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;
332 #endif
333 add_key(KC_CAPSLOCK);
334 send_keyboard_report();
335 del_key(KC_CAPSLOCK);
336 send_keyboard_report();
337 }
338
339 else if (KC_LOCKING_NUM == code) {
340 #ifdef LOCKING_RESYNC_ENABLE
341 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
342 #endif
343 add_key(KC_NUMLOCK);
344 send_keyboard_report();
345 del_key(KC_NUMLOCK);
346 send_keyboard_report();
347 }
348
349 else if (KC_LOCKING_SCROLL == code) {
350 #ifdef LOCKING_RESYNC_ENABLE
351 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
352 #endif
353 add_key(KC_SCROLLLOCK);
354 send_keyboard_report();
355 del_key(KC_SCROLLLOCK);
356 send_keyboard_report();
357 }
358 #endif
359
360 else if IS_KEY(code) {
361 // TODO: should push command_proc out of this block?
362 if (command_proc(code)) return;
363
364 #ifndef NO_ACTION_ONESHOT
365 /* TODO: remove
366 if (oneshot_state.mods && !oneshot_state.disabled) {
367 uint8_t tmp_mods = get_mods();
368 add_mods(oneshot_state.mods);
369
370 add_key(code);
371 send_keyboard_report();
372
373 set_mods(tmp_mods);
374 send_keyboard_report();
375 oneshot_cancel();
376 } else
377 */
378 #endif
379 {
380 add_key(code);
381 send_keyboard_report();
382 }
383 }
384 else if IS_MOD(code) {
385 add_mods(MOD_BIT(code));
386 send_keyboard_report();
387 }
388 else if IS_SYSTEM(code) {
389 host_system_send(KEYCODE2SYSTEM(code));
390 }
391 else if IS_CONSUMER(code) {
392 host_consumer_send(KEYCODE2CONSUMER(code));
393 }
394 }
395
396 void unregister_code(uint8_t code)
397 {
398 if (code == KC_NO) {
399 return;
400 }
401
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;
407 #endif
408 add_key(KC_CAPSLOCK);
409 send_keyboard_report();
410 del_key(KC_CAPSLOCK);
411 send_keyboard_report();
412 }
413
414 else if (KC_LOCKING_NUM == code) {
415 #ifdef LOCKING_RESYNC_ENABLE
416 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
417 #endif
418 add_key(KC_NUMLOCK);
419 send_keyboard_report();
420 del_key(KC_NUMLOCK);
421 send_keyboard_report();
422 }
423
424 else if (KC_LOCKING_SCROLL == code) {
425 #ifdef LOCKING_RESYNC_ENABLE
426 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
427 #endif
428 add_key(KC_SCROLLLOCK);
429 send_keyboard_report();
430 del_key(KC_SCROLLLOCK);
431 send_keyboard_report();
432 }
433 #endif
434
435 else if IS_KEY(code) {
436 del_key(code);
437 send_keyboard_report();
438 }
439 else if IS_MOD(code) {
440 del_mods(MOD_BIT(code));
441 send_keyboard_report();
442 }
443 else if IS_SYSTEM(code) {
444 host_system_send(0);
445 }
446 else if IS_CONSUMER(code) {
447 host_consumer_send(0);
448 }
449 }
450
451 void register_mods(uint8_t mods)
452 {
453 if (mods) {
454 add_mods(mods);
455 send_keyboard_report();
456 }
457 }
458
459 void unregister_mods(uint8_t mods)
460 {
461 if (mods) {
462 del_mods(mods);
463 send_keyboard_report();
464 }
465 }
466
467 void clear_keyboard(void)
468 {
469 clear_mods();
470 clear_keyboard_but_mods();
471 }
472
473 void clear_keyboard_but_mods(void)
474 {
475 clear_weak_mods();
476 clear_keys();
477 send_keyboard_report();
478 #ifdef MOUSEKEY_ENABLE
479 mousekey_clear();
480 mousekey_send();
481 #endif
482 #ifdef EXTRAKEY_ENABLE
483 host_system_send(0);
484 host_consumer_send(0);
485 #endif
486 }
487
488 bool is_tap_key(key_t key)
489 {
490 action_t action = layer_switch_get_action(key);
491
492 switch (action.kind.id) {
493 case ACT_LMODS_TAP:
494 case ACT_RMODS_TAP:
495 case ACT_LAYER_TAP:
496 case ACT_LAYER_TAP_EXT:
497 return true;
498 case ACT_MACRO:
499 case ACT_FUNCTION:
500 if (action.func.opt & FUNC_TAP) { return true; }
501 return false;
502 }
503 return false;
504 }
505
506
507 /*
508 * debug print
509 */
510 void debug_event(keyevent_t event)
511 {
512 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
513 }
514
515 void debug_record(keyrecord_t record)
516 {
517 debug_event(record.event);
518 #ifndef NO_ACTION_TAPPING
519 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
520 #endif
521 }
522
523 void debug_action(action_t action)
524 {
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;
539 }
540 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
541 }
Imprint / Impressum