2008-09-28 Robert Millan <rmh@aybabtu.com>
[grub2/phcoder.git] / kern / term.c
blob4c45d713d8b901260089347a6867c2f314da2875
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_t grub_term_list;
28 /* The current terminal. */
29 static grub_term_t grub_cur_term;
31 /* The amount of lines counted by the pager. */
32 static int grub_more_lines;
34 /* If the more pager is active. */
35 static int grub_more;
37 /* The current cursor state. */
38 static int cursor_state = 1;
40 void
41 grub_term_register (grub_term_t term)
43 term->next = grub_term_list;
44 grub_term_list = term;
47 void
48 grub_term_unregister (grub_term_t term)
50 grub_term_t *p, q;
52 for (p = &grub_term_list, q = *p; q; p = &(q->next), q = q->next)
53 if (q == term)
55 *p = q->next;
56 break;
60 void
61 grub_term_iterate (int (*hook) (grub_term_t term))
63 grub_term_t p;
65 for (p = grub_term_list; p; p = p->next)
66 if (hook (p))
67 break;
70 grub_err_t
71 grub_term_set_current (grub_term_t term)
73 if (grub_cur_term && grub_cur_term->fini)
74 if ((grub_cur_term->fini) () != GRUB_ERR_NONE)
75 return grub_errno;
77 if (term->init)
78 if ((term->init) () != GRUB_ERR_NONE)
79 return grub_errno;
81 grub_cur_term = term;
82 grub_cls ();
83 grub_setcursor (grub_getcursor ());
84 return GRUB_ERR_NONE;
87 grub_term_t
88 grub_term_get_current (void)
90 return grub_cur_term;
93 /* Put a Unicode character. */
94 void
95 grub_putcode (grub_uint32_t code)
97 int height = grub_getwh () & 255;
99 if (code == '\t' && grub_cur_term->getxy)
101 int n;
103 n = 8 - ((grub_getxy () >> 8) & 7);
104 while (n--)
105 grub_putcode (' ');
107 return;
110 (grub_cur_term->putchar) (code);
112 if (code == '\n')
114 grub_putcode ('\r');
116 grub_more_lines++;
118 if (grub_more && grub_more_lines == height - 1)
120 char key;
121 int pos = grub_getxy ();
123 /* Show --MORE-- on the lower left side of the screen. */
124 grub_gotoxy (1, height - 1);
125 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
126 grub_printf ("--MORE--");
127 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
129 key = grub_getkey ();
131 /* Remove the message. */
132 grub_gotoxy (1, height - 1);
133 grub_printf (" ");
134 grub_gotoxy (pos >> 8, pos & 0xFF);
136 /* Scroll one lines or an entire page, depending on the key. */
137 if (key == '\r' || key =='\n')
138 grub_more_lines--;
139 else
140 grub_more_lines = 0;
145 /* Put a character. C is one byte of a UTF-8 stream.
146 This function gathers bytes until a valid Unicode character is found. */
147 void
148 grub_putchar (int c)
150 static grub_size_t size = 0;
151 static grub_uint8_t buf[6];
152 grub_uint32_t code;
153 grub_ssize_t ret;
155 buf[size++] = c;
156 ret = grub_utf8_to_ucs4 (&code, buf, size);
158 if (ret > 0)
160 size = 0;
161 grub_putcode (code);
163 else if (ret < 0)
165 size = 0;
166 grub_putcode ('?');
170 /* Return the number of columns occupied by the character code CODE. */
171 grub_ssize_t
172 grub_getcharwidth (grub_uint32_t code)
174 return (grub_cur_term->getcharwidth) (code);
178 grub_getkey (void)
180 return (grub_cur_term->getkey) ();
184 grub_checkkey (void)
186 return (grub_cur_term->checkkey) ();
189 grub_uint16_t
190 grub_getxy (void)
192 return (grub_cur_term->getxy) ();
195 grub_uint16_t
196 grub_getwh (void)
198 return (grub_cur_term->getwh) ();
201 void
202 grub_gotoxy (grub_uint8_t x, grub_uint8_t y)
204 (grub_cur_term->gotoxy) (x, y);
207 void
208 grub_cls (void)
210 if ((grub_cur_term->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
212 grub_putchar ('\n');
213 grub_refresh ();
215 else
216 (grub_cur_term->cls) ();
219 void
220 grub_setcolorstate (grub_term_color_state state)
222 if (grub_cur_term->setcolorstate)
223 (grub_cur_term->setcolorstate) (state);
226 void
227 grub_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
229 if (grub_cur_term->setcolor)
230 (grub_cur_term->setcolor) (normal_color, highlight_color);
233 void
234 grub_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
236 if (grub_cur_term->getcolor)
237 (grub_cur_term->getcolor) (normal_color, highlight_color);
241 grub_setcursor (int on)
243 int ret = cursor_state;
245 if (grub_cur_term->setcursor)
247 (grub_cur_term->setcursor) (on);
248 cursor_state = on;
251 return ret;
255 grub_getcursor (void)
257 return cursor_state;
260 void
261 grub_refresh (void)
263 if (grub_cur_term->refresh)
264 (grub_cur_term->refresh) ();
267 void
268 grub_set_more (int onoff)
270 if (onoff == 1)
271 grub_more++;
272 else
273 grub_more--;
275 grub_more_lines = 0;