]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/term.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / 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
87 grub_getkey_noblock (void)
88 {
89 grub_term_input_t term;
90
91 if (grub_term_poll_usb)
92 grub_term_poll_usb (0);
93
94 if (grub_net_poll_cards_idle)
95 grub_net_poll_cards_idle ();
96
97 FOR_ACTIVE_TERM_INPUTS(term)
98 {
99 int key = term->getkey (term);
100 if (key != GRUB_TERM_NO_KEY)
101 return key;
102 }
103
104 return GRUB_TERM_NO_KEY;
105 }
106
107 int
108 grub_getkey (void)
109 {
110 int ret;
111
112 grub_refresh ();
113
114 while (1)
115 {
116 ret = grub_getkey_noblock ();
117 if (ret != GRUB_TERM_NO_KEY)
118 return ret;
119 grub_cpu_idle ();
120 }
121 }
122
123 void
124 grub_refresh (void)
125 {
126 struct grub_term_output *term;
127
128 FOR_ACTIVE_TERM_OUTPUTS(term)
129 grub_term_refresh (term);
130 }