/*---------------------------------------------------------------------------------
 libBAG  Copyright (C) 2010 - 2010		
  BassAceGold - <BassAceGold@gmail.com>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you
     must not claim that you wrote the original software. If you use
     this software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.
	 
  2. Altered source versions must be plainly marked as such, and
     must not be misrepresented as being the original software.
	 
  3. Restricting the usage of or portions of code in this library
     from other software developers and or content creators
     is strictly prohibited in applications with software creation capabilities
     and or with the intent to profit from said restrictions.
	 
  4. This notice may not be removed or altered from any source
     distribution.
---------------------------------------------------------------------------------*/

#include "input.h"

_BAG_Pad Pad;
_BAG_Stylus Stylus;
_BAG_Lid Lid;

#define _BAG_UPDATEPAD(pad, var) do{\
	Pad.pad.A = (var & KEY_A);\
	Pad.pad.B = (var & KEY_B)>>1;\
	Pad.pad.X = (var & KEY_X)>>10; \
	Pad.pad.Y = (var & KEY_Y)>>11;\
	Pad.pad.Up = (var & KEY_UP)>>6;\
	Pad.pad.Down = (var & KEY_DOWN)>>7;\
	Pad.pad.Left = (var & KEY_LEFT)>>5;\
	Pad.pad.Right = (var & KEY_RIGHT)>>4;\
	Pad.pad.Start = (var & KEY_START)>>3;\
	Pad.pad.Select = (var & KEY_SELECT)>>2;\
	Pad.pad.L = (var & KEY_L)>>9;\
	Pad.pad.R = (var & KEY_R)>>8;\
}while(0)

#define _BAG_UPDATESTYLUS(type, var) do{\
	type = (var & KEY_TOUCH)>>12;\
}while(0)

#define _BAG_UPDATELID(type, var) do{\
    type=(var & KEY_LID)>>13;\
}while(0)


unsigned short _BAG_CompletePad, _BAG_ExPad, _BAG_TempPad;

void BAG_Input_ResetStylusDblClick(void){
	Stylus.Uptime = 0;
	Stylus.Downtime = 0;
}

void BAG_Input_SetDblClickTime(int time){
	Stylus.ClickTime = time;
}

int BAG_Input_StylusDblClick(void){
	if(Stylus.Uptime++ < Stylus.ClickTime){
		Stylus.Downtime += Stylus.Newpress;
		if(Stylus.Downtime > 1){
			BAG_Input_ResetStylusDblClick();
			return 1;
		}
	}
	else
		BAG_Input_ResetStylusDblClick();

	return 0;
}
	
void BAG_Input_UpdateIN(void) {
	_BAG_ExPad = _BAG_CompletePad;
	
	struct key_buf rawin;

	ds2_getrawInput(&rawin);
	_BAG_CompletePad = Pad.key = rawin.key;
	
	
	_BAG_UPDATEPAD(Held, _BAG_CompletePad);
	_BAG_UPDATEPAD(Released, (_BAG_ExPad & (~_BAG_CompletePad)));
	_BAG_UPDATEPAD(Newpress, (_BAG_CompletePad & (~_BAG_ExPad)));
	
	//update stylus
	_BAG_UPDATESTYLUS(Stylus.Held, _BAG_CompletePad);
	_BAG_UPDATESTYLUS(Stylus.Released, (_BAG_ExPad & (~_BAG_CompletePad)));
	_BAG_UPDATESTYLUS(Stylus.Newpress, (_BAG_CompletePad & (~_BAG_ExPad)));	
	Stylus.X = rawin.x;
	Stylus.Y = rawin.y;
	
	Stylus.DblClick = BAG_Input_StylusDblClick();
    
    //update lid
	_BAG_UPDATELID(Lid.Shut, _BAG_CompletePad);
    _BAG_UPDATELID(Lid.Opened, (_BAG_ExPad & (~_BAG_CompletePad)));
    _BAG_UPDATELID(Lid.Closed, (_BAG_CompletePad & (~_BAG_ExPad)));
}


