~ubuntu-branches/debian/sid/grub2/sid-200907171837

« back to all changes in this revision

Viewing changes to term/efi/console.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-06-10 19:57:01 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060610195701-2khcfacexb229tq4
Tags: 1.94-3
Fix FTBFS in amd64. Closes: 372548

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2006  Free Software Foundation, Inc.
 
4
 *
 
5
 *  This program 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 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program 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.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 */
 
19
 
 
20
#include <grub/term.h>
 
21
#include <grub/misc.h>
 
22
#include <grub/types.h>
 
23
#include <grub/err.h>
 
24
#include <grub/efi/efi.h>
 
25
#include <grub/efi/api.h>
 
26
#include <grub/efi/console.h>
 
27
 
 
28
static grub_uint8_t
 
29
grub_console_standard_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_YELLOW,
 
30
                                                  GRUB_EFI_BACKGROUND_BLACK);
 
31
static grub_uint8_t
 
32
grub_console_normal_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_LIGHTGRAY,
 
33
                                                GRUB_EFI_BACKGROUND_BLACK);
 
34
static grub_uint8_t
 
35
grub_console_highlight_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_BLACK,
 
36
                                                   GRUB_EFI_BACKGROUND_LIGHTGRAY);
 
37
 
 
38
static int read_key = -1;
 
39
 
 
40
static void
 
41
grub_console_putchar (grub_uint32_t c)
 
42
{
 
43
  grub_efi_char16_t str[2];
 
44
  grub_efi_simple_text_output_interface_t *o;
 
45
  
 
46
  o = grub_efi_system_table->con_out;
 
47
  
 
48
  /* For now, do not try to use a surrogate pair.  */
 
49
  if (c > 0xffff)
 
50
    c = '?';
 
51
 
 
52
  str[0] = (grub_efi_char16_t)  (c & 0xffff);
 
53
  str[1] = 0;
 
54
 
 
55
  /* Should this test be cached?  */
 
56
  if (c > 0x7f && o->test_string (o, str) != GRUB_EFI_SUCCESS)
 
57
    return;
 
58
  
 
59
  o->output_string (o, str);
 
60
}
 
61
 
 
62
static grub_ssize_t
 
63
grub_console_getcharwidth (grub_uint32_t c __attribute__ ((unused)))
 
64
{
 
65
  /* For now, every printable character has the width 1.  */
 
66
  return 1;
 
67
}
 
68
 
 
69
static int
 
70
grub_console_checkkey (void)
 
71
{
 
72
  grub_efi_simple_input_interface_t *i;
 
73
  grub_efi_input_key_t key;
 
74
  grub_efi_status_t status;
 
75
  
 
76
  if (read_key >= 0)
 
77
    return 1;
 
78
 
 
79
  i = grub_efi_system_table->con_in;
 
80
  status = i->read_key_stroke (i, &key);
 
81
#if 1
 
82
  switch (status)
 
83
    {
 
84
    case GRUB_EFI_SUCCESS:
 
85
      {
 
86
        grub_uint16_t xy;
 
87
        
 
88
        xy = grub_getxy ();
 
89
        grub_gotoxy (0, 0);
 
90
        grub_printf ("scan_code=%x,unicode_char=%x  ",
 
91
                     (unsigned) key.scan_code,
 
92
                     (unsigned) key.unicode_char);
 
93
        grub_gotoxy (xy >> 8, xy & 0xff);
 
94
      }
 
95
      break;
 
96
 
 
97
    case GRUB_EFI_NOT_READY:
 
98
      //grub_printf ("not ready   ");
 
99
      break;
 
100
 
 
101
    default:
 
102
      //grub_printf ("device error   ");
 
103
      break;
 
104
    }
 
105
#endif
 
106
  
 
107
  if (status == GRUB_EFI_SUCCESS)
 
108
    {
 
109
      switch (key.scan_code)
 
110
        {
 
111
        case 0x00:
 
112
          read_key = key.unicode_char;
 
113
          break;
 
114
        case 0x01:
 
115
          read_key = 16;
 
116
          break;
 
117
        case 0x02:
 
118
          read_key = 14;
 
119
          break;
 
120
        case 0x03:
 
121
          read_key = 6;
 
122
          break;
 
123
        case 0x04:
 
124
          read_key = 2;
 
125
          break;
 
126
        case 0x05:
 
127
          read_key = 1;
 
128
          break;
 
129
        case 0x06:
 
130
          read_key = 5;
 
131
          break;
 
132
        case 0x07:
 
133
          break;
 
134
        case 0x08:
 
135
          read_key = 4;
 
136
          break;
 
137
        case 0x09:
 
138
          break;
 
139
        case 0x0a:
 
140
          break;
 
141
        case 0x17:
 
142
          read_key = '\e';
 
143
          break;
 
144
        default:
 
145
          break;
 
146
        }
 
147
    }
 
148
 
 
149
  return read_key;
 
150
}
 
151
 
 
152
static int
 
153
grub_console_getkey (void)
 
154
{
 
155
  grub_efi_simple_input_interface_t *i;
 
156
  grub_efi_boot_services_t *b;
 
157
  grub_efi_uintn_t index;
 
158
  grub_efi_status_t status;
 
159
  int key;
 
160
 
 
161
  if (read_key >= 0)
 
162
    {
 
163
      key = read_key;
 
164
      read_key = -1;
 
165
      return key;
 
166
    }
 
167
 
 
168
  i = grub_efi_system_table->con_in;
 
169
  b = grub_efi_system_table->boot_services;
 
170
 
 
171
  do
 
172
    {
 
173
      status = b->wait_for_event (1, &(i->wait_for_key), &index);
 
174
      if (status != GRUB_EFI_SUCCESS)
 
175
        return -1;
 
176
      
 
177
      grub_console_checkkey ();
 
178
    }
 
179
  while (read_key < 0);
 
180
  
 
181
  key = read_key;
 
182
  read_key = -1;
 
183
  return key;
 
184
}
 
185
 
 
186
static grub_uint16_t
 
187
grub_console_getwh (void)
 
188
{
 
189
  grub_efi_simple_text_output_interface_t *o;
 
190
  grub_efi_uintn_t columns, rows;
 
191
  
 
192
  o = grub_efi_system_table->con_out;
 
193
  if (o->query_mode (o, o->mode->mode, &columns, &rows) != GRUB_EFI_SUCCESS)
 
194
    {
 
195
      /* Why does this fail?  */
 
196
      columns = 80;
 
197
      rows = 25;
 
198
    }
 
199
 
 
200
  return ((columns << 8) | rows);
 
201
}
 
202
 
 
203
static grub_uint16_t
 
204
grub_console_getxy (void)
 
205
{
 
206
  grub_efi_simple_text_output_interface_t *o;
 
207
  
 
208
  o = grub_efi_system_table->con_out;
 
209
  return ((o->mode->cursor_column << 8) | o->mode->cursor_row);
 
210
}
 
211
 
 
212
static void
 
213
grub_console_gotoxy (grub_uint8_t x, grub_uint8_t y)
 
214
{
 
215
  grub_efi_simple_text_output_interface_t *o;
 
216
  
 
217
  o = grub_efi_system_table->con_out;
 
218
  o->set_cursor_position (o, x, y);
 
219
}
 
220
 
 
221
static void
 
222
grub_console_cls (void)
 
223
{
 
224
  grub_efi_simple_text_output_interface_t *o;
 
225
  grub_efi_int32_t orig_attr;
 
226
  
 
227
  o = grub_efi_system_table->con_out;
 
228
  orig_attr = o->mode->attribute;
 
229
  o->set_attributes (o, GRUB_EFI_BACKGROUND_BLACK);
 
230
  o->clear_screen (o);
 
231
  o->set_attributes (o, orig_attr);
 
232
}
 
233
 
 
234
static void
 
235
grub_console_setcolorstate (grub_term_color_state state)
 
236
{
 
237
  grub_efi_simple_text_output_interface_t *o;
 
238
 
 
239
  o = grub_efi_system_table->con_out;
 
240
 
 
241
  switch (state) {
 
242
    case GRUB_TERM_COLOR_STANDARD:
 
243
      o->set_attributes (o, grub_console_standard_color);
 
244
      break;
 
245
    case GRUB_TERM_COLOR_NORMAL:
 
246
      o->set_attributes (o, grub_console_normal_color);
 
247
      break;
 
248
    case GRUB_TERM_COLOR_HIGHLIGHT:
 
249
      o->set_attributes (o, grub_console_highlight_color);
 
250
      break;
 
251
    default:
 
252
      break;
 
253
  }
 
254
}
 
255
 
 
256
static void
 
257
grub_console_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
 
258
{
 
259
  grub_console_normal_color = normal_color;
 
260
  grub_console_highlight_color = highlight_color;
 
261
}
 
262
 
 
263
static void
 
264
grub_console_setcursor (int on)
 
265
{
 
266
  grub_efi_simple_text_output_interface_t *o;
 
267
 
 
268
  o = grub_efi_system_table->con_out;
 
269
  o->enable_cursor (o, on);
 
270
}
 
271
 
 
272
static struct grub_term grub_console_term =
 
273
  {
 
274
    .name = "console",
 
275
    .init = 0,
 
276
    .fini = 0,
 
277
    .putchar = grub_console_putchar,
 
278
    .getcharwidth = grub_console_getcharwidth,
 
279
    .checkkey = grub_console_checkkey,
 
280
    .getkey = grub_console_getkey,
 
281
    .getwh = grub_console_getwh,
 
282
    .getxy = grub_console_getxy,
 
283
    .gotoxy = grub_console_gotoxy,
 
284
    .cls = grub_console_cls,
 
285
    .setcolorstate = grub_console_setcolorstate,
 
286
    .setcolor = grub_console_setcolor,
 
287
    .setcursor = grub_console_setcursor,
 
288
    .flags = 0,
 
289
    .next = 0
 
290
  };
 
291
 
 
292
void
 
293
grub_console_init (void)
 
294
{
 
295
  /* FIXME: it is necessary to consider the case where no console control
 
296
     is present but the default is already in text mode.  */
 
297
  if (! grub_efi_set_text_mode (1))
 
298
    {
 
299
      grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
 
300
      return;
 
301
    }
 
302
 
 
303
  grub_term_register (&grub_console_term);
 
304
  grub_term_set_current (&grub_console_term);
 
305
}
 
306
 
 
307
void
 
308
grub_console_fini (void)
 
309
{
 
310
  grub_term_unregister (&grub_console_term);
 
311
}