3 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This file handles reading keystrokes from serial and the console
32 * and "cooking" them so that they are correct for curses.
33 * Also, implement key related functions (mainly wgetch)
36 * Actually cook the serial (handle special keys)
39 #include <libpayload-config.h>
43 static int _halfdelay
= 0;
45 /* ============== Serial ==================== */
47 #if CONFIG(LP_SERIAL_CONSOLE)
48 /* We treat serial like a vt100 terminal. For now we
49 do the cooking in here, but we should probably eventually
50 pass it to dedicated vt100 code */
52 static int getkeyseq(char *buffer
, int len
, int max
)
57 for(i
= 0; i
< 75; i
++) {
58 if (serial_havechar())
66 buffer
[len
++] = serial_getchar();
95 { "[21~", KEY_F(10) },
96 { "[23~", KEY_F(11) },
97 { "[24~", KEY_F(12) },
101 static int handle_escape(void)
104 int len
= getkeyseq(buffer
, 0, sizeof(buffer
));
110 for(i
= 0; escape_codes
[i
].seq
!= NULL
; i
++) {
111 const char *p
= escape_codes
[i
].seq
;
113 for(t
= 0; t
< len
; t
++) {
114 if (!*p
|| *p
!= buffer
[t
])
120 return escape_codes
[i
].key
;
126 static int cook_serial(unsigned char ch
)
130 return KEY_BACKSPACE
;
136 return handle_escape();
144 /* ================ Keyboard ================ */
146 static int curses_getchar(int _delay
)
148 #if CONFIG(LP_USB_HID) || CONFIG(LP_PC_KEYBOARD) || \
149 CONFIG(LP_SERIAL_CONSOLE)
154 #if CONFIG(LP_USB_HID)
156 if ((curses_flags
& F_ENABLE_CONSOLE
) &&
158 c
= usbhid_getchar();
159 if (c
!= 0) return c
;
162 #if CONFIG(LP_PC_KEYBOARD)
163 if ((curses_flags
& F_ENABLE_CONSOLE
) &&
164 keyboard_havechar()) {
165 c
= keyboard_getchar();
166 if (c
!= 0) return c
;
170 #if CONFIG(LP_SERIAL_CONSOLE)
171 if ((curses_flags
& F_ENABLE_SERIAL
) &&
173 c
= serial_getchar();
174 return cook_serial(c
);
180 } else if (_delay
>= 10) {
183 } else if (_delay
> 0) {
192 /* === Public functions === */
194 int wgetch(WINDOW
*win
)
201 _delay
= win
->_delay
;
203 return curses_getchar(_delay
);
206 int nodelay(WINDOW
*win
, NCURSES_BOOL flag
)
208 win
->_delay
= flag
? 0 : -1;
212 int halfdelay(int tenths
)
223 /* Remove half delay timeout. */
228 #if CONFIG(LP_VGA_VIDEO_CONSOLE)
229 void curses_enable_vga(int state
)
232 curses_flags
|= F_ENABLE_CONSOLE
;
234 curses_flags
&= ~F_ENABLE_CONSOLE
;
237 int curses_vga_enabled(void)
239 return (curses_flags
& F_ENABLE_CONSOLE
) != 0;
242 void curses_enable_vga(int state
) { }
243 int curses_vga_enabled(void) { return 0; }
246 #if CONFIG(LP_SERIAL_CONSOLE)
247 void curses_enable_serial(int state
)
250 curses_flags
|= F_ENABLE_SERIAL
;
252 curses_flags
&= ~F_ENABLE_SERIAL
;
255 int curses_serial_enabled(void)
257 return (curses_flags
& F_ENABLE_SERIAL
) != 0;
261 void curses_enable_serial(int state
) { }
262 int curses_serial_enabled(void) { return 0; }