VBoxTray.cpp revision 4327fb0461a76d4c2adba6ce9f1ece4d93d816be
/* $Id$ */
/** @file
* VBoxTray - Guest Additions Tray Application
*/
/*
* Copyright (C) 2006-2011 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "VBoxTray.h"
#include "VBoxTrayMsg.h"
#include "VBoxHelpers.h"
#include "VBoxSeamless.h"
#include "VBoxClipboard.h"
#include "VBoxDisplay.h"
#include "VBoxRestore.h"
#include "VBoxVRDP.h"
#include "VBoxHostVersion.h"
#include "VBoxSharedFolders.h"
#include "VBoxIPC.h"
#include "VBoxLA.h"
#include "VBoxMMR.h"
#include <VBoxHook.h>
#include "resource.h"
#include <malloc.h>
#include <VBoxGuestInternal.h>
#include <sddl.h>
#include <iprt/buildconfig.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int vboxTrayCreateTrayIcon(void);
/* Global message handler prototypes. */
/*static int vboxTrayGlMsgShowBalloonMsg(WPARAM lParam, LPARAM wParam);*/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/* The service table. */
static VBOXSERVICEINFO vboxServiceTable[] =
{
{
"Display",
},
{
"Shared Clipboard",
},
{
"Seamless Windows",
},
{
"Restore",
},
#endif
{
"VRDP",
},
{
"IPC",
},
{
"Location Awareness",
},
#ifdef VBOX_WITH_MMR
{
"Multimedia Redirection",
},
#endif
{
}
};
/* The global message table. */
static VBOXGLOBALMESSAGE s_vboxGlobalMessageTable[] =
{
/* Windows specific stuff. */
{
"TaskbarCreated",
},
/* VBoxTray specific stuff. */
/** @todo Add new messages here! */
{
}
};
/**
* Gets called whenever the Windows main taskbar
* get (re-)created. Nice to install our tray icon.
*
* @return IPRT status code.
* @param wParam
* @param lParam
*/
{
return vboxTrayCreateTrayIcon();
}
static int vboxTrayCreateTrayIcon(void)
{
{
return RTErrConvertFromWin32(dwErr);
}
/* Prepare the system tray icon. */
int rc = VINF_SUCCESS;
{
}
if (hIcon)
return rc;
}
static void vboxTrayRemoveTrayIcon()
{
if (gNotifyIconData.cbSize > 0)
{
/* Remove the system tray icon and refresh system tray. */
if (hTrayWnd)
{
if (hTrayNotifyWnd)
}
}
}
{
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_SUCCESS(rc))
else
{
Log(("VBoxTray: Failed to start the thread\n"));
if (pTable->pfnDestroy)
}
}
/* 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++;
}
}
{
int rc = VINF_SUCCESS;
return rc;
&& RT_SUCCESS(rc))
{
/* Register global accessible window messages. */
{
}
/* Advance to next table element. */
pTable++;
}
return rc;
}
{
return false;
{
{
if (pTable->pfnHandler)
return true;
}
/* Advance to next table element. */
pTable++;
}
return false;
}
static int vboxTrayOpenBaseDriver(void)
{
/* Open VBox guest driver. */
NULL,
NULL);
if (ghVBoxDriver == INVALID_HANDLE_VALUE)
{
dwErr = GetLastError();
LogRel(("VBoxTray: Could not open VirtualBox Guest Additions driver! Please install / start it first! Error = %08X\n", dwErr));
}
return RTErrConvertFromWin32(dwErr);
}
static void vboxTrayCloseBaseDriver(void)
{
if (ghVBoxDriver)
{
ghVBoxDriver = NULL;
}
}
static void vboxTrayDestroyToolWindow(void)
{
if (ghwndToolWindow)
{
Log(("VBoxTray: Destroying tool window ...\n"));
/* Destroy the tool window. */
}
}
static int vboxTrayCreateToolWindow(void)
{
/* Create a custom window class. */
WNDCLASS windowClass = {0};
if (!RegisterClass(&windowClass))
{
dwErr = GetLastError();
}
else
{
/*
* Create our (invisible) tool window.
* Note: The window name ("VBoxTrayToolWnd") and class ("VBoxTrayToolWndClass") is
* needed for posting globally registered messages to VBoxTray and must not be
* changed! Otherwise things get broken!
*
*/
"VBoxTrayToolWndClass", "VBoxTrayToolWnd",
if (!ghwndToolWindow)
{
dwErr = GetLastError();
}
else
{
/* Reload the cursor(s). */
}
}
if (dwErr != ERROR_SUCCESS)
return RTErrConvertFromWin32(dwErr);
}
static int vboxTraySetupSeamless(void)
{
if (GetVersionEx(&info))
{
}
/* We need to setup a security descriptor to allow other processes modify access to the seamless notification event semaphore. */
if (!fRC)
{
dwErr = GetLastError();
}
else
{
/* For Vista and up we need to change the integrity of the security descriptor, too. */
if (gMajorVersion >= 6)
{
BOOL (WINAPI * pfnConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR *SecurityDescriptor, PULONG SecurityDescriptorSize);
if (!hModule)
{
dwErr = GetLastError();
}
else
{
*(uintptr_t *)&pfnConvertStringSecurityDescriptorToSecurityDescriptorA = (uintptr_t)GetProcAddress(hModule, "ConvertStringSecurityDescriptorToSecurityDescriptorA");
Log(("VBoxTray: pfnConvertStringSecurityDescriptorToSecurityDescriptorA = %x\n", pfnConvertStringSecurityDescriptorToSecurityDescriptorA));
{
fRC = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
if (!fRC)
{
dwErr = GetLastError();
Log(("VBoxTray: ConvertStringSecurityDescriptorToSecurityDescriptorA failed with last error = %08X\n", dwErr));
}
else
{
if (!fRC)
{
dwErr = GetLastError();
}
else
{
if (!fRC)
{
dwErr = GetLastError();
}
}
}
}
}
}
if ( dwErr == ERROR_SUCCESS
{
if (ghSeamlessNotifyEvent == NULL)
{
dwErr = GetLastError();
}
}
}
return RTErrConvertFromWin32(dwErr);
}
static void vboxTrayShutdownSeamless(void)
{
{
}
}
static int vboxTrayServiceMain(void)
{
int rc = VINF_SUCCESS;
Log(("VBoxTray: Entering vboxTrayServiceMain\n"));
{
}
else
{
/*
* Start services listed in the vboxServiceTable.
*/
/* Initializes disp-if to default (XPDM) mode. */
#ifdef VBOX_WITH_WDDM
/*
* 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
/* Finally start all the built-in services! */
if (RT_FAILURE(rc))
{
/* Terminate service if something went wrong. */
}
else
{
rc = vboxTrayCreateTrayIcon();
if ( RT_SUCCESS(rc)
{
/* We're ready to create the tooltip balloon.
Check in 10 seconds (@todo make seconds configurable) ... */
10 * 1000, /* 10 seconds */
NULL /* No timerproc */);
}
if (RT_SUCCESS(rc))
{
/* Do the Shared Folders auto-mounting stuff. */
if (RT_SUCCESS(rc))
{
/* Report the host that we're up and running! */
}
}
if (RT_SUCCESS(rc))
{
/* 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)
{
/* Only enable for message debugging, lots of traffic! */
//Log(("VBoxTray: Wait result = %ld\n", waitResult));
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;
}
}
}
}
Log(("VBoxTray: Returned from main loop, exiting ...\n"));
}
Log(("VBoxTray: Waiting for services to stop ...\n"));
} /* Services started */
} /* Stop event created */
return rc;
}
/**
* 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;
}
int rc = RTR3InitExeNoArguments(0);
if (RT_SUCCESS(rc))
{
rc = VbglR3Init();
if (RT_SUCCESS(rc))
rc = vboxTrayOpenBaseDriver();
}
if (RT_SUCCESS(rc))
{
/* Save instance handle. */
if (RT_SUCCESS(rc))
{
rc = vboxTraySetupSeamless();
if (RT_SUCCESS(rc))
{
Log(("VBoxTray: Init successful\n"));
rc = vboxTrayServiceMain();
if (RT_SUCCESS(rc))
}
}
if (RT_SUCCESS(rc))
}
if (RT_FAILURE(rc))
{
}
LogRel(("VBoxTray: Ended\n"));
/* Release instance mutex. */
if (hMutexAppRunning != NULL)
{
}
VbglR3Term();
}
/**
* Window procedure for our tool window
*/
{
switch (uMsg)
{
case WM_CREATE:
{
Log(("VBoxTray: Tool window created\n"));
if (RT_FAILURE(rc))
return 0;
}
case WM_CLOSE:
return 0;
case WM_DESTROY:
Log(("VBoxTray: Tool window destroyed\n"));
return 0;
case WM_TIMER:
switch (wParam)
{
if (RT_SUCCESS(VBoxCheckHostVersion()))
{
/* After successful run we don't need to check again. */
}
return 0;
default:
break;
}
break; /* Make sure other timers get processed the usual way! */
case WM_VBOXTRAY_TRAY_ICON:
switch (lParam)
{
case WM_LBUTTONDBLCLK:
break;
case WM_RBUTTONDOWN:
break;
}
return 0;
return 0;
return 0;
case WM_VBOX_SEAMLESS_UPDATE:
return 0;
case WM_VBOXTRAY_VM_RESTORED:
return 0;
case WM_VBOXTRAY_VRDP_CHECK:
return 0;
default:
/* Handle all globally registered window messages. */
{
return 0; /* We handled the message. @todo Add return value!*/
}
break; /* We did not handle the message, dispatch to DefWndProc. */
}
/* Only if message was *not* handled by our switch above, dispatch
* to DefWindowProc. */
}