]> git.proxmox.com Git - grub2.git/blob - kern/term.c
FORALL_ACTIVE_TERM_OUTPUTS macro
[grub2.git] / kern / term.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
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>
24 #include <grub/cpu/time.h>
25
26 struct grub_term_output *grub_term_outputs;
27 struct grub_term_input *grub_term_inputs;
28
29 void (*grub_newline_hook) (void) = NULL;
30
31 /* Put a Unicode character. */
32 void
33 grub_putcode (grub_uint32_t code, struct grub_term_output *term)
34 {
35 int height;
36
37 height = grub_term_height (term);
38
39 if (code == '\t' && term->getxy)
40 {
41 int n;
42
43 n = 8 - ((term->getxy () >> 8) & 7);
44 while (n--)
45 grub_putcode (' ', term);
46
47 return;
48 }
49
50 if (code == '\n')
51 (term->putchar) ('\r');
52 (term->putchar) (code);
53 }
54
55 /* Put a character. C is one byte of a UTF-8 stream.
56 This function gathers bytes until a valid Unicode character is found. */
57 void
58 grub_putchar (int c)
59 {
60 static grub_size_t size = 0;
61 static grub_uint8_t buf[6];
62 grub_uint32_t code;
63 grub_ssize_t ret;
64
65 buf[size++] = c;
66 ret = grub_utf8_to_ucs4 (&code, 1, buf, size, 0);
67
68 if (ret != 0)
69 {
70 struct grub_term_output *term;
71 size = 0;
72 FOR_ACTIVE_TERM_OUTPUTS(term)
73 grub_putcode (code, term);
74 }
75 if (ret == '\n' && grub_newline_hook)
76 grub_newline_hook ();
77 }
78
79 int
80 grub_getkey (void)
81 {
82 grub_term_input_t term;
83
84 while (1)
85 {
86 FOR_ACTIVE_TERM_INPUTS(term)
87 {
88 int key = term->checkkey ();
89 if (key != -1)
90 return term->getkey ();
91 }
92
93 grub_cpu_idle ();
94 }
95 }
96
97 int
98 grub_checkkey (void)
99 {
100 grub_term_input_t term;
101
102 FOR_ACTIVE_TERM_INPUTS(term)
103 {
104 int key = term->checkkey ();
105 if (key != -1)
106 return key;
107 }
108
109 return -1;
110 }
111
112 int
113 grub_getkeystatus (void)
114 {
115 int status = 0;
116 grub_term_input_t term;
117
118 FOR_ACTIVE_TERM_INPUTS(term)
119 {
120 if (term->getkeystatus)
121 status |= term->getkeystatus ();
122 }
123
124 return status;
125 }
126
127 void
128 grub_cls (void)
129 {
130 struct grub_term_output *term;
131
132 FOR_ACTIVE_TERM_OUTPUTS(term)
133 {
134 if ((term->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
135 {
136 grub_putcode ('\n', term);
137 grub_refresh ();
138 }
139 else
140 (term->cls) ();
141 }
142 }
143
144 void
145 grub_setcolorstate (grub_term_color_state state)
146 {
147 struct grub_term_output *term;
148
149 FOR_ACTIVE_TERM_OUTPUTS(term)
150 grub_term_setcolorstate (term, state);
151 }
152
153 void
154 grub_refresh (void)
155 {
156 struct grub_term_output *term;
157
158 FOR_ACTIVE_TERM_OUTPUTS(term)
159 grub_term_refresh (term);
160 }