2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
19 #ifndef GRUB_TERM_HEADER
20 #define GRUB_TERM_HEADER 1
22 #define GRUB_TERM_NO_KEY 0
24 /* Internal codes used by GRUB to represent terminal input. */
25 /* Only for keys otherwise not having shifted modification. */
26 #define GRUB_TERM_SHIFT 0x01000000
27 #define GRUB_TERM_CTRL 0x02000000
28 #define GRUB_TERM_ALT 0x04000000
30 /* Keys without associated character. */
31 #define GRUB_TERM_EXTENDED 0x00800000
32 #define GRUB_TERM_KEY_MASK 0x00ffffff
34 #define GRUB_TERM_KEY_LEFT (GRUB_TERM_EXTENDED | 0x4b)
35 #define GRUB_TERM_KEY_RIGHT (GRUB_TERM_EXTENDED | 0x4d)
36 #define GRUB_TERM_KEY_UP (GRUB_TERM_EXTENDED | 0x48)
37 #define GRUB_TERM_KEY_DOWN (GRUB_TERM_EXTENDED | 0x50)
38 #define GRUB_TERM_KEY_HOME (GRUB_TERM_EXTENDED | 0x47)
39 #define GRUB_TERM_KEY_END (GRUB_TERM_EXTENDED | 0x4f)
40 #define GRUB_TERM_KEY_DC (GRUB_TERM_EXTENDED | 0x53)
41 #define GRUB_TERM_KEY_PPAGE (GRUB_TERM_EXTENDED | 0x49)
42 #define GRUB_TERM_KEY_NPAGE (GRUB_TERM_EXTENDED | 0x51)
43 #define GRUB_TERM_KEY_F1 (GRUB_TERM_EXTENDED | 0x3b)
44 #define GRUB_TERM_KEY_F2 (GRUB_TERM_EXTENDED | 0x3c)
45 #define GRUB_TERM_KEY_F3 (GRUB_TERM_EXTENDED | 0x3d)
46 #define GRUB_TERM_KEY_F4 (GRUB_TERM_EXTENDED | 0x3e)
47 #define GRUB_TERM_KEY_F5 (GRUB_TERM_EXTENDED | 0x3f)
48 #define GRUB_TERM_KEY_F6 (GRUB_TERM_EXTENDED | 0x40)
49 #define GRUB_TERM_KEY_F7 (GRUB_TERM_EXTENDED | 0x41)
50 #define GRUB_TERM_KEY_F8 (GRUB_TERM_EXTENDED | 0x42)
51 #define GRUB_TERM_KEY_F9 (GRUB_TERM_EXTENDED | 0x43)
52 #define GRUB_TERM_KEY_F10 (GRUB_TERM_EXTENDED | 0x44)
53 #define GRUB_TERM_KEY_F11 (GRUB_TERM_EXTENDED | 0x57)
54 #define GRUB_TERM_KEY_F12 (GRUB_TERM_EXTENDED | 0x58)
55 #define GRUB_TERM_KEY_INSERT (GRUB_TERM_EXTENDED | 0x52)
56 #define GRUB_TERM_KEY_CENTER (GRUB_TERM_EXTENDED | 0x4c)
58 #define GRUB_TERM_ESC '\e'
59 #define GRUB_TERM_TAB '\t'
60 #define GRUB_TERM_BACKSPACE '\b'
65 #include <grub/symbol.h>
66 #include <grub/types.h>
67 #include <grub/unicode.h>
68 #include <grub/list.h>
70 /* These are used to represent the various color states we use. */
73 /* The color used to display all text that does not use the
74 user defined colors below. */
75 GRUB_TERM_COLOR_STANDARD
,
76 /* The user defined colors for normal text. */
77 GRUB_TERM_COLOR_NORMAL
,
78 /* The user defined colors for highlighted text. */
79 GRUB_TERM_COLOR_HIGHLIGHT
81 grub_term_color_state
;
83 /* Flags for representing the capabilities of a terminal. */
84 /* Some notes about the flags:
85 - These flags are used by higher-level functions but not terminals
87 - If a terminal is dumb, you may assume that only putchar, getkey and
89 - Some fancy features (setcolorstate, setcolor and setcursor) can be set
92 /* Set when input characters shouldn't be echoed back. */
93 #define GRUB_TERM_NO_ECHO (1 << 0)
94 /* Set when the editing feature should be disabled. */
95 #define GRUB_TERM_NO_EDIT (1 << 1)
96 /* Set when the terminal cannot do fancy things. */
97 #define GRUB_TERM_DUMB (1 << 2)
98 /* Which encoding does terminal expect stream to be. */
99 #define GRUB_TERM_CODE_TYPE_SHIFT 3
100 #define GRUB_TERM_CODE_TYPE_MASK (7 << GRUB_TERM_CODE_TYPE_SHIFT)
101 /* Only ASCII characters accepted. */
102 #define GRUB_TERM_CODE_TYPE_ASCII (0 << GRUB_TERM_CODE_TYPE_SHIFT)
103 /* Expects CP-437 characters (ASCII + pseudographics). */
104 #define GRUB_TERM_CODE_TYPE_CP437 (1 << GRUB_TERM_CODE_TYPE_SHIFT)
105 /* UTF-8 stream in logical order. Usually used for terminals
106 which just forward the stream to another computer. */
107 #define GRUB_TERM_CODE_TYPE_UTF8_LOGICAL (2 << GRUB_TERM_CODE_TYPE_SHIFT)
108 /* UTF-8 in visual order. Like UTF-8 logical but for buggy endpoints. */
109 #define GRUB_TERM_CODE_TYPE_UTF8_VISUAL (3 << GRUB_TERM_CODE_TYPE_SHIFT)
110 /* Glyph description in visual order. */
111 #define GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS (4 << GRUB_TERM_CODE_TYPE_SHIFT)
114 /* Bitmasks for modifier keys returned by grub_getkeystatus. */
115 #define GRUB_TERM_STATUS_RSHIFT (1 << 0)
116 #define GRUB_TERM_STATUS_LSHIFT (1 << 1)
117 #define GRUB_TERM_STATUS_RCTRL (1 << 2)
118 #define GRUB_TERM_STATUS_RALT (1 << 3)
119 #define GRUB_TERM_STATUS_SCROLL (1 << 4)
120 #define GRUB_TERM_STATUS_NUM (1 << 5)
121 #define GRUB_TERM_STATUS_CAPS (1 << 6)
122 #define GRUB_TERM_STATUS_LCTRL (1 << 8)
123 #define GRUB_TERM_STATUS_LALT (1 << 9)
125 /* Menu-related geometrical constants. */
127 /* The number of lines of "GRUB version..." at the top. */
128 #define GRUB_TERM_INFO_HEIGHT 1
130 /* The number of columns/lines between messages/borders/etc. */
131 #define GRUB_TERM_MARGIN 1
133 /* The number of columns of scroll information. */
134 #define GRUB_TERM_SCROLL_WIDTH 1
136 /* The Y position of the top border. */
137 #define GRUB_TERM_TOP_BORDER_Y (GRUB_TERM_MARGIN + GRUB_TERM_INFO_HEIGHT \
140 /* The X position of the left border. */
141 #define GRUB_TERM_LEFT_BORDER_X GRUB_TERM_MARGIN
143 /* The Y position of the first entry. */
144 #define GRUB_TERM_FIRST_ENTRY_Y (GRUB_TERM_TOP_BORDER_Y + 1)
146 struct grub_term_input
148 /* The next terminal. */
149 struct grub_term_input
*next
;
150 struct grub_term_input
**prev
;
152 /* The terminal name. */
155 /* Initialize the terminal. */
156 grub_err_t (*init
) (struct grub_term_input
*term
);
158 /* Clean up the terminal. */
159 grub_err_t (*fini
) (struct grub_term_input
*term
);
161 /* Get a character if any input character is available. Otherwise return -1 */
162 int (*getkey
) (struct grub_term_input
*term
);
164 /* Get keyboard modifier status. */
165 int (*getkeystatus
) (struct grub_term_input
*term
);
169 typedef struct grub_term_input
*grub_term_input_t
;
171 struct grub_term_output
173 /* The next terminal. */
174 struct grub_term_output
*next
;
175 struct grub_term_output
**prev
;
177 /* The terminal name. */
180 /* Initialize the terminal. */
181 grub_err_t (*init
) (struct grub_term_output
*term
);
183 /* Clean up the terminal. */
184 grub_err_t (*fini
) (struct grub_term_output
*term
);
186 /* Put a character. C is encoded in Unicode. */
187 void (*putchar
) (struct grub_term_output
*term
,
188 const struct grub_unicode_glyph
*c
);
190 /* Get the number of columns occupied by a given character C. C is
191 encoded in Unicode. */
192 grub_ssize_t (*getcharwidth
) (struct grub_term_output
*term
,
193 const struct grub_unicode_glyph
*c
);
195 /* Get the screen size. The return value is ((Width << 8) | Height). */
196 grub_uint16_t (*getwh
) (struct grub_term_output
*term
);
198 /* Get the cursor position. The return value is ((X << 8) | Y). */
199 grub_uint16_t (*getxy
) (struct grub_term_output
*term
);
201 /* Go to the position (X, Y). */
202 void (*gotoxy
) (struct grub_term_output
*term
,
203 grub_uint8_t x
, grub_uint8_t y
);
205 /* Clear the screen. */
206 void (*cls
) (struct grub_term_output
*term
);
208 /* Set the current color to be used */
209 void (*setcolorstate
) (struct grub_term_output
*term
,
210 grub_term_color_state state
);
212 /* Turn on/off the cursor. */
213 void (*setcursor
) (struct grub_term_output
*term
, int on
);
215 /* Update the screen. */
216 void (*refresh
) (struct grub_term_output
*term
);
218 /* gfxterm only: put in fullscreen mode. */
219 grub_err_t (*fullscreen
) (void);
221 /* The feature flags defined above. */
224 /* Current color state. */
225 grub_uint8_t normal_color
;
226 grub_uint8_t highlight_color
;
230 typedef struct grub_term_output
*grub_term_output_t
;
232 #define GRUB_TERM_DEFAULT_NORMAL_COLOR 0x07
233 #define GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR 0x70
234 #define GRUB_TERM_DEFAULT_STANDARD_COLOR 0x07
236 extern struct grub_term_output
*EXPORT_VAR(grub_term_outputs_disabled
);
237 extern struct grub_term_input
*EXPORT_VAR(grub_term_inputs_disabled
);
238 extern struct grub_term_output
*EXPORT_VAR(grub_term_outputs
);
239 extern struct grub_term_input
*EXPORT_VAR(grub_term_inputs
);
242 grub_term_register_input (const char *name
__attribute__ ((unused
)),
243 grub_term_input_t term
)
245 if (grub_term_inputs
)
246 grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs_disabled
),
247 GRUB_AS_LIST (term
));
250 /* If this is the first terminal, enable automatically. */
251 if (! term
->init
|| term
->init (term
) == GRUB_ERR_NONE
)
252 grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs
), GRUB_AS_LIST (term
));
257 grub_term_register_input_inactive (const char *name
__attribute__ ((unused
)),
258 grub_term_input_t term
)
260 grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs_disabled
),
261 GRUB_AS_LIST (term
));
265 grub_term_register_input_active (const char *name
__attribute__ ((unused
)),
266 grub_term_input_t term
)
268 if (! term
->init
|| term
->init (term
) == GRUB_ERR_NONE
)
269 grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs
), GRUB_AS_LIST (term
));
273 grub_term_register_output (const char *name
__attribute__ ((unused
)),
274 grub_term_output_t term
)
276 if (grub_term_outputs
)
277 grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs_disabled
),
278 GRUB_AS_LIST (term
));
281 /* If this is the first terminal, enable automatically. */
282 if (! term
->init
|| term
->init (term
) == GRUB_ERR_NONE
)
283 grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs
),
284 GRUB_AS_LIST (term
));
289 grub_term_register_output_inactive (const char *name
__attribute__ ((unused
)),
290 grub_term_output_t term
)
292 grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs_disabled
),
293 GRUB_AS_LIST (term
));
297 grub_term_register_output_active (const char *name
__attribute__ ((unused
)),
298 grub_term_output_t term
)
300 if (! term
->init
|| term
->init (term
) == GRUB_ERR_NONE
)
301 grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs
),
302 GRUB_AS_LIST (term
));
306 grub_term_unregister_input (grub_term_input_t term
)
308 grub_list_remove (GRUB_AS_LIST (term
));
309 grub_list_remove (GRUB_AS_LIST (term
));
313 grub_term_unregister_output (grub_term_output_t term
)
315 grub_list_remove (GRUB_AS_LIST (term
));
316 grub_list_remove (GRUB_AS_LIST (term
));
319 #define FOR_ACTIVE_TERM_INPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_inputs))
320 #define FOR_DISABLED_TERM_INPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_inputs_disabled))
321 #define FOR_ACTIVE_TERM_OUTPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_outputs))
322 #define FOR_DISABLED_TERM_OUTPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_outputs_disabled))
324 void grub_putcode (grub_uint32_t code
, struct grub_term_output
*term
);
325 int EXPORT_FUNC(grub_getkey
) (void);
326 int EXPORT_FUNC(grub_getkey_noblock
) (void);
327 void grub_cls (void);
328 void EXPORT_FUNC(grub_refresh
) (void);
329 void grub_puts_terminal (const char *str
, struct grub_term_output
*term
);
330 grub_uint16_t
*grub_term_save_pos (void);
331 void grub_term_restore_pos (grub_uint16_t
*pos
);
333 static inline unsigned grub_term_width (struct grub_term_output
*term
)
335 return ((term
->getwh(term
)&0xFF00)>>8);
338 static inline unsigned grub_term_height (struct grub_term_output
*term
)
340 return (term
->getwh(term
)&0xFF);
343 /* The width of the border. */
344 static inline unsigned
345 grub_term_border_width (struct grub_term_output
*term
)
347 return grub_term_width (term
) - GRUB_TERM_MARGIN
* 3 - GRUB_TERM_SCROLL_WIDTH
;
350 /* The max column number of an entry. The last "-1" is for a
351 continuation marker. */
353 grub_term_entry_width (struct grub_term_output
*term
)
355 return grub_term_border_width (term
) - 2 - GRUB_TERM_MARGIN
* 2 - 1;
358 static inline grub_uint16_t
359 grub_term_getxy (struct grub_term_output
*term
)
361 return term
->getxy (term
);
365 grub_term_refresh (struct grub_term_output
*term
)
368 term
->refresh (term
);
372 grub_term_gotoxy (struct grub_term_output
*term
, grub_uint8_t x
, grub_uint8_t y
)
374 term
->gotoxy (term
, x
, y
);
378 grub_term_setcolorstate (struct grub_term_output
*term
,
379 grub_term_color_state state
)
381 if (term
->setcolorstate
)
382 term
->setcolorstate (term
, state
);
386 grub_setcolorstate (grub_term_color_state state
)
388 struct grub_term_output
*term
;
390 FOR_ACTIVE_TERM_OUTPUTS(term
)
391 grub_term_setcolorstate (term
, state
);
394 /* Set the normal color and the highlight color. The format of each
397 grub_term_setcolor (struct grub_term_output
*term
,
398 grub_uint8_t normal_color
, grub_uint8_t highlight_color
)
400 term
->normal_color
= normal_color
;
401 term
->highlight_color
= highlight_color
;
404 /* Turn on/off the cursor. */
406 grub_term_setcursor (struct grub_term_output
*term
, int on
)
409 term
->setcursor (term
, on
);
413 grub_term_cls (struct grub_term_output
*term
)
419 grub_putcode ('\n', term
);
420 grub_term_refresh (term
);
424 #ifdef HAVE_UNIFONT_WIDTHSPEC
427 grub_unicode_estimate_width (const struct grub_unicode_glyph
*c
);
431 static inline grub_ssize_t
432 grub_unicode_estimate_width (const struct grub_unicode_glyph
*c
__attribute__ ((unused
)))
434 if (grub_unicode_get_comb_type (c
->base
))
441 #define GRUB_TERM_TAB_WIDTH 8
443 static inline grub_ssize_t
444 grub_term_getcharwidth (struct grub_term_output
*term
,
445 const struct grub_unicode_glyph
*c
)
448 return GRUB_TERM_TAB_WIDTH
;
450 if (term
->getcharwidth
)
451 return term
->getcharwidth (term
, c
);
452 else if (((term
->flags
& GRUB_TERM_CODE_TYPE_MASK
)
453 == GRUB_TERM_CODE_TYPE_UTF8_LOGICAL
)
454 || ((term
->flags
& GRUB_TERM_CODE_TYPE_MASK
)
455 == GRUB_TERM_CODE_TYPE_UTF8_VISUAL
)
456 || ((term
->flags
& GRUB_TERM_CODE_TYPE_MASK
)
457 == GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS
))
458 return grub_unicode_estimate_width (c
);
464 grub_term_getcolor (struct grub_term_output
*term
,
465 grub_uint8_t
*normal_color
, grub_uint8_t
*highlight_color
)
467 *normal_color
= term
->normal_color
;
468 *highlight_color
= term
->highlight_color
;
471 struct grub_term_autoload
473 struct grub_term_autoload
*next
;
478 extern struct grub_term_autoload
*grub_term_input_autoload
;
479 extern struct grub_term_autoload
*grub_term_output_autoload
;
482 grub_print_spaces (struct grub_term_output
*term
, int number_spaces
)
484 while (--number_spaces
>= 0)
485 grub_putcode (' ', term
);
488 extern void (*EXPORT_VAR (grub_term_poll_usb
)) (void);
490 #define GRUB_TERM_REPEAT_PRE_INTERVAL 400
491 #define GRUB_TERM_REPEAT_INTERVAL 50
493 #endif /* ! ASM_FILE */
495 #endif /* ! GRUB_TERM_HEADER */