input.c 20 KB

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