VBoxService.cpp revision 897aeea81122648c298d7d059f8186ae6d6127ce
/** @file
* VBoxService - Guest Additions Service
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*
*/
#include "VBoxService.h"
#include "VBoxSeamless.h"
#include <VBoxHook.h>
#include "resource.h"
#include <malloc.h>
#include "helpers.h"
/* global variables */
/* prototypes */
#ifdef DEBUG
/**
* Helper function to send a message to WinDbg
*
* @param String message string
*/
{
return;
}
#endif
/* The shared clipboard service prototypes. */
/* The service table. */
static VBOXSERVICEINFO vboxServiceTable[] =
{
{
"Shared Clipboard",
},
{
"Seamless Windows",
},
{
}
};
{
if (!pEnv->hStopEvent)
{
/* Could not create event. */
return VERR_NOT_SUPPORTED;
}
{
int rc = VINF_SUCCESS;
bool fStartThread = false;
{
}
if (VBOX_FAILURE (rc))
{
}
else
{
{
unsigned threadid;
0, /* stacksize */
0, /* initflag */
&threadid);
{
}
}
if (VBOX_FAILURE (rc))
{
dprintf(("Failed to start the thread.\n"));
if (pTable->pfnDestroy)
{
}
}
else
{
}
}
/* Advance to next table element. */
pTable++;
}
return VINF_SUCCESS;
}
{
if (!pEnv->hStopEvent)
{
return;
}
/* Signal to all threads. */
{
{
{
/* There is a thread, wait for termination. */
}
if (pTable->pfnDestroy)
{
}
}
/* Advance to next table element. */
pTable++;
}
}
void WINAPI VBoxServiceStart(void)
{
dprintf(("VBoxService: Start\n"));
/* open VBox guest driver */
NULL,
NULL);
if (gVBoxDriver == INVALID_HANDLE_VALUE)
{
}
{
/* create a custom window class */
WNDCLASS windowClass = {0};
if (!RegisterClass(&windowClass))
status = GetLastError();
}
{
/* create our window */
"VirtualBoxTool", "VirtualBoxTool",
if (!gToolWindow)
status = GetLastError();
else
{
/* move the window beneach the mouse pointer so that we get access to it */
/* change the mouse pointer so that we can go for a hardware shape */
/* move back our tool window */
}
}
{
{
return;
}
}
/*
* Start services listed in the vboxServiceTable.
*/
{
if (VBOX_FAILURE (rc))
{
}
}
/* create display change thread */
{
if ((int)hDisplayChangeThread == -1L)
}
/* terminate service if something went wrong */
{
return;
}
BOOL fTrayIconCreated = false;
/* prepare the system tray icon */
sprintf(ndata.szTip, "innotek VirtualBox Guest Additions %d.%d.%d", VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD);
/*
* Main execution loop
* Wait for the stop semaphore to be posted or a window event to arrive
*/
while(true)
{
if (waitResult == WAIT_OBJECT_0)
{
dprintf(("VBoxService: exit\n"));
/* exit */
break;
}
{
/* a window message, handle it */
{
{
dprintf(("VBoxService: WM_QUIT!\n"));
continue;
}
}
}
else /* timeout */
{
#ifndef DEBUG_sandervl
dprintf(("VBoxService: timed out\n"));
#endif
/* we might have to repeat this operation because the shell might not be loaded yet */
if (!fTrayIconCreated)
{
}
}
}
dprintf(("VBoxService: returned from main loop, exiting...\n"));
/* remove the system tray icon */
dprintf(("VBoxService: waiting for display change thread...\n"));
/* wait for the display change thread to terminate */
dprintf(("VBoxService: destroying tool window...\n"));
/* destroy the tool window */
dprintf(("VBoxService: leaving service main function\n"));
return;
}
/**
* Main function
*/
{
dprintf(("VBoxService: WinMain\n"));
VBoxServiceStart ();
return 0;
}
static bool isVBoxDisplayDriverActive (void)
{
bool result = false;
while (EnumDisplayDevices(NULL,
0))
{
DDCLOG(("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],
{
DDCLOG(("Primary device.\n"));
{
DDCLOG(("VBox display driver is active.\n"));
result = true;
}
break;
}
devNum++;
}
return result;
}
/* ChangeDisplaySettingsEx does not exist in NT. ResizeDisplayDevice uses the function. */
typedef LONG WINAPI defChangeDisplaySettingsEx(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam);
/* 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;
}
DDCLOG(("%dx%d at %d,%d\n",
DevNum++;
}
i++;
}
if (Width == 0)
{
}
if (Height == 0)
{
}
/* Check whether a mode reset or a change is requested. */
if ( !fModeReset
{
DDCLOG(("VBoxService: already at desired resolution.\n"));
return FALSE;
}
#ifdef DDCLOG
for (i = 0; i < NumDevices; i++)
{
DDCLOG(("[%d]: %d,%d %dx%d\n",
}
#endif /* DDCLOG */
/* 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;
if (hUser)
{
pChangeDisplaySettingsEx = (defChangeDisplaySettingsEx *)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
}
maskInfo.u32NotMask = 0;
if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
{
DDCLOG(("VBoxService: DeviceIOControl(CtlMask - or) succeeded\n"));
}
else
{
dprintf(("VBoxService: DeviceIOControl(CtlMask) failed, DisplayChangeThread exited\n"));
return;
}
do
{
/* wait for a display change event */
if (DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
{
DDCLOG(("VBoxService: DeviceIOControl succeded\n"));
/* are we supposed to stop? */
break;
DDCLOG(("VBoxService: checking event\n"));
/* did we get the right event? */
{
DDCLOG(("VBoxService: 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),
}
{
DDCLOG(("VBoxService: 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 ())
{
if (pChangeDisplaySettingsEx != 0)
{
/* W2K or later. */
{
break;
}
}
else
{
/* Single monitor NT. */
/* get the current screen setup */
{
dprintf(("VBoxService: 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. */
}
/* Verify that the mode is indeed changed. */
{
dprintf(("VBoxService: 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)
DDCLOG(("VBoxService: 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(("VBoxService: vboxDisplayDriver is not active.\n"));
}
/* Retry the change a bit later. */
/* are we supposed to stop? */
{
fTerminate = true;
break;
}
}
else
{
dprintf(("VBoxService: 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(("VBoxService: 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))
{
DDCLOG(("VBoxService: DeviceIOControl(CtlMask - not) succeeded\n"));
}
else
{
dprintf(("VBoxService: DeviceIOControl(CtlMask) failed\n"));
}
dprintf(("VBoxService: finished display change request thread\n"));
}
/**
* Window procedure for our tool window
*/
{
switch (msg)
{
case WM_CLOSE:
break;
case WM_DESTROY:
break;
break;
break;
case WM_VBOX_SEAMLESS_UPDATE:
break;
default:
}
return 0;
}