VirtualBox

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

Last change on this file since 51556 was 51469, checked in by vboxsync, 11 years ago

VBoxTray: Logging; ripped out all custom logging.

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