From bfa7f4ee0dd5988ea54795862a12a1eb1680b3f7 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 27 Nov 2018 20:16:55 +0100 Subject: [PATCH] gtk-primary-selection: use impl pattern for sources --- include/wlr/types/wlr_gtk_primary_selection.h | 24 ++-- types/wlr_gtk_primary_selection.c | 107 +++++++++++------- xwayland/selection/incoming.c | 28 +++-- xwayland/selection/outgoing.c | 2 +- 4 files changed, 100 insertions(+), 61 deletions(-) diff --git a/include/wlr/types/wlr_gtk_primary_selection.h b/include/wlr/types/wlr_gtk_primary_selection.h index ed025869..cbf29088 100644 --- a/include/wlr/types/wlr_gtk_primary_selection.h +++ b/include/wlr/types/wlr_gtk_primary_selection.h @@ -45,18 +45,24 @@ struct wlr_gtk_primary_selection_device { void *data; }; +/** + * A data source implementation. Only the `send` function is mandatory. + */ +struct wlr_gtk_primary_selection_source_impl { + void (*send)(struct wlr_gtk_primary_selection_source *source, + const char *mime_type, int fd); + void (*destroy)(struct wlr_gtk_primary_selection_source *source); +}; + /** * A source is the sending side of a selection. */ struct wlr_gtk_primary_selection_source { + const struct wlr_gtk_primary_selection_source_impl *impl; + // source metadata struct wl_array mime_types; - // source implementation - void (*send)(struct wlr_gtk_primary_selection_source *source, - const char *mime_type, int32_t fd); - void (*cancel)(struct wlr_gtk_primary_selection_source *source); - struct { struct wl_signal destroy; } events; @@ -75,8 +81,12 @@ void wlr_gtk_primary_selection_device_manager_set_selection( struct wlr_gtk_primary_selection_source *source); void wlr_gtk_primary_selection_source_init( + struct wlr_gtk_primary_selection_source *source, + const struct wlr_gtk_primary_selection_source_impl *impl); +void wlr_gtk_primary_selection_source_destroy( struct wlr_gtk_primary_selection_source *source); -void wlr_gtk_primary_selection_source_finish( - struct wlr_gtk_primary_selection_source *source); +void wlr_gtk_primary_selection_source_send( + struct wlr_gtk_primary_selection_source *source, const char *mime_type, + int fd); #endif diff --git a/types/wlr_gtk_primary_selection.c b/types/wlr_gtk_primary_selection.c index 9a8848d6..5a31d242 100644 --- a/types/wlr_gtk_primary_selection.c +++ b/types/wlr_gtk_primary_selection.c @@ -29,7 +29,7 @@ static void offer_handle_receive(struct wl_client *client, return; } - device->source->send(device->source, mime_type, fd); + wlr_gtk_primary_selection_source_send(device->source, mime_type, fd); } static void offer_handle_destroy(struct wl_client *client, @@ -99,19 +99,26 @@ struct client_data_source { static void client_source_send( struct wlr_gtk_primary_selection_source *wlr_source, - const char *mime_type, int32_t fd) { + const char *mime_type, int fd) { struct client_data_source *source = (struct client_data_source *)wlr_source; gtk_primary_selection_source_send_send(source->resource, mime_type, fd); close(fd); } -static void client_source_cancel( +static void client_source_destroy( struct wlr_gtk_primary_selection_source *wlr_source) { struct client_data_source *source = (struct client_data_source *)wlr_source; gtk_primary_selection_source_send_cancelled(source->resource); - // TODO: make the source resource inert + // Make the source resource inert + wl_resource_set_user_data(source->resource, NULL); + free(source); } +static const struct wlr_gtk_primary_selection_source_impl client_source_impl = { + .send = client_source_send, + .destroy = client_source_destroy, +}; + static const struct gtk_primary_selection_source_interface source_impl; static struct client_data_source *client_data_source_from_resource( @@ -125,6 +132,9 @@ static void source_handle_offer(struct wl_client *client, struct wl_resource *resource, const char *mime_type) { struct client_data_source *source = client_data_source_from_resource(resource); + if (source == NULL) { + return; + } char **p = wl_array_add(&source->source.mime_types, sizeof(*p)); if (p) { @@ -151,8 +161,10 @@ static const struct gtk_primary_selection_source_interface source_impl = { static void source_resource_handle_destroy(struct wl_resource *resource) { struct client_data_source *source = client_data_source_from_resource(resource); - wlr_gtk_primary_selection_source_finish(&source->source); - free(source); + if (source == NULL) { + return; + } + wlr_gtk_primary_selection_source_destroy(&source->source); } @@ -174,15 +186,20 @@ static void device_handle_set_selection(struct wl_client *client, return; } - struct client_data_source *source = NULL; + struct client_data_source *client_source = NULL; if (source_resource != NULL) { - source = client_data_source_from_resource(source_resource); + client_source = client_data_source_from_resource(source_resource); + } + + struct wlr_gtk_primary_selection_source *source = NULL; + if (client_source != NULL) { + source = &client_source->source; } // TODO: improve serial checking struct wlr_seat *seat = device->seat; wlr_gtk_primary_selection_device_manager_set_selection(device->manager, - seat, &source->source); + seat, source); } static void device_handle_destroy(struct wl_client *client, @@ -230,14 +247,9 @@ static void device_handle_source_destroy(struct wl_listener *listener, static void device_set_selection( struct wlr_gtk_primary_selection_device *device, struct wlr_gtk_primary_selection_source *source) { - if (source != NULL) { - assert(source->send); - assert(source->cancel); - } - if (device->source != NULL) { wl_list_remove(&device->source_destroy.link); - device->source->cancel(device->source); + wlr_gtk_primary_selection_source_destroy(device->source); device->source = NULL; } @@ -328,7 +340,7 @@ static void device_destroy(struct wlr_gtk_primary_selection_device *device) { wl_list_remove(&device->seat_focus_change.link); if (device->source != NULL) { wl_list_remove(&device->source_destroy.link); - device->source->cancel(device->source); + wlr_gtk_primary_selection_source_destroy(device->source); } struct wl_resource *resource, *resource_tmp; wl_resource_for_each_safe(resource, resource_tmp, &device->offers) { @@ -356,27 +368,6 @@ struct wlr_gtk_primary_selection_device_manager *manager_from_resource( return wl_resource_get_user_data(resource); } -void wlr_gtk_primary_selection_source_init( - struct wlr_gtk_primary_selection_source *source) { - wl_array_init(&source->mime_types); - wl_signal_init(&source->events.destroy); -} - -void wlr_gtk_primary_selection_source_finish( - struct wlr_gtk_primary_selection_source *source) { - if (source == NULL) { - return; - } - - wlr_signal_emit_safe(&source->events.destroy, source); - - char **p; - wl_array_for_each(p, &source->mime_types) { - free(*p); - } - wl_array_release(&source->mime_types); -} - static void device_manager_handle_create_source(struct wl_client *client, struct wl_resource *manager_resource, uint32_t id) { struct client_data_source *source = @@ -385,7 +376,7 @@ static void device_manager_handle_create_source(struct wl_client *client, wl_client_post_no_memory(client); return; } - wlr_gtk_primary_selection_source_init(&source->source); + wlr_gtk_primary_selection_source_init(&source->source, &client_source_impl); uint32_t version = wl_resource_get_version(manager_resource); source->resource = wl_resource_create(client, @@ -397,9 +388,6 @@ static void device_manager_handle_create_source(struct wl_client *client, } wl_resource_set_implementation(source->resource, &source_impl, source, source_resource_handle_destroy); - - source->source.send = client_source_send; - source->source.cancel = client_source_cancel; } void device_manager_handle_get_device(struct wl_client *client, @@ -525,3 +513,40 @@ void wlr_gtk_primary_selection_device_manager_destroy( wl_global_destroy(manager->global); free(manager); } + + +void wlr_gtk_primary_selection_source_init( + struct wlr_gtk_primary_selection_source *source, + const struct wlr_gtk_primary_selection_source_impl *impl) { + assert(impl->send); + wl_array_init(&source->mime_types); + wl_signal_init(&source->events.destroy); + source->impl = impl; +} + +void wlr_gtk_primary_selection_source_destroy( + struct wlr_gtk_primary_selection_source *source) { + if (source == NULL) { + return; + } + + wlr_signal_emit_safe(&source->events.destroy, source); + + char **p; + wl_array_for_each(p, &source->mime_types) { + free(*p); + } + wl_array_release(&source->mime_types); + + if (source->impl->destroy) { + source->impl->destroy(source); + } else { + free(source); + } +} + +void wlr_gtk_primary_selection_source_send( + struct wlr_gtk_primary_selection_source *source, const char *mime_type, + int32_t fd) { + source->impl->send(source, mime_type, fd); +} diff --git a/xwayland/selection/incoming.c b/xwayland/selection/incoming.c index 0fe759a0..3342a761 100644 --- a/xwayland/selection/incoming.c +++ b/xwayland/selection/incoming.c @@ -137,7 +137,7 @@ static void xwm_selection_get_data(struct wlr_xwm_selection *selection) { static void source_send(struct wlr_xwm_selection *selection, struct wl_array *mime_types, struct wl_array *mime_types_atoms, - const char *requested_mime_type, int32_t fd) { + const char *requested_mime_type, int fd) { struct wlr_xwm *xwm = selection->xwm; struct wlr_xwm_selection_transfer *transfer = &selection->incoming; @@ -222,17 +222,17 @@ struct x11_primary_selection_source { struct wl_array mime_types_atoms; }; -static void primary_selection_source_cancel( - struct wlr_gtk_primary_selection_source *wlr_source); +static const struct wlr_gtk_primary_selection_source_impl + primary_selection_source_impl; bool primary_selection_source_is_xwayland( struct wlr_gtk_primary_selection_source *wlr_source) { - return wlr_source->cancel == primary_selection_source_cancel; + return wlr_source->impl == &primary_selection_source_impl; } static void primary_selection_source_send( - struct wlr_gtk_primary_selection_source *wlr_source, const char *mime_type, - int32_t fd) { + struct wlr_gtk_primary_selection_source *wlr_source, + const char *mime_type, int fd) { struct x11_primary_selection_source *source = (struct x11_primary_selection_source *)wlr_source; struct wlr_xwm_selection *selection = source->selection; @@ -241,15 +241,20 @@ static void primary_selection_source_send( mime_type, fd); } -static void primary_selection_source_cancel( +static void primary_selection_source_destroy( struct wlr_gtk_primary_selection_source *wlr_source) { struct x11_primary_selection_source *source = (struct x11_primary_selection_source *)wlr_source; - wlr_gtk_primary_selection_source_finish(&source->base); wl_array_release(&source->mime_types_atoms); free(source); } +static const struct wlr_gtk_primary_selection_source_impl + primary_selection_source_impl = { + .send = primary_selection_source_send, + .destroy = primary_selection_source_destroy, +}; + static bool source_get_targets(struct wlr_xwm_selection *selection, struct wl_array *mime_types, struct wl_array *mime_types_atoms) { struct wlr_xwm *xwm = selection->xwm; @@ -356,9 +361,8 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { if (source == NULL) { return; } - wlr_gtk_primary_selection_source_init(&source->base); - source->base.send = primary_selection_source_send; - source->base.cancel = primary_selection_source_cancel; + wlr_gtk_primary_selection_source_init(&source->base, + &primary_selection_source_impl); source->selection = selection; wl_array_init(&source->mime_types_atoms); @@ -369,7 +373,7 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { wlr_gtk_primary_selection_device_manager_set_selection( xwm->xwayland->gtk_primary_selection, xwm->seat, &source->base); } else { - source->base.cancel(&source->base); + wlr_gtk_primary_selection_source_destroy(&source->base); } } else if (selection == &xwm->dnd_selection) { // TODO diff --git a/xwayland/selection/outgoing.c b/xwayland/selection/outgoing.c index b658ab52..1c994e18 100644 --- a/xwayland/selection/outgoing.c +++ b/xwayland/selection/outgoing.c @@ -198,7 +198,7 @@ static void xwm_selection_source_send(struct wlr_xwm_selection *selection, struct wlr_gtk_primary_selection_source *source = selection->xwm->seat->primary_selection_source; if (source != NULL) { - source->send(source, mime_type, fd); + wlr_gtk_primary_selection_source_send(source, mime_type, fd); return; } } else if (selection == &selection->xwm->dnd_selection) {