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