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>
22 #include <grub/misc.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. */
39 /* The current cursor state. */
40 static int cursor_state
= 1;
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
);
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
);
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
)
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
)
87 grub_term_iterate_input (int (*hook
) (grub_term_input_t term
))
91 for (p
= grub_term_list_input
; p
; p
= p
->next
)
97 grub_term_iterate_output (int (*hook
) (grub_term_output_t term
))
101 for (p
= grub_term_list_output
; p
; p
= p
->next
)
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
)
114 if ((term
->init
) () != GRUB_ERR_NONE
)
117 grub_cur_term_input
= term
;
118 return GRUB_ERR_NONE
;
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
)
129 if ((term
->init
) () != GRUB_ERR_NONE
)
132 grub_cur_term_output
= term
;
133 return GRUB_ERR_NONE
;
137 grub_term_get_current_input (void)
139 return grub_cur_term_input
;
143 grub_term_get_current_output (void)
145 return grub_cur_term_output
;
148 /* Put a Unicode character. */
150 grub_putcode (grub_uint32_t code
)
152 int height
= grub_getwh () & 255;
154 if (code
== '\t' && grub_cur_term_output
->getxy
)
158 n
= 8 - ((grub_getxy () >> 8) & 7);
165 (grub_cur_term_output
->putchar
) (code
);
173 if (grub_more
&& grub_more_lines
== height
- 1)
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);
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')
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. */
205 static grub_size_t size
= 0;
206 static grub_uint8_t buf
[6];
211 ret
= grub_utf8_to_ucs4 (&code
, buf
, size
);
225 /* Return the number of columns occupied by the character code CODE. */
227 grub_getcharwidth (grub_uint32_t code
)
229 return (grub_cur_term_output
->getcharwidth
) (code
);
235 return (grub_cur_term_input
->getkey
) ();
241 return (grub_cur_term_input
->checkkey
) ();
247 return (grub_cur_term_output
->getxy
) ();
253 return (grub_cur_term_output
->getwh
) ();
257 grub_gotoxy (grub_uint8_t x
, grub_uint8_t y
)
259 (grub_cur_term_output
->gotoxy
) (x
, y
);
265 if ((grub_cur_term_output
->flags
& GRUB_TERM_DUMB
) || (grub_env_get ("debug")))
271 (grub_cur_term_output
->cls
) ();
275 grub_setcolorstate (grub_term_color_state state
)
277 if (grub_cur_term_output
->setcolorstate
)
278 (grub_cur_term_output
->setcolorstate
) (state
);
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
);
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
);
310 grub_getcursor (void)
318 if (grub_cur_term_output
->refresh
)
319 (grub_cur_term_output
->refresh
) ();
323 grub_set_more (int onoff
)