input.c 20 KB

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