You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
5.0 KiB
212 lines
5.0 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: EventQueue.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of EventQueue.cpp
|
|
//----------------------------------------------------------------------------
|
|
// EventQueue.cpp: implementation of the CEventQueue class.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
|
//#include "chicago.h"
|
|
#include "AuxLib.h"
|
|
#include "..\..\LgLib\stdafx.h"
|
|
#include "EventQueue.h"
|
|
#include "..\..\LgLib\Version.h"
|
|
/*
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
*/
|
|
//////////////////////////////////////////////////////////////////////
|
|
// Construction/Destruction
|
|
//////////////////////////////////////////////////////////////////////
|
|
CEventQueue::CEventQueue( int nBufferSize )
|
|
{
|
|
m_Begin = 0;
|
|
m_End = 0;
|
|
m_nBufferSize = nBufferSize;
|
|
m_pBuffer = new EVENT[ m_nBufferSize ];
|
|
m_hMutex = ::CreateMutex( NULL, FALSE, NULL );
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventQueue::CEventQueue") );
|
|
}
|
|
|
|
CEventQueue::~CEventQueue()
|
|
{
|
|
::CloseHandle( m_hMutex );
|
|
delete [] m_pBuffer;
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventQueue::~CEventQueue") );
|
|
}
|
|
|
|
BOOL CEventQueue::PostEvent( BYTE event, BYTE data8, WORD data16, DWORD data32, COLORREF color, LPSTR pFormat, ... )
|
|
{
|
|
::WaitForSingleObject( m_hMutex, INFINITE );
|
|
|
|
int newEnd = ( m_End + 1 ) % m_nBufferSize;
|
|
if( m_Begin == newEnd )
|
|
{
|
|
::ReleaseMutex( m_hMutex );
|
|
return FALSE;
|
|
}
|
|
|
|
m_pBuffer[ m_End ].id = event;
|
|
m_pBuffer[ m_End ].data8 = data8;
|
|
m_pBuffer[ m_End ].data16 = data16;
|
|
m_pBuffer[ m_End ].data32 = data32;
|
|
m_pBuffer[ m_End ].color = color;
|
|
|
|
// place text
|
|
if( pFormat )
|
|
{
|
|
const int nMaxCount = sizeof(m_pBuffer[ m_End ].pText);
|
|
|
|
va_list pMarker;
|
|
va_start( pMarker, pFormat );
|
|
|
|
int iCount = _vsntprintf( m_pBuffer[ m_End ].pText, nMaxCount, pFormat, pMarker );
|
|
if( iCount < 0 ) iCount = nMaxCount;
|
|
va_end( pMarker );
|
|
|
|
m_pBuffer[m_End].pText[iCount] = 0;
|
|
}
|
|
else
|
|
m_pBuffer[m_End].pText[0] = 0;
|
|
|
|
SYSTEMTIME st;
|
|
::GetLocalTime( &st );
|
|
::SystemTimeToFileTime( &st, (LPFILETIME)&m_pBuffer[ m_End ].time );
|
|
|
|
m_End = newEnd;
|
|
|
|
::ReleaseMutex( m_hMutex );
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CEventQueue::PostEvent( EVENT &event )
|
|
{
|
|
::WaitForSingleObject( m_hMutex, INFINITE );
|
|
|
|
int newEnd = ( m_End + 1 ) % m_nBufferSize;
|
|
if( m_Begin == newEnd )
|
|
{
|
|
::ReleaseMutex( m_hMutex );
|
|
return FALSE;
|
|
}
|
|
|
|
m_pBuffer[ m_End ] = event;
|
|
|
|
m_End = newEnd;
|
|
|
|
::ReleaseMutex( m_hMutex );
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CEventQueue::PostEventImageCapture( CSensor* pSensor, HBITMAP hBitmap )
|
|
{
|
|
EVENT event;
|
|
BuildEventImageCapture( &event, pSensor, hBitmap );
|
|
return PostEvent( event );
|
|
}
|
|
|
|
/*
|
|
|
|
//try to optimize queue :-)
|
|
|
|
BOOL CEventQueue::PostEvent( EVENT &event )
|
|
{
|
|
::WaitForSingleObject( m_hMutex, INFINITE );
|
|
|
|
if( m_nCount + 1 > m_nSize )
|
|
{
|
|
int nSize = m_nSize + 128;
|
|
LPEVENT pBuffer = new EVENT[ nSize ];
|
|
::CopyMemory( pBuffer, m_pBuffer + m_nPos, sizeof(EVENT) * ( m_nSize - m_nPos ) );
|
|
::CopyMemory( pBuffer + m_nSize - m_nPos, m_pBuffer + m_nPos, sizeof(EVENT) * ( m_nSize - m_nPos ) );
|
|
delete [] m_pBuffer;
|
|
m_nSize = nSize;
|
|
m_pBuffer = pBuffer;
|
|
m_nPos = 0;
|
|
}
|
|
|
|
m_pBuffer[ m_nPos ] = event;
|
|
m_nPos = ( m_nPos + 1 ) % m_nSize;
|
|
m_nCount++;
|
|
|
|
::ReleaseMutex( m_hMutex );
|
|
return FALSE;
|
|
}
|
|
*/
|
|
BOOL CEventQueue::GetEvent(EVENT &event)
|
|
{
|
|
::WaitForSingleObject( m_hMutex, INFINITE );
|
|
|
|
if( m_Begin == m_End )
|
|
{
|
|
::ReleaseMutex( m_hMutex );
|
|
return FALSE;
|
|
}
|
|
|
|
event = m_pBuffer[ m_Begin ];
|
|
m_Begin = ( m_Begin + 1 ) % m_nBufferSize;
|
|
|
|
::ReleaseMutex( m_hMutex );
|
|
return TRUE;
|
|
}
|
|
|
|
void CEventQueue::Clear()
|
|
{
|
|
::WaitForSingleObject( m_hMutex, INFINITE );
|
|
m_Begin = m_End;
|
|
::ReleaseMutex( m_hMutex );
|
|
}
|
|
|
|
BOOL CEventQueue::IsEmpty()
|
|
{
|
|
::WaitForSingleObject( m_hMutex, INFINITE );
|
|
BOOL bIsEmpty = ( m_Begin == m_End );
|
|
::ReleaseMutex( m_hMutex );
|
|
return bIsEmpty;
|
|
}
|
|
|
|
void GetEventString( LPEVENT pEvent, CString& str )
|
|
{
|
|
SYSTEMTIME st;
|
|
::FileTimeToSystemTime( (LPFILETIME)&pEvent->time, &st );
|
|
str.Format( "%02hu/%02hu/%04hu %02hu:%02hu:%02hu %s",
|
|
st.wMonth, st.wDay, st.wYear,
|
|
st.wHour, st.wMinute, st.wSecond, pEvent->pText );
|
|
}
|
|
|
|
void CEventQueue::BuildEventTime( PLARGE_INTEGER pTime )
|
|
{
|
|
FILETIME SystemTime;
|
|
|
|
ASSERT(pTime);
|
|
|
|
::GetSystemTimeAsFileTime( &SystemTime );
|
|
|
|
::FileTimeToLocalFileTime( &SystemTime, (LPFILETIME)pTime );
|
|
}
|
|
|
|
void CEventQueue::BuildEventImageCapture( PEVENT pEvent, CSensor* pSensor, HBITMAP hBitmap )
|
|
{
|
|
BuildEventTime( &pEvent->time );
|
|
|
|
pEvent->id = LGS_EVENT_IMAGE_CAPTURE;
|
|
|
|
pEvent->pSensor = pSensor;
|
|
|
|
pEvent->hBitmap = hBitmap;
|
|
}
|