1 | /* $Id: darwin.cpp 7117 2008-02-25 15:54:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard: Mac OS X host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 innotek GmbH
|
---|
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 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
19 |
|
---|
20 | #include <iprt/assert.h>
|
---|
21 | #include <iprt/thread.h>
|
---|
22 |
|
---|
23 | #include "VBoxClipboard.h"
|
---|
24 | /* We do the work in a separate cpp file because
|
---|
25 | * of the conflicting typedef "OSType". This is
|
---|
26 | * defined in Carbon and in VBox/ostypes.h also. */
|
---|
27 | #include "darwin-pasteboard.h"
|
---|
28 |
|
---|
29 | /** Global clipboard context information */
|
---|
30 | struct _VBOXCLIPBOARDCONTEXT
|
---|
31 | {
|
---|
32 | /** We have a separate thread to poll for new clipboard content */
|
---|
33 | RTTHREAD thread;
|
---|
34 | bool volatile fTerminate;
|
---|
35 |
|
---|
36 | /** The reference to the current pasteboard */
|
---|
37 | PasteboardRef pasteboard;
|
---|
38 |
|
---|
39 | VBOXCLIPBOARDCLIENTDATA *pClient;
|
---|
40 | };
|
---|
41 |
|
---|
42 | /** Only one client is supported. There seems to be no need for more clients. */
|
---|
43 | static VBOXCLIPBOARDCONTEXT g_ctx;
|
---|
44 |
|
---|
45 | int vboxClipboardChanged (VBOXCLIPBOARDCONTEXT *pCtx)
|
---|
46 | {
|
---|
47 | if (pCtx->pClient == NULL)
|
---|
48 | return VINF_SUCCESS;
|
---|
49 |
|
---|
50 | uint32_t fFormats = 0;
|
---|
51 | /* Retrieve the formats currently in the clipboard and supported by vbox */
|
---|
52 | int rc = queryPasteboardFormats (pCtx->pasteboard, fFormats);
|
---|
53 |
|
---|
54 | if (fFormats > 0)
|
---|
55 | {
|
---|
56 | vboxSvcClipboardReportMsg (pCtx->pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS, fFormats);
|
---|
57 | Log (("vboxClipboardChanged fFormats %02X\n", fFormats));
|
---|
58 | }
|
---|
59 |
|
---|
60 | return rc;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int vboxClipboardThread (RTTHREAD self, void *pvUser)
|
---|
64 | {
|
---|
65 | Log (("vboxClipboardThread: starting clipboard thread\n"));
|
---|
66 |
|
---|
67 | AssertReturn (VALID_PTR (pvUser), VERR_INVALID_PARAMETER);
|
---|
68 |
|
---|
69 | VBOXCLIPBOARDCONTEXT *pCtx = static_cast <VBOXCLIPBOARDCONTEXT*> (pvUser);
|
---|
70 |
|
---|
71 | while (!pCtx->fTerminate)
|
---|
72 | {
|
---|
73 | vboxClipboardChanged (pCtx);
|
---|
74 | /* Sleep for 200 msecs before next poll */
|
---|
75 | RTThreadSleep (200);
|
---|
76 | }
|
---|
77 |
|
---|
78 | Log (("vboxClipboardThread: clipboard thread terminated successfully with return code %Vrc\n", VINF_SUCCESS));
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Public platform dependent functions.
|
---|
84 | */
|
---|
85 |
|
---|
86 | /** Initialise the host side of the shared clipboard - called by the hgcm layer. */
|
---|
87 | int vboxClipboardInit (void)
|
---|
88 | {
|
---|
89 | Log (("vboxClipboardInit\n"));
|
---|
90 |
|
---|
91 | int rc = VINF_SUCCESS;
|
---|
92 |
|
---|
93 | g_ctx.fTerminate = false;
|
---|
94 |
|
---|
95 | rc = initPasteboard (&g_ctx.pasteboard);
|
---|
96 |
|
---|
97 | rc = RTThreadCreate (&g_ctx.thread, vboxClipboardThread, &g_ctx, 0,
|
---|
98 | RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
|
---|
99 |
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Terminate the host side of the shared clipboard - called by the hgcm layer. */
|
---|
104 | void vboxClipboardDestroy (void)
|
---|
105 | {
|
---|
106 | Log (("vboxClipboardDestroy\n"));
|
---|
107 |
|
---|
108 | g_ctx.fTerminate = true;
|
---|
109 |
|
---|
110 | destroyPasteboard (&g_ctx.pasteboard);
|
---|
111 |
|
---|
112 | /* Wait for the clipboard thread to terminate. */
|
---|
113 | RTThreadWait (g_ctx.thread, RT_INDEFINITE_WAIT, NULL);
|
---|
114 |
|
---|
115 | g_ctx.thread = NIL_RTTHREAD;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Enable the shared clipboard - called by the hgcm clipboard subsystem.
|
---|
120 | *
|
---|
121 | * @param pClient Structure containing context information about the guest system
|
---|
122 | * @returns RT status code
|
---|
123 | */
|
---|
124 | int vboxClipboardConnect (VBOXCLIPBOARDCLIENTDATA *pClient)
|
---|
125 | {
|
---|
126 | if (g_ctx.pClient != NULL)
|
---|
127 | {
|
---|
128 | /* One client only. */
|
---|
129 | return VERR_NOT_SUPPORTED;
|
---|
130 | }
|
---|
131 |
|
---|
132 | pClient->pCtx = &g_ctx;
|
---|
133 | pClient->pCtx->pClient = pClient;
|
---|
134 |
|
---|
135 | /* Initially sync the host clipboard content with the client. */
|
---|
136 | int rc = vboxClipboardSync (pClient);
|
---|
137 |
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Synchronise the contents of the host clipboard with the guest, called by the HGCM layer
|
---|
143 | * after a save and restore of the guest.
|
---|
144 | */
|
---|
145 | int vboxClipboardSync (VBOXCLIPBOARDCLIENTDATA *pClient)
|
---|
146 | {
|
---|
147 | /* Sync the host clipboard content with the client. */
|
---|
148 | int rc = vboxClipboardChanged (pClient->pCtx);
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Shut down the shared clipboard subsystem and "disconnect" the guest.
|
---|
155 | */
|
---|
156 | void vboxClipboardDisconnect (VBOXCLIPBOARDCLIENTDATA * /* pClient */)
|
---|
157 | {
|
---|
158 | Log (("vboxClipboardDisconnect\n"));
|
---|
159 |
|
---|
160 | g_ctx.pClient = NULL;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * The guest is taking possession of the shared clipboard. Called by the HGCM clipboard
|
---|
165 | * subsystem.
|
---|
166 | *
|
---|
167 | * @param pClient Context data for the guest system
|
---|
168 | * @param u32Formats Clipboard formats the the guest is offering
|
---|
169 | */
|
---|
170 | void vboxClipboardFormatAnnounce (VBOXCLIPBOARDCLIENTDATA * /* pClient */,
|
---|
171 | uint32_t u32Formats)
|
---|
172 | {
|
---|
173 | Log (("vboxClipboardFormatAnnounce u32Formats %02X\n", u32Formats));
|
---|
174 | if (u32Formats == 0)
|
---|
175 | {
|
---|
176 | /* This is just an automatism, not a genuine anouncement */
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | vboxSvcClipboardReportMsg (g_ctx.pClient, VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA,
|
---|
181 | u32Formats);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Called by the HGCM clipboard subsystem when the guest wants to read the host clipboard.
|
---|
186 | *
|
---|
187 | * @param pClient Context information about the guest VM
|
---|
188 | * @param u32Format The format that the guest would like to receive the data in
|
---|
189 | * @param pv Where to write the data to
|
---|
190 | * @param cb The size of the buffer to write the data to
|
---|
191 | * @param pcbActual Where to write the actual size of the written data
|
---|
192 | */
|
---|
193 | int vboxClipboardReadData (VBOXCLIPBOARDCLIENTDATA * /* pClient */, uint32_t u32Format,
|
---|
194 | void *pv, uint32_t cb, uint32_t * pcbActual)
|
---|
195 | {
|
---|
196 | /* Default to no data available. */
|
---|
197 | *pcbActual = 0;
|
---|
198 | int rc = readFromPasteboard (g_ctx.pasteboard, u32Format, pv, cb, pcbActual);
|
---|
199 |
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Called by the HGCM clipboard subsystem when we have requested data and that data arrives.
|
---|
205 | *
|
---|
206 | * @param pClient Context information about the guest VM
|
---|
207 | * @param pv Buffer to which the data was written
|
---|
208 | * @param cb The size of the data written
|
---|
209 | * @param u32Format The format of the data written
|
---|
210 | */
|
---|
211 | void vboxClipboardWriteData (VBOXCLIPBOARDCLIENTDATA * /* pClient */, void *pv,
|
---|
212 | uint32_t cb, uint32_t u32Format)
|
---|
213 | {
|
---|
214 | writeToPasteboard (g_ctx.pasteboard, pv, cb, u32Format);
|
---|
215 | }
|
---|