VirtualBox

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

© 2023 Oracle
ContactPrivacy policyTerms of Use