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