GRUB-1.98 changes
[grub2/jjazz.git] / include / grub / term.h
blob143aabe1e4a9f8c259edfdc539b86b4e26bfbd5a
1 /*
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 /* Internal codes used by GRUB to represent terminal input. */
23 #define GRUB_TERM_LEFT 2
24 #define GRUB_TERM_RIGHT 6
25 #define GRUB_TERM_UP 16
26 #define GRUB_TERM_DOWN 14
27 #define GRUB_TERM_HOME 1
28 #define GRUB_TERM_END 5
29 #define GRUB_TERM_DC 4
30 #define GRUB_TERM_PPAGE 7
31 #define GRUB_TERM_NPAGE 3
32 #define GRUB_TERM_ESC '\e'
33 #define GRUB_TERM_TAB '\t'
34 #define GRUB_TERM_BACKSPACE 8
36 #ifndef ASM_FILE
38 #include <grub/err.h>
39 #include <grub/symbol.h>
40 #include <grub/types.h>
41 #include <grub/handler.h>
43 /* These are used to represent the various color states we use. */
44 typedef enum
46 /* The color used to display all text that does not use the
47 user defined colors below. */
48 GRUB_TERM_COLOR_STANDARD,
49 /* The user defined colors for normal text. */
50 GRUB_TERM_COLOR_NORMAL,
51 /* The user defined colors for highlighted text. */
52 GRUB_TERM_COLOR_HIGHLIGHT
54 grub_term_color_state;
56 /* Flags for representing the capabilities of a terminal. */
57 /* Some notes about the flags:
58 - These flags are used by higher-level functions but not terminals
59 themselves.
60 - If a terminal is dumb, you may assume that only putchar, getkey and
61 checkkey are called.
62 - Some fancy features (setcolorstate, setcolor and setcursor) can be set
63 to NULL. */
65 /* Set when input characters shouldn't be echoed back. */
66 #define GRUB_TERM_NO_ECHO (1 << 0)
67 /* Set when the editing feature should be disabled. */
68 #define GRUB_TERM_NO_EDIT (1 << 1)
69 /* Set when the terminal cannot do fancy things. */
70 #define GRUB_TERM_DUMB (1 << 2)
73 /* Bitmasks for modifier keys returned by grub_getkeystatus. */
74 #define GRUB_TERM_STATUS_SHIFT (1 << 0)
75 #define GRUB_TERM_STATUS_CTRL (1 << 1)
76 #define GRUB_TERM_STATUS_ALT (1 << 2)
79 /* Unicode characters for fancy graphics. */
80 #define GRUB_TERM_DISP_LEFT 0x2190
81 #define GRUB_TERM_DISP_UP 0x2191
82 #define GRUB_TERM_DISP_RIGHT 0x2192
83 #define GRUB_TERM_DISP_DOWN 0x2193
84 #define GRUB_TERM_DISP_HLINE 0x2501
85 #define GRUB_TERM_DISP_VLINE 0x2503
86 #define GRUB_TERM_DISP_UL 0x250F
87 #define GRUB_TERM_DISP_UR 0x2513
88 #define GRUB_TERM_DISP_LL 0x2517
89 #define GRUB_TERM_DISP_LR 0x251B
92 /* Menu-related geometrical constants. */
94 /* The number of lines of "GRUB version..." at the top. */
95 #define GRUB_TERM_INFO_HEIGHT 1
97 /* The number of columns/lines between messages/borders/etc. */
98 #define GRUB_TERM_MARGIN 1
100 /* The number of columns of scroll information. */
101 #define GRUB_TERM_SCROLL_WIDTH 1
103 /* The Y position of the top border. */
104 #define GRUB_TERM_TOP_BORDER_Y (GRUB_TERM_MARGIN + GRUB_TERM_INFO_HEIGHT \
105 + GRUB_TERM_MARGIN)
107 /* The X position of the left border. */
108 #define GRUB_TERM_LEFT_BORDER_X GRUB_TERM_MARGIN
110 /* The number of lines of messages at the bottom. */
111 #define GRUB_TERM_MESSAGE_HEIGHT 8
113 /* The Y position of the first entry. */
114 #define GRUB_TERM_FIRST_ENTRY_Y (GRUB_TERM_TOP_BORDER_Y + 1)
116 struct grub_term_input
118 /* The next terminal. */
119 struct grub_term_input *next;
121 /* The terminal name. */
122 const char *name;
124 /* Initialize the terminal. */
125 grub_err_t (*init) (void);
127 /* Clean up the terminal. */
128 grub_err_t (*fini) (void);
130 /* Check if any input character is available. */
131 int (*checkkey) (void);
133 /* Get a character. */
134 int (*getkey) (void);
136 /* Get keyboard modifier status. */
137 int (*getkeystatus) (void);
139 typedef struct grub_term_input *grub_term_input_t;
141 struct grub_term_output
143 /* The next terminal. */
144 struct grub_term_output *next;
146 /* The terminal name. */
147 const char *name;
149 /* Initialize the terminal. */
150 grub_err_t (*init) (void);
152 /* Clean up the terminal. */
153 grub_err_t (*fini) (void);
155 /* Put a character. C is encoded in Unicode. */
156 void (*putchar) (grub_uint32_t c);
158 /* Get the number of columns occupied by a given character C. C is
159 encoded in Unicode. */
160 grub_ssize_t (*getcharwidth) (grub_uint32_t c);
162 /* Get the screen size. The return value is ((Width << 8) | Height). */
163 grub_uint16_t (*getwh) (void);
165 /* Get the cursor position. The return value is ((X << 8) | Y). */
166 grub_uint16_t (*getxy) (void);
168 /* Go to the position (X, Y). */
169 void (*gotoxy) (grub_uint8_t x, grub_uint8_t y);
171 /* Clear the screen. */
172 void (*cls) (void);
174 /* Set the current color to be used */
175 void (*setcolorstate) (grub_term_color_state state);
177 /* Set the normal color and the highlight color. The format of each
178 color is VGA's. */
179 void (*setcolor) (grub_uint8_t normal_color, grub_uint8_t highlight_color);
181 /* Get the normal color and the highlight color. The format of each
182 color is VGA's. */
183 void (*getcolor) (grub_uint8_t *normal_color, grub_uint8_t *highlight_color);
185 /* Turn on/off the cursor. */
186 void (*setcursor) (int on);
188 /* Update the screen. */
189 void (*refresh) (void);
191 /* The feature flags defined above. */
192 grub_uint32_t flags;
194 typedef struct grub_term_output *grub_term_output_t;
196 extern struct grub_term_output *EXPORT_VAR(grub_term_outputs_disabled);
197 extern struct grub_term_input *EXPORT_VAR(grub_term_inputs_disabled);
198 extern struct grub_term_output *EXPORT_VAR(grub_term_outputs);
199 extern struct grub_term_input *EXPORT_VAR(grub_term_inputs);
201 static inline void
202 grub_term_register_input (const char *name __attribute__ ((unused)),
203 grub_term_input_t term)
205 if (grub_term_inputs)
206 grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs_disabled),
207 GRUB_AS_LIST (term));
208 else
210 /* If this is the first terminal, enable automatically. */
211 if (! term->init || term->init () == GRUB_ERR_NONE)
212 grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term));
216 static inline void
217 grub_term_register_output (const char *name __attribute__ ((unused)),
218 grub_term_output_t term)
220 if (grub_term_outputs)
221 grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs_disabled),
222 GRUB_AS_LIST (term));
223 else
225 /* If this is the first terminal, enable automatically. */
226 if (! term->init || term->init () == GRUB_ERR_NONE)
227 grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs),
228 GRUB_AS_LIST (term));
232 static inline void
233 grub_term_unregister_input (grub_term_input_t term)
235 grub_list_remove (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term));
236 grub_list_remove (GRUB_AS_LIST_P (&grub_term_inputs_disabled),
237 GRUB_AS_LIST (term));
240 static inline void
241 grub_term_unregister_output (grub_term_output_t term)
243 grub_list_remove (GRUB_AS_LIST_P (&grub_term_outputs), GRUB_AS_LIST (term));
244 grub_list_remove (GRUB_AS_LIST_P (&(grub_term_outputs_disabled)),
245 GRUB_AS_LIST (term));
248 #define FOR_ACTIVE_TERM_INPUTS(var) for (var = grub_term_inputs; var; var = var->next)
249 #define FOR_DISABLED_TERM_INPUTS(var) for (var = grub_term_inputs_disabled; var; var = var->next)
250 #define FOR_ACTIVE_TERM_OUTPUTS(var) for (var = grub_term_outputs; var; var = var->next)
251 #define FOR_DISABLED_TERM_OUTPUTS(var) for (var = grub_term_outputs_disabled; var; var = var->next)
253 void EXPORT_FUNC(grub_putchar) (int c);
254 void EXPORT_FUNC(grub_putcode) (grub_uint32_t code,
255 struct grub_term_output *term);
256 int EXPORT_FUNC(grub_getkey) (void);
257 int EXPORT_FUNC(grub_checkkey) (void);
258 int EXPORT_FUNC(grub_getkeystatus) (void);
259 void EXPORT_FUNC(grub_cls) (void);
260 void EXPORT_FUNC(grub_setcolorstate) (grub_term_color_state state);
261 void EXPORT_FUNC(grub_refresh) (void);
262 void grub_puts_terminal (const char *str, struct grub_term_output *term);
263 grub_uint16_t *grub_term_save_pos (void);
264 void grub_term_restore_pos (grub_uint16_t *pos);
266 static inline unsigned grub_term_width (struct grub_term_output *term)
268 return ((term->getwh()&0xFF00)>>8);
271 static inline unsigned grub_term_height (struct grub_term_output *term)
273 return (term->getwh()&0xFF);
276 /* The width of the border. */
277 static inline unsigned
278 grub_term_border_width (struct grub_term_output *term)
280 return grub_term_width (term) - GRUB_TERM_MARGIN * 3 - GRUB_TERM_SCROLL_WIDTH;
283 /* The max column number of an entry. The last "-1" is for a
284 continuation marker. */
285 static inline int
286 grub_term_entry_width (struct grub_term_output *term)
288 return grub_term_border_width (term) - 2 - GRUB_TERM_MARGIN * 2 - 1;
291 /* The height of the border. */
293 static inline unsigned
294 grub_term_border_height (struct grub_term_output *term)
296 return grub_term_height (term) - GRUB_TERM_TOP_BORDER_Y
297 - GRUB_TERM_MESSAGE_HEIGHT;
300 /* The number of entries shown at a time. */
301 static inline int
302 grub_term_num_entries (struct grub_term_output *term)
304 return grub_term_border_height (term) - 2;
307 static inline int
308 grub_term_cursor_x (struct grub_term_output *term)
310 return (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
311 - GRUB_TERM_MARGIN - 1);
314 static inline grub_uint16_t
315 grub_term_getxy (struct grub_term_output *term)
317 return term->getxy ();
320 static inline void
321 grub_term_refresh (struct grub_term_output *term)
323 if (term->refresh)
324 term->refresh ();
327 static inline void
328 grub_term_gotoxy (struct grub_term_output *term, grub_uint8_t x, grub_uint8_t y)
330 term->gotoxy (x, y);
333 static inline void
334 grub_term_setcolorstate (struct grub_term_output *term,
335 grub_term_color_state state)
337 if (term->setcolorstate)
338 term->setcolorstate (state);
341 /* Set the normal color and the highlight color. The format of each
342 color is VGA's. */
343 static inline void
344 grub_term_setcolor (struct grub_term_output *term,
345 grub_uint8_t normal_color, grub_uint8_t highlight_color)
347 if (term->setcolor)
348 term->setcolor (normal_color, highlight_color);
351 /* Turn on/off the cursor. */
352 static inline void
353 grub_term_setcursor (struct grub_term_output *term, int on)
355 if (term->setcursor)
356 term->setcursor (on);
359 static inline void
360 grub_term_cls (struct grub_term_output *term)
362 if (term->cls)
363 (term->cls) ();
364 else
366 grub_putcode ('\n', term);
367 grub_term_refresh (term);
371 static inline grub_ssize_t
372 grub_term_getcharwidth (struct grub_term_output *term, grub_uint32_t c)
374 if (term->getcharwidth)
375 return term->getcharwidth (c);
376 else
377 return 1;
380 static inline void
381 grub_term_getcolor (struct grub_term_output *term,
382 grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
384 if (term->getcolor)
385 term->getcolor (normal_color, highlight_color);
386 else
388 *normal_color = 0x07;
389 *highlight_color = 0x07;
393 extern void (*EXPORT_VAR (grub_newline_hook)) (void);
395 struct grub_term_autoload
397 struct grub_term_autoload *next;
398 char *name;
399 char *modname;
402 extern struct grub_term_autoload *grub_term_input_autoload;
403 extern struct grub_term_autoload *grub_term_output_autoload;
405 static inline void
406 grub_print_spaces (struct grub_term_output *term, int number_spaces)
408 while (--number_spaces >= 0)
409 grub_putcode (' ', term);
413 /* For convenience. */
414 #define GRUB_TERM_ASCII_CHAR(c) ((c) & 0xff)
416 #endif /* ! ASM_FILE */
418 #endif /* ! GRUB_TERM_HEADER */