wayland: Add additional MIME types for text drag & drop

Previously, it only specifically accepted "text/plain;charset=utf8", which caused it to reject valid text from certain apps.
This commit is contained in:
Frank Praznik
2025-03-05 20:14:19 -05:00
parent e20e27e1fb
commit c4be7f77a6
3 changed files with 29 additions and 9 deletions

View File

@@ -142,8 +142,13 @@ char *Wayland_GetPrimarySelectionText(SDL_VideoDevice *_this)
primary_selection_device = video_data->input->primary_selection_device; primary_selection_device = video_data->input->primary_selection_device;
if (primary_selection_device->selection_source) { if (primary_selection_device->selection_source) {
text = Wayland_primary_selection_source_get_data(primary_selection_device->selection_source, TEXT_MIME, &length); text = Wayland_primary_selection_source_get_data(primary_selection_device->selection_source, TEXT_MIME, &length);
} else if (Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, TEXT_MIME)) { } else {
text = Wayland_primary_selection_offer_receive(primary_selection_device->selection_offer, TEXT_MIME, &length); for (size_t i = 0; i < SDL_arraysize(text_mime_types); i++) {
if (Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, text_mime_types[i])) {
text = Wayland_primary_selection_offer_receive(primary_selection_device->selection_offer, text_mime_types[i], &length);
break;
}
}
} }
} }
@@ -165,7 +170,14 @@ bool Wayland_HasPrimarySelectionText(SDL_VideoDevice *_this)
if (primary_selection_device->selection_source) { if (primary_selection_device->selection_source) {
result = true; result = true;
} else { } else {
result = Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, TEXT_MIME); size_t mime_count = 0;
const char **mime_types = Wayland_GetTextMimeTypes(_this, &mime_count);
for (size_t i = 0; i < mime_count; i++) {
if (Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, mime_types[i])) {
result = true;
break;
}
}
} }
} }
return result; return result;

View File

@@ -85,6 +85,7 @@ typedef struct
uint32_t drag_serial; uint32_t drag_serial;
SDL_WaylandDataOffer *drag_offer; SDL_WaylandDataOffer *drag_offer;
SDL_WaylandDataOffer *selection_offer; SDL_WaylandDataOffer *selection_offer;
const char *mime_type;
bool has_mime_file, has_mime_text; bool has_mime_file, has_mime_text;
SDL_Window *dnd_window; SDL_Window *dnd_window;

View File

@@ -34,6 +34,7 @@
#include "SDL_waylandevents_c.h" #include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.h" #include "SDL_waylandwindow.h"
#include "SDL_waylandmouse.h" #include "SDL_waylandmouse.h"
#include "SDL_waylandclipboard.h"
#include "pointer-constraints-unstable-v1-client-protocol.h" #include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h" #include "relative-pointer-unstable-v1-client-protocol.h"
@@ -2258,17 +2259,25 @@ static void data_device_handle_enter(void *data, struct wl_data_device *wl_data_
#ifdef SDL_USE_LIBDBUS #ifdef SDL_USE_LIBDBUS
if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_PORTAL_MIME)) { if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_PORTAL_MIME)) {
data_device->has_mime_file = true; data_device->has_mime_file = true;
data_device->mime_type = FILE_PORTAL_MIME;
wl_data_offer_accept(id, serial, FILE_PORTAL_MIME); wl_data_offer_accept(id, serial, FILE_PORTAL_MIME);
} }
#endif #endif
if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_MIME)) { if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_MIME)) {
data_device->has_mime_file = true; data_device->has_mime_file = true;
data_device->mime_type = FILE_MIME;
wl_data_offer_accept(id, serial, FILE_MIME); wl_data_offer_accept(id, serial, FILE_MIME);
} }
if (Wayland_data_offer_has_mime(data_device->drag_offer, TEXT_MIME)) { size_t mime_count = 0;
const char **text_mime_types = Wayland_GetTextMimeTypes(SDL_GetVideoDevice(), &mime_count);
for (size_t i = 0; i < mime_count; ++i) {
if (Wayland_data_offer_has_mime(data_device->drag_offer, text_mime_types[i])) {
data_device->has_mime_text = true; data_device->has_mime_text = true;
wl_data_offer_accept(id, serial, TEXT_MIME); data_device->mime_type = text_mime_types[i];
wl_data_offer_accept(id, serial, text_mime_types[i]);
break;
}
} }
// SDL only supports "copy" style drag and drop // SDL only supports "copy" style drag and drop
@@ -2411,9 +2420,7 @@ static void data_device_handle_drop(void *data, struct wl_data_device *wl_data_d
* non paths that are not visible to the application * non paths that are not visible to the application
*/ */
if (!drop_handled) { if (!drop_handled) {
const char *mime_type = data_device->has_mime_file ? FILE_MIME : (data_device->has_mime_text ? TEXT_MIME : ""); void *buffer = Wayland_data_offer_receive(data_device->drag_offer, data_device->mime_type, &length);
void *buffer = Wayland_data_offer_receive(data_device->drag_offer,
mime_type, &length);
if (data_device->has_mime_file) { if (data_device->has_mime_file) {
if (buffer) { if (buffer) {
char *saveptr = NULL; char *saveptr = NULL;