VBoxDisplay.cpp revision 20137491c8bb00fc94c60bd08729b7798a94d91a
/* $Id$ */
/** @file
* VBoxSeamless - Display notifications.
*/
/*
* Copyright (C) 2006-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#define _WIN32_WINNT 0x0500
#include "VBoxTray.h"
#include "VBoxHelpers.h"
#include "VBoxSeamless.h"
#include <VBoxHook.h>
#include <VBoxDisplay.h>
#include <malloc.h>
#include <VBoxGuestInternal.h>
#ifdef VBOX_WITH_WDDM
#endif
typedef struct _VBOXDISPLAYCONTEXT
{
const VBOXSERVICEENV *pEnv;
/* ChangeDisplaySettingsEx does not exist in NT. ResizeDisplayDevice uses the function. */
LONG (WINAPI * pfnChangeDisplaySettingsEx)(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam);
/* EnumDisplayDevices does not exist in NT. isVBoxDisplayDriverActive et al. are using these functions. */
BOOL (WINAPI * pfnEnumDisplayDevices)(IN LPCSTR lpDevice, IN DWORD iDevNum, OUT PDISPLAY_DEVICEA lpDisplayDevice, IN DWORD dwFlags);
static VBOXDISPLAYCONTEXT gCtx = {0};
#ifdef VBOX_WITH_WDDM
typedef enum
{
#endif
{
Log(("VBoxTray: VBoxDisplayInit ...\n"));
GetVersionEx (&OSinfo);
{
Log(("VBoxTray: VBoxDisplayInit: Could not get module handle of USER32.DLL!\n"));
return VERR_NOT_IMPLEMENTED;
}
{
*(uintptr_t *)&gCtx.pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
Log(("VBoxTray: VBoxDisplayInit: pfnChangeDisplaySettingsEx = %p\n", gCtx.pfnChangeDisplaySettingsEx));
*(uintptr_t *)&gCtx.pfnEnumDisplayDevices = (uintptr_t)GetProcAddress(hUser, "EnumDisplayDevicesA");
#ifdef VBOX_WITH_WDDM
{
/* this is vista and up, check if we need to switch the display driver if to WDDM mode */
Log(("VBoxTray: VBoxDisplayInit: this is Windows Vista and up\n"));
if (enmType == VBOXDISPLAY_DRIVER_TYPE_WDDM)
{
Log(("VBoxTray: VBoxDisplayInit: WDDM driver is installed, switching display driver if to WDDM mode\n"));
/* this is hacky, but the most easiest way */
DWORD err = VBoxDispIfSwitchMode(const_cast<PVBOXDISPIF>(&pEnv->dispIf), VBOXDISPIF_MODE_WDDM, NULL /* old mode, we don't care about it */);
Log(("VBoxTray: VBoxDisplayInit: DispIf switched to WDDM mode successfully\n"));
else
}
}
#endif
}
{
/* Nothing to do here yet */
}
else /* Unsupported platform */
{
Log(("VBoxTray: VBoxDisplayInit: Warning, display for platform not handled yet!\n"));
return VERR_NOT_IMPLEMENTED;
}
VBOXDISPIFESCAPE_ISANYX IsAnyX = {0};
else
Log(("VBoxTray: VBoxDisplayInit: Display init successful\n"));
*pfStartThread = true;
*ppInstance = (void *)&gCtx;
return VINF_SUCCESS;
}
{
return;
}
#ifdef VBOX_WITH_WDDM
#else
#endif
{
#ifdef VBOX_WITH_WDDM
#else
bool result = false;
#endif
if( pCtx->pfnEnumDisplayDevices )
{
Log(("VBoxTray: isVBoxDisplayDriverActive: Checking for active VBox display driver (W2K+) ...\n"));
while (EnumDisplayDevices(NULL,
0))
{
Log(("VBoxTray: isVBoxDisplayDriverActive: DevNum:%d\nName:%s\nString:%s\nID:%s\nKey:%s\nFlags=%08X\n\n",
&dispDevice.DeviceName[0],
&dispDevice.DeviceString[0],
&dispDevice.DeviceID[0],
&dispDevice.DeviceKey[0],
{
Log(("VBoxTray: isVBoxDisplayDriverActive: Primary device\n"));
#ifndef VBOX_WITH_WDDM
result = true;
#else
/* WDDM driver can now have multiple incarnations,
* if the driver name contains VirtualBox, and does NOT match the XPDM name,
* assume it to be WDDM */
#endif
break;
}
devNum++;
}
}
else /* This must be NT 4 or something really old, so don't use EnumDisplayDevices() here ... */
{
Log(("VBoxTray: isVBoxDisplayDriverActive: Checking for active VBox display driver (NT or older) ...\n"));
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &tempDevMode); /* Get current display device settings */
/* Check for the short name, because all long stuff would be truncated */
#ifndef VBOX_WITH_WDDM
result = true;
#else
#endif
}
#ifndef VBOX_WITH_WDDM
return result;
#else
return enmType;
#endif
}
static DWORD EnableAndResizeDispDev(DEVMODE *paDeviceModes, DISPLAY_DEVICE *paDisplayDevices, DWORD totalDispNum, ULONG Id, DWORD aWidth, DWORD aHeight,
DWORD aBitsPerPixel, DWORD aPosX, DWORD aPosY, BOOL fEnabled, BOOL fExtDispSup, VBOXDISPLAYCONTEXT *pCtx)
{
{
{
LogRel(("VBoxTray:Initially disabling the monitor with id = %d . Total Monitor=%d\n", iter, totalDispNum));
}
}
if (fExtDispSup) /* Extended Display Support possible*/
{
if (fEnabled)
{
/* Special case for enabling the secondary monitor. */
{
LogRel(("VBoxTray: Secondary Monitor with ID=%d and name=%s Not Enabled. Enabling it.\n", Id, displayDevice.DeviceName));
deviceMode.dmPosition.y = 0;
GetVersionEx (&OSinfo);
/* dont any more flags here as, only DM_POISITON is used to enable the secondary display */
else /* for win 7 and above */
/* for vista and aboce DM_BITSPERPELis necessary */
dwStatus = gCtx.pfnChangeDisplaySettingsEx((LPSTR)displayDevice.DeviceName,&deviceMode, NULL, (CDS_UPDATEREGISTRY | CDS_NORESET), NULL);
/* A second call to ChangeDisplaySettings updates the monitor.*/
}
else /* secondary monitor already enabled. Request to change the resolution or position. */
{
{
LogRel(("VBoxTray: Display : %s , Change Height: %d & Width: %d\n", displayDevice.DeviceName, aWidth, aHeight));
}
{
}
/* A second call to ChangeDisplaySettings updates the monitor. */
}
}
else /* Request is there to disable the monitor with ID = Id*/
{
}
}
return dwStatus;
}
/* Returns TRUE to try again. */
{
Log(("VBoxTray: ResizeDisplayDevice Width= %d, Height=%d , PosX=%d and PosY=%d \
fEnabled = %d, fExtDisSup = %d\n",
Width &= 0xFFF8;
/* Find out how many display devices the system has */
DWORD NumDevices = 0;
DWORD i = 0;
{
{
NumDevices++;
}
{
NumDevices++;
}
i++;
}
Log(("VBoxTray: ResizeDisplayDevice: Found total %d devices. err %d\n", NumDevices, GetLastError ()));
{
Log(("VBoxTray: ResizeDisplayDevice: Requested identifier %d is invalid. err %d\n", Id, GetLastError ()));
return FALSE;
}
/* Fetch information about current devices and modes. */
DWORD DevPrimaryNum = 0;
i = 0;
{
{
bFetchDevice = TRUE;
}
{
bFetchDevice = TRUE;
}
if (bFetchDevice)
{
if (DevNum >= NumDevices)
{
return FALSE;
}
/* First try to get the video mode stored in registry (ENUM_REGISTRY_SETTINGS).
* A secondary display could be not active at the moment and would not have
* a current video mode (ENUM_CURRENT_SETTINGS).
*/
{
}
{
/* No ENUM_REGISTRY_SETTINGS yet. Seen on Vista after installation.
* Get the current video mode then.
*/
{
/* ENUM_CURRENT_SETTINGS returns FALSE when the display is not active:
* for example a disabled secondary display.
* Do not return here, ignore the error and set the display info to 0x0x0.
*/
Log(("VBoxTray: ResizeDisplayDevice: EnumDisplaySettings(ENUM_CURRENT_SETTINGS) error %d\n", GetLastError ()));
}
}
if (fExtDispSup)
{
LogRel(("VBoxTray: Extended Display Support.\n"));
Log(("VBoxTray: ResizeDisplayDevice1: %dx%dx%d at %d,%d . Id = %d and DevNum=%d, fEnabled=%d\n",
{
/* Calculation of new position for enabled
* secondary monitor .
*/
/* Used when a secondary monitor just needs to be enabled, without any
* change in its position
*/
if (dwNewPosX != 0)
{
fChangePosRequest = true;
}
}
else
{
}
}
else
{
LogRel(("VBoxTray: NO Ext Display Support \n"));
}
DevNum++;
}
i++;
}
/* Keep a record if the display with ID is already active or not. */
{
}
/* Width, height equal to 0 means that this value must be not changed.
* Update input parameters if necessary.
* Note: BitsPerPixel is taken into account later, when new rectangles
* are assigned to displays.
*/
if (Width == 0)
{
}
if (Height == 0)
{
}
/* Check whether a mode reset or a change is requested.
* Rectangle position is recalculated only if fEnabled is 1.
* For non extended supported modes (old Host VMs), fEnabled
* is always 1.
*/
/* Handled the case where previouseresolution of secondary monitor
* was for eg. 1024*768*32 and monitor was in disabled state.
* User gives the command
* setvideomode 1024 768 32 1 yes.
* Now in this case the resolution request is same as previous one but
* monitor is going from disabled to enabled state so the below condition
* shour return false
* The below condition will only return true , if no mode reset has
* been requested AND fEnabled is 1 and fDispAlreadyEnabled is also 1 AND
* all rect conditions are true. Thus in this case nothing has to be done.
*/
{
LogRel(("VBoxTray: Already at desired resolution. No Change.\n"));
return FALSE;
}
#ifdef Log
for (i = 0; i < NumDevices; i++)
{
Log(("VBoxTray: ResizeDisplayDevice: [%d]: %d,%d %dx%d\n",
}
#endif /* Log */
#ifdef VBOX_WITH_WDDM
{
/* Assign the new rectangles to displays. */
for (i = 0; i < NumDevices; i++)
{
/* On Vista one must specify DM_BITSPERPEL.
* Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
*/
if (i == Id && BitsPerPixel != 0)
{
LogRel(("VBoxTray: (WDDM)Changing resolution and position. \n"));
/* Change dmBitsPerPel if requested. */
{
}
else
{
}
}
Log(("VBoxTray: (WDDM) ResizeDisplayDevice: pfnChangeDisplaySettingsEx %x: %dx%dx%d at %d,%d\n",
paDeviceModes[i].dmPosition.x,
paDeviceModes[i].dmPosition.y));
}
/* Reques to enable /disable the secondary Display Device. Won't take the resize request now.*/
{
GetVersionEx (&OSinfo);
/* for win 7 and above */
{
if(dwStatus != ERROR_SUCCESS)
{
/* Not going to retry for enabling or disabling of the secondary display device.*/
LogRel(("VBoxTray: (WDDM) Failed to enable the Display Device \n"));
}
}
else /* case: vista in wddm mode. SetDisplayConfig APIs etc is not avilable in this mode. */
{
/* use traditional approach of ChangeDisplaySettingEx to enable/disable secondary monitor for Vista WDDM mode.*/
dwStatus = EnableAndResizeDispDev(paDeviceModes, paDisplayDevices, DevNum, Id, Width, Height, BitsPerPixel,
if (dwStatus != DISP_CHANGE_SUCCESSFUL )
{
/* Successfully set new video mode or our driver can not set
* the requested mode. Stop trying.
*/
}
}
return FALSE; /* for enable disable not retrying */
}
else
{
/* Resize request. Secondary display device should be in an enabled state. */
LogRel(("VBoxTray: (WDDM) Request to resize the display \n"));
DWORD err = VBoxDispIfResizeModes(&pCtx->pEnv->dispIf, Id, paDisplayDevices, paDeviceModes, NumDevices);
{
LogRel(("VBoxTray: VBoxDisplayThread: (WDDM) VBoxDispIfResizeModes succeeded\n"));
else
return FALSE;
}
}
Log(("VBoxTray: ResizeDisplayDevice: (WDDM) RETRY requested\n"));
return TRUE;
}
#endif
/* Without this, Windows will not ask the miniport for its
* mode table but uses an internal cache instead.
*/
for (i = 0; i < NumDevices; i++)
{
}
/* Assign the new rectangles to displays. */
for (i = 0; i < NumDevices; i++)
{
/* On Vista one must specify DM_BITSPERPEL.
* Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
*/
if ( i == Id
&& BitsPerPixel != 0)
{
/* Change dmBitsPerPel if requested. */
}
Log(("VBoxTray: ResizeDisplayDevice: pfnChangeDisplaySettingsEx Current MonitorId=%d: %dx%dx%d at %d,%d\n",
i,
paDeviceModes[i].dmPosition.x,
paDeviceModes[i].dmPosition.y));
Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", status, GetLastError ()));
}
Log(("VBoxTray: Enable And Resize Device. Id = %d, Width=%d Height=%d, \
dwNewPosX = %d, dwNewPosY = %d fEnabled=%d & fExtDispSupport = %d \n",
dwStatus = EnableAndResizeDispDev(paDeviceModes, paDisplayDevices, DevNum, Id, Width, Height, BitsPerPixel,
{
/* Successfully set new video mode or our driver can not set
* the requested mode. Stop trying.
*/
return FALSE;
}
/* Retry the request. */
return TRUE;
}
/**
* Thread function to wait for and process display change
* requests
*/
{
Log(("VBoxTray: VBoxDisplayThread: Entered\n"));
bool fTerminate = false;
maskInfo.u32NotMask = 0;
if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
{
Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - or) failed, thread exiting\n"));
return 0;
}
if (RT_FAILURE(rc))
{
LogRel(("VBoxTray: VBoxDisplayThread: Failed to set the graphics capability with rc=%Rrc, thread exiting\n", rc));
return 0;
}
do
{
/* Wait for a display change event. */
waitEvent.u32EventMaskIn = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
{
/*Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl succeeded\n"));*/
Log(("VBoxTray: VBoxDisplayThread: Invalid context detected!\n"));
break;
}
Log(("VBoxTray: VBoxDisplayThread: Invalid context environment detected!\n"));
break;
}
/* are we supposed to stop? */
break;
/*Log(("VBoxTray: VBoxDisplayThread: checking event\n"));*/
/* did we get the right event? */
{
Log(("VBoxTray: VBoxDisplayThread: going to get display change information\n"));
/* We got at least one event. Read the requested resolution
* and try to set it until success. New events will not be seen
* but a new resolution will be read in this poll loop.
*/
/* Try if extended mode display information is available from the host. */
fExtDispSup = TRUE;
fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequestEx)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequestEx),
if (!fDisplayChangeQueried)
{
Log(("VBoxTray: Extended Display Not Supported. Trying VMMDevDisplayChangeRequest2\n"));
fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest2)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2),
}
if (!fDisplayChangeQueried)
{
Log(("VBoxTray: Extended Display Not Supported. Trying VMMDevDisplayChangeRequest\n"));
/* Try the old version of the request for old VBox hosts. */
fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest),
}
{
/* Try to set the requested video mode. Repeat until it is successful or is rejected by the driver. */
for (;;)
{
Log(("VBoxTray: VBoxDisplayThread: VMMDevReq_GetDisplayChangeRequest2: %dx%dx%d at %d\n", displayChangeRequest.xres, displayChangeRequest.yres, displayChangeRequest.bpp, displayChangeRequest.display));
/*
* Only try to change video mode if the active display driver is VBox additions.
*/
#ifdef VBOX_WITH_WDDM
Log(("VBoxTray: VBoxDisplayThread: Detected WDDM Driver\n"));
#else
if (isVBoxDisplayDriverActive (pCtx))
#endif
{
Log(("VBoxTray: VBoxDisplayThread: Display driver is active!\n"));
if (pCtx->pfnChangeDisplaySettingsEx != 0)
{
Log(("VBoxTray: VBoxDisplayThread: Detected W2K or later\n"));
/* W2K or later. */
Log(("DisplayChangeReqEx parameters aDisplay=%d x xRes=%d x yRes=%d x bpp=%d x SecondayMonEnb=%d x NewOriginX=%d x NewOriginY=%d x ChangeOrigin=%d\n",
pCtx,
))
{
Log(("ResizeDipspalyDevice return 0\n"));
break;
}
}
else
{
Log(("VBoxTray: VBoxDisplayThread: Detected NT\n"));
/* Single monitor NT. */
/* get the current screen setup */
{
Log(("VBoxTray: VBoxDisplayThread: Current mode: %d x %d x %d at %d,%d\n",
devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmPosition.x, devMode.dmPosition.y));
/* Check whether a mode reset or a change is requested. */
{
/* A change is requested.
* Set values which are not to be changed to the current values.
*/
if (!displayChangeRequest.xres)
if (!displayChangeRequest.yres)
if (!displayChangeRequest.bpp)
}
else
{
/* All zero values means a forced mode reset. Do nothing. */
Log(("VBoxTray: VBoxDisplayThread: Forced mode reset\n"));
}
/* Verify that the mode is indeed changed. */
{
Log(("VBoxTray: VBoxDisplayThread: already at desired resolution\n"));
break;
}
// without this, Windows will not ask the miniport for its
// mode table but uses an internal cache instead
DEVMODE tempDevMode = {0};
/* adjust the values that are supposed to change */
if (displayChangeRequest.xres)
if (displayChangeRequest.yres)
if (displayChangeRequest.bpp)
Log(("VBoxTray: VBoxDisplayThread: setting new mode %d x %d, %d BPP\n",
/* set the new mode */
if (status != DISP_CHANGE_SUCCESSFUL)
{
if (status == DISP_CHANGE_BADMODE)
{
/* Our driver can not set the requested mode. Stop trying. */
break;
}
}
else
{
/* Successfully set new video mode. */
break;
}
}
else
{
break;
}
}
}
else
{
Log(("VBoxTray: VBoxDisplayThread: vboxDisplayDriver is not active\n"));
}
/* Retry the change a bit later. */
/* are we supposed to stop? */
{
fTerminate = true;
break;
}
}
}
else
{
Log(("VBoxTray: VBoxDisplayThread: error from DeviceIoControl VBOXGUEST_IOCTL_VMMREQUEST\n"));
/* sleep a bit to not eat too much CPU while retrying */
/* are we supposed to stop? */
{
fTerminate = true;
break;
}
}
}
} else
{
Log(("VBoxTray: VBoxDisplayThread: error 0 from DeviceIoControl VBOXGUEST_IOCTL_WAITEVENT\n"));
/* sleep a bit to not eat too much CPU in case the above call always fails */
{
fTerminate = true;
break;
}
}
} while (!fTerminate);
/*
* Remove event filter and graphics capability report.
*/
maskInfo.u32NotMask = VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST | VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED;
if (!DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
Log(("VBoxTray: VBoxDisplayThread: DeviceIOControl(CtlMask - not) failed\n"));
Log(("VBoxTray: VBoxDisplayThread: finished display change request thread\n"));
return 0;
}