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.
370 lines
9.0 KiB
370 lines
9.0 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 15/01/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: EventPrinter.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of EventPrinter.cpp
|
|
//----------------------------------------------------------------------------
|
|
// EventPrinter.cpp: implementation of the CEventPrinter class.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
|
#include "chicago.h"
|
|
#include "EventPrinter.h"
|
|
#include "winspool.h"
|
|
#include "..\..\LgLib\Version.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// Construction/Destruction
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
CEventPrinter::CEventPrinter()
|
|
{
|
|
m_pSensorArray = NULL;
|
|
|
|
m_hEventCommand = NULL;
|
|
m_hEventQuit = NULL;
|
|
m_hEventBusy = NULL;
|
|
m_hEventFile = NULL;
|
|
m_hThread = NULL;
|
|
|
|
// m_bFileInPrint = FALSE;
|
|
m_bDocStart = FALSE;
|
|
m_bPageStart = FALSE;
|
|
m_nFontHeight = 0;
|
|
m_nLineCount = 0;
|
|
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventPrinter::CEventPrinter") );
|
|
}
|
|
|
|
CEventPrinter::~CEventPrinter()
|
|
{
|
|
// Clear();
|
|
::SetEvent( m_hEventQuit );
|
|
::WaitForSingleObject( m_hThread, INFINITE );
|
|
::CloseHandle( m_hThread );
|
|
::CloseHandle( m_hEventQuit );
|
|
::CloseHandle( m_hEventBusy );
|
|
::CloseHandle( m_hEventFile );
|
|
::CloseHandle( m_hEventCommand );
|
|
m_PrintDC.DeleteDC();
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventPrinter::~CEventPrinter") );
|
|
}
|
|
|
|
BOOL CEventPrinter::Create(CString Driver, CString Device)
|
|
{
|
|
m_Device = Device;
|
|
m_Driver = Driver;
|
|
|
|
m_hEventCommand = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
m_hEventQuit = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
m_hEventBusy = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
m_hEventFile = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
|
|
if( !m_PrintDC.CreateDC( m_Driver, m_Device, NULL, NULL ) )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer not present" );
|
|
return FALSE;
|
|
}
|
|
|
|
CString str;
|
|
str = "Printer <" + Device + "> found";
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, RGB(0,0xFF,0), (LPSTR)(LPCTSTR)str );
|
|
|
|
DWORD dwThreadId;
|
|
m_hThread = ::CreateThread( NULL, 0UL, ThreadProcEventPrinter, this, 0UL, &dwThreadId );
|
|
if( m_hThread )
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
BOOL CEventPrinter::PrintEvent(EVENT &event)
|
|
{
|
|
m_EventQueue.PostEvent( event );
|
|
::SetEvent( m_hEventBusy );
|
|
::SetEvent( m_hEventCommand );
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CEventPrinter::PrintFileStart( LPSTR pFileName, EVENTFILTER filter )
|
|
{
|
|
if( !IsPrintFile() )
|
|
{
|
|
m_hFile = (HANDLE)::CreateFile( pFileName, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ,
|
|
NULL, OPEN_EXISTING, 0, NULL );
|
|
|
|
if( m_hFile != (HANDLE)INVALID_HANDLE_VALUE )
|
|
{
|
|
m_Filter = filter;
|
|
m_CommandQueue.PostEvent( LGS_EVENT_PRINTER, 0, 1, 0, 0, 0 );
|
|
::SetEvent( m_hEventFile );
|
|
::SetEvent( m_hEventBusy );
|
|
::SetEvent( m_hEventCommand );
|
|
return TRUE;
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
BOOL CEventPrinter::PrintFileAbort()
|
|
{
|
|
if( IsPrintFile() )
|
|
{
|
|
m_CommandQueue.PostEvent( LGS_EVENT_PRINTER, 0, 0, 0, 0, 0 );
|
|
::SetEvent( m_hEventFile );
|
|
::SetEvent( m_hEventBusy );
|
|
::SetEvent( m_hEventCommand );
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
//DEL BOOL CEventPrinter::PrintEventContinue()
|
|
//DEL {
|
|
//DEL if( m_bFileInPrint ) return FALSE;
|
|
//DEL
|
|
//DEL ::SetEvent( m_hEventPost );
|
|
//DEL
|
|
//DEL return TRUE;
|
|
//DEL }
|
|
|
|
DWORD CEventPrinter::ThreadProc()
|
|
{
|
|
/* if( !m_PrintDC.CreateDC( m_Driver, m_Device, NULL, NULL ) )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, 0, NULL );
|
|
return 0;
|
|
}
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, 0, NULL );
|
|
*/
|
|
m_bDocStart = FALSE;
|
|
m_bPageStart = FALSE;
|
|
m_nFontHeight = m_PrintDC.GetTextExtent( CString("Gg") ).cy;
|
|
m_nFontMargin = m_PrintDC.GetTextExtent( CString("WWWWW") ).cx;
|
|
m_nLineCount = 0;
|
|
|
|
EVENT event;
|
|
HANDLE hEvents[] = { m_hEventQuit, m_hEventCommand };
|
|
|
|
BOOL bFileInPrint = FALSE;
|
|
BOOL bError = FALSE;
|
|
|
|
while( TRUE )
|
|
{
|
|
switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, 10000 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0:
|
|
goto THREAD_EXIT;
|
|
|
|
case WAIT_TIMEOUT:
|
|
if( m_bPageStart )
|
|
if( !PageEnd() )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
|
|
goto THREAD_EXIT;
|
|
}
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
if( m_CommandQueue.GetEvent( event ) )
|
|
{
|
|
if( event.id == LGS_EVENT_PRINTER )
|
|
{
|
|
if( event.data16 )
|
|
bFileInPrint = TRUE;
|
|
else
|
|
{
|
|
if( m_hFile != INVALID_HANDLE_VALUE )
|
|
{
|
|
::CloseHandle( m_hFile );
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
|
}
|
|
::ResetEvent( m_hEventFile );
|
|
bFileInPrint = FALSE;
|
|
if( !PageEnd() )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
|
|
goto THREAD_EXIT;
|
|
}
|
|
else
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, RGB(0,0xFF,0), "report printing aborted" );
|
|
}
|
|
}
|
|
}
|
|
|
|
if( bFileInPrint )
|
|
{
|
|
DWORD dwRead = 0;
|
|
if( ReadFile( m_hFile, &event, sizeof( event ), &dwRead, NULL ) && dwRead == sizeof( event ) )
|
|
{
|
|
if( m_Filter.Pass( &event ) )
|
|
{
|
|
if( !OutLine( &event ) )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error 0" );
|
|
if( m_hFile != INVALID_HANDLE_VALUE )
|
|
{
|
|
::CloseHandle( m_hFile );
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
|
}
|
|
goto THREAD_EXIT;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if( m_hFile != INVALID_HANDLE_VALUE )
|
|
{
|
|
::CloseHandle( m_hFile );
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
|
}
|
|
::ResetEvent( m_hEventFile );
|
|
bFileInPrint = FALSE;
|
|
if( !PageEnd() )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
|
|
goto THREAD_EXIT;
|
|
}
|
|
else
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, RGB(0,0xFF,0), "report printing performed" );
|
|
}
|
|
::SetEvent( m_hEventCommand );
|
|
break;
|
|
}
|
|
else
|
|
if( m_EventQueue.GetEvent( event ) )
|
|
{
|
|
if( !OutLine( &event ) )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
|
|
goto THREAD_EXIT;
|
|
}
|
|
::SetEvent( m_hEventCommand );
|
|
break;
|
|
}
|
|
|
|
::ResetEvent( m_hEventBusy );
|
|
break;
|
|
}
|
|
}
|
|
|
|
THREAD_EXIT:
|
|
if( m_bPageStart )
|
|
m_PrintDC.EndPage( );
|
|
if( m_bDocStart )
|
|
m_PrintDC.EndDoc();
|
|
|
|
TRACE("<EventPrinter> thread: ");
|
|
|
|
return 0UL;
|
|
}
|
|
|
|
void CEventPrinter::SetSensorArray(CSensorArray *pSensorArray)
|
|
{
|
|
m_pSensorArray = pSensorArray;
|
|
}
|
|
|
|
//DEL void CEventPrinter::Clear()
|
|
//DEL {
|
|
//DEL if( m_hThread )
|
|
//DEL m_EventQueue.Clear();
|
|
//DEL }
|
|
|
|
//DEL BOOL CEventPrinter::PrintFileSuspend()
|
|
//DEL {
|
|
//DEL if( m_bFileInPrint && !m_bFileSuspend )
|
|
//DEL {
|
|
//DEL m_bFileSuspend = TRUE;
|
|
//DEL ::SetEvent( m_hEventFile );
|
|
//DEL return TRUE;
|
|
//DEL }
|
|
//DEL return FALSE;
|
|
//DEL }
|
|
|
|
//DEL BOOL CEventPrinter::PrintFileContinue()
|
|
//DEL {
|
|
//DEL if( m_bFileInPrint && m_bFileSuspend )
|
|
//DEL {
|
|
//DEL m_bFileSuspend = FALSE;
|
|
//DEL ::SetEvent( m_hEventFile );
|
|
//DEL return TRUE;
|
|
//DEL }
|
|
//DEL return FALSE;
|
|
//DEL }
|
|
|
|
BOOL CEventPrinter::OutLine( LPEVENT pEvent )
|
|
{
|
|
CString str;
|
|
|
|
GetEventString( pEvent, str );
|
|
|
|
if( m_nLineCount == 0 )
|
|
{
|
|
if( !m_bDocStart )
|
|
{
|
|
DOCINFO di;
|
|
di.cbSize = sizeof( DOCINFO );
|
|
di.fwType = 0;
|
|
di.lpszDatatype = "raw";
|
|
di.lpszDocName = "text";
|
|
di.lpszOutput = NULL;
|
|
if( m_PrintDC.StartDoc( &di ) == -1 ) return FALSE;
|
|
m_bDocStart = TRUE;
|
|
}
|
|
if( !m_PrintDC.StartPage() ) return FALSE;
|
|
m_bPageStart = TRUE;
|
|
m_nLineCount = 3;
|
|
}
|
|
|
|
if( !m_PrintDC.TextOut( m_nFontMargin, m_nLineCount * m_nFontHeight, str ) )
|
|
return FALSE;
|
|
|
|
m_nLineCount++;
|
|
if( m_nLineCount > 50 )
|
|
{
|
|
m_nLineCount = 0;
|
|
if( m_PrintDC.EndPage() < 0 ) return FALSE;
|
|
m_bPageStart = FALSE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CEventPrinter::IsPrintFile()
|
|
{
|
|
return ::WaitForSingleObject( m_hEventFile, 0 ) == WAIT_OBJECT_0;
|
|
}
|
|
|
|
BOOL CEventPrinter::IsBusy()
|
|
{
|
|
return ::WaitForSingleObject( m_hEventBusy, 0 ) == WAIT_OBJECT_0;
|
|
}
|
|
|
|
BOOL CEventPrinter::PageEnd()
|
|
{
|
|
if( m_bPageStart )
|
|
{
|
|
m_nLineCount = 0;
|
|
if( m_PrintDC.EndPage() < 0 ) return FALSE;
|
|
m_bPageStart = FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|