input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Translate key codes into ASCII
  3. *
  4. * Copyright (c) 2011 The Chromium OS Authors.
  5. * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <stdio_dev.h>
  11. #include <input.h>
  12. #include <linux/input.h>
  13. enum {
  14. /* These correspond to the lights on the keyboard */
  15. FLAG_NUM_LOCK = 1 << 0,
  16. FLAG_CAPS_LOCK = 1 << 1,
  17. FLAG_SCROLL_LOCK = 1 << 2,
  18. /* Special flag ORed with key code to indicate release */
  19. KEY_RELEASE = 1 << 15,
  20. KEY_MASK = 0xfff,
  21. };
  22. /*
  23. * These takes map key codes to ASCII. 0xff means no key, or special key.
  24. * Three tables are provided - one for plain keys, one for when the shift
  25. * 'modifier' key is pressed and one for when the ctrl modifier key is
  26. * pressed.
  27. */
  28. static const uchar kbd_plain_xlate[] = {
  29. 0xff, 0x1b, '1', '2', '3', '4', '5', '6',
  30. '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */
  31. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  32. 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */
  33. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  34. '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
  35. 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff,
  36. ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
  37. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
  38. '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  39. '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
  40. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
  41. '\r', 0xff, 0xff
  42. };
  43. static unsigned char kbd_shift_xlate[] = {
  44. 0xff, 0x1b, '!', '@', '#', '$', '%', '^',
  45. '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */
  46. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  47. 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */
  48. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  49. '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
  50. 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
  51. ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
  52. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
  53. '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  54. '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
  55. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
  56. '\r', 0xff, 0xff
  57. };
  58. static unsigned char kbd_ctrl_xlate[] = {
  59. 0xff, 0x1b, '1', 0x00, '3', '4', '5', 0x1E,
  60. '7', '8', '9', '0', 0x1F, '=', '\b', '\t', /* 0x00 - 0x0f */
  61. 0x11, 0x17, 0x05, 0x12, 0x14, 0x18, 0x15, 0x09,
  62. 0x0f, 0x10, 0x1b, 0x1d, '\n', 0xff, 0x01, 0x13, /* 0x10 - 0x1f */
  63. 0x04, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0c, ';',
  64. '\'', '~', 0x00, 0x1c, 0x1a, 0x18, 0x03, 0x16, /* 0x20 - 0x2f */
  65. 0x02, 0x0e, 0x0d, '<', '>', '?', 0xff, 0xff,
  66. 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
  67. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
  68. '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  69. '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
  70. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
  71. '\r', 0xff, 0xff
  72. };
  73. /*
  74. * Scan key code to ANSI 3.64 escape sequence table. This table is
  75. * incomplete in that it does not include all possible extra keys.
  76. */
  77. static struct {
  78. int kbd_scan_code;
  79. char *escape;
  80. } kbd_to_ansi364[] = {
  81. { KEY_UP, "\033[A"},
  82. { KEY_DOWN, "\033[B"},
  83. { KEY_RIGHT, "\033[C"},
  84. { KEY_LEFT, "\033[D"},
  85. };
  86. /* Maximum number of output characters that an ANSI sequence expands to */
  87. #define ANSI_CHAR_MAX 3
  88. static int input_queue_ascii(struct input_config *config, int ch)
  89. {
  90. if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
  91. if (!config->fifo_out)
  92. return -1; /* buffer full */
  93. else
  94. config->fifo_in = 0;
  95. } else {
  96. if (config->fifo_in + 1 == config->fifo_out)
  97. return -1; /* buffer full */
  98. config->fifo_in++;
  99. }
  100. config->fifo[config->fifo_in] = (uchar)ch;
  101. return 0;
  102. }
  103. int input_tstc(struct input_config *config)
  104. {
  105. if (config->fifo_in == config->fifo_out && config->read_keys) {
  106. if (!(*config->read_keys)(config))
  107. return 0;
  108. }
  109. return config->fifo_in != config->fifo_out;
  110. }
  111. int input_getc(struct input_config *config)
  112. {
  113. int err = 0;
  114. while (config->fifo_in == config->fifo_out) {
  115. if (config->read_keys)
  116. err = (*config->read_keys)(config);
  117. if (err)
  118. return -1;
  119. }
  120. if (++config->fifo_out == INPUT_BUFFER_LEN)
  121. config->fifo_out = 0;
  122. return config->fifo[config->fifo_out];
  123. }
  124. /**
  125. * Process a modifier/special key press or release and decide which key
  126. * translation array should be used as a result.
  127. *
  128. * TODO: Should keep track of modifier press/release
  129. *
  130. * @param config Input state
  131. * @param key Key code to process
  132. * @param release 0 if a press, 1 if a release
  133. * @return pointer to keycode->ascii translation table that should be used
  134. */
  135. static struct input_key_xlate *process_modifier(struct input_config *config,
  136. int key, int release)
  137. {
  138. struct input_key_xlate *table;
  139. int flip = -1;
  140. int i;
  141. /* Start with the main table, and see what modifiers change it */
  142. assert(config->num_tables > 0);
  143. table = &config->table[0];
  144. for (i = 1; i < config->num_tables; i++) {
  145. struct input_key_xlate *tab = &config->table[i];
  146. if (key == tab->left_keycode || key == tab->right_keycode)
  147. table = tab;
  148. }
  149. /* Handle the lighted keys */
  150. if (!release) {
  151. switch (key) {
  152. case KEY_SCROLLLOCK:
  153. flip = FLAG_SCROLL_LOCK;
  154. break;
  155. case KEY_NUMLOCK:
  156. flip = FLAG_NUM_LOCK;
  157. break;
  158. case KEY_CAPSLOCK:
  159. flip = FLAG_CAPS_LOCK;
  160. break;
  161. }
  162. }
  163. if (flip != -1) {
  164. int leds = 0;
  165. config->leds ^= flip;
  166. if (config->flags & FLAG_NUM_LOCK)
  167. leds |= INPUT_LED_NUM;
  168. if (config->flags & FLAG_CAPS_LOCK)
  169. leds |= INPUT_LED_CAPS;
  170. if (config->flags & FLAG_SCROLL_LOCK)
  171. leds |= INPUT_LED_SCROLL;
  172. config->leds = leds;
  173. }
  174. return table;
  175. }
  176. /**
  177. * Search an int array for a key value
  178. *
  179. * @param array Array to search
  180. * @param count Number of elements in array
  181. * @param key Key value to find
  182. * @return element where value was first found, -1 if none
  183. */
  184. static int array_search(int *array, int count, int key)
  185. {
  186. int i;
  187. for (i = 0; i < count; i++) {
  188. if (array[i] == key)
  189. return i;
  190. }
  191. return -1;
  192. }
  193. /**
  194. * Sort an array so that those elements that exist in the ordering are
  195. * first in the array, and in the same order as the ordering. The algorithm
  196. * is O(count * ocount) and designed for small arrays.
  197. *
  198. * TODO: Move this to common / lib?
  199. *
  200. * @param dest Array with elements to sort, also destination array
  201. * @param count Number of elements to sort
  202. * @param order Array containing ordering elements
  203. * @param ocount Number of ordering elements
  204. * @return number of elements in dest that are in order (these will be at the
  205. * start of dest).
  206. */
  207. static int sort_array_by_ordering(int *dest, int count, int *order,
  208. int ocount)
  209. {
  210. int temp[count];
  211. int dest_count;
  212. int same; /* number of elements which are the same */
  213. int i;
  214. /* setup output items, copy items to be sorted into our temp area */
  215. memcpy(temp, dest, count * sizeof(*dest));
  216. dest_count = 0;
  217. /* work through the ordering, move over the elements we agree on */
  218. for (i = 0; i < ocount; i++) {
  219. if (array_search(temp, count, order[i]) != -1)
  220. dest[dest_count++] = order[i];
  221. }
  222. same = dest_count;
  223. /* now move over the elements that are not in the ordering */
  224. for (i = 0; i < count; i++) {
  225. if (array_search(order, ocount, temp[i]) == -1)
  226. dest[dest_count++] = temp[i];
  227. }
  228. assert(dest_count == count);
  229. return same;
  230. }
  231. /**
  232. * Check a list of key codes against the previous key scan
  233. *
  234. * Given a list of new key codes, we check how many of these are the same
  235. * as last time.
  236. *
  237. * @param config Input state
  238. * @param keycode List of key codes to examine
  239. * @param num_keycodes Number of key codes
  240. * @param same Returns number of key codes which are the same
  241. */
  242. static int input_check_keycodes(struct input_config *config,
  243. int keycode[], int num_keycodes, int *same)
  244. {
  245. /* Select the 'plain' xlate table to start with */
  246. if (!config->num_tables) {
  247. debug("%s: No xlate tables: cannot decode keys\n", __func__);
  248. return -1;
  249. }
  250. /* sort the keycodes into the same order as the previous ones */
  251. *same = sort_array_by_ordering(keycode, num_keycodes,
  252. config->prev_keycodes, config->num_prev_keycodes);
  253. memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
  254. config->num_prev_keycodes = num_keycodes;
  255. return *same != num_keycodes;
  256. }
  257. /**
  258. * Checks and converts a special key code into ANSI 3.64 escape sequence.
  259. *
  260. * @param config Input state
  261. * @param keycode Key code to examine
  262. * @param output_ch Buffer to place output characters into. It should
  263. * be at least ANSI_CHAR_MAX bytes long, to allow for
  264. * an ANSI sequence.
  265. * @param max_chars Maximum number of characters to add to output_ch
  266. * @return number of characters output, if the key was converted, otherwise 0.
  267. * This may be larger than max_chars, in which case the overflow
  268. * characters are not output.
  269. */
  270. static int input_keycode_to_ansi364(struct input_config *config,
  271. int keycode, char output_ch[], int max_chars)
  272. {
  273. const char *escape;
  274. int ch_count;
  275. int i;
  276. for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
  277. if (keycode != kbd_to_ansi364[i].kbd_scan_code)
  278. continue;
  279. for (escape = kbd_to_ansi364[i].escape; *escape; escape++) {
  280. if (ch_count < max_chars)
  281. output_ch[ch_count] = *escape;
  282. ch_count++;
  283. }
  284. return ch_count;
  285. }
  286. return 0;
  287. }
  288. /**
  289. * Converts and queues a list of key codes in escaped ASCII string form
  290. * Convert a list of key codes into ASCII
  291. *
  292. * You must call input_check_keycodes() before this. It turns the keycode
  293. * list into a list of ASCII characters and sends them to the input layer.
  294. *
  295. * Characters which were seen last time do not generate fresh ASCII output.
  296. * The output (calls to queue_ascii) may be longer than num_keycodes, if the
  297. * keycode contains special keys that was encoded to longer escaped sequence.
  298. *
  299. * @param config Input state
  300. * @param keycode List of key codes to examine
  301. * @param num_keycodes Number of key codes
  302. * @param output_ch Buffer to place output characters into. It should
  303. * be at last ANSI_CHAR_MAX * num_keycodes, to allow for
  304. * ANSI sequences.
  305. * @param max_chars Maximum number of characters to add to output_ch
  306. * @param same Number of key codes which are the same
  307. * @return number of characters written into output_ch, or -1 if we would
  308. * exceed max_chars chars.
  309. */
  310. static int input_keycodes_to_ascii(struct input_config *config,
  311. int keycode[], int num_keycodes, char output_ch[],
  312. int max_chars, int same)
  313. {
  314. struct input_key_xlate *table;
  315. int ch_count = 0;
  316. int i;
  317. table = &config->table[0];
  318. /* deal with modifiers first */
  319. for (i = 0; i < num_keycodes; i++) {
  320. int key = keycode[i] & KEY_MASK;
  321. if (key >= table->num_entries || table->xlate[key] == 0xff) {
  322. table = process_modifier(config, key,
  323. keycode[i] & KEY_RELEASE);
  324. }
  325. }
  326. /* Start conversion by looking for the first new keycode (by same). */
  327. for (i = same; i < num_keycodes; i++) {
  328. int key = keycode[i];
  329. int ch = (key < table->num_entries) ? table->xlate[key] : 0xff;
  330. /*
  331. * For a normal key (with an ASCII value), add it; otherwise
  332. * translate special key to escape sequence if possible.
  333. */
  334. if (ch != 0xff) {
  335. if (ch_count < max_chars)
  336. output_ch[ch_count] = (uchar)ch;
  337. ch_count++;
  338. } else {
  339. ch_count += input_keycode_to_ansi364(config, key,
  340. output_ch, max_chars);
  341. }
  342. }
  343. if (ch_count > max_chars) {
  344. debug("%s: Output char buffer overflow size=%d, need=%d\n",
  345. __func__, max_chars, ch_count);
  346. return -1;
  347. }
  348. /* ok, so return keys */
  349. return ch_count;
  350. }
  351. int input_send_keycodes(struct input_config *config,
  352. int keycode[], int num_keycodes)
  353. {
  354. char ch[num_keycodes * ANSI_CHAR_MAX];
  355. int count, i, same = 0;
  356. int is_repeat = 0;
  357. unsigned delay_ms;
  358. config->modifiers = 0;
  359. if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
  360. /*
  361. * Same as last time - is it time for another repeat?
  362. * TODO(sjg@chromium.org) We drop repeats here and since
  363. * the caller may not call in again for a while, our
  364. * auto-repeat speed is not quite correct. We should
  365. * insert another character if we later realise that we
  366. * have missed a repeat slot.
  367. */
  368. is_repeat = config->repeat_rate_ms &&
  369. (int)get_timer(config->next_repeat_ms) >= 0;
  370. if (!is_repeat)
  371. return 0;
  372. }
  373. count = input_keycodes_to_ascii(config, keycode, num_keycodes,
  374. ch, sizeof(ch), is_repeat ? 0 : same);
  375. for (i = 0; i < count; i++)
  376. input_queue_ascii(config, ch[i]);
  377. delay_ms = is_repeat ?
  378. config->repeat_rate_ms :
  379. config->repeat_delay_ms;
  380. config->next_repeat_ms = get_timer(0) + delay_ms;
  381. return count;
  382. }
  383. int input_add_table(struct input_config *config, int left_keycode,
  384. int right_keycode, const uchar *xlate, int num_entries)
  385. {
  386. struct input_key_xlate *table;
  387. if (config->num_tables == INPUT_MAX_MODIFIERS) {
  388. debug("%s: Too many modifier tables\n", __func__);
  389. return -1;
  390. }
  391. table = &config->table[config->num_tables++];
  392. table->left_keycode = left_keycode;
  393. table->right_keycode = right_keycode;
  394. table->xlate = xlate;
  395. table->num_entries = num_entries;
  396. return 0;
  397. }
  398. void input_set_delays(struct input_config *config, int repeat_delay_ms,
  399. int repeat_rate_ms)
  400. {
  401. config->repeat_delay_ms = repeat_delay_ms;
  402. config->repeat_rate_ms = repeat_rate_ms;
  403. }
  404. int input_init(struct input_config *config, int leds)
  405. {
  406. memset(config, '\0', sizeof(*config));
  407. config->leds = leds;
  408. if (input_add_table(config, -1, -1,
  409. kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate)) ||
  410. input_add_table(config, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
  411. kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate)) ||
  412. input_add_table(config, KEY_LEFTCTRL, KEY_RIGHTCTRL,
  413. kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate))) {
  414. debug("%s: Could not add modifier tables\n", __func__);
  415. return -1;
  416. }
  417. return 0;
  418. }
  419. int input_stdio_register(struct stdio_dev *dev)
  420. {
  421. int error;
  422. error = stdio_register(dev);
  423. /* check if this is the standard input device */
  424. if (!error && strcmp(getenv("stdin"), dev->name) == 0) {
  425. /* reassign the console */
  426. if (OVERWRITE_CONSOLE ||
  427. console_assign(stdin, dev->name))
  428. return -1;
  429. }
  430. return 0;
  431. }