/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.

This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2[b] requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").

Contact information
-------------------

Circuits At Home, LTD
Web      :  http://www.circuitsathome.com
e-mail   :  support@circuitsathome.com
*/

#ifndef HIDBOOT_H_INCLUDED
#define HIDBOOT_H_INCLUDED

#include <stdint.h>
#include "keyboardpipe.h"

#define KEY_SPACE					0x2c
#define KEY_ZERO					0x27
#define KEY_ZERO2					0x62
#define KEY_ENTER					0x28
#define KEY_PERIOD					0x63
#define KEY_LESSMORE				0x64
#define KEY_DELETE					0x4c
/**
 * \brief MOUSEINFO definition.
 */
struct MOUSEINFO
{
	struct
	{
		uint8_t		bmLeftButton	: 1;
		uint8_t		bmRightButton	: 1;
		uint8_t		bmMiddleButton	: 1;
		uint8_t		bmDummy			: 1;
	};
	int8_t			dX;
	int8_t			dY;
};



/**
 * \brief MODIFIERKEYS definition.
 */
struct MODIFIERKEYS
{
	uint8_t		bmLeftCtrl		: 1;
	uint8_t		bmLeftShift		: 1;
	uint8_t		bmLeftAlt		: 1;
	uint8_t		bmLeftGUI		: 1;
	uint8_t		bmRightCtrl		: 1;
	uint8_t		bmRightShift	: 1;
	uint8_t		bmRightAlt		: 1;
	uint8_t		bmRightGUI		: 1;
};

/**
 * \brief KBDINFO definition.
 */
struct KBDINFO
{
	struct
	{
		uint8_t		bmLeftCtrl		: 1;
		uint8_t		bmLeftShift		: 1;
		uint8_t		bmLeftAlt		: 1;
		uint8_t		bmLeftGUI		: 1;
		uint8_t		bmRightCtrl		: 1;
		uint8_t		bmRightShift	: 1;
		uint8_t		bmRightAlt		: 1;
		uint8_t		bmRightGUI		: 1;
	};
	uint8_t			bReserved;
	uint8_t			Keys[6];
};

/**
 * \brief KBDLEDS definition.
 */
struct KBDLEDS
{
	uint8_t		bmNumLock		: 1;
	uint8_t		bmCapsLock		: 1;
	uint8_t		bmScrollLock	: 1;
	uint8_t		bmCompose		: 1;
	uint8_t		bmKana			: 1;
	uint8_t		bmReserved		: 3;
};

#define KEY_NUM_LOCK				0x53

// Clear compiler warning
#ifdef KEY_CAPS_LOCK
#undef KEY_CAPS_LOCK
#endif

#define KEY_CAPS_LOCK				0x39
#define KEY_SCROLL_LOCK				0x47
class HIDReportParser
{
public:
	virtual void Parse(uint32_t len, uint8_t *buf,USBHostKeyboard *port) = 0;
};
/**
 * \class KeyboardReportParser definition.
 */
class KeyboardReportParser : public HIDReportParser
{
	static const uint8_t numKeys[];
	static const uint8_t symKeysUp[];
	static const uint8_t symKeysLo[];
	static const uint8_t padKeys[];

protected:
	union
	{
		KBDINFO		kbdInfo;
		uint8_t		bInfo[sizeof(KBDINFO)];
	}	prevState;

	union
	{
		KBDLEDS		kbdLeds;
		uint8_t		bLeds;
	}	kbdLockingKeys;

	

public:
	KeyboardReportParser() { kbdLockingKeys.bLeds = 0; };

	virtual void Parse(uint32_t len, uint8_t *buf,USBHostKeyboard *port);
	uint8_t OemToAscii(uint8_t mod, uint8_t key);
	uint8_t OemToABC80(uint8_t mod, uint8_t key);
protected:
	uint8_t HandleLockingKeys(uint8_t key,USBHostKeyboard *port);

	virtual void OnKeyDown	(uint8_t mod, uint8_t key)	{};
	virtual void OnKeyUp	(uint8_t mod, uint8_t key)	{};
};


#endif /* HIDBOOT_H_INCLUDED */
