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/>.
17 #include "process_combo.h"
21 __attribute__ ((weak))
22 combo_t key_combos[COMBO_COUNT] = {
26 __attribute__ ((weak))
27 void process_combo_event(uint8_t combo_index, bool pressed) {
31 static uint8_t current_combo_index = 0;
33 static inline void send_combo(uint16_t action, bool pressed)
37 register_code16(action);
39 unregister_code16(action);
42 process_combo_event(current_combo_index, pressed);
46 #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state)
47 #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
48 #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0)
49 #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0)
50 static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record)
54 /* Find index of keycode and number of combo keys */
55 for (const uint16_t *keys = combo->keys; ;++count) {
56 uint16_t key = pgm_read_word(&keys[count]);
57 if (keycode == key) index = count;
58 if (COMBO_END == key) break;
61 /* Return if not a combo key */
62 if (-1 == (int8_t)index) return false;
64 /* The combos timer is used to signal whether the combo is active */
65 bool is_combo_active = combo->is_active;
67 if (record->event.pressed) {
68 KEY_STATE_DOWN(index);
70 if (is_combo_active) {
71 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
72 send_combo(combo->keycode, true);
73 combo->is_active = false;
74 } else { /* Combo key was pressed */
75 combo->timer = timer_read();
76 combo->is_active = true;
77 #ifdef COMBO_ALLOW_ACTION_KEYS
78 combo->prev_record = *record;
80 combo->prev_key = keycode;
85 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
86 send_combo(combo->keycode, false);
89 if (is_combo_active) { /* Combo key was tapped */
90 #ifdef COMBO_ALLOW_ACTION_KEYS
91 record->event.pressed = true;
92 process_action(record, store_or_get_action(record->event.pressed, record->event.key));
93 record->event.pressed = false;
94 process_action(record, store_or_get_action(record->event.pressed, record->event.key));
96 register_code16(keycode);
97 send_keyboard_report();
98 unregister_code16(keycode);
100 combo->is_active = false;
107 if (NO_COMBO_KEYS_ARE_DOWN) {
108 combo->is_active = true;
112 return is_combo_active;
115 bool process_combo(uint16_t keycode, keyrecord_t *record)
117 bool is_combo_key = false;
119 for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) {
120 combo_t *combo = &key_combos[current_combo_index];
121 is_combo_key |= process_single_combo(combo, keycode, record);
124 return !is_combo_key;
127 void matrix_scan_combo(void)
129 for (int i = 0; i < COMBO_COUNT; ++i) {
130 // Do not treat the (weak) key_combos too strict.
131 #pragma GCC diagnostic push
132 #pragma GCC diagnostic ignored "-Warray-bounds"
133 combo_t *combo = &key_combos[i];
134 #pragma GCC diagnostic pop
135 if (combo->is_active &&
137 timer_elapsed(combo->timer) > COMBO_TERM) {
139 /* This disables the combo, meaning key events for this
140 * combo will be handled by the next processors in the chain
142 combo->is_active = false;
144 #ifdef COMBO_ALLOW_ACTION_KEYS
145 process_action(&combo->prev_record,
146 store_or_get_action(combo->prev_record.event.pressed,
147 combo->prev_record.event.key));
149 unregister_code16(combo->prev_key);
150 register_code16(combo->prev_key);