1 /* console.c -- console for GRUB. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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/>.
21 #include <config-util.h>
23 #include <grub/term.h>
24 #include <grub/types.h>
25 #include <grub/misc.h>
27 #include <grub/time.h>
28 #include <grub/terminfo.h>
31 #include <sys/types.h>
37 #include <sys/ioctl.h>
40 #include <grub/emu/console.h>
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
;
49 put (struct grub_term_output
*term
__attribute__ ((unused
)), const int c
)
53 write (STDOUT_FILENO
, &chr
, 1);
57 readkey (struct grub_term_input
*term
__attribute__ ((unused
)))
62 actual
= read (STDIN_FILENO
, &c
, 1);
69 grub_console_init_input (struct grub_term_input
*term
)
73 original_fl
= fcntl (STDIN_FILENO
, F_GETFL
);
74 fcntl (STDIN_FILENO
, F_SETFL
, original_fl
| O_NONBLOCK
);
79 tcgetattr(STDIN_FILENO
, &orig_tty
);
81 new_tty
.c_lflag
&= ~(ICANON
| ECHO
);
82 new_tty
.c_cc
[VMIN
] = 1;
83 tcsetattr(STDIN_FILENO
, TCSANOW
, &new_tty
);
85 return grub_terminfo_input_init (term
);
89 grub_console_fini_input (struct grub_term_input
*term
90 __attribute__ ((unused
)))
92 fcntl (STDIN_FILENO
, F_SETFL
, original_fl
);
93 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_tty
);
99 grub_console_init_output (struct grub_term_output
*term
)
102 if (ioctl (STDOUT_FILENO
, TIOCGWINSZ
, &size
) >= 0)
104 grub_console_terminfo_output
.size
.x
= size
.ws_col
;
105 grub_console_terminfo_output
.size
.y
= size
.ws_row
;
109 grub_console_terminfo_output
.size
.x
= 80;
110 grub_console_terminfo_output
.size
.y
= 24;
113 grub_terminfo_output_init (term
);
120 struct grub_terminfo_input_state grub_console_terminfo_input
=
125 struct grub_terminfo_output_state grub_console_terminfo_output
=
131 static struct grub_term_input grub_console_term_input
=
134 .init
= grub_console_init_input
,
135 .fini
= grub_console_fini_input
,
136 .getkey
= grub_terminfo_getkey
,
137 .data
= &grub_console_terminfo_input
140 static struct grub_term_output grub_console_term_output
=
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
,
155 grub_console_init (void)
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
;
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");
169 grub_console_fini (void)
173 fcntl (STDIN_FILENO
, F_SETFL
, original_fl
);
174 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_tty
);