20 #include <winpr/config.h>
22 #include <winpr/crt.h>
23 #include <winpr/collections.h>
24 #include <winpr/wlog.h>
26 #include <winpr/clipboard.h>
28 #include "clipboard.h"
30 #include "synthetic_file.h"
33 #define TAG WINPR_TAG("clipboard")
35 const char* mime_text_plain =
"text/plain";
45 static const char* CF_STANDARD_STRINGS[] = {
66 const char* ClipboardGetFormatIdString(UINT32 formatId)
68 if (formatId < ARRAYSIZE(CF_STANDARD_STRINGS))
69 return CF_STANDARD_STRINGS[formatId];
70 return "CF_REGISTERED_FORMAT";
73 static 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)
142 void ClipboardLock(wClipboard* clipboard)
147 EnterCriticalSection(&(clipboard->lock));
150 void ClipboardUnlock(wClipboard* clipboard)
155 LeaveCriticalSection(&(clipboard->lock));
158 BOOL ClipboardEmpty(wClipboard* clipboard)
165 free(clipboard->data);
166 clipboard->data = NULL;
170 clipboard->formatId = 0;
171 clipboard->sequenceNumber++;
175 UINT32 ClipboardCountRegisteredFormats(wClipboard* clipboard)
180 return clipboard->numFormats;
183 UINT32 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;
215 UINT32 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;
257 BOOL 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;
298 UINT32 ClipboardCountFormats(wClipboard* clipboard)
306 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
311 count = 1 + format->numSynthesizers;
315 UINT32 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;
358 static 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;
371 static 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);
400 UINT32 ClipboardGetFormatId(wClipboard* clipboard,
const char* name)
407 format = ClipboardFindFormat(clipboard, 0, name);
412 return format->formatId;
415 const char* ClipboardGetFormatName(wClipboard* clipboard, UINT32 formatId)
422 format = ClipboardFindFormat(clipboard, formatId, NULL);
427 return format->formatName;
430 void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize)
434 void* pSrcData = NULL;
435 void* pDstData = NULL;
446 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
451 SrcSize = clipboard->size;
452 pSrcData = clipboard->data;
454 if (formatId == format->formatId)
457 pDstData = malloc(DstSize);
462 CopyMemory(pDstData, pSrcData, SrcSize);
467 synthesizer = ClipboardFindSynthesizer(format, formatId);
469 if (!synthesizer || !synthesizer->pfnSynthesize)
473 pDstData = synthesizer->pfnSynthesize(clipboard, format->formatId, pSrcData, &DstSize);
481 BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId,
const void* data, UINT32 size)
488 format = ClipboardFindFormat(clipboard, formatId, NULL);
493 free(clipboard->data);
494 clipboard->data = malloc(size);
496 if (!clipboard->data)
499 memcpy(clipboard->data, data, size);
500 clipboard->size = size;
501 clipboard->formatId = formatId;
502 clipboard->sequenceNumber++;
506 UINT64 ClipboardGetOwner(wClipboard* clipboard)
511 return clipboard->ownerId;
514 void ClipboardSetOwner(wClipboard* clipboard, UINT64 ownerId)
519 clipboard->ownerId = ownerId;
522 wClipboardDelegate* ClipboardGetDelegate(wClipboard* clipboard)
527 return &clipboard->delegate;
530 static void ClipboardInitLocalFileSubsystem(wClipboard* clipboard)
536 if (ClipboardInitSyntheticFileSubsystem(clipboard))
538 WLog_DBG(TAG,
"initialized synthetic local file subsystem");
543 WLog_WARN(TAG,
"failed to initialize synthetic local file subsystem");
546 WLog_INFO(TAG,
"failed to initialize local file subsystem, file transfer not available");
549 wClipboard* ClipboardCreate(
void)
551 wClipboard* clipboard = (wClipboard*)calloc(1,
sizeof(wClipboard));
556 clipboard->nextFormatId = 0xC000;
557 clipboard->sequenceNumber = 0;
559 if (!InitializeCriticalSectionAndSpinCount(&(clipboard->lock), 4000))
562 clipboard->numFormats = 0;
563 clipboard->maxFormats = 64;
566 if (!clipboard->formats)
569 if (!ClipboardInitFormats(clipboard))
572 clipboard->delegate.clipboard = clipboard;
573 ClipboardInitLocalFileSubsystem(clipboard);
576 ClipboardDestroy(clipboard);
580 void ClipboardDestroy(wClipboard* clipboard)
585 ArrayList_Free(clipboard->localFiles);
586 clipboard->localFiles = NULL;
588 ClipboardUninitFormats(clipboard);
590 free(clipboard->data);
591 clipboard->data = NULL;
593 clipboard->numFormats = 0;
594 free(clipboard->formats);
595 DeleteCriticalSection(&(clipboard->lock));
599 static BOOL is_dos_drive(
const char* path,
size_t len)
605 if (path[1] ==
':' || path[1] ==
'|')
607 if (((path[0] >=
'A') && (path[0] <=
'Z')) || ((path[0] >=
'a') && (path[0] <=
'z')))
613 char* parse_uri_to_local_file(
const char* uri,
size_t uri_len)
616 const char prefix[] =
"file:";
617 const char prefixTraditional[] =
"file://";
618 const char* localName = NULL;
621 const size_t prefixLen = strnlen(prefix,
sizeof(prefix));
622 const size_t prefixTraditionalLen = strnlen(prefixTraditional,
sizeof(prefixTraditional));
624 WINPR_ASSERT(uri || (uri_len == 0));
626 WLog_VRB(TAG,
"processing URI: %.*s", uri_len, uri);
628 if ((uri_len <= prefixLen) || strncmp(uri, prefix, prefixLen) != 0)
630 WLog_ERR(TAG,
"non-'file:' URI schemes are not supported");
649 if (uri[prefixLen] !=
'/')
652 if (is_dos_drive(&uri[prefixLen], uri_len - prefixLen))
655 localName = &uri[prefixLen];
656 localLen = uri_len - prefixLen;
661 WLog_ERR(TAG,
"URI format are not supported: %s", uri);
673 else if ((uri_len > prefixLen + 1) && (uri[prefixLen + 1] !=
'/'))
675 if (is_dos_drive(&uri[prefixLen + 1], uri_len - prefixLen - 1))
678 localName = (uri + prefixLen + 1);
679 localLen = uri_len - prefixLen - 1;
683 localName = &uri[prefixLen];
684 localLen = uri_len - prefixLen;
694 if ((uri_len < prefixTraditionalLen) ||
695 strncmp(uri, prefixTraditional, prefixTraditionalLen) != 0)
697 WLog_ERR(TAG,
"non-'file:' URI schemes are not supported");
701 localName = &uri[prefixTraditionalLen];
702 localLen = uri_len - prefixTraditionalLen;
706 WLog_ERR(TAG,
"empty 'file:' URI schemes are not supported");
714 if (localName[0] !=
'/')
716 WLog_ERR(TAG,
"URI format are not supported: %s", uri);
720 if (is_dos_drive(&localName[1], localLen - 1))
728 buffer = winpr_str_url_decode(localName, localLen);
731 if (buffer[1] ==
'|' &&
732 ((buffer[0] >=
'A' && buffer[0] <=
'Z') || (buffer[0] >=
'a' && buffer[0] <=
'z')))