-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitcher.cpp
More file actions
539 lines (435 loc) · 10.6 KB
/
switcher.cpp
File metadata and controls
539 lines (435 loc) · 10.6 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include "stdafx.h"
#include "keys.h"
CHotKey::CHotKey() :
m_dwMod( 0 ),
m_dwKey( 0 ),
m_pExpr( NULL ),
m_dwLastMatch( DWORD_MAX ),
m_hLastMatch( NULL )
{
}
CHotKey::~CHotKey()
{
delete m_pExpr;
m_pExpr = NULL;
}
BOOL
CHotKey::ParseModifier(
__inout const WCHAR **ppwszKey,
__inout DWORD *pdwMod
)
{
DWORD i = 0;
DWORD dwLen = 0;
for( i = 0; i < ARRAYSIZE( ModKeys ); ++i )
{
dwLen = (DWORD) wcslen( ModKeys[i].pwszName );
if( _wcsnicmp( *ppwszKey, ModKeys[i].pwszName, dwLen ) == 0 && (*ppwszKey)[dwLen] == L'+' )
{
*pdwMod |= ModKeys[i].dwValue;
(*ppwszKey) += (dwLen + 1);
return TRUE;
}
}
return FALSE;
}
BOOL
CHotKey::ParseKey(
__in const WCHAR *pwszKey,
__out DWORD *pdwKey
)
{
DWORD i = 0;
for( i = 0; i < ARRAYSIZE( Keys ); ++i )
{
if( _wcsicmp( pwszKey, Keys[i].pwszName ) == 0 )
{
*pdwKey = Keys[i].dwValue;
return TRUE;
}
}
return FALSE;
}
BOOL
CHotKey::Initialize(
__in const WCHAR *pwszKey,
__in const WCHAR *pwszExpr
)
{
BOOL fResult = FALSE;
DWORD dwMod = 0;
DWORD dwKey = 0;
RegExp *pExpr = NULL;
while( ParseModifier( &pwszKey, &dwMod ) )
{
}
if( !ParseKey( pwszKey, &dwKey ) )
{
goto Exit;
}
try
{
pExpr = new RegExp( pwszExpr, std::regex::ECMAScript | std::regex::icase );
}
catch( const std::regex_error& )
{
}
if( pExpr == NULL )
{
goto Exit;
}
m_dwMod = dwMod;
m_dwKey = dwKey;
m_pExpr = pExpr;
pExpr = NULL;
fResult = TRUE;
Exit:
delete pExpr;
pExpr = NULL;
return fResult;
}
BOOL
CHotKey::Activate(
__in BOOL fNextWindow
)
{
BOOL fResult = FALSE;
HWND hForeground = NULL;
HWND phMatches[20] = {};
DWORD dwMatches = 0;
if( !FindMatches( phMatches, ARRAYSIZE( phMatches ), &dwMatches ) )
{
goto Exit;
}
if( dwMatches == 0 )
{
fResult = TRUE;
goto Exit;
}
hForeground = GetForegroundWindow();
if( m_hLastMatch == hForeground && fNextWindow )
{
++m_dwLastMatch;
}
if( m_dwLastMatch >= dwMatches )
{
m_dwLastMatch = 0;
}
m_hLastMatch = phMatches[m_dwLastMatch];
if( !SetForegroundWindow( m_hLastMatch ) )
{
goto Exit;
}
fResult = TRUE;
Exit:
return fResult;
}
struct FindMatchesEnumContext
{
RegExp *pExpr;
HWND *phMatches;
DWORD dwMaxMatches;
DWORD dwMatches;
};
BOOL
TextOrImageMatch(
__in HWND hwnd,
__in RegExp *pExpr
)
{
BOOL fResult = FALSE;
WCHAR wszText[1024] = L"";
if( GetWindowTextW( hwnd, wszText, ARRAYSIZE( wszText ) ) == 0 )
{
DebugOut( L"GetWindowTextW for %p failed with %d\n", hwnd, GetLastError() );
goto Exit;
}
DebugOut( L"Text for %p is '%s'\n", hwnd, wszText );
if( std::regex_search( wszText, *pExpr ) )
{
DebugOut( L"Matched text for %p\n", hwnd );
fResult = TRUE;
goto Exit;
}
if( !GetProcessImageName( hwnd, wszText, ARRAYSIZE( wszText ) ) )
{
DebugOut( L"GetProcessImageName for %p failed\n", hwnd );
goto Exit;
}
DebugOut( L"Image for %p is '%s'\n", hwnd, wszText );
if( std::regex_search( wszText, *pExpr ) )
{
DebugOut( L"Matched image for %p\n", hwnd );
fResult = TRUE;
goto Exit;
}
Exit:
return fResult;
}
BOOL CALLBACK
FindMatchesEnumCallback(
__in HWND hwnd,
__in LPARAM lParam
)
{
FindMatchesEnumContext *pContext = NULL;
WINDOWINFO info = {};
pContext = (FindMatchesEnumContext *) lParam;
DebugOut( L"Attempt match for hwnd %p\n", hwnd );
if( pContext->dwMatches >= pContext->dwMaxMatches )
{
DebugOut( L"Too many matches, skipping\n" );
goto Exit;
}
info.cbSize = sizeof info;
if( !GetWindowInfo( hwnd, &info ) )
{
DebugOut( L"GetWindowInfo for %p failed with %d\n", hwnd, GetLastError() );
goto Exit;
}
if( ( info.dwStyle & VALID_WINDOW_STYLE_MASK ) != VALID_WINDOW_STYLE_VALUE )
{
DebugOut( L"dwStyle for %p is 0x%08x, skipping\n", hwnd, info.dwStyle );
goto Exit;
}
if( !TextOrImageMatch( hwnd, pContext->pExpr ) )
{
DebugOut( L"No match for %p\n", hwnd );
goto Exit;
}
DebugOut( L"Match succeeded for %p\n", hwnd );
pContext->phMatches[pContext->dwMatches] = hwnd;
pContext->dwMatches++;
Exit:
return TRUE;
}
int __cdecl
MatchesSortCallback(
const void *e1,
const void *e2
)
{
const HWND *h1 = (const HWND *) e1;
const HWND *h2 = (const HWND *) e2;
if( *h1 == *h2 )
{
return 0;
}
if( *h1 > *h2 )
{
return 1;
}
return -1;
}
BOOL
CHotKey::FindMatches(
__out_ecount(dwMatches) HWND *phMatches,
__in DWORD dwMatches,
__out DWORD *pdwActual
)
{
BOOL fResult = FALSE;
FindMatchesEnumContext context = {};
*pdwActual = 0;
context.pExpr = m_pExpr;
context.phMatches = phMatches;
context.dwMaxMatches = dwMatches;
context.dwMatches = 0;
if( !EnumWindows( FindMatchesEnumCallback, (LPARAM) &context ) )
{
goto Exit;
}
qsort( phMatches, context.dwMatches, sizeof *phMatches, MatchesSortCallback );
*pdwActual = context.dwMatches;
fResult = TRUE;
Exit:
return fResult;
}
CSwitcher::CSwitcher() :
m_dwKeys( 0 ),
m_dwLastKey( 0 )
{
memset( m_rgpKeys, 0, sizeof m_rgpKeys );
}
CSwitcher::~CSwitcher()
{
FreeKeys();
}
VOID
CSwitcher::FreeKeys()
{
DWORD i = 0;
for( i = 0; i < m_dwKeys; ++i )
{
UnregisterHotKey( m_hWnd, i + 1 );
delete m_rgpKeys[i];
m_rgpKeys[i] = NULL;
}
m_dwKeys = 0;
m_dwLastKey = 0;
}
BOOL
CSwitcher::LoadFromRegistry()
{
BOOL fResult = FALSE;
HKEY hKeys = NULL;
WCHAR pwszKey[256] = L"";
WCHAR pwszExpr[2048] = L"";
DWORD dwIndex = 0;
DWORD dwType = 0;
CHotKey *pKey = NULL;
DWORD dwError = ERROR_SUCCESS;
DWORD cchKey = 0;
DWORD cbExpr = 0;
DWORD cchExpr = 0;
FreeKeys();
DebugOut( L"Loading configuration from registry\n" );
dwError = (DWORD) RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Trunley\\HotSwitcher\\Keys", 0, KEY_READ, &hKeys );
if( dwError != ERROR_SUCCESS )
{
DebugOut( L"RegOpenKeyExW(HKCU\\Software\\Trunley\\HotSwitcher\\Keys) failed with %d\n", dwError );
hKeys = NULL;
goto Exit;
}
for( dwIndex = 0; m_dwKeys < ARRAYSIZE( m_rgpKeys ); dwIndex++ )
{
cchKey = ARRAYSIZE( pwszKey );
cbExpr = sizeof pwszExpr;
dwError = (DWORD) RegEnumValueW( hKeys, dwIndex, pwszKey, &cchKey, NULL, &dwType, (BYTE *) pwszExpr, &cbExpr );
if( dwError == ERROR_NO_MORE_ITEMS )
{
break;
}
if( dwError != ERROR_SUCCESS )
{
DebugOut( L"RegEnumValueW failed with %d\n", dwError );
goto Exit;
}
if( dwType != REG_SZ )
{
DebugOut( L"Value %s is not REG_SZ, skipping\n", pwszKey );
continue;
}
if( ( cbExpr % sizeof *pwszExpr ) != 0 )
{
DebugOut( L"Value %s has non-integral length, skipping\n", pwszKey );
continue;
}
cchExpr = cbExpr / sizeof *pwszExpr;
if( cchExpr >= ARRAYSIZE( pwszExpr ) )
{
DebugOut( L"Value %s has excessive length, skipping\n", pwszKey );
continue;
}
pwszExpr[cchExpr] = 0;
if( wcslen( pwszExpr ) == 0 )
{
DebugOut( L"Value %s has zero length, skipping\n", pwszKey );
continue;
}
pKey = new CHotKey();
if( pKey == NULL )
{
fwprintf( stderr, L"Failed to allocate new CHotKey\n" );
goto Exit;
}
if( !pKey->Initialize( pwszKey, pwszExpr ) )
{
fwprintf( stderr, L"Failed to parse key '%s' (match '%s'), skipping\n", pwszKey, pwszExpr );
delete pKey;
pKey = NULL;
continue;
}
DebugOut( L"Adding key '%s' match '%s'\n", pwszKey, pwszExpr );
m_rgpKeys[m_dwKeys++] = pKey;
pKey = NULL;
}
fResult = TRUE;
Exit:
if( hKeys != NULL )
{
RegCloseKey( hKeys );
hKeys = NULL;
}
delete pKey;
pKey = NULL;
return fResult;
}
BOOL
CSwitcher::RegisterKeys(
__in HWND hWnd
)
{
BOOL fResult = FALSE;
DWORD i = 0;
for( i = 0; i < m_dwKeys; ++i )
{
if( !RegisterHotKey( hWnd, i+1, m_rgpKeys[i]->m_dwMod, m_rgpKeys[i]->m_dwKey ) )
{
fwprintf( stderr, L"RegisterHotKey(%p, %d, 0x%02x, 0x%02x) failed with %d\n", hWnd, i+1, m_rgpKeys[i]->m_dwMod, m_rgpKeys[i]->m_dwKey, GetLastError() );
goto Exit;
}
}
DebugOut( L"Registered %d keys\n", m_dwKeys );
fResult = TRUE;
Exit:
return fResult;
}
BOOL
CSwitcher::ProcessHotKeyMessage(
__in MSG *pMsg
)
{
BOOL fResult = FALSE;
DWORD dwIndex = 0;
DWORD dwLastKey = 0;
CHotKey *pKey = NULL;
dwIndex = ((DWORD) pMsg->wParam) - 1;
if( dwIndex >= m_dwKeys )
{
goto Exit;
}
pKey = m_rgpKeys[dwIndex];
dwLastKey = m_dwLastKey;
m_dwLastKey = dwIndex;
if( !pKey->Activate( dwLastKey == dwIndex ) )
{
goto Exit;
}
fResult = TRUE;
Exit:
return fResult;
}
VOID ShowKeys(
)
{
DWORD i = 0;
WCHAR wszName[100] = L"";
fwprintf( stderr, L"Examples:\n" );
fwprintf( stderr, L" CTRL+ALT+A - Control, Alt and 'A'\n" );
fwprintf( stderr, L" ALT+F1 - Alt and F1\n" );
fwprintf( stderr, L" VOLUME_MUTE - Mute\n\n");
fwprintf( stderr, L"Modifier keys:" );
for( i = 0; i < ARRAYSIZE( ModKeys ); ++i )
{
if( ( i % 4 ) == 0 )
{
fwprintf( stderr, L"\n " );
}
StringCchCopyW( wszName, ARRAYSIZE( wszName ), ModKeys[i].pwszName );
_wcsupr_s( wszName, ARRAYSIZE( wszName) );
fwprintf( stderr, L"%-20s", wszName );
}
fwprintf( stderr, L"\n\nKeys:" );
for( i = 0; i < ARRAYSIZE( Keys ); ++i )
{
if( ( i % 4 ) == 0 )
{
fwprintf( stderr, L"\n " );
}
StringCchCopyW( wszName, ARRAYSIZE( wszName ), Keys[i].pwszName );
_wcsupr_s( wszName, ARRAYSIZE( wszName) );
fwprintf( stderr, L"%-20s", wszName );
}
fwprintf( stderr, L"\n" );
}