VirtualBox

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

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

VBoxTray: Use VbglR3 APIs for everything. Untested.

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