aboutsummaryrefslogtreecommitdiffstats
path: root/grub-core/kern/emu/console.c
blob: 7fd1fc0c95728797a45a1699f48ce06a769e7b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*  console.c -- Ncurses console for GRUB.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2003,2005,2007,2008  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <config-util.h>

/* For compatibility.  */
#ifndef A_NORMAL
# define A_NORMAL	0
#endif /* ! A_NORMAL */
#ifndef A_STANDOUT
# define A_STANDOUT	0
#endif /* ! A_STANDOUT */

#include <grub/emu/console.h>
#include <grub/term.h>
#include <grub/types.h>

#if defined(HAVE_NCURSES_CURSES_H)
# include <ncurses/curses.h>
#elif defined(HAVE_NCURSES_H)
# include <ncurses.h>
#elif defined(HAVE_CURSES_H)
# include <curses.h>
#else
#error What the hell?
#endif

static int grub_console_attr = A_NORMAL;

grub_uint8_t grub_console_cur_color = 7;

static const grub_uint8_t grub_console_standard_color = 0x7;

#define NUM_COLORS	8

static grub_uint8_t color_map[NUM_COLORS] =
{
  COLOR_BLACK,
  COLOR_BLUE,
  COLOR_GREEN,
  COLOR_CYAN,
  COLOR_RED,
  COLOR_MAGENTA,
  COLOR_YELLOW,
  COLOR_WHITE
};

static int use_color;

static void
grub_ncurses_putchar (struct grub_term_output *term __attribute__ ((unused)),
		      const struct grub_unicode_glyph *c)
{
  addch (c->base | grub_console_attr);
}

static void
grub_ncurses_setcolorstate (struct grub_term_output *term,
			    grub_term_color_state state)
{
  switch (state)
    {
    case GRUB_TERM_COLOR_STANDARD:
      grub_console_cur_color = grub_console_standard_color;
      grub_console_attr = A_NORMAL;
      break;
    case GRUB_TERM_COLOR_NORMAL:
      grub_console_cur_color = term->normal_color;
      grub_console_attr = A_NORMAL;
      break;
    case GRUB_TERM_COLOR_HIGHLIGHT:
      grub_console_cur_color = term->highlight_color;
      grub_console_attr = A_STANDOUT;
      break;
    default:
      break;
    }

  if (use_color)
    {
      grub_uint8_t fg, bg;

      fg = (grub_console_cur_color & 7);
      bg = (grub_console_cur_color >> 4) & 7;

      grub_console_attr = (grub_console_cur_color & 8) ? A_BOLD : A_NORMAL;
      color_set ((bg << 3) + fg, 0);
    }
}

static int
grub_ncurses_getkey (struct grub_term_input *term __attribute__ ((unused)))
{
  int c;

  wtimeout (stdscr, 100);
  c = getch ();

  switch (c)
    {
    case ERR:
      return -1;
    case KEY_LEFT:
      c = GRUB_TERM_KEY_LEFT;
      break;

    case KEY_RIGHT:
      c = GRUB_TERM_KEY_RIGHT;
      break;

    case KEY_UP:
      c = GRUB_TERM_KEY_UP;
      break;

    case KEY_DOWN:
      c = GRUB_TERM_KEY_DOWN;
      break;

    case KEY_IC:
      c = 24;
      break;

    case KEY_DC:
      c = GRUB_TERM_KEY_DC;
      break;

    case KEY_BACKSPACE:
      /* XXX: For some reason ncurses on xterm does not return
	 KEY_BACKSPACE.  */
    case 127:
      c = '\b';
      break;

    case KEY_HOME:
      c = GRUB_TERM_KEY_HOME;
      break;

    case KEY_END:
      c = GRUB_TERM_KEY_END;
      break;

    case KEY_NPAGE:
      c = GRUB_TERM_KEY_NPAGE;
      break;

    case KEY_PPAGE:
      c = GRUB_TERM_KEY_PPAGE;
      break;
    }

  return c;
}

static grub_uint16_t
grub_ncurses_getxy (struct grub_term_output *term __attribute__ ((unused)))
{
  int x;
  int y;

  getyx (stdscr, y, x);

  return (x << 8) | y;
}

static grub_uint16_t
grub_ncurses_getwh (struct grub_term_output *term __attribute__ ((unused)))
{
  int x;
  int y;

  getmaxyx (stdscr, y, x);

  return (x << 8) | y;
}

static void
grub_ncurses_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
		     grub_uint8_t x, grub_uint8_t y)
{
  move (y, x);
}

static void
grub_ncurses_cls (struct grub_term_output *term __attribute__ ((unused)))
{
  clear ();
  refresh ();
}

static void
grub_ncurses_setcursor (struct grub_term_output *term __attribute__ ((unused)),
			int on)
{
  curs_set (on ? 1 : 0);
}

static void
grub_ncurses_refresh (struct grub_term_output *term __attribute__ ((unused)))
{
  refresh ();
}

static grub_err_t
grub_ncurses_init (struct grub_term_output *term __attribute__ ((unused)))
{
  initscr ();
  raw ();
  noecho ();
  scrollok (stdscr, TRUE);

  nonl ();
  intrflush (stdscr, FALSE);
  keypad (stdscr, TRUE);

  if (has_colors ())
    {
      start_color ();

      if ((COLORS >= NUM_COLORS) && (COLOR_PAIRS >= NUM_COLORS * NUM_COLORS))
        {
          int i, j, n;

          n = 0;
          for (i = 0; i < NUM_COLORS; i++)
            for (j = 0; j < NUM_COLORS; j++)
              init_pair(n++, color_map[j], color_map[i]);

          use_color = 1;
        }
    }

  return 0;
}

static grub_err_t
grub_ncurses_fini (struct grub_term_output *term __attribute__ ((unused)))
{
  endwin ();
  return 0;
}


static struct grub_term_input grub_ncurses_term_input =
  {
    .name = "console",
    .getkey = grub_ncurses_getkey,
  };

static struct grub_term_output grub_ncurses_term_output =
  {
    .name = "console",
    .init = grub_ncurses_init,
    .fini = grub_ncurses_fini,
    .putchar = grub_ncurses_putchar,
    .getxy = grub_ncurses_getxy,
    .getwh = grub_ncurses_getwh,
    .gotoxy = grub_ncurses_gotoxy,
    .cls = grub_ncurses_cls,
    .setcolorstate = grub_ncurses_setcolorstate,
    .setcursor = grub_ncurses_setcursor,
    .refresh = grub_ncurses_refresh,
    .flags = GRUB_TERM_CODE_TYPE_ASCII
  };

void
grub_console_init (void)
{
  grub_term_register_output ("console", &grub_ncurses_term_output);
  grub_term_register_input ("console", &grub_ncurses_term_input);
}

void
grub_console_fini (void)
{
  grub_ncurses_fini (&grub_ncurses_term_output);
}