VirtualBox

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

Last change on this file since 34575 was 34130, checked in by vboxsync, 14 years ago

wddm: autoresize fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 32.1 KB
Line 
1/* $Id: VBoxDisplay.cpp 34130 2010-11-16 22:42:54Z 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 <windows.h>
19#include "VBoxTray.h"
20#include "VBoxHelpers.h"
21#include "VBoxSeamless.h"
22#include <VBoxHook.h>
23#include <VBoxDisplay.h>
24#include <VBox/VMMDev.h>
25#include <iprt/assert.h>
26#include <malloc.h>
27#include <VBoxGuestInternal.h>
28
29typedef struct _VBOXDISPLAYCONTEXT
30{
31 const VBOXSERVICEENV *pEnv;
32
33 /* ChangeDisplaySettingsEx does not exist in NT. ResizeDisplayDevice uses the function. */
34 LONG (WINAPI * pfnChangeDisplaySettingsEx)(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam);
35
36 /* EnumDisplayDevices does not exist in NT. isVBoxDisplayDriverActive et al. are using these functions. */
37 BOOL (WINAPI * pfnEnumDisplayDevices)(IN LPCSTR lpDevice, IN DWORD iDevNum, OUT PDISPLAY_DEVICEA lpDisplayDevice, IN DWORD dwFlags);
38} VBOXDISPLAYCONTEXT;
39
40static VBOXDISPLAYCONTEXT gCtx = {0};
41
42#ifdef VBOX_WITH_WDDM
43static bool vboxWddmReinitVideoModes(VBOXDISPLAYCONTEXT *pCtx)
44{
45 VBOXDISPIFESCAPE escape = {0};
46 escape.escapeCode = VBOXESC_REINITVIDEOMODES;
47 DWORD err = VBoxDispIfEscape(&pCtx->pEnv->dispIf, &escape, 0);
48 if (err != NO_ERROR)
49 {
50 Log((__FUNCTION__": VBoxDispIfEscape failed with err (%d)\n", err));
51 return false;
52 }
53 return true;
54}
55
56typedef enum
57{
58 VBOXDISPLAY_DRIVER_TYPE_UNKNOWN = 0,
59 VBOXDISPLAY_DRIVER_TYPE_XPDM = 1,
60 VBOXDISPLAY_DRIVER_TYPE_WDDM = 2
61} VBOXDISPLAY_DRIVER_TYPE;
62
63static VBOXDISPLAY_DRIVER_TYPE getVBoxDisplayDriverType (VBOXDISPLAYCONTEXT *pCtx);
64#endif
65
66int VBoxDisplayInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
67{
68 Log(("VBoxTray: VBoxDisplayInit ...\n"));
69
70 OSVERSIONINFO OSinfo;
71 OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
72 GetVersionEx (&OSinfo);
73
74 HMODULE hUser = GetModuleHandle("USER32");
75
76 gCtx.pEnv = pEnv;
77
78 if (NULL == hUser)
79 {
80 Log(("VBoxTray: VBoxDisplayInit: Could not get module handle of USER32.DLL!\n"));
81 return VERR_NOT_IMPLEMENTED;
82 }
83 else if (OSinfo.dwMajorVersion >= 5) /* APIs available only on W2K and up! */
84 {
85 *(uintptr_t *)&gCtx.pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
86 Log(("VBoxTray: VBoxDisplayInit: pfnChangeDisplaySettingsEx = %p\n", gCtx.pfnChangeDisplaySettingsEx));
87
88 *(uintptr_t *)&gCtx.pfnEnumDisplayDevices = (uintptr_t)GetProcAddress(hUser, "EnumDisplayDevicesA");
89 Log(("VBoxTray: VBoxDisplayInit: pfnEnumDisplayDevices = %p\n", gCtx.pfnEnumDisplayDevices));
90
91#ifdef VBOX_WITH_WDDM
92 if (OSinfo.dwMajorVersion >= 6)
93 {
94 /* this is vista and up, check if we need to switch the display driver if to WDDM mode */
95 Log(("VBoxTray: VBoxDisplayInit: this is Windows Vista and up\n"));
96 VBOXDISPLAY_DRIVER_TYPE enmType = getVBoxDisplayDriverType (&gCtx);
97 if (enmType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
98 {
99 Log(("VBoxTray: VBoxDisplayInit: WDDM driver is installed, switching display driver if to WDDM mode\n"));
100 /* this is hacky, but the most easiest way */
101 DWORD err = VBoxDispIfSwitchMode(const_cast<PVBOXDISPIF>(&pEnv->dispIf), VBOXDISPIF_MODE_WDDM, NULL /* old mode, we don't care about it */);
102 if (err == NO_ERROR)
103 Log(("VBoxTray: VBoxDisplayInit: DispIf switched to WDDM mode successfully\n"));
104 else
105 Log(("VBoxTray: VBoxDisplayInit: Failed to switch DispIf to WDDM mode, err (%d)\n", err));
106 }
107 }
108#endif
109 }
110 else if (OSinfo.dwMajorVersion <= 4) /* Windows NT 4.0 */
111 {
112 /* Nothing to do here yet */
113 }
114 else /* Unsupported platform */
115 {
116 Log(("VBoxTray: VBoxDisplayInit: Warning, display for platform not handled yet!\n"));
117 return VERR_NOT_IMPLEMENTED;
118 }
119
120 Log(("VBoxTray: VBoxDisplayInit: Display init successful\n"));
121
122 *pfStartThread = true;
123 *ppInstance = (void *)&gCtx;
124 return VINF_SUCCESS;
125}
126
127void VBoxDisplayDestroy (const VBOXSERVICEENV *pEnv, void *pInstance)
128{
129 return;
130}
131
132#ifdef VBOX_WITH_WDDM
133static VBOXDISPLAY_DRIVER_TYPE getVBoxDisplayDriverType(VBOXDISPLAYCONTEXT *pCtx)
134#else
135static bool isVBoxDisplayDriverActive(VBOXDISPLAYCONTEXT *pCtx)
136#endif
137{
138#ifdef VBOX_WITH_WDDM
139 VBOXDISPLAY_DRIVER_TYPE enmType = VBOXDISPLAY_DRIVER_TYPE_UNKNOWN;
140#else
141 bool result = false;
142#endif
143
144 if( pCtx->pfnEnumDisplayDevices )
145 {
146 INT devNum = 0;
147 DISPLAY_DEVICE dispDevice;
148 FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
149 dispDevice.cb = sizeof(DISPLAY_DEVICE);
150
151 Log(("VBoxTray: isVBoxDisplayDriverActive: Checking for active VBox display driver (W2K+) ...\n"));
152
153 while (EnumDisplayDevices(NULL,
154 devNum,
155 &dispDevice,
156 0))
157 {
158 Log(("VBoxTray: isVBoxDisplayDriverActive: DevNum:%d\nName:%s\nString:%s\nID:%s\nKey:%s\nFlags=%08X\n\n",
159 devNum,
160 &dispDevice.DeviceName[0],
161 &dispDevice.DeviceString[0],
162 &dispDevice.DeviceID[0],
163 &dispDevice.DeviceKey[0],
164 dispDevice.StateFlags));
165
166 if (dispDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
167 {
168 Log(("VBoxTray: isVBoxDisplayDriverActive: Primary device\n"));
169
170 if (strcmp(&dispDevice.DeviceString[0], "VirtualBox Graphics Adapter") == 0)
171#ifndef VBOX_WITH_WDDM
172 result = true;
173#else
174 enmType = VBOXDISPLAY_DRIVER_TYPE_XPDM;
175 else if (strcmp(&dispDevice.DeviceString[0], "VirtualBox Graphics Adapter (Microsoft Corporation - WDDM)") == 0)
176 enmType = VBOXDISPLAY_DRIVER_TYPE_WDDM;
177#endif
178 break;
179 }
180
181 FillMemory(&dispDevice, sizeof(DISPLAY_DEVICE), 0);
182
183 dispDevice.cb = sizeof(DISPLAY_DEVICE);
184
185 devNum++;
186 }
187 }
188 else /* This must be NT 4 or something really old, so don't use EnumDisplayDevices() here ... */
189 {
190 Log(("VBoxTray: isVBoxDisplayDriverActive: Checking for active VBox display driver (NT or older) ...\n"));
191
192 DEVMODE tempDevMode;
193 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
194 tempDevMode.dmSize = sizeof(DEVMODE);
195 EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &tempDevMode); /* Get current display device settings */
196
197 /* Check for the short name, because all long stuff would be truncated */
198 if (strcmp((char*)&tempDevMode.dmDeviceName[0], "VBoxDisp") == 0)
199#ifndef VBOX_WITH_WDDM
200 result = true;
201#else
202 enmType = VBOXDISPLAY_DRIVER_TYPE_XPDM;
203#endif
204 }
205
206#ifndef VBOX_WITH_WDDM
207 return result;
208#else
209 return enmType;
210#endif
211}
212
213/* Returns TRUE to try again. */
214static BOOL ResizeDisplayDevice(ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel,
215 VBOXDISPLAYCONTEXT *pCtx)
216{
217 BOOL fModeReset = (Width == 0 && Height == 0 && BitsPerPixel == 0);
218
219 DISPLAY_DEVICE DisplayDevice;
220
221 ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
222 DisplayDevice.cb = sizeof(DisplayDevice);
223
224 /* Find out how many display devices the system has */
225 DWORD NumDevices = 0;
226 DWORD i = 0;
227 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
228 {
229 Log(("VBoxTray: ResizeDisplayDevice: [%d] %s\n", i, DisplayDevice.DeviceName));
230
231 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
232 {
233 Log(("VBoxTray: ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
234 NumDevices++;
235 }
236 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
237 {
238
239 Log(("VBoxTray: ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
240 NumDevices++;
241 }
242
243 ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
244 DisplayDevice.cb = sizeof(DisplayDevice);
245 i++;
246 }
247
248 Log(("VBoxTray: ResizeDisplayDevice: Found total %d devices. err %d\n", NumDevices, GetLastError ()));
249
250 if (NumDevices == 0 || Id >= NumDevices)
251 {
252 Log(("VBoxTray: ResizeDisplayDevice: Requested identifier %d is invalid. err %d\n", Id, GetLastError ()));
253 return FALSE;
254 }
255
256 DISPLAY_DEVICE *paDisplayDevices = (DISPLAY_DEVICE *)alloca (sizeof (DISPLAY_DEVICE) * NumDevices);
257 DEVMODE *paDeviceModes = (DEVMODE *)alloca (sizeof (DEVMODE) * NumDevices);
258 RECTL *paRects = (RECTL *)alloca (sizeof (RECTL) * NumDevices);
259
260 /* Fetch information about current devices and modes. */
261 DWORD DevNum = 0;
262 DWORD DevPrimaryNum = 0;
263
264 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
265 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
266
267 i = 0;
268 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
269 {
270 Log(("VBoxTray: ResizeDisplayDevice: [%d(%d)] %s\n", i, DevNum, DisplayDevice.DeviceName));
271
272 BOOL bFetchDevice = FALSE;
273
274 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
275 {
276 Log(("VBoxTray: ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
277 DevPrimaryNum = DevNum;
278 bFetchDevice = TRUE;
279 }
280 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
281 {
282
283 Log(("VBoxTray: ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
284 bFetchDevice = TRUE;
285 }
286
287 if (bFetchDevice)
288 {
289 if (DevNum >= NumDevices)
290 {
291 Log(("VBoxTray: ResizeDisplayDevice: %d >= %d\n", NumDevices, DevNum));
292 return FALSE;
293 }
294
295 paDisplayDevices[DevNum] = DisplayDevice;
296
297 /* First try to get the video mode stored in registry (ENUM_REGISTRY_SETTINGS).
298 * A secondary display could be not active at the moment and would not have
299 * a current video mode (ENUM_CURRENT_SETTINGS).
300 */
301 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
302 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
303 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
304 ENUM_REGISTRY_SETTINGS, &paDeviceModes[DevNum]))
305 {
306 Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings error %d\n", GetLastError ()));
307 return FALSE;
308 }
309
310 if ( paDeviceModes[DevNum].dmPelsWidth == 0
311 || paDeviceModes[DevNum].dmPelsHeight == 0)
312 {
313 /* No ENUM_REGISTRY_SETTINGS yet. Seen on Vista after installation.
314 * Get the current video mode then.
315 */
316 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
317 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
318 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
319 ENUM_CURRENT_SETTINGS, &paDeviceModes[DevNum]))
320 {
321 /* ENUM_CURRENT_SETTINGS returns FALSE when the display is not active:
322 * for example a disabled secondary display.
323 * Do not return here, ignore the error and set the display info to 0x0x0.
324 */
325 Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings(ENUM_CURRENT_SETTINGS) error %d\n", GetLastError ()));
326 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
327 }
328 }
329
330 Log(("VBoxTray: ResizeDisplayDevice: %dx%dx%d at %d,%d\n",
331 paDeviceModes[DevNum].dmPelsWidth,
332 paDeviceModes[DevNum].dmPelsHeight,
333 paDeviceModes[DevNum].dmBitsPerPel,
334 paDeviceModes[DevNum].dmPosition.x,
335 paDeviceModes[DevNum].dmPosition.y));
336
337 paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
338 paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
339 paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
340 paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
341 DevNum++;
342 }
343
344 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
345 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
346 i++;
347 }
348
349 /* Width, height equal to 0 means that this value must be not changed.
350 * Update input parameters if necessary.
351 * Note: BitsPerPixel is taken into account later, when new rectangles
352 * are assigned to displays.
353 */
354 if (Width == 0)
355 {
356 Width = paRects[Id].right - paRects[Id].left;
357 }
358
359 if (Height == 0)
360 {
361 Height = paRects[Id].bottom - paRects[Id].top;
362 }
363
364 /* Check whether a mode reset or a change is requested. */
365 if ( !fModeReset
366 && paRects[Id].right - paRects[Id].left == Width
367 && paRects[Id].bottom - paRects[Id].top == Height
368 && paDeviceModes[Id].dmBitsPerPel == BitsPerPixel)
369 {
370 Log(("VBoxTray: ResizeDisplayDevice: Already at desired resolution\n"));
371 return FALSE;
372 }
373
374 hlpResizeRect(paRects, NumDevices, DevPrimaryNum, Id, Width, Height);
375#ifdef Log
376 for (i = 0; i < NumDevices; i++)
377 {
378 Log(("VBoxTray: ResizeDisplayDevice: [%d]: %d,%d %dx%d\n",
379 i, paRects[i].left, paRects[i].top,
380 paRects[i].right - paRects[i].left,
381 paRects[i].bottom - paRects[i].top));
382 }
383#endif /* Log */
384
385#ifdef VBOX_WITH_WDDM
386 VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType (pCtx);
387 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
388 {
389 /* Assign the new rectangles to displays. */
390 for (i = 0; i < NumDevices; i++)
391 {
392 paDeviceModes[i].dmPosition.x = paRects[i].left;
393 paDeviceModes[i].dmPosition.y = paRects[i].top;
394 paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
395 paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
396
397 /* On Vista one must specify DM_BITSPERPEL.
398 * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
399 */
400 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
401
402 if ( i == Id
403 && BitsPerPixel != 0)
404 {
405 /* Change dmBitsPerPel if requested. */
406 paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
407 }
408
409 Log(("VBoxTray: ResizeDisplayDevice: pfnChangeDisplaySettingsEx %x: %dx%dx%d at %d,%d\n",
410 gCtx.pfnChangeDisplaySettingsEx,
411 paDeviceModes[i].dmPelsWidth,
412 paDeviceModes[i].dmPelsHeight,
413 paDeviceModes[i].dmBitsPerPel,
414 paDeviceModes[i].dmPosition.x,
415 paDeviceModes[i].dmPosition.y));
416
417 }
418
419 DWORD err = VBoxDispIfResizeModes(&pCtx->pEnv->dispIf, paDisplayDevices, paDeviceModes, NumDevices);
420 if (err == NO_ERROR || err != ERROR_RETRY)
421 {
422 if (err == NO_ERROR)
423 Log(("VBoxTray: VBoxDisplayThread: (WDDM) VBoxDispIfResizeModes succeeded\n"));
424 else
425 Log(("VBoxTray: VBoxDisplayThread: (WDDM) Failure VBoxDispIfResizeModes (%d)\n", err));
426 return FALSE;
427 }
428
429 Log(("VBoxTray: ResizeDisplayDevice: (WDDM) RETRY requested\n"));
430 return TRUE;
431 }
432#endif
433 /* Without this, Windows will not ask the miniport for its
434 * mode table but uses an internal cache instead.
435 */
436 for (i = 0; i < NumDevices; i++)
437 {
438 DEVMODE tempDevMode;
439 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
440 tempDevMode.dmSize = sizeof(DEVMODE);
441 EnumDisplaySettings((LPSTR)paDisplayDevices[i].DeviceName, 0xffffff, &tempDevMode);
442 Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings last error %d\n", GetLastError ()));
443 }
444
445 /* Assign the new rectangles to displays. */
446 for (i = 0; i < NumDevices; i++)
447 {
448 paDeviceModes[i].dmPosition.x = paRects[i].left;
449 paDeviceModes[i].dmPosition.y = paRects[i].top;
450 paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
451 paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
452
453 /* On Vista one must specify DM_BITSPERPEL.
454 * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
455 */
456 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
457
458 if ( i == Id
459 && BitsPerPixel != 0)
460 {
461 /* Change dmBitsPerPel if requested. */
462 paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
463 }
464
465 Log(("VBoxTray: ResizeDisplayDevice: pfnChangeDisplaySettingsEx %x: %dx%dx%d at %d,%d\n",
466 gCtx.pfnChangeDisplaySettingsEx,
467 paDeviceModes[i].dmPelsWidth,
468 paDeviceModes[i].dmPelsHeight,
469 paDeviceModes[i].dmBitsPerPel,
470 paDeviceModes[i].dmPosition.x,
471 paDeviceModes[i].dmPosition.y));
472
473 LONG status = gCtx.pfnChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
474 &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
475 Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", status, GetLastError ()));
476 }
477
478 /* A second call to ChangeDisplaySettings updates the monitor. */
479 LONG status = gCtx.pfnChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
480 Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettings update status %d\n", status));
481 if (status == DISP_CHANGE_SUCCESSFUL || status == DISP_CHANGE_BADMODE)
482 {
483 /* Successfully set new video mode or our driver can not set the requested mode. Stop trying. */
484 return FALSE;
485 }
486
487 /* Retry the request. */
488 return TRUE;
489}
490
491/**
492 * Thread function to wait for and process display change
493 * requests
494 */
495unsigned __stdcall VBoxDisplayThread(void *pInstance)
496{
497 Log(("VBoxTray: VBoxDisplayThread: Entered\n"));
498
499 VBOXDISPLAYCONTEXT *pCtx = (VBOXDISPLAYCONTEXT *)pInstance;
500 HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
501 bool fTerminate = false;
502 VBoxGuestFilterMaskInfo maskInfo;
503 DWORD cbReturned;
504
505 maskInfo.u32OrMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
506 maskInfo.u32NotMask = 0;
507 if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
508 {
509 Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - or) failed, thread exiting\n"));
510 return 0;
511 }
512
513 int rc = VbglR3SetGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0);
514 if (RT_FAILURE(rc))
515 {
516 LogRel(("VBoxTray: VBoxDisplayThread: Failed to set the graphics capability with rc=%Rrc, thread exiting\n", rc));
517 return 0;
518 }
519
520 do
521 {
522 /* Wait for a display change event. */
523 VBoxGuestWaitEventInfo waitEvent;
524 waitEvent.u32TimeoutIn = 1000;
525 waitEvent.u32EventMaskIn = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
526 if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
527 {
528 /*Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl succeeded\n"));*/
529
530 if (NULL == pCtx) {
531 Log(("VBoxTray: VBoxDisplayThread: Invalid context detected!\n"));
532 break;
533 }
534
535 if (NULL == pCtx->pEnv) {
536 Log(("VBoxTray: VBoxDisplayThread: Invalid context environment detected!\n"));
537 break;
538 }
539
540 /* are we supposed to stop? */
541 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
542 break;
543
544 /*Log(("VBoxTray: VBoxDisplayThread: checking event\n"));*/
545
546 /* did we get the right event? */
547 if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
548 {
549 Log(("VBoxTray: VBoxDisplayThread: going to get display change information\n"));
550
551 /* We got at least one event. Read the requested resolution
552 * and try to set it until success. New events will not be seen
553 * but a new resolution will be read in this poll loop.
554 */
555 for (;;)
556 {
557 /* get the display change request */
558 VMMDevDisplayChangeRequest2 displayChangeRequest = {0};
559 displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest2);
560 displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
561 displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest2;
562 displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
563 BOOL fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest2)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2),
564 &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2), &cbReturned, NULL);
565 if (!fDisplayChangeQueried)
566 {
567 /* Try the old version of the request for old VBox hosts. */
568 displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest);
569 displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
570 displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest;
571 displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
572 fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest),
573 &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest), &cbReturned, NULL);
574 displayChangeRequest.display = 0;
575 }
576
577 if (fDisplayChangeQueried)
578 {
579 Log(("VBoxTray: VBoxDisplayThread: VMMDevReq_GetDisplayChangeRequest2: %dx%dx%d at %d\n", displayChangeRequest.xres, displayChangeRequest.yres, displayChangeRequest.bpp, displayChangeRequest.display));
580
581 /*
582 * Only try to change video mode if the active display driver is VBox additions.
583 */
584#ifdef VBOX_WITH_WDDM
585 VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType (pCtx);
586
587 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
588 Log(("VBoxTray: VBoxDisplayThread: Detected WDDM Driver\n"));
589
590 if (enmDriverType != VBOXDISPLAY_DRIVER_TYPE_UNKNOWN)
591#else
592 if (isVBoxDisplayDriverActive (pCtx))
593#endif
594 {
595 Log(("VBoxTray: VBoxDisplayThread: Display driver is active!\n"));
596
597 if (pCtx->pfnChangeDisplaySettingsEx != 0)
598 {
599 Log(("VBoxTray: VBoxDisplayThread: Detected W2K or later\n"));
600
601 /* W2K or later. */
602 if (!ResizeDisplayDevice(displayChangeRequest.display,
603 displayChangeRequest.xres,
604 displayChangeRequest.yres,
605 displayChangeRequest.bpp,
606 pCtx
607 ))
608 {
609 break;
610 }
611 }
612 else
613 {
614 Log(("VBoxTray: VBoxDisplayThread: Detected NT\n"));
615
616 /* Single monitor NT. */
617 DEVMODE devMode;
618 RT_ZERO(devMode);
619 devMode.dmSize = sizeof(DEVMODE);
620
621 /* get the current screen setup */
622 if (EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &devMode))
623 {
624 Log(("VBoxTray: VBoxDisplayThread: Current mode: %d x %d x %d at %d,%d\n",
625 devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmPosition.x, devMode.dmPosition.y));
626
627 /* Check whether a mode reset or a change is requested. */
628 if (displayChangeRequest.xres || displayChangeRequest.yres || displayChangeRequest.bpp)
629 {
630 /* A change is requested.
631 * Set values which are not to be changed to the current values.
632 */
633 if (!displayChangeRequest.xres)
634 displayChangeRequest.xres = devMode.dmPelsWidth;
635 if (!displayChangeRequest.yres)
636 displayChangeRequest.yres = devMode.dmPelsHeight;
637 if (!displayChangeRequest.bpp)
638 displayChangeRequest.bpp = devMode.dmBitsPerPel;
639 }
640 else
641 {
642 /* All zero values means a forced mode reset. Do nothing. */
643 Log(("VBoxTray: VBoxDisplayThread: Forced mode reset\n"));
644 }
645
646 /* Verify that the mode is indeed changed. */
647 if ( devMode.dmPelsWidth == displayChangeRequest.xres
648 && devMode.dmPelsHeight == displayChangeRequest.yres
649 && devMode.dmBitsPerPel == displayChangeRequest.bpp)
650 {
651 Log(("VBoxTray: VBoxDisplayThread: already at desired resolution\n"));
652 break;
653 }
654
655 // without this, Windows will not ask the miniport for its
656 // mode table but uses an internal cache instead
657 DEVMODE tempDevMode = {0};
658 tempDevMode.dmSize = sizeof(DEVMODE);
659 EnumDisplaySettings(NULL, 0xffffff, &tempDevMode);
660
661 /* adjust the values that are supposed to change */
662 if (displayChangeRequest.xres)
663 devMode.dmPelsWidth = displayChangeRequest.xres;
664 if (displayChangeRequest.yres)
665 devMode.dmPelsHeight = displayChangeRequest.yres;
666 if (displayChangeRequest.bpp)
667 devMode.dmBitsPerPel = displayChangeRequest.bpp;
668
669 Log(("VBoxTray: VBoxDisplayThread: setting new mode %d x %d, %d BPP\n",
670 devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel));
671
672 /* set the new mode */
673 LONG status = ChangeDisplaySettings(&devMode, CDS_UPDATEREGISTRY);
674 if (status != DISP_CHANGE_SUCCESSFUL)
675 {
676 Log(("VBoxTray: VBoxDisplayThread: error from ChangeDisplaySettings: %d\n", status));
677
678 if (status == DISP_CHANGE_BADMODE)
679 {
680 /* Our driver can not set the requested mode. Stop trying. */
681 break;
682 }
683 }
684 else
685 {
686 /* Successfully set new video mode. */
687 break;
688 }
689 }
690 else
691 {
692 Log(("VBoxTray: VBoxDisplayThread: error from EnumDisplaySettings: %d\n", GetLastError ()));
693 break;
694 }
695 }
696 }
697 else
698 {
699 Log(("VBoxTray: VBoxDisplayThread: vboxDisplayDriver is not active\n"));
700 }
701
702 /* Retry the change a bit later. */
703 /* are we supposed to stop? */
704 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 1000) == WAIT_OBJECT_0)
705 {
706 fTerminate = true;
707 break;
708 }
709 }
710 else
711 {
712 Log(("VBoxTray: VBoxDisplayThread: error from DeviceIoControl VBOXGUEST_IOCTL_VMMREQUEST\n"));
713 /* sleep a bit to not eat too much CPU while retrying */
714 /* are we supposed to stop? */
715 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 50) == WAIT_OBJECT_0)
716 {
717 fTerminate = true;
718 break;
719 }
720 }
721 }
722 }
723 if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED)
724 hlpReloadCursor();
725 } else
726 {
727 Log(("VBoxTray: VBoxDisplayThread: error 0 from DeviceIoControl VBOXGUEST_IOCTL_WAITEVENT\n"));
728 /* sleep a bit to not eat too much CPU in case the above call always fails */
729 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
730 {
731 fTerminate = true;
732 break;
733 }
734 }
735 } while (!fTerminate);
736
737 /*
738 * Remove event filter and graphics capability report.
739 */
740 maskInfo.u32OrMask = 0;
741 maskInfo.u32NotMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
742 if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
743 Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - not) failed\n"));
744 VbglR3SetGuestCaps(0, VMMDEV_GUEST_SUPPORTS_GRAPHICS);
745
746 Log(("VBoxTray: VBoxDisplayThread: finished display change request thread\n"));
747 return 0;
748}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette