diff options
| author | Ofer Plesser <plesserofer@gmail.com> | 2016-12-16 21:50:28 +0200 |
|---|---|---|
| committer | Ofer Plesser <plesserofer@gmail.com> | 2016-12-16 21:50:28 +0200 |
| commit | 6e7cfa83b9424061914793b02757fa4ec75b356b (patch) | |
| tree | 8ddb3fd03ce5666ad75ebfd5c4abce7d3b737d86 | |
| parent | b6bf4e0dce062a535685c4e772f613252d401ed3 (diff) | |
| download | qmk_firmware-6e7cfa83b9424061914793b02757fa4ec75b356b.tar.gz qmk_firmware-6e7cfa83b9424061914793b02757fa4ec75b356b.zip | |
Refactored as well as added support for action keys in combos
| -rw-r--r-- | quantum/process_keycode/process_combo.c | 123 | ||||
| -rw-r--r-- | quantum/process_keycode/process_combo.h | 34 |
2 files changed, 89 insertions, 68 deletions
diff --git a/quantum/process_keycode/process_combo.c b/quantum/process_keycode/process_combo.c index ff7e8aba5..e2189ad98 100644 --- a/quantum/process_keycode/process_combo.c +++ b/quantum/process_keycode/process_combo.c | |||
| @@ -1,39 +1,39 @@ | |||
| 1 | #include "process_combo.h" | 1 | #include "process_combo.h" |
| 2 | #include "print.h" | 2 | #include "print.h" |
| 3 | 3 | ||
| 4 | #define SEND_KEY(key) \ | ||
| 5 | do { \ | ||
| 6 | register_code16(key); \ | ||
| 7 | send_keyboard_report(); \ | ||
| 8 | unregister_code16(key); \ | ||
| 9 | } while(0) | ||
| 10 | 4 | ||
| 11 | #define COMBO_TIMER_ELAPSED -1 | 5 | #define COMBO_TIMER_ELAPSED -1 |
| 12 | 6 | ||
| 13 | #if COMBO_TERM | ||
| 14 | #define IS_COMBO_KEY_HELD(combo) (COMBO_TIMER_ELAPSED == combo->timer ? false : true) | ||
| 15 | #define RESET_COMBO_TIMER_AND_KEY(combo) combo->timer = 0; combo->key = 0 | ||
| 16 | #else | ||
| 17 | #define IS_COMBO_KEY_HELD(combo) (true) | ||
| 18 | #define RESET_COMBO_TIMER_AND_KEY(combo) do {} while (0) | ||
| 19 | #endif | ||
| 20 | |||
| 21 | 7 | ||
| 22 | __attribute__ ((weak)) | 8 | __attribute__ ((weak)) |
| 23 | combo_t key_combos[COMBO_COUNT] = { | 9 | combo_t key_combos[] = { |
| 24 | 10 | ||
| 25 | }; | 11 | }; |
| 26 | 12 | ||
| 27 | static inline void reset_combo(combo_t *combo) | 13 | __attribute__ ((weak)) |
| 14 | void process_combo_event(uint8_t combo_index, bool pressed) { | ||
| 15 | |||
| 16 | } | ||
| 17 | |||
| 18 | static uint8_t current_combo_index = 0; | ||
| 19 | |||
| 20 | static inline void send_combo(uint16_t action, bool pressed) | ||
| 28 | { | 21 | { |
| 29 | combo->state = 0; | 22 | if (action) { |
| 30 | RESET_COMBO_TIMER_AND_KEY(combo); | 23 | if (pressed) { |
| 24 | register_code16(action); | ||
| 25 | } else { | ||
| 26 | unregister_code16(action); | ||
| 27 | } | ||
| 28 | } else { | ||
| 29 | process_combo_event(current_combo_index, pressed); | ||
| 30 | } | ||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state) | 33 | #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state) |
| 34 | #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) | 34 | #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) |
| 35 | #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0) | 35 | #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0) |
| 36 | #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0) | 36 | #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0) |
| 37 | static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) | 37 | static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) |
| 38 | { | 38 | { |
| 39 | uint8_t count = 0; | 39 | uint8_t count = 0; |
| @@ -46,42 +46,51 @@ static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t * | |||
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | /* Return if not a combo key */ | 48 | /* Return if not a combo key */ |
| 49 | if (-1 == index) return false; | 49 | if (-1 == (int8_t)index) return false; |
| 50 | 50 | ||
| 51 | bool is_combo_active = IS_COMBO_KEY_HELD(combo); | 51 | /* The combos timer is used to signal whether the combo is active */ |
| 52 | bool is_combo_active = COMBO_TIMER_ELAPSED == combo->timer ? false : true; | ||
| 52 | 53 | ||
| 53 | if (record->event.pressed) { | 54 | if (record->event.pressed) { |
| 54 | KEY_STATE_DOWN(index); | 55 | KEY_STATE_DOWN(index); |
| 55 | 56 | ||
| 56 | #if COMBO_TERM | ||
| 57 | if (is_combo_active) { | 57 | if (is_combo_active) { |
| 58 | combo->timer = timer_read(); | 58 | if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */ |
| 59 | combo->key = keycode; | 59 | send_combo(combo->keycode, true); |
| 60 | } | 60 | combo->timer = COMBO_TIMER_ELAPSED; |
| 61 | } else { /* Combo key was pressed */ | ||
| 62 | combo->timer = timer_read(); | ||
| 63 | #ifdef COMBO_ALLOW_ACTION_KEYS | ||
| 64 | combo->prev_record = *record; | ||
| 65 | #else | ||
| 66 | combo->prev_key = keycode; | ||
| 61 | #endif | 67 | #endif |
| 62 | 68 | } | |
| 69 | } | ||
| 63 | } else { | 70 | } else { |
| 64 | if (is_combo_active && combo->state) { /* Combo key was tapped */ | 71 | if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */ |
| 65 | RESET_COMBO_TIMER_AND_KEY(combo); | 72 | send_combo(combo->keycode, false); |
| 66 | SEND_KEY(keycode); | ||
| 67 | } | 73 | } |
| 68 | 74 | ||
| 69 | #if COMBO_TERM | 75 | if (is_combo_active) { /* Combo key was tapped */ |
| 70 | if (!is_combo_active && keycode == combo->key) { /* Held combo key was released */ | 76 | #ifdef COMBO_ALLOW_ACTION_KEYS |
| 71 | unregister_code16(combo->key); | 77 | record->event.pressed = true; |
| 72 | } | 78 | process_action(record, store_or_get_action(record->event.pressed, record->event.key)); |
| 79 | record->event.pressed = false; | ||
| 80 | process_action(record, store_or_get_action(record->event.pressed, record->event.key)); | ||
| 81 | #else | ||
| 82 | register_code16(keycode); | ||
| 83 | send_keyboard_report(); | ||
| 84 | unregister_code16(keycode); | ||
| 73 | #endif | 85 | #endif |
| 86 | combo->timer = 0; | ||
| 87 | } | ||
| 74 | 88 | ||
| 75 | KEY_STATE_UP(index); | 89 | KEY_STATE_UP(index); |
| 76 | } | 90 | } |
| 77 | 91 | ||
| 78 | if (ALL_COMBO_KEYS_ARE_DOWN && is_combo_active) { | 92 | if (NO_COMBO_KEYS_ARE_DOWN) { |
| 79 | SEND_KEY(combo->action); | 93 | combo->timer = 0; |
| 80 | reset_combo(combo); | ||
| 81 | } | ||
| 82 | |||
| 83 | if(NO_COMBO_KEYS_ARE_DOWN && !is_combo_active) { | ||
| 84 | reset_combo(combo); | ||
| 85 | } | 94 | } |
| 86 | 95 | ||
| 87 | return is_combo_active; | 96 | return is_combo_active; |
| @@ -91,8 +100,8 @@ bool process_combo(uint16_t keycode, keyrecord_t *record) | |||
| 91 | { | 100 | { |
| 92 | bool is_combo_key = false; | 101 | bool is_combo_key = false; |
| 93 | 102 | ||
| 94 | for (int i = 0; i < COMBO_COUNT; ++i) { | 103 | for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) { |
| 95 | combo_t *combo = &key_combos[i]; | 104 | combo_t *combo = &key_combos[current_combo_index]; |
| 96 | is_combo_key |= process_single_combo(combo, keycode, record); | 105 | is_combo_key |= process_single_combo(combo, keycode, record); |
| 97 | } | 106 | } |
| 98 | 107 | ||
| @@ -101,17 +110,25 @@ bool process_combo(uint16_t keycode, keyrecord_t *record) | |||
| 101 | 110 | ||
| 102 | void matrix_scan_combo(void) | 111 | void matrix_scan_combo(void) |
| 103 | { | 112 | { |
| 104 | #if COMBO_TERM | ||
| 105 | for (int i = 0; i < COMBO_COUNT; ++i) { | 113 | for (int i = 0; i < COMBO_COUNT; ++i) { |
| 106 | combo_t *combo = &key_combos[i]; | 114 | combo_t *combo = &key_combos[i]; |
| 107 | if (combo->timer && | 115 | if (combo->timer && |
| 108 | combo->timer != COMBO_TIMER_ELAPSED && | 116 | combo->timer != COMBO_TIMER_ELAPSED && |
| 109 | timer_elapsed(combo->timer) > COMBO_TERM) { | 117 | timer_elapsed(combo->timer) > COMBO_TERM) { |
| 110 | 118 | ||
| 119 | /* This disables the combo, meaning key events for this | ||
| 120 | * combo will be handled by the next processors in the chain | ||
| 121 | */ | ||
| 111 | combo->timer = COMBO_TIMER_ELAPSED; | 122 | combo->timer = COMBO_TIMER_ELAPSED; |
| 112 | unregister_code16(combo->key); | 123 | |
| 113 | register_code16(combo->key); | 124 | #ifdef COMBO_ALLOW_ACTION_KEYS |
| 125 | process_action(&combo->prev_record, | ||
| 126 | store_or_get_action(combo->prev_record.event.pressed, | ||
| 127 | combo->prev_record.event.key)); | ||
| 128 | #else | ||
| 129 | unregister_code16(combo->prev_key); | ||
| 130 | register_code16(combo->prev_key); | ||
| 131 | #endif | ||
| 114 | } | 132 | } |
| 115 | } | 133 | } |
| 116 | #endif | 134 | } |
| 117 | } \ No newline at end of file | ||
diff --git a/quantum/process_keycode/process_combo.h b/quantum/process_keycode/process_combo.h index c475acd33..847f2b737 100644 --- a/quantum/process_keycode/process_combo.h +++ b/quantum/process_keycode/process_combo.h | |||
| @@ -5,35 +5,39 @@ | |||
| 5 | #include "progmem.h" | 5 | #include "progmem.h" |
| 6 | #include "quantum.h" | 6 | #include "quantum.h" |
| 7 | 7 | ||
| 8 | #ifndef COMBO_TERM | ||
| 9 | #define COMBO_TERM TAPPING_TERM | ||
| 10 | #endif | ||
| 11 | |||
| 12 | typedef struct | 8 | typedef struct |
| 13 | { | 9 | { |
| 14 | const uint16_t *keys; | 10 | const uint16_t *keys; |
| 15 | uint16_t action; | 11 | uint16_t keycode; |
| 12 | #ifdef EXTRA_EXTRA_LONG_COMBOS | ||
| 16 | uint32_t state; | 13 | uint32_t state; |
| 17 | #if COMBO_TERM | 14 | #elif EXTRA_LONG_COMBOS |
| 15 | uint16_t state; | ||
| 16 | #else | ||
| 17 | uint8_t state; | ||
| 18 | #endif | ||
| 18 | uint16_t timer; | 19 | uint16_t timer; |
| 19 | uint16_t key; | 20 | #ifdef COMBO_ALLOW_ACTION_KEYS |
| 21 | keyrecord_t prev_record; | ||
| 22 | #else | ||
| 23 | uint16_t prev_key; | ||
| 20 | #endif | 24 | #endif |
| 21 | } combo_t; | 25 | } combo_t; |
| 22 | 26 | ||
| 23 | 27 | ||
| 24 | #if COMBO_TERM | 28 | #define COMBO(ck, ca) {.keys = &(ck)[0], .keycode = (ca)} |
| 25 | #define COMBO(ck, ca) {.keys = &(ck)[0], .action = (ca), .state = 0, .timer = 0, .key = 0} | 29 | #define COMBO_ACTION(ck) {.keys = &(ck)[0]} |
| 26 | #else | 30 | |
| 27 | #define COMBO(ck, ca) {.keys = &(ck)[0], .action = (ca), .state = 0 } | ||
| 28 | #endif | ||
| 29 | #define COMBO_END 0 | 31 | #define COMBO_END 0 |
| 30 | #ifndef COMBO_COUNT | 32 | #ifndef COMBO_COUNT |
| 31 | #define COMBO_COUNT 0 | 33 | #define COMBO_COUNT 0 |
| 32 | #endif | 34 | #endif |
| 33 | 35 | #ifndef COMBO_TERM | |
| 34 | extern combo_t key_combos[COMBO_COUNT]; | 36 | #define COMBO_TERM TAPPING_TERM |
| 37 | #endif | ||
| 35 | 38 | ||
| 36 | bool process_combo(uint16_t keycode, keyrecord_t *record); | 39 | bool process_combo(uint16_t keycode, keyrecord_t *record); |
| 37 | void matrix_scan_combo(void); | 40 | void matrix_scan_combo(void); |
| 41 | void process_combo_event(uint8_t combo_index, bool pressed); | ||
| 38 | 42 | ||
| 39 | #endif \ No newline at end of file | 43 | #endif |
