input.c 15 KB

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