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 #if defined(HAVE_NCURSES_CURSES_H)
23 # include <ncurses/curses.h>
24 #elif defined(HAVE_NCURSES_H)
26 #elif defined(HAVE_CURSES_H)
30 /* For compatibility. */
33 #endif /* ! A_NORMAL */
36 #endif /* ! A_STANDOUT */
38 #include <grub/machine/console.h>
39 #include <grub/term.h>
40 #include <grub/types.h>
42 static int grub_console_attr
= A_NORMAL
;
44 grub_uint8_t grub_console_cur_color
= 7;
46 static grub_uint8_t grub_console_standard_color
= 0x7;
47 static grub_uint8_t grub_console_normal_color
= 0x7;
48 static grub_uint8_t grub_console_highlight_color
= 0x70;
52 static grub_uint8_t color_map
[NUM_COLORS
] =
67 grub_ncurses_putchar (grub_uint32_t c
)
69 /* Better than nothing. */
72 case GRUB_TERM_DISP_LEFT
:
76 case GRUB_TERM_DISP_UP
:
80 case GRUB_TERM_DISP_RIGHT
:
84 case GRUB_TERM_DISP_DOWN
:
88 case GRUB_TERM_DISP_HLINE
:
92 case GRUB_TERM_DISP_VLINE
:
96 case GRUB_TERM_DISP_UL
:
97 case GRUB_TERM_DISP_UR
:
98 case GRUB_TERM_DISP_LL
:
99 case GRUB_TERM_DISP_LR
:
104 /* ncurses does not support Unicode. */
110 addch (c
| grub_console_attr
);
114 grub_ncurses_getcharwidth (grub_uint32_t code
__attribute__ ((unused
)))
120 grub_ncurses_setcolorstate (grub_term_color_state state
)
124 case GRUB_TERM_COLOR_STANDARD
:
125 grub_console_cur_color
= grub_console_standard_color
;
126 grub_console_attr
= A_NORMAL
;
128 case GRUB_TERM_COLOR_NORMAL
:
129 grub_console_cur_color
= grub_console_normal_color
;
130 grub_console_attr
= A_NORMAL
;
132 case GRUB_TERM_COLOR_HIGHLIGHT
:
133 grub_console_cur_color
= grub_console_highlight_color
;
134 grub_console_attr
= A_STANDOUT
;
144 fg
= (grub_console_cur_color
& 7);
145 bg
= (grub_console_cur_color
>> 4) & 7;
147 grub_console_attr
= (grub_console_cur_color
& 8) ? A_BOLD
: A_NORMAL
;
148 color_set ((bg
<< 3) + fg
, 0);
152 /* XXX: This function is never called. */
154 grub_ncurses_setcolor (grub_uint8_t normal_color
, grub_uint8_t highlight_color
)
156 grub_console_normal_color
= normal_color
;
157 grub_console_highlight_color
= highlight_color
;
161 grub_ncurses_getcolor (grub_uint8_t
*normal_color
, grub_uint8_t
*highlight_color
)
163 *normal_color
= grub_console_normal_color
;
164 *highlight_color
= grub_console_highlight_color
;
167 static int saved_char
= ERR
;
170 grub_ncurses_checkkey (void)
174 /* Check for SAVED_CHAR. This should not be true, because this
175 means checkkey is called twice continuously. */
176 if (saved_char
!= ERR
)
179 wtimeout (stdscr
, 100);
181 /* If C is not ERR, then put it back in the input queue. */
192 grub_ncurses_getkey (void)
196 /* If checkkey has already got a character, then return it. */
197 if (saved_char
!= ERR
)
204 wtimeout (stdscr
, -1);
235 /* XXX: For some reason ncurses on xterm does not return
262 grub_ncurses_getxy (void)
267 getyx (stdscr
, y
, x
);
273 grub_ncurses_getwh (void)
278 getmaxyx (stdscr
, y
, x
);
284 grub_ncurses_gotoxy (grub_uint8_t x
, grub_uint8_t y
)
290 grub_ncurses_cls (void)
297 grub_ncurses_setcursor (int on
)
299 curs_set (on
? 1 : 0);
303 grub_ncurses_refresh (void)
309 grub_ncurses_init (void)
314 scrollok (stdscr
, TRUE
);
317 intrflush (stdscr
, FALSE
);
318 keypad (stdscr
, TRUE
);
324 if ((COLORS
>= NUM_COLORS
) && (COLOR_PAIRS
>= NUM_COLORS
* NUM_COLORS
))
329 for (i
= 0; i
< NUM_COLORS
; i
++)
330 for (j
= 0; j
< NUM_COLORS
; j
++)
331 init_pair(n
++, color_map
[j
], color_map
[i
]);
341 grub_ncurses_fini (void)
348 static struct grub_term_input grub_ncurses_term_input
=
351 .checkkey
= grub_ncurses_checkkey
,
352 .getkey
= grub_ncurses_getkey
,
355 static struct grub_term_output grub_ncurses_term_output
=
358 .init
= grub_ncurses_init
,
359 .fini
= grub_ncurses_fini
,
360 .putchar
= grub_ncurses_putchar
,
361 .getcharwidth
= grub_ncurses_getcharwidth
,
362 .getxy
= grub_ncurses_getxy
,
363 .getwh
= grub_ncurses_getwh
,
364 .gotoxy
= grub_ncurses_gotoxy
,
365 .cls
= grub_ncurses_cls
,
366 .setcolorstate
= grub_ncurses_setcolorstate
,
367 .setcolor
= grub_ncurses_setcolor
,
368 .getcolor
= grub_ncurses_getcolor
,
369 .setcursor
= grub_ncurses_setcursor
,
370 .refresh
= grub_ncurses_refresh
,
375 grub_console_init (void)
377 grub_term_register_output ("console", &grub_ncurses_term_output
);
378 grub_term_register_input ("console", &grub_ncurses_term_input
);
379 grub_term_set_current_output (&grub_ncurses_term_output
);
380 grub_term_set_current_input (&grub_ncurses_term_input
);
384 grub_console_fini (void)
386 grub_ncurses_fini ();