diff --git a/include/swappy.h b/include/swappy.h index c0e36c7..1ba025c 100644 --- a/include/swappy.h +++ b/include/swappy.h @@ -10,6 +10,7 @@ #include #endif +#include "wlr-data-control-unstable-v1-client-protocol.h" #include "wlr-screencopy-unstable-v1-client-protocol.h" #define MAX_PATH 4096 @@ -132,6 +133,12 @@ struct swappy_buffer { enum wl_shm_format format; }; +struct swappy_wl_seat { + struct swappy_state *state; + struct wl_seat *wl_seat; + struct wl_list link; +}; + struct swappy_output { struct swappy_state *state; struct swappy_box geometry; @@ -159,7 +166,9 @@ struct swappy_wayland { struct wl_compositor *compositor; struct wl_shm *shm; struct wl_list outputs; + struct wl_list seats; struct zwlr_screencopy_manager_v1 *zwlr_screencopy_manager; + struct zwlr_data_control_manager_v1 *zwlr_data_control_manager; size_t n_done; #ifdef HAVE_WAYLAND_PROTOCOLS struct zxdg_output_manager_v1 *xdg_output_manager; diff --git a/protocol/meson.build b/protocol/meson.build index a3bd96a..66c1e91 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -24,10 +24,12 @@ if wayland_protos.found() client_protocols = [ [wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'], [wl_protocol_dir, 'unstable/xdg-output/xdg-output-unstable-v1.xml'], + ['wlr-data-control-unstable-v1.xml'], ['wlr-screencopy-unstable-v1.xml'], ] else client_protocols = [ + ['wlr-data-control-unstable-v1.xml'], ['wlr-screencopy-unstable-v1.xml'], ] endif diff --git a/protocol/wlr-data-control-unstable-v1.xml b/protocol/wlr-data-control-unstable-v1.xml new file mode 100644 index 0000000..75e8671 --- /dev/null +++ b/protocol/wlr-data-control-unstable-v1.xml @@ -0,0 +1,278 @@ + + + + Copyright © 2018 Simon Ser + Copyright © 2019 Ivan Molodetskikh + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + + + + This protocol allows a privileged client to control data devices. In + particular, the client will be able to manage the current selection and take + the role of a clipboard manager. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + + + + + This interface is a manager that allows creating per-seat data device + controls. + + + + + Create a new data source. + + + + + + + Create a data device that can be used to manage a seat's selection. + + + + + + + + All objects created by the manager will still remain valid, until their + appropriate destroy request has been called. + + + + + + + This interface allows a client to manage a seat's selection. + + When the seat is destroyed, this object becomes inert. + + + + + This request asks the compositor to set the selection to the data from + the source on behalf of the client. + + The given source may not be used in any further set_selection or + set_primary_selection requests. Attempting to use a previously used + source is a protocol error. + + To unset the selection, set the source to NULL. + + + + + + + Destroys the data device object. + + + + + + The data_offer event introduces a new wlr_data_control_offer object, + which will subsequently be used in either the + wlr_data_control_device.selection event (for the regular clipboard + selections) or the wlr_data_control_device.primary_selection event (for + the primary clipboard selections). Immediately following the + wlr_data_control_device.data_offer event, the new data_offer object + will send out wlr_data_control_offer.offer events to describe the MIME + types it offers. + + + + + + + The selection event is sent out to notify the client of a new + wlr_data_control_offer for the selection for this device. The + wlr_data_control_device.data_offer and the wlr_data_control_offer.offer + events are sent out immediately before this event to introduce the data + offer object. The selection event is sent to a client when a new + selection is set. The wlr_data_control_offer is valid until a new + wlr_data_control_offer or NULL is received. The client must destroy the + previous selection wlr_data_control_offer, if any, upon receiving this + event. + + The first selection event is sent upon binding the + wlr_data_control_device object. + + + + + + + This data control object is no longer valid and should be destroyed by + the client. + + + + + + + + The primary_selection event is sent out to notify the client of a new + wlr_data_control_offer for the primary selection for this device. The + wlr_data_control_device.data_offer and the wlr_data_control_offer.offer + events are sent out immediately before this event to introduce the data + offer object. The primary_selection event is sent to a client when a + new primary selection is set. The wlr_data_control_offer is valid until + a new wlr_data_control_offer or NULL is received. The client must + destroy the previous primary selection wlr_data_control_offer, if any, + upon receiving this event. + + If the compositor supports primary selection, the first + primary_selection event is sent upon binding the + wlr_data_control_device object. + + + + + + + This request asks the compositor to set the primary selection to the + data from the source on behalf of the client. + + The given source may not be used in any further set_selection or + set_primary_selection requests. Attempting to use a previously used + source is a protocol error. + + To unset the primary selection, set the source to NULL. + + The compositor will ignore this request if it does not support primary + selection. + + + + + + + + + + + + The wlr_data_control_source object is the source side of a + wlr_data_control_offer. It is created by the source client in a data + transfer and provides a way to describe the offered data and a way to + respond to requests to transfer the data. + + + + + + + + + This request adds a MIME type to the set of MIME types advertised to + targets. Can be called several times to offer multiple types. + + Calling this after wlr_data_control_device.set_selection is a protocol + error. + + + + + + + Destroys the data source object. + + + + + + Request for data from the client. Send the data as the specified MIME + type over the passed file descriptor, then close it. + + + + + + + + This data source is no longer valid. The data source has been replaced + by another data source. + + The client should clean up and destroy this data source. + + + + + + + A wlr_data_control_offer represents a piece of data offered for transfer + by another client (the source client). The offer describes the different + MIME types that the data can be converted to and provides the mechanism + for transferring the data directly from the source client. + + + + + To transfer the offered data, the client issues this request and + indicates the MIME type it wants to receive. The transfer happens + through the passed file descriptor (typically created with the pipe + system call). The source client writes the data in the MIME type + representation requested and then closes the file descriptor. + + The receiving client reads from the read end of the pipe until EOF and + then closes its end, at which point the transfer is complete. + + This request may happen multiple times for different MIME types. + + + + + + + + Destroys the data offer object. + + + + + + Sent immediately after creating the wlr_data_control_offer object. + One event per offered MIME type. + + + + + diff --git a/src/clipboard.c b/src/clipboard.c index 118b4b8..5d1ac20 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -1,17 +1,70 @@ #include "clipboard.h" #include "notification.h" +#include "wlr-data-control-unstable-v1-client-protocol.h" -bool clipboard_copy_drawing_area_to_selection(struct swappy_state *state) { +#define gdk_pixbuf_t GdkPixbuf + +static void data_control_data_offer( + void *data, struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1, + struct zwlr_data_control_offer_v1 *id) { + g_debug("data control data offer"); +} + +static void data_control_selection( + void *data, struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1, + struct zwlr_data_control_offer_v1 *id) { + g_debug("data control selection"); +} + +static void data_control_primary_selection( + void *data, struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1, + struct zwlr_data_control_offer_v1 *id) { + g_debug("data control primary selection"); +} + +static void data_control_finished( + void *data, + struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1) { + g_debug("data control_finished"); +} + +static void send_pixbuf_to_gdk_clipboard(gdk_pixbuf_t *pixbuf) { GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); - int width = gtk_widget_get_allocated_width(state->ui->area); - int height = gtk_widget_get_allocated_height(state->ui->area); - GdkPixbuf *pixbuf = - gdk_pixbuf_get_from_surface(state->cairo_surface, 0, 0, width, height); gtk_clipboard_set_image(clipboard, pixbuf); - gtk_clipboard_store(clipboard); + gtk_clipboard_store(clipboard); // Does not work for Wayland gdk backend +} +struct zwlr_data_control_device_v1_listener listener = { + .data_offer = data_control_data_offer, + .selection = data_control_selection, + .primary_selection = data_control_primary_selection, + .finished = data_control_finished, +}; + +void send_pixbuf_to_data_control_clipboard(struct swappy_state *state, + gdk_pixbuf_t *pixbuf) { + struct zwlr_data_control_manager_v1 *zwlr_data_control_manager_v1 = + state->wl->zwlr_data_control_manager; + + struct swappy_wl_seat *seat; + wl_list_for_each(seat, &state->wl->seats, link) { + struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1 = + zwlr_data_control_manager_v1_get_data_device( + zwlr_data_control_manager_v1, seat->wl_seat); + + zwlr_data_control_device_v1_add_listener(zwlr_data_control_device_v1, + &listener, pixbuf); + } +} + +bool clipboard_copy_drawing_area_to_selection(struct swappy_state *state) { + int width = gtk_widget_get_allocated_width(state->ui->area); + int height = gtk_widget_get_allocated_height(state->ui->area); + gdk_pixbuf_t *pixbuf = + gdk_pixbuf_get_from_surface(state->cairo_surface, 0, 0, width, height); + send_pixbuf_to_gdk_clipboard(pixbuf); char message[MAX_PATH]; snprintf(message, MAX_PATH, "Swappshot copied to clipboard\n"); notification_send("Swappy", message); diff --git a/src/wayland.c b/src/wayland.c index 3890354..3d8752d 100644 --- a/src/wayland.c +++ b/src/wayland.c @@ -6,6 +6,7 @@ #include "buffer.h" #include "swappy.h" +#include "wlr-data-control-unstable-v1-client-protocol.h" #include "wlr-screencopy-unstable-v1-client-protocol.h" #ifdef HAVE_WAYLAND_PROTOCOLS @@ -76,6 +77,13 @@ static const struct zxdg_output_v1_listener xdg_output_listener = { }; #endif +static void seat_capabilities(void *data, struct wl_seat *wl_seat, + uint32_t capabilities) {} + +static void seat_name(void *data, struct wl_seat *wl_seat, const char *name) { + g_debug("received seat name: %s", name); +} + static void output_handle_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, @@ -116,10 +124,16 @@ static const struct wl_output_listener output_listener = { .scale = output_handle_scale, }; +static const struct wl_seat_listener seat_listener = { + .capabilities = seat_capabilities, + .name = seat_name, +}; + static void global_registry_handler(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { - g_debug("got a registry event for interface: %s, name: %d", interface, name); + g_debug("got a registry event for interface: %s, name: %d, version: %d", + interface, name, version); struct swappy_state *state = data; bool bound = false; @@ -141,11 +155,24 @@ static void global_registry_handler(void *data, struct wl_registry *registry, wl_output_add_listener(output->wl_output, &output_listener, output); wl_list_insert(&state->wl->outputs, &output->link); bound = true; + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + struct swappy_wl_seat *seat = calloc(1, sizeof(struct swappy_wl_seat)); + seat->state = state; + seat->wl_seat = + wl_registry_bind(registry, name, &wl_seat_interface, version); + wl_seat_add_listener(seat->wl_seat, &seat_listener, seat); + wl_list_insert(&state->wl->seats, &seat->link); + bound = true; } else if (strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) { state->wl->zwlr_screencopy_manager = wl_registry_bind( registry, name, &zwlr_screencopy_manager_v1_interface, version); bound = true; + } else if (strcmp(interface, zwlr_data_control_manager_v1_interface.name) == + 0) { + state->wl->zwlr_data_control_manager = wl_registry_bind( + registry, name, &zwlr_data_control_manager_v1_interface, version); + bound = true; } else { #ifdef HAVE_WAYLAND_PROTOCOLS if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) { @@ -181,6 +208,7 @@ bool wayland_init(struct swappy_state *state) { } wl_list_init(&state->wl->outputs); + wl_list_init(&state->wl->seats); state->wl->registry = wl_display_get_registry(state->wl->display); wl_registry_add_listener(state->wl->registry, ®istry_listener, state); wl_display_roundtrip(state->wl->display); @@ -199,6 +227,11 @@ bool wayland_init(struct swappy_state *state) { return false; } + if (wl_list_empty(&state->wl->seats)) { + g_warning("no wl_seats found"); + return false; + } + bool found_output_layout = false; #ifdef HAVE_WAYLAND_PROTOCOLS if (state->wl->xdg_output_manager != NULL) { @@ -231,8 +264,13 @@ bool wayland_init(struct swappy_state *state) { } if (state->wl->zwlr_screencopy_manager == NULL) { - g_warning("compositor does not support zwlr_screencopy_v1"); - return false; + g_warning( + "compositor does not support zwlr_screencopy_v1, -g option will not " + "work"); + } else if (state->wl->zwlr_data_control_manager == NULL) { + g_warning( + "compositor does not support zwlr_data_control_v1, copy will fallback " + "to gdk clipboard"); } return true; @@ -241,6 +279,8 @@ bool wayland_init(struct swappy_state *state) { void wayland_finish(struct swappy_state *state) { struct swappy_output *output; struct swappy_output *output_tmp; + struct swappy_wl_seat *seat; + struct swappy_wl_seat *seat_tmp; if (!state->wl) { return; @@ -262,6 +302,12 @@ void wayland_finish(struct swappy_state *state) { free(output); } + wl_list_for_each_safe(seat, seat_tmp, &state->wl->seats, link) { + wl_list_remove(&seat->link); + wl_seat_release(seat->wl_seat); + free(seat); + } + if (state->wl->compositor != NULL) { wl_compositor_destroy(state->wl->compositor); } @@ -270,6 +316,10 @@ void wayland_finish(struct swappy_state *state) { zwlr_screencopy_manager_v1_destroy(state->wl->zwlr_screencopy_manager); } + if (state->wl->zwlr_data_control_manager != NULL) { + zwlr_data_control_manager_v1_destroy(state->wl->zwlr_data_control_manager); + } + #ifdef HAVE_WAYLAND_PROTOCOLS if (state->wl->xdg_output_manager != NULL) { zxdg_output_manager_v1_destroy(state->wl->xdg_output_manager);