diff options
| author | tmk <nobody@nowhere> | 2013-03-05 15:45:15 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2013-03-05 15:45:15 +0900 |
| commit | 38bbe976e00a9a7bf6f8157016717e80503bf6a9 (patch) | |
| tree | 9113cd57c1bff3deef394ee966e2fdac8731a244 /common | |
| parent | 1720cf34caa518a2cf85f286d1ca077ebe1a1451 (diff) | |
| parent | 5808317b694004c43a6e0f76e9715415cce19a25 (diff) | |
| download | qmk_firmware-38bbe976e00a9a7bf6f8157016717e80503bf6a9.tar.gz qmk_firmware-38bbe976e00a9a7bf6f8157016717e80503bf6a9.zip | |
Merge branch 'overlays'
Diffstat (limited to 'common')
| -rw-r--r-- | common/action.c | 443 | ||||
| -rw-r--r-- | common/action.h | 251 | ||||
| -rw-r--r-- | common/action_macro.c | 1 | ||||
| -rw-r--r-- | common/action_macro.h | 4 | ||||
| -rw-r--r-- | common/command.c | 29 | ||||
| -rw-r--r-- | common/keymap.c | 91 | ||||
| -rw-r--r-- | common/keymap.h | 17 | ||||
| -rw-r--r-- | common/layer_switch.c | 201 | ||||
| -rw-r--r-- | common/layer_switch.h | 80 | ||||
| -rw-r--r-- | common/util.c | 11 | ||||
| -rw-r--r-- | common/util.h | 1 |
11 files changed, 811 insertions, 318 deletions
diff --git a/common/action.c b/common/action.c index 6528cd46c..15e125a3e 100644 --- a/common/action.c +++ b/common/action.c | |||
| @@ -23,15 +23,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 23 | #include "command.h" | 23 | #include "command.h" |
| 24 | #include "util.h" | 24 | #include "util.h" |
| 25 | #include "debug.h" | 25 | #include "debug.h" |
| 26 | #include "layer_switch.h" | ||
| 27 | #include "action_macro.h" | ||
| 26 | #include "action.h" | 28 | #include "action.h" |
| 27 | 29 | ||
| 28 | 30 | ||
| 29 | /* default layer indicates base layer */ | ||
| 30 | uint8_t default_layer = 0; | ||
| 31 | /* current layer indicates active layer at this time */ | ||
| 32 | uint8_t current_layer = 0; | ||
| 33 | |||
| 34 | |||
| 35 | static void process_action(keyrecord_t *record); | 31 | static void process_action(keyrecord_t *record); |
| 36 | static bool process_tapping(keyrecord_t *record); | 32 | static bool process_tapping(keyrecord_t *record); |
| 37 | static void waiting_buffer_scan_tap(void); | 33 | static void waiting_buffer_scan_tap(void); |
| @@ -207,28 +203,18 @@ void action_exec(keyevent_t event) | |||
| 207 | } | 203 | } |
| 208 | } | 204 | } |
| 209 | 205 | ||
| 210 | static action_t get_action(key_t key) | ||
| 211 | { | ||
| 212 | action_t action = action_for_key(current_layer, key); | ||
| 213 | |||
| 214 | /* Transparently use default layer */ | ||
| 215 | if (action.code == ACTION_TRANSPARENT) { | ||
| 216 | // TODO: layer stacking | ||
| 217 | action = action_for_key(default_layer, key); | ||
| 218 | debug("TRNASPARENT: "); debug_hex16(action.code); debug("\n"); | ||
| 219 | } | ||
| 220 | return action; | ||
| 221 | } | ||
| 222 | |||
| 223 | static void process_action(keyrecord_t *record) | 206 | static void process_action(keyrecord_t *record) |
| 224 | { | 207 | { |
| 225 | keyevent_t event = record->event; | 208 | keyevent_t event = record->event; |
| 226 | uint8_t tap_count = record->tap_count; | 209 | uint8_t tap_count = record->tap.count; |
| 227 | 210 | ||
| 228 | if (IS_NOEVENT(event)) { return; } | 211 | if (IS_NOEVENT(event)) { return; } |
| 229 | 212 | ||
| 230 | action_t action = get_action(event.key); | 213 | action_t action = layer_switch_get_action(event.key); |
| 231 | debug("ACTION: "); debug_action(action); debug("\n"); | 214 | debug("ACTION: "); debug_action(action); |
| 215 | debug(" overlays: "); overlay_debug(); | ||
| 216 | debug(" keymaps: "); keymap_debug(); | ||
| 217 | debug(" default_layer: "); debug_dec(default_layer); debug("\n"); | ||
| 232 | 218 | ||
| 233 | switch (action.kind.id) { | 219 | switch (action.kind.id) { |
| 234 | /* Key and Mods */ | 220 | /* Key and Mods */ |
| @@ -287,7 +273,7 @@ static void process_action(keyrecord_t *record) | |||
| 287 | } else { | 273 | } else { |
| 288 | if (tap_count == 0) { | 274 | if (tap_count == 0) { |
| 289 | debug("MODS_TAP: Oneshot: cancel/del_mods\n"); | 275 | debug("MODS_TAP: Oneshot: cancel/del_mods\n"); |
| 290 | // cancel oneshot by holding. | 276 | // cancel oneshot on hold |
| 291 | oneshot_cancel(); | 277 | oneshot_cancel(); |
| 292 | del_mods(mods); | 278 | del_mods(mods); |
| 293 | } | 279 | } |
| @@ -309,7 +295,7 @@ static void process_action(keyrecord_t *record) | |||
| 309 | if (waiting_buffer_has_anykey_pressed()) { | 295 | if (waiting_buffer_has_anykey_pressed()) { |
| 310 | debug("MODS_TAP: Tap: Cancel: add_mods\n"); | 296 | debug("MODS_TAP: Tap: Cancel: add_mods\n"); |
| 311 | // ad hoc: set 0 to cancel tap | 297 | // ad hoc: set 0 to cancel tap |
| 312 | record->tap_count = 0; | 298 | record->tap.count = 0; |
| 313 | add_mods(mods); | 299 | add_mods(mods); |
| 314 | } else { | 300 | } else { |
| 315 | debug("MODS_TAP: Tap: register_code\n"); | 301 | debug("MODS_TAP: Tap: register_code\n"); |
| @@ -368,145 +354,316 @@ static void process_action(keyrecord_t *record) | |||
| 368 | #endif | 354 | #endif |
| 369 | break; | 355 | break; |
| 370 | 356 | ||
| 371 | /* Layer key */ | 357 | case ACT_KEYMAP: |
| 372 | case ACT_LAYER: | ||
| 373 | switch (action.layer.code) { | 358 | switch (action.layer.code) { |
| 374 | case LAYER_MOMENTARY: /* momentary */ | 359 | /* Keymap clear */ |
| 375 | if (event.pressed) { | 360 | case OP_RESET: |
| 376 | layer_switch(action.layer.val); | 361 | switch (action.layer.val & 0x03) { |
| 377 | } | 362 | case 0: |
| 378 | else { | 363 | // NOTE: reserved |
| 379 | // NOTE: This is needed by legacy keymap support | 364 | overlay_clear(); |
| 380 | layer_switch(default_layer); | 365 | keymap_clear(); |
| 381 | } | ||
| 382 | break; | ||
| 383 | case LAYER_ON_PRESS: | ||
| 384 | if (event.pressed) { | ||
| 385 | layer_switch(action.layer.val); | ||
| 386 | } | ||
| 387 | break; | ||
| 388 | case LAYER_ON_RELEASE: | ||
| 389 | if (!event.pressed) { | ||
| 390 | layer_switch(action.layer.val); | ||
| 391 | } | ||
| 392 | break; | ||
| 393 | case LAYER_DEFAULT: /* default layer */ | ||
| 394 | switch (action.layer.val) { | ||
| 395 | case DEFAULT_ON_BOTH: | ||
| 396 | layer_switch(default_layer); | ||
| 397 | break; | 366 | break; |
| 398 | case DEFAULT_ON_PRESS: | 367 | case ON_PRESS: |
| 399 | if (event.pressed) { | 368 | if (event.pressed) { |
| 400 | layer_switch(default_layer); | 369 | overlay_clear(); |
| 370 | keymap_clear(); | ||
| 401 | } | 371 | } |
| 402 | break; | 372 | break; |
| 403 | case DEFAULT_ON_RELEASE: | 373 | case ON_RELEASE: |
| 404 | if (!event.pressed) { | 374 | if (!event.pressed) { |
| 405 | layer_switch(default_layer); | 375 | overlay_clear(); |
| 376 | keymap_clear(); | ||
| 406 | } | 377 | } |
| 407 | break; | 378 | break; |
| 379 | case ON_BOTH: | ||
| 380 | overlay_clear(); | ||
| 381 | keymap_clear(); | ||
| 382 | break; | ||
| 383 | /* NOTE: 4-7 rserved */ | ||
| 408 | } | 384 | } |
| 409 | break; | 385 | break; |
| 410 | case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ | 386 | /* Keymap Reset default layer */ |
| 387 | case (OP_RESET | ON_PRESS): | ||
| 388 | if (event.pressed) { | ||
| 389 | default_layer_set(action.layer.val); | ||
| 390 | } | ||
| 391 | break; | ||
| 392 | case (OP_RESET | ON_RELEASE): | ||
| 393 | if (!event.pressed) { | ||
| 394 | default_layer_set(action.layer.val); | ||
| 395 | } | ||
| 396 | break; | ||
| 397 | case (OP_RESET | ON_BOTH): | ||
| 398 | default_layer_set(action.layer.val); | ||
| 399 | break; | ||
| 400 | |||
| 401 | /* Keymap Bit invert */ | ||
| 402 | case OP_INV: | ||
| 403 | /* with tap toggle */ | ||
| 411 | if (event.pressed) { | 404 | if (event.pressed) { |
| 412 | if (tap_count < TAPPING_TOGGLE) { | 405 | if (tap_count < TAPPING_TOGGLE) { |
| 413 | layer_switch(action.layer.val); | 406 | debug("KEYMAP_INV: tap toggle(press).\n"); |
| 407 | keymap_invert(action.layer.val); | ||
| 414 | } | 408 | } |
| 415 | } else { | 409 | } else { |
| 416 | if (tap_count >= TAPPING_TOGGLE) { | 410 | if (tap_count <= TAPPING_TOGGLE) { |
| 417 | debug("LAYER_PRESSED: tap toggle.\n"); | 411 | debug("KEYMAP_INV: tap toggle(release).\n"); |
| 418 | layer_switch(action.layer.val); | 412 | keymap_invert(action.layer.val); |
| 419 | } | 413 | } |
| 420 | } | 414 | } |
| 421 | break; | 415 | break; |
| 422 | case LAYER_CHANGE_DEFAULT: /* change default layer */ | 416 | case (OP_INV | ON_PRESS): |
| 423 | if (event.pressed) { | 417 | if (event.pressed) { |
| 424 | default_layer = action.layer.val; | 418 | keymap_invert(action.layer.val); |
| 425 | layer_switch(default_layer); | 419 | } |
| 420 | break; | ||
| 421 | case (OP_INV | ON_RELEASE): | ||
| 422 | if (!event.pressed) { | ||
| 423 | keymap_invert(action.layer.val); | ||
| 426 | } | 424 | } |
| 427 | break; | 425 | break; |
| 428 | default: /* switch layer on hold and key on tap*/ | 426 | case (OP_INV | ON_BOTH): |
| 427 | keymap_invert(action.layer.val); | ||
| 428 | break; | ||
| 429 | |||
| 430 | /* Keymap Bit on */ | ||
| 431 | case OP_ON: | ||
| 432 | if (event.pressed) { | ||
| 433 | keymap_on(action.layer.val); | ||
| 434 | } else { | ||
| 435 | keymap_off(action.layer.val); | ||
| 436 | } | ||
| 437 | break; | ||
| 438 | case (OP_ON | ON_PRESS): | ||
| 439 | if (event.pressed) { | ||
| 440 | keymap_on(action.layer.val); | ||
| 441 | } | ||
| 442 | break; | ||
| 443 | case (OP_ON | ON_RELEASE): | ||
| 444 | if (!event.pressed) { | ||
| 445 | keymap_on(action.layer.val); | ||
| 446 | } | ||
| 447 | break; | ||
| 448 | case (OP_ON | ON_BOTH): | ||
| 449 | keymap_on(action.layer.val); | ||
| 450 | break; | ||
| 451 | |||
| 452 | /* Keymap Bit off */ | ||
| 453 | case OP_OFF: | ||
| 454 | if (event.pressed) { | ||
| 455 | keymap_off(action.layer.val); | ||
| 456 | } else { | ||
| 457 | keymap_on(action.layer.val); | ||
| 458 | } | ||
| 459 | break; | ||
| 460 | case (OP_OFF | ON_PRESS): | ||
| 461 | if (event.pressed) { | ||
| 462 | keymap_off(action.layer.val); | ||
| 463 | } | ||
| 464 | break; | ||
| 465 | case (OP_OFF | ON_RELEASE): | ||
| 466 | if (!event.pressed) { | ||
| 467 | keymap_off(action.layer.val); | ||
| 468 | } | ||
| 469 | break; | ||
| 470 | case (OP_OFF | ON_BOTH): | ||
| 471 | keymap_off(action.layer.val); | ||
| 472 | break; | ||
| 473 | |||
| 474 | /* Keymap Bit set */ | ||
| 475 | case OP_SET: | ||
| 476 | if (event.pressed) { | ||
| 477 | keymap_set(action.layer.val); | ||
| 478 | } else { | ||
| 479 | keymap_clear(); | ||
| 480 | } | ||
| 481 | break; | ||
| 482 | case (OP_SET | ON_PRESS): | ||
| 483 | if (event.pressed) { | ||
| 484 | keymap_set(action.layer.val); | ||
| 485 | } | ||
| 486 | break; | ||
| 487 | case (OP_SET | ON_RELEASE): | ||
| 488 | if (!event.pressed) { | ||
| 489 | keymap_set(action.layer.val); | ||
| 490 | } | ||
| 491 | break; | ||
| 492 | case (OP_SET | ON_BOTH): | ||
| 493 | keymap_set(action.layer.val); | ||
| 494 | break; | ||
| 495 | |||
| 496 | /* Keymap Bit invert with tap key */ | ||
| 497 | default: | ||
| 429 | if (event.pressed) { | 498 | if (event.pressed) { |
| 430 | if (tap_count > 0) { | 499 | if (tap_count > 0) { |
| 431 | debug("LAYER_PRESSED: Tap: register_code\n"); | 500 | debug("KEYMAP_TAP_KEY: Tap: register_code\n"); |
| 432 | register_code(action.layer.code); | 501 | register_code(action.layer.code); |
| 433 | } else { | 502 | } else { |
| 434 | debug("LAYER_PRESSED: No tap: layer_switch\n"); | 503 | debug("KEYMAP_TAP_KEY: No tap: On on press\n"); |
| 435 | layer_switch(action.layer.val); | 504 | keymap_on(action.layer.val); |
| 436 | } | 505 | } |
| 437 | } else { | 506 | } else { |
| 438 | if (tap_count > 0) { | 507 | if (tap_count > 0) { |
| 439 | debug("LAYER_PRESSED: Tap: unregister_code\n"); | 508 | debug("KEYMAP_TAP_KEY: Tap: unregister_code\n"); |
| 440 | unregister_code(action.layer.code); | 509 | unregister_code(action.layer.code); |
| 441 | } else { | 510 | } else { |
| 442 | //debug("LAYER_PRESSED: No tap: NO ACTION\n"); | 511 | debug("KEYMAP_TAP_KEY: No tap: Off on release\n"); |
| 443 | // NOTE: This is needed by legacy keymap support | 512 | keymap_off(action.layer.val); |
| 444 | debug("LAYER_PRESSED: No tap: return to default layer\n"); | ||
| 445 | layer_switch(default_layer); | ||
| 446 | } | 513 | } |
| 447 | } | 514 | } |
| 448 | break; | 515 | break; |
| 449 | } | 516 | } |
| 450 | break; | 517 | break; |
| 451 | case ACT_LAYER_BIT: | 518 | |
| 519 | case ACT_OVERLAY: | ||
| 452 | switch (action.layer.code) { | 520 | switch (action.layer.code) { |
| 453 | case LAYER_MOMENTARY: /* momentary */ | 521 | // Overlay Invert bit4 |
| 454 | if (event.pressed) { | 522 | case OP_INV4 | 0: |
| 455 | layer_switch(current_layer ^ action.layer.val); | 523 | if (action.layer.val == 0) { |
| 524 | // NOTE: reserved for future use | ||
| 525 | overlay_clear(); | ||
| 456 | } else { | 526 | } else { |
| 457 | layer_switch(current_layer ^ action.layer.val); | 527 | overlay_set(overlay_stat ^ action.layer.val); |
| 458 | } | 528 | } |
| 459 | break; | 529 | break; |
| 460 | case LAYER_ON_PRESS: | 530 | case OP_INV4 | 1: |
| 461 | if (event.pressed) { | 531 | if (action.layer.val == 0) { |
| 462 | layer_switch(current_layer ^ action.layer.val); | 532 | // on pressed |
| 533 | if (event.pressed) overlay_clear(); | ||
| 534 | } else { | ||
| 535 | overlay_set(overlay_stat ^ action.layer.val<<4); | ||
| 463 | } | 536 | } |
| 464 | break; | 537 | break; |
| 465 | case LAYER_ON_RELEASE: | 538 | case OP_INV4 | 2: |
| 466 | if (!event.pressed) { | 539 | if (action.layer.val == 0) { |
| 467 | layer_switch(current_layer ^ action.layer.val); | 540 | // on released |
| 541 | if (!event.pressed) overlay_clear(); | ||
| 542 | } else { | ||
| 543 | overlay_set(overlay_stat ^ action.layer.val<<8); | ||
| 544 | } | ||
| 545 | break; | ||
| 546 | case OP_INV4 | 3: | ||
| 547 | if (action.layer.val == 0) { | ||
| 548 | // on both | ||
| 549 | overlay_clear(); | ||
| 550 | } else { | ||
| 551 | overlay_set(overlay_stat ^ action.layer.val<<12); | ||
| 468 | } | 552 | } |
| 469 | break; | 553 | break; |
| 470 | case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ | 554 | |
| 555 | /* Overlay Bit invert */ | ||
| 556 | case OP_INV: | ||
| 557 | /* with tap toggle */ | ||
| 471 | if (event.pressed) { | 558 | if (event.pressed) { |
| 472 | if (tap_count < TAPPING_TOGGLE) { | 559 | if (tap_count < TAPPING_TOGGLE) { |
| 473 | debug("LAYER_BIT: tap toggle(press).\n"); | 560 | debug("OVERLAY_INV: tap toggle(press).\n"); |
| 474 | layer_switch(current_layer ^ action.layer.val); | 561 | overlay_invert(action.layer.val); |
| 475 | } | 562 | } |
| 476 | } else { | 563 | } else { |
| 477 | if (tap_count <= TAPPING_TOGGLE) { | 564 | if (tap_count <= TAPPING_TOGGLE) { |
| 478 | debug("LAYER_BIT: tap toggle(release).\n"); | 565 | debug("OVERLAY_INV: tap toggle(release).\n"); |
| 479 | layer_switch(current_layer ^ action.layer.val); | 566 | overlay_invert(action.layer.val); |
| 480 | } | 567 | } |
| 481 | } | 568 | } |
| 482 | break; | 569 | break; |
| 483 | case 0xFF: | 570 | case (OP_INV | ON_PRESS): |
| 484 | // change default layer | 571 | if (event.pressed) { |
| 572 | overlay_invert(action.layer.val); | ||
| 573 | } | ||
| 574 | break; | ||
| 575 | case (OP_INV | ON_RELEASE): | ||
| 576 | if (!event.pressed) { | ||
| 577 | overlay_invert(action.layer.val); | ||
| 578 | } | ||
| 579 | break; | ||
| 580 | case (OP_INV | ON_BOTH): | ||
| 581 | overlay_invert(action.layer.val); | ||
| 582 | break; | ||
| 583 | |||
| 584 | /* Overlay Bit on */ | ||
| 585 | case OP_ON: | ||
| 485 | if (event.pressed) { | 586 | if (event.pressed) { |
| 486 | default_layer = current_layer ^ action.layer.val; | 587 | overlay_on(action.layer.val); |
| 487 | layer_switch(default_layer); | ||
| 488 | } else { | 588 | } else { |
| 489 | default_layer = current_layer ^ action.layer.val; | 589 | overlay_off(action.layer.val); |
| 490 | layer_switch(default_layer); | 590 | } |
| 591 | break; | ||
| 592 | case (OP_ON | ON_PRESS): | ||
| 593 | if (event.pressed) { | ||
| 594 | overlay_on(action.layer.val); | ||
| 491 | } | 595 | } |
| 492 | break; | 596 | break; |
| 597 | case (OP_ON | ON_RELEASE): | ||
| 598 | if (!event.pressed) { | ||
| 599 | overlay_on(action.layer.val); | ||
| 600 | } | ||
| 601 | break; | ||
| 602 | case (OP_ON | ON_BOTH): | ||
| 603 | overlay_on(action.layer.val); | ||
| 604 | break; | ||
| 605 | |||
| 606 | /* Overlay Bit off */ | ||
| 607 | case OP_OFF: | ||
| 608 | if (event.pressed) { | ||
| 609 | overlay_off(action.layer.val); | ||
| 610 | } else { | ||
| 611 | overlay_on(action.layer.val); | ||
| 612 | } | ||
| 613 | break; | ||
| 614 | case (OP_OFF | ON_PRESS): | ||
| 615 | if (event.pressed) { | ||
| 616 | overlay_off(action.layer.val); | ||
| 617 | } | ||
| 618 | break; | ||
| 619 | case (OP_OFF | ON_RELEASE): | ||
| 620 | if (!event.pressed) { | ||
| 621 | overlay_off(action.layer.val); | ||
| 622 | } | ||
| 623 | break; | ||
| 624 | case (OP_OFF | ON_BOTH): | ||
| 625 | overlay_off(action.layer.val); | ||
| 626 | break; | ||
| 627 | |||
| 628 | /* Overlay Bit set */ | ||
| 629 | case OP_SET: | ||
| 630 | if (event.pressed) { | ||
| 631 | overlay_move(action.layer.val); | ||
| 632 | } else { | ||
| 633 | overlay_clear(); | ||
| 634 | } | ||
| 635 | break; | ||
| 636 | case (OP_SET | ON_PRESS): | ||
| 637 | if (event.pressed) { | ||
| 638 | overlay_move(action.layer.val); | ||
| 639 | } | ||
| 640 | break; | ||
| 641 | case (OP_SET | ON_RELEASE): | ||
| 642 | if (!event.pressed) { | ||
| 643 | overlay_move(action.layer.val); | ||
| 644 | } | ||
| 645 | break; | ||
| 646 | case (OP_SET | ON_BOTH): | ||
| 647 | overlay_move(action.layer.val); | ||
| 648 | break; | ||
| 649 | |||
| 650 | /* Overlay Bit invert with tap key */ | ||
| 493 | default: | 651 | default: |
| 494 | // with tap key | ||
| 495 | if (event.pressed) { | 652 | if (event.pressed) { |
| 496 | if (IS_TAPPING_KEY(event.key) && tap_count > 0) { | 653 | if (tap_count > 0) { |
| 497 | debug("LAYER_BIT: Tap: register_code\n"); | 654 | debug("OVERLAY_TAP_KEY: Tap: register_code\n"); |
| 498 | register_code(action.layer.code); | 655 | register_code(action.layer.code); |
| 499 | } else { | 656 | } else { |
| 500 | debug("LAYER_BIT: No tap: layer_switch(bit on)\n"); | 657 | debug("OVERLAY_TAP_KEY: No tap: On on press\n"); |
| 501 | layer_switch(current_layer ^ action.layer.val); | 658 | overlay_on(action.layer.val); |
| 502 | } | 659 | } |
| 503 | } else { | 660 | } else { |
| 504 | if (IS_TAPPING_KEY(event.key) && tap_count > 0) { | 661 | if (tap_count > 0) { |
| 505 | debug("LAYER_BIT: Tap: unregister_code\n"); | 662 | debug("OVERLAY_TAP_KEY: Tap: unregister_code\n"); |
| 506 | unregister_code(action.layer.code); | 663 | unregister_code(action.layer.code); |
| 507 | } else { | 664 | } else { |
| 508 | debug("LAYER_BIT: No tap: layer_switch(bit off)\n"); | 665 | debug("OVERLAY_TAP_KEY: No tap: Off on release\n"); |
| 509 | layer_switch(current_layer ^ action.layer.val); | 666 | overlay_off(action.layer.val); |
| 510 | } | 667 | } |
| 511 | } | 668 | } |
| 512 | break; | 669 | break; |
| @@ -515,7 +672,7 @@ static void process_action(keyrecord_t *record) | |||
| 515 | 672 | ||
| 516 | /* Extentions */ | 673 | /* Extentions */ |
| 517 | case ACT_MACRO: | 674 | case ACT_MACRO: |
| 518 | // TODO | 675 | action_macro_play(action_get_macro(record, action.func.id, action.func.opt)); |
| 519 | break; | 676 | break; |
| 520 | case ACT_COMMAND: | 677 | case ACT_COMMAND: |
| 521 | break; | 678 | break; |
| @@ -540,16 +697,17 @@ static bool process_tapping(keyrecord_t *keyp) | |||
| 540 | // if tapping | 697 | // if tapping |
| 541 | if (IS_TAPPING_PRESSED()) { | 698 | if (IS_TAPPING_PRESSED()) { |
| 542 | if (WITHIN_TAPPING_TERM(event)) { | 699 | if (WITHIN_TAPPING_TERM(event)) { |
| 543 | if (tapping_key.tap_count == 0) { | 700 | if (tapping_key.tap.count == 0) { |
| 544 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | 701 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { |
| 545 | // first tap! | 702 | // first tap! |
| 546 | debug("Tapping: First tap(0->1).\n"); | 703 | debug("Tapping: First tap(0->1).\n"); |
| 547 | tapping_key.tap_count = 1; | 704 | tapping_key.tap.count = 1; |
| 705 | tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false); | ||
| 548 | debug_tapping_key(); | 706 | debug_tapping_key(); |
| 549 | process_action(&tapping_key); | 707 | process_action(&tapping_key); |
| 550 | 708 | ||
| 551 | // enqueue | 709 | // enqueue |
| 552 | keyp->tap_count = tapping_key.tap_count; | 710 | keyp->tap = tapping_key.tap; |
| 553 | return false; | 711 | return false; |
| 554 | } | 712 | } |
| 555 | #if TAPPING_TERM >= 500 | 713 | #if TAPPING_TERM >= 500 |
| @@ -573,19 +731,19 @@ static bool process_tapping(keyrecord_t *keyp) | |||
| 573 | // tap_count > 0 | 731 | // tap_count > 0 |
| 574 | else { | 732 | else { |
| 575 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | 733 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { |
| 576 | debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n"); | 734 | debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n"); |
| 577 | keyp->tap_count = tapping_key.tap_count; | 735 | keyp->tap = tapping_key.tap; |
| 578 | process_action(keyp); | 736 | process_action(keyp); |
| 579 | tapping_key = *keyp; | 737 | tapping_key = *keyp; |
| 580 | debug_tapping_key(); | 738 | debug_tapping_key(); |
| 581 | return true; | 739 | return true; |
| 582 | } | 740 | } |
| 583 | else if (is_tap_key(keyp->event.key) && event.pressed) { | 741 | else if (is_tap_key(keyp->event.key) && event.pressed) { |
| 584 | if (tapping_key.tap_count > 1) { | 742 | if (tapping_key.tap.count > 1) { |
| 585 | debug("Tapping: Start new tap with releasing last tap(>1).\n"); | 743 | debug("Tapping: Start new tap with releasing last tap(>1).\n"); |
| 586 | // unregister key | 744 | // unregister key |
| 587 | process_action(&(keyrecord_t){ | 745 | process_action(&(keyrecord_t){ |
| 588 | .tap_count = tapping_key.tap_count, | 746 | .tap = tapping_key.tap, |
| 589 | .event.key = tapping_key.event.key, | 747 | .event.key = tapping_key.event.key, |
| 590 | .event.time = event.time, | 748 | .event.time = event.time, |
| 591 | .event.pressed = false | 749 | .event.pressed = false |
| @@ -609,7 +767,7 @@ static bool process_tapping(keyrecord_t *keyp) | |||
| 609 | } | 767 | } |
| 610 | // after TAPPING_TERM | 768 | // after TAPPING_TERM |
| 611 | else { | 769 | else { |
| 612 | if (tapping_key.tap_count == 0) { | 770 | if (tapping_key.tap.count == 0) { |
| 613 | debug("Tapping: End. Timeout. Not tap(0): "); | 771 | debug("Tapping: End. Timeout. Not tap(0): "); |
| 614 | debug_event(event); debug("\n"); | 772 | debug_event(event); debug("\n"); |
| 615 | process_action(&tapping_key); | 773 | process_action(&tapping_key); |
| @@ -619,17 +777,17 @@ static bool process_tapping(keyrecord_t *keyp) | |||
| 619 | } else { | 777 | } else { |
| 620 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { | 778 | if (IS_TAPPING_KEY(event.key) && !event.pressed) { |
| 621 | debug("Tapping: End. last timeout tap release(>0)."); | 779 | debug("Tapping: End. last timeout tap release(>0)."); |
| 622 | keyp->tap_count = tapping_key.tap_count; | 780 | keyp->tap = tapping_key.tap; |
| 623 | process_action(keyp); | 781 | process_action(keyp); |
| 624 | tapping_key = (keyrecord_t){}; | 782 | tapping_key = (keyrecord_t){}; |
| 625 | return true; | 783 | return true; |
| 626 | } | 784 | } |
| 627 | else if (is_tap_key(keyp->event.key) && event.pressed) { | 785 | else if (is_tap_key(keyp->event.key) && event.pressed) { |
| 628 | if (tapping_key.tap_count > 1) { | 786 | if (tapping_key.tap.count > 1) { |
| 629 | debug("Tapping: Start new tap with releasing last timeout tap(>1).\n"); | 787 | debug("Tapping: Start new tap with releasing last timeout tap(>1).\n"); |
| 630 | // unregister key | 788 | // unregister key |
| 631 | process_action(&(keyrecord_t){ | 789 | process_action(&(keyrecord_t){ |
| 632 | .tap_count = tapping_key.tap_count, | 790 | .tap = tapping_key.tap, |
| 633 | .event.key = tapping_key.event.key, | 791 | .event.key = tapping_key.event.key, |
| 634 | .event.time = event.time, | 792 | .event.time = event.time, |
| 635 | .event.pressed = false | 793 | .event.pressed = false |
| @@ -653,10 +811,11 @@ static bool process_tapping(keyrecord_t *keyp) | |||
| 653 | } | 811 | } |
| 654 | } else if (IS_TAPPING_RELEASED()) { | 812 | } else if (IS_TAPPING_RELEASED()) { |
| 655 | if (WITHIN_TAPPING_TERM(event)) { | 813 | if (WITHIN_TAPPING_TERM(event)) { |
| 656 | if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { | 814 | if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) { |
| 657 | // sequential tap. | 815 | // sequential tap. |
| 658 | keyp->tap_count = tapping_key.tap_count + 1; | 816 | keyp->tap = tapping_key.tap; |
| 659 | debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n"); | 817 | keyp->tap.count += 1; |
| 818 | debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n"); | ||
| 660 | process_action(keyp); | 819 | process_action(keyp); |
| 661 | tapping_key = *keyp; | 820 | tapping_key = *keyp; |
| 662 | debug_tapping_key(); | 821 | debug_tapping_key(); |
| @@ -701,16 +860,16 @@ static bool process_tapping(keyrecord_t *keyp) | |||
| 701 | static void waiting_buffer_scan_tap(void) | 860 | static void waiting_buffer_scan_tap(void) |
| 702 | { | 861 | { |
| 703 | // tapping already is settled | 862 | // tapping already is settled |
| 704 | if (tapping_key.tap_count > 0) return; | 863 | if (tapping_key.tap.count > 0) return; |
| 705 | // invalid state: tapping_key released && tap_count == 0 | 864 | // invalid state: tapping_key released && tap.count == 0 |
| 706 | if (!tapping_key.event.pressed) return; | 865 | if (!tapping_key.event.pressed) return; |
| 707 | 866 | ||
| 708 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { | 867 | for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { |
| 709 | if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && | 868 | if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && |
| 710 | !waiting_buffer[i].event.pressed && | 869 | !waiting_buffer[i].event.pressed && |
| 711 | WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { | 870 | WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { |
| 712 | tapping_key.tap_count = 1; | 871 | tapping_key.tap.count = 1; |
| 713 | waiting_buffer[i].tap_count = 1; | 872 | waiting_buffer[i].tap.count = 1; |
| 714 | process_action(&tapping_key); | 873 | process_action(&tapping_key); |
| 715 | 874 | ||
| 716 | debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n"); | 875 | debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n"); |
| @@ -813,39 +972,24 @@ bool sending_anykey(void) | |||
| 813 | host_last_sysytem_report() || host_last_consumer_report()); | 972 | host_last_sysytem_report() || host_last_consumer_report()); |
| 814 | } | 973 | } |
| 815 | 974 | ||
| 816 | void layer_switch(uint8_t new_layer) | ||
| 817 | { | ||
| 818 | if (current_layer != new_layer) { | ||
| 819 | debug("Layer Switch: "); debug_hex(current_layer); | ||
| 820 | debug(" -> "); debug_hex(new_layer); debug("\n"); | ||
| 821 | |||
| 822 | current_layer = new_layer; | ||
| 823 | clear_keyboard_but_mods(); // To avoid stuck keys | ||
| 824 | // NOTE: update mods with full scan of matrix? if modifier changes between layers | ||
| 825 | } | ||
| 826 | } | ||
| 827 | |||
| 828 | bool is_tap_key(key_t key) | 975 | bool is_tap_key(key_t key) |
| 829 | { | 976 | { |
| 830 | action_t action = get_action(key); | 977 | action_t action = layer_switch_get_action(key); |
| 831 | 978 | ||
| 832 | switch (action.kind.id) { | 979 | switch (action.kind.id) { |
| 833 | case ACT_LMODS_TAP: | 980 | case ACT_LMODS_TAP: |
| 834 | case ACT_RMODS_TAP: | 981 | case ACT_RMODS_TAP: |
| 835 | return true; | 982 | return true; |
| 836 | case ACT_LAYER: | 983 | case ACT_KEYMAP: |
| 837 | case ACT_LAYER_BIT: | 984 | case ACT_OVERLAY: |
| 838 | switch (action.layer.code) { | 985 | switch (action.layer.code) { |
| 839 | case LAYER_MOMENTARY: | 986 | case 0x04 ... 0xEF: /* tap key */ |
| 840 | case LAYER_ON_PRESS: | 987 | case OP_INV: |
| 841 | case LAYER_ON_RELEASE: | ||
| 842 | case LAYER_DEFAULT: | ||
| 843 | return false; | ||
| 844 | case LAYER_TAP_TOGGLE: | ||
| 845 | default: /* tap key */ | ||
| 846 | return true; | 988 | return true; |
| 989 | default: | ||
| 990 | return false; | ||
| 847 | } | 991 | } |
| 848 | return false; | 992 | case ACT_MACRO: |
| 849 | case ACT_FUNCTION: | 993 | case ACT_FUNCTION: |
| 850 | if (action.func.opt & FUNC_TAP) { return true; } | 994 | if (action.func.opt & FUNC_TAP) { return true; } |
| 851 | return false; | 995 | return false; |
| @@ -865,7 +1009,8 @@ static void debug_event(keyevent_t event) | |||
| 865 | } | 1009 | } |
| 866 | static void debug_record(keyrecord_t record) | 1010 | static void debug_record(keyrecord_t record) |
| 867 | { | 1011 | { |
| 868 | debug_event(record.event); debug(":"); debug_dec(record.tap_count); | 1012 | debug_event(record.event); debug(":"); debug_dec(record.tap.count); |
| 1013 | if (record.tap.interrupted) debug("-"); | ||
| 869 | } | 1014 | } |
| 870 | static void debug_action(action_t action) | 1015 | static void debug_action(action_t action) |
| 871 | { | 1016 | { |
| @@ -876,8 +1021,8 @@ static void debug_action(action_t action) | |||
| 876 | case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; | 1021 | case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; |
| 877 | case ACT_USAGE: debug("ACT_USAGE"); break; | 1022 | case ACT_USAGE: debug("ACT_USAGE"); break; |
| 878 | case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; | 1023 | case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; |
| 879 | case ACT_LAYER: debug("ACT_LAYER"); break; | 1024 | case ACT_KEYMAP: debug("ACT_KEYMAP"); break; |
| 880 | case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; | 1025 | case ACT_OVERLAY: debug("ACT_OVERLAY"); break; |
| 881 | case ACT_MACRO: debug("ACT_MACRO"); break; | 1026 | case ACT_MACRO: debug("ACT_MACRO"); break; |
| 882 | case ACT_COMMAND: debug("ACT_COMMAND"); break; | 1027 | case ACT_COMMAND: debug("ACT_COMMAND"); break; |
| 883 | case ACT_FUNCTION: debug("ACT_FUNCTION"); break; | 1028 | case ACT_FUNCTION: debug("ACT_FUNCTION"); break; |
diff --git a/common/action.h b/common/action.h index b9a6cb5b4..ead917983 100644 --- a/common/action.h +++ b/common/action.h | |||
| @@ -19,12 +19,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 19 | 19 | ||
| 20 | #include "keyboard.h" | 20 | #include "keyboard.h" |
| 21 | #include "keycode.h" | 21 | #include "keycode.h" |
| 22 | #include "action_macro.h" | ||
| 22 | 23 | ||
| 23 | 24 | ||
| 24 | /* Struct to record event and tap count */ | 25 | /* Struct to record event and tap count */ |
| 26 | typedef union { | ||
| 27 | struct { | ||
| 28 | bool interrupted :1; | ||
| 29 | bool reserved2 :1; | ||
| 30 | bool reserved1 :1; | ||
| 31 | bool reserved0 :1; | ||
| 32 | uint8_t count :4; | ||
| 33 | }; | ||
| 34 | } tap_t; | ||
| 35 | |||
| 25 | typedef struct { | 36 | typedef struct { |
| 26 | keyevent_t event; | 37 | keyevent_t event; |
| 27 | uint8_t tap_count; | 38 | tap_t tap; |
| 28 | } keyrecord_t; | 39 | } keyrecord_t; |
| 29 | 40 | ||
| 30 | /* Action struct. | 41 | /* Action struct. |
| @@ -76,17 +87,15 @@ typedef union { | |||
| 76 | 87 | ||
| 77 | 88 | ||
| 78 | 89 | ||
| 79 | /* layer used currently */ | ||
| 80 | extern uint8_t current_layer; | ||
| 81 | /* layer to return or start with */ | ||
| 82 | extern uint8_t default_layer; | ||
| 83 | |||
| 84 | /* Execute action per keyevent */ | 90 | /* Execute action per keyevent */ |
| 85 | void action_exec(keyevent_t event); | 91 | void action_exec(keyevent_t event); |
| 86 | 92 | ||
| 87 | /* action for key */ | 93 | /* action for key */ |
| 88 | action_t action_for_key(uint8_t layer, key_t key); | 94 | action_t action_for_key(uint8_t layer, key_t key); |
| 89 | 95 | ||
| 96 | /* macro */ | ||
| 97 | const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt); | ||
| 98 | |||
| 90 | /* user defined special function */ | 99 | /* user defined special function */ |
| 91 | void action_function(keyrecord_t *record, uint8_t id, uint8_t opt); | 100 | void action_function(keyrecord_t *record, uint8_t id, uint8_t opt); |
| 92 | 101 | ||
| @@ -112,8 +121,8 @@ bool waiting_buffer_has_anykey_pressed(void); | |||
| 112 | * ============ | 121 | * ============ |
| 113 | * 16bit code: action_kind(4bit) + action_parameter(12bit) | 122 | * 16bit code: action_kind(4bit) + action_parameter(12bit) |
| 114 | * | 123 | * |
| 115 | * Keyboard Keys | 124 | * Keyboard Keys(00XX) |
| 116 | * ------------- | 125 | * ------------------- |
| 117 | * ACT_LMODS(0000): | 126 | * ACT_LMODS(0000): |
| 118 | * 0000|0000|000000|00 No action | 127 | * 0000|0000|000000|00 No action |
| 119 | * 0000|0000|000000|01 Transparent | 128 | * 0000|0000|000000|01 Transparent |
| @@ -143,8 +152,8 @@ bool waiting_buffer_has_anykey_pressed(void); | |||
| 143 | * 0011|mods| keycode Right mods + tap Key | 152 | * 0011|mods| keycode Right mods + tap Key |
| 144 | * | 153 | * |
| 145 | * | 154 | * |
| 146 | * Other HID Usage | 155 | * Other keys(01XX) |
| 147 | * --------------- | 156 | * -------------------- |
| 148 | * This action handles other usages than keyboard. | 157 | * This action handles other usages than keyboard. |
| 149 | * ACT_USAGE(0100): | 158 | * ACT_USAGE(0100): |
| 150 | * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01) | 159 | * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01) |
| @@ -152,41 +161,45 @@ bool waiting_buffer_has_anykey_pressed(void); | |||
| 152 | * 0100|10| usage(10) (reserved) | 161 | * 0100|10| usage(10) (reserved) |
| 153 | * 0100|11| usage(10) (reserved) | 162 | * 0100|11| usage(10) (reserved) |
| 154 | * | 163 | * |
| 155 | * | ||
| 156 | * Mouse Keys | ||
| 157 | * ---------- | ||
| 158 | * TODO: can be combined with 'Other HID Usage'? to save action kind id. | ||
| 159 | * ACT_MOUSEKEY(0110): | 164 | * ACT_MOUSEKEY(0110): |
| 160 | * 0101|XXXX| keycode Mouse key | 165 | * 0101|XXXX| keycode Mouse key |
| 161 | * | 166 | * |
| 162 | * | 167 | * |
| 163 | * Layer Actions | 168 | * Layer Actions(10XX) |
| 164 | * ------------- | 169 | * ------------------- |
| 165 | * ACT_LAYER(1000): Set layer | 170 | * ACT_KEYMAP: |
| 166 | * ACT_LAYER_BIT(1001): Bit-op layer | 171 | * 1000|--xx|0000 0000 Clear keyamp and overlay |
| 167 | * | 172 | * 1000|LLLL|0000 00xx Reset default layer and clear keymap and overlay |
| 168 | * 1000|LLLL|0000 0000 set L to layer on press and set default on release(momentary) | 173 | * 1000|LLLL| keycode Invert with tap key |
| 169 | * 1000|LLLL|0000 0001 set L to layer on press | 174 | * 1000|LLLL|1111 0000 Invert with tap toggle |
| 170 | * 1000|LLLL|0000 0010 set L to layer on release | 175 | * 1000|LLLL|1111 00xx Invert[^= 1<<L] |
| 171 | * 1000|----|0000 0011 set default to layer on both(return to default layer) | 176 | * 1000|LLLL|1111 0100 On/Off |
| 172 | * 1000|LLLL| keycode set L to layer while hold and send key on tap | 177 | * 1000|LLLL|1111 01xx On[|= 1<<L] |
| 173 | * 1000|LLLL|1111 0000 set L to layer while hold and toggle on several taps | 178 | * 1000|LLLL|1111 1000 Off/On |
| 174 | * 1000|LLLL|1111 1111 set L to default and layer(on press) | 179 | * 1000|LLLL|1111 10xx Off[&= ~(1<<L)] |
| 175 | * | 180 | * 1000|LLLL|1111 1100 Set/Clear |
| 176 | * 1001|BBBB|0000 0000 (not used) | 181 | * 1000|LLLL|1111 11xx Set[= 1<<L] |
| 177 | * 1001|BBBB|0000 0001 bit-xor layer with B on press | 182 | * default layer: 0-15(4bit) |
| 178 | * 1001|BBBB|0000 0010 bit-xor layer with B on release | 183 | * xx: On {00:for special use, 01:press, 10:release, 11:both} |
| 179 | * 1001|BBBB|0000 0011 bit-xor layer with B on both(momentary) | ||
| 180 | * 1001|BBBB| keycode bit-xor layer with B while hold and send key on tap | ||
| 181 | * 1001|BBBB|1111 0000 bit-xor layer with B while hold and toggle on several taps | ||
| 182 | * 1001|BBBB|1111 1111 bit-xor default with B and set layer(on press) | ||
| 183 | * | 184 | * |
| 185 | * ACT_OVERLAY: | ||
| 186 | * 1011|0000|0000 0000 Clear overlay | ||
| 187 | * 1011|LLLL|0000 00ss Invert 4-bit chunk [^= L<<(4*ss)] | ||
| 188 | * 1011|LLLL| keycode Invert with tap key | ||
| 189 | * 1011|LLLL|1111 0000 Invert with tap toggle | ||
| 190 | * 1011|LLLL|1111 00xx Invert[^= 1<<L] | ||
| 191 | * 1011|LLLL|1111 0100 On/Off(momentary) | ||
| 192 | * 1011|LLLL|1111 01xx On[|= 1<<L] | ||
| 193 | * 1011|LLLL|1111 1000 Off/On | ||
| 194 | * 1011|LLLL|1111 10xx Off[&= ~(1<<L)] | ||
| 195 | * 1011|LLLL|1111 1100 Set/Clear | ||
| 196 | * 1011|LLLL|1111 11xx Set[= 1<<L] | ||
| 197 | * overlays: 16-layer on/off status(16bit) | ||
| 198 | * xx: On {00:for special use, 01:press, 10:release, 11:both} | ||
| 184 | * | 199 | * |
| 185 | * | 200 | * |
| 186 | * Extensions(11XX) | 201 | * Extensions(11XX) |
| 187 | * ---------------- | 202 | * ---------------- |
| 188 | * NOTE: NOT FIXED | ||
| 189 | * | ||
| 190 | * ACT_MACRO(1100): | 203 | * ACT_MACRO(1100): |
| 191 | * 1100|opt | id(8) Macro play? | 204 | * 1100|opt | id(8) Macro play? |
| 192 | * 1100|1111| id(8) Macro record? | 205 | * 1100|1111| id(8) Macro record? |
| @@ -208,8 +221,8 @@ enum action_kind_id { | |||
| 208 | ACT_USAGE = 0b0100, | 221 | ACT_USAGE = 0b0100, |
| 209 | ACT_MOUSEKEY = 0b0101, | 222 | ACT_MOUSEKEY = 0b0101, |
| 210 | 223 | ||
| 211 | ACT_LAYER = 0b1000, | 224 | ACT_KEYMAP = 0b1000, |
| 212 | ACT_LAYER_BIT = 0b1001, | 225 | ACT_OVERLAY = 0b1001, |
| 213 | 226 | ||
| 214 | ACT_MACRO = 0b1100, | 227 | ACT_MACRO = 0b1100, |
| 215 | ACT_COMMAND = 0b1110, | 228 | ACT_COMMAND = 0b1110, |
| @@ -223,20 +236,20 @@ enum action_kind_id { | |||
| 223 | #define ACTION(kind, param) ((kind)<<12 | (param)) | 236 | #define ACTION(kind, param) ((kind)<<12 | (param)) |
| 224 | #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) | 237 | #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) |
| 225 | 238 | ||
| 226 | /* Key */ | 239 | /* |
| 240 | * Key | ||
| 241 | */ | ||
| 227 | #define ACTION_KEY(key) ACTION(ACT_LMODS, key) | 242 | #define ACTION_KEY(key) ACTION(ACT_LMODS, key) |
| 228 | /* Mods & key */ | 243 | /* Mods & key */ |
| 229 | #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00) | 244 | #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00) |
| 230 | #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key)) | 245 | #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key)) |
| 231 | #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00) | 246 | #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00) |
| 232 | #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key)) | 247 | #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key)) |
| 233 | /* Mod & key */ | ||
| 234 | #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) | 248 | #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) |
| 235 | #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key)) | 249 | #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key)) |
| 236 | #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) | 250 | #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) |
| 237 | #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key)) | 251 | #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key)) |
| 238 | 252 | /* Tap key */ | |
| 239 | /* Mods + Tap key */ | ||
| 240 | enum mods_codes { | 253 | enum mods_codes { |
| 241 | MODS_ONESHOT = 0x00, | 254 | MODS_ONESHOT = 0x00, |
| 242 | }; | 255 | }; |
| @@ -244,102 +257,112 @@ enum mods_codes { | |||
| 244 | #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT) | 257 | #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT) |
| 245 | #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key)) | 258 | #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key)) |
| 246 | #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT) | 259 | #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT) |
| 247 | /* Mod + Tap key */ | ||
| 248 | #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) | 260 | #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) |
| 249 | #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT) | 261 | #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT) |
| 250 | #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) | 262 | #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key)) |
| 251 | #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT) | 263 | #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT) |
| 252 | 264 | ||
| 265 | /* HID Usage */ | ||
| 266 | enum usage_pages { | ||
| 267 | PAGE_SYSTEM, | ||
| 268 | PAGE_CONSUMER | ||
| 269 | }; | ||
| 270 | #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id)) | ||
| 271 | #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id)) | ||
| 253 | 272 | ||
| 254 | /* | 273 | /* Mousekey */ |
| 255 | * Switch layer | 274 | #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key) |
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | /* Layer Actions: | ||
| 279 | * Invert layer ^= (1<<layer) | ||
| 280 | * On layer |= (1<<layer) | ||
| 281 | * Off layer &= ~(1<<layer) | ||
| 282 | * Set layer = (1<<layer) | ||
| 283 | * Clear layer = 0 | ||
| 256 | */ | 284 | */ |
| 257 | enum layer_codes { | 285 | enum layer_params { |
| 258 | LAYER_MOMENTARY = 0, | 286 | ON_PRESS = 1, |
| 259 | LAYER_ON_PRESS = 1, | 287 | ON_RELEASE = 2, |
| 260 | LAYER_ON_RELEASE = 2, | 288 | ON_BOTH = 3, |
| 261 | LAYER_DEFAULT =3, | 289 | |
| 262 | LAYER_TAP_TOGGLE = 0xF0, | 290 | OP_RESET = 0x00, |
| 263 | LAYER_CHANGE_DEFAULT = 0xFF | 291 | OP_INV4 = 0x00, |
| 264 | }; | 292 | OP_INV = 0xF0, |
| 265 | enum layer_vals_default { | 293 | OP_ON = 0xF4, |
| 266 | DEFAULT_ON_PRESS = 1, | 294 | OP_OFF = 0xF8, |
| 267 | DEFAULT_ON_RELEASE = 2, | 295 | OP_SET = 0xFC, |
| 268 | DEFAULT_ON_BOTH = 3, | ||
| 269 | }; | 296 | }; |
| 270 | 297 | ||
| 271 | /* | 298 | /* |
| 272 | * return to default layer | 299 | * Default Layer |
| 273 | */ | 300 | */ |
| 274 | #define ACTION_LAYER_DEFAULT ACTION_LAYER_DEFAULT_R | 301 | #define ACTION_DEFAULT_LAYER ACTION(ACT_KEYMAP, ON_RELEASE<<8 | OP_RESET | 0) |
| 275 | /* set default layer on press */ | 302 | #define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE) |
| 276 | #define ACTION_LAYER_DEFAULT_P ACTION(ACT_LAYER, DEFAULT_ON_PRESS<<8 | LAYER_DEFAULT) | 303 | #define ACTION_DEFAULT_LAYER_TO(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_RESET | (on)) |
| 277 | /* set default layer on release */ | ||
| 278 | #define ACTION_LAYER_DEFAULT_R ACTION(ACT_LAYER, DEFAULT_ON_RELEASE<<8 | LAYER_DEFAULT) | ||
| 279 | /* change default layer and set layer */ | ||
| 280 | |||
| 281 | /* | 304 | /* |
| 282 | * Set layer | 305 | * Keymap Layer |
| 283 | */ | 306 | */ |
| 284 | /* set layer on press and none on release */ | 307 | #define ACTION_KEYMAP_MOMENTARY(layer) ACTION_KEYMAP_ON_OFF(layer) |
| 285 | #define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer) | 308 | #define ACTION_KEYMAP_TOGGLE(layer) ACTION_KEYMAP_INV(layer, ON_RELEASE) |
| 286 | /* set layer on press and set default on release (This is needed by legacy keymap support.) */ | 309 | /* Keymap Invert */ |
| 287 | #define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY) | 310 | #define ACTION_KEYMAP_INV(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | (on)) |
| 288 | /* set layer on press and none on release */ | 311 | #define ACTION_KEYMAP_TAP_TOGGLE(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | 0) |
| 289 | #define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer) | 312 | /* Keymap On */ |
| 290 | /* set layer while hold and send key on tap */ | 313 | #define ACTION_KEYMAP_ON(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | (on)) |
| 291 | #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key)) | 314 | #define ACTION_KEYMAP_ON_OFF(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | 0) |
| 292 | /* set layer on press */ | 315 | /* Keymap Off */ |
| 293 | #define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS) | 316 | #define ACTION_KEYMAP_OFF(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | (on)) |
| 294 | /* set layer on release */ | 317 | #define ACTION_KEYMAP_OFF_ON(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | 0) |
| 295 | #define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE) | 318 | /* Keymap Set */ |
| 296 | /* set layer on hold and toggle on several taps */ | 319 | #define ACTION_KEYMAP_SET(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | (on)) |
| 297 | #define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE) | 320 | #define ACTION_KEYMAP_SET_CLEAR(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | 0) |
| 298 | /* set default layer on both press and release */ | 321 | /* Keymap Invert with tap key */ |
| 299 | #define ACTION_LAYER_SET_DEFAULT(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_CHANGE_DEFAULT) | 322 | #define ACTION_KEYMAP_TAP_KEY(layer, key) ACTION(ACT_KEYMAP, (layer)<<8 | (key)) |
| 300 | 323 | ||
| 301 | /* | 324 | /* |
| 302 | * Bit-op layer | 325 | * Overlay Layer |
| 303 | */ | 326 | */ |
| 304 | /* bit-xor on both press and release */ | 327 | #define ACTION_OVERLAY_MOMENTARY(layer) ACTION_OVERLAY_ON_OFF(layer) |
| 305 | #define ACTION_LAYER_BIT(bits) ACTION_LAYER_BIT_MOMENTARY(bits) | 328 | #define ACTION_OVERLAY_TOGGLE(layer) ACTION_OVERLAY_INV(layer, ON_RELEASE) |
| 306 | #define ACTION_LAYER_BIT_MOMENTARY(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY) | 329 | /* Overlay Clear */ |
| 307 | /* bit-xor on press */ | 330 | #define ACTION_OVERLAY_CLEAR(on) ACTION(ACT_OVERLAY, 0<<8 | OP_INV4 | (on)) |
| 308 | #define ACTION_LAYER_BIT_TOGGLE(bits) ACTION_LAYER_BIT_R(bits) | 331 | /* Overlay Invert 4-bit chunk */ |
| 309 | /* bit-xor while hold and send key on tap */ | 332 | #define ACTION_OVERLAY_INV4(bits, shift) ACTION(ACT_OVERLAY, (bits)<<8 | OP_INV4 | shift) |
| 310 | #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key)) | 333 | /* Overlay Invert */ |
| 311 | /* bit-xor on press */ | 334 | #define ACTION_OVERLAY_INV(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | (on)) |
| 312 | #define ACTION_LAYER_BIT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS) | 335 | #define ACTION_OVERLAY_TAP_TOGGLE(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | 0) |
| 313 | /* bit-xor on release */ | 336 | /* Overlay On */ |
| 314 | #define ACTION_LAYER_BIT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE) | 337 | #define ACTION_OVERLAY_ON(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | (on)) |
| 315 | /* bit-xor while hold and toggle on several taps */ | 338 | #define ACTION_OVERLAY_ON_OFF(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | 0) |
| 316 | #define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE) | 339 | /* Overlay Off */ |
| 317 | /* bit-xor default layer and set layer */ | 340 | #define ACTION_OVERLAY_OFF(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | (on)) |
| 318 | #define ACTION_LAYER_BIT_DEFAULT(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_CHANGE_DEFAULT) | 341 | #define ACTION_OVERLAY_OFF_ON(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | 0) |
| 319 | 342 | /* Overlay Set */ | |
| 320 | 343 | #define ACTION_OVERLAY_SET(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | (on)) | |
| 321 | /* HID Usage */ | 344 | #define ACTION_OVERLAY_SET_CLEAR(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | 0) |
| 322 | enum usage_pages { | 345 | /* Overlay Invert with tap key */ |
| 323 | PAGE_SYSTEM, | 346 | #define ACTION_OVERLAY_TAP_KEY(layer, key) ACTION(ACT_OVERLAY, (layer)<<8 | (key)) |
| 324 | PAGE_CONSUMER | ||
| 325 | }; | ||
| 326 | #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id)) | ||
| 327 | #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id)) | ||
| 328 | 347 | ||
| 329 | /* Mousekey */ | ||
| 330 | #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key) | ||
| 331 | 348 | ||
| 349 | /* | ||
| 350 | * Extensions | ||
| 351 | */ | ||
| 332 | /* Macro */ | 352 | /* Macro */ |
| 333 | #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr)) | 353 | #define ACTION_MACRO(id) ACTION(ACT_MACRO, (id)) |
| 354 | #define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id)) | ||
| 355 | #define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id)) | ||
| 334 | 356 | ||
| 335 | /* Command */ | 357 | /* Command */ |
| 336 | #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr)) | 358 | #define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr)) |
| 337 | 359 | ||
| 338 | /* Function */ | 360 | /* Function */ |
| 339 | enum function_opts { | 361 | enum function_opts { |
| 340 | FUNC_TAP = 0x8, /* indciates function is tappable */ | 362 | FUNC_TAP = 0x8, /* indciates function is tappable */ |
| 341 | }; | 363 | }; |
| 342 | #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id) | 364 | #define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id)) |
| 343 | #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id) | 365 | #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id)) |
| 366 | #define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id)) | ||
| 344 | 367 | ||
| 345 | #endif /* ACTION_H */ | 368 | #endif /* ACTION_H */ |
diff --git a/common/action_macro.c b/common/action_macro.c index 72859c0dd..ca7ffa822 100644 --- a/common/action_macro.c +++ b/common/action_macro.c | |||
| @@ -41,7 +41,6 @@ void action_macro_play(const prog_macro_t *macro_p) | |||
| 41 | case MODS_DOWN: | 41 | case MODS_DOWN: |
| 42 | MACRO_READ(); | 42 | MACRO_READ(); |
| 43 | debug("MODS_DOWN("); debug_hex(macro); debug(")\n"); | 43 | debug("MODS_DOWN("); debug_hex(macro); debug(")\n"); |
| 44 | debug("MODS_UP("); debug_hex(macro); debug(")\n"); | ||
| 45 | add_mods(macro); | 44 | add_mods(macro); |
| 46 | break; | 45 | break; |
| 47 | case MODS_UP: | 46 | case MODS_UP: |
diff --git a/common/action_macro.h b/common/action_macro.h index 3833c7c8a..db6577959 100644 --- a/common/action_macro.h +++ b/common/action_macro.h | |||
| @@ -20,6 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 20 | #include <avr/pgmspace.h> | 20 | #include <avr/pgmspace.h> |
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | #define MACRO_NONE 0 | ||
| 24 | #define MACRO(...) ({ static prog_macro_t _m[] PROGMEM = { __VA_ARGS__ }; _m; }) | ||
| 25 | |||
| 26 | |||
| 23 | typedef uint8_t macro_t; | 27 | typedef uint8_t macro_t; |
| 24 | typedef macro_t prog_macro_t PROGMEM; | 28 | typedef macro_t prog_macro_t PROGMEM; |
| 25 | 29 | ||
diff --git a/common/command.c b/common/command.c index 7bb2a23f1..202d531fd 100644 --- a/common/command.c +++ b/common/command.c | |||
| @@ -26,7 +26,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 26 | #include "timer.h" | 26 | #include "timer.h" |
| 27 | #include "keyboard.h" | 27 | #include "keyboard.h" |
| 28 | #include "bootloader.h" | 28 | #include "bootloader.h" |
| 29 | #include "layer_switch.h" | ||
| 29 | #include "command.h" | 30 | #include "command.h" |
| 31 | |||
| 30 | #ifdef MOUSEKEY_ENABLE | 32 | #ifdef MOUSEKEY_ENABLE |
| 31 | #include "mousekey.h" | 33 | #include "mousekey.h" |
| 32 | #endif | 34 | #endif |
| @@ -53,7 +55,7 @@ static void mousekey_console_help(void); | |||
| 53 | #endif | 55 | #endif |
| 54 | 56 | ||
| 55 | static uint8_t numkey2num(uint8_t code); | 57 | static uint8_t numkey2num(uint8_t code); |
| 56 | static void switch_layer(uint8_t layer); | 58 | static void switch_default_layer(uint8_t layer); |
| 57 | 59 | ||
| 58 | 60 | ||
| 59 | typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t; | 61 | typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t; |
| @@ -261,18 +263,16 @@ static bool command_common(uint8_t code) | |||
| 261 | #endif | 263 | #endif |
| 262 | break; | 264 | break; |
| 263 | #endif | 265 | #endif |
| 266 | case KC_ESC: | ||
| 267 | case KC_GRV: | ||
| 264 | case KC_0: | 268 | case KC_0: |
| 265 | case KC_F10: | 269 | switch_default_layer(0); |
| 266 | clear_keyboard(); | ||
| 267 | switch_layer(0); | ||
| 268 | break; | 270 | break; |
| 269 | case KC_1 ... KC_9: | 271 | case KC_1 ... KC_9: |
| 270 | clear_keyboard(); | 272 | switch_default_layer((code - KC_1) + 1); |
| 271 | switch_layer((code - KC_1) + 1); | ||
| 272 | break; | 273 | break; |
| 273 | case KC_F1 ... KC_F9: | 274 | case KC_F1 ... KC_F12: |
| 274 | clear_keyboard(); | 275 | switch_default_layer((code - KC_F1) + 1); |
| 275 | switch_layer((code - KC_F1) + 1); | ||
| 276 | break; | 276 | break; |
| 277 | default: | 277 | default: |
| 278 | print("?"); | 278 | print("?"); |
| @@ -541,11 +541,10 @@ static uint8_t numkey2num(uint8_t code) | |||
| 541 | return 0; | 541 | return 0; |
| 542 | } | 542 | } |
| 543 | 543 | ||
| 544 | static void switch_layer(uint8_t layer) | 544 | static void switch_default_layer(uint8_t layer) |
| 545 | { | 545 | { |
| 546 | print_val_hex8(current_layer); | 546 | print("switch_default_layer: "); print_dec(default_layer); print(" to "); print_dec(layer); print("\n"); |
| 547 | print_val_hex8(default_layer); | 547 | default_layer_set(layer); |
| 548 | current_layer = layer; | 548 | overlay_clear(); |
| 549 | default_layer = layer; | 549 | clear_keyboard(); |
| 550 | print("switch to "); print_val_hex8(layer); | ||
| 551 | } | 550 | } |
diff --git a/common/keymap.c b/common/keymap.c index 078615814..aa8d944a7 100644 --- a/common/keymap.c +++ b/common/keymap.c | |||
| @@ -14,13 +14,71 @@ GNU General Public License for more details. | |||
| 14 | You should have received a copy of the GNU General Public License | 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/>. | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ | 16 | */ |
| 17 | #include <avr/pgmspace.h> | ||
| 17 | #include "keymap.h" | 18 | #include "keymap.h" |
| 18 | #include "report.h" | 19 | #include "report.h" |
| 19 | #include "keycode.h" | 20 | #include "keycode.h" |
| 21 | #include "layer_switch.h" | ||
| 20 | #include "action.h" | 22 | #include "action.h" |
| 23 | #include "action_macro.h" | ||
| 24 | #include "debug.h" | ||
| 21 | 25 | ||
| 22 | 26 | ||
| 23 | action_t keymap_keycode_to_action(uint8_t keycode) | 27 | static action_t keycode_to_action(uint8_t keycode); |
| 28 | |||
| 29 | #ifdef USE_KEYMAP_V2 | ||
| 30 | /* converts key to action */ | ||
| 31 | action_t action_for_key(uint8_t layer, key_t key) | ||
| 32 | { | ||
| 33 | uint8_t keycode = keymap_key_to_keycode(layer, key); | ||
| 34 | switch (keycode) { | ||
| 35 | case KC_FN0 ... KC_FN31: | ||
| 36 | return keymap_fn_to_action(keycode); | ||
| 37 | default: | ||
| 38 | return keycode_to_action(keycode); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | #else | ||
| 42 | /* | ||
| 43 | * legacy keymap support | ||
| 44 | */ | ||
| 45 | /* translation for legacy keymap */ | ||
| 46 | action_t action_for_key(uint8_t layer, key_t key) | ||
| 47 | { | ||
| 48 | /* convert from legacy keycode to action */ | ||
| 49 | /* layer 16-31 indicate 'overlay' but not supported in legacy keymap */ | ||
| 50 | uint8_t keycode = keymap_get_keycode((layer & OVERLAY_MASK), key.row, key.col); | ||
| 51 | action_t action; | ||
| 52 | switch (keycode) { | ||
| 53 | case KC_FN0 ... KC_FN31: | ||
| 54 | { | ||
| 55 | uint8_t layer = keymap_fn_layer(FN_INDEX(keycode)); | ||
| 56 | uint8_t key = keymap_fn_keycode(FN_INDEX(keycode)); | ||
| 57 | if (key) { | ||
| 58 | action.code = ACTION_KEYMAP_TAP_KEY(layer, key); | ||
| 59 | } else { | ||
| 60 | action.code = ACTION_KEYMAP_MOMENTARY(layer); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | return action; | ||
| 64 | default: | ||
| 65 | return keycode_to_action(keycode); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | #endif | ||
| 69 | |||
| 70 | |||
| 71 | __attribute__ ((weak)) | ||
| 72 | const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) { return MACRO_NONE; } | ||
| 73 | |||
| 74 | __attribute__ ((weak)) | ||
| 75 | void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) {} | ||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | /* translates keycode to action */ | ||
| 81 | static action_t keycode_to_action(uint8_t keycode) | ||
| 24 | { | 82 | { |
| 25 | action_t action; | 83 | action_t action; |
| 26 | switch (keycode) { | 84 | switch (keycode) { |
| @@ -51,34 +109,3 @@ action_t keymap_keycode_to_action(uint8_t keycode) | |||
| 51 | } | 109 | } |
| 52 | return action; | 110 | return action; |
| 53 | } | 111 | } |
| 54 | |||
| 55 | #ifndef NO_LEGACY_KEYMAP_SUPPORT | ||
| 56 | /* legacy support with weak reference */ | ||
| 57 | __attribute__ ((weak)) | ||
| 58 | action_t action_for_key(uint8_t layer, key_t key) | ||
| 59 | { | ||
| 60 | /* convert from legacy keycode to action */ | ||
| 61 | uint8_t keycode = keymap_get_keycode(layer, key.row, key.col); | ||
| 62 | action_t action; | ||
| 63 | switch (keycode) { | ||
| 64 | case KC_FN0 ... KC_FN31: | ||
| 65 | { | ||
| 66 | uint8_t layer = keymap_fn_layer(FN_INDEX(keycode)); | ||
| 67 | uint8_t key = keymap_fn_keycode(FN_INDEX(keycode)); | ||
| 68 | if (key) { | ||
| 69 | action.code = ACTION_LAYER_SET_TAP_KEY(layer, key); | ||
| 70 | } else { | ||
| 71 | action.code = ACTION_LAYER_SET_MOMENTARY(layer); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | return action; | ||
| 75 | default: | ||
| 76 | return keymap_keycode_to_action(keycode); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | #endif | ||
| 80 | |||
| 81 | __attribute__ ((weak)) | ||
| 82 | void action_function(keyrecord_t *event, uint8_t id, uint8_t opt) | ||
| 83 | { | ||
| 84 | } | ||
diff --git a/common/keymap.h b/common/keymap.h index 63bf14482..0c483483f 100644 --- a/common/keymap.h +++ b/common/keymap.h | |||
| @@ -23,16 +23,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 23 | #include "action.h" | 23 | #include "action.h" |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | /* translates key_t to keycode */ | 26 | #ifdef USE_KEYMAP_V2 |
| 27 | /* translates key to keycode | ||
| 28 | * layer: 0-15 for base layers | ||
| 29 | * 16-31 for overlays | ||
| 30 | */ | ||
| 27 | uint8_t keymap_key_to_keycode(uint8_t layer, key_t key); | 31 | uint8_t keymap_key_to_keycode(uint8_t layer, key_t key); |
| 28 | /* translates keycode to action */ | ||
| 29 | action_t keymap_keycode_to_action(uint8_t keycode); | ||
| 30 | /* translates Fn keycode to action */ | 32 | /* translates Fn keycode to action */ |
| 31 | action_t keymap_fn_to_action(uint8_t keycode); | 33 | action_t keymap_fn_to_action(uint8_t keycode); |
| 32 | 34 | #else | |
| 33 | 35 | #warning "You are using LEGACY KEYAMP. Consider using NEW KEYMAP." | |
| 34 | 36 | /* | |
| 35 | #ifndef NO_LEGACY_KEYMAP_SUPPORT | 37 | * legacy keymap support |
| 38 | */ | ||
| 36 | /* keycode of key */ | 39 | /* keycode of key */ |
| 37 | uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col); | 40 | uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col); |
| 38 | /* layer to move during press Fn key */ | 41 | /* layer to move during press Fn key */ |
diff --git a/common/layer_switch.c b/common/layer_switch.c new file mode 100644 index 000000000..19e286f88 --- /dev/null +++ b/common/layer_switch.c | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | #include <stdint.h> | ||
| 2 | #include "keyboard.h" | ||
| 3 | #include "action.h" | ||
| 4 | #include "debug.h" | ||
| 5 | #include "util.h" | ||
| 6 | #include "layer_switch.h" | ||
| 7 | |||
| 8 | |||
| 9 | /* | ||
| 10 | * Default Layer (0-15) | ||
| 11 | */ | ||
| 12 | uint8_t default_layer = 0; | ||
| 13 | |||
| 14 | void default_layer_set(uint8_t layer) | ||
| 15 | { | ||
| 16 | debug("default_layer_set: "); | ||
| 17 | debug_dec(default_layer); debug(" to "); | ||
| 18 | |||
| 19 | default_layer = layer; | ||
| 20 | |||
| 21 | debug_dec(default_layer); debug("\n"); | ||
| 22 | |||
| 23 | clear_keyboard_but_mods(); // To avoid stuck keys | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | /* | ||
| 28 | * Keymap Layer (0-15) | ||
| 29 | */ | ||
| 30 | uint16_t keymap_stat = 0; | ||
| 31 | |||
| 32 | /* return highest layer whose state is on */ | ||
| 33 | uint8_t keymap_get_layer(void) | ||
| 34 | { | ||
| 35 | return biton16(keymap_stat); | ||
| 36 | } | ||
| 37 | |||
| 38 | static void keymap_stat_set(uint16_t stat) | ||
| 39 | { | ||
| 40 | debug("keymap: "); | ||
| 41 | keymap_debug(); debug(" to "); | ||
| 42 | |||
| 43 | keymap_stat = stat; | ||
| 44 | |||
| 45 | keymap_debug(); debug("\n"); | ||
| 46 | |||
| 47 | clear_keyboard_but_mods(); // To avoid stuck keys | ||
| 48 | } | ||
| 49 | |||
| 50 | void keymap_clear(void) | ||
| 51 | { | ||
| 52 | keymap_stat_set(0); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | void keymap_set(uint16_t stat) | ||
| 57 | { | ||
| 58 | keymap_stat_set(stat); | ||
| 59 | } | ||
| 60 | |||
| 61 | void keymap_move(uint8_t layer) | ||
| 62 | { | ||
| 63 | keymap_stat_set(1<<layer); | ||
| 64 | } | ||
| 65 | |||
| 66 | void keymap_on(uint8_t layer) | ||
| 67 | { | ||
| 68 | keymap_stat_set(keymap_stat | (1<<layer)); | ||
| 69 | } | ||
| 70 | |||
| 71 | void keymap_off(uint8_t layer) | ||
| 72 | { | ||
| 73 | keymap_stat_set(keymap_stat & ~(1<<layer)); | ||
| 74 | } | ||
| 75 | |||
| 76 | void keymap_invert(uint8_t layer) | ||
| 77 | { | ||
| 78 | keymap_stat_set(keymap_stat ^ (1<<layer)); | ||
| 79 | } | ||
| 80 | |||
| 81 | void keymap_or(uint16_t stat) | ||
| 82 | { | ||
| 83 | keymap_stat_set(keymap_stat | stat); | ||
| 84 | } | ||
| 85 | void keymap_and(uint16_t stat) | ||
| 86 | { | ||
| 87 | keymap_stat_set(keymap_stat & stat); | ||
| 88 | } | ||
| 89 | void keymap_xor(uint16_t stat) | ||
| 90 | { | ||
| 91 | keymap_stat_set(keymap_stat ^ stat); | ||
| 92 | } | ||
| 93 | |||
| 94 | void keymap_debug(void) | ||
| 95 | { | ||
| 96 | debug_hex16(keymap_stat); debug("("); debug_dec(keymap_get_layer()); debug(")"); | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | /* | ||
| 102 | * Overlay Layer (16-31 = 0-15|0x10) | ||
| 103 | */ | ||
| 104 | uint16_t overlay_stat = 0; | ||
| 105 | |||
| 106 | /* return highest layer whose state is on */ | ||
| 107 | uint8_t overlay_get_layer(void) | ||
| 108 | { | ||
| 109 | return biton16(overlay_stat); | ||
| 110 | } | ||
| 111 | |||
| 112 | static void overlay_stat_set(uint16_t stat) | ||
| 113 | { | ||
| 114 | debug("overlay: "); | ||
| 115 | overlay_debug(); debug(" to "); | ||
| 116 | |||
| 117 | overlay_stat = stat; | ||
| 118 | |||
| 119 | overlay_debug(); debug("\n"); | ||
| 120 | |||
| 121 | clear_keyboard_but_mods(); // To avoid stuck keys | ||
| 122 | } | ||
| 123 | |||
| 124 | void overlay_clear(void) | ||
| 125 | { | ||
| 126 | overlay_stat_set(0); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | void overlay_set(uint16_t stat) | ||
| 131 | { | ||
| 132 | overlay_stat_set(stat); | ||
| 133 | } | ||
| 134 | |||
| 135 | void overlay_move(uint8_t layer) | ||
| 136 | { | ||
| 137 | overlay_stat_set(1<<layer); | ||
| 138 | } | ||
| 139 | |||
| 140 | void overlay_on(uint8_t layer) | ||
| 141 | { | ||
| 142 | overlay_stat_set(overlay_stat | (1<<layer)); | ||
| 143 | } | ||
| 144 | |||
| 145 | void overlay_off(uint8_t layer) | ||
| 146 | { | ||
| 147 | overlay_stat_set(overlay_stat & ~(1<<layer)); | ||
| 148 | } | ||
| 149 | |||
| 150 | void overlay_invert(uint8_t layer) | ||
| 151 | { | ||
| 152 | overlay_stat_set(overlay_stat ^ (1<<layer)); | ||
| 153 | } | ||
| 154 | |||
| 155 | void overlay_or(uint16_t stat) | ||
| 156 | { | ||
| 157 | overlay_stat_set(overlay_stat | stat); | ||
| 158 | } | ||
| 159 | void overlay_and(uint16_t stat) | ||
| 160 | { | ||
| 161 | overlay_stat_set(overlay_stat & stat); | ||
| 162 | } | ||
| 163 | void overlay_xor(uint16_t stat) | ||
| 164 | { | ||
| 165 | overlay_stat_set(overlay_stat ^ stat); | ||
| 166 | } | ||
| 167 | |||
| 168 | void overlay_debug(void) | ||
| 169 | { | ||
| 170 | debug_hex16(overlay_stat); debug("("); debug_dec(overlay_get_layer()); debug(")"); | ||
| 171 | } | ||
| 172 | |||
| 173 | action_t layer_switch_get_action(key_t key) | ||
| 174 | { | ||
| 175 | action_t action; | ||
| 176 | action.code = ACTION_TRANSPARENT; | ||
| 177 | |||
| 178 | /* overlay: top layer first */ | ||
| 179 | for (int8_t i = 15; i >= 0; i--) { | ||
| 180 | if (overlay_stat & (1<<i)) { | ||
| 181 | action = action_for_key(i | OVERLAY_BIT, key); | ||
| 182 | if (action.code != ACTION_TRANSPARENT) { | ||
| 183 | return action; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | /* keymap: top layer first */ | ||
| 189 | for (int8_t i = 15; i >= 0; i--) { | ||
| 190 | if (keymap_stat & (1<<i)) { | ||
| 191 | action = action_for_key(i, key); | ||
| 192 | if (action.code != ACTION_TRANSPARENT) { | ||
| 193 | return action; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | /* default layer */ | ||
| 199 | action = action_for_key(default_layer, key); | ||
| 200 | return action; | ||
| 201 | } | ||
diff --git a/common/layer_switch.h b/common/layer_switch.h new file mode 100644 index 000000000..a566ab12b --- /dev/null +++ b/common/layer_switch.h | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | /* | ||
| 2 | Copyright 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 | #ifndef LAYER_SWITCH_H | ||
| 18 | #define LAYER_SWITCH_H | ||
| 19 | |||
| 20 | #include <stdint.h> | ||
| 21 | #include "keyboard.h" | ||
| 22 | #include "action.h" | ||
| 23 | |||
| 24 | |||
| 25 | /* overlays are asigned at layer 16-31 */ | ||
| 26 | #define OVERLAY_BIT 0x10 | ||
| 27 | #define OVERLAY_MASK 0x0F | ||
| 28 | |||
| 29 | |||
| 30 | /* | ||
| 31 | * Default Layer | ||
| 32 | */ | ||
| 33 | /* base layer to fall back */ | ||
| 34 | extern uint8_t default_layer; | ||
| 35 | void default_layer_set(uint8_t layer); | ||
| 36 | |||
| 37 | |||
| 38 | /* | ||
| 39 | * Keymap Layer | ||
| 40 | */ | ||
| 41 | extern uint16_t keymap_stat; | ||
| 42 | /* return current active layer */ | ||
| 43 | uint8_t keymap_get_layer(void); | ||
| 44 | void keymap_clear(void); | ||
| 45 | void keymap_set(uint16_t stat); | ||
| 46 | void keymap_move(uint8_t layer); | ||
| 47 | void keymap_on(uint8_t layer); | ||
| 48 | void keymap_off(uint8_t layer); | ||
| 49 | void keymap_invert(uint8_t layer); | ||
| 50 | /* bitwise operation */ | ||
| 51 | void keymap_or(uint16_t stat); | ||
| 52 | void keymap_and(uint16_t stat); | ||
| 53 | void keymap_xor(uint16_t stat); | ||
| 54 | void keymap_debug(void); | ||
| 55 | |||
| 56 | |||
| 57 | /* | ||
| 58 | * Overlay Layer | ||
| 59 | */ | ||
| 60 | extern uint16_t overlay_stat; | ||
| 61 | /* return current active layer */ | ||
| 62 | uint8_t overlay_get_layer(void); | ||
| 63 | void overlay_clear(void); | ||
| 64 | void overlay_set(uint16_t stat); | ||
| 65 | void overlay_move(uint8_t layer); | ||
| 66 | void overlay_on(uint8_t layer); | ||
| 67 | void overlay_off(uint8_t layer); | ||
| 68 | void overlay_invert(uint8_t layer); | ||
| 69 | /* bitwise operation */ | ||
| 70 | void overlay_or(uint16_t stat); | ||
| 71 | void overlay_and(uint16_t stat); | ||
| 72 | void overlay_xor(uint16_t stat); | ||
| 73 | void overlay_debug(void); | ||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | /* return action depending on current layer status */ | ||
| 78 | action_t layer_switch_get_action(key_t key); | ||
| 79 | |||
| 80 | #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 | ||
| 42 | uint8_t biton(uint8_t bits) | 43 | uint8_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 | |||
| 52 | uint8_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/>. | |||
| 31 | uint8_t bitpop(uint8_t bits); | 31 | uint8_t bitpop(uint8_t bits); |
| 32 | uint8_t bitpop16(uint16_t bits); | 32 | uint8_t bitpop16(uint16_t bits); |
| 33 | uint8_t biton(uint8_t bits); | 33 | uint8_t biton(uint8_t bits); |
| 34 | uint8_t biton16(uint16_t bits); | ||
| 34 | 35 | ||
| 35 | #endif | 36 | #endif |
