2004-04-04 13:46:03 +00:00
|
|
|
|
/* console.c -- Ncurses console for GRUB. */
|
2003-11-17 18:07:09 +00:00
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2009-01-10 13:07:44 +00:00
|
|
|
|
* Copyright (C) 2003,2005,2007,2008 Free Software Foundation, Inc.
|
2003-11-17 18:07:09 +00:00
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2003-11-17 18:07:09 +00:00
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2003-11-17 18:07:09 +00:00
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2003-11-17 18:07:09 +00:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2003-11-17 18:07:09 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2005-08-07 17:12:52 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_NCURSES_CURSES_H)
|
|
|
|
|
# include <ncurses/curses.h>
|
|
|
|
|
#elif defined(HAVE_NCURSES_H)
|
|
|
|
|
# include <ncurses.h>
|
|
|
|
|
#elif defined(HAVE_CURSES_H)
|
|
|
|
|
# include <curses.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* For compatibility. */
|
|
|
|
|
#ifndef A_NORMAL
|
|
|
|
|
# define A_NORMAL 0
|
|
|
|
|
#endif /* ! A_NORMAL */
|
|
|
|
|
#ifndef A_STANDOUT
|
|
|
|
|
# define A_STANDOUT 0
|
|
|
|
|
#endif /* ! A_STANDOUT */
|
|
|
|
|
|
2005-08-06 15:50:07 +00:00
|
|
|
|
#include <grub/machine/console.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
|
#include <grub/term.h>
|
|
|
|
|
#include <grub/types.h>
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static int grub_console_attr = A_NORMAL;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
2008-08-05 14:20:00 +00:00
|
|
|
|
grub_uint8_t grub_console_cur_color = 7;
|
|
|
|
|
|
|
|
|
|
static grub_uint8_t grub_console_standard_color = 0x7;
|
|
|
|
|
static grub_uint8_t grub_console_normal_color = 0x7;
|
|
|
|
|
static grub_uint8_t grub_console_highlight_color = 0x70;
|
|
|
|
|
|
|
|
|
|
#define NUM_COLORS 8
|
|
|
|
|
|
|
|
|
|
static grub_uint8_t color_map[NUM_COLORS] =
|
|
|
|
|
{
|
|
|
|
|
COLOR_BLACK,
|
|
|
|
|
COLOR_BLUE,
|
|
|
|
|
COLOR_GREEN,
|
|
|
|
|
COLOR_CYAN,
|
|
|
|
|
COLOR_RED,
|
|
|
|
|
COLOR_MAGENTA,
|
|
|
|
|
COLOR_YELLOW,
|
|
|
|
|
COLOR_WHITE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int use_color;
|
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_putchar (grub_uint32_t c)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2005-02-19 20:56:07 +00:00
|
|
|
|
/* Better than nothing. */
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case GRUB_TERM_DISP_LEFT:
|
|
|
|
|
c = '<';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GRUB_TERM_DISP_UP:
|
|
|
|
|
c = '^';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GRUB_TERM_DISP_RIGHT:
|
|
|
|
|
c = '>';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GRUB_TERM_DISP_DOWN:
|
|
|
|
|
c = 'v';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GRUB_TERM_DISP_HLINE:
|
|
|
|
|
c = '-';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GRUB_TERM_DISP_VLINE:
|
|
|
|
|
c = '|';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GRUB_TERM_DISP_UL:
|
|
|
|
|
case GRUB_TERM_DISP_UR:
|
|
|
|
|
case GRUB_TERM_DISP_LL:
|
|
|
|
|
case GRUB_TERM_DISP_LR:
|
|
|
|
|
c = '+';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2005-08-21 07:22:51 +00:00
|
|
|
|
/* ncurses does not support Unicode. */
|
|
|
|
|
if (c > 0x7f)
|
|
|
|
|
c = '?';
|
2005-02-19 20:56:07 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
addch (c | grub_console_attr);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-08-21 07:22:51 +00:00
|
|
|
|
static grub_ssize_t
|
|
|
|
|
grub_ncurses_getcharwidth (grub_uint32_t code __attribute__ ((unused)))
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_setcolorstate (grub_term_color_state state)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2009-06-10 21:04:23 +00:00
|
|
|
|
switch (state)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
case GRUB_TERM_COLOR_STANDARD:
|
2008-08-05 14:20:00 +00:00
|
|
|
|
grub_console_cur_color = grub_console_standard_color;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_console_attr = A_NORMAL;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
case GRUB_TERM_COLOR_NORMAL:
|
2008-08-05 14:20:00 +00:00
|
|
|
|
grub_console_cur_color = grub_console_normal_color;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_console_attr = A_NORMAL;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
case GRUB_TERM_COLOR_HIGHLIGHT:
|
2008-08-05 14:20:00 +00:00
|
|
|
|
grub_console_cur_color = grub_console_highlight_color;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_console_attr = A_STANDOUT;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-08-05 14:20:00 +00:00
|
|
|
|
|
|
|
|
|
if (use_color)
|
|
|
|
|
{
|
|
|
|
|
grub_uint8_t fg, bg;
|
|
|
|
|
|
|
|
|
|
fg = (grub_console_cur_color & 7);
|
|
|
|
|
bg = (grub_console_cur_color >> 4) & 7;
|
|
|
|
|
|
|
|
|
|
grub_console_attr = (grub_console_cur_color & 8) ? A_BOLD : A_NORMAL;
|
|
|
|
|
color_set ((bg << 3) + fg, 0);
|
|
|
|
|
}
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* XXX: This function is never called. */
|
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2008-08-05 14:20:00 +00:00
|
|
|
|
grub_console_normal_color = normal_color;
|
|
|
|
|
grub_console_highlight_color = highlight_color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
grub_ncurses_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
|
|
|
|
|
{
|
|
|
|
|
*normal_color = grub_console_normal_color;
|
|
|
|
|
*highlight_color = grub_console_highlight_color;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-19 20:56:07 +00:00
|
|
|
|
static int saved_char = ERR;
|
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
static int
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_checkkey (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2005-02-19 20:56:07 +00:00
|
|
|
|
int c;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2005-02-19 20:56:07 +00:00
|
|
|
|
/* Check for SAVED_CHAR. This should not be true, because this
|
|
|
|
|
means checkkey is called twice continuously. */
|
|
|
|
|
if (saved_char != ERR)
|
2005-02-27 21:19:06 +00:00
|
|
|
|
return saved_char;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2005-02-19 20:56:07 +00:00
|
|
|
|
wtimeout (stdscr, 100);
|
|
|
|
|
c = getch ();
|
|
|
|
|
/* If C is not ERR, then put it back in the input queue. */
|
|
|
|
|
if (c != ERR)
|
|
|
|
|
{
|
|
|
|
|
saved_char = c;
|
2005-02-27 21:19:06 +00:00
|
|
|
|
return c;
|
2005-02-19 20:56:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-27 21:19:06 +00:00
|
|
|
|
return -1;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_getkey (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2005-02-19 20:56:07 +00:00
|
|
|
|
int c;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2005-02-19 20:56:07 +00:00
|
|
|
|
/* If checkkey has already got a character, then return it. */
|
|
|
|
|
if (saved_char != ERR)
|
|
|
|
|
{
|
|
|
|
|
c = saved_char;
|
|
|
|
|
saved_char = ERR;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
wtimeout (stdscr, -1);
|
|
|
|
|
c = getch ();
|
|
|
|
|
}
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case KEY_LEFT:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 2;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_RIGHT:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 6;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
case KEY_UP:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 16;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_DOWN:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 14;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_IC:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 24;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_DC:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 4;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_BACKSPACE:
|
2003-12-03 19:17:27 +00:00
|
|
|
|
/* XXX: For some reason ncurses on xterm does not return
|
|
|
|
|
KEY_BACKSPACE. */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
case 127:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 8;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_HOME:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 1;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_END:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 5;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_NPAGE:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 3;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_PPAGE:
|
2007-11-18 07:20:45 +00:00
|
|
|
|
c = 7;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_uint16_t
|
|
|
|
|
grub_ncurses_getxy (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
int x;
|
|
|
|
|
int y;
|
|
|
|
|
|
|
|
|
|
getyx (stdscr, y, x);
|
|
|
|
|
|
|
|
|
|
return (x << 8) | y;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-12 19:53:33 +00:00
|
|
|
|
static grub_uint16_t
|
|
|
|
|
grub_ncurses_getwh (void)
|
|
|
|
|
{
|
|
|
|
|
int x;
|
|
|
|
|
int y;
|
|
|
|
|
|
|
|
|
|
getmaxyx (stdscr, y, x);
|
|
|
|
|
|
|
|
|
|
return (x << 8) | y;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_gotoxy (grub_uint8_t x, grub_uint8_t y)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
move (y, x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_cls (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
clear ();
|
|
|
|
|
refresh ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_setcursor (int on)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
curs_set (on ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_ncurses_refresh (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
refresh ();
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_ncurses_init (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
initscr ();
|
2005-02-19 20:56:07 +00:00
|
|
|
|
raw ();
|
2003-11-17 18:07:09 +00:00
|
|
|
|
noecho ();
|
|
|
|
|
scrollok (stdscr, TRUE);
|
|
|
|
|
|
|
|
|
|
nonl ();
|
|
|
|
|
intrflush (stdscr, FALSE);
|
|
|
|
|
keypad (stdscr, TRUE);
|
2008-08-05 14:20:00 +00:00
|
|
|
|
|
|
|
|
|
if (has_colors ())
|
|
|
|
|
{
|
|
|
|
|
start_color ();
|
|
|
|
|
|
|
|
|
|
if ((COLORS >= NUM_COLORS) && (COLOR_PAIRS >= NUM_COLORS * NUM_COLORS))
|
|
|
|
|
{
|
|
|
|
|
int i, j, n;
|
|
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
for (i = 0; i < NUM_COLORS; i++)
|
|
|
|
|
for (j = 0; j < NUM_COLORS; j++)
|
|
|
|
|
init_pair(n++, color_map[j], color_map[i]);
|
|
|
|
|
|
|
|
|
|
use_color = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_ncurses_fini (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
endwin ();
|
2004-03-14 17:48:25 +00:00
|
|
|
|
return 0;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-11-08 12:17:51 +00:00
|
|
|
|
static struct grub_term_input grub_ncurses_term_input =
|
|
|
|
|
{
|
|
|
|
|
.name = "console",
|
|
|
|
|
.checkkey = grub_ncurses_checkkey,
|
|
|
|
|
.getkey = grub_ncurses_getkey,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct grub_term_output grub_ncurses_term_output =
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
.name = "console",
|
2004-04-04 13:46:03 +00:00
|
|
|
|
.init = grub_ncurses_init,
|
|
|
|
|
.fini = grub_ncurses_fini,
|
|
|
|
|
.putchar = grub_ncurses_putchar,
|
2005-08-21 07:22:51 +00:00
|
|
|
|
.getcharwidth = grub_ncurses_getcharwidth,
|
2004-04-04 13:46:03 +00:00
|
|
|
|
.getxy = grub_ncurses_getxy,
|
2005-08-12 19:53:33 +00:00
|
|
|
|
.getwh = grub_ncurses_getwh,
|
2004-04-04 13:46:03 +00:00
|
|
|
|
.gotoxy = grub_ncurses_gotoxy,
|
|
|
|
|
.cls = grub_ncurses_cls,
|
|
|
|
|
.setcolorstate = grub_ncurses_setcolorstate,
|
|
|
|
|
.setcolor = grub_ncurses_setcolor,
|
2008-08-05 14:20:00 +00:00
|
|
|
|
.getcolor = grub_ncurses_getcolor,
|
2004-04-04 13:46:03 +00:00
|
|
|
|
.setcursor = grub_ncurses_setcursor,
|
|
|
|
|
.refresh = grub_ncurses_refresh,
|
2003-11-17 18:07:09 +00:00
|
|
|
|
.flags = 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_console_init (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2009-04-14 18:12:14 +00:00
|
|
|
|
grub_term_register_output ("console", &grub_ncurses_term_output);
|
|
|
|
|
grub_term_register_input ("console", &grub_ncurses_term_input);
|
2008-11-08 12:17:51 +00:00
|
|
|
|
grub_term_set_current_output (&grub_ncurses_term_output);
|
|
|
|
|
grub_term_set_current_input (&grub_ncurses_term_input);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
2005-02-15 00:07:01 +00:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
grub_console_fini (void)
|
|
|
|
|
{
|
|
|
|
|
grub_ncurses_fini ();
|
|
|
|
|
}
|