SDL: wayland: Consolidate data manager handling

From c2560b2940a86bddbd45f8e6f30497fcb3387f67 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Tue, 20 Oct 2026 15:59:55 -0400
Subject: [PATCH] wayland: Consolidate data manager handling

The data management devices that handle clipboard, drag & drop, and primary selection had functionality split across several source files. Consolidate all handling into the data manager source file, and expose only what is necessary.

Data device handling is untangled from multiple source files, duplicate functionality has been removed, trivial setters/getters have been removed, the internal API has shrunk and been simplified, and many functions have become static.
---
 src/video/wayland/SDL_waylandclipboard.c   |  110 +-
 src/video/wayland/SDL_waylanddatamanager.c | 1337 +++++++++++++++-----
 src/video/wayland/SDL_waylanddatamanager.h |  135 +-
 src/video/wayland/SDL_waylandevents.c      |  575 +--------
 src/video/wayland/SDL_waylandevents_c.h    |   11 +-
 5 files changed, 1073 insertions(+), 1095 deletions(-)

diff --git a/src/video/wayland/SDL_waylandclipboard.c b/src/video/wayland/SDL_waylandclipboard.c
index 3922dd0acb8ba..532d770e37ce6 100644
--- a/src/video/wayland/SDL_waylandclipboard.c
+++ b/src/video/wayland/SDL_waylandclipboard.c
@@ -28,6 +28,14 @@
 #include "../SDL_clipboard_c.h"
 #include "../../events/SDL_events_c.h"
 
+static const char *const text_mime_types[] = {
+    TEXT_MIME,
+    "text/plain",
+    "TEXT",
+    "UTF8_STRING",
+    "STRING"
+};
+
 bool Wayland_SetClipboardData(SDL_VideoDevice *_this)
 {
     SDL_VideoData *video_data = _this->internal;
@@ -42,38 +50,21 @@ bool Wayland_SetClipboardData(SDL_VideoDevice *_this)
     video_data->current_data_offer_seat = seat;
 
     if (seat && seat->data_device) {
-        /* Clear references to the clipboard held by other seats, as they are about to become invalid.
-         * For the target seat, the new source data is set before clearing the old source, as some
-         * clipboard managers prefer this behavior.
-         */
+        // Clear references to the clipboard held by other seats, as they are about to become invalid.
         SDL_WaylandSeat *s;
         wl_list_for_each (s, &video_data->seat_list, link) {
             if (s->data_device) {
                 if (s != seat) {
-                    Wayland_DataSourceDestroy(s->data_device->selection_source);
-                    s->data_device->selection_source = NULL;
+                    Wayland_DataDeviceSetSelectionSource(s->data_device, NULL, 0, NULL, NULL, NULL, 0);
                 }
 
-                Wayland_DataOfferDestroy(s->data_device->selection_offer);
-                s->data_device->selection_offer = NULL;
+                Wayland_DataDeviceDestroySelectionOffer(s->data_device, false);
             }
         }
 
-        SDL_WaylandDataDevice *data_device = seat->data_device;
-
-        if (_this->clipboard_callback && _this->clipboard_mime_types) {
-            SDL_WaylandDataSource *source = Wayland_DataSourceCreate(video_data);
-            Wayland_DataSourceSetCallback(source, _this->clipboard_callback, _this->clipboard_cleanup, _this->clipboard_userdata, _this->clipboard_sequence);
-
-            result = Wayland_DataDeviceSetSelectionSource(data_device, source, (const char **)_this->clipboard_mime_types, _this->num_clipboard_mime_types);
-            if (!result) {
-                Wayland_DataSourceDestroy(source);
-            }
-        } else {
-            Wayland_DataSourceDestroy(data_device->selection_source);
-            data_device->selection_source = NULL;
-            result = true;
-        }
+        Wayland_DataDevice *data_device = seat->data_device;
+        result = Wayland_DataDeviceSetSelectionSource(data_device, (const char *const *)_this->clipboard_mime_types, _this->num_clipboard_mime_types,
+                                                      _this->clipboard_callback, _this->clipboard_cleanup, _this->clipboard_userdata, _this->clipboard_sequence);
     }
 
     return result;
@@ -86,11 +77,11 @@ void *Wayland_GetClipboardData(SDL_VideoDevice *_this, const char *mime_type, si
     void *buffer = NULL;
 
     if (seat && seat->data_device) {
-        SDL_WaylandDataDevice *data_device = seat->data_device;
-        if (data_device->selection_source) {
+        Wayland_DataDevice *data_device = seat->data_device;
+        if (Wayland_DataDeviceHasSelectionSource(data_device)) {
             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);
+        } else if (Wayland_DataDeviceSelectionOfferHasMIME(data_device, mime_type)) {
+            buffer = Wayland_DataDeviceReceiveSelectionOffer(data_device, mime_type, length);
         }
     }
 
@@ -104,24 +95,16 @@ bool Wayland_HasClipboardData(SDL_VideoDevice *_this, const char *mime_type)
     bool result = false;
 
     if (seat && seat->data_device) {
-        SDL_WaylandDataDevice *data_device = seat->data_device;
-        if (data_device->selection_source) {
+        Wayland_DataDevice *data_device = seat->data_device;
+        if (Wayland_DataDeviceHasSelectionSource(data_device)) {
             result = SDL_HasInternalClipboardData(_this, mime_type);
         } else {
-            result = Wayland_DataOfferHasMIME(data_device->selection_offer, mime_type);
+            result = Wayland_DataDeviceSelectionOfferHasMIME(data_device, mime_type);
         }
     }
     return result;
 }
 
-static const char *const text_mime_types[] = {
-    TEXT_MIME,
-    "text/plain",
-    "TEXT",
-    "UTF8_STRING",
-    "STRING"
-};
-
 const char *const *Wayland_GetTextMimeTypes(SDL_VideoDevice *_this, size_t *num_mime_types)
 {
     *num_mime_types = SDL_arraysize(text_mime_types);
@@ -142,20 +125,18 @@ bool Wayland_SetPrimarySelectionText(SDL_VideoDevice *_this, const char *text)
     video_data->current_primary_selection_seat = seat;
 
     if (seat && seat->primary_selection_device) {
-        SDL_WaylandPrimarySelectionDevice *primary_selection_device = seat->primary_selection_device;
-        if (text[0] != '\0') {
-            SDL_WaylandPrimarySelectionSource *source = Wayland_PrimarySelectionSourceCreate(video_data);
-            Wayland_PrimarySelectionSourceSetCallback(source, SDL_ClipboardTextCallback, SDL_free, SDL_strdup(text));
-
-            result = Wayland_PrimarySelectionDeviceSetSelection(primary_selection_device, source, text_mime_types, SDL_arraysize(text_mime_types));
-            if (!result) {
-                Wayland_PrimarySelectionSourceDestroy(source);
+        // Clear references to the primary selection held by other seats, as they are about to become invalid.
+        SDL_WaylandSeat *s;
+        wl_list_for_each (s, &video_data->seat_list, link) {
+            if (s->data_device) {
+                if (s != seat) {
+                    Wayland_PrimarySelectionDeviceSetText(s->primary_selection_device, NULL);
+                }
+
+                Wayland_PrimarySelectionDeviceDestroyOffer(s->primary_selection_device);
             }
-        } else {
-            Wayland_PrimarySelectionSourceDestroy(seat->primary_selection_device->selection_source);
-            seat->primary_selection_device->selection_source = NULL;
-            result = true;
         }
+        result = Wayland_PrimarySelectionDeviceSetText(seat->primary_selection_device, text);
     } else {
         result = SDL_SetError("Primary selection not supported");
     }
@@ -170,17 +151,7 @@ char *Wayland_GetPrimarySelectionText(SDL_VideoDevice *_this)
     size_t length = 0;
 
     if (seat && seat->primary_selection_device) {
-        SDL_WaylandPrimarySelectionDevice *primary_selection_device = seat->primary_selection_device;
-        if (primary_selection_device->selection_source) {
-            text = Wayland_PrimarySelectionSourceGetData(primary_selection_device->selection_source, TEXT_MIME, &length);
-        } else {
-            for (size_t i = 0; i < SDL_arraysize(text_mime_types); i++) {
-                if (Wayland_PrimarySelectionOfferHasMIME(primary_selection_device->selection_offer, text_mime_types[i])) {
-                    text = Wayland_PrimarySelectionOfferReceive(primary_selection_device->selection_offer, text_mime_types[i], &length);
-                    break;
-                }
-            }
-        }
+        text = Wayland_PrimarySelectionDeviceGetText(seat->primary_selection_device, &length);
     }
 
     if (!text) {
@@ -194,24 +165,11 @@ bool Wayland_HasPrimarySelectionText(SDL_VideoDevice *_this)
 {
     SDL_VideoData *video_data = _this->internal;
     SDL_WaylandSeat *seat = video_data->current_primary_selection_seat;
-    bool result = false;
 
     if (seat && seat->primary_selection_device) {
-        SDL_WaylandPrimarySelectionDevice *primary_selection_device = seat->primary_selection_device;
-        if (primary_selection_device->selection_source) {
-            result = true;
-        } else {
-            size_t mime_count = 0;
-            const char *const *mime_types = Wayland_GetTextMimeTypes(_this, &mime_count);
-            for (size_t i = 0; i < mime_count; i++) {
-                if (Wayland_PrimarySelectionOfferHasMIME(primary_selection_device->selection_offer, mime_types[i])) {
-                    result = true;
-                    break;
-                }
-            }
-        }
+        return Wayland_PrimarySelectionDeviceHasText(seat->primary_selection_device);
     }
-    return result;
+    return false;
 }
 
 #endif // SDL_VIDEO_DRIVER_WAYLAND
diff --git a/src/video/wayland/SDL_waylanddatamanager.c b/src/video/wayland/SDL_waylanddatamanager.c
index b31f1cff95111..7d0b492e14d38 100644
--- a/src/video/wayland/SDL_waylanddatamanager.c
+++ b/src/video/wayland/SDL_waylanddatamanager.c
@@ -24,16 +24,15 @@
 #ifdef SDL_VIDEO_DRIVER_WAYLAND
 
 #include <fcntl.h>
-#include <unistd.h>
 #include <limits.h>
 #include <signal.h>
+#include <unistd.h>
 
 #include "../../core/unix/SDL_poll.h"
 #include "../../events/SDL_events_c.h"
 #include "../SDL_clipboard_c.h"
 
-#include "SDL_waylandvideo.h"
-#include "SDL_waylandevents_c.h"
+#include "SDL_waylandclipboard.h"
 #include "SDL_waylanddatamanager.h"
 #include "primary-selection-unstable-v1-client-protocol.h"
 
@@ -43,6 +42,88 @@
  */
 #define DEFAULT_PIPE_TIMEOUT_NS SDL_MS_TO_NS(5000)
 
+typedef struct SDL_WaylandUserdata
+{
+    void *data;
+    Uint32 sequence;
+} Wayland_Userdata;
+
+typedef struct Wayland_DataSource
+{
+    struct wl_data_source *source;
+    Wayland_DataDevice *data_device;
+    SDL_ClipboardDataCallback data_callback;
+    SDL_ClipboardCleanupCallback cleanup_callback;
+    Wayland_Userdata userdata;
+} Wayland_DataSource;
+
+typedef struct Wayland_PrimarySelectionSource
+{
+    struct zwp_primary_selection_source_v1 *source;
+    Wayland_PrimarySelectionDevice *primary_selection_device;
+    SDL_ClipboardDataCallback data_callback;
+    SDL_ClipboardCleanupCallback cleanup_callback;
+    Wayland_Userdata userdata;
+} Wayland_PrimarySelectionSource;
+
+typedef struct Wayland_DataOffer
+{
+    struct wl_data_offer *offer;
+    struct wl_array mimes;
+    Wayland_DataDevice *data_device;
+
+    // Callback data for queued receive.
+    struct wl_callback *callback;
+    int read_fd;
+} Wayland_DataOffer;
+
+typedef struct Wayland_DropData
+{
+    Wayland_DataOffer *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 Wayland_PrimarySelectionOffer
+{
+    struct zwp_primary_selection_offer_v1 *offer;
+    struct wl_array mimes;
+    Wayland_PrimarySelectionDevice *primary_selection_device;
+} Wayland_PrimarySelectionOffer;
+
+struct Wayland_DataDevice
+{
+    struct wl_data_device *data_device;
+    SDL_WaylandSeat *seat;
+    char *id_str;
+
+    // Drag and Drop
+    uint32_t drag_serial;
+    Wayland_DataOffer *drag_offer;
+    const char *text_mime_type;
+    bool has_mime_file;
+    SDL_Window *dnd_window;
+    struct wl_surface *dnd_surface;
+    struct wl_list pending_drops;
+
+    // Clipboard
+    Wayland_DataOffer *selection_offer;
+    Wayland_DataSource *selection_source;
+};
+
+struct Wayland_PrimarySelectionDevice
+{
+    struct zwp_primary_selection_device_v1 *primary_selection_device;
+    SDL_WaylandSeat *seat;
+
+    Wayland_PrimarySelectionSource *source;
+    Wayland_PrimarySelectionOffer *offer;
+};
+
 /* 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
  */
@@ -85,6 +166,38 @@ static int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct times
 }
 #endif
 
+static bool MIMEDataListHasType(struct wl_array *list, const char *mime_type)
+{
+    const char **item;
+    wl_array_for_each (item, list) {
+        if (SDL_strcmp(*item, mime_type) == 0) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+static bool MIMEDataListAdd(struct wl_array *list, const char *mime_type)
+{
+    if (!MIMEDataListHasType(list, mime_type)) {
+        char **item = WAYLAND_wl_array_add(list, sizeof(char *));
+        *item = SDL_strdup(mime_type);
+    }
+
+    return true;
+}
+
+static void MIMEDataListFree(struct wl_array *list)
+{
+    char **item;
+    wl_array_for_each (item, list) {
+        SDL_free(*item);
+    }
+
+    WAYLAND_wl_array_release(list);
+}
+
 static ssize_t WritePipe(int fd, const void *buffer, size_t total_length, size_t *pos)
 {
     ssize_t bytes_written = 0;
@@ -115,7 +228,7 @@ static ssize_t WritePipe(int fd, const void *buffer, size_t total_length, size_t
         }
     } else if (ready == 0) {
         SDL_SetError("Pipe timeout");
-    } else  {
+    } else {
         SDL_SetError("Pipe poll error");
     }
 
@@ -171,36 +284,111 @@ static ssize_t ReadPipe(int fd, void **buffer, size_t *total_length, Sint64 time
     return bytes_read;
 }
 
-static bool MIMEDataListHasType(struct wl_array *list, const char *mime_type)
+static size_t SendData(const void *data, size_t length, int fd)
 {
-    const char **item;
-    wl_array_for_each(item, list) {
-        if (SDL_strcmp(*item, mime_type) == 0) {
-            return true;
+    size_t result = 0;
+
+    if (length > 0 && data) {
+        while (WritePipe(fd, data, length, &result) > 0) {
+            // Just keep spinning
         }
     }
+    close(fd);
 
-    return false;
+    return result;
 }
 
-static bool MIMEDataListAdd(struct wl_array *list, const char *mime_type)
+static ssize_t DataSourceSend(Wayland_DataSource *source, const char *mime_type, int fd)
 {
-    if (!MIMEDataListHasType(list, mime_type)) {
-        char **item = WAYLAND_wl_array_add(list, sizeof(char *));
-        *item = SDL_strdup(mime_type);
+    const void *data = NULL;
+    size_t length = 0;
+
+    if (SDL_strcmp(mime_type, SDL_DATA_ORIGIN_MIME) == 0) {
+        data = source->data_device->id_str;
+        length = SDL_strlen(source->data_device->id_str);
+    } else if (source->data_callback) {
+        data = source->data_callback(source->userdata.data, mime_type, &length);
     }
 
-    return true;
+    return SendData(data, length, fd);
 }
 
-static void MIMEDataListFree(struct wl_array *list)
+static ssize_t PrimarySelectionSourceSend(Wayland_PrimarySelectionSource *source, const char *mime_type, int fd)
 {
-    char **item;
-    wl_array_for_each(item, list) {
-        SDL_free(*item);
+    const void *data = NULL;
+    size_t length = 0;
+
+    if (source->data_callback) {
+        data = source->data_callback(source->userdata.data, mime_type, &length);
     }
 
-    WAYLAND_wl_array_release(list);
+    return SendData(data, length, fd);
+}
+
+static void DestroyDataSource(Wayland_DataSource *source)
+{
+    if (source) {
+        Wayland_DataDevice *data_device = source->data_device;
+        if (data_device && (data_device->selection_source == source)) {
+            data_device->selection_source = NULL;
+        }
+        wl_data_source_destroy(source->source);
+        if (source->userdata.sequence) {
+            SDL_CancelClipboardData(source->userdata.sequence);
+        } else if (source->cleanup_callback) {
+            source->cleanup_callback(source->userdata.data);
+        }
+        SDL_free(source);
+    }
+}
+
+static void DestroyPrimarySelectionSource(Wayland_PrimarySelectionSource *source)
+{
+    if (source) {
+        Wayland_PrimarySelectionDevice *primary_selection_device = source->primary_selection_device;
+        if (primary_selection_device && (primary_selection_device->source == source)) {
+            primary_selection_device->source = NULL;
+        }
+        zwp_primary_selection_source_v1_destroy(source->source);
+        if (source->cleanup_callback) {
+            source->cleanup_callback(source->userdata.data);
+        }
+        SDL_free(source);
+    }
+}
+
+static void DestroyDataOffer(Wayland_DataOffer *offer)
+{
+    if (offer) {
+        if (offer->callback) {
+            wl_callback_destroy(offer->callback);
+        }
+        if (offer->read_fd >= 0) {
+            close(offer->read_fd);
+        }
+        wl_data_offer_destroy(offer->offer);
+        MIMEDataListFree(&offer->mimes);
+        SDL_free(offer);
+    }
+}
+
+static void DestroyPrimarySelectionOffer(Wayland_PrimarySelectionOffer *offer)
+{
+    if (offer) {
+        zwp_primary_selection_offer_v1_destroy(offer->offer);
+        MIMEDataListFree(&offer->mimes);
+        SDL_free(offer);
+    }
+}
+
+static void DestroyDropData(Wayland_DropData *drop_data)
+{
+    if (drop_data->cleanup_callback) {
+        wl_callback_destroy(drop_data->cleanup_callback);
+    }
+    DestroyDataOffer(drop_data->offer);
+    WAYLAND_wl_list_remove(&drop_data->link);
+    SDL_free(drop_data);
 }
 
 static void data_source_handle_target(void *data, struct wl_data_source *wl_data_source, const char *mime_type)
@@ -209,14 +397,14 @@ static void data_source_handle_target(void *data, struct wl_data_source *wl_data
 
 static void data_source_handle_send(void *data, struct wl_data_source *wl_data_source, const char *mime_type, int32_t fd)
 {
-    Wayland_DataSourceSend((SDL_WaylandDataSource *)data, mime_type, fd);
+    DataSourceSend((Wayland_DataSource *)data, mime_type, fd);
 }
 
 static void data_source_handle_cancelled(void *data, struct wl_data_source *wl_data_source)
 {
-    SDL_WaylandDataSource *source = data;
+    Wayland_DataSource *source = data;
     if (source) {
-        Wayland_DataSourceDestroy(source);
+        DestroyDataSource(source);
     }
 }
 
@@ -226,9 +414,9 @@ static void data_source_handle_dnd_drop_performed(void *data, struct wl_data_sou
 
 static void data_source_handle_dnd_finished(void *data, struct wl_data_source *wl_data_source)
 {
-    SDL_WaylandDataSource *source = data;
+    Wayland_DataSource *source = data;
     if (source) {
-        Wayland_DataSourceDestroy(source);
+        DestroyDataSource(source);
     }
 }
 
@@ -245,15 +433,18 @@ static const struct wl_data_source_listener data_source_listener = {
     data_source_handle_action,             // Version 3
 };
 
-SDL_WaylandDataSource *Wayland_DataSourceCreate(SDL_VideoData *video_data)
+static Wayland_DataSource *CreateDataSource(Wayland_DataDevice *data_device)
 {
-    SDL_WaylandDataSource *data_source = SDL_calloc(1, sizeof(*data_source));
+    SDL_VideoData *videodata = data_device->seat->display;
+
+    Wayland_DataSource *data_source = SDL_calloc(1, sizeof(*data_source));
     if (!data_source) {
         return NULL;
     }
 
-    struct wl_data_source *id = wl_data_device_manager_create_data_source(video_data->data_device_manager);
+    struct wl_data_source *id = wl_data_device_manager_create_data_source(videodata->data_device_manager);
     data_source->source = id;
+    data_source->data_device = data_device;
     wl_data_source_set_user_data(id, data_source);
     wl_data_source_add_listener(id, &data_source_listener, data_source);
 
@@ -262,12 +453,12 @@ SDL_WaylandDataSource *Wayland_DataSourceCreate(SDL_VideoData *video_data)
 
 static void primary_selection_source_send(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1, const char *mime_type, int32_t fd)
 {
-    Wayland_PrimarySelectionSourceSend((SDL_WaylandPrimarySelectionSource *)data, mime_type, fd);
+    PrimarySelectionSourceSend((Wayland_PrimarySelectionSource *)data, mime_type, fd);
 }
 
 static void primary_selection_source_cancelled(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1)
 {
-    Wayland_PrimarySelectionSourceDestroy(data);
+    DestroyPrimarySelectionSource(data);
 }
 
 static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
@@ -275,220 +466,138 @@ static const struct zwp_primary_selection_source_v1_listener primary_selection_s
     primary_selection_source_cancelled,
 };
 
-SDL_WaylandPrimarySelectionSource *Wayland_PrimarySelectionSourceCreate(SDL_VideoData *video_data)
+static Wayland_PrimarySelectionSource *CreatePrimarySelectionSource(Wayland_PrimarySelectionDevice *primary_selection_device)
 {
-    SDL_WaylandPrimarySelectionSource *primary_selection_source = SDL_calloc(1, sizeof(*primary_selection_source));
+    SDL_VideoData *videodata = primary_selection_device->seat->display;
+
+    Wayland_PrimarySelectionSource *primary_selection_source = SDL_calloc(1, sizeof(*primary_selection_source));
     if (!primary_selection_source) {
         return NULL;
     }
 
-    struct zwp_primary_selection_source_v1 *id = zwp_primary_selection_device_manager_v1_create_source(video_data->primary_selection_device_manager);
+    struct zwp_primary_selection_source_v1 *id = zwp_primary_selection_device_manager_v1_create_source(videodata->primary_selection_device_manager);
     primary_selection_source->source = id;
+    primary_selection_source->primary_selection_device = primary_selection_device;
     zwp_primary_selection_source_v1_add_listener(id, &primary_selection_source_listener, primary_selection_source);
 
     return primary_selection_source;
 }
 
-static size_t SendData(const void *data, size_t length, int fd)
+static void *GetOfferDataFromSelf(Wayland_DataOffer *offer, size_t *length)
 {
-    size_t result = 0;
+    if (!offer) {
+        SDL_SetError("Invalid data offer");
+        return NULL;
+    }
 
-    if (length > 0 && data) {
-        while (WritePipe(fd, data, length, &result) > 0) {
-            // Just keep spinning
-        }
+    if (offer->read_fd < 0) {
+        SDL_SetError("Pipe closed");
+        return NULL;
     }
-    close(fd);
 
-    return result;
+    // The timeout is 0, as the data will already be in the pipe if it is from this process.
+    void *buffer = NULL;
+    while (ReadPipe(offer->read_fd, &buffer, length, 0) > 0) {
+    }
+
+    return buffer;
 }
 
-ssize_t Wayland_DataSourceSend(SDL_WaylandDataSource *source, const char *mime_type, int fd)
+static bool RequestOfferDataFromSelf(Wayland_DataOffer *offer, const char *mime_type, const struct wl_callback_listener *completion_listener, void *userdata)
 {
-    const void *data = NULL;
-    size_t length = 0;
+    int pipefd[2];
 
-    if (SDL_strcmp(mime_type, SDL_DATA_ORIGIN_MIME) == 0) {
-        data = source->data_device->id_str;
-        length = SDL_strlen(source->data_device->id_str);
-    } else if (source->data_callback) {
-        data = source->data_callback(source->userdata.data, mime_type, &length);
+    if (!offer) {
+        SDL_SetError("Invalid data offer");
+        return false;
     }
 
-    return SendData(data, length, fd);
-}
+    Wayland_DataDevice *data_device = offer->data_device;
+    if (offer->read_fd >= 0) {
+        close(offer->read_fd);
+        offer->read_fd = -1;
+    }
+    if (offer->callback) {
+        wl_callback_destroy(offer->callback);
+        offer->callback = NULL;
+    }
 
-ssize_t Wayland_PrimarySelectionSourceSend(SDL_WaylandPrimarySelectionSource *source, const char *mime_type, int fd)
-{
-    const void *data = NULL;
-    size_t length = 0;
+    if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) == 0) {
+        offer->read_fd = pipefd[0];
+        wl_data_offer_receive(offer->offer, mime_type, pipefd[1]);
+        close(pipefd[1]);
 
-    if (source->data_callback) {
-        data = source->data_callback(source->userdata.data, mime_type, &length);
+        offer->callback = wl_display_sync(data_device->seat->display->display);
+        wl_callback_add_listener(offer->callback, completion_listener, userdata);
+
+        WAYLAND_wl_display_flush(data_device->seat->display->display);
+    } else {
+        return SDL_SetError("Could not create pipe");
     }
 
-    return SendData(data, length, fd);
+    return true;
 }
 
-void Wayland_DataSourceSetCallback(SDL_WaylandDataSource *source, SDL_ClipboardDataCallback data_callback, SDL_ClipboardCleanupCallback cleanup_callback, void *userdata, Uint32 sequence)
-{
-    if (source) {
-        source->data_callback = data_callback;
-        source->cleanup_callback = cleanup_callback;
-        source->userdata.sequence = sequence;
-        source->userdata.data = userdata;
-    }
-}
+static void SelectionOfferNotifyFromMIMEs(Wayland_DataDevice *data_device, bool check_origin);
 
-void Wayland_PrimarySelectionSourceSetCallback(SDL_WaylandPrimarySelectionSource *source, SDL_ClipboardDataCallback data_callback, SDL_ClipboardCleanupCallback cleanup_callback, void *userdata)
+static void offer_source_done_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
 {
-    if (source) {
-        source->data_callback = data_callback;
-        source->cleanup_callback = cleanup_callback;
-        source->userdata.sequence = 0;
-        source->userdata.data = userdata;
+    if (!callback) {
+        return;
     }
-}
 
-static void *CloneDataBuffer(const void *buffer, const size_t *len)
-{
-    void *clone = NULL;
-    if (*len > 0 && buffer) {
-        clone = SDL_malloc((*len)+sizeof(Uint32));
-        if (clone) {
-            SDL_memcpy(clone, buffer, *len);
-            SDL_memset((Uint8 *)clone + *len, 0, sizeof(Uint32));
+    Wayland_DataOffer *offer = (Wayland_DataOffer *)data;
+    char *id = NULL;
+    size_t length = 0;
+
+    wl_callback_destroy(offer->callback);
+    offer->callback = NULL;
+
+    id = GetOfferDataFromSelf(offer, &length);
+    if (id) {
+        const bool source_is_external = SDL_strncmp(offer->data_device->id_str, id, length) != 0;
+        SDL_free(id);
+        if (source_is_external) {
+            SelectionOfferNotifyFromMIMEs(offer->data_device, false);
+        } else {
+            // Recursive data offer; just destroy it.
+            Wayland_DataDevice *data_device = offer->data_device;
+            DestroyDataOffer(offer);
+            data_device->selection_offer = NULL;
         }
     }
-    return clone;
 }
 
-void *Wayland_DataSourceGetData(SDL_WaylandDataSource *source, const char *mime_type, size_t *length)
+static const struct wl_callback_listener offer_source_listener = {
+    offer_source_done_handler
+};
+
+static void UpdateSeatOffers(Wayland_DataDevice *data_device)
 {
-    void *buffer = NULL;
-    *length = 0;
+    SDL_WaylandSeat *offer_seat = data_device->seat;
+    SDL_VideoData *video_data = offer_seat->display;
 
-    if (!source) {
-        SDL_SetError("Invalid data source");
-    } else if (source->data_callback) {
-        const void *internal_buffer = source->data_callback(source->userdata.data, mime_type, length);
-        buffer = CloneDataBuffer(internal_buffer, length);
+    // Clear any existing references to the existing clipboard data before replacing the current offer.
+    SDL_WaylandSeat *s;
+    wl_list_for_each (s, &video_data->seat_list, link) {
+        if (s->data_device) {
+            DestroyDataSource(s->data_device->selection_source);
+            s->data_device->selection_source = NULL;
+
+            // Don't clear the offer that is about to be set.
+            if (s != offer_seat) {
+                DestroyDataOffer(s->data_device->selection_offer);
+                s->data_device->selection_offer = NULL;
+            }
+        }
     }
 
-    return buffer;
+    video_data->current_data_offer_seat = offer_seat;
 }
 
-void *Wayland_PrimarySelectionSourceGetData(SDL_WaylandPrimarySelectionSource *source, const char *mime_type, size_t *length)
+static void SelectionOfferNotifyFromMIMEs(Wayland_DataDevice *data_device, bool check_origin)
 {
-    void *buffer = NULL;
-    *length = 0;
-
-    if (!source) {
-        SDL_SetError("Invalid primary selection source");
-    } else if (source->data_callback) {
-        const void *internal_buffer = source->data_callback(source->userdata.data, mime_type, length);
-        buffer = CloneDataBuffer(internal_buffer, length);
-    }
-
-    return buffer;
-}
-
-void Wayland_DataSourceDestroy(SDL_WaylandDataSource *source)
-{
-    if (source) {
-        SDL_WaylandDataDevice *data_device = source->data_device;
-        if (data_device && (data_device->selection_source == source)) {
-            data_device->selection_source = NULL;
-        }
-        wl_data_source_destroy(source->source);
-        if (source->userdata.sequence) {
-            SDL_CancelClipboardData(source->userdata.sequence);
-        } else if (source->cleanup_callback) {
-            source->cleanup_callback(source->userdata.data);
-        }
-        SDL_free(source);
-    }
-}
-
-void Wayland_PrimarySelectionSourceDestroy(SDL_WaylandPrimarySelectionSource *source)
-{
-    if (source) {
-        SDL_WaylandPrimarySelectionDevice *primary_selection_device = source->primary_selection_device;
-        if (primary_selection_device && (primary_selection_device->selection_source == source)) {
-            primary_selection_device->selection_source = NULL;
-        }
-        zwp_primary_selection_source_v1_destroy(source->source);
-        if (source->cleanup_callback) {
-            source->cleanup_callback(source->userdata.data);
-        }
-        SDL_free(source);
-    }
-}
-
-static void SelectionOfferNotifyFromMIMEs(SDL_WaylandDataDevice *data_device, bool check_origin);
-
-static void offer_source_done_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
-{
-    if (!callback) {
-        return;
-    }
-
-    SDL_WaylandDataOffer *offer = (SDL_WaylandDataOffer *)data;
-    char *id = NULL;
-    size_t length = 0;
-
-    wl_callback_destroy(offer->callback);
-    offer->callback = NULL;
-
-    while (ReadPipe(offer->read_fd, (void **)&id, &length, DEFAULT_PIPE_TIMEOUT_NS) > 0) {
-    }
-    close(offer->read_fd);
-    offer->read_fd = -1;
-
-    if (id) {
-        const bool source_is_external = SDL_strncmp(offer->data_device->id_str, id, length) != 0;
-        SDL_free(id);
-        if (source_is_external) {
-            SelectionOfferNotifyFromMIMEs(offer->data_device, false);
-        } else {
-            // Recursive data offer; just destroy it.
-            SDL_WaylandDataDevice *data_device = offer->data_device;
-            Wayland_DataOfferDestroy(offer);
-            data_device->selection_offer = NULL;
-        }
-    }
-}
-
-static const struct wl_callback_listener offer_source_listener = {
-    offer_source_done_handler
-};
-
-static void UpdateSeatOffers(SDL_WaylandDataDevice *data_device)
-{
-    SDL_WaylandSeat *offer_seat = data_device->seat;
-    SDL_VideoData *video_data = offer_seat->display;
-
-    // Clear any existing references to the exis

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