VirtualBox

source: vbox/trunk/include/VBox/GuestHost/SharedClipboard-win.h@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1/** @file
2 * Shared Clipboard - Common Guest and Host Code, for Windows OSes.
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_win_h
37#define VBOX_INCLUDED_GuestHost_SharedClipboard_win_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/win/windows.h>
45
46#include <VBox/GuestHost/SharedClipboard.h>
47
48# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
49# include <vector>
50
51# include <iprt/cpp/ministring.h> /* For RTCString. */
52# include <iprt/win/shlobj.h> /* For DROPFILES and friends. */
53# include <VBox/com/string.h> /* For Utf8Str. */
54# include <oleidl.h>
55
56# include <VBox/GuestHost/SharedClipboard-transfers.h>
57
58using namespace com;
59# endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
60
61#ifndef WM_CLIPBOARDUPDATE
62# define WM_CLIPBOARDUPDATE 0x031D
63#endif
64
65#define SHCL_WIN_WNDCLASS_NAME "VBoxSharedClipboardClass"
66
67/** See: https://docs.microsoft.com/en-us/windows/desktop/dataxchg/html-clipboard-format
68 * Do *not* change the name, as this will break compatbility with other (legacy) applications! */
69#define SHCL_WIN_REGFMT_HTML "HTML Format"
70
71/** Default timeout (in ms) for passing down messages down the clipboard chain. */
72#define SHCL_WIN_CBCHAIN_TIMEOUT_MS 5000
73
74/** Reports clipboard formats. */
75#define SHCL_WIN_WM_REPORT_FORMATS WM_USER
76/** Reads data from the clipboard and sends it to the destination. */
77#define SHCL_WIN_WM_READ_DATA WM_USER + 1
78#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
79/** Starts a transfer on the guest.
80 * This creates the necessary IDataObject in the matching window thread. */
81# define SHCL_WIN_WM_TRANSFER_START WM_USER + 2
82#endif
83
84/* Dynamically load clipboard functions from User32.dll. */
85typedef BOOL WINAPI FNADDCLIPBOARDFORMATLISTENER(HWND);
86typedef FNADDCLIPBOARDFORMATLISTENER *PFNADDCLIPBOARDFORMATLISTENER;
87
88typedef BOOL WINAPI FNREMOVECLIPBOARDFORMATLISTENER(HWND);
89typedef FNREMOVECLIPBOARDFORMATLISTENER *PFNREMOVECLIPBOARDFORMATLISTENER;
90
91/**
92 * Structure for keeping function pointers for the new clipboard API.
93 * If the new API is not available, those function pointer are NULL.
94 */
95typedef struct _SHCLWINAPINEW
96{
97 PFNADDCLIPBOARDFORMATLISTENER pfnAddClipboardFormatListener;
98 PFNREMOVECLIPBOARDFORMATLISTENER pfnRemoveClipboardFormatListener;
99} SHCLWINAPINEW, *PSHCLWINAPINEW;
100
101/**
102 * Structure for keeping variables which are needed to drive the old clipboard API.
103 */
104typedef struct _SHCLWINAPIOLD
105{
106 /** Timer ID for the refresh timer. */
107 UINT timerRefresh;
108 /** Whether "pinging" the clipboard chain currently is in progress or not. */
109 bool fCBChainPingInProcess;
110} SHCLWINAPIOLD, *PSHCLWINAPIOLD;
111
112/**
113 * Structure for maintaining a Shared Clipboard context on Windows platforms.
114 */
115typedef struct _SHCLWINCTX
116{
117 /** Critical section to serialize access. */
118 RTCRITSECT CritSect;
119 /** Window handle of our (invisible) clipbaord window. */
120 HWND hWnd;
121 /** Window handle which is next to us in the clipboard chain. */
122 HWND hWndNextInChain;
123 /** Window handle of the clipboard owner *if* we are the owner.
124 * @todo r=bird: Ignore the misleading statement above. This is only set to
125 * NULL by the initialization code and then it's set to the clipboard owner
126 * after we announce data to the clipboard. So, essentially this will be our
127 * windows handle or NULL. End of story. */
128 HWND hWndClipboardOwnerUs;
129 /** Structure for maintaining the new clipboard API. */
130 SHCLWINAPINEW newAPI;
131 /** Structure for maintaining the old clipboard API. */
132 SHCLWINAPIOLD oldAPI;
133} SHCLWINCTX, *PSHCLWINCTX;
134
135int SharedClipboardWinOpen(HWND hWnd);
136int SharedClipboardWinClose(void);
137int SharedClipboardWinClear(void);
138
139int SharedClipboardWinCtxInit(PSHCLWINCTX pWinCtx);
140void SharedClipboardWinCtxDestroy(PSHCLWINCTX pWinCtx);
141
142int SharedClipboardWinCheckAndInitNewAPI(PSHCLWINAPINEW pAPI);
143bool SharedClipboardWinIsNewAPI(PSHCLWINAPINEW pAPI);
144
145int SharedClipboardWinDataWrite(UINT cfFormat, void *pvData, uint32_t cbData);
146
147int SharedClipboardWinChainAdd(PSHCLWINCTX pCtx);
148int SharedClipboardWinChainRemove(PSHCLWINCTX pCtx);
149VOID CALLBACK SharedClipboardWinChainPingProc(HWND hWnd, UINT uMsg, ULONG_PTR dwData, LRESULT lResult) RT_NOTHROW_DEF;
150LRESULT SharedClipboardWinChainPassToNext(PSHCLWINCTX pWinCtx, UINT msg, WPARAM wParam, LPARAM lParam);
151
152SHCLFORMAT SharedClipboardWinClipboardFormatToVBox(UINT uFormat);
153int SharedClipboardWinGetFormats(PSHCLWINCTX pCtx, PSHCLFORMATS pfFormats);
154
155#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
156int SharedClipboardWinGetRoots(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
157int SharedClipboardWinDropFilesToStringList(DROPFILES *pDropFiles, char **papszList, uint32_t *pcbList);
158#endif
159
160int SharedClipboardWinGetCFHTMLHeaderValue(const char *pszSrc, const char *pszOption, uint32_t *puValue);
161bool SharedClipboardWinIsCFHTML(const char *pszSource);
162int SharedClipboardWinConvertCFHTMLToMIME(const char *pszSource, const uint32_t cch, char **ppszOutput, uint32_t *pcbOutput);
163int SharedClipboardWinConvertMIMEToCFHTML(const char *pszSource, size_t cb, char **ppszOutput, uint32_t *pcbOutput);
164
165LRESULT SharedClipboardWinHandleWMChangeCBChain(PSHCLWINCTX pWinCtx, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
166int SharedClipboardWinHandleWMDestroy(PSHCLWINCTX pWinCtx);
167int SharedClipboardWinHandleWMRenderAllFormats(PSHCLWINCTX pWinCtx, HWND hWnd);
168int SharedClipboardWinHandleWMTimer(PSHCLWINCTX pWinCtx);
169
170int SharedClipboardWinClearAndAnnounceFormats(PSHCLWINCTX pWinCtx, SHCLFORMATS fFormats, HWND hWnd);
171#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
172int SharedClipboardWinTransferCreate(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
173void SharedClipboardWinTransferDestroy(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
174#endif
175
176# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
177class SharedClipboardTransferList;
178# ifndef FILEGROUPDESCRIPTOR
179class FILEGROUPDESCRIPTOR;
180# endif
181
182class SharedClipboardWinDataObject : public IDataObject //, public IDataObjectAsyncCapability
183{
184public:
185
186 enum Status
187 {
188 /** The object is uninitialized (not ready). */
189 Uninitialized = 0,
190 /** The object is initialized and ready to use. */
191 Initialized,
192 /** The operation has been successfully completed. */
193 Completed,
194 /** The operation has been canceled. */
195 Canceled,
196 /** An (unrecoverable) error occurred. */
197 Error
198 };
199
200public:
201
202 SharedClipboardWinDataObject(PSHCLTRANSFER pTransfer,
203 LPFORMATETC pFormatEtc = NULL, LPSTGMEDIUM pStgMed = NULL, ULONG cFormats = 0);
204 virtual ~SharedClipboardWinDataObject(void);
205
206public: /* IUnknown methods. */
207
208 STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
209 STDMETHOD_(ULONG, AddRef)(void);
210 STDMETHOD_(ULONG, Release)(void);
211
212public: /* IDataObject methods. */
213
214 STDMETHOD(GetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
215 STDMETHOD(GetDataHere)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
216 STDMETHOD(QueryGetData)(LPFORMATETC pFormatEtc);
217 STDMETHOD(GetCanonicalFormatEtc)(LPFORMATETC pFormatEct, LPFORMATETC pFormatEtcOut);
218 STDMETHOD(SetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium, BOOL fRelease);
219 STDMETHOD(EnumFormatEtc)(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc);
220 STDMETHOD(DAdvise)(LPFORMATETC pFormatEtc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection);
221 STDMETHOD(DUnadvise)(DWORD dwConnection);
222 STDMETHOD(EnumDAdvise)(IEnumSTATDATA **ppEnumAdvise);
223
224#ifdef VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC
225public: /* IDataObjectAsyncCapability methods. */
226
227 STDMETHOD(EndOperation)(HRESULT hResult, IBindCtx* pbcReserved, DWORD dwEffects);
228 STDMETHOD(GetAsyncMode)(BOOL* pfIsOpAsync);
229 STDMETHOD(InOperation)(BOOL* pfInAsyncOp);
230 STDMETHOD(SetAsyncMode)(BOOL fDoOpAsync);
231 STDMETHOD(StartOperation)(IBindCtx* pbcReserved);
232#endif /* VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC */
233
234public:
235
236 int Init(void);
237 void OnTransferComplete(int rc = VINF_SUCCESS);
238 void OnTransferCanceled();
239
240public:
241
242 static DECLCALLBACK(int) readThread(RTTHREAD ThreadSelf, void *pvUser);
243
244 static void logFormat(CLIPFORMAT fmt);
245
246protected:
247
248 static int Thread(RTTHREAD hThread, void *pvUser);
249
250 int readDir(PSHCLTRANSFER pTransfer, const Utf8Str &strPath);
251
252 int copyToHGlobal(const void *pvData, size_t cbData, UINT fFlags, HGLOBAL *phGlobal);
253 int createFileGroupDescriptorFromTransfer(PSHCLTRANSFER pTransfer,
254 bool fUnicode, HGLOBAL *phGlobal);
255
256 bool lookupFormatEtc(LPFORMATETC pFormatEtc, ULONG *puIndex);
257 void registerFormat(LPFORMATETC pFormatEtc, CLIPFORMAT clipFormat, TYMED tyMed = TYMED_HGLOBAL,
258 LONG lindex = -1, DWORD dwAspect = DVASPECT_CONTENT, DVTARGETDEVICE *pTargetDevice = NULL);
259protected:
260
261 /**
262 * Structure for keeping a single file system object entry.
263 */
264 struct FSOBJENTRY
265 {
266 /** Relative path of the object. */
267 Utf8Str strPath;
268 /** Related (cached) object information. */
269 SHCLFSOBJINFO objInfo;
270 };
271
272 /** Vector containing file system objects with its (cached) objection information. */
273 typedef std::vector<FSOBJENTRY> FsObjEntryList;
274
275 /** The object's current status. */
276 Status m_enmStatus;
277 /** The object's current reference count. */
278 LONG m_lRefCount;
279 /** How many formats have been registered. */
280 ULONG m_cFormats;
281 LPFORMATETC m_pFormatEtc;
282 LPSTGMEDIUM m_pStgMedium;
283 /** Pointer to the associated transfer object being handled. */
284 PSHCLTRANSFER m_pTransfer;
285 /** Current stream object being used. */
286 IStream *m_pStream;
287 /** Current object index being handled by the data object.
288 * This is needed to create the next IStream object for e.g. the next upcoming file/dir/++ in the transfer. */
289 ULONG m_uObjIdx;
290 /** List of (cached) file system objects. */
291 FsObjEntryList m_lstEntries;
292 /** Whether the transfer thread is running. */
293 bool m_fRunning;
294 /** Event being triggered when reading the transfer list been completed. */
295 RTSEMEVENT m_EventListComplete;
296 /** Event being triggered when the transfer has been completed. */
297 RTSEMEVENT m_EventTransferComplete;
298 /** Registered format for CFSTR_FILEDESCRIPTORA. */
299 UINT m_cfFileDescriptorA;
300 /** Registered format for CFSTR_FILEDESCRIPTORW. */
301 UINT m_cfFileDescriptorW;
302 /** Registered format for CFSTR_FILECONTENTS. */
303 UINT m_cfFileContents;
304 /** Registered format for CFSTR_PERFORMEDDROPEFFECT. */
305 UINT m_cfPerformedDropEffect;
306};
307
308class SharedClipboardWinEnumFormatEtc : public IEnumFORMATETC
309{
310public:
311
312 SharedClipboardWinEnumFormatEtc(LPFORMATETC pFormatEtc, ULONG cFormats);
313 virtual ~SharedClipboardWinEnumFormatEtc(void);
314
315public: /* IUnknown methods. */
316
317 STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
318 STDMETHOD_(ULONG, AddRef)(void);
319 STDMETHOD_(ULONG, Release)(void);
320
321public: /* IEnumFORMATETC methods. */
322
323 STDMETHOD(Next)(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched);
324 STDMETHOD(Skip)(ULONG cFormats);
325 STDMETHOD(Reset)(void);
326 STDMETHOD(Clone)(IEnumFORMATETC **ppEnumFormatEtc);
327
328public:
329
330 static void CopyFormat(LPFORMATETC pFormatDest, LPFORMATETC pFormatSource);
331 static HRESULT CreateEnumFormatEtc(UINT cFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc);
332
333private:
334
335 LONG m_lRefCount;
336 ULONG m_nIndex;
337 ULONG m_nNumFormats;
338 LPFORMATETC m_pFormatEtc;
339};
340
341/**
342 * Own IStream implementation to implement file-based clipboard operations
343 * through HGCM. Needed on Windows hosts and guests.
344 */
345class SharedClipboardWinStreamImpl : public IStream
346{
347public:
348
349 SharedClipboardWinStreamImpl(SharedClipboardWinDataObject *pParent, PSHCLTRANSFER pTransfer,
350 const Utf8Str &strPath, PSHCLFSOBJINFO pObjInfo);
351 virtual ~SharedClipboardWinStreamImpl(void);
352
353public: /* IUnknown methods. */
354
355 STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
356 STDMETHOD_(ULONG, AddRef)(void);
357 STDMETHOD_(ULONG, Release)(void);
358
359public: /* IStream methods. */
360
361 STDMETHOD(Clone)(IStream** ppStream);
362 STDMETHOD(Commit)(DWORD dwFrags);
363 STDMETHOD(CopyTo)(IStream* pDestStream, ULARGE_INTEGER nBytesToCopy, ULARGE_INTEGER* nBytesRead, ULARGE_INTEGER* nBytesWritten);
364 STDMETHOD(LockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes,DWORD dwFlags);
365 STDMETHOD(Read)(void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
366 STDMETHOD(Revert)(void);
367 STDMETHOD(Seek)(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos);
368 STDMETHOD(SetSize)(ULARGE_INTEGER nNewSize);
369 STDMETHOD(Stat)(STATSTG* statstg, DWORD dwFlags);
370 STDMETHOD(UnlockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags);
371 STDMETHOD(Write)(const void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
372
373public: /* Own methods. */
374
375 static HRESULT Create(SharedClipboardWinDataObject *pParent, PSHCLTRANSFER pTransfer, const Utf8Str &strPath,
376 PSHCLFSOBJINFO pObjInfo, IStream **ppStream);
377private:
378
379 /** Pointer to the parent data object. */
380 SharedClipboardWinDataObject *m_pParent;
381 /** The stream object's current reference count. */
382 LONG m_lRefCount;
383 /** Pointer to the associated Shared Clipboard transfer. */
384 PSHCLTRANSFER m_pTransfer;
385 /** The object handle to use. */
386 SHCLOBJHANDLE m_hObj;
387 /** Object path. */
388 Utf8Str m_strPath;
389 /** (Cached) object information. */
390 SHCLFSOBJINFO m_objInfo;
391 /** Number of bytes already processed. */
392 uint64_t m_cbProcessed;
393 /** Whether this object already is in completed state or not. */
394 bool m_fIsComplete;
395};
396
397/**
398 * Class for Windows-specifics for maintaining a single Shared Clipboard transfer.
399 * Set as pvUser / cbUser in SHCLTRANSFERCTX.
400 */
401class SharedClipboardWinTransferCtx
402{
403public:
404 SharedClipboardWinTransferCtx()
405 : pDataObj(NULL) { }
406
407 virtual ~SharedClipboardWinTransferCtx()
408 {
409 if (pDataObj)
410 delete pDataObj;
411 }
412
413 /** Pointer to data object to use for this transfer.
414 * Can be NULL if not being used. */
415 SharedClipboardWinDataObject *pDataObj;
416};
417# endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
418#endif /* !VBOX_INCLUDED_GuestHost_SharedClipboard_win_h */
419
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