1 #include "process_combo.h"
4 #define SEND_KEY(key) \
6 register_code16(key); \
7 send_keyboard_report(); \
8 unregister_code16(key); \
11 #define COMBO_TIMER_ELAPSED -1
14 #define IS_COMBO_KEY_HELD(combo) (COMBO_TIMER_ELAPSED == combo->timer ? false : true)
15 #define RESET_COMBO_TIMER_AND_KEY(combo) combo->timer = 0; combo->key = 0
17 #define IS_COMBO_KEY_HELD(combo) (true)
18 #define RESET_COMBO_TIMER_AND_KEY(combo) do {} while (0)
22 __attribute__ ((weak))
23 combo_t key_combos[COMBO_COUNT] = {
27 static inline void reset_combo(combo_t *combo)
30 RESET_COMBO_TIMER_AND_KEY(combo);
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 == index) return false;
51 bool is_combo_active = IS_COMBO_KEY_HELD(combo);
53 if (record->event.pressed) {
54 KEY_STATE_DOWN(index);
57 if (is_combo_active) {
58 combo->timer = timer_read();
64 if (is_combo_active && combo->state) { /* Combo key was tapped */
65 RESET_COMBO_TIMER_AND_KEY(combo);
70 if (!is_combo_active && keycode == combo->key) { /* Held combo key was released */
71 unregister_code16(combo->key);
78 if (ALL_COMBO_KEYS_ARE_DOWN && is_combo_active) {
79 SEND_KEY(combo->action);
83 if(NO_COMBO_KEYS_ARE_DOWN && !is_combo_active) {
87 return is_combo_active;
90 bool process_combo(uint16_t keycode, keyrecord_t *record)
92 bool is_combo_key = false;
94 for (int i = 0; i < COMBO_COUNT; ++i) {
95 combo_t *combo = &key_combos[i];
96 is_combo_key |= process_single_combo(combo, keycode, record);
102 void matrix_scan_combo(void)
105 for (int i = 0; i < COMBO_COUNT; ++i) {
106 combo_t *combo = &key_combos[i];
108 combo->timer != COMBO_TIMER_ELAPSED &&
109 timer_elapsed(combo->timer) > COMBO_TERM) {
111 combo->timer = COMBO_TIMER_ELAPSED;
112 unregister_code16(combo->key);
113 register_code16(combo->key);