1 /* console.c -- Ncurses console for GRUB. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2007,2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
22 /* For compatibility. */
25 #endif /* ! A_NORMAL */
28 #endif /* ! A_STANDOUT */
30 #include <grub/emu/console.h>
31 #include <grub/term.h>
32 #include <grub/types.h>
34 #if defined(HAVE_NCURSES_CURSES_H)
35 # include <ncurses/curses.h>
36 #elif defined(HAVE_NCURSES_H)
38 #elif defined(HAVE_CURSES_H)
42 static int grub_console_attr
= A_NORMAL
;
44 grub_uint8_t grub_console_cur_color
= 7;
46 static const grub_uint8_t grub_console_standard_color
= 0x7;
50 static grub_uint8_t color_map
[NUM_COLORS
] =
65 grub_ncurses_putchar (struct grub_term_output
*term
__attribute__ ((unused
)),
66 const struct grub_unicode_glyph
*c
)
68 addch (c
->base
| grub_console_attr
);
72 grub_ncurses_setcolorstate (struct grub_term_output
*term
,
73 grub_term_color_state state
)
77 case GRUB_TERM_COLOR_STANDARD
:
78 grub_console_cur_color
= grub_console_standard_color
;
79 grub_console_attr
= A_NORMAL
;
81 case GRUB_TERM_COLOR_NORMAL
:
82 grub_console_cur_color
= term
->normal_color
;
83 grub_console_attr
= A_NORMAL
;
85 case GRUB_TERM_COLOR_HIGHLIGHT
:
86 grub_console_cur_color
= term
->highlight_color
;
87 grub_console_attr
= A_STANDOUT
;
97 fg
= (grub_console_cur_color
& 7);
98 bg
= (grub_console_cur_color
>> 4) & 7;
100 grub_console_attr
= (grub_console_cur_color
& 8) ? A_BOLD
: A_NORMAL
;
101 color_set ((bg
<< 3) + fg
, 0);
105 static int saved_char
= ERR
;
108 grub_ncurses_checkkey (struct grub_term_input
*term
__attribute__ ((unused
)))
112 /* Check for SAVED_CHAR. This should not be true, because this
113 means checkkey is called twice continuously. */
114 if (saved_char
!= ERR
)
117 wtimeout (stdscr
, 100);
119 /* If C is not ERR, then put it back in the input queue. */
130 grub_ncurses_getkey (struct grub_term_input
*term
__attribute__ ((unused
)))
134 /* If checkkey has already got a character, then return it. */
135 if (saved_char
!= ERR
)
142 wtimeout (stdscr
, -1);
173 /* XXX: For some reason ncurses on xterm does not return
176 c
= GRUB_TERM_BACKSPACE
;
200 grub_ncurses_getxy (struct grub_term_output
*term
__attribute__ ((unused
)))
205 getyx (stdscr
, y
, x
);
211 grub_ncurses_getwh (struct grub_term_output
*term
__attribute__ ((unused
)))
216 getmaxyx (stdscr
, y
, x
);
222 grub_ncurses_gotoxy (struct grub_term_output
*term
__attribute__ ((unused
)),
223 grub_uint8_t x
, grub_uint8_t y
)
229 grub_ncurses_cls (struct grub_term_output
*term
__attribute__ ((unused
)))
236 grub_ncurses_setcursor (struct grub_term_output
*term
__attribute__ ((unused
)),
239 curs_set (on
? 1 : 0);
243 grub_ncurses_refresh (struct grub_term_output
*term
__attribute__ ((unused
)))
249 grub_ncurses_init (struct grub_term_output
*term
__attribute__ ((unused
)))
254 scrollok (stdscr
, TRUE
);
257 intrflush (stdscr
, FALSE
);
258 keypad (stdscr
, TRUE
);
264 if ((COLORS
>= NUM_COLORS
) && (COLOR_PAIRS
>= NUM_COLORS
* NUM_COLORS
))
269 for (i
= 0; i
< NUM_COLORS
; i
++)
270 for (j
= 0; j
< NUM_COLORS
; j
++)
271 init_pair(n
++, color_map
[j
], color_map
[i
]);
281 grub_ncurses_fini (struct grub_term_output
*term
__attribute__ ((unused
)))
288 static struct grub_term_input grub_ncurses_term_input
=
291 .checkkey
= grub_ncurses_checkkey
,
292 .getkey
= grub_ncurses_getkey
,
295 static struct grub_term_output grub_ncurses_term_output
=
298 .init
= grub_ncurses_init
,
299 .fini
= grub_ncurses_fini
,
300 .putchar
= grub_ncurses_putchar
,
301 .getxy
= grub_ncurses_getxy
,
302 .getwh
= grub_ncurses_getwh
,
303 .gotoxy
= grub_ncurses_gotoxy
,
304 .cls
= grub_ncurses_cls
,
305 .setcolorstate
= grub_ncurses_setcolorstate
,
306 .setcursor
= grub_ncurses_setcursor
,
307 .refresh
= grub_ncurses_refresh
,
308 .flags
= GRUB_TERM_CODE_TYPE_ASCII
312 grub_console_init (void)
314 grub_term_register_output ("console", &grub_ncurses_term_output
);
315 grub_term_register_input ("console", &grub_ncurses_term_input
);
319 grub_console_fini (void)
321 grub_ncurses_fini (&grub_ncurses_term_output
);