Variable combo (#8120)
[jackhill/qmk/firmware.git] / quantum / process_keycode / process_combo.c
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 "print.h"
18 #include "process_combo.h"
19
20 #ifndef COMBO_VARIABLE_LEN
21 __attribute__((weak)) combo_t key_combos[COMBO_COUNT] = {};
22 #else
23 extern combo_t key_combos[];
24 extern int COMBO_LEN;
25 #endif
26
27 __attribute__((weak)) void process_combo_event(uint8_t combo_index, bool pressed) {}
28
29 static uint16_t timer = 0;
30 static uint8_t current_combo_index = 0;
31 static bool drop_buffer = false;
32 static bool is_active = false;
33 static bool b_combo_enable = true; // defaults to enabled
34
35 static uint8_t buffer_size = 0;
36 #ifdef COMBO_ALLOW_ACTION_KEYS
37 static keyrecord_t key_buffer[MAX_COMBO_LENGTH];
38 #else
39 static uint16_t key_buffer[MAX_COMBO_LENGTH];
40 #endif
41
42 static inline void send_combo(uint16_t action, bool pressed) {
43 if (action) {
44 if (pressed) {
45 register_code16(action);
46 } else {
47 unregister_code16(action);
48 }
49 } else {
50 process_combo_event(current_combo_index, pressed);
51 }
52 }
53
54 static inline void dump_key_buffer(bool emit) {
55 if (buffer_size == 0) {
56 return;
57 }
58
59 if (emit) {
60 for (uint8_t i = 0; i < buffer_size; i++) {
61 #ifdef COMBO_ALLOW_ACTION_KEYS
62 const action_t action = store_or_get_action(key_buffer[i].event.pressed, key_buffer[i].event.key);
63 process_action(&(key_buffer[i]), action);
64 #else
65 register_code16(key_buffer[i]);
66 send_keyboard_report();
67 #endif
68 }
69 }
70
71 buffer_size = 0;
72 }
73
74 #define ALL_COMBO_KEYS_ARE_DOWN (((1 << count) - 1) == combo->state)
75 #define KEY_STATE_DOWN(key) \
76 do { \
77 combo->state |= (1 << key); \
78 } while (0)
79 #define KEY_STATE_UP(key) \
80 do { \
81 combo->state &= ~(1 << key); \
82 } while (0)
83
84 static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) {
85 uint8_t count = 0;
86 uint8_t index = -1;
87 /* Find index of keycode and number of combo keys */
88 for (const uint16_t *keys = combo->keys;; ++count) {
89 uint16_t key = pgm_read_word(&keys[count]);
90 if (keycode == key) index = count;
91 if (COMBO_END == key) break;
92 }
93
94 /* Continue processing if not a combo key */
95 if (-1 == (int8_t)index) return false;
96
97 bool is_combo_active = is_active;
98
99 if (record->event.pressed) {
100 KEY_STATE_DOWN(index);
101
102 if (is_combo_active) {
103 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
104 send_combo(combo->keycode, true);
105 drop_buffer = true;
106 }
107 }
108 } else {
109 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
110 send_combo(combo->keycode, false);
111 } else {
112 /* continue processing without immediately returning */
113 is_combo_active = false;
114 }
115
116 KEY_STATE_UP(index);
117 }
118
119 return is_combo_active;
120 }
121
122 #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
123
124 bool process_combo(uint16_t keycode, keyrecord_t *record) {
125 bool is_combo_key = false;
126 drop_buffer = false;
127 bool no_combo_keys_pressed = true;
128
129 if (keycode == CMB_ON && record->event.pressed) {
130 combo_enable();
131 return true;
132 }
133
134 if (keycode == CMB_OFF && record->event.pressed) {
135 combo_disable();
136 return true;
137 }
138
139 if (keycode == CMB_TOG && record->event.pressed) {
140 combo_toggle();
141 return true;
142 }
143
144 if (!is_combo_enabled()) {
145 return true;
146 }
147 #ifndef COMBO_VARIABLE_LEN
148 for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) {
149 #else
150 for (current_combo_index = 0; current_combo_index < COMBO_LEN; ++current_combo_index) {
151 #endif
152 combo_t *combo = &key_combos[current_combo_index];
153 is_combo_key |= process_single_combo(combo, keycode, record);
154 no_combo_keys_pressed = no_combo_keys_pressed && NO_COMBO_KEYS_ARE_DOWN;
155 }
156
157 if (drop_buffer) {
158 /* buffer is only dropped when we complete a combo, so we refresh the timer
159 * here */
160 timer = timer_read();
161 dump_key_buffer(false);
162 } else if (!is_combo_key) {
163 /* if no combos claim the key we need to emit the keybuffer */
164 dump_key_buffer(true);
165
166 // reset state if there are no combo keys pressed at all
167 if (no_combo_keys_pressed) {
168 timer = 0;
169 is_active = true;
170 }
171 } else if (record->event.pressed && is_active) {
172 /* otherwise the key is consumed and placed in the buffer */
173 timer = timer_read();
174
175 if (buffer_size < MAX_COMBO_LENGTH) {
176 #ifdef COMBO_ALLOW_ACTION_KEYS
177 key_buffer[buffer_size++] = *record;
178 #else
179 key_buffer[buffer_size++] = keycode;
180 #endif
181 }
182 }
183
184 return !is_combo_key;
185 }
186
187 void matrix_scan_combo(void) {
188 if (b_combo_enable && is_active && timer && timer_elapsed(timer) > COMBO_TERM) {
189 /* This disables the combo, meaning key events for this
190 * combo will be handled by the next processors in the chain
191 */
192 is_active = false;
193 dump_key_buffer(true);
194 }
195 }
196
197 void combo_enable(void) { b_combo_enable = true; }
198
199 void combo_disable(void) {
200 b_combo_enable = is_active = false;
201 timer = 0;
202 dump_key_buffer(true);
203 }
204
205 void combo_toggle(void) {
206 if (b_combo_enable) {
207 combo_disable();
208 } else {
209 combo_enable();
210 }
211 }
212
213 bool is_combo_enabled(void) { return b_combo_enable; }