//-----------------------------------------------------------------------------
#include <apps/Common/wxAbstraction.h>
#include "EventThread.h"
#include "PropViewFrame.h"
#include <wx/event.h>

using namespace mvIMPACT::acquire;

//-----------------------------------------------------------------------------
EventThread::EventThread( Device* pDev, EventSubSystem* pESS, wxWindow* pParentWindow, bool boSendEvent )
  : wxThread(wxTHREAD_JOINABLE), m_pDevice(pDev), m_pEventSubSystem(pESS), m_boSendEvent(boSendEvent),
    m_pParentWindowEventHandler(pParentWindow->GetEventHandler()), m_parentWindowID(pParentWindow->GetId()) {}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
void* EventThread::Entry( void )
//-----------------------------------------------------------------------------
{
	while( !TestDestroy() )
	{
		EventWaitResults waitResults = m_pEventSubSystem->waitFor( 500, detAll );
		if( waitResults.errorCode() == DMR_NO_ERROR )
		{
			if( waitResults.resultMask() != detNone )
			{
				Event* pEvent = m_pEventSubSystem->getEventByType( waitResults.resultMask() );
				pEvent->getData();
				if( m_boSendEvent )
				{
					wxCommandEvent e( eventNotified, m_parentWindowID );
					ConvertedString s(pEvent->type.readS());
					e.SetString( s );
					e.SetClientData( m_pDevice );
					wxPostEvent( m_pParentWindowEventHandler, e );
				}
			}
		}
	}
	return 0;
}
