VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSeamless.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.9 KB
Line 
1/* $Id: VBoxSeamless.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBoxSeamless - Seamless windows
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <iprt/assert.h>
33#include <iprt/ldr.h>
34#include <iprt/log.h>
35#include <iprt/mem.h>
36#include <iprt/system.h>
37
38#define _WIN32_WINNT 0x0500
39#include <iprt/win/windows.h>
40
41#include <VBoxHook.h> /* from ../include/ */
42
43#include "VBoxTray.h"
44#include "VBoxTrayInternal.h"
45#include "VBoxHelpers.h"
46#include "VBoxSeamless.h"
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52typedef struct _VBOXSEAMLESSCONTEXT
53{
54 const VBOXSERVICEENV *pEnv;
55
56 RTLDRMOD hModHook;
57
58 BOOL (* pfnVBoxHookInstallWindowTracker)(HMODULE hDll);
59 BOOL (* pfnVBoxHookRemoveWindowTracker)();
60
61 PVBOXDISPIFESCAPE lpEscapeData;
62} VBOXSEAMLESSCONTEXT, *PVBOXSEAMLESSCONTEXT;
63
64typedef struct
65{
66 HDC hdc;
67 HRGN hrgn;
68} VBOX_ENUM_PARAM, *PVBOX_ENUM_PARAM;
69
70
71/*********************************************************************************************************************************
72* Global Variables *
73*********************************************************************************************************************************/
74static VBOXSEAMLESSCONTEXT g_Ctx = { 0 };
75
76
77/*********************************************************************************************************************************
78* Internal Functions *
79*********************************************************************************************************************************/
80void VBoxLogString(HANDLE hDriver, char *pszStr);
81static void vboxSeamlessSetSupported(BOOL fSupported);
82
83
84static DECLCALLBACK(int) VBoxSeamlessInit(const PVBOXSERVICEENV pEnv, void **ppInstance)
85{
86 LogFlowFuncEnter();
87
88 PVBOXSEAMLESSCONTEXT pCtx = &g_Ctx; /* Only one instance at the moment. */
89 AssertPtr(pCtx);
90
91 pCtx->pEnv = pEnv;
92 pCtx->hModHook = NIL_RTLDRMOD;
93
94 int rc;
95
96 /* We have to jump out here when using NT4, otherwise it complains about
97 a missing API function "UnhookWinEvent" used by the dynamically loaded VBoxHook.dll below */
98 uint64_t const uNtVersion = RTSystemGetNtVersion();
99 if (uNtVersion < RTSYSTEM_MAKE_NT_VERSION(5, 0, 0)) /* Windows NT 4.0 or older */
100 {
101 LogRel(("Seamless: Windows NT 4.0 or older not supported!\n"));
102 rc = VERR_NOT_SUPPORTED;
103 }
104 else
105 {
106 /* Will fail if SetWinEventHook is not present (version < NT4 SP6 apparently) */
107 rc = RTLdrLoadAppPriv(VBOXHOOK_DLL_NAME, &pCtx->hModHook);
108 if (RT_SUCCESS(rc))
109 {
110 *(PFNRT *)&pCtx->pfnVBoxHookInstallWindowTracker = RTLdrGetFunction(pCtx->hModHook, "VBoxHookInstallWindowTracker");
111 *(PFNRT *)&pCtx->pfnVBoxHookRemoveWindowTracker = RTLdrGetFunction(pCtx->hModHook, "VBoxHookRemoveWindowTracker");
112
113 if ( pCtx->pfnVBoxHookInstallWindowTracker
114 && pCtx->pfnVBoxHookRemoveWindowTracker)
115 {
116 vboxSeamlessSetSupported(TRUE);
117
118 *ppInstance = pCtx;
119 }
120 else
121 {
122 LogRel(("Seamless: Not supported, skipping\n"));
123 rc = VERR_NOT_SUPPORTED;
124 }
125 }
126 else
127 {
128 LogRel(("Seamless: Could not load %s (%Rrc), skipping\n", VBOXHOOK_DLL_NAME, rc));
129 rc = VERR_NOT_SUPPORTED;
130 }
131 }
132
133 LogFlowFuncLeaveRC(rc);
134 return rc;
135}
136
137static DECLCALLBACK(void) VBoxSeamlessDestroy(void *pInstance)
138{
139 LogFlowFuncEnter();
140
141 if (!pInstance)
142 return;
143
144 PVBOXSEAMLESSCONTEXT pCtx = (PVBOXSEAMLESSCONTEXT)pInstance;
145 AssertPtr(pCtx);
146
147 vboxSeamlessSetSupported(FALSE);
148
149 /* Inform the host that we no longer support the seamless window mode. */
150 if (pCtx->pfnVBoxHookRemoveWindowTracker)
151 pCtx->pfnVBoxHookRemoveWindowTracker();
152 if (pCtx->hModHook != NIL_RTLDRMOD)
153 {
154 RTLdrClose(pCtx->hModHook);
155 pCtx->hModHook = NIL_RTLDRMOD;
156 }
157 return;
158}
159
160static void VBoxSeamlessInstallHook(void)
161{
162 PVBOXSEAMLESSCONTEXT pCtx = &g_Ctx; /** @todo r=andy Use instance data via service lookup (add void *pInstance). */
163 AssertPtr(pCtx);
164
165 if (pCtx->pfnVBoxHookInstallWindowTracker)
166 {
167 /* Check current visible region state */
168 VBoxSeamlessCheckWindows(true);
169
170 HMODULE hMod = (HMODULE)RTLdrGetNativeHandle(pCtx->hModHook);
171 Assert(hMod != (HMODULE)~(uintptr_t)0);
172 pCtx->pfnVBoxHookInstallWindowTracker(hMod);
173 }
174}
175
176static void VBoxSeamlessRemoveHook(void)
177{
178 PVBOXSEAMLESSCONTEXT pCtx = &g_Ctx; /** @todo r=andy Use instance data via service lookup (add void *pInstance). */
179 AssertPtr(pCtx);
180
181 if (pCtx->pfnVBoxHookRemoveWindowTracker)
182 pCtx->pfnVBoxHookRemoveWindowTracker();
183
184 if (pCtx->lpEscapeData)
185 {
186 RTMemFree(pCtx->lpEscapeData);
187 pCtx->lpEscapeData = NULL;
188 }
189}
190
191extern HANDLE g_hSeamlessKmNotifyEvent;
192
193static VBOXDISPIF_SEAMLESS gVBoxDispIfSeamless; /** @todo r=andy Move this into VBOXSEAMLESSCONTEXT? */
194
195
196void VBoxSeamlessEnable(void)
197{
198 PVBOXSEAMLESSCONTEXT pCtx = &g_Ctx; /** @todo r=andy Use instance data via service lookup (add void *pInstance). */
199 AssertPtr(pCtx);
200
201 Assert(g_hSeamlessKmNotifyEvent);
202
203 VBoxDispIfSeamlessCreate(&pCtx->pEnv->dispIf, &gVBoxDispIfSeamless, g_hSeamlessKmNotifyEvent);
204
205 VBoxSeamlessInstallHook();
206}
207
208void VBoxSeamlessDisable(void)
209{
210 PVBOXSEAMLESSCONTEXT pCtx = &g_Ctx; /** @todo r=andy Use instance data via service lookup (add void *pInstance). */
211 AssertPtr(pCtx);
212 NOREF(pCtx);
213
214 VBoxSeamlessRemoveHook();
215
216 VBoxDispIfSeamlessTerm(&gVBoxDispIfSeamless);
217}
218
219void vboxSeamlessSetSupported(BOOL fSupported)
220{
221 VBoxConsoleCapSetSupported(VBOXCAPS_ENTRY_IDX_SEAMLESS, fSupported);
222}
223
224BOOL CALLBACK VBoxEnumFunc(HWND hwnd, LPARAM lParam) RT_NOTHROW_DEF
225{
226 PVBOX_ENUM_PARAM lpParam = (PVBOX_ENUM_PARAM)lParam;
227 DWORD dwStyle, dwExStyle;
228 RECT rectWindow, rectVisible;
229
230 dwStyle = GetWindowLong(hwnd, GWL_STYLE);
231 dwExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
232
233 if ( !(dwStyle & WS_VISIBLE) || (dwStyle & WS_CHILD))
234 return TRUE;
235
236 LogFlow(("VBoxTray: VBoxEnumFunc %x\n", hwnd));
237 /* Only visible windows that are present on the desktop are interesting here */
238 if (!GetWindowRect(hwnd, &rectWindow))
239 {
240 return TRUE;
241 }
242
243 char szWindowText[256];
244 char szWindowClass[256];
245 HWND hStart = NULL;
246
247 szWindowText[0] = 0;
248 szWindowClass[0] = 0;
249
250 GetWindowText(hwnd, szWindowText, sizeof(szWindowText));
251 GetClassName(hwnd, szWindowClass, sizeof(szWindowClass));
252
253 uint64_t const uNtVersion = RTSystemGetNtVersion();
254 if (uNtVersion >= RTSYSTEM_MAKE_NT_VERSION(6, 0, 0))
255 {
256 hStart = ::FindWindowEx(GetDesktopWindow(), NULL, "Button", "Start");
257
258 if ( hwnd == hStart && !strcmp(szWindowText, "Start") )
259 {
260 /* for vista and above. To solve the issue of small bar above
261 * the Start button when mouse is hovered over the start button in seamless mode.
262 * Difference of 7 is observed in Win 7 platform between the dimensions of rectangle with Start title and its shadow.
263 */
264 rectWindow.top += 7;
265 rectWindow.bottom -=7;
266 }
267 }
268
269 rectVisible = rectWindow;
270
271 /* Filter out Windows XP shadow windows */
272 /** @todo still shows inside the guest */
273 if ( szWindowText[0] == 0 &&
274 (dwStyle == (WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS)
275 && dwExStyle == (WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST))
276 || (dwStyle == (WS_POPUP | WS_VISIBLE | WS_DISABLED | WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
277 && dwExStyle == (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_NOACTIVATE))
278 || (dwStyle == (WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
279 && dwExStyle == (WS_EX_TOOLWINDOW)) )
280 {
281 Log(("VBoxTray: Filter out shadow window style=%x exstyle=%x\n", dwStyle, dwExStyle));
282 Log(("VBoxTray: Enum hwnd=%x rect (%d,%d) (%d,%d) (filtered)\n", hwnd, rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom));
283 Log(("VBoxTray: title=%s style=%x exStyle=%x\n", szWindowText, dwStyle, dwExStyle));
284 return TRUE;
285 }
286
287 /** Such a windows covers the whole screen making desktop background*/
288 if (strcmp(szWindowText, "Program Manager") && strcmp(szWindowClass, "ApplicationFrameWindow"))
289 {
290 Log(("VBoxTray: Enum hwnd=%x rect (%d,%d)-(%d,%d) [%d x %d](applying)\n", hwnd,
291 rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom,
292 rectWindow.left - rectWindow.right, rectWindow.bottom - rectWindow.top));
293 Log(("VBoxTray: title=%s style=%x exStyle=%x\n", szWindowText, dwStyle, dwExStyle));
294
295 HRGN hrgn = CreateRectRgn(0, 0, 0, 0);
296
297 int ret = GetWindowRgn(hwnd, hrgn);
298
299 if (ret == ERROR)
300 {
301 Log(("VBoxTray: GetWindowRgn failed with rc=%d, adding antire rect\n", GetLastError()));
302 SetRectRgn(hrgn, rectVisible.left, rectVisible.top, rectVisible.right, rectVisible.bottom);
303 }
304 else
305 {
306 /* this region is relative to the window origin instead of the desktop origin */
307 OffsetRgn(hrgn, rectWindow.left, rectWindow.top);
308 }
309
310 if (lpParam->hrgn)
311 {
312 /* create a union of the current visible region and the visible rectangle of this window. */
313 CombineRgn(lpParam->hrgn, lpParam->hrgn, hrgn, RGN_OR);
314 DeleteObject(hrgn);
315 }
316 else
317 lpParam->hrgn = hrgn;
318 }
319 else
320 {
321 Log(("VBoxTray: Enum hwnd=%x rect (%d,%d)-(%d,%d) [%d x %d](ignored)\n", hwnd,
322 rectWindow.left, rectWindow.top, rectWindow.right, rectWindow.bottom,
323 rectWindow.left - rectWindow.right, rectWindow.bottom - rectWindow.top));
324 Log(("VBoxTray: title=%s style=%x exStyle=%x\n", szWindowText, dwStyle, dwExStyle));
325 }
326
327 return TRUE; /* continue enumeration */
328}
329
330void VBoxSeamlessCheckWindows(bool fForce)
331{
332 PVBOXSEAMLESSCONTEXT pCtx = &g_Ctx; /** @todo r=andy Use instance data via service lookup (add void *pInstance). */
333 AssertPtr(pCtx);
334
335 if (!VBoxDispIfSeamlesIsValid(&gVBoxDispIfSeamless))
336 return;
337
338 VBOX_ENUM_PARAM param;
339
340 param.hdc = GetDC(HWND_DESKTOP);
341 param.hrgn = 0;
342
343 EnumWindows(VBoxEnumFunc, (LPARAM)&param);
344
345 if (param.hrgn)
346 {
347 DWORD cbSize = GetRegionData(param.hrgn, 0, NULL);
348 if (cbSize)
349 {
350 PVBOXDISPIFESCAPE lpEscapeData = (PVBOXDISPIFESCAPE)RTMemAllocZ(VBOXDISPIFESCAPE_SIZE(cbSize));
351 if (lpEscapeData)
352 {
353 lpEscapeData->escapeCode = VBOXESC_SETVISIBLEREGION;
354 LPRGNDATA lpRgnData = VBOXDISPIFESCAPE_DATA(lpEscapeData, RGNDATA);
355
356 cbSize = GetRegionData(param.hrgn, cbSize, lpRgnData);
357 if (cbSize)
358 {
359#ifdef LOG_ENABLED
360 RECT *paRects = (RECT *)&lpRgnData->Buffer[0];
361 Log(("VBoxTray: New visible region: \n"));
362 for (DWORD i = 0; i < lpRgnData->rdh.nCount; i++)
363 Log(("VBoxTray: visible rect (%d,%d)(%d,%d)\n",
364 paRects[i].left, paRects[i].top, paRects[i].right, paRects[i].bottom));
365#endif
366
367 LPRGNDATA lpCtxRgnData = VBOXDISPIFESCAPE_DATA(pCtx->lpEscapeData, RGNDATA);
368
369 if ( fForce
370 || !pCtx->lpEscapeData
371 || (lpCtxRgnData->rdh.dwSize + lpCtxRgnData->rdh.nRgnSize != cbSize)
372 || memcmp(lpCtxRgnData, lpRgnData, cbSize))
373 {
374 /* send to display driver */
375 VBoxDispIfSeamlessSubmit(&gVBoxDispIfSeamless, lpEscapeData, cbSize);
376
377 if (pCtx->lpEscapeData)
378 RTMemFree(pCtx->lpEscapeData);
379 pCtx->lpEscapeData = lpEscapeData;
380 }
381 else
382 Log(("VBoxTray: Visible rectangles haven't changed; ignore\n"));
383 }
384 if (lpEscapeData != pCtx->lpEscapeData)
385 RTMemFree(lpEscapeData);
386 }
387 }
388
389 DeleteObject(param.hrgn);
390 }
391
392 ReleaseDC(HWND_DESKTOP, param.hdc);
393}
394
395/**
396 * Thread function to wait for and process seamless mode change
397 * requests
398 */
399static DECLCALLBACK(int) VBoxSeamlessWorker(void *pvInstance, bool volatile *pfShutdown)
400{
401 AssertPtrReturn(pvInstance, VERR_INVALID_POINTER);
402 LogFlowFunc(("pvInstance=%p\n", pvInstance));
403
404 /*
405 * Tell the control thread that it can continue spawning services.
406 */
407 RTThreadUserSignal(RTThreadSelf());
408
409 int rc = VbglR3CtlFilterMask(VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST, 0 /*fNot*/);
410 if (RT_FAILURE(rc))
411 {
412 LogRel(("Seamless: VbglR3CtlFilterMask(VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST,0) failed with %Rrc, exiting ...\n", rc));
413 return rc;
414 }
415
416 BOOL fWasScreenSaverActive = FALSE;
417 for (;;)
418 {
419 /*
420 * Wait for a seamless change event, check for shutdown both before and after.
421 */
422 if (*pfShutdown)
423 {
424 rc = VINF_SUCCESS;
425 break;
426 }
427
428 /** @todo r=andy We do duplicate code here (see VbglR3SeamlessWaitEvent()). */
429 uint32_t fEvent = 0;
430 rc = VbglR3WaitEvent(VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST, 5000 /*ms*/, &fEvent);
431
432 if (*pfShutdown)
433 {
434 rc = VINF_SUCCESS;
435 break;
436 }
437
438 if (RT_SUCCESS(rc))
439 {
440 /* did we get the right event? */
441 if (fEvent & VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST)
442 {
443 /*
444 * We got at least one event. Read the requested resolution
445 * and try to set it until success. New events will not be seen
446 * but a new resolution will be read in this poll loop.
447 */
448 for (;;)
449 {
450 /* get the seamless change request */
451 VMMDevSeamlessMode enmMode = (VMMDevSeamlessMode)-1;
452 rc = VbglR3SeamlessGetLastEvent(&enmMode);
453 if (RT_SUCCESS(rc))
454 {
455 LogFlowFunc(("Mode changed to %d\n", enmMode));
456
457 BOOL fRet;
458 switch (enmMode)
459 {
460 case VMMDev_Seamless_Disabled:
461 if (fWasScreenSaverActive)
462 {
463 LogRel(("Seamless: Re-enabling the screensaver\n"));
464 fRet = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
465 if (!fRet)
466 LogRel(("Seamless: SystemParametersInfo SPI_SETSCREENSAVEACTIVE failed with %ld\n", GetLastError()));
467 }
468 PostMessage(g_hwndToolWindow, WM_VBOX_SEAMLESS_DISABLE, 0, 0);
469 break;
470
471 case VMMDev_Seamless_Visible_Region:
472 fRet = SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &fWasScreenSaverActive, 0);
473 if (!fRet)
474 LogRel(("Seamless: SystemParametersInfo SPI_GETSCREENSAVEACTIVE failed with %ld\n", GetLastError()));
475
476 if (fWasScreenSaverActive)
477 LogRel(("Seamless: Disabling the screensaver\n"));
478
479 fRet = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
480 if (!fRet)
481 LogRel(("Seamless: SystemParametersInfo SPI_SETSCREENSAVEACTIVE failed with %ld\n", GetLastError()));
482 PostMessage(g_hwndToolWindow, WM_VBOX_SEAMLESS_ENABLE, 0, 0);
483 break;
484
485 case VMMDev_Seamless_Host_Window:
486 break;
487
488 default:
489 AssertFailed();
490 break;
491 }
492 break;
493 }
494
495 LogRel(("Seamless: VbglR3SeamlessGetLastEvent() failed with %Rrc\n", rc));
496
497 if (*pfShutdown)
498 break;
499
500 /* sleep a bit to not eat too much CPU while retrying */
501 RTThreadSleep(10);
502 }
503 }
504 }
505 /* sleep a bit to not eat too much CPU in case the above call always fails */
506 else if (rc != VERR_TIMEOUT)
507 RTThreadSleep(10);
508 }
509
510 int rc2 = VbglR3CtlFilterMask(0 /*fOk*/, VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST);
511 if (RT_FAILURE(rc2))
512 LogRel(("Seamless: VbglR3CtlFilterMask(0, VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST) failed with %Rrc\n", rc));
513
514 LogFlowFuncLeaveRC(rc);
515 return rc;
516}
517
518/**
519 * The service description.
520 */
521VBOXSERVICEDESC g_SvcDescSeamless =
522{
523 /* pszName. */
524 "seamless",
525 /* pszDescription. */
526 "Seamless Windows",
527 /* methods */
528 VBoxSeamlessInit,
529 VBoxSeamlessWorker,
530 NULL /* pfnStop */,
531 VBoxSeamlessDestroy
532};
533
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use