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
345
346
|
/* $Id: VBoxSharedClipboardSvc-darwin.cpp $ */
/** @file
* Shared Clipboard Service - Mac OS X host.
*/
/*
* Copyright (C) 2008-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
#include <VBox/HostServices/VBoxClipboardSvc.h>
#include <iprt/assert.h>
#include <iprt/asm.h>
#include <iprt/process.h>
#include <iprt/rand.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include "VBoxSharedClipboardSvc-internal.h"
#include "darwin-pasteboard.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/** Global clipboard context information */
typedef struct SHCLCONTEXT
{
/** We have a separate thread to poll for new clipboard content. */
RTTHREAD hThread;
/** Termination indicator. */
bool volatile fTerminate;
/** The reference to the current pasteboard */
PasteboardRef hPasteboard;
/** Shared clipboard client. */
PSHCLCLIENT pClient;
/** Random 64-bit number embedded into szGuestOwnershipFlavor. */
uint64_t idGuestOwnership;
/** Ownership flavor CFStringRef returned by takePasteboardOwnership().
* This is the same a szGuestOwnershipFlavor only in core foundation terms. */
void *hStrOwnershipFlavor;
/** The guest ownership flavor (type) string. */
char szGuestOwnershipFlavor[64];
} SHCLCONTEXT;
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** Only one client is supported. There seems to be no need for more clients. */
static SHCLCONTEXT g_ctx;
/**
* Checks if something is present on the clipboard and calls shclSvcReportMsg.
*
* @returns IPRT status code (ignored).
* @param pCtx The context.
*
* @note Call must own lock.
*/
static int vboxClipboardChanged(SHCLCONTEXT *pCtx)
{
if (pCtx->pClient == NULL)
return VINF_SUCCESS;
/* Retrieve the formats currently in the clipboard and supported by vbox */
uint32_t fFormats = 0;
bool fChanged = false;
int rc = queryNewPasteboardFormats(pCtx->hPasteboard, pCtx->idGuestOwnership, pCtx->hStrOwnershipFlavor,
&fFormats, &fChanged);
if ( RT_SUCCESS(rc)
&& fChanged
&& ShClSvcIsBackendActive())
rc = ShClSvcHostReportFormats(pCtx->pClient, fFormats);
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* @callback_method_impl{FNRTTHREAD, The poller thread.
*
* This thread will check for the arrival of new data on the clipboard.}
*/
static DECLCALLBACK(int) vboxClipboardThread(RTTHREAD ThreadSelf, void *pvUser)
{
SHCLCONTEXT *pCtx = (SHCLCONTEXT *)pvUser;
AssertPtr(pCtx);
LogFlowFuncEnter();
while (!pCtx->fTerminate)
{
/* call this behind the lock because we don't know if the api is
thread safe and in any case we're calling several methods. */
ShClSvcLock();
vboxClipboardChanged(pCtx);
ShClSvcUnlock();
/* Sleep for 200 msecs before next poll */
RTThreadUserWait(ThreadSelf, 200);
}
LogFlowFuncLeaveRC(VINF_SUCCESS);
return VINF_SUCCESS;
}
int ShClBackendInit(PSHCLBACKEND pBackend, VBOXHGCMSVCFNTABLE *pTable)
{
RT_NOREF(pBackend, pTable);
g_ctx.fTerminate = false;
int rc = initPasteboard(&g_ctx.hPasteboard);
AssertRCReturn(rc, rc);
rc = RTThreadCreate(&g_ctx.hThread, vboxClipboardThread, &g_ctx, 0,
RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
if (RT_FAILURE(rc))
{
g_ctx.hThread = NIL_RTTHREAD;
destroyPasteboard(&g_ctx.hPasteboard);
}
return rc;
}
void ShClBackendDestroy(PSHCLBACKEND pBackend)
{
RT_NOREF(pBackend);
/*
* Signal the termination of the polling thread and wait for it to respond.
*/
ASMAtomicWriteBool(&g_ctx.fTerminate, true);
int rc = RTThreadUserSignal(g_ctx.hThread);
AssertRC(rc);
rc = RTThreadWait(g_ctx.hThread, RT_INDEFINITE_WAIT, NULL);
AssertRC(rc);
/*
* Destroy the hPasteboard and uninitialize the global context record.
*/
destroyPasteboard(&g_ctx.hPasteboard);
g_ctx.hThread = NIL_RTTHREAD;
g_ctx.pClient = NULL;
}
int ShClBackendConnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, bool fHeadless)
{
RT_NOREF(pBackend, fHeadless);
if (g_ctx.pClient != NULL)
{
/* One client only. */
return VERR_NOT_SUPPORTED;
}
ShClSvcLock();
pClient->State.pCtx = &g_ctx;
pClient->State.pCtx->pClient = pClient;
ShClSvcUnlock();
return VINF_SUCCESS;
}
int ShClBackendSync(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
{
RT_NOREF(pBackend);
/* Sync the host clipboard content with the client. */
ShClSvcLock();
int rc = vboxClipboardChanged(pClient->State.pCtx);
ShClSvcUnlock();
return rc;
}
int ShClBackendDisconnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
{
RT_NOREF(pBackend);
ShClSvcLock();
pClient->State.pCtx->pClient = NULL;
ShClSvcUnlock();
return VINF_SUCCESS;
}
int ShClBackendReportFormats(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, SHCLFORMATS fFormats)
{
RT_NOREF(pBackend);
LogFlowFunc(("fFormats=%02X\n", fFormats));
/** @todo r=bird: BUGBUG: The following is probably a mistake. */
/** @todo r=andy: BUGBUG: Has been there since forever; needs investigation first before removing. */
if (fFormats == VBOX_SHCL_FMT_NONE)
{
/* This is just an automatism, not a genuine announcement */
return VINF_SUCCESS;
}
#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
if (fFormats & VBOX_SHCL_FMT_URI_LIST) /* No transfer support yet. */
return VINF_SUCCESS;
#endif
SHCLCONTEXT *pCtx = pClient->State.pCtx;
ShClSvcLock();
/*
* Generate a unique flavor string for this format announcement.
*/
uint64_t idFlavor = RTRandU64();
pCtx->idGuestOwnership = idFlavor;
RTStrPrintf(pCtx->szGuestOwnershipFlavor, sizeof(pCtx->szGuestOwnershipFlavor),
"org.virtualbox.sharedclipboard.%RTproc.%RX64", RTProcSelf(), idFlavor);
/*
* Empty the pasteboard and put our ownership indicator flavor there
* with the stringified formats as value.
*/
char szValue[32];
RTStrPrintf(szValue, sizeof(szValue), "%#x", fFormats);
takePasteboardOwnership(pCtx->hPasteboard, pCtx->idGuestOwnership, pCtx->szGuestOwnershipFlavor, szValue,
&pCtx->hStrOwnershipFlavor);
ShClSvcUnlock();
/*
* Now, request the data from the guest.
*/
return ShClSvcGuestDataRequest(pClient, fFormats, NULL /* pidEvent */);
}
int ShClBackendReadData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT fFormat,
void *pvData, uint32_t cbData, uint32_t *pcbActual)
{
AssertPtrReturn(pClient, VERR_INVALID_POINTER);
AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
AssertPtrReturn(pvData, VERR_INVALID_POINTER);
AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
RT_NOREF(pBackend, pCmdCtx);
ShClSvcLock();
/* Default to no data available. */
*pcbActual = 0;
int rc = readFromPasteboard(pClient->State.pCtx->hPasteboard, fFormat, pvData, cbData, pcbActual);
if (RT_FAILURE(rc))
LogRel(("Shared Clipboard: Error reading host clipboard data from macOS, rc=%Rrc\n", rc));
ShClSvcUnlock();
return rc;
}
int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT fFormat, void *pvData, uint32_t cbData)
{
RT_NOREF(pBackend, pCmdCtx);
LogFlowFuncEnter();
ShClSvcLock();
writeToPasteboard(pClient->State.pCtx->hPasteboard, pClient->State.pCtx->idGuestOwnership, pvData, cbData, fFormat);
ShClSvcUnlock();
LogFlowFuncLeaveRC(VINF_SUCCESS);
return VINF_SUCCESS;
}
#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
int ShClBackendTransferReadDir(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLDIRDATA pDirData)
{
RT_NOREF(pBackend, pClient, pDirData);
return VERR_NOT_IMPLEMENTED;
}
int ShClBackendTransferWriteDir(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLDIRDATA pDirData)
{
RT_NOREF(pBackend, pClient, pDirData);
return VERR_NOT_IMPLEMENTED;
}
int ShClBackendTransferReadFileHdr(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLFILEHDR pFileHdr)
{
RT_NOREF(pBackend, pClient, pFileHdr);
return VERR_NOT_IMPLEMENTED;
}
int ShClBackendTransferWriteFileHdr(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLFILEHDR pFileHdr)
{
RT_NOREF(pBackend, pClient, pFileHdr);
return VERR_NOT_IMPLEMENTED;
}
int ShClBackendTransferReadFileData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLFILEDATA pFileData)
{
RT_NOREF(pBackend, pClient, pFileData);
return VERR_NOT_IMPLEMENTED;
}
int ShClBackendTransferWriteFileData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLFILEDATA pFileData)
{
RT_NOREF(pBackend, pClient, pFileData);
return VERR_NOT_IMPLEMENTED;
}
#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
|