VBoxTray.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
/** @file
* VBoxTray - Guest Additions Tray Application
*/
/*
* Copyright (C) 2006-2009 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.
*/
#include "VBoxTray.h"
#include "VBoxSeamless.h"
#include "VBoxClipboard.h"
#include "VBoxDisplay.h"
#include "VBoxRestore.h"
#include "VBoxVRDP.h"
#include "VBoxHostVersion.h"
#include <VBoxHook.h>
#include "resource.h"
#include <malloc.h>
#include <VBoxGuestInternal.h>
#include "helpers.h"
#include <sddl.h>
#include <iprt/buildconfig.h>
/* global variables */
/* Prototypes */
/* The service table. */
static VBOXSERVICEINFO vboxServiceTable[] =
{
{
"Display",
},
{
"Shared Clipboard",
},
{
"Seamless Windows",
},
{
"Restore",
},
#endif
{
"VRDP",
},
{
}
};
{
Log(("VBoxTray: Starting services...\n"));
if (!pEnv->hStopEvent)
{
/* Could not create event. */
return VERR_NOT_SUPPORTED;
}
{
int rc = VINF_SUCCESS;
bool fStartThread = false;
{
}
if (RT_FAILURE (rc))
{
}
else
{
{
unsigned threadid;
0, /* stacksize */
0, /* initflag */
&threadid);
{
}
}
if (RT_FAILURE (rc))
{
Log(("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++;
}
}
/** Attempt to force Windows to reload the cursor image by attaching to the
* thread of the window currently under the mouse, hiding the cursor and
* showing it again. This could fail to work in any number of ways (no
* window under the cursor, the cursor has moved to a different window while
* we are processing), but we just accept this, as the cursor will be reloaded
* at some point anyway. */
void VBoxServiceReloadCursor(void)
{
LogFlowFunc(("\n"));
if (hWin)
{
if (hCurrentThread != hThread)
}
ShowCursor(false);
ShowCursor(true);
LogFlowFunc(("exiting\n"));
}
void WINAPI VBoxServiceStart(void)
{
Log(("VBoxTray: Leaving service main function"));
/* open VBox guest driver */
NULL,
NULL);
if (gVBoxDriver == INVALID_HANDLE_VALUE)
{
LogRel(("VBoxTray: Could not open VirtualBox Guest Additions driver! Please install / start it first! rc = %d\n", GetLastError()));
}
{
/* create a custom window class */
WNDCLASS windowClass = {0};
if (!RegisterClass(&windowClass))
status = GetLastError();
}
{
/* create our window */
"VirtualBoxTool", "VirtualBoxTool",
if (!gToolWindow)
status = GetLastError();
else
}
if (GetVersionEx(&info))
{
}
{
{
return;
}
/* We need to setup a security descriptor to allow other processes modify access to the seamless notification event semaphore */
if (!ret)
/* For Vista and up we need to change the integrity of the security descriptor too */
if (dwMajorVersion >= 6)
{
BOOL (WINAPI * pfnConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR *SecurityDescriptor, PULONG SecurityDescriptorSize);
if (hModule)
{
*(uintptr_t *)&pfnConvertStringSecurityDescriptorToSecurityDescriptorA = (uintptr_t)GetProcAddress(hModule, "ConvertStringSecurityDescriptorToSecurityDescriptorA");
Log(("VBoxTray: pfnConvertStringSecurityDescriptorToSecurityDescriptorA = %x\n", pfnConvertStringSecurityDescriptorToSecurityDescriptorA));
{
ret = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
if (!ret)
Log(("VBoxTray: ConvertStringSecurityDescriptorToSecurityDescriptorA failed with %d\n", GetLastError()));
if (!ret)
if (!ret)
}
}
}
{
if (ghSeamlessNotifyEvent == NULL)
{
return;
}
}
}
/*
* Start services listed in the vboxServiceTable.
*/
/* initializes disp-if to default (XPDM) mode */
#ifdef VBOXWDDM
/* for now the display mode will be adjusted to WDDM mode if needed
* on display service initialization when it detects the display driver type */
#endif
{
if (RT_FAILURE (rc))
{
}
}
/* terminate service if something went wrong */
{
return;
}
BOOL fTrayIconCreated = false;
/* prepare the system tray icon */
sprintf(ndata.szTip, "%s Guest Additions %d.%d.%dr%d", VBOX_PRODUCT, VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
/* Boost thread priority to make sure we wake up early for seamless window notifications (not sure if it actually makes any difference though) */
/*
* Main execution loop
* Wait for the stop semaphore to be posted or a window event to arrive
*/
if (0 == ghSeamlessNotifyEvent) /* If seamless mode is not active / supported, reduce event array count */
dwEventCount = 1;
while(true)
{
if (waitResult == 0)
{
Log(("VBoxTray: Event 'Exit' triggered.\n"));
/* exit */
break;
}
else if ((waitResult == 1) && (ghSeamlessNotifyEvent!=0)) /* Only jump in, if seamless is active! */
{
Log(("VBoxTray: Event 'Seamless' triggered.\n"));
/* seamless window notification */
}
else
{
/* timeout or a window message, handle it */
{
{
Log(("VBoxTray: WM_QUIT!\n"));
continue;
}
}
/* we might have to repeat this operation because the shell might not be loaded yet */
if (!fTrayIconCreated)
{
/* We're ready to create the tooltip balloon. */
{
/* Check in 10 seconds (@todo make seconds configurable) ... */
10000, /* 10 seconds */
NULL /* no timerproc */);
}
}
}
}
Log(("VBoxTray: Returned from main loop, exiting ...\n"));
/* remove the system tray icon and refresh system tray */
if (hTrayNotifyWnd)
Log(("VBoxTray: waiting for display change thread ...\n"));
Log(("VBoxTray: Destroying tool window ...\n"));
/* destroy the tool window */
Log(("VBoxTray: Leaving service main function\n"));
return;
}
/**
* Main function
*/
{
/* Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */
if ( (hMutexAppRunning != NULL)
&& (GetLastError() == ERROR_ALREADY_EXISTS))
{
/* Close the mutex for this application instance. */
return 0;
}
if (RT_FAILURE(rc))
return rc;
rc = VbglR3Init();
if (RT_FAILURE(rc))
return rc;
LogRel(("VBoxTray: Ended.\n"));
/* Release instance mutex. */
if (hMutexAppRunning != NULL) {
}
VbglR3Term();
return 0;
}
/**
* Window procedure for our tool window
*/
{
switch (msg)
{
case WM_CLOSE:
break;
case WM_DESTROY:
break;
case WM_TIMER:
switch (wParam)
{
if (RT_SUCCESS(VBoxCheckHostVersion()))
{
/* After successful run we don't need to check again. */
}
return 0;
default:
break;
}
break;
case WM_VBOX_TRAY:
switch (lParam)
{
case WM_LBUTTONDBLCLK:
break;
case WM_RBUTTONDOWN:
break;
}
break;
break;
break;
case WM_VBOX_SEAMLESS_UPDATE:
break;
case WM_VBOX_RESTORED:
break;
case WM_VBOX_CHECK_VRDP:
break;
default:
}
return 0;
}
/* display driver interface abstraction for XPDM & WDDM
* with WDDM we can not use ExtEscape to communicate with our driver
* because we do not have XPDM display driver any more, i.e. escape requests are handled by cdd
* that knows nothing about us */
{
return NO_ERROR;
}
{
return NO_ERROR;
}
{
if (iRet > 0)
return VINF_SUCCESS;
else if (iRet == 0)
return ERROR_NOT_SUPPORTED;
/* else */
return ERROR_GEN_FAILURE;
}
#ifdef VBOXWDDM
{
GetVersionEx (&OSinfo);
{
/* this is vista and up */
{
bool bSupported = true;
pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc = (PFND3DKMT_OPENADAPTERFROMHDC)GetProcAddress(hGdi32, "D3DKMTOpenAdapterFromHdc");
Log((__FUNCTION__"pfnD3DKMTOpenAdapterFromHdc = %p\n", pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc));
pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName = (PFND3DKMT_OPENADAPTERFROMGDIDISPLAYNAME)GetProcAddress(hGdi32, "D3DKMTOpenAdapterFromGdiDisplayName");
Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName = %p\n", pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName));
pIf->modeData.wddm.pfnD3DKMTCloseAdapter = (PFND3DKMT_CLOSEADAPTER)GetProcAddress(hGdi32, "D3DKMTCloseAdapter");
pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn = (PFND3DKMT_INVALIDATEACTIVEVIDPN)GetProcAddress(hGdi32, "D3DKMTInvalidateActiveVidPn");
Log((__FUNCTION__": pfnD3DKMTInvalidateActiveVidPn = %p\n", pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn));
if (!bSupported)
{
}
}
else
{
}
}
else
{
}
return err;
}
typedef DECLCALLBACK(BOOLEAN) FNVBOXDISPIFWDDM_ADAPTEROP(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, LPCWSTR pDevName, PVOID pContext);
static DWORD vboxDispIfWDDMAdapterOp(PCVBOXDISPIF pIf, LPCWSTR pDevName, PFNVBOXDISPIFWDDM_ADAPTEROP pfnOp, PVOID pContext)
{
wcsncpy(OpenAdapterData.DeviceName, pDevName, RT_ELEMENTS(OpenAdapterData.DeviceName) - 1 /* the last one is always \0 */);
if (!Status)
{
if (bCloseAdapter)
{
if (Status)
{
/* ignore */
Status = 0;
}
}
}
else
return err;
}
typedef struct
{
int cbData;
DECLCALLBACK(BOOLEAN) vboxDispIfEscapeWDDMOp(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, LPCWSTR pDevName, PVOID pContext)
{
D3DKMT_ESCAPE EscapeData = {0};
//EscapeData.hDevice = NULL;
//EscapeData.hContext = NULL;
return TRUE;
}
{
{
else
{
else
}
}
else
return err;
}
typedef struct
{
DECLCALLBACK(BOOLEAN) vboxDispIfResizeWDDMOp(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, LPCWSTR pDevName, PVOID pContext)
{
if (pData)
{
}
else
{
}
return TRUE;
}
static DWORD vboxDispIfResizeWDDM(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
{
{
else
{
else
}
}
else
return err;
}
#endif
{
{
case VBOXDISPIF_MODE_XPDM_NT4:
case VBOXDISPIF_MODE_XPDM:
#ifdef VBOXWDDM
case VBOXDISPIF_MODE_WDDM:
#endif
default:
return ERROR_INVALID_PARAMETER;
}
}
static DWORD vboxDispIfResizeXPDM(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
{
return ERROR_NOT_SUPPORTED;
}
DWORD VBoxDispIfResize(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
{
{
case VBOXDISPIF_MODE_XPDM_NT4:
return ERROR_NOT_SUPPORTED;
case VBOXDISPIF_MODE_XPDM:
#ifdef VBOXWDDM
case VBOXDISPIF_MODE_WDDM:
#endif
default:
return ERROR_INVALID_PARAMETER;
}
}
{
return NO_ERROR;
}
{
GetVersionEx (&OSinfo);
{
{
bool bSupported = true;
*(uintptr_t *)&pIf->modeData.xpdm.pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
Log((__FUNCTION__": pfnChangeDisplaySettingsEx = %p\n", pIf->modeData.xpdm.pfnChangeDisplaySettingsEx));
if (!bSupported)
{
}
}
else
{
}
}
else
{
}
return err;
}
{
/* @todo: may need to addd synchronization in case we want to change modes dynamically
* i.e. currently the mode is supposed to be initialized once on service initialization */
if (penmOldMode)
return VINF_ALREADY_INITIALIZED;
switch (enmMode)
{
case VBOXDISPIF_MODE_XPDM_NT4:
{
}
else
break;
case VBOXDISPIF_MODE_XPDM:
{
}
else
break;
#ifdef VBOXWDDM
case VBOXDISPIF_MODE_WDDM:
{
{
}
else
break;
}
#endif
default:
break;
}
return err;
}