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

📄 uihw_mt.c

📁 嵌入工linux开发的源码
💻 C
字号:
#include <pr2k.h>
#include <uiwnd.h>
//#include <wtpen.h>
//#include <Penpower.h>
#include <uiSw_Kbd.h>
//#include <uiHW_ZG.h>
#include <uiHW_MT.h>
#include <uiHW_New.h>
#include <stdio.h>
#include <sysvar.h>

#ifdef _DEBUG
#ifndef __WIN32__
//#define _DEBUG_HANDWRITE_
#endif
#endif


#ifdef __WIN32__
HINSTANCE  hHW_Dll;
#endif
/**************************************************************************/
//global variable
/**************************************************************************/
//zg hw need

extern int gLanguage;
extern unsigned short SwapWord(unsigned short code);          
//extern unsigned short Big5ToGB(unsigned short bigCode);
static HWRData theHWRData;
static HWRBOX  hwBox0, hwBox1, hwBox2;
                    

//char gHWR_RamAddress[HWR_RAM_SIZE];

//short debug_HWR_Stroke_XY[1][MAX_POINT_IN_ONE_CHAR][2];
short debug_HWR_Stroke_XY[MAX_POINT_IN_ONE_CHAR*2];
//save point message
//unsigned char gHWR_Stroke_XY[HWR_REGION_NUMBER][MAX_POINT_IN_ONE_CHAR][2];
unsigned short gHWR_Stroke_XY[HWR_REGION_NUMBER][MAX_POINT_IN_ONE_CHAR][2];


//candidate buffer
unsigned short gpHWR_Candidate[HWR_CANDIDATE_BUFFER_SIZE];

//keyboard handle
static HNDL gh_HWR_KB=NULL;
//the number of saved point;
static int gHWR_Count=0;

//the region of responding pen's message
static int gHWR_Response_Region=-1;

//the region of recognition.express by bit,for example, bit 0 express region 0;
static DWORD gHWR_Recognition_Region=0;

//the handwrite is or not active;
//static int gHWR_Active=0;
static int gHWR_tid=-1;

//is moving;
static BOOL gHWR_Move=0;

//all region coordinate
static THWR_Region gHWR_Region[HWR_REGION_NUMBER];

//handwrite message queue
static TGuiMessageQueue *gpHWR_MsgQueue;

//handwrite timer
static HNDL gHWR_Timer;

BOOL guiHWR_Handler(WORD msg, WORD x, WORD y)
{
	int i;
	unsigned char Related_X,Related_Y;
	THWR_Point *pPoint,tempPoint;

	TGuiMessage tMsg;
	//if no active,return ;
	if(gHWR_tid==-1)
	{
		return FALSE;
	}

	if(guiQueryWindowOfControl(gh_HWR_KB)!=(HNDL)gpTopWindow)
	{
		return FALSE;
	}
	//finish one stroke;
	if(msg==WIN_PEN_UP&&gHWR_Move)
	{
		if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-1)
		{
			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=-1;
			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=-1;

			 gHWR_Count++;
		}
		gHWR_Move=FALSE;

		_guiHWR_Timer_Start();
		return TRUE;
	}
	else if(msg==WIN_PEN_DOWN)
	{
		for(i=0;i<HWR_REGION_NUMBER;i++)
		{
			//judge if in recognition region;
			if(x>=gHWR_Region[i].left&&x<=gHWR_Region[i].right
				&&y>=gHWR_Region[i].top&&y<=gHWR_Region[i].bottom)
			{
				break;
			}
		}

		//outside of region
		if(i==HWR_REGION_NUMBER)
		{
			if(gHWR_Response_Region!=-1)
			{
				_guiHWR_Timer_Stop();
				tMsg.messageType=WIN_HWR_RECOGNIZE;
				tMsg.x=gHWR_Response_Region;
				tMsg.y=gHWR_Count;
				guiEnqueue(gpHWR_MsgQueue,&tMsg);
				gHWR_Recognition_Region|=1<<gHWR_Response_Region;
				gHWR_Response_Region=-1;
				gHWR_Count=0;
			}
			return FALSE;
		}
		//in response region
		else if(gHWR_Response_Region==i)
		{
			if(guiTimer_IsRun(gHWR_Timer)==0)
			{
				//sc_logString("Timer not run",TO_MONITOR);
				gHWR_Move=FALSE;
				return TRUE;
			}

			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;
			if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-2)
			{
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
				gHWR_Count++;

				guiDrawPixel(0,x,y,GUI_BLACK);
			}

			_guiHWR_Timer_Stop();
			gHWR_Move=TRUE;
			return TRUE;
		}
		//new response region
		else if(gHWR_Response_Region==-1)
		{

			if(gHWR_Recognition_Region&(1<<i))
			{
				return TRUE;
			}
			gHWR_Response_Region=i;
			gHWR_Count=0;
			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;

			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
			gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
			gHWR_Count++;
			guiDrawPixel(0,x,y,GUI_BLACK);

			gHWR_Move=TRUE;
			return TRUE;
		}
		//other region
		else
		{
			_guiHWR_Timer_Stop();
			//send recognition message;
			tMsg.messageType=WIN_HWR_RECOGNIZE;
			tMsg.x=gHWR_Response_Region;
			tMsg.y=gHWR_Count;
			guiEnqueue(gpHWR_MsgQueue,&tMsg);
			gHWR_Recognition_Region|=1<<gHWR_Response_Region;


			//modify gHWR_Count;
			gHWR_Count=0;

			if(gHWR_Recognition_Region&(1<<i))
			{
				gHWR_Response_Region=-1;
				return TRUE;
			}
			//modify gHWR_Response_Region
			gHWR_Response_Region=i;

			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;

			if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-2)
			{
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
				gHWR_Count++;
				guiDrawPixel(0,x,y,GUI_BLACK);
			}
			gHWR_Move=TRUE;
			return TRUE;
		}

	}
	else if(msg==WIN_PEN_MOVE)
	{
		if(gHWR_Move==0||gHWR_Response_Region==-1)
		{
			return FALSE;
		}

		//judge if in recognition region;
		if(x>=gHWR_Region[gHWR_Response_Region].left&&x<=gHWR_Region[gHWR_Response_Region].right&&
		   y>=gHWR_Region[gHWR_Response_Region].top&&y<=gHWR_Region[gHWR_Response_Region].bottom)
		{
			Related_X=x-gHWR_Region[gHWR_Response_Region].left;
			Related_Y=y-gHWR_Region[gHWR_Response_Region].top;

			if(gHWR_Count==0)
			{
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
				gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
				gHWR_Count++;

				guiDrawPixel(0,x,y,GUI_BLACK);
			}
			else if(gHWR_Count<MAX_POINT_IN_ONE_CHAR-2)
			{
				//pPoint=&gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count-1];
				tempPoint.x=gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count-1][0];
				tempPoint.y=gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count-1][1];
				pPoint=&tempPoint;


				if(abs(Related_X-pPoint->x)>=HWR_POINT_INTERVAL||abs(Related_Y-pPoint->y)>=HWR_POINT_INTERVAL)
				{
					guiDrawLine(0,pPoint->x+gHWR_Region[gHWR_Response_Region].left,
							  pPoint->y+gHWR_Region[gHWR_Response_Region].top,
							  x,y,
					          GUI_BLACK,0);
					gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][0]=Related_X;
					gHWR_Stroke_XY[gHWR_Response_Region][gHWR_Count][1]=Related_Y;
					gHWR_Count++;
				}
			}
			return TRUE;
		}
		else
		{

			return FALSE;
		}
	}
	return FALSE;
}


userThread guiHWR_Thread(void)
{
	int recognition_type, ret;
	short left,top,right,bottom;//region coordinate
	//unsigned short CandidateBuf[32];
	unsigned short *pSrcCode, *pDestCode;
	unsigned short i, count, code;
	int iRegion;
	short iCount;
	TGuiMessage tMsg;
	struct tagGuiSw_Kbd *pKB;
                 
	short hError;

	//allocate message queue and timer;
	gpHWR_MsgQueue=guiAllocateMessageQueue();
	if(gpHWR_MsgQueue==NULL)
	{
		sc_exit();
		return;
	}
	gHWR_Timer=gui_allocateTimer();
	if(gHWR_Timer==NULL)
	{
		//sc_free(gpHWR_MsgQueue);
		guiReleaseMessageQueue(gpHWR_MsgQueue);
		sc_exit();
		return;
	}
	//associate timer and MsgQueue;
	((TGuiTimer *)gHWR_Timer)->pMsgQueue=gpHWR_MsgQueue;

	pKB=(struct tagGuiSw_Kbd *)gh_HWR_KB;

	recognition_type = IN_5401_TYPE|IN_13052_TYPE;

	ret = PPHWRSetType(&theHWRData, recognition_type);

	while(1)
	{
		if(guiDequeue(gpHWR_MsgQueue,&tMsg)==-1)
		{
			continue;
		}
		switch(tMsg.messageType)
		{
		case TIMER_TIMEOUT:
			guiEnterWCS();

			if(guiPequeue(gpHWR_MsgQueue,&tMsg)==STATUS_OK)
			{
				if(tMsg.messageType==WIN_HWR_RECOGNIZE)
				{
					guiExitWCS();
					break;
				}
			}

			tMsg.x=gHWR_Response_Region;
			tMsg.y=gHWR_Count;

			gHWR_Recognition_Region|=1<<gHWR_Response_Region;
			gHWR_Response_Region=-1;
			gHWR_Count=0;
			guiExitWCS();

		case WIN_HWR_RECOGNIZE:
			iRegion=tMsg.x;
			iCount=tMsg.y;

			left=gHWR_Region[iRegion].left;
			top=gHWR_Region[iRegion].top;
			right=gHWR_Region[iRegion].right;
			bottom=gHWR_Region[iRegion].bottom;

			
			gHWR_Stroke_XY[iRegion][iCount-1][0] = -1;
			gHWR_Stroke_XY[iRegion][iCount-1][1] = 0;
			
			if(iRegion!=2)
			{
				recognition_type = IN_5401_TYPE|IN_13052_TYPE;         
				//recognition_type = UPPER_ALPHA_TYPE | LOWER_ALPHA_TYPE;
				if(gLanguage==LANGUAGE_CHS)
					recognition_type |= RECOG_GB_TYPE;
				
			}
			else
			{
				recognition_type = UPPER_ALPHA_TYPE | LOWER_ALPHA_TYPE | NUMA_TYPE;
			}   
/*
			if(gLanguage==LANGUAGE_CHS)
				recognition_type |= RECOG_GB_TYPE;
			else if(gLanguage==LANGUAGE_ENG)                             
				recognition_type = UPPER_ALPHA_TYPE | LOWER_ALPHA_TYPE  | NUMA_TYPE;

*/
			if(gLanguage==LANGUAGE_ENG)                             
				recognition_type = UPPER_ALPHA_TYPE | LOWER_ALPHA_TYPE  | NUMA_TYPE;

			ret = PPHWRSetType(&theHWRData, recognition_type);                                  
//			memset(debug_HWR_Stroke_XY, 0x00,MAX_POINT_IN_ONE_CHAR*2);
			memcpy(debug_HWR_Stroke_XY,gHWR_Stroke_XY[iRegion],MAX_POINT_IN_ONE_CHAR*2);

			hwBox0.left   = 0;
			hwBox0.top    = 0;
			hwBox0.right  = right - left;
			hwBox0.bottom = bottom - top;
			ret = PPHWRSetBox(&theHWRData, &hwBox0);
			ret = PPHWRSetCandidateNum(&theHWRData,5);  			                                        

			//ret = start_recognition(iCount,(struct point_type *)debug_HWR_Stroke_XY[0],gpHWR_Candidate);
			ret=PPHWRRecognize(&theHWRData,(WORD *)debug_HWR_Stroke_XY, gpHWR_Candidate); //gpHWR_Candidate);
			if(ret!=0)
			{
				sc_logString("PPRecognize failure",TO_MONITOR);
				while(1);
			}

			guiClearBlock(0,left,top,right,bottom,GUI_WHITE,0);
                                                  
                                           
			//count = strlen((char *)gpHWR_Candidate);
			count = 0;
			while(gpHWR_Candidate[count])			
			{
            	count++;
			}
			//gpHWR_Candidate[count/2+1] = 0x0000;
			//count /= 2;

			for(i=count+1; i>0; i--)
			{
				gpHWR_Candidate[i] = gpHWR_Candidate[i-1];            	                                                                 			
			}
			gpHWR_Candidate[0] = count;                                       
			
#ifdef __WIN32__
			if(recognition_type!=(UPPER_ALPHA_TYPE | LOWER_ALPHA_TYPE | NUMA_TYPE))
			{
				for(i=1; i<count; i++)
				{
					code = gpHWR_Candidate[i];							
					gpHWR_Candidate[i] = SwapWord(code);
				}
			}

#endif
			
			gpKbd_Info[pKB->base.style].showFun(gh_HWR_KB,iRegion,gpHWR_Candidate);

			//can reuse this region
			gHWR_Recognition_Region&=~(1<<iRegion);

			break;
		default:
			break;
		}
	}
}

/****************************************************************/
//external function
/****************************************************************/
STATUS guiHWR_Init(void)
{
	char * LibStartAddress=0;//tong warning
	int ret, ramSize;      

#ifdef __WIN32__

    hHW_Dll=LoadLibrary("PenHW.dll");       // free while close
 
	PPHWRInit = (pPPHWRInit)GetProcAddress(hHW_Dll,"PPHWRInit");
	PPHWRGetRamSize = (pPPHWRGetRamSize)GetProcAddress(hHW_Dll,"PPHWRGetRamSize");
	PPHWRSetType = (pPPHWRSetType)GetProcAddress(hHW_Dll,"PPHWRSetType");
	PPHWRSetBox = (pPPHWRSetBox)GetProcAddress(hHW_Dll,"PPHWRSetBox");
	PPHWRSetCandidateNum = (pPPHWRSetCandidateNum)GetProcAddress(hHW_Dll,"PPHWRSetCandidateNum");
	PPHWRRecognize = (pPPHWRRecognize)GetProcAddress(hHW_Dll,"PPHWRRecognize");
#endif
	
	ramSize = PPHWRGetRamSize();
	//theHWRData.pPrivate=(char *)sc_malloc(ramSize);
	theHWRData.pPrivate=(char *)ap_malloc(ramSize);

	theHWRData.pInternalDict = 0; //(signed char *)HWR_DATABASE_ADDRESS;
	theHWRData.pExternalDict = 0; //(signed char *)HWR_DATABASE_ADDRESS;

	if(PPHWRInit(&theHWRData)!=STATUS_OK)
	{
		return STATUS_ERR;
	}

	return STATUS_OK;

}
                                            
STATUS guiHWR_Begin(HNDL hKB, THWR_Region *pHWR_Region)
{
	int i;

	struct tagGuiSw_Kbd *pKB;

#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_Begin 0",TO_MONITOR);
#endif

	guiEnterWCS();

	if(gHWR_tid!=-1)
	{
		guiExitWCS();
		return STATUS_ERR;
	}

	for(i=0;i<HWR_REGION_NUMBER;i++)
	{
		gHWR_Stroke_XY[i][MAX_POINT_IN_ONE_CHAR-1][0]=-1;
		gHWR_Stroke_XY[i][MAX_POINT_IN_ONE_CHAR-1][1]=-1;
	}

	gHWR_Count=0;
	gHWR_Response_Region=-1;
	gHWR_Recognition_Region=0;

	gHWR_Move=0;

	//copy region
	memcpy(gHWR_Region,pHWR_Region,sizeof(gHWR_Region));
	gh_HWR_KB=hKB;
	//gHWR_tid=sc_createThread(guiHWR_Thread,100,4*1024,NULL,0);
	gHWR_tid=sc_createThread(guiHWR_Thread,100,32*1024,NULL,0);

	if(gHWR_tid==-1)
	{
		guiExitWCS();
		return STATUS_ERR;
	}

	guiExitWCS();
#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_Begin 1",TO_MONITOR);
#endif
	return STATUS_OK;
}

STATUS guiHWR_End(void)
{
	int result;

	guiEnterWCS();

#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_End 0",TO_MONITOR);
#endif

	if(gHWR_tid!=-1)
	{
		sc_deleteThread(gHWR_tid);
	}

	gHWR_tid=-1;
	gh_HWR_KB=NULL;

	if(gHWR_Timer)
	{
		_guiTimer_Delete((TGuiTimer *)gHWR_Timer);
		gHWR_Timer=NULL;
	}

	if(gpHWR_MsgQueue)
	{
		//sc_free(gpHWR_MsgQueue);
		guiReleaseMessageQueue(gpHWR_MsgQueue);
		gpHWR_MsgQueue=NULL;
	}

	guiExitWCS();

#ifdef _DEBUG_HANDWRITE_
	sc_logString("HWR_End",TO_MONITOR);
#endif
	return STATUS_OK;
}

/****************************************************************/
//internal function
/****************************************************************/
STATUS _guiHWR_Timer_Start(void)
{
	int count;
	if(gHWR_Timer==0)
	{
		return STATUS_ERR;
	}
//#ifdef __WIN32__
//	count = 80;
//#else
	sys_GetSysVar_Integer(SYSVAR_INTEGER_HW_WAITTIME,&count);
//#endif
	gui_restartTimer(gHWR_Timer,count,0);
	return STATUS_OK;
}

STATUS _guiHWR_Timer_Stop(void)
{
	if(gHWR_Timer==0)
	{
		return STATUS_ERR;
	}
	gui_stopTimer(gHWR_Timer);
	return STATUS_OK;
}


//#ifdef __WIN32__
#if 0
/**************************************************************/
//temp function(should be contained in penpower.cpp)
/**************************************************************/

unsigned short PPHWRInit(HWRData* pData)
{
	return STATUS_OK;
}                    

unsigned long  PPHWRGetRamSize(void)	/* Return the size of memory in pPrivate, in bytes. */
{
	return 10*1024;
}                    

unsigned short PPHWRSetType(HWRData* pData, DWORD type)
{
	return STATUS_OK;
}                    

unsigned short PPHWRSetBox(HWRData* pData, LPHWRBOX pbox)
{
	return STATUS_OK;
}                    

unsigned short PPHWRSetCandidateNum(HWRData* pData, WORD number)
{
	return STATUS_OK;
}                    

unsigned short PPHWRRecognize(HWRData* pData, WORD* pTrace, WORD* pResult)
{
	int i;
	WORD code;

	/*
	j=abs(rand());
	j%=8;
	j=j>4?4:j;
	pResult[0]=j;
	*/
// A4A4 B0EA A448 A5C1 
	pResult[0] = 0xDAA7;
	pResult[1] = 0x52B7;
	pResult[2] = 0xFAA9;
	pResult[3] = 0xEBA4;
	pResult[4] = 0x0000;
		
	return 0;

}

#endif
 

⌨️ 快捷键说明

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