2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / kern / term.c
blob94d5a9e1d5edd2f8b951b9247e3ccd9fb596330b
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2007,2008,2009 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 amount of lines counted by the pager. */
26 static int grub_more_lines;
28 /* If the more pager is active. */
29 static int grub_more;
31 /* The current cursor state. */
32 static int cursor_state = 1;
34 struct grub_handler_class grub_term_input_class =
36 .name = "terminal_input"
39 struct grub_handler_class grub_term_output_class =
41 .name = "terminal_output"
44 #define grub_cur_term_input grub_term_get_current_input ()
45 #define grub_cur_term_output grub_term_get_current_output ()
47 /* Put a Unicode character. */
48 void
49 grub_putcode (grub_uint32_t code)
51 int height = grub_getwh () & 255;
53 if (code == '\t' && grub_cur_term_output->getxy)
55 int n;
57 n = 8 - ((grub_getxy () >> 8) & 7);
58 while (n--)
59 grub_putcode (' ');
61 return;
64 (grub_cur_term_output->putchar) (code);
66 if (code == '\n')
68 grub_putcode ('\r');
70 grub_more_lines++;
72 if (grub_more && grub_more_lines == height - 1)
74 char key;
75 int pos = grub_getxy ();
77 /* Show --MORE-- on the lower left side of the screen. */
78 grub_gotoxy (1, height - 1);
79 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
80 grub_printf ("--MORE--");
81 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
83 key = grub_getkey ();
85 /* Remove the message. */
86 grub_gotoxy (1, height - 1);
87 grub_printf (" ");
88 grub_gotoxy (pos >> 8, pos & 0xFF);
90 /* Scroll one lines or an entire page, depending on the key. */
91 if (key == '\r' || key =='\n')
92 grub_more_lines--;
93 else
94 grub_more_lines = 0;
99 /* Put a character. C is one byte of a UTF-8 stream.
100 This function gathers bytes until a valid Unicode character is found. */
101 void
102 grub_putchar (int c)
104 static grub_size_t size = 0;
105 static grub_uint8_t buf[6];
106 grub_uint32_t code;
107 grub_ssize_t ret;
109 buf[size++] = c;
110 ret = grub_utf8_to_ucs4 (&code, 1, buf, size, 0);
112 if (ret > 0)
114 size = 0;
115 grub_putcode (code);
117 else if (ret < 0)
119 size = 0;
120 grub_putcode ('?');
124 /* Return the number of columns occupied by the character code CODE. */
125 grub_ssize_t
126 grub_getcharwidth (grub_uint32_t code)
128 return (grub_cur_term_output->getcharwidth) (code);
132 grub_getkey (void)
134 return (grub_cur_term_input->getkey) ();
138 grub_checkkey (void)
140 return (grub_cur_term_input->checkkey) ();
144 grub_getkeystatus (void)
146 if (grub_cur_term_input->getkeystatus)
147 return (grub_cur_term_input->getkeystatus) ();
148 else
149 return 0;
152 grub_uint16_t
153 grub_getxy (void)
155 return (grub_cur_term_output->getxy) ();
158 grub_uint16_t
159 grub_getwh (void)
161 return (grub_cur_term_output->getwh) ();
164 void
165 grub_gotoxy (grub_uint8_t x, grub_uint8_t y)
167 (grub_cur_term_output->gotoxy) (x, y);
170 void
171 grub_cls (void)
173 if ((grub_cur_term_output->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
175 grub_putchar ('\n');
176 grub_refresh ();
178 else
179 (grub_cur_term_output->cls) ();
182 void
183 grub_setcolorstate (grub_term_color_state state)
185 if (grub_cur_term_output->setcolorstate)
186 (grub_cur_term_output->setcolorstate) (state);
189 void
190 grub_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
192 if (grub_cur_term_output->setcolor)
193 (grub_cur_term_output->setcolor) (normal_color, highlight_color);
196 void
197 grub_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
199 if (grub_cur_term_output->getcolor)
200 (grub_cur_term_output->getcolor) (normal_color, highlight_color);
204 grub_setcursor (int on)
206 int ret = cursor_state;
208 if (grub_cur_term_output->setcursor)
210 (grub_cur_term_output->setcursor) (on);
211 cursor_state = on;
214 return ret;
218 grub_getcursor (void)
220 return cursor_state;
223 void
224 grub_refresh (void)
226 if (grub_cur_term_output->refresh)
227 (grub_cur_term_output->refresh) ();
230 void
231 grub_set_more (int onoff)
233 if (onoff == 1)
234 grub_more++;
235 else
236 grub_more--;
238 grub_more_lines = 0;