Old fashion design for PC-104
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

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 15/01/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: EventPrinter.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of EventPrinter.cpp
  13. //----------------------------------------------------------------------------
  14. // EventPrinter.cpp: implementation of the CEventPrinter class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "chicago.h"
  19. #include "EventPrinter.h"
  20. #include "winspool.h"
  21. #include "..\..\LgLib\Version.h"
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char THIS_FILE[]=__FILE__;
  25. #define new DEBUG_NEW
  26. #endif
  27. //////////////////////////////////////////////////////////////////////
  28. // Construction/Destruction
  29. //////////////////////////////////////////////////////////////////////
  30. CEventPrinter::CEventPrinter()
  31. {
  32. m_pSensorArray = NULL;
  33. m_hEventCommand = NULL;
  34. m_hEventQuit = NULL;
  35. m_hEventBusy = NULL;
  36. m_hEventFile = NULL;
  37. m_hThread = NULL;
  38. // m_bFileInPrint = FALSE;
  39. m_bDocStart = FALSE;
  40. m_bPageStart = FALSE;
  41. m_nFontHeight = 0;
  42. m_nLineCount = 0;
  43. m_hFile = INVALID_HANDLE_VALUE;
  44. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventPrinter::CEventPrinter") );
  45. }
  46. CEventPrinter::~CEventPrinter()
  47. {
  48. // Clear();
  49. ::SetEvent( m_hEventQuit );
  50. ::WaitForSingleObject( m_hThread, INFINITE );
  51. ::CloseHandle( m_hThread );
  52. ::CloseHandle( m_hEventQuit );
  53. ::CloseHandle( m_hEventBusy );
  54. ::CloseHandle( m_hEventFile );
  55. ::CloseHandle( m_hEventCommand );
  56. m_PrintDC.DeleteDC();
  57. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CEventPrinter::~CEventPrinter") );
  58. }
  59. BOOL CEventPrinter::Create(CString Driver, CString Device)
  60. {
  61. m_Device = Device;
  62. m_Driver = Driver;
  63. m_hEventCommand = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  64. m_hEventQuit = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  65. m_hEventBusy = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  66. m_hEventFile = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  67. if( !m_PrintDC.CreateDC( m_Driver, m_Device, NULL, NULL ) )
  68. {
  69. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer not present" );
  70. return FALSE;
  71. }
  72. CString str;
  73. str = "Printer <" + Device + "> found";
  74. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, RGB(0,0xFF,0), (LPSTR)(LPCTSTR)str );
  75. DWORD dwThreadId;
  76. m_hThread = ::CreateThread( NULL, 0UL, ThreadProcEventPrinter, this, 0UL, &dwThreadId );
  77. if( m_hThread )
  78. return TRUE;
  79. return FALSE;
  80. }
  81. BOOL CEventPrinter::PrintEvent(EVENT &event)
  82. {
  83. m_EventQueue.PostEvent( event );
  84. ::SetEvent( m_hEventBusy );
  85. ::SetEvent( m_hEventCommand );
  86. return TRUE;
  87. }
  88. BOOL CEventPrinter::PrintFileStart( LPSTR pFileName, EVENTFILTER filter )
  89. {
  90. if( !IsPrintFile() )
  91. {
  92. m_hFile = (HANDLE)::CreateFile( pFileName, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ,
  93. NULL, OPEN_EXISTING, 0, NULL );
  94. if( m_hFile != (HANDLE)INVALID_HANDLE_VALUE )
  95. {
  96. m_Filter = filter;
  97. m_CommandQueue.PostEvent( LGS_EVENT_PRINTER, 0, 1, 0, 0, 0 );
  98. ::SetEvent( m_hEventFile );
  99. ::SetEvent( m_hEventBusy );
  100. ::SetEvent( m_hEventCommand );
  101. return TRUE;
  102. }
  103. }
  104. return FALSE;
  105. }
  106. BOOL CEventPrinter::PrintFileAbort()
  107. {
  108. if( IsPrintFile() )
  109. {
  110. m_CommandQueue.PostEvent( LGS_EVENT_PRINTER, 0, 0, 0, 0, 0 );
  111. ::SetEvent( m_hEventFile );
  112. ::SetEvent( m_hEventBusy );
  113. ::SetEvent( m_hEventCommand );
  114. return TRUE;
  115. }
  116. return FALSE;
  117. }
  118. //DEL BOOL CEventPrinter::PrintEventContinue()
  119. //DEL {
  120. //DEL if( m_bFileInPrint ) return FALSE;
  121. //DEL
  122. //DEL ::SetEvent( m_hEventPost );
  123. //DEL
  124. //DEL return TRUE;
  125. //DEL }
  126. DWORD CEventPrinter::ThreadProc()
  127. {
  128. /* if( !m_PrintDC.CreateDC( m_Driver, m_Device, NULL, NULL ) )
  129. {
  130. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, 0, NULL );
  131. return 0;
  132. }
  133. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, 0, NULL );
  134. */
  135. m_bDocStart = FALSE;
  136. m_bPageStart = FALSE;
  137. m_nFontHeight = m_PrintDC.GetTextExtent( CString("Gg") ).cy;
  138. m_nFontMargin = m_PrintDC.GetTextExtent( CString("WWWWW") ).cx;
  139. m_nLineCount = 0;
  140. EVENT event;
  141. HANDLE hEvents[] = { m_hEventQuit, m_hEventCommand };
  142. BOOL bFileInPrint = FALSE;
  143. BOOL bError = FALSE;
  144. while( TRUE )
  145. {
  146. switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, 10000 ) )
  147. {
  148. case WAIT_OBJECT_0 + 0:
  149. goto THREAD_EXIT;
  150. case WAIT_TIMEOUT:
  151. if( m_bPageStart )
  152. if( !PageEnd() )
  153. {
  154. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
  155. goto THREAD_EXIT;
  156. }
  157. break;
  158. case WAIT_OBJECT_0 + 1:
  159. if( m_CommandQueue.GetEvent( event ) )
  160. {
  161. if( event.id == LGS_EVENT_PRINTER )
  162. {
  163. if( event.data16 )
  164. bFileInPrint = TRUE;
  165. else
  166. {
  167. if( m_hFile != INVALID_HANDLE_VALUE )
  168. {
  169. ::CloseHandle( m_hFile );
  170. m_hFile = INVALID_HANDLE_VALUE;
  171. }
  172. ::ResetEvent( m_hEventFile );
  173. bFileInPrint = FALSE;
  174. if( !PageEnd() )
  175. {
  176. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
  177. goto THREAD_EXIT;
  178. }
  179. else
  180. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, RGB(0,0xFF,0), "report printing aborted" );
  181. }
  182. }
  183. }
  184. if( bFileInPrint )
  185. {
  186. DWORD dwRead = 0;
  187. if( ReadFile( m_hFile, &event, sizeof( event ), &dwRead, NULL ) && dwRead == sizeof( event ) )
  188. {
  189. if( m_Filter.Pass( &event ) )
  190. {
  191. if( !OutLine( &event ) )
  192. {
  193. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error 0" );
  194. if( m_hFile != INVALID_HANDLE_VALUE )
  195. {
  196. ::CloseHandle( m_hFile );
  197. m_hFile = INVALID_HANDLE_VALUE;
  198. }
  199. goto THREAD_EXIT;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. if( m_hFile != INVALID_HANDLE_VALUE )
  206. {
  207. ::CloseHandle( m_hFile );
  208. m_hFile = INVALID_HANDLE_VALUE;
  209. }
  210. ::ResetEvent( m_hEventFile );
  211. bFileInPrint = FALSE;
  212. if( !PageEnd() )
  213. {
  214. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
  215. goto THREAD_EXIT;
  216. }
  217. else
  218. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, TRUE, 0, 0, RGB(0,0xFF,0), "report printing performed" );
  219. }
  220. ::SetEvent( m_hEventCommand );
  221. break;
  222. }
  223. else
  224. if( m_EventQueue.GetEvent( event ) )
  225. {
  226. if( !OutLine( &event ) )
  227. {
  228. m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PRINTER, FALSE, 0, 0, RGB(0xFF,0,0), "printer error" );
  229. goto THREAD_EXIT;
  230. }
  231. ::SetEvent( m_hEventCommand );
  232. break;
  233. }
  234. ::ResetEvent( m_hEventBusy );
  235. break;
  236. }
  237. }
  238. THREAD_EXIT:
  239. if( m_bPageStart )
  240. m_PrintDC.EndPage( );
  241. if( m_bDocStart )
  242. m_PrintDC.EndDoc();
  243. TRACE("<EventPrinter> thread: ");
  244. return 0UL;
  245. }
  246. void CEventPrinter::SetSensorArray(CSensorArray *pSensorArray)
  247. {
  248. m_pSensorArray = pSensorArray;
  249. }
  250. //DEL void CEventPrinter::Clear()
  251. //DEL {
  252. //DEL if( m_hThread )
  253. //DEL m_EventQueue.Clear();
  254. //DEL }
  255. //DEL BOOL CEventPrinter::PrintFileSuspend()
  256. //DEL {
  257. //DEL if( m_bFileInPrint && !m_bFileSuspend )
  258. //DEL {
  259. //DEL m_bFileSuspend = TRUE;
  260. //DEL ::SetEvent( m_hEventFile );
  261. //DEL return TRUE;
  262. //DEL }
  263. //DEL return FALSE;
  264. //DEL }
  265. //DEL BOOL CEventPrinter::PrintFileContinue()
  266. //DEL {
  267. //DEL if( m_bFileInPrint && m_bFileSuspend )
  268. //DEL {
  269. //DEL m_bFileSuspend = FALSE;
  270. //DEL ::SetEvent( m_hEventFile );
  271. //DEL return TRUE;
  272. //DEL }
  273. //DEL return FALSE;
  274. //DEL }
  275. BOOL CEventPrinter::OutLine( LPEVENT pEvent )
  276. {
  277. CString str;
  278. GetEventString( pEvent, str );
  279. if( m_nLineCount == 0 )
  280. {
  281. if( !m_bDocStart )
  282. {
  283. DOCINFO di;
  284. di.cbSize = sizeof( DOCINFO );
  285. di.fwType = 0;
  286. di.lpszDatatype = "raw";
  287. di.lpszDocName = "text";
  288. di.lpszOutput = NULL;
  289. if( m_PrintDC.StartDoc( &di ) == -1 ) return FALSE;
  290. m_bDocStart = TRUE;
  291. }
  292. if( !m_PrintDC.StartPage() ) return FALSE;
  293. m_bPageStart = TRUE;
  294. m_nLineCount = 3;
  295. }
  296. if( !m_PrintDC.TextOut( m_nFontMargin, m_nLineCount * m_nFontHeight, str ) )
  297. return FALSE;
  298. m_nLineCount++;
  299. if( m_nLineCount > 50 )
  300. {
  301. m_nLineCount = 0;
  302. if( m_PrintDC.EndPage() < 0 ) return FALSE;
  303. m_bPageStart = FALSE;
  304. }
  305. return TRUE;
  306. }
  307. BOOL CEventPrinter::IsPrintFile()
  308. {
  309. return ::WaitForSingleObject( m_hEventFile, 0 ) == WAIT_OBJECT_0;
  310. }
  311. BOOL CEventPrinter::IsBusy()
  312. {
  313. return ::WaitForSingleObject( m_hEventBusy, 0 ) == WAIT_OBJECT_0;
  314. }
  315. BOOL CEventPrinter::PageEnd()
  316. {
  317. if( m_bPageStart )
  318. {
  319. m_nLineCount = 0;
  320. if( m_PrintDC.EndPage() < 0 ) return FALSE;
  321. m_bPageStart = FALSE;
  322. }
  323. return TRUE;
  324. }