-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_kbd.c
312 lines (274 loc) · 7.13 KB
/
input_kbd.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
/* *******************************************
* frabenu - Framebuffer menu
* Copyright (C) 2018 Frank Mueller
*
* SPDX-License-Identifier: GPL-2.0-or-later
* *******************************************/
#include "input_kbd.h"
#include "debug.h"
#include "timer.h"
#include <linux/input.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
typedef enum CSISeqState
{
CSI_none, // no CSI sequence ist stated
CSI_ESCread, // just ESC readed
CSI_waitForEnd, // ESC+[ readed, wait for end of seq
} CSISeqState;
static int init = 0;
static CSISeqState curCSISeqState = CSI_none;
static int kbdPipe[2] = { -1, -1 }; // 0-r, 1-w
static int escTimeout = 1000; // ms
static struct timespec startCSI;
#define CSI_1 0x1B /* ESC */
#define CSI_2 0x5B /* [ */
#define CSI_F_FIRST 0x40
#define CSI_F_LAST 0x7E
typedef struct {
uint8_t stdIn; // normal stdIn data or CSI final byte
uint16_t keyCode; // key code
} CSI_F_2_Key;
static CSI_F_2_Key mapNormal_2_KeyCode[] = {
{0x1B, KEY_ESC},
{'0', KEY_0},
{'1', KEY_1},
{'2', KEY_2},
{'3', KEY_3},
{'4', KEY_4},
{'5', KEY_5},
{'6', KEY_6},
{'7', KEY_7},
{'8', KEY_8},
{'9', KEY_9},
{' ', KEY_SPACE},
{'q', KEY_Q},
{'Q', KEY_Q},
{0x0A, KEY_ENTER},
};
#define MAPNORMAL_2_KEYCODE_COUNT (sizeof(mapNormal_2_KeyCode)/sizeof(mapNormal_2_KeyCode[0]))
static CSI_F_2_Key mapCSI_F_2_KeyCode[] = {
{ 'A', KEY_UP},
{ 'B', KEY_DOWN},
{ 'C', KEY_RIGHT},
{ 'D', KEY_LEFT},
};
#define MAPCSI_F_2_KEYCODE_COUNT (sizeof(mapCSI_F_2_KeyCode)/sizeof(mapCSI_F_2_KeyCode[0]))
static int kbd_select1[] = {KEY_1, 0};
static int kbd_select2[] = {KEY_2, 0};
static int kbd_select3[] = {KEY_3, 0};
static int kbd_select4[] = {KEY_4, 0};
static int kbd_select5[] = {KEY_5, 0};
static int kbd_select6[] = {KEY_6, 0};
static int kbd_select7[] = {KEY_7, 0};
static int kbd_select8[] = {KEY_8, 0};
static int kbd_select9[] = {KEY_9, 0};
static int kbd_select10[] = {KEY_0, 0};
static int kbd_left[] = {KEY_LEFT, 0};
static int kbd_right[] = {KEY_RIGHT, 0};
static int kbd_up[] = {KEY_UP, 0};
static int kbd_down[] = {KEY_DOWN, 0};
static int kbd_select[] = {KEY_ENTER, KEY_SPACE, 0};
static int kbd_abort[] = {KEY_ESC, KEY_Q, 0};
static event_map kbd_event_map = {NULL,
kbd_select1,
kbd_select2,
kbd_select3,
kbd_select4,
kbd_select5,
kbd_select6,
kbd_select7,
kbd_select8,
kbd_select9,
kbd_select10,
kbd_left,
kbd_right,
kbd_up,
kbd_down,
kbd_select,
kbd_abort};
static int16_t map2keyCode(const CSI_F_2_Key *map, size_t size, int8_t stdIn)
{
int16_t keyCode = KEY_RESERVED;
int i;
for (i = 0; i < size; ++i)
{
if (map[i].stdIn == stdIn)
{
keyCode = map[i].keyCode;
break;
}
}
return keyCode;
}
#define normalMap2keyCode(stdIn) map2keyCode(mapNormal_2_KeyCode, MAPNORMAL_2_KEYCODE_COUNT, stdIn)
#define csi_F_Map2keyCode(stdIn) map2keyCode(mapCSI_F_2_KeyCode, MAPCSI_F_2_KEYCODE_COUNT, stdIn)
static void readNextStdIn()
{
uint8_t keyIn;
uint16_t keyOut;
ssize_t retval;
retval = read(STDIN_FILENO, &keyIn, sizeof(keyIn));
if (retval > 0)
{
switch (curCSISeqState)
{
case CSI_none:
if (keyIn == CSI_1)
{
curCSISeqState = CSI_ESCread;
getCurClock(&startCSI);
}
else
{
keyOut = normalMap2keyCode(keyIn);
if (keyOut != KEY_RESERVED)
{
write(kbdPipe[1], &keyOut, sizeof(keyOut));
}
}
break;
case CSI_ESCread:
if (keyIn == CSI_2)
{
curCSISeqState = CSI_waitForEnd;
}
else
{
keyOut = KEY_ESC;
write(kbdPipe[1], &keyOut, sizeof(keyOut));
if (keyIn != CSI_1)
{
keyOut = normalMap2keyCode(keyIn);;
if (keyOut != KEY_RESERVED)
{
write(kbdPipe[1], &keyOut, sizeof(keyOut));
}
curCSISeqState = CSI_none;
}
}
break;
case CSI_waitForEnd:
if ((keyIn >= CSI_F_FIRST) && (keyIn <= CSI_F_LAST))
{
keyOut = csi_F_Map2keyCode(keyIn);
if (keyOut != KEY_RESERVED)
{
write(kbdPipe[1], &keyOut, sizeof(keyOut));
}
curCSISeqState = CSI_none;
}
break;
}
}
else
{
debugOut(debug_level2, "KBD error stdin\n");
}
}
int kbd_init()
{
if (!init)
{
int retval;
char *env;
curCSISeqState = CSI_none;
retval = pipe(kbdPipe);
if(retval)
{
return -1;
}
env = getenv("ESCDELAY");
if (env != NULL)
{
long val;
char *next;
val = strtol(env, &next, 10);
if ((next != env) && (val > 0) && (val <= INT_MAX))
{
escTimeout = val;
}
}
init = 1;
return 0;
}
else
{
return -1;
}
}
void kbd_task()
{
if (init)
{
if (curCSISeqState != CSI_none)
{
int16_t keyOut = KEY_ESC;
write(kbdPipe[1], &keyOut, sizeof(keyOut));
curCSISeqState = CSI_none;
}
}
}
int kbd_getTaskTimeout()
{
if (init && (curCSISeqState != CSI_none))
{
int to = getTimeout(&startCSI, escTimeout);
if (to < 0) { kbd_task(); }
return to;
}
else
{
return -1;
}
}
void kbd_stop()
{
if (init)
{
close(kbdPipe[1]);
close(kbdPipe[0]);
kbdPipe[1] = kbdPipe[0] = -1;
init = 0;
}
}
int kbd_getFd(int devNr)
{
switch (devNr)
{
case 0:
return STDIN_FILENO;
case 1:
return kbdPipe[0];
default:
return -1;
}
}
input_event kbd_getEvent(int devNr)
{
switch (devNr)
{
case 0:
readNextStdIn();
return input_none;
case 1:
{
uint16_t keyCode;
ssize_t retval;
retval = read(kbdPipe[0], &keyCode, sizeof(keyCode));
if (retval == sizeof(keyCode))
{
return key2event(kbd_event_map, keyCode);
}
else
{
return input_none;
}
}
default:
return input_none;
}
}