1 | /* $Id: VBoxDisplay.cpp 44735 2013-02-18 14:01:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxSeamless - Display notifications.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2013 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(DEVMODE *paDeviceModes, DISPLAY_DEVICE *paDisplayDevices, DWORD totalDispNum, UINT Id, DWORD aWidth, DWORD aHeight,
|
---|
216 | DWORD aBitsPerPixel, DWORD aPosX, DWORD aPosY, BOOL fEnabled, BOOL fExtDispSup, VBOXDISPLAYCONTEXT *pCtx)
|
---|
217 | {
|
---|
218 | DISPLAY_DEVICE displayDeviceTmp;
|
---|
219 | DISPLAY_DEVICE displayDevice;
|
---|
220 | DEVMODE deviceMode;
|
---|
221 | DWORD dwStatus = DISP_CHANGE_SUCCESSFUL;
|
---|
222 | DWORD iter ;
|
---|
223 |
|
---|
224 | deviceMode = paDeviceModes[Id];
|
---|
225 | displayDevice = paDisplayDevices[Id];
|
---|
226 |
|
---|
227 | for (iter = 0; iter < totalDispNum; iter++)
|
---|
228 | {
|
---|
229 | if (iter != 0 && iter != Id && !(paDisplayDevices[iter].StateFlags & DISPLAY_DEVICE_ACTIVE))
|
---|
230 | {
|
---|
231 | LogRel(("VBoxTray:Initially disabling the monitor with id = %d . Total Monitor=%d\n", iter, totalDispNum));
|
---|
232 | DEVMODE deviceModeTmp;
|
---|
233 | ZeroMemory(&deviceModeTmp, sizeof(DEVMODE));
|
---|
234 | deviceModeTmp.dmSize = sizeof(DEVMODE);
|
---|
235 | deviceModeTmp.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION
|
---|
236 | | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS ;
|
---|
237 | displayDeviceTmp = paDisplayDevices[iter];
|
---|
238 | gCtx.pfnChangeDisplaySettingsEx(displayDeviceTmp.DeviceName, &deviceModeTmp, NULL,
|
---|
239 | (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (fExtDispSup) /* Extended Display Support possible*/
|
---|
244 | {
|
---|
245 | if (fEnabled)
|
---|
246 | {
|
---|
247 | /* Special case for enabling the secondary monitor. */
|
---|
248 | if(!(displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE))
|
---|
249 | {
|
---|
250 | LogRel(("VBoxTray: Secondary Monitor with ID=%d and name=%s Not Enabled. Enabling it.\n", Id, displayDevice.DeviceName));
|
---|
251 | deviceMode.dmPosition.x = paDeviceModes[0].dmPelsWidth;
|
---|
252 | deviceMode.dmPosition.y = 0;
|
---|
253 | deviceMode.dmBitsPerPel = 32;
|
---|
254 | OSVERSIONINFO OSinfo;
|
---|
255 | OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
|
---|
256 | GetVersionEx (&OSinfo);
|
---|
257 |
|
---|
258 | if (OSinfo.dwMajorVersion < 6)
|
---|
259 | /* dont any more flags here as, only DM_POISITON is used to enable the secondary display */
|
---|
260 | deviceMode.dmFields = DM_POSITION;
|
---|
261 | else /* for win 7 and above */
|
---|
262 | /* for vista and aboce DM_BITSPERPELis necessary */
|
---|
263 | deviceMode.dmFields = DM_BITSPERPEL | DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY | DM_POSITION;
|
---|
264 |
|
---|
265 | dwStatus = gCtx.pfnChangeDisplaySettingsEx((LPSTR)displayDevice.DeviceName,&deviceMode, NULL, (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
|
---|
266 | /* A second call to ChangeDisplaySettings updates the monitor.*/
|
---|
267 | gCtx.pfnChangeDisplaySettingsEx(NULL, NULL, NULL,0, NULL);
|
---|
268 | }
|
---|
269 | else /* secondary monitor already enabled. Request to change the resolution or position. */
|
---|
270 | {
|
---|
271 | if (aWidth !=0 && aHeight != 0)
|
---|
272 | {
|
---|
273 | LogRel(("VBoxTray: Display : %s , Change Height: %d & Width: %d\n", displayDevice.DeviceName, aWidth, aHeight));
|
---|
274 | deviceMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL
|
---|
275 | | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS;
|
---|
276 | deviceMode.dmPelsWidth = aWidth;
|
---|
277 | deviceMode.dmPelsHeight = aHeight;
|
---|
278 | deviceMode.dmBitsPerPel = aBitsPerPixel;
|
---|
279 | }
|
---|
280 | if (aPosX != 0 || aPosY != 0)
|
---|
281 | {
|
---|
282 | LogRel(("VBoxTray: Display: %s PosX: %d, PosY: %d\n", displayDevice.DeviceName, aPosX, aPosY));
|
---|
283 | deviceMode.dmFields |= DM_POSITION;
|
---|
284 | deviceMode.dmPosition.x = aPosX;
|
---|
285 | deviceMode.dmPosition.y = aPosY;
|
---|
286 | }
|
---|
287 | dwStatus = gCtx.pfnChangeDisplaySettingsEx((LPSTR)displayDevice.DeviceName,
|
---|
288 | &deviceMode, NULL, CDS_NORESET|CDS_UPDATEREGISTRY, NULL);
|
---|
289 | /* A second call to ChangeDisplaySettings updates the monitor. */
|
---|
290 | gCtx.pfnChangeDisplaySettingsEx(NULL, NULL, NULL,0, NULL);
|
---|
291 | }
|
---|
292 | }
|
---|
293 | else /* Request is there to disable the monitor with ID = Id*/
|
---|
294 | {
|
---|
295 | LogRel(("VBoxTray: Disable the Display: %d\n", displayDevice.DeviceName));
|
---|
296 |
|
---|
297 | DEVMODE deviceModeTmp;
|
---|
298 | ZeroMemory(&deviceModeTmp, sizeof(DEVMODE));
|
---|
299 | deviceModeTmp.dmSize = sizeof(DEVMODE);
|
---|
300 | deviceModeTmp.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION
|
---|
301 | | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS ;
|
---|
302 | displayDeviceTmp = paDisplayDevices[Id];
|
---|
303 | dwStatus = gCtx.pfnChangeDisplaySettingsEx(displayDeviceTmp.DeviceName, &deviceModeTmp, NULL,
|
---|
304 | (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
|
---|
305 | gCtx.pfnChangeDisplaySettingsEx(NULL, NULL, NULL,0, NULL);
|
---|
306 | }
|
---|
307 | }
|
---|
308 | return dwStatus;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /* Returns TRUE to try again. */
|
---|
312 | static BOOL ResizeDisplayDevice(UINT Id, DWORD Width, DWORD Height, DWORD BitsPerPixel,
|
---|
313 | BOOL fEnabled, DWORD dwNewPosX, DWORD dwNewPosY,
|
---|
314 | VBOXDISPLAYCONTEXT *pCtx, BOOL fExtDispSup)
|
---|
315 | {
|
---|
316 | BOOL fDispAlreadyEnabled = false; /* check whether the monitor with ID is already enabled. */
|
---|
317 | BOOL fModeReset = (Width == 0 && Height == 0 && BitsPerPixel == 0 &&
|
---|
318 | dwNewPosX == 0 && dwNewPosY == 0);
|
---|
319 | BOOL fChangePosRequest = false; /* change in position requested */
|
---|
320 |
|
---|
321 | Log(("VBoxTray: ResizeDisplayDevice Width= %d, Height=%d , PosX=%d and PosY=%d \
|
---|
322 | fEnabled = %d, fExtDisSup = %d\n",
|
---|
323 | Width, Height, dwNewPosX, dwNewPosY, fEnabled, fExtDispSup));
|
---|
324 |
|
---|
325 | if (!gCtx.fAnyX)
|
---|
326 | Width &= 0xFFF8;
|
---|
327 |
|
---|
328 | DISPLAY_DEVICE DisplayDevice;
|
---|
329 | DWORD dwStatus;
|
---|
330 |
|
---|
331 | ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
|
---|
332 | DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
|
---|
333 |
|
---|
334 | VBoxDispIfCancelPendingResize(&pCtx->pEnv->dispIf);
|
---|
335 |
|
---|
336 | /* Find out how many display devices the system has */
|
---|
337 | DWORD NumDevices = 0;
|
---|
338 | DWORD i = 0;
|
---|
339 | while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
|
---|
340 | {
|
---|
341 | Log(("VBoxTray: ResizeDisplayDevice: [%d] %s\n", i, DisplayDevice.DeviceName));
|
---|
342 |
|
---|
343 | if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
|
---|
344 | {
|
---|
345 | Log(("VBoxTray: ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
|
---|
346 | NumDevices++;
|
---|
347 | }
|
---|
348 | else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
|
---|
349 | {
|
---|
350 |
|
---|
351 | Log(("VBoxTray: ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
|
---|
352 | NumDevices++;
|
---|
353 | }
|
---|
354 |
|
---|
355 | ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
|
---|
356 | DisplayDevice.cb = sizeof(DisplayDevice);
|
---|
357 | i++;
|
---|
358 | }
|
---|
359 |
|
---|
360 | Log(("VBoxTray: ResizeDisplayDevice: Found total %d devices. err %d\n", NumDevices, GetLastError ()));
|
---|
361 |
|
---|
362 | if (NumDevices == 0 || Id >= NumDevices)
|
---|
363 | {
|
---|
364 | Log(("VBoxTray: ResizeDisplayDevice: Requested identifier %d is invalid. err %d\n", Id, GetLastError ()));
|
---|
365 | return FALSE;
|
---|
366 | }
|
---|
367 |
|
---|
368 | DISPLAY_DEVICE *paDisplayDevices = (DISPLAY_DEVICE *)alloca (sizeof (DISPLAY_DEVICE) * NumDevices);
|
---|
369 | DEVMODE *paDeviceModes = (DEVMODE *)alloca (sizeof (DEVMODE) * NumDevices);
|
---|
370 | RECTL *paRects = (RECTL *)alloca (sizeof (RECTL) * NumDevices);
|
---|
371 |
|
---|
372 | /* Fetch information about current devices and modes. */
|
---|
373 | DWORD DevNum = 0;
|
---|
374 | DWORD DevPrimaryNum = 0;
|
---|
375 |
|
---|
376 | ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
|
---|
377 | DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
|
---|
378 |
|
---|
379 | i = 0;
|
---|
380 | while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
|
---|
381 | {
|
---|
382 | Log(("VBoxTray: ResizeDisplayDevice: [%d(%d)] %s\n", i, DevNum, DisplayDevice.DeviceName));
|
---|
383 |
|
---|
384 | BOOL bFetchDevice = FALSE;
|
---|
385 |
|
---|
386 | if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
|
---|
387 | {
|
---|
388 | Log(("VBoxTray: ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
|
---|
389 | DevPrimaryNum = DevNum;
|
---|
390 | bFetchDevice = TRUE;
|
---|
391 | }
|
---|
392 | else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
|
---|
393 | {
|
---|
394 |
|
---|
395 | Log(("VBoxTray: ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
|
---|
396 | bFetchDevice = TRUE;
|
---|
397 | }
|
---|
398 |
|
---|
399 | if (bFetchDevice)
|
---|
400 | {
|
---|
401 | if (DevNum >= NumDevices)
|
---|
402 | {
|
---|
403 | Log(("VBoxTray: ResizeDisplayDevice: %d >= %d\n", NumDevices, DevNum));
|
---|
404 | return FALSE;
|
---|
405 | }
|
---|
406 |
|
---|
407 | paDisplayDevices[DevNum] = DisplayDevice;
|
---|
408 |
|
---|
409 | /* First try to get the video mode stored in registry (ENUM_REGISTRY_SETTINGS).
|
---|
410 | * A secondary display could be not active at the moment and would not have
|
---|
411 | * a current video mode (ENUM_CURRENT_SETTINGS).
|
---|
412 | */
|
---|
413 | ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
|
---|
414 | paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
|
---|
415 | if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
|
---|
416 | ENUM_REGISTRY_SETTINGS, &paDeviceModes[DevNum]))
|
---|
417 | {
|
---|
418 | Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings error %d\n", GetLastError ()));
|
---|
419 | }
|
---|
420 |
|
---|
421 | if ( paDeviceModes[DevNum].dmPelsWidth == 0
|
---|
422 | || paDeviceModes[DevNum].dmPelsHeight == 0)
|
---|
423 | {
|
---|
424 | /* No ENUM_REGISTRY_SETTINGS yet. Seen on Vista after installation.
|
---|
425 | * Get the current video mode then.
|
---|
426 | */
|
---|
427 | ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
|
---|
428 | paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
|
---|
429 | if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
|
---|
430 | ENUM_CURRENT_SETTINGS, &paDeviceModes[DevNum]))
|
---|
431 | {
|
---|
432 | /* ENUM_CURRENT_SETTINGS returns FALSE when the display is not active:
|
---|
433 | * for example a disabled secondary display.
|
---|
434 | * Do not return here, ignore the error and set the display info to 0x0x0.
|
---|
435 | */
|
---|
436 | Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings(ENUM_CURRENT_SETTINGS) error %d\n", GetLastError ()));
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | if (fExtDispSup)
|
---|
441 | {
|
---|
442 | LogRel(("VBoxTray: Extended Display Support.\n"));
|
---|
443 | Log(("VBoxTray: ResizeDisplayDevice1: %dx%dx%d at %d,%d . Id = %d and DevNum=%d, fEnabled=%d\n",
|
---|
444 | paDeviceModes[Id].dmPelsWidth,
|
---|
445 | paDeviceModes[Id].dmPelsHeight,
|
---|
446 | paDeviceModes[Id].dmBitsPerPel,
|
---|
447 | paDeviceModes[Id].dmPosition.x,
|
---|
448 | paDeviceModes[Id].dmPosition.y,
|
---|
449 | Id, DevNum, fEnabled));
|
---|
450 | if ((DevNum == Id && fEnabled == 1))
|
---|
451 | {
|
---|
452 | /* Calculation of new position for enabled
|
---|
453 | * secondary monitor .
|
---|
454 | */
|
---|
455 | /* Used when a secondary monitor just needs to be enabled, without any
|
---|
456 | * change in its position
|
---|
457 | */
|
---|
458 | if (dwNewPosX != 0)
|
---|
459 | {
|
---|
460 | LogRel(("VBoxTray: Setting Rectangle position x=%d*y=%d\n", dwNewPosX, dwNewPosY));
|
---|
461 | paDeviceModes[DevNum].dmPosition.x = dwNewPosX;
|
---|
462 | paDeviceModes[DevNum].dmPosition.y = dwNewPosY;
|
---|
463 | fChangePosRequest = true;
|
---|
464 | }
|
---|
465 | paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
|
---|
466 | paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
|
---|
467 | paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
|
---|
468 | paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
|
---|
469 | }
|
---|
470 | else
|
---|
471 | {
|
---|
472 | paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
|
---|
473 | paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
|
---|
474 | paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
|
---|
475 | paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
|
---|
476 | }
|
---|
477 | }
|
---|
478 | else
|
---|
479 | {
|
---|
480 | LogRel(("VBoxTray: NO Ext Display Support \n"));
|
---|
481 | paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
|
---|
482 | paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
|
---|
483 | paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
|
---|
484 | paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
|
---|
485 | }
|
---|
486 | DevNum++;
|
---|
487 | }
|
---|
488 |
|
---|
489 | ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
|
---|
490 | DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
|
---|
491 | i++;
|
---|
492 | }
|
---|
493 | /* Keep a record if the display with ID is already active or not. */
|
---|
494 | if (paDisplayDevices[Id].StateFlags & DISPLAY_DEVICE_ACTIVE)
|
---|
495 | {
|
---|
496 | LogRel(("VBoxTray: Display with ID=%d already enabled\n", Id));
|
---|
497 | fDispAlreadyEnabled = TRUE;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /* Width, height equal to 0 means that this value must be not changed.
|
---|
501 | * Update input parameters if necessary.
|
---|
502 | * Note: BitsPerPixel is taken into account later, when new rectangles
|
---|
503 | * are assigned to displays.
|
---|
504 | */
|
---|
505 | if (Width == 0)
|
---|
506 | {
|
---|
507 | Width = paRects[Id].right - paRects[Id].left;
|
---|
508 | }
|
---|
509 |
|
---|
510 | if (Height == 0)
|
---|
511 | {
|
---|
512 | Height = paRects[Id].bottom - paRects[Id].top;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /* Check whether a mode reset or a change is requested.
|
---|
516 | * Rectangle position is recalculated only if fEnabled is 1.
|
---|
517 | * For non extended supported modes (old Host VMs), fEnabled
|
---|
518 | * is always 1.
|
---|
519 | */
|
---|
520 | /* Handled the case where previouseresolution of secondary monitor
|
---|
521 | * was for eg. 1024*768*32 and monitor was in disabled state.
|
---|
522 | * User gives the command
|
---|
523 | * setvideomode 1024 768 32 1 yes.
|
---|
524 | * Now in this case the resolution request is same as previous one but
|
---|
525 | * monitor is going from disabled to enabled state so the below condition
|
---|
526 | * shour return false
|
---|
527 | * The below condition will only return true , if no mode reset has
|
---|
528 | * been requested AND fEnabled is 1 and fDispAlreadyEnabled is also 1 AND
|
---|
529 | * all rect conditions are true. Thus in this case nothing has to be done.
|
---|
530 | */
|
---|
531 | if ( !fModeReset && fEnabled && fDispAlreadyEnabled && !fChangePosRequest
|
---|
532 | && paRects[Id].right - paRects[Id].left == Width
|
---|
533 | && paRects[Id].bottom - paRects[Id].top == Height
|
---|
534 | && paDeviceModes[Id].dmBitsPerPel == BitsPerPixel)
|
---|
535 | {
|
---|
536 | LogRel(("VBoxTray: Already at desired resolution. No Change.\n"));
|
---|
537 | return FALSE;
|
---|
538 | }
|
---|
539 |
|
---|
540 | hlpResizeRect(paRects, NumDevices, DevPrimaryNum, Id, Width, Height);
|
---|
541 | #ifdef Log
|
---|
542 | for (i = 0; i < NumDevices; i++)
|
---|
543 | {
|
---|
544 | Log(("VBoxTray: ResizeDisplayDevice: [%d]: %d,%d %dx%d\n",
|
---|
545 | i, paRects[i].left, paRects[i].top,
|
---|
546 | paRects[i].right - paRects[i].left,
|
---|
547 | paRects[i].bottom - paRects[i].top));
|
---|
548 | }
|
---|
549 | #endif /* Log */
|
---|
550 |
|
---|
551 | #ifdef VBOX_WITH_WDDM
|
---|
552 | VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType (pCtx);
|
---|
553 | if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
|
---|
554 | {
|
---|
555 | /* Assign the new rectangles to displays. */
|
---|
556 | for (i = 0; i < NumDevices; i++)
|
---|
557 | {
|
---|
558 | paDeviceModes[i].dmPosition.x = paRects[i].left;
|
---|
559 | paDeviceModes[i].dmPosition.y = paRects[i].top;
|
---|
560 | paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
|
---|
561 | paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
|
---|
562 |
|
---|
563 | /* On Vista one must specify DM_BITSPERPEL.
|
---|
564 | * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
|
---|
565 | */
|
---|
566 | paDeviceModes[i].dmFields = DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
|
---|
567 |
|
---|
568 | if (i == Id && BitsPerPixel != 0)
|
---|
569 | {
|
---|
570 | LogRel(("VBoxTray: (WDDM)Changing resolution and position. \n"));
|
---|
571 | /* Change dmBitsPerPel if requested. */
|
---|
572 | paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
|
---|
573 | paDeviceModes[i].dmPelsWidth = Width;
|
---|
574 | paDeviceModes[i].dmPelsHeight = Height;
|
---|
575 | if (dwNewPosX != 0 || dwNewPosY != 0)
|
---|
576 | {
|
---|
577 | paDeviceModes[Id].dmPosition.x = dwNewPosX;
|
---|
578 | paDeviceModes[Id].dmPosition.y = dwNewPosY;
|
---|
579 | }
|
---|
580 | else
|
---|
581 | {
|
---|
582 | paDeviceModes[i].dmFields |= DM_POSITION;
|
---|
583 | paDeviceModes[Id].dmPosition.x = 0;
|
---|
584 | paDeviceModes[Id].dmPosition.y = 0;
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | Log(("VBoxTray: (WDDM) ResizeDisplayDevice: pfnChangeDisplaySettingsEx %x: %dx%dx%d at %d,%d\n",
|
---|
589 | gCtx.pfnChangeDisplaySettingsEx,
|
---|
590 | paDeviceModes[i].dmPelsWidth,
|
---|
591 | paDeviceModes[i].dmPelsHeight,
|
---|
592 | paDeviceModes[i].dmBitsPerPel,
|
---|
593 | paDeviceModes[i].dmPosition.x,
|
---|
594 | paDeviceModes[i].dmPosition.y));
|
---|
595 |
|
---|
596 | }
|
---|
597 | /* Reques to enable /disable the secondary Display Device. Won't take the resize request now.*/
|
---|
598 | if (!fDispAlreadyEnabled && fEnabled || fDispAlreadyEnabled && !fEnabled)
|
---|
599 | {
|
---|
600 | OSVERSIONINFO OSinfo;
|
---|
601 | OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
|
---|
602 | GetVersionEx (&OSinfo);
|
---|
603 |
|
---|
604 | /* for win 7 and above */
|
---|
605 | if (OSinfo.dwMajorVersion >= 6 && OSinfo.dwMinorVersion >= 1)
|
---|
606 | {
|
---|
607 | LogRel(("VBoxTray: (WDDM) Request to enable/disable %d display device\n", fEnabled));
|
---|
608 | DWORD dwStatus = vboxDispIfWddmEnableDisplay(&pCtx->pEnv->dispIf, Id, fEnabled);
|
---|
609 | if(dwStatus != ERROR_SUCCESS)
|
---|
610 | {
|
---|
611 | /* Not going to retry for enabling or disabling of the secondary display device.*/
|
---|
612 | LogRel(("VBoxTray: (WDDM) Failed to enable the Display Device \n"));
|
---|
613 | }
|
---|
614 | }
|
---|
615 | else /* case: vista in wddm mode. SetDisplayConfig APIs etc is not avilable in this mode. */
|
---|
616 | {
|
---|
617 | /* use traditional approach of ChangeDisplaySettingEx to enable/disable secondary monitor for Vista WDDM mode.*/
|
---|
618 | dwStatus = EnableAndResizeDispDev(paDeviceModes, paDisplayDevices, DevNum, Id, Width, Height, BitsPerPixel,
|
---|
619 | dwNewPosX, dwNewPosY, fEnabled, fExtDispSup, pCtx);
|
---|
620 | if (dwStatus != DISP_CHANGE_SUCCESSFUL )
|
---|
621 | {
|
---|
622 | /* Successfully set new video mode or our driver can not set
|
---|
623 | * the requested mode. Stop trying.
|
---|
624 | */
|
---|
625 | LogRel(("VBoxTray: (WDDM) Failed to enable/disable the Display Device \n"));
|
---|
626 | }
|
---|
627 | }
|
---|
628 | return FALSE; /* for enable disable not retrying */
|
---|
629 | }
|
---|
630 | else
|
---|
631 | {
|
---|
632 | /* Resize request. Secondary display device should be in an enabled state. */
|
---|
633 | LogRel(("VBoxTray: (WDDM) Request to resize the display \n"));
|
---|
634 | DWORD err = VBoxDispIfResizeModes(&pCtx->pEnv->dispIf, Id, paDisplayDevices, paDeviceModes, NumDevices);
|
---|
635 | if (err == NO_ERROR || err != ERROR_RETRY)
|
---|
636 | {
|
---|
637 | if (err == NO_ERROR)
|
---|
638 | LogRel(("VBoxTray: VBoxDisplayThread: (WDDM) VBoxDispIfResizeModes succeeded\n"));
|
---|
639 | else
|
---|
640 | LogRel(("VBoxTray: VBoxDisplayThread: (WDDM) Failure VBoxDispIfResizeModes (%d)\n", err));
|
---|
641 | return FALSE;
|
---|
642 | }
|
---|
643 | }
|
---|
644 | Log(("VBoxTray: ResizeDisplayDevice: (WDDM) RETRY requested\n"));
|
---|
645 | return TRUE;
|
---|
646 | }
|
---|
647 | #endif
|
---|
648 | /* Without this, Windows will not ask the miniport for its
|
---|
649 | * mode table but uses an internal cache instead.
|
---|
650 | */
|
---|
651 | for (i = 0; i < NumDevices; i++)
|
---|
652 | {
|
---|
653 | DEVMODE tempDevMode;
|
---|
654 | ZeroMemory (&tempDevMode, sizeof (tempDevMode));
|
---|
655 | tempDevMode.dmSize = sizeof(DEVMODE);
|
---|
656 | EnumDisplaySettings((LPSTR)paDisplayDevices[i].DeviceName, 0xffffff, &tempDevMode);
|
---|
657 | Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings last error %d\n", GetLastError ()));
|
---|
658 | }
|
---|
659 |
|
---|
660 | /* Assign the new rectangles to displays. */
|
---|
661 | for (i = 0; i < NumDevices; i++)
|
---|
662 | {
|
---|
663 | paDeviceModes[i].dmPosition.x = paRects[i].left;
|
---|
664 | paDeviceModes[i].dmPosition.y = paRects[i].top;
|
---|
665 | paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
|
---|
666 | paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
|
---|
667 |
|
---|
668 | /* On Vista one must specify DM_BITSPERPEL.
|
---|
669 | * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
|
---|
670 | */
|
---|
671 | paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
|
---|
672 |
|
---|
673 | if ( i == Id
|
---|
674 | && BitsPerPixel != 0)
|
---|
675 | {
|
---|
676 | /* Change dmBitsPerPel if requested. */
|
---|
677 | paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
|
---|
678 | }
|
---|
679 |
|
---|
680 | Log(("VBoxTray: ResizeDisplayDevice: pfnChangeDisplaySettingsEx Current MonitorId=%d: %dx%dx%d at %d,%d\n",
|
---|
681 | i,
|
---|
682 | paDeviceModes[i].dmPelsWidth,
|
---|
683 | paDeviceModes[i].dmPelsHeight,
|
---|
684 | paDeviceModes[i].dmBitsPerPel,
|
---|
685 | paDeviceModes[i].dmPosition.x,
|
---|
686 | paDeviceModes[i].dmPosition.y));
|
---|
687 |
|
---|
688 | LONG status = gCtx.pfnChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
|
---|
689 | &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
|
---|
690 | Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", status, GetLastError ()));
|
---|
691 | }
|
---|
692 |
|
---|
693 | Log(("VBoxTray: Enable And Resize Device. Id = %d, Width=%d Height=%d, \
|
---|
694 | dwNewPosX = %d, dwNewPosY = %d fEnabled=%d & fExtDispSupport = %d \n",
|
---|
695 | Id, Width, Height, dwNewPosX, dwNewPosY, fEnabled, fExtDispSup));
|
---|
696 | dwStatus = EnableAndResizeDispDev(paDeviceModes, paDisplayDevices, DevNum, Id, Width, Height, BitsPerPixel,
|
---|
697 | dwNewPosX, dwNewPosY, fEnabled, fExtDispSup, pCtx);
|
---|
698 | if (dwStatus == DISP_CHANGE_SUCCESSFUL || dwStatus == DISP_CHANGE_BADMODE)
|
---|
699 | {
|
---|
700 | /* Successfully set new video mode or our driver can not set
|
---|
701 | * the requested mode. Stop trying.
|
---|
702 | */
|
---|
703 | return FALSE;
|
---|
704 | }
|
---|
705 | /* Retry the request. */
|
---|
706 | return TRUE;
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Thread function to wait for and process display change
|
---|
711 | * requests
|
---|
712 | */
|
---|
713 | unsigned __stdcall VBoxDisplayThread(void *pInstance)
|
---|
714 | {
|
---|
715 | Log(("VBoxTray: VBoxDisplayThread: Entered\n"));
|
---|
716 |
|
---|
717 | VBOXDISPLAYCONTEXT *pCtx = (VBOXDISPLAYCONTEXT *)pInstance;
|
---|
718 | HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
|
---|
719 | bool fTerminate = false;
|
---|
720 | VBoxGuestFilterMaskInfo maskInfo;
|
---|
721 | DWORD cbReturned;
|
---|
722 |
|
---|
723 | maskInfo.u32OrMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
|
---|
724 | maskInfo.u32NotMask = 0;
|
---|
725 | if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
726 | {
|
---|
727 | Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - or) failed, thread exiting\n"));
|
---|
728 | return 0;
|
---|
729 | }
|
---|
730 |
|
---|
731 | int rc = VbglR3SetGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0);
|
---|
732 | if (RT_FAILURE(rc))
|
---|
733 | {
|
---|
734 | LogRel(("VBoxTray: VBoxDisplayThread: Failed to set the graphics capability with rc=%Rrc, thread exiting\n", rc));
|
---|
735 | return 0;
|
---|
736 | }
|
---|
737 |
|
---|
738 | do
|
---|
739 | {
|
---|
740 | BOOL fExtDispSup = TRUE;
|
---|
741 | /* Wait for a display change event. */
|
---|
742 | VBoxGuestWaitEventInfo waitEvent;
|
---|
743 | waitEvent.u32TimeoutIn = 1000;
|
---|
744 | waitEvent.u32EventMaskIn = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
|
---|
745 | if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
|
---|
746 | {
|
---|
747 | /*Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl succeeded\n"));*/
|
---|
748 |
|
---|
749 | if (NULL == pCtx) {
|
---|
750 | Log(("VBoxTray: VBoxDisplayThread: Invalid context detected!\n"));
|
---|
751 | break;
|
---|
752 | }
|
---|
753 |
|
---|
754 | if (NULL == pCtx->pEnv) {
|
---|
755 | Log(("VBoxTray: VBoxDisplayThread: Invalid context environment detected!\n"));
|
---|
756 | break;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* are we supposed to stop? */
|
---|
760 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
|
---|
761 | break;
|
---|
762 |
|
---|
763 | /*Log(("VBoxTray: VBoxDisplayThread: checking event\n"));*/
|
---|
764 |
|
---|
765 | /* did we get the right event? */
|
---|
766 | if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
|
---|
767 | {
|
---|
768 | Log(("VBoxTray: VBoxDisplayThread: going to get display change information\n"));
|
---|
769 | BOOL fDisplayChangeQueried;
|
---|
770 |
|
---|
771 |
|
---|
772 | /* We got at least one event. Read the requested resolution
|
---|
773 | * and try to set it until success. New events will not be seen
|
---|
774 | * but a new resolution will be read in this poll loop.
|
---|
775 | */
|
---|
776 | /* Try if extended mode display information is available from the host. */
|
---|
777 | VMMDevDisplayChangeRequestEx displayChangeRequest = {0};
|
---|
778 | fExtDispSup = TRUE;
|
---|
779 | displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequestEx);
|
---|
780 | displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
|
---|
781 | displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequestEx;
|
---|
782 | displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
783 | fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequestEx)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequestEx),
|
---|
784 | &displayChangeRequest, sizeof(VMMDevDisplayChangeRequestEx), &cbReturned, NULL);
|
---|
785 |
|
---|
786 | if (!fDisplayChangeQueried)
|
---|
787 | {
|
---|
788 | Log(("VBoxTray: Extended Display Not Supported. Trying VMMDevDisplayChangeRequest2\n"));
|
---|
789 | fExtDispSup = FALSE; /* Extended display Change request is not supported */
|
---|
790 |
|
---|
791 | displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest2);
|
---|
792 | displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
|
---|
793 | displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest2;
|
---|
794 | displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
795 | fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest2)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2),
|
---|
796 | &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2), &cbReturned, NULL);
|
---|
797 | displayChangeRequest.cxOrigin = 0;
|
---|
798 | displayChangeRequest.cyOrigin = 0;
|
---|
799 | displayChangeRequest.fChangeOrigin = 0;
|
---|
800 | displayChangeRequest.fEnabled = 1; /* Always Enabled for old VMs on Host.*/
|
---|
801 | }
|
---|
802 |
|
---|
803 | if (!fDisplayChangeQueried)
|
---|
804 | {
|
---|
805 | Log(("VBoxTray: Extended Display Not Supported. Trying VMMDevDisplayChangeRequest\n"));
|
---|
806 | fExtDispSup = FALSE; /*Extended display Change request is not supported */
|
---|
807 | /* Try the old version of the request for old VBox hosts. */
|
---|
808 | displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest);
|
---|
809 | displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
|
---|
810 | displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest;
|
---|
811 | displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
|
---|
812 | fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest),
|
---|
813 | &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest), &cbReturned, NULL);
|
---|
814 | displayChangeRequest.display = 0;
|
---|
815 | displayChangeRequest.cxOrigin = 0;
|
---|
816 | displayChangeRequest.cyOrigin = 0;
|
---|
817 | displayChangeRequest.fChangeOrigin = 0;
|
---|
818 | displayChangeRequest.fEnabled = 1; /* Always Enabled for old VMs on Host.*/
|
---|
819 | }
|
---|
820 |
|
---|
821 | if (fDisplayChangeQueried)
|
---|
822 | {
|
---|
823 | /* Try to set the requested video mode. Repeat until it is successful or is rejected by the driver. */
|
---|
824 | for (;;)
|
---|
825 | {
|
---|
826 | Log(("VBoxTray: VBoxDisplayThread: VMMDevReq_GetDisplayChangeRequest2: %dx%dx%d at %d\n", displayChangeRequest.xres, displayChangeRequest.yres, displayChangeRequest.bpp, displayChangeRequest.display));
|
---|
827 |
|
---|
828 | /*
|
---|
829 | * Only try to change video mode if the active display driver is VBox additions.
|
---|
830 | */
|
---|
831 | #ifdef VBOX_WITH_WDDM
|
---|
832 | VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType (pCtx);
|
---|
833 |
|
---|
834 | if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
|
---|
835 | Log(("VBoxTray: VBoxDisplayThread: Detected WDDM Driver\n"));
|
---|
836 |
|
---|
837 | if (enmDriverType != VBOXDISPLAY_DRIVER_TYPE_UNKNOWN)
|
---|
838 | #else
|
---|
839 | if (isVBoxDisplayDriverActive (pCtx))
|
---|
840 | #endif
|
---|
841 | {
|
---|
842 | Log(("VBoxTray: VBoxDisplayThread: Display driver is active!\n"));
|
---|
843 |
|
---|
844 | if (pCtx->pfnChangeDisplaySettingsEx != 0)
|
---|
845 | {
|
---|
846 | Log(("VBoxTray: VBoxDisplayThread: Detected W2K or later\n"));
|
---|
847 | /* W2K or later. */
|
---|
848 | 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",
|
---|
849 | displayChangeRequest.display,
|
---|
850 | displayChangeRequest.xres,
|
---|
851 | displayChangeRequest.yres,
|
---|
852 | displayChangeRequest.bpp,
|
---|
853 | displayChangeRequest.fEnabled,
|
---|
854 | displayChangeRequest.cxOrigin,
|
---|
855 | displayChangeRequest.cyOrigin,
|
---|
856 | displayChangeRequest.fChangeOrigin));
|
---|
857 | if (!ResizeDisplayDevice(displayChangeRequest.display,
|
---|
858 | displayChangeRequest.xres,
|
---|
859 | displayChangeRequest.yres,
|
---|
860 | displayChangeRequest.bpp,
|
---|
861 | displayChangeRequest.fEnabled,
|
---|
862 | displayChangeRequest.cxOrigin,
|
---|
863 | displayChangeRequest.cyOrigin,
|
---|
864 | pCtx,
|
---|
865 | fExtDispSup
|
---|
866 | ))
|
---|
867 | {
|
---|
868 | Log(("ResizeDipspalyDevice return 0\n"));
|
---|
869 | break;
|
---|
870 | }
|
---|
871 |
|
---|
872 | }
|
---|
873 | else
|
---|
874 | {
|
---|
875 | Log(("VBoxTray: VBoxDisplayThread: Detected NT\n"));
|
---|
876 |
|
---|
877 | /* Single monitor NT. */
|
---|
878 | DEVMODE devMode;
|
---|
879 | RT_ZERO(devMode);
|
---|
880 | devMode.dmSize = sizeof(DEVMODE);
|
---|
881 |
|
---|
882 | /* get the current screen setup */
|
---|
883 | if (EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &devMode))
|
---|
884 | {
|
---|
885 | Log(("VBoxTray: VBoxDisplayThread: Current mode: %d x %d x %d at %d,%d\n",
|
---|
886 | devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmPosition.x, devMode.dmPosition.y));
|
---|
887 |
|
---|
888 | /* Check whether a mode reset or a change is requested. */
|
---|
889 | if (displayChangeRequest.xres || displayChangeRequest.yres || displayChangeRequest.bpp)
|
---|
890 | {
|
---|
891 | /* A change is requested.
|
---|
892 | * Set values which are not to be changed to the current values.
|
---|
893 | */
|
---|
894 | if (!displayChangeRequest.xres)
|
---|
895 | displayChangeRequest.xres = devMode.dmPelsWidth;
|
---|
896 | if (!displayChangeRequest.yres)
|
---|
897 | displayChangeRequest.yres = devMode.dmPelsHeight;
|
---|
898 | if (!displayChangeRequest.bpp)
|
---|
899 | displayChangeRequest.bpp = devMode.dmBitsPerPel;
|
---|
900 | }
|
---|
901 | else
|
---|
902 | {
|
---|
903 | /* All zero values means a forced mode reset. Do nothing. */
|
---|
904 | Log(("VBoxTray: VBoxDisplayThread: Forced mode reset\n"));
|
---|
905 | }
|
---|
906 |
|
---|
907 | /* Verify that the mode is indeed changed. */
|
---|
908 | if ( devMode.dmPelsWidth == displayChangeRequest.xres
|
---|
909 | && devMode.dmPelsHeight == displayChangeRequest.yres
|
---|
910 | && devMode.dmBitsPerPel == displayChangeRequest.bpp)
|
---|
911 | {
|
---|
912 | Log(("VBoxTray: VBoxDisplayThread: already at desired resolution\n"));
|
---|
913 | break;
|
---|
914 | }
|
---|
915 |
|
---|
916 | // without this, Windows will not ask the miniport for its
|
---|
917 | // mode table but uses an internal cache instead
|
---|
918 | DEVMODE tempDevMode = {0};
|
---|
919 | tempDevMode.dmSize = sizeof(DEVMODE);
|
---|
920 | EnumDisplaySettings(NULL, 0xffffff, &tempDevMode);
|
---|
921 |
|
---|
922 | /* adjust the values that are supposed to change */
|
---|
923 | if (displayChangeRequest.xres)
|
---|
924 | devMode.dmPelsWidth = displayChangeRequest.xres;
|
---|
925 | if (displayChangeRequest.yres)
|
---|
926 | devMode.dmPelsHeight = displayChangeRequest.yres;
|
---|
927 | if (displayChangeRequest.bpp)
|
---|
928 | devMode.dmBitsPerPel = displayChangeRequest.bpp;
|
---|
929 |
|
---|
930 | Log(("VBoxTray: VBoxDisplayThread: setting new mode %d x %d, %d BPP\n",
|
---|
931 | devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel));
|
---|
932 |
|
---|
933 | /* set the new mode */
|
---|
934 | LONG status = ChangeDisplaySettings(&devMode, CDS_UPDATEREGISTRY);
|
---|
935 | if (status != DISP_CHANGE_SUCCESSFUL)
|
---|
936 | {
|
---|
937 | Log(("VBoxTray: VBoxDisplayThread: error from ChangeDisplaySettings: %d\n", status));
|
---|
938 |
|
---|
939 | if (status == DISP_CHANGE_BADMODE)
|
---|
940 | {
|
---|
941 | /* Our driver can not set the requested mode. Stop trying. */
|
---|
942 | break;
|
---|
943 | }
|
---|
944 | }
|
---|
945 | else
|
---|
946 | {
|
---|
947 | /* Successfully set new video mode. */
|
---|
948 | break;
|
---|
949 | }
|
---|
950 | }
|
---|
951 | else
|
---|
952 | {
|
---|
953 | Log(("VBoxTray: VBoxDisplayThread: error from EnumDisplaySettings: %d\n", GetLastError ()));
|
---|
954 | break;
|
---|
955 | }
|
---|
956 | }
|
---|
957 | }
|
---|
958 | else
|
---|
959 | {
|
---|
960 | Log(("VBoxTray: VBoxDisplayThread: vboxDisplayDriver is not active\n"));
|
---|
961 | }
|
---|
962 |
|
---|
963 | /* Retry the change a bit later. */
|
---|
964 | /* are we supposed to stop? */
|
---|
965 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 1000) == WAIT_OBJECT_0)
|
---|
966 | {
|
---|
967 | fTerminate = true;
|
---|
968 | break;
|
---|
969 | }
|
---|
970 | }
|
---|
971 | }
|
---|
972 | else
|
---|
973 | {
|
---|
974 | Log(("VBoxTray: VBoxDisplayThread: error from DeviceIoControl VBOXGUEST_IOCTL_VMMREQUEST\n"));
|
---|
975 | /* sleep a bit to not eat too much CPU while retrying */
|
---|
976 | /* are we supposed to stop? */
|
---|
977 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 50) == WAIT_OBJECT_0)
|
---|
978 | {
|
---|
979 | fTerminate = true;
|
---|
980 | break;
|
---|
981 | }
|
---|
982 | }
|
---|
983 | }
|
---|
984 | if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED)
|
---|
985 | hlpReloadCursor();
|
---|
986 | } else
|
---|
987 | {
|
---|
988 | Log(("VBoxTray: VBoxDisplayThread: error 0 from DeviceIoControl VBOXGUEST_IOCTL_WAITEVENT\n"));
|
---|
989 | /* sleep a bit to not eat too much CPU in case the above call always fails */
|
---|
990 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
|
---|
991 | {
|
---|
992 | fTerminate = true;
|
---|
993 | break;
|
---|
994 | }
|
---|
995 | }
|
---|
996 | } while (!fTerminate);
|
---|
997 |
|
---|
998 | /*
|
---|
999 | * Remove event filter and graphics capability report.
|
---|
1000 | */
|
---|
1001 | maskInfo.u32OrMask = 0;
|
---|
1002 | maskInfo.u32NotMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
|
---|
1003 | if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
1004 | Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - not) failed\n"));
|
---|
1005 | VbglR3SetGuestCaps(0, VMMDEV_GUEST_SUPPORTS_GRAPHICS);
|
---|
1006 |
|
---|
1007 | Log(("VBoxTray: VBoxDisplayThread: finished display change request thread\n"));
|
---|
1008 | return 0;
|
---|
1009 | }
|
---|