VBoxRestore.cpp revision f89791b8a3692c4368c020bbb9c9981209759e95
d46ee884c41b808b239563b1978468aae12e33a2vboxsync/** @file
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync *
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync * VBoxRestore - Restore notification
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync *
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync */
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync/*
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync *
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync * available from http://www.virtualbox.org. This file is free software;
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync * you can redistribute it and/or modify it under the terms of the GNU
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync * General Public License (GPL) as published by the Free Software
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
d46ee884c41b808b239563b1978468aae12e33a2vboxsync *
d46ee884c41b808b239563b1978468aae12e33a2vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
d46ee884c41b808b239563b1978468aae12e33a2vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
01b2e367e947049139a5d5813ccc1fa162b11f76vboxsync * additional information or have any questions.
d46ee884c41b808b239563b1978468aae12e33a2vboxsync */
d46ee884c41b808b239563b1978468aae12e33a2vboxsync#define _WIN32_WINNT 0x0500
d46ee884c41b808b239563b1978468aae12e33a2vboxsync#include <windows.h>
d46ee884c41b808b239563b1978468aae12e33a2vboxsync#include "VBoxTray.h"
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync#include "VBoxRestore.h"
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync#include <VBoxDisplay.h>
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync#include <VBox/VBoxDev.h>
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync#include <VBoxGuestInternal.h>
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync#include <iprt/assert.h>
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync#include "helpers.h"
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsynctypedef struct _VBOXRESTORECONTEXT
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync{
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync const VBOXSERVICEENV *pEnv;
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync BOOL fRDPState;
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync} VBOXRESTORECONTEXT;
d46ee884c41b808b239563b1978468aae12e33a2vboxsync
d46ee884c41b808b239563b1978468aae12e33a2vboxsync
d46ee884c41b808b239563b1978468aae12e33a2vboxsyncstatic VBOXRESTORECONTEXT gCtx = {0};
d46ee884c41b808b239563b1978468aae12e33a2vboxsync
d46ee884c41b808b239563b1978468aae12e33a2vboxsync
d46ee884c41b808b239563b1978468aae12e33a2vboxsyncint VBoxRestoreInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
3b70c9ab3cea93ab1a771d2db319311282041382vboxsync{
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync dprintf(("VBoxRestoreInit\n"));
d46ee884c41b808b239563b1978468aae12e33a2vboxsync
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync gCtx.pEnv = pEnv;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync gCtx.fRDPState = FALSE;
d46ee884c41b808b239563b1978468aae12e33a2vboxsync
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync VBoxRestoreCheckVRDP();
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync
2a229554eb081e98411c81dcdef146c35a000f80vboxsync *pfStartThread = true;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync *ppInstance = &gCtx;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync return VINF_SUCCESS;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync}
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync
e33247bff4fddfdba92538374bcc9e2753044a38vboxsync
d46ee884c41b808b239563b1978468aae12e33a2vboxsyncvoid VBoxRestoreDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
d46ee884c41b808b239563b1978468aae12e33a2vboxsync{
d46ee884c41b808b239563b1978468aae12e33a2vboxsync dprintf(("VBoxRestoreDestroy\n"));
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync return;
e33247bff4fddfdba92538374bcc9e2753044a38vboxsync}
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsyncvoid VBoxRestoreSession()
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync{
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync VBoxRestoreCheckVRDP();
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync}
e33247bff4fddfdba92538374bcc9e2753044a38vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsyncvoid VBoxRestoreCheckVRDP()
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync{
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync HDC hdc;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync BOOL ret;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync /* Check VRDP activity */
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync hdc = GetDC(HWND_DESKTOP);
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync /* send to display driver */
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync ret = ExtEscape(hdc, VBOXESC_ISVRDPACTIVE, 0, NULL, 0, NULL);
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync dprintf(("VBoxRestoreSession -> VRDP activate state = %d\n", ret));
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync ReleaseDC(HWND_DESKTOP, hdc);
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync if (ret != gCtx.fRDPState)
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync {
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync DWORD cbReturned;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync if (!DeviceIoControl (gCtx.pEnv->hDriver, (ret) ? IOCTL_VBOXGUEST_ENABLE_VRDP_SESSION : IOCTL_VBOXGUEST_DISABLE_VRDP_SESSION, NULL, 0, NULL, 0, &cbReturned, NULL))
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync {
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync dprintf(("VBoxRestoreThread: DeviceIOControl(CtlMask) failed, SeamlessChangeThread exited\n"));
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync }
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync gCtx.fRDPState = ret;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync }
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync}
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync/**
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync * Thread function to wait for and process seamless mode change
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync * requests
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync */
d8523ff7d948462e328eec88b602effe2e7f7080vboxsyncunsigned __stdcall VBoxRestoreThread(void *pInstance)
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync{
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync VBOXRESTORECONTEXT *pCtx = (VBOXRESTORECONTEXT *)pInstance;
64836f6a22eea42b83b0ec64abcb3aa7ccc27f25vboxsync HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
01b2e367e947049139a5d5813ccc1fa162b11f76vboxsync bool fTerminate = false;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync VBoxGuestFilterMaskInfo maskInfo;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync DWORD cbReturned;
71e8510a26b72d539cf6d7d7157bd87a53de8cf4vboxsync
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync maskInfo.u32OrMask = VMMDEV_EVENT_RESTORED;
e33247bff4fddfdba92538374bcc9e2753044a38vboxsync maskInfo.u32NotMask = 0;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync {
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync dprintf(("VBoxRestoreThread: DeviceIOControl(CtlMask - or) succeeded\n"));
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync }
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync else
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync {
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync dprintf(("VBoxRestoreThread: DeviceIOControl(CtlMask) failed, SeamlessChangeThread exited\n"));
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync return 0;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync }
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
6063286f0f0d78e627c9ef48073f5753da93ba10vboxsync do
6063286f0f0d78e627c9ef48073f5753da93ba10vboxsync {
6063286f0f0d78e627c9ef48073f5753da93ba10vboxsync /* wait for a seamless change event */
6063286f0f0d78e627c9ef48073f5753da93ba10vboxsync VBoxGuestWaitEventInfo waitEvent;
6063286f0f0d78e627c9ef48073f5753da93ba10vboxsync waitEvent.u32TimeoutIn = 5000;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync waitEvent.u32EventMaskIn = VMMDEV_EVENT_RESTORED;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync if (DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync {
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync dprintf(("VBoxRestoreThread: DeviceIOControl succeded\n"));
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync /* are we supposed to stop? */
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
64836f6a22eea42b83b0ec64abcb3aa7ccc27f25vboxsync break;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync dprintf(("VBoxRestoreThread: checking event\n"));
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync /* did we get the right event? */
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_RESTORED)
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync PostMessage(gToolWindow, WM_VBOX_RESTORED, 0, 0);
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync else
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync /** @todo Don't poll, but wait for connect/disconnect events */
b099c6398b85f527d7343cb1da573e1e95f9fd10vboxsync PostMessage(gToolWindow, WM_VBOX_CHECK_VRDP, 0, 0);
b099c6398b85f527d7343cb1da573e1e95f9fd10vboxsync }
b099c6398b85f527d7343cb1da573e1e95f9fd10vboxsync else
b099c6398b85f527d7343cb1da573e1e95f9fd10vboxsync {
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync dprintf(("VBoxTray: error 0 from DeviceIoControl IOCTL_VBOXGUEST_WAITEVENT\n"));
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync /* sleep a bit to not eat too much CPU in case the above call always fails */
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync {
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync fTerminate = true;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync break;
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync }
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync }
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync }
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync while (!fTerminate);
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync
d8523ff7d948462e328eec88b602effe2e7f7080vboxsync maskInfo.u32OrMask = 0;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync maskInfo.u32NotMask = VMMDEV_EVENT_RESTORED;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync {
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync dprintf(("VBoxRestoreThread: DeviceIOControl(CtlMask - not) succeeded\n"));
358a99c385080f7f31166943f3ac3a2aea6b5263vboxsync }
e33247bff4fddfdba92538374bcc9e2753044a38vboxsync else
e33247bff4fddfdba92538374bcc9e2753044a38vboxsync {
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync dprintf(("VBoxRestoreThread: DeviceIOControl(CtlMask) failed\n"));
e33247bff4fddfdba92538374bcc9e2753044a38vboxsync }
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync
8cd393943ea52545c4d063f5a94436639f0f80b6vboxsync dprintf(("VBoxRestoreThread: finished seamless change request thread\n"));
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync return 0;
fe06619ae576367ff3568e6abd99fb8ad28cc73avboxsync}
1d9143584d5616e94efe0ff5ce57e04708529775vboxsync
1d9143584d5616e94efe0ff5ce57e04708529775vboxsync
1d9143584d5616e94efe0ff5ce57e04708529775vboxsync