VBoxService.cpp revision 3b64bbc0d9ea63f40c6019c060f43eca45061b28
/** @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 "resource.h"
// #define DEBUG_DISPLAY_CHANGE
/* global variables */
/* prototypes */
{
{
#ifdef DEBUG_DISPLAY_CHANGE
if (f)
{
fclose (f);
}
#endif /* DEBUG_DISPLAY_CHANGE */
}
}
#ifdef DEBUG_DISPLAY_CHANGE
#define DDCLOG(a) do { SvcDebugOut2 a; } while (0)
#else
#define DDCLOG(a) do {} while (0)
#endif /* DEBUG_DISPLAY_CHANGE */
/**
* Helper function to send a message to WinDbg
*
* @param String message string
* @param Status error code, will be inserted into String's placeholder
*/
{
}
/* The shared clipboard service prototypes. */
/* The service table. */
static VBOXSERVICEINFO vboxServiceTable[] =
{
{
"Shared Clipboard",
},
{
}
};
{
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))
{
SvcDebugOut2 ("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)
{
SvcDebugOut2("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)
{
SvcDebugOut2("VBoxService: exit\n");
/* exit */
break;
}
{
/* a window message, handle it */
{
{
SvcDebugOut2("VBoxService: WM_QUIT!\n");
continue;
}
}
}
else /* timeout */
{
#ifndef DEBUG_sandervl
SvcDebugOut2("VBoxService: timed out\n");
#endif
/* we might have to repeat this operation because the shell might not be loaded yet */
if (!fTrayIconCreated)
{
}
}
}
SvcDebugOut("VBoxService: returned from main loop, exiting...\n", 0);
/* remove the system tray icon */
SvcDebugOut("VBoxService: waiting for display change thread...\n", 0);
/* wait for the display change thread to terminate */
SvcDebugOut("VBoxService: destroying tool window...\n", 0);
/* destroy the tool window */
SvcDebugOut("VBoxService: leaving service main function\n", 0);
return;
}
/**
* Main function
*/
{
SvcDebugOut2("VBoxService: WinMain\n");
VBoxServiceStart ();
}
{
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;
}
{
#ifdef DEBUG_DISPLAY_CHANGE
while (EnumDisplayDevices(DeviceName,
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],
devNum++;
}
#endif /* DEBUG_DISPLAY_CHANGE */
DDCLOG(("VBoxService: vboxQueryMonitorName: DeviceName: %s, idx %d (sizeof (TCHAR) %d)\n", DeviceName, idx, sizeof (TCHAR)));
idx,
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],
result = &MonitorName[0];
}
return result;
}
/**
* Thread function to wait for and process display change
* requests
*/
{
bool fTerminate = false;
typedef LONG WINAPI defChangeDisplaySettingsEx(LPCTSTR lpszDeviceName, LPDEVMODE lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam);
typedef BOOL WINAPI defEnumDisplaySettingsEx(LPCTSTR lpszDeviceName, DWORD iModeNum, LPDEVMODE lpDevMode, DWORD dwFlags);
if (hUser)
{
pChangeDisplaySettingsEx = (defChangeDisplaySettingsEx *)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
pEnumDisplaySettingsEx = (defEnumDisplaySettingsEx *)GetProcAddress(hUser, "EnumDisplaySettingsExA");
DDCLOG(("VBoxService: pChangeDisplaySettingsEx = %p, pEnumDisplaySettingsEx = %p\n", pChangeDisplaySettingsEx, pEnumDisplaySettingsEx));
}
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
{
SvcDebugOut2("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));
/*
* Only try to change video mode if the active display driver is VBox additions.
*/
{
{
}
/* get the current screen setup, using Ex to get dmPosition if available. */
{
SvcDebugOut2("VBoxService: Current mode: %dx%dx%d at %d,%d\n", devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmPosition.x, devMode.dmPosition.y);
/* Horizontal resolution must be a multiple of 8, round down. */
/* 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. */
{
SvcDebugOut2("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
{
SvcDebugOut("VBoxService: error from EnumDisplaySettings\n", 0);
}
}
else
{
SvcDebugOut2("VBoxService: vboxDisplayDriver is not active.\n", 0);
}
/* Retry the change a bit later. */
/* are we supposed to stop? */
{
fTerminate = true;
break;
}
}
else
{
SvcDebugOut("VBoxService: error from DeviceIoControl IOCTL_VBOXGUEST_VMMREQUEST\n", 0);
/* sleep a bit to not eat too much CPU while retrying */
/* are we supposed to stop? */
{
fTerminate = true;
break;
}
}
}
}
} else
{
SvcDebugOut("VBoxService: error from DeviceIoControl IOCTL_VBOXGUEST_WAITEVENT\n", 0);
/* 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
{
SvcDebugOut2 ("VBoxService: DeviceIOControl(CtlMask) failed\n");
}
SvcDebugOut2("VBoxService: finished display change request thread\n");
}
/**
* Window procedure for our tool window
*/
{
switch (msg)
{
case WM_CLOSE:
break;
case WM_DESTROY:
break;
default:
}
return 0;
}