VirtualBox

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

Last change on this file since 86996 was 85121, checked in by vboxsync, 4 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

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