-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathremap.c
278 lines (242 loc) · 6.76 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "input.h"
#include "keys.c"
// Types
// --------------------------------------
enum State {
IDLE,
HELD_DOWN_ALONE,
HELD_DOWN_WITH_OTHER,
};
struct Remap
{
KEY_DEF * from;
KEY_DEF * to_when_alone;
KEY_DEF * to_with_other;
enum State state;
struct Remap * next;
};
// Globals
// --------------------------------------
int g_debug = 0;
struct Remap * g_remap_list;
struct Remap * g_remap_parsee = NULL;
// Debug Logging
// --------------------------------------
char * fmt_dir(enum Direction dir)
{
return dir ? "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, int dir, int is_injected)
{
if (!g_debug) return;
print_log_prefix();
printf("[%s] %s %s (scan:0x%02x virt:0x%02x)",
is_injected ? "output" : "input",
friendly_virt_code_name(virt_code),
fmt_dir(dir),
scan_code,
virt_code);
log_indent_level++;
}
void log_handle_input_end(int scan_code, int virt_code, int dir, int is_injected, 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(dir));
}
}
void log_send_input(char * remap_name, KEY_DEF * key, int dir)
{
if (!g_debug) return;
print_log_prefix();
printf("(sending:%s) %s %s",
remap_name,
key ? key->name : "???",
fmt_dir(dir));
}
// Remapping
// -------------------------------------
struct Remap * new_remap(KEY_DEF * from, KEY_DEF * to_when_alone, KEY_DEF * to_with_other)
{
struct Remap * remap = malloc(sizeof(struct Remap));
remap->from = from;
remap->to_when_alone = to_when_alone;
remap->to_with_other = to_with_other;
remap->state = IDLE;
remap->next = NULL;
return remap;
}
void register_remap(struct Remap * remap)
{
if (g_remap_list) {
struct Remap * tail = g_remap_list;
while (tail->next) tail = tail->next;
tail->next = remap;
} else {
g_remap_list = remap;
}
}
struct Remap * find_remap_for_virt_code(int virt_code)
{
struct Remap * remap = g_remap_list;
while(remap) {
if (remap->from->virt_code == virt_code) {
return remap;
}
remap = remap->next;
}
return NULL;
}
void send_key_def_input(char * input_name, KEY_DEF * key_def, enum Direction dir)
{
log_send_input(input_name, key_def, dir);
send_input(key_def->scan_code, key_def->virt_code, dir);
}
/* @return block_input */
int event_remapped_key_down(struct Remap * remap)
{
if (remap->state == IDLE) {
remap->state = HELD_DOWN_ALONE;
}
return 1;
}
/* @return block_input */
int event_remapped_key_up(struct Remap * remap)
{
if (remap->state == HELD_DOWN_WITH_OTHER) {
remap->state = IDLE;
send_key_def_input("with_other", remap->to_with_other, UP);
} else {
remap->state = IDLE;
send_key_def_input("when_alone", remap->to_when_alone, DOWN);
send_key_def_input("when_alone", remap->to_when_alone, UP);
}
return 1;
}
/* @return block_input */
int event_other_input()
{
struct Remap * remap = g_remap_list;
while(remap) {
if (remap->state == HELD_DOWN_ALONE) {
remap->state = HELD_DOWN_WITH_OTHER;
send_key_def_input("with_other", remap->to_with_other, DOWN);
}
remap = remap->next;
}
return 0;
}
/* @return block_input */
int handle_input(int scan_code, int virt_code, int direction, int is_injected)
{
log_handle_input_start(scan_code, virt_code, direction, is_injected);
// Note: injected keys are never remapped to avoid complex nested scenarios
struct Remap * remap_for_input = is_injected ? NULL : find_remap_for_virt_code(virt_code);
int block_input = 0;
if (!remap_for_input) {
block_input = event_other_input(scan_code, virt_code, direction);
} else {
block_input = direction == DOWN
? event_remapped_key_down(remap_for_input)
: event_remapped_key_up(remap_for_input);
}
log_handle_input_end(scan_code, virt_code, direction, is_injected, block_input);
return block_input;
}
// Config
// ---------------------------------
void trim_newline(char * str)
{
str[strcspn(str, "\r\n")] = 0;
}
int parsee_is_valid()
{
return g_remap_parsee &&
g_remap_parsee->from &&
g_remap_parsee->to_when_alone &&
g_remap_parsee->to_with_other;
}
/* @return error */
int load_config_line(char * line, int linenum)
{
trim_newline(line);
// Ignore comments and empty lines
if (line[0] == '#' || line[0] == '\0') {
return 0;
}
// Handle config declaration
if (strstr(line, "debug=1")) {
g_debug = 1;
return 0;
}
if (strstr(line, "debug=0")) {
g_debug = 0;
return 0;
}
// Handle key remappings
char * after_eq = (char *)strchr(line, '=');
if (!after_eq) {
printf("Config error (line %d): Couldn't understand '%s'.\n", linenum, line);
return 1;
}
char * key_name = after_eq + 1;
KEY_DEF * key_def = find_key_def_by_name(key_name);
if (!key_def) {
printf("Config error (line %d): Invalid key name '%s'.\n", linenum, key_name);
printf("Key names were changed in the most recent version. Please review review the wiki for the new names!\n");
return 1;
}
if (g_remap_parsee == NULL) {
g_remap_parsee = new_remap(NULL, NULL, NULL);
}
if (strstr(line, "remap_key=")) {
if (g_remap_parsee->from && !parsee_is_valid()) {
printf("Config error (line %d): Incomplete remapping.\n"
"Each remapping must have a 'remap_key', 'when_alone', and 'with_other'.\n",
linenum);
return 1;
}
g_remap_parsee->from = key_def;
} else if (strstr(line, "when_alone=")) {
g_remap_parsee->to_when_alone = key_def;
} else if (strstr(line, "with_other=")) {
g_remap_parsee->to_with_other = key_def;
} else {
after_eq[0] = 0;
printf("Config error (line %d): Invalid setting '%s'.\n", linenum, line);
return 1;
}
if (parsee_is_valid()) {
register_remap(g_remap_parsee);
g_remap_parsee = NULL;
}
return 0;
}
void reset_config()
{
free(g_remap_parsee);
g_remap_parsee = NULL;
while (g_remap_list) {
struct Remap * remap = g_remap_list;
g_remap_list = remap->next;
free(remap);
}
g_remap_list = NULL;
}