summaryrefslogtreecommitdiff
path: root/payloads/libpayload/curses/keyboard.c
blob: ae3e1c5d70e7067860ea6d9a99717ee3b394404d (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * This file is part of the libpayload project.
 *
 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
 * Copyright (C) 2008 Advanced Micro Devices, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * This file handles reading keystrokes from serial and the console
 * and "cooking" them so that they are correct for curses.
 * Also, implement key related functions (mainly wgetch)
 *
 * TODO:
 * Actually cook the serial (handle special keys)
 */

#include <config.h>
#include "local.h"

static int _halfdelay = 0;

/* ============== Serial ==================== */

/* We treat serial like a vt100 terminal.  For now we
   do the cooking in here, but we should probably eventually
   pass it to dedicated vt100 code */

static int getkeyseq(char *buffer, int len)
{
	int i;

	for(i = 0; i < 75; i++) {
		if (serial_havechar())
			break;
		mdelay(1);
	}

	if (i == 75)
		return len;

	buffer[len++] = serial_getchar();
	return getkeyseq(buffer, len);
}

static struct {
	char *seq;
	int key;
} escape_codes[] = {
	{ "[A", KEY_UP },
	{ "[B", KEY_DOWN },
	{ "[C", KEY_RIGHT },
	{ "[D", KEY_LEFT },
	{ "OP", KEY_F(1) },
	{ "OQ", KEY_F(2) },
	{ "OR", KEY_F(3) },
	{ "OS", KEY_F(4) },
	{ "[15~", KEY_F(5) },
	{ "[17~", KEY_F(6) },
	{ "[18~", KEY_F(7) },
	{ "[19~", KEY_F(8) },
	{ "[20~", KEY_F(9) },
	{ "[21~", KEY_F(10) },
	{ "[24~", KEY_F(12) },
	{ NULL },
};

static int handle_escape(void)
{
	char buffer[5];
	int len = getkeyseq(buffer, 0);
	int i, t;

	if (len == 0)
		return 27;

	for(i = 0; escape_codes[i].seq != NULL; i++) {
		char *p = escape_codes[i].seq;

		for(t = 0; t < len; t++) {
			if (!*p || *p != buffer[t])
				break;
			p++;
		}

		if (t == len)
			return escape_codes[i].key;
	}

	return 0;
}

static int cook_serial(unsigned char ch)
{
	switch(ch) {
	case 8:
		return KEY_BACKSPACE;

	case 13:
		return KEY_ENTER;

	case 27:
		return handle_escape();

	default:
		return ch;
	}
}

/* ================ Keyboard ================ */

/* Scancode macros */

#define DOWN(_c) (0x80 | (_c))
#define UP(_c) (_c)

#define ISDOWN(_c) ((_c) & 0x80)
#define ISUP(_c) (!ISDOWN((_c)))

#define SCANCODE(_c) ((_c) & ~0x80)

/* Scancode definitions for the modifiers */

#define SCANCODE_RSHIFT   0x36
#define SCANCODE_LSHIFT   0x2a
#define SCANCODE_CAPSLOCK 0x3a
#define SCANCODE_LALT     0x38
#define SCANCODE_LCTRL    0x1d

/* Modifier flags */

#define SHIFT_MODIFIER    0x1
#define CAPSLOCK_MODIFIER 0x2
#define ALT_MODIFIER      0x4
#define CTRL_MODIFIER     0x8

#define CTRL(_c) (_c & 0x1f)

struct {
	int normal;
	int shift;
} scancode_map[] = {
	{ },
	{ CTRL('['), CTRL('[')}, 
	{ '1', '!' },
	{ '2', '@' },
	{ '3', '#' },
	{ '4', '$' },
	{ '5', '%' },
	{ '6', '^' },
	{ '7', '&' },
	{ '8', '*' },
	{ '9', '(' },
	{ '0', ')' },
	{ '-', '_' },
	{ '=', '+' },
	{ KEY_BACKSPACE, KEY_BACKSPACE},
	{ CTRL('I' ), KEY_BTAB },          /* 0x0F */
	{ 'q', 'Q' },
	{ 'w', 'W' },
	{ 'e', 'E' },
	{ 'r', 'R' },
	{ 't', 'T' },
	{ 'y', 'Y' },
	{ 'u', 'U' },
	{ 'i', 'I' },
	{ 'o', 'O' },
	{ 'p', 'P' },
	{ '[', '{' },
	{ ']', '{' },
	{ KEY_ENTER, KEY_ENTER },
	{ 0 , 0 },
	{ 'a', 'A' },
	{ 's', 'S' },                    /* 0x1F */
	{ 'd', 'D' },
	{ 'f', 'F' },
	{ 'g', 'G' },
	{ 'h', 'H' },
	{ 'j', 'J' },
	{ 'k', 'K' },
	{ 'l', 'L' },
	{ ';', ':' },
	{ '\'', '\"' },
	{ '`', '~', },
	{ 0, 0 },
	{ '\\', '|' },
	{ 'z', 'Z' },
	{ 'x', 'X' },
	{ 'c', 'C' },
	{ 'v', 'V' },                   /* 0x2F */
	{ 'b', 'B' },
	{ 'n', 'N' },
	{ 'm', 'M' },
	{ ',', '<'},
	{ '.', '>' },
	{ '/', '?' },
	{ 0, 0 },                       /* RSHIFT */
	{ '*', '*' },
	{ 0, 0 },                       /* LALT */
	{ ' ', ' ' },                   /* Space */
	{ 0, 0 },                       /* Capslock */
	{ KEY_F(1), KEY_F(1) },
	{ KEY_F(2), KEY_F(2) },
	{ KEY_F(3), KEY_F(3) },
	{ KEY_F(4), KEY_F(4) },
	{ KEY_F(5), KEY_F(5) },         /* 0x3F */
	{ KEY_F(6), KEY_F(6) },
	{ KEY_F(7), KEY_F(7) },
	{ KEY_F(8), KEY_F(8) },
	{ KEY_F(9), KEY_F(9) },
	{ KEY_F(10), KEY_F(10) },
	{ 0, 0 },                      /* Numlock */
	{ 0, 0 },                      /* Scroll lock */
	{ KEY_HOME, KEY_HOME },
	{ KEY_UP,   KEY_UP },
	{ KEY_PPAGE, KEY_PPAGE },
	{ '-',      '-' },
	{ KEY_LEFT, KEY_LEFT },
	{ 0,        0 },
	{ KEY_RIGHT, KEY_RIGHT },
	{ '-',       '-' },
	{ KEY_END,  KEY_END },         /* 0x4F */
	{ KEY_DOWN, KEY_DOWN },
	{ KEY_NPAGE, KEY_NPAGE },
	{ KEY_IC,    KEY_IC },
	{ KEY_DC,    KEY_DC },
	{ 0, 0 },                     /* sysreq */
	{ 0, 0 },
	{ KEY_F(11), KEY_F(11) },
	{ KEY_F(12), KEY_F(12) },
};

static int cook_scancodes(unsigned char code)
{
	static int modifiers = 0;
	int ch = 0, sc, shift;

	switch (code) {
	case DOWN(SCANCODE_RSHIFT):
	case DOWN(SCANCODE_LSHIFT):
		modifiers |= SHIFT_MODIFIER;
		return 0;
	case UP(SCANCODE_RSHIFT):
	case UP(SCANCODE_LSHIFT):
		modifiers &= ~SHIFT_MODIFIER;
		return 0;
	case UP(SCANCODE_CAPSLOCK):
		if (modifiers & CAPSLOCK_MODIFIER)
			modifiers &= ~CAPSLOCK_MODIFIER;
		else
			modifiers |= CAPSLOCK_MODIFIER;
		return 0;
	case DOWN(SCANCODE_LALT):
		modifiers |= ALT_MODIFIER;
		return 0;
	case UP(SCANCODE_LALT):
		modifiers &= ~ALT_MODIFIER;
		return 0;
	case DOWN(SCANCODE_LCTRL):
		modifiers |= CTRL_MODIFIER;
		return 0;
	case UP(SCANCODE_LCTRL):
		modifiers &= ~CTRL_MODIFIER;
		return 0;
	}

	/* Only process keys on an upstroke. */
	if (!ISUP(code))
		return 0;

	sc = SCANCODE(code);

	if (sc == 0 || sc > 0x59)
		return ERR;

	shift = (modifiers & SHIFT_MODIFIER) ^ (modifiers & CAPSLOCK_MODIFIER);

	ch = shift ? scancode_map[sc].shift : scancode_map[sc].normal;

	if (modifiers & CTRL_MODIFIER)
		ch = (ch >= 0x3F && ch <= 0x5F) ? CTRL(ch) : 0;

	return ch;
}

static int curses_getchar(int delay)
{
	unsigned char c = 0;
	int ret;

	do {
		if (curses_flags & F_ENABLE_CONSOLE)
			c = inb(0x64);

		if ((c & 1) == 0) {

			if ((curses_flags & F_ENABLE_SERIAL) &&
			    serial_havechar()) {
				c = serial_getchar();
				return cook_serial(c);
			}

			if (delay == 0)
				break;

			if (delay > 0) {
				mdelay(100);
				delay--;
			}

			continue;
		}

		c = inb(0x60);

		ret = cook_scancodes(c);

		if (ret != 0) {
			return ret;
		}

	} while (1);

	return ERR;
}

/* === Public functions === */

int wgetch(WINDOW *win)
{
	int delay = -1;

	if (_halfdelay || win->_delay)
		delay = win->_delay ? 0 : _halfdelay;

	return curses_getchar(delay);
}

int nodelay(WINDOW *win, NCURSES_BOOL flag)
{
	win->_delay = flag ? 1 : 0;
	return 0;
}

int halfdelay(int tenths)
{
	if (tenths > 255)
		return ERR;

	_halfdelay = tenths;
	return 0;
}

int nocbreak(void)
{
	/* Remove half delay timeout. */
	_halfdelay = 0;
	return 0;
}

#ifdef CONFIG_VGA_CONSOLE
void curses_enable_vga(int state)
{
	if (state)
		curses_flags |= F_ENABLE_CONSOLE;
	else
		curses_flags &= ~F_ENABLE_CONSOLE;
}
#else
void curses_enable_vga(int state) { }
#endif

#ifdef CONFIG_SERIAL_CONSOLE
void curses_enable_serial(int state)
{
	if (state)
		curses_flags |= F_ENABLE_SERIAL;
	else
		curses_flags &= ~F_ENABLE_SERIAL;
}
#else
void curses_enable_serial(int state) { }
#endif