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