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