VirtualBox

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

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

wddm: VBOXWDDM->VBOX_WITH_WDDM

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