1 #include "process_combo.h"
5 #define COMBO_TIMER_ELAPSED -1
9 combo_t key_combos[] = {
13 __attribute__ ((weak))
14 void process_combo_event(uint8_t combo_index, bool pressed) {
18 static uint8_t current_combo_index = 0;
20 static inline void send_combo(uint16_t action, bool pressed)
24 register_code16(action);
26 unregister_code16(action);
29 process_combo_event(current_combo_index, pressed);
33 #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state)
34 #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
35 #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0)
36 #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0)
37 static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record)
41 /* Find index of keycode and number of combo keys */
42 for (const uint16_t *keys = combo->keys; ;++count) {
43 uint16_t key = pgm_read_word(&keys[count]);
44 if (keycode == key) index = count;
45 if (COMBO_END == key) break;
48 /* Return if not a combo key */
49 if (-1 == (int8_t)index) return false;
51 /* The combos timer is used to signal whether the combo is active */
52 bool is_combo_active = COMBO_TIMER_ELAPSED == combo->timer ? false : true;
54 if (record->event.pressed) {
55 KEY_STATE_DOWN(index);
57 if (is_combo_active) {
58 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
59 send_combo(combo->keycode, true);
60 combo->timer = COMBO_TIMER_ELAPSED;
61 } else { /* Combo key was pressed */
62 combo->timer = timer_read();
63 #ifdef COMBO_ALLOW_ACTION_KEYS
64 combo->prev_record = *record;
66 combo->prev_key = keycode;
71 if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
72 send_combo(combo->keycode, false);
75 if (is_combo_active) { /* Combo key was tapped */
76 #ifdef COMBO_ALLOW_ACTION_KEYS
77 record->event.pressed = true;
78 process_action(record, store_or_get_action(record->event.pressed, record->event.key));
79 record->event.pressed = false;
80 process_action(record, store_or_get_action(record->event.pressed, record->event.key));
82 register_code16(keycode);
83 send_keyboard_report();
84 unregister_code16(keycode);
92 if (NO_COMBO_KEYS_ARE_DOWN) {
96 return is_combo_active;
99 bool process_combo(uint16_t keycode, keyrecord_t *record)
101 bool is_combo_key = false;
103 for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) {
104 combo_t *combo = &key_combos[current_combo_index];
105 is_combo_key |= process_single_combo(combo, keycode, record);
108 return !is_combo_key;
111 void matrix_scan_combo(void)
113 for (int i = 0; i < COMBO_COUNT; ++i) {
114 combo_t *combo = &key_combos[i];
116 combo->timer != COMBO_TIMER_ELAPSED &&
117 timer_elapsed(combo->timer) > COMBO_TERM) {
119 /* This disables the combo, meaning key events for this
120 * combo will be handled by the next processors in the chain
122 combo->timer = COMBO_TIMER_ELAPSED;
124 #ifdef COMBO_ALLOW_ACTION_KEYS
125 process_action(&combo->prev_record,
126 store_or_get_action(combo->prev_record.event.pressed,
127 combo->prev_record.event.key));
129 unregister_code16(combo->prev_key);
130 register_code16(combo->prev_key);