1fc17fe17SAxel Dörfler/*
22545ff58SAxel Dörfler * Copyright 2004-2011, Axel D��rfler, axeld@pinc-software.de.
3a1b2e9e9SAxel Dörfler * Distributed under the terms of the MIT License.
4a1b2e9e9SAxel Dörfler */
5fc17fe17SAxel Dörfler
6fc17fe17SAxel Dörfler
7fc17fe17SAxel Dörfler#include "keyboard.h"
8fc17fe17SAxel Dörfler#include "bios.h"
9fc17fe17SAxel Dörfler
10fc17fe17SAxel Dörfler#include <boot/platform.h>
11fc17fe17SAxel Dörfler
12fc17fe17SAxel Dörfler
13a1b2e9e9SAxel Dörfler/*!	Note, checking for keys doesn't seem to work in graphics
14a1b2e9e9SAxel Dörfler	mode, at least in Bochs.
15a1b2e9e9SAxel Dörfler*/
16fc17fe17SAxel Dörflerstatic uint16
17fc17fe17SAxel Dörflercheck_for_key(void)
18fc17fe17SAxel Dörfler{
19fc17fe17SAxel Dörfler	bios_regs regs;
20fc17fe17SAxel Dörfler	regs.eax = 0x0100;
21fc17fe17SAxel Dörfler	call_bios(0x16, &regs);
22fc17fe17SAxel Dörfler
23fc17fe17SAxel Dörfler	// the zero flag is set when there is no key stroke waiting for us
24fc17fe17SAxel Dörfler	if (regs.flags & ZERO_FLAG)
25fc17fe17SAxel Dörfler		return 0;
26fc17fe17SAxel Dörfler
27fc17fe17SAxel Dörfler	// remove the key from the buffer
28fc17fe17SAxel Dörfler	regs.eax = 0;
29fc17fe17SAxel Dörfler	call_bios(0x16, &regs);
30fc17fe17SAxel Dörfler
31fc17fe17SAxel Dörfler	return regs.eax & 0xffff;
32fc17fe17SAxel Dörfler}
33fc17fe17SAxel Dörfler
34fc17fe17SAxel Dörfler
35356f4957SAxel Dörflerextern "C" void
36356f4957SAxel Dörflerclear_key_buffer(void)
37356f4957SAxel Dörfler{
38356f4957SAxel Dörfler	while (check_for_key() != 0)
39356f4957SAxel Dörfler		;
40356f4957SAxel Dörfler}
41356f4957SAxel Dörfler
42356f4957SAxel Dörfler
43fc17fe17SAxel Dörflerextern "C" union key
44fc17fe17SAxel Dörflerwait_for_key(void)
45fc17fe17SAxel Dörfler{
46fc17fe17SAxel Dörfler	union key key;
472545ff58SAxel Dörfler	do {
482545ff58SAxel Dörfler		key.ax = check_for_key();
492545ff58SAxel Dörfler	} while (key.ax == 0);
50fc17fe17SAxel Dörfler
51fc17fe17SAxel Dörfler	return key;
52fc17fe17SAxel Dörfler}
53fc17fe17SAxel Dörfler
54fc17fe17SAxel Dörfler
55fc17fe17SAxel Dörflerextern "C" uint32
56fc17fe17SAxel Dörflercheck_for_boot_keys(void)
57fc17fe17SAxel Dörfler{
5806f34bf2SStephan Aßmus	bios_regs regs;
59fc17fe17SAxel Dörfler	uint32 options = 0;
6006f34bf2SStephan Aßmus	uint32 keycode = 0;
6106f34bf2SStephan Aßmus	regs.eax = 0x0200;
6206f34bf2SStephan Aßmus	call_bios(0x16, &regs);
6306f34bf2SStephan Aßmus		// Read Keyboard flags. bit 0 LShift, bit 1 RShift
6406f34bf2SStephan Aßmus	if ((regs.eax & 0x03) != 0) {
6506f34bf2SStephan Aßmus		// LShift or RShift - option menu
6606f34bf2SStephan Aßmus		options |= BOOT_OPTION_MENU;
6706f34bf2SStephan Aßmus	} else {
68a1b2e9e9SAxel Dörfler		keycode = boot_key_in_keyboard_buffer();
69a1b2e9e9SAxel Dörfler		if (keycode == 0x3920) {
70a1b2e9e9SAxel Dörfler			// space - option menu
7106f34bf2SStephan Aßmus			options |= BOOT_OPTION_MENU;
7206f34bf2SStephan Aßmus		} else if (keycode == 0x011B) {
7306f34bf2SStephan Aßmus			// ESC - debug output
7406f34bf2SStephan Aßmus			options |= BOOT_OPTION_DEBUG_OUTPUT;
7506f34bf2SStephan Aßmus 		}
76fc17fe17SAxel Dörfler	}
77fc17fe17SAxel Dörfler
78fc17fe17SAxel Dörfler	dprintf("options = %ld\n", options);
79fc17fe17SAxel Dörfler	return options;
80fc17fe17SAxel Dörfler}
81fc17fe17SAxel Dörfler
82