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