1 /* Copyright 2016 Jack Humbert
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.
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.
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/>.
18 #include "process_combo.h"
20 __attribute__((weak)) combo_t key_combos[COMBO_COUNT] = {
24 __attribute__((weak)) void process_combo_event(uint8_t combo_index,
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
33 static uint8_t buffer_size = 0;
34 #ifdef COMBO_ALLOW_ACTION_KEYS
35 static keyrecord_t key_buffer[MAX_COMBO_LENGTH];
37 static uint16_t key_buffer[MAX_COMBO_LENGTH];
40 static inline void send_combo(uint16_t action, bool pressed) {
43 register_code16(action);
45 unregister_code16(action);
48 process_combo_event(current_combo_index, pressed);
52 static inline void dump_key_buffer(bool emit) {
53 if (buffer_size == 0) {
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);
64 register_code16(key_buffer[i]);
65 send_keyboard_report();
73 #define ALL_COMBO_KEYS_ARE_DOWN (((1 << count) - 1) == combo->state)
74 #define KEY_STATE_DOWN(key) \
76 combo->state |= (1 << key); \
78 #define KEY_STATE_UP(key) \
80 combo->state &= ~(1 << key); \
83 static bool process_single_combo(combo_t *combo, uint16_t keycode,
84 keyrecord_t *record) {
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]);
96 /* Continue processing if not a combo key */
97 if (-1 == (int8_t)index)
100 bool is_combo_active = is_active;
102 if (record->event.pressed) {
103 KEY_STATE_DOWN(index);
105 if (is_combo_active) {
106 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
107 send_combo(combo->keycode, true);
112 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
113 send_combo(combo->keycode, false);
115 /* continue processing without immediately returning */
116 is_combo_active = false;
122 return is_combo_active;
125 #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
127 bool process_combo(uint16_t keycode, keyrecord_t *record) {
128 bool is_combo_key = false;
130 bool no_combo_keys_pressed = true;
132 if (keycode == CMB_ON && record->event.pressed) {
137 if (keycode == CMB_OFF && record->event.pressed) {
142 if (keycode == CMB_TOG && record->event.pressed) {
147 if (!is_combo_enabled()) { return true; }
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;
157 /* buffer is only dropped when we complete a combo, so we refresh the timer
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);
165 // reset state if there are no combo keys pressed at all
166 if (no_combo_keys_pressed) {
170 } else if (record->event.pressed && is_active) {
171 /* otherwise the key is consumed and placed in the buffer */
172 timer = timer_read();
174 if (buffer_size < MAX_COMBO_LENGTH) {
175 #ifdef COMBO_ALLOW_ACTION_KEYS
176 key_buffer[buffer_size++] = *record;
178 key_buffer[buffer_size++] = keycode;
183 return !is_combo_key;
186 void matrix_scan_combo(void) {
187 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
193 dump_key_buffer(true);
197 void combo_enable(void) {
198 b_combo_enable = true;
201 void combo_disable(void) {
202 b_combo_enable = is_active = false;
204 dump_key_buffer(true);
208 void combo_toggle(void) {
209 if (b_combo_enable) {
216 bool is_combo_enabled(void) {
217 return b_combo_enable;