Ensure all lines that should be omitted from public includes are marked
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / emu / console.c
blob7fd1fc0c95728797a45a1699f48ce06a769e7b10
1 /* console.c -- Ncurses console for GRUB. */
2 /*
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/>.
20 #include <config.h>
21 #include <config-util.h>
23 /* For compatibility. */
24 #ifndef A_NORMAL
25 # define A_NORMAL 0
26 #endif /* ! A_NORMAL */
27 #ifndef A_STANDOUT
28 # define A_STANDOUT 0
29 #endif /* ! A_STANDOUT */
31 #include <grub/emu/console.h>
32 #include <grub/term.h>
33 #include <grub/types.h>
35 #if defined(HAVE_NCURSES_CURSES_H)
36 # include <ncurses/curses.h>
37 #elif defined(HAVE_NCURSES_H)
38 # include <ncurses.h>
39 #elif defined(HAVE_CURSES_H)
40 # include <curses.h>
41 #else
42 #error What the hell?
43 #endif
45 static int grub_console_attr = A_NORMAL;
47 grub_uint8_t grub_console_cur_color = 7;
49 static const grub_uint8_t grub_console_standard_color = 0x7;
51 #define NUM_COLORS 8
53 static grub_uint8_t color_map[NUM_COLORS] =
55 COLOR_BLACK,
56 COLOR_BLUE,
57 COLOR_GREEN,
58 COLOR_CYAN,
59 COLOR_RED,
60 COLOR_MAGENTA,
61 COLOR_YELLOW,
62 COLOR_WHITE
65 static int use_color;
67 static void
68 grub_ncurses_putchar (struct grub_term_output *term __attribute__ ((unused)),
69 const struct grub_unicode_glyph *c)
71 addch (c->base | grub_console_attr);
74 static void
75 grub_ncurses_setcolorstate (struct grub_term_output *term,
76 grub_term_color_state state)
78 switch (state)
80 case GRUB_TERM_COLOR_STANDARD:
81 grub_console_cur_color = grub_console_standard_color;
82 grub_console_attr = A_NORMAL;
83 break;
84 case GRUB_TERM_COLOR_NORMAL:
85 grub_console_cur_color = term->normal_color;
86 grub_console_attr = A_NORMAL;
87 break;
88 case GRUB_TERM_COLOR_HIGHLIGHT:
89 grub_console_cur_color = term->highlight_color;
90 grub_console_attr = A_STANDOUT;
91 break;
92 default:
93 break;
96 if (use_color)
98 grub_uint8_t fg, bg;
100 fg = (grub_console_cur_color & 7);
101 bg = (grub_console_cur_color >> 4) & 7;
103 grub_console_attr = (grub_console_cur_color & 8) ? A_BOLD : A_NORMAL;
104 color_set ((bg << 3) + fg, 0);
108 static int
109 grub_ncurses_getkey (struct grub_term_input *term __attribute__ ((unused)))
111 int c;
113 wtimeout (stdscr, 100);
114 c = getch ();
116 switch (c)
118 case ERR:
119 return -1;
120 case KEY_LEFT:
121 c = GRUB_TERM_KEY_LEFT;
122 break;
124 case KEY_RIGHT:
125 c = GRUB_TERM_KEY_RIGHT;
126 break;
128 case KEY_UP:
129 c = GRUB_TERM_KEY_UP;
130 break;
132 case KEY_DOWN:
133 c = GRUB_TERM_KEY_DOWN;
134 break;
136 case KEY_IC:
137 c = 24;
138 break;
140 case KEY_DC:
141 c = GRUB_TERM_KEY_DC;
142 break;
144 case KEY_BACKSPACE:
145 /* XXX: For some reason ncurses on xterm does not return
146 KEY_BACKSPACE. */
147 case 127:
148 c = '\b';
149 break;
151 case KEY_HOME:
152 c = GRUB_TERM_KEY_HOME;
153 break;
155 case KEY_END:
156 c = GRUB_TERM_KEY_END;
157 break;
159 case KEY_NPAGE:
160 c = GRUB_TERM_KEY_NPAGE;
161 break;
163 case KEY_PPAGE:
164 c = GRUB_TERM_KEY_PPAGE;
165 break;
168 return c;
171 static grub_uint16_t
172 grub_ncurses_getxy (struct grub_term_output *term __attribute__ ((unused)))
174 int x;
175 int y;
177 getyx (stdscr, y, x);
179 return (x << 8) | y;
182 static grub_uint16_t
183 grub_ncurses_getwh (struct grub_term_output *term __attribute__ ((unused)))
185 int x;
186 int y;
188 getmaxyx (stdscr, y, x);
190 return (x << 8) | y;
193 static void
194 grub_ncurses_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
195 grub_uint8_t x, grub_uint8_t y)
197 move (y, x);
200 static void
201 grub_ncurses_cls (struct grub_term_output *term __attribute__ ((unused)))
203 clear ();
204 refresh ();
207 static void
208 grub_ncurses_setcursor (struct grub_term_output *term __attribute__ ((unused)),
209 int on)
211 curs_set (on ? 1 : 0);
214 static void
215 grub_ncurses_refresh (struct grub_term_output *term __attribute__ ((unused)))
217 refresh ();
220 static grub_err_t
221 grub_ncurses_init (struct grub_term_output *term __attribute__ ((unused)))
223 initscr ();
224 raw ();
225 noecho ();
226 scrollok (stdscr, TRUE);
228 nonl ();
229 intrflush (stdscr, FALSE);
230 keypad (stdscr, TRUE);
232 if (has_colors ())
234 start_color ();
236 if ((COLORS >= NUM_COLORS) && (COLOR_PAIRS >= NUM_COLORS * NUM_COLORS))
238 int i, j, n;
240 n = 0;
241 for (i = 0; i < NUM_COLORS; i++)
242 for (j = 0; j < NUM_COLORS; j++)
243 init_pair(n++, color_map[j], color_map[i]);
245 use_color = 1;
249 return 0;
252 static grub_err_t
253 grub_ncurses_fini (struct grub_term_output *term __attribute__ ((unused)))
255 endwin ();
256 return 0;
260 static struct grub_term_input grub_ncurses_term_input =
262 .name = "console",
263 .getkey = grub_ncurses_getkey,
266 static struct grub_term_output grub_ncurses_term_output =
268 .name = "console",
269 .init = grub_ncurses_init,
270 .fini = grub_ncurses_fini,
271 .putchar = grub_ncurses_putchar,
272 .getxy = grub_ncurses_getxy,
273 .getwh = grub_ncurses_getwh,
274 .gotoxy = grub_ncurses_gotoxy,
275 .cls = grub_ncurses_cls,
276 .setcolorstate = grub_ncurses_setcolorstate,
277 .setcursor = grub_ncurses_setcursor,
278 .refresh = grub_ncurses_refresh,
279 .flags = GRUB_TERM_CODE_TYPE_ASCII
282 void
283 grub_console_init (void)
285 grub_term_register_output ("console", &grub_ncurses_term_output);
286 grub_term_register_input ("console", &grub_ncurses_term_input);
289 void
290 grub_console_fini (void)
292 grub_ncurses_fini (&grub_ncurses_term_output);