2 * Translate key codes into ASCII
4 * Copyright (c) 2011 The Chromium OS Authors.
5 * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <stdio_dev.h>
14 #include <linux/input.h>
17 /* These correspond to the lights on the keyboard */
18 FLAG_NUM_LOCK = 1 << 0,
19 FLAG_CAPS_LOCK = 1 << 1,
20 FLAG_SCROLL_LOCK = 1 << 2,
22 /* Special flag ORed with key code to indicate release */
23 KEY_RELEASE = 1 << 15,
28 * These takes map key codes to ASCII. 0xff means no key, or special key.
29 * Three tables are provided - one for plain keys, one for when the shift
30 * 'modifier' key is pressed and one for when the ctrl modifier key is
33 static const uchar kbd_plain_xlate[] = {
34 0xff, 0x1b, '1', '2', '3', '4', '5', '6',
35 '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */
36 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
37 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */
38 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
39 '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
40 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff,
41 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
43 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
44 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
49 static unsigned char kbd_shift_xlate[] = {
50 0xff, 0x1b, '!', '@', '#', '$', '%', '^',
51 '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */
52 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
53 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */
54 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
55 '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
56 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
57 ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
59 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
60 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
65 static unsigned char kbd_ctrl_xlate[] = {
66 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E,
67 '7', '8', '9', '0', 0x1F, '=', '\b', '\t', /* 0x00 - 0x0f */
68 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09,
69 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13, /* 0x10 - 0x1f */
70 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';',
71 '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16, /* 0x20 - 0x2f */
72 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff,
73 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
75 '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
76 '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
82 * Scan key code to ANSI 3.64 escape sequence table. This table is
83 * incomplete in that it does not include all possible extra keys.
88 } kbd_to_ansi364[] = {
90 { KEY_DOWN, "\033[B"},
91 { KEY_RIGHT, "\033[C"},
92 { KEY_LEFT, "\033[D"},
95 /* Maximum number of output characters that an ANSI sequence expands to */
96 #define ANSI_CHAR_MAX 3
98 static int input_queue_ascii(struct input_config *config, int ch)
100 if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
101 if (!config->fifo_out)
102 return -1; /* buffer full */
106 if (config->fifo_in + 1 == config->fifo_out)
107 return -1; /* buffer full */
110 debug(" {%02x} ", ch);
111 config->fifo[config->fifo_in] = (uchar)ch;
116 int input_tstc(struct input_config *config)
118 if (config->fifo_in == config->fifo_out && config->read_keys) {
119 if (!(*config->read_keys)(config))
122 return config->fifo_in != config->fifo_out;
125 int input_getc(struct input_config *config)
129 while (config->fifo_in == config->fifo_out) {
130 if (config->read_keys)
131 err = (*config->read_keys)(config);
136 if (++config->fifo_out == INPUT_BUFFER_LEN)
137 config->fifo_out = 0;
139 return config->fifo[config->fifo_out];
143 * Process a modifier/special key press or release and decide which key
144 * translation array should be used as a result.
146 * TODO: Should keep track of modifier press/release
148 * @param config Input state
149 * @param key Key code to process
150 * @param release 0 if a press, 1 if a release
151 * @return pointer to keycode->ascii translation table that should be used
153 static struct input_key_xlate *process_modifier(struct input_config *config,
154 int key, int release)
156 struct input_key_xlate *table;
160 /* Start with the main table, and see what modifiers change it */
161 assert(config->num_tables > 0);
162 table = &config->table[0];
163 for (i = 1; i < config->num_tables; i++) {
164 struct input_key_xlate *tab = &config->table[i];
166 if (key == tab->left_keycode || key == tab->right_keycode)
170 /* Handle the lighted keys */
174 flip = FLAG_SCROLL_LOCK;
177 flip = FLAG_NUM_LOCK;
180 flip = FLAG_CAPS_LOCK;
188 config->leds ^= flip;
189 if (config->flags & FLAG_NUM_LOCK)
190 leds |= INPUT_LED_NUM;
191 if (config->flags & FLAG_CAPS_LOCK)
192 leds |= INPUT_LED_CAPS;
193 if (config->flags & FLAG_SCROLL_LOCK)
194 leds |= INPUT_LED_SCROLL;
202 * Search an int array for a key value
204 * @param array Array to search
205 * @param count Number of elements in array
206 * @param key Key value to find
207 * @return element where value was first found, -1 if none
209 static int array_search(int *array, int count, int key)
213 for (i = 0; i < count; i++) {
222 * Sort an array so that those elements that exist in the ordering are
223 * first in the array, and in the same order as the ordering. The algorithm
224 * is O(count * ocount) and designed for small arrays.
226 * TODO: Move this to common / lib?
228 * @param dest Array with elements to sort, also destination array
229 * @param count Number of elements to sort
230 * @param order Array containing ordering elements
231 * @param ocount Number of ordering elements
232 * @return number of elements in dest that are in order (these will be at the
235 static int sort_array_by_ordering(int *dest, int count, int *order,
240 int same; /* number of elements which are the same */
243 /* setup output items, copy items to be sorted into our temp area */
244 memcpy(temp, dest, count * sizeof(*dest));
247 /* work through the ordering, move over the elements we agree on */
248 for (i = 0; i < ocount; i++) {
249 if (array_search(temp, count, order[i]) != -1)
250 dest[dest_count++] = order[i];
254 /* now move over the elements that are not in the ordering */
255 for (i = 0; i < count; i++) {
256 if (array_search(order, ocount, temp[i]) == -1)
257 dest[dest_count++] = temp[i];
259 assert(dest_count == count);
264 * Check a list of key codes against the previous key scan
266 * Given a list of new key codes, we check how many of these are the same
269 * @param config Input state
270 * @param keycode List of key codes to examine
271 * @param num_keycodes Number of key codes
272 * @param same Returns number of key codes which are the same
274 static int input_check_keycodes(struct input_config *config,
275 int keycode[], int num_keycodes, int *same)
277 /* Select the 'plain' xlate table to start with */
278 if (!config->num_tables) {
279 debug("%s: No xlate tables: cannot decode keys\n", __func__);
283 /* sort the keycodes into the same order as the previous ones */
284 *same = sort_array_by_ordering(keycode, num_keycodes,
285 config->prev_keycodes, config->num_prev_keycodes);
287 memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
288 config->num_prev_keycodes = num_keycodes;
290 return *same != num_keycodes;
294 * Checks and converts a special key code into ANSI 3.64 escape sequence.
296 * @param config Input state
297 * @param keycode Key code to examine
298 * @param output_ch Buffer to place output characters into. It should
299 * be at least ANSI_CHAR_MAX bytes long, to allow for
301 * @param max_chars Maximum number of characters to add to output_ch
302 * @return number of characters output, if the key was converted, otherwise 0.
303 * This may be larger than max_chars, in which case the overflow
304 * characters are not output.
306 static int input_keycode_to_ansi364(struct input_config *config,
307 int keycode, char output_ch[], int max_chars)
313 for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
314 if (keycode != kbd_to_ansi364[i].kbd_scan_code)
316 for (escape = kbd_to_ansi364[i].escape; *escape; escape++) {
317 if (ch_count < max_chars)
318 output_ch[ch_count] = *escape;
328 * Converts and queues a list of key codes in escaped ASCII string form
329 * Convert a list of key codes into ASCII
331 * You must call input_check_keycodes() before this. It turns the keycode
332 * list into a list of ASCII characters and sends them to the input layer.
334 * Characters which were seen last time do not generate fresh ASCII output.
335 * The output (calls to queue_ascii) may be longer than num_keycodes, if the
336 * keycode contains special keys that was encoded to longer escaped sequence.
338 * @param config Input state
339 * @param keycode List of key codes to examine
340 * @param num_keycodes Number of key codes
341 * @param output_ch Buffer to place output characters into. It should
342 * be at last ANSI_CHAR_MAX * num_keycodes, to allow for
344 * @param max_chars Maximum number of characters to add to output_ch
345 * @param same Number of key codes which are the same
346 * @return number of characters written into output_ch, or -1 if we would
347 * exceed max_chars chars.
349 static int input_keycodes_to_ascii(struct input_config *config,
350 int keycode[], int num_keycodes, char output_ch[],
351 int max_chars, int same)
353 struct input_key_xlate *table;
357 table = &config->table[0];
359 /* deal with modifiers first */
360 for (i = 0; i < num_keycodes; i++) {
361 int key = keycode[i] & KEY_MASK;
363 if (key >= table->num_entries || table->xlate[key] == 0xff) {
364 table = process_modifier(config, key,
365 keycode[i] & KEY_RELEASE);
369 /* Start conversion by looking for the first new keycode (by same). */
370 for (i = same; i < num_keycodes; i++) {
371 int key = keycode[i];
372 int ch = (key < table->num_entries) ? table->xlate[key] : 0xff;
375 * For a normal key (with an ASCII value), add it; otherwise
376 * translate special key to escape sequence if possible.
379 if (ch_count < max_chars)
380 output_ch[ch_count] = (uchar)ch;
383 ch_count += input_keycode_to_ansi364(config, key,
384 output_ch, max_chars);
388 if (ch_count > max_chars) {
389 debug("%s: Output char buffer overflow size=%d, need=%d\n",
390 __func__, max_chars, ch_count);
394 /* ok, so return keys */
398 static int _input_send_keycodes(struct input_config *config, int keycode[],
399 int num_keycodes, bool do_send)
401 char ch[num_keycodes * ANSI_CHAR_MAX];
402 int count, i, same = 0;
406 config->modifiers = 0;
407 if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
409 * Same as last time - is it time for another repeat?
410 * TODO(sjg@chromium.org) We drop repeats here and since
411 * the caller may not call in again for a while, our
412 * auto-repeat speed is not quite correct. We should
413 * insert another character if we later realise that we
414 * have missed a repeat slot.
416 is_repeat = config->allow_repeats || (config->repeat_rate_ms &&
417 (int)get_timer(config->next_repeat_ms) >= 0);
422 count = input_keycodes_to_ascii(config, keycode, num_keycodes,
423 ch, sizeof(ch), is_repeat ? 0 : same);
425 for (i = 0; i < count; i++)
426 input_queue_ascii(config, ch[i]);
428 delay_ms = is_repeat ?
429 config->repeat_rate_ms :
430 config->repeat_delay_ms;
432 config->next_repeat_ms = get_timer(0) + delay_ms;
437 int input_send_keycodes(struct input_config *config, int keycode[],
440 return _input_send_keycodes(config, keycode, num_keycodes, true);
443 int input_add_keycode(struct input_config *config, int new_keycode,
446 int keycode[INPUT_MAX_MODIFIERS + 1];
449 /* Add the old keycodes which are not removed by this new one */
450 for (i = 0, count = 0; i < config->num_prev_keycodes; i++) {
451 int code = config->prev_keycodes[i];
453 if (new_keycode == code) {
458 keycode[count++] = code;
461 if (!release && new_keycode != -1)
462 keycode[count++] = new_keycode;
463 debug("\ncodes for %02x/%d: ", new_keycode, release);
464 for (i = 0; i < count; i++)
465 debug("%02x ", keycode[i]);
468 /* Don't output any ASCII characters if this is a key release */
469 return _input_send_keycodes(config, keycode, count, !release);
472 int input_add_table(struct input_config *config, int left_keycode,
473 int right_keycode, const uchar *xlate, int num_entries)
475 struct input_key_xlate *table;
477 if (config->num_tables == INPUT_MAX_MODIFIERS) {
478 debug("%s: Too many modifier tables\n", __func__);
482 table = &config->table[config->num_tables++];
483 table->left_keycode = left_keycode;
484 table->right_keycode = right_keycode;
485 table->xlate = xlate;
486 table->num_entries = num_entries;
491 void input_set_delays(struct input_config *config, int repeat_delay_ms,
494 config->repeat_delay_ms = repeat_delay_ms;
495 config->repeat_rate_ms = repeat_rate_ms;
498 void input_allow_repeats(struct input_config *config, bool allow_repeats)
500 config->allow_repeats = allow_repeats;
503 int input_add_tables(struct input_config *config)
507 ret = input_add_table(config, -1, -1,
508 kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate));
511 ret = input_add_table(config, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
512 kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate));
516 return input_add_table(config, KEY_LEFTCTRL, KEY_RIGHTCTRL,
517 kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate));
520 int input_init(struct input_config *config, int leds)
522 memset(config, '\0', sizeof(*config));
528 int input_stdio_register(struct stdio_dev *dev)
532 error = stdio_register(dev);
534 /* check if this is the standard input device */
535 if (!error && strcmp(getenv("stdin"), dev->name) == 0) {
536 /* reassign the console */
537 if (OVERWRITE_CONSOLE ||
538 console_assign(stdin, dev->name))