From 790f0c52a18d5ecb1f7a6da7166a8fb5d0909fdb Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 30 Jan 2019 22:08:18 +0100 Subject: [PATCH] De-duplicate data source MIME types --- types/data_device/wlr_data_source.c | 27 ++++++++++++++++++++------- types/wlr_data_control_v1.c | 10 ++++++++++ types/wlr_gtk_primary_selection.c | 9 +++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/types/data_device/wlr_data_source.c b/types/data_device/wlr_data_source.c index ed794454..0ab0d7f7 100644 --- a/types/data_device/wlr_data_source.c +++ b/types/data_device/wlr_data_source.c @@ -190,16 +190,29 @@ static void data_source_offer(struct wl_client *client, "wl_data_device.set_selection"); } - char **p = wl_array_add(&source->source.mime_types, sizeof(*p)); - if (p) { - *p = strdup(mime_type); - } - if (!p || !*p) { - if (p) { - source->source.mime_types.size -= sizeof(*p); + const char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, &source->source.mime_types) { + if (strcmp(*mime_type_ptr, mime_type) == 0) { + wlr_log(WLR_DEBUG, "Ignoring duplicate MIME type offer %s", + mime_type); + return; } + } + + char *dup_mime_type = strdup(mime_type); + if (dup_mime_type == NULL) { wl_resource_post_no_memory(resource); + return; } + + char **p = wl_array_add(&source->source.mime_types, sizeof(*p)); + if (p == NULL) { + free(dup_mime_type); + wl_resource_post_no_memory(resource); + return; + } + + *p = dup_mime_type; } static const struct wl_data_source_interface data_source_impl = { diff --git a/types/wlr_data_control_v1.c b/types/wlr_data_control_v1.c index 5bb7e81c..2b1a33bf 100644 --- a/types/wlr_data_control_v1.c +++ b/types/wlr_data_control_v1.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "util/signal.h" #include "wlr-data-control-unstable-v1-protocol.h" @@ -69,6 +70,15 @@ static void source_handle_offer(struct wl_client *client, return; } + const char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, &source->source.mime_types) { + if (strcmp(*mime_type_ptr, mime_type) == 0) { + wlr_log(WLR_DEBUG, "Ignoring duplicate MIME type offer %s", + mime_type); + return; + } + } + char *dup_mime_type = strdup(mime_type); if (dup_mime_type == NULL) { wl_resource_post_no_memory(resource); diff --git a/types/wlr_gtk_primary_selection.c b/types/wlr_gtk_primary_selection.c index 6c2e3f43..a7b159b0 100644 --- a/types/wlr_gtk_primary_selection.c +++ b/types/wlr_gtk_primary_selection.c @@ -143,6 +143,15 @@ static void source_handle_offer(struct wl_client *client, wlr_log(WLR_DEBUG, "Offering additional MIME type after set_selection"); } + const char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, &source->source.mime_types) { + if (strcmp(*mime_type_ptr, mime_type) == 0) { + wlr_log(WLR_DEBUG, "Ignoring duplicate MIME type offer %s", + mime_type); + return; + } + } + char *dup_mime_type = strdup(mime_type); if (dup_mime_type == NULL) { wl_resource_post_no_memory(resource);