]> git.proxmox.com Git - grub2.git/blob - util/console.c
3af2cbe6250db743c4bbbae34294cfa8ea2ffc69
[grub2.git] / util / console.c
1 /* console.c -- Ncurses console for GRUB. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005 Free Software Foundation, Inc.
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
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
39 #include <grub/machine/console.h>
40 #include <grub/term.h>
41 #include <grub/types.h>
42
43 static int grub_console_attr = A_NORMAL;
44
45 static void
46 grub_ncurses_putchar (grub_uint32_t c)
47 {
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:
83 /* ncurses does not support Unicode. */
84 if (c > 0x7f)
85 c = '?';
86 break;
87 }
88
89 addch (c | grub_console_attr);
90 }
91
92 static grub_ssize_t
93 grub_ncurses_getcharwidth (grub_uint32_t code __attribute__ ((unused)))
94 {
95 return 1;
96 }
97
98 static void
99 grub_ncurses_setcolorstate (grub_term_color_state state)
100 {
101 switch (state)
102 {
103 case GRUB_TERM_COLOR_STANDARD:
104 grub_console_attr = A_NORMAL;
105 break;
106 case GRUB_TERM_COLOR_NORMAL:
107 grub_console_attr = A_NORMAL;
108 break;
109 case GRUB_TERM_COLOR_HIGHLIGHT:
110 grub_console_attr = A_STANDOUT;
111 break;
112 default:
113 break;
114 }
115 }
116
117 /* XXX: This function is never called. */
118 static void
119 grub_ncurses_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
120 {
121 color_set (normal_color << 8 | highlight_color, 0);
122 }
123
124 static int saved_char = ERR;
125
126 static int
127 grub_ncurses_checkkey (void)
128 {
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)
134 return saved_char;
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;
142 return c;
143 }
144
145 return -1;
146 }
147
148 static int
149 grub_ncurses_getkey (void)
150 {
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 }
164
165 switch (c)
166 {
167 case KEY_LEFT:
168 c = GRUB_CONSOLE_KEY_LEFT;
169 break;
170
171 case KEY_RIGHT:
172 c = GRUB_CONSOLE_KEY_RIGHT;
173 break;
174
175 case KEY_UP:
176 c = GRUB_CONSOLE_KEY_UP;
177 break;
178
179 case KEY_DOWN:
180 c = GRUB_CONSOLE_KEY_DOWN;
181 break;
182
183 case KEY_IC:
184 c = GRUB_CONSOLE_KEY_IC;
185 break;
186
187 case KEY_DC:
188 c = GRUB_CONSOLE_KEY_DC;
189 break;
190
191 case KEY_BACKSPACE:
192 /* XXX: For some reason ncurses on xterm does not return
193 KEY_BACKSPACE. */
194 case 127:
195 c = GRUB_CONSOLE_KEY_BACKSPACE;
196 break;
197
198 case KEY_HOME:
199 c = GRUB_CONSOLE_KEY_HOME;
200 break;
201
202 case KEY_END:
203 c = GRUB_CONSOLE_KEY_END;
204 break;
205
206 case KEY_NPAGE:
207 c = GRUB_CONSOLE_KEY_NPAGE;
208 break;
209
210 case KEY_PPAGE:
211 c = GRUB_CONSOLE_KEY_PPAGE;
212 break;
213 }
214
215 return c;
216 }
217
218 static grub_uint16_t
219 grub_ncurses_getxy (void)
220 {
221 int x;
222 int y;
223
224 getyx (stdscr, y, x);
225
226 return (x << 8) | y;
227 }
228
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
240 static void
241 grub_ncurses_gotoxy (grub_uint8_t x, grub_uint8_t y)
242 {
243 move (y, x);
244 }
245
246 static void
247 grub_ncurses_cls (void)
248 {
249 clear ();
250 refresh ();
251 }
252
253 static void
254 grub_ncurses_setcursor (int on)
255 {
256 curs_set (on ? 1 : 0);
257 }
258
259 static void
260 grub_ncurses_refresh (void)
261 {
262 refresh ();
263 }
264
265 static grub_err_t
266 grub_ncurses_init (void)
267 {
268 initscr ();
269 raw ();
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
281 static grub_err_t
282 grub_ncurses_fini (void)
283 {
284 endwin ();
285 return 0;
286 }
287
288 \f
289 static struct grub_term grub_ncurses_term =
290 {
291 .name = "console",
292 .init = grub_ncurses_init,
293 .fini = grub_ncurses_fini,
294 .putchar = grub_ncurses_putchar,
295 .getcharwidth = grub_ncurses_getcharwidth,
296 .checkkey = grub_ncurses_checkkey,
297 .getkey = grub_ncurses_getkey,
298 .getxy = grub_ncurses_getxy,
299 .getwh = grub_ncurses_getwh,
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,
306 .flags = 0,
307 .next = 0
308 };
309
310 void
311 grub_console_init (void)
312 {
313 grub_term_register (&grub_ncurses_term);
314 grub_term_set_current (&grub_ncurses_term);
315 }
316
317 void
318 grub_console_fini (void)
319 {
320 grub_ncurses_fini ();
321 }