20#include <winpr/config.h>
23#include <winpr/collections.h>
24#include <winpr/wlog.h>
26#include <winpr/clipboard.h>
30#include "synthetic_file.h"
33#define TAG WINPR_TAG("clipboard")
35const char* mime_text_plain =
"text/plain";
45static const char* CF_STANDARD_STRINGS[] = {
66const char* ClipboardGetFormatIdString(UINT32 formatId)
68 if (formatId < ARRAYSIZE(CF_STANDARD_STRINGS))
69 return CF_STANDARD_STRINGS[formatId];
70 return "CF_REGISTERED_FORMAT";
73static wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId,
83 for (UINT32 index = 0; index < clipboard->numFormats; index++)
86 if (formatId == cformat->formatId)
95 for (UINT32 index = 0; index < clipboard->numFormats; index++)
98 if (!cformat->formatName)
101 if (strcmp(name, cformat->formatName) == 0)
111 if (clipboard->numFormats > 0)
113 format = &clipboard->formats[0];
115 if (format->formatId)
118 if (!format->formatName || (strcmp(format->formatName, CF_STANDARD_STRINGS[0]) == 0))
131 for (UINT32 index = 0; index < format->numSynthesizers; index++)
135 if (formatId == synthesizer->syntheticId)
142void ClipboardLock(wClipboard* clipboard)
147 EnterCriticalSection(&(clipboard->lock));
150void ClipboardUnlock(wClipboard* clipboard)
155 LeaveCriticalSection(&(clipboard->lock));
158BOOL ClipboardEmpty(wClipboard* clipboard)
165 free(clipboard->data);
166 clipboard->data = NULL;
170 clipboard->formatId = 0;
171 clipboard->sequenceNumber++;
175UINT32 ClipboardCountRegisteredFormats(wClipboard* clipboard)
180 return clipboard->numFormats;
183UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard, UINT32** ppFormatIds)
185 UINT32* pFormatIds = NULL;
194 pFormatIds = *ppFormatIds;
198 pFormatIds = calloc(clipboard->numFormats,
sizeof(UINT32));
203 *ppFormatIds = pFormatIds;
206 for (UINT32 index = 0; index < clipboard->numFormats; index++)
208 format = &(clipboard->formats[index]);
209 pFormatIds[index] = format->formatId;
212 return clipboard->numFormats;
215UINT32 ClipboardRegisterFormat(wClipboard* clipboard,
const char* name)
222 format = ClipboardFindFormat(clipboard, 0, name);
225 return format->formatId;
227 if ((clipboard->numFormats + 1) >= clipboard->maxFormats)
229 UINT32 numFormats = clipboard->maxFormats * 2;
237 clipboard->formats = tmpFormat;
238 clipboard->maxFormats = numFormats;
241 format = &(clipboard->formats[clipboard->numFormats]);
246 format->formatName = _strdup(name);
248 if (!format->formatName)
252 format->formatId = clipboard->nextFormatId++;
253 clipboard->numFormats++;
254 return format->formatId;
257BOOL ClipboardRegisterSynthesizer(wClipboard* clipboard, UINT32 formatId, UINT32 syntheticId,
258 CLIPBOARD_SYNTHESIZE_FN pfnSynthesize)
267 format = ClipboardFindFormat(clipboard, formatId, NULL);
272 if (format->formatId == syntheticId)
275 synthesizer = ClipboardFindSynthesizer(format, formatId);
280 UINT32 numSynthesizers = format->numSynthesizers + 1;
287 format->synthesizers = tmpSynthesizer;
288 format->numSynthesizers = numSynthesizers;
289 index = numSynthesizers - 1;
290 synthesizer = &(format->synthesizers[index]);
293 synthesizer->syntheticId = syntheticId;
294 synthesizer->pfnSynthesize = pfnSynthesize;
298UINT32 ClipboardCountFormats(wClipboard* clipboard)
306 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
311 count = 1 + format->numSynthesizers;
315UINT32 ClipboardGetFormatIds(wClipboard* clipboard, UINT32** ppFormatIds)
318 UINT32* pFormatIds = NULL;
325 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
330 count = 1 + format->numSynthesizers;
335 pFormatIds = *ppFormatIds;
339 pFormatIds = calloc(count,
sizeof(UINT32));
344 *ppFormatIds = pFormatIds;
347 pFormatIds[0] = format->formatId;
349 for (UINT32 index = 1; index < count; index++)
351 synthesizer = &(format->synthesizers[index - 1]);
352 pFormatIds[index] = synthesizer->syntheticId;
358static void ClipboardUninitFormats(wClipboard* clipboard)
360 WINPR_ASSERT(clipboard);
361 for (UINT32 formatId = 0; formatId < clipboard->numFormats; formatId++)
364 free(format->formatName);
365 free(format->synthesizers);
366 format->formatName = NULL;
367 format->synthesizers = NULL;
371static BOOL ClipboardInitFormats(wClipboard* clipboard)
379 for (formatId = 0; formatId < CF_MAX; formatId++, clipboard->numFormats++)
381 format = &(clipboard->formats[clipboard->numFormats]);
383 format->formatId = formatId;
384 format->formatName = _strdup(CF_STANDARD_STRINGS[formatId]);
386 if (!format->formatName)
390 if (!ClipboardInitSynthesizers(clipboard))
396 ClipboardUninitFormats(clipboard);
400UINT32 ClipboardGetFormatId(wClipboard* clipboard,
const char* name)
407 format = ClipboardFindFormat(clipboard, 0, name);
412 return format->formatId;
415const char* ClipboardGetFormatName(wClipboard* clipboard, UINT32 formatId)
422 format = ClipboardFindFormat(clipboard, formatId, NULL);
427 return format->formatName;
430void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize)
434 void* pSrcData = NULL;
435 void* pDstData = NULL;
439 if (!clipboard || !pSize)
441 WLog_ERR(TAG,
"Invalid parameters clipboard=%p, pSize=%p", clipboard, pSize);
446 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
450 WLog_ERR(TAG,
"Format [0x%08" PRIx32
"] not found", clipboard->formatId);
454 SrcSize = clipboard->size;
455 pSrcData = clipboard->data;
457 if (formatId == format->formatId)
460 pDstData = malloc(DstSize);
465 CopyMemory(pDstData, pSrcData, SrcSize);
470 synthesizer = ClipboardFindSynthesizer(format, formatId);
472 if (!synthesizer || !synthesizer->pfnSynthesize)
474 WLog_ERR(TAG,
"No synthesizer for format %s [0x%08" PRIx32
"] --> %s [0x%08" PRIx32
"]",
475 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
476 ClipboardGetFormatName(clipboard, formatId), formatId);
481 pDstData = synthesizer->pfnSynthesize(clipboard, format->formatId, pSrcData, &DstSize);
486 WLog_DBG(TAG,
"getting formatId=%s [0x%08" PRIx32
"] data=%p, size=%" PRIu32,
487 ClipboardGetFormatName(clipboard, formatId), formatId, pDstData, *pSize);
491BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId,
const void* data, UINT32 size)
495 WLog_DBG(TAG,
"setting formatId=%s [0x%08" PRIx32
"], size=%" PRIu32,
496 ClipboardGetFormatName(clipboard, formatId), formatId, size);
500 format = ClipboardFindFormat(clipboard, formatId, NULL);
505 free(clipboard->data);
507 clipboard->data = calloc(size +
sizeof(WCHAR),
sizeof(
char));
509 if (!clipboard->data)
512 memcpy(clipboard->data, data, size);
521 clipboard->size = (UINT32)(strnlen(clipboard->data, size) + 1UL);
525 (UINT32)((_wcsnlen(clipboard->data, size /
sizeof(WCHAR)) + 1UL) *
sizeof(WCHAR));
528 clipboard->size = size;
532 clipboard->formatId = formatId;
533 clipboard->sequenceNumber++;
537UINT64 ClipboardGetOwner(wClipboard* clipboard)
542 return clipboard->ownerId;
545void ClipboardSetOwner(wClipboard* clipboard, UINT64 ownerId)
550 clipboard->ownerId = ownerId;
553wClipboardDelegate* ClipboardGetDelegate(wClipboard* clipboard)
558 return &clipboard->delegate;
561static void ClipboardInitLocalFileSubsystem(wClipboard* clipboard)
567 if (ClipboardInitSyntheticFileSubsystem(clipboard))
569 WLog_DBG(TAG,
"initialized synthetic local file subsystem");
574 WLog_WARN(TAG,
"failed to initialize synthetic local file subsystem");
577 WLog_INFO(TAG,
"failed to initialize local file subsystem, file transfer not available");
580wClipboard* ClipboardCreate(
void)
582 wClipboard* clipboard = (wClipboard*)calloc(1,
sizeof(wClipboard));
587 clipboard->nextFormatId = 0xC000;
588 clipboard->sequenceNumber = 0;
590 if (!InitializeCriticalSectionAndSpinCount(&(clipboard->lock), 4000))
593 clipboard->numFormats = 0;
594 clipboard->maxFormats = 64;
597 if (!clipboard->formats)
600 if (!ClipboardInitFormats(clipboard))
603 clipboard->delegate.clipboard = clipboard;
604 ClipboardInitLocalFileSubsystem(clipboard);
607 ClipboardDestroy(clipboard);
611void ClipboardDestroy(wClipboard* clipboard)
616 ArrayList_Free(clipboard->localFiles);
617 clipboard->localFiles = NULL;
619 ClipboardUninitFormats(clipboard);
621 free(clipboard->data);
622 clipboard->data = NULL;
624 clipboard->numFormats = 0;
625 free(clipboard->formats);
626 DeleteCriticalSection(&(clipboard->lock));
630static BOOL is_dos_drive(
const char* path,
size_t len)
636 if (path[1] ==
':' || path[1] ==
'|')
638 if (((path[0] >=
'A') && (path[0] <=
'Z')) || ((path[0] >=
'a') && (path[0] <=
'z')))
644char* parse_uri_to_local_file(
const char* uri,
size_t uri_len)
647 const char prefix[] =
"file:";
648 const char prefixTraditional[] =
"file://";
649 const char* localName = NULL;
652 const size_t prefixLen = strnlen(prefix,
sizeof(prefix));
653 const size_t prefixTraditionalLen = strnlen(prefixTraditional,
sizeof(prefixTraditional));
655 WINPR_ASSERT(uri || (uri_len == 0));
657 WLog_VRB(TAG,
"processing URI: %.*s", uri_len, uri);
659 if ((uri_len <= prefixLen) || strncmp(uri, prefix, prefixLen) != 0)
661 WLog_ERR(TAG,
"non-'file:' URI schemes are not supported");
680 if (uri[prefixLen] !=
'/')
683 if (is_dos_drive(&uri[prefixLen], uri_len - prefixLen))
686 localName = &uri[prefixLen];
687 localLen = uri_len - prefixLen;
692 WLog_ERR(TAG,
"URI format are not supported: %s", uri);
704 else if ((uri_len > prefixLen + 1) && (uri[prefixLen + 1] !=
'/'))
706 if (is_dos_drive(&uri[prefixLen + 1], uri_len - prefixLen - 1))
709 localName = (uri + prefixLen + 1);
710 localLen = uri_len - prefixLen - 1;
714 localName = &uri[prefixLen];
715 localLen = uri_len - prefixLen;
725 if ((uri_len < prefixTraditionalLen) ||
726 strncmp(uri, prefixTraditional, prefixTraditionalLen) != 0)
728 WLog_ERR(TAG,
"non-'file:' URI schemes are not supported");
732 localName = &uri[prefixTraditionalLen];
733 localLen = uri_len - prefixTraditionalLen;
737 WLog_ERR(TAG,
"empty 'file:' URI schemes are not supported");
745 if (localName[0] !=
'/')
747 WLog_ERR(TAG,
"URI format are not supported: %s", uri);
751 if (is_dos_drive(&localName[1], localLen - 1))
759 buffer = winpr_str_url_decode(localName, localLen);
762 if (buffer[1] ==
'|' &&
763 ((buffer[0] >=
'A' && buffer[0] <=
'Z') || (buffer[0] >=
'a' && buffer[0] <=
'z')))