From 811a4d997b2e00c7cdc3851ccc55a6f52d79126c Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 23 Nov 2018 11:58:56 +0100 Subject: Rename wlr_primary_selection to wlr_gtk_primary_selection --- include/rootston/desktop.h | 4 +- include/wlr/types/meson.build | 2 +- include/wlr/types/wlr_gtk_primary_selection.h | 70 +++++ include/wlr/types/wlr_primary_selection.h | 70 ----- include/wlr/types/wlr_seat.h | 2 +- include/xwayland/selection.h | 2 +- rootston/desktop.c | 4 +- types/meson.build | 2 +- types/seat/wlr_seat.c | 2 +- types/seat/wlr_seat_keyboard.c | 4 +- types/wlr_gtk_primary_selection.c | 425 ++++++++++++++++++++++++++ types/wlr_primary_selection.c | 425 -------------------------- xwayland/selection/dnd.c | 2 +- xwayland/selection/incoming.c | 20 +- xwayland/selection/outgoing.c | 6 +- xwayland/selection/selection.c | 6 +- 16 files changed, 523 insertions(+), 523 deletions(-) create mode 100644 include/wlr/types/wlr_gtk_primary_selection.h delete mode 100644 include/wlr/types/wlr_primary_selection.h create mode 100644 types/wlr_gtk_primary_selection.c delete mode 100644 types/wlr_primary_selection.c diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index 4ce92e2b..31338acf 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -51,7 +51,7 @@ struct roots_desktop { struct wlr_export_dmabuf_manager_v1 *export_dmabuf_manager_v1; struct wlr_server_decoration_manager *server_decoration_manager; struct wlr_xdg_decoration_manager_v1 *xdg_decoration_manager; - struct wlr_primary_selection_device_manager *primary_selection_device_manager; + struct wlr_gtk_primary_selection_device_manager *primary_selection_device_manager; struct wlr_idle *idle; struct wlr_idle_inhibit_manager_v1 *idle_inhibit; struct wlr_input_inhibit_manager *input_inhibit; diff --git a/include/wlr/types/meson.build b/include/wlr/types/meson.build index 3f61ae20..df611970 100644 --- a/include/wlr/types/meson.build +++ b/include/wlr/types/meson.build @@ -7,6 +7,7 @@ install_headers( 'wlr_export_dmabuf_v1.h', 'wlr_gamma_control_v1.h', 'wlr_gamma_control.h', + 'wlr_gtk_primary_selection.h', 'wlr_idle_inhibit_v1.h', 'wlr_idle.h', 'wlr_input_device.h', @@ -22,7 +23,6 @@ install_headers( 'wlr_output.h', 'wlr_pointer.h', 'wlr_presentation_time.h', - 'wlr_primary_selection.h', 'wlr_region.h', 'wlr_screencopy_v1.h', 'wlr_screenshooter.h', diff --git a/include/wlr/types/wlr_gtk_primary_selection.h b/include/wlr/types/wlr_gtk_primary_selection.h new file mode 100644 index 00000000..f3410f69 --- /dev/null +++ b/include/wlr/types/wlr_gtk_primary_selection.h @@ -0,0 +1,70 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_GTK_PRIMARY_SELECTION_H +#define WLR_TYPES_WLR_GTK_PRIMARY_SELECTION_H + +#include +#include + +struct wlr_gtk_primary_selection_device_manager { + struct wl_global *global; + struct wl_list resources; + + struct wl_listener display_destroy; + + struct { + struct wl_signal destroy; + } events; + + void *data; +}; + +struct wlr_gtk_primary_selection_source { + // 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); + + // source status + struct wlr_seat_client *seat_client; + + struct { + struct wl_signal destroy; + } events; + + void *data; +}; + +struct wlr_gtk_primary_selection_offer { + struct wl_resource *resource; + struct wlr_gtk_primary_selection_source *source; + + struct wl_listener source_destroy; + + void *data; +}; + +struct wlr_gtk_primary_selection_device_manager * + wlr_gtk_primary_selection_device_manager_create(struct wl_display *display); +void wlr_gtk_primary_selection_device_manager_destroy( + struct wlr_gtk_primary_selection_device_manager *manager); + +void wlr_seat_client_send_gtk_primary_selection(struct wlr_seat_client *seat_client); +void wlr_seat_set_gtk_primary_selection(struct wlr_seat *seat, + struct wlr_gtk_primary_selection_source *source, uint32_t serial); + +void wlr_gtk_primary_selection_source_init( + struct wlr_gtk_primary_selection_source *source); +void wlr_gtk_primary_selection_source_finish( + struct wlr_gtk_primary_selection_source *source); + +#endif diff --git a/include/wlr/types/wlr_primary_selection.h b/include/wlr/types/wlr_primary_selection.h deleted file mode 100644 index 192bfc02..00000000 --- a/include/wlr/types/wlr_primary_selection.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * This an unstable interface of wlroots. No guarantees are made regarding the - * future consistency of this API. - */ -#ifndef WLR_USE_UNSTABLE -#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" -#endif - -#ifndef WLR_TYPES_WLR_PRIMARY_SELECTION_H -#define WLR_TYPES_WLR_PRIMARY_SELECTION_H - -#include -#include - -struct wlr_primary_selection_device_manager { - struct wl_global *global; - struct wl_list resources; - - struct wl_listener display_destroy; - - struct { - struct wl_signal destroy; - } events; - - void *data; -}; - -struct wlr_primary_selection_source { - // source metadata - struct wl_array mime_types; - - // source implementation - void (*send)(struct wlr_primary_selection_source *source, - const char *mime_type, int32_t fd); - void (*cancel)(struct wlr_primary_selection_source *source); - - // source status - struct wlr_seat_client *seat_client; - - struct { - struct wl_signal destroy; - } events; - - void *data; -}; - -struct wlr_primary_selection_offer { - struct wl_resource *resource; - struct wlr_primary_selection_source *source; - - struct wl_listener source_destroy; - - void *data; -}; - -struct wlr_primary_selection_device_manager * - wlr_primary_selection_device_manager_create(struct wl_display *display); -void wlr_primary_selection_device_manager_destroy( - struct wlr_primary_selection_device_manager *manager); - -void wlr_seat_client_send_primary_selection(struct wlr_seat_client *seat_client); -void wlr_seat_set_primary_selection(struct wlr_seat *seat, - struct wlr_primary_selection_source *source, uint32_t serial); - -void wlr_primary_selection_source_init( - struct wlr_primary_selection_source *source); -void wlr_primary_selection_source_finish( - struct wlr_primary_selection_source *source); - -#endif diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index 59de8207..9670e6a5 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -194,7 +194,7 @@ struct wlr_seat { struct wlr_data_source *selection_source; uint32_t selection_serial; - struct wlr_primary_selection_source *primary_selection_source; + struct wlr_gtk_primary_selection_source *primary_selection_source; uint32_t primary_selection_serial; // `drag` goes away before `drag_source`, when the implicit grab ends diff --git a/include/xwayland/selection.h b/include/xwayland/selection.h index fc910e46..6529e9eb 100644 --- a/include/xwayland/selection.h +++ b/include/xwayland/selection.h @@ -62,7 +62,7 @@ int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, xcb_xfixes_selection_notify_event_t *event); bool data_source_is_xwayland(struct wlr_data_source *wlr_source); bool primary_selection_source_is_xwayland( - struct wlr_primary_selection_source *wlr_source); + struct wlr_gtk_primary_selection_source *wlr_source); void xwm_seat_handle_start_drag(struct wlr_xwm *xwm, struct wlr_drag *drag); diff --git a/rootston/desktop.c b/rootston/desktop.c index e49d61a1..65a3509a 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -956,7 +956,7 @@ struct roots_desktop *desktop_create(struct roots_server *server, desktop->server_decoration_manager, WLR_SERVER_DECORATION_MANAGER_MODE_CLIENT); desktop->primary_selection_device_manager = - wlr_primary_selection_device_manager_create(server->wl_display); + wlr_gtk_primary_selection_device_manager_create(server->wl_display); desktop->idle = wlr_idle_create(server->wl_display); desktop->idle_inhibit = wlr_idle_inhibit_v1_create(server->wl_display); diff --git a/types/meson.build b/types/meson.build index bd7fd1a0..f7653e55 100644 --- a/types/meson.build +++ b/types/meson.build @@ -46,7 +46,7 @@ lib_wlr_types = static_library( 'wlr_pointer_constraints_v1.c', 'wlr_pointer.c', 'wlr_presentation_time.c', - 'wlr_primary_selection.c', + 'wlr_gtk_primary_selection.c', 'wlr_region.c', 'wlr_screencopy_v1.c', 'wlr_screenshooter.c', diff --git a/types/seat/wlr_seat.c b/types/seat/wlr_seat.c index 1940d7d6..537a3bae 100644 --- a/types/seat/wlr_seat.c +++ b/types/seat/wlr_seat.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include "types/wlr_seat.h" diff --git a/types/seat/wlr_seat_keyboard.c b/types/seat/wlr_seat_keyboard.c index 53415f0c..1269c972 100644 --- a/types/seat/wlr_seat_keyboard.c +++ b/types/seat/wlr_seat_keyboard.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include "types/wlr_seat.h" #include "util/signal.h" @@ -273,7 +273,7 @@ void wlr_seat_keyboard_enter(struct wlr_seat *seat, wl_array_release(&keys); wlr_seat_client_send_selection(client); - wlr_seat_client_send_primary_selection(client); + wlr_seat_client_send_gtk_primary_selection(client); } // reinitialize the focus destroy events diff --git a/types/wlr_gtk_primary_selection.c b/types/wlr_gtk_primary_selection.c new file mode 100644 index 00000000..5493a8c0 --- /dev/null +++ b/types/wlr_gtk_primary_selection.c @@ -0,0 +1,425 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include "gtk-primary-selection-protocol.h" +#include "util/signal.h" + +#define DEVICE_MANAGER_VERSION 1 + +static const struct gtk_primary_selection_offer_interface offer_impl; + +static struct wlr_gtk_primary_selection_offer *offer_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + >k_primary_selection_offer_interface, &offer_impl)); + return wl_resource_get_user_data(resource); +} + +static void offer_handle_receive(struct wl_client *client, + struct wl_resource *resource, const char *mime_type, int32_t fd) { + struct wlr_gtk_primary_selection_offer *offer = offer_from_resource(resource); + if (offer == NULL) { + close(fd); + return; + } + + offer->source->send(offer->source, mime_type, fd); +} + +static void offer_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct gtk_primary_selection_offer_interface offer_impl = { + .receive = offer_handle_receive, + .destroy = offer_handle_destroy, +}; + +static void offer_destroy(struct wlr_gtk_primary_selection_offer *offer) { + if (offer == NULL) { + return; + } + // Make resource inert + wl_resource_set_user_data(offer->resource, NULL); + wl_list_remove(&offer->source_destroy.link); + free(offer); +} + +static void offer_handle_resource_destroy(struct wl_resource *resource) { + struct wlr_gtk_primary_selection_offer *offer = offer_from_resource(resource); + offer_destroy(offer); +} + +static void offer_handle_source_destroy(struct wl_listener *listener, + void *data) { + struct wlr_gtk_primary_selection_offer *offer = + wl_container_of(listener, offer, source_destroy); + offer_destroy(offer); +} + + +struct client_data_source { + struct wlr_gtk_primary_selection_source source; + struct wl_resource *resource; +}; + +static void client_source_send(struct wlr_gtk_primary_selection_source *wlr_source, + const char *mime_type, int32_t 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( + 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); +} + +static void source_send_offer(struct wlr_gtk_primary_selection_source *source, + struct wl_resource *device_resource) { + struct wlr_gtk_primary_selection_offer *offer = + calloc(1, sizeof(struct wlr_gtk_primary_selection_offer)); + if (offer == NULL) { + wl_resource_post_no_memory(device_resource); + return; + } + + struct wl_client *client = wl_resource_get_client(device_resource); + uint32_t version = wl_resource_get_version(device_resource); + offer->resource = wl_resource_create(client, + >k_primary_selection_offer_interface, version, 0); + if (offer->resource == NULL) { + free(offer); + wl_resource_post_no_memory(device_resource); + return; + } + wl_resource_set_implementation(offer->resource, &offer_impl, offer, + offer_handle_resource_destroy); + + offer->source_destroy.notify = offer_handle_source_destroy; + wl_signal_add(&source->events.destroy, &offer->source_destroy); + + gtk_primary_selection_device_send_data_offer(device_resource, + offer->resource); + + char **p; + wl_array_for_each(p, &source->mime_types) { + gtk_primary_selection_offer_send_offer(offer->resource, *p); + } + + offer->source = source; + + gtk_primary_selection_device_send_selection(device_resource, + offer->resource); +} + +static const struct gtk_primary_selection_source_interface source_impl; + +static struct client_data_source *client_data_source_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + >k_primary_selection_source_interface, &source_impl)); + return wl_resource_get_user_data(resource); +} + +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); + + char **p = wl_array_add(&source->source.mime_types, sizeof(*p)); + if (p) { + *p = strdup(mime_type); + } + if (p == NULL || *p == NULL) { + if (p) { + source->source.mime_types.size -= sizeof(*p); + } + wl_resource_post_no_memory(resource); + } +} + +static void source_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct gtk_primary_selection_source_interface source_impl = { + .offer = source_handle_offer, + .destroy = source_handle_destroy, +}; + +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); +} + + +void wlr_seat_client_send_gtk_primary_selection( + struct wlr_seat_client *seat_client) { + if (wl_list_empty(&seat_client->primary_selection_devices)) { + return; + } + + struct wlr_gtk_primary_selection_source *source = + seat_client->seat->primary_selection_source; + struct wl_resource *resource; + wl_resource_for_each(resource, &seat_client->primary_selection_devices) { + if (source) { + source_send_offer(source, resource); + } else { + gtk_primary_selection_device_send_selection(resource, NULL); + } + } +} + +static void seat_client_primary_selection_source_destroy( + struct wl_listener *listener, void *data) { + struct wlr_seat *seat = + wl_container_of(listener, seat, primary_selection_source_destroy); + struct wlr_seat_client *seat_client = seat->keyboard_state.focused_client; + + if (seat_client && seat->keyboard_state.focused_surface) { + struct wl_resource *resource; + wl_resource_for_each(resource, &seat_client->primary_selection_devices) { + gtk_primary_selection_device_send_selection(resource, NULL); + } + } + + seat->primary_selection_source = NULL; + + wlr_signal_emit_safe(&seat->events.primary_selection, seat); +} + +void wlr_seat_set_gtk_primary_selection(struct wlr_seat *seat, + struct wlr_gtk_primary_selection_source *source, uint32_t serial) { + if (source) { + assert(source->send); + assert(source->cancel); + } + + if (seat->primary_selection_source && + seat->primary_selection_serial - serial < UINT32_MAX / 2) { + return; + } + + // TODO: make all offers inert + if (seat->primary_selection_source) { + wl_list_remove(&seat->primary_selection_source_destroy.link); + seat->primary_selection_source->cancel(seat->primary_selection_source); + seat->primary_selection_source = NULL; + } + + seat->primary_selection_source = source; + seat->primary_selection_serial = serial; + + struct wlr_seat_client *focused_client = + seat->keyboard_state.focused_client; + if (focused_client) { + wlr_seat_client_send_gtk_primary_selection(focused_client); + } + + wlr_signal_emit_safe(&seat->events.primary_selection, seat); + + if (source) { + seat->primary_selection_source_destroy.notify = + seat_client_primary_selection_source_destroy; + wl_signal_add(&source->events.destroy, + &seat->primary_selection_source_destroy); + } +} + + +static const struct gtk_primary_selection_device_interface device_impl; + +static struct wlr_seat_client *seat_client_from_device_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + >k_primary_selection_device_interface, &device_impl)); + return wl_resource_get_user_data(resource); +} + +static void device_handle_set_selection(struct wl_client *client, + struct wl_resource *resource, struct wl_resource *source_resource, + uint32_t serial) { + struct client_data_source *source = NULL; + if (source_resource != NULL) { + source = client_data_source_from_resource(source_resource); + } + + struct wlr_seat_client *seat_client = + seat_client_from_device_resource(resource); + + struct wlr_gtk_primary_selection_source *wlr_source = + (struct wlr_gtk_primary_selection_source *)source; + wlr_seat_set_gtk_primary_selection(seat_client->seat, wlr_source, serial); +} + +static void device_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct gtk_primary_selection_device_interface device_impl = { + .set_selection = device_handle_set_selection, + .destroy = device_handle_destroy, +}; + +static void device_resource_handle_destroy(struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(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 = + calloc(1, sizeof(struct client_data_source)); + if (source == NULL) { + wl_client_post_no_memory(client); + return; + } + wlr_gtk_primary_selection_source_init(&source->source); + + int version = wl_resource_get_version(manager_resource); + source->resource = wl_resource_create(client, + >k_primary_selection_source_interface, version, id); + if (source->resource == NULL) { + free(source); + wl_client_post_no_memory(client); + return; + } + 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, + struct wl_resource *manager_resource, uint32_t id, + struct wl_resource *seat_resource) { + struct wlr_seat_client *seat_client = + wlr_seat_client_from_resource(seat_resource); + + uint32_t version = wl_resource_get_version(manager_resource); + struct wl_resource *resource = wl_resource_create(client, + >k_primary_selection_device_interface, version, id); + if (resource == NULL) { + wl_resource_post_no_memory(manager_resource); + return; + } + wl_resource_set_implementation(resource, &device_impl, seat_client, + &device_resource_handle_destroy); + wl_list_insert(&seat_client->primary_selection_devices, + wl_resource_get_link(resource)); +} + +static void device_manager_handle_destroy(struct wl_client *client, + struct wl_resource *manager_resource) { + wl_resource_destroy(manager_resource); +} + +static const struct gtk_primary_selection_device_manager_interface + device_manager_impl = { + .create_source = device_manager_handle_create_source, + .get_device = device_manager_handle_get_device, + .destroy = device_manager_handle_destroy, +}; + +static void device_manager_handle_resource_destroy( + struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + + +static void primary_selection_device_manager_bind(struct wl_client *client, + void *data, uint32_t version, uint32_t id) { + struct wlr_gtk_primary_selection_device_manager *manager = data; + + struct wl_resource *resource = wl_resource_create(client, + >k_primary_selection_device_manager_interface, version, id); + if (resource == NULL) { + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(resource, &device_manager_impl, manager, + device_manager_handle_resource_destroy); + + wl_list_insert(&manager->resources, wl_resource_get_link(resource)); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_gtk_primary_selection_device_manager *manager = + wl_container_of(listener, manager, display_destroy); + wlr_gtk_primary_selection_device_manager_destroy(manager); +} + +struct wlr_gtk_primary_selection_device_manager * + wlr_gtk_primary_selection_device_manager_create( + struct wl_display *display) { + struct wlr_gtk_primary_selection_device_manager *manager = + calloc(1, sizeof(struct wlr_gtk_primary_selection_device_manager)); + if (manager == NULL) { + return NULL; + } + manager->global = wl_global_create(display, + >k_primary_selection_device_manager_interface, DEVICE_MANAGER_VERSION, + manager, primary_selection_device_manager_bind); + if (manager->global == NULL) { + free(manager); + return NULL; + } + + wl_list_init(&manager->resources); + wl_signal_init(&manager->events.destroy); + + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); + + return manager; +} + +void wlr_gtk_primary_selection_device_manager_destroy( + struct wlr_gtk_primary_selection_device_manager *manager) { + if (manager == NULL) { + return; + } + wlr_signal_emit_safe(&manager->events.destroy, manager); + wl_list_remove(&manager->display_destroy.link); + struct wl_resource *resource, *resource_tmp; + wl_resource_for_each_safe(resource, resource_tmp, &manager->resources) { + wl_resource_destroy(resource); + } + wl_global_destroy(manager->global); + free(manager); +} diff --git a/types/wlr_primary_selection.c b/types/wlr_primary_selection.c deleted file mode 100644 index 74312540..00000000 --- a/types/wlr_primary_selection.c +++ /dev/null @@ -1,425 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include -#include -#include "gtk-primary-selection-protocol.h" -#include "util/signal.h" - -#define DEVICE_MANAGER_VERSION 1 - -static const struct gtk_primary_selection_offer_interface offer_impl; - -static struct wlr_primary_selection_offer *offer_from_resource( - struct wl_resource *resource) { - assert(wl_resource_instance_of(resource, - >k_primary_selection_offer_interface, &offer_impl)); - return wl_resource_get_user_data(resource); -} - -static void offer_handle_receive(struct wl_client *client, - struct wl_resource *resource, const char *mime_type, int32_t fd) { - struct wlr_primary_selection_offer *offer = offer_from_resource(resource); - if (offer == NULL) { - close(fd); - return; - } - - offer->source->send(offer->source, mime_type, fd); -} - -static void offer_handle_destroy(struct wl_client *client, - struct wl_resource *resource) { - wl_resource_destroy(resource); -} - -static const struct gtk_primary_selection_offer_interface offer_impl = { - .receive = offer_handle_receive, - .destroy = offer_handle_destroy, -}; - -static void offer_destroy(struct wlr_primary_selection_offer *offer) { - if (offer == NULL) { - return; - } - // Make resource inert - wl_resource_set_user_data(offer->resource, NULL); - wl_list_remove(&offer->source_destroy.link); - free(offer); -} - -static void offer_handle_resource_destroy(struct wl_resource *resource) { - struct wlr_primary_selection_offer *offer = offer_from_resource(resource); - offer_destroy(offer); -} - -static void offer_handle_source_destroy(struct wl_listener *listener, - void *data) { - struct wlr_primary_selection_offer *offer = - wl_container_of(listener, offer, source_destroy); - offer_destroy(offer); -} - - -struct client_data_source { - struct wlr_primary_selection_source source; - struct wl_resource *resource; -}; - -static void client_source_send(struct wlr_primary_selection_source *wlr_source, - const char *mime_type, int32_t 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( - struct wlr_primary_selection_source *wlr_source) { - struct client_data_source *source = (struct client_data_source *)wlr_source; - gtk_primary_selection_source_send_cancelled(source->resource); -} - -static void source_send_offer(struct wlr_primary_selection_source *source, - struct wl_resource *device_resource) { - struct wlr_primary_selection_offer *offer = - calloc(1, sizeof(struct wlr_primary_selection_offer)); - if (offer == NULL) { - wl_resource_post_no_memory(device_resource); - return; - } - - struct wl_client *client = wl_resource_get_client(device_resource); - uint32_t version = wl_resource_get_version(device_resource); - offer->resource = wl_resource_create(client, - >k_primary_selection_offer_interface, version, 0); - if (offer->resource == NULL) { - free(offer); - wl_resource_post_no_memory(device_resource); - return; - } - wl_resource_set_implementation(offer->resource, &offer_impl, offer, - offer_handle_resource_destroy); - - offer->source_destroy.notify = offer_handle_source_destroy; - wl_signal_add(&source->events.destroy, &offer->source_destroy); - - gtk_primary_selection_device_send_data_offer(device_resource, - offer->resource); - - char **p; - wl_array_for_each(p, &source->mime_types) { - gtk_primary_selection_offer_send_offer(offer->resource, *p); - } - - offer->source = source; - - gtk_primary_selection_device_send_selection(device_resource, - offer->resource); -} - -static const struct gtk_primary_selection_source_interface source_impl; - -static struct client_data_source *client_data_source_from_resource( - struct wl_resource *resource) { - assert(wl_resource_instance_of(resource, - >k_primary_selection_source_interface, &source_impl)); - return wl_resource_get_user_data(resource); -} - -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); - - char **p = wl_array_add(&source->source.mime_types, sizeof(*p)); - if (p) { - *p = strdup(mime_type); - } - if (p == NULL || *p == NULL) { - if (p) { - source->source.mime_types.size -= sizeof(*p); - } - wl_resource_post_no_memory(resource); - } -} - -static void source_handle_destroy(struct wl_client *client, - struct wl_resource *resource) { - wl_resource_destroy(resource); -} - -static const struct gtk_primary_selection_source_interface source_impl = { - .offer = source_handle_offer, - .destroy = source_handle_destroy, -}; - -static void source_resource_handle_destroy(struct wl_resource *resource) { - struct client_data_source *source = - client_data_source_from_resource(resource); - wlr_primary_selection_source_finish(&source->source); - free(source); -} - - -void wlr_seat_client_send_primary_selection( - struct wlr_seat_client *seat_client) { - if (wl_list_empty(&seat_client->primary_selection_devices)) { - return; - } - - struct wlr_primary_selection_source *source = - seat_client->seat->primary_selection_source; - struct wl_resource *resource; - wl_resource_for_each(resource, &seat_client->primary_selection_devices) { - if (source) { - source_send_offer(source, resource); - } else { - gtk_primary_selection_device_send_selection(resource, NULL); - } - } -} - -static void seat_client_primary_selection_source_destroy( - struct wl_listener *listener, void *data) { - struct wlr_seat *seat = - wl_container_of(listener, seat, primary_selection_source_destroy); - struct wlr_seat_client *seat_client = seat->keyboard_state.focused_client; - - if (seat_client && seat->keyboard_state.focused_surface) { - struct wl_resource *resource; - wl_resource_for_each(resource, &seat_client->primary_selection_devices) { - gtk_primary_selection_device_send_selection(resource, NULL); - } - } - - seat->primary_selection_source = NULL; - - wlr_signal_emit_safe(&seat->events.primary_selection, seat); -} - -void wlr_seat_set_primary_selection(struct wlr_seat *seat, - struct wlr_primary_selection_source *source, uint32_t serial) { - if (source) { - assert(source->send); - assert(source->cancel); - } - - if (seat->primary_selection_source && - seat->primary_selection_serial - serial < UINT32_MAX / 2) { - return; - } - - // TODO: make all offers inert - if (seat->primary_selection_source) { - wl_list_remove(&seat->primary_selection_source_destroy.link); - seat->primary_selection_source->cancel(seat->primary_selection_source); - seat->primary_selection_source = NULL; - } - - seat->primary_selection_source = source; - seat->primary_selection_serial = serial; - - struct wlr_seat_client *focused_client = - seat->keyboard_state.focused_client; - if (focused_client) { - wlr_seat_client_send_primary_selection(focused_client); - } - - wlr_signal_emit_safe(&seat->events.primary_selection, seat); - - if (source) { - seat->primary_selection_source_destroy.notify = - seat_client_primary_selection_source_destroy; - wl_signal_add(&source->events.destroy, - &seat->primary_selection_source_destroy); - } -} - - -static const struct gtk_primary_selection_device_interface device_impl; - -static struct wlr_seat_client *seat_client_from_device_resource( - struct wl_resource *resource) { - assert(wl_resource_instance_of(resource, - >k_primary_selection_device_interface, &device_impl)); - return wl_resource_get_user_data(resource); -} - -static void device_handle_set_selection(struct wl_client *client, - struct wl_resource *resource, struct wl_resource *source_resource, - uint32_t serial) { - struct client_data_source *source = NULL; - if (source_resource != NULL) { - source = client_data_source_from_resource(source_resource); - } - - struct wlr_seat_client *seat_client = - seat_client_from_device_resource(resource); - - struct wlr_primary_selection_source *wlr_source = - (struct wlr_primary_selection_source *)source; - wlr_seat_set_primary_selection(seat_client->seat, wlr_source, serial); -} - -static void device_handle_destroy(struct wl_client *client, - struct wl_resource *resource) { - wl_resource_destroy(resource); -} - -static const struct gtk_primary_selection_device_interface device_impl = { - .set_selection = device_handle_set_selection, - .destroy = device_handle_destroy, -}; - -static void device_resource_handle_destroy(struct wl_resource *resource) { - wl_list_remove(wl_resource_get_link(resource)); -} - - -void wlr_primary_selection_source_init( - struct wlr_primary_selection_source *source) { - wl_array_init(&source->mime_types); - wl_signal_init(&source->events.destroy); -} - -void wlr_primary_selection_source_finish( - struct wlr_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 = - calloc(1, sizeof(struct client_data_source)); - if (source == NULL) { - wl_client_post_no_memory(client); - return; - } - wlr_primary_selection_source_init(&source->source); - - int version = wl_resource_get_version(manager_resource); - source->resource = wl_resource_create(client, - >k_primary_selection_source_interface, version, id); - if (source->resource == NULL) { - free(source); - wl_client_post_no_memory(client); - return; - } - 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, - struct wl_resource *manager_resource, uint32_t id, - struct wl_resource *seat_resource) { - struct wlr_seat_client *seat_client = - wlr_seat_client_from_resource(seat_resource); - - uint32_t version = wl_resource_get_version(manager_resource); - struct wl_resource *resource = wl_resource_create(client, - >k_primary_selection_device_interface, version, id); - if (resource == NULL) { - wl_resource_post_no_memory(manager_resource); - return; - } - wl_resource_set_implementation(resource, &device_impl, seat_client, - &device_resource_handle_destroy); - wl_list_insert(&seat_client->primary_selection_devices, - wl_resource_get_link(resource)); -} - -static void device_manager_handle_destroy(struct wl_client *client, - struct wl_resource *manager_resource) { - wl_resource_destroy(manager_resource); -} - -static const struct gtk_primary_selection_device_manager_interface - device_manager_impl = { - .create_source = device_manager_handle_create_source, - .get_device = device_manager_handle_get_device, - .destroy = device_manager_handle_destroy, -}; - -static void device_manager_handle_resource_destroy( - struct wl_resource *resource) { - wl_list_remove(wl_resource_get_link(resource)); -} - - -static void primary_selection_device_manager_bind(struct wl_client *client, - void *data, uint32_t version, uint32_t id) { - struct wlr_primary_selection_device_manager *manager = data; - - struct wl_resource *resource = wl_resource_create(client, - >k_primary_selection_device_manager_interface, version, id); - if (resource == NULL) { - wl_client_post_no_memory(client); - return; - } - wl_resource_set_implementation(resource, &device_manager_impl, manager, - device_manager_handle_resource_destroy); - - wl_list_insert(&manager->resources, wl_resource_get_link(resource)); -} - -static void handle_display_destroy(struct wl_listener *listener, void *data) { - struct wlr_primary_selection_device_manager *manager = - wl_container_of(listener, manager, display_destroy); - wlr_primary_selection_device_manager_destroy(manager); -} - -struct wlr_primary_selection_device_manager * - wlr_primary_selection_device_manager_create( - struct wl_display *display) { - struct wlr_primary_selection_device_manager *manager = - calloc(1, sizeof(struct wlr_primary_selection_device_manager)); - if (manager == NULL) { - return NULL; - } - manager->global = wl_global_create(display, - >k_primary_selection_device_manager_interface, DEVICE_MANAGER_VERSION, - manager, primary_selection_device_manager_bind); - if (manager->global == NULL) { - free(manager); - return NULL; - } - - wl_list_init(&manager->resources); - wl_signal_init(&manager->events.destroy); - - manager->display_destroy.notify = handle_display_destroy; - wl_display_add_destroy_listener(display, &manager->display_destroy); - - return manager; -} - -void wlr_primary_selection_device_manager_destroy( - struct wlr_primary_selection_device_manager *manager) { - if (manager == NULL) { - return; - } - wlr_signal_emit_safe(&manager->events.destroy, manager); - wl_list_remove(&manager->display_destroy.link); - struct wl_resource *resource, *resource_tmp; - wl_resource_for_each_safe(resource, resource_tmp, &manager->resources) { - wl_resource_destroy(resource); - } - wl_global_destroy(manager->global); - free(manager); -} diff --git a/xwayland/selection/dnd.c b/xwayland/selection/dnd.c index 534d056c..ec5f16c7 100644 --- a/xwayland/selection/dnd.c +++ b/xwayland/selection/dnd.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include "xwayland/xwm.h" diff --git a/xwayland/selection/incoming.c b/xwayland/selection/incoming.c index 5b253b5d..d4d7d553 100644 --- a/xwayland/selection/incoming.c +++ b/xwayland/selection/incoming.c @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include "xwayland/xwm.h" @@ -217,21 +217,21 @@ static const struct wlr_data_source_impl data_source_impl = { }; struct x11_primary_selection_source { - struct wlr_primary_selection_source base; + struct wlr_gtk_primary_selection_source base; struct wlr_xwm_selection *selection; struct wl_array mime_types_atoms; }; static void primary_selection_source_cancel( - struct wlr_primary_selection_source *wlr_source); + struct wlr_gtk_primary_selection_source *wlr_source); bool primary_selection_source_is_xwayland( - struct wlr_primary_selection_source *wlr_source) { + struct wlr_gtk_primary_selection_source *wlr_source) { return wlr_source->cancel == primary_selection_source_cancel; } static void primary_selection_source_send( - struct wlr_primary_selection_source *wlr_source, const char *mime_type, + struct wlr_gtk_primary_selection_source *wlr_source, const char *mime_type, int32_t fd) { struct x11_primary_selection_source *source = (struct x11_primary_selection_source *)wlr_source; @@ -242,10 +242,10 @@ static void primary_selection_source_send( } static void primary_selection_source_cancel( - struct wlr_primary_selection_source *wlr_source) { + struct wlr_gtk_primary_selection_source *wlr_source) { struct x11_primary_selection_source *source = (struct x11_primary_selection_source *)wlr_source; - wlr_primary_selection_source_finish(&source->base); + wlr_gtk_primary_selection_source_finish(&source->base); wl_array_release(&source->mime_types_atoms); free(source); } @@ -356,7 +356,7 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { if (source == NULL) { return; } - wlr_primary_selection_source_init(&source->base); + wlr_gtk_primary_selection_source_init(&source->base); source->base.send = primary_selection_source_send; source->base.cancel = primary_selection_source_cancel; @@ -366,7 +366,7 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { bool ok = source_get_targets(selection, &source->base.mime_types, &source->mime_types_atoms); if (ok) { - wlr_seat_set_primary_selection(xwm->seat, &source->base, + wlr_seat_set_gtk_primary_selection(xwm->seat, &source->base, wl_display_next_serial(xwm->xwayland->wl_display)); } else { source->base.cancel(&source->base); @@ -424,7 +424,7 @@ int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, wlr_seat_set_selection(xwm->seat, NULL, wl_display_next_serial(xwm->xwayland->wl_display)); } else if (selection == &xwm->primary_selection) { - wlr_seat_set_primary_selection(xwm->seat, NULL, + wlr_seat_set_gtk_primary_selection(xwm->seat, NULL, wl_display_next_serial(xwm->xwayland->wl_display)); } else if (selection == &xwm->dnd_selection) { // TODO: DND diff --git a/xwayland/selection/outgoing.c b/xwayland/selection/outgoing.c index 0c095d25..b658ab52 100644 --- a/xwayland/selection/outgoing.c +++ b/xwayland/selection/outgoing.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include "xwayland/xwm.h" @@ -195,7 +195,7 @@ static void xwm_selection_source_send(struct wlr_xwm_selection *selection, return; } } else if (selection == &selection->xwm->primary_selection) { - struct wlr_primary_selection_source *source = + struct wlr_gtk_primary_selection_source *source = selection->xwm->seat->primary_selection_source; if (source != NULL) { source->send(source, mime_type, fd); @@ -231,7 +231,7 @@ static struct wl_array *xwm_selection_source_get_mime_types( return &source->mime_types; } } else if (selection == &selection->xwm->primary_selection) { - struct wlr_primary_selection_source *source = + struct wlr_gtk_primary_selection_source *source = selection->xwm->seat->primary_selection_source; if (source != NULL) { return &source->mime_types; diff --git a/xwayland/selection/selection.c b/xwayland/selection/selection.c index d9de5643..a29eeeae 100644 --- a/xwayland/selection/selection.c +++ b/xwayland/selection/selection.c @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include "xwayland/xwm.h" @@ -231,7 +231,7 @@ void xwm_selection_finish(struct wlr_xwm *xwm) { if (xwm->seat->primary_selection_source && primary_selection_source_is_xwayland( xwm->seat->primary_selection_source)) { - wlr_seat_set_primary_selection(xwm->seat, NULL, + wlr_seat_set_gtk_primary_selection(xwm->seat, NULL, wl_display_next_serial(xwm->xwayland->wl_display)); } wlr_xwayland_set_seat(xwm->xwayland, NULL); @@ -274,7 +274,7 @@ static void seat_handle_primary_selection(struct wl_listener *listener, struct wlr_seat *seat = data; struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_primary_selection); - struct wlr_primary_selection_source *source = seat->primary_selection_source; + struct wlr_gtk_primary_selection_source *source = seat->primary_selection_source; if (source != NULL && primary_selection_source_is_xwayland( -- cgit v1.2.3