-
Notifications
You must be signed in to change notification settings - Fork 0
/
remap.c
1294 lines (1209 loc) · 49.3 KB
/
remap.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
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "input.h"
#include "keys.c"
// Types
// --------------------------------------
enum State {
IDLE,
HELD_DOWN_ALONE,
HELD_DOWN_WITH_OTHER,
TAP,
TAPPED,
DOUBLE_TAP,
};
struct KeyDefNode {
KEY_DEF * key_def;
struct KeyDefNode * next;
struct KeyDefNode * previous;
};
struct LayerNode {
struct Layer * layer;
struct LayerNode * next;
};
struct Layer {
char * name;
int state;
int lock, prev_lock;
struct LayerNode * or_master_layers;
struct LayerNode * and_master_layers;
struct LayerNode * and_not_master_layers;
struct LayerNode * slave_layers;
struct Layer * next;
};
struct LayerConf {
struct Layer * layer;
void (*conf)(struct Layer * layer);
struct LayerConf * next;
};
struct Remap {
int id;
KEY_DEF * from;
struct Layer * layer;
struct Layer * to_when_press_layer;
struct Layer * to_when_doublepress_layer;
struct LayerConf * to_when_tap_lock_layer;
struct LayerConf * to_when_double_tap_lock_layer;
struct KeyDefNode * to_when_alone;
struct KeyDefNode * to_with_other;
struct KeyDefNode * to_when_doublepress;
struct KeyDefNode * to_when_tap_lock;
struct KeyDefNode * to_when_double_tap_lock;
int to_with_other_dummy;
int to_when_alone_modifiers;
int to_with_other_modifiers;
int to_when_doublepress_modifiers;
int to_when_tap_lock_modifiers;
int to_when_double_tap_lock_modifiers;
int to_when_alone_is_modifier_only;
int to_when_doublepress_is_modifier_only;
int tap_lock;
int double_tap_lock;
enum State state;
DWORD time;
int active_modifiers;
struct Remap * next;
};
struct RemapNode {
struct Remap * remap;
struct RemapNode * next;
};
// Globals
// --------------------------------------
int g_debug = 0;
int g_hold_delay = 0;
int g_tap_timeout = 0;
int g_doublepress_timeout = 0;
int g_rehook_timeout = 1000;
int g_unlock_timeout = 60000;
int g_scancode = 0;
int g_priority = 1;
int g_last_input = 0;
struct Remap * g_remap_list = NULL;
struct Remap * g_remap_parsee = NULL;
struct Remap * g_remap_by_id[256] = {NULL};
struct RemapNode * g_remap_array[256] = {NULL};
struct Layer * g_layer_list = NULL;
struct Layer * g_layer_parsee = NULL;
// Debug Logging
// --------------------------------------
char * fmt_dir(enum Direction direction) {
return (direction == DOWN) ? "DOWN" : "UP";
}
int log_indent_level = 0;
int log_counter = 1;
void print_log_prefix() {
printf("\n%03d. ", log_counter++);
for (int i = 0; i < log_indent_level; i++)
{
printf("\t");
}
}
void log_handle_input_start(int scan_code, int virt_code, enum Direction direction, int is_injected, DWORD flags, ULONG_PTR dwExtraInfo) {
if (!g_debug) return;
print_log_prefix();
printf("[%s] %s %s (scan:0x%04X virt:0x%02X flags:0x%02X dwExtraInfo:0x%IX)",
(is_injected && ((dwExtraInfo & 0xFFFFFF00) == INJECTED_KEY_ID)) ? "output" : "input",
friendly_virt_code_name(virt_code),
fmt_dir(direction),
scan_code, // MapVirtualKeyA(virt_code, MAPVK_VK_TO_VSC_EX)
virt_code,
flags,
dwExtraInfo);
log_indent_level++;
}
void log_handle_input_end(int scan_code, int virt_code, enum Direction direction, int block_input) {
if (!g_debug) return;
log_indent_level--;
if (block_input) {
print_log_prefix();
printf("#blocked-input# %s %s",
friendly_virt_code_name(virt_code),
fmt_dir(direction));
}
}
void log_send_input(char * remap_name, KEY_DEF * key, enum Direction direction) {
if (!g_debug) return;
print_log_prefix();
printf("(sending:%s) %s %s",
remap_name,
key ? key->name : "???",
fmt_dir(direction));
}
// Remapping
// -------------------------------------
void toggle_layer_lock(struct Layer * layer) {
layer->prev_lock = layer->lock;
layer->lock = 1 - layer->lock;
}
void set_layer_lock(struct Layer * layer) {
layer->prev_lock = layer->lock;
layer->lock = 1;
}
void reset_layer_lock(struct Layer * layer) {
layer->prev_lock = layer->lock;
layer->lock = 0;
}
int check_layer_state(struct Layer * layer) {
if (layer->lock) return 1;
int state = layer->state;
if (state) {
struct Remap * remap_iter = g_remap_list;
while (remap_iter) {
if ((remap_iter->to_when_press_layer == layer &&
(remap_iter->state == HELD_DOWN_ALONE ||
remap_iter->state == HELD_DOWN_WITH_OTHER ||
remap_iter->state == TAP)) ||
(remap_iter->to_when_doublepress_layer == layer &&
remap_iter->state == DOUBLE_TAP)) {
return 1;
}
remap_iter = remap_iter->next;
}
}
struct LayerNode * layer_node_iter = layer->or_master_layers;
if (layer_node_iter) {
while (layer_node_iter) {
if (check_layer_state(layer_node_iter->layer) != 0) {
return 1;
}
layer_node_iter = layer_node_iter->next;
}
state = 0;
}
layer_node_iter = layer->and_master_layers;
if (layer_node_iter) {
while (layer_node_iter) {
if (check_layer_state(layer_node_iter->layer) == 0) {
return 0;
}
layer_node_iter = layer_node_iter->next;
}
state = 1;
}
layer_node_iter = layer->and_not_master_layers;
while (layer_node_iter) {
if (check_layer_state(layer_node_iter->layer) != 0) {
return 0;
}
layer_node_iter = layer_node_iter->next;
}
return state;
}
void set_layer_state(struct Layer * layer, int state) {
//if (!layer) return;
layer->state = state;
struct LayerNode * slave_iter = layer->slave_layers;
while (slave_iter) {
set_layer_state(slave_iter->layer, check_layer_state(slave_iter->layer) ? 1 : slave_iter->layer->lock);
slave_iter = slave_iter->next;
}
}
struct KeyDefNode * new_key_node(KEY_DEF * key_def) {
struct KeyDefNode * key_node = malloc(sizeof(struct KeyDefNode));
key_node->key_def = key_def;
key_node->next = key_node;
key_node->previous = key_node;
return key_node;
}
struct LayerNode * new_layer_node(struct Layer * layer) {
struct LayerNode * layer_node = malloc(sizeof(struct LayerNode));
layer_node->layer = layer;
layer_node->next = NULL;
return layer_node;
}
struct Layer * new_layer(char * name) {
struct Layer * layer = malloc(sizeof(struct Layer));
layer->name = strdup(name);
layer->state = 0;
layer->lock = 0;
layer->prev_lock = 0;
layer->or_master_layers = NULL;
layer->and_master_layers = NULL;
layer->and_not_master_layers = NULL;
layer->slave_layers = NULL;
layer->next = NULL;
return layer;
}
struct LayerConf * new_layer_conf(struct Layer * layer, void (*conf)(struct Layer * layer)) {
struct LayerConf * layer_conf = malloc(sizeof(struct LayerConf));
layer_conf->layer = layer;
layer_conf->conf = conf;
layer_conf->next = NULL;
return layer_conf;
}
struct Remap * new_remap(KEY_DEF * from,
struct Layer * layer,
struct KeyDefNode * to_when_alone,
struct KeyDefNode * to_with_other,
struct KeyDefNode * to_when_doublepress,
struct KeyDefNode * to_when_tap_lock,
struct KeyDefNode * to_when_double_tap_lock) {
struct Remap * remap = malloc(sizeof(struct Remap));
remap->id = 0;
remap->from = from;
remap->layer = layer;
remap->to_when_press_layer = NULL;
remap->to_when_doublepress_layer = NULL;
remap->to_when_tap_lock_layer = NULL;
remap->to_when_double_tap_lock_layer = NULL;
remap->to_when_alone = to_when_alone;
remap->to_with_other = to_with_other;
remap->to_when_doublepress = to_when_doublepress;
remap->to_when_tap_lock = to_when_tap_lock;
remap->to_when_double_tap_lock = to_when_double_tap_lock;
remap->to_with_other_dummy = 0;
remap->to_when_alone_modifiers = 0;
remap->to_with_other_modifiers = 0;
remap->to_when_doublepress_modifiers = 0;
remap->to_when_tap_lock_modifiers = 0;
remap->to_when_double_tap_lock_modifiers = 0;
remap->to_when_alone_is_modifier_only = 0;
remap->to_when_doublepress_is_modifier_only = 0;
remap->tap_lock = 0;
remap->double_tap_lock = 0;
remap->state = IDLE;
remap->time = 0;
remap->active_modifiers = 0;
remap->next = NULL;
return remap;
}
struct RemapNode * new_remap_node(struct Remap * remap) {
struct RemapNode * remap_node = malloc(sizeof(struct RemapNode));
remap_node->remap = remap;
remap_node->next = NULL;
return remap_node;
}
void append_key_node(struct KeyDefNode * head, KEY_DEF * key_def) {
head->previous->next = new_key_node(key_def);
head->previous->next->previous = head->previous;
head->previous = head->previous->next;
head->previous->next = head;
}
void append_layer_node(struct LayerNode ** list, struct LayerNode * elem) {
while (*list) list = &(*list)->next;
*list = elem;
}
int check_layer_states(struct LayerNode * layer_list, int expected_state) {
while (layer_list) {
if (layer_list->layer->state != expected_state) {
return 0;
}
layer_list = layer_list->next;
}
return 1;
}
int is_master_layer(struct Layer * master_layer, struct Layer * slave_layer) {
struct LayerNode * master_iter = slave_layer->or_master_layers;
while (master_iter) {
if (master_iter->layer == master_layer || is_master_layer(master_layer, master_iter->layer)) {
return master_iter->layer->state;
}
master_iter = master_iter->next;
}
if (check_layer_states(slave_layer->and_master_layers, 1) &&
check_layer_states(slave_layer->and_not_master_layers, 0)) {
master_iter = slave_layer->and_master_layers;
while (master_iter) {
if (master_iter->layer == master_layer || is_master_layer(master_layer, master_iter->layer)) {
return 1;
}
master_iter = master_iter->next;
}
}
return 0;
}
int has_to_block_modifiers(struct Remap * remap, struct Layer * layer) {
return remap && remap->layer &&
(remap->layer == layer || is_master_layer(layer, remap->layer));
}
int key_eq(struct KeyDefNode * head_a, struct KeyDefNode * head_b) {
struct KeyDefNode * cur_a = head_a;
struct KeyDefNode * cur_b = head_b;
while (cur_a && cur_b) {
if (cur_a->key_def != cur_b->key_def) return 0;
cur_a = cur_a->next;
cur_b = cur_b->next;
if (cur_a == head_a && cur_b == head_b) return 1;
}
return 0;
}
int modifiers(struct KeyDefNode * head) {
struct KeyDefNode * cur = head;
int modifiers = 0;
do {
modifiers |= cur->key_def->modifier;
cur = cur->next;
} while (cur != head);
return modifiers;
}
int is_modifier_only(struct KeyDefNode * head)
{
struct KeyDefNode * cur = head;
int modifier_only = 1;
do {
modifier_only *= cur->key_def->modifier;
cur = cur->next;
} while(cur != head);
return modifier_only ? 1 : 0;
}
void free_key_nodes(struct KeyDefNode * head) {
if (head) {
head->previous->next = NULL;
struct KeyDefNode * cur = head;
while (cur) {
struct KeyDefNode * key_node = cur;
cur = cur->next;
free(key_node);
}
}
}
void free_layer_nodes(struct LayerNode * head) {
struct LayerNode * cur = head;
while (cur) {
struct LayerNode * layer_node = cur;
cur = cur->next;
free(layer_node);
}
}
void free_layers(struct Layer * head) {
struct Layer * cur = head;
while (cur) {
struct Layer * layer = cur;
cur = cur->next;
free(layer->name);
free_layer_nodes(layer->or_master_layers);
free_layer_nodes(layer->and_master_layers);
free_layer_nodes(layer->and_not_master_layers);
free_layer_nodes(layer->slave_layers);
free(layer);
}
}
void free_layer_confs(struct LayerConf * head) {
struct LayerConf * cur = head;
while (cur) {
struct LayerConf * layer_conf = cur;
cur = cur->next;
free(layer_conf);
}
}
void free_remap(struct Remap * remap) {
free_layer_confs(remap->to_when_tap_lock_layer);
free_layer_confs(remap->to_when_double_tap_lock_layer);
free_key_nodes(remap->to_when_alone);
free_key_nodes(remap->to_with_other);
free_key_nodes(remap->to_when_doublepress);
free_key_nodes(remap->to_when_tap_lock);
free_key_nodes(remap->to_when_double_tap_lock);
free(remap);
}
void free_remap_nodes(struct RemapNode * head) {
struct RemapNode * cur = head;
while (cur) {
struct RemapNode * remap_node = cur;
cur = cur->next;
free_remap(remap_node->remap);
free(remap_node);
}
}
void free_all() {
free(g_remap_parsee);
g_remap_parsee = NULL;
g_layer_parsee = NULL;
g_remap_list = NULL;
free_layers(g_layer_list);
g_layer_list = NULL;
for (int i = 0; i < 256; i++) {
free_remap_nodes(g_remap_array[i]);
g_remap_array[i] = NULL;
g_remap_by_id[i] = NULL;
}
}
struct Layer * find_layer(struct Layer * list, char * name) {
while (list) {
if (strcmp(list->name, name) == 0) {
return list;
}
list = list->next;
}
return NULL;
}
struct Layer * append_layer(struct Layer ** list, struct Layer * elem) {
while (*list) list = &(*list)->next;
*list = elem;
return *list;
}
void append_layer_conf(struct LayerConf ** list, struct LayerConf * elem) {
while (*list) list = &(*list)->next;
*list = elem;
}
int register_remap(struct Remap * remap) {
if (g_remap_list) {
struct Remap * tail = g_remap_list;
while (tail->next) tail = tail->next;
if (tail->id == 255) return 1;
tail->next = remap;
remap->id = tail->id + 1;
} else {
g_remap_list = remap;
remap->id = 1;
}
g_remap_by_id[remap->id] = remap;
if (key_eq(remap->to_when_alone, remap->to_with_other)) {
free_key_nodes(remap->to_with_other);
remap->to_with_other = NULL;
}
if (key_eq(remap->to_when_alone, remap->to_when_doublepress)) {
free_key_nodes(remap->to_when_doublepress);
remap->to_when_doublepress = NULL;
}
if (remap->to_when_alone) {
remap->to_when_alone_modifiers = modifiers(remap->to_when_alone);
remap->to_when_alone_is_modifier_only = is_modifier_only(remap->to_when_alone);
}
if (remap->to_with_other) {
remap->to_with_other_modifiers = modifiers(remap->to_with_other);
if (!is_modifier_only(remap->to_with_other)) {
free_key_nodes(remap->to_with_other);
remap->to_with_other = NULL;
remap->to_with_other_modifiers = 0;
}
}
if (remap->to_when_doublepress) {
remap->to_when_doublepress_modifiers = modifiers(remap->to_when_doublepress);
remap->to_when_doublepress_is_modifier_only = is_modifier_only(remap->to_when_doublepress);
}
if (remap->to_when_tap_lock) {
remap->to_when_tap_lock_modifiers = modifiers(remap->to_when_tap_lock);
}
if (remap->to_when_double_tap_lock) {
remap->to_when_double_tap_lock_modifiers = modifiers(remap->to_when_double_tap_lock);
}
return 0;
}
int remap_list_depth() {
int depth = 0;
struct Remap * remap_iter = g_remap_list;
while (remap_iter) {
depth++;
remap_iter = remap_iter->next;
}
return depth;
}
void append_active_remap(struct Remap ** list, struct Remap * elem) {
while (*list) {
if (*list == elem) {
DEBUG(1, debug_print(RED, "\nRemap list depth = %d", remap_list_depth()));
return;
}
list = &(*list)->next;
}
*list = elem;
elem->next = NULL;
DEBUG(1, debug_print(RED, "\nRemap list depth = %d", remap_list_depth()));
}
void remove_active_remap(struct Remap ** list, struct Remap * elem) {
while (*list && *list != elem) {
list = &(*list)->next;
}
if (*list) {
*list = (*list)->next;
elem->next = NULL;
}
DEBUG(1, debug_print(RED, "\nRemap list depth = %d", remap_list_depth()));
}
int send_key_def_input_down(char * input_name, struct KeyDefNode * head, int remap_id, int modifiers_mask, struct InputBuffer * input_buffer) {
int key_sent = 0;
struct KeyDefNode * cur = head;
do {
if (!(modifiers_mask & KEY_ARRAY[cur->key_def->virt_code & 0xFF].modifier)) {
log_send_input(input_name, cur->key_def, DOWN);
send_input(cur->key_def->scan_code, cur->key_def->virt_code, DOWN, remap_id, input_buffer);
key_sent = 1;
}
cur = cur->next;
} while (cur != head);
return key_sent;
}
int send_key_def_input_up(char * input_name, struct KeyDefNode * head, int remap_id, int modifiers_mask, struct InputBuffer * input_buffer) {
int key_sent = 0;
struct KeyDefNode * cur = head;
do {
cur = cur->previous;
if (!(modifiers_mask & KEY_ARRAY[cur->key_def->virt_code & 0xFF].modifier)) {
log_send_input(input_name, cur->key_def, UP);
send_input(cur->key_def->scan_code, cur->key_def->virt_code, UP, remap_id, input_buffer);
key_sent = 1;
}
} while (cur != head);
return key_sent;
}
void unlock_all(struct InputBuffer * input_buffer) {
struct Layer * layer_iter = g_layer_list;
while (layer_iter) {
layer_iter->state = 0;
layer_iter->lock = 0;
layer_iter->prev_lock = 0;
layer_iter = layer_iter->next;
}
struct Remap * remap_iter = g_remap_list;
while (remap_iter) {
g_remap_list = remap_iter->next;
if (remap_iter->state == HELD_DOWN_ALONE) {
} else if (remap_iter->state == HELD_DOWN_WITH_OTHER) {
if (remap_iter->to_with_other) {
send_key_def_input_up("unlock_with_other", remap_iter->to_with_other, remap_iter->id, 0, input_buffer);
}
} else if (remap_iter->state == TAP) {
if (remap_iter->to_when_alone) {
send_key_def_input_up("unlock_when_alone", remap_iter->to_when_alone, remap_iter->id, 0, input_buffer);
}
} else if (remap_iter->state == DOUBLE_TAP) {
if (remap_iter->to_when_doublepress) {
send_key_def_input_up("unlock_when_doublepress", remap_iter->to_when_doublepress, remap_iter->id, 0, input_buffer);
}
}
if (remap_iter->double_tap_lock) {
send_key_def_input_up("unlock_when_double_tap_lock", remap_iter->to_when_double_tap_lock, remap_iter->id, 0, input_buffer);
remap_iter->double_tap_lock = 0;
}
if (remap_iter->tap_lock) {
send_key_def_input_up("unlock_when_tap_lock", remap_iter->to_when_tap_lock, remap_iter->id, 0, input_buffer);
remap_iter->tap_lock = 0;
}
remap_iter->state = IDLE;
remap_iter->active_modifiers = 0;
remap_iter->next = NULL;
remap_iter = g_remap_list;
DEBUG(1, debug_print(RED, "\nRemap list depth = %d", remap_list_depth()));
}
}
/* @return block_input */
int event_remapped_key_down(struct Remap * remap, DWORD time, struct InputBuffer * input_buffer) {
if (remap->state == IDLE) {
if (remap->to_with_other || remap->to_with_other_dummy) {
remap->time = time;
remap->state = HELD_DOWN_ALONE;
} else {
remap->time = time;
remap->state = TAP;
if (remap->to_when_alone) {
send_key_def_input_down("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_when_alone_modifiers;
}
}
if (remap->to_when_press_layer) {
set_layer_state(remap->to_when_press_layer, 1);
}
append_active_remap(&g_remap_list, remap);
} else if (remap->state == HELD_DOWN_WITH_OTHER) {
if (remap->to_with_other) {
send_key_def_input_down("with_other", remap->to_with_other, remap->id, 0, input_buffer);
}
} else if (remap->state == TAP) {
if (remap->to_when_alone) {
send_key_def_input_down("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
}
} else if (remap->state == TAPPED) {
remap->time = time;
remap->state = DOUBLE_TAP;
if (remap->to_when_tap_lock) {
remap->tap_lock = 1 - remap->tap_lock;
if (remap->tap_lock == 0) {
send_key_def_input_up("when_tap_lock", remap->to_when_tap_lock, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
}
struct LayerConf * layer_conf = remap->to_when_tap_lock_layer;
while (layer_conf) {
layer_conf->layer->lock = layer_conf->layer->prev_lock;
set_layer_state(layer_conf->layer, layer_conf->layer->lock);
layer_conf = layer_conf->next;
}
if (remap->to_when_doublepress_layer) {
set_layer_state(remap->to_when_doublepress_layer, 1);
}
if (remap->to_when_doublepress) {
send_key_def_input_down("when_doublepress", remap->to_when_doublepress, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_when_doublepress_modifiers;
} else if (remap->to_when_doublepress_layer) {
} else if (remap->to_when_alone) {
send_key_def_input_down("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_when_alone_modifiers;
}
} else if (remap->state == DOUBLE_TAP) {
if (remap->to_when_doublepress) {
send_key_def_input_down("when_doublepress", remap->to_when_doublepress, remap->id, 0, input_buffer);
} else if (remap->to_when_doublepress_layer) {
} else if (remap->to_when_alone) {
send_key_def_input_down("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
}
}
return 1;
}
/* @return block_input */
int event_remapped_key_up(struct Remap * remap, DWORD time, struct InputBuffer * input_buffer) {
if (remap->state == HELD_DOWN_ALONE) {
if ((g_tap_timeout == 0) || (time - remap->time < g_tap_timeout)) {
remap->time = time;
if (g_doublepress_timeout > 0)
remap->state = TAPPED;
else
remap->state = IDLE;
if (remap->to_when_alone) {
send_key_def_input_down("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
send_key_def_input_up("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
}
if (remap->to_when_tap_lock) {
remap->tap_lock = 1 - remap->tap_lock;
if (remap->tap_lock) {
send_key_def_input_down("when_tap_lock", remap->to_when_tap_lock, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_when_tap_lock_modifiers;
} else {
send_key_def_input_up("when_tap_lock", remap->to_when_tap_lock, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
}
struct LayerConf * layer_conf = remap->to_when_tap_lock_layer;
while (layer_conf) {
layer_conf->conf(layer_conf->layer);
set_layer_state(layer_conf->layer, layer_conf->layer->lock);
layer_conf = layer_conf->next;
}
} else {
remap->state = IDLE;
}
if (remap->to_when_press_layer) {
set_layer_state(remap->to_when_press_layer, remap->to_when_press_layer->lock);
}
} else if (remap->state == HELD_DOWN_WITH_OTHER) {
remap->state = IDLE;
if (remap->to_with_other) {
send_key_def_input_up("with_other", remap->to_with_other, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
if (remap->to_when_press_layer) {
set_layer_state(remap->to_when_press_layer, remap->to_when_press_layer->lock);
}
} else if (remap->state == TAP) {
if ((g_tap_timeout == 0) || (time - remap->time < g_tap_timeout)) {
remap->time = time;
if (g_doublepress_timeout > 0)
remap->state = TAPPED;
else
remap->state = IDLE;
if (remap->to_when_alone) {
send_key_def_input_up("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
if (remap->to_when_tap_lock) {
remap->tap_lock = 1 - remap->tap_lock;
if (remap->tap_lock) {
send_key_def_input_down("when_tap_lock", remap->to_when_tap_lock, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_when_tap_lock_modifiers;
} else {
send_key_def_input_up("when_tap_lock", remap->to_when_tap_lock, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
}
struct LayerConf * layer_conf = remap->to_when_tap_lock_layer;
while (layer_conf) {
layer_conf->conf(layer_conf->layer);
set_layer_state(layer_conf->layer, layer_conf->layer->lock);
layer_conf = layer_conf->next;
}
} else {
remap->state = IDLE;
if (remap->to_when_alone) {
send_key_def_input_up("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
}
if (remap->to_when_press_layer) {
set_layer_state(remap->to_when_press_layer, remap->to_when_press_layer->lock);
}
} else if (remap->state == DOUBLE_TAP) {
remap->state = IDLE;
if (remap->to_when_doublepress) {
send_key_def_input_up("when_doublepress", remap->to_when_doublepress, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
} else if (remap->to_when_doublepress_layer) {
} else if (remap->to_when_alone) {
send_key_def_input_up("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
if ((g_tap_timeout == 0) || (time - remap->time < g_tap_timeout)) {
if (remap->to_when_double_tap_lock) {
remap->double_tap_lock = 1 - remap->double_tap_lock;
if (remap->double_tap_lock) {
send_key_def_input_down("when_double_tap_lock", remap->to_when_double_tap_lock, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_when_double_tap_lock_modifiers;
} else {
send_key_def_input_up("when_double_tap_lock", remap->to_when_double_tap_lock, remap->id, 0, input_buffer);
remap->active_modifiers = 0;
}
}
struct LayerConf * layer_conf = remap->to_when_double_tap_lock_layer;
while (layer_conf) {
layer_conf->conf(layer_conf->layer);
set_layer_state(layer_conf->layer, layer_conf->layer->lock);
layer_conf = layer_conf->next;
}
}
if (remap->to_when_doublepress_layer) {
set_layer_state(remap->to_when_doublepress_layer, remap->to_when_doublepress_layer->lock);
}
}
if (remap->state == IDLE && remap->tap_lock == 0 && remap->double_tap_lock == 0) {
remove_active_remap(&g_remap_list, remap);
}
return 1;
}
/* @return block_input */
int event_other_input(int virt_code, enum Direction direction, DWORD time, int remap_id, struct InputBuffer * input_buffer) {
int block_input = 0;
if (direction == DOWN && !KEY_ARRAY[virt_code & 0xFF].modifier) {
struct Remap * remap = g_remap_list;
while (remap) {
if (remap->id != remap_id) {
if (remap->state == HELD_DOWN_ALONE) {
if ((g_hold_delay > 0) && (time - remap->time < g_hold_delay) && remap->to_when_alone) {
remap->state = TAP;
block_input |= send_key_def_input_down("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_when_alone_modifiers;
} else {
if (!has_to_block_modifiers(g_remap_by_id[remap_id], remap->to_when_press_layer)) {
remap->state = HELD_DOWN_WITH_OTHER;
if (remap->to_with_other) {
block_input |= send_key_def_input_down("with_other", remap->to_with_other, remap->id, 0, input_buffer);
remap->active_modifiers = remap->to_with_other_modifiers;
}
}
}
} else if (remap->state == HELD_DOWN_WITH_OTHER) {
if (remap->to_with_other) {
if (!has_to_block_modifiers(g_remap_by_id[remap_id], remap->to_when_press_layer)) {
block_input |= send_key_def_input_down("with_other", remap->to_with_other, remap->id, 0, input_buffer);
} else {
block_input |= send_key_def_input_up("with_other", remap->to_with_other, remap->id, g_remap_by_id[remap_id]->active_modifiers, input_buffer);
}
}
} else if (remap->state == TAP) {
if (remap->to_when_alone && remap->to_when_alone_is_modifier_only) {
if (!has_to_block_modifiers(g_remap_by_id[remap_id], remap->to_when_press_layer)) {
block_input |= send_key_def_input_down("when_alone", remap->to_when_alone, remap->id, 0, input_buffer);
} else {
block_input |= send_key_def_input_up("when_alone", remap->to_when_alone, remap->id, g_remap_by_id[remap_id]->active_modifiers, input_buffer);
}
}
} else if (remap->state == DOUBLE_TAP) {
if (remap->to_when_doublepress && remap->to_when_doublepress_is_modifier_only) {
if (!has_to_block_modifiers(g_remap_by_id[remap_id], remap->to_when_doublepress_layer)) {
block_input |= send_key_def_input_down("when_doublepress", remap->to_when_doublepress, remap->id, 0, input_buffer);
} else {
block_input |= send_key_def_input_up("when_doublepress", remap->to_when_doublepress, remap->id, g_remap_by_id[remap_id]->active_modifiers, input_buffer);
}
}
} else {
if (remap->double_tap_lock) {
block_input |= send_key_def_input_down("when_double_tap_lock", remap->to_when_double_tap_lock, remap->id, 0, input_buffer);
}
if (remap->tap_lock) {
block_input |= send_key_def_input_down("when_tap_lock", remap->to_when_tap_lock, remap->id, 0, input_buffer);
}
}
remap->time = 0; // disable tap and double_tap
}
remap = remap->next;
}
}
return -block_input;
}
/* @return block_input */
int handle_input(int scan_code, int virt_code, enum Direction direction, DWORD time, int is_injected, DWORD flags, ULONG_PTR dwExtraInfo, struct InputBuffer * input_buffer) {
struct Remap * remap_for_input;
int block_input;
int remap_id = 0; // if 0 then no remapped injected key
log_handle_input_start(scan_code, virt_code, direction, is_injected, flags, dwExtraInfo);
if ((g_unlock_timeout > 0) && (time - g_last_input > g_unlock_timeout)) {
unlock_all(input_buffer);
}
if (is_injected && ((dwExtraInfo & 0xFFFFFF00) != INJECTED_KEY_ID || dwExtraInfo == INJECTED_KEY_ID)) {
// Note: passthrough of injected keys from other tools or
// from Dual-key-remap self when passthrough is requested (remap_id = 0).
block_input = 0;
if ((g_rehook_timeout > 0) && (time - g_last_input > g_rehook_timeout)) {
rehook();
g_last_input = time;
}
} else if (scan_code == 0x022A) {
// To filter out unwanted key events generated on certain HP laptops
block_input = 1;
} else {
g_last_input = time;
if (is_injected) {
// Note: injected keys are never remapped to avoid complex nested scenarios
remap_for_input = NULL;
remap_id = dwExtraInfo & 0x000000FF;
} else {
struct Remap ** list = &g_remap_list;
while (*list) {
if ((*list)->state == TAPPED && (time - (*list)->time >= g_doublepress_timeout)) {
(*list)->state = IDLE;
if ((*list)->tap_lock == 0 && (*list)->double_tap_lock == 0) {
struct Remap * elem = *list;
*list = (*list)->next;
elem->next = NULL;
DEBUG(1, debug_print(RED, "\nRemap list depth = %d", remap_list_depth()));
continue;
}
} else if ((*list)->from->virt_code == virt_code) {
break;
}
list = &(*list)->next;
}
remap_for_input = *list;
if (remap_for_input == NULL) {
struct RemapNode * remap_node_iter = g_remap_array[virt_code & 0xFF];
while (remap_node_iter) {
if (remap_node_iter->remap->layer == NULL) {
break;
} else if (remap_node_iter->remap->layer->state) {
break;
}
remap_node_iter = remap_node_iter->next;
}
if (remap_node_iter) {
remap_for_input = remap_node_iter->remap;
} else {
// auto_unlock if not modifier
}
}
}
if (remap_for_input) {
if (direction == UP) {
block_input = event_remapped_key_up(remap_for_input, time, input_buffer);
} else {
block_input = event_remapped_key_down(remap_for_input, time, input_buffer);
}
} else {
block_input = event_other_input(virt_code, direction, time, remap_id, input_buffer);
}
}
log_handle_input_end(scan_code, virt_code, direction, block_input);
return block_input;
}
// Config
// ---------------------------------
void trim_newline(char * str) {
str[strcspn(str, "\r\n")] = 0;
int length = strlen(str);
while (length > 0 && isspace((unsigned char)str[length - 1])) {
str[length - 1] = 0;
length--;
}
}
int parsee_is_valid() {
return g_remap_parsee &&
g_remap_parsee->from &&
(g_remap_parsee->to_when_alone || g_remap_parsee->to_with_other ||
g_remap_parsee->to_when_doublepress ||
g_remap_parsee->to_when_tap_lock || g_remap_parsee->to_when_double_tap_lock ||
g_remap_parsee->to_when_press_layer || g_remap_parsee->to_when_doublepress_layer ||
g_remap_parsee->to_when_tap_lock_layer || g_remap_parsee->to_when_double_tap_lock_layer);
}
/* @return error */
int load_config_line(char * line, int linenum) {
if (line == NULL) {
if (parsee_is_valid()) {
if (register_remap(g_remap_parsee)) {
g_remap_parsee = NULL;
printf("Config error (line %d): Exceeded the maximum limit of 255 remappings.\n", linenum);
return 1;
}
g_remap_parsee = NULL;
}
while (g_remap_list) {
struct RemapNode * remap_node = new_remap_node(g_remap_list);