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
|
/* $Id: VBoxSeamless.cpp $ */
/** @file
* VBoxSeamless - Seamless windows
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#define _WIN32_WINNT 0x0500
#include <Windows.h>
#include "VBoxTray.h"
#include "VBoxHelpers.h"
#include "VBoxSeamless.h"
#include <VBoxHook.h>
#include <VBoxDisplay.h>
#include <VBox/VMMDev.h>
#include <iprt/assert.h>
#include <iprt/ldr.h>
#include <VBoxGuestInternal.h>
typedef struct _VBOXSEAMLESSCONTEXT
{
const VBOXSERVICEENV *pEnv;
RTLDRMOD hModHook;
BOOL (* pfnVBoxHookInstallWindowTracker)(HMODULE hDll);
BOOL (* pfnVBoxHookRemoveWindowTracker)();
PVBOXDISPIFESCAPE lpEscapeData;
} VBOXSEAMLESSCONTEXT;
typedef struct
{
HDC hdc;
HRGN hrgn;
} VBOX_ENUM_PARAM, *PVBOX_ENUM_PARAM;
static VBOXSEAMLESSCONTEXT gCtx = {0};
void VBoxLogString(HANDLE hDriver, char *pszStr);
int VBoxSeamlessInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
{
Log(("VBoxTray: VBoxSeamlessInit\n"));
*pfStartThread = false;
gCtx.pEnv = pEnv;
gCtx.hModHook = NIL_RTLDRMOD;
OSVERSIONINFO OSinfo;
OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
GetVersionEx (&OSinfo);
int rc = VINF_SUCCESS;
/* We have to jump out here when using NT4, otherwise it complains about
a missing API function "UnhookWinEvent" used by the dynamically loaded VBoxHook.dll below */
if (OSinfo.dwMajorVersion <= 4) /* Windows NT 4.0 or older */
{
Log(("VBoxTray: VBoxSeamlessInit: Windows NT 4.0 or older not supported!\n"));
rc = VERR_NOT_SUPPORTED;
}
else
{
/* Will fail if SetWinEventHook is not present (version < NT4 SP6 apparently) */
rc = RTLdrLoadAppPriv(VBOXHOOK_DLL_NAME, &gCtx.hModHook);
if (RT_SUCCESS(rc))
{
*(PFNRT *)&gCtx.pfnVBoxHookInstallWindowTracker = RTLdrGetFunction(gCtx.hModHook, "VBoxHookInstallWindowTracker");
*(PFNRT *)&gCtx.pfnVBoxHookRemoveWindowTracker = RTLdrGetFunction(gCtx.hModHook, "VBoxHookRemoveWindowTracker");
/* rc should contain success status */
AssertRC(rc);
VBoxSeamlessSetSupported(TRUE);
// if (RT_SUCCESS(rc))
{
*pfStartThread = true;
*ppInstance = &gCtx;
}
}
else
Log(("VBoxTray: VBoxSeamlessInit: LoadLibrary of \"%s\" failed with rc=%Rrc\n", VBOXHOOK_DLL_NAME, rc));
}
return rc;
}
void VBoxSeamlessDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
{
Log(("VBoxTray: VBoxSeamlessDestroy\n"));
VBoxSeamlessSetSupported(FALSE);
/* Inform the host that we no longer support the seamless window mode. */
if (gCtx.pfnVBoxHookRemoveWindowTracker)
gCtx.pfnVBoxHookRemoveWindowTracker();
if (gCtx.hModHook != NIL_RTLDRMOD)
{
RTLdrClose(gCtx.hModHook);
gCtx.hModHook = NIL_RTLDRMOD;
}
return;
}
static void VBoxSeamlessInstallHook()
{
if (gCtx.pfnVBoxHookInstallWindowTracker)
{
/* Check current visible region state */
VBoxSeamlessCheckWindows(true);
HMODULE hMod = (HMODULE)RTLdrGetNativeHandle(gCtx.hModHook);
Assert(hMod != (HMODULE)~(uintptr_t)0);
gCtx.pfnVBoxHookInstallWindowTracker(hMod);
}
}
static void VBoxSeamlessRemoveHook()
{
if (gCtx.pfnVBoxHookRemoveWindowTracker)
gCtx.pfnVBoxHookRemoveWindowTracker();
if (gCtx.lpEscapeData)
{
free(gCtx.lpEscapeData);
gCtx.lpEscapeData = NULL;
}
}
extern HANDLE ghSeamlessKmNotifyEvent;
static VBOXDISPIF_SEAMLESS gVBoxDispIfSeamless;
void VBoxSeamlessEnable()
{
Assert(ghSeamlessKmNotifyEvent);
VBoxDispIfSeamlesCreate(&gCtx.pEnv->dispIf, &gVBoxDispIfSeamless, ghSeamlessKmNotifyEvent);
VBoxSeamlessInstallHook();
}
void VBoxSeamlessDisable()
{
VBoxSeamlessRemoveHook();
VBoxDispIfSeamlesTerm(&gVBoxDispIfSeamless);
}
BOOL CALLBACK VBoxEnumFunc(HWND hwnd, LPARAM lParam)
{
PVBOX_ENUM_PARAM lpParam = (PVBOX_ENUM_PARAM)lParam;
DWORD dwStyle, dwExStyle;
RECT rectWindow, rectVisible;
dwStyle = GetWindowLong(hwnd, GWL_STYLE);
dwExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
if ( !(dwStyle & WS_VISIBLE)
|| (dwStyle & WS_CHILD))
return TRUE;
Log(("VBoxTray: VBoxEnumFunc %x\n", hwnd));
/* Only visible windows that are present on the desktop are interesting here */
if (GetWindowRect(hwnd, &rectWindow))
{
char szWindowText[256];
szWindowText[0] = 0;
OSVERSIONINFO OSinfo;
HWND hStart = NULL;
GetWindowText(hwnd, szWindowText, sizeof(szWindowText));
OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
GetVersionEx (&OSinfo);
if (OSinfo.dwMajorVersion >= 6)
{
hStart = ::FindWindowEx(GetDesktopWindow(), NULL, "Button", "Start");
if ( hwnd == hStart && szWindowText != NULL
&& !(strcmp(szWindowText, "Start"))
)
{
/* for vista and above. To solve the issue of small bar above
* the Start button when mouse is hovered over the start button in seamless mode.
* Difference of 7 is observed in Win 7 platform between the dimensionsof rectangle with Start title and its shadow.
*/
rectWindow.top += 7;
rectWindow.bottom -=7;
}
}
rectVisible = rectWindow;
#ifdef LOG_ENABLED
DWORD pid = 0;
DWORD tid = GetWindowThreadProcessId(hwnd, &pid);
#endif
/* Filter out Windows XP shadow windows */
/** @todo still shows inside the guest */
if ( szWindowText[0] == 0
&& (
(dwStyle == (WS_POPUP|WS_VISIBLE|WS_CLIPSIBLINGS)
&& dwExStyle == (WS_EX_LAYERED|WS_EX_TOOLWINDOW|WS_EX_TRANSPARENT|WS_EX_TOPMOST))
|| (dwStyle == (WS_POPUP|WS_VISIBLE|WS_DISABLED|WS_CLIPSIBLINGS|WS_CLIPCHILDREN)
&& dwExStyle == (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_NOACTIVATE))
|| (dwStyle == (WS_POPUP|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN)
&& dwExStyle == (WS_EX_TOOLWINDOW))
))
{
Log(("VBoxTray: Filter out shadow window style=%x exstyle=%x\n", dwStyle, dwExStyle));
Log(("VBoxTray: Enum hwnd=%x rect (%d,%d) (%d,%d) (filtered)\n", hwnd, rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom));
Log(("VBoxTray: title=%s style=%x exStyle=%x\n", szWindowText, dwStyle, dwExStyle));
Log(("VBoxTray: pid=%d tid=%d\n", pid, tid));
return TRUE;
}
/** @todo will this suffice? The Program Manager window covers the whole screen */
if (strcmp(szWindowText, "Program Manager"))
{
Log(("VBoxTray: Enum hwnd=%x rect (%d,%d) (%d,%d) (applying)\n", hwnd, rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom));
Log(("VBoxTray: title=%s style=%x exStyle=%x\n", szWindowText, dwStyle, dwExStyle));
Log(("VBoxTray: pid=%d tid=%d\n", pid, tid));
HRGN hrgn = CreateRectRgn(0,0,0,0);
int ret = GetWindowRgn(hwnd, hrgn);
if (ret == ERROR)
{
Log(("VBoxTray: GetWindowRgn failed with rc=%d\n", GetLastError()));
SetRectRgn(hrgn, rectVisible.left, rectVisible.top, rectVisible.right, rectVisible.bottom);
}
else
{
/* this region is relative to the window origin instead of the desktop origin */
OffsetRgn(hrgn, rectWindow.left, rectWindow.top);
}
if (lpParam->hrgn)
{
/* create a union of the current visible region and the visible rectangle of this window. */
CombineRgn(lpParam->hrgn, lpParam->hrgn, hrgn, RGN_OR);
DeleteObject(hrgn);
}
else
lpParam->hrgn = hrgn;
}
else
{
Log(("VBoxTray: Enum hwnd=%x rect (%d,%d) (%d,%d) (ignored)\n", hwnd, rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom));
Log(("VBoxTray: title=%s style=%x\n", szWindowText, dwStyle));
Log(("VBoxTray: pid=%d tid=%d\n", pid, tid));
}
}
return TRUE; /* continue enumeration */
}
void VBoxSeamlessCheckWindows(bool fForce)
{
if (!VBoxDispIfSeamlesIsValid(&gVBoxDispIfSeamless))
return;
VBOX_ENUM_PARAM param;
param.hdc = GetDC(HWND_DESKTOP);
param.hrgn = 0;
EnumWindows(VBoxEnumFunc, (LPARAM)¶m);
if (param.hrgn)
{
DWORD cbSize;
cbSize = GetRegionData(param.hrgn, 0, NULL);
if (cbSize)
{
PVBOXDISPIFESCAPE lpEscapeData = (PVBOXDISPIFESCAPE)malloc(VBOXDISPIFESCAPE_SIZE(cbSize));
if (lpEscapeData)
{
lpEscapeData->escapeCode = VBOXESC_SETVISIBLEREGION;
LPRGNDATA lpRgnData = VBOXDISPIFESCAPE_DATA(lpEscapeData, RGNDATA);
memset(lpRgnData, 0, cbSize);
cbSize = GetRegionData(param.hrgn, cbSize, lpRgnData);
if (cbSize)
{
#ifdef DEBUG
RECT *lpRect = (RECT *)&lpRgnData->Buffer[0];
Log(("VBoxTray: New visible region: \n"));
for (DWORD i=0;i<lpRgnData->rdh.nCount;i++)
{
Log(("VBoxTray: visible rect (%d,%d)(%d,%d)\n", lpRect[i].left, lpRect[i].top, lpRect[i].right, lpRect[i].bottom));
}
#endif
LPRGNDATA lpCtxRgnData = VBOXDISPIFESCAPE_DATA(gCtx.lpEscapeData, RGNDATA);
if (fForce
|| !gCtx.lpEscapeData
|| (lpCtxRgnData->rdh.dwSize + lpCtxRgnData->rdh.nRgnSize != cbSize)
|| memcmp(lpCtxRgnData, lpRgnData, cbSize))
{
/* send to display driver */
VBoxDispIfSeamlesSubmit(&gVBoxDispIfSeamless, lpEscapeData, cbSize);
if (gCtx.lpEscapeData)
free(gCtx.lpEscapeData);
gCtx.lpEscapeData = lpEscapeData;
}
else
Log(("VBoxTray: Visible rectangles haven't changed; ignore\n"));
}
if (lpEscapeData != gCtx.lpEscapeData)
free(lpEscapeData);
}
}
DeleteObject(param.hrgn);
}
ReleaseDC(HWND_DESKTOP, param.hdc);
}
/**
* Thread function to wait for and process seamless mode change
* requests
*/
unsigned __stdcall VBoxSeamlessThread(void *pInstance)
{
VBOXSEAMLESSCONTEXT *pCtx = (VBOXSEAMLESSCONTEXT *)pInstance;
HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
bool fTerminate = false;
VBoxGuestFilterMaskInfo maskInfo;
DWORD cbReturned;
BOOL fWasScreenSaverActive = FALSE, ret;
maskInfo.u32OrMask = VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST;
maskInfo.u32NotMask = 0;
if (DeviceIoControl (gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
{
Log(("VBoxTray: VBoxSeamlessThread: DeviceIOControl(CtlMask - or) succeeded\n"));
}
else
{
Log(("VBoxTray: VBoxSeamlessThread: DeviceIOControl(CtlMask) failed, SeamlessChangeThread exited\n"));
return 0;
}
do
{
/* wait for a seamless change event */
VBoxGuestWaitEventInfo waitEvent;
waitEvent.u32TimeoutIn = 5000;
waitEvent.u32EventMaskIn = VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST;
if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
{
Log(("VBoxTray: VBoxSeamlessThread: DeviceIOControl succeeded\n"));
/* are we supposed to stop? */
if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
break;
Log(("VBoxTray: VBoxSeamlessThread: checking event\n"));
/* did we get the right event? */
if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST)
{
Log(("VBoxTray: VBoxTray: going to get seamless change information\n"));
/* We got at least one event. Read the requested resolution
* and try to set it until success. New events will not be seen
* but a new resolution will be read in this poll loop.
*/
for (;;)
{
/* get the seamless change request */
VMMDevSeamlessChangeRequest seamlessChangeRequest = {0};
vmmdevInitRequest((VMMDevRequestHeader*)&seamlessChangeRequest, VMMDevReq_GetSeamlessChangeRequest);
seamlessChangeRequest.eventAck = VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST;
BOOL fSeamlessChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(seamlessChangeRequest)), &seamlessChangeRequest, sizeof(seamlessChangeRequest),
&seamlessChangeRequest, sizeof(seamlessChangeRequest), &cbReturned, NULL);
if (fSeamlessChangeQueried)
{
Log(("VBoxTray: VBoxSeamlessThread: mode change to %d\n", seamlessChangeRequest.mode));
switch(seamlessChangeRequest.mode)
{
case VMMDev_Seamless_Disabled:
if (fWasScreenSaverActive)
{
Log(("VBoxTray: Re-enabling the screensaver\n"));
ret = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
if (!ret)
Log(("VBoxTray: SystemParametersInfo SPI_SETSCREENSAVEACTIVE failed with %d\n", GetLastError()));
}
PostMessage(ghwndToolWindow, WM_VBOX_SEAMLESS_DISABLE, 0, 0);
break;
case VMMDev_Seamless_Visible_Region:
ret = SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &fWasScreenSaverActive, 0);
if (!ret)
Log(("VBoxTray: SystemParametersInfo SPI_GETSCREENSAVEACTIVE failed with %d\n", GetLastError()));
if (fWasScreenSaverActive)
Log(("VBoxTray: Disabling the screensaver\n"));
ret = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
if (!ret)
Log(("VBoxTray: SystemParametersInfo SPI_SETSCREENSAVEACTIVE failed with %d\n", GetLastError()));
PostMessage(ghwndToolWindow, WM_VBOX_SEAMLESS_ENABLE, 0, 0);
break;
case VMMDev_Seamless_Host_Window:
break;
default:
AssertFailed();
break;
}
break;
}
else
{
Log(("VBoxTray: VBoxSeamlessThread: error from DeviceIoControl VBOXGUEST_IOCTL_VMMREQUEST\n"));
}
/* sleep a bit to not eat too much CPU while retrying */
/* are we supposed to stop? */
if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 50) == WAIT_OBJECT_0)
{
fTerminate = true;
break;
}
}
}
}
else
{
Log(("VBoxTray: VBoxTray: error 0 from DeviceIoControl VBOXGUEST_IOCTL_WAITEVENT\n"));
/* sleep a bit to not eat too much CPU in case the above call always fails */
if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
{
fTerminate = true;
break;
}
}
}
while (!fTerminate);
maskInfo.u32OrMask = 0;
maskInfo.u32NotMask = VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST;
if (DeviceIoControl (gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
{
Log(("VBoxTray: VBoxSeamlessThread: DeviceIOControl(CtlMask - not) succeeded\n"));
}
else
{
Log(("VBoxTray: VBoxSeamlessThread: DeviceIOControl(CtlMask) failed\n"));
}
Log(("VBoxTray: VBoxSeamlessThread: finished seamless change request thread\n"));
return 0;
}
|