SDL: wayland: Handle drop event data reads asynchronously

From d0183f0ee3b2497f49c6322c62354d114c22af29 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Wed, 14 Oct 2026 14:09:55 -0400
Subject: [PATCH] wayland: Handle drop event data reads asynchronously

Previously, drop events had a hard timeout value of 14ms to prevent blocking the main event loop for too long while reading remote data. This timeout could be too short in some cases, leading to failed drop events, so the timeout has been increased, and drop events read the remote data asynchronously to prevent blocking the main event loop for excessive periods.
---
 src/video/wayland/SDL_waylandclipboard.c   |   2 +-
 src/video/wayland/SDL_waylanddatamanager.c |  23 +--
 src/video/wayland/SDL_waylanddatamanager.h |  18 +-
 src/video/wayland/SDL_waylandevents.c      | 186 ++++++++++++------
 src/video/wayland/SDL_waylandeventthread.c | 211 +++++++++++++++++++++
 src/video/wayland/SDL_waylandeventthread.h |   8 +
 src/video/wayland/SDL_waylandvideo.c       |   8 +
 src/video/wayland/SDL_waylandvideo.h       |   1 +
 8 files changed, 379 insertions(+), 78 deletions(-)

diff --git a/src/video/wayland/SDL_waylandclipboard.c b/src/video/wayland/SDL_waylandclipboard.c
index ea86f4424fa58..3922dd0acb8ba 100644
--- a/src/video/wayland/SDL_waylandclipboard.c
+++ b/src/video/wayland/SDL_waylandclipboard.c
@@ -90,7 +90,7 @@ void *Wayland_GetClipboardData(SDL_VideoDevice *_this, const char *mime_type, si
         if (data_device->selection_source) {
             buffer = SDL_GetInternalClipboardData(_this, mime_type, length);
         } else if (Wayland_DataOfferHasMIME(data_device->selection_offer, mime_type)) {
-            buffer = Wayland_DataOfferReceive(data_device->selection_offer, mime_type, length, true);
+            buffer = Wayland_DataOfferReceive(data_device->selection_offer, mime_type, length);
         }
     }
 
diff --git a/src/video/wayland/SDL_waylanddatamanager.c b/src/video/wayland/SDL_waylanddatamanager.c
index e960dba62f71a..b31f1cff95111 100644
--- a/src/video/wayland/SDL_waylanddatamanager.c
+++ b/src/video/wayland/SDL_waylanddatamanager.c
@@ -37,14 +37,11 @@
 #include "SDL_waylanddatamanager.h"
 #include "primary-selection-unstable-v1-client-protocol.h"
 
-/* This is arbitrary, but reading while polling should block for less than a frame, to
- * prevent hanging while pumping events.
- *
- * When querying the clipboard data directly, a larger value is needed to avoid timing
- * out if the source needs to process or transfer a large amount of data.
+/* This value is arbitrary, but needs to be long enough to allow the remote end to
+ * wake up and do any necessary processing of the data before sending it. In some
+ * cases, this can be multiple seconds.
  */
-#define DEFAULT_PIPE_TIMEOUT_NS SDL_MS_TO_NS(14)
-#define EXTENDED_PIPE_TIMEOUT_NS SDL_MS_TO_NS(5000)
+#define DEFAULT_PIPE_TIMEOUT_NS SDL_MS_TO_NS(5000)
 
 /* sigtimedwait() is an optional part of POSIX.1-2001, and OpenBSD doesn't implement it.
  * Based on https://comp.unix.programmer.narkive.com/rEDH0sPT/sigtimedwait-implementation
@@ -608,10 +605,8 @@ bool Wayland_DataOfferRequestDataFromSelf(SDL_WaylandDataOffer *offer, const cha
     return true;
 }
 
-void *Wayland_DataOfferReceive(SDL_WaylandDataOffer *offer, const char *mime_type, size_t *length, bool extended_timeout)
+void *Wayland_DataOfferReceive(SDL_WaylandDataOffer *offer, const char *mime_type, size_t *length)
 {
-    const Sint64 timeout = extended_timeout ? EXTENDED_PIPE_TIMEOUT_NS : DEFAULT_PIPE_TIMEOUT_NS;
-
     int pipefd[2];
     void *buffer = NULL;
     *length = 0;
@@ -629,7 +624,7 @@ void *Wayland_DataOfferReceive(SDL_WaylandDataOffer *offer, const char *mime_typ
 
         WAYLAND_wl_display_flush(data_device->seat->display->display);
 
-        while (ReadPipe(pipefd[0], &buffer, length, timeout) > 0) {
+        while (ReadPipe(pipefd[0], &buffer, length, DEFAULT_PIPE_TIMEOUT_NS) > 0) {
         }
         close(pipefd[0]);
     } else {
@@ -662,7 +657,7 @@ void *Wayland_PrimarySelectionOfferReceive(SDL_WaylandPrimarySelectionOffer *off
 
         WAYLAND_wl_display_flush(primary_selection_device->seat->display->display);
 
-        while (ReadPipe(pipefd[0], &buffer, length, EXTENDED_PIPE_TIMEOUT_NS) > 0) {
+        while (ReadPipe(pipefd[0], &buffer, length, DEFAULT_PIPE_TIMEOUT_NS) > 0) {
         }
         close(pipefd[0]);
 
@@ -689,7 +684,7 @@ bool Wayland_PrimarySelectionOfferAddMIME(SDL_WaylandPrimarySelectionOffer *offe
 
 bool Wayland_DataOfferHasMIME(SDL_WaylandDataOffer *offer, const char *mime_type)
 {
-    if (offer) {
+    if (offer && mime_type) {
         return MIMEDataListHasType(&offer->mimes, mime_type);
     }
     return false;
@@ -697,7 +692,7 @@ bool Wayland_DataOfferHasMIME(SDL_WaylandDataOffer *offer, const char *mime_type
 
 bool Wayland_PrimarySelectionOfferHasMIME(SDL_WaylandPrimarySelectionOffer *offer, const char *mime_type)
 {
-    if (offer) {
+    if (offer && mime_type) {
         return MIMEDataListHasType(&offer->mimes, mime_type);
     }
     return false;
diff --git a/src/video/wayland/SDL_waylanddatamanager.h b/src/video/wayland/SDL_waylanddatamanager.h
index 46a9a16b54544..c44d27e91ca6e 100644
--- a/src/video/wayland/SDL_waylanddatamanager.h
+++ b/src/video/wayland/SDL_waylanddatamanager.h
@@ -70,6 +70,17 @@ typedef struct
     int read_fd;
 } SDL_WaylandDataOffer;
 
+typedef struct Wayland_DropData
+{
+    SDL_WaylandDataOffer *offer;
+    SDL_Window *dnd_window;
+    const char *text_mime_type;
+    struct wl_callback *cleanup_callback;
+    Wayland_JobID id;
+
+    struct wl_list link;
+} Wayland_DropData;
+
 typedef struct
 {
     struct zwp_primary_selection_offer_v1 *offer;
@@ -87,10 +98,11 @@ struct SDL_WaylandDataDevice
     uint32_t drag_serial;
     SDL_WaylandDataOffer *drag_offer;
     SDL_WaylandDataOffer *selection_offer;
-    const char *mime_type;
-    bool has_mime_file, has_mime_text;
+    const char *text_mime_type;
+    bool has_mime_file;
     SDL_Window *dnd_window;
     struct wl_surface *dnd_surface;
+    struct wl_list pending_drops;
 
     // Clipboard and Primary Selection
     SDL_WaylandDataSource *selection_source;
@@ -120,7 +132,7 @@ extern void Wayland_PrimarySelectionSourceDestroy(SDL_WaylandPrimarySelectionSou
 // Wayland Data / Primary Selection Offer - (Receiving)
 extern bool Wayland_DataOfferRequestDataFromSelf(SDL_WaylandDataOffer *offer, const char *mime_type, const struct wl_callback_listener *completion_listener, void *userdata);
 extern void *Wayland_DataOfferGetDataFromSelf(SDL_WaylandDataOffer *offer, size_t *length);
-extern void *Wayland_DataOfferReceive(SDL_WaylandDataOffer *offer, const char *mime_type, size_t *length, bool extended_timeout);
+extern void *Wayland_DataOfferReceive(SDL_WaylandDataOffer *offer, const char *mime_type, size_t *length);
 extern void *Wayland_PrimarySelectionOfferReceive(SDL_WaylandPrimarySelectionOffer *offer, const char *mime_type, size_t *length);
 extern bool Wayland_DataOfferHasMIME(SDL_WaylandDataOffer *offer, const char *mime_type);
 extern void Wayland_DataDeviceSetSelectionOffer(SDL_WaylandDataDevice *data_device, SDL_WaylandDataOffer *offer);
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index 9d4810c9acef0..523e8c19d806b 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -2720,7 +2720,7 @@ static void data_device_handle_enter(void *data, struct wl_data_device *wl_data_
     SDL_WaylandDataDevice *data_device = data;
     SDL_WindowData *window = surface ? Wayland_GetWindowDataForOwnedSurface(surface) : NULL;
     data_device->has_mime_file = false;
-    data_device->has_mime_text = false;
+    data_device->text_mime_type = NULL;
 
     data_device->drag_serial = serial;
 
@@ -2730,32 +2730,30 @@ static void data_device_handle_enter(void *data, struct wl_data_device *wl_data_
     }
 
     if (data_device->drag_offer && window && window->accepts_drag_and_drop) {
-        // TODO: SDL Support more mime types
 #ifdef SDL_USE_LIBDBUS
         if (Wayland_DataOfferHasMIME(data_device->drag_offer, FILE_PORTAL_MIME)) {
             data_device->has_mime_file = true;
-            data_device->mime_type = FILE_PORTAL_MIME;
             wl_data_offer_accept(id, serial, FILE_PORTAL_MIME);
         }
 #endif
         if (Wayland_DataOfferHasMIME(data_device->drag_offer, FILE_MIME)) {
             data_device->has_mime_file = true;
-            data_device->mime_type = FILE_MIME;
             wl_data_offer_accept(id, serial, FILE_MIME);
         }
 
-        size_t mime_count = 0;
-        const char *const *text_mime_types = Wayland_GetTextMimeTypes(SDL_GetVideoDevice(), &mime_count);
-        for (size_t i = 0; i < mime_count; ++i) {
-            if (Wayland_DataOfferHasMIME(data_device->drag_offer, text_mime_types[i])) {
-                data_device->has_mime_text = true;
-                data_device->mime_type = text_mime_types[i];
-                wl_data_offer_accept(id, serial, text_mime_types[i]);
-                break;
+        if (!data_device->has_mime_file) {
+            size_t mime_count = 0;
+            const char *const *text_mime_types = Wayland_GetTextMimeTypes(SDL_GetVideoDevice(), &mime_count);
+            for (size_t i = 0; i < mime_count; ++i) {
+                if (Wayland_DataOfferHasMIME(data_device->drag_offer, text_mime_types[i])) {
+                    data_device->text_mime_type = text_mime_types[i];
+                    wl_data_offer_accept(id, serial, text_mime_types[i]);
+                    break;
+                }
             }
         }
 
-        if (data_device->has_mime_file || data_device->has_mime_text) {
+        if (data_device->has_mime_file || data_device->text_mime_type) {
             // SDL only supports "copy" style drag and drop
             if (wl_data_offer_get_version(data_device->drag_offer->offer) >= WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
                 wl_data_offer_set_actions(data_device->drag_offer->offer, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY);
@@ -2835,7 +2833,7 @@ static void data_device_handle_leave(void *data, struct wl_data_device *wl_data_
                      -1, -1);
     }
     data_device->has_mime_file = false;
-    data_device->has_mime_text = false;
+    data_device->text_mime_type = NULL;
 }
 
 static void data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
@@ -2843,7 +2841,7 @@ static void data_device_handle_motion(void *data, struct wl_data_device *wl_data
 {
     SDL_WaylandDataDevice *data_device = data;
 
-    if (data_device->drag_offer && data_device->dnd_window && (data_device->has_mime_file || data_device->has_mime_text)) {
+    if (data_device->drag_offer && data_device->dnd_window && (data_device->has_mime_file || data_device->text_mime_type)) {
         SDL_WindowData *window_data = data_device->dnd_window->internal;
         double dx = wl_fixed_to_double(x);
         double dy = wl_fixed_to_double(y);
@@ -2874,21 +2872,32 @@ static void data_device_handle_motion(void *data, struct wl_data_device *wl_data
     }
 }
 
-static void data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
+static void drop_cleanup_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
 {
-    SDL_WaylandDataDevice *data_device = data;
+    Wayland_DropData *drop = (Wayland_DropData *)data;
 
-    if (data_device->drag_offer && data_device->dnd_window && (data_device->has_mime_file || data_device->has_mime_text)) {
-        SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
-                     ". In wl_data_device_listener . data_device_handle_drop on data_offer 0x%08x in window %d serial %d",
-                     WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
-                     SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
-        // TODO: SDL Support more mime types
+    wl_callback_destroy(drop->cleanup_callback);
+    WAYLAND_wl_list_remove(&drop->link);
+    SDL_free(drop);
+}
+
+static const struct wl_callback_listener drop_cleanup_listener = {
+    drop_cleanup_handler
+};
+
+static void drop_dispatch_handler(void *data)
+{
+    Wayland_DropData *drop = (Wayland_DropData *)data;
+    SDL_WaylandDataOffer *offer = drop->offer;
+    SDL_WaylandDataDevice *data_device = offer->data_device;
+
+    if (drop->dnd_window) {
         size_t length;
         bool drop_handled = false;
+
 #ifdef SDL_USE_LIBDBUS
-        if (Wayland_DataOfferHasMIME(data_device->drag_offer, FILE_PORTAL_MIME)) {
-            void *buffer = Wayland_DataOfferReceive(data_device->drag_offer, FILE_PORTAL_MIME, &length, false);
+        if (Wayland_DataOfferHasMIME(offer, FILE_PORTAL_MIME)) {
+            void *buffer = Wayland_DataOfferReceive(offer, FILE_PORTAL_MIME, &length);
             if (buffer) {
                 SDL_DBusContext *dbus = SDL_DBus_GetContext();
                 if (dbus) {
@@ -2896,12 +2905,10 @@ static void data_device_handle_drop(void *data, struct wl_data_device *wl_data_d
                     char **paths = SDL_DBus_DocumentsPortalRetrieveFiles(buffer, &path_count);
                     // If dropped files contain a directory the list is empty
                     if (paths && path_count > 0) {
-                        int i;
-                        for (i = 0; i < path_count; i++) {
-                            SDL_SendDropFile(data_device->dnd_window, NULL, paths[i]);
+                        for (int i = 0; i < path_count; i++) {
+                            SDL_SendDropFile(drop->dnd_window, NULL, paths[i]);
                         }
                         dbus->free_string_array(paths);
-                        SDL_SendDropComplete(data_device->dnd_window);
                         drop_handled = true;
                     }
                 }
@@ -2909,61 +2916,90 @@ static void data_device_handle_drop(void *data, struct wl_data_device *wl_data_d
             }
         }
 #endif
-        /* If XDG document portal fails fallback.
-         * When running a flatpak sandbox this will most likely be a list of
-         * non paths that are not visible to the application
+        /* If the XDG document portal fails, fall back to text/uri-list, if available.
+         * When running inside of a flatpak sandbox, this will most likely be a list of
+         * non-paths that are not visible to the application.
          */
         if (!drop_handled) {
-            void *buffer = Wayland_DataOfferReceive(data_device->drag_offer, data_device->mime_type, &length, false);
-            if (data_device->has_mime_file) {
+            if (Wayland_DataOfferHasMIME(offer, FILE_MIME)) {
+                void *buffer = Wayland_DataOfferReceive(offer, FILE_MIME, &length);
                 if (buffer) {
                     char *saveptr = NULL;
                     char *token = SDL_strtok_r((char *)buffer, "\r\n", &saveptr);
                     while (token) {
                         if (SDL_URIToLocal(token, token) >= 0) {
-                            SDL_SendDropFile(data_device->dnd_window, NULL, token);
+                            SDL_SendDropFile(drop->dnd_window, NULL, token);
                         }
                         token = SDL_strtok_r(NULL, "\r\n", &saveptr);
                     }
                     SDL_free(buffer);
-                    SDL_SendDropComplete(data_device->dnd_window);
-                } else {
-                    SDL_SendDropComplete(data_device->dnd_window);
+                    drop_handled = true;
                 }
-                drop_handled = true;
-            } else if (data_device->has_mime_text) {
+            } else if (Wayland_DataOfferHasMIME(offer, drop->text_mime_type)) {
+                void *buffer = Wayland_DataOfferReceive(offer, drop->text_mime_type, &length);
                 if (buffer) {
                     char *saveptr = NULL;
                     char *token = SDL_strtok_r((char *)buffer, "\r\n", &saveptr);
                     while (token) {
-                        SDL_SendDropText(data_device->dnd_window, token);
+                        SDL_SendDropText(drop->dnd_window, token);
                         token = SDL_strtok_r(NULL, "\r\n", &saveptr);
                     }
                     SDL_free(buffer);
-                    SDL_SendDropComplete(data_device->dnd_window);
-                } else {
-                    /* Even though there has been a valid data offer,
-                     *  and there have been valid Enter, Motion, and Drop callbacks,
-                     *  Wayland_data_offer_receive may return an empty buffer,
-                     *  because the data is actually in the primary selection device,
-                     *  not in the data device.
-                     */
-                    SDL_SendDropComplete(data_device->dnd_window);
+                    drop_handled = true;
                 }
-                drop_handled = true;
             }
         }
 
-        if (drop_handled && wl_data_offer_get_version(data_device->drag_offer->offer) >= WL_DATA_OFFER_FINISH_SINCE_VERSION) {
-            wl_data_offer_finish(data_device->drag_offer->offer);
+        if (drop_handled && wl_data_offer_get_version(offer->offer) >= WL_DATA_OFFER_FINISH_SINCE_VERSION) {
+            wl_data_offer_finish(offer->offer);
+        }
+
+        SDL_SendDropComplete(drop->dnd_window);
+    }
+
+    Wayland_DataOfferDestroy(drop->offer);
+    drop->offer = NULL;
+
+    // Queue a cleanup event to delete the drop object on the event queue thread.
+    drop->cleanup_callback = wl_display_sync(data_device->seat->display->display);
+    wl_callback_add_listener(drop->cleanup_callback, &drop_cleanup_listener, drop);
+}
+
+static void data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
+{
+    SDL_WaylandDataDevice *data_device = data;
+
+    if (data_device->drag_offer && data_device->dnd_window &&
+        (data_device->has_mime_file || data_device->text_mime_type)) {
+        SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
+                     ". In wl_data_device_listener . data_device_handle_drop on data_offer 0x%08x in window %d serial %d",
+                     WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
+                     SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
+
+        Wayland_JobThreadContext *ctx = data_device->seat->display->job_thread_context;
+
+        /* Dispatch the read on the event thread, as the remote end may be slow to respond,
+         * and we don't want to block the main event loop for an excessive time.
+         */
+        Wayland_DropData *drop = SDL_calloc(1, sizeof(Wayland_DropData));
+        if (drop) {
+            drop->offer = data_device->drag_offer;
+            drop->dnd_window = data_device->dnd_window;
+            drop->text_mime_type = data_device->text_mime_type;
+
+            WAYLAND_wl_list_insert(&data_device->pending_drops, &drop->link);
+            Wayland_JobThreadAddJob(ctx, drop_dispatch_handler, drop, &drop->id);
+        } else {
+            Wayland_DataOfferDestroy(data_device->drag_offer);
         }
     } else {
         SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
                      ". In wl_data_device_listener . data_device_handle_drop on data_offer 0x%08x serial %d",
                      -1, -1);
+
+        Wayland_DataOfferDestroy(data_device->drag_offer);
     }
 
-    Wayland_DataOfferDestroy(data_device->drag_offer);
     data_device->drag_offer = NULL;
 }
 
@@ -3151,13 +3187,14 @@ static void Wayland_SeatCreateDataDevice(SDL_WaylandSeat *seat)
 
     if (!data_device->data_device) {
         SDL_free(data_device);
-    } else {
-        Wayland_DataDeviceSetID(data_device);
-        wl_data_device_set_user_data(data_device->data_device, data_device);
-        wl_data_device_add_listener(data_device->data_device,
-                                    &data_device_listener, data_device);
-        seat->data_device = data_device;
+        return;
     }
+
+    Wayland_DataDeviceSetID(data_device);
+    WAYLAND_wl_list_init(&data_device->pending_drops);
+    wl_data_device_set_user_data(data_device->data_device, data_device);
+    wl_data_device_add_listener(data_device->data_device, &data_device_listener, data_device);
+    seat->data_device = data_device;
 }
 
 void Wayland_DisplayInitDataDeviceManager(SDL_VideoData *display)
@@ -3633,6 +3670,21 @@ void Wayland_DisplayRemoveWindowReferencesFromSeats(SDL_VideoData *display, SDL_
                 }
             }
         }
+
+        // Cancel any pending drop events.
+        Wayland_DropData *drop, *drop_temp;
+        wl_list_for_each_safe (drop, drop_temp, &seat->data_device->pending_drops, link) {
+            if (drop->dnd_window == window->sdlwindow) {
+                Wayland_JobThreadCancelJob(display->job_thread_context, drop->id);
+
+                if (drop->cleanup_callback) {
+                    wl_callback_destroy(drop->cleanup_callback);
+                }
+                Wayland_DataOfferDestroy(drop->offer);
+                WAYLAND_wl_list_remove(&drop->link);
+                SDL_free(drop);
+            }
+        }
     }
 }
 
@@ -3654,6 +3706,20 @@ void Wayland_SeatDestroy(SDL_WaylandSeat *seat, bool shutting_down)
         if (seat->data_device->drag_offer) {
             Wayland_DataOfferDestroy(seat->data_device->drag_offer);
         }
+
+        // Free any remaining drop events.
+        Wayland_DropData *drop, *drop_temp;
+        wl_list_for_each_safe (drop, drop_temp, &seat->data_device->pending_drops, link) {
+            Wayland_JobThreadCancelJob(seat->display->job_thread_context, drop->id);
+
+            if (drop->cleanup_callback) {
+                wl_callback_destroy(drop->cleanup_callback);
+            }
+            Wayland_DataOfferDestroy(drop->offer);
+            WAYLAND_wl_list_remove(&drop->link);
+            SDL_free(drop);
+        }
+
         if (seat->data_device->data_device) {
             if (wl_data_device_get_version(seat->data_device->data_device) >= WL_DATA_DEVICE_RELEASE_SINCE_VERSION) {
                 wl_data_device_release(seat->data_device->data_device);
diff --git a/src/video/wayland/SDL_waylandeventthread.c b/src/video/wayland/SDL_waylandeventthread.c
index b1a24fef2ba4f..5c0d1a92a0839 100644
--- a/src/video/wayland/SDL_waylandeventthread.c
+++ b/src/video/wayland/SDL_waylandeventthread.c
@@ -28,6 +28,36 @@
 
 #include <errno.h>
 
+typedef enum Wayland_JobStatus
+{
+    JOB_STATUS_WAITING = 0,
+    JOB_STATUS_RUNNING,
+    JOB_STATUS_SIGNAL_PENDING,
+    JOB_STATUS_SIGNALED,
+    JOB_STATUS_CANCELED
+} Wayland_JobStatus;
+
+typedef struct Wayland_Job
+{
+    Wayland_JobThreadCallback callback;
+    void *userdata;
+    Wayland_JobID id;
+    Wayland_JobStatus status;
+
+    struct wl_list link;
+} Wayland_Job;
+
+struct Wayland_JobThreadContext
+{
+    SDL_Thread *thread;
+    SDL_Mutex *queue_lock;
+    SDL_Mutex *completion_lock;
+    SDL_Condition *cond;
+    struct wl_list jobs;
+    Wayland_JobID id_counter;
+    bool should_exit;
+};
+
 struct Wayland_EventThreadContext
 {
     SDL_VideoData *wayland_data;
@@ -206,4 +236,185 @@ void Wayland_UnlockEventThread(Wayland_EventThreadContext *context)
     }
 }
 
+static int SDLCALL Wayland_JobThreadFunc(void *data)
+{
+    Wayland_JobThreadContext *context = (Wayland_JobThreadContext *)data;
+
+    SDL_LockMutex(context->queue_lock);
+    while (!context->should_exit) {
+        if (WAYLAND_wl_list_empty(&context->jobs)) {
+            SDL_WaitCondition(context->cond, context->queue_lock);
+        }
+
+        Wayland_Job *job, *job_temp;
+        wl_list_for_each_safe (job, job_temp, &context->jobs, link) {
+            if (job->status == JOB_STATUS_WAITING) {
+                job->status = JOB_STATUS_RUNNING;
+            }
+
+            // Don't block queue operations while dispatching the job.
+            SDL_UnlockMutex(context->queue_lock);
+
+            // If the job was cancelled, just remove it.
+            if (job->status == JOB_STATUS_RUNNING) {
+                job->callback(job->userdata);
+
+                // Signal completion to the waiting thread.
+                SDL_LockMutex(context->completion_lock);
+                if (job->status == JOB_STATUS_SIGNAL_PENDING) {
+                    SDL_SignalCondition(context->cond);
+                }
+                job->status = JOB_STATUS_SIGNALED;
+                SDL_UnlockMutex(context->completion_lock);
+            }
+
+            SDL_LockMutex(context->queue_lock);
+
+            WAYLAND_wl_list_remove(&job->link);
+            SDL_free(job);
+        }
+    }
+    SDL_UnlockMutex(context->queue_lock);
+
+    return 0;
+}
+
+extern Wayland_JobThreadContext *Wayland_CreateJobThread()
+{
+    Wayland_JobThreadContext *context = SDL_calloc(1, sizeof(Wayland_JobThreadContext));
+    if (!context) {
+        return NULL;
+    }
+
+    WAYLAND_wl_list_init(&context->jobs);
+
+    context->queue_lock = SDL_CreateMutex();
+    if (!context->queue_lock) {
+        goto cleanup;
+    }
+
+    context->completion_lock = SDL_CreateMutex();
+    if (!context->completion_lock) {
+        goto cleanup;
+    }
+
+    context->cond = SDL_CreateCondition();
+    if (!context->cond) {
+        goto cleanup;
+    }
+
+    context->thread = SDL_CreateThread(Wayland_JobThreadFunc, "wl_job_thread", context);
+    if (!context->thread) {
+        goto cleanup;
+    }
+
+    return context;
+
+cleanup:
+    if (context->queue_lock) {
+        SDL_DestroyMutex(context->queue_lock);
+    }
+
+    if (context->completion_lock) {
+        SDL_DestroyMutex(context->completion_lock);
+    }
+
+    if (context->cond) {
+        SDL_DestroyCondition(context->cond);
+    }
+
+    SDL_free(context);
+
+    return NULL;
+}
+
+static void job_thread_exit(void *data)
+{
+    Wayland_JobThreadContext *context = (Wayland_JobThreadContext *)data;
+    context->should_exit = true;
+}
+
+extern void Wayland_DestroyJobThread(Wayland_JobThreadContext *context)
+{
+    if (!context) {
+        return;
+    }
+
+    // Dispatch the exit job to unblock the thread and set the exit condition.
+    Wayland_JobThreadAddJob(context, job_thread_exit, context, NULL);
+
+    // Wait for the thread to return; all jobs will be complete at this point.
+    SDL_WaitThread(context->thread, NULL);
+
+    // Cleanup.
+    SDL_DestroyCondition(context->cond);
+    SDL_DestroyMutex(context->queue_lock);
+    SDL_DestroyMutex(context->completion_lock);
+    SDL_free(context);
+}
+
+bool Wayland_JobThreadAddJob(Wayland_JobThreadContext *context, Wayland_JobThreadCallback callback, void *userdata, Wayland_JobID *id)
+{
+    if (!callback) {
+        return false;
+    }
+
+    if (context) {
+        SDL_LockMutex(context->queue_lock);
+        Wayland_Job *job = SDL_calloc(1, sizeof(Wayland_Job));
+        if (job) {
+            job->id = context->id_counter++;
+            if (id) {
+                *id = job->id;
+            }
+
+            job->callback = callback;
+            job->userdata = userdata;
+
+            WAYLAND_wl_list_insert(context->jobs.prev, &job->link);
+
+            SDL_SignalCondition(context->cond);
+            SDL_UnlockMutex(context->queue_lock);
+
+            return true;
+        }
+
+        SDL_UnlockMutex(context->queue_lock);
+        return false;
+    } else {
+        // No thread? Just run the job.
+        callback(userdata);
+    }
+
+    return true;
+}
+
+void Wayland_JobThreadCancelJob(Wayland_JobThreadContext *context, Wayland_JobID id)
+{
+    if (!context) {
+        return;
+    }
+
+    SDL_LockMutex(context->queue_lock);
+
+    Wayland_Job *job;
+    wl_list_for_each (job, &context->jobs, link) {
+        if (job->id == id) {
+            SDL_LockMutex(context->completion_lock);
+            if (job->status == JOB_STATUS_WAITING) {
+                // In the queue, waiting to run; mark as cancelled.
+                job->status = JOB_STATUS_CANCELED;
+            } else if (job->status == JOB_STATUS_RUNNING) {
+                // The job is running; wait for completion.
+                job->status = JOB_STATUS_SIGNAL_PENDING;
+                SDL_WaitCondition(context->cond, context->completion_lock);
+            }
+            SDL_UnlockMutex(context->completion_lock);
+            break;
+        }
+    }
+
+    SDL_UnlockMutex(context->queue_lock);
+}
+
 #endif // SDL_VIDEO_DRIVER_WAYLAND
diff --git a/src/video/wayland/SDL_waylandeventthread.h b/src/video/wayland/SDL_waylandeventthread.h
index 81f9f606b4005..622f530ecee9e 100644
--- a/src/video/wayland/SDL_waylandeventthread.h
+++ b/src/video/wayland/SDL_waylandeventthread.h
@@ -26,6 +26,9 @@
 #include "../SDL_sysvideo.h"
 
 typedef struct Wayland_EventThreadContext Wayland_EventThreadContext;
+typedef struct Wayland_JobThreadContext Wayland_JobThreadContext;
+typedef void (*Wayland_JobThreadCallback)(void *userdata);
+typedef Uint32 Wayland_JobID;
 
 extern Wayland_EventThreadContext *Wayland_CreateEventThread(SDL_VideoData *data, const char *queue_name);
 extern void Wayland_DestroyEventThread(Wayland_EventThreadContext *context);
@@ -33,4 +36,9 @@ extern void *Wayland_CreateEventThreadProxyWrapper(Wayland_EventThreadContext *c
 extern void Wayland_LockEventThread(Wayland_EventThreadContext *context);
 extern void Wayland_UnlockEventThread(Wayland_EventThreadContext *context);
 
+extern Wayland_JobThreadContext *Wayland_CreateJobThread();
+extern void Wayland_DestroyJobThread(Wayland_JobThreadContext *context);
+extern bool Wayland_JobThreadAddJob(Wayland_JobThreadContext *context, Wayland_JobThreadCallback callback, void *userdata, Wayland_JobID *id);
+extern void Wayland_JobThreadCancelJob(Wayland_JobThreadContext *context, Wayland_JobID id);
+
 #endif // SDL_waylandeventthread_h_
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index 5b7b0b5c4325a..999bfe4dfa4f8 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -1711,6 +1711,11 @@ bool Wayland_VideoInit(SDL_VideoDevice *_this)
         SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "wayland: Failed to create event thread context");
     }
 
+    data->job_thread_context = Wayland_CreateJobThread();
+    if (!data->job_thread_context) {
+        SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "wayland: Failed to create job thread context");
+    }
+
     data->xkb_context = WAYLAND_xkb_context_new(0);
     if (!data->xkb_context) {
         return SDL_SetError("Failed to create XKB context");
@@ -1819,6 +1824,9 @@ static void Wayland_VideoCleanup(SDL_VideoDevice *_this)
     Wayland_DestroyEventThread(data->event_thread_context);
     data->event_thread_context = NULL;
 
+    Wayland_DestroyJobThread(data->job_thread_context);
+    data->job_thread_context = NULL;
+
     if (data->pointer_constraints) {
         zwp_pointer_constraints_v1_destroy(data->pointer_constraints);
         data->pointer_constraints = NULL;
diff --git a/src/video/wayland/SDL_waylandvideo.h b/src/video/wayland/SDL_waylandvideo.h
index 76fd67940890e..c9b39b35cda35 100644
--- a/src/video/wayland/SDL_waylandvideo.h
+++ b/src/video/wayland/SDL_waylandvideo.h
@@ -99,6 +99,7 @@ struct SDL_VideoData
     struct SDL_WaylandSeat *current_data_offer_seat;
     struct SDL_WaylandSeat *current_primary_selection_seat;
     Wayland_EventThreadContext *event_thread_context;
+    Wayland_JobThreadContext *job_thread_context;
 
     SDL_DisplayData **output_list;
     int output_count;

(Patch may be truncated, please check the link at the top of this post.)