Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / kern / term.c
blob2acfdae7278775a0a1415ec8d34651abfe5e4e51
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2007 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/term.h>
20 #include <grub/err.h>
21 #include <grub/mm.h>
22 #include <grub/misc.h>
23 #include <grub/env.h>
25 /* The list of terminals. */
26 static grub_term_input_t grub_term_list_input;
27 static grub_term_output_t grub_term_list_output;
29 /* The current terminal. */
30 static grub_term_input_t grub_cur_term_input;
31 static grub_term_output_t grub_cur_term_output;
33 /* The amount of lines counted by the pager. */
34 static int grub_more_lines;
36 /* If the more pager is active. */
37 static int grub_more;
39 /* The current cursor state. */
40 static int cursor_state = 1;
42 void
43 grub_term_register_input (grub_term_input_t term)
45 term->next = grub_term_list_input;
46 grub_term_list_input = term;
47 if (! grub_cur_term_input)
48 grub_term_set_current_input (term);
51 void
52 grub_term_register_output (grub_term_output_t term)
54 term->next = grub_term_list_output;
55 grub_term_list_output = term;
56 if (! grub_cur_term_output)
57 grub_term_set_current_output (term);
60 void
61 grub_term_unregister_input (grub_term_input_t term)
63 grub_term_input_t *p, q;
65 for (p = &grub_term_list_input, q = *p; q; p = &(q->next), q = q->next)
66 if (q == term)
68 *p = q->next;
69 break;
73 void
74 grub_term_unregister_output (grub_term_output_t term)
76 grub_term_output_t *p, q;
78 for (p = &grub_term_list_output, q = *p; q; p = &(q->next), q = q->next)
79 if (q == term)
81 *p = q->next;
82 break;
86 void
87 grub_term_iterate_input (int (*hook) (grub_term_input_t term))
89 grub_term_input_t p;
91 for (p = grub_term_list_input; p; p = p->next)
92 if (hook (p))
93 break;
96 void
97 grub_term_iterate_output (int (*hook) (grub_term_output_t term))
99 grub_term_output_t p;
101 for (p = grub_term_list_output; p; p = p->next)
102 if (hook (p))
103 break;
106 grub_err_t
107 grub_term_set_current_input (grub_term_input_t term)
109 if (grub_cur_term_input && grub_cur_term_input->fini)
110 if ((grub_cur_term_input->fini) () != GRUB_ERR_NONE)
111 return grub_errno;
113 if (term->init)
114 if ((term->init) () != GRUB_ERR_NONE)
115 return grub_errno;
117 grub_cur_term_input = term;
118 return GRUB_ERR_NONE;
121 grub_err_t
122 grub_term_set_current_output (grub_term_output_t term)
124 if (grub_cur_term_output && grub_cur_term_output->fini)
125 if ((grub_cur_term_output->fini) () != GRUB_ERR_NONE)
126 return grub_errno;
128 if (term->init)
129 if ((term->init) () != GRUB_ERR_NONE)
130 return grub_errno;
132 grub_cur_term_output = term;
133 return GRUB_ERR_NONE;
136 grub_term_input_t
137 grub_term_get_current_input (void)
139 return grub_cur_term_input;
142 grub_term_output_t
143 grub_term_get_current_output (void)
145 return grub_cur_term_output;
148 /* Put a Unicode character. */
149 void
150 grub_putcode (grub_uint32_t code)
152 int height = grub_getwh () & 255;
154 if (code == '\t' && grub_cur_term_output->getxy)
156 int n;
158 n = 8 - ((grub_getxy () >> 8) & 7);
159 while (n--)
160 grub_putcode (' ');
162 return;
165 (grub_cur_term_output->putchar) (code);
167 if (code == '\n')
169 grub_putcode ('\r');
171 grub_more_lines++;
173 if (grub_more && grub_more_lines == height - 1)
175 char key;
176 int pos = grub_getxy ();
178 /* Show --MORE-- on the lower left side of the screen. */
179 grub_gotoxy (1, height - 1);
180 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
181 grub_printf ("--MORE--");
182 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
184 key = grub_getkey ();
186 /* Remove the message. */
187 grub_gotoxy (1, height - 1);
188 grub_printf (" ");
189 grub_gotoxy (pos >> 8, pos & 0xFF);
191 /* Scroll one lines or an entire page, depending on the key. */
192 if (key == '\r' || key =='\n')
193 grub_more_lines--;
194 else
195 grub_more_lines = 0;
200 /* Put a character. C is one byte of a UTF-8 stream.
201 This function gathers bytes until a valid Unicode character is found. */
202 void
203 grub_putchar (int c)
205 static grub_size_t size = 0;
206 static grub_uint8_t buf[6];
207 grub_uint32_t code;
208 grub_ssize_t ret;
210 buf[size++] = c;
211 ret = grub_utf8_to_ucs4 (&code, buf, size);
213 if (ret > 0)
215 size = 0;
216 grub_putcode (code);
218 else if (ret < 0)
220 size = 0;
221 grub_putcode ('?');
225 /* Return the number of columns occupied by the character code CODE. */
226 grub_ssize_t
227 grub_getcharwidth (grub_uint32_t code)
229 return (grub_cur_term_output->getcharwidth) (code);
233 grub_getkey (void)
235 return (grub_cur_term_input->getkey) ();
239 grub_checkkey (void)
241 return (grub_cur_term_input->checkkey) ();
244 grub_uint16_t
245 grub_getxy (void)
247 return (grub_cur_term_output->getxy) ();
250 grub_uint16_t
251 grub_getwh (void)
253 return (grub_cur_term_output->getwh) ();
256 void
257 grub_gotoxy (grub_uint8_t x, grub_uint8_t y)
259 (grub_cur_term_output->gotoxy) (x, y);
262 void
263 grub_cls (void)
265 if ((grub_cur_term_output->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
267 grub_putchar ('\n');
268 grub_refresh ();
270 else
271 (grub_cur_term_output->cls) ();
274 void
275 grub_setcolorstate (grub_term_color_state state)
277 if (grub_cur_term_output->setcolorstate)
278 (grub_cur_term_output->setcolorstate) (state);
281 void
282 grub_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
284 if (grub_cur_term_output->setcolor)
285 (grub_cur_term_output->setcolor) (normal_color, highlight_color);
288 void
289 grub_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
291 if (grub_cur_term_output->getcolor)
292 (grub_cur_term_output->getcolor) (normal_color, highlight_color);
296 grub_setcursor (int on)
298 int ret = cursor_state;
300 if (grub_cur_term_output->setcursor)
302 (grub_cur_term_output->setcursor) (on);
303 cursor_state = on;
306 return ret;
310 grub_getcursor (void)
312 return cursor_state;
315 void
316 grub_refresh (void)
318 if (grub_cur_term_output->refresh)
319 (grub_cur_term_output->refresh) ();
322 void
323 grub_set_more (int onoff)
325 if (onoff == 1)
326 grub_more++;
327 else
328 grub_more--;
330 grub_more_lines = 0;