VBoxDisplay.cpp revision f89791b8a3692c4368c020bbb9c9981209759e95
/** @file
*
* VBoxSeamless - Display notifications
*
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "VBoxTray.h"
#include "VBoxSeamless.h"
#include <VBoxHook.h>
#include <VBoxDisplay.h>
#include "helpers.h"
#include <malloc.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};
{
GetVersionEx (&OSinfo);
{
dprintf(("VBoxTray: Could not get module handle of USER32.DLL!\n"));
return VERR_NOT_IMPLEMENTED;
}
{
*(uintptr_t *)&gCtx.pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
*(uintptr_t *)&gCtx.pfnEnumDisplayDevices = (uintptr_t)GetProcAddress(hUser, "EnumDisplayDevicesA");
}
{
/* Nothing to do here yet */
}
else /* Unsupported platform */
{
dprintf(("VBoxTray: Warning, display for platform not handled yet!\n"));
return VERR_NOT_IMPLEMENTED;
}
dprintf(("VBoxTray: Display init successful.\n"));
*pfStartThread = true;
*ppInstance = (void *)&gCtx;
return VINF_SUCCESS;
}
{
return;
}
{
bool result = false;
if( pCtx->pfnEnumDisplayDevices )
{
dprintf(("Checking for active VBox display driver (W2K+)...\n"));
while (EnumDisplayDevices(NULL,
0))
{
dprintf(("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],
{
dprintf(("Primary device.\n"));
result = true;
break;
}
devNum++;
}
}
else /* This must be NT 4 or something really old, so don't use EnumDisplayDevices() here ... */
{
dprintf(("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 */
result = true;
}
return result;
}
/* Returns TRUE to try again. */
{
/* Find out how many display devices the system has */
DWORD NumDevices = 0;
DWORD i = 0;
{
{
NumDevices++;
}
{
NumDevices++;
}
i++;
}
{
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;
}
{
return FALSE;
}
dprintf(("%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
{
dprintf(("VBoxDisplayThread : already at desired resolution.\n"));
return FALSE;
}
#ifdef dprintf
for (i = 0; i < NumDevices; i++)
{
dprintf(("[%d]: %d,%d %dx%d\n",
}
#endif /* dprintf */
/* Without this, Windows will not ask the miniport for its
* mode table but uses an internal cache instead.
*/
/* Assign the new rectangles to displays. */
for (i = 0; i < NumDevices; i++)
{
if ( i == Id
&& BitsPerPixel != 0)
{
}
}
/* 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
*/
{
bool fTerminate = false;
maskInfo.u32NotMask = 0;
if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
{
dprintf(("VBoxDisplayThread : DeviceIOControl(CtlMask - or) succeeded\n"));
}
else
{
dprintf(("VBoxDisplayThread : DeviceIOControl(CtlMask) failed, DisplayChangeThread exited\n"));
return -1;
}
do
{
/* wait for a display change event */
if (DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
{
dprintf(("VBoxDisplayThread : DeviceIOControl succeded\n"));
dprintf(("VBoxDisplayThread : Invalid context detected!\n"));
break;
}
dprintf(("VBoxDisplayThread : Invalid context environment detected!\n"));
break;
}
/* are we supposed to stop? */
break;
dprintf(("VBoxDisplayThread : checking event\n"));
/* did we get the right event? */
{
dprintf(("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, IOCTL_VBOXGUEST_VMMREQUEST, &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest2),
if (!fDisplayChangeQueried)
{
/* Try the old version of the request for old VBox hosts. */
fDisplayChangeQueried = DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_VMMREQUEST, &displayChangeRequest, sizeof(VMMDevDisplayChangeRequest),
}
{
dprintf(("VBoxDisplayThread : VMMDevReq_GetDisplayChangeRequest2: %dx%dx%d at %d\n", displayChangeRequest.xres, displayChangeRequest.yres, displayChangeRequest.bpp, displayChangeRequest.display));
/* Horizontal resolution must be a multiple of 8, round down. */
/*
* Only try to change video mode if the active display driver is VBox additions.
*/
if (isVBoxDisplayDriverActive (pCtx))
{
dprintf(("VBoxDisplayThread : Display driver is active!\n"));
if (pCtx->pfnChangeDisplaySettingsEx != 0)
{
dprintf(("VBoxDisplayThread : Detected W2K or later."));
/* W2K or later. */
{
break;
}
}
else
{
dprintf(("VBoxDisplayThread : Detected NT.\n"));
/* Single monitor NT. */
/* get the current screen setup */
{
dprintf(("VBoxDisplayThread : Current mode: %dx%dx%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. */
dprintf(("VBoxDisplayThread : Forced mode reset.\n"));
}
/* Verify that the mode is indeed changed. */
{
dprintf(("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)
dprintf(("VBoxDisplayThread : setting the new mode %dx%dx%d\n", devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel));
/* 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
{
dprintf(("VBoxDisplayThread : vboxDisplayDriver is not active.\n"));
}
/* Retry the change a bit later. */
/* are we supposed to stop? */
{
fTerminate = true;
break;
}
}
else
{
dprintf(("VBoxDisplayThread : error from DeviceIoControl IOCTL_VBOXGUEST_VMMREQUEST\n"));
/* sleep a bit to not eat too much CPU while retrying */
/* are we supposed to stop? */
{
fTerminate = true;
break;
}
}
}
}
} else
{
dprintf(("VBoxDisplayThread : error 0 from DeviceIoControl IOCTL_VBOXGUEST_WAITEVENT\n"));
/* sleep a bit to not eat too much CPU in case the above call always fails */
{
fTerminate = true;
break;
}
}
} while (!fTerminate);
if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
{
dprintf(("VBoxDisplayThread : DeviceIOControl(CtlMask - not) succeeded\n"));
}
else
{
dprintf(("VBoxDisplayThread : DeviceIOControl(CtlMask) failed\n"));
}
dprintf(("VBoxDisplayThread : finished display change request thread\n"));
return 0;
}