VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxDisplay.cpp@ 106411

Last change on this file since 106411 was 106411, checked in by vboxsync, 4 months ago

Additions/VBoxTray: Implemented ability for easier user-controllable logging (also via verbose levels), support for running in foreground mode (with a console window attached to) and selective starting of sub services to easier pinpoint errors in release builds. Cleaned up initialization / termination code a little. See command line help for new options. bugref:10763

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 38.9 KB
Line 
1/* $Id: VBoxDisplay.cpp 106411 2024-10-17 07:44:43Z vboxsync $ */
2/** @file
3 * VBoxSeamless - Display notifications.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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 "VBoxTray.h"
33#include "VBoxHelpers.h"
34#include "VBoxSeamless.h"
35
36#include <iprt/alloca.h>
37#include <iprt/assert.h>
38#ifdef VBOX_WITH_WDDM
39# include <iprt/asm.h>
40#endif
41#include <iprt/log.h>
42#include <iprt/system.h>
43
44#include <VBoxDisplay.h>
45#include <VBoxHook.h>
46
47
48/*********************************************************************************************************************************
49* Structures and Typedefs *
50*********************************************************************************************************************************/
51typedef struct _VBOXDISPLAYCONTEXT
52{
53 const VBOXTRAYSVCENV *pEnv;
54 BOOL fAnyX;
55 /** ChangeDisplaySettingsEx does not exist in NT. ResizeDisplayDevice uses the function. */
56 DECLCALLBACKMEMBER_EX(LONG,WINAPI, pfnChangeDisplaySettingsEx,(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd,
57 DWORD dwflags, LPVOID lParam));
58 /** EnumDisplayDevices does not exist in NT. */
59 DECLCALLBACKMEMBER_EX(BOOL, WINAPI, pfnEnumDisplayDevices,(IN LPCSTR lpDevice, IN DWORD iDevNum,
60 OUT PDISPLAY_DEVICEA lpDisplayDevice, IN DWORD dwFlags));
61 /** Display driver interface, XPDM - WDDM abstraction see VBOXDISPIF** definitions above */
62 VBOXDISPIF dispIf;
63} VBOXDISPLAYCONTEXT, *PVBOXDISPLAYCONTEXT;
64
65typedef enum
66{
67 VBOXDISPLAY_DRIVER_TYPE_UNKNOWN = 0,
68 VBOXDISPLAY_DRIVER_TYPE_XPDM = 1,
69 VBOXDISPLAY_DRIVER_TYPE_WDDM = 2
70} VBOXDISPLAY_DRIVER_TYPE;
71
72
73/*********************************************************************************************************************************
74* Global Variables *
75*********************************************************************************************************************************/
76static VBOXDISPLAYCONTEXT g_Ctx = { 0 };
77
78
79/*********************************************************************************************************************************
80* Internal Functions *
81*********************************************************************************************************************************/
82static VBOXDISPLAY_DRIVER_TYPE getVBoxDisplayDriverType(VBOXDISPLAYCONTEXT *pCtx);
83
84
85/**
86 * @interface_method_impl{VBOXSERVICEDESC,pfnPreInit}
87 */
88static DECLCALLBACK(int) vbtrDispPreInit(void)
89{
90 return VINF_SUCCESS;
91}
92
93/**
94 * @interface_method_impl{VBOXSERVICEDESC,pfnOption}
95 */
96static DECLCALLBACK(int) vbtrDispOption(const char **ppszShort, int argc, char **argv, int *pi)
97{
98 RT_NOREF(ppszShort, argc, argv, pi);
99
100 return -1;
101}
102
103/**
104 * @interface_method_impl{VBOXSERVICEDESC,pfnInit}
105 */
106static DECLCALLBACK(int) vbtrDisplayInit(const PVBOXTRAYSVCENV pEnv, void **ppInstance)
107{
108 LogFlowFuncEnter();
109
110 PVBOXDISPLAYCONTEXT pCtx = &g_Ctx; /** @todo r=andy Use instance data via service lookup (add void *pInstance). */
111 AssertPtr(pCtx);
112
113 int rc;
114 HMODULE hUser = GetModuleHandle("user32.dll"); /** @todo r=andy Use RTLdrXXX and friends. */
115
116 pCtx->pEnv = pEnv;
117
118 uint64_t const uNtVersion = RTSystemGetNtVersion();
119
120 if (NULL == hUser)
121 {
122 LogFlowFunc(("Could not get module handle of USER32.DLL!\n"));
123 rc = VERR_NOT_IMPLEMENTED;
124 }
125 else if (uNtVersion >= RTSYSTEM_MAKE_NT_VERSION(5, 0, 0)) /* APIs available only on W2K and up. */
126 {
127 /** @todo r=andy Use RTLdrXXX and friends. */
128 /** @todo r=andy No unicode version available? */
129 *(uintptr_t *)&pCtx->pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
130 LogFlowFunc(("pfnChangeDisplaySettingsEx = %p\n", pCtx->pfnChangeDisplaySettingsEx));
131
132 *(uintptr_t *)&pCtx->pfnEnumDisplayDevices = (uintptr_t)GetProcAddress(hUser, "EnumDisplayDevicesA");
133 LogFlowFunc(("pfnEnumDisplayDevices = %p\n", pCtx->pfnEnumDisplayDevices));
134
135#ifdef VBOX_WITH_WDDM
136 if (uNtVersion >= RTSYSTEM_MAKE_NT_VERSION(6, 0, 0))
137 {
138 /* This is Vista and up, check if we need to switch the display driver if to WDDM mode. */
139 LogFlowFunc(("this is Windows Vista and up\n"));
140 VBOXDISPLAY_DRIVER_TYPE enmType = getVBoxDisplayDriverType(pCtx);
141 if (enmType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
142 {
143 LogFlowFunc(("WDDM driver is installed, switching display driver if to WDDM mode\n"));
144 /* This is hacky, but the most easiest way. */
145 VBOXDISPIF_MODE enmMode = uNtVersion < RTSYSTEM_MAKE_NT_VERSION(6, 1, 0)
146 ? VBOXDISPIF_MODE_WDDM : VBOXDISPIF_MODE_WDDM_W7;
147 DWORD dwErr = VBoxDispIfSwitchMode(const_cast<PVBOXDISPIF>(&pEnv->dispIf), enmMode, NULL /* old mode, we don't care about it */);
148 if (dwErr == NO_ERROR)
149 {
150 LogFlowFunc(("DispIf successfully switched to WDDM mode\n"));
151 rc = VINF_SUCCESS;
152 }
153 else
154 {
155 LogFlowFunc(("Failed to switch DispIf to WDDM mode, error (%d)\n", dwErr));
156 rc = RTErrConvertFromWin32(dwErr);
157 }
158 }
159 else
160 rc = VINF_SUCCESS;
161 }
162 else
163 rc = VINF_SUCCESS;
164#endif
165 }
166 else if (uNtVersion < RTSYSTEM_MAKE_NT_VERSION(5, 0, 0)) /* Windows NT 4.0. */
167 {
168 /* Nothing to do here yet. */
169 /** @todo r=andy Has this been tested? */
170 rc = VINF_SUCCESS;
171 }
172 else /* Unsupported platform. */
173 {
174 LogFlowFunc(("Warning: Display for platform not handled yet!\n"));
175 rc = VERR_NOT_IMPLEMENTED;
176 }
177
178 if (RT_SUCCESS(rc))
179 {
180 VBOXDISPIFESCAPE_ISANYX IsAnyX = { {0} };
181 IsAnyX.EscapeHdr.escapeCode = VBOXESC_ISANYX;
182 DWORD err = VBoxDispIfEscapeInOut(&pEnv->dispIf, &IsAnyX.EscapeHdr, sizeof(uint32_t));
183 if (err == NO_ERROR)
184 pCtx->fAnyX = !!IsAnyX.u32IsAnyX;
185 else
186 pCtx->fAnyX = TRUE;
187
188 *ppInstance = pCtx;
189 }
190
191 LogFlowFuncLeaveRC(rc);
192 return rc;
193}
194
195/**
196 * @interface_method_impl{VBOXSERVICEDESC,pfnDestroy}
197 */
198static DECLCALLBACK(void) vbtrDisplayDestroy(void *pInstance)
199{
200 RT_NOREF(pInstance);
201 return;
202}
203
204static VBOXDISPLAY_DRIVER_TYPE getVBoxDisplayDriverType(PVBOXDISPLAYCONTEXT pCtx)
205{
206 VBOXDISPLAY_DRIVER_TYPE enmType = VBOXDISPLAY_DRIVER_TYPE_UNKNOWN;
207
208 if (pCtx->pfnEnumDisplayDevices)
209 {
210 INT devNum = 0;
211 DISPLAY_DEVICE dispDevice;
212 FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
213 dispDevice.cb = sizeof(DISPLAY_DEVICE);
214
215 LogFlowFunc(("getVBoxDisplayDriverType: Checking for active VBox display driver (W2K+) ...\n"));
216
217 while (EnumDisplayDevices(NULL,
218 devNum,
219 &dispDevice,
220 0))
221 {
222 LogFlowFunc(("getVBoxDisplayDriverType: DevNum:%d\nName:%s\nString:%s\nID:%s\nKey:%s\nFlags=%08X\n\n",
223 devNum,
224 &dispDevice.DeviceName[0],
225 &dispDevice.DeviceString[0],
226 &dispDevice.DeviceID[0],
227 &dispDevice.DeviceKey[0],
228 dispDevice.StateFlags));
229
230 if (dispDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
231 {
232 LogFlowFunc(("getVBoxDisplayDriverType: Primary device\n"));
233
234 /* WDDM driver can now have multiple incarnations,
235 * if the driver name contains VirtualBox, and does NOT match the XPDM name,
236 * assume it to be WDDM */
237 if (strcmp(&dispDevice.DeviceString[0], "VirtualBox Graphics Adapter") == 0)
238 enmType = VBOXDISPLAY_DRIVER_TYPE_XPDM;
239 else if (strstr(&dispDevice.DeviceString[0], "VirtualBox"))
240 enmType = VBOXDISPLAY_DRIVER_TYPE_WDDM;
241
242 break;
243 }
244
245 FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
246
247 dispDevice.cb = sizeof(DISPLAY_DEVICE);
248
249 devNum++;
250 }
251 }
252 else /* This must be NT 4 or something really old, so don't use EnumDisplayDevices() here ... */
253 {
254 LogFlowFunc(("getVBoxDisplayDriverType: Checking for active VBox display driver (NT or older) ...\n"));
255
256 DEVMODE tempDevMode;
257 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
258 tempDevMode.dmSize = sizeof(DEVMODE);
259 EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &tempDevMode); /* Get current display device settings */
260
261 /* Check for the short name, because all long stuff would be truncated */
262 if (strcmp((char*)&tempDevMode.dmDeviceName[0], "VBoxDisp") == 0)
263 enmType = VBOXDISPLAY_DRIVER_TYPE_XPDM;
264 }
265
266 return enmType;
267}
268
269/** @todo r=andy The "display", "seamless" (and VBoxCaps facility in VBoxTray.cpp indirectly) is using this.
270 * Add a PVBOXDISPLAYCONTEXT here for properly getting the display (XPDM/WDDM) abstraction interfaces. */
271DWORD EnableAndResizeDispDev(DEVMODE *paDeviceModes, DISPLAY_DEVICE *paDisplayDevices,
272 DWORD totalDispNum, UINT Id, DWORD aWidth, DWORD aHeight,
273 DWORD aBitsPerPixel, LONG aPosX, LONG aPosY, BOOL fEnabled, BOOL fExtDispSup)
274{
275 DISPLAY_DEVICE displayDeviceTmp;
276 DISPLAY_DEVICE displayDevice;
277 DEVMODE deviceMode;
278 DWORD dwStatus = DISP_CHANGE_SUCCESSFUL;
279 DWORD iter ;
280
281 PVBOXDISPLAYCONTEXT pCtx = &g_Ctx; /* See todo above. */
282
283 deviceMode = paDeviceModes[Id];
284 displayDevice = paDisplayDevices[Id];
285
286 for (iter = 0; iter < totalDispNum; iter++)
287 {
288 if (iter != 0 && iter != Id && !(paDisplayDevices[iter].StateFlags & DISPLAY_DEVICE_ACTIVE))
289 {
290 LogRel(("Display: Initially disabling monitor with ID=%ld; total monitor count is %ld\n", iter, totalDispNum));
291 DEVMODE deviceModeTmp;
292 ZeroMemory(&deviceModeTmp, sizeof(DEVMODE));
293 deviceModeTmp.dmSize = sizeof(DEVMODE);
294 deviceModeTmp.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION
295 | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS ;
296 displayDeviceTmp = paDisplayDevices[iter];
297 pCtx->pfnChangeDisplaySettingsEx(displayDeviceTmp.DeviceName, &deviceModeTmp, NULL,
298 (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
299 }
300 }
301
302 if (fExtDispSup) /* Extended Display Support possible*/
303 {
304 if (fEnabled)
305 {
306 /* Special case for enabling the secondary monitor. */
307 if(!(displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE))
308 {
309 LogRel(("Display [ID=%ld, name='%s']: Is a secondary monitor and disabled -- enabling it\n", Id, displayDevice.DeviceName));
310 deviceMode.dmPosition.x = paDeviceModes[0].dmPelsWidth;
311 deviceMode.dmPosition.y = 0;
312 deviceMode.dmBitsPerPel = 32;
313
314 uint64_t const uNtVersion = RTSystemGetNtVersion();
315 if (uNtVersion < RTSYSTEM_MAKE_NT_VERSION(6, 0, 0))
316 /* dont any more flags here as, only DM_POISITON is used to enable the secondary display */
317 deviceMode.dmFields = DM_POSITION;
318 else /* for win 7 and above */
319 /* for vista and above DM_BITSPERPEL is necessary */
320 deviceMode.dmFields = DM_BITSPERPEL | DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY | DM_POSITION;
321
322 dwStatus = pCtx->pfnChangeDisplaySettingsEx((LPSTR)displayDevice.DeviceName,&deviceMode, NULL, (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
323 /* A second call to ChangeDisplaySettings updates the monitor.*/
324 pCtx->pfnChangeDisplaySettingsEx(NULL, NULL, NULL,0, NULL);
325 }
326 else /* secondary monitor already enabled. Request to change the resolution or position. */
327 {
328 if (aWidth !=0 && aHeight != 0)
329 {
330 LogRel(("Display [ID=%ld, name='%s']: Changing resolution to %ldx%ld\n", Id, displayDevice.DeviceName, aWidth, aHeight));
331 deviceMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL
332 | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS;
333 deviceMode.dmPelsWidth = aWidth;
334 deviceMode.dmPelsHeight = aHeight;
335 deviceMode.dmBitsPerPel = aBitsPerPixel;
336 }
337 if (aPosX != 0 || aPosY != 0)
338 {
339 LogRel(("Display [ID=%ld, name='%s']: Changing position to %ld,%ld\n", Id, displayDevice.DeviceName, aPosX, aPosY));
340 deviceMode.dmFields |= DM_POSITION;
341 deviceMode.dmPosition.x = aPosX;
342 deviceMode.dmPosition.y = aPosY;
343 }
344 dwStatus = pCtx->pfnChangeDisplaySettingsEx((LPSTR)displayDevice.DeviceName,
345 &deviceMode, NULL, CDS_NORESET|CDS_UPDATEREGISTRY, NULL);
346 /* A second call to ChangeDisplaySettings updates the monitor. */
347 pCtx->pfnChangeDisplaySettingsEx(NULL, NULL, NULL,0, NULL);
348 }
349 }
350 else /* Request is there to disable the monitor with ID = Id*/
351 {
352 LogRel(("Display [ID=%ld, name='%s']: Disalbing\n", Id, displayDevice.DeviceName));
353
354 DEVMODE deviceModeTmp;
355 ZeroMemory(&deviceModeTmp, sizeof(DEVMODE));
356 deviceModeTmp.dmSize = sizeof(DEVMODE);
357 deviceModeTmp.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION
358 | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS ;
359 displayDeviceTmp = paDisplayDevices[Id];
360 dwStatus = pCtx->pfnChangeDisplaySettingsEx(displayDeviceTmp.DeviceName, &deviceModeTmp, NULL,
361 (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
362 pCtx->pfnChangeDisplaySettingsEx(NULL, NULL, NULL,0, NULL);
363 }
364 }
365 return dwStatus;
366}
367
368DWORD VBoxDisplayGetCount(void)
369{
370 DISPLAY_DEVICE DisplayDevice;
371
372 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
373 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
374
375 /* Find out how many display devices the system has */
376 DWORD NumDevices = 0;
377 DWORD i = 0;
378 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
379 {
380 LogFlowFunc(("ResizeDisplayDevice: [%d] %s\n", i, DisplayDevice.DeviceName));
381
382 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
383 {
384 LogFlowFunc(("ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
385 NumDevices++;
386 }
387 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
388 {
389
390 LogFlowFunc(("ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
391 NumDevices++;
392 }
393
394 ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
395 DisplayDevice.cb = sizeof(DisplayDevice);
396 i++;
397 }
398
399 return NumDevices;
400}
401
402DWORD VBoxDisplayGetConfig(const DWORD NumDevices, DWORD *pDevPrimaryNum, DWORD *pNumDevices,
403 DISPLAY_DEVICE *paDisplayDevices, DEVMODE *paDeviceModes)
404{
405 /* Fetch information about current devices and modes. */
406 DWORD DevNum = 0;
407 DWORD DevPrimaryNum = 0;
408
409 DISPLAY_DEVICE DisplayDevice;
410
411 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
412 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
413
414 DWORD i = 0;
415 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
416 {
417 LogFlowFunc(("ResizeDisplayDevice: [%d(%d)] %s\n", i, DevNum, DisplayDevice.DeviceName));
418
419 BOOL bFetchDevice = FALSE;
420
421 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
422 {
423 LogFlowFunc(("ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
424 DevPrimaryNum = DevNum;
425 bFetchDevice = TRUE;
426 }
427 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
428 {
429
430 LogFlowFunc(("ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
431 bFetchDevice = TRUE;
432 }
433
434 if (bFetchDevice)
435 {
436 if (DevNum >= NumDevices)
437 {
438 LogFlowFunc(("ResizeDisplayDevice: %d >= %d\n", NumDevices, DevNum));
439 return ERROR_BUFFER_OVERFLOW;
440 }
441
442 paDisplayDevices[DevNum] = DisplayDevice;
443
444 /* First try to get the video mode stored in registry (ENUM_REGISTRY_SETTINGS).
445 * A secondary display could be not active at the moment and would not have
446 * a current video mode (ENUM_CURRENT_SETTINGS).
447 */
448 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
449 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
450 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
451 ENUM_REGISTRY_SETTINGS, &paDeviceModes[DevNum]))
452 {
453 LogFlowFunc(("ResizeDisplayDevice: EnumDisplaySettings error %d\n", GetLastError ()));
454 }
455
456 if ( paDeviceModes[DevNum].dmPelsWidth == 0
457 || paDeviceModes[DevNum].dmPelsHeight == 0)
458 {
459 /* No ENUM_REGISTRY_SETTINGS yet. Seen on Vista after installation.
460 * Get the current video mode then.
461 */
462 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
463 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
464 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
465 ENUM_CURRENT_SETTINGS, &paDeviceModes[DevNum]))
466 {
467 /* ENUM_CURRENT_SETTINGS returns FALSE when the display is not active:
468 * for example a disabled secondary display.
469 * Do not return here, ignore the error and set the display info to 0x0x0.
470 */
471 LogFlowFunc(("ResizeDisplayDevice: EnumDisplaySettings(ENUM_CURRENT_SETTINGS) error %d\n", GetLastError ()));
472 }
473 }
474
475
476 DevNum++;
477 }
478
479 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
480 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
481 i++;
482 }
483
484 *pNumDevices = DevNum;
485 *pDevPrimaryNum = DevPrimaryNum;
486
487 return NO_ERROR;
488}
489
490static void ResizeDisplayDeviceNT4(DWORD dwNewXRes, DWORD dwNewYRes, DWORD dwNewBpp)
491{
492 DEVMODE devMode;
493
494 RT_ZERO(devMode);
495 devMode.dmSize = sizeof(DEVMODE);
496
497 /* get the current screen setup */
498 if (!EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &devMode))
499 {
500 LogFlowFunc(("error from EnumDisplaySettings: %d\n", GetLastError()));
501 return;
502 }
503
504 LogFlowFunc(("Current mode: %d x %d x %d at %d,%d\n",
505 devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmPosition.x, devMode.dmPosition.y));
506
507 /* Check whether a mode reset or a change is requested. */
508 if (dwNewXRes || dwNewYRes || dwNewBpp)
509 {
510 /* A change is requested.
511 * Set values which are not to be changed to the current values.
512 */
513 if (!dwNewXRes)
514 dwNewXRes = devMode.dmPelsWidth;
515 if (!dwNewYRes)
516 dwNewYRes = devMode.dmPelsHeight;
517 if (!dwNewBpp)
518 dwNewBpp = devMode.dmBitsPerPel;
519 }
520 else
521 {
522 /* All zero values means a forced mode reset. Do nothing. */
523 LogFlowFunc(("Forced mode reset\n"));
524 }
525
526 /* Verify that the mode is indeed changed. */
527 if (devMode.dmPelsWidth == dwNewXRes
528 && devMode.dmPelsHeight == dwNewYRes
529 && devMode.dmBitsPerPel == dwNewBpp)
530 {
531 LogFlowFunc(("already at desired resolution\n"));
532 return;
533 }
534
535 // without this, Windows will not ask the miniport for its
536 // mode table but uses an internal cache instead
537 DEVMODE tempDevMode = { 0 };
538 tempDevMode.dmSize = sizeof(DEVMODE);
539 EnumDisplaySettings(NULL, 0xffffff, &tempDevMode);
540
541 /* adjust the values that are supposed to change */
542 if (dwNewXRes)
543 devMode.dmPelsWidth = dwNewXRes;
544 if (dwNewYRes)
545 devMode.dmPelsHeight = dwNewYRes;
546 if (dwNewBpp)
547 devMode.dmBitsPerPel = dwNewBpp;
548
549 LogFlowFunc(("setting new mode %d x %d, %d BPP\n",
550 devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel));
551
552 /* set the new mode */
553 LONG status = ChangeDisplaySettings(&devMode, CDS_UPDATEREGISTRY);
554 if (status != DISP_CHANGE_SUCCESSFUL)
555 {
556 LogFlowFunc(("error from ChangeDisplaySettings: %d\n", status));
557
558 if (status == DISP_CHANGE_BADMODE)
559 {
560 /* Our driver can not set the requested mode. Stop trying. */
561 return;
562 }
563 }
564}
565
566/* Returns TRUE to try again. */
567/** @todo r=andy Why not using the VMMDevDisplayChangeRequestEx structure for all those parameters here? */
568static BOOL ResizeDisplayDevice(PVBOXDISPLAYCONTEXT pCtx,
569 UINT Id, DWORD Width, DWORD Height, DWORD BitsPerPixel,
570 BOOL fEnabled, LONG dwNewPosX, LONG dwNewPosY, bool fChangeOrigin,
571 BOOL fExtDispSup)
572{
573 BOOL fDispAlreadyEnabled = false; /* check whether the monitor with ID is already enabled. */
574 BOOL fModeReset = ( Width == 0 && Height == 0 && BitsPerPixel == 0
575 && dwNewPosX == 0 && dwNewPosY == 0 && !fChangeOrigin);
576 DWORD dmFields = 0;
577 VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType(pCtx);
578
579 LogFlowFunc(("[%d] %dx%d at %d,%d fChangeOrigin %d fEnabled %d fExtDisSup %d\n",
580 Id, Width, Height, dwNewPosX, dwNewPosY, fChangeOrigin, fEnabled, fExtDispSup));
581
582 if (!pCtx->fAnyX)
583 Width &= 0xFFF8;
584
585 VBoxDispIfCancelPendingResize(&pCtx->pEnv->dispIf);
586
587 DWORD NumDevices = VBoxDisplayGetCount();
588
589 if (NumDevices == 0 || Id >= NumDevices)
590 {
591 LogFlowFunc(("ResizeDisplayDevice: Requested identifier %d is invalid. err %d\n", Id, GetLastError ()));
592 return FALSE;
593 }
594
595 LogFlowFunc(("ResizeDisplayDevice: Found total %d devices. err %d\n", NumDevices, GetLastError ()));
596
597 DISPLAY_DEVICE *paDisplayDevices = (DISPLAY_DEVICE *)alloca(sizeof (DISPLAY_DEVICE) * NumDevices);
598 DEVMODE *paDeviceModes = (DEVMODE *)alloca(sizeof (DEVMODE) * NumDevices);
599 RECTL *paRects = (RECTL *)alloca (sizeof (RECTL) * NumDevices);
600 DWORD DevNum = 0;
601 DWORD DevPrimaryNum = 0;
602 DWORD dwStatus = VBoxDisplayGetConfig(NumDevices, &DevPrimaryNum, &DevNum, paDisplayDevices, paDeviceModes);
603 if (dwStatus != NO_ERROR)
604 {
605 LogFlowFunc(("ResizeDisplayDevice: VBoxGetDisplayConfig failed, %d\n", dwStatus));
606 return dwStatus;
607 }
608
609 if (NumDevices != DevNum)
610 LogFlowFunc(("ResizeDisplayDevice: NumDevices(%d) != DevNum(%d)\n", NumDevices, DevNum));
611
612 DWORD i;
613 for (i = 0; i < DevNum; ++i)
614 {
615 if (fExtDispSup)
616 {
617 LogRel(("Extended Display Support.\n"));
618 LogFlowFunc(("[%d] %dx%dx%d at %d,%d, dmFields 0x%x\n",
619 i,
620 paDeviceModes[i].dmPelsWidth,
621 paDeviceModes[i].dmPelsHeight,
622 paDeviceModes[i].dmBitsPerPel,
623 paDeviceModes[i].dmPosition.x,
624 paDeviceModes[i].dmPosition.y,
625 paDeviceModes[i].dmFields));
626 }
627 else
628 {
629 LogRel(("NO Ext Display Support \n"));
630 }
631
632 paRects[i].left = paDeviceModes[i].dmPosition.x;
633 paRects[i].top = paDeviceModes[i].dmPosition.y;
634 paRects[i].right = paDeviceModes[i].dmPosition.x + paDeviceModes[i].dmPelsWidth;
635 paRects[i].bottom = paDeviceModes[i].dmPosition.y + paDeviceModes[i].dmPelsHeight;
636 }
637
638 /* Keep a record if the display with ID is already active or not. */
639 if (paDisplayDevices[Id].StateFlags & DISPLAY_DEVICE_ACTIVE)
640 {
641 LogRel(("Display with ID=%d already enabled\n", Id));
642 fDispAlreadyEnabled = TRUE;
643 }
644
645 /* Width, height equal to 0 means that this value must be not changed.
646 * Update input parameters if necessary.
647 * Note: BitsPerPixel is taken into account later, when new rectangles
648 * are assigned to displays.
649 */
650 if (Width == 0)
651 Width = paRects[Id].right - paRects[Id].left;
652 else
653 dmFields |= DM_PELSWIDTH;
654
655 if (Height == 0)
656 Height = paRects[Id].bottom - paRects[Id].top;
657 else
658 dmFields |= DM_PELSHEIGHT;
659
660 if (BitsPerPixel == 0)
661 BitsPerPixel = paDeviceModes[Id].dmBitsPerPel;
662 else
663 dmFields |= DM_BITSPERPEL;
664
665 if (!fChangeOrigin)
666 {
667 /* Use existing position. */
668 dwNewPosX = paRects[Id].left;
669 dwNewPosY = paRects[Id].top;
670 LogFlowFunc(("existing dwNewPosX %d, dwNewPosY %d\n", dwNewPosX, dwNewPosY));
671 }
672
673 /* Always update the position.
674 * It is either explicitly requested or must be set to the existing position.
675 */
676 dmFields |= DM_POSITION;
677
678 /* Check whether a mode reset or a change is requested.
679 * Rectangle position is recalculated only if fEnabled is 1.
680 * For non extended supported modes (old Host VMs), fEnabled
681 * is always 1.
682 */
683 /* Handled the case where previouseresolution of secondary monitor
684 * was for eg. 1024*768*32 and monitor was in disabled state.
685 * User gives the command
686 * setvideomode 1024 768 32 1 yes.
687 * Now in this case the resolution request is same as previous one but
688 * monitor is going from disabled to enabled state so the below condition
689 * shour return false
690 * The below condition will only return true , if no mode reset has
691 * been requested AND fEnabled is 1 and fDispAlreadyEnabled is also 1 AND
692 * all rect conditions are true. Thus in this case nothing has to be done.
693 */
694 if ( !fModeReset
695 && (!fEnabled == !fDispAlreadyEnabled)
696 && paRects[Id].left == dwNewPosX
697 && paRects[Id].top == dwNewPosY
698 && paRects[Id].right - paRects[Id].left == (LONG)Width
699 && paRects[Id].bottom - paRects[Id].top == (LONG)Height
700 && paDeviceModes[Id].dmBitsPerPel == BitsPerPixel)
701 {
702 LogRel(("Already at desired resolution. No Change.\n"));
703 return FALSE;
704 }
705
706 hlpResizeRect(paRects, NumDevices, DevPrimaryNum, Id,
707 fEnabled ? Width : 0, fEnabled ? Height : 0, dwNewPosX, dwNewPosY);
708
709 for (i = 0; i < NumDevices; i++)
710 {
711 LogFlowFunc(("ResizeDisplayDevice: [%d]: %d,%d %dx%d\n",
712 i, paRects[i].left, paRects[i].top,
713 paRects[i].right - paRects[i].left,
714 paRects[i].bottom - paRects[i].top));
715 }
716
717 /* Assign the new rectangles to displays. */
718 for (i = 0; i < NumDevices; i++)
719 {
720 paDeviceModes[i].dmPosition.x = paRects[i].left;
721 paDeviceModes[i].dmPosition.y = paRects[i].top;
722 paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
723 paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
724
725 if (i == Id)
726 paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
727
728 if (enmDriverType >= VBOXDISPLAY_DRIVER_TYPE_WDDM)
729 {
730 paDeviceModes[i].dmFields |= dmFields;
731
732 /* On Vista one must specify DM_BITSPERPEL.
733 * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
734 */
735 if (!(paDeviceModes[i].dmFields & DM_BITSPERPEL))
736 {
737 LogFlowFunc(("no DM_BITSPERPEL\n"));
738 paDeviceModes[i].dmFields |= DM_BITSPERPEL;
739 paDeviceModes[i].dmBitsPerPel = 32;
740 }
741 }
742 else
743 {
744 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
745 }
746
747 LogFlowFunc(("ResizeDisplayDevice: Going to resize display %d to %dx%dx%d at %d,%d fields 0x%X\n",
748 i,
749 paDeviceModes[i].dmPelsWidth,
750 paDeviceModes[i].dmPelsHeight,
751 paDeviceModes[i].dmBitsPerPel,
752 paDeviceModes[i].dmPosition.x,
753 paDeviceModes[i].dmPosition.y,
754 paDeviceModes[i].dmFields));
755 }
756
757 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
758 {
759 DWORD err = VBoxDispIfResizeModes(&pCtx->pEnv->dispIf, Id, fEnabled, fExtDispSup, paDisplayDevices, paDeviceModes, DevNum);
760
761 return (err == ERROR_RETRY);
762 }
763
764 /* The XPDM code path goes below.
765 * Re-requesting modes with EnumDisplaySettings forces Windows to again ask the miniport for its mode table.
766 */
767 for (i = 0; i < NumDevices; i++)
768 {
769 DEVMODE tempDevMode;
770 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
771 tempDevMode.dmSize = sizeof(DEVMODE);
772 EnumDisplaySettings((LPSTR)paDisplayDevices[i].DeviceName, 0xffffff, &tempDevMode);
773 LogFlowFunc(("ResizeDisplayDevice: EnumDisplaySettings last error %d\n", GetLastError ()));
774 }
775
776 /* Assign the new rectangles to displays. */
777 for (i = 0; i < NumDevices; i++)
778 {
779 LONG status = pCtx->pfnChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
780 &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
781 LogFlowFunc(("ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", status, GetLastError()));
782 NOREF(status);
783 }
784
785 LogFlowFunc(("Enable And Resize Device. Id = %d, Width=%d Height=%d, dwNewPosX = %d, dwNewPosY = %d fEnabled=%d & fExtDispSupport = %d \n",
786 Id, Width, Height, dwNewPosX, dwNewPosY, fEnabled, fExtDispSup));
787 dwStatus = EnableAndResizeDispDev(paDeviceModes, paDisplayDevices, DevNum, Id, Width, Height, BitsPerPixel,
788 dwNewPosX, dwNewPosY, fEnabled, fExtDispSup);
789 if (dwStatus == DISP_CHANGE_SUCCESSFUL || dwStatus == DISP_CHANGE_BADMODE)
790 {
791 /* Successfully set new video mode or our driver can not set
792 * the requested mode. Stop trying.
793 */
794 return FALSE;
795 }
796 /* Retry the request. */
797 return TRUE;
798}
799
800static void doResize(PVBOXDISPLAYCONTEXT pCtx,
801 uint32_t iDisplay,
802 uint32_t cx,
803 uint32_t cy,
804 uint32_t cBits,
805 bool fEnabled,
806 uint32_t cxOrigin,
807 uint32_t cyOrigin,
808 bool fChangeOrigin)
809{
810 for (;;)
811 {
812 VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType(pCtx);
813 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_UNKNOWN)
814 {
815 LogFlowFunc(("vboxDisplayDriver is not active\n"));
816 break;
817 }
818
819 if (pCtx->pfnChangeDisplaySettingsEx != 0)
820 {
821 LogFlowFunc(("Detected W2K or later\n"));
822 if (!ResizeDisplayDevice(pCtx,
823 iDisplay,
824 cx,
825 cy,
826 cBits,
827 fEnabled,
828 cxOrigin,
829 cyOrigin,
830 fChangeOrigin,
831 true /*fExtDispSup*/ ))
832 {
833 LogFlowFunc(("ResizeDipspalyDevice return 0\n"));
834 break;
835 }
836
837 }
838 else
839 {
840 LogFlowFunc(("Detected NT\n"));
841 ResizeDisplayDeviceNT4(cx, cy, cBits);
842 break;
843 }
844
845 /* Retry the change a bit later. */
846 RTThreadSleep(1000);
847 }
848}
849
850static BOOL DisplayChangeRequestHandler(PVBOXDISPLAYCONTEXT pCtx)
851{
852 VMMDevDisplayDef aDisplays[64];
853 uint32_t cDisplays = RT_ELEMENTS(aDisplays);
854 int rc = VINF_SUCCESS;
855
856 /* Multidisplay resize is still implemented only for Win7 and newer guests. */
857 if (pCtx->pEnv->dispIf.enmMode >= VBOXDISPIF_MODE_WDDM_W7 &&
858 RT_SUCCESS(rc = VbglR3GetDisplayChangeRequestMulti(cDisplays, &cDisplays, &aDisplays[0], true /* fAck */)))
859 {
860 uint32_t i;
861
862 LogRel(("Got multi resize request %d displays\n", cDisplays));
863
864 for (i = 0; i < cDisplays; ++i)
865 {
866 LogRel(("[%d]: %d 0x%02X %d,%d %dx%d %d\n",
867 i, aDisplays[i].idDisplay,
868 aDisplays[i].fDisplayFlags,
869 aDisplays[i].xOrigin,
870 aDisplays[i].yOrigin,
871 aDisplays[i].cx,
872 aDisplays[i].cy,
873 aDisplays[i].cBitsPerPixel));
874 }
875
876 return VBoxDispIfResizeDisplayWin7(&pCtx->pEnv->dispIf, cDisplays, &aDisplays[0]);
877 }
878
879 /* Fall back to the single monitor resize request. */
880
881 /*
882 * We got at least one event. (bird: What does that mean actually? The driver wakes us up immediately upon
883 * receiving the event. Or are we refering to mouse & display? In the latter case it's misleading.)
884 *
885 * Read the requested resolution and try to set it until success.
886 * New events will not be seen but a new resolution will be read in
887 * this poll loop.
888 *
889 * Note! The interface we're using here was added in VBox 4.2.4. As of 2017-08-16, this
890 * version has been unsupported for a long time and we therefore don't bother
891 * implementing fallbacks using VMMDevDisplayChangeRequest2 and VMMDevDisplayChangeRequest.
892 */
893 uint32_t cx = 0;
894 uint32_t cy = 0;
895 uint32_t cBits = 0;
896 uint32_t iDisplay = 0;
897 uint32_t cxOrigin = 0;
898 uint32_t cyOrigin = 0;
899 bool fChangeOrigin = false;
900 bool fEnabled = false;
901 rc = VbglR3GetDisplayChangeRequest(&cx, &cy, &cBits, &iDisplay, &cxOrigin, &cyOrigin, &fEnabled, &fChangeOrigin,
902 true /*fAck*/);
903 if (RT_SUCCESS(rc))
904 {
905 /* Try to set the requested video mode. Repeat until it is successful or is rejected by the driver. */
906 LogFlowFunc(("DisplayChangeReqEx parameters iDisplay=%d x cx=%d x cy=%d x cBits=%d x SecondayMonEnb=%d x NewOriginX=%d x NewOriginY=%d x ChangeOrigin=%d\n",
907 iDisplay, cx, cy, cBits, fEnabled, cxOrigin, cyOrigin, fChangeOrigin));
908
909 doResize(pCtx,
910 iDisplay,
911 cx,
912 cy,
913 cBits,
914 fEnabled,
915 cxOrigin,
916 cyOrigin,
917 fChangeOrigin);
918 }
919
920 return rc;
921}
922
923/**
924 * @interface_method_impl{VBOXSERVICEDESC,pfnWorker}
925 */
926static DECLCALLBACK(int) vbtrDisplayWorker(void *pvInstance, bool volatile *pfShutdown)
927{
928 AssertPtr(pvInstance);
929 PVBOXDISPLAYCONTEXT pCtx = (PVBOXDISPLAYCONTEXT)pvInstance;
930 AssertPtr(pCtx->pEnv);
931 LogFlowFunc(("pvInstance=%p\n", pvInstance));
932
933 /*
934 * Tell the control thread that it can continue
935 * spawning services.
936 */
937 RTThreadUserSignal(RTThreadSelf());
938
939 int rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED, 0 /*fNot*/);
940 if (RT_FAILURE(rc))
941 {
942 LogFlowFunc(("VbglR3CtlFilterMask(mask,0): %Rrc\n", rc));
943 return rc;
944 }
945
946 PostMessage(g_hwndToolWindow, WM_VBOX_GRAPHICS_SUPPORTED, 0, 0);
947
948 VBoxDispIfResizeStarted(&pCtx->pEnv->dispIf);
949
950 for (;;)
951 {
952 /*
953 * Wait for a display change event, checking for shutdown both before and after.
954 */
955 if (*pfShutdown)
956 {
957 rc = VINF_SUCCESS;
958 break;
959 }
960
961 uint32_t fEvents = 0;
962 rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED, 1000 /*ms*/, &fEvents);
963
964 if (*pfShutdown)
965 {
966 rc = VINF_SUCCESS;
967 break;
968 }
969
970 if (RT_SUCCESS(rc))
971 {
972 if (fEvents & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
973 DisplayChangeRequestHandler(pCtx);
974
975 if (fEvents & VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED)
976 hlpReloadCursor();
977 }
978 else
979 {
980 // Checking once a second whether or not WM_DISPLAYCHANGED happened.
981 if (ASMAtomicXchgU32(&g_fGuestDisplaysChanged, 0))
982 {
983 // XPDM driver has VBoxDispDrvNotify to receive such a notifications
984 if (pCtx->pEnv->dispIf.enmMode >= VBOXDISPIF_MODE_WDDM)
985 {
986 VBOXDISPIFESCAPE EscapeHdr = { 0 };
987 EscapeHdr.escapeCode = VBOXESC_GUEST_DISPLAYCHANGED;
988
989 DWORD err = VBoxDispIfEscapeInOut(&pCtx->pEnv->dispIf, &EscapeHdr, 0);
990 LogFlowFunc(("VBoxDispIfEscapeInOut returned %d\n", err)); NOREF(err);
991 }
992 }
993
994 /* sleep a bit to not eat too much CPU in case the above call always fails */
995 if (rc != VERR_TIMEOUT)
996 RTThreadSleep(10);
997 }
998 }
999
1000 /*
1001 * Remove event filter and graphics capability report.
1002 */
1003 int rc2 = VbglR3CtlFilterMask(0 /*fOr*/, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED /*fNot*/);
1004 if (RT_FAILURE(rc2))
1005 LogFlowFunc(("VbglR3CtlFilterMask failed: %Rrc\n", rc2));
1006 PostMessage(g_hwndToolWindow, WM_VBOX_GRAPHICS_UNSUPPORTED, 0, 0);
1007
1008 LogFlowFuncLeaveRC(rc);
1009 return rc;
1010}
1011
1012/**
1013 * The service description.
1014 */
1015VBOXTRAYSVCDESC g_SvcDescDisplay =
1016{
1017 /* pszName. */
1018 "display",
1019 /* pszDescription. */
1020 "Display Notifications",
1021 /* pszUsage. */
1022 NULL,
1023 /* pszOptions. */
1024 NULL,
1025 /* methods */
1026 vbtrDispPreInit,
1027 vbtrDispOption,
1028 vbtrDisplayInit,
1029 vbtrDisplayWorker,
1030 NULL /* pfnStop */,
1031 vbtrDisplayDestroy
1032};
1033
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette