2009-07-05 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / kern / term.c
blob3583a301d2468d5d4620827436e8ee24e497aa26
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) ();
143 grub_uint16_t
144 grub_getxy (void)
146 return (grub_cur_term_output->getxy) ();
149 grub_uint16_t
150 grub_getwh (void)
152 return (grub_cur_term_output->getwh) ();
155 void
156 grub_gotoxy (grub_uint8_t x, grub_uint8_t y)
158 (grub_cur_term_output->gotoxy) (x, y);
161 void
162 grub_cls (void)
164 if ((grub_cur_term_output->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
166 grub_putchar ('\n');
167 grub_refresh ();
169 else
170 (grub_cur_term_output->cls) ();
173 void
174 grub_setcolorstate (grub_term_color_state state)
176 if (grub_cur_term_output->setcolorstate)
177 (grub_cur_term_output->setcolorstate) (state);
180 void
181 grub_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
183 if (grub_cur_term_output->setcolor)
184 (grub_cur_term_output->setcolor) (normal_color, highlight_color);
187 void
188 grub_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
190 if (grub_cur_term_output->getcolor)
191 (grub_cur_term_output->getcolor) (normal_color, highlight_color);
195 grub_setcursor (int on)
197 int ret = cursor_state;
199 if (grub_cur_term_output->setcursor)
201 (grub_cur_term_output->setcursor) (on);
202 cursor_state = on;
205 return ret;
209 grub_getcursor (void)
211 return cursor_state;
214 void
215 grub_refresh (void)
217 if (grub_cur_term_output->refresh)
218 (grub_cur_term_output->refresh) ();
221 void
222 grub_set_more (int onoff)
224 if (onoff == 1)
225 grub_more++;
226 else
227 grub_more--;
229 grub_more_lines = 0;