]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/kern/term.c
keep up with 1.0.67 (#1464)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / 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 /* Current color state. */
32 grub_uint8_t grub_term_normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR;
33 grub_uint8_t grub_term_highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR;
34
35 void (*grub_term_poll_usb) (int wait_for_completion) = NULL;
36 void (*grub_net_poll_cards_idle) (void) = NULL;
37
38 /* Put a Unicode character. */
39 static void
40 grub_putcode_dumb (grub_uint32_t code,
41 struct grub_term_output *term)
42 {
43 struct grub_unicode_glyph c =
44 {
45 .base = code,
46 .variant = 0,
47 .attributes = 0,
48 .ncomb = 0,
49 .estimated_width = 1
50 };
51
52 if (code == '\t' && term->getxy)
53 {
54 int n;
55
56 n = GRUB_TERM_TAB_WIDTH - ((term->getxy (term).x)
57 % GRUB_TERM_TAB_WIDTH);
58 while (n--)
59 grub_putcode_dumb (' ', term);
60
61 return;
62 }
63
64 (term->putchar) (term, &c);
65 if (code == '\n')
66 grub_putcode_dumb ('\r', term);
67 }
68
69 static void
70 grub_xputs_dumb (const char *str)
71 {
72 for (; *str; str++)
73 {
74 grub_term_output_t term;
75 grub_uint32_t code = *str;
76 if (code > 0x7f)
77 code = '?';
78
79 FOR_ACTIVE_TERM_OUTPUTS(term)
80 grub_putcode_dumb (code, term);
81 }
82 }
83
84 void (*grub_xputs) (const char *str) = grub_xputs_dumb;
85
86 int (*grub_key_remap)(int key) = NULL;
87 int
88 grub_getkey_noblock (void)
89 {
90 grub_term_input_t term;
91
92 if (grub_term_poll_usb)
93 grub_term_poll_usb (0);
94
95 if (grub_net_poll_cards_idle)
96 grub_net_poll_cards_idle ();
97
98 FOR_ACTIVE_TERM_INPUTS(term)
99 {
100 int key = term->getkey (term);
101 if (grub_key_remap)
102 key = grub_key_remap(key);
103 if (key != GRUB_TERM_NO_KEY)
104 return key;
105 }
106
107 return GRUB_TERM_NO_KEY;
108 }
109
110 int
111 grub_getkey (void)
112 {
113 int ret;
114
115 grub_refresh ();
116
117 while (1)
118 {
119 ret = grub_getkey_noblock ();
120 if (ret != GRUB_TERM_NO_KEY)
121 return ret;
122 grub_cpu_idle ();
123 }
124 }
125
126 void
127 grub_refresh (void)
128 {
129 struct grub_term_output *term;
130
131 FOR_ACTIVE_TERM_OUTPUTS(term)
132 grub_term_refresh (term);
133 }