1/*
2 * Copyright 2004-2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "keyboard.h"
8#include "bios.h"
9
10#include <boot/platform.h>
11
12
13/*!	Note, checking for keys doesn't seem to work in graphics
14	mode, at least in Bochs.
15*/
16static uint16
17check_for_key(void)
18{
19	bios_regs regs;
20	regs.eax = 0x0100;
21	call_bios(0x16, &regs);
22
23	// the zero flag is set when there is no key stroke waiting for us
24	if (regs.flags & ZERO_FLAG)
25		return 0;
26
27	// remove the key from the buffer
28	regs.eax = 0;
29	call_bios(0x16, &regs);
30
31	return regs.eax & 0xffff;
32}
33
34
35extern "C" void
36clear_key_buffer(void)
37{
38	while (check_for_key() != 0)
39		;
40}
41
42
43extern "C" union key
44wait_for_key(void)
45{
46	union key key;
47	do {
48		key.ax = check_for_key();
49	} while (key.ax == 0);
50
51	return key;
52}
53
54
55extern "C" uint32
56check_for_boot_keys(void)
57{
58	bios_regs regs;
59	uint32 options = 0;
60	uint32 keycode = 0;
61	regs.eax = 0x0200;
62	call_bios(0x16, &regs);
63		// Read Keyboard flags. bit 0 LShift, bit 1 RShift
64	if ((regs.eax & 0x03) != 0) {
65		// LShift or RShift - option menu
66		options |= BOOT_OPTION_MENU;
67	} else {
68		keycode = boot_key_in_keyboard_buffer();
69		if (keycode == 0x3920) {
70			// space - option menu
71			options |= BOOT_OPTION_MENU;
72		} else if (keycode == 0x011B) {
73			// ESC - debug output
74			options |= BOOT_OPTION_DEBUG_OUTPUT;
75 		}
76	}
77
78	dprintf("options = %ld\n", options);
79	return options;
80}
81
82