// =========================================================================
// 
// EE_keyboard.c
// =========================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <exec/types.h>
#include <devices/keyboard.h>

#include <proto/exec.h>

#include "EE_keyboard.h"

struct MsgPort *KeyboardMsgPort;
struct IOStdReq *KeyboardIO;
UBYTE KeyboardMatrix[16];
KEYBOARD_EVENTS KeyboardEvents;

BOOL InitKeyboardDevice(VOID)
{

  if((KeyboardMsgPort = CreatePort(NULL, 0L)) == NULL)
    return FALSE;

  if((KeyboardIO = (struct IOStdReq *)CreateExtIO(KeyboardMsgPort,
                     sizeof(struct IOStdReq))) == NULL)
  {
    DeletePort(KeyboardMsgPort);
    return FALSE;
  }

  if(OpenDevice("keyboard.device", 0L, (struct IORequest *)KeyboardIO, 0L))
  {
    DeleteExtIO((struct IORequest *)KeyboardIO);
    DeletePort(KeyboardMsgPort);
    return FALSE;
  }

  KeyboardIO->io_Data = KeyboardMatrix;
  KeyboardIO->io_Length = 16;
  KeyboardIO->io_Flags = 0;
  KeyboardIO->io_Command = CMD_CLEAR;
  DoIO((struct IORequest *)KeyboardIO);

  KeyboardIO->io_Data = KeyboardMatrix;
  KeyboardIO->io_Length = 16;
  KeyboardIO->io_Flags = 0;
  KeyboardIO->io_Command = KBD_READMATRIX;
  SendIO((struct IORequest *)KeyboardIO);

  return TRUE;
}


VOID RemoveKeyboardDevice(VOID)
{

  if(!CheckIO((struct IORequest *)KeyboardIO))
    AbortIO((struct IORequest *)KeyboardIO);

  WaitIO((struct IORequest *)KeyboardIO);

  CloseDevice((struct IORequest *)KeyboardIO);

  if(KeyboardIO)
    DeleteExtIO((struct IORequest *)KeyboardIO);

  if(KeyboardMsgPort)
    DeletePort(KeyboardMsgPort);
}


VOID GetKeyboardEvents(VOID)
{

  if(CheckIO((struct IORequest *)KeyboardIO))
  {
		if(TEST_KEY(KEY_CLOSED_QBRACKETS))
	    KeyboardEvents.ClosedQBrackets++;
		else
	    KeyboardEvents.ClosedQBrackets = 0;

		if(TEST_KEY(KEY_APOSTROPHE))
	    KeyboardEvents.Apostrophe++;
		else
	    KeyboardEvents.Apostrophe = 0;

		if(TEST_KEY(KEY_SPACE))
	    KeyboardEvents.Space++;
		else
	    KeyboardEvents.Space = 0;

		if(TEST_KEY(KEY_ESC))
	    KeyboardEvents.ESC++;
		else
	    KeyboardEvents.ESC = 0;

		if(TEST_KEY(KEY_UP_ARROW))
			KeyboardEvents.UpArrow++;
		else
			KeyboardEvents.UpArrow = 0;

		if(TEST_KEY(KEY_DOWN_ARROW))
			KeyboardEvents.DownArrow++;
		else
			KeyboardEvents.DownArrow = 0;

		if(TEST_KEY(KEY_RIGHT_ARROW))
			KeyboardEvents.RightArrow++;
		else
			KeyboardEvents.RightArrow = 0;

		if(TEST_KEY(KEY_LEFT_ARROW))
			KeyboardEvents.LeftArrow++;
		else
			KeyboardEvents.LeftArrow = 0;

		if(TEST_KEY(KEY_F1))
			KeyboardEvents.F1++;
		else
			KeyboardEvents.F1 = 0;

		if(TEST_KEY(KEY_F2))
			KeyboardEvents.F2++;
		else
			KeyboardEvents.F2 = 0;

		if(TEST_KEY(KEY_F3))
			KeyboardEvents.F3++;
		else
			KeyboardEvents.F3 = 0;

		if(TEST_KEY(KEY_F4))
			KeyboardEvents.F4++;
		else
			KeyboardEvents.F4 = 0;

		if(TEST_KEY(KEY_F5))
			KeyboardEvents.F5++;
		else
			KeyboardEvents.F5 = 0;

		if(TEST_KEY(KEY_F6))
			KeyboardEvents.F6++;
		else
			KeyboardEvents.F6 = 0;

		if(TEST_KEY(KEY_F7))
			KeyboardEvents.F7++;
		else
			KeyboardEvents.F7 = 0;

		if(TEST_KEY(KEY_F8))
			KeyboardEvents.F8++;
		else
			KeyboardEvents.F8 = 0;

		if(TEST_KEY(KEY_F9))
			KeyboardEvents.F9++;
		else
			KeyboardEvents.F9 = 0;

		if(TEST_KEY(KEY_F10))
			KeyboardEvents.F10++;
		else
			KeyboardEvents.F10 = 0;

		if(TEST_KEY(KEY_LEFT_SHIFT))
			KeyboardEvents.LeftShift++;
		else
			KeyboardEvents.LeftShift = 0;

		if(TEST_KEY(KEY_LEFT_ALT))
			KeyboardEvents.LeftAlt++;
		else
			KeyboardEvents.LeftAlt = 0;

    Remove((struct Node *)KeyboardIO);

    KeyboardIO->io_Data = KeyboardMatrix;
    KeyboardIO->io_Length = 16;
    KeyboardIO->io_Flags = 0;
    KeyboardIO->io_Command = KBD_READMATRIX;
    SendIO((struct IORequest *)KeyboardIO);
  }
}
