2008-03-20 00:56:58 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the libpayload project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
|
|
|
|
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file handles reading keystrokes from serial and the console
|
|
|
|
* and "cooking" them so that they are correct for curses.
|
|
|
|
* Also, implement key related functions (mainly wgetch)
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* Actually cook the serial (handle special keys)
|
|
|
|
*/
|
|
|
|
|
2008-11-24 18:54:46 +01:00
|
|
|
#include <libpayload-config.h>
|
2008-10-16 21:20:51 +02:00
|
|
|
#include <usb/usb.h>
|
2008-03-20 00:56:58 +01:00
|
|
|
#include "local.h"
|
|
|
|
|
2008-04-09 01:21:33 +02:00
|
|
|
static int _halfdelay = 0;
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
/* ============== Serial ==================== */
|
|
|
|
|
2008-09-26 20:36:26 +02:00
|
|
|
#ifdef CONFIG_SERIAL_CONSOLE
|
2008-06-20 02:01:42 +02:00
|
|
|
/* We treat serial like a vt100 terminal. For now we
|
|
|
|
do the cooking in here, but we should probably eventually
|
|
|
|
pass it to dedicated vt100 code */
|
|
|
|
|
2008-10-23 14:22:24 +02:00
|
|
|
static int getkeyseq(char *buffer, int len, int max)
|
2008-06-20 02:01:42 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-10-23 14:22:24 +02:00
|
|
|
while (1) {
|
|
|
|
for(i = 0; i < 75; i++) {
|
|
|
|
if (serial_havechar())
|
|
|
|
break;
|
|
|
|
mdelay(1);
|
|
|
|
}
|
2008-06-20 02:01:42 +02:00
|
|
|
|
2008-10-23 14:22:24 +02:00
|
|
|
if (i == 75)
|
|
|
|
return len;
|
2008-06-20 02:01:42 +02:00
|
|
|
|
2008-10-23 14:22:24 +02:00
|
|
|
buffer[len++] = serial_getchar();
|
|
|
|
if (len == max)
|
|
|
|
return len;
|
|
|
|
}
|
2008-06-20 02:01:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
char *seq;
|
|
|
|
int key;
|
|
|
|
} escape_codes[] = {
|
|
|
|
{ "[A", KEY_UP },
|
|
|
|
{ "[B", KEY_DOWN },
|
|
|
|
{ "[C", KEY_RIGHT },
|
|
|
|
{ "[D", KEY_LEFT },
|
2008-09-05 23:23:02 +02:00
|
|
|
{ "[F", KEY_END },
|
|
|
|
{ "[H", KEY_HOME },
|
|
|
|
{ "[2~", KEY_IC },
|
|
|
|
{ "[3~", KEY_DC },
|
|
|
|
{ "[5~", KEY_PPAGE },
|
|
|
|
{ "[6~", KEY_NPAGE },
|
2008-06-20 02:01:42 +02:00
|
|
|
{ "OP", KEY_F(1) },
|
|
|
|
{ "OQ", KEY_F(2) },
|
|
|
|
{ "OR", KEY_F(3) },
|
|
|
|
{ "OS", KEY_F(4) },
|
|
|
|
{ "[15~", KEY_F(5) },
|
|
|
|
{ "[17~", KEY_F(6) },
|
|
|
|
{ "[18~", KEY_F(7) },
|
|
|
|
{ "[19~", KEY_F(8) },
|
|
|
|
{ "[20~", KEY_F(9) },
|
|
|
|
{ "[21~", KEY_F(10) },
|
2008-09-05 23:23:02 +02:00
|
|
|
{ "[23~", KEY_F(11) },
|
2008-06-20 02:01:42 +02:00
|
|
|
{ "[24~", KEY_F(12) },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int handle_escape(void)
|
|
|
|
{
|
|
|
|
char buffer[5];
|
2008-10-23 14:22:24 +02:00
|
|
|
int len = getkeyseq(buffer, 0, sizeof(buffer));
|
2008-06-20 02:01:42 +02:00
|
|
|
int i, t;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 27;
|
|
|
|
|
|
|
|
for(i = 0; escape_codes[i].seq != NULL; i++) {
|
|
|
|
char *p = escape_codes[i].seq;
|
|
|
|
|
|
|
|
for(t = 0; t < len; t++) {
|
|
|
|
if (!*p || *p != buffer[t])
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t == len)
|
|
|
|
return escape_codes[i].key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
static int cook_serial(unsigned char ch)
|
|
|
|
{
|
2008-06-20 02:01:42 +02:00
|
|
|
switch(ch) {
|
|
|
|
case 8:
|
|
|
|
return KEY_BACKSPACE;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
return KEY_ENTER;
|
|
|
|
|
|
|
|
case 27:
|
|
|
|
return handle_escape();
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ch;
|
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
2008-09-26 20:36:26 +02:00
|
|
|
#endif
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
/* ================ Keyboard ================ */
|
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
static int curses_getchar(int delay)
|
|
|
|
{
|
2009-02-23 00:13:33 +01:00
|
|
|
#if defined(CONFIG_USB_HID) || defined(CONFIG_PC_KEYBOARD) || defined(CONFIG_SERIAL_CONSOLE)
|
2008-09-17 20:12:46 +02:00
|
|
|
unsigned short c;
|
2009-02-23 00:13:33 +01:00
|
|
|
#endif
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
do {
|
2008-10-16 21:20:51 +02:00
|
|
|
#ifdef CONFIG_USB_HID
|
|
|
|
usb_poll();
|
|
|
|
if ((curses_flags & F_ENABLE_CONSOLE) &&
|
|
|
|
usbhid_havechar()) {
|
|
|
|
c = usbhid_getchar();
|
|
|
|
if (c != 0) return c;
|
|
|
|
}
|
|
|
|
#endif
|
2008-09-17 20:12:46 +02:00
|
|
|
#ifdef CONFIG_PC_KEYBOARD
|
|
|
|
if ((curses_flags & F_ENABLE_CONSOLE) &&
|
|
|
|
keyboard_havechar()) {
|
|
|
|
c = keyboard_getchar();
|
|
|
|
if (c != 0) return c;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
2008-09-17 20:12:46 +02:00
|
|
|
#endif
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-09-17 20:12:46 +02:00
|
|
|
#ifdef CONFIG_SERIAL_CONSOLE
|
|
|
|
if ((curses_flags & F_ENABLE_SERIAL) &&
|
|
|
|
serial_havechar()) {
|
|
|
|
c = serial_getchar();
|
|
|
|
return cook_serial(c);
|
|
|
|
}
|
|
|
|
#endif
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-09-17 20:12:46 +02:00
|
|
|
if (delay == 0)
|
|
|
|
break;
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-09-17 20:12:46 +02:00
|
|
|
if (delay > 0) {
|
2009-07-31 13:38:59 +02:00
|
|
|
mdelay(1);
|
2008-09-17 20:12:46 +02:00
|
|
|
delay--;
|
2008-03-20 20:54:59 +01:00
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
|
2008-09-17 20:12:46 +02:00
|
|
|
|
2008-03-20 20:54:59 +01:00
|
|
|
} while (1);
|
2008-03-20 00:56:58 +01:00
|
|
|
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* === Public functions === */
|
|
|
|
|
|
|
|
int wgetch(WINDOW *win)
|
|
|
|
{
|
2008-04-09 01:21:33 +02:00
|
|
|
int delay = -1;
|
|
|
|
|
2009-07-31 13:38:59 +02:00
|
|
|
if (_halfdelay)
|
|
|
|
delay = _halfdelay;
|
|
|
|
else
|
|
|
|
delay = win->_delay;
|
2008-04-09 01:21:33 +02:00
|
|
|
|
|
|
|
return curses_getchar(delay);
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nodelay(WINDOW *win, NCURSES_BOOL flag)
|
|
|
|
{
|
2009-07-31 13:38:59 +02:00
|
|
|
win->_delay = flag ? 0 : -1;
|
2008-04-09 01:21:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int halfdelay(int tenths)
|
|
|
|
{
|
|
|
|
if (tenths > 255)
|
|
|
|
return ERR;
|
|
|
|
|
|
|
|
_halfdelay = tenths;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nocbreak(void)
|
|
|
|
{
|
|
|
|
/* Remove half delay timeout. */
|
|
|
|
_halfdelay = 0;
|
2008-03-20 21:46:44 +01:00
|
|
|
return 0;
|
2008-03-20 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_VGA_CONSOLE
|
|
|
|
void curses_enable_vga(int state)
|
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
curses_flags |= F_ENABLE_CONSOLE;
|
|
|
|
else
|
|
|
|
curses_flags &= ~F_ENABLE_CONSOLE;
|
|
|
|
}
|
2008-09-26 20:36:26 +02:00
|
|
|
|
|
|
|
int curses_vga_enabled(void)
|
|
|
|
{
|
|
|
|
return (curses_flags & F_ENABLE_CONSOLE) != 0;
|
|
|
|
}
|
2008-03-20 00:56:58 +01:00
|
|
|
#else
|
|
|
|
void curses_enable_vga(int state) { }
|
2008-09-26 20:36:26 +02:00
|
|
|
int curses_vga_enabled(void) { return 0; }
|
2008-03-20 00:56:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_CONSOLE
|
|
|
|
void curses_enable_serial(int state)
|
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
curses_flags |= F_ENABLE_SERIAL;
|
|
|
|
else
|
|
|
|
curses_flags &= ~F_ENABLE_SERIAL;
|
|
|
|
}
|
2008-09-26 20:36:26 +02:00
|
|
|
|
|
|
|
int curses_serial_enabled(void)
|
|
|
|
{
|
|
|
|
return (curses_flags & F_ENABLE_SERIAL) != 0;
|
|
|
|
}
|
|
|
|
|
2008-03-20 00:56:58 +01:00
|
|
|
#else
|
|
|
|
void curses_enable_serial(int state) { }
|
2008-09-26 20:36:26 +02:00
|
|
|
int curses_serial_enabled(void) { return 0; }
|
2008-03-20 00:56:58 +01:00
|
|
|
#endif
|
|
|
|
|