diff options
Diffstat (limited to 'quantum/process_keycode/process_combo.c')
| -rw-r--r-- | quantum/process_keycode/process_combo.c | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/quantum/process_keycode/process_combo.c b/quantum/process_keycode/process_combo.c new file mode 100644 index 000000000..58d45add2 --- /dev/null +++ b/quantum/process_keycode/process_combo.c | |||
| @@ -0,0 +1,150 @@ | |||
| 1 | /* Copyright 2016 Jack Humbert | ||
| 2 | * | ||
| 3 | * This program is free software: you can redistribute it and/or modify | ||
| 4 | * it under the terms of the GNU General Public License as published by | ||
| 5 | * the Free Software Foundation, either version 2 of the License, or | ||
| 6 | * (at your option) any later version. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope that it will be useful, | ||
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | * GNU General Public License for more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "process_combo.h" | ||
| 18 | #include "print.h" | ||
| 19 | |||
| 20 | |||
| 21 | #define COMBO_TIMER_ELAPSED -1 | ||
| 22 | |||
| 23 | |||
| 24 | __attribute__ ((weak)) | ||
| 25 | combo_t key_combos[] = { | ||
| 26 | |||
| 27 | }; | ||
| 28 | |||
| 29 | __attribute__ ((weak)) | ||
| 30 | void process_combo_event(uint8_t combo_index, bool pressed) { | ||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | static uint8_t current_combo_index = 0; | ||
| 35 | |||
| 36 | static inline void send_combo(uint16_t action, bool pressed) | ||
| 37 | { | ||
| 38 | if (action) { | ||
| 39 | if (pressed) { | ||
| 40 | register_code16(action); | ||
| 41 | } else { | ||
| 42 | unregister_code16(action); | ||
| 43 | } | ||
| 44 | } else { | ||
| 45 | process_combo_event(current_combo_index, pressed); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state) | ||
| 50 | #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) | ||
| 51 | #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0) | ||
| 52 | #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0) | ||
| 53 | static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) | ||
| 54 | { | ||
| 55 | uint8_t count = 0; | ||
| 56 | uint8_t index = -1; | ||
| 57 | /* Find index of keycode and number of combo keys */ | ||
| 58 | for (const uint16_t *keys = combo->keys; ;++count) { | ||
| 59 | uint16_t key = pgm_read_word(&keys[count]); | ||
| 60 | if (keycode == key) index = count; | ||
| 61 | if (COMBO_END == key) break; | ||
| 62 | } | ||
| 63 | |||
| 64 | /* Return if not a combo key */ | ||
| 65 | if (-1 == (int8_t)index) return false; | ||
| 66 | |||
| 67 | /* The combos timer is used to signal whether the combo is active */ | ||
| 68 | bool is_combo_active = COMBO_TIMER_ELAPSED == combo->timer ? false : true; | ||
| 69 | |||
| 70 | if (record->event.pressed) { | ||
| 71 | KEY_STATE_DOWN(index); | ||
| 72 | |||
| 73 | if (is_combo_active) { | ||
| 74 | if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */ | ||
| 75 | send_combo(combo->keycode, true); | ||
| 76 | combo->timer = COMBO_TIMER_ELAPSED; | ||
| 77 | } else { /* Combo key was pressed */ | ||
| 78 | combo->timer = timer_read(); | ||
| 79 | #ifdef COMBO_ALLOW_ACTION_KEYS | ||
| 80 | combo->prev_record = *record; | ||
| 81 | #else | ||
| 82 | combo->prev_key = keycode; | ||
| 83 | #endif | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */ | ||
| 88 | send_combo(combo->keycode, false); | ||
| 89 | } | ||
| 90 | |||
| 91 | if (is_combo_active) { /* Combo key was tapped */ | ||
| 92 | #ifdef COMBO_ALLOW_ACTION_KEYS | ||
| 93 | record->event.pressed = true; | ||
| 94 | process_action(record, store_or_get_action(record->event.pressed, record->event.key)); | ||
| 95 | record->event.pressed = false; | ||
| 96 | process_action(record, store_or_get_action(record->event.pressed, record->event.key)); | ||
| 97 | #else | ||
| 98 | register_code16(keycode); | ||
| 99 | send_keyboard_report(); | ||
| 100 | unregister_code16(keycode); | ||
| 101 | #endif | ||
| 102 | combo->timer = 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | KEY_STATE_UP(index); | ||
| 106 | } | ||
| 107 | |||
| 108 | if (NO_COMBO_KEYS_ARE_DOWN) { | ||
| 109 | combo->timer = 0; | ||
| 110 | } | ||
| 111 | |||
| 112 | return is_combo_active; | ||
| 113 | } | ||
| 114 | |||
| 115 | bool process_combo(uint16_t keycode, keyrecord_t *record) | ||
| 116 | { | ||
| 117 | bool is_combo_key = false; | ||
| 118 | |||
| 119 | for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) { | ||
| 120 | combo_t *combo = &key_combos[current_combo_index]; | ||
| 121 | is_combo_key |= process_single_combo(combo, keycode, record); | ||
| 122 | } | ||
| 123 | |||
| 124 | return !is_combo_key; | ||
| 125 | } | ||
| 126 | |||
| 127 | void matrix_scan_combo(void) | ||
| 128 | { | ||
| 129 | for (int i = 0; i < COMBO_COUNT; ++i) { | ||
| 130 | combo_t *combo = &key_combos[i]; | ||
| 131 | if (combo->timer && | ||
| 132 | combo->timer != COMBO_TIMER_ELAPSED && | ||
| 133 | timer_elapsed(combo->timer) > COMBO_TERM) { | ||
| 134 | |||
| 135 | /* This disables the combo, meaning key events for this | ||
| 136 | * combo will be handled by the next processors in the chain | ||
| 137 | */ | ||
| 138 | combo->timer = COMBO_TIMER_ELAPSED; | ||
| 139 | |||
| 140 | #ifdef COMBO_ALLOW_ACTION_KEYS | ||
| 141 | process_action(&combo->prev_record, | ||
| 142 | store_or_get_action(combo->prev_record.event.pressed, | ||
| 143 | combo->prev_record.event.key)); | ||
| 144 | #else | ||
| 145 | unregister_code16(combo->prev_key); | ||
| 146 | register_code16(combo->prev_key); | ||
| 147 | #endif | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
