1 | /** @file
|
---|
2 | * Shared Clipboard - Common Guest and Host Code, for Windows OSes.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef VBOX_INCLUDED_GuestHost_SharedClipboard_win_h
|
---|
27 | #define VBOX_INCLUDED_GuestHost_SharedClipboard_win_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/types.h>
|
---|
33 | #include <iprt/win/windows.h>
|
---|
34 |
|
---|
35 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
36 |
|
---|
37 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
38 | # include <iprt/cpp/ministring.h> /* For RTCString. */
|
---|
39 | # include <iprt/win/shlobj.h> /* For DROPFILES and friends. */
|
---|
40 | # include <oleidl.h>
|
---|
41 |
|
---|
42 | # include <VBox/GuestHost/SharedClipboard-uri.h>
|
---|
43 | # endif
|
---|
44 |
|
---|
45 | #ifndef WM_CLIPBOARDUPDATE
|
---|
46 | # define WM_CLIPBOARDUPDATE 0x031D
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #define VBOX_CLIPBOARD_WNDCLASS_NAME "VBoxSharedClipboardClass"
|
---|
50 |
|
---|
51 | /** See: https://docs.microsoft.com/en-us/windows/desktop/dataxchg/html-clipboard-format
|
---|
52 | * Do *not* change the name, as this will break compatbility with other (legacy) applications! */
|
---|
53 | #define VBOX_CLIPBOARD_WIN_REGFMT_HTML "HTML Format"
|
---|
54 |
|
---|
55 | /** Default timeout (in ms) for passing down messages down the clipboard chain. */
|
---|
56 | #define VBOX_CLIPBOARD_CBCHAIN_TIMEOUT_MS 5000
|
---|
57 |
|
---|
58 | /** Sets announced clipboard formats from the host. */
|
---|
59 | #define VBOX_CLIPBOARD_WM_SET_FORMATS WM_USER
|
---|
60 | /** Reads data from the clipboard and sends it to the host. */
|
---|
61 | #define VBOX_CLIPBOARD_WM_READ_DATA WM_USER + 1
|
---|
62 |
|
---|
63 | /* Dynamically load clipboard functions from User32.dll. */
|
---|
64 | typedef BOOL WINAPI FNADDCLIPBOARDFORMATLISTENER(HWND);
|
---|
65 | typedef FNADDCLIPBOARDFORMATLISTENER *PFNADDCLIPBOARDFORMATLISTENER;
|
---|
66 |
|
---|
67 | typedef BOOL WINAPI FNREMOVECLIPBOARDFORMATLISTENER(HWND);
|
---|
68 | typedef FNREMOVECLIPBOARDFORMATLISTENER *PFNREMOVECLIPBOARDFORMATLISTENER;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Structure for keeping function pointers for the new clipboard API.
|
---|
72 | * If the new API is not available, those function pointer are NULL.
|
---|
73 | */
|
---|
74 | typedef struct _VBOXCLIPBOARDWINAPINEW
|
---|
75 | {
|
---|
76 | PFNADDCLIPBOARDFORMATLISTENER pfnAddClipboardFormatListener;
|
---|
77 | PFNREMOVECLIPBOARDFORMATLISTENER pfnRemoveClipboardFormatListener;
|
---|
78 | } VBOXCLIPBOARDWINAPINEW, *PVBOXCLIPBOARDWINAPINEW;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Structure for keeping variables which are needed to drive the old clipboard API.
|
---|
82 | */
|
---|
83 | typedef struct _VBOXCLIPBOARDWINAPIOLD
|
---|
84 | {
|
---|
85 | /** Timer ID for the refresh timer. */
|
---|
86 | UINT timerRefresh;
|
---|
87 | /** Whether "pinging" the clipboard chain currently is in progress or not. */
|
---|
88 | bool fCBChainPingInProcess;
|
---|
89 | } VBOXCLIPBOARDWINAPIOLD, *PVBOXCLIPBOARDWINAPIOLD;
|
---|
90 |
|
---|
91 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
92 | class VBoxClipboardWinDataObject;
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Structure for maintaining a single Windows URI transfer.
|
---|
96 | */
|
---|
97 | typedef struct _VBOXCLIPBOARDWINURITRANSFER
|
---|
98 | {
|
---|
99 | /** The Shared Clipboard provider in charge for this transfer. */
|
---|
100 | SharedClipboardProvider *pProvider;
|
---|
101 | /** Pointer to data object to use for this transfer. */
|
---|
102 | VBoxClipboardWinDataObject *pDataObj;
|
---|
103 | } VBOXCLIPBOARDWINURITRANSFER, *PVBOXCLIPBOARDWINURITRANSFER;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Structure for keeping Windows URI clipboard information around.
|
---|
107 | */
|
---|
108 | typedef struct _VBOXCLIPBOARDWINURI
|
---|
109 | {
|
---|
110 | /** Transfer data; at the moment we only support one transfer at a time.
|
---|
111 | * Use a list or something lateron. */
|
---|
112 | VBOXCLIPBOARDWINURITRANSFER Transfer;
|
---|
113 | /** Number of concurrent transfers.
|
---|
114 | * At the moment we only support only one transfer at a time. */
|
---|
115 | uint32_t cTransfers;
|
---|
116 | } VBOXCLIPBOARDWINURI, *PVBOXCLIPBOARDWINURI;
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Structure for maintaining a Shared Clipboard context on Windows platforms.
|
---|
121 | */
|
---|
122 | typedef struct _VBOXCLIPBOARDWINCTX
|
---|
123 | {
|
---|
124 | /** Window handle of our (invisible) clipbaord window. */
|
---|
125 | HWND hWnd;
|
---|
126 | /** Window handle which is next to us in the clipboard chain. */
|
---|
127 | HWND hWndNextInChain;
|
---|
128 | /** Structure for maintaining the new clipboard API. */
|
---|
129 | VBOXCLIPBOARDWINAPINEW newAPI;
|
---|
130 | /** Structure for maintaining the old clipboard API. */
|
---|
131 | VBOXCLIPBOARDWINAPIOLD oldAPI;
|
---|
132 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
133 | /** Structure for keeping URI clipboard information around. */
|
---|
134 | VBOXCLIPBOARDWINURI URI;
|
---|
135 | #endif
|
---|
136 | } VBOXCLIPBOARDWINCTX, *PVBOXCLIPBOARDWINCTX;
|
---|
137 |
|
---|
138 | int VBoxClipboardWinOpen(HWND hWnd);
|
---|
139 | int VBoxClipboardWinClose(void);
|
---|
140 | int VBoxClipboardWinClear(void);
|
---|
141 | int VBoxClipboardWinCheckAndInitNewAPI(PVBOXCLIPBOARDWINAPINEW pAPI);
|
---|
142 | bool VBoxClipboardWinIsNewAPI(PVBOXCLIPBOARDWINAPINEW pAPI);
|
---|
143 | int VBoxClipboardWinAddToCBChain(PVBOXCLIPBOARDWINCTX pCtx);
|
---|
144 | int VBoxClipboardWinRemoveFromCBChain(PVBOXCLIPBOARDWINCTX pCtx);
|
---|
145 | VOID CALLBACK VBoxClipboardWinChainPingProc(HWND hWnd, UINT uMsg, ULONG_PTR dwData, LRESULT lResult);
|
---|
146 |
|
---|
147 | VBOXCLIPBOARDFORMAT VBoxClipboardWinClipboardFormatToVBox(UINT uFormat);
|
---|
148 | int VBoxClipboardWinGetFormats(PVBOXCLIPBOARDWINCTX pCtx, PVBOXCLIPBOARDFORMAT pfFormats);
|
---|
149 |
|
---|
150 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
151 | int VBoxClipboardWinDropFilesToStringList(DROPFILES *pDropFiles, char **ppszData, size_t *pcbData);
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | int VBoxClipboardWinGetCFHTMLHeaderValue(const char *pszSrc, const char *pszOption, uint32_t *puValue);
|
---|
155 | bool VBoxClipboardWinIsCFHTML(const char *pszSource);
|
---|
156 | int VBoxClipboardWinConvertCFHTMLToMIME(const char *pszSource, const uint32_t cch, char **ppszOutput, uint32_t *pcbOutput);
|
---|
157 | int VBoxClipboardWinConvertMIMEToCFHTML(const char *pszSource, size_t cb, char **ppszOutput, uint32_t *pcbOutput);
|
---|
158 |
|
---|
159 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
160 | int VBoxClipboardWinURIInit(PVBOXCLIPBOARDWINURI pURI, PSHAREDCLIPBOARDPROVIDERCREATIONCTX pCtx);
|
---|
161 | void VBoxClipboardWinURIDestroy(PVBOXCLIPBOARDWINURI pURI);
|
---|
162 | void VBoxClipboardWinURIReset(PVBOXCLIPBOARDWINURI pURI);
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
166 | class SharedClipboardURIList;
|
---|
167 | # ifndef FILEGROUPDESCRIPTOR
|
---|
168 | class FILEGROUPDESCRIPTOR;
|
---|
169 | # endif
|
---|
170 |
|
---|
171 | class VBoxClipboardWinDataObject : public IDataObject //, public IDataObjectAsyncCapability
|
---|
172 | {
|
---|
173 | public:
|
---|
174 |
|
---|
175 | enum Status
|
---|
176 | {
|
---|
177 | Uninitialized = 0,
|
---|
178 | Initialized
|
---|
179 | };
|
---|
180 |
|
---|
181 | enum FormatIndex
|
---|
182 | {
|
---|
183 | /** File descriptor, ANSI version. */
|
---|
184 | FormatIndex_FileDescriptorA = 0,
|
---|
185 | /** File descriptor, Unicode version. */
|
---|
186 | FormatIndex_FileDescriptorW,
|
---|
187 | /** File contents. */
|
---|
188 | FormatIndex_FileContents
|
---|
189 | };
|
---|
190 |
|
---|
191 | public:
|
---|
192 |
|
---|
193 | VBoxClipboardWinDataObject(SharedClipboardProvider *pProvider,
|
---|
194 | LPFORMATETC pFormatEtc = NULL, LPSTGMEDIUM pStgMed = NULL, ULONG cFormats = 0);
|
---|
195 | virtual ~VBoxClipboardWinDataObject(void);
|
---|
196 |
|
---|
197 | public: /* IUnknown methods. */
|
---|
198 |
|
---|
199 | STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
|
---|
200 | STDMETHOD_(ULONG, AddRef)(void);
|
---|
201 | STDMETHOD_(ULONG, Release)(void);
|
---|
202 |
|
---|
203 | public: /* IDataObject methods. */
|
---|
204 |
|
---|
205 | STDMETHOD(GetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
|
---|
206 | STDMETHOD(GetDataHere)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
|
---|
207 | STDMETHOD(QueryGetData)(LPFORMATETC pFormatEtc);
|
---|
208 | STDMETHOD(GetCanonicalFormatEtc)(LPFORMATETC pFormatEct, LPFORMATETC pFormatEtcOut);
|
---|
209 | STDMETHOD(SetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium, BOOL fRelease);
|
---|
210 | STDMETHOD(EnumFormatEtc)(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc);
|
---|
211 | STDMETHOD(DAdvise)(LPFORMATETC pFormatEtc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection);
|
---|
212 | STDMETHOD(DUnadvise)(DWORD dwConnection);
|
---|
213 | STDMETHOD(EnumDAdvise)(IEnumSTATDATA **ppEnumAdvise);
|
---|
214 |
|
---|
215 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC
|
---|
216 | public: /* IDataObjectAsyncCapability methods. */
|
---|
217 |
|
---|
218 | STDMETHOD(EndOperation)(HRESULT hResult, IBindCtx* pbcReserved, DWORD dwEffects);
|
---|
219 | STDMETHOD(GetAsyncMode)(BOOL* pfIsOpAsync);
|
---|
220 | STDMETHOD(InOperation)(BOOL* pfInAsyncOp);
|
---|
221 | STDMETHOD(SetAsyncMode)(BOOL fDoOpAsync);
|
---|
222 | STDMETHOD(StartOperation)(IBindCtx* pbcReserved);
|
---|
223 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC */
|
---|
224 |
|
---|
225 | public:
|
---|
226 |
|
---|
227 | int Init(void);
|
---|
228 | static const char* ClipboardFormatToString(CLIPFORMAT fmt);
|
---|
229 |
|
---|
230 | protected:
|
---|
231 |
|
---|
232 | static int Thread(RTTHREAD hThread, void *pvUser);
|
---|
233 |
|
---|
234 | int copyToHGlobal(const void *pvData, size_t cbData, UINT fFlags, HGLOBAL *phGlobal);
|
---|
235 | int createFileGroupDescriptorFromURIList(const SharedClipboardURIList &URIList, bool fUnicode, HGLOBAL *phGlobal);
|
---|
236 |
|
---|
237 | bool lookupFormatEtc(LPFORMATETC pFormatEtc, ULONG *puIndex);
|
---|
238 | void registerFormat(LPFORMATETC pFormatEtc, CLIPFORMAT clipFormat, TYMED tyMed = TYMED_HGLOBAL,
|
---|
239 | LONG lindex = -1, DWORD dwAspect = DVASPECT_CONTENT, DVTARGETDEVICE *pTargetDevice = NULL);
|
---|
240 |
|
---|
241 | protected:
|
---|
242 |
|
---|
243 | Status m_enmStatus;
|
---|
244 | LONG m_lRefCount;
|
---|
245 | ULONG m_cFormats;
|
---|
246 | LPFORMATETC m_pFormatEtc;
|
---|
247 | LPSTGMEDIUM m_pStgMedium;
|
---|
248 | SharedClipboardProvider *m_pProvider;
|
---|
249 | IStream *m_pStream;
|
---|
250 | ULONG m_uObjIdx;
|
---|
251 | };
|
---|
252 |
|
---|
253 | class VBoxClipboardWinEnumFormatEtc : public IEnumFORMATETC
|
---|
254 | {
|
---|
255 | public:
|
---|
256 |
|
---|
257 | VBoxClipboardWinEnumFormatEtc(LPFORMATETC pFormatEtc, ULONG cFormats);
|
---|
258 | virtual ~VBoxClipboardWinEnumFormatEtc(void);
|
---|
259 |
|
---|
260 | public: /* IUnknown methods. */
|
---|
261 |
|
---|
262 | STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
|
---|
263 | STDMETHOD_(ULONG, AddRef)(void);
|
---|
264 | STDMETHOD_(ULONG, Release)(void);
|
---|
265 |
|
---|
266 | public: /* IEnumFORMATETC methods. */
|
---|
267 |
|
---|
268 | STDMETHOD(Next)(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched);
|
---|
269 | STDMETHOD(Skip)(ULONG cFormats);
|
---|
270 | STDMETHOD(Reset)(void);
|
---|
271 | STDMETHOD(Clone)(IEnumFORMATETC **ppEnumFormatEtc);
|
---|
272 |
|
---|
273 | public:
|
---|
274 |
|
---|
275 | static void CopyFormat(LPFORMATETC pFormatDest, LPFORMATETC pFormatSource);
|
---|
276 | static HRESULT CreateEnumFormatEtc(UINT cFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc);
|
---|
277 |
|
---|
278 | private:
|
---|
279 |
|
---|
280 | LONG m_lRefCount;
|
---|
281 | ULONG m_nIndex;
|
---|
282 | ULONG m_nNumFormats;
|
---|
283 | LPFORMATETC m_pFormatEtc;
|
---|
284 | };
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Own IStream implementation to implement file-based clipboard operations
|
---|
288 | * through HGCM. Needed on Windows hosts and guests.
|
---|
289 | */
|
---|
290 | class VBoxClipboardWinStreamImpl : public IStream
|
---|
291 | {
|
---|
292 | public:
|
---|
293 |
|
---|
294 | VBoxClipboardWinStreamImpl(SharedClipboardProvider *pProvider, SharedClipboardURIObject *pURIObj);
|
---|
295 | virtual ~VBoxClipboardWinStreamImpl(void);
|
---|
296 |
|
---|
297 | public: /* IUnknown methods. */
|
---|
298 |
|
---|
299 | STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
|
---|
300 | STDMETHOD_(ULONG, AddRef)(void);
|
---|
301 | STDMETHOD_(ULONG, Release)(void);
|
---|
302 |
|
---|
303 | public: /* IStream methods. */
|
---|
304 |
|
---|
305 | STDMETHOD(Clone)(IStream** ppStream);
|
---|
306 | STDMETHOD(Commit)(DWORD dwFrags);
|
---|
307 | STDMETHOD(CopyTo)(IStream* pDestStream, ULARGE_INTEGER nBytesToCopy, ULARGE_INTEGER* nBytesRead, ULARGE_INTEGER* nBytesWritten);
|
---|
308 | STDMETHOD(LockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes,DWORD dwFlags);
|
---|
309 | STDMETHOD(Read)(void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
|
---|
310 | STDMETHOD(Revert)(void);
|
---|
311 | STDMETHOD(Seek)(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos);
|
---|
312 | STDMETHOD(SetSize)(ULARGE_INTEGER nNewSize);
|
---|
313 | STDMETHOD(Stat)(STATSTG* statstg, DWORD dwFlags);
|
---|
314 | STDMETHOD(UnlockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags);
|
---|
315 | STDMETHOD(Write)(const void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
|
---|
316 |
|
---|
317 | public: /* Own methods. */
|
---|
318 |
|
---|
319 | static HRESULT Create(SharedClipboardProvider *pProvider, SharedClipboardURIObject *pURIObj, IStream **ppStream);
|
---|
320 |
|
---|
321 | private:
|
---|
322 |
|
---|
323 | /** The stream object's current reference count. */
|
---|
324 | LONG m_lRefCount;
|
---|
325 | /** Pointer to the associated Shared Clipboard provider. */
|
---|
326 | SharedClipboardProvider *m_pProvider;
|
---|
327 | /** Pointer to the associated URI object. */
|
---|
328 | SharedClipboardURIObject *m_pURIObj;
|
---|
329 | };
|
---|
330 |
|
---|
331 | # endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
|
---|
332 | #endif /* !VBOX_INCLUDED_GuestHost_SharedClipboard_win_h */
|
---|
333 |
|
---|