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 | # endif
|
---|
42 |
|
---|
43 | #ifndef WM_CLIPBOARDUPDATE
|
---|
44 | # define WM_CLIPBOARDUPDATE 0x031D
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #define VBOX_CLIPBOARD_WNDCLASS_NAME "VBoxSharedClipboardClass"
|
---|
48 |
|
---|
49 | /** See: https://docs.microsoft.com/en-us/windows/desktop/dataxchg/html-clipboard-format
|
---|
50 | * Do *not* change the name, as this will break compatbility with other (legacy) applications! */
|
---|
51 | #define VBOX_CLIPBOARD_WIN_REGFMT_HTML "HTML Format"
|
---|
52 |
|
---|
53 | /** Default timeout (in ms) for passing down messages down the clipboard chain. */
|
---|
54 | #define VBOX_CLIPBOARD_CBCHAIN_TIMEOUT_MS 5000
|
---|
55 |
|
---|
56 | /** Sets announced clipboard formats from the host. */
|
---|
57 | #define VBOX_CLIPBOARD_WM_SET_FORMATS WM_USER
|
---|
58 | /** Reads data from the clipboard and sends it to the host. */
|
---|
59 | #define VBOX_CLIPBOARD_WM_READ_DATA WM_USER + 1
|
---|
60 |
|
---|
61 | /* Dynamically load clipboard functions from User32.dll. */
|
---|
62 | typedef BOOL WINAPI FNADDCLIPBOARDFORMATLISTENER(HWND);
|
---|
63 | typedef FNADDCLIPBOARDFORMATLISTENER *PFNADDCLIPBOARDFORMATLISTENER;
|
---|
64 |
|
---|
65 | typedef BOOL WINAPI FNREMOVECLIPBOARDFORMATLISTENER(HWND);
|
---|
66 | typedef FNREMOVECLIPBOARDFORMATLISTENER *PFNREMOVECLIPBOARDFORMATLISTENER;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Structure for keeping function pointers for the new clipboard API.
|
---|
70 | * If the new API is not available, those function pointer are NULL.
|
---|
71 | */
|
---|
72 | typedef struct VBOXCLIPBOARDWINAPINEW
|
---|
73 | {
|
---|
74 | PFNADDCLIPBOARDFORMATLISTENER pfnAddClipboardFormatListener;
|
---|
75 | PFNREMOVECLIPBOARDFORMATLISTENER pfnRemoveClipboardFormatListener;
|
---|
76 | } VBOXCLIPBOARDWINAPINEW, *PVBOXCLIPBOARDWINAPINEW;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Structure for keeping variables which are needed to drive the old clipboard API.
|
---|
80 | */
|
---|
81 | typedef struct VBOXCLIPBOARDWINAPIOLD
|
---|
82 | {
|
---|
83 | /** Timer ID for the refresh timer. */
|
---|
84 | UINT timerRefresh;
|
---|
85 | /** Whether "pinging" the clipboard chain currently is in progress or not. */
|
---|
86 | bool fCBChainPingInProcess;
|
---|
87 | } VBOXCLIPBOARDWINAPIOLD, *PVBOXCLIPBOARDWINAPIOLD;
|
---|
88 |
|
---|
89 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
90 | class VBoxClipboardWinDataObject;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Structure for keeping URI clipboard information around.
|
---|
94 | */
|
---|
95 | typedef struct _VBOXCLIPBOARDWINURI
|
---|
96 | {
|
---|
97 | UINT cfFileGroupDescriptor;
|
---|
98 | UINT cfFileContents;
|
---|
99 | VBoxClipboardWinDataObject *pDataObj;
|
---|
100 | } VBOXCLIPBOARDWINURI, *PVBOXCLIPBOARDWINURI;
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | typedef struct VBOXCLIPBOARDWINCTX
|
---|
104 | {
|
---|
105 | /** Window handle of our (invisible) clipbaord window. */
|
---|
106 | HWND hWnd;
|
---|
107 | /** Window handle which is next to us in the clipboard chain. */
|
---|
108 | HWND hWndNextInChain;
|
---|
109 | /** Structure for maintaining the new clipboard API. */
|
---|
110 | VBOXCLIPBOARDWINAPINEW newAPI;
|
---|
111 | /** Structure for maintaining the old clipboard API. */
|
---|
112 | VBOXCLIPBOARDWINAPIOLD oldAPI;
|
---|
113 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
114 | /** Structure for keeping URI clipboard information around. */
|
---|
115 | VBOXCLIPBOARDWINURI URI;
|
---|
116 | #endif
|
---|
117 | } VBOXCLIPBOARDWINCTX, *PVBOXCLIPBOARDWINCTX;
|
---|
118 |
|
---|
119 | int VBoxClipboardWinOpen(HWND hWnd);
|
---|
120 | int VBoxClipboardWinClose(void);
|
---|
121 | int VBoxClipboardWinClear(void);
|
---|
122 | int VBoxClipboardWinCheckAndInitNewAPI(PVBOXCLIPBOARDWINAPINEW pAPI);
|
---|
123 | bool VBoxClipboardWinIsNewAPI(PVBOXCLIPBOARDWINAPINEW pAPI);
|
---|
124 | int VBoxClipboardWinAddToCBChain(PVBOXCLIPBOARDWINCTX pCtx);
|
---|
125 | int VBoxClipboardWinRemoveFromCBChain(PVBOXCLIPBOARDWINCTX pCtx);
|
---|
126 | VOID CALLBACK VBoxClipboardWinChainPingProc(HWND hWnd, UINT uMsg, ULONG_PTR dwData, LRESULT lResult);
|
---|
127 |
|
---|
128 | VBOXCLIPBOARDFORMAT VBoxClipboardWinClipboardFormatToVBox(UINT uFormat);
|
---|
129 | int VBoxClipboardWinGetFormats(PVBOXCLIPBOARDWINCTX pCtx, PVBOXCLIPBOARDFORMAT pfFormats);
|
---|
130 |
|
---|
131 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
132 | int VBoxClipboardWinDropFilesToStringList(DROPFILES *pDropFiles, void **ppvData, size_t *pcbData);
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | int VBoxClipboardWinGetCFHTMLHeaderValue(const char *pszSrc, const char *pszOption, uint32_t *puValue);
|
---|
136 | bool VBoxClipboardWinIsCFHTML(const char *pszSource);
|
---|
137 | int VBoxClipboardWinConvertCFHTMLToMIME(const char *pszSource, const uint32_t cch, char **ppszOutput, uint32_t *pcbOutput);
|
---|
138 | int VBoxClipboardWinConvertMIMEToCFHTML(const char *pszSource, size_t cb, char **ppszOutput, uint32_t *pcbOutput);
|
---|
139 |
|
---|
140 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
|
---|
141 | class SharedClipboardURIList;
|
---|
142 | # ifndef FILEGROUPDESCRIPTOR
|
---|
143 | class FILEGROUPDESCRIPTOR;
|
---|
144 | # endif
|
---|
145 |
|
---|
146 | class VBoxClipboardWinDataObject : public IDataObject //, public IDataObjectAsyncCapability
|
---|
147 | {
|
---|
148 | public:
|
---|
149 |
|
---|
150 | enum Status
|
---|
151 | {
|
---|
152 | Uninitialized = 0,
|
---|
153 | Initialized
|
---|
154 | };
|
---|
155 |
|
---|
156 | enum FormatIndex
|
---|
157 | {
|
---|
158 | /** File descriptor, ANSI version. */
|
---|
159 | FormatIndex_FileDescriptorA = 0,
|
---|
160 | /** File descriptor, Unicode version. */
|
---|
161 | FormatIndex_FileDescriptorW,
|
---|
162 | /** File contents. */
|
---|
163 | FormatIndex_FileContents
|
---|
164 | };
|
---|
165 |
|
---|
166 | public:
|
---|
167 |
|
---|
168 | VBoxClipboardWinDataObject(LPFORMATETC pFormatEtc = NULL, LPSTGMEDIUM pStgMed = NULL, ULONG cFormats = 0);
|
---|
169 | virtual ~VBoxClipboardWinDataObject(void);
|
---|
170 |
|
---|
171 | public: /* IUnknown methods. */
|
---|
172 |
|
---|
173 | STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
|
---|
174 | STDMETHOD_(ULONG, AddRef)(void);
|
---|
175 | STDMETHOD_(ULONG, Release)(void);
|
---|
176 |
|
---|
177 | public: /* IDataObject methods. */
|
---|
178 |
|
---|
179 | STDMETHOD(GetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
|
---|
180 | STDMETHOD(GetDataHere)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
|
---|
181 | STDMETHOD(QueryGetData)(LPFORMATETC pFormatEtc);
|
---|
182 | STDMETHOD(GetCanonicalFormatEtc)(LPFORMATETC pFormatEct, LPFORMATETC pFormatEtcOut);
|
---|
183 | STDMETHOD(SetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium, BOOL fRelease);
|
---|
184 | STDMETHOD(EnumFormatEtc)(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc);
|
---|
185 | STDMETHOD(DAdvise)(LPFORMATETC pFormatEtc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection);
|
---|
186 | STDMETHOD(DUnadvise)(DWORD dwConnection);
|
---|
187 | STDMETHOD(EnumDAdvise)(IEnumSTATDATA **ppEnumAdvise);
|
---|
188 |
|
---|
189 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC
|
---|
190 | public: /* IDataObjectAsyncCapability methods. */
|
---|
191 |
|
---|
192 | STDMETHOD(EndOperation)(HRESULT hResult, IBindCtx* pbcReserved, DWORD dwEffects);
|
---|
193 | STDMETHOD(GetAsyncMode)(BOOL* pfIsOpAsync);
|
---|
194 | STDMETHOD(InOperation)(BOOL* pfInAsyncOp);
|
---|
195 | STDMETHOD(SetAsyncMode)(BOOL fDoOpAsync);
|
---|
196 | STDMETHOD(StartOperation)(IBindCtx* pbcReserved);
|
---|
197 | #endif /* VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC */
|
---|
198 |
|
---|
199 | public:
|
---|
200 |
|
---|
201 | int Init(uint32_t idClient);
|
---|
202 | static const char* ClipboardFormatToString(CLIPFORMAT fmt);
|
---|
203 |
|
---|
204 | protected:
|
---|
205 |
|
---|
206 | static int Thread(RTTHREAD hThread, void *pvUser);
|
---|
207 |
|
---|
208 | int copyToHGlobal(const void *pvData, size_t cbData, UINT fFlags, HGLOBAL *phGlobal);
|
---|
209 | int createFileGroupDescriptor(const SharedClipboardURIList &URIList, HGLOBAL *phGlobal);
|
---|
210 |
|
---|
211 | bool lookupFormatEtc(LPFORMATETC pFormatEtc, ULONG *puIndex);
|
---|
212 | void registerFormat(LPFORMATETC pFormatEtc, CLIPFORMAT clipFormat, TYMED tyMed = TYMED_HGLOBAL,
|
---|
213 | LONG lindex = -1, DWORD dwAspect = DVASPECT_CONTENT, DVTARGETDEVICE *pTargetDevice = NULL);
|
---|
214 |
|
---|
215 | Status mStatus;
|
---|
216 | LONG mRefCount;
|
---|
217 | ULONG mcFormats;
|
---|
218 | LPFORMATETC mpFormatEtc;
|
---|
219 | LPSTGMEDIUM mpStgMedium;
|
---|
220 | /** The HGCM client ID for the URI data transfers. */
|
---|
221 | uint32_t muClientID;
|
---|
222 | IStream *mpStream;
|
---|
223 | };
|
---|
224 |
|
---|
225 | class VBoxClipboardWinEnumFormatEtc : public IEnumFORMATETC
|
---|
226 | {
|
---|
227 | public:
|
---|
228 |
|
---|
229 | VBoxClipboardWinEnumFormatEtc(LPFORMATETC pFormatEtc, ULONG cFormats);
|
---|
230 | virtual ~VBoxClipboardWinEnumFormatEtc(void);
|
---|
231 |
|
---|
232 | public: /* IUnknown methods. */
|
---|
233 |
|
---|
234 | STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
|
---|
235 | STDMETHOD_(ULONG, AddRef)(void);
|
---|
236 | STDMETHOD_(ULONG, Release)(void);
|
---|
237 |
|
---|
238 | public: /* IEnumFORMATETC methods. */
|
---|
239 |
|
---|
240 | STDMETHOD(Next)(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched);
|
---|
241 | STDMETHOD(Skip)(ULONG cFormats);
|
---|
242 | STDMETHOD(Reset)(void);
|
---|
243 | STDMETHOD(Clone)(IEnumFORMATETC **ppEnumFormatEtc);
|
---|
244 |
|
---|
245 | public:
|
---|
246 |
|
---|
247 | static void CopyFormat(LPFORMATETC pFormatDest, LPFORMATETC pFormatSource);
|
---|
248 | static HRESULT CreateEnumFormatEtc(UINT cFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc);
|
---|
249 |
|
---|
250 | private:
|
---|
251 |
|
---|
252 | LONG m_lRefCount;
|
---|
253 | ULONG m_nIndex;
|
---|
254 | ULONG m_nNumFormats;
|
---|
255 | LPFORMATETC m_pFormatEtc;
|
---|
256 | };
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Own IStream implementation to implement file-based clipboard operations
|
---|
260 | * through HGCM. Needed on Windows hosts and guests.
|
---|
261 | */
|
---|
262 | class VBoxClipboardWinStreamImpl : public IStream
|
---|
263 | {
|
---|
264 | public:
|
---|
265 |
|
---|
266 | VBoxClipboardWinStreamImpl(void);
|
---|
267 | virtual ~VBoxClipboardWinStreamImpl(void);
|
---|
268 |
|
---|
269 | public: /* IUnknown methods. */
|
---|
270 |
|
---|
271 | STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
|
---|
272 | STDMETHOD_(ULONG, AddRef)(void);
|
---|
273 | STDMETHOD_(ULONG, Release)(void);
|
---|
274 |
|
---|
275 | public: /* IStream methods. */
|
---|
276 |
|
---|
277 | STDMETHOD(Clone)(IStream** ppStream);
|
---|
278 | STDMETHOD(Commit)(DWORD dwFrags);
|
---|
279 | STDMETHOD(CopyTo)(IStream* pDestStream, ULARGE_INTEGER nBytesToCopy, ULARGE_INTEGER* nBytesRead, ULARGE_INTEGER* nBytesWritten);
|
---|
280 | STDMETHOD(LockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes,DWORD dwFlags);
|
---|
281 | STDMETHOD(Read)(void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
|
---|
282 | STDMETHOD(Revert)(void);
|
---|
283 | STDMETHOD(Seek)(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos);
|
---|
284 | STDMETHOD(SetSize)(ULARGE_INTEGER nNewSize);
|
---|
285 | STDMETHOD(Stat)(STATSTG* statstg, DWORD dwFlags);
|
---|
286 | STDMETHOD(UnlockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags);
|
---|
287 | STDMETHOD(Write)(const void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
|
---|
288 |
|
---|
289 | public: /* Own methods. */
|
---|
290 |
|
---|
291 | static HRESULT Create(IStream **ppStream);
|
---|
292 |
|
---|
293 | private:
|
---|
294 |
|
---|
295 | LONG m_lRefCount;
|
---|
296 | };
|
---|
297 |
|
---|
298 | # endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
|
---|
299 | #endif /* !VBOX_INCLUDED_GuestHost_SharedClipboard_win_h */
|
---|
300 |
|
---|