VirtualBox

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

Last change on this file since 69180 was 69180, checked in by vboxsync, 7 years ago

Additions/win/vboxtray: Code cleanup in VBoxDisplay service, step 1.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 37.9 KB
Line 
1/* $Id: VBoxDisplay.cpp 69180 2017-10-23 18:08:32Z vboxsync $ */
2/** @file
3 * VBoxSeamless - Display notifications.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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
32#ifdef DEBUG /** @todo r=bird: these are all default values. sigh. */
33# define LOG_ENABLED
34# define LOG_GROUP LOG_GROUP_DEFAULT
35#endif
36#include <VBox/log.h>
37
38#include <VBoxDisplay.h>
39#include <VBoxHook.h>
40
41
42/*********************************************************************************************************************************
43* Structures and Typedefs *
44*********************************************************************************************************************************/
45typedef struct _VBOXDISPLAYCONTEXT
46{
47 const VBOXSERVICEENV *pEnv;
48 BOOL fAnyX;
49 /** ChangeDisplaySettingsEx does not exist in NT. ResizeDisplayDevice uses the function. */
50 LONG (WINAPI * pfnChangeDisplaySettingsEx)(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam);
51 /** EnumDisplayDevices does not exist in NT. isVBoxDisplayDriverActive et al. are using these functions. */
52 BOOL (WINAPI * pfnEnumDisplayDevices)(IN LPCSTR lpDevice, IN DWORD iDevNum, OUT PDISPLAY_DEVICEA lpDisplayDevice, IN DWORD dwFlags);
53 /** Display driver interface, XPDM - WDDM abstraction see VBOXDISPIF** definitions above */
54 VBOXDISPIF dispIf;
55} VBOXDISPLAYCONTEXT, *PVBOXDISPLAYCONTEXT;
56
57typedef enum
58{
59 VBOXDISPLAY_DRIVER_TYPE_UNKNOWN = 0,
60 VBOXDISPLAY_DRIVER_TYPE_XPDM = 1,
61 VBOXDISPLAY_DRIVER_TYPE_WDDM = 2
62} VBOXDISPLAY_DRIVER_TYPE;
63
64
65/*********************************************************************************************************************************
66* Global Variables *
67*********************************************************************************************************************************/
68static VBOXDISPLAYCONTEXT g_Ctx = { 0 };
69
70
71/*********************************************************************************************************************************
72* Internal Functions *
73*********************************************************************************************************************************/
74static VBOXDISPLAY_DRIVER_TYPE getVBoxDisplayDriverType(VBOXDISPLAYCONTEXT *pCtx);
75
76
77static DECLCALLBACK(int) VBoxDisplayInit(const PVBOXSERVICEENV pEnv, void **ppInstance)
78{
79 LogFlowFuncEnter();
80
81 PVBOXDISPLAYCONTEXT pCtx = &g_Ctx; /** @todo r=andy Use instance data via service lookup (add void *pInstance). */
82 AssertPtr(pCtx);
83
84 OSVERSIONINFO OSinfo; /** @todo r=andy Use VBoxTray's g_dwMajorVersion? */
85 OSinfo.dwOSVersionInfoSize = sizeof(OSinfo);
86 GetVersionEx (&OSinfo);
87
88 int rc;
89 HMODULE hUser = GetModuleHandle("user32.dll"); /** @todo r=andy Use RTLdrXXX and friends. */
90
91 pCtx->pEnv = pEnv;
92
93 if (NULL == hUser)
94 {
95 LogFlowFunc(("Could not get module handle of USER32.DLL!\n"));
96 rc = VERR_NOT_IMPLEMENTED;
97 }
98 else if (OSinfo.dwMajorVersion >= 5) /* APIs available only on W2K and up. */
99 {
100 /** @todo r=andy Use RTLdrXXX and friends. */
101 /** @todo r=andy No unicode version available? */
102 *(uintptr_t *)&pCtx->pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
103 LogFlowFunc(("pfnChangeDisplaySettingsEx = %p\n", pCtx->pfnChangeDisplaySettingsEx));
104
105 *(uintptr_t *)&pCtx->pfnEnumDisplayDevices = (uintptr_t)GetProcAddress(hUser, "EnumDisplayDevicesA");
106 LogFlowFunc(("pfnEnumDisplayDevices = %p\n", pCtx->pfnEnumDisplayDevices));
107
108#ifdef VBOX_WITH_WDDM
109 if (OSinfo.dwMajorVersion >= 6)
110 {
111 /* This is Vista and up, check if we need to switch the display driver if to WDDM mode. */
112 LogFlowFunc(("this is Windows Vista and up\n"));
113 VBOXDISPLAY_DRIVER_TYPE enmType = getVBoxDisplayDriverType(pCtx);
114 if (enmType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
115 {
116 LogFlowFunc(("WDDM driver is installed, switching display driver if to WDDM mode\n"));
117 /* This is hacky, but the most easiest way. */
118 VBOXDISPIF_MODE enmMode = (OSinfo.dwMajorVersion == 6 && OSinfo.dwMinorVersion == 0) ? VBOXDISPIF_MODE_WDDM : VBOXDISPIF_MODE_WDDM_W7;
119 DWORD dwErr = VBoxDispIfSwitchMode(const_cast<PVBOXDISPIF>(&pEnv->dispIf), enmMode, NULL /* old mode, we don't care about it */);
120 if (dwErr == NO_ERROR)
121 {
122 LogFlowFunc(("DispIf successfully switched to WDDM mode\n"));
123 rc = VINF_SUCCESS;
124 }
125 else
126 {
127 LogFlowFunc(("Failed to switch DispIf to WDDM mode, error (%d)\n", dwErr));
128 rc = RTErrConvertFromWin32(dwErr);
129 }
130 }
131 else
132 rc = VINF_SUCCESS;
133 }
134 else
135 rc = VINF_SUCCESS;
136#endif
137 }
138 else if (OSinfo.dwMajorVersion <= 4) /* Windows NT 4.0. */
139 {
140 /* Nothing to do here yet. */
141 /** @todo r=andy Has this been tested? */
142 rc = VINF_SUCCESS;
143 }
144 else /* Unsupported platform. */
145 {
146 LogFlowFunc(("Warning: Display for platform not handled yet!\n"));
147 rc = VERR_NOT_IMPLEMENTED;
148 }
149
150 if (RT_SUCCESS(rc))
151 {
152 VBOXDISPIFESCAPE_ISANYX IsAnyX = { 0 };
153 IsAnyX.EscapeHdr.escapeCode = VBOXESC_ISANYX;
154 DWORD err = VBoxDispIfEscapeInOut(&pEnv->dispIf, &IsAnyX.EscapeHdr, sizeof(uint32_t));
155 if (err == NO_ERROR)
156 pCtx->fAnyX = !!IsAnyX.u32IsAnyX;
157 else
158 pCtx->fAnyX = TRUE;
159
160 *ppInstance = pCtx;
161 }
162
163 LogFlowFuncLeaveRC(rc);
164 return rc;
165}
166
167static DECLCALLBACK(void) VBoxDisplayDestroy(void *pInstance)
168{
169 RT_NOREF(pInstance);
170 return;
171}
172
173static VBOXDISPLAY_DRIVER_TYPE getVBoxDisplayDriverType(PVBOXDISPLAYCONTEXT pCtx)
174{
175 VBOXDISPLAY_DRIVER_TYPE enmType = VBOXDISPLAY_DRIVER_TYPE_UNKNOWN;
176
177 if (pCtx->pfnEnumDisplayDevices)
178 {
179 INT devNum = 0;
180 DISPLAY_DEVICE dispDevice;
181 FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
182 dispDevice.cb = sizeof(DISPLAY_DEVICE);
183
184 LogFlowFunc(("getVBoxDisplayDriverType: Checking for active VBox display driver (W2K+) ...\n"));
185
186 while (EnumDisplayDevices(NULL,
187 devNum,
188 &dispDevice,
189 0))
190 {
191 LogFlowFunc(("getVBoxDisplayDriverType: DevNum:%d\nName:%s\nString:%s\nID:%s\nKey:%s\nFlags=%08X\n\n",
192 devNum,
193 &dispDevice.DeviceName[0],
194 &dispDevice.DeviceString[0],
195 &dispDevice.DeviceID[0],
196 &dispDevice.DeviceKey[0],
197 dispDevice.StateFlags));
198
199 if (dispDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
200 {
201 LogFlowFunc(("getVBoxDisplayDriverType: Primary device\n"));
202
203 /* WDDM driver can now have multiple incarnations,
204 * if the driver name contains VirtualBox, and does NOT match the XPDM name,
205 * assume it to be WDDM */
206 if (strcmp(&dispDevice.DeviceString[0], "VirtualBox Graphics Adapter") == 0)
207 enmType = VBOXDISPLAY_DRIVER_TYPE_XPDM;
208 else if (strstr(&dispDevice.DeviceString[0], "VirtualBox"))
209 enmType = VBOXDISPLAY_DRIVER_TYPE_WDDM;
210
211 break;
212 }
213
214 FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
215
216 dispDevice.cb = sizeof(DISPLAY_DEVICE);
217
218 devNum++;
219 }
220 }
221 else /* This must be NT 4 or something really old, so don't use EnumDisplayDevices() here ... */
222 {
223 LogFlowFunc(("getVBoxDisplayDriverType: Checking for active VBox display driver (NT or older) ...\n"));
224
225 DEVMODE tempDevMode;
226 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
227 tempDevMode.dmSize = sizeof(DEVMODE);
228 EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &tempDevMode); /* Get current display device settings */
229
230 /* Check for the short name, because all long stuff would be truncated */
231 if (strcmp((char*)&tempDevMode.dmDeviceName[0], "VBoxDisp") == 0)
232 enmType = VBOXDISPLAY_DRIVER_TYPE_XPDM;
233 }
234
235 return enmType;
236}
237
238/** @todo r=andy The "display", "seamless" (and VBoxCaps facility in VBoxTray.cpp indirectly) is using this.
239 * Add a PVBOXDISPLAYCONTEXT here for properly getting the display (XPDM/WDDM) abstraction interfaces. */
240DWORD EnableAndResizeDispDev(DEVMODE *paDeviceModes, DISPLAY_DEVICE *paDisplayDevices,
241 DWORD totalDispNum, UINT Id, DWORD aWidth, DWORD aHeight,
242 DWORD aBitsPerPixel, LONG aPosX, LONG aPosY, BOOL fEnabled, BOOL fExtDispSup)
243{
244 DISPLAY_DEVICE displayDeviceTmp;
245 DISPLAY_DEVICE displayDevice;
246 DEVMODE deviceMode;
247 DWORD dwStatus = DISP_CHANGE_SUCCESSFUL;
248 DWORD iter ;
249
250 PVBOXDISPLAYCONTEXT pCtx = &g_Ctx; /* See todo above. */
251
252 deviceMode = paDeviceModes[Id];
253 displayDevice = paDisplayDevices[Id];
254
255 for (iter = 0; iter < totalDispNum; iter++)
256 {
257 if (iter != 0 && iter != Id && !(paDisplayDevices[iter].StateFlags & DISPLAY_DEVICE_ACTIVE))
258 {
259 LogRel(("Display: Initially disabling monitor with ID=%ld; total monitor count is %ld\n", iter, totalDispNum));
260 DEVMODE deviceModeTmp;
261 ZeroMemory(&deviceModeTmp, sizeof(DEVMODE));
262 deviceModeTmp.dmSize = sizeof(DEVMODE);
263 deviceModeTmp.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION
264 | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS ;
265 displayDeviceTmp = paDisplayDevices[iter];
266 pCtx->pfnChangeDisplaySettingsEx(displayDeviceTmp.DeviceName, &deviceModeTmp, NULL,
267 (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
268 }
269 }
270
271 if (fExtDispSup) /* Extended Display Support possible*/
272 {
273 if (fEnabled)
274 {
275 /* Special case for enabling the secondary monitor. */
276 if(!(displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE))
277 {
278 LogRel(("Display [ID=%ld, name='%s']: Is a secondary monitor and disabled -- enabling it\n", Id, displayDevice.DeviceName));
279 deviceMode.dmPosition.x = paDeviceModes[0].dmPelsWidth;
280 deviceMode.dmPosition.y = 0;
281 deviceMode.dmBitsPerPel = 32;
282 OSVERSIONINFO OSinfo;
283 OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
284 GetVersionEx (&OSinfo);
285
286 if (OSinfo.dwMajorVersion < 6)
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#ifdef VBOX_WITH_WDDM
729 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
730 {
731 LogFlowFunc(("Request to resize the displa\n"));
732 DWORD err = VBoxDispIfResizeModes(&pCtx->pEnv->dispIf, Id, fEnabled, fExtDispSup, paDisplayDevices, paDeviceModes, DevNum);
733 if (err != ERROR_RETRY)
734 {
735 if (err == NO_ERROR)
736 LogFlowFunc(("VBoxDispIfResizeModes succeeded\n"));
737 else
738 LogFlowFunc(("Failure VBoxDispIfResizeModes (%d)\n", err));
739 return FALSE;
740 }
741
742 LogFlowFunc(("ResizeDisplayDevice: RETRY requested\n"));
743 return TRUE;
744 }
745#endif
746 /* Without this, Windows will not ask the miniport for its
747 * mode table but uses an internal cache instead.
748 */
749 for (i = 0; i < NumDevices; i++)
750 {
751 DEVMODE tempDevMode;
752 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
753 tempDevMode.dmSize = sizeof(DEVMODE);
754 EnumDisplaySettings((LPSTR)paDisplayDevices[i].DeviceName, 0xffffff, &tempDevMode);
755 LogFlowFunc(("ResizeDisplayDevice: EnumDisplaySettings last error %d\n", GetLastError ()));
756 }
757
758 /* Assign the new rectangles to displays. */
759 for (i = 0; i < NumDevices; i++)
760 {
761 LONG status = pCtx->pfnChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
762 &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
763 LogFlowFunc(("ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", status, GetLastError()));
764 NOREF(status);
765 }
766
767 LogFlowFunc(("Enable And Resize Device. Id = %d, Width=%d Height=%d, dwNewPosX = %d, dwNewPosY = %d fEnabled=%d & fExtDispSupport = %d \n",
768 Id, Width, Height, dwNewPosX, dwNewPosY, fEnabled, fExtDispSup));
769 dwStatus = EnableAndResizeDispDev(paDeviceModes, paDisplayDevices, DevNum, Id, Width, Height, BitsPerPixel,
770 dwNewPosX, dwNewPosY, fEnabled, fExtDispSup);
771 if (dwStatus == DISP_CHANGE_SUCCESSFUL || dwStatus == DISP_CHANGE_BADMODE)
772 {
773 /* Successfully set new video mode or our driver can not set
774 * the requested mode. Stop trying.
775 */
776 return FALSE;
777 }
778 /* Retry the request. */
779 return TRUE;
780}
781
782/**
783 * Thread function to wait for and process display change requests.
784 */
785static DECLCALLBACK(int) VBoxDisplayWorker(void *pvInstance, bool volatile *pfShutdown)
786{
787 AssertPtr(pvInstance);
788 PVBOXDISPLAYCONTEXT pCtx = (PVBOXDISPLAYCONTEXT)pvInstance;
789 AssertPtr(pCtx->pEnv);
790 LogFlowFunc(("pvInstance=%p\n", pvInstance));
791
792 /*
793 * Tell the control thread that it can continue
794 * spawning services.
795 */
796 RTThreadUserSignal(RTThreadSelf());
797
798 int rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED, 0 /*fNot*/);
799 if (RT_FAILURE(rc))
800 {
801 LogFlowFunc(("VbglR3CtlFilterMask(mask,0): %Rrc\n", rc));
802 return rc;
803 }
804
805 PostMessage(g_hwndToolWindow, WM_VBOX_GRAPHICS_SUPPORTED, 0, 0);
806
807 VBoxDispIfResizeStarted(&pCtx->pEnv->dispIf);
808
809 for (;;)
810 {
811 /*
812 * Wait for a display change event, checking for shutdown both before and after.
813 */
814 if (*pfShutdown)
815 {
816 rc = VINF_SUCCESS;
817 break;
818 }
819
820 uint32_t fEvents = 0;
821 rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED, 1000 /*ms*/, &fEvents);
822
823 if (*pfShutdown)
824 {
825 rc = VINF_SUCCESS;
826 break;
827 }
828
829 if (RT_SUCCESS(rc))
830 {
831 if (fEvents & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
832 {
833 LogFlowFunc(("going to get display change information\n"));
834
835 /*
836 * We got at least one event. (bird: What does that mean actually? The driver wakes us up immediately upon
837 * receiving the event. Or are we refering to mouse & display? In the latter case it's misleading.)
838 *
839 * Read the requested resolution and try to set it until success.
840 * New events will not be seen but a new resolution will be read in
841 * this poll loop.
842 *
843 * Note! The interface we're using here was added in VBox 4.2.4. As of 2017-08-16, this
844 * version has been unsupported for a long time and we therefore don't bother
845 * implementing fallbacks using VMMDevDisplayChangeRequest2 and VMMDevDisplayChangeRequest.
846 */
847 uint32_t cx = 0;
848 uint32_t cy = 0;
849 uint32_t cBits = 0;
850 uint32_t iDisplay = 0;
851 uint32_t cxOrigin = 0;
852 uint32_t cyOrigin = 0;
853 bool fChangeOrigin = false;
854 bool fEnabled = false;
855 rc = VbglR3GetDisplayChangeRequest(&cx, &cy, &cBits, &iDisplay, &cxOrigin, &cyOrigin, &fEnabled, &fChangeOrigin,
856 true /*fAck*/);
857 if (RT_SUCCESS(rc))
858 {
859 /* Try to set the requested video mode. Repeat until it is successful or is rejected by the driver. */
860 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",
861 iDisplay, cx, cy, cBits, fEnabled, cxOrigin, cyOrigin, fChangeOrigin));
862
863 for (;;)
864 {
865 VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType(pCtx);
866 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_UNKNOWN)
867 {
868 LogFlowFunc(("vboxDisplayDriver is not active\n"));
869 break;
870 }
871
872 if (pCtx->pfnChangeDisplaySettingsEx != 0)
873 {
874 LogFlowFunc(("Detected W2K or later\n"));
875 if (!ResizeDisplayDevice(pCtx,
876 iDisplay,
877 cx,
878 cy,
879 cBits,
880 fEnabled,
881 cxOrigin,
882 cyOrigin,
883 fChangeOrigin,
884 true /*fExtDispSup*/ ))
885 {
886 LogFlowFunc(("ResizeDipspalyDevice return 0\n"));
887 break;
888 }
889
890 }
891 else
892 {
893 LogFlowFunc(("Detected NT\n"));
894 ResizeDisplayDeviceNT4(cx, cy, cBits);
895 break;
896 }
897
898 /* Retry the change a bit later. */
899 RTThreadSleep(1000);
900 }
901 }
902 } // if (fDisplayChangeQueried)
903
904 if (fEvents & VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED)
905 hlpReloadCursor();
906 }
907 else
908 {
909 // Checking once a second whether or not WM_DISPLAYCHANGED happened.
910 if (ASMAtomicXchgU32(&g_fGuestDisplaysChanged, 0))
911 {
912 // XPDM driver has VBoxDispDrvNotify to receive such a notifications
913 if (pCtx->pEnv->dispIf.enmMode >= VBOXDISPIF_MODE_WDDM)
914 {
915 VBOXDISPIFESCAPE EscapeHdr = { 0 };
916 EscapeHdr.escapeCode = VBOXESC_GUEST_DISPLAYCHANGED;
917
918 DWORD err = VBoxDispIfEscapeInOut(&pCtx->pEnv->dispIf, &EscapeHdr, 0);
919 LogFlowFunc(("VBoxDispIfEscapeInOut returned %d\n", err)); NOREF(err);
920 }
921 }
922
923 /* sleep a bit to not eat too much CPU in case the above call always fails */
924 if (rc != VERR_TIMEOUT)
925 RTThreadSleep(10);
926 }
927 }
928
929 /*
930 * Remove event filter and graphics capability report.
931 */
932 int rc2 = VbglR3CtlFilterMask(0 /*fOr*/, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED /*fNot*/);
933 if (RT_FAILURE(rc2))
934 LogFlowFunc(("VbglR3CtlFilterMask failed: %Rrc\n", rc2));
935 PostMessage(g_hwndToolWindow, WM_VBOX_GRAPHICS_UNSUPPORTED, 0, 0);
936
937 LogFlowFuncLeaveRC(rc);
938 return rc;
939}
940
941/**
942 * The service description.
943 */
944VBOXSERVICEDESC g_SvcDescDisplay =
945{
946 /* pszName. */
947 "display",
948 /* pszDescription. */
949 "Display Notifications",
950 /* methods */
951 VBoxDisplayInit,
952 VBoxDisplayWorker,
953 NULL /* pfnStop */,
954 VBoxDisplayDestroy
955};
956
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