VirtualBox

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

Last change on this file since 42842 was 42232, checked in by vboxsync, 13 years ago

fAnyX basics (not impl for xpdm miniport yet)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 32.2 KB
Line 
1/* $Id: VBoxDisplay.cpp 42232 2012-07-19 16:06:17Z vboxsync $ */
2/** @file
3 * VBoxSeamless - Display notifications.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17#define _WIN32_WINNT 0x0500
18#include "VBoxTray.h"
19#include "VBoxHelpers.h"
20#include "VBoxSeamless.h"
21#include <VBoxHook.h>
22#include <VBoxDisplay.h>
23#include <VBox/VMMDev.h>
24#include <iprt/assert.h>
25#include <malloc.h>
26#include <VBoxGuestInternal.h>
27#ifdef VBOX_WITH_WDDM
28#include <iprt/asm.h>
29#endif
30
31typedef 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
44static VBOXDISPLAYCONTEXT gCtx = {0};
45
46#ifdef VBOX_WITH_WDDM
47typedef 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
54static VBOXDISPLAY_DRIVER_TYPE getVBoxDisplayDriverType (VBOXDISPLAYCONTEXT *pCtx);
55#endif
56
57int 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
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 /* 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/* Returns TRUE to try again. */
216static BOOL ResizeDisplayDevice(ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel,
217 VBOXDISPLAYCONTEXT *pCtx)
218{
219 BOOL fModeReset = (Width == 0 && Height == 0 && BitsPerPixel == 0);
220
221 if (!gCtx.fAnyX)
222 Width &= 0xFFF8;
223
224 DISPLAY_DEVICE DisplayDevice;
225
226 ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
227 DisplayDevice.cb = sizeof(DisplayDevice);
228
229 /* Find out how many display devices the system has */
230 DWORD NumDevices = 0;
231 DWORD i = 0;
232 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
233 {
234 Log(("VBoxTray: ResizeDisplayDevice: [%d] %s\n", i, DisplayDevice.DeviceName));
235
236 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
237 {
238 Log(("VBoxTray: ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
239 NumDevices++;
240 }
241 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
242 {
243
244 Log(("VBoxTray: ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
245 NumDevices++;
246 }
247
248 ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
249 DisplayDevice.cb = sizeof(DisplayDevice);
250 i++;
251 }
252
253 Log(("VBoxTray: ResizeDisplayDevice: Found total %d devices. err %d\n", NumDevices, GetLastError ()));
254
255 if (NumDevices == 0 || Id >= NumDevices)
256 {
257 Log(("VBoxTray: ResizeDisplayDevice: Requested identifier %d is invalid. err %d\n", Id, GetLastError ()));
258 return FALSE;
259 }
260
261 DISPLAY_DEVICE *paDisplayDevices = (DISPLAY_DEVICE *)alloca (sizeof (DISPLAY_DEVICE) * NumDevices);
262 DEVMODE *paDeviceModes = (DEVMODE *)alloca (sizeof (DEVMODE) * NumDevices);
263 RECTL *paRects = (RECTL *)alloca (sizeof (RECTL) * NumDevices);
264
265 /* Fetch information about current devices and modes. */
266 DWORD DevNum = 0;
267 DWORD DevPrimaryNum = 0;
268
269 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
270 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
271
272 i = 0;
273 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
274 {
275 Log(("VBoxTray: ResizeDisplayDevice: [%d(%d)] %s\n", i, DevNum, DisplayDevice.DeviceName));
276
277 BOOL bFetchDevice = FALSE;
278
279 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
280 {
281 Log(("VBoxTray: ResizeDisplayDevice: Found primary device. err %d\n", GetLastError ()));
282 DevPrimaryNum = DevNum;
283 bFetchDevice = TRUE;
284 }
285 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
286 {
287
288 Log(("VBoxTray: ResizeDisplayDevice: Found secondary device. err %d\n", GetLastError ()));
289 bFetchDevice = TRUE;
290 }
291
292 if (bFetchDevice)
293 {
294 if (DevNum >= NumDevices)
295 {
296 Log(("VBoxTray: ResizeDisplayDevice: %d >= %d\n", NumDevices, DevNum));
297 return FALSE;
298 }
299
300 paDisplayDevices[DevNum] = DisplayDevice;
301
302 /* First try to get the video mode stored in registry (ENUM_REGISTRY_SETTINGS).
303 * A secondary display could be not active at the moment and would not have
304 * a current video mode (ENUM_CURRENT_SETTINGS).
305 */
306 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
307 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
308 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
309 ENUM_REGISTRY_SETTINGS, &paDeviceModes[DevNum]))
310 {
311 Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings error %d\n", GetLastError ()));
312 return FALSE;
313 }
314
315 if ( paDeviceModes[DevNum].dmPelsWidth == 0
316 || paDeviceModes[DevNum].dmPelsHeight == 0)
317 {
318 /* No ENUM_REGISTRY_SETTINGS yet. Seen on Vista after installation.
319 * Get the current video mode then.
320 */
321 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
322 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
323 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
324 ENUM_CURRENT_SETTINGS, &paDeviceModes[DevNum]))
325 {
326 /* ENUM_CURRENT_SETTINGS returns FALSE when the display is not active:
327 * for example a disabled secondary display.
328 * Do not return here, ignore the error and set the display info to 0x0x0.
329 */
330 Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings(ENUM_CURRENT_SETTINGS) error %d\n", GetLastError ()));
331 ZeroMemory(&paDeviceModes[DevNum], sizeof(DEVMODE));
332 }
333 }
334
335 Log(("VBoxTray: ResizeDisplayDevice: %dx%dx%d at %d,%d\n",
336 paDeviceModes[DevNum].dmPelsWidth,
337 paDeviceModes[DevNum].dmPelsHeight,
338 paDeviceModes[DevNum].dmBitsPerPel,
339 paDeviceModes[DevNum].dmPosition.x,
340 paDeviceModes[DevNum].dmPosition.y));
341
342 paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
343 paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
344 paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
345 paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
346 DevNum++;
347 }
348
349 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
350 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
351 i++;
352 }
353
354 /* Width, height equal to 0 means that this value must be not changed.
355 * Update input parameters if necessary.
356 * Note: BitsPerPixel is taken into account later, when new rectangles
357 * are assigned to displays.
358 */
359 if (Width == 0)
360 {
361 Width = paRects[Id].right - paRects[Id].left;
362 }
363
364 if (Height == 0)
365 {
366 Height = paRects[Id].bottom - paRects[Id].top;
367 }
368
369 /* Check whether a mode reset or a change is requested. */
370 if ( !fModeReset
371 && paRects[Id].right - paRects[Id].left == Width
372 && paRects[Id].bottom - paRects[Id].top == Height
373 && paDeviceModes[Id].dmBitsPerPel == BitsPerPixel)
374 {
375 Log(("VBoxTray: ResizeDisplayDevice: Already at desired resolution\n"));
376 return FALSE;
377 }
378
379 hlpResizeRect(paRects, NumDevices, DevPrimaryNum, Id, Width, Height);
380#ifdef Log
381 for (i = 0; i < NumDevices; i++)
382 {
383 Log(("VBoxTray: ResizeDisplayDevice: [%d]: %d,%d %dx%d\n",
384 i, paRects[i].left, paRects[i].top,
385 paRects[i].right - paRects[i].left,
386 paRects[i].bottom - paRects[i].top));
387 }
388#endif /* Log */
389
390#ifdef VBOX_WITH_WDDM
391 VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType (pCtx);
392 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
393 {
394 /* Assign the new rectangles to displays. */
395 for (i = 0; i < NumDevices; i++)
396 {
397 paDeviceModes[i].dmPosition.x = paRects[i].left;
398 paDeviceModes[i].dmPosition.y = paRects[i].top;
399 paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
400 paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
401
402 /* On Vista one must specify DM_BITSPERPEL.
403 * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
404 */
405 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
406
407 if ( i == Id
408 && BitsPerPixel != 0)
409 {
410 /* Change dmBitsPerPel if requested. */
411 paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
412 }
413
414 Log(("VBoxTray: ResizeDisplayDevice: pfnChangeDisplaySettingsEx %x: %dx%dx%d at %d,%d\n",
415 gCtx.pfnChangeDisplaySettingsEx,
416 paDeviceModes[i].dmPelsWidth,
417 paDeviceModes[i].dmPelsHeight,
418 paDeviceModes[i].dmBitsPerPel,
419 paDeviceModes[i].dmPosition.x,
420 paDeviceModes[i].dmPosition.y));
421
422 }
423
424 DWORD err = VBoxDispIfResizeModes(&pCtx->pEnv->dispIf, Id, paDisplayDevices, paDeviceModes, NumDevices);
425 if (err == NO_ERROR || err != ERROR_RETRY)
426 {
427 if (err == NO_ERROR)
428 Log(("VBoxTray: VBoxDisplayThread: (WDDM) VBoxDispIfResizeModes succeeded\n"));
429 else
430 Log(("VBoxTray: VBoxDisplayThread: (WDDM) Failure VBoxDispIfResizeModes (%d)\n", err));
431 return FALSE;
432 }
433
434 Log(("VBoxTray: ResizeDisplayDevice: (WDDM) RETRY requested\n"));
435 return TRUE;
436 }
437#endif
438 /* Without this, Windows will not ask the miniport for its
439 * mode table but uses an internal cache instead.
440 */
441 for (i = 0; i < NumDevices; i++)
442 {
443 DEVMODE tempDevMode;
444 ZeroMemory (&tempDevMode, sizeof (tempDevMode));
445 tempDevMode.dmSize = sizeof(DEVMODE);
446 EnumDisplaySettings((LPSTR)paDisplayDevices[i].DeviceName, 0xffffff, &tempDevMode);
447 Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings last error %d\n", GetLastError ()));
448 }
449
450 /* Assign the new rectangles to displays. */
451 for (i = 0; i < NumDevices; i++)
452 {
453 paDeviceModes[i].dmPosition.x = paRects[i].left;
454 paDeviceModes[i].dmPosition.y = paRects[i].top;
455 paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
456 paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
457
458 /* On Vista one must specify DM_BITSPERPEL.
459 * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
460 */
461 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
462
463 if ( i == Id
464 && BitsPerPixel != 0)
465 {
466 /* Change dmBitsPerPel if requested. */
467 paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
468 }
469
470 Log(("VBoxTray: ResizeDisplayDevice: pfnChangeDisplaySettingsEx %x: %dx%dx%d at %d,%d\n",
471 gCtx.pfnChangeDisplaySettingsEx,
472 paDeviceModes[i].dmPelsWidth,
473 paDeviceModes[i].dmPelsHeight,
474 paDeviceModes[i].dmBitsPerPel,
475 paDeviceModes[i].dmPosition.x,
476 paDeviceModes[i].dmPosition.y));
477
478 LONG status = gCtx.pfnChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
479 &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
480 Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", status, GetLastError ()));
481 }
482
483 /* A second call to ChangeDisplaySettings updates the monitor. */
484 LONG status = gCtx.pfnChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
485 Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettings update status %d\n", status));
486 if (status == DISP_CHANGE_SUCCESSFUL || status == DISP_CHANGE_BADMODE)
487 {
488 /* Successfully set new video mode or our driver can not set the requested mode. Stop trying. */
489 return FALSE;
490 }
491
492 /* Retry the request. */
493 return TRUE;
494}
495
496/**
497 * Thread function to wait for and process display change
498 * requests
499 */
500unsigned __stdcall VBoxDisplayThread(void *pInstance)
501{
502 Log(("VBoxTray: VBoxDisplayThread: Entered\n"));
503
504 VBOXDISPLAYCONTEXT *pCtx = (VBOXDISPLAYCONTEXT *)pInstance;
505 HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
506 bool fTerminate = false;
507 VBoxGuestFilterMaskInfo maskInfo;
508 DWORD cbReturned;
509
510 maskInfo.u32OrMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
511 maskInfo.u32NotMask = 0;
512 if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
513 {
514 Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - or) failed, thread exiting\n"));
515 return 0;
516 }
517
518 int rc = VbglR3SetGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0);
519 if (RT_FAILURE(rc))
520 {
521 LogRel(("VBoxTray: VBoxDisplayThread: Failed to set the graphics capability with rc=%Rrc, thread exiting\n", rc));
522 return 0;
523 }
524
525 do
526 {
527 /* Wait for a display change event. */
528 VBoxGuestWaitEventInfo waitEvent;
529 waitEvent.u32TimeoutIn = 1000;
530 waitEvent.u32EventMaskIn = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
531 if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
532 {
533 /*Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl succeeded\n"));*/
534
535 if (NULL == pCtx) {
536 Log(("VBoxTray: VBoxDisplayThread: Invalid context detected!\n"));
537 break;
538 }
539
540 if (NULL == pCtx->pEnv) {
541 Log(("VBoxTray: VBoxDisplayThread: Invalid context environment detected!\n"));
542 break;
543 }
544
545 /* are we supposed to stop? */
546 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
547 break;
548
549 /*Log(("VBoxTray: VBoxDisplayThread: checking event\n"));*/
550
551 /* did we get the right event? */
552 if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
553 {
554 Log(("VBoxTray: VBoxDisplayThread: going to get display change information\n"));
555
556 /* We got at least one event. Read the requested resolution
557 * and try to set it until success. New events will not be seen
558 * but a new resolution will be read in this poll loop.
559 */
560 VMMDevDisplayChangeRequest2 displayChangeRequest = {0};
561 displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest2);
562 displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
563 displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest2;
564 displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
565 BOOL fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest2)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2),
566 &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2), &cbReturned, NULL);
567 if (!fDisplayChangeQueried)
568 {
569 /* Try the old version of the request for old VBox hosts. */
570 displayChangeRequest.header.size = sizeof(VMMDevDisplayChangeRequest);
571 displayChangeRequest.header.version = VMMDEV_REQUEST_HEADER_VERSION;
572 displayChangeRequest.header.requestType = VMMDevReq_GetDisplayChangeRequest;
573 displayChangeRequest.eventAck = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
574 fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest),
575 &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest), &cbReturned, NULL);
576 displayChangeRequest.display = 0;
577 }
578
579 if (fDisplayChangeQueried)
580 {
581 /* Try to set the requested video mode. Repeat until it is successful or is rejected by the driver. */
582 for (;;)
583 {
584 Log(("VBoxTray: VBoxDisplayThread: VMMDevReq_GetDisplayChangeRequest2: %dx%dx%d at %d\n", displayChangeRequest.xres, displayChangeRequest.yres, displayChangeRequest.bpp, displayChangeRequest.display));
585
586 /*
587 * Only try to change video mode if the active display driver is VBox additions.
588 */
589#ifdef VBOX_WITH_WDDM
590 VBOXDISPLAY_DRIVER_TYPE enmDriverType = getVBoxDisplayDriverType (pCtx);
591
592 if (enmDriverType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
593 Log(("VBoxTray: VBoxDisplayThread: Detected WDDM Driver\n"));
594
595 if (enmDriverType != VBOXDISPLAY_DRIVER_TYPE_UNKNOWN)
596#else
597 if (isVBoxDisplayDriverActive (pCtx))
598#endif
599 {
600 Log(("VBoxTray: VBoxDisplayThread: Display driver is active!\n"));
601
602 if (pCtx->pfnChangeDisplaySettingsEx != 0)
603 {
604 Log(("VBoxTray: VBoxDisplayThread: Detected W2K or later\n"));
605
606 /* W2K or later. */
607 if (!ResizeDisplayDevice(displayChangeRequest.display,
608 displayChangeRequest.xres,
609 displayChangeRequest.yres,
610 displayChangeRequest.bpp,
611 pCtx
612 ))
613 {
614 break;
615 }
616 }
617 else
618 {
619 Log(("VBoxTray: VBoxDisplayThread: Detected NT\n"));
620
621 /* Single monitor NT. */
622 DEVMODE devMode;
623 RT_ZERO(devMode);
624 devMode.dmSize = sizeof(DEVMODE);
625
626 /* get the current screen setup */
627 if (EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &devMode))
628 {
629 Log(("VBoxTray: VBoxDisplayThread: Current mode: %d x %d x %d at %d,%d\n",
630 devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmPosition.x, devMode.dmPosition.y));
631
632 /* Check whether a mode reset or a change is requested. */
633 if (displayChangeRequest.xres || displayChangeRequest.yres || displayChangeRequest.bpp)
634 {
635 /* A change is requested.
636 * Set values which are not to be changed to the current values.
637 */
638 if (!displayChangeRequest.xres)
639 displayChangeRequest.xres = devMode.dmPelsWidth;
640 if (!displayChangeRequest.yres)
641 displayChangeRequest.yres = devMode.dmPelsHeight;
642 if (!displayChangeRequest.bpp)
643 displayChangeRequest.bpp = devMode.dmBitsPerPel;
644 }
645 else
646 {
647 /* All zero values means a forced mode reset. Do nothing. */
648 Log(("VBoxTray: VBoxDisplayThread: Forced mode reset\n"));
649 }
650
651 /* Verify that the mode is indeed changed. */
652 if ( devMode.dmPelsWidth == displayChangeRequest.xres
653 && devMode.dmPelsHeight == displayChangeRequest.yres
654 && devMode.dmBitsPerPel == displayChangeRequest.bpp)
655 {
656 Log(("VBoxTray: VBoxDisplayThread: already at desired resolution\n"));
657 break;
658 }
659
660 // without this, Windows will not ask the miniport for its
661 // mode table but uses an internal cache instead
662 DEVMODE tempDevMode = {0};
663 tempDevMode.dmSize = sizeof(DEVMODE);
664 EnumDisplaySettings(NULL, 0xffffff, &tempDevMode);
665
666 /* adjust the values that are supposed to change */
667 if (displayChangeRequest.xres)
668 devMode.dmPelsWidth = displayChangeRequest.xres;
669 if (displayChangeRequest.yres)
670 devMode.dmPelsHeight = displayChangeRequest.yres;
671 if (displayChangeRequest.bpp)
672 devMode.dmBitsPerPel = displayChangeRequest.bpp;
673
674 Log(("VBoxTray: VBoxDisplayThread: setting new mode %d x %d, %d BPP\n",
675 devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel));
676
677 /* set the new mode */
678 LONG status = ChangeDisplaySettings(&devMode, CDS_UPDATEREGISTRY);
679 if (status != DISP_CHANGE_SUCCESSFUL)
680 {
681 Log(("VBoxTray: VBoxDisplayThread: error from ChangeDisplaySettings: %d\n", status));
682
683 if (status == DISP_CHANGE_BADMODE)
684 {
685 /* Our driver can not set the requested mode. Stop trying. */
686 break;
687 }
688 }
689 else
690 {
691 /* Successfully set new video mode. */
692 break;
693 }
694 }
695 else
696 {
697 Log(("VBoxTray: VBoxDisplayThread: error from EnumDisplaySettings: %d\n", GetLastError ()));
698 break;
699 }
700 }
701 }
702 else
703 {
704 Log(("VBoxTray: VBoxDisplayThread: vboxDisplayDriver is not active\n"));
705 }
706
707 /* Retry the change a bit later. */
708 /* are we supposed to stop? */
709 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 1000) == WAIT_OBJECT_0)
710 {
711 fTerminate = true;
712 break;
713 }
714 }
715 }
716 else
717 {
718 Log(("VBoxTray: VBoxDisplayThread: error from DeviceIoControl VBOXGUEST_IOCTL_VMMREQUEST\n"));
719 /* sleep a bit to not eat too much CPU while retrying */
720 /* are we supposed to stop? */
721 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 50) == WAIT_OBJECT_0)
722 {
723 fTerminate = true;
724 break;
725 }
726 }
727 }
728 if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED)
729 hlpReloadCursor();
730 } else
731 {
732 Log(("VBoxTray: VBoxDisplayThread: error 0 from DeviceIoControl VBOXGUEST_IOCTL_WAITEVENT\n"));
733 /* sleep a bit to not eat too much CPU in case the above call always fails */
734 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
735 {
736 fTerminate = true;
737 break;
738 }
739 }
740 } while (!fTerminate);
741
742 /*
743 * Remove event filter and graphics capability report.
744 */
745 maskInfo.u32OrMask = 0;
746 maskInfo.u32NotMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
747 if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
748 Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - not) failed\n"));
749 VbglR3SetGuestCaps(0, VMMDEV_GUEST_SUPPORTS_GRAPHICS);
750
751 Log(("VBoxTray: VBoxDisplayThread: finished display change request thread\n"));
752 return 0;
753}
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