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