]> git.proxmox.com Git - grub2.git/blob - kern/term.c
Fix combining characters messing with width counter
[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/time.h>
25
26 struct grub_term_output *grub_term_outputs_disabled;
27 struct grub_term_input *grub_term_inputs_disabled;
28 struct grub_term_output *grub_term_outputs;
29 struct grub_term_input *grub_term_inputs;
30
31 void (*grub_newline_hook) (void) = NULL;
32
33 /* Put a Unicode character. */
34 static void
35 grub_putcode_dumb (grub_uint32_t code,
36 struct grub_term_output *term)
37 {
38 struct grub_unicode_glyph c =
39 {
40 .base = code,
41 .variant = 0,
42 .attributes = 0,
43 .ncomb = 0,
44 .combining = 0,
45 .estimated_width = 1
46 };
47
48 if (code == '\t' && term->getxy)
49 {
50 int n;
51
52 n = 8 - ((term->getxy () >> 8) & 7);
53 while (n--)
54 grub_putcode_dumb (' ', term);
55
56 return;
57 }
58
59 (term->putchar) (&c);
60 if (code == '\n')
61 grub_putcode_dumb ('\r', term);
62 }
63
64 static void
65 grub_xputs_dumb (const char *str)
66 {
67 for (; *str; str++)
68 {
69 grub_term_output_t term;
70 grub_uint32_t code = *str;
71 if (code > 0x7f)
72 code = '?';
73
74 FOR_ACTIVE_TERM_OUTPUTS(term)
75 grub_putcode_dumb (code, term);
76 }
77 }
78
79 void (*grub_xputs) (const char *str) = grub_xputs_dumb;
80
81 int
82 grub_getkey (void)
83 {
84 grub_term_input_t term;
85
86 grub_refresh ();
87
88 while (1)
89 {
90 FOR_ACTIVE_TERM_INPUTS(term)
91 {
92 int key = term->checkkey ();
93 if (key != -1)
94 return term->getkey ();
95 }
96
97 grub_cpu_idle ();
98 }
99 }
100
101 int
102 grub_checkkey (void)
103 {
104 grub_term_input_t term;
105
106 FOR_ACTIVE_TERM_INPUTS(term)
107 {
108 int key = term->checkkey ();
109 if (key != -1)
110 return key;
111 }
112
113 return -1;
114 }
115
116 int
117 grub_getkeystatus (void)
118 {
119 int status = 0;
120 grub_term_input_t term;
121
122 FOR_ACTIVE_TERM_INPUTS(term)
123 {
124 if (term->getkeystatus)
125 status |= term->getkeystatus ();
126 }
127
128 return status;
129 }
130
131 void
132 grub_cls (void)
133 {
134 struct grub_term_output *term;
135
136 FOR_ACTIVE_TERM_OUTPUTS(term)
137 {
138 if ((term->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
139 {
140 grub_putcode_dumb ('\n', term);
141 grub_term_refresh (term);
142 }
143 else
144 (term->cls) ();
145 }
146 }
147
148 void
149 grub_setcolorstate (grub_term_color_state state)
150 {
151 struct grub_term_output *term;
152
153 FOR_ACTIVE_TERM_OUTPUTS(term)
154 grub_term_setcolorstate (term, state);
155 }
156
157 void
158 grub_refresh (void)
159 {
160 struct grub_term_output *term;
161
162 FOR_ACTIVE_TERM_OUTPUTS(term)
163 grub_term_refresh (term);
164 }