VBoxDisplay.cpp revision 74804912a237136f06d0b49682a119577135902b
/* $Id$ */
/** @file
* VBoxSeamless - Display notifications.
*/
/*
* Copyright (C) 2006-2010 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 <windows.h>
#include "VBoxTray.h"
#include "VBoxHelpers.h"
#include "VBoxSeamless.h"
#include <VBoxHook.h>
#include <VBoxDisplay.h>
#include <malloc.h>
#include <VBoxGuestInternal.h>
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
{
VBOXDISPIFESCAPE escape = {0};
{
return false;
}
return true;
}
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;
}
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
else if (strcmp(&dispDevice.DeviceString[0], "VirtualBox Graphics Adapter (Microsoft Corporation - WDDM)") == 0)
#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
}
/* Returns TRUE to try again. */
{
/* 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).
*/
{
return FALSE;
}
{
/* 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 ()));
}
}
Log(("VBoxTray: ResizeDisplayDevice: %dx%dx%d at %d,%d\n",
DevNum++;
}
i++;
}
/* 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. */
if ( !fModeReset
{
Log(("VBoxTray: ResizeDisplayDevice: Already at desired resolution\n"));
return FALSE;
}
#ifdef Log
for (i = 0; i < NumDevices; i++)
{
Log(("VBoxTray: ResizeDisplayDevice: [%d]: %d,%d %dx%d\n",
}
#endif /* Log */
/* 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 %x: %dx%dx%d at %d,%d\n",
paDeviceModes[i].dmPosition.x,
paDeviceModes[i].dmPosition.y));
Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", status, GetLastError ()));
}
/* A second call to ChangeDisplaySettings updates the monitor. */
{
/* 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.
*/
for (;;)
{
/* get the display change request */
BOOL fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest2)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2),
if (!fDisplayChangeQueried)
{
/* Try the old version of the request for old VBox hosts. */
fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(VMMDevDisplayChangeRequest)), &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest),
}
{
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"));
#ifdef VBOX_WITH_WDDM
{
{
Log(("VBoxTray: VBoxDisplayThread: VBoxDispIfResize succeeded\n"));
break;
}
}
#endif
/* W2K or later. */
))
{
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;
}