VirtualBox

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

Last change on this file since 100451 was 100451, checked in by vboxsync, 17 months ago

Shared Clipboard: Lowered the default timeout from 30s to 5s, as the UI can feel sluggish on Windows otherwise. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 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} SHCLEVENT;
192/** Pointer to a shared clipboard event. */
193typedef SHCLEVENT *PSHCLEVENT;
194
195/**
196 * Shared Clipboard event source.
197 *
198 * Each event source maintains an own counter for events, so that it can be used
199 * in different contexts.
200 */
201typedef struct SHCLEVENTSOURCE
202{
203 /** The event source ID. */
204 SHCLEVENTSOURCEID uID;
205 /** Critical section for serializing access. */
206 RTCRITSECT CritSect;
207 /** Next upcoming event ID. */
208 SHCLEVENTID idNextEvent;
209 /** List of events (PSHCLEVENT). */
210 RTLISTANCHOR lstEvents;
211} SHCLEVENTSOURCE;
212
213/** @name Shared Clipboard data payload functions.
214 * @{
215 */
216int ShClPayloadInit(uint32_t uID, void *pvData, uint32_t cbData, PSHCLEVENTPAYLOAD *ppPayload);
217int ShClPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData, PSHCLEVENTPAYLOAD *ppPayload);
218void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload);
219/** @} */
220
221/** @name Shared Clipboard event source functions.
222 * @{
223 */
224int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID idEvtSrc);
225int ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource);
226void ShClEventSourceReset(PSHCLEVENTSOURCE pSource);
227int ShClEventSourceGenerateAndRegisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT *ppEvent);
228PSHCLEVENT ShClEventSourceGetFromId(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent);
229PSHCLEVENT ShClEventSourceGetLast(PSHCLEVENTSOURCE pSource);
230/** @} */
231
232/** @name Shared Clipboard event functions.
233 * @{
234 */
235uint32_t ShClEventGetRefs(PSHCLEVENT pEvent);
236uint32_t ShClEventRetain(PSHCLEVENT pEvent);
237uint32_t ShClEventRelease(PSHCLEVENT pEvent);
238int ShClEventSignal(PSHCLEVENT pEvent, PSHCLEVENTPAYLOAD pPayload);
239int ShClEventWait(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, PSHCLEVENTPAYLOAD *ppPayload);
240/** @} */
241
242/**
243 * Shared Clipboard transfer source type.
244 * @note Part of saved state!
245 */
246typedef enum SHCLSOURCE
247{
248 /** Invalid source type. */
249 SHCLSOURCE_INVALID = 0,
250 /** Source is local. */
251 SHCLSOURCE_LOCAL,
252 /** Source is remote. */
253 SHCLSOURCE_REMOTE,
254 /** The usual 32-bit hack. */
255 SHCLSOURCE_32BIT_HACK = 0x7fffffff
256} SHCLSOURCE;
257
258/** @name Shared Clipboard caching.
259 * @{
260 */
261/**
262 * A single Shared CLipboard cache entry.
263 */
264typedef struct _SHCLCACHEENTRY
265{
266 /** Entry data.
267 * Acts as a beacon for entry validation. */
268 void *pvData;
269 /** Entry data size (in bytes). */
270 size_t cbData;
271} SHCLCACHEENTRY;
272/** Pointer to a Shared Clipboard cache entry. */
273typedef SHCLCACHEENTRY *PSHCLCACHEENTRY;
274
275/**
276 * A (very simple) Shared Clipboard cache.
277 */
278typedef struct _SHCLCACHE
279{
280 /** Entries for all formats.
281 * Right now this is static to keep it simple. */
282 SHCLCACHEENTRY aEntries[VBOX_SHCL_FMT_MAX];
283} SHCLCACHE;
284/** Pointer to a Shared Clipboard cache. */
285typedef SHCLCACHE *PSHCLCACHE;
286
287void ShClCacheEntryGet(PSHCLCACHEENTRY pCacheEntry, void **pvData, size_t *pcbData);
288
289void ShClCacheInit(PSHCLCACHE pCache);
290void ShClCacheDestroy(PSHCLCACHE pCache);
291void ShClCacheInvalidate(PSHCLCACHE pCache);
292void ShClCacheInvalidateEntry(PSHCLCACHE pCache, SHCLFORMAT uFmt);
293PSHCLCACHEENTRY ShClCacheGet(PSHCLCACHE pCache, SHCLFORMAT uFmt);
294int ShClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData);
295/** @} */
296
297/** Opaque data structure for the X11/VBox frontend/glue code.
298 * @{ */
299struct SHCLCONTEXT;
300typedef struct SHCLCONTEXT SHCLCONTEXT;
301/** @} */
302/** Pointer to opaque data structure the X11/VBox frontend/glue code. */
303typedef SHCLCONTEXT *PSHCLCONTEXT;
304
305/**
306 * @name Shared Clipboard callback table.
307 *
308 * This table gets used by
309 * - the backends on the host (where required)
310 * - guest side implementations (e.g. VBoxClient)
311 * - by the underlying core code (e.g. X11 backend -> X11 common code -> callback)
312 *
313 * Some clipboard mechanisms (e.g. X11) require asynchronous and/or event-driven handling
314 * of clipboard data, making it hard to control our program flow when testing stuff.
315 *
316 * So overriding required callbacks on runtime for testing purposes makes this approach much
317 * more flexible without implementing separate code paths for production code and test units.
318 *
319 * @{
320 */
321typedef struct _SHCLCALLBACKS
322{
323 /**
324 * Callback for reporting supported clipoard formats of current clipboard data.
325 *
326 * @note On X11:
327 * Runs in Xt event thread for the X11 code.
328 *
329 * @returns VBox status code.
330 * @param pCtx Opaque context pointer for the glue code.
331 * @param fFormats The formats available.
332 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
333 * Optional and can be NULL.
334 */
335 DECLCALLBACKMEMBER(int, pfnReportFormats, (PSHCLCONTEXT pCtx, SHCLFORMATS fFormats, void *pvUser));
336
337 /**
338 * Callback for reading data from the clipboard.
339 * Optional and can be NULL.
340 *
341 * @note Used for testing X11 clipboard code.
342 *
343 * @returns VBox status code.
344 * @param pCtx Opaque context pointer for the glue code.
345 * @param uFmt The format in which the data should be read
346 * (VBOX_SHCL_FMT_XXX).
347 * @param ppv Returns an allocated buffer with data from on success.
348 * Needs to be free'd with RTMemFree() by the caller.
349 * @param pcb Returns the amount of data read (in bytes) on success.
350 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
351 * Optional and can be NULL.
352 */
353 DECLCALLBACKMEMBER(int, pfnOnClipboardRead, (PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, size_t *pcb, void *pvUser));
354
355 /**
356 * Callback for writing data to the clipboard.
357 * Optional and can be NULL.
358 *
359 * @note Used for testing X11 clipboard code.
360 *
361 * @returns VBox status code.
362 * @param pCtx Opaque context pointer for the glue code.
363 * @param uFmt The format in which the data should be written as
364 * (VBOX_SHCL_FMT_XXX).
365 * @param pv The clipboard data to write.
366 * @param cb The size of the data in @a pv.
367 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
368 * Optional and can be NULL.
369 */
370 DECLCALLBACKMEMBER(int, pfnOnClipboardWrite, (PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void *pv, size_t cb, void *pvUser));
371
372 /**
373 * Callback for requesting clipboard data from the source.
374 *
375 * @note On X11:
376 * The function will be invoked for every single target the clipboard requests.
377 * Runs in Xt event thread for the X11 code.
378 *
379 * @returns VBox status code.
380 * @retval VERR_NO_DATA if no data available.
381 * @param pCtx Opaque context pointer for the glue code.
382 * @param uFmt The format in which the data should be transferred
383 * (VBOX_SHCL_FMT_XXX).
384 * @param ppv Returns an allocated buffer with data read from the guest on success.
385 * Needs to be free'd with RTMemFree() by the caller.
386 * @param pcb Returns the amount of data read (in bytes) on success.
387 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
388 * Optional and can be NULL.
389 * On X11: Of type PSHCLX11READDATAREQ; We RTMemFree() this in this function.
390 */
391 DECLCALLBACKMEMBER(int, pfnOnRequestDataFromSource, (PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser));
392
393 /**
394 * Callback for sending clipboard data to the destination.
395 *
396 * @returns VBox status code.
397 * @param pCtx Opaque context pointer for the glue code.
398 * @param pv The clipboard data returned if the request succeeded.
399 * @param cb The size of the data in @a pv.
400 * @param pvUser Implementation-dependent pointer to data for fullfilling the request.
401 * Optional and can be NUL
402 * On X11: Of type PSHCLX11READDATAREQ.
403 */
404 DECLCALLBACKMEMBER(int, pfnOnSendDataToDest, (PSHCLCONTEXT pCtx, void *pv, uint32_t cb, void *pvUser));
405} SHCLCALLBACKS;
406/** Pointer to a Shared Clipboard callback table. */
407typedef SHCLCALLBACKS *PSHCLCALLBACKS;
408/** @} */
409
410#endif /* !VBOX_INCLUDED_GuestHost_SharedClipboard_h */
411
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette