]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_combo.c
Fix process_combo which assign -1 to uint16_t (#3697)
[qmk_firmware.git] / quantum / process_keycode / process_combo.c
1 /* Copyright 2016 Jack Humbert
2  *
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.
7  *
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.
12  *
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/>.
15  */
16
17 #include "process_combo.h"
18 #include "print.h"
19
20
21 __attribute__ ((weak))
22 combo_t key_combos[COMBO_COUNT] = {
23
24 };
25
26 __attribute__ ((weak))
27 void process_combo_event(uint8_t combo_index, bool pressed) {
28
29 }
30
31 static uint8_t current_combo_index = 0;
32
33 static inline void send_combo(uint16_t action, bool pressed)
34 {
35     if (action) {
36         if (pressed) {
37             register_code16(action);
38         } else {
39             unregister_code16(action);
40         }
41     } else {
42         process_combo_event(current_combo_index, pressed);
43     }
44 }
45
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) 
51 {
52     uint8_t count = 0;
53     uint8_t index = -1;
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;
59     }
60
61     /* Return if not a combo key */
62     if (-1 == (int8_t)index) return false;
63
64     /* The combos timer is used to signal whether the combo is active */
65     bool is_combo_active = combo->is_active;
66
67     if (record->event.pressed) {
68         KEY_STATE_DOWN(index);
69
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;
79 #else
80                 combo->prev_key = keycode;
81 #endif
82             }
83         }
84     } else {
85         if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
86             send_combo(combo->keycode, false);
87         }
88
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));
95 #else
96             register_code16(keycode);
97             send_keyboard_report();
98             unregister_code16(keycode);
99 #endif
100             combo->is_active = false;
101             combo->timer = 0;            
102         }
103
104         KEY_STATE_UP(index);        
105     }
106
107     if (NO_COMBO_KEYS_ARE_DOWN) {
108         combo->is_active = true;
109         combo->timer = 0;
110     }
111
112     return is_combo_active;
113 }
114
115 bool process_combo(uint16_t keycode, keyrecord_t *record)
116 {
117     bool is_combo_key = false;
118
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);
122     }    
123
124     return !is_combo_key;
125 }
126
127 void matrix_scan_combo(void)
128 {
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 &&
136             combo->timer &&
137             timer_elapsed(combo->timer) > COMBO_TERM) {
138
139             /* This disables the combo, meaning key events for this
140              * combo will be handled by the next processors in the chain 
141              */
142             combo->is_active = false;
143
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));
148 #else
149             unregister_code16(combo->prev_key);
150             register_code16(combo->prev_key);
151 #endif
152         }
153     }
154 }