forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouseover.cc
387 lines (310 loc) · 11.2 KB
/
mouseover.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "mouseover.hh"
#include "utf8.hh"
#include <QCoreApplication>
#include <QDir>
#include <algorithm>
#ifdef Q_OS_WIN32
#undef WINVER
#define WINVER 0x0500
#include <sddl.h>
#include <accctrl.h>
#include <aclapi.h>
#include "mouseover_win32/ThTypes.h"
#include "wordbyauto.hh"
#include "x64.hh"
#endif
MouseOver & MouseOver::instance()
{
static MouseOver m;
return m;
}
#ifdef Q_OS_WIN32
const UINT WM_MY_SHOW_TRANSLATION = WM_USER + 301;
static wchar_t className[] = L"GoldenDictMouseover";
typedef BOOL ( WINAPI *ChangeWindowMessageFilterFunc )( UINT, DWORD );
#ifndef _MSC_VER
typedef struct tagCHANGEFILTERSTRUCT {
DWORD cbSize;
DWORD ExtStatus;
} CHANGEFILTERSTRUCT, *PCHANGEFILTERSTRUCT;
#endif
typedef BOOL ( WINAPI *ChangeWindowMessageFilterExFunc )( HWND, UINT, DWORD, PCHANGEFILTERSTRUCT );
#endif // Q_OS_WIN32
#ifdef Q_OS_WIN32
#ifndef ConvertStringSecurityDescriptorToSecurityDescriptor
extern "C" BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptorW(
LPCWSTR StringSecurityDescriptor,
DWORD StringSDRevision,
PSECURITY_DESCRIPTOR *SecurityDescriptor,
PULONG SecurityDescriptorSize );
#endif
static void SetLowLabelToGDSynchroObjects()
{
// The LABEL_SECURITY_INFORMATION SDDL SACL to be set for low integrity
#define LOW_INTEGRITY_SDDL_SACL_W L"S:(ML;;NW;;;LW)"
// DWORD dwErr = ERROR_SUCCESS;
PSECURITY_DESCRIPTOR pSD = NULL;
PACL pSacl = NULL; // not allocated
BOOL fSaclPresent = FALSE;
BOOL fSaclDefaulted = FALSE;
#ifdef Q_OS_WIN64
LPCWSTR pwszMapFileName64 = L"GoldenDictTextOutHookSharedMem64";
#endif
LPCWSTR pwszMapFileName = L"GoldenDictTextOutHookSharedMem";
LPCWSTR pwszSpyMutexName = L"GoldenDictTextOutSpyMutex";
if( ConvertStringSecurityDescriptorToSecurityDescriptorW( LOW_INTEGRITY_SDDL_SACL_W, 1 /* SDDL_REVISION_1 */, &pSD, NULL ) )
{
if( GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted))
{
// Note that psidOwner, psidGroup, and pDacl are
// all NULL and set the new LABEL_SECURITY_INFORMATION
#ifdef Q_OS_WIN64
/* dwErr = */ SetNamedSecurityInfoW( (LPWSTR)pwszMapFileName64,
SE_KERNEL_OBJECT, LABEL_SECURITY_INFORMATION, NULL, NULL, NULL, pSacl);
#endif
/* dwErr = */ SetNamedSecurityInfoW( (LPWSTR)pwszMapFileName,
SE_KERNEL_OBJECT, LABEL_SECURITY_INFORMATION, NULL, NULL, NULL, pSacl);
/* dwErr = */ SetNamedSecurityInfoW( (LPWSTR)pwszSpyMutexName,
SE_KERNEL_OBJECT, LABEL_SECURITY_INFORMATION, NULL, NULL, NULL, pSacl);
}
LocalFree(pSD);
}
}
#endif // Q_OS_WIN32
MouseOver::MouseOver() :
pPref(NULL)
{
#ifdef Q_OS_WIN32
HMODULE hm;
ChangeWindowMessageFilterFunc changeWindowMessageFilterFunc = NULL;
ChangeWindowMessageFilterExFunc changeWindowMessageFilterExFunc = NULL;
mouseOverEnabled = false;
ThTypes_Init();
memset( GlobalData, 0, sizeof( TGlobalDLLData ) );
#ifdef Q_OS_WIN64
memset( GlobalData32, 0, sizeof( TGlobalDLLData32 ) );
#endif
// strcpy( GlobalData->LibName,
// QDir::toNativeSeparators( QDir( QCoreApplication::applicationDirPath() ).filePath( "GdTextOutHook.dll" ) ).toLocal8Bit().data() );
#ifdef Q_OS_WIN64
QDir::toNativeSeparators( QDir( QCoreApplication::applicationDirPath() ).filePath( "GdTextOutHook64.dll" ) ).toWCharArray( GlobalData->LibName );
QDir::toNativeSeparators( QDir( QCoreApplication::applicationDirPath() + "/x86" ).filePath( "GdTextOutHook.dll" ) ).toWCharArray( GlobalData32->LibName );
#else
QDir::toNativeSeparators( QDir( QCoreApplication::applicationDirPath() ).filePath( "GdTextOutHook.dll" ) ).toWCharArray( GlobalData->LibName );
#endif
// Create the window to recive spying results to
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = 0;
wcex.lpfnWndProc = ( WNDPROC ) eventHandler;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetModuleHandle( 0 );
wcex.hIcon = NULL;
wcex.hCursor = NULL,
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = className;
wcex.hIconSm = NULL;
RegisterClassEx( &wcex );
GlobalData->ServerWND = CreateWindow( className, L"", 0, 0, 0, 0, 0, GetDesktopWindow(), NULL, GetModuleHandle( 0 ), 0 );
#ifdef Q_OS_WIN64
GlobalData32->ServerWND = HandleToLong( GlobalData->ServerWND );
#endif
#ifdef Q_OS_WIN64
spyDll = LoadLibrary( QDir::toNativeSeparators( QDir( QCoreApplication::applicationDirPath() ).filePath( "GdTextOutSpy64.dll" ) ).toStdWString().c_str() );
#else
spyDll = LoadLibrary( QDir::toNativeSeparators( QDir( QCoreApplication::applicationDirPath() ).filePath( "GdTextOutSpy.dll" ) ).toStdWString().c_str() );
#endif
if ( spyDll )
activateSpyFn = ( ActivateSpyFn ) GetProcAddress( spyDll, "ActivateTextOutSpying" );
// Allow messages from low intehrity process - for Vista and Win7
hm = GetModuleHandle( __TEXT("user32.dll"));
if ( hm != NULL ) {
changeWindowMessageFilterExFunc = (ChangeWindowMessageFilterExFunc)GetProcAddress( hm, "ChangeWindowMessageFilterEx" );
if( changeWindowMessageFilterExFunc ) {
CHANGEFILTERSTRUCT cfs = { sizeof( CHANGEFILTERSTRUCT ), 0 };
changeWindowMessageFilterExFunc( GlobalData->ServerWND, WM_MY_SHOW_TRANSLATION, 1 /* MSGFLT_ALLOW */, &cfs );
} else {
changeWindowMessageFilterFunc = (ChangeWindowMessageFilterFunc)GetProcAddress( hm, "ChangeWindowMessageFilter" );
if( changeWindowMessageFilterFunc )
changeWindowMessageFilterFunc( WM_MY_SHOW_TRANSLATION, 1 /* MSGFLT_ADD */ );
}
}
//Allow object access from low intehrity process - for Vista and Win7
SetLowLabelToGDSynchroObjects();
#endif
}
void MouseOver::enableMouseOver()
{
#ifdef Q_OS_WIN32
if ( !mouseOverEnabled && activateSpyFn )
{
activateSpyFn( true );
installx64Hooks();
mouseOverEnabled = true;
}
#endif
}
void MouseOver::disableMouseOver()
{
#ifdef Q_OS_WIN32
if ( mouseOverEnabled && activateSpyFn )
{
activateSpyFn( false );
removex64Hooks();
mouseOverEnabled = false;
}
#endif
}
#ifdef Q_OS_WIN32
LRESULT MouseOver::makeScanBitMask()
{
LRESULT res = 0;
if( pPref == NULL )
return 0;
if( !pPref->enableScanPopupModifiers || checkModifiersPressed( pPref->scanPopupModifiers ) ) {
res = GD_FLAG_METHOD_STANDARD;
if( pPref->scanPopupUseUIAutomation !=0 )
res |= GD_FLAG_METHOD_UI_AUTOMATION;
if( pPref->scanPopupUseIAccessibleEx !=0 )
res |= GD_FLAG_METHOD_IACCESSIBLEEX;
if( pPref->scanPopupUseGDMessage !=0 )
res |= GD_FLAG_METHOD_GD_MESSAGE;
}
return res;
}
#ifdef Q_OS_WIN64
#define Global_Data GlobalData32
#else
#define Global_Data GlobalData
#endif
LRESULT CALLBACK MouseOver::eventHandler( HWND hwnd_, UINT msg,
WPARAM wparam, LPARAM lparam )
{
if ( msg == WM_MY_SHOW_TRANSLATION )
{
LRESULT res = instance().makeScanBitMask();
if( res == 0 )
return 0; // Don't handle word without necessity
if( wparam != 0) //Ask for methods of word retrieving
return res;
int wordSeqPos = 0;
QString wordSeq;
#ifdef Q_OS_WIN64
HWND hwnd = ( HWND )LongToHandle( Global_Data->LastWND );
#else
HWND hwnd = Global_Data->LastWND;
#endif
if( Global_Data->CurMod.WordLen == 0)
{
if( ( res & GD_FLAG_METHOD_UI_AUTOMATION ) == 0 )
return 0;
POINT pt = Global_Data->LastPt;
WCHAR *pwstr = gdGetWordAtPointByAutomation( pt );
if( pwstr == NULL ) return 0;
wordSeq = QString::fromWCharArray( pwstr );
}
else
{
// Is the string in utf8 or in locale encoding?
gd::wchar testBuf[ 256 ];
long result = Utf8::decode( Global_Data->CurMod.MatchedWord,
strlen( Global_Data->CurMod.MatchedWord ),
testBuf );
if ( result >= 0 )
{
// It seems to be
QString begin = QString::fromUtf8( Global_Data->CurMod.MatchedWord,
Global_Data->CurMod.BeginPos ).normalized( QString::NormalizationForm_C );
QString end = QString::fromUtf8( Global_Data->CurMod.MatchedWord +
Global_Data->CurMod.BeginPos ).normalized( QString::NormalizationForm_C );
wordSeq = begin + end;
wordSeqPos = begin.size();
}
else
{
// It's not, so interpret it as in local encoding
QString begin = QString::fromLocal8Bit( Global_Data->CurMod.MatchedWord,
Global_Data->CurMod.BeginPos ).normalized( QString::NormalizationForm_C );
QString end = QString::fromLocal8Bit( Global_Data->CurMod.MatchedWord +
Global_Data->CurMod.BeginPos ).normalized( QString::NormalizationForm_C );
wordSeq = begin + end;
wordSeqPos = begin.size();
}
}
// Now locate the word inside the sequence
QString word;
if ( wordSeq[ wordSeqPos ].isSpace() )
{
// Currently we ignore such cases
return 0;
}
else
if ( !wordSeq[ wordSeqPos ].isLetterOrNumber() )
{
// Special case: the cursor points to something which doesn't look like a
// middle of the word -- assume that it's something that joins two words
// together.
int begin = wordSeqPos;
for( ; begin; --begin )
if ( !wordSeq[ begin - 1 ].isLetterOrNumber() )
break;
int end = wordSeqPos;
while( ++end < wordSeq.size() )
if ( !wordSeq[ end ].isLetterOrNumber() )
break;
if ( end - begin == 1 )
{
// Well, turns out it was just a single non-letter char, discard it
return 0;
}
word = wordSeq.mid( begin, end - begin );
}
else
{
// Cursor points to a letter -- cut the word it points to
int begin = wordSeqPos;
for( ; begin; --begin )
if ( !wordSeq[ begin - 1 ].isLetterOrNumber() )
break;
int end = wordSeqPos;
while( ++end < wordSeq.size() )
{
if ( !wordSeq[ end ].isLetterOrNumber() )
break;
}
word = wordSeq.mid( begin, end - begin );
}
// See if we have an RTL char. Reverse the whole string if we do.
if( lparam == 0 )
{
for( int x = 0; x < word.size(); ++x )
{
QChar::Direction d = word[ x ].direction();
if ( d == QChar::DirR || d == QChar::DirAL ||
d == QChar::DirRLE || d == QChar::DirRLO )
{
std::reverse( word.begin(), word.end() );
break;
}
}
}
bool forcePopup = false;
forcePopup = emit instance().isGoldenDictWindow( hwnd );
emit instance().hovered( word, forcePopup );
return 0;
}
return DefWindowProc( hwnd_, msg, wparam, lparam );
}
#endif
MouseOver::~MouseOver()
{
#ifdef Q_OS_WIN32
disableMouseOver();
FreeLibrary( spyDll );
DestroyWindow( GlobalData->ServerWND );
UnregisterClass( className, GetModuleHandle( 0 ) );
ThTypes_End();
#endif
}