-
Notifications
You must be signed in to change notification settings - Fork 5
/
mapper.c
304 lines (233 loc) · 4.92 KB
/
mapper.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/param.h>
#include <string.h>
#include "compat.h"
#include "mapper.h"
/* Represent the different values for one key
*/
struct key
{
char *normal;
char *shift;
char *altgr;
char *fn;
};
/* Represent the keyboard : all of the different values for all of the keys.
* Plus, it stores some attributes within the flags variable.
*/
struct map
{
struct key key[KEY_CNT];
uint32_t flags;
#define MAP_F_CAPS (1 << 0)
#define MAP_F_LSHIFT (1 << 1)
#define MAP_F_RSHIFT (1 << 2)
#define MAP_F_ALTGR (1 << 3)
#define MAP_F_CTRL (1 << 4)
#define MAP_F_NLOCK (1 << 5)
};
enum event_value
{
EVENT_RELEASED = 0, /* key released */
EVENT_PRESSED, /* key pressed */
EVENT_REPEAT, /* key switches to repeating after short delay */
};
static void mapper_init_key(struct key *key)
{
key->normal = key->shift = key->altgr = key->fn = NULL;
}
static int mapper_line_to_key(char *line, size_t len, struct key *key)
{
char *save,
*token,
*ptr;
size_t i = 0;
mapper_init_key(key);
/* The line represent the "normal" "shift" "altgr" "fn" key
*/
for ( ptr = line, i = 0 ; i < 5 ; ptr = NULL, i++ )
{
token = strtok_r(ptr, " \t", &save);
if ( token == NULL )
break;
switch ( i )
{
case 0:
if ( (key->normal = strdup(token)) == NULL )
return -1;
break;
case 1:
if ( (key->shift = strdup(token)) == NULL )
return -1;
break;
case 2:
if ( (key->altgr = strdup(token)) == NULL )
return -1;
break;
case 4:
if ( (key->fn = strdup(token)) == NULL )
return -1;
break;
}
}
return 0;
}
static int mapper_set_map(FILE *file, struct map *map)
{
char *line = NULL;
size_t i = 0;
size_t len = 0;
ssize_t read;
int ret = 0;
map->flags = 0;
/* The event code 0 is reserved (see linux/input.h with EV_RESERVED)
*/
mapper_init_key(map->key + i++);
/* Each line represents the key code event
*/
while ( (read = getline(&line, &len, file)) != -1 )
{
if ( mapper_line_to_key(line, len, map->key + i) )
{
ret = -1;
break;
}
if ( ++i >= KEY_CNT )
break;
}
free(line);
for ( ; i < KEY_CNT ; i++ )
mapper_init_key(map->key + i);
return ret;
}
struct map *mapper_load(const char *filename)
{
FILE *file;
struct map *map;
if ( filename == NULL || (file = fopen(filename, "r")) == NULL )
return NULL;
if ( (map = calloc(1, sizeof(struct map))) == NULL )
goto error;
if ( mapper_set_map(file, map) )
{
mapper_unload(map);
map = NULL;
goto error;
}
error:
fclose(file);
return map;
}
static void mapper_del_key(struct key *key)
{
if ( key == NULL )
return;
free(key->normal);
free(key->shift);
free(key->altgr);
free(key->fn);
}
void mapper_unload(struct map *map)
{
size_t i;
if ( map == NULL )
return;
for ( i = 0 ; i < KEY_CNT ; i++ )
mapper_del_key(map->key + i);
free(map);
}
static int mapper_is_shift(const struct map *map)
{
if ( (map->flags & (MAP_F_LSHIFT|MAP_F_RSHIFT)) &&
(map->flags & MAP_F_CAPS) == 0 )
return 1;
if ( (map->flags & (MAP_F_LSHIFT|MAP_F_RSHIFT)) == 0 &&
(map->flags & MAP_F_CAPS) )
return 1;
return 0;
}
static int mapper_is_altgr(const struct map *map)
{
return ((map->flags & MAP_F_ALTGR) == 0) ? 0 : 1;
}
static const char *mapper_get_key_pressed(uint16_t code, struct map *map)
{
if ( !(code > 0 && code < KEY_CNT) )
return NULL;
/* Handle special keys for the next run
*/
switch ( code )
{
case KEY_RIGHTSHIFT:
map->flags |= MAP_F_RSHIFT;
break;
case KEY_LEFTSHIFT:
map->flags |= MAP_F_LSHIFT;
break;
case KEY_RIGHTALT:
map->flags |= MAP_F_ALTGR;
break;
/* Capslock is THE special one among of specials
*/
case KEY_CAPSLOCK:
if ( (map->flags & MAP_F_CAPS) == 0 )
map->flags |= MAP_F_CAPS;
else
map->flags &= ~MAP_F_CAPS;
break;
default:
if ( mapper_is_shift(map) )
return map->key[code].shift;
if ( mapper_is_altgr(map) )
return map->key[code].altgr;
}
return map->key[code].normal;
}
static const char *mapper_get_key_released(uint16_t code, struct map *map)
{
/* Handle the special keys for the next run
*/
switch ( code )
{
case KEY_RIGHTSHIFT:
map->flags &= ~MAP_F_RSHIFT;
break;
case KEY_LEFTSHIFT:
map->flags &= ~MAP_F_LSHIFT;
break;
case KEY_RIGHTALT:
map->flags &= ~MAP_F_ALTGR;
break;
}
return NULL;
}
const char *mapper_get_key(const struct input_event *event, struct map *map)
{
if ( event == NULL || map == NULL )
return NULL;
/* Keyboard events are always of type EV_KEY
*/
if ( event->type != EV_KEY )
return NULL;
switch ( event->value )
{
/* The capslock should not be processed twice.
*/
case EVENT_REPEAT:
if ( event->code == KEY_CAPSLOCK )
return NULL;
/* FALLTHROUGH
*/
case EVENT_PRESSED:
return mapper_get_key_pressed(event->code, map);
case EVENT_RELEASED:
return mapper_get_key_released(event->code, map);
}
/* Should not get here
*/
return NULL;
}