| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#include <libpayload-config.h> |
| 40 |
#include <usb/usb.h> |
| 41 |
#include "local.h" |
| 42 |
|
| 43 |
static int _halfdelay = 0; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#if CONFIG(LP_SERIAL_CONSOLE) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static int getkeyseq(char *buffer, int len, int max) |
| 53 |
{ |
| 54 |
int i; |
| 55 |
|
| 56 |
while (1) { |
| 57 |
for(i = 0; i < 75; i++) { |
| 58 |
if (serial_havechar()) |
| 59 |
break; |
| 60 |
mdelay(1); |
| 61 |
} |
| 62 |
|
| 63 |
if (i == 75) |
| 64 |
return len; |
| 65 |
|
| 66 |
buffer[len++] = serial_getchar(); |
| 67 |
if (len == max) |
| 68 |
return len; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static struct { |
| 73 |
const char *seq; |
| 74 |
int key; |
| 75 |
} escape_codes[] = { |
| 76 |
{ "[A", KEY_UP }, |
| 77 |
{ "[B", KEY_DOWN }, |
| 78 |
{ "[C", KEY_RIGHT }, |
| 79 |
{ "[D", KEY_LEFT }, |
| 80 |
{ "[F", KEY_END }, |
| 81 |
{ "[H", KEY_HOME }, |
| 82 |
{ "[2~", KEY_IC }, |
| 83 |
{ "[3~", KEY_DC }, |
| 84 |
{ "[5~", KEY_PPAGE }, |
| 85 |
{ "[6~", KEY_NPAGE }, |
| 86 |
{ "OP", KEY_F(1) }, |
| 87 |
{ "OQ", KEY_F(2) }, |
| 88 |
{ "OR", KEY_F(3) }, |
| 89 |
{ "OS", KEY_F(4) }, |
| 90 |
{ "[15~", KEY_F(5) }, |
| 91 |
{ "[17~", KEY_F(6) }, |
| 92 |
{ "[18~", KEY_F(7) }, |
| 93 |
{ "[19~", KEY_F(8) }, |
| 94 |
{ "[20~", KEY_F(9) }, |
| 95 |
{ "[21~", KEY_F(10) }, |
| 96 |
{ "[23~", KEY_F(11) }, |
| 97 |
{ "[24~", KEY_F(12) }, |
| 98 |
{ NULL }, |
| 99 |
}; |
| 100 |
|
| 101 |
static int handle_escape(void) |
| 102 |
{ |
| 103 |
char buffer[5]; |
| 104 |
int len = getkeyseq(buffer, 0, sizeof(buffer)); |
| 105 |
int i, t; |
| 106 |
|
| 107 |
if (len == 0) |
| 108 |
return 27; |
| 109 |
|
| 110 |
for(i = 0; escape_codes[i].seq != NULL; i++) { |
| 111 |
const char *p = escape_codes[i].seq; |
| 112 |
|
| 113 |
for(t = 0; t < len; t++) { |
| 114 |
if (!*p || *p != buffer[t]) |
| 115 |
break; |
| 116 |
p++; |
| 117 |
} |
| 118 |
|
| 119 |
if (t == len) |
| 120 |
return escape_codes[i].key; |
| 121 |
} |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int cook_serial(unsigned char ch) |
| 127 |
{ |
| 128 |
switch(ch) { |
| 129 |
case 8: |
| 130 |
return KEY_BACKSPACE; |
| 131 |
|
| 132 |
case 13: |
| 133 |
return KEY_ENTER; |
| 134 |
|
| 135 |
case 27: |
| 136 |
return handle_escape(); |
| 137 |
|
| 138 |
default: |
| 139 |
return ch; |
| 140 |
} |
| 141 |
} |
| 142 |
#endif |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
static int curses_getchar(int _delay) |
| 147 |
{ |
| 148 |
#if CONFIG(LP_USB_HID) || CONFIG(LP_PC_KEYBOARD) || \ |
| 149 |
CONFIG(LP_SERIAL_CONSOLE) |
| 150 |
unsigned short c; |
| 151 |
#endif |
| 152 |
|
| 153 |
do { |
| 154 |
#if CONFIG(LP_USB_HID) |
| 155 |
usb_poll(); |
| 156 |
if ((curses_flags & F_ENABLE_CONSOLE) && |
| 157 |
usbhid_havechar()) { |
| 158 |
c = usbhid_getchar(); |
| 159 |
if (c != 0) return c; |
| 160 |
} |
| 161 |
#endif |
| 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; |
| 167 |
} |
| 168 |
#endif |
| 169 |
|
| 170 |
#if CONFIG(LP_SERIAL_CONSOLE) |
| 171 |
if ((curses_flags & F_ENABLE_SERIAL) && |
| 172 |
serial_havechar()) { |
| 173 |
c = serial_getchar(); |
| 174 |
return cook_serial(c); |
| 175 |
} |
| 176 |
#endif |
| 177 |
|
| 178 |
if (_delay == 0) { |
| 179 |
break; |
| 180 |
} else if (_delay >= 10) { |
| 181 |
mdelay(10); |
| 182 |
_delay -= 10; |
| 183 |
} else if (_delay > 0) { |
| 184 |
mdelay(_delay); |
| 185 |
_delay = 0; |
| 186 |
} |
| 187 |
} while (1); |
| 188 |
|
| 189 |
return ERR; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
int wgetch(WINDOW *win) |
| 195 |
{ |
| 196 |
int _delay = -1; |
| 197 |
|
| 198 |
if (_halfdelay) |
| 199 |
_delay = _halfdelay; |
| 200 |
else |
| 201 |
_delay = win->_delay; |
| 202 |
|
| 203 |
return curses_getchar(_delay); |
| 204 |
} |
| 205 |
|
| 206 |
int nodelay(WINDOW *win, NCURSES_BOOL flag) |
| 207 |
{ |
| 208 |
win->_delay = flag ? 0 : -1; |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
int halfdelay(int tenths) |
| 213 |
{ |
| 214 |
if (tenths > 255) |
| 215 |
return ERR; |
| 216 |
|
| 217 |
_halfdelay = tenths; |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
int nocbreak(void) |
| 222 |
{ |
| 223 |
|
| 224 |
_halfdelay = 0; |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
#if CONFIG(LP_VGA_VIDEO_CONSOLE) |
| 229 |
void curses_enable_vga(int state) |
| 230 |
{ |
| 231 |
if (state) |
| 232 |
curses_flags |= F_ENABLE_CONSOLE; |
| 233 |
else |
| 234 |
curses_flags &= ~F_ENABLE_CONSOLE; |
| 235 |
} |
| 236 |
|
| 237 |
int curses_vga_enabled(void) |
| 238 |
{ |
| 239 |
return (curses_flags & F_ENABLE_CONSOLE) != 0; |
| 240 |
} |
| 241 |
#else |
| 242 |
void curses_enable_vga(int state) { } |
| 243 |
int curses_vga_enabled(void) { return 0; } |
| 244 |
#endif |
| 245 |
|
| 246 |
#if CONFIG(LP_SERIAL_CONSOLE) |
| 247 |
void curses_enable_serial(int state) |
| 248 |
{ |
| 249 |
if (state) |
| 250 |
curses_flags |= F_ENABLE_SERIAL; |
| 251 |
else |
| 252 |
curses_flags &= ~F_ENABLE_SERIAL; |
| 253 |
} |
| 254 |
|
| 255 |
int curses_serial_enabled(void) |
| 256 |
{ |
| 257 |
return (curses_flags & F_ENABLE_SERIAL) != 0; |
| 258 |
} |
| 259 |
|
| 260 |
#else |
| 261 |
void curses_enable_serial(int state) { } |
| 262 |
int curses_serial_enabled(void) { return 0; } |
| 263 |
#endif |