VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/clipboard-x11.cpp@ 99600

Last change on this file since 99600 was 99590, checked in by vboxsync, 22 months ago

Guest Additions/VBoxClient: Moved the X11-specific code for Shared Clipboard into an own submodule and using the actual service as a (runtime) wrapper (for also the Wayland-specific code later). bugref:10427

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.9 KB
Line 
1/** $Id: clipboard-x11.cpp 99590 2023-05-03 15:59:12Z vboxsync $ */
2/** @file
3 * Guest Additions - X11 Shared Clipboard implementation.
4 */
5
6/*
7 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <iprt/alloc.h>
33#include <iprt/asm.h>
34#include <iprt/assert.h>
35#include <iprt/initterm.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38#include <iprt/path.h>
39#include <iprt/process.h>
40#include <iprt/semaphore.h>
41
42#include <VBox/log.h>
43#include <VBox/VBoxGuestLib.h>
44#include <VBox/HostServices/VBoxClipboardSvc.h>
45#include <VBox/GuestHost/SharedClipboard.h>
46#include <VBox/GuestHost/SharedClipboard-x11.h>
47
48#include "VBoxClient.h"
49
50#include "clipboard.h"
51
52
53static DECLCALLBACK(int) vbclX11OnRequestDataFromSourceCallback(PSHCLCONTEXT pCtx,
54 SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
55{
56 RT_NOREF(pvUser);
57
58 LogFlowFunc(("pCtx=%p, uFmt=%#x\n", pCtx, uFmt));
59
60 int rc = VINF_SUCCESS;
61
62#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
63 if (uFmt == VBOX_SHCL_FMT_URI_LIST)
64 {
65 //rc = VbglR3ClipboardRootListRead()
66 rc = VERR_NO_DATA;
67 }
68 else
69#endif
70 {
71 uint32_t cbRead = 0;
72
73 uint32_t cbData = _4K; /** @todo Make this dynamic. */
74 void *pvData = RTMemAlloc(cbData);
75 if (pvData)
76 {
77 rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, pvData, cbData, &cbRead);
78 }
79 else
80 rc = VERR_NO_MEMORY;
81
82 /*
83 * A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
84 * larger buffer. The size of the buffer needed is placed in *pcb.
85 * So we start all over again.
86 */
87 if (rc == VINF_BUFFER_OVERFLOW)
88 {
89 /* cbRead contains the size required. */
90
91 cbData = cbRead;
92 pvData = RTMemRealloc(pvData, cbRead);
93 if (pvData)
94 {
95 rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, pvData, cbData, &cbRead);
96 if (rc == VINF_BUFFER_OVERFLOW)
97 rc = VERR_BUFFER_OVERFLOW;
98 }
99 else
100 rc = VERR_NO_MEMORY;
101 }
102
103 if (!cbRead)
104 rc = VERR_NO_DATA;
105
106 if (RT_SUCCESS(rc))
107 {
108 *pcb = cbRead; /* Actual bytes read. */
109 *ppv = pvData;
110 }
111 else
112 {
113 /*
114 * Catch other errors. This also catches the case in which the buffer was
115 * too small a second time, possibly because the clipboard contents
116 * changed half-way through the operation. Since we can't say whether or
117 * not this is actually an error, we just return size 0.
118 */
119 RTMemFree(pvData);
120 }
121 }
122
123 if (RT_FAILURE(rc))
124 LogRel(("Requesting data in format %#x from host failed with %Rrc\n", uFmt, rc));
125
126 LogFlowFuncLeaveRC(rc);
127 return rc;
128}
129
130/**
131 * Opaque data structure describing a request from the host for clipboard
132 * data, passed in when the request is forwarded to the X11 backend so that
133 * it can be completed correctly.
134 */
135struct CLIPREADCBREQ
136{
137 /** The data format that was requested. */
138 SHCLFORMAT uFmt;
139};
140
141static DECLCALLBACK(int) vbclX11ReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser)
142{
143 RT_NOREF(pvUser);
144
145 LogFlowFunc(("fFormats=%#x\n", fFormats));
146
147 int rc = VbglR3ClipboardReportFormats(pCtx->CmdCtx.idClient, fFormats);
148 LogFlowFuncLeaveRC(rc);
149
150 return rc;
151}
152
153static DECLCALLBACK(int) vbclX11OnSendDataToDestCallback(PSHCLCONTEXT pCtx, void *pv, uint32_t cb, void *pvUser)
154{
155 PSHCLX11READDATAREQ pData = (PSHCLX11READDATAREQ)pvUser;
156 AssertPtrReturn(pData, VERR_INVALID_POINTER);
157
158 LogFlowFunc(("rcCompletion=%Rrc, Format=0x%x, pv=%p, cb=%RU32\n", pData->rcCompletion, pData->pReq->uFmt, pv, cb));
159
160 Assert((cb == 0 && pv == NULL) || (cb != 0 && pv != NULL));
161 pData->rcCompletion = VbglR3ClipboardWriteDataEx(&pCtx->CmdCtx, pData->pReq->uFmt, pv, cb);
162
163 RTMemFree(pData->pReq);
164
165 LogFlowFuncLeaveRC(pData->rcCompletion);
166
167 return VINF_SUCCESS;
168}
169
170/**
171 * Initializes the X11-specifc Shared Clipboard code.
172 *
173 * @returns VBox status code.
174 */
175int VBClX11ClipboardInit(void)
176{
177 LogFlowFuncEnter();
178
179 SHCLCALLBACKS Callbacks;
180 RT_ZERO(Callbacks);
181 Callbacks.pfnReportFormats = vbclX11ReportFormatsCallback;
182 Callbacks.pfnOnRequestDataFromSource = vbclX11OnRequestDataFromSourceCallback;
183 Callbacks.pfnOnSendDataToDest = vbclX11OnSendDataToDestCallback;
184
185 int rc = ShClX11Init(&g_Ctx.X11, &Callbacks, &g_Ctx, false /* fHeadless */);
186 if (RT_SUCCESS(rc))
187 {
188 rc = ShClX11ThreadStart(&g_Ctx.X11, false /* grab */);
189 if (RT_SUCCESS(rc))
190 {
191 rc = VbglR3ClipboardConnectEx(&g_Ctx.CmdCtx, VBOX_SHCL_GF_0_CONTEXT_ID);
192 if (RT_FAILURE(rc))
193 ShClX11ThreadStop(&g_Ctx.X11);
194 }
195 }
196 else
197 rc = VERR_NO_MEMORY;
198
199 if (RT_FAILURE(rc))
200 {
201 VBClLogError("Error connecting to host service, rc=%Rrc\n", rc);
202
203 VbglR3ClipboardDisconnectEx(&g_Ctx.CmdCtx);
204 ShClX11Destroy(&g_Ctx.X11);
205 }
206
207 LogFlowFuncLeaveRC(rc);
208 return rc;
209}
210
211/**
212 * Destroys the X11-specifc Shared Clipboard code.
213 *
214 * @returns VBox status code.
215 */
216int VBClX11ClipboardDestroy(void)
217{
218 /* Nothing to do here currently. */
219 return VINF_SUCCESS;
220}
221
222/**
223 * The main loop of the X11-specifc Shared Clipboard code.
224 *
225 * @returns VBox status code.
226 */
227int VBClX11ClipboardMain(void)
228{
229 int rc;
230
231 PSHCLCONTEXT pCtx = &g_Ctx;
232
233 bool fShutdown = false;
234
235 /* The thread waits for incoming messages from the host. */
236 for (;;)
237 {
238 PVBGLR3CLIPBOARDEVENT pEvent = (PVBGLR3CLIPBOARDEVENT)RTMemAllocZ(sizeof(VBGLR3CLIPBOARDEVENT));
239 AssertPtrBreakStmt(pEvent, rc = VERR_NO_MEMORY);
240
241 LogFlowFunc(("Waiting for host message (fUseLegacyProtocol=%RTbool, fHostFeatures=%#RX64) ...\n",
242 pCtx->CmdCtx.fUseLegacyProtocol, pCtx->CmdCtx.fHostFeatures));
243
244 uint32_t idMsg = 0;
245 uint32_t cParms = 0;
246 rc = VbglR3ClipboardMsgPeekWait(&pCtx->CmdCtx, &idMsg, &cParms, NULL /* pidRestoreCheck */);
247 if (RT_SUCCESS(rc))
248 {
249#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
250 rc = VbglR3ClipboardEventGetNextEx(idMsg, cParms, &pCtx->CmdCtx, &pCtx->TransferCtx, pEvent);
251#else
252 rc = VbglR3ClipboardEventGetNext(idMsg, cParms, &pCtx->CmdCtx, pEvent);
253#endif
254 }
255
256 if (RT_FAILURE(rc))
257 {
258 LogFlowFunc(("Getting next event failed with %Rrc\n", rc));
259
260 VbglR3ClipboardEventFree(pEvent);
261 pEvent = NULL;
262
263 if (fShutdown)
264 break;
265
266 /* Wait a bit before retrying. */
267 RTThreadSleep(1000);
268 continue;
269 }
270 else
271 {
272 AssertPtr(pEvent);
273 LogFlowFunc(("Event uType=%RU32\n", pEvent->enmType));
274
275 switch (pEvent->enmType)
276 {
277 case VBGLR3CLIPBOARDEVENTTYPE_REPORT_FORMATS:
278 {
279 ShClX11ReportFormatsToX11(&g_Ctx.X11, pEvent->u.fReportedFormats);
280 break;
281 }
282
283 case VBGLR3CLIPBOARDEVENTTYPE_READ_DATA:
284 {
285 /* The host needs data in the specified format. */
286 CLIPREADCBREQ *pReq;
287 pReq = (CLIPREADCBREQ *)RTMemAllocZ(sizeof(CLIPREADCBREQ));
288 if (pReq)
289 {
290 pReq->uFmt = pEvent->u.fReadData;
291 ShClX11ReadDataFromX11(&g_Ctx.X11, pReq->uFmt, pReq);
292 }
293 else
294 rc = VERR_NO_MEMORY;
295 break;
296 }
297
298 case VBGLR3CLIPBOARDEVENTTYPE_QUIT:
299 {
300 VBClLogVerbose(2, "Host requested termination\n");
301 fShutdown = true;
302 break;
303 }
304
305#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
306 case VBGLR3CLIPBOARDEVENTTYPE_TRANSFER_STATUS:
307 {
308 /* Nothing to do here. */
309 rc = VINF_SUCCESS;
310 break;
311 }
312#endif
313 case VBGLR3CLIPBOARDEVENTTYPE_NONE:
314 {
315 /* Nothing to do here. */
316 rc = VINF_SUCCESS;
317 break;
318 }
319
320 default:
321 {
322 AssertMsgFailedBreakStmt(("Event type %RU32 not implemented\n", pEvent->enmType), rc = VERR_NOT_SUPPORTED);
323 }
324 }
325
326 if (pEvent)
327 {
328 VbglR3ClipboardEventFree(pEvent);
329 pEvent = NULL;
330 }
331 }
332
333 if (fShutdown)
334 break;
335 }
336
337 LogFlowFuncLeaveRC(rc);
338 return rc;
339}
340
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