]>
Commit | Line | Data |
---|---|---|
4b13b216 | 1 | /* console.c -- Ncurses console for GRUB. */ |
1f7315a3 | 2 | /* |
4b13b216 | 3 | * GRUB -- GRand Unified Bootloader |
e6b92c8a | 4 | * Copyright (C) 2003,2005 Free Software Foundation, Inc. |
1f7315a3 | 5 | * |
6 | * This program 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 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program 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. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
f4917dfd | 21 | #include <config.h> |
22 | ||
23 | #if defined(HAVE_NCURSES_CURSES_H) | |
24 | # include <ncurses/curses.h> | |
25 | #elif defined(HAVE_NCURSES_H) | |
26 | # include <ncurses.h> | |
27 | #elif defined(HAVE_CURSES_H) | |
28 | # include <curses.h> | |
29 | #endif | |
30 | ||
31 | /* For compatibility. */ | |
32 | #ifndef A_NORMAL | |
33 | # define A_NORMAL 0 | |
34 | #endif /* ! A_NORMAL */ | |
35 | #ifndef A_STANDOUT | |
36 | # define A_STANDOUT 0 | |
37 | #endif /* ! A_STANDOUT */ | |
38 | ||
8ceafda2 | 39 | #include <grub/machine/console.h> |
4b13b216 | 40 | #include <grub/term.h> |
41 | #include <grub/types.h> | |
1f7315a3 | 42 | |
4b13b216 | 43 | static int grub_console_attr = A_NORMAL; |
1f7315a3 | 44 | |
45 | static void | |
4b13b216 | 46 | grub_ncurses_putchar (grub_uint32_t c) |
1f7315a3 | 47 | { |
4d4e372e | 48 | /* Better than nothing. */ |
49 | switch (c) | |
50 | { | |
51 | case GRUB_TERM_DISP_LEFT: | |
52 | c = '<'; | |
53 | break; | |
54 | ||
55 | case GRUB_TERM_DISP_UP: | |
56 | c = '^'; | |
57 | break; | |
58 | ||
59 | case GRUB_TERM_DISP_RIGHT: | |
60 | c = '>'; | |
61 | break; | |
62 | ||
63 | case GRUB_TERM_DISP_DOWN: | |
64 | c = 'v'; | |
65 | break; | |
66 | ||
67 | case GRUB_TERM_DISP_HLINE: | |
68 | c = '-'; | |
69 | break; | |
70 | ||
71 | case GRUB_TERM_DISP_VLINE: | |
72 | c = '|'; | |
73 | break; | |
74 | ||
75 | case GRUB_TERM_DISP_UL: | |
76 | case GRUB_TERM_DISP_UR: | |
77 | case GRUB_TERM_DISP_LL: | |
78 | case GRUB_TERM_DISP_LR: | |
79 | c = '+'; | |
80 | break; | |
81 | ||
82 | default: | |
385c6a92 | 83 | /* ncurses does not support Unicode. */ |
84 | if (c > 0x7f) | |
85 | c = '?'; | |
4d4e372e | 86 | break; |
87 | } | |
88 | ||
4b13b216 | 89 | addch (c | grub_console_attr); |
1f7315a3 | 90 | } |
91 | ||
385c6a92 | 92 | static grub_ssize_t |
93 | grub_ncurses_getcharwidth (grub_uint32_t code __attribute__ ((unused))) | |
94 | { | |
95 | return 1; | |
96 | } | |
97 | ||
1f7315a3 | 98 | static void |
4b13b216 | 99 | grub_ncurses_setcolorstate (grub_term_color_state state) |
1f7315a3 | 100 | { |
101 | switch (state) | |
102 | { | |
4b13b216 | 103 | case GRUB_TERM_COLOR_STANDARD: |
104 | grub_console_attr = A_NORMAL; | |
1f7315a3 | 105 | break; |
4b13b216 | 106 | case GRUB_TERM_COLOR_NORMAL: |
107 | grub_console_attr = A_NORMAL; | |
1f7315a3 | 108 | break; |
4b13b216 | 109 | case GRUB_TERM_COLOR_HIGHLIGHT: |
110 | grub_console_attr = A_STANDOUT; | |
1f7315a3 | 111 | break; |
112 | default: | |
113 | break; | |
114 | } | |
115 | } | |
116 | ||
117 | /* XXX: This function is never called. */ | |
118 | static void | |
4b13b216 | 119 | grub_ncurses_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color) |
1f7315a3 | 120 | { |
121 | color_set (normal_color << 8 | highlight_color, 0); | |
122 | } | |
123 | ||
4d4e372e | 124 | static int saved_char = ERR; |
125 | ||
1f7315a3 | 126 | static int |
4b13b216 | 127 | grub_ncurses_checkkey (void) |
1f7315a3 | 128 | { |
4d4e372e | 129 | int c; |
130 | ||
131 | /* Check for SAVED_CHAR. This should not be true, because this | |
132 | means checkkey is called twice continuously. */ | |
133 | if (saved_char != ERR) | |
93f3a1d8 | 134 | return saved_char; |
4d4e372e | 135 | |
136 | wtimeout (stdscr, 100); | |
137 | c = getch (); | |
138 | /* If C is not ERR, then put it back in the input queue. */ | |
139 | if (c != ERR) | |
140 | { | |
141 | saved_char = c; | |
93f3a1d8 | 142 | return c; |
4d4e372e | 143 | } |
144 | ||
93f3a1d8 | 145 | return -1; |
1f7315a3 | 146 | } |
147 | ||
148 | static int | |
4b13b216 | 149 | grub_ncurses_getkey (void) |
1f7315a3 | 150 | { |
4d4e372e | 151 | int c; |
152 | ||
153 | /* If checkkey has already got a character, then return it. */ | |
154 | if (saved_char != ERR) | |
155 | { | |
156 | c = saved_char; | |
157 | saved_char = ERR; | |
158 | } | |
159 | else | |
160 | { | |
161 | wtimeout (stdscr, -1); | |
162 | c = getch (); | |
163 | } | |
1f7315a3 | 164 | |
165 | switch (c) | |
166 | { | |
167 | case KEY_LEFT: | |
4b13b216 | 168 | c = GRUB_CONSOLE_KEY_LEFT; |
1f7315a3 | 169 | break; |
170 | ||
171 | case KEY_RIGHT: | |
4b13b216 | 172 | c = GRUB_CONSOLE_KEY_RIGHT; |
1f7315a3 | 173 | break; |
174 | ||
175 | case KEY_UP: | |
4b13b216 | 176 | c = GRUB_CONSOLE_KEY_UP; |
1f7315a3 | 177 | break; |
178 | ||
179 | case KEY_DOWN: | |
4b13b216 | 180 | c = GRUB_CONSOLE_KEY_DOWN; |
1f7315a3 | 181 | break; |
182 | ||
183 | case KEY_IC: | |
4b13b216 | 184 | c = GRUB_CONSOLE_KEY_IC; |
1f7315a3 | 185 | break; |
186 | ||
187 | case KEY_DC: | |
4b13b216 | 188 | c = GRUB_CONSOLE_KEY_DC; |
1f7315a3 | 189 | break; |
190 | ||
191 | case KEY_BACKSPACE: | |
5aded270 | 192 | /* XXX: For some reason ncurses on xterm does not return |
193 | KEY_BACKSPACE. */ | |
194 | case 127: | |
4b13b216 | 195 | c = GRUB_CONSOLE_KEY_BACKSPACE; |
1f7315a3 | 196 | break; |
197 | ||
198 | case KEY_HOME: | |
4b13b216 | 199 | c = GRUB_CONSOLE_KEY_HOME; |
1f7315a3 | 200 | break; |
201 | ||
202 | case KEY_END: | |
4b13b216 | 203 | c = GRUB_CONSOLE_KEY_END; |
1f7315a3 | 204 | break; |
205 | ||
206 | case KEY_NPAGE: | |
4b13b216 | 207 | c = GRUB_CONSOLE_KEY_NPAGE; |
1f7315a3 | 208 | break; |
209 | ||
210 | case KEY_PPAGE: | |
4b13b216 | 211 | c = GRUB_CONSOLE_KEY_PPAGE; |
1f7315a3 | 212 | break; |
213 | } | |
214 | ||
215 | return c; | |
216 | } | |
217 | ||
4b13b216 | 218 | static grub_uint16_t |
219 | grub_ncurses_getxy (void) | |
1f7315a3 | 220 | { |
221 | int x; | |
222 | int y; | |
223 | ||
224 | getyx (stdscr, y, x); | |
225 | ||
226 | return (x << 8) | y; | |
227 | } | |
228 | ||
8de3495c | 229 | static grub_uint16_t |
230 | grub_ncurses_getwh (void) | |
231 | { | |
232 | int x; | |
233 | int y; | |
234 | ||
235 | getmaxyx (stdscr, y, x); | |
236 | ||
237 | return (x << 8) | y; | |
238 | } | |
239 | ||
1f7315a3 | 240 | static void |
4b13b216 | 241 | grub_ncurses_gotoxy (grub_uint8_t x, grub_uint8_t y) |
1f7315a3 | 242 | { |
243 | move (y, x); | |
244 | } | |
245 | ||
246 | static void | |
4b13b216 | 247 | grub_ncurses_cls (void) |
1f7315a3 | 248 | { |
249 | clear (); | |
250 | refresh (); | |
251 | } | |
252 | ||
253 | static void | |
4b13b216 | 254 | grub_ncurses_setcursor (int on) |
1f7315a3 | 255 | { |
256 | curs_set (on ? 1 : 0); | |
257 | } | |
258 | ||
259 | static void | |
4b13b216 | 260 | grub_ncurses_refresh (void) |
1f7315a3 | 261 | { |
262 | refresh (); | |
263 | } | |
264 | ||
4b13b216 | 265 | static grub_err_t |
266 | grub_ncurses_init (void) | |
1f7315a3 | 267 | { |
268 | initscr (); | |
4d4e372e | 269 | raw (); |
1f7315a3 | 270 | noecho (); |
271 | scrollok (stdscr, TRUE); | |
272 | ||
273 | nonl (); | |
274 | intrflush (stdscr, FALSE); | |
275 | keypad (stdscr, TRUE); | |
276 | start_color (); | |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
4b13b216 | 281 | static grub_err_t |
282 | grub_ncurses_fini (void) | |
1f7315a3 | 283 | { |
284 | endwin (); | |
8c8cc205 | 285 | return 0; |
1f7315a3 | 286 | } |
287 | ||
288 | \f | |
4b13b216 | 289 | static struct grub_term grub_ncurses_term = |
1f7315a3 | 290 | { |
291 | .name = "console", | |
4b13b216 | 292 | .init = grub_ncurses_init, |
293 | .fini = grub_ncurses_fini, | |
294 | .putchar = grub_ncurses_putchar, | |
385c6a92 | 295 | .getcharwidth = grub_ncurses_getcharwidth, |
4b13b216 | 296 | .checkkey = grub_ncurses_checkkey, |
297 | .getkey = grub_ncurses_getkey, | |
298 | .getxy = grub_ncurses_getxy, | |
8de3495c | 299 | .getwh = grub_ncurses_getwh, |
4b13b216 | 300 | .gotoxy = grub_ncurses_gotoxy, |
301 | .cls = grub_ncurses_cls, | |
302 | .setcolorstate = grub_ncurses_setcolorstate, | |
303 | .setcolor = grub_ncurses_setcolor, | |
304 | .setcursor = grub_ncurses_setcursor, | |
305 | .refresh = grub_ncurses_refresh, | |
1f7315a3 | 306 | .flags = 0, |
307 | .next = 0 | |
308 | }; | |
309 | ||
310 | void | |
4b13b216 | 311 | grub_console_init (void) |
1f7315a3 | 312 | { |
4b13b216 | 313 | grub_term_register (&grub_ncurses_term); |
314 | grub_term_set_current (&grub_ncurses_term); | |
1f7315a3 | 315 | } |
e6b92c8a | 316 | |
317 | void | |
318 | grub_console_fini (void) | |
319 | { | |
320 | grub_ncurses_fini (); | |
321 | } |