2N/A/* console.c -- Ncurses console for GRUB. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2003,2005,2007,2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <config.h>
2N/A#include <config-util.h>
2N/A
2N/A/* For compatibility. */
2N/A#ifndef A_NORMAL
2N/A# define A_NORMAL 0
2N/A#endif /* ! A_NORMAL */
2N/A#ifndef A_STANDOUT
2N/A# define A_STANDOUT 0
2N/A#endif /* ! A_STANDOUT */
2N/A
2N/A#include <grub/emu/console.h>
2N/A#include <grub/term.h>
2N/A#include <grub/types.h>
2N/A
2N/A#if defined(HAVE_NCURSES_CURSES_H)
2N/A# include <ncurses/curses.h>
2N/A#elif defined(HAVE_NCURSES_H)
2N/A# include <ncurses.h>
2N/A#elif defined(HAVE_CURSES_H)
2N/A# include <curses.h>
2N/A#else
2N/A#error What the hell?
2N/A#endif
2N/A
2N/Astatic int grub_console_attr = A_NORMAL;
2N/A
2N/Agrub_uint8_t grub_console_cur_color = 7;
2N/A
2N/Astatic const grub_uint8_t grub_console_standard_color = 0x7;
2N/A
2N/A#define NUM_COLORS 8
2N/A
2N/Astatic grub_uint8_t color_map[NUM_COLORS] =
2N/A{
2N/A COLOR_BLACK,
2N/A COLOR_BLUE,
2N/A COLOR_GREEN,
2N/A COLOR_CYAN,
2N/A COLOR_RED,
2N/A COLOR_MAGENTA,
2N/A COLOR_YELLOW,
2N/A COLOR_WHITE
2N/A};
2N/A
2N/Astatic int use_color;
2N/A
2N/Astatic void
2N/Agrub_ncurses_putchar (struct grub_term_output *term __attribute__ ((unused)),
2N/A const struct grub_unicode_glyph *c)
2N/A{
2N/A addch (c->base | grub_console_attr);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_ncurses_setcolorstate (struct grub_term_output *term,
2N/A grub_term_color_state state)
2N/A{
2N/A switch (state)
2N/A {
2N/A case GRUB_TERM_COLOR_STANDARD:
2N/A grub_console_cur_color = grub_console_standard_color;
2N/A grub_console_attr = A_NORMAL;
2N/A break;
2N/A case GRUB_TERM_COLOR_NORMAL:
2N/A grub_console_cur_color = term->normal_color;
2N/A grub_console_attr = A_NORMAL;
2N/A break;
2N/A case GRUB_TERM_COLOR_HIGHLIGHT:
2N/A grub_console_cur_color = term->highlight_color;
2N/A grub_console_attr = A_STANDOUT;
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A
2N/A if (use_color)
2N/A {
2N/A grub_uint8_t fg, bg;
2N/A
2N/A fg = (grub_console_cur_color & 7);
2N/A bg = (grub_console_cur_color >> 4) & 7;
2N/A
2N/A grub_console_attr = (grub_console_cur_color & 8) ? A_BOLD : A_NORMAL;
2N/A color_set ((bg << 3) + fg, 0);
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/Agrub_ncurses_getkey (struct grub_term_input *term __attribute__ ((unused)))
2N/A{
2N/A int c;
2N/A
2N/A wtimeout (stdscr, 100);
2N/A c = getch ();
2N/A
2N/A switch (c)
2N/A {
2N/A case ERR:
2N/A return GRUB_TERM_NO_KEY;
2N/A case KEY_LEFT:
2N/A c = GRUB_TERM_KEY_LEFT;
2N/A break;
2N/A
2N/A case KEY_RIGHT:
2N/A c = GRUB_TERM_KEY_RIGHT;
2N/A break;
2N/A
2N/A case KEY_UP:
2N/A c = GRUB_TERM_KEY_UP;
2N/A break;
2N/A
2N/A case KEY_DOWN:
2N/A c = GRUB_TERM_KEY_DOWN;
2N/A break;
2N/A
2N/A case KEY_IC:
2N/A c = 24;
2N/A break;
2N/A
2N/A case KEY_DC:
2N/A c = GRUB_TERM_KEY_DC;
2N/A break;
2N/A
2N/A case KEY_BACKSPACE:
2N/A /* XXX: For some reason ncurses on xterm does not return
2N/A KEY_BACKSPACE. */
2N/A case 127:
2N/A c = '\b';
2N/A break;
2N/A
2N/A case KEY_HOME:
2N/A c = GRUB_TERM_KEY_HOME;
2N/A break;
2N/A
2N/A case KEY_END:
2N/A c = GRUB_TERM_KEY_END;
2N/A break;
2N/A
2N/A case KEY_NPAGE:
2N/A c = GRUB_TERM_KEY_NPAGE;
2N/A break;
2N/A
2N/A case KEY_PPAGE:
2N/A c = GRUB_TERM_KEY_PPAGE;
2N/A break;
2N/A }
2N/A
2N/A return c;
2N/A}
2N/A
2N/Astatic grub_uint16_t
2N/Agrub_ncurses_getxy (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A int x;
2N/A int y;
2N/A
2N/A getyx (stdscr, y, x);
2N/A
2N/A return (x << 8) | y;
2N/A}
2N/A
2N/Astatic grub_uint16_t
2N/Agrub_ncurses_getwh (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A int x;
2N/A int y;
2N/A
2N/A getmaxyx (stdscr, y, x);
2N/A
2N/A return (x << 8) | y;
2N/A}
2N/A
2N/Astatic void
2N/Agrub_ncurses_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
2N/A grub_uint8_t x, grub_uint8_t y)
2N/A{
2N/A move (y, x);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_ncurses_cls (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A clear ();
2N/A refresh ();
2N/A}
2N/A
2N/Astatic void
2N/Agrub_ncurses_setcursor (struct grub_term_output *term __attribute__ ((unused)),
2N/A int on)
2N/A{
2N/A curs_set (on ? 1 : 0);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_ncurses_refresh (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A refresh ();
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_ncurses_init (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A initscr ();
2N/A raw ();
2N/A noecho ();
2N/A scrollok (stdscr, TRUE);
2N/A
2N/A nonl ();
2N/A intrflush (stdscr, FALSE);
2N/A keypad (stdscr, TRUE);
2N/A
2N/A if (has_colors ())
2N/A {
2N/A start_color ();
2N/A
2N/A if ((COLORS >= NUM_COLORS) && (COLOR_PAIRS >= NUM_COLORS * NUM_COLORS))
2N/A {
2N/A int i, j, n;
2N/A
2N/A n = 0;
2N/A for (i = 0; i < NUM_COLORS; i++)
2N/A for (j = 0; j < NUM_COLORS; j++)
2N/A init_pair(n++, color_map[j], color_map[i]);
2N/A
2N/A use_color = 1;
2N/A }
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_ncurses_fini (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A endwin ();
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic struct grub_term_input grub_ncurses_term_input =
2N/A {
2N/A .name = "console",
2N/A .getkey = grub_ncurses_getkey,
2N/A };
2N/A
2N/Astatic struct grub_term_output grub_ncurses_term_output =
2N/A {
2N/A .name = "console",
2N/A .init = grub_ncurses_init,
2N/A .fini = grub_ncurses_fini,
2N/A .putchar = grub_ncurses_putchar,
2N/A .getxy = grub_ncurses_getxy,
2N/A .getwh = grub_ncurses_getwh,
2N/A .gotoxy = grub_ncurses_gotoxy,
2N/A .cls = grub_ncurses_cls,
2N/A .setcolorstate = grub_ncurses_setcolorstate,
2N/A .setcursor = grub_ncurses_setcursor,
2N/A .refresh = grub_ncurses_refresh,
2N/A .flags = GRUB_TERM_CODE_TYPE_ASCII
2N/A };
2N/A
2N/Avoid
2N/Agrub_console_init (void)
2N/A{
2N/A grub_term_register_output ("console", &grub_ncurses_term_output);
2N/A grub_term_register_input ("console", &grub_ncurses_term_input);
2N/A}
2N/A
2N/Avoid
2N/Agrub_console_fini (void)
2N/A{
2N/A grub_ncurses_fini (&grub_ncurses_term_output);
2N/A}