23 #include <freerdp/config.h>
33 #include <winpr/assert.h>
34 #include <winpr/library.h>
36 #include <winpr/crt.h>
37 #include <winpr/tchar.h>
38 #include <winpr/stream.h>
40 #include <freerdp/log.h>
41 #include <freerdp/client/cliprdr.h>
45 #include "wf_cliprdr.h"
47 #define TAG CLIENT_TAG("windows")
49 #ifdef WITH_DEBUG_CLIPRDR
50 #define DEBUG_CLIPRDR(...) WLog_DBG(TAG, __VA_ARGS__)
52 #define DEBUG_CLIPRDR(...) \
58 typedef BOOL(WINAPI* fnAddClipboardFormatListener)(HWND hwnd);
59 typedef BOOL(WINAPI* fnRemoveClipboardFormatListener)(HWND hwnd);
60 typedef BOOL(WINAPI* fnGetUpdatedClipboardFormats)(PUINT lpuiFormats, UINT cFormats,
65 UINT32 remote_format_id;
66 UINT32 local_format_id;
72 IEnumFORMATETC iEnumFORMATETC;
77 FORMATETC* m_pFormatEtc;
78 } CliprdrEnumFORMATETC;
94 IDataObject iDataObject;
97 FORMATETC* m_pFormatEtc;
98 STGMEDIUM* m_pStgMedium;
108 rdpChannels* channels;
109 CliprdrClientContext* context;
116 formatMapping* format_mappings;
118 UINT32 requestedFormatId;
123 HANDLE response_data_event;
125 LPDATAOBJECT data_obj;
131 size_t file_array_size;
138 fnAddClipboardFormatListener AddClipboardFormatListener;
139 fnRemoveClipboardFormatListener RemoveClipboardFormatListener;
140 fnGetUpdatedClipboardFormats GetUpdatedClipboardFormats;
143 #define WM_CLIPRDR_MESSAGE (WM_USER + 156)
144 #define OLE_SETCLIPBOARD 1
146 static BOOL wf_create_file_obj(wfClipboard* cliprdrrdr, IDataObject** ppDataObject);
147 static void wf_destroy_file_obj(IDataObject* instance);
148 static UINT32 get_remote_format_id(wfClipboard* clipboard, UINT32 local_format);
149 static UINT cliprdr_send_data_request(wfClipboard* clipboard, UINT32 format);
150 static UINT cliprdr_send_lock(wfClipboard* clipboard);
151 static UINT cliprdr_send_unlock(wfClipboard* clipboard);
152 static UINT cliprdr_send_request_filecontents(wfClipboard* clipboard,
const void* streamid,
153 ULONG index, UINT32 flag, UINT64 position,
156 static void CliprdrDataObject_Delete(CliprdrDataObject* instance);
158 static CliprdrEnumFORMATETC* CliprdrEnumFORMATETC_New(ULONG nFormats, FORMATETC* pFormatEtc);
159 static void CliprdrEnumFORMATETC_Delete(CliprdrEnumFORMATETC* instance);
161 static void CliprdrStream_Delete(CliprdrStream* instance);
163 static BOOL try_open_clipboard(HWND hwnd)
165 for (
size_t x = 0; x < 10; x++)
167 if (OpenClipboard(hwnd))
178 static HRESULT STDMETHODCALLTYPE CliprdrStream_QueryInterface(IStream* This, REFIID riid,
181 if (IsEqualIID(riid, &IID_IStream) || IsEqualIID(riid, &IID_IUnknown))
183 IStream_AddRef(This);
190 return E_NOINTERFACE;
194 static ULONG STDMETHODCALLTYPE CliprdrStream_AddRef(IStream* This)
196 CliprdrStream* instance = (CliprdrStream*)This;
201 return InterlockedIncrement(&instance->m_lRefCount);
204 static ULONG STDMETHODCALLTYPE CliprdrStream_Release(IStream* This)
207 CliprdrStream* instance = (CliprdrStream*)This;
212 count = InterlockedDecrement(&instance->m_lRefCount);
216 CliprdrStream_Delete(instance);
225 static HRESULT STDMETHODCALLTYPE CliprdrStream_Read(IStream* This,
void* pv, ULONG cb,
229 CliprdrStream* instance = (CliprdrStream*)This;
230 wfClipboard* clipboard;
232 if (!pv || !pcbRead || !instance)
235 clipboard = (wfClipboard*)instance->m_pData;
238 if (instance->m_lOffset.QuadPart >= instance->m_lSize.QuadPart)
241 ret = cliprdr_send_request_filecontents(clipboard, (
void*)This, instance->m_lIndex,
242 FILECONTENTS_RANGE, instance->m_lOffset.QuadPart, cb);
247 if (clipboard->req_fdata)
249 CopyMemory(pv, clipboard->req_fdata, clipboard->req_fsize);
250 free(clipboard->req_fdata);
253 *pcbRead = clipboard->req_fsize;
254 instance->m_lOffset.QuadPart += clipboard->req_fsize;
256 if (clipboard->req_fsize < cb)
262 static HRESULT STDMETHODCALLTYPE CliprdrStream_Write(IStream* This,
const void* pv, ULONG cb,
269 return STG_E_ACCESSDENIED;
272 static HRESULT STDMETHODCALLTYPE CliprdrStream_Seek(IStream* This,
LARGE_INTEGER dlibMove,
276 CliprdrStream* instance = (CliprdrStream*)This;
281 newoffset = instance->m_lOffset.QuadPart;
285 case STREAM_SEEK_SET:
286 newoffset = dlibMove.QuadPart;
289 case STREAM_SEEK_CUR:
290 newoffset += dlibMove.QuadPart;
293 case STREAM_SEEK_END:
294 newoffset = instance->m_lSize.QuadPart + dlibMove.QuadPart;
301 if (newoffset < 0 || newoffset >= instance->m_lSize.QuadPart)
304 instance->m_lOffset.QuadPart = newoffset;
307 plibNewPosition->QuadPart = instance->m_lOffset.QuadPart;
312 static HRESULT STDMETHODCALLTYPE CliprdrStream_SetSize(IStream* This,
ULARGE_INTEGER libNewSize)
319 static HRESULT STDMETHODCALLTYPE CliprdrStream_CopyTo(IStream* This, IStream* pstm,
331 static HRESULT STDMETHODCALLTYPE CliprdrStream_Commit(IStream* This, DWORD grfCommitFlags)
334 (void)grfCommitFlags;
338 static HRESULT STDMETHODCALLTYPE CliprdrStream_Revert(IStream* This)
344 static HRESULT STDMETHODCALLTYPE CliprdrStream_LockRegion(IStream* This,
ULARGE_INTEGER libOffset,
354 static HRESULT STDMETHODCALLTYPE CliprdrStream_UnlockRegion(IStream* This,
ULARGE_INTEGER libOffset,
364 static HRESULT STDMETHODCALLTYPE CliprdrStream_Stat(IStream* This, STATSTG* pstatstg,
367 CliprdrStream* instance = (CliprdrStream*)This;
372 if (pstatstg == NULL)
373 return STG_E_INVALIDPOINTER;
375 ZeroMemory(pstatstg,
sizeof(STATSTG));
379 case STATFLAG_DEFAULT:
380 return STG_E_INSUFFICIENTMEMORY;
382 case STATFLAG_NONAME:
383 pstatstg->cbSize.QuadPart = instance->m_lSize.QuadPart;
384 pstatstg->grfLocksSupported = LOCK_EXCLUSIVE;
385 pstatstg->grfMode = GENERIC_READ;
386 pstatstg->grfStateBits = 0;
387 pstatstg->type = STGTY_STREAM;
390 case STATFLAG_NOOPEN:
391 return STG_E_INVALIDFLAG;
394 return STG_E_INVALIDFLAG;
400 static HRESULT STDMETHODCALLTYPE CliprdrStream_Clone(IStream* This, IStream** ppstm)
407 static CliprdrStream* CliprdrStream_New(ULONG index,
void* pData,
const FILEDESCRIPTORW* dsc)
410 BOOL success = FALSE;
412 CliprdrStream* instance;
413 wfClipboard* clipboard = (wfClipboard*)pData;
414 instance = (CliprdrStream*)calloc(1,
sizeof(CliprdrStream));
418 instance->m_Dsc = *dsc;
419 iStream = &instance->iStream;
420 iStream->lpVtbl = (IStreamVtbl*)calloc(1,
sizeof(IStreamVtbl));
424 iStream->lpVtbl->QueryInterface = CliprdrStream_QueryInterface;
425 iStream->lpVtbl->AddRef = CliprdrStream_AddRef;
426 iStream->lpVtbl->Release = CliprdrStream_Release;
427 iStream->lpVtbl->Read = CliprdrStream_Read;
428 iStream->lpVtbl->Write = CliprdrStream_Write;
429 iStream->lpVtbl->Seek = CliprdrStream_Seek;
430 iStream->lpVtbl->SetSize = CliprdrStream_SetSize;
431 iStream->lpVtbl->CopyTo = CliprdrStream_CopyTo;
432 iStream->lpVtbl->Commit = CliprdrStream_Commit;
433 iStream->lpVtbl->Revert = CliprdrStream_Revert;
434 iStream->lpVtbl->LockRegion = CliprdrStream_LockRegion;
435 iStream->lpVtbl->UnlockRegion = CliprdrStream_UnlockRegion;
436 iStream->lpVtbl->Stat = CliprdrStream_Stat;
437 iStream->lpVtbl->Clone = CliprdrStream_Clone;
438 instance->m_lRefCount = 1;
439 instance->m_lIndex = index;
440 instance->m_pData = pData;
441 instance->m_lOffset.QuadPart = 0;
443 if (instance->m_Dsc.dwFlags & FD_ATTRIBUTES)
445 if (instance->m_Dsc.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
449 if (((instance->m_Dsc.dwFlags & FD_FILESIZE) == 0) && !isDir)
452 if (cliprdr_send_request_filecontents(clipboard, (
void*)instance,
453 instance->m_lIndex, FILECONTENTS_SIZE, 0,
459 instance->m_lSize.QuadPart = *((LONGLONG*)clipboard->req_fdata);
460 free(clipboard->req_fdata);
464 instance->m_lSize.QuadPart =
465 ((UINT64)instance->m_Dsc.nFileSizeHigh << 32) | instance->m_Dsc.nFileSizeLow;
473 CliprdrStream_Delete(instance);
480 void CliprdrStream_Delete(CliprdrStream* instance)
484 free(instance->iStream.lpVtbl);
493 static LONG cliprdr_lookup_format(CliprdrDataObject* instance, FORMATETC* pFormatEtc)
495 if (!instance || !pFormatEtc)
498 for (ULONG i = 0; i < instance->m_nNumFormats; i++)
500 if ((pFormatEtc->tymed & instance->m_pFormatEtc[i].tymed) &&
501 pFormatEtc->cfFormat == instance->m_pFormatEtc[i].cfFormat &&
502 pFormatEtc->dwAspect & instance->m_pFormatEtc[i].dwAspect)
511 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_QueryInterface(IDataObject* This, REFIID riid,
519 if (IsEqualIID(riid, &IID_IDataObject) || IsEqualIID(riid, &IID_IUnknown))
521 IDataObject_AddRef(This);
528 return E_NOINTERFACE;
532 static ULONG STDMETHODCALLTYPE CliprdrDataObject_AddRef(IDataObject* This)
534 CliprdrDataObject* instance = (CliprdrDataObject*)This;
539 return InterlockedIncrement(&instance->m_lRefCount);
542 static ULONG STDMETHODCALLTYPE CliprdrDataObject_Release(IDataObject* This)
545 CliprdrDataObject* instance = (CliprdrDataObject*)This;
550 count = InterlockedDecrement(&instance->m_lRefCount);
554 CliprdrDataObject_Delete(instance);
561 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_GetData(IDataObject* This, FORMATETC* pFormatEtc,
565 CliprdrDataObject* instance = (CliprdrDataObject*)This;
566 wfClipboard* clipboard;
568 if (!pFormatEtc || !pMedium || !instance)
571 clipboard = (wfClipboard*)instance->m_pData;
576 if ((idx = cliprdr_lookup_format(instance, pFormatEtc)) == -1)
577 return DV_E_FORMATETC;
579 pMedium->tymed = instance->m_pFormatEtc[idx].tymed;
580 pMedium->pUnkForRelease = 0;
582 if (instance->m_pFormatEtc[idx].cfFormat == RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW))
584 FILEGROUPDESCRIPTOR* dsc;
585 DWORD remote = get_remote_format_id(clipboard, instance->m_pFormatEtc[idx].cfFormat);
587 if (cliprdr_send_data_request(clipboard, remote) != 0)
590 pMedium->u.hGlobal = clipboard->hmem;
595 dsc = (FILEGROUPDESCRIPTOR*)GlobalLock(clipboard->hmem);
596 instance->m_nStreams = dsc->cItems;
597 GlobalUnlock(clipboard->hmem);
599 if (instance->m_nStreams > 0)
601 if (!instance->m_pStream)
603 instance->m_pStream = (LPSTREAM*)calloc(instance->m_nStreams,
sizeof(LPSTREAM));
605 if (instance->m_pStream)
607 for (ULONG i = 0; i < instance->m_nStreams; i++)
609 instance->m_pStream[i] =
610 (IStream*)CliprdrStream_New(i, clipboard, &dsc->fgd[i]);
612 if (!instance->m_pStream[i])
613 return E_OUTOFMEMORY;
619 if (!instance->m_pStream)
623 GlobalFree(clipboard->hmem);
624 clipboard->hmem = NULL;
627 pMedium->u.hGlobal = NULL;
628 return E_OUTOFMEMORY;
631 else if (instance->m_pFormatEtc[idx].cfFormat == RegisterClipboardFormat(CFSTR_FILECONTENTS))
633 if ((pFormatEtc->lindex >= 0) && ((ULONG)pFormatEtc->lindex < instance->m_nStreams))
635 pMedium->u.pstm = instance->m_pStream[pFormatEtc->lindex];
636 IDataObject_AddRef(instance->m_pStream[pFormatEtc->lindex]);
647 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_GetDataHere(IDataObject* This,
648 FORMATETC* pformatetc,
657 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_QueryGetData(IDataObject* This,
658 FORMATETC* pformatetc)
660 CliprdrDataObject* instance = (CliprdrDataObject*)This;
665 if (cliprdr_lookup_format(instance, pformatetc) == -1)
666 return DV_E_FORMATETC;
671 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_GetCanonicalFormatEtc(IDataObject* This,
672 FORMATETC* pformatectIn,
673 FORMATETC* pformatetcOut)
681 pformatetcOut->ptd = NULL;
685 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_SetData(IDataObject* This, FORMATETC* pformatetc,
686 STGMEDIUM* pmedium, BOOL fRelease)
695 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_EnumFormatEtc(IDataObject* This,
697 IEnumFORMATETC** ppenumFormatEtc)
699 CliprdrDataObject* instance = (CliprdrDataObject*)This;
701 if (!instance || !ppenumFormatEtc)
704 if (dwDirection == DATADIR_GET)
706 *ppenumFormatEtc = (IEnumFORMATETC*)CliprdrEnumFORMATETC_New(instance->m_nNumFormats,
707 instance->m_pFormatEtc);
708 return (*ppenumFormatEtc) ? S_OK : E_OUTOFMEMORY;
716 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_DAdvise(IDataObject* This, FORMATETC* pformatetc,
717 DWORD advf, IAdviseSink* pAdvSink,
718 DWORD* pdwConnection)
725 return OLE_E_ADVISENOTSUPPORTED;
728 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_DUnadvise(IDataObject* This, DWORD dwConnection)
732 return OLE_E_ADVISENOTSUPPORTED;
735 static HRESULT STDMETHODCALLTYPE CliprdrDataObject_EnumDAdvise(IDataObject* This,
736 IEnumSTATDATA** ppenumAdvise)
740 return OLE_E_ADVISENOTSUPPORTED;
743 static CliprdrDataObject* CliprdrDataObject_New(FORMATETC* fmtetc, STGMEDIUM* stgmed, ULONG count,
746 CliprdrDataObject* instance;
747 IDataObject* iDataObject;
748 instance = (CliprdrDataObject*)calloc(1,
sizeof(CliprdrDataObject));
753 iDataObject = &instance->iDataObject;
754 iDataObject->lpVtbl = (IDataObjectVtbl*)calloc(1,
sizeof(IDataObjectVtbl));
756 if (!iDataObject->lpVtbl)
759 iDataObject->lpVtbl->QueryInterface = CliprdrDataObject_QueryInterface;
760 iDataObject->lpVtbl->AddRef = CliprdrDataObject_AddRef;
761 iDataObject->lpVtbl->Release = CliprdrDataObject_Release;
762 iDataObject->lpVtbl->GetData = CliprdrDataObject_GetData;
763 iDataObject->lpVtbl->GetDataHere = CliprdrDataObject_GetDataHere;
764 iDataObject->lpVtbl->QueryGetData = CliprdrDataObject_QueryGetData;
765 iDataObject->lpVtbl->GetCanonicalFormatEtc = CliprdrDataObject_GetCanonicalFormatEtc;
766 iDataObject->lpVtbl->SetData = CliprdrDataObject_SetData;
767 iDataObject->lpVtbl->EnumFormatEtc = CliprdrDataObject_EnumFormatEtc;
768 iDataObject->lpVtbl->DAdvise = CliprdrDataObject_DAdvise;
769 iDataObject->lpVtbl->DUnadvise = CliprdrDataObject_DUnadvise;
770 iDataObject->lpVtbl->EnumDAdvise = CliprdrDataObject_EnumDAdvise;
771 instance->m_lRefCount = 1;
772 instance->m_nNumFormats = count;
773 instance->m_pData = data;
774 instance->m_nStreams = 0;
775 instance->m_pStream = NULL;
779 instance->m_pFormatEtc = (FORMATETC*)calloc(count,
sizeof(FORMATETC));
781 if (!instance->m_pFormatEtc)
784 instance->m_pStgMedium = (STGMEDIUM*)calloc(count,
sizeof(STGMEDIUM));
786 if (!instance->m_pStgMedium)
789 for (ULONG i = 0; i < count; i++)
791 instance->m_pFormatEtc[i] = fmtetc[i];
792 instance->m_pStgMedium[i] = stgmed[i];
798 CliprdrDataObject_Delete(instance);
802 void CliprdrDataObject_Delete(CliprdrDataObject* instance)
806 free(instance->iDataObject.lpVtbl);
807 free(instance->m_pFormatEtc);
808 free(instance->m_pStgMedium);
810 if (instance->m_pStream)
812 for (ULONG i = 0; i < instance->m_nStreams; i++)
813 CliprdrStream_Release(instance->m_pStream[i]);
815 free(instance->m_pStream);
822 static BOOL wf_create_file_obj(wfClipboard* clipboard, IDataObject** ppDataObject)
824 FORMATETC fmtetc[2] = { 0 };
825 STGMEDIUM stgmeds[2] = { 0 };
830 fmtetc[0].cfFormat = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
831 fmtetc[0].dwAspect = DVASPECT_CONTENT;
833 fmtetc[0].tymed = TYMED_HGLOBAL;
834 stgmeds[0].tymed = TYMED_HGLOBAL;
836 fmtetc[1].cfFormat = RegisterClipboardFormat(CFSTR_FILECONTENTS);
837 fmtetc[1].dwAspect = DVASPECT_CONTENT;
839 fmtetc[1].tymed = TYMED_ISTREAM;
840 stgmeds[1].tymed = TYMED_ISTREAM;
842 *ppDataObject = (IDataObject*)CliprdrDataObject_New(fmtetc, stgmeds, 2, clipboard);
843 return (*ppDataObject) ? TRUE : FALSE;
846 static void wf_destroy_file_obj(IDataObject* instance)
849 IDataObject_Release(instance);
856 static void cliprdr_format_deep_copy(FORMATETC* dest, FORMATETC* source)
862 dest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(
sizeof(DVTARGETDEVICE));
865 *(dest->ptd) = *(source->ptd);
869 static HRESULT STDMETHODCALLTYPE CliprdrEnumFORMATETC_QueryInterface(IEnumFORMATETC* This,
870 REFIID riid,
void** ppvObject)
874 if (IsEqualIID(riid, &IID_IEnumFORMATETC) || IsEqualIID(riid, &IID_IUnknown))
876 IEnumFORMATETC_AddRef(This);
883 return E_NOINTERFACE;
887 static ULONG STDMETHODCALLTYPE CliprdrEnumFORMATETC_AddRef(IEnumFORMATETC* This)
889 CliprdrEnumFORMATETC* instance = (CliprdrEnumFORMATETC*)This;
894 return InterlockedIncrement(&instance->m_lRefCount);
897 static ULONG STDMETHODCALLTYPE CliprdrEnumFORMATETC_Release(IEnumFORMATETC* This)
900 CliprdrEnumFORMATETC* instance = (CliprdrEnumFORMATETC*)This;
905 count = InterlockedDecrement(&instance->m_lRefCount);
909 CliprdrEnumFORMATETC_Delete(instance);
918 static HRESULT STDMETHODCALLTYPE CliprdrEnumFORMATETC_Next(IEnumFORMATETC* This, ULONG celt,
919 FORMATETC* rgelt, ULONG* pceltFetched)
922 CliprdrEnumFORMATETC* instance = (CliprdrEnumFORMATETC*)This;
924 if (!instance || !celt || !rgelt)
927 while ((instance->m_nIndex < instance->m_nNumFormats) && (copied < celt))
929 cliprdr_format_deep_copy(&rgelt[copied++], &instance->m_pFormatEtc[instance->m_nIndex++]);
932 if (pceltFetched != 0)
933 *pceltFetched = copied;
935 return (copied == celt) ? S_OK : E_FAIL;
938 static HRESULT STDMETHODCALLTYPE CliprdrEnumFORMATETC_Skip(IEnumFORMATETC* This, ULONG celt)
940 CliprdrEnumFORMATETC* instance = (CliprdrEnumFORMATETC*)This;
945 if (instance->m_nIndex + (LONG)celt > instance->m_nNumFormats)
948 instance->m_nIndex += celt;
952 static HRESULT STDMETHODCALLTYPE CliprdrEnumFORMATETC_Reset(IEnumFORMATETC* This)
954 CliprdrEnumFORMATETC* instance = (CliprdrEnumFORMATETC*)This;
959 instance->m_nIndex = 0;
963 static HRESULT STDMETHODCALLTYPE CliprdrEnumFORMATETC_Clone(IEnumFORMATETC* This,
964 IEnumFORMATETC** ppEnum)
966 CliprdrEnumFORMATETC* instance = (CliprdrEnumFORMATETC*)This;
968 if (!instance || !ppEnum)
972 (IEnumFORMATETC*)CliprdrEnumFORMATETC_New(instance->m_nNumFormats, instance->m_pFormatEtc);
975 return E_OUTOFMEMORY;
977 ((CliprdrEnumFORMATETC*)*ppEnum)->m_nIndex = instance->m_nIndex;
981 CliprdrEnumFORMATETC* CliprdrEnumFORMATETC_New(ULONG nFormats, FORMATETC* pFormatEtc)
983 CliprdrEnumFORMATETC* instance;
984 IEnumFORMATETC* iEnumFORMATETC;
986 if ((nFormats != 0) && !pFormatEtc)
989 instance = (CliprdrEnumFORMATETC*)calloc(1,
sizeof(CliprdrEnumFORMATETC));
994 iEnumFORMATETC = &instance->iEnumFORMATETC;
995 iEnumFORMATETC->lpVtbl = (IEnumFORMATETCVtbl*)calloc(1,
sizeof(IEnumFORMATETCVtbl));
997 if (!iEnumFORMATETC->lpVtbl)
1000 iEnumFORMATETC->lpVtbl->QueryInterface = CliprdrEnumFORMATETC_QueryInterface;
1001 iEnumFORMATETC->lpVtbl->AddRef = CliprdrEnumFORMATETC_AddRef;
1002 iEnumFORMATETC->lpVtbl->Release = CliprdrEnumFORMATETC_Release;
1003 iEnumFORMATETC->lpVtbl->Next = CliprdrEnumFORMATETC_Next;
1004 iEnumFORMATETC->lpVtbl->Skip = CliprdrEnumFORMATETC_Skip;
1005 iEnumFORMATETC->lpVtbl->Reset = CliprdrEnumFORMATETC_Reset;
1006 iEnumFORMATETC->lpVtbl->Clone = CliprdrEnumFORMATETC_Clone;
1007 instance->m_lRefCount = 1;
1008 instance->m_nIndex = 0;
1009 instance->m_nNumFormats = nFormats;
1013 instance->m_pFormatEtc = (FORMATETC*)calloc(nFormats,
sizeof(FORMATETC));
1015 if (!instance->m_pFormatEtc)
1018 for (ULONG i = 0; i < nFormats; i++)
1019 cliprdr_format_deep_copy(&instance->m_pFormatEtc[i], &pFormatEtc[i]);
1024 CliprdrEnumFORMATETC_Delete(instance);
1028 void CliprdrEnumFORMATETC_Delete(CliprdrEnumFORMATETC* instance)
1032 free(instance->iEnumFORMATETC.lpVtbl);
1034 if (instance->m_pFormatEtc)
1036 for (LONG i = 0; i < instance->m_nNumFormats; i++)
1038 if (instance->m_pFormatEtc[i].ptd)
1039 CoTaskMemFree(instance->m_pFormatEtc[i].ptd);
1042 free(instance->m_pFormatEtc);
1051 static UINT32 get_local_format_id_by_name(wfClipboard* clipboard,
const TCHAR* format_name)
1054 WCHAR* unicode_name;
1055 #if !defined(UNICODE)
1059 if (!clipboard || !format_name)
1062 #if defined(UNICODE)
1063 unicode_name = _wcsdup(format_name);
1065 size = _tcslen(format_name);
1066 unicode_name = calloc(size + 1,
sizeof(WCHAR));
1071 MultiByteToWideChar(CP_OEMCP, 0, format_name, strlen(format_name), unicode_name, size);
1077 for (
size_t i = 0; i < clipboard->map_size; i++)
1079 map = &clipboard->format_mappings[i];
1083 if (wcscmp(map->name, unicode_name) == 0)
1086 return map->local_format_id;
1095 static INLINE BOOL file_transferring(wfClipboard* clipboard)
1097 return get_local_format_id_by_name(clipboard, CFSTR_FILEDESCRIPTORW) ? TRUE : FALSE;
1100 static UINT32 get_remote_format_id(wfClipboard* clipboard, UINT32 local_format)
1107 for (UINT32 i = 0; i < clipboard->map_size; i++)
1109 map = &clipboard->format_mappings[i];
1111 if (map->local_format_id == local_format)
1112 return map->remote_format_id;
1115 return local_format;
1118 static void map_ensure_capacity(wfClipboard* clipboard)
1123 if (clipboard->map_size >= clipboard->map_capacity)
1126 formatMapping* new_map;
1127 new_size = clipboard->map_capacity * 2;
1129 (formatMapping*)realloc(clipboard->format_mappings,
sizeof(formatMapping) * new_size);
1134 clipboard->format_mappings = new_map;
1135 clipboard->map_capacity = new_size;
1139 static BOOL clear_format_map(wfClipboard* clipboard)
1146 if (clipboard->format_mappings)
1148 for (
size_t i = 0; i < clipboard->map_capacity; i++)
1150 map = &clipboard->format_mappings[i];
1151 map->remote_format_id = 0;
1152 map->local_format_id = 0;
1158 clipboard->map_size = 0;
1162 static UINT cliprdr_send_tempdir(wfClipboard* clipboard)
1169 if (GetEnvironmentVariableA(
"TEMP", tempDirectory.szTempDir,
sizeof(tempDirectory.szTempDir)) ==
1173 return clipboard->context->TempDirectory(clipboard->context, &tempDirectory);
1176 static BOOL cliprdr_GetUpdatedClipboardFormats(wfClipboard* clipboard, PUINT lpuiFormats,
1177 UINT cFormats, PUINT pcFormatsOut)
1181 BOOL clipboardOpen = FALSE;
1183 if (!clipboard->legacyApi)
1184 return clipboard->GetUpdatedClipboardFormats(lpuiFormats, cFormats, pcFormatsOut);
1186 clipboardOpen = try_open_clipboard(clipboard->hwnd);
1194 while (index < cFormats)
1196 format = EnumClipboardFormats(format);
1201 lpuiFormats[index] = format;
1205 *pcFormatsOut = index;
1210 static UINT cliprdr_send_format_list(wfClipboard* clipboard)
1214 UINT32 numFormats = 0;
1215 UINT32 formatId = 0;
1216 char formatName[1024];
1221 return ERROR_INTERNAL_ERROR;
1224 if (try_open_clipboard(clipboard->hwnd))
1226 count = CountClipboardFormats();
1227 numFormats = (UINT32)count;
1233 return CHANNEL_RC_NO_MEMORY;
1239 if (IsClipboardFormatAvailable(CF_HDROP))
1241 formats[index++].formatId = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
1242 formats[index++].formatId = RegisterClipboardFormat(CFSTR_FILECONTENTS);
1246 while ((formatId = EnumClipboardFormats(formatId)) != 0)
1247 formats[index++].formatId = formatId;
1253 if (!CloseClipboard())
1256 return ERROR_INTERNAL_ERROR;
1259 for (UINT index = 0; index < numFormats; index++)
1261 if (GetClipboardFormatNameA(formats[index].formatId, formatName,
sizeof(formatName)))
1263 formats[index].formatName = _strdup(formatName);
1268 formatList.numFormats = numFormats;
1269 formatList.formats = formats;
1270 formatList.common.msgType = CB_FORMAT_LIST;
1271 rc = clipboard->context->ClientFormatList(clipboard->context, &formatList);
1273 for (UINT index = 0; index < numFormats; index++)
1274 free(formats[index].formatName);
1280 static UINT cliprdr_send_data_request(wfClipboard* clipboard, UINT32 formatId)
1283 UINT32 remoteFormatId;
1286 if (!clipboard || !clipboard->context || !clipboard->context->ClientFormatDataRequest)
1287 return ERROR_INTERNAL_ERROR;
1289 remoteFormatId = get_remote_format_id(clipboard, formatId);
1291 formatDataRequest.requestedFormatId = remoteFormatId;
1292 clipboard->requestedFormatId = formatId;
1293 rc = clipboard->context->ClientFormatDataRequest(clipboard->context, &formatDataRequest);
1295 if (WaitForSingleObject(clipboard->response_data_event, INFINITE) != WAIT_OBJECT_0)
1296 rc = ERROR_INTERNAL_ERROR;
1297 else if (!ResetEvent(clipboard->response_data_event))
1298 rc = ERROR_INTERNAL_ERROR;
1303 UINT cliprdr_send_request_filecontents(wfClipboard* clipboard,
const void* streamid, ULONG index,
1304 UINT32 flag, UINT64 position, ULONG nreq)
1309 if (!clipboard || !clipboard->context || !clipboard->context->ClientFileContentsRequest)
1310 return ERROR_INTERNAL_ERROR;
1312 fileContentsRequest.streamId = (UINT32)(ULONG_PTR)streamid;
1313 fileContentsRequest.listIndex = index;
1314 fileContentsRequest.dwFlags = flag;
1315 fileContentsRequest.nPositionLow = position & 0xFFFFFFFF;
1316 fileContentsRequest.nPositionHigh = (position >> 32) & 0xFFFFFFFF;
1317 fileContentsRequest.cbRequested = nreq;
1318 fileContentsRequest.clipDataId = 0;
1319 fileContentsRequest.common.msgFlags = 0;
1320 rc = clipboard->context->ClientFileContentsRequest(clipboard->context, &fileContentsRequest);
1322 if (WaitForSingleObject(clipboard->req_fevent, INFINITE) != WAIT_OBJECT_0)
1323 rc = ERROR_INTERNAL_ERROR;
1324 else if (!ResetEvent(clipboard->req_fevent))
1325 rc = ERROR_INTERNAL_ERROR;
1330 static UINT cliprdr_send_response_filecontents(wfClipboard* clipboard, UINT32 streamId, UINT32 size,
1335 if (!clipboard || !clipboard->context || !clipboard->context->ClientFileContentsResponse)
1336 return ERROR_INTERNAL_ERROR;
1338 fileContentsResponse.streamId = streamId;
1339 fileContentsResponse.cbRequested = size;
1340 fileContentsResponse.requestedData = data;
1341 fileContentsResponse.common.msgFlags = CB_RESPONSE_OK;
1342 return clipboard->context->ClientFileContentsResponse(clipboard->context,
1343 &fileContentsResponse);
1346 static LRESULT CALLBACK cliprdr_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
1348 static wfClipboard* clipboard = NULL;
1353 DEBUG_CLIPRDR(
"info: WM_CREATE");
1354 clipboard = (wfClipboard*)((CREATESTRUCT*)lParam)->lpCreateParams;
1355 clipboard->hwnd = hWnd;
1357 if (!clipboard->legacyApi)
1358 clipboard->AddClipboardFormatListener(hWnd);
1360 clipboard->hWndNextViewer = SetClipboardViewer(hWnd);
1365 DEBUG_CLIPRDR(
"info: WM_CLOSE");
1367 if (!clipboard->legacyApi)
1368 clipboard->RemoveClipboardFormatListener(hWnd);
1373 if (clipboard->legacyApi)
1374 ChangeClipboardChain(hWnd, clipboard->hWndNextViewer);
1378 case WM_CLIPBOARDUPDATE:
1379 DEBUG_CLIPRDR(
"info: WM_CLIPBOARDUPDATE");
1381 if (clipboard->sync)
1383 if ((GetClipboardOwner() != clipboard->hwnd) &&
1384 (S_FALSE == OleIsCurrentClipboard(clipboard->data_obj)))
1386 if (clipboard->hmem)
1388 GlobalFree(clipboard->hmem);
1389 clipboard->hmem = NULL;
1392 cliprdr_send_format_list(clipboard);
1398 case WM_RENDERALLFORMATS:
1399 DEBUG_CLIPRDR(
"info: WM_RENDERALLFORMATS");
1402 if (!try_open_clipboard(clipboard->hwnd))
1404 DEBUG_CLIPRDR(
"OpenClipboard failed with 0x%x", GetLastError());
1412 case WM_RENDERFORMAT:
1413 DEBUG_CLIPRDR(
"info: WM_RENDERFORMAT");
1415 if (cliprdr_send_data_request(clipboard, (UINT32)wParam) != 0)
1417 DEBUG_CLIPRDR(
"error: cliprdr_send_data_request failed.");
1421 if (!SetClipboardData((UINT)wParam, clipboard->hmem))
1423 DEBUG_CLIPRDR(
"SetClipboardData failed with 0x%x", GetLastError());
1425 if (clipboard->hmem)
1427 GlobalFree(clipboard->hmem);
1428 clipboard->hmem = NULL;
1435 case WM_DRAWCLIPBOARD:
1436 if (clipboard->legacyApi)
1438 if ((GetClipboardOwner() != clipboard->hwnd) &&
1439 (S_FALSE == OleIsCurrentClipboard(clipboard->data_obj)))
1441 cliprdr_send_format_list(clipboard);
1444 SendMessage(clipboard->hWndNextViewer, Msg, wParam, lParam);
1449 case WM_CHANGECBCHAIN:
1450 if (clipboard->legacyApi)
1452 HWND hWndCurrViewer = (HWND)wParam;
1453 HWND hWndNextViewer = (HWND)lParam;
1455 if (hWndCurrViewer == clipboard->hWndNextViewer)
1456 clipboard->hWndNextViewer = hWndNextViewer;
1457 else if (clipboard->hWndNextViewer)
1458 SendMessage(clipboard->hWndNextViewer, Msg, wParam, lParam);
1463 case WM_CLIPRDR_MESSAGE:
1464 DEBUG_CLIPRDR(
"info: WM_CLIPRDR_MESSAGE");
1468 case OLE_SETCLIPBOARD:
1469 DEBUG_CLIPRDR(
"info: OLE_SETCLIPBOARD");
1471 if (wf_create_file_obj(clipboard, &clipboard->data_obj))
1473 if (OleSetClipboard(clipboard->data_obj) != S_OK)
1475 wf_destroy_file_obj(clipboard->data_obj);
1476 clipboard->data_obj = NULL;
1488 case WM_DESTROYCLIPBOARD:
1489 case WM_ASKCBFORMATNAME:
1490 case WM_HSCROLLCLIPBOARD:
1491 case WM_PAINTCLIPBOARD:
1492 case WM_SIZECLIPBOARD:
1493 case WM_VSCROLLCLIPBOARD:
1495 return DefWindowProc(hWnd, Msg, wParam, lParam);
1501 static int create_cliprdr_window(wfClipboard* clipboard)
1503 WNDCLASSEX wnd_cls = { 0 };
1505 wnd_cls.cbSize =
sizeof(WNDCLASSEX);
1506 wnd_cls.style = CS_OWNDC;
1507 wnd_cls.lpfnWndProc = cliprdr_proc;
1508 wnd_cls.cbClsExtra = 0;
1509 wnd_cls.cbWndExtra = 0;
1510 wnd_cls.hIcon = NULL;
1511 wnd_cls.hCursor = NULL;
1512 wnd_cls.hbrBackground = NULL;
1513 wnd_cls.lpszMenuName = NULL;
1514 wnd_cls.lpszClassName = _T(
"ClipboardHiddenMessageProcessor");
1515 wnd_cls.hInstance = GetModuleHandle(NULL);
1516 wnd_cls.hIconSm = NULL;
1517 RegisterClassEx(&wnd_cls);
1519 CreateWindowEx(WS_EX_LEFT, _T(
"ClipboardHiddenMessageProcessor"), _T(
"rdpclip"), 0, 0, 0, 0,
1520 0, HWND_MESSAGE, NULL, GetModuleHandle(NULL), clipboard);
1522 if (!clipboard->hwnd)
1524 DEBUG_CLIPRDR(
"error: CreateWindowEx failed with %x.", GetLastError());
1531 static DWORD WINAPI cliprdr_thread_func(LPVOID arg)
1536 wfClipboard* clipboard = (wfClipboard*)arg;
1539 if ((ret = create_cliprdr_window(clipboard)) != 0)
1542 DEBUG_CLIPRDR(
"error: create clipboard window failed.");
1546 while ((mcode = GetMessage(&msg, 0, 0, 0)) != 0)
1550 DEBUG_CLIPRDR(
"error: clipboard thread GetMessage failed.");
1555 TranslateMessage(&msg);
1556 DispatchMessage(&msg);
1564 static void clear_file_array(wfClipboard* clipboard)
1570 if (clipboard->file_names)
1572 for (
size_t i = 0; i < clipboard->nFiles; i++)
1574 free(clipboard->file_names[i]);
1575 clipboard->file_names[i] = NULL;
1578 free(clipboard->file_names);
1579 clipboard->file_names = NULL;
1583 if (clipboard->fileDescriptor)
1585 for (
size_t i = 0; i < clipboard->nFiles; i++)
1587 free(clipboard->fileDescriptor[i]);
1588 clipboard->fileDescriptor[i] = NULL;
1591 free(clipboard->fileDescriptor);
1592 clipboard->fileDescriptor = NULL;
1595 clipboard->file_array_size = 0;
1596 clipboard->nFiles = 0;
1599 static BOOL wf_cliprdr_get_file_contents(WCHAR* file_name, BYTE* buffer, LONG positionLow,
1600 LONG positionHigh, DWORD nRequested, DWORD* puSize)
1606 if (!file_name || !buffer || !puSize)
1608 WLog_ERR(TAG,
"get file contents Invalid Arguments.");
1612 hFile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1613 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1615 if (hFile == INVALID_HANDLE_VALUE)
1618 rc = SetFilePointer(hFile, positionLow, &positionHigh, FILE_BEGIN);
1620 if (rc == INVALID_SET_FILE_POINTER)
1623 if (!ReadFile(hFile, buffer, nRequested, &nGet, NULL))
1625 DEBUG_CLIPRDR(
"ReadFile failed with 0x%08lX.", GetLastError());
1632 if (!CloseHandle(hFile))
1642 static FILEDESCRIPTORW* wf_cliprdr_get_file_descriptor(WCHAR* file_name,
size_t pathLen)
1651 hFile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1652 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1654 if (hFile == INVALID_HANDLE_VALUE)
1660 fd->dwFlags = FD_ATTRIBUTES | FD_FILESIZE | FD_WRITESTIME | FD_PROGRESSUI;
1661 fd->dwFileAttributes = GetFileAttributes(file_name);
1663 if (!GetFileTime(hFile, NULL, NULL, &fd->ftLastWriteTime))
1665 fd->dwFlags &= ~FD_WRITESTIME;
1668 fd->nFileSizeLow = GetFileSize(hFile, &fd->nFileSizeHigh);
1669 wcscpy_s(fd->cFileName,
sizeof(fd->cFileName) / 2, file_name + pathLen);
1670 (void)CloseHandle(hFile);
1674 static BOOL wf_cliprdr_array_ensure_capacity(wfClipboard* clipboard)
1679 if (clipboard->nFiles == clipboard->file_array_size)
1684 new_size = (clipboard->file_array_size + 1) * 2;
1689 clipboard->fileDescriptor = new_fd;
1691 new_name = (WCHAR**)realloc(clipboard->file_names, new_size *
sizeof(WCHAR*));
1694 clipboard->file_names = new_name;
1696 if (!new_fd || !new_name)
1699 clipboard->file_array_size = new_size;
1705 static BOOL wf_cliprdr_add_to_file_arrays(wfClipboard* clipboard, WCHAR* full_file_name,
1708 if (!wf_cliprdr_array_ensure_capacity(clipboard))
1712 clipboard->file_names[clipboard->nFiles] = (LPWSTR)malloc(MAX_PATH * 2);
1714 if (!clipboard->file_names[clipboard->nFiles])
1717 wcscpy_s(clipboard->file_names[clipboard->nFiles], MAX_PATH, full_file_name);
1719 clipboard->fileDescriptor[clipboard->nFiles] =
1720 wf_cliprdr_get_file_descriptor(full_file_name, pathLen);
1722 if (!clipboard->fileDescriptor[clipboard->nFiles])
1724 free(clipboard->file_names[clipboard->nFiles]);
1728 clipboard->nFiles++;
1732 static BOOL wf_cliprdr_traverse_directory(wfClipboard* clipboard, WCHAR* Dir,
size_t pathLen)
1735 WCHAR DirSpec[MAX_PATH];
1736 WIN32_FIND_DATA FindFileData;
1738 if (!clipboard || !Dir)
1741 StringCchCopy(DirSpec, MAX_PATH, Dir);
1742 StringCchCat(DirSpec, MAX_PATH, TEXT(
"\\*"));
1743 hFind = FindFirstFile(DirSpec, &FindFileData);
1745 if (hFind == INVALID_HANDLE_VALUE)
1747 DEBUG_CLIPRDR(
"FindFirstFile failed with 0x%x.", GetLastError());
1751 while (FindNextFile(hFind, &FindFileData))
1753 if ((((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) &&
1754 (wcscmp(FindFileData.cFileName, _T(
".")) == 0)) ||
1755 (wcscmp(FindFileData.cFileName, _T(
"..")) == 0))
1760 if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
1762 WCHAR DirAdd[MAX_PATH];
1763 StringCchCopy(DirAdd, MAX_PATH, Dir);
1764 StringCchCat(DirAdd, MAX_PATH, _T(
"\\"));
1765 StringCchCat(DirAdd, MAX_PATH, FindFileData.cFileName);
1767 if (!wf_cliprdr_add_to_file_arrays(clipboard, DirAdd, pathLen))
1770 if (!wf_cliprdr_traverse_directory(clipboard, DirAdd, pathLen))
1775 WCHAR fileName[MAX_PATH];
1776 StringCchCopy(fileName, MAX_PATH, Dir);
1777 StringCchCat(fileName, MAX_PATH, _T(
"\\"));
1778 StringCchCat(fileName, MAX_PATH, FindFileData.cFileName);
1780 if (!wf_cliprdr_add_to_file_arrays(clipboard, fileName, pathLen))
1789 static UINT wf_cliprdr_send_client_capabilities(wfClipboard* clipboard)
1794 if (!clipboard || !clipboard->context || !clipboard->context->ClientCapabilities)
1795 return ERROR_INTERNAL_ERROR;
1797 capabilities.cCapabilitiesSets = 1;
1799 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1800 generalCapabilitySet.capabilitySetLength = 12;
1801 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1802 generalCapabilitySet.generalFlags =
1803 CB_USE_LONG_FORMAT_NAMES | CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS;
1804 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1812 static UINT wf_cliprdr_monitor_ready(CliprdrClientContext* context,
1816 wfClipboard* clipboard = (wfClipboard*)context->custom;
1818 if (!context || !monitorReady)
1819 return ERROR_INTERNAL_ERROR;
1821 clipboard->sync = TRUE;
1822 rc = wf_cliprdr_send_client_capabilities(clipboard);
1824 if (rc != CHANNEL_RC_OK)
1827 return cliprdr_send_format_list(clipboard);
1835 static UINT wf_cliprdr_server_capabilities(CliprdrClientContext* context,
1839 wfClipboard* clipboard = (wfClipboard*)context->custom;
1841 if (!context || !capabilities)
1842 return ERROR_INTERNAL_ERROR;
1844 for (UINT32 index = 0; index < capabilities->cCapabilitiesSets; index++)
1846 capabilitySet = &(capabilities->capabilitySets[index]);
1848 if ((capabilitySet->capabilitySetType == CB_CAPSTYPE_GENERAL) &&
1849 (capabilitySet->capabilitySetLength >= CB_CAPSTYPE_GENERAL_LEN))
1853 clipboard->capabilities = generalCapabilitySet->generalFlags;
1858 return CHANNEL_RC_OK;
1866 static UINT wf_cliprdr_server_format_list(CliprdrClientContext* context,
1869 UINT rc = ERROR_INTERNAL_ERROR;
1870 formatMapping* mapping;
1872 wfClipboard* clipboard = (wfClipboard*)context->custom;
1874 if (!clear_format_map(clipboard))
1875 return ERROR_INTERNAL_ERROR;
1877 for (UINT32 i = 0; i < formatList->numFormats; i++)
1879 format = &(formatList->formats[i]);
1880 mapping = &(clipboard->format_mappings[i]);
1881 mapping->remote_format_id = format->formatId;
1883 if (format->formatName)
1885 mapping->name = ConvertUtf8ToWCharAlloc(format->formatName, NULL);
1888 mapping->local_format_id = RegisterClipboardFormatW((LPWSTR)mapping->name);
1892 mapping->name = NULL;
1893 mapping->local_format_id = mapping->remote_format_id;
1896 clipboard->map_size++;
1897 map_ensure_capacity(clipboard);
1900 if (file_transferring(clipboard))
1902 if (PostMessage(clipboard->hwnd, WM_CLIPRDR_MESSAGE, OLE_SETCLIPBOARD, 0))
1907 if (!try_open_clipboard(clipboard->hwnd))
1908 return CHANNEL_RC_OK;
1910 if (EmptyClipboard())
1912 for (UINT32 i = 0; i < (UINT32)clipboard->map_size; i++)
1913 SetClipboardData(clipboard->format_mappings[i].local_format_id, NULL);
1918 if (!CloseClipboard() && GetLastError())
1919 return ERROR_INTERNAL_ERROR;
1931 wf_cliprdr_server_format_list_response(CliprdrClientContext* context,
1935 (void)formatListResponse;
1937 if (formatListResponse->common.msgFlags != CB_RESPONSE_OK)
1938 WLog_WARN(TAG,
"format list update failed");
1940 return CHANNEL_RC_OK;
1949 wf_cliprdr_server_lock_clipboard_data(CliprdrClientContext* context,
1953 (void)lockClipboardData;
1954 return CHANNEL_RC_OK;
1963 wf_cliprdr_server_unlock_clipboard_data(CliprdrClientContext* context,
1967 (void)unlockClipboardData;
1968 return CHANNEL_RC_OK;
1971 static BOOL wf_cliprdr_process_filename(wfClipboard* clipboard, WCHAR* wFileName,
size_t str_len)
1974 size_t offset = str_len;
1976 if (!clipboard || !wFileName)
1982 if (wFileName[offset] == L
'\\')
1988 pathLen = offset + 1;
1990 if (!wf_cliprdr_add_to_file_arrays(clipboard, wFileName, pathLen))
1993 if ((clipboard->fileDescriptor[clipboard->nFiles - 1]->dwFileAttributes &
1994 FILE_ATTRIBUTE_DIRECTORY) != 0)
1997 if (!wf_cliprdr_traverse_directory(clipboard, wFileName, pathLen))
2004 static SSIZE_T wf_cliprdr_tryopen(wfClipboard* clipboard, UINT32 requestedFormatId, BYTE** pData)
2007 WINPR_ASSERT(clipboard);
2008 WINPR_ASSERT(pData);
2013 if (!try_open_clipboard(clipboard->hwnd))
2016 HANDLE hClipdata = GetClipboardData(requestedFormatId);
2021 char* globlemem = (
char*)GlobalLock(hClipdata);
2022 const SSIZE_T size = GlobalSize(hClipdata);
2026 BYTE* buff = malloc(size);
2029 CopyMemory(buff, globlemem, size);
2034 GlobalUnlock(hClipdata);
2042 static SSIZE_T wf_cliprdr_get_filedescriptor(wfClipboard* clipboard, BYTE** pData)
2044 WINPR_ASSERT(clipboard);
2045 WINPR_ASSERT(pData);
2048 LPDATAOBJECT dataObj = NULL;
2049 FORMATETC format_etc = { 0 };
2050 STGMEDIUM stg_medium = { 0 };
2054 HRESULT result = OleGetClipboard(&dataObj);
2059 format_etc.cfFormat = CF_HDROP;
2060 format_etc.tymed = TYMED_HGLOBAL;
2061 format_etc.dwAspect = 1;
2062 format_etc.lindex = -1;
2063 result = IDataObject_GetData(dataObj, &format_etc, &stg_medium);
2067 DEBUG_CLIPRDR(
"dataObj->GetData failed.");
2071 HGLOBAL hdl = stg_medium.u.hGlobal;
2072 DROPFILES* dropFiles = (DROPFILES*)GlobalLock(hdl);
2076 ReleaseStgMedium(&stg_medium);
2077 clipboard->nFiles = 0;
2081 clear_file_array(clipboard);
2083 if (dropFiles->fWide)
2087 for (WCHAR* wFileName = (WCHAR*)((
char*)dropFiles + dropFiles->pFiles);
2088 (len = wcslen(wFileName)) > 0; wFileName += len + 1)
2090 wf_cliprdr_process_filename(clipboard, wFileName, wcslen(wFileName));
2096 for (
char* p = (
char*)((
char*)dropFiles + dropFiles->pFiles); (len = strlen(p)) > 0;
2097 p += len + 1, clipboard->nFiles++)
2099 const int cchWideChar = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, p, len, NULL, 0);
2100 WCHAR* wFileName = (LPWSTR)calloc(cchWideChar,
sizeof(WCHAR));
2101 MultiByteToWideChar(CP_ACP, MB_COMPOSITE, p, len, wFileName, cchWideChar);
2102 wf_cliprdr_process_filename(clipboard, wFileName, cchWideChar);
2108 ReleaseStgMedium(&stg_medium);
2111 const size_t size = 4ull + clipboard->nFiles *
sizeof(
FILEDESCRIPTORW);
2112 FILEGROUPDESCRIPTORW* groupDsc = (FILEGROUPDESCRIPTORW*)calloc(size, 1);
2116 groupDsc->cItems = clipboard->nFiles;
2118 for (
size_t i = 0; i < clipboard->nFiles; i++)
2120 if (clipboard->fileDescriptor[i])
2121 groupDsc->fgd[i] = *clipboard->fileDescriptor[i];
2124 *pData = (BYTE*)groupDsc;
2129 IDataObject_Release(dataObj);
2139 wf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2144 if (!context || !formatDataRequest)
2145 return ERROR_INTERNAL_ERROR;
2147 wfClipboard* clipboard = (wfClipboard*)context->custom;
2150 return ERROR_INTERNAL_ERROR;
2152 const UINT32 requestedFormatId = formatDataRequest->requestedFormatId;
2154 if (requestedFormatId == RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW))
2156 const SSIZE_T res = wf_cliprdr_get_filedescriptor(clipboard, &response.requestedFormatData);
2158 response.common.dataLen = (UINT32)res;
2163 wf_cliprdr_tryopen(clipboard, requestedFormatId, &response.requestedFormatData);
2165 response.common.dataLen = (UINT32)res;
2168 response.common.msgFlags = CB_RESPONSE_OK;
2170 const UINT rc = clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
2171 free(response.requestedFormatData);
2181 wf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2186 wfClipboard* clipboard;
2188 if (!context || !formatDataResponse)
2189 return ERROR_INTERNAL_ERROR;
2191 clipboard = (wfClipboard*)context->custom;
2194 return ERROR_INTERNAL_ERROR;
2196 if (formatDataResponse->common.msgFlags != CB_RESPONSE_OK)
2198 clipboard->hmem = NULL;
2200 if (!SetEvent(clipboard->response_data_event))
2201 return ERROR_INTERNAL_ERROR;
2203 return CHANNEL_RC_OK;
2206 hMem = GlobalAlloc(GMEM_MOVEABLE, formatDataResponse->common.dataLen);
2209 return ERROR_INTERNAL_ERROR;
2211 data = (BYTE*)GlobalLock(hMem);
2216 return ERROR_INTERNAL_ERROR;
2219 CopyMemory(data, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
2221 if (!GlobalUnlock(hMem) && GetLastError())
2224 return ERROR_INTERNAL_ERROR;
2227 clipboard->hmem = hMem;
2229 if (!SetEvent(clipboard->response_data_event))
2230 return ERROR_INTERNAL_ERROR;
2232 return CHANNEL_RC_OK;
2241 wf_cliprdr_server_file_contents_request(CliprdrClientContext* context,
2246 HRESULT hRet = S_OK;
2247 FORMATETC vFormatEtc = { 0 };
2248 LPDATAOBJECT pDataObj = NULL;
2249 STGMEDIUM vStgMedium = { 0 };
2250 BOOL bIsStreamFile = TRUE;
2251 static LPSTREAM pStreamStc = NULL;
2252 static UINT32 uStreamIdStc = 0;
2253 wfClipboard* clipboard;
2254 UINT rc = ERROR_INTERNAL_ERROR;
2258 if (!context || !fileContentsRequest)
2259 return ERROR_INTERNAL_ERROR;
2261 clipboard = (wfClipboard*)context->custom;
2264 return ERROR_INTERNAL_ERROR;
2266 cbRequested = fileContentsRequest->cbRequested;
2267 if (fileContentsRequest->dwFlags == FILECONTENTS_SIZE)
2268 cbRequested =
sizeof(UINT64);
2270 pData = (BYTE*)calloc(1, cbRequested);
2275 hRet = OleGetClipboard(&pDataObj);
2279 WLog_ERR(TAG,
"filecontents: get ole clipboard failed.");
2283 vFormatEtc.cfFormat = RegisterClipboardFormat(CFSTR_FILECONTENTS);
2284 vFormatEtc.tymed = TYMED_ISTREAM;
2285 vFormatEtc.dwAspect = 1;
2286 vFormatEtc.lindex = fileContentsRequest->listIndex;
2287 vFormatEtc.ptd = NULL;
2289 if ((uStreamIdStc != fileContentsRequest->streamId) || !pStreamStc)
2291 LPENUMFORMATETC pEnumFormatEtc;
2293 FORMATETC vFormatEtc2;
2297 IStream_Release(pStreamStc);
2301 bIsStreamFile = FALSE;
2302 hRet = IDataObject_EnumFormatEtc(pDataObj, DATADIR_GET, &pEnumFormatEtc);
2308 hRet = IEnumFORMATETC_Next(pEnumFormatEtc, 1, &vFormatEtc2, &CeltFetched);
2312 if (vFormatEtc2.cfFormat == RegisterClipboardFormat(CFSTR_FILECONTENTS))
2314 hRet = IDataObject_GetData(pDataObj, &vFormatEtc, &vStgMedium);
2318 pStreamStc = vStgMedium.u.pstm;
2319 uStreamIdStc = fileContentsRequest->streamId;
2320 bIsStreamFile = TRUE;
2326 }
while (hRet == S_OK);
2330 if (bIsStreamFile == TRUE)
2332 if (fileContentsRequest->dwFlags == FILECONTENTS_SIZE)
2334 STATSTG vStatStg = { 0 };
2335 hRet = IStream_Stat(pStreamStc, &vStatStg, STATFLAG_NONAME);
2339 *((UINT32*)&pData[0]) = vStatStg.cbSize.QuadPart & 0xFFFFFFFF;
2340 *((UINT32*)&pData[4]) = (vStatStg.cbSize.QuadPart >> 32) & 0xFFFFFFFF;
2341 uSize = cbRequested;
2344 else if (fileContentsRequest->dwFlags == FILECONTENTS_RANGE)
2348 dlibMove.QuadPart = (INT64)(((UINT64)fileContentsRequest->nPositionHigh << 32) |
2349 fileContentsRequest->nPositionLow);
2350 hRet = IStream_Seek(pStreamStc, dlibMove, STREAM_SEEK_SET, &dlibNewPosition);
2352 if (SUCCEEDED(hRet))
2353 hRet = IStream_Read(pStreamStc, pData, cbRequested, (PULONG)&uSize);
2358 if (fileContentsRequest->dwFlags == FILECONTENTS_SIZE)
2360 if (clipboard->nFiles <= fileContentsRequest->listIndex)
2362 *((UINT32*)&pData[0]) =
2363 clipboard->fileDescriptor[fileContentsRequest->listIndex]->nFileSizeLow;
2364 *((UINT32*)&pData[4]) =
2365 clipboard->fileDescriptor[fileContentsRequest->listIndex]->nFileSizeHigh;
2366 uSize = cbRequested;
2368 else if (fileContentsRequest->dwFlags == FILECONTENTS_RANGE)
2371 if (clipboard->nFiles <= fileContentsRequest->listIndex)
2373 bRet = wf_cliprdr_get_file_contents(
2374 clipboard->file_names[fileContentsRequest->listIndex], pData,
2375 fileContentsRequest->nPositionLow, fileContentsRequest->nPositionHigh, cbRequested,
2380 WLog_ERR(TAG,
"get file contents failed.");
2391 IDataObject_Release(pDataObj);
2400 cliprdr_send_response_filecontents(clipboard, fileContentsRequest->streamId, uSize, pData);
2403 if (sRc != CHANNEL_RC_OK)
2415 wf_cliprdr_server_file_contents_response(CliprdrClientContext* context,
2418 wfClipboard* clipboard;
2420 if (!context || !fileContentsResponse)
2421 return ERROR_INTERNAL_ERROR;
2423 if (fileContentsResponse->common.msgFlags != CB_RESPONSE_OK)
2426 clipboard = (wfClipboard*)context->custom;
2429 return ERROR_INTERNAL_ERROR;
2431 clipboard->req_fsize = fileContentsResponse->cbRequested;
2432 clipboard->req_fdata = (
char*)malloc(fileContentsResponse->cbRequested);
2434 if (!clipboard->req_fdata)
2435 return ERROR_INTERNAL_ERROR;
2437 CopyMemory(clipboard->req_fdata, fileContentsResponse->requestedData,
2438 fileContentsResponse->cbRequested);
2440 if (!SetEvent(clipboard->req_fevent))
2442 free(clipboard->req_fdata);
2443 return ERROR_INTERNAL_ERROR;
2446 return CHANNEL_RC_OK;
2449 BOOL wf_cliprdr_init(wfContext* wfc, CliprdrClientContext* cliprdr)
2451 wfClipboard* clipboard;
2452 rdpContext* context = (rdpContext*)wfc;
2454 if (!context || !cliprdr)
2457 wfc->clipboard = (wfClipboard*)calloc(1,
sizeof(wfClipboard));
2459 if (!wfc->clipboard)
2462 clipboard = wfc->clipboard;
2463 clipboard->wfc = wfc;
2464 clipboard->context = cliprdr;
2465 clipboard->channels = context->channels;
2466 clipboard->sync = FALSE;
2467 clipboard->map_capacity = 32;
2468 clipboard->map_size = 0;
2469 clipboard->hUser32 = LoadLibraryA(
"user32.dll");
2471 if (clipboard->hUser32)
2473 clipboard->AddClipboardFormatListener = GetProcAddressAs(
2474 clipboard->hUser32,
"AddClipboardFormatListener", fnAddClipboardFormatListener);
2475 clipboard->RemoveClipboardFormatListener = GetProcAddressAs(
2476 clipboard->hUser32,
"RemoveClipboardFormatListener", fnRemoveClipboardFormatListener);
2477 clipboard->GetUpdatedClipboardFormats = GetProcAddressAs(
2478 clipboard->hUser32,
"GetUpdatedClipboardFormats", fnGetUpdatedClipboardFormats);
2481 if (!(clipboard->hUser32 && clipboard->AddClipboardFormatListener &&
2482 clipboard->RemoveClipboardFormatListener && clipboard->GetUpdatedClipboardFormats))
2483 clipboard->legacyApi = TRUE;
2485 if (!(clipboard->format_mappings =
2486 (formatMapping*)calloc(clipboard->map_capacity,
sizeof(formatMapping))))
2489 if (!(clipboard->response_data_event = CreateEvent(NULL, TRUE, FALSE, NULL)))
2492 if (!(clipboard->req_fevent = CreateEvent(NULL, TRUE, FALSE, NULL)))
2495 if (!(clipboard->thread = CreateThread(NULL, 0, cliprdr_thread_func, clipboard, 0, NULL)))
2498 cliprdr->MonitorReady = wf_cliprdr_monitor_ready;
2499 cliprdr->ServerCapabilities = wf_cliprdr_server_capabilities;
2500 cliprdr->ServerFormatList = wf_cliprdr_server_format_list;
2501 cliprdr->ServerFormatListResponse = wf_cliprdr_server_format_list_response;
2502 cliprdr->ServerLockClipboardData = wf_cliprdr_server_lock_clipboard_data;
2503 cliprdr->ServerUnlockClipboardData = wf_cliprdr_server_unlock_clipboard_data;
2504 cliprdr->ServerFormatDataRequest = wf_cliprdr_server_format_data_request;
2505 cliprdr->ServerFormatDataResponse = wf_cliprdr_server_format_data_response;
2506 cliprdr->ServerFileContentsRequest = wf_cliprdr_server_file_contents_request;
2507 cliprdr->ServerFileContentsResponse = wf_cliprdr_server_file_contents_response;
2508 cliprdr->custom = (
void*)wfc->clipboard;
2511 wf_cliprdr_uninit(wfc, cliprdr);
2515 BOOL wf_cliprdr_uninit(wfContext* wfc, CliprdrClientContext* cliprdr)
2517 wfClipboard* clipboard;
2519 if (!wfc || !cliprdr)
2522 clipboard = wfc->clipboard;
2527 cliprdr->custom = NULL;
2529 if (clipboard->hwnd)
2530 PostMessage(clipboard->hwnd, WM_QUIT, 0, 0);
2532 if (clipboard->thread)
2534 (void)WaitForSingleObject(clipboard->thread, INFINITE);
2535 (void)CloseHandle(clipboard->thread);
2538 if (clipboard->response_data_event)
2539 (void)CloseHandle(clipboard->response_data_event);
2541 if (clipboard->req_fevent)
2542 (void)CloseHandle(clipboard->req_fevent);
2544 clear_file_array(clipboard);
2545 clear_format_map(clipboard);
2546 free(clipboard->format_mappings);