-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeys.c
1080 lines (995 loc) · 34 KB
/
keys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* keys.c: Key processing.
*
* This does all the work of managing layers, executing macros, handling
* remapping, keeping track of modifiers, etc.
*
* Copyright (c) 2021-2022 Kimmo Kulovesi, https://arkku.dev/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include "keys.h"
#include "aakbd.h"
#include "usbkbd.h"
#include "usb_keys.h"
#include "keycodes.h"
#include <layers.c> // Yes, including .c
#if LAYER_COUNT > 0
#include <macros.c> // Yes, including .c
#endif
#ifndef DEFAULT_BASE_LAYER
#define DEFAULT_BASE_LAYER 1
#endif
#if LAYER_COUNT > 1
// To simplify code elsewhere, define arrays of 1 element for every layer,
// it's only a few dozen wasted bytes at worst (and in practice it will get
// optimised away by the compiler)
#if LAYER_COUNT < 3
DEFINE_EMPTY_LAYER(3);
#endif
#if LAYER_COUNT < 4
DEFINE_EMPTY_LAYER(4);
#endif
#if LAYER_COUNT < 5
DEFINE_EMPTY_LAYER(5);
#endif
#if LAYER_COUNT < 6
DEFINE_EMPTY_LAYER(6);
#endif
#if LAYER_COUNT < 7
DEFINE_EMPTY_LAYER(7);
#endif
#if LAYER_COUNT < 8
DEFINE_EMPTY_LAYER(8);
#endif
#if LAYER_COUNT > 8
#if LAYER_COUNT < 10
DEFINE_EMPTY_LAYER(10);
#endif
#if LAYER_COUNT < 11
DEFINE_EMPTY_LAYER(11);
#endif
#if LAYER_COUNT < 12
DEFINE_EMPTY_LAYER(12);
#endif
#if LAYER_COUNT < 13
DEFINE_EMPTY_LAYER(13);
#endif
#if LAYER_COUNT < 14
DEFINE_EMPTY_LAYER(14);
#endif
#if LAYER_COUNT < 15
DEFINE_EMPTY_LAYER(15);
#endif
#if LAYER_COUNT < 16
DEFINE_EMPTY_LAYER(16);
#endif
#if LAYER_COUNT > 16
#if LAYER_COUNT < 18
DEFINE_EMPTY_LAYER(18);
#endif
#if LAYER_COUNT < 19
DEFINE_EMPTY_LAYER(19);
#endif
#if LAYER_COUNT < 20
DEFINE_EMPTY_LAYER(20);
#endif
#if LAYER_COUNT < 21
DEFINE_EMPTY_LAYER(21);
#endif
#if LAYER_COUNT < 22
DEFINE_EMPTY_LAYER(22);
#endif
#if LAYER_COUNT < 23
DEFINE_EMPTY_LAYER(23);
#endif
#if LAYER_COUNT < 24
DEFINE_EMPTY_LAYER(24);
#endif
#if LAYER_COUNT < 25
DEFINE_EMPTY_LAYER(25);
#endif
#if LAYER_COUNT < 26
DEFINE_EMPTY_LAYER(26);
#endif
#if LAYER_COUNT < 27
DEFINE_EMPTY_LAYER(27);
#endif
#if LAYER_COUNT < 28
DEFINE_EMPTY_LAYER(28);
#endif
#if LAYER_COUNT < 29
DEFINE_EMPTY_LAYER(29);
#endif
#if LAYER_COUNT < 30
DEFINE_EMPTY_LAYER(30);
#endif
#if LAYER_COUNT < 31
DEFINE_EMPTY_LAYER(31);
#endif
#endif // LAYER_COUNT > 16
#endif // LAYER_COUNT > 8
#endif // LAYER_COUNT > 1
#if MAX_KEY_ROLLOVER <= 10
#define MAX_REMAPPED_KEY_ROLLOVER MAX_KEY_ROLLOVER
#else
#define MAX_REMAPPED_KEY_ROLLOVER 10
#endif
struct key_source {
uint8_t key;
uint8_t data;
uint16_t keycode;
};
/// A list of source layers for keys where the source layer differs from the
/// default keycode. This allows matching key releases to the correct layer,
/// even if layer states have changed.
static struct key_source keybuffer[MAX_REMAPPED_KEY_ROLLOVER + 1] = { { 0, 0, 0 } };
#if ENABLE_KEYLOCK
/// Keylock state, either 0, the key that is locked, or `KEYLOCK_ARMED`.
static uint8_t keylock_key = 0;
#define KEYLOCK_ARMED (0xFFU)
#define is_keylock_armed (keylock_key == KEYLOCK_ARMED)
#define arm_keylock() do { keylock_key = KEYLOCK_ARMED; } while (0)
static inline bool
is_keylock_enabled (void) {
return keylock_key != 0;
}
#else
static inline bool
is_keylock_enabled (void) {
return false;
}
#endif
/// Get the key code for `key` in layer number `num`. This has to be a macro
/// because this needs the name of a specific array.
#define get_key_from_layer(key, num) ( \
(sizeof(keycode_t) == 1) ? \
pgm_read_byte(LAYER_ARRAY(num) + (key)) : \
pgm_read_word(LAYER_ARRAY(num) + (key)) \
)
#define layer_bit(num) (((layer_mask_t) 1) << ((num) - 1))
#define is_key_in_layer(key, num) ((key) < LAYER_SIZE(num))
#define is_layer_enabled(num) (((num) == base_layer) || (layer_mask & layer_bit(num)))
#define layer_enabled(num) layer_state_changed((num), true)
#define layer_disabled(num) layer_state_changed((num), false)
// Note: This sets local variables inside `process_key`
#define set_keycode_from_layer(key, num) do { \
if (keycode == 0 && (num) >= base_layer && is_key_in_layer((key), num) && is_layer_enabled(num)) { \
keycode = get_key_from_layer((key), num); \
} \
} while (0)
#if LAYER_COUNT > 0
/// The current base layer number.
static uint8_t base_layer = DEFAULT_BASE_LAYER;
static uint8_t previous_base_layer = DEFAULT_BASE_LAYER;
/// The bitmask for active layers.
static layer_mask_t layer_mask = 0;
static layer_mask_t previous_layer_mask = 0;
/// "Strong" modifiers mask. These are real modifier keys that are active as
/// long as they are held.
static uint8_t strong_modifiers = 0;
/// "Weak" modififers mask. These are modifiers not set by the actual modifier
/// keys, but rather mapped from extended keycodes, such as for keys that
/// simulate the actual key + a modifier. Weak modifiers are cleared whenever
/// another key is pressed, so that they don't influence them.
static uint8_t weak_modifiers = 0;
static inline void
set_base_layer (const uint8_t num) {
previous_base_layer = base_layer;
if (base_layer == num) {
return;
}
base_layer = num;
for (int_fast8_t i = previous_base_layer; i < num; ++i) {
// Active layers with a lower number than the new base became disabled
if (i == previous_base_layer || is_layer_active(i)) {
layer_disabled(i);
}
}
if (previous_base_layer > num) {
for (int_fast8_t i = num; i < previous_base_layer; ++i) {
// Active layers with a higher number than the new base became enabled
if (i == num || is_layer_active(i)) {
layer_enabled(i);
}
}
if (!is_layer_active(previous_base_layer)) {
// The previous base can still have become disabled
layer_disabled(previous_base_layer);
}
}
}
static inline void
restore_previous_base_layer (void) {
set_base_layer(previous_base_layer);
}
static inline bool
is_layer_active (const uint8_t num) {
return (layer_mask & layer_bit(num)) != 0;
}
static inline uint8_t
highest_active_layer (void) {
if (layer_mask == 0) {
return 0;
}
#if LAYER_MASK_BITS <= 16
return (LAYER_MASK_BITS - ((sizeof(unsigned int) * 8) - LAYER_MASK_BITS)) - __builtin_clz((unsigned int) layer_mask);
#else
return (LAYER_MASK_BITS - ((sizeof(unsigned long) * 8) - LAYER_MASK_BITS)) - __builtin_clzl((unsigned long) layer_mask);
#endif
}
static inline layer_mask_t
active_layers_mask (void) {
return layer_mask;
}
static inline uint8_t
current_base_layer (void) {
return base_layer;
}
static inline void
clear_weak_modifiers (void) {
weak_modifiers = 0U;
}
static inline void
clear_strong_modifiers (void) {
strong_modifiers = 0U;
}
static inline void
add_weak_modifiers (const uint8_t mods) {
weak_modifiers |= mods;
}
static inline void
remove_weak_modifiers (const uint8_t mods) {
weak_modifiers &= ~(mods);
}
static inline uint8_t
weak_modifiers_mask (void) {
return weak_modifiers;
}
static inline void
add_strong_modifiers (const uint8_t mods) {
strong_modifiers |= mods;
}
static inline void
remove_strong_modifiers (const uint8_t mods) {
strong_modifiers &= ~(mods);
}
static inline uint8_t
strong_modifiers_mask (void) {
return strong_modifiers;
}
static inline void
enable_layer (const uint8_t num) {
const layer_mask_t bit = layer_bit(num);
if (!(layer_mask & bit)) {
layer_mask |= bit;
if (num > base_layer) {
layer_enabled(num);
}
}
}
static inline void
disable_layer (const uint8_t num) {
const layer_mask_t bit = layer_bit(num);
if (layer_mask & bit) {
layer_mask &= ~bit;
if (num > base_layer) {
layer_disabled(num);
}
}
}
static inline void
toggle_layer (const uint8_t num) {
const layer_mask_t bit = layer_bit(num);
layer_mask ^= layer_bit(num);
if (num > base_layer) {
if (layer_mask & bit) {
layer_enabled(num);
} else {
layer_disabled(num);
}
}
}
static inline void
set_active_layer (const uint8_t num) {
const layer_mask_t bit = ((num == 0) ? num : layer_bit(num));
set_active_layers_mask(bit);
}
static inline void
set_active_layers_mask (const layer_mask_t mask) {
previous_layer_mask = layer_mask;
if (layer_mask == mask) {
return;
}
layer_mask = mask;
const layer_mask_t enabled_layers = mask & ~previous_layer_mask;
const layer_mask_t disabled_layers = previous_layer_mask & ~mask;
for (int_fast8_t i = base_layer + 1; i <= LAYER_COUNT; ++i) {
const layer_mask_t bit = layer_bit(i);
if (disabled_layers & bit) {
layer_disabled(i);
} else if (enabled_layers & bit) {
layer_enabled(i);
}
}
}
static inline void
restore_previous_layer_state (void) {
set_active_layers_mask(previous_layer_mask);
}
static inline keycode_t keycode_from_layer(uint8_t key, uint8_t num);
/// Is a dual-action key currently being held down such that another keypress
/// during that time will trigger the alternative action? For example, a
/// key that works as Ctrl when held but sends Esc if only tapped without
/// another keypress.
static bool is_pending_keypress = false;
/// If we have a simulated keypress in progress, this is the pending keycode
/// to release either based on time elapsed or the
static uint8_t pending_release = 0;
/// The tick since which `pending_release` has been pending.
static uint8_t pending_release_since = 0;
static inline void
set_pending_keypress (const bool is_pending) {
is_pending_keypress = is_pending;
}
static inline bool
pending_keypress (void) {
return is_pending_keypress;
}
static inline void
send_pending_release (void) {
usb_keyboard_release(pending_release);
pending_release = 0;
(void) usb_keyboard_send_if_needed();
}
static inline void
set_pending_release (const uint8_t key) {
pending_release = key;
pending_release_since = current_10ms_tick_count();
}
static inline void
register_press_and_release (const uint8_t key, const uint8_t mods) {
if (pending_release) {
// Clear any previous release if there was one pending.
// (Doesn't happen in this file, but macros have access to this.)
send_pending_release();
}
usb_keyboard_set_modifiers(mods);
(void) usb_keyboard_send_if_needed();
usb_keyboard_press(key);
(void) usb_keyboard_send_if_needed();
// The release will be handled later to give some time for it to register.
set_pending_release(key);
}
static inline void
send_pending_key_down (const uint8_t key) {
set_pending_keypress(false);
// Use strong modifiers only since this is only used in contexts where
// the weak modifiers are not intended for this key
register_press_and_release(key, strong_modifiers_mask());
}
static void
reset_layers (void) {
clear_override_leds();
set_active_layers_mask(0);
previous_layer_mask = 0;
set_base_layer(DEFAULT_BASE_LAYER);
previous_base_layer = DEFAULT_BASE_LAYER;
set_pending_keypress(false);
}
static inline void
register_key (const uint8_t key, const bool is_release) {
if (is_release) {
if (IS_MODIFIER(key)) {
remove_strong_modifiers(MODIFIER_BIT(key));
} else {
usb_keyboard_release(key);
}
} else {
if (IS_MODIFIER(key)) {
add_strong_modifiers(MODIFIER_BIT(key));
} else {
usb_keyboard_press(key);
}
}
usb_keyboard_set_modifiers(strong_modifiers_mask() | weak_modifiers_mask());
}
#endif // ^ LAYER_COUNT > 0
void
report_keyboard_error (bool is_rollover_error) {
usb_keyboard_press(is_rollover_error ? USB_KEY_ROLLOVER : USB_KEY_UNDEFINED_ERROR);
}
void
process_key (uint8_t key, bool is_release) {
#if LAYER_COUNT > 0
keycode_t keycode = PASS;
const bool was_pending_keypress = pending_keypress();
const uint8_t physical_key = key;
uint8_t data_or_index = 0;
if (pending_release) {
// If we are pending some timed key release, release it now since
// otherwise timing between it and this key event will be wrong.
send_pending_release();
}
if (is_release) {
#if ENABLE_KEYLOCK
if (physical_key == keylock_key) {
// Do not release the locked key
return;
}
#endif
// Find the keycode corresponding to the original press of this key,
// which might differ with the current layer activation state.
int_fast8_t ri = 0, wi = 0;
do {
if (keybuffer[ri].key != key) {
keybuffer[wi].key = keybuffer[ri].key;
keybuffer[wi].data = keybuffer[ri].data;
keybuffer[wi].keycode = keybuffer[ri].keycode;
++wi;
} else {
keycode = keybuffer[ri].keycode;
data_or_index = keybuffer[ri].data;
}
++ri;
} while (keybuffer[wi].key);
} else {
#if ENABLE_KEYLOCK
if (physical_key == keylock_key) {
// The locked key was pressed again, unlock it.
// (It will be released when this new press is released.)
keylock_key = 0;
return;
}
#endif
#if LAYER_COUNT > 1
// The reason this is unrolled is that we get more compile-time
// constants like `sizeof` here, and the compiler is more likely to
// optimise away unused layers.
#if LAYER_MASK_BITS > 16
set_keycode_from_layer(key, 31);
set_keycode_from_layer(key, 30);
set_keycode_from_layer(key, 29);
set_keycode_from_layer(key, 28);
set_keycode_from_layer(key, 27);
set_keycode_from_layer(key, 26);
set_keycode_from_layer(key, 25);
set_keycode_from_layer(key, 24);
set_keycode_from_layer(key, 23);
set_keycode_from_layer(key, 22);
set_keycode_from_layer(key, 21);
set_keycode_from_layer(key, 20);
set_keycode_from_layer(key, 19);
set_keycode_from_layer(key, 18);
set_keycode_from_layer(key, 17);
#endif
#if LAYER_MASK_BITS > 8
set_keycode_from_layer(key, 16);
set_keycode_from_layer(key, 15);
set_keycode_from_layer(key, 14);
set_keycode_from_layer(key, 13);
set_keycode_from_layer(key, 12);
set_keycode_from_layer(key, 11);
set_keycode_from_layer(key, 10);
set_keycode_from_layer(key, 9);
#endif
set_keycode_from_layer(key, 8);
set_keycode_from_layer(key, 7);
set_keycode_from_layer(key, 6);
set_keycode_from_layer(key, 5);
set_keycode_from_layer(key, 4);
set_keycode_from_layer(key, 3);
set_keycode_from_layer(key, 2);
#endif
set_keycode_from_layer(key, 1);
keycode = preprocess_press(keycode, physical_key, &data_or_index);
if (keycode == PASS) {
keycode = key;
}
if (keycode != key || data_or_index != 0) {
// The key differs from the physical key, so we need to record
// the keycode so that it will be correctly released even if the
// layer configuration changes before then. Since we only do this
// for keys that differ from the physical key, the list we have
// to maintain should normally be much shorter than the full set
// of pressed keys.
int_fast8_t i = 0;
while (keybuffer[i].key && keybuffer[i].key != key) {
++i;
}
if (i == MAX_REMAPPED_KEY_ROLLOVER || keybuffer[i].key == key) {
usb_keyboard_press(KEY_ROLLOVER_ERROR_CODE);
is_release = true;
goto postprocess;
}
keybuffer[i].key = key;
keybuffer[i].data = data_or_index;
keybuffer[i].keycode = keycode;
data_or_index = i;
}
}
if (keycode == PASS) {
// No mapping, pass to default
keycode = key;
} else {
key = PLAIN_KEY_OF(keycode);
}
if (!is_release) {
// Weak modifiers should not affect presses other than the actual
// key itself.
clear_weak_modifiers();
set_pending_keypress(false);
#if ENABLE_KEYLOCK
if (is_keylock_armed) {
// Keylock was armed, lock this key down
keylock_key = physical_key;
}
#endif
}
if (keycode == NONE) {
// Special keycode to do nothing
goto postprocess;
} else if (is_extended_keycode(keycode)) {
uint8_t mods = MODIFIERS_OF_EXTENDED(keycode);
uint8_t command = COMMAND_OF(keycode);
// Double modifiers like `CTRL(ALT)` needs both modifiers to
// be strong or the second one would be cancelled by the next
// keypress, which would make the combo useless.
bool is_strong_modifier = (mods && IS_MODIFIER(key));
if (command) {
if (command == CMD_MODIFIER_OR_KEY) {
// The modifier must be strong to have effect
is_strong_modifier = true;
if (is_release) {
mods = data_or_index; // Undo only the actual mods we added
if (was_pending_keypress) {
// No other key was pressed, act as a keypress
remove_strong_modifiers(mods);
send_pending_key_down(key);
}
} else {
// Don't affect the same modifiers if they are already
// present so that this can be used for the key and that
// modifier from another key.
mods &= ~strong_modifiers_mask();
keybuffer[data_or_index].data = mods;
set_pending_keypress(true);
}
key = NONE;
} else {
// Layer command
uint8_t layer;
uint8_t modifier;
if (command == CMD_LAYER_OR_KEY) {
// A special case where the layer number isn't in the
// keycode section since there is an actual keycode
if (is_release && was_pending_keypress) {
send_pending_key_down(key);
} else {
set_pending_keypress(!is_release);
}
layer = LAYER_OF_LAYER_OR_KEY(keycode);
mods = 0; // The layer is in the modifier bits
modifier = ACT_ON_HOLD;
command = CMD_LAYER_TOGGLE;
} else {
layer = LAYER_OF_COMMAND(keycode);
is_strong_modifier = true; // Need strength to have effect
modifier = LAYER_CMD_MODIFIER_OF(keycode);
}
key = NONE; // The key part of this keycode is not a key
if (layer >= 0 && layer <= LAYER_COUNT) {
// Layer modifying command
int_fast8_t action; // 1 = activate, -1 = deactivate
switch (modifier) {
case ACT_ON_HOLD:
action = is_release ? -1 : 1;
break;
case ACT_ON_RELEASE:
action = is_release ? 1 : 0;
break;
case ACT_ON_PRESS:
action = is_release ? 0 : 1;
break;
case ACT_IF_NO_KEYPRESS:
if (is_release) {
action = was_pending_keypress ? 1 : 0;
} else {
action = 0;
set_pending_keypress(true);
}
break;
case ACT_ON_HOLD_KEEP_IF_NO_KEYPRESS:
if (is_release) {
action = was_pending_keypress ? 0 : -1;
} else {
action = 1;
set_pending_keypress(true);
}
break;
default:
action = 0;
break;
}
if (action && (layer || command == CMD_LAYER_SET_BASE || command == CMD_LAYER_SET_MASK)) {
switch (command) {
case CMD_LAYER_DISABLE:
action = -action;
// fallthrough
case CMD_LAYER_ENABLE:
if (action == 1) {
enable_layer(layer);
} else {
disable_layer(layer);
}
break;
case CMD_LAYER_TOGGLE:
toggle_layer(layer);
break;
case CMD_LAYER_SET_MASK:
if (action == 1) {
set_active_layer(layer);
} else {
restore_previous_layer_state();
}
break;
case CMD_LAYER_SET_BASE:
if (action == 1) {
set_base_layer(layer);
} else {
restore_previous_base_layer();
}
break;
default:
break;
}
}
}
}
} else if (!mods) {
// Extended keycode that is not a modifier or a command
if (extended_keycode_is_macro(key)) {
// On release the previous data is in `data_or_index`
// since the keypress object is already overwritten, while
// on release it is the index of the new object.
execute_macro(
MACRO_OF_EXTENDED(key),
is_release,
physical_key,
is_release ? &data_or_index : &(keybuffer[data_or_index].data)
);
} else if (extended_keycode_is_exact_modifiers(key)) {
is_strong_modifier = true;
if (is_release) {
// Unset the added modifiers - we can't restore the old
// modifiers since those keys might have been already
// released, which would leave their modifiers stuck
mods = data_or_index;
} else {
mods = EXACT_MODS_OF_EXTENDED(key);
// Store the modifiers we added with this key
keybuffer[data_or_index].data = mods & ~strong_modifiers_mask();
// Set exactly these modifiers, nothing else
clear_strong_modifiers();
}
} else {
const enum extended_keycode extended_key = key;
// Built-in extended keycode
switch (extended_key) {
case EXT_RESET_KEYBOARD:
if (is_release) {
keyboard_reset();
}
break;
case EXT_ENTER_BOOTLOADER:
jump_to_bootloader();
break;
case EXT_RESET_LAYERS:
if (is_release) {
reset_layers();
}
break;
case EXT_HYPER_MODIFIERS:
mods |= CMD_BIT;
// fallthrough
case EXT_MEH_MODIFIERS:
if (is_release) {
mods = data_or_index;
} else {
mods |= SHIFT_BIT | CTRL_BIT | ALT_BIT;
mods = mods & ~strong_modifiers_mask();
keybuffer[data_or_index].data = mods;
}
is_strong_modifier = true;
break;
#if ENABLE_KEYLOCK
case EXT_KEYLOCK:
if (!is_release) {
if (keylock_key) {
// Keylock pressed again, release the locked key
key = keylock_key;
keylock_key = 0;
if (key != physical_key) {
process_key(key, true);
}
} else {
arm_keylock();
}
}
break;
#endif
case EXT_TOGGLE_BOOT_PROTOCOL:
if (is_release) {
usb_keyboard_toggle_boot_protocol();
}
break;
#if ENABLE_SIMULATED_TYPING
case EXT_PRINT_DEBUG_INFO:
if (!is_release) {
usb_keyboard_type_debug_report();
}
break;
#endif
default:
break;
}
}
key = NONE;
}
if (mods) {
if (is_strong_modifier) {
if (is_release) {
remove_strong_modifiers(mods);
} else {
add_strong_modifiers(mods);
}
} else {
if (is_release) {
remove_weak_modifiers(mods);
} else {
add_weak_modifiers(mods);
}
}
}
}
register_key(key, is_release);
postprocess:
if (is_release) {
postprocess_release(keycode, physical_key, data_or_index);
}
#else // ^ LAYER_COUNT > 0
// No layers, just use the key as is
if (is_release) {
usb_keyboard_release(key);
} else {
usb_keyboard_press(key);
}
#endif
// Update the USB host if there were any changes
(void) usb_keyboard_send_if_needed();
}
void
reset_keys (void) {
#if LAYER_COUNT > 0
clear_weak_modifiers();
clear_strong_modifiers();
reset_layers();
pending_release = 0;
#endif
usb_keyboard_release_all_keys();
#if ENABLE_KEYLOCK
keylock_key = 0;
#endif
for (int_fast8_t i = 0; i < MAX_REMAPPED_KEY_ROLLOVER; ++i) {
keybuffer[i].key = 0;
keybuffer[i].data = 0;
keybuffer[i].keycode = 0;
}
#if LAYER_COUNT > 0
handle_reset();
#endif
}
#if SIMULATED_KEYPRESS_TIME_MS >= 15
#define is_time_to_release_at(now) (((pending_release_since + ((SIMULATED_KEYPRESS_TIME_MS - 5)/ 10)) - (now)) & 0x80U)
#elif SIMULATED_KEYPRESS_TIME_MS <= 5
#define is_time_to_release_at(now) (1)
#else
#define is_time_to_release_at(now) ((now) != pending_release_since)
#endif
/// Mask of overridden LEDs, where lower 4 bits are a mask to add to the
/// mask requested by host, and the upper 4 bits are a mask to subtract
/// from the LEDs requested by host. The subtraction is done first.
static uint8_t override_leds = 0;
/// The previous USB LED state - so we can react to updates, such as
/// programmatically turning on Num Lock.
static uint8_t previous_usb_led_state = 0;
static inline void
remove_override_leds_on (const uint8_t mask) {
override_leds &= ~(mask & 0x0FU);
}
static inline void
add_override_leds_on (const uint8_t mask) {
override_leds |= mask & 0x0FU;
}
static inline void
remove_override_leds_off (const uint8_t mask) {
override_leds &= ~(mask << 4);
}
static inline void
add_override_leds_off (const uint8_t mask) {
override_leds |= (mask << 4);
}
static inline void
clear_override_leds (void) {
override_leds = 0;
}
uint8_t
keys_led_state (void) {
uint8_t leds = usb_keyboard_led_state();
if (leds != previous_usb_led_state) {
previous_usb_led_state = leds;
#if LAYER_COUNT > 0
keyboard_host_leds_changed(leds);
#endif
}
leds &= ~(override_leds >> 4);
leds |= override_leds & 0x0FU;
return leds;
}
uint8_t
keys_error (void) {
return usb_key_error();
}
static inline keycode_t
keycode_from_layer (uint8_t key, uint8_t num) {
switch (num) {
case 0: break;
#if LAYER_COUNT >= 1
_Static_assert(LAYER_SIZE(1) <= 0xFF, "Extended keycode as index in layer 1");
case 1: return get_key_from_layer(key, 1);
#endif
#if LAYER_COUNT >= 2
_Static_assert(LAYER_SIZE(2) <= 0xFF, "Extended keycode as index in layer 2");
case 2: return get_key_from_layer(key, 2);
#endif
#if LAYER_COUNT >= 3
_Static_assert(LAYER_SIZE(3) <= 0xFF, "Extended keycode as index in layer 3");
case 3: return get_key_from_layer(key, 3);
#endif
#if LAYER_COUNT >= 4
_Static_assert(LAYER_SIZE(4) <= 0xFF, "Extended keycode as index in layer 4");
case 4: return get_key_from_layer(key, 4);
#endif
#if LAYER_COUNT >= 5
_Static_assert(LAYER_SIZE(5) <= 0xFF, "Extended keycode as index in layer 5");
case 5: return get_key_from_layer(key, 5);
#endif
#if LAYER_COUNT >= 6
_Static_assert(LAYER_SIZE(6) <= 0xFF, "Extended keycode as index in layer 6");
case 6: return get_key_from_layer(key, 6);
#endif
#if LAYER_COUNT >= 7
_Static_assert(LAYER_SIZE(7) <= 0xFF, "Extended keycode as index in layer 7");
case 7: return get_key_from_layer(key, 7);
#endif
#if LAYER_COUNT >= 8
_Static_assert(LAYER_SIZE(8) <= 0xFF, "Extended keycode as index in layer 8");
case 8: return get_key_from_layer(key, 8);
#endif
#if LAYER_COUNT >= 9
_Static_assert(LAYER_SIZE(9) <= 0xFF, "Extended keycode as index in layer 9");
case 9: return get_key_from_layer(key, 9);
#endif
#if LAYER_COUNT >= 10
_Static_assert(LAYER_SIZE(10) <= 0xFF, "Extended keycode as index in layer 10");
case 10: return get_key_from_layer(key, 10);
#endif
#if LAYER_COUNT >= 11
_Static_assert(LAYER_SIZE(11) <= 0xFF, "Extended keycode as index in layer 11");
case 11: return get_key_from_layer(key, 11);
#endif
#if LAYER_COUNT >= 12
_Static_assert(LAYER_SIZE(12) <= 0xFF, "Extended keycode as index in layer 12");
case 12: return get_key_from_layer(key, 12);
#endif
#if LAYER_COUNT >= 13
_Static_assert(LAYER_SIZE(13) <= 0xFF, "Extended keycode as index in layer 13");
case 13: return get_key_from_layer(key, 13);
#endif
#if LAYER_COUNT >= 14
_Static_assert(LAYER_SIZE(14) <= 0xFF, "Extended keycode as index in layer 14");
case 14: return get_key_from_layer(key, 14);
#endif
#if LAYER_COUNT >= 15
_Static_assert(LAYER_SIZE(15) <= 0xFF, "Extended keycode as index in layer 15");