]> git.proxmox.com Git - grub2.git/blob - kern/term.c
2003-11-17 Marco Gerards <metgerards@student.han.nl>
[grub2.git] / kern / term.c
1 /*
2 * PUPA -- Preliminary Universal Programming Architecture for GRUB
3 * Copyright (C) 2002 Free Software Foundation, Inc.
4 * Copyright (C) 2002,2003 Yoshinori K. Okuji <okuji@enbug.org>
5 *
6 * PUPA 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 PUPA; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <pupa/term.h>
22 #include <pupa/err.h>
23 #include <pupa/mm.h>
24
25 /* The list of terminals. */
26 static pupa_term_t pupa_term_list;
27
28 /* The current terminal. */
29 static pupa_term_t pupa_cur_term;
30
31 void
32 pupa_term_register (pupa_term_t term)
33 {
34 term->next = pupa_term_list;
35 pupa_term_list = term;
36 }
37
38 void
39 pupa_term_unregister (pupa_term_t term)
40 {
41 pupa_term_t *p, q;
42
43 for (p = &pupa_term_list, q = *p; q; p = &(q->next), q = q->next)
44 if (q == term)
45 {
46 *p = q->next;
47 break;
48 }
49 }
50
51 void
52 pupa_term_iterate (int (*hook) (pupa_term_t term))
53 {
54 pupa_term_t p;
55
56 for (p = pupa_term_list; p; p = p->next)
57 if (hook (p))
58 break;
59 }
60
61 pupa_err_t
62 pupa_term_set_current (pupa_term_t term)
63 {
64 if (pupa_cur_term && pupa_cur_term->fini)
65 if ((pupa_cur_term->fini) () != PUPA_ERR_NONE)
66 return pupa_errno;
67
68 if (term->init)
69 if ((term->init) () != PUPA_ERR_NONE)
70 return pupa_errno;
71
72 pupa_cur_term = term;
73 pupa_cls ();
74 return PUPA_ERR_NONE;
75 }
76
77 pupa_term_t
78 pupa_term_get_current (void)
79 {
80 return pupa_cur_term;
81 }
82
83 /* Put a Unicode character. */
84 void
85 pupa_putcode (pupa_uint32_t code)
86 {
87 if (code == '\t' && pupa_cur_term->getxy)
88 {
89 int n;
90
91 n = 8 - ((pupa_getxy () >> 8) & 7);
92 while (n--)
93 pupa_putcode (' ');
94
95 return;
96 }
97
98 (pupa_cur_term->putchar) (code);
99
100 if (code == '\n')
101 pupa_putcode ('\r');
102 }
103
104 /* Put a character. C is one byte of a UTF-8 stream.
105 This function gathers bytes until a valid Unicode character is found. */
106 void
107 pupa_putchar (int c)
108 {
109 static pupa_uint32_t code = 0;
110 static int count = 0;
111
112 if (count)
113 {
114 if ((c & 0xc0) != 0x80)
115 {
116 /* invalid */
117 code = '@';
118 count = 0;
119 }
120 else
121 {
122 code <<= 6;
123 code |= (c & 0x3f);
124 count--;
125 }
126 }
127 else
128 {
129 if ((c & 0x80) == 0x00)
130 code = c;
131 else if ((c & 0xe0) == 0xc0)
132 {
133 count = 1;
134 code = c & 0x1f;
135 }
136 else if ((c & 0xf0) == 0xe0)
137 {
138 count = 2;
139 code = c & 0x0f;
140 }
141 else if ((c & 0xf8) == 0xf0)
142 {
143 count = 3;
144 code = c & 0x07;
145 }
146 else if ((c & 0xfc) == 0xf8)
147 {
148 count = 4;
149 code = c & 0x03;
150 }
151 else if ((c & 0xfe) == 0xfc)
152 {
153 count = 5;
154 code = c & 0x01;
155 }
156 else
157 /* invalid */
158 code = '?';
159 }
160
161 if (count)
162 /* Not finished yet. */
163 return;
164
165 pupa_putcode (code);
166 }
167
168 int
169 pupa_getkey (void)
170 {
171 return (pupa_cur_term->getkey) ();
172 }
173
174 int
175 pupa_checkkey (void)
176 {
177 return (pupa_cur_term->checkkey) ();
178 }
179
180 pupa_uint16_t
181 pupa_getxy (void)
182 {
183 return (pupa_cur_term->getxy) ();
184 }
185
186 void
187 pupa_gotoxy (pupa_uint8_t x, pupa_uint8_t y)
188 {
189 (pupa_cur_term->gotoxy) (x, y);
190 }
191
192 void
193 pupa_cls (void)
194 {
195 if (pupa_cur_term->flags & PUPA_TERM_DUMB)
196 {
197 pupa_putchar ('\n');
198 pupa_refresh ();
199 }
200 else
201 (pupa_cur_term->cls) ();
202 }
203
204 void
205 pupa_setcolorstate (pupa_term_color_state state)
206 {
207 if (pupa_cur_term->setcolorstate)
208 (pupa_cur_term->setcolorstate) (state);
209 }
210
211 void
212 pupa_setcolor (pupa_uint8_t normal_color, pupa_uint8_t highlight_color)
213 {
214 if (pupa_cur_term->setcolor)
215 (pupa_cur_term->setcolor) (normal_color, highlight_color);
216 }
217
218 int
219 pupa_setcursor (int on)
220 {
221 static int prev = 1;
222 int ret = prev;
223
224 if (pupa_cur_term->setcursor)
225 {
226 (pupa_cur_term->setcursor) (on);
227 prev = on;
228 }
229
230 return ret;
231 }
232
233 void
234 pupa_refresh (void)
235 {
236 if (pupa_cur_term->refresh)
237 (pupa_cur_term->refresh) ();
238 }