
#error "Não está compilando!!!"

#include "BCEventLoopDirectFB.h"


namespace BAL
{

BCEventLoopDirectFB::BCEventLoopDirectFB()
	: m_isInitialized(false) // <--- true né ???
	, m_event(0)
{
	// Loop de eventos do DirectFB já chega inicializado neste instante ????
	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "BCEventLoopDirectFB()");
}

BCEventLoopDirectFB::~BCEventLoopDirectFB()
{
	// Não sou eu quem vai destruir o loop de eventos !!!
	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "~BCEventLoopDirectFB()");
}

bool BCEventLoopDirectFB::isInitialized() const
{
	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "isInitialized()");
	return m_isInitialized;
}

void BCEventLoopDirectFB::quit()
{
	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "quit()");
	BCWindowEvent* aWindowEvent = new BCWindowEvent(BIWindowEvent::QUIT);
	PushEvent(aWindowEvent);
}

bool BCEventLoopDirectFB::WaitEvent(BAL::BIEvent*& evt)
{
	IDirectFB *dfb;
	DFBResult err;
// DFBInputEvent evt;

	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "WaitEvent()");

	err = dfb->CreateInputEventBuffer( dfb, DICAPS_ALL, DFB_FALSE, &m_eventBuffer );
	if (err != DFB_OK) {
		 DirectFBError( "CreateInputEventBuffer failed", err );
		 dfb->Release( dfb );
		 return 1;
	}

	while (m_eventBuffer->GetEvent( m_eventBuffer, DFB_EVENT(&evt) ) == DFB_OK) {
	  if ( evt.type == DIET_AXISMOTION){
		  if (evt.flags & DIEF_AXISABS) {
			  switch (evt.axis) {
			  case DIAI_X:
				  mouse_x = evt.axisabs;
				  break;
			  case DIAI_Y:
				  mouse_y = evt.axisabs;
				  break;
			 default:
				  break;
			 }
		  }
	  }
	  if ( evt.type == DIET_BUTTONPRESS ){
		  switch(++touch){
			  case 1: //save first touchscreen position
				  tx1=mouse_x;
				  ty1=mouse_y;
				  break;
			  case 2://build new calibration values and quit
			  {   
				  float dx = ((float)mouse_x-tx1)/(rightx-leftx);
				  float dy = ((float)mouse_y-ty1)/(bottomy-topy);
				  quit = 1;
				  break;
			  }
		  }
	  }
		}
	// m_eventBuffer->Release( m_eventBuffer );
}

bool BCEventLoopDirectFB::PushEvent(BAL::BIEvent* evt)
{
	ASSERT(evt->queryIsTimerEvent() || evt->queryIsWindowEvent() || evt->queryIsKeyboardEvent() );

	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "PushEvent()");

	if (!m_isInitialized)
		return false;

	if (!evt)
		return false;

	m_event.append(evt);

	return true;
}

#ifdef OWB_CONSOLE_INPUT
BIEvent* BC::BCEventLoopDirectFB::CreateEventFromStdin(int key)
{
	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "CreateEventFromStdin()");
}
#endif

BAL::BIEvent* BCEventLoopDirectFB::CreateEventFromDirectFBEvent(const DFBEvent& evt)
{
	DBGML(MODULE_EVENTS, LEVEL_CRITICAL, "CreateEventFromDirectFBEvent()");
}

}
