1 | /* $Id: darwin.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard: Mac OS X host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
23 |
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/asm.h>
|
---|
26 | #include <iprt/thread.h>
|
---|
27 |
|
---|
28 | #include "VBoxClipboard.h"
|
---|
29 | #include "darwin-pasteboard.h"
|
---|
30 |
|
---|
31 | /** Global clipboard context information */
|
---|
32 | struct _VBOXCLIPBOARDCONTEXT
|
---|
33 | {
|
---|
34 | /** We have a separate thread to poll for new clipboard content */
|
---|
35 | RTTHREAD thread;
|
---|
36 | bool volatile fTerminate;
|
---|
37 |
|
---|
38 | /** The reference to the current pasteboard */
|
---|
39 | PasteboardRef pasteboard;
|
---|
40 |
|
---|
41 | VBOXCLIPBOARDCLIENTDATA *pClient;
|
---|
42 | };
|
---|
43 |
|
---|
44 | /** Only one client is supported. There seems to be no need for more clients. */
|
---|
45 | static VBOXCLIPBOARDCONTEXT g_ctx;
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Checks if something is present on the clipboard and calls vboxSvcClipboardReportMsg.
|
---|
50 | *
|
---|
51 | * @returns IPRT status code (ignored).
|
---|
52 | * @param pCtx The context.
|
---|
53 | */
|
---|
54 | static int vboxClipboardChanged (VBOXCLIPBOARDCONTEXT *pCtx)
|
---|
55 | {
|
---|
56 | if (pCtx->pClient == NULL)
|
---|
57 | return VINF_SUCCESS;
|
---|
58 |
|
---|
59 | uint32_t fFormats = 0;
|
---|
60 | bool fChanged = false;
|
---|
61 | /* Retrieve the formats currently in the clipboard and supported by vbox */
|
---|
62 | int rc = queryNewPasteboardFormats (pCtx->pasteboard, &fFormats, &fChanged);
|
---|
63 | if (RT_SUCCESS (rc) && fChanged)
|
---|
64 | {
|
---|
65 | vboxSvcClipboardReportMsg (pCtx->pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, fFormats);
|
---|
66 | Log (("vboxClipboardChanged fFormats %02X\n", fFormats));
|
---|
67 | }
|
---|
68 |
|
---|
69 | return rc;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * The poller thread.
|
---|
75 | *
|
---|
76 | * This thread will check for the arrival of new data on the clipboard.
|
---|
77 | *
|
---|
78 | * @returns VINF_SUCCESS (not used).
|
---|
79 | * @param Thread Our thread handle.
|
---|
80 | * @param pvUser Pointer to the VBOXCLIPBOARDCONTEXT structure.
|
---|
81 | *
|
---|
82 | */
|
---|
83 | static int vboxClipboardThread (RTTHREAD ThreadSelf, void *pvUser)
|
---|
84 | {
|
---|
85 | Log (("vboxClipboardThread: starting clipboard thread\n"));
|
---|
86 |
|
---|
87 | AssertPtrReturn (pvUser, VERR_INVALID_PARAMETER);
|
---|
88 | VBOXCLIPBOARDCONTEXT *pCtx = (VBOXCLIPBOARDCONTEXT *) pvUser;
|
---|
89 |
|
---|
90 | while (!pCtx->fTerminate)
|
---|
91 | {
|
---|
92 | /* call this behind the lock because we don't know if the api is
|
---|
93 | thread safe and in any case we're calling several methods. */
|
---|
94 | vboxSvcClipboardLock();
|
---|
95 | vboxClipboardChanged (pCtx);
|
---|
96 | vboxSvcClipboardUnlock();
|
---|
97 |
|
---|
98 | /* Sleep for 200 msecs before next poll */
|
---|
99 | RTThreadUserWait (ThreadSelf, 200);
|
---|
100 | }
|
---|
101 |
|
---|
102 | Log (("vboxClipboardThread: clipboard thread terminated successfully with return code %Rrc\n", VINF_SUCCESS));
|
---|
103 | return VINF_SUCCESS;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Public platform dependent functions.
|
---|
108 | */
|
---|
109 |
|
---|
110 | /** Initialise the host side of the shared clipboard - called by the hgcm layer. */
|
---|
111 | int vboxClipboardInit (void)
|
---|
112 | {
|
---|
113 | Log (("vboxClipboardInit\n"));
|
---|
114 |
|
---|
115 | g_ctx.fTerminate = false;
|
---|
116 |
|
---|
117 | int rc = initPasteboard (&g_ctx.pasteboard);
|
---|
118 | AssertRCReturn (rc, rc);
|
---|
119 |
|
---|
120 | rc = RTThreadCreate (&g_ctx.thread, vboxClipboardThread, &g_ctx, 0,
|
---|
121 | RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
|
---|
122 | if (RT_FAILURE (rc))
|
---|
123 | {
|
---|
124 | g_ctx.thread = NIL_RTTHREAD;
|
---|
125 | destroyPasteboard (&g_ctx.pasteboard);
|
---|
126 | }
|
---|
127 |
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Terminate the host side of the shared clipboard - called by the hgcm layer. */
|
---|
132 | void vboxClipboardDestroy (void)
|
---|
133 | {
|
---|
134 | Log (("vboxClipboardDestroy\n"));
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Signal the termination of the polling thread and wait for it to respond.
|
---|
138 | */
|
---|
139 | ASMAtomicWriteBool (&g_ctx.fTerminate, true);
|
---|
140 | int rc = RTThreadUserSignal (g_ctx.thread);
|
---|
141 | AssertRC (rc);
|
---|
142 | rc = RTThreadWait (g_ctx.thread, RT_INDEFINITE_WAIT, NULL);
|
---|
143 | AssertRC (rc);
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Destroy the pasteboard and uninitialize the global context record.
|
---|
147 | */
|
---|
148 | destroyPasteboard (&g_ctx.pasteboard);
|
---|
149 | g_ctx.thread = NIL_RTTHREAD;
|
---|
150 | g_ctx.pClient = NULL;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Enable the shared clipboard - called by the hgcm clipboard subsystem.
|
---|
155 | *
|
---|
156 | * @param pClient Structure containing context information about the guest system
|
---|
157 | * @returns RT status code
|
---|
158 | */
|
---|
159 | int vboxClipboardConnect (VBOXCLIPBOARDCLIENTDATA *pClient)
|
---|
160 | {
|
---|
161 | if (g_ctx.pClient != NULL)
|
---|
162 | {
|
---|
163 | /* One client only. */
|
---|
164 | return VERR_NOT_SUPPORTED;
|
---|
165 | }
|
---|
166 |
|
---|
167 | vboxSvcClipboardLock();
|
---|
168 |
|
---|
169 | pClient->pCtx = &g_ctx;
|
---|
170 | pClient->pCtx->pClient = pClient;
|
---|
171 |
|
---|
172 | /* Initially sync the host clipboard content with the client. */
|
---|
173 | int rc = vboxClipboardSync (pClient);
|
---|
174 |
|
---|
175 | vboxSvcClipboardUnlock();
|
---|
176 | return rc;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Synchronise the contents of the host clipboard with the guest, called by the HGCM layer
|
---|
181 | * after a save and restore of the guest.
|
---|
182 | */
|
---|
183 | int vboxClipboardSync (VBOXCLIPBOARDCLIENTDATA *pClient)
|
---|
184 | {
|
---|
185 | /* Sync the host clipboard content with the client. */
|
---|
186 | vboxSvcClipboardLock();
|
---|
187 | int rc = vboxClipboardChanged (pClient->pCtx);
|
---|
188 | vboxSvcClipboardUnlock();
|
---|
189 |
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Shut down the shared clipboard subsystem and "disconnect" the guest.
|
---|
195 | */
|
---|
196 | void vboxClipboardDisconnect (VBOXCLIPBOARDCLIENTDATA *pClient)
|
---|
197 | {
|
---|
198 | Log (("vboxClipboardDisconnect\n"));
|
---|
199 |
|
---|
200 | vboxSvcClipboardLock();
|
---|
201 | pClient->pCtx->pClient = NULL;
|
---|
202 | vboxSvcClipboardUnlock();
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * The guest is taking possession of the shared clipboard. Called by the HGCM clipboard
|
---|
207 | * subsystem.
|
---|
208 | *
|
---|
209 | * @param pClient Context data for the guest system
|
---|
210 | * @param u32Formats Clipboard formats the the guest is offering
|
---|
211 | */
|
---|
212 | void vboxClipboardFormatAnnounce (VBOXCLIPBOARDCLIENTDATA *pClient, uint32_t u32Formats)
|
---|
213 | {
|
---|
214 | Log (("vboxClipboardFormatAnnounce u32Formats %02X\n", u32Formats));
|
---|
215 | if (u32Formats == 0)
|
---|
216 | {
|
---|
217 | /* This is just an automatism, not a genuine anouncement */
|
---|
218 | return;
|
---|
219 | }
|
---|
220 |
|
---|
221 | vboxSvcClipboardReportMsg (pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA,
|
---|
222 | u32Formats);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Called by the HGCM clipboard subsystem when the guest wants to read the host clipboard.
|
---|
227 | *
|
---|
228 | * @param pClient Context information about the guest VM
|
---|
229 | * @param u32Format The format that the guest would like to receive the data in
|
---|
230 | * @param pv Where to write the data to
|
---|
231 | * @param cb The size of the buffer to write the data to
|
---|
232 | * @param pcbActual Where to write the actual size of the written data
|
---|
233 | */
|
---|
234 | int vboxClipboardReadData (VBOXCLIPBOARDCLIENTDATA *pClient, uint32_t u32Format,
|
---|
235 | void *pv, uint32_t cb, uint32_t * pcbActual)
|
---|
236 | {
|
---|
237 | vboxSvcClipboardLock();
|
---|
238 |
|
---|
239 | /* Default to no data available. */
|
---|
240 | *pcbActual = 0;
|
---|
241 | int rc = readFromPasteboard (pClient->pCtx->pasteboard, u32Format, pv, cb, pcbActual);
|
---|
242 |
|
---|
243 | vboxSvcClipboardUnlock();
|
---|
244 | return rc;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Called by the HGCM clipboard subsystem when we have requested data and that data arrives.
|
---|
249 | *
|
---|
250 | * @param pClient Context information about the guest VM
|
---|
251 | * @param pv Buffer to which the data was written
|
---|
252 | * @param cb The size of the data written
|
---|
253 | * @param u32Format The format of the data written
|
---|
254 | */
|
---|
255 | void vboxClipboardWriteData (VBOXCLIPBOARDCLIENTDATA *pClient, void *pv,
|
---|
256 | uint32_t cb, uint32_t u32Format)
|
---|
257 | {
|
---|
258 | vboxSvcClipboardLock();
|
---|
259 |
|
---|
260 | writeToPasteboard (pClient->pCtx->pasteboard, pv, cb, u32Format);
|
---|
261 |
|
---|
262 | vboxSvcClipboardUnlock();
|
---|
263 | }
|
---|