diff options
-rw-r--r-- | backend/drm/drm.c | 7 | ||||
-rw-r--r-- | backend/session/direct-ipc.c | 3 | ||||
-rw-r--r-- | backend/session/logind.c | 5 | ||||
-rw-r--r-- | backend/session/session.c | 5 | ||||
-rw-r--r-- | examples/support/config.c | 2 | ||||
-rw-r--r-- | include/rootston/desktop.h | 3 | ||||
-rw-r--r-- | include/rootston/server.h | 3 | ||||
-rw-r--r-- | include/rootston/view.h | 5 | ||||
-rw-r--r-- | include/wlr/types/wlr_data_device.h | 5 | ||||
-rw-r--r-- | include/wlr/types/wlr_output.h | 15 | ||||
-rw-r--r-- | include/wlr/types/wlr_primary_selection.h | 7 | ||||
-rw-r--r-- | include/wlr/xwayland.h | 3 | ||||
-rw-r--r-- | include/wlr/xwm.h | 37 | ||||
-rw-r--r-- | meson.build | 12 | ||||
-rw-r--r-- | rootston/config.c | 1 | ||||
-rw-r--r-- | rootston/desktop.c | 3 | ||||
-rw-r--r-- | rootston/keyboard.c | 2 | ||||
-rw-r--r-- | rootston/main.c | 3 | ||||
-rw-r--r-- | rootston/seat.c | 1 | ||||
-rw-r--r-- | rootston/xwayland.c | 3 | ||||
-rw-r--r-- | types/wlr_data_device.c | 44 | ||||
-rw-r--r-- | types/wlr_output.c | 21 | ||||
-rw-r--r-- | types/wlr_primary_selection.c | 35 | ||||
-rw-r--r-- | types/wlr_xdg_shell_v6.c | 1 | ||||
-rw-r--r-- | xwayland/selection.c | 705 | ||||
-rw-r--r-- | xwayland/xwm.c | 8 |
26 files changed, 600 insertions, 339 deletions
diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 06f012a3..dd5c97f5 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -543,10 +543,11 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, // OpenGL will read the pixels out upside down, // so we need to flip the image vertically + enum wl_output_transform transform = wlr_output_transform_compose( + wlr_output_transform_invert(output->transform), + WL_OUTPUT_TRANSFORM_FLIPPED_180); wlr_matrix_texture(plane->matrix, plane->surf.width, plane->surf.height, - conn->output.transform ^ WL_OUTPUT_TRANSFORM_FLIPPED_180); - - // TODO the image needs to be rotated depending on the output rotation + transform); plane->wlr_tex = wlr_render_texture_create(plane->surf.renderer->wlr_rend); diff --git a/backend/session/direct-ipc.c b/backend/session/direct-ipc.c index 0ea51e2f..52cdbbb9 100644 --- a/backend/session/direct-ipc.c +++ b/backend/session/direct-ipc.c @@ -18,12 +18,13 @@ #include <linux/major.h> #endif #include <xf86drm.h> +#include "config.h" #include <wlr/util/log.h> #include "backend/session/direct-ipc.h" enum { DRM_MAJOR = 226 }; -#ifdef HAS_LIBCAP +#ifdef WLR_HAS_LIBCAP #include <sys/capability.h> static bool have_permissions(void) { diff --git a/backend/session/logind.c b/backend/session/logind.c index e9d4c8f3..539490de 100644 --- a/backend/session/logind.c +++ b/backend/session/logind.c @@ -10,13 +10,14 @@ #include <sys/stat.h> #include <fcntl.h> #include <wayland-server.h> +#include "config.h" #include <wlr/backend/session/interface.h> #include <wlr/util/log.h> -#ifdef HAS_SYSTEMD +#ifdef WLR_HAS_SYSTEMD #include <systemd/sd-bus.h> #include <systemd/sd-login.h> -#elif HAS_ELOGIND +#elif WLR_HAS_ELOGIND #include <elogind/sd-bus.h> #include <elogind/sd-login.h> #endif diff --git a/backend/session/session.c b/backend/session/session.c index 0ed54499..725aa1b9 100644 --- a/backend/session/session.c +++ b/backend/session/session.c @@ -9,6 +9,7 @@ #include <wayland-server.h> #include <xf86drm.h> #include <xf86drmMode.h> +#include "config.h" #include <wlr/backend/session.h> #include <wlr/backend/session/interface.h> #include <wlr/util/log.h> @@ -17,9 +18,9 @@ extern const struct session_impl session_logind; extern const struct session_impl session_direct; static const struct session_impl *impls[] = { -#ifdef HAS_SYSTEMD +#ifdef WLR_HAS_SYSTEMD &session_logind, -#elif HAS_ELOGIND +#elif WLR_HAS_ELOGIND &session_logind, #endif &session_direct, diff --git a/examples/support/config.c b/examples/support/config.c index 0ef025c0..ff63b078 100644 --- a/examples/support/config.c +++ b/examples/support/config.c @@ -7,10 +7,10 @@ #include <string.h> #include <unistd.h> #include <sys/param.h> +#include "config.h" #include <wlr/util/log.h> #include <wlr/types/wlr_box.h> #include "shared.h" -#include "config.h" #include "ini.h" static void usage(const char *name, int ret) { diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index 289875c5..7039cfcf 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -2,6 +2,7 @@ #define _ROOTSTON_DESKTOP_H #include <time.h> #include <wayland-server.h> +#include "config.h" #include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_compositor.h> @@ -50,7 +51,7 @@ struct roots_desktop { struct wl_listener wl_shell_surface; struct wl_listener decoration_new; -#ifdef HAS_XWAYLAND +#ifdef WLR_HAS_XWAYLAND struct wlr_xwayland *xwayland; struct wl_listener xwayland_surface; struct wl_listener xwayland_ready; diff --git a/include/rootston/server.h b/include/rootston/server.h index 8fc6530e..a014c749 100644 --- a/include/rootston/server.h +++ b/include/rootston/server.h @@ -1,11 +1,12 @@ #ifndef _ROOTSTON_SERVER_H #define _ROOTSTON_SERVER_H #include <wayland-server.h> +#include "config.h" #include <wlr/backend.h> #include <wlr/backend/session.h> #include <wlr/types/wlr_data_device.h> #include <wlr/render.h> -#ifdef HAS_XWAYLAND +#ifdef WLR_HAS_XWAYLAND #include <wlr/xwayland.h> #endif #include "rootston/config.h" diff --git a/include/rootston/view.h b/include/rootston/view.h index bb7297d0..53e96764 100644 --- a/include/rootston/view.h +++ b/include/rootston/view.h @@ -2,6 +2,7 @@ #define _ROOTSTON_VIEW_H #include <stdbool.h> +#include "config.h" #include <wlr/types/wlr_box.h> #include <wlr/types/wlr_surface.h> #include <wlr/types/wlr_xdg_shell_v6.h> @@ -79,14 +80,14 @@ struct roots_view { union { struct wlr_wl_shell_surface *wl_shell_surface; struct wlr_xdg_surface_v6 *xdg_surface_v6; -#ifdef HAS_XWAYLAND +#ifdef WLR_HAS_XWAYLAND struct wlr_xwayland_surface *xwayland_surface; #endif }; union { struct roots_wl_shell_surface *roots_wl_shell_surface; struct roots_xdg_surface_v6 *roots_xdg_surface_v6; -#ifdef HAS_XWAYLAND +#ifdef WLR_HAS_XWAYLAND struct roots_xwayland_surface *roots_xwayland_surface; #endif }; diff --git a/include/wlr/types/wlr_data_device.h b/include/wlr/types/wlr_data_device.h index e0205101..92d15972 100644 --- a/include/wlr/types/wlr_data_device.h +++ b/include/wlr/types/wlr_data_device.h @@ -34,6 +34,7 @@ struct wlr_data_source { struct wl_resource *resource; struct wlr_data_offer *offer; struct wlr_seat_client *seat_client; + struct wl_array mime_types; bool accepted; @@ -122,4 +123,8 @@ void wlr_seat_client_send_selection(struct wlr_seat_client *seat_client); void wlr_seat_set_selection(struct wlr_seat *seat, struct wlr_data_source *source, uint32_t serial); +void wlr_data_source_init(struct wlr_data_source *source); + +void wlr_data_source_finish(struct wlr_data_source *source); + #endif diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index a974a154..8e4f5446 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -118,6 +118,19 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, double x, double y); void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor); -enum wl_output_transform wlr_output_transform_invert(enum wl_output_transform); + +/** + * Returns the transform that, when composed with `tr`, gives + * `WL_OUTPUT_TRANSFORM_NORMAL`. + */ +enum wl_output_transform wlr_output_transform_invert( + enum wl_output_transform tr); + +/** + * Returns a transform that, when applied, has the same effect as applying + * sequentially `tr_a` and `tr_b`. + */ +enum wl_output_transform wlr_output_transform_compose( + enum wl_output_transform tr_a, enum wl_output_transform tr_b); #endif diff --git a/include/wlr/types/wlr_primary_selection.h b/include/wlr/types/wlr_primary_selection.h index a639b913..b4eceb78 100644 --- a/include/wlr/types/wlr_primary_selection.h +++ b/include/wlr/types/wlr_primary_selection.h @@ -48,6 +48,11 @@ void wlr_primary_selection_device_manager_destroy( 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); + 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/xwayland.h b/include/wlr/xwayland.h index 220b831c..37b09204 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -3,11 +3,12 @@ #include <time.h> #include <stdbool.h> +#include "config.h" #include <wlr/types/wlr_compositor.h> #include <wlr/types/wlr_seat.h> #include <xcb/xcb.h> -#ifdef HAS_XCB_ICCCM +#ifdef WLR_HAS_XCB_ICCCM #include <xcb/xcb_icccm.h> #endif diff --git a/include/wlr/xwm.h b/include/wlr/xwm.h index 47e674c0..cbc2f132 100644 --- a/include/wlr/xwm.h +++ b/include/wlr/xwm.h @@ -32,6 +32,7 @@ enum atom_name { _NET_WM_STATE_MAXIMIZED_HORZ, WM_STATE, CLIPBOARD, + PRIMARY, WL_SELECTION, TARGETS, CLIPBOARD_MANAGER, @@ -49,6 +50,24 @@ enum net_wm_state_action { NET_WM_STATE_TOGGLE = 2, }; +struct wlr_xwm_selection { + struct wlr_xwm *xwm; + xcb_atom_t atom; + xcb_window_t window; + xcb_selection_request_event_t request; + xcb_window_t owner; + xcb_timestamp_t timestamp; + int incr; + int source_fd; + int property_start; + xcb_get_property_reply_t *property_reply; + struct wl_event_source *property_source; + int flush_property_on_delete; + struct wl_array source_data; + xcb_atom_t target; + bool property_set; +}; + struct wlr_xwm { struct wlr_xwayland *xwayland; struct wl_event_source *event_source; @@ -63,20 +82,9 @@ struct wlr_xwm { xcb_render_pictformat_t render_format_id; xcb_cursor_t cursor; - // selection properties xcb_window_t selection_window; - xcb_selection_request_event_t selection_request; - xcb_window_t selection_owner; - xcb_timestamp_t selection_timestamp; - int incr; - int data_source_fd; - int property_start; - xcb_get_property_reply_t *property_reply; - struct wl_event_source *property_source; - int flush_property_on_delete; - struct wl_array source_data; - xcb_atom_t selection_target; - bool selection_property_set; + struct wlr_xwm_selection clipboard_selection; + struct wlr_xwm_selection primary_selection; struct wlr_xwayland_surface *focus_surface; @@ -86,7 +94,8 @@ struct wlr_xwm { const xcb_query_extension_reply_t *xfixes; struct wl_listener compositor_surface_create; - struct wl_listener seat_selection_change; + struct wl_listener seat_selection; + struct wl_listener seat_primary_selection; }; struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland); diff --git a/meson.build b/meson.build index a3f5e7f9..51de7a4c 100644 --- a/meson.build +++ b/meson.build @@ -24,6 +24,8 @@ add_project_link_arguments( language: 'c', ) +conf_data = configuration_data() + wlr_inc = include_directories('include') cc = meson.get_compiler('c') @@ -63,26 +65,24 @@ elogind = dependency('libelogind', required: false) math = cc.find_library('m', required: false) if xcb_icccm.found() - add_project_arguments('-DHAS_XCB_ICCCM', language: 'c') + conf_data.set('WLR_HAS_XCB_ICCCM', true) endif if libcap.found() and get_option('enable_libcap') - add_project_arguments('-DHAS_LIBCAP', language: 'c') + conf_data.set('WLR_HAS_LIBCAP', true) endif if systemd.found() and get_option('enable_systemd') - add_project_arguments('-DHAS_SYSTEMD', language: 'c') + conf_data.set('WLR_HAS_SYSTEMD', true) endif if elogind.found() and get_option('enable_elogind') - add_project_arguments('-DHAS_ELOGIND', language: 'c') + conf_data.set('WLR_HAS_ELOGIND', true) endif exclude_files = [] wlr_parts = [] -conf_data = configuration_data() if get_option('enable_xwayland') - add_project_arguments('-DHAS_XWAYLAND', language: 'c') subdir('xwayland') wlr_parts += [lib_wlr_xwayland] conf_data.set('WLR_HAS_XWAYLAND', true) diff --git a/rootston/config.c b/rootston/config.c index dd741186..f369c4f5 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -9,6 +9,7 @@ #include <strings.h> #include <unistd.h> #include <sys/param.h> +#include "config.h" #include <wlr/util/log.h> #include <wlr/types/wlr_box.h> #include "rootston/config.h" diff --git a/rootston/desktop.c b/rootston/desktop.c index 1431dc5d..a4692439 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -3,6 +3,7 @@ #include <time.h> #include <stdlib.h> #include <math.h> +#include "config.h" #include <wlr/types/wlr_box.h> #include <wlr/types/wlr_compositor.h> #include <wlr/types/wlr_cursor.h> @@ -440,7 +441,7 @@ struct roots_desktop *desktop_create(struct roots_server *server, &desktop->wl_shell_surface); desktop->wl_shell_surface.notify = handle_wl_shell_surface; -#ifdef HAS_XWAYLAND +#ifdef WLR_HAS_XWAYLAND if (config->xwayland) { desktop->xwayland = wlr_xwayland_create(server->wl_display, desktop->compositor); diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 16071c0c..7b281308 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -117,6 +117,8 @@ static void keyboard_binding_execute(struct roots_keyboard *keyboard, if (focus != NULL) { view_maximize(focus, !focus->maximized); } + } else if (strcmp(command, "nop") == 0) { + wlr_log(L_DEBUG, "nop command"); } else { wlr_log(L_ERROR, "unknown binding command: %s", command); } diff --git a/rootston/main.c b/rootston/main.c index 161824d6..87f1c0b4 100644 --- a/rootston/main.c +++ b/rootston/main.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <unistd.h> #include <wayland-server.h> +#include "config.h" #include <wlr/backend.h> #include <wlr/backend/headless.h> #include <wlr/backend/multi.h> @@ -64,7 +65,7 @@ int main(int argc, char **argv) { } setenv("WAYLAND_DISPLAY", socket, true); -#ifndef HAS_XWAYLAND +#ifndef WLR_HAS_XWAYLAND ready(NULL, NULL); #else if (server.desktop->xwayland != NULL) { diff --git a/rootston/seat.c b/rootston/seat.c index ce0f1374..635bc8b1 100644 --- a/rootston/seat.c +++ b/rootston/seat.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <string.h> #include <wayland-server.h> +#include "config.h" #include <wlr/types/wlr_xcursor_manager.h> #include <wlr/util/log.h> #include "rootston/xcursor.h" diff --git a/rootston/xwayland.c b/rootston/xwayland.c index 5f677116..b00b61bc 100644 --- a/rootston/xwayland.c +++ b/rootston/xwayland.c @@ -2,11 +2,12 @@ #include <stdlib.h> #include <stdbool.h> #include <wayland-server.h> +#include "config.h" #include <wlr/types/wlr_box.h> #include <wlr/types/wlr_surface.h> #include <wlr/xwayland.h> #include <wlr/util/log.h> -#include "rootston/desktop.h" +#include "rootston/server.h" #include "rootston/server.h" static void activate(struct roots_view *view, bool active) { diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index 376c8304..af81d861 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -855,16 +855,7 @@ void data_device_manager_get_data_device(struct wl_client *client, static void data_source_resource_destroy(struct wl_resource *resource) { struct wlr_data_source *source = wl_resource_get_user_data(resource); - char **p; - - wl_signal_emit(&source->events.destroy, source); - - wl_array_for_each(p, &source->mime_types) { - free(*p); - } - - wl_array_release(&source->mime_types); - + wlr_data_source_finish(source); free(source); } @@ -926,6 +917,25 @@ static struct wl_data_source_interface data_source_impl = { .set_actions = data_source_set_actions, }; +void wlr_data_source_init(struct wlr_data_source *source) { + wl_array_init(&source->mime_types); + wl_signal_init(&source->events.destroy); +} + +void wlr_data_source_finish(struct wlr_data_source *source) { + if (source == NULL) { + return; + } + + wl_signal_emit(&source->events.destroy, source); + + char **p; + wl_array_for_each(p, &source->mime_types) { + free(*p); + } + wl_array_release(&source->mime_types); +} + static void data_device_manager_create_data_source(struct wl_client *client, struct wl_resource *resource, uint32_t id) { struct wlr_data_source *source = calloc(1, sizeof(struct wlr_data_source)); @@ -933,25 +943,21 @@ static void data_device_manager_create_data_source(struct wl_client *client, wl_resource_post_no_memory(resource); return; } + wlr_data_source_init(source); - source->resource = - wl_resource_create(client, &wl_data_source_interface, - wl_resource_get_version(resource), id); + source->resource = wl_resource_create(client, &wl_data_source_interface, + wl_resource_get_version(resource), id); if (source->resource == NULL) { free(source); wl_resource_post_no_memory(resource); return; } + wl_resource_set_implementation(source->resource, &data_source_impl, + source, data_source_resource_destroy); source->accept = client_data_source_accept; source->send = client_data_source_send; source->cancel = client_data_source_cancel; - - wl_array_init(&source->mime_types); - wl_signal_init(&source->events.destroy); - - wl_resource_set_implementation(source->resource, &data_source_impl, - source, data_source_resource_destroy); } static const struct wl_data_device_manager_interface diff --git a/types/wlr_output.c b/types/wlr_output.c index f319905c..115f3e8b 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -726,11 +726,22 @@ void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) { free(cursor); } + enum wl_output_transform wlr_output_transform_invert( - enum wl_output_transform transform) { - if ((transform & WL_OUTPUT_TRANSFORM_90) && - !(transform & WL_OUTPUT_TRANSFORM_FLIPPED)) { - transform ^= WL_OUTPUT_TRANSFORM_180; + enum wl_output_transform tr) { + if ((tr & WL_OUTPUT_TRANSFORM_90) && !(tr & WL_OUTPUT_TRANSFORM_FLIPPED)) { + tr ^= WL_OUTPUT_TRANSFORM_180; + } + return tr; +} + +enum wl_output_transform wlr_output_transform_compose( + enum wl_output_transform tr_a, enum wl_output_transform tr_b) { + uint32_t flipped = (tr_a ^ tr_b) & WL_OUTPUT_TRANSFORM_FLIPPED; + uint32_t rotated = + (tr_a + tr_b) & (WL_OUTPUT_TRANSFORM_90 | WL_OUTPUT_TRANSFORM_180); + if ((tr_a & WL_OUTPUT_TRANSFORM_FLIPPED) && (tr_b & WL_OUTPUT_TRANSFORM_FLIPPED)) { + rotated = wlr_output_transform_invert(rotated); } - return transform; + return flipped | rotated; } diff --git a/types/wlr_primary_selection.c b/types/wlr_primary_selection.c index 63bdeceb..8163d2e5 100644 --- a/types/wlr_primary_selection.c +++ b/types/wlr_primary_selection.c @@ -144,15 +144,7 @@ static const struct gtk_primary_selection_source_interface source_impl = { static void source_resource_handle_destroy(struct wl_resource *resource) { struct wlr_primary_selection_source *source = wl_resource_get_user_data(resource); - - wl_signal_emit(&source->events.destroy, source); - - char **p; - wl_array_for_each(p, &source->mime_types) { - free(*p); - } - wl_array_release(&source->mime_types); - + wlr_primary_selection_source_finish(source); free(source); } @@ -262,6 +254,27 @@ static void device_resource_handle_destroy(struct wl_resource *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; + } + + wl_signal_emit(&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 wlr_primary_selection_source *source = @@ -270,6 +283,7 @@ static void device_manager_handle_create_source(struct wl_client *client, wl_client_post_no_memory(client); return; } + wlr_primary_selection_source_init(source); int version = wl_resource_get_version(manager_resource); source->resource = wl_resource_create(client, @@ -284,9 +298,6 @@ static void device_manager_handle_create_source(struct wl_client *client, source->send = client_source_send; source->cancel = client_source_cancel; - - wl_array_init(&source->mime_types); - wl_signal_init(&source->events.destroy); } void device_manager_handle_get_device(struct wl_client *client, diff --git a/types/wlr_xdg_shell_v6.c b/types/wlr_xdg_shell_v6.c index bb6c171c..09675a7b 100644 --- a/types/wlr_xdg_shell_v6.c +++ b/types/wlr_xdg_shell_v6.c @@ -5,6 +5,7 @@ #include <stdlib.h> #include <string.h> #include <wayland-server.h> +#include "config.h" #include <wlr/types/wlr_xdg_shell_v6.h> #include <wlr/types/wlr_surface.h> #include <wlr/types/wlr_seat.h> diff --git a/xwayland/selection.c b/xwayland/selection.c index 25386b1a..0d7f1588 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -7,131 +7,133 @@ #include <fcntl.h> #include "wlr/util/log.h" #include "wlr/types/wlr_data_device.h" +#include "wlr/types/wlr_primary_selection.h" #include "wlr/xwm.h" static const size_t incr_chunk_size = 64 * 1024; -static void xwm_send_selection_notify(struct wlr_xwm *xwm, +static void xwm_selection_send_notify(struct wlr_xwm_selection *selection, xcb_atom_t property) { - xcb_selection_notify_event_t selection_notify; - - memset(&selection_notify, 0, sizeof selection_notify); - selection_notify.response_type = XCB_SELECTION_NOTIFY; - selection_notify.sequence = 0; - selection_notify.time = xwm->selection_request.time; - selection_notify.requestor = xwm->selection_request.requestor; - selection_notify.selection = xwm->selection_request.selection; - selection_notify.target = xwm->selection_request.target; - selection_notify.property = property; - - xcb_send_event(xwm->xcb_conn, 0, // propagate - xwm->selection_request.requestor, - XCB_EVENT_MASK_NO_EVENT, (char *)&selection_notify); + xcb_selection_notify_event_t selection_notify = { + .response_type = XCB_SELECTION_NOTIFY, + .sequence = 0, + .time = selection->request.time, + .requestor = selection->request.requestor, + .selection = selection->request.selection, + .target = selection->request.target, + .property = property, + }; + + xcb_send_event(selection->xwm->xcb_conn, + 0, // propagate + selection->request.requestor, + XCB_EVENT_MASK_NO_EVENT, + (char *)&selection_notify); } -static int xwm_flush_source_data(struct wlr_xwm *xwm) -{ - xcb_change_property(xwm->xcb_conn, +static int xwm_selection_flush_source_data(struct wlr_xwm_selection *selection) { + xcb_change_property(selection->xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->selection_request.requestor, - xwm->selection_request.property, - xwm->selection_target, + selection->request.requestor, + selection->request.property, + selection->target, 8, // format - xwm->source_data.size, - xwm->source_data.data); - xwm->selection_property_set = 1; - int length = xwm->source_data.size; - xwm->source_data.size = 0; + selection->source_data.size, + selection->source_data.data); + selection->property_set = true; + int length = selection->source_data.size; + selection->source_data.size = 0; return length; } static int xwm_read_data_source(int fd, uint32_t mask, void *data) { - struct wlr_xwm *xwm = data; - void *p; + struct wlr_xwm_selection *selection = data; + struct wlr_xwm *xwm = selection->xwm; - int current = xwm->source_data.size; - if (xwm->source_data.size < incr_chunk_size) { - p = wl_array_add(&xwm->source_data, incr_chunk_size); + void *p; + int current = selection->source_data.size; + if (selection->source_data.size < incr_chunk_size) { + p = wl_array_add(&selection->source_data, incr_chunk_size); } else { - p = (char *) xwm->source_data.data + xwm->source_data.size; + p = (char *) selection->source_data.data + selection->source_data.size; } - int available = xwm->source_data.alloc - current; + int available = selection->source_data.alloc - current; int len = read(fd, p, available); if (len == -1) { wlr_log(L_ERROR, "read error from data source: %m"); - xwm_send_selection_notify(xwm, XCB_ATOM_NONE); - wl_event_source_remove(xwm->property_source); - xwm->property_source = NULL; + xwm_selection_send_notify(selection, XCB_ATOM_NONE); + wl_event_source_remove(selection->property_source); + selection->property_source = NULL; close(fd); - wl_array_release(&xwm->source_data); + wl_array_release(&selection->source_data); } wlr_log(L_DEBUG, "read %d (available %d, mask 0x%x) bytes: \"%.*s\"", len, available, mask, len, (char *) p); - xwm->source_data.size = current + len; - if (xwm->source_data.size >= incr_chunk_size) { - if (!xwm->incr) { + selection->source_data.size = current + len; + if (selection->source_data.size >= incr_chunk_size) { + if (!selection->incr) { wlr_log(L_DEBUG, "got %zu bytes, starting incr", - xwm->source_data.size); - xwm->incr = 1; + selection->source_data.size); + selection->incr = 1; xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->selection_request.requestor, - xwm->selection_request.property, + selection->request.requestor, + selection->request.property, xwm->atoms[INCR], 32, /* format */ 1, &incr_chunk_size); - xwm->selection_property_set = 1; - xwm->flush_property_on_delete = 1; - wl_event_source_remove(xwm->property_source); - xwm->property_source = NULL; - xwm_send_selection_notify(xwm, xwm->selection_request.property); - } else if (xwm->selection_property_set) { + selection->property_set = true; + selection->flush_property_on_delete = 1; + wl_event_source_remove(selection->property_source); + selection->property_source = NULL; + xwm_selection_send_notify(selection, selection->request.property); + } else if (selection->property_set) { wlr_log(L_DEBUG, "got %zu bytes, waiting for " - "property delete", xwm->source_data.size); + "property delete", selection->source_data.size); - xwm->flush_property_on_delete = 1; - wl_event_source_remove(xwm->property_source); - xwm->property_source = NULL; + selection->flush_property_on_delete = 1; + wl_event_source_remove(selection->property_source); + selection->property_source = NULL; } else { wlr_log(L_DEBUG, "got %zu bytes, " "property deleted, setting new property", - xwm->source_data.size); - xwm_flush_source_data(xwm); + selection->source_data.size); + xwm_selection_flush_source_data(selection); } - } else if (len == 0 && !xwm->incr) { + } else if (len == 0 && !selection->incr) { wlr_log(L_DEBUG, "non-incr transfer complete"); /* Non-incr transfer all done. */ - xwm_flush_source_data(xwm); - xwm_send_selection_notify(xwm, xwm->selection_request.property); + xwm_selection_flush_source_data(selection); + xwm_selection_send_notify(selection, selection->request.property); xcb_flush(xwm->xcb_conn); - wl_event_source_remove(xwm->property_source); - xwm->property_source = NULL; + wl_event_source_remove(selection->property_source); + selection->property_source = NULL; close(fd); - wl_array_release(&xwm->source_data); - xwm->selection_request.requestor = XCB_NONE; - } else if (len == 0 && xwm->incr) { + wl_array_release(&selection->source_data); + selection->request.requestor = XCB_NONE; + } else if (len == 0 && selection->incr) { wlr_log(L_DEBUG, "incr transfer complete"); - xwm->flush_property_on_delete = 1; - if (xwm->selection_property_set) { + selection->flush_property_on_delete = 1; + if (selection->property_set) { wlr_log(L_DEBUG, "got %zu bytes, waiting for " - "property delete", xwm->source_data.size); + "property delete", selection->source_data.size); } else { wlr_log(L_DEBUG, "got %zu bytes, " "property deleted, setting new property", - xwm->source_data.size); - xwm_flush_source_data(xwm); + selection->source_data.size); + xwm_selection_flush_source_data(selection); } xcb_flush(xwm->xcb_conn); - wl_event_source_remove(xwm->property_source); - xwm->property_source = NULL; - close(xwm->data_source_fd); - xwm->data_source_fd = -1; + wl_event_source_remove(selection->property_source); + selection->property_source = NULL; + close(selection->source_fd); + selection->source_fd = -1; close(fd); } else { wlr_log(L_DEBUG, "nothing happened, buffered the bytes"); @@ -140,14 +142,32 @@ static int xwm_read_data_source(int fd, uint32_t mask, void *data) { return 1; } -static void xwm_send_data(struct wlr_xwm *xwm, xcb_atom_t target, - const char *mime_type) { - struct wlr_data_source *source; - int p[2]; +static void xwm_selection_source_send(struct wlr_xwm_selection *selection, + const char *mime_type, int32_t fd) { + if (selection == &selection->xwm->clipboard_selection) { + struct wlr_data_source *source = selection->xwm->seat->selection_source; + if (source != NULL) { + source->send(source, mime_type, fd); + return; + } + } else if (selection == &selection->xwm->primary_selection) { + struct wlr_primary_selection_source *source = + selection->xwm->seat->primary_selection_source; + if (source != NULL) { + source->send(source, mime_type, fd); + return; + } + } + + wlr_log(L_DEBUG, "not sending selection: no selection source available"); +} +static void xwm_selection_send_data(struct wlr_xwm_selection *selection, + xcb_atom_t target, const char *mime_type) { + int p[2]; if (pipe(p) == -1) { wlr_log(L_ERROR, "pipe failed: %m"); - xwm_send_selection_notify(xwm, XCB_ATOM_NONE); + xwm_selection_send_notify(selection, XCB_ATOM_NONE); return; } @@ -156,37 +176,62 @@ static void xwm_send_data(struct wlr_xwm *xwm, xcb_atom_t target, fcntl(p[1], F_SETFD, FD_CLOEXEC); fcntl(p[1], F_SETFL, O_NONBLOCK); - wl_array_init(&xwm->source_data); - xwm->selection_target = target; - xwm->data_source_fd = p[0]; + wl_array_init(&selection->source_data); + selection->target = target; + selection->source_fd = p[0]; struct wl_event_loop *loop = - wl_display_get_event_loop(xwm->xwayland->wl_display); - xwm->property_source = wl_event_loop_add_fd(loop, - xwm->data_source_fd, + wl_display_get_event_loop(selection->xwm->xwayland->wl_display); + selection->property_source = wl_event_loop_add_fd(loop, + selection->source_fd, WL_EVENT_READABLE, xwm_read_data_source, - xwm); + selection); - source = xwm->seat->selection_source; - source->send(source, mime_type, p[1]); + xwm_selection_source_send(selection, mime_type, p[1]); close(p[1]); } -static void xwm_send_timestamp(struct wlr_xwm *xwm) { - xcb_change_property(xwm->xcb_conn, +static void xwm_selection_send_timestamp(struct wlr_xwm_selection *selection) { + xcb_change_property(selection->xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->selection_request.requestor, - xwm->selection_request.property, + selection->request.requestor, + selection->request.property, XCB_ATOM_INTEGER, 32, // format - 1, &xwm->selection_timestamp); + 1, &selection->timestamp); + + xwm_selection_send_notify(selection, selection->request.property); +} - xwm_send_selection_notify(xwm, xwm->selection_request.property); +static struct wl_array *xwm_selection_source_get_mime_types( + struct wlr_xwm_selection *selection) { + if (selection == &selection->xwm->clipboard_selection) { + struct wlr_data_source *source = selection->xwm->seat->selection_source; + if (source != NULL) { + return &source->mime_types; + } + } else if (selection == &selection->xwm->primary_selection) { + struct wlr_primary_selection_source *source = + selection->xwm->seat->primary_selection_source; + if (source != NULL) { + return &source->mime_types; + } + } + return NULL; } -static void xwm_send_targets(struct wlr_xwm *xwm) { - struct wlr_data_source *source = xwm->seat->selection_source; - size_t n = 2 + source->mime_types.size / sizeof(char *); +static void xwm_selection_send_targets(struct wlr_xwm_selection *selection) { + struct wlr_xwm *xwm = selection->xwm; + + struct wl_array *mime_types = xwm_selection_source_get_mime_types(selection); + if (mime_types == NULL) { + wlr_log(L_DEBUG, "not sending selection targets: " + "no selection source available"); + xwm_selection_send_notify(selection, XCB_ATOM_NONE); + return; + } + + size_t n = 2 + mime_types->size / sizeof(char *); xcb_atom_t *targets = malloc(n * sizeof(xcb_atom_t)); if (targets == NULL) { return; @@ -196,7 +241,7 @@ static void xwm_send_targets(struct wlr_xwm *xwm) { size_t i = 2; char **mime_type_ptr; - wl_array_for_each(mime_type_ptr, &source->mime_types) { + wl_array_for_each(mime_type_ptr, mime_types) { char *mime_type = *mime_type_ptr; xcb_atom_t atom; if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) { @@ -221,63 +266,77 @@ static void xwm_send_targets(struct wlr_xwm *xwm) { xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->selection_request.requestor, - xwm->selection_request.property, + selection->request.requestor, + selection->request.property, XCB_ATOM_ATOM, 32, // format n, targets); - xwm_send_selection_notify(xwm, xwm->selection_request.property); + xwm_selection_send_notify(selection, selection->request.property); free(targets); } +static struct wlr_xwm_selection *xwm_get_selection(struct wlr_xwm *xwm, + xcb_atom_t selection_atom) { + if (selection_atom == xwm->atoms[CLIPBOARD]) { + return &xwm->clipboard_selection; + } else if (selection_atom == xwm->atoms[PRIMARY]) { + return &xwm->primary_selection; + } else { + return NULL; + } +} + static void xwm_handle_selection_request(struct wlr_xwm *xwm, xcb_generic_event_t *event) { xcb_selection_request_event_t *selection_request = (xcb_selection_request_event_t *) event; - if (selection_request->selection != xwm->atoms[CLIPBOARD]) { - xwm_send_selection_notify(xwm, XCB_ATOM_NONE); - return; - } - - xwm->selection_request = *selection_request; - xwm->incr = 0; - xwm->flush_property_on_delete = 0; + wlr_log(L_DEBUG, "XCB_SELECTION_REQUEST (selection=%u, target=%u)", + selection_request->selection, selection_request->target); if (selection_request->selection == xwm->atoms[CLIPBOARD_MANAGER]) { - // The wlroots clipboard should already have grabbed - // the first target, so just send selection notify - // now. This isn't synchronized with the clipboard - // finishing getting the data, so there's a race here. - xwm_send_selection_notify(xwm, xwm->selection_request.property); + // The wlroots clipboard should already have grabbed the first target, + // so just send selection notify now. This isn't synchronized with the + // clipboard finishing getting the data, so there's a race here. + struct wlr_xwm_selection *selection = &xwm->clipboard_selection; + selection->request = *selection_request; + selection->incr = 0; + selection->flush_property_on_delete = 0; + xwm_selection_send_notify(selection, selection->request.property); return; } - if (xwm->seat->selection_source == NULL) { - wlr_log(L_DEBUG, "not handling selection request: " - "no selection source assigned to xwayland seat"); - xwm_send_selection_notify(xwm, XCB_ATOM_NONE); + struct wlr_xwm_selection *selection = + xwm_get_selection(xwm, selection_request->selection); + if (selection == NULL) { + xwm_selection_send_notify(selection, XCB_ATOM_NONE); return; } + selection->request = *selection_request; + selection->incr = 0; + selection->flush_property_on_delete = 0; + // No xwayland surface focused, deny access to clipboard if (xwm->focus_surface == NULL) { wlr_log(L_DEBUG, "denying read access to clipboard: " "no xwayland surface focused"); - xwm_send_selection_notify(xwm, XCB_ATOM_NONE); + xwm_selection_send_notify(selection, XCB_ATOM_NONE); return; } if (selection_request->target == xwm->atoms[TARGETS]) { - xwm_send_targets(xwm); + xwm_selection_send_targets(selection); } else if (selection_request->target == xwm->atoms[TIMESTAMP]) { - xwm_send_timestamp(xwm); + xwm_selection_send_timestamp(selection); } else if (selection_request->target == xwm->atoms[UTF8_STRING]) { - xwm_send_data(xwm, selection_request->target, "text/plain;charset=utf-8"); + xwm_selection_send_data(selection, selection_request->target, + "text/plain;charset=utf-8"); } else if (selection_request->target == xwm->atoms[TEXT]) { - xwm_send_data(xwm, selection_request->target, "text/plain"); + xwm_selection_send_data(selection, selection_request->target, + "text/plain"); } else { xcb_get_atom_name_cookie_t name_cookie = xcb_get_atom_name(xwm->xcb_conn, selection_request->target); @@ -285,7 +344,7 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, xcb_get_atom_name_reply(xwm->xcb_conn, name_cookie, NULL); if (name_reply == NULL) { wlr_log(L_DEBUG, "not handling selection request: unknown atom"); - xwm_send_selection_notify(xwm, XCB_ATOM_NONE); + xwm_selection_send_notify(selection, XCB_ATOM_NONE); return; } size_t len = xcb_get_atom_name_name_length(name_reply); @@ -296,48 +355,49 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, } memcpy(mime_type, xcb_get_atom_name_name(name_reply), len); mime_type[len] = '\0'; - xwm_send_data(xwm, selection_request->target, mime_type); + xwm_selection_send_data(selection, selection_request->target, mime_type); free(mime_type); free(name_reply); } } static int writable_callback(int fd, uint32_t mask, void *data) { - struct wlr_xwm *xwm = data; + struct wlr_xwm_selection *selection = data; + struct wlr_xwm *xwm = selection->xwm; - unsigned char *property = xcb_get_property_value(xwm->property_reply); - int remainder = xcb_get_property_value_length(xwm->property_reply) - - xwm->property_start; + unsigned char *property = xcb_get_property_value(selection->property_reply); + int remainder = xcb_get_property_value_length(selection->property_reply) - + selection->property_start; - int len = write(fd, property + xwm->property_start, remainder); + int len = write(fd, property + selection->property_start, remainder); if (len == -1) { - free(xwm->property_reply); - xwm->property_reply = NULL; - if (xwm->property_source) { - wl_event_source_remove(xwm->property_source); + free(selection->property_reply); + selection->property_reply = NULL; + if (selection->property_source) { + wl_event_source_remove(selection->property_source); } - xwm->property_source = NULL; + selection->property_source = NULL; close(fd); wlr_log(L_ERROR, "write error to target fd: %m"); return 1; } wlr_log(L_DEBUG, "wrote %d (chunk size %d) of %d bytes", - xwm->property_start + len, - len, xcb_get_property_value_length(xwm->property_reply)); + selection->property_start + len, + len, xcb_get_property_value_length(selection->property_reply)); - xwm->property_start += len; + selection->property_start += len; if (len == remainder) { - free(xwm->property_reply); - xwm->property_reply = NULL; - if (xwm->property_source) { - wl_event_source_remove(xwm->property_source); + free(selection->property_reply); + selection->property_reply = NULL; + if (selection->property_source) { + wl_event_source_remove(selection->property_source); } - xwm->property_source = NULL; + selection->property_source = NULL; - if (xwm->incr) { + if (selection->incr) { xcb_delete_property(xwm->xcb_conn, - xwm->selection_window, + selection->window, xwm->atoms[WL_SELECTION]); } else { wlr_log(L_DEBUG, "transfer complete"); @@ -348,86 +408,77 @@ static int writable_callback(int fd, uint32_t mask, void *data) { return 1; } -static void xwm_write_property(struct wlr_xwm *xwm, +static void xwm_write_property(struct wlr_xwm_selection *selection, xcb_get_property_reply_t *reply) { - xwm->property_start = 0; - xwm->property_reply = reply; - writable_callback(xwm->data_source_fd, WL_EVENT_WRITABLE, xwm); + selection->property_start = 0; + selection->property_reply = reply; + writable_callback(selection->source_fd, WL_EVENT_WRITABLE, selection); - if (xwm->property_reply) { + if (selection->property_reply) { struct wl_event_loop *loop = - wl_display_get_event_loop(xwm->xwayland->wl_display); - xwm->property_source = - wl_event_loop_add_fd(loop, - xwm->data_source_fd, - WL_EVENT_WRITABLE, - writable_callback, xwm); + wl_display_get_event_loop(selection->xwm->xwayland->wl_display); + selection->property_source = wl_event_loop_add_fd(loop, + selection->source_fd, WL_EVENT_WRITABLE, writable_callback, + selection); } } -static void xwm_get_selection_data(struct wlr_xwm *xwm) { - xcb_get_property_cookie_t cookie = - xcb_get_property(xwm->xcb_conn, - 1, // delete - xwm->selection_window, - xwm->atoms[WL_SELECTION], - XCB_GET_PROPERTY_TYPE_ANY, - 0, // offset - 0x1fffffff // length - ); +static void xwm_selection_get_data(struct wlr_xwm_selection *selection) { + struct wlr_xwm *xwm = selection->xwm; + + xcb_get_property_cookie_t cookie = xcb_get_property(xwm->xcb_conn, + 1, // delete + selection->window, + xwm->atoms[WL_SELECTION], + XCB_GET_PROPERTY_TYPE_ANY, + 0, // offset + 0x1fffffff // length + ); xcb_get_property_reply_t *reply = xcb_get_property_reply(xwm->xcb_conn, cookie, NULL); - if (reply == NULL) { return; - } else if (reply->type == xwm->atoms[INCR]) { - xwm->incr = 1; + } + + if (reply->type == xwm->atoms[INCR]) { + selection->incr = 1; free(reply); } else { - xwm->incr = 0; + selection->incr = 0; // reply's ownership is transferred to wm, which is responsible // for freeing it - xwm_write_property(xwm, reply); + xwm_write_property(selection, reply); } } -struct x11_data_source { - struct wlr_data_source base; - struct wlr_xwm *xwm; - struct wl_array mime_types_atoms; -}; - -static void data_source_accept(struct wlr_data_source *source, uint32_t time, - const char *mime_type) { -} - -static void data_source_send(struct wlr_data_source *base, +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) { - struct x11_data_source *source = (struct x11_data_source *)base; - struct wlr_xwm *xwm = source->xwm; + struct wlr_xwm *xwm = selection->xwm; + xcb_atom_t *atoms = mime_types_atoms->data; bool found = false; xcb_atom_t mime_type_atom; char **mime_type_ptr; size_t i = 0; - wl_array_for_each(mime_type_ptr, &source->base.mime_types) { + wl_array_for_each(mime_type_ptr, mime_types) { char *mime_type = *mime_type_ptr; if (strcmp(mime_type, requested_mime_type) == 0) { found = true; - xcb_atom_t *atoms = source->mime_types_atoms.data; mime_type_atom = atoms[i]; break; } ++i; } if (!found) { + wlr_log(L_DEBUG, "cannot send X11 selection: unsupported MIME type"); return; } xcb_convert_selection(xwm->xcb_conn, - xwm->selection_window, - xwm->atoms[CLIPBOARD], + selection->window, + selection->atom, mime_type_atom, xwm->atoms[WL_SELECTION], XCB_TIME_CURRENT_TIME); @@ -435,18 +486,69 @@ static void data_source_send(struct wlr_data_source *base, xcb_flush(xwm->xcb_conn); fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); - xwm->data_source_fd = fd; + selection->source_fd = fd; +} + +struct x11_data_source { + struct wlr_data_source base; + struct wlr_xwm_selection *selection; + struct wl_array mime_types_atoms; +}; + +static void data_source_accept(struct wlr_data_source *base, uint32_t time, + const char *mime_type) { + // No-op } -static void data_source_cancel(struct wlr_data_source *source) { +static void data_source_send(struct wlr_data_source *base, + const char *mime_type, int32_t fd) { + struct x11_data_source *source = (struct x11_data_source *)base; + struct wlr_xwm_selection *selection = source->selection; + + source_send(selection, &base->mime_types, &source->mime_types_atoms, + mime_type, fd); } -static void xwm_get_selection_targets(struct wlr_xwm *xwm) { - // set the wayland clipboard selection to the copied selection +static void data_source_cancel(struct wlr_data_source *base) { + struct x11_data_source *source = (struct x11_data_source *)base; + wlr_data_source_finish(&source->base); + wl_array_release(&source->mime_types_atoms); + free(source); +} + +struct x11_primary_selection_source { + struct wlr_primary_selection_source base; + struct wlr_xwm_selection *selection; + struct wl_array mime_types_atoms; +}; + +static void primary_selection_source_send( + struct wlr_primary_selection_source *base, const char *mime_type, + int32_t fd) { + struct x11_primary_selection_source *source = + (struct x11_primary_selection_source *)base; + struct wlr_xwm_selection *selection = source->selection; + + source_send(selection, &base->mime_types, &source->mime_types_atoms, + mime_type, fd); +} + +static void primary_selection_source_cancel( + struct wlr_primary_selection_source *base) { + struct x11_primary_selection_source *source = + (struct x11_primary_selection_source *)base; + wlr_primary_selection_source_finish(&source->base); + wl_array_release(&source->mime_types_atoms); + free(source); +} + +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; xcb_get_property_cookie_t cookie = xcb_get_property(xwm->xcb_conn, 1, // delete - xwm->selection_window, + selection->window, xwm->atoms[WL_SELECTION], XCB_GET_PROPERTY_TYPE_ANY, 0, // offset @@ -456,28 +558,14 @@ static void xwm_get_selection_targets(struct wlr_xwm *xwm) { xcb_get_property_reply_t *reply = xcb_get_property_reply(xwm->xcb_conn, cookie, NULL); if (reply == NULL) { - return; + return false; } if (reply->type != XCB_ATOM_ATOM) { free(reply); - return; - } - - struct x11_data_source *source = calloc(1, sizeof(struct x11_data_source)); - if (source == NULL) { - free(reply); - return; + return false; } - wl_signal_init(&source->base.events.destroy); - source->base.accept = data_source_accept; - source->base.send = data_source_send; - source->base.cancel = data_source_cancel; - source->xwm = xwm; - - wl_array_init(&source->base.mime_types); - wl_array_init(&source->mime_types_atoms); xcb_atom_t *value = xcb_get_property_value(reply); for (uint32_t i = 0; i < reply->value_len; i++) { char *mime_type = NULL; @@ -511,14 +599,14 @@ static void xwm_get_selection_targets(struct wlr_xwm *xwm) { if (mime_type != NULL) { char **mime_type_ptr = - wl_array_add(&source->base.mime_types, sizeof(*mime_type_ptr)); + wl_array_add(mime_types, sizeof(*mime_type_ptr)); if (mime_type_ptr == NULL) { break; } *mime_type_ptr = mime_type; xcb_atom_t *atom_ptr = - wl_array_add(&source->mime_types_atoms, sizeof(*atom_ptr)); + wl_array_add(mime_types_atoms, sizeof(*atom_ptr)); if (atom_ptr == NULL) { break; } @@ -526,10 +614,58 @@ static void xwm_get_selection_targets(struct wlr_xwm *xwm) { } } - wlr_seat_set_selection(xwm->seat, &source->base, - wl_display_next_serial(xwm->xwayland->wl_display)); - free(reply); + return true; +} + +static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { + // set the wayland selection to the X11 selection + struct wlr_xwm *xwm = selection->xwm; + + if (selection == &xwm->clipboard_selection) { + struct x11_data_source *source = + calloc(1, sizeof(struct x11_data_source)); + if (source == NULL) { + return; + } + wlr_data_source_init(&source->base); + source->base.accept = data_source_accept; + source->base.send = data_source_send; + source->base.cancel = data_source_cancel; + + source->selection = selection; + wl_array_init(&source->mime_types_atoms); + + bool ok = source_get_targets(selection, &source->base.mime_types, + &source->mime_types_atoms); + if (ok) { + wlr_seat_set_selection(xwm->seat, &source->base, + wl_display_next_serial(xwm->xwayland->wl_display)); + } else { + source->base.cancel(&source->base); + } + } else if (selection == &xwm->primary_selection) { + struct x11_primary_selection_source *source = + calloc(1, sizeof(struct x11_primary_selection_source)); + if (source == NULL) { + return; + } + wlr_primary_selection_source_init(&source->base); + source->base.send = primary_selection_source_send; + source->base.cancel = primary_selection_source_cancel; + + source->selection = selection; + wl_array_init(&source->mime_types_atoms); + + 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, + wl_display_next_serial(xwm->xwayland->wl_display)); + } else { + source->base.cancel(&source->base); + } + } } static void xwm_handle_selection_notify(struct wlr_xwm *xwm, @@ -537,7 +673,13 @@ static void xwm_handle_selection_notify(struct wlr_xwm *xwm, xcb_selection_notify_event_t *selection_notify = (xcb_selection_notify_event_t *) event; - if (selection_notify->selection != xwm->atoms[CLIPBOARD]) { + wlr_log(L_DEBUG, "XCB_SELECTION_NOTIFY (selection=%u, property=%u, target=%u)", + selection_notify->selection, selection_notify->property, + selection_notify->target); + + struct wlr_xwm_selection *selection = + xwm_get_selection(xwm, selection_notify->selection); + if (selection == NULL) { return; } @@ -552,48 +694,57 @@ static void xwm_handle_selection_notify(struct wlr_xwm *xwm, } // This sets the Wayland clipboard (by calling wlr_seat_set_selection) - xwm_get_selection_targets(xwm); + xwm_selection_get_targets(selection); } else { - xwm_get_selection_data(xwm); + xwm_selection_get_data(selection); } } static int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, xcb_generic_event_t *event) { xcb_xfixes_selection_notify_event_t *xfixes_selection_notify = - (xcb_xfixes_selection_notify_event_t *) event; + (xcb_xfixes_selection_notify_event_t *)event; - if (xfixes_selection_notify->selection != xwm->atoms[CLIPBOARD]) { + wlr_log(L_DEBUG, "XCB_XFIXES_SELECTION_NOTIFY (selection=%u, owner=%u)", + xfixes_selection_notify->selection, xfixes_selection_notify->owner); + + struct wlr_xwm_selection *selection = + xwm_get_selection(xwm, xfixes_selection_notify->selection); + if (selection == NULL) { return 0; } if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) { - if (xwm->selection_owner != xwm->selection_window) { + if (selection->owner != selection->window) { // A real X client selection went away, not our // proxy selection - wlr_seat_set_selection(xwm->seat, NULL, - wl_display_next_serial(xwm->xwayland->wl_display)); + if (selection == &xwm->clipboard_selection) { + 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, + wl_display_next_serial(xwm->xwayland->wl_display)); + } } - xwm->selection_owner = XCB_WINDOW_NONE; + selection->owner = XCB_WINDOW_NONE; return 1; } - xwm->selection_owner = xfixes_selection_notify->owner; + selection->owner = xfixes_selection_notify->owner; // We have to use XCB_TIME_CURRENT_TIME when we claim the // selection, so grab the actual timestamp here so we can // answer TIMESTAMP conversion requests correctly. - if (xfixes_selection_notify->owner == xwm->selection_window) { - xwm->selection_timestamp = xfixes_selection_notify->timestamp; - wlr_log(L_DEBUG, "our window, skipping"); + if (xfixes_selection_notify->owner == selection->window) { + selection->timestamp = xfixes_selection_notify->timestamp; return 1; } - xwm->incr = 0; + selection->incr = 0; // doing this will give a selection notify where we actually handle the sync - xcb_convert_selection(xwm->xcb_conn, xwm->selection_window, - xfixes_selection_notify->selection, + xcb_convert_selection(xwm->xcb_conn, selection->window, + selection->atom, xwm->atoms[TARGETS], xwm->atoms[WL_SELECTION], xfixes_selection_notify->timestamp); @@ -629,12 +780,23 @@ int xwm_handle_selection_event(struct wlr_xwm *xwm, return 0; } -void xwm_selection_init(struct wlr_xwm *xwm) { - uint32_t values[1], mask; +static void selection_init(struct wlr_xwm *xwm, + struct wlr_xwm_selection *selection, xcb_atom_t atom) { + selection->xwm = xwm; + selection->atom = atom; + selection->window = xwm->selection_window; + selection->request.requestor = XCB_NONE; - xwm->selection_request.requestor = XCB_NONE; + uint32_t mask = + XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | + XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | + XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE; + xcb_xfixes_select_selection_input(xwm->xcb_conn, selection->window, + selection->atom, mask); +} - values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; +void xwm_selection_init(struct wlr_xwm *xwm) { + uint32_t values[] = { XCB_EVENT_MASK_PROPERTY_CHANGE }; xwm->selection_window = xcb_generate_id(xwm->xcb_conn); xcb_create_window(xwm->xcb_conn, XCB_COPY_FROM_PARENT, @@ -652,52 +814,73 @@ void xwm_selection_init(struct wlr_xwm *xwm) { xwm->atoms[CLIPBOARD_MANAGER], XCB_TIME_CURRENT_TIME); - mask = - XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | - XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | - XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE; - xcb_xfixes_select_selection_input(xwm->xcb_conn, xwm->selection_window, - xwm->atoms[CLIPBOARD], mask); + selection_init(xwm, &xwm->clipboard_selection, xwm->atoms[CLIPBOARD]); + selection_init(xwm, &xwm->primary_selection, xwm->atoms[PRIMARY]); +} + +static void xwm_selection_set_owner(struct wlr_xwm_selection *selection, + bool set) { + if (set) { + xcb_set_selection_owner(selection->xwm->xcb_conn, + selection->window, + selection->atom, + XCB_TIME_CURRENT_TIME); + } else { + if (selection->owner == selection->window) { + xcb_set_selection_owner(selection->xwm->xcb_conn, + XCB_WINDOW_NONE, + selection->atom, + selection->timestamp); + } + } } -static void handle_seat_set_selection(struct wl_listener *listener, +static void seat_handle_selection(struct wl_listener *listener, void *data) { struct wlr_seat *seat = data; struct wlr_xwm *xwm = - wl_container_of(listener, xwm, seat_selection_change); + wl_container_of(listener, xwm, seat_selection); struct wlr_data_source *source = seat->selection_source; - if (source == NULL) { - if (xwm->selection_owner == xwm->selection_window) { - xcb_set_selection_owner(xwm->xcb_conn, - XCB_ATOM_NONE, - xwm->atoms[CLIPBOARD], - xwm->selection_timestamp); - } - + if (source != NULL && source->send == data_source_send) { return; } - if (source->send == data_source_send) { + xwm_selection_set_owner(&xwm->clipboard_selection, source != NULL); +} + +static void seat_handle_primary_selection(struct wl_listener *listener, + void *data) { + 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; + + if (source != NULL && source->send == primary_selection_source_send) { return; } - xcb_set_selection_owner(xwm->xcb_conn, - xwm->selection_window, - xwm->atoms[CLIPBOARD], - XCB_TIME_CURRENT_TIME); + xwm_selection_set_owner(&xwm->primary_selection, source != NULL); } void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat) { - assert(xwm); - assert(seat); - if (xwm->seat) { - wl_list_remove(&xwm->seat_selection_change.link); + if (xwm->seat != NULL) { + wl_list_remove(&xwm->seat_selection.link); + wl_list_remove(&xwm->seat_primary_selection.link); xwm->seat = NULL; } - wl_signal_add(&seat->events.selection, &xwm->seat_selection_change); - xwm->seat_selection_change.notify = handle_seat_set_selection; + if (seat == NULL) { + return; + } + xwm->seat = seat; - handle_seat_set_selection(&xwm->seat_selection_change, seat); + + wl_signal_add(&seat->events.selection, &xwm->seat_selection); + xwm->seat_selection.notify = seat_handle_selection; + wl_signal_add(&seat->events.primary_selection, &xwm->seat_primary_selection); + xwm->seat_primary_selection.notify = seat_handle_primary_selection; + + seat_handle_selection(&xwm->seat_selection, seat); + seat_handle_primary_selection(&xwm->seat_primary_selection, seat); } diff --git a/xwayland/xwm.c b/xwayland/xwm.c index a642f164..d9bfaf2e 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -7,6 +7,7 @@ #include <xcb/xfixes.h> #include <xcb/xcb_image.h> #include <xcb/render.h> +#include "config.h" #include "wlr/util/log.h" #include "wlr/util/edges.h" #include "wlr/types/wlr_surface.h" @@ -14,7 +15,7 @@ #include "wlr/xcursor.h" #include "wlr/xwm.h" -#ifdef HAS_XCB_ICCCM +#ifdef WLR_HAS_XCB_ICCCM #include <xcb/xcb_icccm.h> #endif @@ -45,6 +46,7 @@ const char *atom_map[ATOM_LAST] = { "_NET_WM_STATE_MAXIMIZED_HORZ", "WM_STATE", "CLIPBOARD", + "PRIMARY", "_WL_SELECTION", "TARGETS", "CLIPBOARD_MANAGER", @@ -365,7 +367,7 @@ static void read_surface_protocols(struct wlr_xwm *xwm, wlr_log(L_DEBUG, "WM_PROTOCOLS (%zu)", atoms_len); } -#ifdef HAS_XCB_ICCCM +#ifdef WLR_HAS_XCB_ICCCM static void read_surface_hints(struct wlr_xwm *xwm, struct wlr_xwayland_surface *xsurface, xcb_get_property_reply_t *reply) { @@ -396,7 +398,7 @@ static void read_surface_hints(struct wlr_xwm *xwm, } #endif -#ifdef HAS_XCB_ICCCM +#ifdef WLR_HAS_XCB_ICCCM static void read_surface_normal_hints(struct wlr_xwm *xwm, struct wlr_xwayland_surface *xsurface, xcb_get_property_reply_t *reply) { |