aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-02-16 04:05:58 +0900
committertmk <nobody@nowhere>2013-02-16 04:08:31 +0900
commite324fa29187dff7868d9d7fd378e0e46d77107a5 (patch)
treea72966995df47eef01ced9873c14f972de8af069
parent0142b571c46c7db314063615c956cbc5ec9720f1 (diff)
downloadqmk_firmware-e324fa29187dff7868d9d7fd378e0e46d77107a5.tar.gz
qmk_firmware-e324fa29187dff7868d9d7fd378e0e46d77107a5.zip
Rewrite layer action with layer_switch
-rw-r--r--common/action.c107
-rw-r--r--common/action.h35
-rw-r--r--common/command.c7
-rw-r--r--common/layer_switch.c66
-rw-r--r--common/layer_switch.h33
-rw-r--r--common/util.c11
-rw-r--r--common/util.h1
7 files changed, 148 insertions, 112 deletions
diff --git a/common/action.c b/common/action.c
index 4f0a5f906..246fd99d8 100644
--- a/common/action.c
+++ b/common/action.c
@@ -27,12 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27#include "layer_switch.h" 27#include "layer_switch.h"
28 28
29 29
30/* default layer indicates base layer */
31uint8_t default_layer = 0;
32/* current layer indicates active layer at this time */
33uint8_t current_layer = 0;
34
35
36static void process_action(keyrecord_t *record); 30static void process_action(keyrecord_t *record);
37static bool process_tapping(keyrecord_t *record); 31static bool process_tapping(keyrecord_t *record);
38static void waiting_buffer_scan_tap(void); 32static void waiting_buffer_scan_tap(void);
@@ -219,17 +213,8 @@ static action_t get_action(key_t key)
219 return action; 213 return action;
220 } 214 }
221 215
222 /* current layer: 0 means default layer */
223 if (current_layer) {
224 action = action_for_key(current_layer, key);
225 if (action.code != ACTION_TRANSPARENT) {
226 debug("current layer: used. "); debug_dec(current_layer); debug("\n");
227 return action;
228 }
229 }
230
231 /* default layer */ 216 /* default layer */
232 debug("default layer: used. \n"); 217 //debug("get_aciton: default layer: "); debug_dec(default_layer); debug("\n");
233 action = action_for_key(default_layer, key); 218 action = action_for_key(default_layer, key);
234 return action; 219 return action;
235} 220}
@@ -242,7 +227,8 @@ static void process_action(keyrecord_t *record)
242 if (IS_NOEVENT(event)) { return; } 227 if (IS_NOEVENT(event)) { return; }
243 228
244 action_t action = get_action(event.key); 229 action_t action = get_action(event.key);
245 debug("ACTION: "); debug_action(action); debug("\n"); 230 debug("ACTION: "); debug_action(action); debug(" ");
231 layer_switch_debug(); debug("["); debug_dec(default_layer); debug("]\n");
246 232
247 switch (action.kind.id) { 233 switch (action.kind.id) {
248 /* Key and Mods */ 234 /* Key and Mods */
@@ -383,57 +369,57 @@ static void process_action(keyrecord_t *record)
383 break; 369 break;
384 370
385 /* Layer key */ 371 /* Layer key */
386 case ACT_LAYER: 372 case ACT_LAYER_SET:
387 switch (action.layer.code) { 373 switch (action.layer.code) {
388 case LAYER_MOMENTARY: /* momentary */ 374 case LAYER_MOMENTARY: /* momentary */
389 if (event.pressed) { 375 if (event.pressed) {
390 layer_switch(action.layer.val); 376 layer_switch_move(action.layer.val);
391 } 377 }
392 else { 378 else {
393 // NOTE: This is needed by legacy keymap support 379 // NOTE: This is needed by legacy keymap support
394 layer_switch(0); 380 layer_switch_move(0);
395 } 381 }
396 break; 382 break;
397 case LAYER_ON_PRESS: 383 case LAYER_ON_PRESS:
398 if (event.pressed) { 384 if (event.pressed) {
399 layer_switch(action.layer.val); 385 layer_switch_move(action.layer.val);
400 } 386 }
401 break; 387 break;
402 case LAYER_ON_RELEASE: 388 case LAYER_ON_RELEASE:
403 if (!event.pressed) { 389 if (!event.pressed) {
404 layer_switch(action.layer.val); 390 layer_switch_move(action.layer.val);
405 } 391 }
406 break; 392 break;
407 case LAYER_ON_BOTH: 393 case LAYER_ON_BOTH:
408 layer_switch(action.layer.val); 394 layer_switch_move(action.layer.val);
409 break; 395 break;
410 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ 396 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
411 if (event.pressed) { 397 if (event.pressed) {
412 if (tap_count < TAPPING_TOGGLE) { 398 if (tap_count < TAPPING_TOGGLE) {
413 layer_switch(action.layer.val); 399 layer_switch_move(action.layer.val);
414 } 400 }
415 } else { 401 } else {
416 if (tap_count >= TAPPING_TOGGLE) { 402 if (tap_count >= TAPPING_TOGGLE) {
417 debug("LAYER_PRESSED: tap toggle.\n"); 403 debug("LAYER_PRESSED: tap toggle.\n");
418 layer_switch(action.layer.val); 404 layer_switch_move(action.layer.val);
419 } 405 }
420 } 406 }
421 break; 407 break;
422 case LAYER_SET_DEFAULT_ON_PRESS: 408 case LAYER_SET_DEFAULT_ON_PRESS:
423 if (event.pressed) { 409 if (event.pressed) {
424 default_layer = action.layer.val; 410 default_layer = action.layer.val;
425 layer_switch(0); 411 layer_switch_move(0);
426 } 412 }
427 break; 413 break;
428 case LAYER_SET_DEFAULT_ON_RELEASE: 414 case LAYER_SET_DEFAULT_ON_RELEASE:
429 if (!event.pressed) { 415 if (!event.pressed) {
430 default_layer = action.layer.val; 416 default_layer = action.layer.val;
431 layer_switch(0); 417 layer_switch_move(0);
432 } 418 }
433 break; 419 break;
434 case LAYER_SET_DEFAULT_ON_BOTH: 420 case LAYER_SET_DEFAULT_ON_BOTH:
435 default_layer = action.layer.val; 421 default_layer = action.layer.val;
436 layer_switch(0); 422 layer_switch_move(0);
437 break; 423 break;
438 default: 424 default:
439 /* tap key */ 425 /* tap key */
@@ -443,7 +429,7 @@ static void process_action(keyrecord_t *record)
443 register_code(action.layer.code); 429 register_code(action.layer.code);
444 } else { 430 } else {
445 debug("LAYER_SET: No tap: layer_set(on press)\n"); 431 debug("LAYER_SET: No tap: layer_set(on press)\n");
446 layer_switch(action.layer.val); 432 layer_switch_move(action.layer.val);
447 } 433 }
448 } else { 434 } else {
449 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 435 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
@@ -452,7 +438,7 @@ static void process_action(keyrecord_t *record)
452 } else { 438 } else {
453 // NOTE: This is needed by legacy keymap support 439 // NOTE: This is needed by legacy keymap support
454 debug("LAYER_SET: No tap: return to default layer(on release)\n"); 440 debug("LAYER_SET: No tap: return to default layer(on release)\n");
455 layer_switch(0); 441 layer_switch_move(0);
456 } 442 }
457 } 443 }
458 break; 444 break;
@@ -462,52 +448,52 @@ static void process_action(keyrecord_t *record)
462 switch (action.layer.code) { 448 switch (action.layer.code) {
463 case LAYER_MOMENTARY: /* momentary */ 449 case LAYER_MOMENTARY: /* momentary */
464 if (event.pressed) { 450 if (event.pressed) {
465 layer_switch(current_layer | action.layer.val); 451 layer_switch_move(layer_switch_get_layer() | action.layer.val);
466 } else { 452 } else {
467 layer_switch(current_layer & ~action.layer.val); 453 layer_switch_move(layer_switch_get_layer() & ~action.layer.val);
468 } 454 }
469 break; 455 break;
470 case LAYER_ON_PRESS: 456 case LAYER_ON_PRESS:
471 if (event.pressed) { 457 if (event.pressed) {
472 layer_switch(current_layer ^ action.layer.val); 458 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
473 } 459 }
474 break; 460 break;
475 case LAYER_ON_RELEASE: 461 case LAYER_ON_RELEASE:
476 if (!event.pressed) { 462 if (!event.pressed) {
477 layer_switch(current_layer ^ action.layer.val); 463 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
478 } 464 }
479 break; 465 break;
480 case LAYER_ON_BOTH: 466 case LAYER_ON_BOTH:
481 layer_switch(current_layer ^ action.layer.val); 467 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
482 break; 468 break;
483 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ 469 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
484 if (event.pressed) { 470 if (event.pressed) {
485 if (tap_count < TAPPING_TOGGLE) { 471 if (tap_count < TAPPING_TOGGLE) {
486 debug("LAYER_BIT: tap toggle(press).\n"); 472 debug("LAYER_BIT: tap toggle(press).\n");
487 layer_switch(current_layer ^ action.layer.val); 473 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
488 } 474 }
489 } else { 475 } else {
490 if (tap_count <= TAPPING_TOGGLE) { 476 if (tap_count <= TAPPING_TOGGLE) {
491 debug("LAYER_BIT: tap toggle(release).\n"); 477 debug("LAYER_BIT: tap toggle(release).\n");
492 layer_switch(current_layer ^ action.layer.val); 478 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
493 } 479 }
494 } 480 }
495 break; 481 break;
496 case LAYER_SET_DEFAULT_ON_PRESS: 482 case LAYER_SET_DEFAULT_ON_PRESS:
497 if (event.pressed) { 483 if (event.pressed) {
498 default_layer = default_layer ^ action.layer.val; 484 default_layer = default_layer ^ action.layer.val;
499 layer_switch(0); 485 layer_switch_move(default_layer);
500 } 486 }
501 break; 487 break;
502 case LAYER_SET_DEFAULT_ON_RELEASE: 488 case LAYER_SET_DEFAULT_ON_RELEASE:
503 if (!event.pressed) { 489 if (!event.pressed) {
504 default_layer = default_layer ^ action.layer.val; 490 default_layer = default_layer ^ action.layer.val;
505 layer_switch(0); 491 layer_switch_move(default_layer);
506 } 492 }
507 break; 493 break;
508 case LAYER_SET_DEFAULT_ON_BOTH: 494 case LAYER_SET_DEFAULT_ON_BOTH:
509 default_layer = default_layer ^ action.layer.val; 495 default_layer = default_layer ^ action.layer.val;
510 layer_switch(0); 496 layer_switch_move(default_layer);
511 break; 497 break;
512 default: 498 default:
513 // tap key 499 // tap key
@@ -517,7 +503,7 @@ static void process_action(keyrecord_t *record)
517 register_code(action.layer.code); 503 register_code(action.layer.code);
518 } else { 504 } else {
519 debug("LAYER_BIT: No tap: layer_bit(on press)\n"); 505 debug("LAYER_BIT: No tap: layer_bit(on press)\n");
520 layer_switch(current_layer ^ action.layer.val); 506 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
521 } 507 }
522 } else { 508 } else {
523 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 509 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
@@ -525,7 +511,7 @@ static void process_action(keyrecord_t *record)
525 unregister_code(action.layer.code); 511 unregister_code(action.layer.code);
526 } else { 512 } else {
527 debug("LAYER_BIT: No tap: layer_bit(on release)\n"); 513 debug("LAYER_BIT: No tap: layer_bit(on release)\n");
528 layer_switch(current_layer ^ action.layer.val); 514 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
529 } 515 }
530 } 516 }
531 break; 517 break;
@@ -542,27 +528,27 @@ static void process_action(keyrecord_t *record)
542 break; 528 break;
543 case LAYER_ON_PRESS: 529 case LAYER_ON_PRESS:
544 if (event.pressed) { 530 if (event.pressed) {
545 layer_switch_inv(action.layer.val); 531 layer_switch_invert(action.layer.val);
546 } 532 }
547 break; 533 break;
548 case LAYER_ON_RELEASE: 534 case LAYER_ON_RELEASE:
549 if (!event.pressed) { 535 if (!event.pressed) {
550 layer_switch_inv(action.layer.val); 536 layer_switch_invert(action.layer.val);
551 } 537 }
552 break; 538 break;
553 case LAYER_ON_BOTH: 539 case LAYER_ON_BOTH:
554 layer_switch_inv(action.layer.val); 540 layer_switch_invert(action.layer.val);
555 break; 541 break;
556 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ 542 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
557 if (event.pressed) { 543 if (event.pressed) {
558 if (tap_count < TAPPING_TOGGLE) { 544 if (tap_count < TAPPING_TOGGLE) {
559 debug("LAYER_SWITCH: tap toggle(press).\n"); 545 debug("LAYER_SWITCH: tap toggle(press).\n");
560 layer_switch_inv(action.layer.val); 546 layer_switch_invert(action.layer.val);
561 } 547 }
562 } else { 548 } else {
563 if (tap_count <= TAPPING_TOGGLE) { 549 if (tap_count <= TAPPING_TOGGLE) {
564 debug("LAYER_SWITCH: tap toggle(release).\n"); 550 debug("LAYER_SWITCH: tap toggle(release).\n");
565 layer_switch_inv(action.layer.val); 551 layer_switch_invert(action.layer.val);
566 } 552 }
567 } 553 }
568 break; 554 break;
@@ -573,16 +559,16 @@ static void process_action(keyrecord_t *record)
573 debug("LAYER_SWITCH: Tap: register_code\n"); 559 debug("LAYER_SWITCH: Tap: register_code\n");
574 register_code(action.layer.code); 560 register_code(action.layer.code);
575 } else { 561 } else {
576 debug("LAYER_SWITCH: No tap: layer_switch(on press)\n"); 562 debug("LAYER_SWITCH: No tap: layer_switch on press\n");
577 layer_switch_inv(action.layer.val); 563 layer_switch_invert(action.layer.val);
578 } 564 }
579 } else { 565 } else {
580 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 566 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
581 debug("LAYER_SWITCH: Tap: unregister_code\n"); 567 debug("LAYER_SWITCH: Tap: unregister_code\n");
582 unregister_code(action.layer.code); 568 unregister_code(action.layer.code);
583 } else { 569 } else {
584 debug("LAYER_SWITCH: No tap: layer_switch(on release)\n"); 570 debug("LAYER_SWITCH: No tap: layer_switch on release\n");
585 layer_switch_inv(action.layer.val); 571 layer_switch_invert(action.layer.val);
586 } 572 }
587 } 573 }
588 break; 574 break;
@@ -889,19 +875,6 @@ bool sending_anykey(void)
889 host_last_sysytem_report() || host_last_consumer_report()); 875 host_last_sysytem_report() || host_last_consumer_report());
890} 876}
891 877
892// TODO: rename or reinpl with new layer_switch.c
893void layer_switch(uint8_t new_layer)
894{
895 if (current_layer != new_layer) {
896 debug("Layer Switch: "); debug_hex(current_layer);
897 debug(" -> "); debug_hex(new_layer); debug("\n");
898
899 current_layer = new_layer;
900 clear_keyboard_but_mods(); // To avoid stuck keys
901 // NOTE: update mods with full scan of matrix? if modifier changes between layers
902 }
903}
904
905bool is_tap_key(key_t key) 878bool is_tap_key(key_t key)
906{ 879{
907 action_t action = get_action(key); 880 action_t action = get_action(key);
@@ -910,7 +883,7 @@ bool is_tap_key(key_t key)
910 case ACT_LMODS_TAP: 883 case ACT_LMODS_TAP:
911 case ACT_RMODS_TAP: 884 case ACT_RMODS_TAP:
912 return true; 885 return true;
913 case ACT_LAYER: 886 case ACT_LAYER_SET:
914 case ACT_LAYER_BIT: 887 case ACT_LAYER_BIT:
915 switch (action.layer.code) { 888 switch (action.layer.code) {
916 case LAYER_MOMENTARY: 889 case LAYER_MOMENTARY:
@@ -956,9 +929,9 @@ static void debug_action(action_t action)
956 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; 929 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
957 case ACT_USAGE: debug("ACT_USAGE"); break; 930 case ACT_USAGE: debug("ACT_USAGE"); break;
958 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; 931 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
959 case ACT_LAYER: debug("ACT_LAYER"); break; 932 case ACT_LAYER_SET: debug("ACT_LAYER_SET"); break;
960 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; 933 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
961 case ACT_LAYER_SWITCH: debug("ACT_LAYER_SWITCH"); break; 934 case ACT_LAYER_SWITCH: debug("ACT_LAYER_SWITCH"); break;
962 case ACT_MACRO: debug("ACT_MACRO"); break; 935 case ACT_MACRO: debug("ACT_MACRO"); break;
963 case ACT_COMMAND: debug("ACT_COMMAND"); break; 936 case ACT_COMMAND: debug("ACT_COMMAND"); break;
964 case ACT_FUNCTION: debug("ACT_FUNCTION"); break; 937 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
diff --git a/common/action.h b/common/action.h
index 1d00e02d5..46ae809cb 100644
--- a/common/action.h
+++ b/common/action.h
@@ -76,11 +76,6 @@ typedef union {
76 76
77 77
78 78
79/* layer used currently */
80extern uint8_t current_layer;
81/* layer to return or start with */
82extern uint8_t default_layer;
83
84/* Execute action per keyevent */ 79/* Execute action per keyevent */
85void action_exec(keyevent_t event); 80void action_exec(keyevent_t event);
86 81
@@ -155,14 +150,14 @@ bool waiting_buffer_has_anykey_pressed(void);
155 * 150 *
156 * Mouse Keys 151 * Mouse Keys
157 * ---------- 152 * ----------
158 * TODO: can be combined with 'Other HID Usage'? to save action kind id. 153 * NOTE: can be combined with 'Other HID Usage'? to save action kind id.
159 * ACT_MOUSEKEY(0110): 154 * ACT_MOUSEKEY(0110):
160 * 0101|XXXX| keycode Mouse key 155 * 0101|XXXX| keycode Mouse key
161 * 156 *
162 * 157 *
163 * Layer Actions 158 * Layer Actions
164 * ------------- 159 * -------------
165 * ACT_LAYER(1000): Set layer 160 * ACT_LAYER_SET(1000): Set layer
166 * 1000|LLLL|0000 0000 set current layer on press and return to default on release(momentary) 161 * 1000|LLLL|0000 0000 set current layer on press and return to default on release(momentary)
167 * 1000|LLLL|0000 0001 set current layer on press 162 * 1000|LLLL|0000 0001 set current layer on press
168 * 1000|LLLL|0000 0010 set current layer on release 163 * 1000|LLLL|0000 0010 set current layer on release
@@ -216,7 +211,7 @@ enum action_kind_id {
216 ACT_USAGE = 0b0100, 211 ACT_USAGE = 0b0100,
217 ACT_MOUSEKEY = 0b0101, 212 ACT_MOUSEKEY = 0b0101,
218 213
219 ACT_LAYER = 0b1000, 214 ACT_LAYER_SET = 0b1000,
220 ACT_LAYER_BIT = 0b1001, 215 ACT_LAYER_BIT = 0b1001,
221 ACT_LAYER_SWITCH = 0b1011, 216 ACT_LAYER_SWITCH = 0b1011,
222 217
@@ -277,14 +272,14 @@ enum layer_codes {
277 */ 272 */
278/* set default layer */ 273/* set default layer */
279#define ACTION_LAYER_SET_DEFAULT(layer) ACTION_LAYER_SET_DEFAULT_R(layer) 274#define ACTION_LAYER_SET_DEFAULT(layer) ACTION_LAYER_SET_DEFAULT_R(layer)
280#define ACTION_LAYER_SET_DEFAULT_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_PRESS) 275#define ACTION_LAYER_SET_DEFAULT_P(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
281#define ACTION_LAYER_SET_DEFAULT_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_RELEASE) 276#define ACTION_LAYER_SET_DEFAULT_R(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
282#define ACTION_LAYER_SET_DEFAULT_B(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_BOTH) 277#define ACTION_LAYER_SET_DEFAULT_B(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
283/* bit-xor default layer */ 278/* bit-xor default layer */
284#define ACTION_LAYER_BIT_DEFAULT(bits) ACTION_LAYER_BIT_DEFAULT_R(bits) 279#define ACTION_LAYER_BIT_DEFAULT(bits) ACTION_LAYER_BIT_DEFAULT_R(bits)
285#define ACTION_LAYER_BIT_DEFAULT_P(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_PRESS) 280#define ACTION_LAYER_BIT_DEFAULT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
286#define ACTION_LAYER_BIT_DEFAULT_R(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_RELEASE) 281#define ACTION_LAYER_BIT_DEFAULT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
287#define ACTION_LAYER_BIT_DEFAULT_B(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_BOTH) 282#define ACTION_LAYER_BIT_DEFAULT_B(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
288/* 283/*
289 * Current layer: Return to default layer 284 * Current layer: Return to default layer
290 */ 285 */
@@ -296,13 +291,13 @@ enum layer_codes {
296 * Current layer: Set 291 * Current layer: Set
297 */ 292 */
298#define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer) 293#define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer)
299#define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY) 294#define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_MOMENTARY)
300#define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer) 295#define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer)
301#define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS) 296#define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_ON_PRESS)
302#define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE) 297#define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_ON_RELEASE)
303#define ACTION_LAYER_SET_B(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_BOTH) 298#define ACTION_LAYER_SET_B(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_ON_BOTH)
304#define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE) 299#define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_TAP_TOGGLE)
305#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key)) 300#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_SET, (layer)<<8 | (key))
306/* 301/*
307 * Current layer: Bit-op 302 * Current layer: Bit-op
308 */ 303 */
diff --git a/common/command.c b/common/command.c
index 82f647c8f..2d01c95e6 100644
--- a/common/command.c
+++ b/common/command.c
@@ -543,12 +543,9 @@ static uint8_t numkey2num(uint8_t code)
543 543
544static void switch_default_layer(uint8_t layer) 544static void switch_default_layer(uint8_t layer)
545{ 545{
546 print_val_hex8(current_layer); 546 // TODO check existence of layer or whether it can be used as default layer
547 print_val_hex8(default_layer); 547 print("switch_default_layer: "); print_dec(default_layer); print(" to "); print_dec(layer);
548 print("switch to "); print_val_hex8(layer);
549
550 default_layer = layer; 548 default_layer = layer;
551 current_layer = 0; /* 0 means default_layer */
552 layer_switch_clear(); 549 layer_switch_clear();
553 clear_keyboard(); 550 clear_keyboard();
554} 551}
diff --git a/common/layer_switch.c b/common/layer_switch.c
index 9bc804e64..22bfb34f6 100644
--- a/common/layer_switch.c
+++ b/common/layer_switch.c
@@ -2,50 +2,88 @@
2#include "keyboard.h" 2#include "keyboard.h"
3#include "action.h" 3#include "action.h"
4#include "debug.h" 4#include "debug.h"
5#include "util.h"
5#include "layer_switch.h" 6#include "layer_switch.h"
6 7
7 8
9uint8_t default_layer = 0;
10
8uint16_t layer_switch_stat = 0; 11uint16_t layer_switch_stat = 0;
9 12
10 13
11uint16_t layer_switch_stat_get(void) 14uint16_t layer_switch_get_stat(void)
12{ 15{
13 return layer_switch_stat; 16 return layer_switch_stat;
14} 17}
15 18
16void layer_switch_stat_set(uint16_t stat) 19/* return highest layer whose state is on */
20uint8_t layer_switch_get_layer(void)
21{
22 return biton16(layer_switch_stat);
23}
24
25static inline void stat_set(uint16_t stat)
17{ 26{
27 debug("layer_switch: ");
28 layer_switch_debug(); debug(" to ");
29
18 layer_switch_stat = stat; 30 layer_switch_stat = stat;
19 layer_switch_debug(); 31
32 layer_switch_debug(); debug("\n");
33
34 clear_keyboard_but_mods(); // To avoid stuck keys
20} 35}
21 36
22void layer_switch_clear(void) 37void layer_switch_clear(void)
23{ 38{
24 layer_switch_stat = 0; 39 stat_set(0);
25 layer_switch_debug(); 40}
41
42
43void layer_switch_set(uint16_t stat)
44{
45 stat_set(stat);
46}
47
48void layer_switch_move(uint8_t layer)
49{
50 if (layer)
51 stat_set(1<<layer);
52 else
53 stat_set(0); // fall back to default layer
26} 54}
27 55
28void layer_switch_on(uint8_t layer) 56void layer_switch_on(uint8_t layer)
29{ 57{
30 layer_switch_stat |= (1<<layer); 58 stat_set(layer_switch_stat | (1<<layer));
31 layer_switch_debug();
32} 59}
33 60
34void layer_switch_off(uint8_t layer) 61void layer_switch_off(uint8_t layer)
35{ 62{
36 layer_switch_stat &= ~(1<<layer); 63 stat_set(layer_switch_stat & ~(1<<layer));
37 layer_switch_debug(); 64}
65
66void layer_switch_invert(uint8_t layer)
67{
68 stat_set(layer_switch_stat ^ (1<<layer));
38} 69}
39 70
40void layer_switch_inv(uint8_t layer) 71void layer_switch_or(uint16_t stat)
72{
73 stat_set(layer_switch_stat | stat);
74}
75void layer_switch_and(uint16_t stat)
76{
77 stat_set(layer_switch_stat & stat);
78}
79void layer_switch_xor(uint16_t stat)
41{ 80{
42 layer_switch_stat ^= (1<<layer); 81 stat_set(layer_switch_stat ^ stat);
43 layer_switch_debug();
44} 82}
45 83
46void layer_switch_debug(void) 84void layer_switch_debug(void)
47{ 85{
48 debug("layer_switch_stat: "); debug_bin16(layer_switch_stat); debug("\n"); 86 debug_hex16(layer_switch_stat); debug("("); debug_dec(layer_switch_get_layer()); debug(")");
49} 87}
50 88
51action_t layer_switch_get_action(key_t key) 89action_t layer_switch_get_action(key_t key)
@@ -58,8 +96,6 @@ action_t layer_switch_get_action(key_t key)
58 if (layer_switch_stat & (1<<i)) { 96 if (layer_switch_stat & (1<<i)) {
59 action = action_for_key(i, key); 97 action = action_for_key(i, key);
60 if (action.code != ACTION_TRANSPARENT) { 98 if (action.code != ACTION_TRANSPARENT) {
61 layer_switch_debug();
62 debug("layer_switch: used. "); debug_dec(i); debug("\n");
63 return action; 99 return action;
64 } 100 }
65 } 101 }
diff --git a/common/layer_switch.h b/common/layer_switch.h
index 9f0695260..25c81a5dc 100644
--- a/common/layer_switch.h
+++ b/common/layer_switch.h
@@ -21,16 +21,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
21#include "keyboard.h" 21#include "keyboard.h"
22#include "action.h" 22#include "action.h"
23 23
24uint16_t layer_switch_stat;
25 24
26uint16_t layer_switch_stat_get(void); 25/* base layer to fall back */
27void layer_switch_stat_set(uint16_t stat); 26extern uint8_t default_layer;
27
28/* layer status */
29extern uint16_t layer_switch_stat;
30
31/* return layer status */
32uint16_t layer_switch_get_stat(void);
33/* return current active layer */
34uint8_t layer_switch_get_layer(void);
35
36/* switch off all layers */
28void layer_switch_clear(void); 37void layer_switch_clear(void);
38/* set layer status */
39void layer_switch_set(uint16_t stat);
40/* move to layer */
41void layer_switch_move(uint8_t layer);
42/* switch on layer */
29void layer_switch_on(uint8_t layer); 43void layer_switch_on(uint8_t layer);
44/* switch off layer */
30void layer_switch_off(uint8_t layer); 45void layer_switch_off(uint8_t layer);
31/* invert state */ 46/* switch state of layer */
32void layer_switch_inv(uint8_t layer); 47void layer_switch_invert(uint8_t layer);
48
49/* bitwise operation against layer status */
50void layer_switch_or(uint16_t stat);
51void layer_switch_and(uint16_t stat);
52void layer_switch_xor(uint16_t stat);
53
33void layer_switch_debug(void); 54void layer_switch_debug(void);
55
56/* return action depending on current layer status */
34action_t layer_switch_get_action(key_t key); 57action_t layer_switch_get_action(key_t key);
35 58
36#endif 59#endif
diff --git a/common/util.c b/common/util.c
index 9d8fb9321..ff1926d7d 100644
--- a/common/util.c
+++ b/common/util.c
@@ -39,6 +39,7 @@ uint8_t bitpop16(uint16_t bits)
39} 39}
40 40
41// most significant on-bit - return highest location of on-bit 41// most significant on-bit - return highest location of on-bit
42// NOTE: return 0 when bit0 is on or all bits are off
42uint8_t biton(uint8_t bits) 43uint8_t biton(uint8_t bits)
43{ 44{
44 uint8_t n = 0; 45 uint8_t n = 0;
@@ -47,3 +48,13 @@ uint8_t biton(uint8_t bits)
47 if (bits >> 1) { bits >>= 1; n += 1;} 48 if (bits >> 1) { bits >>= 1; n += 1;}
48 return n; 49 return n;
49} 50}
51
52uint8_t biton16(uint16_t bits)
53{
54 uint8_t n = 0;
55 if (bits >> 8) { bits >>= 8; n += 8;}
56 if (bits >> 4) { bits >>= 4; n += 4;}
57 if (bits >> 2) { bits >>= 2; n += 2;}
58 if (bits >> 1) { bits >>= 1; n += 1;}
59 return n;
60}
diff --git a/common/util.h b/common/util.h
index c3734487f..58b7fdf14 100644
--- a/common/util.h
+++ b/common/util.h
@@ -31,5 +31,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
31uint8_t bitpop(uint8_t bits); 31uint8_t bitpop(uint8_t bits);
32uint8_t bitpop16(uint16_t bits); 32uint8_t bitpop16(uint16_t bits);
33uint8_t biton(uint8_t bits); 33uint8_t biton(uint8_t bits);
34uint8_t biton16(uint16_t bits);
34 35
35#endif 36#endif