19
19
#ifndef GRUB_TERM_HEADER
20
20
#define GRUB_TERM_HEADER 1
22
#define GRUB_TERM_NO_KEY 0
22
24
/* 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
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)
32
58
#define GRUB_TERM_ESC '\e'
33
59
#define GRUB_TERM_TAB '\t'
34
#define GRUB_TERM_BACKSPACE 8
60
#define GRUB_TERM_BACKSPACE '\b'
38
64
#include <grub/err.h>
39
65
#include <grub/symbol.h>
40
66
#include <grub/types.h>
41
#include <grub/handler.h>
67
#include <grub/unicode.h>
68
#include <grub/list.h>
43
70
/* These are used to represent the various color states we use. */
65
92
/* Set when input characters shouldn't be echoed back. */
66
#define GRUB_TERM_NO_ECHO (1 << 0)
93
#define GRUB_TERM_NO_ECHO (1 << 0)
67
94
/* Set when the editing feature should be disabled. */
68
#define GRUB_TERM_NO_EDIT (1 << 1)
95
#define GRUB_TERM_NO_EDIT (1 << 1)
69
96
/* Set when the terminal cannot do fancy things. */
70
#define GRUB_TERM_DUMB (1 << 2)
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)
73
114
/* 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
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)
92
125
/* Menu-related geometrical constants. */
122
155
const char *name;
124
157
/* Initialize the terminal. */
125
grub_err_t (*init) (void);
158
grub_err_t (*init) (struct grub_term_input *term);
127
160
/* 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);
161
grub_err_t (*fini) (struct grub_term_input *term);
163
/* Get a character if any input character is available. Otherwise return -1 */
164
int (*getkey) (struct grub_term_input *term);
136
166
/* Get keyboard modifier status. */
137
int (*getkeystatus) (void);
167
int (*getkeystatus) (struct grub_term_input *term);
139
171
typedef struct grub_term_input *grub_term_input_t;
147
179
const char *name;
149
181
/* Initialize the terminal. */
150
grub_err_t (*init) (void);
182
grub_err_t (*init) (struct grub_term_output *term);
152
184
/* Clean up the terminal. */
153
grub_err_t (*fini) (void);
185
grub_err_t (*fini) (struct grub_term_output *term);
155
187
/* Put a character. C is encoded in Unicode. */
156
void (*putchar) (grub_uint32_t c);
188
void (*putchar) (struct grub_term_output *term,
189
const struct grub_unicode_glyph *c);
158
191
/* Get the number of columns occupied by a given character C. C is
159
192
encoded in Unicode. */
160
grub_ssize_t (*getcharwidth) (grub_uint32_t c);
193
grub_ssize_t (*getcharwidth) (struct grub_term_output *term,
194
const struct grub_unicode_glyph *c);
162
196
/* Get the screen size. The return value is ((Width << 8) | Height). */
163
grub_uint16_t (*getwh) (void);
197
grub_uint16_t (*getwh) (struct grub_term_output *term);
165
199
/* Get the cursor position. The return value is ((X << 8) | Y). */
166
grub_uint16_t (*getxy) (void);
200
grub_uint16_t (*getxy) (struct grub_term_output *term);
168
202
/* Go to the position (X, Y). */
169
void (*gotoxy) (grub_uint8_t x, grub_uint8_t y);
203
void (*gotoxy) (struct grub_term_output *term,
204
grub_uint8_t x, grub_uint8_t y);
171
206
/* Clear the screen. */
207
void (*cls) (struct grub_term_output *term);
174
209
/* 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
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
183
void (*getcolor) (grub_uint8_t *normal_color, grub_uint8_t *highlight_color);
210
void (*setcolorstate) (struct grub_term_output *term,
211
grub_term_color_state state);
185
213
/* Turn on/off the cursor. */
186
void (*setcursor) (int on);
214
void (*setcursor) (struct grub_term_output *term, int on);
188
216
/* Update the screen. */
189
void (*refresh) (void);
217
void (*refresh) (struct grub_term_output *term);
191
219
/* The feature flags defined above. */
192
220
grub_uint32_t flags;
222
/* Current color state. */
223
grub_uint8_t normal_color;
224
grub_uint8_t highlight_color;
194
228
typedef struct grub_term_output *grub_term_output_t;
230
#define GRUB_TERM_DEFAULT_NORMAL_COLOR 0x07
231
#define GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR 0x70
232
#define GRUB_TERM_DEFAULT_STANDARD_COLOR 0x07
196
234
extern struct grub_term_output *EXPORT_VAR(grub_term_outputs_disabled);
197
235
extern struct grub_term_input *EXPORT_VAR(grub_term_inputs_disabled);
198
236
extern struct grub_term_output *EXPORT_VAR(grub_term_outputs);
210
248
/* If this is the first terminal, enable automatically. */
211
if (! term->init || term->init () == GRUB_ERR_NONE)
249
if (! term->init || term->init (term) == GRUB_ERR_NONE)
212
250
grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term));
216
254
static inline void
255
grub_term_register_input_active (const char *name __attribute__ ((unused)),
256
grub_term_input_t term)
258
if (! term->init || term->init (term) == GRUB_ERR_NONE)
259
grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term));
217
263
grub_term_register_output (const char *name __attribute__ ((unused)),
218
264
grub_term_output_t term)
225
271
/* If this is the first terminal, enable automatically. */
226
if (! term->init || term->init () == GRUB_ERR_NONE)
272
if (! term->init || term->init (term) == GRUB_ERR_NONE)
227
273
grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs),
228
274
GRUB_AS_LIST (term));
232
278
static inline void
279
grub_term_register_output_active (const char *name __attribute__ ((unused)),
280
grub_term_output_t term)
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));
233
288
grub_term_unregister_input (grub_term_input_t term)
235
290
grub_list_remove (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term));
245
300
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)
303
#define FOR_ACTIVE_TERM_INPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_inputs))
304
#define FOR_DISABLED_TERM_INPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_inputs_disabled))
305
#define FOR_ACTIVE_TERM_OUTPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_outputs))
306
#define FOR_DISABLED_TERM_OUTPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_outputs_disabled))
253
void EXPORT_FUNC(grub_putchar) (int c);
254
void EXPORT_FUNC(grub_putcode) (grub_uint32_t code,
255
struct grub_term_output *term);
308
void grub_putcode (grub_uint32_t code, struct grub_term_output *term);
256
309
int EXPORT_FUNC(grub_getkey) (void);
257
310
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);
311
void grub_cls (void);
261
312
void EXPORT_FUNC(grub_refresh) (void);
262
313
void grub_puts_terminal (const char *str, struct grub_term_output *term);
263
314
grub_uint16_t *grub_term_save_pos (void);
314
365
static inline grub_uint16_t
315
366
grub_term_getxy (struct grub_term_output *term)
317
return term->getxy ();
368
return term->getxy (term);
320
371
static inline void
321
372
grub_term_refresh (struct grub_term_output *term)
323
374
if (term->refresh)
375
term->refresh (term);
327
378
static inline void
328
379
grub_term_gotoxy (struct grub_term_output *term, grub_uint8_t x, grub_uint8_t y)
381
term->gotoxy (term, x, y);
333
384
static inline void
335
386
grub_term_color_state state)
337
388
if (term->setcolorstate)
338
term->setcolorstate (state);
341
/* Set the normal color and the highlight color. The format of each
389
term->setcolorstate (term, state);
393
grub_setcolorstate (grub_term_color_state state)
395
struct grub_term_output *term;
397
FOR_ACTIVE_TERM_OUTPUTS(term)
398
grub_term_setcolorstate (term, state);
401
/* Set the normal color and the highlight color. The format of each
343
403
static inline void
344
404
grub_term_setcolor (struct grub_term_output *term,
345
405
grub_uint8_t normal_color, grub_uint8_t highlight_color)
348
term->setcolor (normal_color, highlight_color);
407
term->normal_color = normal_color;
408
term->highlight_color = highlight_color;
351
411
/* Turn on/off the cursor. */
431
#ifdef HAVE_UNIFONT_WIDTHSPEC
434
grub_unicode_estimate_width (const struct grub_unicode_glyph *c);
438
static inline grub_ssize_t
439
grub_unicode_estimate_width (const struct grub_unicode_glyph *c __attribute__ ((unused)))
441
if (grub_unicode_get_comb_type (c->base))
371
448
static inline grub_ssize_t
372
grub_term_getcharwidth (struct grub_term_output *term, grub_uint32_t c)
449
grub_term_getcharwidth (struct grub_term_output *term,
450
const struct grub_unicode_glyph *c)
374
452
if (term->getcharwidth)
375
return term->getcharwidth (c);
453
return term->getcharwidth (term, c);
454
else if (((term->flags & GRUB_TERM_CODE_TYPE_MASK)
455
== GRUB_TERM_CODE_TYPE_UTF8_LOGICAL)
456
|| ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
457
== GRUB_TERM_CODE_TYPE_UTF8_VISUAL)
458
|| ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
459
== GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS))
460
return grub_unicode_estimate_width (c);
381
466
grub_term_getcolor (struct grub_term_output *term,
382
467
grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
385
term->getcolor (normal_color, highlight_color);
388
*normal_color = 0x07;
389
*highlight_color = 0x07;
469
*normal_color = term->normal_color;
470
*highlight_color = term->highlight_color;
393
extern void (*EXPORT_VAR (grub_newline_hook)) (void);
395
473
struct grub_term_autoload
397
475
struct grub_term_autoload *next;