Skip to content

Commit cc2713c

Browse files
committed
wayland: Don't clear the clipboard when a nil selection is ignored
If the previous selection offering was recursive, a nil selection should be ignored, or clearing the clipboard may clear the backing memory of the client's selection offering as well.
1 parent ae2301e commit cc2713c

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/video/wayland/SDL_waylanddatamanager.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ static void offer_source_done_handler(void *data, struct wl_callback *callback,
422422
SDL_free(id);
423423
if (source_is_external) {
424424
Wayland_data_offer_notify_from_mimes(offer, false);
425+
} else {
426+
// Recursive data offer; just destroy it.
427+
SDL_WaylandDataDevice *data_device = offer->data_device;
428+
Wayland_data_offer_destroy(offer);
429+
data_device->selection_offer = NULL;
425430
}
426431
}
427432
}

src/video/wayland/SDL_waylandevents.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,12 +3065,14 @@ static void data_device_handle_selection(void *data, struct wl_data_device *wl_d
30653065
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
30663066
". In data_device_listener . data_device_handle_selection on data_offer 0x%08x",
30673067
(id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
3068-
if (data_device->selection_offer != offer) {
3069-
Wayland_data_offer_destroy(data_device->selection_offer);
3070-
data_device->selection_offer = offer;
3071-
}
30723068

3073-
Wayland_data_offer_notify_from_mimes(offer, true);
3069+
// Don't notify when clearing the old selection offer if doing so will inadvertently clear the selection source.
3070+
const bool notify = offer || (!offer && data_device->selection_offer && (!data_device->selection_offer->callback || !data_device->selection_source));
3071+
Wayland_data_offer_destroy(data_device->selection_offer);
3072+
data_device->selection_offer = offer;
3073+
if (notify) {
3074+
Wayland_data_offer_notify_from_mimes(offer, true);
3075+
}
30743076
}
30753077

30763078
static const struct wl_data_device_listener data_device_listener = {

0 commit comments

Comments
 (0)