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;
32 static uint8_t buffer_size = 0;
33 #ifdef COMBO_ALLOW_ACTION_KEYS
34 static keyrecord_t key_buffer[MAX_COMBO_LENGTH];
36 static uint16_t key_buffer[MAX_COMBO_LENGTH];
39 static inline void send_combo(uint16_t action, bool pressed) {
42 register_code16(action);
44 unregister_code16(action);
47 process_combo_event(current_combo_index, pressed);
51 static inline void dump_key_buffer(bool emit) {
52 if (buffer_size == 0) {
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,
60 key_buffer[i].event.key);
61 process_action(&(key_buffer[i]), action);
63 register_code16(key_buffer[i]);
64 send_keyboard_report();
72 #define ALL_COMBO_KEYS_ARE_DOWN (((1 << count) - 1) == combo->state)
73 #define KEY_STATE_DOWN(key) \
75 combo->state |= (1 << key); \
77 #define KEY_STATE_UP(key) \
79 combo->state &= ~(1 << key); \
82 static bool process_single_combo(combo_t *combo, uint16_t keycode,
83 keyrecord_t *record) {
86 /* Find index of keycode and number of combo keys */
87 for (const uint16_t *keys = combo->keys;; ++count) {
88 uint16_t key = pgm_read_word(&keys[count]);
95 /* Continue processing if not a combo key */
96 if (-1 == (int8_t)index)
99 bool is_combo_active = is_active;
101 if (record->event.pressed) {
102 KEY_STATE_DOWN(index);
104 if (is_combo_active) {
105 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
106 send_combo(combo->keycode, true);
111 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
112 send_combo(combo->keycode, false);
114 /* continue processing without immediately returning */
115 is_combo_active = false;
121 return is_combo_active;
124 #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
126 bool process_combo(uint16_t keycode, keyrecord_t *record) {
127 bool is_combo_key = false;
129 bool no_combo_keys_pressed = true;
131 for (current_combo_index = 0; current_combo_index < COMBO_COUNT;
132 ++current_combo_index) {
133 combo_t *combo = &key_combos[current_combo_index];
134 is_combo_key |= process_single_combo(combo, keycode, record);
135 no_combo_keys_pressed = no_combo_keys_pressed && NO_COMBO_KEYS_ARE_DOWN;
139 /* buffer is only dropped when we complete a combo, so we refresh the timer
141 timer = timer_read();
142 dump_key_buffer(false);
143 } else if (!is_combo_key) {
144 /* if no combos claim the key we need to emit the keybuffer */
145 dump_key_buffer(true);
147 // reset state if there are no combo keys pressed at all
148 if (no_combo_keys_pressed) {
152 } else if (record->event.pressed && is_active) {
153 /* otherwise the key is consumed and placed in the buffer */
154 timer = timer_read();
156 if (buffer_size < MAX_COMBO_LENGTH) {
157 #ifdef COMBO_ALLOW_ACTION_KEYS
158 key_buffer[buffer_size++] = *record;
160 key_buffer[buffer_size++] = keycode;
165 return !is_combo_key;
168 void matrix_scan_combo(void) {
169 if (is_active && timer && timer_elapsed(timer) > COMBO_TERM) {
171 /* This disables the combo, meaning key events for this
172 * combo will be handled by the next processors in the chain
175 dump_key_buffer(true);