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