input.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Translate key codes into ASCII
  4. *
  5. * Copyright (c) 2011 The Chromium OS Authors.
  6. * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  7. */
  8. #include <common.h>
  9. #include <console.h>
  10. #include <dm.h>
  11. #include <env.h>
  12. #include <errno.h>
  13. #include <log.h>
  14. #include <stdio_dev.h>
  15. #include <input.h>
  16. #ifdef CONFIG_DM_KEYBOARD
  17. #include <keyboard.h>
  18. #endif
  19. #include <linux/input.h>
  20. enum {
  21. /* These correspond to the lights on the keyboard */
  22. FLAG_SCROLL_LOCK = 1 << 0,
  23. FLAG_NUM_LOCK = 1 << 1,
  24. FLAG_CAPS_LOCK = 1 << 2,
  25. /* Special flag ORed with key code to indicate release */
  26. KEY_RELEASE = 1 << 15,
  27. KEY_MASK = 0xfff,
  28. };
  29. /*
  30. * These takes map key codes to ASCII. 0xff means no key, or special key.
  31. * Three tables are provided - one for plain keys, one for when the shift
  32. * 'modifier' key is pressed and one for when the ctrl modifier key is
  33. * pressed.
  34. */
  35. static const uchar kbd_plain_xlate[] = {
  36. 0xff, 0x1b, '1', '2', '3', '4', '5', '6',
  37. '7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x00 - 0x0f */
  38. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  39. 'o', 'p', '[', ']', '\r', 0xff, 'a', 's', /* 0x10 - 0x1f */
  40. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  41. '\'', '`', 0xff, '\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
  42. 'b', 'n', 'm', ',' , '.', '/', 0xff, 0xff, 0xff,
  43. ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
  44. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
  45. '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  46. '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff,
  47. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
  48. '\r', 0xff, '/', '*',
  49. };
  50. static unsigned char kbd_shift_xlate[] = {
  51. 0xff, 0x1b, '!', '@', '#', '$', '%', '^',
  52. '&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x00 - 0x0f */
  53. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  54. 'O', 'P', '{', '}', '\r', 0xff, 'A', 'S', /* 0x10 - 0x1f */
  55. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  56. '"', '~', 0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
  57. 'B', 'N', 'M', '<', '>', '?', 0xff, 0xff, 0xff,
  58. ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x30 - 0x3f */
  59. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7',
  60. '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
  61. '2', '3', '0', '.', 0xff, 0xff, 0xff, 0xff, 0xff,
  62. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0x50 - 0x5F */
  63. '\r', 0xff, '/', '*',
  64. };
  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 */
  78. '\r', 0xff, '/', '*',
  79. };
  80. /*
  81. * German keymap. Special letters are mapped according to code page 437.
  82. */
  83. static const uchar kbd_plain_xlate_german[] = {
  84. 0xff, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
  85. '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
  86. 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
  87. 'o', 'p', 0x81, '+', '\r', 0xff, 'a', 's', /* scan 18-1F */
  88. 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
  89. 0x84, '^', 0xff, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
  90. 'b', 'n', 'm', ',', '.', '-', 0xff, '*', /* scan 30-37 */
  91. ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
  92. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
  93. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  94. '2', '3', '0', ',', 0xff, 0xff, '<', 0xff, /* scan 50-57 */
  95. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
  96. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
  97. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
  98. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
  99. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
  100. '\r', 0xff, '/', '*',
  101. };
  102. static unsigned char kbd_shift_xlate_german[] = {
  103. 0xff, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 00-07 */
  104. '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 08-0F */
  105. 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
  106. 'O', 'P', 0x9a, '*', '\r', 0xff, 'A', 'S', /* scan 18-1F */
  107. 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
  108. 0x8e, 0xf8, 0xff, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
  109. 'B', 'N', 'M', ';', ':', '_', 0xff, '*', /* scan 30-37 */
  110. ' ', ' ', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
  111. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '7', /* scan 40-47 */
  112. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  113. '2', '3', '0', ',', 0xff, 0xff, '>', 0xff, /* scan 50-57 */
  114. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
  115. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
  116. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
  117. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
  118. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
  119. '\r', 0xff, '/', '*',
  120. };
  121. static unsigned char kbd_right_alt_xlate_german[] = {
  122. 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
  123. '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 08-0F */
  124. '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
  125. 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
  126. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
  127. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
  128. 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
  129. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
  130. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
  131. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
  132. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50-57 */
  133. };
  134. enum kbd_mask {
  135. KBD_ENGLISH = 1 << 0,
  136. KBD_GERMAN = 1 << 1,
  137. };
  138. static struct kbd_entry {
  139. int kbd_mask; /* Which languages this is for */
  140. int left_keycode; /* Left keycode to select this map */
  141. int right_keycode; /* Right keycode to select this map */
  142. const uchar *xlate; /* Ascii code for each keycode */
  143. int num_entries; /* Number of entries in xlate */
  144. } kbd_entry[] = {
  145. { KBD_ENGLISH, -1, -1,
  146. kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate) },
  147. { KBD_GERMAN, -1, -1,
  148. kbd_plain_xlate_german, ARRAY_SIZE(kbd_plain_xlate_german) },
  149. { KBD_ENGLISH, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
  150. kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate) },
  151. { KBD_GERMAN, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
  152. kbd_shift_xlate_german, ARRAY_SIZE(kbd_shift_xlate_german) },
  153. { KBD_ENGLISH | KBD_GERMAN, KEY_LEFTCTRL, KEY_RIGHTCTRL,
  154. kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate) },
  155. { KBD_GERMAN, -1, KEY_RIGHTALT,
  156. kbd_right_alt_xlate_german,
  157. ARRAY_SIZE(kbd_right_alt_xlate_german) },
  158. {},
  159. };
  160. /*
  161. * The table contains conversions from scan key codes to ECMA-48 escape
  162. * sequences. The same sequences exist in the withdrawn ANSI 3.64 standard.
  163. *
  164. * As all escape sequences start with 0x1b this byte has been removed.
  165. *
  166. * This table is incomplete in that it does not include all possible extra keys.
  167. */
  168. static struct {
  169. int kbd_scan_code;
  170. char *escape;
  171. } kbd_to_ansi364[] = {
  172. { KEY_UP, "[A"},
  173. { KEY_LEFT, "[D"},
  174. { KEY_RIGHT, "[C"},
  175. { KEY_DOWN, "[B"},
  176. { KEY_F1, "OP"},
  177. { KEY_F2, "OQ"},
  178. { KEY_F3, "OR"},
  179. { KEY_F4, "OS"},
  180. { KEY_F5, "[15~"},
  181. { KEY_F6, "[17~"},
  182. { KEY_F7, "[18~"},
  183. { KEY_F8, "[19~"},
  184. { KEY_F9, "[20~"},
  185. { KEY_F10, "[21~"},
  186. };
  187. /* Maximum number of output characters that an ANSI sequence expands to */
  188. #define ANSI_CHAR_MAX 5
  189. static int input_queue_ascii(struct input_config *config, int ch)
  190. {
  191. if (config->fifo_in + 1 == INPUT_BUFFER_LEN) {
  192. if (!config->fifo_out)
  193. return -1; /* buffer full */
  194. else
  195. config->fifo_in = 0;
  196. } else {
  197. if (config->fifo_in + 1 == config->fifo_out)
  198. return -1; /* buffer full */
  199. config->fifo_in++;
  200. }
  201. debug(" {%02x} ", ch);
  202. config->fifo[config->fifo_in] = (uchar)ch;
  203. return 0;
  204. }
  205. int input_tstc(struct input_config *config)
  206. {
  207. if (config->fifo_in == config->fifo_out && config->read_keys) {
  208. if (!(*config->read_keys)(config))
  209. return 0;
  210. }
  211. return config->fifo_in != config->fifo_out;
  212. }
  213. int input_getc(struct input_config *config)
  214. {
  215. int err = 0;
  216. while (config->fifo_in == config->fifo_out) {
  217. if (config->read_keys)
  218. err = (*config->read_keys)(config);
  219. if (err)
  220. return -1;
  221. }
  222. if (++config->fifo_out == INPUT_BUFFER_LEN)
  223. config->fifo_out = 0;
  224. return config->fifo[config->fifo_out];
  225. }
  226. /**
  227. * Process a modifier/special key press or release and decide which key
  228. * translation array should be used as a result.
  229. *
  230. * TODO: Should keep track of modifier press/release
  231. *
  232. * @param config Input state
  233. * @param key Key code to process
  234. * @param release 0 if a press, 1 if a release
  235. * @return pointer to keycode->ascii translation table that should be used
  236. */
  237. static struct input_key_xlate *process_modifier(struct input_config *config,
  238. int key, int release)
  239. {
  240. #ifdef CONFIG_DM_KEYBOARD
  241. struct udevice *dev = config->dev;
  242. struct keyboard_ops *ops = keyboard_get_ops(dev);
  243. #endif
  244. struct input_key_xlate *table;
  245. int i;
  246. /* Start with the main table, and see what modifiers change it */
  247. assert(config->num_tables > 0);
  248. table = &config->table[0];
  249. for (i = 1; i < config->num_tables; i++) {
  250. struct input_key_xlate *tab = &config->table[i];
  251. if (key == tab->left_keycode || key == tab->right_keycode)
  252. table = tab;
  253. }
  254. /* Handle the lighted keys */
  255. if (!release) {
  256. int flip = -1;
  257. switch (key) {
  258. case KEY_SCROLLLOCK:
  259. flip = FLAG_SCROLL_LOCK;
  260. break;
  261. case KEY_NUMLOCK:
  262. flip = FLAG_NUM_LOCK;
  263. break;
  264. case KEY_CAPSLOCK:
  265. flip = FLAG_CAPS_LOCK;
  266. break;
  267. }
  268. if (flip != -1) {
  269. int leds = 0;
  270. config->flags ^= flip;
  271. if (config->flags & FLAG_NUM_LOCK)
  272. leds |= INPUT_LED_NUM;
  273. if (config->flags & FLAG_CAPS_LOCK)
  274. leds |= INPUT_LED_CAPS;
  275. if (config->flags & FLAG_SCROLL_LOCK)
  276. leds |= INPUT_LED_SCROLL;
  277. config->leds = leds;
  278. config->leds_changed = flip;
  279. #ifdef CONFIG_DM_KEYBOARD
  280. if (ops->update_leds) {
  281. if (ops->update_leds(dev, config->leds))
  282. debug("Update keyboard's LED failed\n");
  283. }
  284. #endif
  285. }
  286. }
  287. return table;
  288. }
  289. /**
  290. * Search an int array for a key value
  291. *
  292. * @param array Array to search
  293. * @param count Number of elements in array
  294. * @param key Key value to find
  295. * @return element where value was first found, -1 if none
  296. */
  297. static int array_search(int *array, int count, int key)
  298. {
  299. int i;
  300. for (i = 0; i < count; i++) {
  301. if (array[i] == key)
  302. return i;
  303. }
  304. return -1;
  305. }
  306. /**
  307. * Sort an array so that those elements that exist in the ordering are
  308. * first in the array, and in the same order as the ordering. The algorithm
  309. * is O(count * ocount) and designed for small arrays.
  310. *
  311. * TODO: Move this to common / lib?
  312. *
  313. * @param dest Array with elements to sort, also destination array
  314. * @param count Number of elements to sort
  315. * @param order Array containing ordering elements
  316. * @param ocount Number of ordering elements
  317. * @return number of elements in dest that are in order (these will be at the
  318. * start of dest).
  319. */
  320. static int sort_array_by_ordering(int *dest, int count, int *order,
  321. int ocount)
  322. {
  323. int temp[count];
  324. int dest_count;
  325. int same; /* number of elements which are the same */
  326. int i;
  327. /* setup output items, copy items to be sorted into our temp area */
  328. memcpy(temp, dest, count * sizeof(*dest));
  329. dest_count = 0;
  330. /* work through the ordering, move over the elements we agree on */
  331. for (i = 0; i < ocount; i++) {
  332. if (array_search(temp, count, order[i]) != -1)
  333. dest[dest_count++] = order[i];
  334. }
  335. same = dest_count;
  336. /* now move over the elements that are not in the ordering */
  337. for (i = 0; i < count; i++) {
  338. if (array_search(order, ocount, temp[i]) == -1)
  339. dest[dest_count++] = temp[i];
  340. }
  341. assert(dest_count == count);
  342. return same;
  343. }
  344. /**
  345. * Check a list of key codes against the previous key scan
  346. *
  347. * Given a list of new key codes, we check how many of these are the same
  348. * as last time.
  349. *
  350. * @param config Input state
  351. * @param keycode List of key codes to examine
  352. * @param num_keycodes Number of key codes
  353. * @param same Returns number of key codes which are the same
  354. */
  355. static int input_check_keycodes(struct input_config *config,
  356. int keycode[], int num_keycodes, int *same)
  357. {
  358. /* Select the 'plain' xlate table to start with */
  359. if (!config->num_tables) {
  360. debug("%s: No xlate tables: cannot decode keys\n", __func__);
  361. return -1;
  362. }
  363. /* sort the keycodes into the same order as the previous ones */
  364. *same = sort_array_by_ordering(keycode, num_keycodes,
  365. config->prev_keycodes, config->num_prev_keycodes);
  366. memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int));
  367. config->num_prev_keycodes = num_keycodes;
  368. return *same != num_keycodes;
  369. }
  370. /**
  371. * Checks and converts a special key code into ANSI 3.64 escape sequence.
  372. *
  373. * @param config Input state
  374. * @param keycode Key code to examine
  375. * @param output_ch Buffer to place output characters into. It should
  376. * be at least ANSI_CHAR_MAX bytes long, to allow for
  377. * an ANSI sequence.
  378. * @param max_chars Maximum number of characters to add to output_ch
  379. * @return number of characters output, if the key was converted, otherwise 0.
  380. * This may be larger than max_chars, in which case the overflow
  381. * characters are not output.
  382. */
  383. static int input_keycode_to_ansi364(struct input_config *config,
  384. int keycode, char output_ch[], int max_chars)
  385. {
  386. const char *escape;
  387. int ch_count;
  388. int i;
  389. for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
  390. if (keycode != kbd_to_ansi364[i].kbd_scan_code)
  391. continue;
  392. output_ch[ch_count++] = 0x1b;
  393. for (escape = kbd_to_ansi364[i].escape; *escape; escape++) {
  394. if (ch_count < max_chars)
  395. output_ch[ch_count] = *escape;
  396. ch_count++;
  397. }
  398. return ch_count;
  399. }
  400. return 0;
  401. }
  402. /**
  403. * Converts and queues a list of key codes in escaped ASCII string form
  404. * Convert a list of key codes into ASCII
  405. *
  406. * You must call input_check_keycodes() before this. It turns the keycode
  407. * list into a list of ASCII characters and sends them to the input layer.
  408. *
  409. * Characters which were seen last time do not generate fresh ASCII output.
  410. * The output (calls to queue_ascii) may be longer than num_keycodes, if the
  411. * keycode contains special keys that was encoded to longer escaped sequence.
  412. *
  413. * @param config Input state
  414. * @param keycode List of key codes to examine
  415. * @param num_keycodes Number of key codes
  416. * @param output_ch Buffer to place output characters into. It should
  417. * be at last ANSI_CHAR_MAX * num_keycodes, to allow for
  418. * ANSI sequences.
  419. * @param max_chars Maximum number of characters to add to output_ch
  420. * @param same Number of key codes which are the same
  421. * @return number of characters written into output_ch, or -1 if we would
  422. * exceed max_chars chars.
  423. */
  424. static int input_keycodes_to_ascii(struct input_config *config,
  425. int keycode[], int num_keycodes, char output_ch[],
  426. int max_chars, int same)
  427. {
  428. struct input_key_xlate *table;
  429. int ch_count = 0;
  430. int i;
  431. table = &config->table[0];
  432. /* deal with modifiers first */
  433. for (i = 0; i < num_keycodes; i++) {
  434. int key = keycode[i] & KEY_MASK;
  435. if (key >= table->num_entries || table->xlate[key] == 0xff) {
  436. table = process_modifier(config, key,
  437. keycode[i] & KEY_RELEASE);
  438. }
  439. }
  440. /* Start conversion by looking for the first new keycode (by same). */
  441. for (i = same; i < num_keycodes; i++) {
  442. int key = keycode[i];
  443. int ch = 0xff;
  444. /*
  445. * For a normal key (with an ASCII value), add it; otherwise
  446. * translate special key to escape sequence if possible.
  447. */
  448. if (key < table->num_entries) {
  449. ch = table->xlate[key];
  450. if ((config->flags & FLAG_CAPS_LOCK) &&
  451. ch >= 'a' && ch <= 'z')
  452. ch -= 'a' - 'A';
  453. /* ban digit numbers if 'Num Lock' is not on */
  454. if (!(config->flags & FLAG_NUM_LOCK)) {
  455. if (key >= KEY_KP7 && key <= KEY_KPDOT &&
  456. key != KEY_KPMINUS && key != KEY_KPPLUS)
  457. ch = 0xff;
  458. }
  459. if (ch_count < max_chars && ch != 0xff)
  460. output_ch[ch_count++] = (uchar)ch;
  461. }
  462. if (ch == 0xff)
  463. ch_count += input_keycode_to_ansi364(config, key,
  464. output_ch, max_chars);
  465. }
  466. if (ch_count > max_chars) {
  467. debug("%s: Output char buffer overflow size=%d, need=%d\n",
  468. __func__, max_chars, ch_count);
  469. return -1;
  470. }
  471. /* ok, so return keys */
  472. return ch_count;
  473. }
  474. static int _input_send_keycodes(struct input_config *config, int keycode[],
  475. int num_keycodes, bool do_send)
  476. {
  477. char ch[num_keycodes * ANSI_CHAR_MAX];
  478. int count, i, same = 0;
  479. int is_repeat = 0;
  480. unsigned delay_ms;
  481. config->modifiers = 0;
  482. if (!input_check_keycodes(config, keycode, num_keycodes, &same)) {
  483. /*
  484. * Same as last time - is it time for another repeat?
  485. * TODO(sjg@chromium.org) We drop repeats here and since
  486. * the caller may not call in again for a while, our
  487. * auto-repeat speed is not quite correct. We should
  488. * insert another character if we later realise that we
  489. * have missed a repeat slot.
  490. */
  491. is_repeat = config->allow_repeats || (config->repeat_rate_ms &&
  492. (int)get_timer(config->next_repeat_ms) >= 0);
  493. if (!is_repeat)
  494. return 0;
  495. }
  496. count = input_keycodes_to_ascii(config, keycode, num_keycodes,
  497. ch, sizeof(ch), is_repeat ? 0 : same);
  498. if (do_send) {
  499. for (i = 0; i < count; i++)
  500. input_queue_ascii(config, ch[i]);
  501. }
  502. delay_ms = is_repeat ?
  503. config->repeat_rate_ms :
  504. config->repeat_delay_ms;
  505. config->next_repeat_ms = get_timer(0) + delay_ms;
  506. return count;
  507. }
  508. int input_send_keycodes(struct input_config *config, int keycode[],
  509. int num_keycodes)
  510. {
  511. return _input_send_keycodes(config, keycode, num_keycodes, true);
  512. }
  513. int input_add_keycode(struct input_config *config, int new_keycode,
  514. bool release)
  515. {
  516. int keycode[INPUT_MAX_MODIFIERS + 1];
  517. int count, i;
  518. /* Add the old keycodes which are not removed by this new one */
  519. for (i = 0, count = 0; i < config->num_prev_keycodes; i++) {
  520. int code = config->prev_keycodes[i];
  521. if (new_keycode == code) {
  522. if (release)
  523. continue;
  524. new_keycode = -1;
  525. }
  526. keycode[count++] = code;
  527. }
  528. if (!release && new_keycode != -1)
  529. keycode[count++] = new_keycode;
  530. debug("\ncodes for %02x/%d: ", new_keycode, release);
  531. for (i = 0; i < count; i++)
  532. debug("%02x ", keycode[i]);
  533. debug("\n");
  534. /* Don't output any ASCII characters if this is a key release */
  535. return _input_send_keycodes(config, keycode, count, !release);
  536. }
  537. int input_add_table(struct input_config *config, int left_keycode,
  538. int right_keycode, const uchar *xlate, int num_entries)
  539. {
  540. struct input_key_xlate *table;
  541. if (config->num_tables == INPUT_MAX_MODIFIERS) {
  542. debug("%s: Too many modifier tables\n", __func__);
  543. return -1;
  544. }
  545. table = &config->table[config->num_tables++];
  546. table->left_keycode = left_keycode;
  547. table->right_keycode = right_keycode;
  548. table->xlate = xlate;
  549. table->num_entries = num_entries;
  550. return 0;
  551. }
  552. void input_set_delays(struct input_config *config, int repeat_delay_ms,
  553. int repeat_rate_ms)
  554. {
  555. config->repeat_delay_ms = repeat_delay_ms;
  556. config->repeat_rate_ms = repeat_rate_ms;
  557. }
  558. void input_allow_repeats(struct input_config *config, bool allow_repeats)
  559. {
  560. config->allow_repeats = allow_repeats;
  561. }
  562. int input_leds_changed(struct input_config *config)
  563. {
  564. if (config->leds_changed)
  565. return config->leds;
  566. return -1;
  567. }
  568. int input_add_tables(struct input_config *config, bool german)
  569. {
  570. struct kbd_entry *entry;
  571. int mask;
  572. int ret;
  573. mask = german ? KBD_GERMAN : KBD_ENGLISH;
  574. for (entry = kbd_entry; entry->kbd_mask; entry++) {
  575. if (!(mask & entry->kbd_mask))
  576. continue;
  577. ret = input_add_table(config, entry->left_keycode,
  578. entry->right_keycode, entry->xlate,
  579. entry->num_entries);
  580. if (ret)
  581. return ret;
  582. }
  583. return 0;
  584. }
  585. int input_init(struct input_config *config, int leds)
  586. {
  587. memset(config, '\0', sizeof(*config));
  588. config->leds = leds;
  589. return 0;
  590. }
  591. int input_stdio_register(struct stdio_dev *dev)
  592. {
  593. int error;
  594. error = stdio_register(dev);
  595. #if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
  596. /* check if this is the standard input device */
  597. if (!error && strcmp(env_get("stdin"), dev->name) == 0) {
  598. /* reassign the console */
  599. if (OVERWRITE_CONSOLE ||
  600. console_assign(stdin, dev->name))
  601. return -1;
  602. }
  603. #else
  604. error = error;
  605. #endif
  606. return 0;
  607. }