]> git.proxmox.com Git - grub2.git/blob - grub-core/osdep/unix/emuconsole.c
Lift 255x255 erminal sie restriction to 65535x65535. Also change from
[grub2.git] / grub-core / osdep / unix / emuconsole.c
1 /* console.c -- console for GRUB. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2013 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <config-util.h>
22
23 #include <grub/term.h>
24 #include <grub/types.h>
25 #include <grub/misc.h>
26 #include <grub/mm.h>
27 #include <grub/time.h>
28 #include <grub/terminfo.h>
29 #include <grub/dl.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <termios.h>
36 #include <stdlib.h>
37 #include <sys/ioctl.h>
38 #include <langinfo.h>
39
40 #include <grub/emu/console.h>
41
42 extern struct grub_terminfo_output_state grub_console_terminfo_output;
43 static int original_fl;
44 static int saved_orig;
45 static struct termios orig_tty;
46 static struct termios new_tty;
47
48 static void
49 put (struct grub_term_output *term __attribute__ ((unused)), const int c)
50 {
51 char chr = c;
52
53 write (STDOUT_FILENO, &chr, 1);
54 }
55
56 static int
57 readkey (struct grub_term_input *term __attribute__ ((unused)))
58 {
59 grub_uint8_t c;
60 ssize_t actual;
61
62 actual = read (STDIN_FILENO, &c, 1);
63 if (actual > 0)
64 return c;
65 return -1;
66 }
67
68 static grub_err_t
69 grub_console_init_input (struct grub_term_input *term)
70 {
71 if (!saved_orig)
72 {
73 original_fl = fcntl (STDIN_FILENO, F_GETFL);
74 fcntl (STDIN_FILENO, F_SETFL, original_fl | O_NONBLOCK);
75 }
76
77 saved_orig = 1;
78
79 tcgetattr(STDIN_FILENO, &orig_tty);
80 new_tty = orig_tty;
81 new_tty.c_lflag &= ~(ICANON | ECHO);
82 new_tty.c_cc[VMIN] = 1;
83 tcsetattr(STDIN_FILENO, TCSANOW, &new_tty);
84
85 return grub_terminfo_input_init (term);
86 }
87
88 static grub_err_t
89 grub_console_fini_input (struct grub_term_input *term
90 __attribute__ ((unused)))
91 {
92 fcntl (STDIN_FILENO, F_SETFL, original_fl);
93 tcsetattr(STDIN_FILENO, TCSANOW, &orig_tty);
94 saved_orig = 0;
95 return GRUB_ERR_NONE;
96 }
97
98 static grub_err_t
99 grub_console_init_output (struct grub_term_output *term)
100 {
101 struct winsize size;
102 if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &size) >= 0)
103 {
104 grub_console_terminfo_output.size.x = size.ws_col;
105 grub_console_terminfo_output.size.y = size.ws_row;
106 }
107 else
108 {
109 grub_console_terminfo_output.size.x = 80;
110 grub_console_terminfo_output.size.y = 24;
111 }
112
113 grub_terminfo_output_init (term);
114
115 return 0;
116 }
117
118 \f
119
120 struct grub_terminfo_input_state grub_console_terminfo_input =
121 {
122 .readkey = readkey
123 };
124
125 struct grub_terminfo_output_state grub_console_terminfo_output =
126 {
127 .put = put,
128 .size = { 80, 24 }
129 };
130
131 static struct grub_term_input grub_console_term_input =
132 {
133 .name = "console",
134 .init = grub_console_init_input,
135 .fini = grub_console_fini_input,
136 .getkey = grub_terminfo_getkey,
137 .data = &grub_console_terminfo_input
138 };
139
140 static struct grub_term_output grub_console_term_output =
141 {
142 .name = "console",
143 .init = grub_console_init_output,
144 .putchar = grub_terminfo_putchar,
145 .getxy = grub_terminfo_getxy,
146 .getwh = grub_terminfo_getwh,
147 .gotoxy = grub_terminfo_gotoxy,
148 .cls = grub_terminfo_cls,
149 .setcolorstate = grub_terminfo_setcolorstate,
150 .setcursor = grub_terminfo_setcursor,
151 .data = &grub_console_terminfo_output,
152 };
153
154 void
155 grub_console_init (void)
156 {
157 const char *cs = nl_langinfo (CODESET);
158 if (cs && grub_strcasecmp (cs, "UTF-8"))
159 grub_console_term_output.flags = GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
160 else
161 grub_console_term_output.flags = GRUB_TERM_CODE_TYPE_ASCII;
162 grub_term_register_input ("console", &grub_console_term_input);
163 grub_term_register_output ("console", &grub_console_term_output);
164 grub_terminfo_init ();
165 grub_terminfo_output_register (&grub_console_term_output, "vt100-color");
166 }
167
168 void
169 grub_console_fini (void)
170 {
171 if (saved_orig)
172 {
173 fcntl (STDIN_FILENO, F_SETFL, original_fl);
174 tcsetattr(STDIN_FILENO, TCSANOW, &orig_tty);
175 }
176 saved_orig = 0;
177 }