aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/action_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/common/action_util.c')
-rw-r--r--tmk_core/common/action_util.c204
1 files changed, 5 insertions, 199 deletions
diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c
index 77848c092..148162a51 100644
--- a/tmk_core/common/action_util.c
+++ b/tmk_core/common/action_util.c
@@ -25,13 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
25extern keymap_config_t keymap_config; 25extern keymap_config_t keymap_config;
26 26
27 27
28static inline void add_key_byte(uint8_t code);
29static inline void del_key_byte(uint8_t code);
30#ifdef NKRO_ENABLE
31static inline void add_key_bit(uint8_t code);
32static inline void del_key_bit(uint8_t code);
33#endif
34
35static uint8_t real_mods = 0; 28static uint8_t real_mods = 0;
36static uint8_t weak_mods = 0; 29static uint8_t weak_mods = 0;
37static uint8_t macro_mods = 0; 30static uint8_t macro_mods = 0;
@@ -50,6 +43,10 @@ static int8_t cb_count = 0;
50//report_keyboard_t keyboard_report = {}; 43//report_keyboard_t keyboard_report = {};
51report_keyboard_t *keyboard_report = &(report_keyboard_t){}; 44report_keyboard_t *keyboard_report = &(report_keyboard_t){};
52 45
46extern inline void add_key(uint8_t key);
47extern inline void del_key(uint8_t key);
48extern inline void clear_keys(void);
49
53#ifndef NO_ACTION_ONESHOT 50#ifndef NO_ACTION_ONESHOT
54static int8_t oneshot_mods = 0; 51static int8_t oneshot_mods = 0;
55static int8_t oneshot_locked_mods = 0; 52static int8_t oneshot_locked_mods = 0;
@@ -134,7 +131,7 @@ void send_keyboard_report(void) {
134 } 131 }
135#endif 132#endif
136 keyboard_report->mods |= oneshot_mods; 133 keyboard_report->mods |= oneshot_mods;
137 if (has_anykey()) { 134 if (has_anykey(keyboard_report)) {
138 clear_oneshot_mods(); 135 clear_oneshot_mods();
139 } 136 }
140 } 137 }
@@ -143,38 +140,6 @@ void send_keyboard_report(void) {
143 host_keyboard_send(keyboard_report); 140 host_keyboard_send(keyboard_report);
144} 141}
145 142
146/* key */
147void add_key(uint8_t key)
148{
149#ifdef NKRO_ENABLE
150 if (keyboard_protocol && keymap_config.nkro) {
151 add_key_bit(key);
152 return;
153 }
154#endif
155 add_key_byte(key);
156}
157
158void del_key(uint8_t key)
159{
160#ifdef NKRO_ENABLE
161 if (keyboard_protocol && keymap_config.nkro) {
162 del_key_bit(key);
163 return;
164 }
165#endif
166 del_key_byte(key);
167}
168
169void clear_keys(void)
170{
171 // not clear mods
172 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
173 keyboard_report->raw[i] = 0;
174 }
175}
176
177
178/* modifier */ 143/* modifier */
179uint8_t get_mods(void) { return real_mods; } 144uint8_t get_mods(void) { return real_mods; }
180void add_mods(uint8_t mods) { real_mods |= mods; } 145void add_mods(uint8_t mods) { real_mods |= mods; }
@@ -221,166 +186,7 @@ uint8_t get_oneshot_mods(void)
221/* 186/*
222 * inspect keyboard state 187 * inspect keyboard state
223 */ 188 */
224uint8_t has_anykey(void)
225{
226 uint8_t cnt = 0;
227 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
228 if (keyboard_report->raw[i])
229 cnt++;
230 }
231 return cnt;
232}
233
234uint8_t has_anymod(void) 189uint8_t has_anymod(void)
235{ 190{
236 return bitpop(real_mods); 191 return bitpop(real_mods);
237} 192}
238
239uint8_t get_first_key(void)
240{
241#ifdef NKRO_ENABLE
242 if (keyboard_protocol && keymap_config.nkro) {
243 uint8_t i = 0;
244 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
245 ;
246 return i<<3 | biton(keyboard_report->nkro.bits[i]);
247 }
248#endif
249#ifdef USB_6KRO_ENABLE
250 uint8_t i = cb_head;
251 do {
252 if (keyboard_report->keys[i] != 0) {
253 break;
254 }
255 i = RO_INC(i);
256 } while (i != cb_tail);
257 return keyboard_report->keys[i];
258#else
259 return keyboard_report->keys[0];
260#endif
261}
262
263
264
265/* local functions */
266static inline void add_key_byte(uint8_t code)
267{
268#ifdef USB_6KRO_ENABLE
269 int8_t i = cb_head;
270 int8_t empty = -1;
271 if (cb_count) {
272 do {
273 if (keyboard_report->keys[i] == code) {
274 return;
275 }
276 if (empty == -1 && keyboard_report->keys[i] == 0) {
277 empty = i;
278 }
279 i = RO_INC(i);
280 } while (i != cb_tail);
281 if (i == cb_tail) {
282 if (cb_tail == cb_head) {
283 // buffer is full
284 if (empty == -1) {
285 // pop head when has no empty space
286 cb_head = RO_INC(cb_head);
287 cb_count--;
288 }
289 else {
290 // left shift when has empty space
291 uint8_t offset = 1;
292 i = RO_INC(empty);
293 do {
294 if (keyboard_report->keys[i] != 0) {
295 keyboard_report->keys[empty] = keyboard_report->keys[i];
296 keyboard_report->keys[i] = 0;
297 empty = RO_INC(empty);
298 }
299 else {
300 offset++;
301 }
302 i = RO_INC(i);
303 } while (i != cb_tail);
304 cb_tail = RO_SUB(cb_tail, offset);
305 }
306 }
307 }
308 }
309 // add to tail
310 keyboard_report->keys[cb_tail] = code;
311 cb_tail = RO_INC(cb_tail);
312 cb_count++;
313#else
314 int8_t i = 0;
315 int8_t empty = -1;
316 for (; i < KEYBOARD_REPORT_KEYS; i++) {
317 if (keyboard_report->keys[i] == code) {
318 break;
319 }
320 if (empty == -1 && keyboard_report->keys[i] == 0) {
321 empty = i;
322 }
323 }
324 if (i == KEYBOARD_REPORT_KEYS) {
325 if (empty != -1) {
326 keyboard_report->keys[empty] = code;
327 }
328 }
329#endif
330}
331
332static inline void del_key_byte(uint8_t code)
333{
334#ifdef USB_6KRO_ENABLE
335 uint8_t i = cb_head;
336 if (cb_count) {
337 do {
338 if (keyboard_report->keys[i] == code) {
339 keyboard_report->keys[i] = 0;
340 cb_count--;
341 if (cb_count == 0) {
342 // reset head and tail
343 cb_tail = cb_head = 0;
344 }
345 if (i == RO_DEC(cb_tail)) {
346 // left shift when next to tail
347 do {
348 cb_tail = RO_DEC(cb_tail);
349 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
350 break;
351 }
352 } while (cb_tail != cb_head);
353 }
354 break;
355 }
356 i = RO_INC(i);
357 } while (i != cb_tail);
358 }
359#else
360 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
361 if (keyboard_report->keys[i] == code) {
362 keyboard_report->keys[i] = 0;
363 }
364 }
365#endif
366}
367
368#ifdef NKRO_ENABLE
369static inline void add_key_bit(uint8_t code)
370{
371 if ((code>>3) < KEYBOARD_REPORT_BITS) {
372 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
373 } else {
374 dprintf("add_key_bit: can't add: %02X\n", code);
375 }
376}
377
378static inline void del_key_bit(uint8_t code)
379{
380 if ((code>>3) < KEYBOARD_REPORT_BITS) {
381 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
382 } else {
383 dprintf("del_key_bit: can't del: %02X\n", code);
384 }
385}
386#endif