1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* $Id: seamless.cpp $ */
/** @file
* X11 Guest client - seamless mode: main logic, communication with the host and
* wrapper interface for the main code of the VBoxClient deamon. The
* X11-specific parts are split out into their own file for ease of testing.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* 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 <X11/Xlib.h>
#include <VBox/log.h>
#include <VBox/VBoxGuestLib.h>
#include <iprt/errcore.h>
#include <iprt/mem.h>
#include "VBoxClient.h"
#include "seamless.h"
#include <new>
SeamlessMain::SeamlessMain(void)
{
LogRelFlowFunc(("\n"));
mX11MonitorThread = NIL_RTTHREAD;
mX11MonitorThreadStopping = false;
mMode = VMMDev_Seamless_Disabled;
mfPaused = true;
}
SeamlessMain::~SeamlessMain()
{
LogRelFlowFunc(("\n"));
stop();
}
/**
* Update the set of visible rectangles in the host.
*/
static void sendRegionUpdate(RTRECT *pRects, size_t cRects)
{
LogRelFlowFunc(("\n"));
if (cRects && !pRects) /* Assertion */
{
LogRelFunc(("ERROR: called with null pointer!\n"));
return;
}
VbglR3SeamlessSendRects(cRects, pRects);
LogRelFlowFunc(("returning\n"));
}
/**
* initialise the service.
*/
int SeamlessMain::init(void)
{
int rc;
const char *pcszStage;
LogRelFlowFunc(("\n"));
do {
pcszStage = "Connecting to the X server";
rc = mX11Monitor.init(sendRegionUpdate);
if (RT_FAILURE(rc))
break;
pcszStage = "Setting guest IRQ filter mask";
rc = VbglR3CtlFilterMask(VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST, 0);
if (RT_FAILURE(rc))
break;
pcszStage = "Reporting support for seamless capability";
rc = VbglR3SeamlessSetCap(true);
if (RT_FAILURE(rc))
break;
rc = startX11MonitorThread();
if (RT_FAILURE(rc))
break;
} while(0);
if (RT_FAILURE(rc))
VBClFatalError(("VBoxClient (seamless): failed to start. Stage: \"%s\" Error: %Rrc\n",
pcszStage, rc));
return rc;
}
/**
* Run the main service thread which listens for host state change
* notifications.
* @returns iprt status value. Service will be set to the stopped state on
* failure.
*/
int SeamlessMain::run(void)
{
int rc = VINF_SUCCESS;
LogRelFlowFunc(("\n"));
/* This will only exit if something goes wrong. */
while (RT_SUCCESS(rc) || rc == VERR_INTERRUPTED)
{
if (RT_FAILURE(rc))
/* If we are not stopping, sleep for a bit to avoid using up too
much CPU while retrying. */
RTThreadYield();
rc = nextStateChangeEvent();
}
if (RT_FAILURE(rc))
{
LogRel(("VBoxClient (seamless): event loop failed with error: %Rrc\n",
rc));
stop();
}
return rc;
}
/** Stops the service. */
void SeamlessMain::stop()
{
LogRelFlowFunc(("\n"));
VbglR3SeamlessSetCap(false);
VbglR3CtlFilterMask(0, VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST);
stopX11MonitorThread();
mX11Monitor.uninit();
LogRelFlowFunc(("returning\n"));
}
/**
* Waits for a seamless state change events from the host and dispatch it.
*
* @returns IRPT return code.
*/
int SeamlessMain::nextStateChangeEvent(void)
{
VMMDevSeamlessMode newMode = VMMDev_Seamless_Disabled;
LogRelFlowFunc(("\n"));
int rc = VbglR3SeamlessWaitEvent(&newMode);
if (RT_SUCCESS(rc))
{
mMode = newMode;
switch (newMode)
{
case VMMDev_Seamless_Visible_Region:
/* A simplified seamless mode, obtained by making the host VM window
* borderless and making the guest desktop transparent. */
LogRelFlowFunc(("\"Visible region\" mode requested (VBoxClient).\n"));
break;
case VMMDev_Seamless_Disabled:
LogRelFlowFunc(("\"Disabled\" mode requested (VBoxClient).\n"));
break;
case VMMDev_Seamless_Host_Window:
/* One host window represents one guest window. Not yet implemented. */
LogRelFunc(("Unsupported \"host window\" mode requested (VBoxClient).\n"));
return VERR_NOT_SUPPORTED;
default:
LogRelFunc(("Unsupported mode %d requested (VBoxClient).\n",
newMode));
return VERR_NOT_SUPPORTED;
}
}
if (RT_SUCCESS(rc) || rc == VERR_TRY_AGAIN)
{
if (mMode == VMMDev_Seamless_Visible_Region)
mfPaused = false;
else
mfPaused = true;
mX11Monitor.interruptEventWait();
}
else
{
LogRelFunc(("VbglR3SeamlessWaitEvent returned %Rrc (VBoxClient)\n", rc));
}
LogRelFlowFunc(("returning %Rrc\n", rc));
return rc;
}
/**
* The actual X11 window configuration change monitor thread function.
*/
int SeamlessMain::x11MonitorThread(RTTHREAD hThreadSelf, void *pvUser)
{
RT_NOREF1(hThreadSelf);
SeamlessMain *pHost = (SeamlessMain *)pvUser;
int rc = VINF_SUCCESS;
LogRelFlowFunc(("\n"));
while (!pHost->mX11MonitorThreadStopping)
{
if (!pHost->mfPaused)
{
rc = pHost->mX11Monitor.start();
if (RT_FAILURE(rc))
VBClFatalError(("Failed to change the X11 seamless service state, mfPaused=%RTbool, rc=%Rrc\n",
pHost->mfPaused, rc));
}
pHost->mX11Monitor.nextConfigurationEvent();
if (pHost->mfPaused || pHost->mX11MonitorThreadStopping)
pHost->mX11Monitor.stop();
}
LogRelFlowFunc(("returning %Rrc\n", rc));
return rc;
}
/**
* Start the X11 window configuration change monitor thread.
*/
int SeamlessMain::startX11MonitorThread(void)
{
int rc;
mX11MonitorThreadStopping = false;
if (isX11MonitorThreadRunning())
return VINF_SUCCESS;
rc = RTThreadCreate(&mX11MonitorThread, x11MonitorThread, this, 0,
RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE,
"X11 events");
if (RT_FAILURE(rc))
LogRelFunc(("Warning: failed to start X11 monitor thread (VBoxClient).\n"));
return rc;
}
/**
* Send a signal to the thread function that it should exit
*/
int SeamlessMain::stopX11MonitorThread(void)
{
int rc;
mX11MonitorThreadStopping = true;
if (!isX11MonitorThreadRunning())
return VINF_SUCCESS;
mX11Monitor.interruptEventWait();
rc = RTThreadWait(mX11MonitorThread, RT_INDEFINITE_WAIT, NULL);
if (RT_SUCCESS(rc))
mX11MonitorThread = NIL_RTTHREAD;
else
LogRelThisFunc(("Failed to stop X11 monitor thread, rc=%Rrc!\n",
rc));
return rc;
}
/** Service magic number, start of a UUID. */
#define SEAMLESSSERVICE_MAGIC 0xd28ba727
/** VBoxClient service class wrapping the logic for the seamless service while
* the main VBoxClient code provides the daemon logic needed by all services.
*/
struct SEAMLESSSERVICE
{
/** The service interface. */
struct VBCLSERVICE *pInterface;
/** Magic number for sanity checks. */
uint32_t magic;
/** Seamless service object. */
SeamlessMain mSeamless;
/** Are we initialised yet? */
bool mIsInitialised;
};
static const char *getPidFilePath(void)
{
return ".vboxclient-seamless.pid";
}
static struct SEAMLESSSERVICE *getClassFromInterface(struct VBCLSERVICE **
ppInterface)
{
struct SEAMLESSSERVICE *pSelf = (struct SEAMLESSSERVICE *)ppInterface;
if (pSelf->magic != SEAMLESSSERVICE_MAGIC)
VBClFatalError(("Bad seamless service object!\n"));
return pSelf;
}
static int init(struct VBCLSERVICE **ppInterface)
{
struct SEAMLESSSERVICE *pSelf = getClassFromInterface(ppInterface);
int rc;
if (pSelf->mIsInitialised)
return VERR_INTERNAL_ERROR;
/* Initialise the guest library. */
rc = VbglR3InitUser();
if (RT_FAILURE(rc))
VBClFatalError(("Failed to connect to the VirtualBox kernel service, rc=%Rrc\n", rc));
rc = pSelf->mSeamless.init();
if (RT_FAILURE(rc))
return rc;
pSelf->mIsInitialised = true;
return VINF_SUCCESS;
}
static int run(struct VBCLSERVICE **ppInterface, bool fDaemonised)
{
RT_NOREF1(fDaemonised);
struct SEAMLESSSERVICE *pSelf = getClassFromInterface(ppInterface);
int rc;
if (!pSelf->mIsInitialised)
return VERR_INTERNAL_ERROR;
/* This only exits on error. */
rc = pSelf->mSeamless.run();
pSelf->mIsInitialised = false;
return rc;
}
static void cleanup(struct VBCLSERVICE **ppInterface)
{
NOREF(ppInterface);
VbglR3SeamlessSetCap(false);
VbglR3Term();
}
struct VBCLSERVICE vbclSeamlessInterface =
{
getPidFilePath,
init,
run,
cleanup
};
struct VBCLSERVICE **VBClGetSeamlessService()
{
struct SEAMLESSSERVICE *pService =
(struct SEAMLESSSERVICE *)RTMemAlloc(sizeof(*pService));
if (!pService)
VBClFatalError(("Out of memory\n"));
pService->pInterface = &vbclSeamlessInterface;
pService->magic = SEAMLESSSERVICE_MAGIC;
new(&pService->mSeamless) SeamlessMain();
pService->mIsInitialised = false;
return &pService->pInterface;
}
|