⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tramousestub.cpp

📁 YLP270的Windows CE5.0 bsp源码。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
   TraMouseStub.cpp   Created April 7 1997 (as tchstub.c)
   Modified for use with the mouse driver Aug 15 2003
--*/

// NOTE - this code was ported from the touch driver code, thus it may say "touch" in places where we 
//  really mean "mouse". 

// Debug guide:  These are the debugging switches used in this driver code.
//
// WRITE_STATISTICSXXX - these functions collect points, and every so often they write them to a log file
//  This is a good way to find point leaks in case the driver does not sync with the Transriber application
//



// Basic architecture:
// The points come in through CgrCallback.  
//  If Transcriber is not running, then the points are returned to the mouse driver.
//  Otherwise are then placed on the inner (internal to the mouse driver) queue (Q2) by SendPtToSelf.
//
// Points are removed from Q2 by the mouse driver thread (MouseInnerThread), which passes them to
//  StubCallback2.
//
// StubCallback2 checks to see where the point is, checks with Transcriber about whether the point should
//  be returned to the system.  If so, the point is turned back into a mouse_event.
//  If Transcriber.exe wants the point, it is placed on an external queue (Q1) which is in MemoryMappedFile
//  for interprocess communication with Transcriber.exe

// Transcriber.exe then retrieves points from this queue, paints them on the screen, and sends them
//  to the recognizer.


#include "TraMouseStub.h"
#include "tchddi.h"
#include "pegc_def.h"  // Local copy, main file is in the transcriber sources

#ifdef __cplusplus
extern "C"{
#endif 

//#define  WRITE_STATISTICSLOG   0 // just for experimnents
//#define  WRITE_STATISTICSINNER 0 // for debugging only
//#define  WRITE_STATISTICSSNDPT 0 // for debugging only

#if defined (WRITE_STATISTICSLOG) || defined (WRITE_STATISTICS_INNER) || defined (WRITE_STATISTICSSNDPT)
   #define  MAX_WRITES       20
   #define  MAX_STATS        200
#endif

HWND  _hClientWnd    = NULL;
HWND  _hLastTouchFocusWnd    = NULL;
short _iClientFlags  = TABLET_ALL_TO_SYSTEM;
DWORD _dwLastSendedTick  = 0;
DWORD _dwLastPostedToClient = 0;
static TOUCH_QUEUE tq2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static TOUCH_QUEUE* ptq = NULL;
CRITICAL_SECTION Q2CritSect;		// protect the inner queue within the cgrtouch driver
HANDLE	hQ1Mutex;					// protect the outer queue between cgrtouch and app
int		_cProcessNumb = 0;
HANDLE  _hFileMapping = NULL;
LPVOID	_pView = NULL;
HWND*	_phStubWnd = NULL;
HANDLE* _phinstDll = NULL;

static int ints[10] = {0};

static BOOL  bClick=0, bDelay=0; 
static INT   _begX=0, _begY=0;
static DWORD begTick=0;
static bNear = FALSE;
static int   iLastDelayX=0, iLastDelayY=0;
static DWORD dwLastDelayTick=0;

static int _iLastProceedX=0, _iLastProceedY=0, _iLastProceedFlags=0;
static int _iLastPostedX=0, _iLastPostedY=0;
static int _iLastSelfX=-1, _iLastSelfY=-1;
static int _iInRow = 0;
static int _iMinDist = 0;
static int _iMaxDist = 24;
HANDLE g_hTabletEvent = NULL;
HANDLE g_hInnerEvent = NULL;
BOOL _bDeferedDelay = FALSE;

// #pragma data_seg() - @todo needed?  what arg should go here?

DWORD    dwMinPauseBeforeClick         =  1500;
int      iMaxClickDist                 =  23;
int      iMaxDelayDist                 =  18;
DWORD    dwMaxClickTime                =  480;
DWORD    dwMinStartDelayTime           =  480;
DWORD    dwMinInterDelayTime           =  800;
BOOL     _bIgnoreStroke = FALSE;
BOOL     _bIgnoreStroke2 = FALSE;

unsigned char _footprint[12] =
 { 0x40, 0x89, 0x96, 0x19, 0x10, 0x2B, 0x25, 0xF4,
   (CGRTOUCH_MAJOR_VERSION|0xC0), (CGRTOUCH_MINOR_VERSION|0x80), CGRTOUCH_RELEASE, 0x80};


BOOL IsCgrTouchFile(TCHAR *szFileName);
BOOL IsStubWndExist();
DWORD WINAPI MouseWindowThread(DWORD dwParameter);
void StubCallback2(DWORD dwtick0, TOUCH_PANEL_SAMPLE_FLAGS Flags, INT X, INT Y);
BOOL MddTouchPanelEnable(PFN_TOUCH_PANEL_CALLBACK pfnCallback);
void CgrMouseStubInitialize(HANDLE  hinstDll);
BOOL CgrCallback(TOUCH_PANEL_SAMPLE_FLAGS Flags, INT X, INT Y, UINT evfMouse);

// returning true indicates that we want the caller to call DriverMouseHook
BOOL DriverMouseHookInitialize(HANDLE hInstDll)
{
    CgrMouseStubInitialize(hInstDll);
    return TRUE;
}

// if this driver stub wants the point, return TRUE, and send the point.
//  otherwise return FALSE.
// The basic version in the driver will always return false, link with
//  TraMouse to get this version.  This way, we don't interfere with
//  the mouse driver if SYSGEN_TRANSCRIBER is not enabled.
BOOL DriverMouseHook(DWORD dwFlags, DWORD dx, DWORD dy, DWORD dwData)
{
    // TRANSCRIBER - if SYSGEN_TRANSCRIBER is turned on, give transcriber a chance here to 
    //    snag the mouse point.  (transcriber.exe must check for the kbdmouse.dll if touch.dll is not available.)

    // variables needed by transcriber
    static BOOL     bLeftMouseButtonDown = FALSE;
    DWORD           tchFlags = 0;
    BOOL            bTranscriberWantsThisPoint = FALSE;
    // transcriber does not use dwData, so avoid warnings that the varaible is unused
    dwData = dwData;

    bTranscriberWantsThisPoint = FALSE;	
				
    // mouse button goes down, tell transcriber
    if (dwFlags & MOUSEEVENTF_LEFTDOWN)
    {
        tchFlags = TouchSampleIsCalibratedFlag | TouchSampleValidFlag | TouchSampleDownFlag;
        bLeftMouseButtonDown = TRUE;
    }

    // moves while the mouse button is down, send all points
    else if (bLeftMouseButtonDown)
    {
        tchFlags = TouchSampleIsCalibratedFlag | TouchSampleValidFlag | TouchSampleDownFlag | TouchSamplePreviousDownFlag;
    }

    // mouse button goes up, send a prev down flag to tell transcriber that the pen is up.
    if (dwFlags & MOUSEEVENTF_LEFTUP)
    {
        tchFlags = TouchSampleIsCalibratedFlag | TouchSampleValidFlag | TouchSamplePreviousDownFlag;	
        bLeftMouseButtonDown = FALSE;
    }

    // ask transcriber if "pen" is now down, or just came back up
    if (TouchSamplePreviousDownFlag & tchFlags || TouchSampleDownFlag & tchFlags )
    {
        bTranscriberWantsThisPoint = CgrCallback(tchFlags, dx, dy, dwFlags);    	
    }

    // Tell the mouse driver whether or not to process the point
    //  if transcriber does not want the point, it is sent back to mouse_event()
     return bTranscriberWantsThisPoint; 
}


// Look at the queue internal to the touch driver, and send the points to StubCallback2
DWORD WINAPI MouseInnerThread(DWORD dwParameter)
{
	TOUCH_QUEUE *pTouchQ = &tq2;
	int i;
	
	while(1)
	{
        BOOL fDone = FALSE;
		WaitForSingleObject(g_hInnerEvent, INFINITE); //2000);
		ResetEvent(g_hInnerEvent);

		// process all touch queue points
		EnterCriticalSection(&Q2CritSect);
		while(!fDone) 
		{
	    		i = pTouchQ->iGet;
	    		if(i == pTouchQ->iPut) 
	    		{
	    		 	// no more points
	    			fDone = TRUE;
	    		} 
	    		else 
	    		{
	    			int iNext;
	    			// copy the data out of the critical section so that we can release it

					// The ARM compiler optimization appears to be broken on the next 3 lines
	    			DWORD flags = pTouchQ->elems[i][0];
	    			short x = ((pTouchQ->elems[i][1])>>16);
	    			short y = ((pTouchQ->elems[i][1])&0x0FFFF);

	                     // don't hold the critical section during the callback
	                     //  (priority inversion can fail here)
	        		LeaveCriticalSection(&Q2CritSect);

	        		// handle the point data
	    			StubCallback2(GetTickCount(), flags, x, y);

				// update the queue pointer
	    			iNext = i+1;
	    			if(iNext>=MAX_TOUCHQUEUE) 
	    			{
	    				iNext=0;
	    			}

	              	// re-enter the critical section to update the read pointer
	              	// and go back to the top of the loop.
	    			EnterCriticalSection(&Q2CritSect);
	    			pTouchQ->iGet = iNext;
	    		}
		}

		// no more points, release the critical section
		LeaveCriticalSection(&Q2CritSect);
	}
	
	return 0; //just avoid of copiler's message
}


/* *********************************************************************************************** */
// post the points on the queue used by the app (outside this DLL boundary, in shared memory)
// send points to the main Transcriber App window
void SendPtToWnd(int iFlags, int iX, int iY)    
{
 int iGet = 0;
 int iPut = 0;
 int i;

   if(_bIgnoreStroke &&
      (iFlags&TouchSamplePreviousDownFlag)!=0)
      {
//        MessageBeep((UINT)-1);
        return; 
      }

   WaitForSingleObject(hQ1Mutex, INFINITE);

   iGet = ptq->iGet;
   iPut = ptq->iPut;



#if WRITE_STATISTICSSNDPT
   {
    static int   _iStat1Writes = 0;
    static int   _iStat1Current = 0;
    static short _iStat1Buf[MAX_STATS][5] = {0};
    static int   count = 0;

if (_iStat1Current > 0 && _iStat1Buf[_iStat1Current-1][4] == iPut) OutputDebugString(L"Got same iPut 2 !!!\n");

      _iStat1Buf[_iStat1Current][0] = (short)iFlags;
      _iStat1Buf[_iStat1Current][1] = (short)iX;
      _iStat1Buf[_iStat1Current][2] = (short)iY;
      _iStat1Buf[_iStat1Current][3] = (short)ptq->iGet;
      _iStat1Buf[_iStat1Current][4] = (short)ptq->iPut;
      _iStat1Current++;
      
	  if(_iStat1Current==MAX_STATS)
         {
          HANDLE hFile = INVALID_HANDLE_VALUE;
          int i;

 
		  
		  _iStat1Current = 0;

             hFile = CreateFile(L"\\tchlogSndPt.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
             if(hFile!=INVALID_HANDLE_VALUE)
                {
                 DWORD dw = 0;
				 int   len;
				 int   j;
      
				 SetFilePointer(hFile, 0, NULL, FILE_END);

				   for (i = 0; i < MAX_STATS; i++)
				   {
				    TCHAR str[64];
					unsigned char str2[64];

                    wsprintf(str, L"%d %d %d %d iGet=%d iPut=%d %c%c", count++, (int)_iStat1Buf[i][1], (int)(3000 - _iStat1Buf[i][2]), (int)_iStat1Buf[i][0], 
						     (int)_iStat1Buf[i][3], (int)_iStat1Buf[i][4], (TCHAR)13, (TCHAR)10); 
  		            
					len = _tcslen(str); 
					if(len > 62) 
						len = 62;

					for (j = 0; j < len; j ++) 
						str2[j] = (unsigned char)str[j]; 
					str2[len] = 0;
					
					WriteFile(hFile, str2, len, &dw, NULL);

				   }

                   CloseHandle(hFile);
                }

         }
   }
#endif



   if((iFlags&TouchSampleDownFlag)!=0)
      i = iGet - 3;
   else
      i = iGet - 2;
   
   // ensure there are at least 2 or 3 empty places to write between 
   //  the write pointer and the read pointer - but why???
   //  presumably we want to always keep 2-3 old points available to read in the queue
   //  We do use one point back in the code to end a trace on a timeout - 
   //  could be that 2 or 3 is a stale number.
   if(iPut<i ||
      (iPut>=iGet && iPut<i+MAX_TOUCHQUEUE))
      {
         _bIgnoreStroke = FALSE;



         ptq->elems[iPut][0]=iFlags;
         ptq->elems[iPut][1]=((iX<<16)|iY);

         iPut++;
         
		 if(iPut>=MAX_TOUCHQUEUE)
           iPut = 0;

         ptq->iPut = iPut;
          
		 SetEvent(g_hTabletEvent);
      }
   else
   {  
	   _bIgnoreStroke = TRUE;
	   DEBUGMSG(1,(TEXT("Outer Queue overrun!\n\r")));
   }

   ReleaseMutex(hQ1Mutex);

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -