1 | /* $Id: VBoxSharedClipboardSvc-darwin.cpp 79041 2019-06-07 15:26:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard Service - Mac OS X host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2019 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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
23 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
24 |
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/asm.h>
|
---|
27 | #include <iprt/thread.h>
|
---|
28 |
|
---|
29 | #include "VBoxSharedClipboardSvc-internal.h"
|
---|
30 | #include "darwin-pasteboard.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | /*********************************************************************************************************************************
|
---|
34 | * Structures and Typedefs *
|
---|
35 | *********************************************************************************************************************************/
|
---|
36 | /** Global clipboard context information */
|
---|
37 | struct _VBOXCLIPBOARDCONTEXT
|
---|
38 | {
|
---|
39 | /** We have a separate thread to poll for new clipboard content */
|
---|
40 | RTTHREAD thread;
|
---|
41 | bool volatile fTerminate;
|
---|
42 |
|
---|
43 | /** The reference to the current pasteboard */
|
---|
44 | PasteboardRef pasteboard;
|
---|
45 |
|
---|
46 | PVBOXCLIPBOARDCLIENTDATA pClientData;
|
---|
47 | };
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** Only one client is supported. There seems to be no need for more clients. */
|
---|
54 | static VBOXCLIPBOARDCONTEXT g_ctx;
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Checks if something is present on the clipboard and calls vboxSvcClipboardReportMsg.
|
---|
59 | *
|
---|
60 | * @returns IPRT status code (ignored).
|
---|
61 | * @param pCtx The context.
|
---|
62 | */
|
---|
63 | static int vboxClipboardChanged(VBOXCLIPBOARDCONTEXT *pCtx)
|
---|
64 | {
|
---|
65 | if (pCtx->pClientData == NULL)
|
---|
66 | return VINF_SUCCESS;
|
---|
67 |
|
---|
68 | uint32_t fFormats = 0;
|
---|
69 | bool fChanged = false;
|
---|
70 | /* Retrieve the formats currently in the clipboard and supported by vbox */
|
---|
71 | int rc = queryNewPasteboardFormats(pCtx->pasteboard, &fFormats, &fChanged);
|
---|
72 | if (RT_SUCCESS(rc) && fChanged)
|
---|
73 | {
|
---|
74 | vboxSvcClipboardReportMsg(pCtx->pClientData, VBOX_SHARED_CLIPBOARD_HOST_MSG_REPORT_FORMATS, fFormats);
|
---|
75 | Log(("vboxClipboardChanged fFormats %02X\n", fFormats));
|
---|
76 | }
|
---|
77 |
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * The poller thread.
|
---|
84 | *
|
---|
85 | * This thread will check for the arrival of new data on the clipboard.
|
---|
86 | *
|
---|
87 | * @returns VINF_SUCCESS (not used).
|
---|
88 | * @param ThreadSelf Our thread handle.
|
---|
89 | * @param pvUser Pointer to the VBOXCLIPBOARDCONTEXT structure.
|
---|
90 | *
|
---|
91 | */
|
---|
92 | static int vboxClipboardThread(RTTHREAD ThreadSelf, void *pvUser)
|
---|
93 | {
|
---|
94 | Log(("vboxClipboardThread: starting clipboard thread\n"));
|
---|
95 |
|
---|
96 | AssertPtrReturn(pvUser, VERR_INVALID_PARAMETER);
|
---|
97 | VBOXCLIPBOARDCONTEXT *pCtx = (VBOXCLIPBOARDCONTEXT *)pvUser;
|
---|
98 |
|
---|
99 | while (!pCtx->fTerminate)
|
---|
100 | {
|
---|
101 | /* call this behind the lock because we don't know if the api is
|
---|
102 | thread safe and in any case we're calling several methods. */
|
---|
103 | VBoxSvcClipboardLock();
|
---|
104 | vboxClipboardChanged(pCtx);
|
---|
105 | VBoxSvcClipboardUnlock();
|
---|
106 |
|
---|
107 | /* Sleep for 200 msecs before next poll */
|
---|
108 | RTThreadUserWait(ThreadSelf, 200);
|
---|
109 | }
|
---|
110 |
|
---|
111 | Log(("vboxClipboardThread: clipboard thread terminated successfully with return code %Rrc\n", VINF_SUCCESS));
|
---|
112 | return VINF_SUCCESS;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Public platform dependent functions.
|
---|
117 | */
|
---|
118 |
|
---|
119 | /** Initialise the host side of the shared clipboard - called by the hgcm layer. */
|
---|
120 | int VBoxClipboardSvcImplInit(void)
|
---|
121 | {
|
---|
122 | Log(("vboxClipboardInit\n"));
|
---|
123 |
|
---|
124 | g_ctx.fTerminate = false;
|
---|
125 |
|
---|
126 | int rc = initPasteboard(&g_ctx.pasteboard);
|
---|
127 | AssertRCReturn(rc, rc);
|
---|
128 |
|
---|
129 | rc = RTThreadCreate(&g_ctx.thread, vboxClipboardThread, &g_ctx, 0,
|
---|
130 | RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
|
---|
131 | if (RT_FAILURE(rc))
|
---|
132 | {
|
---|
133 | g_ctx.thread = NIL_RTTHREAD;
|
---|
134 | destroyPasteboard(&g_ctx.pasteboard);
|
---|
135 | }
|
---|
136 |
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Terminate the host side of the shared clipboard - called by the hgcm layer. */
|
---|
141 | void VBoxClipboardSvcImplDestroy(void)
|
---|
142 | {
|
---|
143 | Log(("vboxClipboardDestroy\n"));
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Signal the termination of the polling thread and wait for it to respond.
|
---|
147 | */
|
---|
148 | ASMAtomicWriteBool(&g_ctx.fTerminate, true);
|
---|
149 | int rc = RTThreadUserSignal(g_ctx.thread);
|
---|
150 | AssertRC(rc);
|
---|
151 | rc = RTThreadWait(g_ctx.thread, RT_INDEFINITE_WAIT, NULL);
|
---|
152 | AssertRC(rc);
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * Destroy the pasteboard and uninitialize the global context record.
|
---|
156 | */
|
---|
157 | destroyPasteboard(&g_ctx.pasteboard);
|
---|
158 | g_ctx.thread = NIL_RTTHREAD;
|
---|
159 | g_ctx.pClientData = NULL;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int VBoxClipboardSvcImplConnect(PVBOXCLIPBOARDCLIENTDATA pClientData, bool fHeadless)
|
---|
163 | {
|
---|
164 | NOREF(fHeadless);
|
---|
165 | if (g_ctx.pClientData != NULL)
|
---|
166 | {
|
---|
167 | /* One client only. */
|
---|
168 | return VERR_NOT_SUPPORTED;
|
---|
169 | }
|
---|
170 |
|
---|
171 | VBoxSvcClipboardLock();
|
---|
172 |
|
---|
173 | pClientData->State.pCtx = &g_ctx;
|
---|
174 | pClientData->State.pCtx->pClientData = pClientData;
|
---|
175 |
|
---|
176 | /* Initially sync the host clipboard content with the client. */
|
---|
177 | int rc = VBoxClipboardSvcImplSync(pClientData);
|
---|
178 |
|
---|
179 | VBoxSvcClipboardUnlock();
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 |
|
---|
183 | int VBoxClipboardSvcImplSync(PVBOXCLIPBOARDCLIENTDATA pClientData)
|
---|
184 | {
|
---|
185 | /* Sync the host clipboard content with the client. */
|
---|
186 | VBoxSvcClipboardLock();
|
---|
187 | int rc = vboxClipboardChanged(pClientData->State.pCtx);
|
---|
188 | VBoxSvcClipboardUnlock();
|
---|
189 |
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 | int VBoxClipboardSvcImplDisconnect(PVBOXCLIPBOARDCLIENTDATA pClientData)
|
---|
194 | {
|
---|
195 | VBoxSvcClipboardLock();
|
---|
196 | pClientData->State.pCtx->pClientData = NULL;
|
---|
197 | VBoxSvcClipboardUnlock();
|
---|
198 |
|
---|
199 | return VINF_SUCCESS;
|
---|
200 | }
|
---|
201 |
|
---|
202 | int VBoxClipboardSvcImplFormatAnnounce(PVBOXCLIPBOARDCLIENTDATA pClientData, uint32_t u32Formats)
|
---|
203 | {
|
---|
204 | Log(("vboxClipboardFormatAnnounce u32Formats %02X\n", u32Formats));
|
---|
205 | if (u32Formats == 0)
|
---|
206 | {
|
---|
207 | /* This is just an automatism, not a genuine announcement */
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | vboxSvcClipboardReportMsg(pClientData, VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA, u32Formats);
|
---|
212 |
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Called by the HGCM clipboard subsystem when the guest wants to read the host clipboard.
|
---|
218 | *
|
---|
219 | * @param pClientData Context information about the guest VM
|
---|
220 | * @param u32Format The format that the guest would like to receive the data in
|
---|
221 | * @param pv Where to write the data to
|
---|
222 | * @param cb The size of the buffer to write the data to
|
---|
223 | * @param pcbActual Where to write the actual size of the written data
|
---|
224 | */
|
---|
225 | int VBoxClipboardSvcImplReadData(PVBOXCLIPBOARDCLIENTDATA pClientData, uint32_t u32Format,
|
---|
226 | void *pv, uint32_t cb, uint32_t *pcbActual)
|
---|
227 | {
|
---|
228 | VBoxSvcClipboardLock();
|
---|
229 |
|
---|
230 | /* Default to no data available. */
|
---|
231 | *pcbActual = 0;
|
---|
232 | int rc = readFromPasteboard(pClientData->State.pCtx->pasteboard, u32Format, pv, cb, pcbActual);
|
---|
233 |
|
---|
234 | VBoxSvcClipboardUnlock();
|
---|
235 | return rc;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Called by the HGCM clipboard subsystem when we have requested data and that data arrives.
|
---|
240 | *
|
---|
241 | * @param pClientData Context information about the guest VM
|
---|
242 | * @param pv Buffer to which the data was written
|
---|
243 | * @param cb The size of the data written
|
---|
244 | * @param u32Format The format of the data written
|
---|
245 | */
|
---|
246 | int VBoxClipboardSvcImplWriteData(PVBOXCLIPBOARDCLIENTDATA pClientData, void *pv, uint32_t cb, uint32_t u32Format)
|
---|
247 | {
|
---|
248 | VBoxSvcClipboardLock();
|
---|
249 |
|
---|
250 | writeToPasteboard(pClientData->State.pCtx->pasteboard, pv, cb, u32Format);
|
---|
251 |
|
---|
252 | VBoxSvcClipboardUnlock();
|
---|
253 |
|
---|
254 | return VINF_SUCCESS;
|
---|
255 | }
|
---|
256 |
|
---|
257 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
258 | int VBoxClipboardSvcImplURIReadDir(PVBOXCLIPBOARDCLIENTDATA pClientData, PVBOXCLIPBOARDDIRDATA pDirData)
|
---|
259 | {
|
---|
260 | RT_NOREF(pClientData, pDirData);
|
---|
261 | return VERR_NOT_IMPLEMENTED;
|
---|
262 | }
|
---|
263 |
|
---|
264 | int VBoxClipboardSvcImplURIWriteDir(PVBOXCLIPBOARDCLIENTDATA pClientData, PVBOXCLIPBOARDDIRDATA pDirData)
|
---|
265 | {
|
---|
266 | RT_NOREF(pClientData, pDirData);
|
---|
267 | return VERR_NOT_IMPLEMENTED;
|
---|
268 | }
|
---|
269 |
|
---|
270 | int VBoxClipboardSvcImplURIReadFileHdr(PVBOXCLIPBOARDCLIENTDATA pClientData, PVBOXCLIPBOARDFILEHDR pFileHdr)
|
---|
271 | {
|
---|
272 | RT_NOREF(pClientData, pFileHdr);
|
---|
273 | return VERR_NOT_IMPLEMENTED;
|
---|
274 | }
|
---|
275 |
|
---|
276 | int VBoxClipboardSvcImplURIWriteFileHdr(PVBOXCLIPBOARDCLIENTDATA pClientData, PVBOXCLIPBOARDFILEHDR pFileHdr)
|
---|
277 | {
|
---|
278 | RT_NOREF(pClientData, pFileHdr);
|
---|
279 | return VERR_NOT_IMPLEMENTED;
|
---|
280 | }
|
---|
281 |
|
---|
282 | int VBoxClipboardSvcImplURIReadFileData(PVBOXCLIPBOARDCLIENTDATA pClientData, PVBOXCLIPBOARDFILEDATA pFileData)
|
---|
283 | {
|
---|
284 | RT_NOREF(pClientData, pFileData);
|
---|
285 | return VERR_NOT_IMPLEMENTED;
|
---|
286 | }
|
---|
287 |
|
---|
288 | int VBoxClipboardSvcImplURIWriteFileData(PVBOXCLIPBOARDCLIENTDATA pClientData, PVBOXCLIPBOARDFILEDATA pFileData)
|
---|
289 | {
|
---|
290 | RT_NOREF(pClientData, pFileData);
|
---|
291 | return VERR_NOT_IMPLEMENTED;
|
---|
292 | }
|
---|
293 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
|
---|
294 |
|
---|