Newer
Older
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2018 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
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
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <string.h>
#include "log.h"
#include "parse.h"
#include "bitmask.h"
#include "kbd.h"
#include "kbd_internal.h"
const KeyboardProperties anyKeyboard = {
.type = KBD_TYPE_Any,
.vendor = 0,
.product = 0
};
int
parseKeyboardProperties (KeyboardProperties *properties, const char *string) {
enum {
KBD_PARM_TYPE,
KBD_PARM_VENDOR,
KBD_PARM_PRODUCT
};
static const char *const names[] = {"type", "vendor", "product", NULL};
char **parameters = getParameters(names, NULL, string);
int ok = 1;
logParameters(names, parameters, "Keyboard Property");
*properties = anyKeyboard;
if (*parameters[KBD_PARM_TYPE]) {
static const KeyboardType types[] = {KBD_TYPE_Any, KBD_TYPE_PS2, KBD_TYPE_USB, KBD_TYPE_Bluetooth};
static const char *choices[] = {"any", "ps2", "usb", "bluetooth", NULL};
unsigned int choice;
if (validateChoice(&choice, parameters[KBD_PARM_TYPE], choices)) {
properties->type = types[choice];
} else {
logMessage(LOG_WARNING, "invalid keyboard type: %s", parameters[KBD_PARM_TYPE]);
ok = 0;
}
}
if (*parameters[KBD_PARM_VENDOR]) {
static const int minimum = 0;
static const int maximum = 0XFFFF;
int value;
if (validateInteger(&value, parameters[KBD_PARM_VENDOR], &minimum, &maximum)) {
properties->vendor = value;
} else {
logMessage(LOG_WARNING, "invalid keyboard vendor code: %s", parameters[KBD_PARM_VENDOR]);
ok = 0;
}
}
if (*parameters[KBD_PARM_PRODUCT]) {
static const int minimum = 0;
static const int maximum = 0XFFFF;
int value;
if (validateInteger(&value, parameters[KBD_PARM_PRODUCT], &minimum, &maximum)) {
properties->product = value;
} else {
logMessage(LOG_WARNING, "invalid keyboard product code: %s", parameters[KBD_PARM_PRODUCT]);
ok = 0;
}
}
deallocateStrings(parameters);
return ok;
}
int
checkKeyboardProperties (const KeyboardProperties *actual, const KeyboardProperties *required) {
if (!required) return 1;
if (!actual) actual = &anyKeyboard;
if (required->type != KBD_TYPE_Any) {
if (required->type != actual->type) return 0;
}
if (required->vendor) {
if (required->vendor != actual->vendor) return 0;
}
if (required->product) {
if (required->product != actual->product) return 0;
}
return 1;
}
static void
logKeyEvent (const char *action, int code, int press) {
logMessage(LOG_CATEGORY(KEYBOARD_KEYS),
"%s %d: %s",
(press? "press": "release"), code, action);
}
static void
flushKeyEvents (KeyboardInstanceObject *kio) {
const KeyEventEntry *event = kio->events.buffer;
while (kio->events.count) {
logKeyEvent("flushing", event->code, event->press);
forwardKeyEvent(kio, event->code, event->press);
event += 1;
kio->events.count -= 1;
}
memset(kio->deferred.mask, 0, kio->deferred.size);
kio->deferred.modifiersOnly = 0;
}
KeyboardInstanceObject *
newKeyboardInstanceObject (KeyboardMonitorObject *kmo) {
KeyboardInstanceObject *kio;
unsigned int count = BITMASK_ELEMENT_COUNT(keyCodeCount, BITMASK_ELEMENT_SIZE(unsigned char));
size_t size = sizeof(*kio) + count;
if ((kio = malloc(size))) {
memset(kio, 0, size);
kio->kmo = kmo;
kio->actualProperties = anyKeyboard;
kio->events.buffer = NULL;
kio->events.size = 0;
kio->events.count = 0;
kio->deferred.modifiersOnly = 0;
kio->deferred.size = count;
if (newKeyboardInstanceExtension(&kio->kix)) {
if (enqueueItem(kmo->instanceQueue, kio)) {
return kio;
}
destroyKeyboardInstanceExtension(kio->kix);
}
free(kio);
} else {
logMallocError();
}
return NULL;
}
void
destroyKeyboardInstanceObject (KeyboardInstanceObject *kio) {
flushKeyEvents(kio);
if (kio->events.buffer) free(kio->events.buffer);
deleteItem(kio->kmo->instanceQueue, kio);
if (kio->kix) destroyKeyboardInstanceExtension(kio->kix);
free(kio);
}
void
destroyKeyboardMonitorObject (KeyboardMonitorObject *kmo) {
kmo->isActive = 0;
while (getQueueSize(kmo->instanceQueue) > 0) {
Element *element = getQueueHead(kmo->instanceQueue);
KeyboardInstanceObject *kio = getElementItem(element);
destroyKeyboardInstanceObject(kio);
}
if (kmo->instanceQueue) deallocateQueue(kmo->instanceQueue);
if (kmo->kmx) destroyKeyboardMonitorExtension(kmo->kmx);
free(kmo);
}
KeyboardMonitorObject *
newKeyboardMonitorObject (const KeyboardProperties *properties, KeyEventHandler handleKeyEvent) {
KeyboardMonitorObject *kmo;
if ((kmo = malloc(sizeof(*kmo)))) {
memset(kmo, 0, sizeof(*kmo));
kmo->requiredProperties = *properties;
kmo->handleKeyEvent = handleKeyEvent;
if (newKeyboardMonitorExtension(&kmo->kmx)) {
if ((kmo->instanceQueue = newQueue(NULL, NULL))) {
if (monitorKeyboards(kmo)) {
kmo->isActive = 1;
return kmo;
}
deallocateQueue(kmo->instanceQueue);
}
destroyKeyboardMonitorExtension(kmo->kmx);
}
free(kmo);
} else {
logMallocError();
}
return NULL;
}
void
handleKeyEvent (KeyboardInstanceObject *kio, int code, int press) {
KeyTableState state = KTS_UNBOUND;
logKeyEvent("received", code, press);
if (kio->kmo->isActive) {
if ((code >= 0) && (code < keyCodeCount)) {
const KeyValue *kv = &keyCodeMap[code];
if ((kv->group != KBD_GROUP(SPECIAL)) || (kv->number != KBD_KEY(SPECIAL, Unmapped))) {
if ((kv->group == KBD_GROUP(SPECIAL)) && (kv->number == KBD_KEY(SPECIAL, Ignore))) return;
state = kio->kmo->handleKeyEvent(kv->group, kv->number, press);
}
}
}
if (state == KTS_HOTKEY) {
logKeyEvent("ignoring", code, press);
} else {
typedef enum {
WKA_NONE,
WKA_CURRENT,
WKA_ALL
} WriteKeysAction;
WriteKeysAction action = WKA_NONE;
if (press) {
kio->deferred.modifiersOnly = state == KTS_MODIFIERS;
if (state == KTS_UNBOUND) {
action = WKA_ALL;
} else {
if (kio->events.count == kio->events.size) {
unsigned int newSize = kio->events.size? kio->events.size<<1: 0X1;
KeyEventEntry *newBuffer = realloc(kio->events.buffer, (newSize * sizeof(*newBuffer)));
if (newBuffer) {
kio->events.buffer = newBuffer;
kio->events.size = newSize;
} else {
logMallocError();
}
}
if (kio->events.count < kio->events.size) {
KeyEventEntry *event = &kio->events.buffer[kio->events.count++];
event->code = code;
event->press = press;
BITMASK_SET(kio->deferred.mask, code);
logKeyEvent("deferring", code, press);
} else {
logKeyEvent("discarding", code, press);
}
}
} else if (kio->deferred.modifiersOnly) {
kio->deferred.modifiersOnly = 0;
action = WKA_ALL;
} else if (BITMASK_TEST(kio->deferred.mask, code)) {
KeyEventEntry *to = kio->events.buffer;
const KeyEventEntry *from = to;
unsigned int count = kio->events.count;
while (count) {
if (from->code == code) {
logKeyEvent("dropping", from->code, from->press);
} else if (to != from) {
*to++ = *from;
} else {
to += 1;
}
from += 1, count -= 1;
}
kio->events.count = to - kio->events.buffer;
BITMASK_CLEAR(kio->deferred.mask, code);
} else {
action = WKA_CURRENT;
}
switch (action) {
case WKA_ALL:
flushKeyEvents(kio);
case WKA_CURRENT:
logKeyEvent("forwarding", code, press);
forwardKeyEvent(kio, code, press);
case WKA_NONE:
break;
}
}
}