VirtualBox

source: vbox/trunk/include/VBox/GuestHost/SharedClipboard.h

Last change on this file was 102815, checked in by vboxsync, 5 months ago

Shared Clipboard: Added ShClCacheSetMultiple(). bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
1/** @file
2 * Shared Clipboard - Common guest and host Code.
3 */
4
5/*
6 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef VBOX_INCLUDED_GuestHost_SharedClipboard_h
37#define VBOX_INCLUDED_GuestHost_SharedClipboard_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/critsect.h>
43#include <iprt/types.h>
44#include <iprt/list.h>
45
46/** @name VBOX_SHCL_FMT_XXX - Data formats (flags) for Shared Clipboard.
47 * @{
48 */
49/** No format set. */
50#define VBOX_SHCL_FMT_NONE 0
51/** Shared Clipboard format is an Unicode text. */
52#define VBOX_SHCL_FMT_UNICODETEXT RT_BIT(0)
53/** Shared Clipboard format is bitmap (BMP / DIB). */
54#define VBOX_SHCL_FMT_BITMAP RT_BIT(1)
55/** Shared Clipboard format is HTML. */
56#define VBOX_SHCL_FMT_HTML RT_BIT(2)
57/** Shared Clipboard format is a transfer list.
58 *
59 * When requesting (reading) data with this format, the following happens:
60 * - Acts as a beacon for transfer negotiation / handshake.
61 * - The receiving side (source) initializes a transfer locally.
62 * - The receiving side reports the transfer status (INIT) to the sending side (target).
63 * - The sending side proceeds initializing the transfer locally.
64 * - The sending side reports its transfer status (INIT) to the receiving side.
65 *
66 * Note: When receiving an error via a transfer status, the transfer must be destroyed and
67 * is considered as being failed wholesale.
68 *
69 * @since 7.1
70 */
71#define VBOX_SHCL_FMT_URI_LIST RT_BIT(3)
72/** Shared Clipboard format valid mask. */
73#define VBOX_SHCL_FMT_VALID_MASK 0xf
74/** Maximum number of Shared Clipboard formats.
75 * This currently ASSUMES that there are no gaps in the bit mask. */
76#define VBOX_SHCL_FMT_MAX VBOX_SHCL_FMT_VALID_MASK
77/** @} */
78
79
80/** A single Shared Clipboard format (VBOX_SHCL_FMT_XXX). */
81typedef uint32_t SHCLFORMAT;
82/** Pointer to a single Shared Clipboard format (VBOX_SHCL_FMT_XXX). */
83typedef SHCLFORMAT *PSHCLFORMAT;
84
85/** Bit map (flags) of Shared Clipboard formats (VBOX_SHCL_FMT_XXX). */
86typedef uint32_t SHCLFORMATS;
87/** Pointer to a bit map of Shared Clipboard formats (VBOX_SHCL_FMT_XXX). */
88typedef SHCLFORMATS *PSHCLFORMATS;
89
90/** Defines the default timeout (in ms) to use for clipboard single wait operations.
91 * Not being used for lenghtly operations as a whole!
92 * Note: Don't set this too high, otherwise the UI feels sluggish. */
93#define SHCL_TIMEOUT_DEFAULT_MS RT_MS_5SEC
94
95
96/**
97 * Shared Clipboard transfer direction.
98 */
99typedef enum SHCLTRANSFERDIR
100{
101 /** Unknown transfer directory. */
102 SHCLTRANSFERDIR_UNKNOWN = 0,
103 /** Read transfer (from source). */
104 SHCLTRANSFERDIR_FROM_REMOTE,
105 /** Write transfer (to target). */
106 SHCLTRANSFERDIR_TO_REMOTE,
107 /** The usual 32-bit hack. */
108 SHCLTRANSFERDIR_32BIT_HACK = 0x7fffffff
109} SHCLTRANSFERDIR;
110/** Pointer to a shared clipboard transfer direction. */
111typedef SHCLTRANSFERDIR *PSHCLTRANSFERDIR;
112
113
114/**
115 * Shared Clipboard data read request.
116 */
117typedef struct SHCLDATAREQ
118{
119 /** In which format the data needs to be sent. */
120 SHCLFORMAT uFmt;
121 /** Read flags; currently unused. */
122 uint32_t fFlags;
123 /** Maximum data (in byte) can be sent. */
124 uint32_t cbSize;
125} SHCLDATAREQ;
126/** Pointer to a shared clipboard data request. */
127typedef SHCLDATAREQ *PSHCLDATAREQ;
128
129/**
130 * Shared Clipboard event payload (optional).
131 */
132typedef struct SHCLEVENTPAYLOAD
133{
134 /** Payload ID; currently unused. */
135 uint32_t uID;
136 /** Size (in bytes) of actual payload data. */
137 uint32_t cbData;
138 /** Pointer to actual payload data. */
139 void *pvData;
140} SHCLEVENTPAYLOAD;
141/** Pointer to a shared clipboard event payload. */
142typedef SHCLEVENTPAYLOAD *PSHCLEVENTPAYLOAD;
143
144/** A shared clipboard event source ID. */
145typedef uint16_t SHCLEVENTSOURCEID;
146/** Pointer to a shared clipboard event source ID. */
147typedef SHCLEVENTSOURCEID *PSHCLEVENTSOURCEID;
148
149/** A shared clipboard session ID. */
150typedef uint16_t SHCLSESSIONID;
151/** Pointer to a shared clipboard session ID. */
152typedef SHCLSESSIONID *PSHCLSESSIONID;
153/** NIL shared clipboard session ID. */
154#define NIL_SHCLSESSIONID UINT16_MAX
155
156/** A shared clipboard transfer ID. */
157typedef uint16_t SHCLTRANSFERID;
158/** Pointer to a shared clipboard transfer ID. */
159typedef SHCLTRANSFERID *PSHCLTRANSFERID;
160/** NIL shared clipboardtransfer ID. */
161#define NIL_SHCLTRANSFERID UINT16_MAX
162
163/** A shared clipboard event ID. */
164typedef uint32_t SHCLEVENTID;
165/** Pointer to a shared clipboard event source ID. */
166typedef SHCLEVENTID *PSHCLEVENTID;
167/** NIL shared clipboard event ID. */
168#define NIL_SHCLEVENTID UINT32_MAX
169
170/** Pointer to a shared clipboard event source.
171 * Forward declaration, needed for SHCLEVENT. */
172typedef struct SHCLEVENTSOURCE *PSHCLEVENTSOURCE;
173
174/**
175 * Shared Clipboard event.
176 */
177typedef struct SHCLEVENT
178{
179 /** List node. */
180 RTLISTNODE Node;
181 /** Parent (source) this event belongs to. */
182 PSHCLEVENTSOURCE pParent;
183 /** The event's ID, for self-reference. */
184 SHCLEVENTID idEvent;
185 /** Reference count to this event. */
186 uint32_t cRefs;
187 /** Event semaphore for signalling the event. */
188 RTSEMEVENTMULTI hEvtMulSem;
189 /** Payload to this event, optional (NULL). */
190 PSHCLEVENTPAYLOAD pPayload;
191 /** Result code (IPRT-style) to assign. */
192 int rc;
193} SHCLEVENT;
194/** Pointer to a shared clipboard event. */
195typedef SHCLEVENT *PSHCLEVENT;
196
197/**
198 * Shared Clipboard event source.
199 *
200 * Each event source maintains an own counter for events, so that it can be used
201 * in different contexts.
202 */
203typedef struct SHCLEVENTSOURCE
204{
205 /** The event source ID. */
206 SHCLEVENTSOURCEID uID;
207 /** Critical section for serializing access. */
208 RTCRITSECT CritSect;
209 /** Next upcoming event ID. */
210 SHCLEVENTID idNextEvent;
211 /** List of events (PSHCLEVENT). */
212 RTLISTANCHOR lstEvents;
213} SHCLEVENTSOURCE;
214
215/** @name Shared Clipboard data payload functions.
216 * @{
217 */
218int ShClPayloadInit(uint32_t uID, void *pvData, uint32_t cbData, PSHCLEVENTPAYLOAD *ppPayload);
219int ShClPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData, PSHCLEVENTPAYLOAD *ppPayload);
220void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload);
221/** @} */
222
223/** @name Shared Clipboard event source functions.
224 * @{
225 */
226int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID idEvtSrc);
227int ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource);
228void ShClEventSourceReset(PSHCLEVENTSOURCE pSource);
229int ShClEventSourceGenerateAndRegisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT *ppEvent);
230PSHCLEVENT ShClEventSourceGetFromId(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent);
231PSHCLEVENT ShClEventSourceGetLast(PSHCLEVENTSOURCE pSource);
232/** @} */
233
234/** @name Shared Clipboard event functions.
235 * @{
236 */
237uint32_t ShClEventGetRefs(PSHCLEVENT pEvent);
238uint32_t ShClEventRetain(PSHCLEVENT pEvent);
239uint32_t ShClEventRelease(PSHCLEVENT pEvent);
240int ShClEventSignalEx(PSHCLEVENT pEvent, int rc, PSHCLEVENTPAYLOAD pPayload);
241int ShClEventSignal(PSHCLEVENT pEvent, PSHCLEVENTPAYLOAD pPayload);
242int ShClEventWait(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, PSHCLEVENTPAYLOAD *ppPayload);
243int ShClEventWaitEx(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, int *pRc, PSHCLEVENTPAYLOAD *ppPayload);
244/** @} */
245
246/**
247 * Shared Clipboard transfer source type.
248 * @note Part of saved state!
249 */
250typedef enum SHCLSOURCE
251{
252 /** Invalid source type. */
253 SHCLSOURCE_INVALID = 0,
254 /** Source is local. */
255 SHCLSOURCE_LOCAL,
256 /** Source is remote. */
257 SHCLSOURCE_REMOTE,
258 /** The usual 32-bit hack. */
259 SHCLSOURCE_32BIT_HACK = 0x7fffffff
260} SHCLSOURCE;
261
262/** @name Shared Clipboard caching.
263 * @{
264 */
265/**
266 * A single Shared Clipboard cache entry.
267 *
268 * One entry marks exactly one clipboard format at a time.
269 */
270typedef struct _SHCLCACHEENTRY
271{
272 /** Entry data.
273 * Acts as a beacon for entry validation. */
274 void *pvData;
275 /** Entry data size (in bytes). */
276 size_t cbData;
277} SHCLCACHEENTRY;
278/** Pointer to a Shared Clipboard cache entry. */
279typedef SHCLCACHEENTRY *PSHCLCACHEENTRY;
280
281/**
282 * A (very simple) Shared Clipboard cache.
283 */
284typedef struct _SHCLCACHE
285{
286 /** Entries for all formats.
287 * Right now this is static to keep it simple. */
288 SHCLCACHEENTRY aEntries[VBOX_SHCL_FMT_MAX];
289} SHCLCACHE;
290/** Pointer to a Shared Clipboard cache. */
291typedef SHCLCACHE *PSHCLCACHE;
292
293void ShClCacheEntryGet(PSHCLCACHEENTRY pCacheEntry, void **pvData, size_t *pcbData);
294
295void ShClCacheInit(PSHCLCACHE pCache);
296void ShClCacheDestroy(PSHCLCACHE pCache);
297void ShClCacheInvalidate(PSHCLCACHE pCache);
298void ShClCacheInvalidateEntry(PSHCLCACHE pCache, SHCLFORMAT uFmt);
299PSHCLCACHEENTRY ShClCacheGet(PSHCLCACHE pCache, SHCLFORMAT uFmt);
300int ShClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData);
301int ShClCacheSetMultiple(PSHCLCACHE pCache, SHCLFORMATS uFmts, const void *pvData, size_t cbData);
302/** @} */
303
304/** Opaque data structure for the X11/VBox frontend/glue code.
305 * @{ */
306struct SHCLCONTEXT;
307typedef struct SHCLCONTEXT SHCLCONTEXT;
308/** @} */
309/** Pointer to opaque data structure the X11/VBox frontend/glue code. */
310typedef SHCLCONTEXT *PSHCLCONTEXT;
311
312/**
313 * @name Shared Clipboard callback table.
314 *
315 * This table gets used by
316 * - the backends on the host (where required)
317 * - guest side implementations (e.g. VBoxClient)
318 * - by the underlying core code (e.g. X11 backend -> X11 common code -> callback)
319 *
320 * Some clipboard mechanisms (e.g. X11) require asynchronous and/or event-driven handling
321 * of clipboard data, making it hard to control our program flow when testing stuff.
322 *
323 * So overriding required callbacks on runtime for testing purposes makes this approach much
324 * more flexible without implementing separate code paths for production code and test units.
325 *
326 * @{
327 */
328typedef struct _SHCLCALLBACKS
329{
330 /**
331 * Callback for reporting supported clipoard formats of current clipboard data.
332 *
333 * @note On X11:
334 * Runs in Xt event thread for the X11 code.
335 *
336 * @returns VBox status code.
337 * @param pCtx Opaque context pointer for the glue code.
338 * @param fFormats The formats available.
339 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
340 * Optional and can be NULL.
341 */
342 DECLCALLBACKMEMBER(int, pfnReportFormats, (PSHCLCONTEXT pCtx, SHCLFORMATS fFormats, void *pvUser));
343
344 /**
345 * Callback for reading data from the clipboard.
346 * Optional and can be NULL.
347 *
348 * @note Used for testing X11 clipboard code.
349 *
350 * @returns VBox status code.
351 * @param pCtx Opaque context pointer for the glue code.
352 * @param uFmt The format in which the data should be read
353 * (VBOX_SHCL_FMT_XXX).
354 * @param ppv Returns an allocated buffer with data from on success.
355 * Needs to be free'd with RTMemFree() by the caller.
356 * @param pcb Returns the amount of data read (in bytes) on success.
357 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
358 * Optional and can be NULL.
359 */
360 DECLCALLBACKMEMBER(int, pfnOnClipboardRead, (PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, size_t *pcb, void *pvUser));
361
362 /**
363 * Callback for writing data to the clipboard.
364 * Optional and can be NULL.
365 *
366 * @note Used for testing X11 clipboard code.
367 *
368 * @returns VBox status code.
369 * @param pCtx Opaque context pointer for the glue code.
370 * @param uFmt The format in which the data should be written as
371 * (VBOX_SHCL_FMT_XXX).
372 * @param pv The clipboard data to write.
373 * @param cb The size of the data in @a pv.
374 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
375 * Optional and can be NULL.
376 */
377 DECLCALLBACKMEMBER(int, pfnOnClipboardWrite, (PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void *pv, size_t cb, void *pvUser));
378
379 /**
380 * Callback for requesting clipboard data from the source.
381 *
382 * @note On X11:
383 * The function will be invoked for every single target the clipboard requests.
384 * Runs in Xt event thread for the X11 code.
385 *
386 * @returns VBox status code.
387 * @retval VERR_NO_DATA if no data available.
388 * @param pCtx Opaque context pointer for the glue code.
389 * @param uFmt The format in which the data should be transferred
390 * (VBOX_SHCL_FMT_XXX).
391 * @param ppv Returns an allocated buffer with data read from the guest on success.
392 * Needs to be free'd with RTMemFree() by the caller.
393 * @param pcb Returns the amount of data read (in bytes) on success.
394 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
395 * Optional and can be NULL.
396 * On X11: Of type PSHCLX11READDATAREQ; We RTMemFree() this in this function.
397 */
398 DECLCALLBACKMEMBER(int, pfnOnRequestDataFromSource, (PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser));
399
400 /**
401 * Callback for sending clipboard data to the destination.
402 *
403 * @returns VBox status code.
404 * @param pCtx Opaque context pointer for the glue code.
405 * @param pv The clipboard data returned if the request succeeded.
406 * @param cb The size of the data in @a pv.
407 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
408 * Optional and can be NUL
409 * On X11: Of type PSHCLX11READDATAREQ.
410 */
411 DECLCALLBACKMEMBER(int, pfnOnSendDataToDest, (PSHCLCONTEXT pCtx, void *pv, uint32_t cb, void *pvUser));
412} SHCLCALLBACKS;
413/** Pointer to a Shared Clipboard callback table. */
414typedef SHCLCALLBACKS *PSHCLCALLBACKS;
415/** @} */
416
417#endif /* !VBOX_INCLUDED_GuestHost_SharedClipboard_h */
418
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use