-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathhookdll.c
851 lines (680 loc) · 19.6 KB
/
hookdll.c
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
Seamless windows - Remote server hook DLL
Based on code copyright (C) 2004-2005 Martin Wickett
Copyright 2005-2010 Peter Åstrand <[email protected]> for Cendio AB
Copyright 2006-2008 Pierre Ossman <[email protected]> for Cendio AB
Copyright 2013-2015 Henrik Andersson <[email protected]> for Cendio AB
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
#include <winuser.h>
#include <dwmapi.h>
#include "shared.h"
#include "vchannel.h"
#define EXTERN __declspec(dllexport)
#define FOCUS_MSG_NAME "WM_SEAMLESS_FOCUS"
static UINT g_wm_seamless_focus = 0; // Non-zero if DLL is initialized
static HHOOK g_cbt_hook = NULL;
static HHOOK g_wndproc_hook = NULL;
static HHOOK g_wndprocret_hook = NULL;
static HINSTANCE g_instance = NULL;
static int g_initialized = 0;
/*
The data shared between 32 and 64 bit processes contains HWNDs. On
win64, HWND is 64 bit but only 32 bits are used. Thus, our
structure only contains 32 bit, using this data type. The structure
alignment is the same on win32 and win64 (the default being 8 byte
boundaries).
*/
typedef ULONG32 HWND32;
typedef struct _shared_variables
{
int instance_count;
// blocks for locally generated events
HWND32 block_move_hwnd;
unsigned int block_move_serial;
RECT block_move;
unsigned int blocked_zchange_serial;
HWND32 blocked_zchange[2];
unsigned int blocked_focus_serial;
HWND32 blocked_focus;
unsigned int blocked_state_serial;
HWND32 blocked_state_hwnd;
int blocked_state;
} shared_variables;
static HANDLE g_mutex = NULL;
static shared_variables *g_shdata = NULL;
static BOOL
is_toplevel(HWND hwnd)
{
BOOL toplevel;
HWND parent;
parent = GetAncestor(hwnd, GA_PARENT);
/* According to MS: "A window that has no parent, or whose
parent is the desktop window, is called a top-level
window." See http://msdn2.microsoft.com/en-us/library/ms632597(VS.85).aspx. */
toplevel = (!parent || parent == GetDesktopWindow());
return toplevel;
}
/* Returns true if a window is a menu window. */
static BOOL
is_menu(HWND hwnd)
{
/* Notepad menus have an owner, but Seamonkey menus does not,
so we cannot use the owner in our check. This leaves us with
checking WS_EX_TOOLWINDOW and WS_EX_TOPMOST. */
LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
return (exstyle & (WS_EX_TOOLWINDOW | WS_EX_TOPMOST));
}
/* Determine the "parent" field for the CREATE response. */
static HWND
get_parent(HWND hwnd)
{
HWND result;
HWND owner;
LONG exstyle;
char name[512];
/* Use the same logic to determine if the window should be
"transient" (ie have no task icon) as MS uses. This is documented at
http://msdn2.microsoft.com/en-us/library/bb776822.aspx */
owner = GetWindow(hwnd, GW_OWNER);
exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
if (!owner && !(exstyle & WS_EX_TOOLWINDOW)) {
/* display taskbar icon */
result = NULL;
} else {
/* no taskbar icon */
if (owner) {
result = owner;
/* MS Office assigns wrong owner for tooltips which will
create problems with focusing of correct window after
hide of tooltip. This is a workaround to always ignore
use of owner of Office Tooltips. */
GetClassName(hwnd, name, sizeof(name));
if (strcmp(name, "OfficeTooltip") == 0)
result = (HWND) - 1;
} else {
result = (HWND) - 1;
}
}
return result;
}
static void
update_position(HWND hwnd)
{
RECT rect, blocked;
HWND blocked_hwnd;
unsigned int serial;
HRESULT res;
WaitForSingleObject(g_mutex, INFINITE);
blocked_hwnd = long_to_hwnd(g_shdata->block_move_hwnd);
serial = g_shdata->block_move_serial;
memcpy(&blocked, &g_shdata->block_move, sizeof(RECT));
ReleaseMutex(g_mutex);
vchannel_block();
res = DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS,
&rect, sizeof(RECT));
/* DwmGetWindowAttribute can fail on Windows 2008 R2. If this
happens, fall back to GetWindowRect.
This assumes that DwmGetWindowAttribute does not fail
intermittently, but is consistently "broken".
(See also: SafeMoveWindow) */
if (res != S_OK) {
if (!GetWindowRect(hwnd, &rect))
goto end;
}
if ((hwnd == blocked_hwnd) && (rect.left == blocked.left)
&& (rect.top == blocked.top)
&& (rect.right == blocked.right) && (rect.bottom == blocked.bottom))
goto end;
vchannel_write("POSITION", "0x%08lx,%d,%d,%d,%d,0x%08x",
hwnd,
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);
end:
vchannel_unblock();
}
static void
update_zorder(HWND hwnd)
{
HWND behind;
HWND block_hwnd, block_behind;
unsigned int serial;
WaitForSingleObject(g_mutex, INFINITE);
serial = g_shdata->blocked_zchange_serial;
block_hwnd = long_to_hwnd(g_shdata->blocked_zchange[0]);
block_behind = long_to_hwnd(g_shdata->blocked_zchange[1]);
ReleaseMutex(g_mutex);
vchannel_block();
behind = GetNextWindow(hwnd, GW_HWNDPREV);
while (behind) {
LONG style;
style = GetWindowLong(behind, GWL_STYLE);
if ((!(style & WS_CHILD) || (style & WS_POPUP)) && (style & WS_VISIBLE))
break;
behind = GetNextWindow(behind, GW_HWNDPREV);
}
if ((hwnd == block_hwnd) && (behind == block_behind))
vchannel_write("ACK", "%u", serial);
else {
int flags = 0;
LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
// handle always on top
if (exstyle & WS_EX_TOPMOST)
flags |= SEAMLESS_CREATE_TOPMOST;
vchannel_write("ZCHANGE", "0x%08lx,0x%08lx,0x%08x", hwnd, behind,
flags);
}
vchannel_unblock();
}
#define ICON_CHUNK 400
static void
update_icon(HWND hwnd, HICON icon, int large)
{
int i, j, size, chunks;
char buf[32 * 32 * 4];
char asciibuf[ICON_CHUNK * 2 + 1];
size = extract_icon(icon, buf, sizeof(buf));
if (size <= 0)
return;
if ((!large && size != 16 * 16 * 4) || (large && size != 32 * 32 * 4)) {
vchannel_debug("Unexpected icon size.");
return;
}
chunks = (size + ICON_CHUNK - 1) / ICON_CHUNK;
for (i = 0; i < chunks; i++) {
for (j = 0; j < ICON_CHUNK; j++) {
if (i * ICON_CHUNK + j >= size)
break;
sprintf(asciibuf + j * 2, "%02x",
(int) (unsigned char) buf[i * ICON_CHUNK + j]);
}
vchannel_write("SETICON", "0x%08lx,%d,RGBA,%d,%d,%s", hwnd, i,
large ? 32 : 16, large ? 32 : 16, asciibuf);
}
}
static LRESULT CALLBACK
wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details)
{
HWND hwnd;
UINT msg;
WPARAM wparam;
LPARAM lparam;
LONG style;
if (!g_initialized)
goto end;
if (code < 0)
goto end;
hwnd = ((CWPSTRUCT *) details)->hwnd;
msg = ((CWPSTRUCT *) details)->message;
wparam = ((CWPSTRUCT *) details)->wParam;
lparam = ((CWPSTRUCT *) details)->lParam;
if (!is_toplevel(hwnd)) {
goto end;
}
style = GetWindowLong(hwnd, GWL_STYLE);
/* cmd.exe console window belongs to CSRSS.exe process which is not
owned by user and therefor hooking into it will fail in several ways,
we do ignore them upon vchannel SYNC command when enumerating windows
but we should also ignore them here if they exists at this point.
*/
char classname[32];
if (GetClassName(hwnd, classname, sizeof(classname))
&& !strcmp(classname, "ConsoleWindowClass"))
goto end;
switch (msg) {
case WM_WINDOWPOSCHANGED:
{
WINDOWPOS *wp = (WINDOWPOS *) lparam;
if (wp->flags & SWP_SHOWWINDOW) {
unsigned short title[150];
int state;
DWORD pid;
int flags;
HICON icon;
LONG exstyle;
exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
GetWindowThreadProcessId(hwnd, &pid);
flags = 0;
if (style & DS_MODALFRAME)
flags |= SEAMLESS_CREATE_MODAL;
// handle always on top
if (exstyle & WS_EX_TOPMOST)
flags |= SEAMLESS_CREATE_TOPMOST;
vchannel_write("CREATE", "0x%08lx,0x%08lx,0x%08lx,0x%08x",
hwnd_to_long(hwnd), (long) pid,
hwnd_to_long(get_parent(hwnd)), flags);
GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
vchannel_write("TITLE", "0x%08lx,%s,0x%08x", hwnd,
vchannel_strfilter_unicode(title), 0);
icon = get_icon(hwnd, 1);
if (icon) {
update_icon(hwnd, icon, 1);
DeleteObject(icon);
}
icon = get_icon(hwnd, 0);
if (icon) {
update_icon(hwnd, icon, 0);
DeleteObject(icon);
}
if (style & WS_MAXIMIZE)
state = 2;
else if (style & WS_MINIMIZE)
state = 1;
else
state = 0;
update_position(hwnd);
vchannel_write("STATE", "0x%08lx,0x%08x,0x%08x", hwnd,
state, 0);
}
if (wp->flags & SWP_HIDEWINDOW)
vchannel_write("DESTROY", "0x%08lx,0x%08x", hwnd, 0);
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
break;
if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))
update_position(hwnd);
break;
}
case WM_SETICON:
if (!(style & WS_VISIBLE))
break;
switch (wparam) {
case ICON_BIG:
if (lparam)
update_icon(hwnd, (HICON) lparam, 1);
else
vchannel_write("DELICON", "0x%08lx,RGBA,32,32", hwnd);
break;
case ICON_SMALL:
case 2:
if (lparam)
update_icon(hwnd, (HICON) lparam, 0);
else
vchannel_write("DELICON", "0x%08lx,RGBA,16,16", hwnd);
break;
default:
vchannel_debug("Weird icon size %d", (int) wparam);
}
break;
case WM_SIZE:
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
break;
update_position(hwnd);
break;
case WM_MOVE:
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
break;
update_position(hwnd);
break;
case WM_DESTROY:
if (!(style & WS_VISIBLE))
break;
vchannel_write("DESTROY", "0x%08lx,0x%08x", hwnd, 0);
break;
default:
break;
}
end:
return CallNextHookEx(g_wndproc_hook, code, cur_thread, details);
}
static LRESULT CALLBACK
wndprocret_hook_proc(int code, WPARAM cur_thread, LPARAM details)
{
HWND hwnd;
UINT msg;
WPARAM wparam;
LPARAM lparam;
LONG style;
if (!g_initialized)
goto end;
if (code < 0)
goto end;
hwnd = ((CWPRETSTRUCT *) details)->hwnd;
msg = ((CWPRETSTRUCT *) details)->message;
wparam = ((CWPRETSTRUCT *) details)->wParam;
lparam = ((CWPRETSTRUCT *) details)->lParam;
if (!is_toplevel(hwnd)) {
goto end;
}
style = GetWindowLong(hwnd, GWL_STYLE);
switch (msg) {
case WM_WINDOWPOSCHANGED:
{
WINDOWPOS *wp = (WINDOWPOS *) lparam;
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
break;
if (!(wp->flags & SWP_NOZORDER))
update_zorder(hwnd);
break;
}
case WM_SETTEXT:
{
unsigned short title[150];
if (!(style & WS_VISIBLE))
break;
/* We cannot use the string in lparam because
we need unicode. */
GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
vchannel_write("TITLE", "0x%08lx,%s,0x%08x", hwnd,
vchannel_strfilter_unicode(title), 0);
break;
}
case WM_SETICON:
{
HICON icon;
if (!(style & WS_VISIBLE))
break;
/*
* Somehow, we never get WM_SETICON for the small icon.
* So trigger a read of it every time the large one is
* changed.
*/
icon = get_icon(hwnd, 0);
if (icon) {
update_icon(hwnd, icon, 0);
DeleteObject(icon);
}
}
default:
break;
}
if (msg == g_wm_seamless_focus) {
/* For some reason, SetForegroundWindow() on menus
closes them. Ignore focus requests for menu windows. */
if ((GetForegroundWindow() != hwnd) && !is_menu(hwnd))
SetForegroundWindow(hwnd);
vchannel_write("ACK", "%u", g_shdata->blocked_focus_serial);
}
end:
return CallNextHookEx(g_wndprocret_hook, code, cur_thread, details);
}
static LRESULT CALLBACK
cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam)
{
if (!g_initialized)
goto end;
if (code < 0)
goto end;
switch (code) {
case HCBT_MINMAX:
{
int show, state, blocked;
HWND hwnd, blocked_hwnd;
unsigned int serial;
LONG style;
WaitForSingleObject(g_mutex, INFINITE);
blocked_hwnd = long_to_hwnd(g_shdata->blocked_state_hwnd);
serial = g_shdata->blocked_state_serial;
blocked = g_shdata->blocked_state;
ReleaseMutex(g_mutex);
hwnd = (HWND) wparam;
style = GetWindowLong(hwnd, GWL_STYLE);
if (!(style & WS_VISIBLE))
break;
show = LOWORD(lparam);
if ((show == SW_NORMAL) || (show == SW_SHOWNORMAL)
|| (show == SW_RESTORE))
state = 0;
else if ((show == SW_MINIMIZE) || (show == SW_SHOWMINIMIZED))
state = 1;
else if ((show == SW_MAXIMIZE) || (show == SW_SHOWMAXIMIZED))
state = 2;
else {
vchannel_debug("Unexpected show: %d", show);
break;
}
if ((blocked_hwnd == hwnd) && (blocked == state))
vchannel_write("ACK", "%u", serial);
else
vchannel_write("STATE", "0x%08lx,0x%08x,0x%08x",
hwnd, state, 0);
break;
}
default:
break;
}
end:
return CallNextHookEx(g_cbt_hook, code, wparam, lparam);
}
EXTERN void
SetHooks(void)
{
if (!g_cbt_hook)
g_cbt_hook = SetWindowsHookEx(WH_CBT, cbt_hook_proc, g_instance, 0);
if (!g_wndproc_hook)
g_wndproc_hook =
SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0);
if (!g_wndprocret_hook)
g_wndprocret_hook =
SetWindowsHookEx(WH_CALLWNDPROCRET, wndprocret_hook_proc,
g_instance, 0);
}
EXTERN void
RemoveHooks(void)
{
if (g_cbt_hook)
UnhookWindowsHookEx(g_cbt_hook);
if (g_wndproc_hook)
UnhookWindowsHookEx(g_wndproc_hook);
if (g_wndprocret_hook)
UnhookWindowsHookEx(g_wndprocret_hook);
}
EXTERN void
SafeMoveWindow(unsigned int serial, HWND hwnd, int x, int y, int width,
int height)
{
RECT rect, fullrect;
int bordersize[4]; /* top, right, bottom, left */
if (!g_initialized)
return;
/* We retrieve the window size both with and without borders
so we can calculate the width of the resize border. The
given coordinates needs to be offset with the width of the
resize borders since update_position removes them. */
if (!GetWindowRect(hwnd, &fullrect)) {
vchannel_debug("GetWindowRect failed!");
goto no_window_shift;
}
if (DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS,
&rect, sizeof(RECT)) == S_OK) {
/* DwmGetWindowAttribute can fail on Windows 2008 R2
for reasons unknown. If we can't calculate the size
of the borders just work with what GetWindowRect
gives us.
This assumes that DwmGetWindowAttribute also fails
in SafeMoveWindow - see comments there. */
bordersize[0] = rect.top - fullrect.top;
bordersize[3] = rect.left - fullrect.left;
bordersize[1] = fullrect.right - rect.right;
bordersize[2] = fullrect.bottom - rect.bottom;
height += bordersize[0];
height += bordersize[2];
width += bordersize[1];
width += bordersize[3];
x -= bordersize[3];
y -= bordersize[0];
}
no_window_shift:
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->block_move_hwnd = hwnd_to_long(hwnd);
g_shdata->block_move_serial = serial;
g_shdata->block_move.left = x;
g_shdata->block_move.top = y;
g_shdata->block_move.right = x + width;
g_shdata->block_move.bottom = y + height;
ReleaseMutex(g_mutex);
SetWindowPos(hwnd, NULL, x, y, width, height,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
vchannel_write("ACK", "%u", serial);
if (!GetWindowRect(hwnd, &rect))
vchannel_debug("GetWindowRect failed!");
else if ((rect.left != x) || (rect.top != y) || (rect.right != x + width)
|| (rect.bottom != y + height))
update_position(hwnd);
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->block_move_hwnd = 0;
memset(&g_shdata->block_move, 0, sizeof(RECT));
ReleaseMutex(g_mutex);
}
EXTERN void
SafeZChange(unsigned int serial, HWND hwnd, HWND behind)
{
if (!g_initialized)
return;
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->blocked_zchange_serial = serial;
g_shdata->blocked_zchange[0] = hwnd_to_long(hwnd);
g_shdata->blocked_zchange[1] = hwnd_to_long(behind);
ReleaseMutex(g_mutex);
LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
if (behind) {
LONG bhstyle = GetWindowLong(behind, GWL_EXSTYLE);
/* Avoid that topmost windows references non-topmost
windows, and vice versa. */
if (exstyle & WS_EX_TOPMOST) {
if (!(bhstyle & WS_EX_TOPMOST)) {
/* Disallow, move to bottom of the topmost
stack. */
/* FIXME: Remains to be implemented */
behind = HWND_TOP;
}
} else {
if (bhstyle & WS_EX_TOPMOST) {
/* Move to top of non-topmost
stack. */
behind = HWND_TOP;
}
}
} else {
behind = HWND_TOP;
}
SetWindowPos(hwnd, behind, 0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->blocked_zchange[0] = 0;
g_shdata->blocked_zchange[1] = 0;
ReleaseMutex(g_mutex);
}
EXTERN void
SafeFocus(unsigned int serial, HWND hwnd)
{
if (!g_initialized)
return;
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->blocked_focus_serial = serial;
g_shdata->blocked_focus = hwnd_to_long(hwnd);
ReleaseMutex(g_mutex);
SendMessage(hwnd, g_wm_seamless_focus, 0, 0);
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->blocked_focus = 0;
ReleaseMutex(g_mutex);
}
EXTERN void
SafeSetState(unsigned int serial, HWND hwnd, int state)
{
LONG style;
int curstate;
if (!g_initialized)
return;
vchannel_block();
style = GetWindowLong(hwnd, GWL_STYLE);
if (style & WS_MAXIMIZE)
curstate = 2;
else if (style & WS_MINIMIZE)
curstate = 1;
else
curstate = 0;
if (state == curstate) {
vchannel_write("ACK", "%u", serial);
vchannel_unblock();
return;
}
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->blocked_state_hwnd = hwnd_to_long(hwnd);
g_shdata->blocked_state_serial = serial;
g_shdata->blocked_state = state;
ReleaseMutex(g_mutex);
vchannel_unblock();
if (state == 0)
ShowWindow(hwnd, SW_RESTORE);
else if (state == 1)
ShowWindow(hwnd, SW_MINIMIZE);
else if (state == 2)
ShowWindow(hwnd, SW_MAXIMIZE);
else
vchannel_debug("Invalid state %d sent.", state);
WaitForSingleObject(g_mutex, INFINITE);
g_shdata->blocked_state_hwnd = 0;
g_shdata->blocked_state = -1;
ReleaseMutex(g_mutex);
}
EXTERN int
GetInstanceCount()
{
if (!g_initialized)
return 0;
return g_shdata->instance_count;
}
BOOL APIENTRY
DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved)
{
HANDLE filemapping = NULL;
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
// remember our instance handle
g_instance = hinstDLL;
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL");
filemapping = CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0, sizeof(shared_variables), "Local\\SeamlessRDPData");
if (filemapping) {
/* From MSDN: The initial contents of
the pages in a file mapping object
backed by the paging file are 0
(zero)." */
g_shdata = MapViewOfFile(filemapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
}
if (g_mutex && filemapping && g_shdata) {
WaitForSingleObject(g_mutex, INFINITE);
++g_shdata->instance_count;
ReleaseMutex(g_mutex);
g_wm_seamless_focus = RegisterWindowMessage(FOCUS_MSG_NAME);
g_initialized = 1;
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
vchannel_write("DESTROYGRP", "0x%08lx, 0x%08lx",
GetCurrentProcessId(), 0);
if (g_mutex) {
WaitForSingleObject(g_mutex, INFINITE);
if (g_shdata) {
--g_shdata->instance_count;
UnmapViewOfFile(g_shdata);
}
ReleaseMutex(g_mutex);
CloseHandle(g_mutex);
}
if (filemapping) {
CloseHandle(filemapping);
}
break;
}
return TRUE;
}