1 | /* $Id: VBoxRestore.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxRestore - Restore notification.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #define _WIN32_WINNT 0x0500
|
---|
18 | #include <windows.h>
|
---|
19 | #include "VBoxTray.h"
|
---|
20 | #include "VBoxRestore.h"
|
---|
21 | #include <VBoxDisplay.h>
|
---|
22 | #include <VBox/VMMDev.h>
|
---|
23 | #include <VBoxGuestInternal.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include "helpers.h"
|
---|
26 |
|
---|
27 | typedef struct _VBOXRESTORECONTEXT
|
---|
28 | {
|
---|
29 | const VBOXSERVICEENV *pEnv;
|
---|
30 |
|
---|
31 | DWORD fRDPState;
|
---|
32 | } VBOXRESTORECONTEXT;
|
---|
33 |
|
---|
34 |
|
---|
35 | static VBOXRESTORECONTEXT gCtx = {0};
|
---|
36 |
|
---|
37 |
|
---|
38 | int VBoxRestoreInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
|
---|
39 | {
|
---|
40 | Log(("VBoxRestoreInit\n"));
|
---|
41 |
|
---|
42 | gCtx.pEnv = pEnv;
|
---|
43 | gCtx.fRDPState = ERROR_NOT_SUPPORTED;
|
---|
44 |
|
---|
45 | VBoxRestoreCheckVRDP();
|
---|
46 |
|
---|
47 | *pfStartThread = true;
|
---|
48 | *ppInstance = &gCtx;
|
---|
49 | return VINF_SUCCESS;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | void VBoxRestoreDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
|
---|
54 | {
|
---|
55 | Log(("VBoxRestoreDestroy\n"));
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | void VBoxRestoreSession()
|
---|
60 | {
|
---|
61 | VBoxRestoreCheckVRDP();
|
---|
62 | }
|
---|
63 |
|
---|
64 | void VBoxRestoreCheckVRDP()
|
---|
65 | {
|
---|
66 | DWORD ret;
|
---|
67 | VBOXDISPIFESCAPE escape = {0};
|
---|
68 | escape.escapeCode = VBOXESC_ISVRDPACTIVE;
|
---|
69 | /* Check VRDP activity */
|
---|
70 |
|
---|
71 | /* send to display driver */
|
---|
72 | ret = VBoxDispIfEscape(&gCtx.pEnv->dispIf, &escape, 0);
|
---|
73 | Log(("VBoxRestoreSession -> VRDP activate state = %d\n", ret));
|
---|
74 |
|
---|
75 | if (ret != gCtx.fRDPState)
|
---|
76 | {
|
---|
77 | DWORD cbReturned;
|
---|
78 |
|
---|
79 | if (!DeviceIoControl (gCtx.pEnv->hDriver, ret == NO_ERROR ? VBOXGUEST_IOCTL_ENABLE_VRDP_SESSION : VBOXGUEST_IOCTL_DISABLE_VRDP_SESSION, NULL, 0, NULL, 0, &cbReturned, NULL))
|
---|
80 | {
|
---|
81 | Log(("VBoxRestoreThread: DeviceIOControl(CtlMask) failed, SeamlessChangeThread exited\n"));
|
---|
82 | }
|
---|
83 | gCtx.fRDPState = ret;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Thread function to wait for and process seamless mode change
|
---|
89 | * requests
|
---|
90 | */
|
---|
91 | unsigned __stdcall VBoxRestoreThread(void *pInstance)
|
---|
92 | {
|
---|
93 | VBOXRESTORECONTEXT *pCtx = (VBOXRESTORECONTEXT *)pInstance;
|
---|
94 | HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
|
---|
95 | bool fTerminate = false;
|
---|
96 | VBoxGuestFilterMaskInfo maskInfo;
|
---|
97 | DWORD cbReturned;
|
---|
98 |
|
---|
99 | maskInfo.u32OrMask = VMMDEV_EVENT_RESTORED;
|
---|
100 | maskInfo.u32NotMask = 0;
|
---|
101 | if (DeviceIoControl (gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
102 | {
|
---|
103 | Log(("VBoxRestoreThread: DeviceIOControl(CtlMask - or) succeeded\n"));
|
---|
104 | }
|
---|
105 | else
|
---|
106 | {
|
---|
107 | Log(("VBoxRestoreThread: DeviceIOControl(CtlMask) failed, SeamlessChangeThread exited\n"));
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | do
|
---|
112 | {
|
---|
113 | /* wait for a seamless change event */
|
---|
114 | VBoxGuestWaitEventInfo waitEvent;
|
---|
115 | waitEvent.u32TimeoutIn = 5000;
|
---|
116 | waitEvent.u32EventMaskIn = VMMDEV_EVENT_RESTORED;
|
---|
117 | if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
|
---|
118 | {
|
---|
119 | Log(("VBoxRestoreThread: DeviceIOControl succeded\n"));
|
---|
120 |
|
---|
121 | /* are we supposed to stop? */
|
---|
122 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
|
---|
123 | break;
|
---|
124 |
|
---|
125 | Log(("VBoxRestoreThread: checking event\n"));
|
---|
126 |
|
---|
127 | /* did we get the right event? */
|
---|
128 | if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_RESTORED)
|
---|
129 | PostMessage(gToolWindow, WM_VBOX_RESTORED, 0, 0);
|
---|
130 | else
|
---|
131 | /** @todo Don't poll, but wait for connect/disconnect events */
|
---|
132 | PostMessage(gToolWindow, WM_VBOX_CHECK_VRDP, 0, 0);
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | Log(("VBoxTray: error 0 from DeviceIoControl VBOXGUEST_IOCTL_WAITEVENT\n"));
|
---|
137 |
|
---|
138 | /* sleep a bit to not eat too much CPU in case the above call always fails */
|
---|
139 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
|
---|
140 | {
|
---|
141 | fTerminate = true;
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | while (!fTerminate);
|
---|
147 |
|
---|
148 | maskInfo.u32OrMask = 0;
|
---|
149 | maskInfo.u32NotMask = VMMDEV_EVENT_RESTORED;
|
---|
150 | if (DeviceIoControl (gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
151 | {
|
---|
152 | Log(("VBoxRestoreThread: DeviceIOControl(CtlMask - not) succeeded\n"));
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | Log(("VBoxRestoreThread: DeviceIOControl(CtlMask) failed\n"));
|
---|
157 | }
|
---|
158 |
|
---|
159 | Log(("VBoxRestoreThread: finished seamless change request thread\n"));
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 |
|
---|