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 #define COMBO_TIMER_ELAPSED -1
24 __attribute__ ((weak))
25 combo_t key_combos[COMBO_COUNT] = {
29 __attribute__ ((weak))
30 void process_combo_event(uint8_t combo_index, bool pressed) {
34 static uint8_t current_combo_index = 0;
36 static inline void send_combo(uint16_t action, bool pressed)
40 register_code16(action);
42 unregister_code16(action);
45 process_combo_event(current_combo_index, pressed);
49 #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state)
50 #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
51 #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0)
52 #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0)
53 static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record)
57 /* Find index of keycode and number of combo keys */
58 for (const uint16_t *keys = combo->keys; ;++count) {
59 uint16_t key = pgm_read_word(&keys[count]);
60 if (keycode == key) index = count;
61 if (COMBO_END == key) break;
64 /* Return if not a combo key */
65 if (-1 == (int8_t)index) return false;
67 /* The combos timer is used to signal whether the combo is active */
68 bool is_combo_active = COMBO_TIMER_ELAPSED == combo->timer ? false : true;
70 if (record->event.pressed) {
71 KEY_STATE_DOWN(index);
73 if (is_combo_active) {
74 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
75 send_combo(combo->keycode, true);
76 combo->timer = COMBO_TIMER_ELAPSED;
77 } else { /* Combo key was pressed */
78 combo->timer = timer_read();
79 #ifdef COMBO_ALLOW_ACTION_KEYS
80 combo->prev_record = *record;
82 combo->prev_key = keycode;
87 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
88 send_combo(combo->keycode, false);
91 if (is_combo_active) { /* Combo key was tapped */
92 #ifdef COMBO_ALLOW_ACTION_KEYS
93 record->event.pressed = true;
94 process_action(record, store_or_get_action(record->event.pressed, record->event.key));
95 record->event.pressed = false;
96 process_action(record, store_or_get_action(record->event.pressed, record->event.key));
98 register_code16(keycode);
99 send_keyboard_report();
100 unregister_code16(keycode);
108 if (NO_COMBO_KEYS_ARE_DOWN) {
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
136 combo->timer != COMBO_TIMER_ELAPSED &&
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->timer = COMBO_TIMER_ELAPSED;
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);