]> git.proxmox.com Git - grub2.git/blame - kern/term.c
FORALL_ACTIVE_TERM_OUTPUTS macro
[grub2.git] / kern / term.c
CommitLineData
6a161fa9 1/*
4b13b216 2 * GRUB -- GRand Unified Bootloader
1e901a75 3 * Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
6a161fa9 4 *
5a79f472 5 * GRUB is free software: you can redistribute it and/or modify
6a161fa9 6 * it under the terms of the GNU General Public License as published by
5a79f472 7 * the Free Software Foundation, either version 3 of the License, or
6a161fa9 8 * (at your option) any later version.
9 *
5a79f472 10 * GRUB is distributed in the hope that it will be useful,
6a161fa9 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
5a79f472 16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
6a161fa9 17 */
18
4b13b216 19#include <grub/term.h>
20#include <grub/err.h>
21#include <grub/mm.h>
22#include <grub/misc.h>
4ce32619 23#include <grub/env.h>
8eca55a6 24#include <grub/cpu/time.h>
f4c623e1
VS
25
26struct grub_term_output *grub_term_outputs;
8eca55a6 27struct grub_term_input *grub_term_inputs;
6a161fa9 28
2e713831
VS
29void (*grub_newline_hook) (void) = NULL;
30
18d9c7cd 31/* Put a Unicode character. */
6a161fa9 32void
f4c623e1 33grub_putcode (grub_uint32_t code, struct grub_term_output *term)
6a161fa9 34{
f4c623e1 35 int height;
267f6cd9 36
2e713831 37 height = grub_term_height (term);
f4c623e1
VS
38
39 if (code == '\t' && term->getxy)
6a161fa9 40 {
41 int n;
b39f9d20 42
f4c623e1 43 n = 8 - ((term->getxy () >> 8) & 7);
6a161fa9 44 while (n--)
f4c623e1 45 grub_putcode (' ', term);
6a161fa9 46
47 return;
48 }
b39f9d20 49
2e713831
VS
50 if (code == '\n')
51 (term->putchar) ('\r');
f4c623e1 52 (term->putchar) (code);
18d9c7cd 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. */
57void
4b13b216 58grub_putchar (int c)
18d9c7cd 59{
ef095434 60 static grub_size_t size = 0;
61 static grub_uint8_t buf[6];
62 grub_uint32_t code;
63 grub_ssize_t ret;
18d9c7cd 64
ef095434 65 buf[size++] = c;
1e901a75 66 ret = grub_utf8_to_ucs4 (&code, 1, buf, size, 0);
b39f9d20 67
f4c623e1 68 if (ret != 0)
18d9c7cd 69 {
2322a126 70 struct grub_term_output *term;
ef095434 71 size = 0;
3be7f8de
VS
72 FOR_ACTIVE_TERM_OUTPUTS(term)
73 grub_putcode (code, term);
ef095434 74 }
2e713831
VS
75 if (ret == '\n' && grub_newline_hook)
76 grub_newline_hook ();
ef095434 77}
18d9c7cd 78
6a161fa9 79int
4b13b216 80grub_getkey (void)
6a161fa9 81{
8eca55a6
RM
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 }
6a161fa9 95}
96
97int
4b13b216 98grub_checkkey (void)
6a161fa9 99{
8eca55a6
RM
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;
6a161fa9 110}
111
4cbe67e5 112int
113grub_getkeystatus (void)
114{
8eca55a6
RM
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;
4cbe67e5 125}
126
6a161fa9 127void
4b13b216 128grub_cls (void)
6a161fa9 129{
2322a126 130 struct grub_term_output *term;
3be7f8de
VS
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 }
6a161fa9 142}
143
533110ad 144void
f4c623e1 145grub_setcolorstate (grub_term_color_state state)
533110ad 146{
2322a126
VS
147 struct grub_term_output *term;
148
3be7f8de
VS
149 FOR_ACTIVE_TERM_OUTPUTS(term)
150 grub_term_setcolorstate (term, state);
688e5699 151}
152
1f7315a3 153void
4b13b216 154grub_refresh (void)
1f7315a3 155{
2322a126 156 struct grub_term_output *term;
f4c623e1 157
3be7f8de
VS
158 FOR_ACTIVE_TERM_OUTPUTS(term)
159 grub_term_refresh (term);
db1771cf 160}