VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/seamless.cpp@ 59010

Last change on this file since 59010 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette