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>
|
|
|
|
|
|
|
|
|
|
/* For compatibility. */
|
|
|
|
|
#ifndef A_NORMAL
|
|
|
|
|
# define A_NORMAL 0
|
|
|
|
|
#endif /* ! A_NORMAL */
|
|
|
|
|
#ifndef A_STANDOUT
|
|
|
|
|
# define A_STANDOUT 0
|
|
|
|
|
#endif /* ! A_STANDOUT */
|
|
|
|
|
|
2009-07-07 20:03:03 +00:00
|
|
|
|
#include <grub/util/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
|
|
|
|
|
2009-12-27 21:36:09 +00:00
|
|
|
|
#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
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-05-07 23:06:22 +00:00
|
|
|
|
static const grub_uint8_t grub_console_standard_color = 0x7;
|
2008-08-05 14:20:00 +00:00
|
|
|
|
|
|
|
|
|
#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
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_putchar (struct grub_term_output *term __attribute__ ((unused)),
|
|
|
|
|
const struct grub_unicode_glyph *c)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2010-03-15 20:14:11 +00:00
|
|
|
|
addch (c->base | grub_console_attr);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-05-07 23:06:22 +00:00
|
|
|
|
grub_ncurses_setcolorstate (struct grub_term_output *term,
|
2010-05-07 00:30:44 +00:00
|
|
|
|
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:
|
2010-05-07 23:06:22 +00:00
|
|
|
|
grub_console_cur_color = term->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:
|
2010-05-07 23:06:22 +00:00
|
|
|
|
grub_console_cur_color = term->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
|
|
|
|
}
|
|
|
|
|
|
2005-02-19 20:56:07 +00:00
|
|
|
|
static int saved_char = ERR;
|
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
static int
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_checkkey (struct grub_term_input *term __attribute__ ((unused)))
|
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
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_getkey (struct grub_term_input *term __attribute__ ((unused)))
|
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:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_LEFT;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_RIGHT:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_RIGHT;
|
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:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_UP;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_DOWN:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_DOWN;
|
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:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_DC;
|
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:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_BACKSPACE;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_HOME:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_HOME;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_END:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_END;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_NPAGE:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_NPAGE;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case KEY_PPAGE:
|
2010-05-07 23:08:58 +00:00
|
|
|
|
c = GRUB_TERM_PPAGE;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_uint16_t
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_getxy (struct grub_term_output *term __attribute__ ((unused)))
|
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
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_getwh (struct grub_term_output *term __attribute__ ((unused)))
|
2005-08-12 19:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
int x;
|
|
|
|
|
int y;
|
|
|
|
|
|
|
|
|
|
getmaxyx (stdscr, y, x);
|
|
|
|
|
|
|
|
|
|
return (x << 8) | y;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
static void
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
|
|
|
|
|
grub_uint8_t x, grub_uint8_t y)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
move (y, x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_cls (struct grub_term_output *term __attribute__ ((unused)))
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
clear ();
|
|
|
|
|
refresh ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_setcursor (struct grub_term_output *term __attribute__ ((unused)),
|
|
|
|
|
int on)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
curs_set (on ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_refresh (struct grub_term_output *term __attribute__ ((unused)))
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
refresh ();
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_err_t
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_init (struct grub_term_output *term __attribute__ ((unused)))
|
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
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_fini (struct grub_term_output *term __attribute__ ((unused)))
|
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,
|
|
|
|
|
.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,
|
|
|
|
|
.setcursor = grub_ncurses_setcursor,
|
2010-03-15 20:14:11 +00:00
|
|
|
|
.refresh = grub_ncurses_refresh,
|
|
|
|
|
.flags = GRUB_TERM_CODE_TYPE_ASCII
|
2003-11-17 18:07:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_console_init (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2010-01-09 22:42:17 +00:00
|
|
|
|
grub_term_register_output ("console", &grub_ncurses_term_output);
|
|
|
|
|
grub_term_register_input ("console", &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)
|
|
|
|
|
{
|
2010-05-07 00:30:44 +00:00
|
|
|
|
grub_ncurses_fini (&grub_ncurses_term_output);
|
2005-02-15 00:07:01 +00:00
|
|
|
|
}
|