Index: trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibEvent.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibMisc.cpp	(revision 27080)
+++ trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibEvent.cpp	(revision 27083)
@@ -1,9 +1,9 @@
 /* $Id$ */
 /** @file
- * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Misc.
+ * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Events.
  */
 
 /*
- * Copyright (C) 2007 Sun Microsystems, Inc.
+ * Copyright (C) 2007-2010 Sun Microsystems, Inc.
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -33,12 +33,7 @@
 *   Header Files                                                               *
 *******************************************************************************/
-#include <iprt/mem.h>
-#include <iprt/string.h>
 #include <VBox/log.h>
-#include <VBox/version.h>
 #include "VBGLR3Internal.h"
 
-/** @todo Split this file up so we can drop most of the VBOX_VBGLR3_XFREE86
- *        #ifdef'ing. */
 
 /**
@@ -104,420 +99,2 @@
 }
 
-
-/**
- * Write to the backdoor logger from ring 3 guest code.
- *
- * @returns IPRT status code.
- *
- * @param   pch     The string to log.  Does not need to be terminated.
- * @param   cb      The number of byte to log.
- * @todo    cb -> cch.
- *
- * @remarks This currently does not accept more than 255 bytes of data at
- *          one time. It should probably be rewritten to use pass a pointer
- *          in the IOCtl.
- */
-VBGLR3DECL(int) VbglR3WriteLog(const char *pch, size_t cb)
-{
-    /*
-     * Quietly skip NULL strings.
-     * (Happens in the RTLogBackdoorPrintf case.)
-     */
-    if (!cb)
-        return VINF_SUCCESS;
-    if (!VALID_PTR(pch))
-        return VERR_INVALID_POINTER;
-
-#ifdef RT_OS_WINDOWS
-    /*
-     * Duplicate the string as it may be read only (a C string).
-     */
-    void *pvTmp = RTMemDup(pch, cb);
-    if (!pvTmp)
-        return VERR_NO_MEMORY;
-    int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_LOG(cb), pvTmp, cb);
-    RTMemFree(pvTmp);
-    return rc;
-
-#elif 0 /** @todo Several OSes could take this route (solaris and freebsd for instance). */
-    /*
-     * Handle the entire request in one go.
-     */
-    return vbglR3DoIOCtl(VBOXGUEST_IOCTL_LOG(cb), pvTmp, cb);
-
-#else
-    /*
-     * *BSD does not accept more than 4KB per ioctl request, while
-     * Linux can't express sizes above 8KB, so, split it up into 2KB chunks.
-     */
-# define STEP 2048
-    int rc = VINF_SUCCESS;
-    for (size_t off = 0; off < cb && RT_SUCCESS(rc); off += STEP)
-    {
-        size_t cbStep = RT_MIN(cb - off, STEP);
-        rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_LOG(cbStep), (char *)pch + off, cbStep);
-    }
-# undef STEP
-    return rc;
-#endif
-}
-
-
-/**
- * Change the IRQ filter mask.
- *
- * @returns IPRT status code.
- * @param   fOr     The OR mask.
- * @param   fNo     The NOT mask.
- */
-VBGLR3DECL(int) VbglR3CtlFilterMask(uint32_t fOr, uint32_t fNot)
-{
-    VBoxGuestFilterMaskInfo Info;
-    Info.u32OrMask = fOr;
-    Info.u32NotMask = fNot;
-    return vbglR3DoIOCtl(VBOXGUEST_IOCTL_CTL_FILTER_MASK, &Info, sizeof(Info));
-}
-
-
-/**
- * Report a change in the capabilities that we support to the host.
- *
- * @returns IPRT status code.
- * @param   fOr     Capabilities which have been added.
- * @param   fNot    Capabilities which have been removed.
- *
- * @todo    Move to a different file.
- */
-VBGLR3DECL(int) VbglR3SetGuestCaps(uint32_t fOr, uint32_t fNot)
-{
-    VMMDevReqGuestCapabilities2 Req;
-
-    vmmdevInitRequest(&Req.header, VMMDevReq_SetGuestCapabilities);
-    Req.u32OrMask = fOr;
-    Req.u32NotMask = fNot;
-    int rc = vbglR3GRPerform(&Req.header);
-#if defined(DEBUG) && !defined(VBOX_VBGLR3_XFREE86)
-    if (RT_SUCCESS(rc))
-        LogRel(("Successfully changed guest capabilities: or mask 0x%x, not mask 0x%x.\n", fOr, fNot));
-    else
-        LogRel(("Failed to change guest capabilities: or mask 0x%x, not mask 0x%x.  rc=%Rrc.\n", fOr, fNot, rc));
-#endif
-    return rc;
-}
-
-#ifndef VBOX_VBGLR3_XFREE86
-
-/**
- * Query the current statistics update interval.
- *
- * @returns IPRT status code.
- * @param   pcMsInterval    Update interval in ms (out).
- */
-VBGLR3DECL(int) VbglR3StatQueryInterval(PRTMSINTERVAL pcMsInterval)
-{
-    VMMDevGetStatisticsChangeRequest Req;
-
-    vmmdevInitRequest(&Req.header, VMMDevReq_GetStatisticsChangeRequest);
-    Req.eventAck = VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST;
-    Req.u32StatInterval = 1;
-    int rc = vbglR3GRPerform(&Req.header);
-    if (RT_SUCCESS(rc))
-    {
-        *pcMsInterval = Req.u32StatInterval * 1000;
-        if (*pcMsInterval / 1000 != Req.u32StatInterval)
-            *pcMsInterval = ~(RTMSINTERVAL)0;
-    }
-    return rc;
-}
-
-
-/**
- * Report guest statistics.
- *
- * @returns IPRT status code.
- * @param   pReq        Request packet with statistics.
- */
-VBGLR3DECL(int) VbglR3StatReport(VMMDevReportGuestStats *pReq)
-{
-    vmmdevInitRequest(&pReq->header, VMMDevReq_ReportGuestStats);
-    return vbglR3GRPerform(&pReq->header);
-}
-
-
-/**
- * Refresh the memory balloon after a change.
- *
- * @returns IPRT status code.
- * @param   pcChunks        The size of the balloon in chunks of 1MB (out).
- * @param   pfHandleInR3    Allocating of memory in R3 required (out).
- */
-VBGLR3DECL(int) VbglR3MemBalloonRefresh(uint32_t *pcChunks, bool *pfHandleInR3)
-{
-    VBoxGuestCheckBalloonInfo Info;
-    int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_CHECK_BALLOON, &Info, sizeof(Info));
-    if (RT_SUCCESS(rc))
-    {
-        *pcChunks = Info.cBalloonChunks;
-        Assert(Info.fHandleInR3 == false || Info.fHandleInR3 == true ||  RT_FAILURE(rc));
-        *pfHandleInR3 = Info.fHandleInR3 != false;
-    }
-    return rc;
-}
-
-
-/**
- * Change the memory by granting/reclaiming memory to/from R0.
- *
- * @returns IPRT status code.
- * @param   pv          Memory chunk (1MB).
- * @param   fInflate    true = inflate balloon (grant memory).
- *                      false = deflate balloon (reclaim memory).
- */
-VBGLR3DECL(int) VbglR3MemBalloonChange(void *pv, bool fInflate)
-{
-    VBoxGuestChangeBalloonInfo Info;
-    Info.u64ChunkAddr = (uint64_t)((uintptr_t)pv);
-    Info.fInflate = fInflate;
-    return vbglR3DoIOCtl(VBOXGUEST_IOCTL_CHANGE_BALLOON, &Info, sizeof(Info));
-}
-
-
-/**
- * Fallback for vbglR3GetAdditionsVersion.
- */
-static int vbglR3GetAdditionsCompileTimeVersion(char **ppszVer, char **ppszRev)
-{
-    if (ppszVer)
-    {
-        *ppszVer = RTStrDup(VBOX_VERSION_STRING);
-        if (!*ppszVer)
-            return VERR_NO_STR_MEMORY;
-    }
-
-    if (ppszRev)
-    {
-        char szRev[64];
-        RTStrPrintf(szRev, sizeof(szRev), "%d", VBOX_SVN_REV);
-        *ppszRev = RTStrDup(szRev);
-        if (!*ppszRev)
-        {
-            if (ppszVer)
-            {
-                RTStrFree(*ppszVer);
-                *ppszVer = NULL;
-            }
-            return VERR_NO_STR_MEMORY;
-        }
-    }
-
-    return VINF_SUCCESS;
-}
-
-#ifdef RT_OS_WINDOWS
-/**
- * Looks up the storage path handle (registry).
- *
- * @returns IPRT status value
- * @param   hKey        Receives pointer of allocated version string. NULL is
- *                      accepted. The returned pointer must be closed by
- *                      vbglR3CloseAdditionsWinStoragePath().
- */
-static int vbglR3GetAdditionsWinStoragePath(PHKEY phKey)
-{
-    /*
-     * Try get the *installed* version first.
-     */
-    LONG r;
-
-    /* Check the new path first. */
-    r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
-# ifdef RT_ARCH_AMD64
-    if (r != ERROR_SUCCESS)
-    {
-        /* Check Wow6432Node (for new entries). */
-        r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
-    }
-# endif
-
-    /* Still no luck? Then try the old xVM paths ... */
-    if (r != ERROR_SUCCESS)
-    {
-        r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
-# ifdef RT_ARCH_AMD64
-        if (r != ERROR_SUCCESS)
-        {
-            /* Check Wow6432Node (for new entries). */
-            r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
-        }
-# endif
-    }
-    return RTErrConvertFromWin32(r);
-}
-
-
-/**
- * Closes the storage path handle (registry).
- *
- * @returns IPRT status value
- * @param   hKey        Handle to close, retrieved by
- *                      vbglR3GetAdditionsWinStoragePath().
- */
-static int vbglR3CloseAdditionsWinStoragePath(HKEY hKey)
-{
-    return RTErrConvertFromWin32(RegCloseKey(hKey));
-}
-
-#endif /* RT_OS_WINDOWS */
-
-/**
- * Retrieves the installed Guest Additions version and/or revision.
- *
- * @returns IPRT status value
- * @param   ppszVer     Receives pointer of allocated version string. NULL is
- *                      accepted. The returned pointer must be freed using
- *                      RTStrFree().
- * @param   ppszRev     Receives pointer of allocated revision string. NULL is
- *                      accepted. The returned pointer must be freed using
- *                      RTStrFree().
- */
-VBGLR3DECL(int) VbglR3GetAdditionsVersion(char **ppszVer, char **ppszRev)
-{
-#ifdef RT_OS_WINDOWS
-    HKEY hKey;
-    int rc = vbglR3GetAdditionsWinStoragePath(&hKey);
-    if (RT_SUCCESS(rc))
-    {
-        /* Version. */
-        LONG l;
-        DWORD dwType;
-        DWORD dwSize = 32;
-        char *pszTmp;
-        if (ppszVer)
-        {
-            pszTmp = (char*)RTMemAlloc(dwSize);
-            if (pszTmp)
-            {
-                l = RegQueryValueEx(hKey, "Version", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
-                if (l == ERROR_SUCCESS)
-                {
-                    if (dwType == REG_SZ)
-                        rc = RTStrDupEx(ppszVer, pszTmp);
-                    else
-                        rc = VERR_INVALID_PARAMETER;
-                }
-                else
-                {
-                    rc = RTErrConvertFromWin32(l);
-                }
-                RTMemFree(pszTmp);
-            }
-            else
-                rc = VERR_NO_MEMORY;
-        }
-        /* Revision. */
-        if (ppszRev)
-        {
-            dwSize = 32; /* Reset */
-            pszTmp = (char*)RTMemAlloc(dwSize);
-            if (pszTmp)
-            {
-                l = RegQueryValueEx(hKey, "Revision", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
-                if (l == ERROR_SUCCESS)
-                {
-                    if (dwType == REG_SZ)
-                        rc = RTStrDupEx(ppszRev, pszTmp);
-                    else
-                        rc = VERR_INVALID_PARAMETER;
-                }
-                else
-                {
-                    rc = RTErrConvertFromWin32(l);
-                }
-                RTMemFree(pszTmp);
-            }
-            else
-                rc = VERR_NO_MEMORY;
-
-            if (RT_FAILURE(rc) && ppszVer)
-            {
-                RTStrFree(*ppszVer);
-                *ppszVer = NULL;
-            }
-        }
-        rc = vbglR3CloseAdditionsWinStoragePath(hKey);
-    }
-    else
-    {
-        /*
-         * No registry entries found, return the version string compiled
-         * into this binary.
-         */
-        rc = vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
-    }
-    return rc;
-
-#else  /* !RT_OS_WINDOWS */
-    /*
-     * On non-Windows platforms just return the compile-time version string.
-     */
-    return vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
-#endif /* !RT_OS_WINDOWS */
-}
-
-
-/**
- * Retrieves the installation path of Guest Additions.
- *
- * @returns IPRT status value
- * @param   ppszPath    Receives pointer of allocated installation path string.
- *                      The returned pointer must be freed using
- *                      RTStrFree().
- */
-VBGLR3DECL(int) VbglR3GetAdditionsInstallationPath(char **ppszPath)
-{
-    int rc;
-#ifdef RT_OS_WINDOWS
-    HKEY hKey;
-    rc = vbglR3GetAdditionsWinStoragePath(&hKey);
-    if (RT_SUCCESS(rc))
-    {
-        /* Installation directory. */
-        DWORD dwType;
-        DWORD dwSize = _MAX_PATH * sizeof(char);
-        char *pszTmp = (char*)RTMemAlloc(dwSize + 1);
-        if (pszTmp)
-        {
-            LONG l = RegQueryValueEx(hKey, "InstallDir", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
-            if ((l != ERROR_SUCCESS) && (l != ERROR_FILE_NOT_FOUND))
-            {
-                rc = RTErrConvertFromNtStatus(l);
-            }
-            else
-            {
-                if (dwType == REG_SZ)
-                    rc = RTStrDupEx(ppszPath, pszTmp);
-                else
-                    rc = VERR_INVALID_PARAMETER;
-                if (RT_SUCCESS(rc))
-                {
-                    /* Flip slashes. */
-                    for (char *pszTmp2 = ppszPath[0]; *pszTmp2; ++pszTmp2)
-                        if (*pszTmp2 == '\\')
-                            *pszTmp2 = '/';
-                }
-            }
-            RTMemFree(pszTmp);
-        }
-        else
-            rc = VERR_NO_MEMORY;
-        rc = vbglR3CloseAdditionsWinStoragePath(hKey);
-    }
-#else
-    /** @todo implement me */
-    rc = VERR_NOT_IMPLEMENTED;
-#endif
-    return rc;
-}
-
-#endif /* !VBOX_VBGLR3_XFREE86 */
-
