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)
40 #include <libpayload-config.h>
44 static int _halfdelay
= 0;
46 /* ============== Serial ==================== */
48 #if IS_ENABLED(CONFIG_LP_SERIAL_CONSOLE)
49 /* We treat serial like a vt100 terminal. For now we
50 do the cooking in here, but we should probably eventually
51 pass it to dedicated vt100 code */
53 static int getkeyseq(char *buffer
, int len
, int max
)
58 for(i
= 0; i
< 75; i
++) {
59 if (serial_havechar())
67 buffer
[len
++] = serial_getchar();
96 { "[21~", KEY_F(10) },
97 { "[23~", KEY_F(11) },
98 { "[24~", KEY_F(12) },
102 static int handle_escape(void)
105 int len
= getkeyseq(buffer
, 0, sizeof(buffer
));
111 for(i
= 0; escape_codes
[i
].seq
!= NULL
; i
++) {
112 const char *p
= escape_codes
[i
].seq
;
114 for(t
= 0; t
< len
; t
++) {
115 if (!*p
|| *p
!= buffer
[t
])
121 return escape_codes
[i
].key
;
127 static int cook_serial(unsigned char ch
)
131 return KEY_BACKSPACE
;
137 return handle_escape();
145 /* ================ Keyboard ================ */
147 static int curses_getchar(int _delay
)
149 #if IS_ENABLED(CONFIG_LP_USB_HID) || IS_ENABLED(CONFIG_LP_PC_KEYBOARD) || \
150 IS_ENABLED(CONFIG_LP_SERIAL_CONSOLE)
155 #if IS_ENABLED(CONFIG_LP_USB_HID)
157 if ((curses_flags
& F_ENABLE_CONSOLE
) &&
159 c
= usbhid_getchar();
160 if (c
!= 0) return c
;
163 #if IS_ENABLED(CONFIG_LP_PC_KEYBOARD)
164 if ((curses_flags
& F_ENABLE_CONSOLE
) &&
165 keyboard_havechar()) {
166 c
= keyboard_getchar();
167 if (c
!= 0) return c
;
171 #if IS_ENABLED(CONFIG_LP_SERIAL_CONSOLE)
172 if ((curses_flags
& F_ENABLE_SERIAL
) &&
174 c
= serial_getchar();
175 return cook_serial(c
);
181 } else if (_delay
>= 10) {
184 } else if (_delay
> 0) {
193 /* === Public functions === */
195 int wgetch(WINDOW
*win
)
202 _delay
= win
->_delay
;
204 return curses_getchar(_delay
);
207 int nodelay(WINDOW
*win
, NCURSES_BOOL flag
)
209 win
->_delay
= flag
? 0 : -1;
213 int halfdelay(int tenths
)
224 /* Remove half delay timeout. */
229 #if IS_ENABLED(CONFIG_LP_VGA_VIDEO_CONSOLE)
230 void curses_enable_vga(int state
)
233 curses_flags
|= F_ENABLE_CONSOLE
;
235 curses_flags
&= ~F_ENABLE_CONSOLE
;
238 int curses_vga_enabled(void)
240 return (curses_flags
& F_ENABLE_CONSOLE
) != 0;
243 void curses_enable_vga(int state
) { }
244 int curses_vga_enabled(void) { return 0; }
247 #if IS_ENABLED(CONFIG_LP_SERIAL_CONSOLE)
248 void curses_enable_serial(int state
)
251 curses_flags
|= F_ENABLE_SERIAL
;
253 curses_flags
&= ~F_ENABLE_SERIAL
;
256 int curses_serial_enabled(void)
258 return (curses_flags
& F_ENABLE_SERIAL
) != 0;
262 void curses_enable_serial(int state
) { }
263 int curses_serial_enabled(void) { return 0; }