Blame SOURCES/0001-wayland-Convert-URI-to-local-path-for-DropFile.patch

e54a86
From 9a2bbd8acbebcde56dd2f89ebca3b196c2b38914 Mon Sep 17 00:00:00 2001
e54a86
From: Ethan Lee <flibitijibibo@gmail.com>
e54a86
Date: Wed, 12 Jan 2022 13:01:05 -0500
e54a86
Subject: [PATCH 01/11] wayland: Convert URI to local path for DropFile
e54a86
e54a86
---
e54a86
 src/video/wayland/SDL_waylandevents.c | 149 ++++++++++++++++++++++----
e54a86
 1 file changed, 129 insertions(+), 20 deletions(-)
e54a86
e54a86
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
e54a86
index dfa62ec69..bf2aeea5b 100644
e54a86
--- a/src/video/wayland/SDL_waylandevents.c
e54a86
+++ b/src/video/wayland/SDL_waylandevents.c
e54a86
@@ -1312,36 +1312,145 @@ data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
e54a86
 {
e54a86
 }
e54a86
 
e54a86
+/* Decodes URI escape sequences in string buf of len bytes
e54a86
+   (excluding the terminating NULL byte) in-place. Since
e54a86
+   URI-encoded characters take three times the space of
e54a86
+   normal characters, this should not be an issue.
e54a86
+
e54a86
+   Returns the number of decoded bytes that wound up in
e54a86
+   the buffer, excluding the terminating NULL byte.
e54a86
+
e54a86
+   The buffer is guaranteed to be NULL-terminated but
e54a86
+   may contain embedded NULL bytes.
e54a86
+
e54a86
+   On error, -1 is returned.
e54a86
+
e54a86
+   FIXME: This was shamelessly copied from SDL_x11events.c
e54a86
+ */
e54a86
+static int Wayland_URIDecode(char *buf, int len) {
e54a86
+    int ri, wi, di;
e54a86
+    char decode = '\0';
e54a86
+    if (buf == NULL || len < 0) {
e54a86
+        errno = EINVAL;
e54a86
+        return -1;
e54a86
+    }
e54a86
+    if (len == 0) {
e54a86
+        len = SDL_strlen(buf);
e54a86
+    }
e54a86
+    for (ri = 0, wi = 0, di = 0; ri < len && wi < len; ri += 1) {
e54a86
+        if (di == 0) {
e54a86
+            /* start decoding */
e54a86
+            if (buf[ri] == '%') {
e54a86
+                decode = '\0';
e54a86
+                di += 1;
e54a86
+                continue;
e54a86
+            }
e54a86
+            /* normal write */
e54a86
+            buf[wi] = buf[ri];
e54a86
+            wi += 1;
e54a86
+            continue;
e54a86
+        } else if (di == 1 || di == 2) {
e54a86
+            char off = '\0';
e54a86
+            char isa = buf[ri] >= 'a' && buf[ri] <= 'f';
e54a86
+            char isA = buf[ri] >= 'A' && buf[ri] <= 'F';
e54a86
+            char isn = buf[ri] >= '0' && buf[ri] <= '9';
e54a86
+            if (!(isa || isA || isn)) {
e54a86
+                /* not a hexadecimal */
e54a86
+                int sri;
e54a86
+                for (sri = ri - di; sri <= ri; sri += 1) {
e54a86
+                    buf[wi] = buf[sri];
e54a86
+                    wi += 1;
e54a86
+                }
e54a86
+                di = 0;
e54a86
+                continue;
e54a86
+            }
e54a86
+            /* itsy bitsy magicsy */
e54a86
+            if (isn) {
e54a86
+                off = 0 - '0';
e54a86
+            } else if (isa) {
e54a86
+                off = 10 - 'a';
e54a86
+            } else if (isA) {
e54a86
+                off = 10 - 'A';
e54a86
+            }
e54a86
+            decode |= (buf[ri] + off) << (2 - di) * 4;
e54a86
+            if (di == 2) {
e54a86
+                buf[wi] = decode;
e54a86
+                wi += 1;
e54a86
+                di = 0;
e54a86
+            } else {
e54a86
+                di += 1;
e54a86
+            }
e54a86
+            continue;
e54a86
+        }
e54a86
+    }
e54a86
+    buf[wi] = '\0';
e54a86
+    return wi;
e54a86
+}
e54a86
+
e54a86
+/* Convert URI to local filename
e54a86
+   return filename if possible, else NULL
e54a86
+
e54a86
+   FIXME: This was shamelessly copied from SDL_x11events.c
e54a86
+*/
e54a86
+static char* Wayland_URIToLocal(char* uri) {
e54a86
+    char *file = NULL;
e54a86
+    SDL_bool local;
e54a86
+
e54a86
+    if (SDL_memcmp(uri,"file:/",6) == 0) uri += 6;      /* local file? */
e54a86
+    else if (SDL_strstr(uri,":/") != NULL) return file; /* wrong scheme */
e54a86
+
e54a86
+    local = uri[0] != '/' || (uri[0] != '\0' && uri[1] == '/');
e54a86
+
e54a86
+    /* got a hostname? */
e54a86
+    if (!local && uri[0] == '/' && uri[2] != '/') {
e54a86
+      char* hostname_end = SDL_strchr(uri+1, '/');
e54a86
+      if (hostname_end != NULL) {
e54a86
+          char hostname[ 257 ];
e54a86
+          if (gethostname(hostname, 255) == 0) {
e54a86
+            hostname[ 256 ] = '\0';
e54a86
+            if (SDL_memcmp(uri+1, hostname, hostname_end - (uri+1)) == 0) {
e54a86
+                uri = hostname_end + 1;
e54a86
+                local = SDL_TRUE;
e54a86
+            }
e54a86
+          }
e54a86
+      }
e54a86
+    }
e54a86
+    if (local) {
e54a86
+      file = uri;
e54a86
+      /* Convert URI escape sequences to real characters */
e54a86
+      Wayland_URIDecode(file, 0);
e54a86
+      if (uri[1] == '/') {
e54a86
+          file++;
e54a86
+      } else {
e54a86
+          file--;
e54a86
+      }
e54a86
+    }
e54a86
+    return file;
e54a86
+}
e54a86
+
e54a86
 static void
e54a86
 data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
e54a86
 {
e54a86
     SDL_WaylandDataDevice *data_device = data;
e54a86
-    void *buffer = NULL;
e54a86
-    size_t length = 0;
e54a86
-
e54a86
-    const char *current_uri = NULL;
e54a86
-    const char *last_char = NULL;
e54a86
-    char *current_char = NULL;
e54a86
 
e54a86
     if (data_device->drag_offer != NULL) {
e54a86
         /* TODO: SDL Support more mime types */
e54a86
-        buffer = Wayland_data_offer_receive(data_device->drag_offer,
e54a86
-                                            &length, FILE_MIME, SDL_FALSE);
e54a86
-
e54a86
-        /* uri-list */
e54a86
-        current_uri = (const char *)buffer;
e54a86
-        last_char = (const char *)buffer + length;
e54a86
-        for (current_char = buffer; current_char < last_char; ++current_char) {
e54a86
-            if (*current_char == '\n' || *current_char == 0) {
e54a86
-                if (*current_uri != 0 && *current_uri != '#') {
e54a86
-                    *current_char = 0;
e54a86
-                    SDL_SendDropFile(NULL, current_uri);
e54a86
+        size_t length;
e54a86
+        void *buffer = Wayland_data_offer_receive(data_device->drag_offer,
e54a86
+                                                  &length, FILE_MIME, SDL_FALSE);
e54a86
+        if (buffer) {
e54a86
+            char *saveptr = NULL;
e54a86
+            char *token = SDL_strtokr((char *) buffer, "\r\n", &saveptr);
e54a86
+            while (token != NULL) {
e54a86
+                char *fn = Wayland_URIToLocal(token);
e54a86
+                if (fn) {
e54a86
+                    SDL_SendDropFile(NULL, fn); /* FIXME: Window? */
e54a86
                 }
e54a86
-                current_uri = (const char *)current_char + 1;
e54a86
+                token = SDL_strtokr(NULL, "\r\n", &saveptr);
e54a86
             }
e54a86
+            SDL_SendDropComplete(NULL); /* FIXME: Window? */
e54a86
+            SDL_free(buffer);
e54a86
         }
e54a86
-
e54a86
-        SDL_free(buffer);
e54a86
     }
e54a86
 }
e54a86
 
e54a86
-- 
e54a86
2.34.1
e54a86