~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to grub-core/kern/emu/console.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-09-13 18:02:04 UTC
  • mfrom: (1.17.15 upstream)
  • mto: (17.6.27 experimental)
  • mto: This revision was merged to the branch mainline in revision 145.
  • Revision ID: package-import@ubuntu.com-20120913180204-mojnmocbimlom4im
Tags: upstream-2.00
ImportĀ upstreamĀ versionĀ 2.00

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  console.c -- Ncurses console for GRUB.  */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2003,2005,2007,2008  Free Software Foundation, Inc.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
 
20
 
#include <config.h>
21
 
#include <config-util.h>
22
 
 
23
 
/* For compatibility.  */
24
 
#ifndef A_NORMAL
25
 
# define A_NORMAL       0
26
 
#endif /* ! A_NORMAL */
27
 
#ifndef A_STANDOUT
28
 
# define A_STANDOUT     0
29
 
#endif /* ! A_STANDOUT */
30
 
 
31
 
#include <grub/emu/console.h>
32
 
#include <grub/term.h>
33
 
#include <grub/types.h>
34
 
 
35
 
#if defined(HAVE_NCURSES_CURSES_H)
36
 
# include <ncurses/curses.h>
37
 
#elif defined(HAVE_NCURSES_H)
38
 
# include <ncurses.h>
39
 
#elif defined(HAVE_CURSES_H)
40
 
# include <curses.h>
41
 
#else
42
 
#error What the hell?
43
 
#endif
44
 
 
45
 
static int grub_console_attr = A_NORMAL;
46
 
 
47
 
grub_uint8_t grub_console_cur_color = 7;
48
 
 
49
 
static const grub_uint8_t grub_console_standard_color = 0x7;
50
 
 
51
 
#define NUM_COLORS      8
52
 
 
53
 
static grub_uint8_t color_map[NUM_COLORS] =
54
 
{
55
 
  COLOR_BLACK,
56
 
  COLOR_BLUE,
57
 
  COLOR_GREEN,
58
 
  COLOR_CYAN,
59
 
  COLOR_RED,
60
 
  COLOR_MAGENTA,
61
 
  COLOR_YELLOW,
62
 
  COLOR_WHITE
63
 
};
64
 
 
65
 
static int use_color;
66
 
 
67
 
static void
68
 
grub_ncurses_putchar (struct grub_term_output *term __attribute__ ((unused)),
69
 
                      const struct grub_unicode_glyph *c)
70
 
{
71
 
  addch (c->base | grub_console_attr);
72
 
}
73
 
 
74
 
static void
75
 
grub_ncurses_setcolorstate (struct grub_term_output *term,
76
 
                            grub_term_color_state state)
77
 
{
78
 
  switch (state)
79
 
    {
80
 
    case GRUB_TERM_COLOR_STANDARD:
81
 
      grub_console_cur_color = grub_console_standard_color;
82
 
      grub_console_attr = A_NORMAL;
83
 
      break;
84
 
    case GRUB_TERM_COLOR_NORMAL:
85
 
      grub_console_cur_color = term->normal_color;
86
 
      grub_console_attr = A_NORMAL;
87
 
      break;
88
 
    case GRUB_TERM_COLOR_HIGHLIGHT:
89
 
      grub_console_cur_color = term->highlight_color;
90
 
      grub_console_attr = A_STANDOUT;
91
 
      break;
92
 
    default:
93
 
      break;
94
 
    }
95
 
 
96
 
  if (use_color)
97
 
    {
98
 
      grub_uint8_t fg, bg;
99
 
 
100
 
      fg = (grub_console_cur_color & 7);
101
 
      bg = (grub_console_cur_color >> 4) & 7;
102
 
 
103
 
      grub_console_attr = (grub_console_cur_color & 8) ? A_BOLD : A_NORMAL;
104
 
      color_set ((bg << 3) + fg, 0);
105
 
    }
106
 
}
107
 
 
108
 
static int
109
 
grub_ncurses_getkey (struct grub_term_input *term __attribute__ ((unused)))
110
 
{
111
 
  int c;
112
 
 
113
 
  wtimeout (stdscr, 100);
114
 
  c = getch ();
115
 
 
116
 
  switch (c)
117
 
    {
118
 
    case ERR:
119
 
      return -1;
120
 
    case KEY_LEFT:
121
 
      c = GRUB_TERM_KEY_LEFT;
122
 
      break;
123
 
 
124
 
    case KEY_RIGHT:
125
 
      c = GRUB_TERM_KEY_RIGHT;
126
 
      break;
127
 
 
128
 
    case KEY_UP:
129
 
      c = GRUB_TERM_KEY_UP;
130
 
      break;
131
 
 
132
 
    case KEY_DOWN:
133
 
      c = GRUB_TERM_KEY_DOWN;
134
 
      break;
135
 
 
136
 
    case KEY_IC:
137
 
      c = 24;
138
 
      break;
139
 
 
140
 
    case KEY_DC:
141
 
      c = GRUB_TERM_KEY_DC;
142
 
      break;
143
 
 
144
 
    case KEY_BACKSPACE:
145
 
      /* XXX: For some reason ncurses on xterm does not return
146
 
         KEY_BACKSPACE.  */
147
 
    case 127:
148
 
      c = '\b';
149
 
      break;
150
 
 
151
 
    case KEY_HOME:
152
 
      c = GRUB_TERM_KEY_HOME;
153
 
      break;
154
 
 
155
 
    case KEY_END:
156
 
      c = GRUB_TERM_KEY_END;
157
 
      break;
158
 
 
159
 
    case KEY_NPAGE:
160
 
      c = GRUB_TERM_KEY_NPAGE;
161
 
      break;
162
 
 
163
 
    case KEY_PPAGE:
164
 
      c = GRUB_TERM_KEY_PPAGE;
165
 
      break;
166
 
    }
167
 
 
168
 
  return c;
169
 
}
170
 
 
171
 
static grub_uint16_t
172
 
grub_ncurses_getxy (struct grub_term_output *term __attribute__ ((unused)))
173
 
{
174
 
  int x;
175
 
  int y;
176
 
 
177
 
  getyx (stdscr, y, x);
178
 
 
179
 
  return (x << 8) | y;
180
 
}
181
 
 
182
 
static grub_uint16_t
183
 
grub_ncurses_getwh (struct grub_term_output *term __attribute__ ((unused)))
184
 
{
185
 
  int x;
186
 
  int y;
187
 
 
188
 
  getmaxyx (stdscr, y, x);
189
 
 
190
 
  return (x << 8) | y;
191
 
}
192
 
 
193
 
static void
194
 
grub_ncurses_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
195
 
                     grub_uint8_t x, grub_uint8_t y)
196
 
{
197
 
  move (y, x);
198
 
}
199
 
 
200
 
static void
201
 
grub_ncurses_cls (struct grub_term_output *term __attribute__ ((unused)))
202
 
{
203
 
  clear ();
204
 
  refresh ();
205
 
}
206
 
 
207
 
static void
208
 
grub_ncurses_setcursor (struct grub_term_output *term __attribute__ ((unused)),
209
 
                        int on)
210
 
{
211
 
  curs_set (on ? 1 : 0);
212
 
}
213
 
 
214
 
static void
215
 
grub_ncurses_refresh (struct grub_term_output *term __attribute__ ((unused)))
216
 
{
217
 
  refresh ();
218
 
}
219
 
 
220
 
static grub_err_t
221
 
grub_ncurses_init (struct grub_term_output *term __attribute__ ((unused)))
222
 
{
223
 
  initscr ();
224
 
  raw ();
225
 
  noecho ();
226
 
  scrollok (stdscr, TRUE);
227
 
 
228
 
  nonl ();
229
 
  intrflush (stdscr, FALSE);
230
 
  keypad (stdscr, TRUE);
231
 
 
232
 
  if (has_colors ())
233
 
    {
234
 
      start_color ();
235
 
 
236
 
      if ((COLORS >= NUM_COLORS) && (COLOR_PAIRS >= NUM_COLORS * NUM_COLORS))
237
 
        {
238
 
          int i, j, n;
239
 
 
240
 
          n = 0;
241
 
          for (i = 0; i < NUM_COLORS; i++)
242
 
            for (j = 0; j < NUM_COLORS; j++)
243
 
              init_pair(n++, color_map[j], color_map[i]);
244
 
 
245
 
          use_color = 1;
246
 
        }
247
 
    }
248
 
 
249
 
  return 0;
250
 
}
251
 
 
252
 
static grub_err_t
253
 
grub_ncurses_fini (struct grub_term_output *term __attribute__ ((unused)))
254
 
{
255
 
  endwin ();
256
 
  return 0;
257
 
}
258
 
 
259
 
 
260
 
static struct grub_term_input grub_ncurses_term_input =
261
 
  {
262
 
    .name = "console",
263
 
    .getkey = grub_ncurses_getkey,
264
 
  };
265
 
 
266
 
static struct grub_term_output grub_ncurses_term_output =
267
 
  {
268
 
    .name = "console",
269
 
    .init = grub_ncurses_init,
270
 
    .fini = grub_ncurses_fini,
271
 
    .putchar = grub_ncurses_putchar,
272
 
    .getxy = grub_ncurses_getxy,
273
 
    .getwh = grub_ncurses_getwh,
274
 
    .gotoxy = grub_ncurses_gotoxy,
275
 
    .cls = grub_ncurses_cls,
276
 
    .setcolorstate = grub_ncurses_setcolorstate,
277
 
    .setcursor = grub_ncurses_setcursor,
278
 
    .refresh = grub_ncurses_refresh,
279
 
    .flags = GRUB_TERM_CODE_TYPE_ASCII
280
 
  };
281
 
 
282
 
void
283
 
grub_console_init (void)
284
 
{
285
 
  grub_term_register_output ("console", &grub_ncurses_term_output);
286
 
  grub_term_register_input ("console", &grub_ncurses_term_input);
287
 
}
288
 
 
289
 
void
290
 
grub_console_fini (void)
291
 
{
292
 
  grub_ncurses_fini (&grub_ncurses_term_output);
293
 
}