2009-02-02 19:26:39 +00:00
|
|
|
#include "TextOutSpy.h"
|
|
|
|
#include "ThTypes.h"
|
2011-06-08 16:12:19 +00:00
|
|
|
#include "GDDataTranfer.h"
|
2011-07-09 19:26:30 +00:00
|
|
|
#include "GetWordByIAccEx.h"
|
2009-02-02 19:26:39 +00:00
|
|
|
|
|
|
|
const int MOUSEOVER_INTERVAL = 300;
|
2011-06-08 16:12:19 +00:00
|
|
|
const int REQUEST_MESSAGE_INTERVAL = 500;
|
2009-02-02 19:26:39 +00:00
|
|
|
const int WM_MY_SHOW_TRANSLATION = WM_USER + 301;
|
|
|
|
|
|
|
|
HINSTANCE g_hInstance = NULL;
|
2011-07-09 19:26:30 +00:00
|
|
|
HANDLE hSynhroMutex = 0, hHookMutex = 0;
|
2009-02-02 19:26:39 +00:00
|
|
|
HINSTANCE hGetWordLib = 0;
|
2010-12-03 21:12:20 +00:00
|
|
|
UINT_PTR TimerID = 0;
|
2009-02-02 19:26:39 +00:00
|
|
|
typedef void (*GetWordProc_t)(TCurrentMode *);
|
|
|
|
GetWordProc_t GetWordProc = NULL;
|
2011-06-08 16:12:19 +00:00
|
|
|
GDDataStruct gds;
|
|
|
|
UINT uGdAskMessage;
|
|
|
|
WCHAR Buffer[256];
|
2009-02-02 19:26:39 +00:00
|
|
|
|
2011-06-08 16:12:19 +00:00
|
|
|
static HWND GetWindowFromPoint(POINT pt)
|
|
|
|
{
|
2011-03-29 14:45:32 +00:00
|
|
|
HWND WndParent,WndChild;
|
|
|
|
WndParent = WindowFromPoint(pt);
|
|
|
|
if(WndParent == NULL) return WndParent;
|
|
|
|
ScreenToClient(WndParent, &pt);
|
2011-06-08 16:12:19 +00:00
|
|
|
WndChild = RealChildWindowFromPoint(WndParent, pt);
|
2011-03-29 14:45:32 +00:00
|
|
|
if(WndChild == NULL) return WndParent;
|
|
|
|
return WndChild;
|
|
|
|
};
|
|
|
|
|
2009-02-02 19:26:39 +00:00
|
|
|
static void SendWordToServer()
|
|
|
|
{
|
2011-07-09 19:26:30 +00:00
|
|
|
DWORD SendMsgAnswer, flags;
|
|
|
|
LRESULT lr;
|
|
|
|
if (hGetWordLib == 0) {
|
|
|
|
hGetWordLib = LoadLibrary(GlobalData->LibName);
|
|
|
|
if (hGetWordLib) {
|
|
|
|
GetWordProc = (GetWordProc_t)GetProcAddress(hGetWordLib, "__gdGetWord");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hGetWordLib = (HINSTANCE)-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for needing to retrieve word - WPARAM = 1
|
|
|
|
lr = SendMessageTimeout(GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 1, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer);
|
|
|
|
if( lr == 0 || SendMsgAnswer == 0) //No answer or no needing
|
|
|
|
return;
|
|
|
|
|
|
|
|
GlobalData->CurMod.MatchedWord[0] = 0;
|
|
|
|
GlobalData->CurMod.WordLen = 0;
|
|
|
|
GlobalData->CurMod.BeginPos = 0;
|
|
|
|
flags = SendMsgAnswer;
|
|
|
|
|
|
|
|
if( ( flags & GD_FLAG_METHOD_GD_MESSAGE ) != 0 && uGdAskMessage != 0 ) {
|
2011-06-08 16:12:19 +00:00
|
|
|
int n;
|
|
|
|
gds.dwSize = sizeof(gds);
|
|
|
|
gds.cwData = Buffer;
|
|
|
|
gds.dwMaxLength = sizeof(Buffer) / sizeof(Buffer[0]);
|
|
|
|
Buffer[0] = 0;
|
|
|
|
gds.hWnd = GlobalData->LastWND;
|
|
|
|
gds.Pt = GlobalData->LastPt;
|
|
|
|
lr = SendMessageTimeout(gds.hWnd, uGdAskMessage, 0, (LPARAM)&gds, SMTO_ABORTIFHUNG, REQUEST_MESSAGE_INTERVAL, &SendMsgAnswer);
|
|
|
|
if(lr != 0 && SendMsgAnswer != 0) {
|
|
|
|
n = WideCharToMultiByte(CP_UTF8, 0, gds.cwData, lstrlenW(gds.cwData), GlobalData->CurMod.MatchedWord, sizeof(GlobalData->CurMod.MatchedWord) - 1, 0, 0);
|
|
|
|
GlobalData->CurMod.MatchedWord[n] = 0;
|
|
|
|
GlobalData->CurMod.WordLen = n;
|
|
|
|
GlobalData->CurMod.BeginPos = 0;
|
|
|
|
if(n > 0) {
|
|
|
|
SendMessageTimeout(GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 0, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-07-09 19:26:30 +00:00
|
|
|
|
|
|
|
if( ( flags & GD_FLAG_METHOD_STANDARD ) != 0 && GetWordProc != 0 ) {
|
2009-02-02 19:26:39 +00:00
|
|
|
GlobalData->CurMod.WND = GlobalData->LastWND;
|
|
|
|
GlobalData->CurMod.Pt = GlobalData->LastPt;
|
|
|
|
GetWordProc(&(GlobalData->CurMod));
|
|
|
|
if (GlobalData->CurMod.WordLen > 0) {
|
|
|
|
SendMessageTimeout(GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 0, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer);
|
2011-07-09 19:26:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( flags & GD_FLAG_METHOD_IACCESSIBLEEX ) != 0 ) {
|
|
|
|
getWordByAccEx( GlobalData->LastPt );
|
|
|
|
if (GlobalData->CurMod.WordLen > 0) {
|
|
|
|
SendMessageTimeout(GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 0, 0, SMTO_ABORTIFHUNG, MOUSEOVER_INTERVAL, &SendMsgAnswer);
|
|
|
|
return;
|
2009-02-02 19:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-09 19:26:30 +00:00
|
|
|
|
|
|
|
if( ( flags & GD_FLAG_METHOD_UI_AUTOMATION ) != 0 ) {
|
|
|
|
PostMessage( GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 0, 0 );
|
|
|
|
}
|
2009-02-02 19:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK TimerFunc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
|
|
|
|
{
|
2010-12-03 21:12:20 +00:00
|
|
|
DWORD wso;
|
|
|
|
wso = WaitForSingleObject(hSynhroMutex, 0);
|
|
|
|
if (wso == WAIT_OBJECT_0 || wso == WAIT_ABANDONED) {
|
|
|
|
KillTimer(0, nTimerid);
|
2011-03-29 14:45:32 +00:00
|
|
|
if ((GlobalData->LastWND!=0)&&(GlobalData->LastWND == GetWindowFromPoint(GlobalData->LastPt))) {
|
2010-12-03 21:12:20 +00:00
|
|
|
SendWordToServer();
|
2009-02-02 19:26:39 +00:00
|
|
|
}
|
2010-12-03 21:12:20 +00:00
|
|
|
ReleaseMutex(hSynhroMutex);
|
2009-02-02 19:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2010-12-03 21:12:20 +00:00
|
|
|
DWORD wso;
|
2011-07-09 19:26:30 +00:00
|
|
|
|
2011-03-29 14:42:14 +00:00
|
|
|
if ((nCode == HC_ACTION) && ((wParam == WM_MOUSEMOVE) || (wParam == WM_NCMOUSEMOVE)) && (GlobalData != NULL)) {
|
2010-12-03 21:12:20 +00:00
|
|
|
wso = WaitForSingleObject(hSynhroMutex, 0);
|
|
|
|
if (wso == WAIT_OBJECT_0 || wso == WAIT_ABANDONED) {
|
2009-02-02 19:26:39 +00:00
|
|
|
HWND WND;
|
|
|
|
TCHAR wClassName[64];
|
|
|
|
|
2011-03-29 14:45:32 +00:00
|
|
|
WND = GetWindowFromPoint(((PMOUSEHOOKSTRUCT)lParam)->pt);
|
2011-03-29 14:42:14 +00:00
|
|
|
|
|
|
|
if(WND == NULL) {
|
|
|
|
ReleaseMutex(hSynhroMutex);
|
|
|
|
return CallNextHookEx(GlobalData->g_hHookMouse, nCode, wParam, lParam);
|
|
|
|
}
|
2011-03-29 14:45:32 +00:00
|
|
|
|
2009-02-02 19:26:39 +00:00
|
|
|
if (GetClassName(WND, wClassName, sizeof(wClassName) / sizeof(TCHAR))) {
|
|
|
|
const char* DisableClasses[] = {
|
|
|
|
"gdkWindowChild",
|
|
|
|
"gdkWindowTemp",
|
2011-03-29 14:42:14 +00:00
|
|
|
"Progman",
|
|
|
|
"WorkerW",
|
2009-02-02 19:26:39 +00:00
|
|
|
};
|
|
|
|
int i;
|
2011-03-29 14:42:14 +00:00
|
|
|
for (i=0; i<4; i++) {
|
2010-12-03 21:12:20 +00:00
|
|
|
if (lstrcmp(wClassName, DisableClasses[i])==0)
|
2009-02-02 19:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-03-29 14:42:14 +00:00
|
|
|
if (i<4) {
|
2009-02-02 19:26:39 +00:00
|
|
|
ReleaseMutex(hSynhroMutex);
|
|
|
|
return CallNextHookEx(GlobalData->g_hHookMouse, nCode, wParam, lParam);
|
|
|
|
}
|
|
|
|
}
|
2010-12-03 21:12:20 +00:00
|
|
|
|
|
|
|
if(GlobalData->LastPt.x!=((PMOUSEHOOKSTRUCT)lParam)->pt.x || GlobalData->LastPt.y!=((PMOUSEHOOKSTRUCT)lParam)->pt.y || GlobalData->LastWND != WND) {
|
|
|
|
TimerID = SetTimer(0, TimerID, MOUSEOVER_INTERVAL, TimerFunc);
|
|
|
|
GlobalData->LastWND = WND;
|
|
|
|
GlobalData->LastPt = ((PMOUSEHOOKSTRUCT)lParam)->pt;
|
|
|
|
}
|
2009-02-02 19:26:39 +00:00
|
|
|
ReleaseMutex(hSynhroMutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CallNextHookEx(GlobalData->g_hHookMouse, nCode, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
DLLIMPORT void ActivateTextOutSpying (int Activate)
|
|
|
|
{
|
|
|
|
// After call SetWindowsHookEx(), when you move mouse to a application's window,
|
|
|
|
// this dll will load into this application automatically. And it is unloaded
|
|
|
|
// after call UnhookWindowsHookEx().
|
2010-12-03 21:12:20 +00:00
|
|
|
DWORD wso;
|
2011-03-29 14:42:14 +00:00
|
|
|
if(GlobalData == NULL) return;
|
2009-02-02 19:26:39 +00:00
|
|
|
if (Activate) {
|
|
|
|
if (GlobalData->g_hHookMouse != NULL) return;
|
|
|
|
GlobalData->g_hHookMouse = SetWindowsHookEx(WH_MOUSE, MouseHookProc, g_hInstance, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (GlobalData->g_hHookMouse == NULL) return;
|
2010-12-03 21:12:20 +00:00
|
|
|
UnhookWindowsHookEx(GlobalData->g_hHookMouse);
|
2011-03-29 14:42:14 +00:00
|
|
|
wso = WaitForSingleObject(hSynhroMutex, 4*MOUSEOVER_INTERVAL);
|
|
|
|
if (wso == WAIT_OBJECT_0 || wso == WAIT_ABANDONED) {
|
|
|
|
if (TimerID) {
|
|
|
|
if (KillTimer(0, TimerID))
|
|
|
|
TimerID=0;
|
|
|
|
}
|
|
|
|
ReleaseMutex(hSynhroMutex);
|
2009-02-02 19:26:39 +00:00
|
|
|
}
|
|
|
|
GlobalData->g_hHookMouse = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
|
|
|
|
DWORD reason /* Reason this function is being called. */ ,
|
|
|
|
LPVOID reserved /* Not used. */ )
|
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
g_hInstance = hInst;
|
2011-07-09 19:26:30 +00:00
|
|
|
if(hHookMutex==0) {
|
|
|
|
hHookMutex = CreateMutex(NULL, FALSE, "GoldenDictTextOutHookMutex");
|
|
|
|
}
|
2010-12-03 21:12:20 +00:00
|
|
|
if(hSynhroMutex==0) {
|
|
|
|
hSynhroMutex = CreateMutex(NULL, FALSE, "GoldenDictTextOutSpyMutex");
|
2011-07-09 19:26:30 +00:00
|
|
|
if(hSynhroMutex==0) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2010-12-03 21:12:20 +00:00
|
|
|
}
|
2011-03-29 14:42:14 +00:00
|
|
|
ThTypes_Init();
|
2011-06-08 16:12:19 +00:00
|
|
|
uGdAskMessage = RegisterWindowMessage(GD_MESSAGE_NAME);
|
2011-07-09 19:26:30 +00:00
|
|
|
FindGetPhysicalCursorPos();
|
2009-02-02 19:26:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
2010-12-03 21:12:20 +00:00
|
|
|
// if(hSynhroMutex) WaitForSingleObject(hSynhroMutex, INFINITE);
|
|
|
|
if(hSynhroMutex) WaitForSingleObject(hSynhroMutex, 2000);
|
|
|
|
if (TimerID) {
|
|
|
|
if (KillTimer(0, TimerID))
|
|
|
|
TimerID=0;
|
|
|
|
}
|
|
|
|
if(hSynhroMutex) {
|
|
|
|
ReleaseMutex(hSynhroMutex);
|
|
|
|
CloseHandle(hSynhroMutex);
|
|
|
|
hSynhroMutex=0;
|
2009-02-02 19:26:39 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
MSG msg ;
|
|
|
|
while (PeekMessage (&msg, 0, WM_TIMER, WM_TIMER, PM_REMOVE)) {}
|
|
|
|
}
|
|
|
|
if ((hGetWordLib != 0)&&(hGetWordLib != (HINSTANCE)(-1))) {
|
|
|
|
FreeLibrary(hGetWordLib);
|
|
|
|
}
|
2011-07-09 19:26:30 +00:00
|
|
|
if(hHookMutex) {
|
|
|
|
DWORD wso = WaitForSingleObject(hHookMutex, 5000);
|
|
|
|
if (wso == WAIT_OBJECT_0 || wso == WAIT_ABANDONED) {
|
|
|
|
ReleaseMutex(hHookMutex);
|
|
|
|
CloseHandle(hHookMutex);
|
|
|
|
hHookMutex=0;
|
|
|
|
}
|
|
|
|
}
|
2009-02-02 19:26:39 +00:00
|
|
|
Thtypes_End();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns TRUE on success, FALSE on failure */
|
|
|
|
return TRUE;
|
|
|
|
}
|