1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <stdio_dev.h>
12#include <input.h>
13#include <linux/input.h>
14
15enum {
16
17 FLAG_NUM_LOCK = 1 << 0,
18 FLAG_CAPS_LOCK = 1 << 1,
19 FLAG_SCROLL_LOCK = 1 << 2,
20
21
22 KEY_RELEASE = 1 << 15,
23 KEY_MASK = 0xfff,
24};
25
26
27
28
29
30
31
32static const uchar kbd_plain_xlate[] = {
33 0xff, 0x1b, '1', '2', '3', '4', '5', '6',
34 '7', '8', '9', '0', '-', '=', '\b', '\t',
35 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
36 'o', 'p', '[', ']', '\r', 0xff, 'a', 's',
37 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
38 '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v',
39 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff,
40 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
42 '8', '9', '-', '4', '5', '6', '+', '1',
43 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 '\r', 0xff, 0xff
46};
47
48static unsigned char kbd_shift_xlate[] = {
49 0xff, 0x1b, '!', '@', '#', '$', '%', '^',
50 '&', '*', '(', ')', '_', '+', '\b', '\t',
51 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
52 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S',
53 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
54 '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V',
55 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
56 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
58 '8', '9', '-', '4', '5', '6', '+', '1',
59 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 '\r', 0xff, 0xff
62};
63
64static unsigned char kbd_ctrl_xlate[] = {
65 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E,
66 '7', '8', '9', '0', 0x1F, '=', '\b', '\t',
67 0x11, 0x17, 0x05, 0x12, 0x14, 0x18, 0x15, 0x09,
68 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13,
69 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';',
70 '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16,
71 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff,
72 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
74 '8', '9', '-', '4', '5', '6', '+', '1',
75 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 '\r', 0xff, 0xff
78};
79
80
81
82
83
84static struct {
85 int kbd_scan_code;
86 char *escape;
87} kbd_to_ansi364[] = {
88 { KEY_UP, "\033[A"},
89 { KEY_DOWN, "\033[B"},
90 { KEY_RIGHT, "\033[C"},
91 { KEY_LEFT, "\033[D"},
92};
93
94
95#define ANSI_CHAR_MAX 3
96
97static int input_queue_ascii(struct input_config *config, int ch)
98{
99 if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
100 if (!config->fifo_out)
101 return -1;
102 else
103 config->fifo_in = 0;
104 } else {
105 if (config->fifo_in + 1 == config->fifo_out)
106 return -1;
107 config->fifo_in++;
108 }
109 config->fifo[config->fifo_in] = (uchar)ch;
110
111 return 0;
112}
113
114int input_tstc(struct input_config *config)
115{
116 if (config->fifo_in == config->fifo_out && config->read_keys) {
117 if (!(*config->read_keys)(config))
118 return 0;
119 }
120 return config->fifo_in != config->fifo_out;
121}
122
123int input_getc(struct input_config *config)
124{
125 int err = 0;
126
127 while (config->fifo_in == config->fifo_out) {
128 if (config->read_keys)
129 err = (*config->read_keys)(config);
130 if (err)
131 return -1;
132 }
133
134 if (++config->fifo_out == INPUT_BUFFER_LEN)
135 config->fifo_out = 0;
136
137 return config->fifo[config->fifo_out];
138}
139
140
141
142
143
144
145
146
147
148
149
150
151static struct input_key_xlate *process_modifier(struct input_config *config,
152 int key, int release)
153{
154 struct input_key_xlate *table;
155 int flip = -1;
156 int i;
157
158
159 assert(config->num_tables > 0);
160 table = &config->table[0];
161 for (i = 1; i < config->num_tables; i++) {
162 struct input_key_xlate *tab = &config->table[i];
163
164 if (key == tab->left_keycode || key == tab->right_keycode)
165 table = tab;
166 }
167
168
169 if (!release) {
170 switch (key) {
171 case KEY_SCROLLLOCK:
172 flip = FLAG_SCROLL_LOCK;
173 break;
174 case KEY_NUMLOCK:
175 flip = FLAG_NUM_LOCK;
176 break;
177 case KEY_CAPSLOCK:
178 flip = FLAG_CAPS_LOCK;
179 break;
180 }
181 }
182
183 if (flip != -1) {
184 int leds = 0;
185
186 config->leds ^= flip;
187 if (config->flags & FLAG_NUM_LOCK)
188 leds |= INPUT_LED_NUM;
189 if (config->flags & FLAG_CAPS_LOCK)
190 leds |= INPUT_LED_CAPS;
191 if (config->flags & FLAG_SCROLL_LOCK)
192 leds |= INPUT_LED_SCROLL;
193 config->leds = leds;
194 }
195
196 return table;
197}
198
199
200
201
202
203
204
205
206
207static int array_search(int *array, int count, int key)
208{
209 int i;
210
211 for (i = 0; i < count; i++) {
212 if (array[i] == key)
213 return i;
214 }
215
216 return -1;
217}
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233static int sort_array_by_ordering(int *dest, int count, int *order,
234 int ocount)
235{
236 int temp[count];
237 int dest_count;
238 int same;
239 int i;
240
241
242 memcpy(temp, dest, count * sizeof(*dest));
243 dest_count = 0;
244
245
246 for (i = 0; i < ocount; i++) {
247 if (array_search(temp, count, order[i]) != -1)
248 dest[dest_count++] = order[i];
249 }
250 same = dest_count;
251
252
253 for (i = 0; i < count; i++) {
254 if (array_search(order, ocount, temp[i]) == -1)
255 dest[dest_count++] = temp[i];
256 }
257 assert(dest_count == count);
258 return same;
259}
260
261
262
263
264
265
266
267
268
269
270
271
272static int input_check_keycodes(struct input_config *config,
273 int keycode[], int num_keycodes, int *same)
274{
275
276 if (!config->num_tables) {
277 debug("%s: No xlate tables: cannot decode keys\n", __func__);
278 return -1;
279 }
280
281
282 *same = sort_array_by_ordering(keycode, num_keycodes,
283 config->prev_keycodes, config->num_prev_keycodes);
284
285 memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
286 config->num_prev_keycodes = num_keycodes;
287
288 return *same != num_keycodes;
289}
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304static int input_keycode_to_ansi364(struct input_config *config,
305 int keycode, char output_ch[], int max_chars)
306{
307 const char *escape;
308 int ch_count;
309 int i;
310
311 for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
312 if (keycode != kbd_to_ansi364[i].kbd_scan_code)
313 continue;
314 for (escape = kbd_to_ansi364[i].escape; *escape; escape++) {
315 if (ch_count < max_chars)
316 output_ch[ch_count] = *escape;
317 ch_count++;
318 }
319 return ch_count;
320 }
321
322 return 0;
323}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347static int input_keycodes_to_ascii(struct input_config *config,
348 int keycode[], int num_keycodes, char output_ch[],
349 int max_chars, int same)
350{
351 struct input_key_xlate *table;
352 int ch_count = 0;
353 int i;
354
355 table = &config->table[0];
356
357
358 for (i = 0; i < num_keycodes; i++) {
359 int key = keycode[i] & KEY_MASK;
360
361 if (key >= table->num_entries || table->xlate[key] == 0xff) {
362 table = process_modifier(config, key,
363 keycode[i] & KEY_RELEASE);
364 }
365 }
366
367
368 for (i = same; i < num_keycodes; i++) {
369 int key = keycode[i];
370 int ch = (key < table->num_entries) ? table->xlate[key] : 0xff;
371
372
373
374
375
376 if (ch != 0xff) {
377 if (ch_count < max_chars)
378 output_ch[ch_count] = (uchar)ch;
379 ch_count++;
380 } else {
381 ch_count += input_keycode_to_ansi364(config, key,
382 output_ch, max_chars);
383 }
384 }
385
386 if (ch_count > max_chars) {
387 debug("%s: Output char buffer overflow size=%d, need=%d\n",
388 __func__, max_chars, ch_count);
389 return -1;
390 }
391
392
393 return ch_count;
394}
395
396int input_send_keycodes(struct input_config *config,
397 int keycode[], int num_keycodes)
398{
399 char ch[num_keycodes * ANSI_CHAR_MAX];
400 int count, i, same = 0;
401 int is_repeat = 0;
402 unsigned delay_ms;
403
404 config->modifiers = 0;
405 if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
406
407
408
409
410
411
412
413
414 is_repeat = config->repeat_rate_ms &&
415 (int)get_timer(config->next_repeat_ms) >= 0;
416 if (!is_repeat)
417 return 0;
418 }
419
420 count = input_keycodes_to_ascii(config, keycode, num_keycodes,
421 ch, sizeof(ch), is_repeat ? 0 : same);
422 for (i = 0; i < count; i++)
423 input_queue_ascii(config, ch[i]);
424 delay_ms = is_repeat ?
425 config->repeat_rate_ms :
426 config->repeat_delay_ms;
427
428 config->next_repeat_ms = get_timer(0) + delay_ms;
429
430 return count;
431}
432
433int input_add_table(struct input_config *config, int left_keycode,
434 int right_keycode, const uchar *xlate, int num_entries)
435{
436 struct input_key_xlate *table;
437
438 if (config->num_tables == INPUT_MAX_MODIFIERS) {
439 debug("%s: Too many modifier tables\n", __func__);
440 return -1;
441 }
442
443 table = &config->table[config->num_tables++];
444 table->left_keycode = left_keycode;
445 table->right_keycode = right_keycode;
446 table->xlate = xlate;
447 table->num_entries = num_entries;
448
449 return 0;
450}
451
452void input_set_delays(struct input_config *config, int repeat_delay_ms,
453 int repeat_rate_ms)
454{
455 config->repeat_delay_ms = repeat_delay_ms;
456 config->repeat_rate_ms = repeat_rate_ms;
457}
458
459int input_init(struct input_config *config, int leds)
460{
461 memset(config, '\0', sizeof(*config));
462 config->leds = leds;
463 if (input_add_table(config, -1, -1,
464 kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate)) ||
465 input_add_table(config, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
466 kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate)) ||
467 input_add_table(config, KEY_LEFTCTRL, KEY_RIGHTCTRL,
468 kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate))) {
469 debug("%s: Could not add modifier tables\n", __func__);
470 return -1;
471 }
472
473 return 0;
474}
475
476int input_stdio_register(struct stdio_dev *dev)
477{
478 int error;
479
480 error = stdio_register(dev);
481
482
483 if (!error && strcmp(getenv("stdin"), dev->name) == 0) {
484
485 if (OVERWRITE_CONSOLE ||
486 console_assign(stdin, dev->name))
487 return -1;
488 }
489
490 return 0;
491}
492