From 48d41b96e9593208ccc82990ac536660b956188a Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Fri, 16 Oct 2026 00:39:55 -0400
Subject: [PATCH] cocoa: Synthesize text/uri-list from pasteboard URLs
If there is no explicit text/uri-list MIME type, and the pasteboard contains URLs, synthesize the text/uri-list contents from the URLs.
---
src/video/cocoa/SDL_cocoaclipboard.m | 60 +++++++++++++++++++++++++++-
1 file changed, 59 insertions(+), 1 deletion(-)
diff --git a/src/video/cocoa/SDL_cocoaclipboard.m b/src/video/cocoa/SDL_cocoaclipboard.m
index 9f785c6be3a50..54bcd68f78295 100644
--- a/src/video/cocoa/SDL_cocoaclipboard.m
+++ b/src/video/cocoa/SDL_cocoaclipboard.m
@@ -71,15 +71,44 @@ - (void)pasteboard:(NSPasteboard *)pasteboard
@end
+static void *GetTextURIsFromURLs(NSPasteboard *pasteboard, size_t *size)
+{
+ @autoreleasepool {
+ NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey: @YES};
+ NSArray<NSURL *> *urls = [pasteboard readObjectsForClasses:@[[NSURL class]] options:options];
+ NSMutableString *string = [NSMutableString string];
+
+ for (NSURL *url in urls) {
+ [string appendFormat:@"%@\r\n", [[url filePathURL] absoluteString]];
+ }
+
+ if ([string length]) {
+ // Copy to the destination, trimming the trailing '/r/n'.
+ *size = [string length] - 1;
+ char *buf = SDL_malloc(*size);
+ if (buf) {
+ SDL_strlcpy(buf, [string UTF8String], *size);
+ return buf;
+ }
+ }
+
+ return NULL;
+ }
+}
+
static char **GetMimeTypes(int *pnformats)
{
+ NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
char **new_mime_types = NULL;
*pnformats = 0;
int nformats = 0;
int formatsSz = 0;
- NSArray<NSPasteboardItem *> *items = [[NSPasteboard generalPasteboard] pasteboardItems];
+ NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey : @YES};
+ const bool has_urls = [pasteboard canReadObjectForClasses:@[ [NSURL class] ] options:options];
+ NSArray<NSPasteboardItem *> *items = [pasteboard pasteboardItems];
+ bool has_text_uri = false;
NSUInteger nitems = [items count];
if (nitems > 0) {
for (NSPasteboardItem *item in items) {
@@ -92,6 +121,9 @@ - (void)pasteboard:(NSPasteboard *)pasteboard
NSUInteger len = [mime_type lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
formatsSz += len;
++nformats;
+ if (!has_text_uri && [mime_type isEqualToString:@"text/uri-list"]) {
+ has_text_uri = true;
+ }
}
}
NSUInteger len = [type lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
@@ -100,6 +132,13 @@ - (void)pasteboard:(NSPasteboard *)pasteboard
}
}
+ // Synthesize the "text/uri-list" type from URLs, if only the latter is present.
+ if (has_urls && !has_text_uri) {
+ NSUInteger len = [@"text/uri-list" lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
+ formatsSz += len;
+ ++nformats;
+ }
+
new_mime_types = SDL_AllocateTemporaryMemory((nformats + 1) * sizeof(char *) + formatsSz);
if (new_mime_types) {
int i = 0;
@@ -124,6 +163,14 @@ - (void)pasteboard:(NSPasteboard *)pasteboard
}
}
+ if (has_urls && !has_text_uri) {
+ NSString *text_uri_list = @"text/uri-list";
+ NSUInteger len = [text_uri_list lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
+ SDL_memcpy(strPtr, [text_uri_list UTF8String], len);
+ new_mime_types[i++] = strPtr;
+ strPtr += len;
+ }
+
new_mime_types[nformats] = NULL;
*pnformats = nformats;
}
@@ -252,6 +299,11 @@ static CFStringRef GetUTIType(const char *tag)
break;
}
}
+
+ if (!data && SDL_strcmp(mime_type, "text/uri-list") == 0) {
+ // Try to synthesize text/uri-list entries from public.file-url.
+ data = GetTextURIsFromURLs(pasteboard, size);
+ }
return data;
}
}
@@ -266,6 +318,12 @@ bool Cocoa_HasClipboardData(SDL_VideoDevice *_this, const char *mime_type)
result = true;
}
CFRelease(utiType);
+
+ // If there's no text/uri-list type, see if URLs are available.
+ if (!result && SDL_strcmp(mime_type, "text/uri-list") == 0) {
+ NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey: @YES};
+ result = [pasteboard canReadObjectForClasses:@[ [NSURL class] ] options:options];
+ }
}
return result;