Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 92778b2 | 2015-10-18 21:17:12 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 12 | #include <stdio_dev.h> |
| 13 | #include <input.h> |
| 14 | #include <linux/input.h> |
| 15 | |
| 16 | enum { |
| 17 | /* These correspond to the lights on the keyboard */ |
| 18 | FLAG_NUM_LOCK = 1 << 0, |
| 19 | FLAG_CAPS_LOCK = 1 << 1, |
| 20 | FLAG_SCROLL_LOCK = 1 << 2, |
| 21 | |
| 22 | /* Special flag ORed with key code to indicate release */ |
| 23 | KEY_RELEASE = 1 << 15, |
| 24 | KEY_MASK = 0xfff, |
| 25 | }; |
| 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 */ |
Simon Glass | 77c7f04 | 2015-10-18 21:17:23 -0600 | [diff] [blame] | 46 | '\r', 0xff, '/', '*', |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 47 | }; |
| 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 */ |
Simon Glass | 77c7f04 | 2015-10-18 21:17:23 -0600 | [diff] [blame] | 62 | '\r', 0xff, '/', '*', |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 63 | }; |
| 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 */ |
Simon Glass | 2e5513b | 2015-10-18 21:17:22 -0600 | [diff] [blame] | 68 | 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 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 */ |
Simon Glass | 77c7f04 | 2015-10-18 21:17:23 -0600 | [diff] [blame] | 78 | '\r', 0xff, '/', '*', |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Scan key code to ANSI 3.64 escape sequence table. This table is |
| 83 | * incomplete in that it does not include all possible extra keys. |
| 84 | */ |
| 85 | static struct { |
| 86 | int kbd_scan_code; |
| 87 | char *escape; |
| 88 | } kbd_to_ansi364[] = { |
| 89 | { KEY_UP, "\033[A"}, |
| 90 | { KEY_DOWN, "\033[B"}, |
| 91 | { KEY_RIGHT, "\033[C"}, |
| 92 | { KEY_LEFT, "\033[D"}, |
| 93 | }; |
| 94 | |
| 95 | /* Maximum number of output characters that an ANSI sequence expands to */ |
| 96 | #define ANSI_CHAR_MAX 3 |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 97 | |
Kim Phillips | c14e94e | 2012-10-29 13:34:42 +0000 | [diff] [blame] | 98 | static int input_queue_ascii(struct input_config *config, int ch) |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 99 | { |
| 100 | if (config->fifo_in + 1 == INPUT_BUFFER_LEN) { |
| 101 | if (!config->fifo_out) |
| 102 | return -1; /* buffer full */ |
| 103 | else |
| 104 | config->fifo_in = 0; |
| 105 | } else { |
| 106 | if (config->fifo_in + 1 == config->fifo_out) |
| 107 | return -1; /* buffer full */ |
| 108 | config->fifo_in++; |
| 109 | } |
Simon Glass | 3a85e43 | 2015-10-18 21:17:24 -0600 | [diff] [blame] | 110 | debug(" {%02x} ", ch); |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 111 | config->fifo[config->fifo_in] = (uchar)ch; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int input_tstc(struct input_config *config) |
| 117 | { |
| 118 | if (config->fifo_in == config->fifo_out && config->read_keys) { |
| 119 | if (!(*config->read_keys)(config)) |
| 120 | return 0; |
| 121 | } |
| 122 | return config->fifo_in != config->fifo_out; |
| 123 | } |
| 124 | |
| 125 | int input_getc(struct input_config *config) |
| 126 | { |
| 127 | int err = 0; |
| 128 | |
| 129 | while (config->fifo_in == config->fifo_out) { |
| 130 | if (config->read_keys) |
| 131 | err = (*config->read_keys)(config); |
| 132 | if (err) |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | if (++config->fifo_out == INPUT_BUFFER_LEN) |
| 137 | config->fifo_out = 0; |
| 138 | |
| 139 | return config->fifo[config->fifo_out]; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Process a modifier/special key press or release and decide which key |
| 144 | * translation array should be used as a result. |
| 145 | * |
| 146 | * TODO: Should keep track of modifier press/release |
| 147 | * |
| 148 | * @param config Input state |
| 149 | * @param key Key code to process |
| 150 | * @param release 0 if a press, 1 if a release |
| 151 | * @return pointer to keycode->ascii translation table that should be used |
| 152 | */ |
| 153 | static struct input_key_xlate *process_modifier(struct input_config *config, |
| 154 | int key, int release) |
| 155 | { |
| 156 | struct input_key_xlate *table; |
| 157 | int flip = -1; |
| 158 | int i; |
| 159 | |
| 160 | /* Start with the main table, and see what modifiers change it */ |
| 161 | assert(config->num_tables > 0); |
| 162 | table = &config->table[0]; |
| 163 | for (i = 1; i < config->num_tables; i++) { |
| 164 | struct input_key_xlate *tab = &config->table[i]; |
| 165 | |
| 166 | if (key == tab->left_keycode || key == tab->right_keycode) |
| 167 | table = tab; |
| 168 | } |
| 169 | |
| 170 | /* Handle the lighted keys */ |
| 171 | if (!release) { |
| 172 | switch (key) { |
| 173 | case KEY_SCROLLLOCK: |
| 174 | flip = FLAG_SCROLL_LOCK; |
| 175 | break; |
| 176 | case KEY_NUMLOCK: |
| 177 | flip = FLAG_NUM_LOCK; |
| 178 | break; |
| 179 | case KEY_CAPSLOCK: |
| 180 | flip = FLAG_CAPS_LOCK; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (flip != -1) { |
| 186 | int leds = 0; |
| 187 | |
| 188 | config->leds ^= flip; |
| 189 | if (config->flags & FLAG_NUM_LOCK) |
| 190 | leds |= INPUT_LED_NUM; |
| 191 | if (config->flags & FLAG_CAPS_LOCK) |
| 192 | leds |= INPUT_LED_CAPS; |
| 193 | if (config->flags & FLAG_SCROLL_LOCK) |
| 194 | leds |= INPUT_LED_SCROLL; |
| 195 | config->leds = leds; |
| 196 | } |
| 197 | |
| 198 | return table; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Search an int array for a key value |
| 203 | * |
| 204 | * @param array Array to search |
| 205 | * @param count Number of elements in array |
| 206 | * @param key Key value to find |
| 207 | * @return element where value was first found, -1 if none |
| 208 | */ |
| 209 | static int array_search(int *array, int count, int key) |
| 210 | { |
| 211 | int i; |
| 212 | |
| 213 | for (i = 0; i < count; i++) { |
| 214 | if (array[i] == key) |
| 215 | return i; |
| 216 | } |
| 217 | |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sort an array so that those elements that exist in the ordering are |
| 223 | * first in the array, and in the same order as the ordering. The algorithm |
| 224 | * is O(count * ocount) and designed for small arrays. |
| 225 | * |
| 226 | * TODO: Move this to common / lib? |
| 227 | * |
| 228 | * @param dest Array with elements to sort, also destination array |
| 229 | * @param count Number of elements to sort |
| 230 | * @param order Array containing ordering elements |
| 231 | * @param ocount Number of ordering elements |
| 232 | * @return number of elements in dest that are in order (these will be at the |
| 233 | * start of dest). |
| 234 | */ |
| 235 | static int sort_array_by_ordering(int *dest, int count, int *order, |
| 236 | int ocount) |
| 237 | { |
| 238 | int temp[count]; |
| 239 | int dest_count; |
| 240 | int same; /* number of elements which are the same */ |
| 241 | int i; |
| 242 | |
| 243 | /* setup output items, copy items to be sorted into our temp area */ |
| 244 | memcpy(temp, dest, count * sizeof(*dest)); |
| 245 | dest_count = 0; |
| 246 | |
| 247 | /* work through the ordering, move over the elements we agree on */ |
| 248 | for (i = 0; i < ocount; i++) { |
| 249 | if (array_search(temp, count, order[i]) != -1) |
| 250 | dest[dest_count++] = order[i]; |
| 251 | } |
| 252 | same = dest_count; |
| 253 | |
| 254 | /* now move over the elements that are not in the ordering */ |
| 255 | for (i = 0; i < count; i++) { |
| 256 | if (array_search(order, ocount, temp[i]) == -1) |
| 257 | dest[dest_count++] = temp[i]; |
| 258 | } |
| 259 | assert(dest_count == count); |
| 260 | return same; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Check a list of key codes against the previous key scan |
| 265 | * |
| 266 | * Given a list of new key codes, we check how many of these are the same |
| 267 | * as last time. |
| 268 | * |
| 269 | * @param config Input state |
| 270 | * @param keycode List of key codes to examine |
| 271 | * @param num_keycodes Number of key codes |
| 272 | * @param same Returns number of key codes which are the same |
| 273 | */ |
| 274 | static int input_check_keycodes(struct input_config *config, |
| 275 | int keycode[], int num_keycodes, int *same) |
| 276 | { |
| 277 | /* Select the 'plain' xlate table to start with */ |
| 278 | if (!config->num_tables) { |
| 279 | debug("%s: No xlate tables: cannot decode keys\n", __func__); |
| 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | /* sort the keycodes into the same order as the previous ones */ |
| 284 | *same = sort_array_by_ordering(keycode, num_keycodes, |
| 285 | config->prev_keycodes, config->num_prev_keycodes); |
| 286 | |
| 287 | memcpy(config->prev_keycodes, keycode, num_keycodes * sizeof(int)); |
| 288 | config->num_prev_keycodes = num_keycodes; |
| 289 | |
| 290 | return *same != num_keycodes; |
| 291 | } |
| 292 | |
| 293 | /** |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 294 | * Checks and converts a special key code into ANSI 3.64 escape sequence. |
| 295 | * |
| 296 | * @param config Input state |
| 297 | * @param keycode Key code to examine |
| 298 | * @param output_ch Buffer to place output characters into. It should |
| 299 | * be at least ANSI_CHAR_MAX bytes long, to allow for |
| 300 | * an ANSI sequence. |
| 301 | * @param max_chars Maximum number of characters to add to output_ch |
| 302 | * @return number of characters output, if the key was converted, otherwise 0. |
| 303 | * This may be larger than max_chars, in which case the overflow |
| 304 | * characters are not output. |
| 305 | */ |
| 306 | static int input_keycode_to_ansi364(struct input_config *config, |
| 307 | int keycode, char output_ch[], int max_chars) |
| 308 | { |
| 309 | const char *escape; |
| 310 | int ch_count; |
| 311 | int i; |
| 312 | |
| 313 | for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) { |
| 314 | if (keycode != kbd_to_ansi364[i].kbd_scan_code) |
| 315 | continue; |
| 316 | for (escape = kbd_to_ansi364[i].escape; *escape; escape++) { |
| 317 | if (ch_count < max_chars) |
| 318 | output_ch[ch_count] = *escape; |
| 319 | ch_count++; |
| 320 | } |
| 321 | return ch_count; |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Converts and queues a list of key codes in escaped ASCII string form |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 329 | * Convert a list of key codes into ASCII |
| 330 | * |
| 331 | * You must call input_check_keycodes() before this. It turns the keycode |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 332 | * list into a list of ASCII characters and sends them to the input layer. |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 333 | * |
| 334 | * Characters which were seen last time do not generate fresh ASCII output. |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 335 | * The output (calls to queue_ascii) may be longer than num_keycodes, if the |
| 336 | * keycode contains special keys that was encoded to longer escaped sequence. |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 337 | * |
| 338 | * @param config Input state |
| 339 | * @param keycode List of key codes to examine |
| 340 | * @param num_keycodes Number of key codes |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 341 | * @param output_ch Buffer to place output characters into. It should |
| 342 | * be at last ANSI_CHAR_MAX * num_keycodes, to allow for |
| 343 | * ANSI sequences. |
| 344 | * @param max_chars Maximum number of characters to add to output_ch |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 345 | * @param same Number of key codes which are the same |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 346 | * @return number of characters written into output_ch, or -1 if we would |
| 347 | * exceed max_chars chars. |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 348 | */ |
| 349 | static int input_keycodes_to_ascii(struct input_config *config, |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 350 | int keycode[], int num_keycodes, char output_ch[], |
| 351 | int max_chars, int same) |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 352 | { |
| 353 | struct input_key_xlate *table; |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 354 | int ch_count = 0; |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 355 | int i; |
| 356 | |
| 357 | table = &config->table[0]; |
| 358 | |
| 359 | /* deal with modifiers first */ |
| 360 | for (i = 0; i < num_keycodes; i++) { |
| 361 | int key = keycode[i] & KEY_MASK; |
| 362 | |
| 363 | if (key >= table->num_entries || table->xlate[key] == 0xff) { |
| 364 | table = process_modifier(config, key, |
| 365 | keycode[i] & KEY_RELEASE); |
| 366 | } |
| 367 | } |
| 368 | |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 369 | /* Start conversion by looking for the first new keycode (by same). */ |
| 370 | for (i = same; i < num_keycodes; i++) { |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 371 | int key = keycode[i]; |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 372 | int ch = (key < table->num_entries) ? table->xlate[key] : 0xff; |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 373 | |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 374 | /* |
| 375 | * For a normal key (with an ASCII value), add it; otherwise |
| 376 | * translate special key to escape sequence if possible. |
| 377 | */ |
| 378 | if (ch != 0xff) { |
| 379 | if (ch_count < max_chars) |
| 380 | output_ch[ch_count] = (uchar)ch; |
| 381 | ch_count++; |
| 382 | } else { |
| 383 | ch_count += input_keycode_to_ansi364(config, key, |
| 384 | output_ch, max_chars); |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 388 | if (ch_count > max_chars) { |
| 389 | debug("%s: Output char buffer overflow size=%d, need=%d\n", |
| 390 | __func__, max_chars, ch_count); |
| 391 | return -1; |
| 392 | } |
| 393 | |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 394 | /* ok, so return keys */ |
| 395 | return ch_count; |
| 396 | } |
| 397 | |
Simon Glass | 3a85e43 | 2015-10-18 21:17:24 -0600 | [diff] [blame] | 398 | static int _input_send_keycodes(struct input_config *config, int keycode[], |
| 399 | int num_keycodes, bool do_send) |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 400 | { |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 401 | char ch[num_keycodes * ANSI_CHAR_MAX]; |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 402 | int count, i, same = 0; |
| 403 | int is_repeat = 0; |
| 404 | unsigned delay_ms; |
| 405 | |
| 406 | config->modifiers = 0; |
| 407 | if (!input_check_keycodes(config, keycode, num_keycodes, &same)) { |
| 408 | /* |
| 409 | * Same as last time - is it time for another repeat? |
| 410 | * TODO(sjg@chromium.org) We drop repeats here and since |
| 411 | * the caller may not call in again for a while, our |
| 412 | * auto-repeat speed is not quite correct. We should |
| 413 | * insert another character if we later realise that we |
| 414 | * have missed a repeat slot. |
| 415 | */ |
Simon Glass | 0b186c0 | 2015-10-18 21:17:25 -0600 | [diff] [blame^] | 416 | is_repeat = config->allow_repeats || (config->repeat_rate_ms && |
| 417 | (int)get_timer(config->next_repeat_ms) >= 0); |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 418 | if (!is_repeat) |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | count = input_keycodes_to_ascii(config, keycode, num_keycodes, |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 423 | ch, sizeof(ch), is_repeat ? 0 : same); |
Simon Glass | 3a85e43 | 2015-10-18 21:17:24 -0600 | [diff] [blame] | 424 | if (do_send) { |
| 425 | for (i = 0; i < count; i++) |
| 426 | input_queue_ascii(config, ch[i]); |
| 427 | } |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 428 | delay_ms = is_repeat ? |
| 429 | config->repeat_rate_ms : |
| 430 | config->repeat_delay_ms; |
| 431 | |
| 432 | config->next_repeat_ms = get_timer(0) + delay_ms; |
Hung-Te Lin | 44abe47 | 2012-10-11 15:15:53 +0000 | [diff] [blame] | 433 | |
| 434 | return count; |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Simon Glass | 3a85e43 | 2015-10-18 21:17:24 -0600 | [diff] [blame] | 437 | int input_send_keycodes(struct input_config *config, int keycode[], |
| 438 | int num_keycodes) |
| 439 | { |
| 440 | return _input_send_keycodes(config, keycode, num_keycodes, true); |
| 441 | } |
| 442 | |
| 443 | int input_add_keycode(struct input_config *config, int new_keycode, |
| 444 | bool release) |
| 445 | { |
| 446 | int keycode[INPUT_MAX_MODIFIERS + 1]; |
| 447 | int count, i; |
| 448 | |
| 449 | /* Add the old keycodes which are not removed by this new one */ |
| 450 | for (i = 0, count = 0; i < config->num_prev_keycodes; i++) { |
| 451 | int code = config->prev_keycodes[i]; |
| 452 | |
| 453 | if (new_keycode == code) { |
| 454 | if (release) |
| 455 | continue; |
| 456 | new_keycode = -1; |
| 457 | } |
| 458 | keycode[count++] = code; |
| 459 | } |
| 460 | |
| 461 | if (!release && new_keycode != -1) |
| 462 | keycode[count++] = new_keycode; |
| 463 | debug("\ncodes for %02x/%d: ", new_keycode, release); |
| 464 | for (i = 0; i < count; i++) |
| 465 | debug("%02x ", keycode[i]); |
| 466 | debug("\n"); |
| 467 | |
| 468 | /* Don't output any ASCII characters if this is a key release */ |
| 469 | return _input_send_keycodes(config, keycode, count, !release); |
| 470 | } |
| 471 | |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 472 | int input_add_table(struct input_config *config, int left_keycode, |
| 473 | int right_keycode, const uchar *xlate, int num_entries) |
| 474 | { |
| 475 | struct input_key_xlate *table; |
| 476 | |
| 477 | if (config->num_tables == INPUT_MAX_MODIFIERS) { |
| 478 | debug("%s: Too many modifier tables\n", __func__); |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | table = &config->table[config->num_tables++]; |
| 483 | table->left_keycode = left_keycode; |
| 484 | table->right_keycode = right_keycode; |
| 485 | table->xlate = xlate; |
| 486 | table->num_entries = num_entries; |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
Simon Glass | 1b1d3e6 | 2012-09-27 15:18:41 +0000 | [diff] [blame] | 491 | void input_set_delays(struct input_config *config, int repeat_delay_ms, |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 492 | int repeat_rate_ms) |
| 493 | { |
Simon Glass | 1b1d3e6 | 2012-09-27 15:18:41 +0000 | [diff] [blame] | 494 | config->repeat_delay_ms = repeat_delay_ms; |
| 495 | config->repeat_rate_ms = repeat_rate_ms; |
| 496 | } |
| 497 | |
Simon Glass | 0b186c0 | 2015-10-18 21:17:25 -0600 | [diff] [blame^] | 498 | void input_allow_repeats(struct input_config *config, bool allow_repeats) |
| 499 | { |
| 500 | config->allow_repeats = allow_repeats; |
| 501 | } |
| 502 | |
Simon Glass | 66877b0 | 2015-10-18 21:17:13 -0600 | [diff] [blame] | 503 | int input_add_tables(struct input_config *config) |
| 504 | { |
| 505 | int ret; |
| 506 | |
| 507 | ret = input_add_table(config, -1, -1, |
| 508 | kbd_plain_xlate, ARRAY_SIZE(kbd_plain_xlate)); |
| 509 | if (ret) |
| 510 | return ret; |
| 511 | ret = input_add_table(config, KEY_LEFTSHIFT, KEY_RIGHTSHIFT, |
| 512 | kbd_shift_xlate, ARRAY_SIZE(kbd_shift_xlate)); |
| 513 | if (ret) |
| 514 | return ret; |
| 515 | |
| 516 | return input_add_table(config, KEY_LEFTCTRL, KEY_RIGHTCTRL, |
| 517 | kbd_ctrl_xlate, ARRAY_SIZE(kbd_ctrl_xlate)); |
| 518 | } |
| 519 | |
Simon Glass | 1b1d3e6 | 2012-09-27 15:18:41 +0000 | [diff] [blame] | 520 | int input_init(struct input_config *config, int leds) |
| 521 | { |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 522 | memset(config, '\0', sizeof(*config)); |
| 523 | config->leds = leds; |
Simon Glass | 9bc590e | 2012-04-17 09:01:30 +0000 | [diff] [blame] | 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int input_stdio_register(struct stdio_dev *dev) |
| 529 | { |
| 530 | int error; |
| 531 | |
| 532 | error = stdio_register(dev); |
| 533 | |
| 534 | /* check if this is the standard input device */ |
| 535 | if (!error && strcmp(getenv("stdin"), dev->name) == 0) { |
| 536 | /* reassign the console */ |
| 537 | if (OVERWRITE_CONSOLE || |
| 538 | console_assign(stdin, dev->name)) |
| 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |