2 * This file is part of the libpayload project.
4 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2008 Advanced Micro Devices, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * This file handles reading keystrokes from serial and the console
33 * and "cooking" them so that they are correct for curses.
34 * Also, implement key related functions (mainly wgetch)
37 * Actually cook the serial (handle special keys)
43 static int _halfdelay = 0;
45 /* ============== Serial ==================== */
47 /* We treat serial like a vt100 terminal. For now we
48 do the cooking in here, but we should probably eventually
49 pass it to dedicated vt100 code */
51 static int getkeyseq(char *buffer, int len)
55 for(i = 0; i < 75; i++) {
56 if (serial_havechar())
64 buffer[len++] = serial_getchar();
65 return getkeyseq(buffer, len);
85 { "[21~", KEY_F(10) },
86 { "[24~", KEY_F(12) },
90 static int handle_escape(void)
93 int len = getkeyseq(buffer, 0);
99 for(i = 0; escape_codes[i].seq != NULL; i++) {
100 char *p = escape_codes[i].seq;
102 for(t = 0; t < len; t++) {
103 if (!*p || *p != buffer[t])
109 return escape_codes[i].key;
115 static int cook_serial(unsigned char ch)
119 return KEY_BACKSPACE;
125 return handle_escape();
132 /* ================ Keyboard ================ */
134 /* Scancode macros */
136 #define DOWN(_c) (0x80 | (_c))
139 #define ISDOWN(_c) ((_c) & 0x80)
140 #define ISUP(_c) (!ISDOWN((_c)))
142 #define SCANCODE(_c) ((_c) & ~0x80)
144 /* Scancode definitions for the modifiers */
146 #define SCANCODE_RSHIFT 0x36
147 #define SCANCODE_LSHIFT 0x2a
148 #define SCANCODE_CAPSLOCK 0x3a
149 #define SCANCODE_LALT 0x38
150 #define SCANCODE_LCTRL 0x1d
154 #define SHIFT_MODIFIER 0x1
155 #define CAPSLOCK_MODIFIER 0x2
156 #define ALT_MODIFIER 0x4
157 #define CTRL_MODIFIER 0x8
159 #define CTRL(_c) (_c & 0x1f)
166 { CTRL('['), CTRL('[')},
179 { KEY_BACKSPACE, KEY_BACKSPACE},
180 { CTRL('I' ), KEY_BTAB }, /* 0x0F */
193 { KEY_ENTER, KEY_ENTER },
196 { 's', 'S' }, /* 0x1F */
212 { 'v', 'V' }, /* 0x2F */
219 { 0, 0 }, /* RSHIFT */
222 { ' ', ' ' }, /* Space */
223 { 0, 0 }, /* Capslock */
224 { KEY_F(1), KEY_F(1) },
225 { KEY_F(2), KEY_F(2) },
226 { KEY_F(3), KEY_F(3) },
227 { KEY_F(4), KEY_F(4) },
228 { KEY_F(5), KEY_F(5) }, /* 0x3F */
229 { KEY_F(6), KEY_F(6) },
230 { KEY_F(7), KEY_F(7) },
231 { KEY_F(8), KEY_F(8) },
232 { KEY_F(9), KEY_F(9) },
233 { KEY_F(10), KEY_F(10) },
234 { 0, 0 }, /* Numlock */
235 { 0, 0 }, /* Scroll lock */
236 { KEY_HOME, KEY_HOME },
238 { KEY_PPAGE, KEY_PPAGE },
240 { KEY_LEFT, KEY_LEFT },
242 { KEY_RIGHT, KEY_RIGHT },
244 { KEY_END, KEY_END }, /* 0x4F */
245 { KEY_DOWN, KEY_DOWN },
246 { KEY_NPAGE, KEY_NPAGE },
249 { 0, 0 }, /* sysreq */
251 { KEY_F(11), KEY_F(11) },
252 { KEY_F(12), KEY_F(12) },
255 static int cook_scancodes(unsigned char code)
257 static int modifiers = 0;
258 int ch = 0, sc, shift;
261 case DOWN(SCANCODE_RSHIFT):
262 case DOWN(SCANCODE_LSHIFT):
263 modifiers |= SHIFT_MODIFIER;
265 case UP(SCANCODE_RSHIFT):
266 case UP(SCANCODE_LSHIFT):
267 modifiers &= ~SHIFT_MODIFIER;
269 case UP(SCANCODE_CAPSLOCK):
270 if (modifiers & CAPSLOCK_MODIFIER)
271 modifiers &= ~CAPSLOCK_MODIFIER;
273 modifiers |= CAPSLOCK_MODIFIER;
275 case DOWN(SCANCODE_LALT):
276 modifiers |= ALT_MODIFIER;
278 case UP(SCANCODE_LALT):
279 modifiers &= ~ALT_MODIFIER;
281 case DOWN(SCANCODE_LCTRL):
282 modifiers |= CTRL_MODIFIER;
284 case UP(SCANCODE_LCTRL):
285 modifiers &= ~CTRL_MODIFIER;
289 /* Only process keys on an upstroke. */
295 if (sc == 0 || sc > 0x59)
298 shift = (modifiers & SHIFT_MODIFIER) ^ (modifiers & CAPSLOCK_MODIFIER);
300 ch = shift ? scancode_map[sc].shift : scancode_map[sc].normal;
302 if (modifiers & CTRL_MODIFIER)
303 ch = (ch >= 0x3F && ch <= 0x5F) ? CTRL(ch) : 0;
308 static int curses_getchar(int delay)
314 if (curses_flags & F_ENABLE_CONSOLE)
319 if ((curses_flags & F_ENABLE_SERIAL) &&
321 c = serial_getchar();
322 return cook_serial(c);
338 ret = cook_scancodes(c);
349 /* === Public functions === */
351 int wgetch(WINDOW *win)
355 if (_halfdelay || win->_delay)
356 delay = win->_delay ? 0 : _halfdelay;
358 return curses_getchar(delay);
361 int nodelay(WINDOW *win, NCURSES_BOOL flag)
363 win->_delay = flag ? 1 : 0;
367 int halfdelay(int tenths)
378 /* Remove half delay timeout. */
383 #ifdef CONFIG_VGA_CONSOLE
384 void curses_enable_vga(int state)
387 curses_flags |= F_ENABLE_CONSOLE;
389 curses_flags &= ~F_ENABLE_CONSOLE;
392 void curses_enable_vga(int state) { }
395 #ifdef CONFIG_SERIAL_CONSOLE
396 void curses_enable_serial(int state)
399 curses_flags |= F_ENABLE_SERIAL;
401 curses_flags &= ~F_ENABLE_SERIAL;
404 void curses_enable_serial(int state) { }