/* Keyboard handling code V1.12 4/9/04
   See keyboard.h for docs
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM 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.
   WOUM 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 WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _KEYBOARD_C
#define _KEYBOARD_C

#include "kernel.h"
#include "swis.h"

#include "keyboard.h"

static _kernel_swi_regs regs;

int keyboard_ispressed(int key)
{
	regs.r[0] = 121;
	regs.r[1] = key ^ 0x80;
	_kernel_swi(OS_Byte,&regs,&regs);
	if (regs.r[1] == 255)
		return 1;
	return 0;
}

void keyboard_flush()
{
	regs.r[0] = 21;
	regs.r[1] = 0;
	_kernel_swi(OS_Byte,&regs,&regs);
}

int keyboard_scan(int key)
{
	regs.r[0] = 121;
	regs.r[1] = key;
	_kernel_swi(OS_Byte,&regs,&regs);
	return regs.r[1];
}

int keyboard_read()
{
	int c;
	regs.r[0] = 145;
	regs.r[1] = 0;
	_kernel_swi_c(OS_Byte,&regs,&regs,&c);
	if (c)
		return -1;
	return regs.r[2];
}

#endif
