De-duplicate data source MIME types

This commit is contained in:
emersion 2019-01-30 22:08:18 +01:00
parent 41af8d8459
commit 790f0c52a1
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 40 additions and 8 deletions

View File

@ -190,16 +190,29 @@ static void data_source_offer(struct wl_client *client,
"wl_data_device.set_selection"); "wl_data_device.set_selection");
} }
char **p = wl_array_add(&source->source.mime_types, sizeof(*p)); const char **mime_type_ptr;
if (p) { wl_array_for_each(mime_type_ptr, &source->source.mime_types) {
*p = strdup(mime_type); if (strcmp(*mime_type_ptr, mime_type) == 0) {
} wlr_log(WLR_DEBUG, "Ignoring duplicate MIME type offer %s",
if (!p || !*p) { mime_type);
if (p) { return;
source->source.mime_types.size -= sizeof(*p);
} }
wl_resource_post_no_memory(resource);
} }
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 = { static const struct wl_data_source_interface data_source_impl = {

View File

@ -5,6 +5,7 @@
#include <unistd.h> #include <unistd.h>
#include <wlr/types/wlr_data_control_v1.h> #include <wlr/types/wlr_data_control_v1.h>
#include <wlr/types/wlr_data_device.h> #include <wlr/types/wlr_data_device.h>
#include <wlr/util/log.h>
#include "util/signal.h" #include "util/signal.h"
#include "wlr-data-control-unstable-v1-protocol.h" #include "wlr-data-control-unstable-v1-protocol.h"
@ -69,6 +70,15 @@ static void source_handle_offer(struct wl_client *client,
return; 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); char *dup_mime_type = strdup(mime_type);
if (dup_mime_type == NULL) { if (dup_mime_type == NULL) {
wl_resource_post_no_memory(resource); wl_resource_post_no_memory(resource);

View File

@ -143,6 +143,15 @@ static void source_handle_offer(struct wl_client *client,
wlr_log(WLR_DEBUG, "Offering additional MIME type after set_selection"); 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); char *dup_mime_type = strdup(mime_type);
if (dup_mime_type == NULL) { if (dup_mime_type == NULL) {
wl_resource_post_no_memory(resource); wl_resource_post_no_memory(resource);