From b6c1760de5952584cb868f491a12addfb9d9c114 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 27 Mar 2018 12:04:37 -0400 Subject: xwayland: create DND window, add DND atom helpers --- include/xwayland/xwm.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'include') diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index 9e21ea3a..81a4df0b 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -53,6 +53,20 @@ enum atom_name { NET_WM_WINDOW_TYPE_DROPDOWN_MENU, NET_WM_WINDOW_TYPE_POPUP_MENU, NET_WM_WINDOW_TYPE_COMBO, + DND_SELECTION, + DND_AWARE, + DND_STATUS, + DND_POSITION, + DND_ENTER, + DND_LEAVE, + DND_DROP, + DND_FINISHED, + DND_PROXY, + DND_TYPE_LIST, + DND_ACTION_MOVE, + DND_ACTION_COPY, + DND_ACTION_ASK, + DND_ACTION_PRIVATE, ATOM_LAST, }; @@ -64,6 +78,8 @@ enum net_wm_state_action { NET_WM_STATE_TOGGLE = 2, }; +#define XDND_VERSION 5 + struct wlr_xwm_selection { struct wlr_xwm *xwm; xcb_atom_t atom; @@ -100,6 +116,8 @@ struct wlr_xwm { struct wlr_xwm_selection clipboard_selection; struct wlr_xwm_selection primary_selection; + xcb_window_t dnd_window; + struct wlr_xwayland_surface *focus_surface; struct wl_list surfaces; // wlr_xwayland_surface::link -- cgit v1.2.3 From 0d7a81ccdfda7d21ece47e0715a25bd301363cc2 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 28 Mar 2018 12:59:11 -0400 Subject: xwayland: send DND_ENTER --- include/wlr/types/wlr_data_device.h | 5 ++ include/wlr/types/wlr_seat.h | 4 + include/xwayland/xwm.h | 6 +- rootston/cursor.c | 3 +- types/wlr_data_device.c | 78 +++++++++------- types/wlr_seat.c | 1 + xwayland/selection.c | 173 ++++++++++++++++++++++++++---------- 7 files changed, 190 insertions(+), 80 deletions(-) (limited to 'include') diff --git a/include/wlr/types/wlr_data_device.h b/include/wlr/types/wlr_data_device.h index ff4a0f7e..cefcc979 100644 --- a/include/wlr/types/wlr_data_device.h +++ b/include/wlr/types/wlr_data_device.h @@ -104,6 +104,11 @@ struct wlr_drag { struct wl_listener source_destroy; struct wl_listener seat_client_destroy; struct wl_listener icon_destroy; + + struct { + struct wl_signal focus; + struct wl_signal destroy; + } events; }; /** diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index 124c1cb8..435a3e7e 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -187,6 +187,9 @@ struct wlr_seat { struct wlr_primary_selection_source *primary_selection_source; uint32_t primary_selection_serial; + struct wlr_drag *drag; + uint32_t drag_serial; + struct wlr_seat_pointer_state pointer_state; struct wlr_seat_keyboard_state keyboard_state; struct wlr_seat_touch_state touch_state; @@ -210,6 +213,7 @@ struct wlr_seat { struct wl_signal selection; struct wl_signal primary_selection; + struct wl_signal start_drag; struct wl_signal new_drag_icon; struct wl_signal destroy; diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index 81a4df0b..e672a2df 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -115,8 +115,7 @@ struct wlr_xwm { xcb_window_t selection_window; struct wlr_xwm_selection clipboard_selection; struct wlr_xwm_selection primary_selection; - - xcb_window_t dnd_window; + struct wlr_xwm_selection dnd_selection; struct wlr_xwayland_surface *focus_surface; @@ -132,6 +131,9 @@ struct wlr_xwm { struct wl_listener compositor_destroy; struct wl_listener seat_selection; struct wl_listener seat_primary_selection; + struct wl_listener seat_start_drag; + struct wl_listener seat_drag_focus; + struct wl_listener seat_drag_destroy; }; struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland); diff --git a/rootston/cursor.c b/rootston/cursor.c index 52439dff..ac597590 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -131,7 +131,8 @@ static void roots_cursor_update_position(struct roots_cursor *cursor, cursor->pointer_view = seat_view; seat_view_deco_motion(seat_view, sx, sy); } - } if (view && surface) { + } + if (view && surface) { // motion over a view surface wlr_seat_pointer_notify_enter(seat->seat, surface, sx, sy); wlr_seat_pointer_notify_motion(seat->seat, time, sx, sy); diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index 50c94bc5..8129b173 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -427,7 +427,7 @@ static void wlr_drag_set_focus(struct wlr_drag *drag, wlr_seat_client_for_wl_client(drag->seat_client->seat, wl_resource_get_client(surface->resource)); - if (!focus_client || wl_list_empty(&focus_client->data_devices)) { + if (!focus_client) { return; } @@ -436,34 +436,36 @@ static void wlr_drag_set_focus(struct wlr_drag *drag, drag->source->accepted = false; struct wlr_data_offer *offer = wlr_data_source_send_offer(drag->source, focus_client); - if (offer == NULL) { - return; - } + if (offer != NULL) { + data_offer_update_action(offer); - data_offer_update_action(offer); + if (wl_resource_get_version(offer->resource) >= + WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) { + wl_data_offer_send_source_actions(offer->resource, + drag->source->actions); + } - if (wl_resource_get_version(offer->resource) >= - WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) { - wl_data_offer_send_source_actions(offer->resource, - drag->source->actions); + offer_resource = offer->resource; } - - offer_resource = offer->resource; } - uint32_t serial = - wl_display_next_serial(drag->seat_client->seat->display); - struct wl_resource *resource; - wl_resource_for_each(resource, &focus_client->data_devices) { - wl_data_device_send_enter(resource, serial, surface->resource, - wl_fixed_from_double(sx), wl_fixed_from_double(sy), offer_resource); + if (!wl_list_empty(&focus_client->data_devices)) { + uint32_t serial = + wl_display_next_serial(drag->seat_client->seat->display); + struct wl_resource *resource; + wl_resource_for_each(resource, &focus_client->data_devices) { + wl_data_device_send_enter(resource, serial, surface->resource, + wl_fixed_from_double(sx), wl_fixed_from_double(sy), + offer_resource); + } } drag->focus = surface; drag->focus_client = focus_client; drag->seat_client_destroy.notify = handle_drag_seat_client_destroy; - wl_signal_add(&focus_client->events.destroy, - &drag->seat_client_destroy); + wl_signal_add(&focus_client->events.destroy, &drag->seat_client_destroy); + + wlr_signal_emit_safe(&drag->events.focus, drag); } static void wlr_drag_end(struct wlr_drag *drag) { @@ -488,10 +490,14 @@ static void wlr_drag_end(struct wlr_drag *drag) { wlr_signal_emit_safe(&drag->icon->events.map, drag->icon); } + wlr_signal_emit_safe(&drag->events.destroy, drag); free(drag); } } +const struct +wlr_pointer_grab_interface wlr_data_device_pointer_drag_interface; + static void pointer_drag_enter(struct wlr_seat_pointer_grab *grab, struct wlr_surface *surface, double sx, double sy) { struct wlr_drag *drag = grab->data; @@ -732,22 +738,26 @@ static bool seat_client_start_drag(struct wlr_seat_client *client, return false; } - drag->seat = client->seat; + wl_signal_init(&drag->events.focus); + wl_signal_init(&drag->events.destroy); + + struct wlr_seat *seat = client->seat; + drag->seat = seat; drag->is_pointer_grab = !wl_list_empty(&client->pointers) && - client->seat->pointer_state.button_count == 1 && - client->seat->pointer_state.grab_serial == serial && - client->seat->pointer_state.focused_surface && - client->seat->pointer_state.focused_surface == origin; + seat->pointer_state.button_count == 1 && + seat->pointer_state.grab_serial == serial && + seat->pointer_state.focused_surface && + seat->pointer_state.focused_surface == origin; bool is_touch_grab = !wl_list_empty(&client->touches) && - wlr_seat_touch_num_points(client->seat) == 1 && - client->seat->touch_state.grab_serial == serial; + wlr_seat_touch_num_points(seat) == 1 && + seat->touch_state.grab_serial == serial; // set in the iteration struct wlr_touch_point *point = NULL; if (is_touch_grab) { - wl_list_for_each(point, &client->seat->touch_state.touch_points, link) { + wl_list_for_each(point, &seat->touch_state.touch_points, link) { is_touch_grab = point->surface && point->surface == origin; break; } @@ -785,22 +795,26 @@ static bool seat_client_start_drag(struct wlr_seat_client *client, drag->touch_grab.data = drag; drag->touch_grab.interface = &wlr_data_device_touch_drag_interface; - drag->grab_touch_id = drag->seat->touch_state.grab_id; + drag->grab_touch_id = seat->touch_state.grab_id; drag->keyboard_grab.data = drag; drag->keyboard_grab.interface = &wlr_data_device_keyboard_drag_interface; - wlr_seat_keyboard_start_grab(drag->seat, &drag->keyboard_grab); + wlr_seat_keyboard_start_grab(seat, &drag->keyboard_grab); if (drag->is_pointer_grab) { - wlr_seat_pointer_clear_focus(drag->seat); - wlr_seat_pointer_start_grab(drag->seat, &drag->pointer_grab); + wlr_seat_pointer_clear_focus(seat); + wlr_seat_pointer_start_grab(seat, &drag->pointer_grab); } else { assert(point); - wlr_seat_touch_start_grab(drag->seat, &drag->touch_grab); + wlr_seat_touch_start_grab(seat, &drag->touch_grab); wlr_drag_set_focus(drag, point->surface, point->sx, point->sy); } + seat->drag = drag; // TODO: unset this thing somewhere + seat->drag_serial = serial; + wlr_signal_emit_safe(&seat->events.start_drag, drag); + return true; } diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 93f6d872..53659f90 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -465,6 +465,7 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) { wl_list_init(&wlr_seat->clients); wl_list_init(&wlr_seat->drag_icons); + wl_signal_init(&wlr_seat->events.start_drag); wl_signal_init(&wlr_seat->events.new_drag_icon); wl_signal_init(&wlr_seat->events.request_set_cursor); diff --git a/xwayland/selection.c b/xwayland/selection.c index a7ed2cf0..8e5905df 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -12,7 +12,7 @@ static const size_t incr_chunk_size = 64 * 1024; -static xcb_atom_t data_device_manager_dnd_action_to_atom( +/*static xcb_atom_t data_device_manager_dnd_action_to_atom( struct wlr_xwm *xwm, enum wl_data_device_manager_dnd_action action) { if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) { return xwm->atoms[DND_ACTION_COPY]; @@ -36,7 +36,7 @@ static enum wl_data_device_manager_dnd_action return WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK; } return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; -} +}*/ static void xwm_selection_send_notify(struct wlr_xwm_selection *selection, xcb_atom_t property) { @@ -241,6 +241,81 @@ static void xwm_selection_send_timestamp(struct wlr_xwm_selection *selection) { xwm_selection_send_notify(selection, selection->request.property); } +static xcb_atom_t xwm_get_mime_type_atom(struct wlr_xwm *xwm, char *mime_type) { + if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) { + return xwm->atoms[UTF8_STRING]; + } else if (strcmp(mime_type, "text/plain") == 0) { + return xwm->atoms[TEXT]; + } + + xcb_intern_atom_cookie_t cookie = + xcb_intern_atom(xwm->xcb_conn, 0, strlen(mime_type), mime_type); + xcb_intern_atom_reply_t *reply = + xcb_intern_atom_reply(xwm->xcb_conn, cookie, NULL); + if (reply == NULL) { + return XCB_ATOM_NONE; + } + xcb_atom_t atom = reply->atom; + free(reply); + return atom; +} + +static void xwm_dnd_send_enter(struct wlr_xwm *xwm, struct wlr_drag *drag, + struct wlr_xwayland_surface *dest) { + struct wl_array *mime_types = &drag->source->mime_types; + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_selection.window; + data.data32[1] = XDND_VERSION << 24; + + size_t n = mime_types->size / sizeof(char *); + if (n <= 3) { + size_t i = 0; + char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, mime_types) { + char *mime_type = *mime_type_ptr; + data.data32[2+i] = xwm_get_mime_type_atom(xwm, mime_type); + ++i; + } + } else { + // Let the client know that targets are not contained in the message + // data and must be retrieved with the DND_TYPE_LIST property + data.data32[1] |= 1; + + xcb_atom_t targets[n]; + size_t i = 0; + char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, mime_types) { + char *mime_type = *mime_type_ptr; + targets[i] = xwm_get_mime_type_atom(xwm, mime_type); + ++i; + } + + xcb_change_property(xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + xwm->dnd_selection.window, + xwm->atoms[DND_TYPE_LIST], + XCB_ATOM_ATOM, + 32, // format + n, targets); + } + + xcb_client_message_event_t event = { + .response_type = XCB_CLIENT_MESSAGE, + .format = 32, + .sequence = 0, + .window = dest->window_id, + .type = xwm->atoms[DND_ENTER], + .data = data, + }; + + xcb_send_event(xwm->xcb_conn, + 0, // propagate + dest->window_id, + XCB_EVENT_MASK_NO_EVENT, + (const char *)&event); +} + static struct wl_array *xwm_selection_source_get_mime_types( struct wlr_xwm_selection *selection) { if (selection == &selection->xwm->clipboard_selection) { @@ -271,35 +346,15 @@ static void xwm_selection_send_targets(struct wlr_xwm_selection *selection) { } size_t n = 2 + mime_types->size / sizeof(char *); - xcb_atom_t *targets = malloc(n * sizeof(xcb_atom_t)); - if (targets == NULL) { - return; - } + xcb_atom_t targets[n]; targets[0] = xwm->atoms[TIMESTAMP]; targets[1] = xwm->atoms[TARGETS]; - size_t i = 2; + size_t i = 0; char **mime_type_ptr; 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) { - atom = xwm->atoms[UTF8_STRING]; - } else if (strcmp(mime_type, "text/plain") == 0) { - atom = xwm->atoms[TEXT]; - } else { - xcb_intern_atom_cookie_t cookie = - xcb_intern_atom(xwm->xcb_conn, 0, strlen(mime_type), mime_type); - xcb_intern_atom_reply_t *reply = - xcb_intern_atom_reply(xwm->xcb_conn, cookie, NULL); - if (reply == NULL) { - --n; - continue; - } - atom = reply->atom; - free(reply); - } - targets[i] = atom; + targets[2+i] = xwm_get_mime_type_atom(xwm, mime_type); ++i; } @@ -312,8 +367,6 @@ static void xwm_selection_send_targets(struct wlr_xwm_selection *selection) { n, targets); xwm_selection_send_notify(selection, selection->request.property); - - free(targets); } static struct wlr_xwm_selection *xwm_get_selection(struct wlr_xwm *xwm, @@ -322,6 +375,8 @@ static struct wlr_xwm_selection *xwm_get_selection(struct wlr_xwm *xwm, return &xwm->clipboard_selection; } else if (selection_atom == xwm->atoms[PRIMARY]) { return &xwm->primary_selection; + } else if (selection_atom == xwm->atoms[DND_SELECTION]) { + return &xwm->dnd_selection; } else { return NULL; } @@ -856,28 +911,18 @@ void xwm_selection_init(struct wlr_xwm *xwm) { selection_init(xwm, &xwm->primary_selection, xwm->atoms[PRIMARY]); // Drag'n'drop - uint32_t dnd_values[] = { XCB_EVENT_MASK_PROPERTY_CHANGE }; - xwm->dnd_window = xcb_generate_id(xwm->xcb_conn); - xcb_create_window(xwm->xcb_conn, - XCB_COPY_FROM_PARENT, - xwm->dnd_window, - xwm->screen->root, - 0, 0, - 10, 10, - 0, - XCB_WINDOW_CLASS_INPUT_OUTPUT, - xwm->screen->root_visual, - XCB_CW_EVENT_MASK, dnd_values); - uint32_t version = XDND_VERSION; xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->dnd_window, + xwm->selection_window, xwm->atoms[DND_AWARE], XCB_ATOM_ATOM, 32, 1, &version); + + selection_init(xwm, &xwm->dnd_selection, xwm->atoms[DND_SELECTION]); + wlr_log(L_DEBUG, "DND_SELECTION=%d", xwm->atoms[DND_SELECTION]); } void xwm_selection_finish(struct wlr_xwm *xwm) { @@ -887,9 +932,6 @@ void xwm_selection_finish(struct wlr_xwm *xwm) { if (xwm->selection_window) { xcb_destroy_window(xwm->xcb_conn, xwm->selection_window); } - if (xwm->dnd_window) { - xcb_destroy_window(xwm->xcb_conn, xwm->dnd_window); - } if (xwm->seat) { if (xwm->seat->selection_data_source && xwm->seat->selection_data_source->cancel == data_source_cancel) { @@ -903,7 +945,6 @@ void xwm_selection_finish(struct wlr_xwm *xwm) { } wlr_xwayland_set_seat(xwm->xwayland, NULL); } - } static void xwm_selection_set_owner(struct wlr_xwm_selection *selection, @@ -951,10 +992,50 @@ static void seat_handle_primary_selection(struct wl_listener *listener, xwm_selection_set_owner(&xwm->primary_selection, source != NULL); } +static void seat_handle_drag_focus(struct wl_listener *listener, void *data) { + struct wlr_drag *drag = data; + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_focus); + + // TODO: check for subsurfaces? + bool found = false; + struct wlr_xwayland_surface *surface; + wl_list_for_each(surface, &xwm->surfaces, link) { + if (surface->surface == drag->focus) { + found = true; + break; + } + } + if (!found) { + return; + } + + xwm_dnd_send_enter(xwm, drag, surface); +} + +static void seat_handle_drag_destroy(struct wl_listener *listener, void *data) { + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_destroy); + + wl_list_remove(&xwm->seat_drag_focus.link); + wl_list_remove(&xwm->seat_drag_destroy.link); +} + +static void seat_handle_start_drag(struct wl_listener *listener, void *data) { + struct wlr_drag *drag = data; + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_start_drag); + + xwm_selection_set_owner(&xwm->dnd_selection, drag != NULL); + + wl_signal_add(&drag->events.focus, &xwm->seat_drag_focus); + xwm->seat_drag_focus.notify = seat_handle_drag_focus; + wl_signal_add(&drag->events.destroy, &xwm->seat_drag_destroy); + xwm->seat_drag_destroy.notify = seat_handle_drag_destroy; +} + void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat) { if (xwm->seat != NULL) { wl_list_remove(&xwm->seat_selection.link); wl_list_remove(&xwm->seat_primary_selection.link); + wl_list_remove(&xwm->seat_start_drag.link); xwm->seat = NULL; } @@ -968,6 +1049,8 @@ void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat) { 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; + wl_signal_add(&seat->events.start_drag, &xwm->seat_start_drag); + xwm->seat_start_drag.notify = seat_handle_start_drag; seat_handle_selection(&xwm->seat_selection, seat); seat_handle_primary_selection(&xwm->seat_primary_selection, seat); -- cgit v1.2.3 From 73394deb761232ff6a5a12400fe9e9fc5ab51114 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 28 Mar 2018 14:16:14 -0400 Subject: xwayland: send DND_POSITION --- include/wlr/types/wlr_data_device.h | 7 +++ include/xwayland/xwm.h | 4 ++ types/wlr_data_device.c | 14 ++++-- xwayland/selection.c | 87 +++++++++++++++++++++++++++++++------ 4 files changed, 94 insertions(+), 18 deletions(-) (limited to 'include') diff --git a/include/wlr/types/wlr_data_device.h b/include/wlr/types/wlr_data_device.h index cefcc979..cd266795 100644 --- a/include/wlr/types/wlr_data_device.h +++ b/include/wlr/types/wlr_data_device.h @@ -107,10 +107,17 @@ struct wlr_drag { struct { struct wl_signal focus; + struct wl_signal motion; struct wl_signal destroy; } events; }; +struct wlr_drag_motion_event { + struct wlr_drag *drag; + uint32_t time; + double sx, sy; +}; + /** * Create a wl data device manager global for this display. */ diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index e672a2df..d4795d3f 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -122,6 +122,9 @@ struct wlr_xwm { struct wl_list surfaces; // wlr_xwayland_surface::link struct wl_list unpaired_surfaces; // wlr_xwayland_surface::unpaired_link + struct wlr_drag *drag; + struct wlr_xwayland_surface *drag_focus; + const xcb_query_extension_reply_t *xfixes; #ifdef WLR_HAS_XCB_ERRORS xcb_errors_context_t *errors_context; @@ -133,6 +136,7 @@ struct wlr_xwm { struct wl_listener seat_primary_selection; struct wl_listener seat_start_drag; struct wl_listener seat_drag_focus; + struct wl_listener seat_drag_motion; struct wl_listener seat_drag_destroy; }; diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index 8129b173..beb13eec 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -495,9 +495,6 @@ static void wlr_drag_end(struct wlr_drag *drag) { } } -const struct -wlr_pointer_grab_interface wlr_data_device_pointer_drag_interface; - static void pointer_drag_enter(struct wlr_seat_pointer_grab *grab, struct wlr_surface *surface, double sx, double sy) { struct wlr_drag *drag = grab->data; @@ -507,12 +504,20 @@ static void pointer_drag_enter(struct wlr_seat_pointer_grab *grab, static void pointer_drag_motion(struct wlr_seat_pointer_grab *grab, uint32_t time, double sx, double sy) { struct wlr_drag *drag = grab->data; - if (drag->focus != NULL&& drag->focus_client != NULL) { + if (drag->focus != NULL && drag->focus_client != NULL) { struct wl_resource *resource; wl_resource_for_each(resource, &drag->focus_client->data_devices) { wl_data_device_send_motion(resource, time, wl_fixed_from_double(sx), wl_fixed_from_double(sy)); } + + struct wlr_drag_motion_event event = { + .drag = drag, + .time = time, + .sx = sx, + .sy = sy, + }; + wlr_signal_emit_safe(&drag->events.motion, &event); } } @@ -739,6 +744,7 @@ static bool seat_client_start_drag(struct wlr_seat_client *client, } wl_signal_init(&drag->events.focus); + wl_signal_init(&drag->events.motion); wl_signal_init(&drag->events.destroy); struct wlr_seat *seat = client->seat; diff --git a/xwayland/selection.c b/xwayland/selection.c index 8e5905df..d1dfbdaa 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -12,7 +12,7 @@ static const size_t incr_chunk_size = 64 * 1024; -/*static xcb_atom_t data_device_manager_dnd_action_to_atom( +static xcb_atom_t data_device_manager_dnd_action_to_atom( struct wlr_xwm *xwm, enum wl_data_device_manager_dnd_action action) { if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) { return xwm->atoms[DND_ACTION_COPY]; @@ -24,7 +24,7 @@ static const size_t incr_chunk_size = 64 * 1024; return XCB_ATOM_NONE; } -static enum wl_data_device_manager_dnd_action +/*static enum wl_data_device_manager_dnd_action data_device_manager_dnd_action_from_atom(struct wlr_xwm *xwm, enum atom_name atom) { if (atom == xwm->atoms[DND_ACTION_COPY] || @@ -183,7 +183,8 @@ error_out: static void xwm_selection_source_send(struct wlr_xwm_selection *selection, const char *mime_type, int32_t fd) { - if (selection == &selection->xwm->clipboard_selection) { + if (selection == &selection->xwm->clipboard_selection || + selection == &selection->xwm->dnd_selection) { struct wlr_data_source *source = selection->xwm->seat->selection_data_source; if (source != NULL) { @@ -260,8 +261,9 @@ static xcb_atom_t xwm_get_mime_type_atom(struct wlr_xwm *xwm, char *mime_type) { return atom; } -static void xwm_dnd_send_enter(struct wlr_xwm *xwm, struct wlr_drag *drag, - struct wlr_xwayland_surface *dest) { +static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { + struct wlr_drag *drag = xwm->drag; + struct wlr_xwayland_surface *dest = xwm->drag_focus; struct wl_array *mime_types = &drag->source->mime_types; xcb_client_message_data_t data = { 0 }; @@ -316,9 +318,38 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm, struct wlr_drag *drag, (const char *)&event); } +static void xwm_dnd_send_position(struct wlr_xwm *xwm, uint32_t time, int16_t x, + int16_t y) { + struct wlr_drag *drag = xwm->drag; + struct wlr_xwayland_surface *dest = xwm->drag_focus; + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_selection.window; + data.data32[2] = (x << 16) | y; + data.data32[3] = time; + data.data32[4] = + data_device_manager_dnd_action_to_atom(xwm, drag->source->actions); + + xcb_client_message_event_t event = { + .response_type = XCB_CLIENT_MESSAGE, + .format = 32, + .sequence = 0, + .window = dest->window_id, + .type = xwm->atoms[DND_POSITION], + .data = data, + }; + + xcb_send_event(xwm->xcb_conn, + 0, // propagate + dest->window_id, + XCB_EVENT_MASK_NO_EVENT, + (const char *)&event); +} + static struct wl_array *xwm_selection_source_get_mime_types( struct wlr_xwm_selection *selection) { - if (selection == &selection->xwm->clipboard_selection) { + if (selection == &selection->xwm->clipboard_selection || + selection == &selection->xwm->dnd_selection) { struct wlr_data_source *source = selection->xwm->seat->selection_data_source; if (source != NULL) { @@ -414,7 +445,7 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, selection->flush_property_on_delete = 0; // No xwayland surface focused, deny access to clipboard - if (xwm->focus_surface == NULL) { + if (xwm->focus_surface == NULL && xwm->drag_focus == NULL) { wlr_log(L_DEBUG, "denying read access to clipboard: " "no xwayland surface focused"); xwm_selection_send_notify(selection, XCB_ATOM_NONE); @@ -714,7 +745,8 @@ 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) { + if (selection == &xwm->clipboard_selection || + selection == &xwm->dnd_selection) { struct x11_data_source *source = calloc(1, sizeof(struct x11_data_source)); if (source == NULL) { @@ -725,6 +757,8 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { source->base.send = data_source_send; source->base.cancel = data_source_cancel; + // TODO: DND + source->selection = selection; wl_array_init(&source->mime_types_atoms); @@ -816,6 +850,11 @@ static int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, } else if (selection == &xwm->primary_selection) { wlr_seat_set_primary_selection(xwm->seat, NULL, wl_display_next_serial(xwm->xwayland->wl_display)); + } else if (selection == &xwm->dnd_selection) { + // TODO: DND + } else { + wlr_log(L_DEBUG, "X11 selection has been cleared, but cannot " + "clear Wayland selection"); } } @@ -1009,7 +1048,21 @@ static void seat_handle_drag_focus(struct wl_listener *listener, void *data) { return; } - xwm_dnd_send_enter(xwm, drag, surface); + xwm->drag_focus = surface; + xwm_dnd_send_enter(xwm); +} + +static void seat_handle_drag_motion(struct wl_listener *listener, void *data) { + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_motion); + struct wlr_drag_motion_event *event = data; + struct wlr_xwayland_surface *surface = xwm->drag_focus; + + if (surface == NULL) { + return; // No xwayland surface focused + } + + xwm_dnd_send_position(xwm, event->time, surface->x + (int16_t)event->sx, + surface->y + (int16_t)event->sy); } static void seat_handle_drag_destroy(struct wl_listener *listener, void *data) { @@ -1017,6 +1070,7 @@ static void seat_handle_drag_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&xwm->seat_drag_focus.link); wl_list_remove(&xwm->seat_drag_destroy.link); + xwm->drag = NULL; } static void seat_handle_start_drag(struct wl_listener *listener, void *data) { @@ -1024,11 +1078,16 @@ static void seat_handle_start_drag(struct wl_listener *listener, void *data) { struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_start_drag); xwm_selection_set_owner(&xwm->dnd_selection, drag != NULL); - - wl_signal_add(&drag->events.focus, &xwm->seat_drag_focus); - xwm->seat_drag_focus.notify = seat_handle_drag_focus; - wl_signal_add(&drag->events.destroy, &xwm->seat_drag_destroy); - xwm->seat_drag_destroy.notify = seat_handle_drag_destroy; + xwm->drag = drag; + + if (drag != NULL) { + wl_signal_add(&drag->events.focus, &xwm->seat_drag_focus); + xwm->seat_drag_focus.notify = seat_handle_drag_focus; + wl_signal_add(&drag->events.motion, &xwm->seat_drag_motion); + xwm->seat_drag_motion.notify = seat_handle_drag_motion; + wl_signal_add(&drag->events.destroy, &xwm->seat_drag_destroy); + xwm->seat_drag_destroy.notify = seat_handle_drag_destroy; + } } void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat) { -- cgit v1.2.3 From 2a34b154e683bb962aed354c1d980a7c10c66824 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 28 Mar 2018 15:33:23 -0400 Subject: xwayland: send DND_DROP --- include/wlr/types/wlr_data_device.h | 6 ++ include/xwayland/xwm.h | 3 + types/wlr_data_device.c | 9 ++- xwayland/selection.c | 107 +++++++++++++++++++++++++++++++++++- xwayland/xwm.c | 2 +- 5 files changed, 123 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/wlr/types/wlr_data_device.h b/include/wlr/types/wlr_data_device.h index cd266795..6733980a 100644 --- a/include/wlr/types/wlr_data_device.h +++ b/include/wlr/types/wlr_data_device.h @@ -108,6 +108,7 @@ struct wlr_drag { struct { struct wl_signal focus; struct wl_signal motion; + struct wl_signal drop; struct wl_signal destroy; } events; }; @@ -118,6 +119,11 @@ struct wlr_drag_motion_event { double sx, sy; }; +struct wlr_drag_drop_event { + struct wlr_drag *drag; + uint32_t time; +}; + /** * Create a wl data device manager global for this display. */ diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index d4795d3f..b6b26227 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -137,6 +137,7 @@ struct wlr_xwm { struct wl_listener seat_start_drag; struct wl_listener seat_drag_focus; struct wl_listener seat_drag_motion; + struct wl_listener seat_drag_drop; struct wl_listener seat_drag_destroy; }; @@ -148,6 +149,8 @@ void xwm_set_cursor(struct wlr_xwm *xwm, const uint8_t *pixels, uint32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x, int32_t hotspot_y); int xwm_handle_selection_event(struct wlr_xwm *xwm, xcb_generic_event_t *event); +int xwm_handle_selection_client_message(struct wlr_xwm *xwm, + xcb_client_message_event_t *ev) ; void xwm_selection_init(struct wlr_xwm *xwm); void xwm_selection_finish(struct wlr_xwm *xwm); diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index beb13eec..8cc2b0e3 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -426,7 +426,6 @@ static void wlr_drag_set_focus(struct wlr_drag *drag, struct wlr_seat_client *focus_client = wlr_seat_client_for_wl_client(drag->seat_client->seat, wl_resource_get_client(surface->resource)); - if (!focus_client) { return; } @@ -541,6 +540,12 @@ static uint32_t pointer_drag_button(struct wlr_seat_pointer_grab *grab, drag->source->offer->in_ask = drag->source->current_dnd_action == WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK; + + struct wlr_drag_drop_event event = { + .drag = drag, + .time = time, + }; + wlr_signal_emit_safe(&drag->events.drop, &event); } else if (drag->source->dnd_finish) { drag->source->cancel(drag->source); } @@ -556,6 +561,7 @@ static uint32_t pointer_drag_button(struct wlr_seat_pointer_grab *grab, static void pointer_drag_axis(struct wlr_seat_pointer_grab *grab, uint32_t time, enum wlr_axis_orientation orientation, double value) { + // This space is intentionally left blank } static void pointer_drag_cancel(struct wlr_seat_pointer_grab *grab) { @@ -745,6 +751,7 @@ static bool seat_client_start_drag(struct wlr_seat_client *client, wl_signal_init(&drag->events.focus); wl_signal_init(&drag->events.motion); + wl_signal_init(&drag->events.drop); wl_signal_init(&drag->events.destroy); struct wlr_seat *seat = client->seat; diff --git a/xwayland/selection.c b/xwayland/selection.c index d1dfbdaa..1e68c997 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -183,8 +183,7 @@ error_out: static void xwm_selection_source_send(struct wlr_xwm_selection *selection, const char *mime_type, int32_t fd) { - if (selection == &selection->xwm->clipboard_selection || - selection == &selection->xwm->dnd_selection) { + if (selection == &selection->xwm->clipboard_selection) { struct wlr_data_source *source = selection->xwm->seat->selection_data_source; if (source != NULL) { @@ -198,6 +197,15 @@ static void xwm_selection_source_send(struct wlr_xwm_selection *selection, source->send(source, mime_type, fd); return; } + } else if (selection == &selection->xwm->dnd_selection) { + if (selection->xwm->seat->drag != NULL) { + struct wlr_data_source *source = + selection->xwm->seat->drag->source; + if (source != NULL) { + source->send(source, mime_type, fd); + return; + } + } } wlr_log(L_DEBUG, "not sending selection: no selection source available"); @@ -263,7 +271,9 @@ static xcb_atom_t xwm_get_mime_type_atom(struct wlr_xwm *xwm, char *mime_type) { static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); struct wl_array *mime_types = &drag->source->mime_types; xcb_client_message_data_t data = { 0 }; @@ -321,7 +331,9 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { static void xwm_dnd_send_position(struct wlr_xwm *xwm, uint32_t time, int16_t x, int16_t y) { struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); xcb_client_message_data_t data = { 0 }; data.data32[0] = xwm->dnd_selection.window; @@ -346,6 +358,63 @@ static void xwm_dnd_send_position(struct wlr_xwm *xwm, uint32_t time, int16_t x, (const char *)&event); } +static void xwm_dnd_send_drop(struct wlr_xwm *xwm, uint32_t time) { + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_selection.window; + data.data32[2] = time; + + xcb_client_message_event_t event = { + .response_type = XCB_CLIENT_MESSAGE, + .format = 32, + .sequence = 0, + .window = dest->window_id, + .type = xwm->atoms[DND_DROP], + .data = data, + }; + + xcb_send_event(xwm->xcb_conn, + 0, // propagate + dest->window_id, + XCB_EVENT_MASK_NO_EVENT, + (const char *)&event); +} + +/*static void xwm_dnd_send_finished(struct wlr_xwm *xwm) { + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_selection.window; + data.data32[1] = drag->source->accepted; + + if (drag->source->accepted) { + data.data32[2] = data_device_manager_dnd_action_to_atom(xwm, + drag->source->current_dnd_action); + } + + xcb_client_message_event_t event = { + .response_type = XCB_CLIENT_MESSAGE, + .format = 32, + .sequence = 0, + .window = dest->window_id, + .type = xwm->atoms[DND_FINISHED], + .data = data, + }; + + xcb_send_event(xwm->xcb_conn, + 0, // propagate + dest->window_id, + XCB_EVENT_MASK_NO_EVENT, + (const char *)&event); +}*/ + static struct wl_array *xwm_selection_source_get_mime_types( struct wlr_xwm_selection *selection) { if (selection == &selection->xwm->clipboard_selection || @@ -911,6 +980,26 @@ int xwm_handle_selection_event(struct wlr_xwm *xwm, return 0; } +int xwm_handle_selection_client_message(struct wlr_xwm *xwm, + xcb_client_message_event_t *ev) { + if (ev->type == xwm->atoms[DND_STATUS]) { + struct wlr_drag *drag = xwm->drag; + if (drag == NULL) { + return 0; + } + + // xcb_client_message_data_t *data = &ev->data; + // xcb_window_t target_window = data->data32[0]; + // bool accepted = data->data32[1] & 1; + // xcb_atom_t action = data->data32[4]; + + // TODO: drag->source->dnd_action(data_device_manager_dnd_action_from_atom(xwm, action)) + return 1; + } else { + return 0; + } +} + static void selection_init(struct wlr_xwm *xwm, struct wlr_xwm_selection *selection, xcb_atom_t atom) { selection->xwm = xwm; @@ -1065,6 +1154,18 @@ static void seat_handle_drag_motion(struct wl_listener *listener, void *data) { surface->y + (int16_t)event->sy); } +static void seat_handle_drag_drop(struct wl_listener *listener, void *data) { + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_drop); + struct wlr_drag_drop_event *event = data; + struct wlr_xwayland_surface *surface = xwm->drag_focus; + + if (surface == NULL) { + return; // No xwayland surface focused + } + + xwm_dnd_send_drop(xwm, event->time); +} + static void seat_handle_drag_destroy(struct wl_listener *listener, void *data) { struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_destroy); @@ -1085,6 +1186,8 @@ static void seat_handle_start_drag(struct wl_listener *listener, void *data) { xwm->seat_drag_focus.notify = seat_handle_drag_focus; wl_signal_add(&drag->events.motion, &xwm->seat_drag_motion); xwm->seat_drag_motion.notify = seat_handle_drag_motion; + wl_signal_add(&drag->events.drop, &xwm->seat_drag_drop); + xwm->seat_drag_drop.notify = seat_handle_drag_drop; wl_signal_add(&drag->events.destroy, &xwm->seat_drag_destroy); xwm->seat_drag_destroy.notify = seat_handle_drag_destroy; } diff --git a/xwayland/xwm.c b/xwayland/xwm.c index a1ac43d6..712e1602 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -943,7 +943,7 @@ static void xwm_handle_client_message(struct wlr_xwm *xwm, xwm_handle_net_wm_state_message(xwm, ev); } else if (ev->type == xwm->atoms[_NET_WM_MOVERESIZE]) { xwm_handle_net_wm_moveresize_message(xwm, ev); - } else { + } else if (!xwm_handle_selection_client_message(xwm, ev)) { wlr_log(L_DEBUG, "unhandled x11 client message %u", ev->type); } } -- cgit v1.2.3 From 6fd50947bda5c609bba0eea1fef5e27a5524dc38 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 28 Mar 2018 21:36:53 -0400 Subject: xwayland: improve error handling --- include/xwayland/xwm.h | 3 +- xwayland/selection.c | 119 +++++++++++++++++++++++++++++++------------------ xwayland/xwm.c | 6 +-- 3 files changed, 80 insertions(+), 48 deletions(-) (limited to 'include') diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index b6b26227..b85b5bae 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -150,13 +150,14 @@ void xwm_set_cursor(struct wlr_xwm *xwm, const uint8_t *pixels, uint32_t stride, int xwm_handle_selection_event(struct wlr_xwm *xwm, xcb_generic_event_t *event); int xwm_handle_selection_client_message(struct wlr_xwm *xwm, - xcb_client_message_event_t *ev) ; + xcb_client_message_event_t *ev); void xwm_selection_init(struct wlr_xwm *xwm); void xwm_selection_finish(struct wlr_xwm *xwm); void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat); +char *xwm_get_atom_name(struct wlr_xwm *xwm, xcb_atom_t atom); bool xwm_atoms_contains(struct wlr_xwm *xwm, xcb_atom_t *atoms, size_t num_atoms, enum atom_name needle); diff --git a/xwayland/selection.c b/xwayland/selection.c index 740fac73..d5872f83 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -54,7 +54,8 @@ static void xwm_selection_send_notify(struct wlr_xwm_selection *selection, 0, // propagate selection->request.requestor, XCB_EVENT_MASK_NO_EVENT, - (char *)&selection_notify); + (const char *)&selection_notify); + xcb_flush(selection->xwm->xcb_conn); } static int xwm_selection_flush_source_data(struct wlr_xwm_selection *selection) { @@ -211,8 +212,56 @@ static void xwm_selection_source_send(struct wlr_xwm_selection *selection, wlr_log(L_DEBUG, "not sending selection: no selection source available"); } +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_data_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; + } + } else if (selection == &selection->xwm->dnd_selection) { + if (selection->xwm->seat->drag != NULL && + selection->xwm->seat->drag->source != NULL) { + return &selection->xwm->seat->drag->source->mime_types; + } + } + return NULL; +} + static void xwm_selection_send_data(struct wlr_xwm_selection *selection, xcb_atom_t target, const char *mime_type) { + // Check MIME type + struct wl_array *mime_types = + xwm_selection_source_get_mime_types(selection); + if (mime_types == NULL) { + wlr_log(L_ERROR, "not sending selection: no MIME type list available"); + xwm_selection_send_notify(selection, XCB_ATOM_NONE); + return; + } + + bool found = false; + char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, mime_types) { + char *t = *mime_type_ptr; + if (strcmp(t, mime_type) == 0) { + found = true; + break; + } + } + if (!found) { + wlr_log(L_ERROR, "not sending selection: " + "requested an unsupported MIME type %s", mime_type); + xwm_selection_send_notify(selection, XCB_ATOM_NONE); + return; + } + int p[2]; if (pipe(p) == -1) { wlr_log(L_ERROR, "pipe failed: %m"); @@ -300,6 +349,8 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { data.data32[0] = xwm->dnd_selection.window; data.data32[1] = XDND_VERSION << 24; + // If we have 3 MIME types or less, we can send them directly in the + // DND_ENTER message size_t n = mime_types->size / sizeof(char *); if (n <= 3) { size_t i = 0; @@ -393,31 +444,12 @@ static void xwm_dnd_send_leave(struct wlr_xwm *xwm) { xwm_dnd_send_event(xwm, xwm->atoms[DND_FINISHED], &data); }*/ -static struct wl_array *xwm_selection_source_get_mime_types( - struct wlr_xwm_selection *selection) { - if (selection == &selection->xwm->clipboard_selection || - selection == &selection->xwm->dnd_selection) { - struct wlr_data_source *source = - selection->xwm->seat->selection_data_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_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: " + wlr_log(L_ERROR, "not sending selection targets: " "no selection source available"); xwm_selection_send_notify(selection, XCB_ATOM_NONE); return; @@ -493,8 +525,10 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, // No xwayland surface focused, deny access to clipboard if (xwm->focus_surface == NULL && xwm->drag_focus == NULL) { - wlr_log(L_DEBUG, "denying read access to clipboard: " - "no xwayland surface focused"); + char *selection_name = xwm_get_atom_name(xwm, selection->atom); + wlr_log(L_DEBUG, "denying read access to selection %u (%s): " + "no xwayland surface focused", selection->atom, selection_name); + free(selection_name); xwm_selection_send_notify(selection, XCB_ATOM_NONE); return; } @@ -510,26 +544,15 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, 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); - xcb_get_atom_name_reply_t *name_reply = - 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_selection_send_notify(selection, XCB_ATOM_NONE); - return; - } - size_t len = xcb_get_atom_name_name_length(name_reply); - char *mime_type = malloc((len + 1) * sizeof(char)); + char *mime_type = xwm_get_atom_name(xwm, selection_request->target); if (mime_type == NULL) { - free(name_reply); + wlr_log(L_ERROR, "ignoring selection request: unknown atom"); + xwm_selection_send_notify(selection, XCB_ATOM_NONE); return; } - memcpy(mime_type, xcb_get_atom_name_name(name_reply), len); - mime_type[len] = '\0'; - xwm_selection_send_data(selection, selection_request->target, mime_type); + xwm_selection_send_data(selection, selection_request->target, + mime_type); free(mime_type); - free(name_reply); } } @@ -961,10 +984,9 @@ int xwm_handle_selection_event(struct wlr_xwm *xwm, int xwm_handle_selection_client_message(struct wlr_xwm *xwm, xcb_client_message_event_t *ev) { if (ev->type == xwm->atoms[DND_STATUS]) { - struct wlr_drag *drag = xwm->drag; - if (drag == NULL) { - wlr_log(L_DEBUG, "Ignoring XdndStatus client message because " - "there's no current drag"); + if (xwm->drag == NULL) { + wlr_log(L_DEBUG, "ignoring XdndStatus client message because " + "there's no drag"); return 1; } @@ -973,9 +995,18 @@ int xwm_handle_selection_client_message(struct wlr_xwm *xwm, bool accepted = data->data32[1] & 1; xcb_atom_t action_atom = data->data32[4]; + if (xwm->drag_focus == NULL || + target_window != xwm->drag_focus->window_id) { + wlr_log(L_DEBUG, "ignoring XdndStatus client message because " + "it doesn't match the current drag focus window ID"); + return 1; + } + enum wl_data_device_manager_dnd_action action = data_device_manager_dnd_action_from_atom(xwm, action_atom); + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); drag->source->accepted = accepted; drag->source->current_dnd_action = action; @@ -1038,7 +1069,6 @@ void xwm_selection_init(struct wlr_xwm *xwm) { &version); selection_init(xwm, &xwm->dnd_selection, xwm->atoms[DND_SELECTION]); - wlr_log(L_DEBUG, "DND_SELECTION=%d", xwm->atoms[DND_SELECTION]); } void xwm_selection_finish(struct wlr_xwm *xwm) { @@ -1182,6 +1212,7 @@ static void seat_handle_start_drag(struct wl_listener *listener, void *data) { xwm_selection_set_owner(&xwm->dnd_selection, drag != NULL); xwm->drag = drag; + xwm->drag_focus = NULL; if (drag != NULL) { wl_signal_add(&drag->events.focus, &xwm->seat_drag_focus); diff --git a/xwayland/xwm.c b/xwayland/xwm.c index f6668796..82210ce0 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -506,7 +506,7 @@ static void read_surface_net_wm_state(struct wlr_xwm *xwm, } } -static char *get_atom_name(struct wlr_xwm *xwm, xcb_atom_t atom) { +char *xwm_get_atom_name(struct wlr_xwm *xwm, xcb_atom_t atom) { xcb_get_atom_name_cookie_t name_cookie = xcb_get_atom_name(xwm->xcb_conn, atom); xcb_get_atom_name_reply_t *name_reply = @@ -558,7 +558,7 @@ static void read_surface_property(struct wlr_xwm *xwm, } else if (property == xwm->atoms[MOTIF_WM_HINTS]) { read_surface_motif_hints(xwm, xsurface, reply); } else { - char *prop_name = get_atom_name(xwm, property); + char *prop_name = xwm_get_atom_name(xwm, property); wlr_log(L_DEBUG, "unhandled X11 property %u (%s) for window %u", property, prop_name, xsurface->window_id); free(prop_name); @@ -967,7 +967,7 @@ static void xwm_handle_client_message(struct wlr_xwm *xwm, } else if (ev->type == xwm->atoms[_NET_WM_MOVERESIZE]) { xwm_handle_net_wm_moveresize_message(xwm, ev); } else if (!xwm_handle_selection_client_message(xwm, ev)) { - char *type_name = get_atom_name(xwm, ev->type); + char *type_name = xwm_get_atom_name(xwm, ev->type); wlr_log(L_DEBUG, "unhandled x11 client message %u (%s)", ev->type, type_name); free(type_name); -- cgit v1.2.3 From 743466d475b744d818584db7f4040fd479c3f27e Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 29 Mar 2018 11:33:40 -0400 Subject: data-device: add seat.drag_source --- include/wlr/types/wlr_seat.h | 7 +++-- types/wlr_data_device.c | 66 +++++++++++++++++++++++++++----------------- types/wlr_seat.c | 8 +++--- 3 files changed, 50 insertions(+), 31 deletions(-) (limited to 'include') diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index 435a3e7e..34105cf0 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -181,13 +181,15 @@ struct wlr_seat { uint32_t capabilities; struct timespec last_event; - struct wlr_data_source *selection_data_source; + struct wlr_data_source *selection_source; uint32_t selection_serial; struct wlr_primary_selection_source *primary_selection_source; uint32_t primary_selection_serial; + // `drag` goes away before `drag_source`, when the implicit grab ends struct wlr_drag *drag; + struct wlr_data_source *drag_source; uint32_t drag_serial; struct wlr_seat_pointer_state pointer_state; @@ -195,8 +197,9 @@ struct wlr_seat { struct wlr_seat_touch_state touch_state; struct wl_listener display_destroy; - struct wl_listener selection_data_source_destroy; + struct wl_listener selection_source_destroy; struct wl_listener primary_selection_source_destroy; + struct wl_listener drag_source_destroy; struct { struct wl_signal pointer_grab_begin; diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index f61ef07e..3e78760b 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -275,9 +275,9 @@ void wlr_seat_client_send_selection(struct wlr_seat_client *seat_client) { return; } - if (seat_client->seat->selection_data_source) { + if (seat_client->seat->selection_source) { struct wlr_data_offer *offer = wlr_data_source_send_offer( - seat_client->seat->selection_data_source, seat_client); + seat_client->seat->selection_source, seat_client); if (offer == NULL) { return; } @@ -294,10 +294,10 @@ void wlr_seat_client_send_selection(struct wlr_seat_client *seat_client) { } } -static void seat_client_selection_data_source_destroy( +static void seat_client_selection_source_destroy( struct wl_listener *listener, void *data) { struct wlr_seat *seat = - wl_container_of(listener, seat, selection_data_source_destroy); + wl_container_of(listener, seat, selection_source_destroy); struct wlr_seat_client *seat_client = seat->keyboard_state.focused_client; if (seat_client && seat->keyboard_state.focused_surface) { @@ -307,7 +307,7 @@ static void seat_client_selection_data_source_destroy( } } - seat->selection_data_source = NULL; + seat->selection_source = NULL; wlr_signal_emit_safe(&seat->events.selection, seat); } @@ -319,18 +319,18 @@ void wlr_seat_set_selection(struct wlr_seat *seat, assert(source->cancel); } - if (seat->selection_data_source && + if (seat->selection_source && seat->selection_serial - serial < UINT32_MAX / 2) { return; } - if (seat->selection_data_source) { - seat->selection_data_source->cancel(seat->selection_data_source); - seat->selection_data_source = NULL; - wl_list_remove(&seat->selection_data_source_destroy.link); + if (seat->selection_source) { + seat->selection_source->cancel(seat->selection_source); + seat->selection_source = NULL; + wl_list_remove(&seat->selection_source_destroy.link); } - seat->selection_data_source = source; + seat->selection_source = source; seat->selection_serial = serial; struct wlr_seat_client *focused_client = @@ -343,10 +343,10 @@ void wlr_seat_set_selection(struct wlr_seat *seat, wlr_signal_emit_safe(&seat->events.selection, seat); if (source) { - seat->selection_data_source_destroy.notify = - seat_client_selection_data_source_destroy; + seat->selection_source_destroy.notify = + seat_client_selection_source_destroy; wl_signal_add(&source->events.destroy, - &seat->selection_data_source_destroy); + &seat->selection_source_destroy); } } @@ -571,8 +571,8 @@ static void pointer_drag_cancel(struct wlr_seat_pointer_grab *grab) { wlr_drag_end(drag); } -const struct -wlr_pointer_grab_interface wlr_data_device_pointer_drag_interface = { +static const struct wlr_pointer_grab_interface + data_device_pointer_drag_interface = { .enter = pointer_drag_enter, .motion = pointer_drag_motion, .button = pointer_drag_button, @@ -627,7 +627,8 @@ static void touch_drag_cancel(struct wlr_seat_touch_grab *grab) { wlr_drag_end(drag); } -const struct wlr_touch_grab_interface wlr_data_device_touch_drag_interface = { +static const struct wlr_touch_grab_interface + data_device_touch_drag_interface = { .down = touch_drag_down, .up = touch_drag_up, .motion = touch_drag_motion, @@ -658,8 +659,8 @@ static void keyboard_drag_cancel(struct wlr_seat_keyboard_grab *grab) { wlr_drag_end(drag); } -const struct -wlr_keyboard_grab_interface wlr_data_device_keyboard_drag_interface = { +static const struct wlr_keyboard_grab_interface + data_device_keyboard_drag_interface = { .enter = keyboard_drag_enter, .key = keyboard_drag_key, .modifiers = keyboard_drag_modifiers, @@ -743,6 +744,14 @@ static struct wlr_drag_icon *wlr_drag_icon_create( return icon; } +static void seat_handle_drag_source_destroy(struct wl_listener *listener, + void *data) { + struct wlr_seat *seat = + wl_container_of(listener, seat, drag_source_destroy); + wl_list_remove(&seat->drag_source_destroy.link); + seat->drag_source = NULL; +} + static bool seat_client_start_drag(struct wlr_seat_client *client, struct wlr_data_source *source, struct wlr_surface *icon_surface, struct wlr_surface *origin, uint32_t serial) { @@ -798,22 +807,22 @@ static bool seat_client_start_drag(struct wlr_seat_client *client, wl_signal_add(&icon->events.destroy, &drag->icon_destroy); } - if (source) { + drag->source = source; + if (source != NULL) { drag->source_destroy.notify = drag_handle_drag_source_destroy; wl_signal_add(&source->events.destroy, &drag->source_destroy); - drag->source = source; } drag->seat_client = client; drag->pointer_grab.data = drag; - drag->pointer_grab.interface = &wlr_data_device_pointer_drag_interface; + drag->pointer_grab.interface = &data_device_pointer_drag_interface; drag->touch_grab.data = drag; - drag->touch_grab.interface = &wlr_data_device_touch_drag_interface; + drag->touch_grab.interface = &data_device_touch_drag_interface; drag->grab_touch_id = seat->touch_state.grab_id; drag->keyboard_grab.data = drag; - drag->keyboard_grab.interface = &wlr_data_device_keyboard_drag_interface; + drag->keyboard_grab.interface = &data_device_keyboard_drag_interface; wlr_seat_keyboard_start_grab(seat, &drag->keyboard_grab); @@ -828,6 +837,13 @@ static bool seat_client_start_drag(struct wlr_seat_client *client, seat->drag = drag; // TODO: unset this thing somewhere seat->drag_serial = serial; + + seat->drag_source = source; + if (source != NULL) { + seat->drag_source_destroy.notify = seat_handle_drag_source_destroy; + wl_signal_add(&source->events.destroy, &seat->drag_source_destroy); + } + wlr_signal_emit_safe(&seat->events.start_drag, drag); return true; @@ -1069,7 +1085,7 @@ static void data_device_manager_create_data_source(struct wl_client *client, } static const struct wl_data_device_manager_interface -data_device_manager_impl = { + data_device_manager_impl = { .create_data_source = data_device_manager_create_data_source, .get_data_device = data_device_manager_get_data_device, }; diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 53659f90..ed7fb470 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -365,10 +365,10 @@ void wlr_seat_destroy(struct wlr_seat *seat) { wl_list_remove(&seat->display_destroy.link); - if (seat->selection_data_source) { - seat->selection_data_source->cancel(seat->selection_data_source); - seat->selection_data_source = NULL; - wl_list_remove(&seat->selection_data_source_destroy.link); + if (seat->selection_source) { + seat->selection_source->cancel(seat->selection_source); + seat->selection_source = NULL; + wl_list_remove(&seat->selection_source_destroy.link); } if (seat->primary_selection_source) { seat->primary_selection_source->cancel(seat->primary_selection_source); -- cgit v1.2.3 From ca2a73b90d98a70d1a175cedf00c40bf7fcdf204 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 29 Mar 2018 11:40:19 -0400 Subject: xwayland: allow drag data source transfer after drag ends --- include/xwayland/xwm.h | 1 + xwayland/selection.c | 51 +++++++++++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 19 deletions(-) (limited to 'include') diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index b85b5bae..d36f9ac0 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -139,6 +139,7 @@ struct wlr_xwm { struct wl_listener seat_drag_motion; struct wl_listener seat_drag_drop; struct wl_listener seat_drag_destroy; + struct wl_listener seat_drag_source_destroy; }; struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland); diff --git a/xwayland/selection.c b/xwayland/selection.c index fa7e478b..f56b39b5 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -186,7 +186,7 @@ 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_data_source; + selection->xwm->seat->selection_source; if (source != NULL) { source->send(source, mime_type, fd); return; @@ -199,13 +199,11 @@ static void xwm_selection_source_send(struct wlr_xwm_selection *selection, return; } } else if (selection == &selection->xwm->dnd_selection) { - if (selection->xwm->seat->drag != NULL) { - struct wlr_data_source *source = - selection->xwm->seat->drag->source; - if (source != NULL) { - source->send(source, mime_type, fd); - return; - } + struct wlr_data_source *source = + selection->xwm->seat->drag_source; + if (source != NULL) { + source->send(source, mime_type, fd); + return; } } @@ -216,7 +214,7 @@ 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_data_source; + selection->xwm->seat->selection_source; if (source != NULL) { return &source->mime_types; } @@ -227,9 +225,10 @@ static struct wl_array *xwm_selection_source_get_mime_types( return &source->mime_types; } } else if (selection == &selection->xwm->dnd_selection) { - if (selection->xwm->seat->drag != NULL && - selection->xwm->seat->drag->source != NULL) { - return &selection->xwm->seat->drag->source->mime_types; + struct wlr_data_source *source = + selection->xwm->seat->drag_source; + if (source != NULL) { + return &source->mime_types; } } return NULL; @@ -815,8 +814,7 @@ 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 || - selection == &xwm->dnd_selection) { + if (selection == &xwm->clipboard_selection) { struct x11_data_source *source = calloc(1, sizeof(struct x11_data_source)); if (source == NULL) { @@ -827,8 +825,6 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { source->base.send = data_source_send; source->base.cancel = data_source_cancel; - // TODO: DND - source->selection = selection; wl_array_init(&source->mime_types_atoms); @@ -861,6 +857,8 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { } else { source->base.cancel(&source->base); } + } else if (selection == &xwm->dnd_selection) { + // TODO } } @@ -1082,8 +1080,8 @@ void xwm_selection_finish(struct wlr_xwm *xwm) { xcb_destroy_window(xwm->xcb_conn, xwm->selection_window); } if (xwm->seat) { - if (xwm->seat->selection_data_source && - xwm->seat->selection_data_source->cancel == data_source_cancel) { + if (xwm->seat->selection_source && + xwm->seat->selection_source->cancel == data_source_cancel) { wlr_seat_set_selection(xwm->seat, NULL, wl_display_next_serial(xwm->xwayland->wl_display)); } @@ -1118,7 +1116,7 @@ static void seat_handle_selection(struct wl_listener *listener, struct wlr_seat *seat = data; struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_selection); - struct wlr_data_source *source = seat->selection_data_source; + struct wlr_data_source *source = seat->selection_source; if (source != NULL && source->send == data_source_send) { return; @@ -1211,10 +1209,21 @@ static void seat_handle_drag_destroy(struct wl_listener *listener, void *data) { } wl_list_remove(&xwm->seat_drag_focus.link); + wl_list_remove(&xwm->seat_drag_motion.link); + wl_list_remove(&xwm->seat_drag_drop.link); wl_list_remove(&xwm->seat_drag_destroy.link); xwm->drag = NULL; } +static void seat_handle_drag_source_destroy(struct wl_listener *listener, + void *data) { + struct wlr_xwm *xwm = + wl_container_of(listener, xwm, seat_drag_source_destroy); + + wl_list_remove(&xwm->seat_drag_source_destroy.link); + xwm->drag_focus = NULL; +} + static void seat_handle_start_drag(struct wl_listener *listener, void *data) { struct wlr_drag *drag = data; struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_start_drag); @@ -1232,6 +1241,10 @@ static void seat_handle_start_drag(struct wl_listener *listener, void *data) { xwm->seat_drag_drop.notify = seat_handle_drag_drop; wl_signal_add(&drag->events.destroy, &xwm->seat_drag_destroy); xwm->seat_drag_destroy.notify = seat_handle_drag_destroy; + + wl_signal_add(&drag->source->events.destroy, + &xwm->seat_drag_source_destroy); + xwm->seat_drag_source_destroy.notify = seat_handle_drag_source_destroy; } } -- cgit v1.2.3 From d5f46f4db4b646faad33f91b1677e46665871e6e Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 29 Mar 2018 17:53:13 -0400 Subject: data-device: redesign wlr_data_source --- include/wlr/types/wlr_data_device.h | 82 ++++++++++++++-- types/wlr_data_device.c | 180 ++++++++++++++++++++++++------------ types/wlr_seat.c | 4 +- 3 files changed, 193 insertions(+), 73 deletions(-) (limited to 'include') diff --git a/include/wlr/types/wlr_data_device.h b/include/wlr/types/wlr_data_device.h index 6733980a..6fb41c29 100644 --- a/include/wlr/types/wlr_data_device.h +++ b/include/wlr/types/wlr_data_device.h @@ -30,23 +30,29 @@ struct wlr_data_offer { struct wl_listener source_destroy; }; -struct wlr_data_source { - // source metadata - struct wl_array mime_types; - int32_t actions; - - // source implementation +/** + * A data source implementation. Only the `send` function is mandatory. Refer to + * the matching wl_data_source_* functions documentation to know what they do. + */ +struct wlr_data_source_impl { void (*send)(struct wlr_data_source *source, const char *mime_type, int32_t fd); void (*accept)(struct wlr_data_source *source, uint32_t serial, const char *mime_type); void (*cancel)(struct wlr_data_source *source); - // drag'n'drop implementation void (*dnd_drop)(struct wlr_data_source *source); void (*dnd_finish)(struct wlr_data_source *source); void (*dnd_action)(struct wlr_data_source *source, enum wl_data_device_manager_dnd_action action); +}; + +struct wlr_data_source { + const struct wlr_data_source_impl *impl; + + // source metadata + struct wl_array mime_types; + int32_t actions; // source status bool accepted; @@ -128,7 +134,7 @@ struct wlr_drag_drop_event { * Create a wl data device manager global for this display. */ struct wlr_data_device_manager *wlr_data_device_manager_create( - struct wl_display *display); + struct wl_display *display); /** * Destroys a wlr_data_device_manager and removes its wl_data_device_manager global. @@ -144,11 +150,67 @@ void wlr_data_device_manager_destroy(struct wlr_data_device_manager *manager); */ void wlr_seat_client_send_selection(struct wlr_seat_client *seat_client); +/** + * Sets the current selection for the seat. This removes the previous one if + * there was any. + */ void wlr_seat_set_selection(struct wlr_seat *seat, - struct wlr_data_source *source, uint32_t serial); + struct wlr_data_source *source, uint32_t serial); -void wlr_data_source_init(struct wlr_data_source *source); +/** + * Initializes the data source with the provided implementation. + */ +void wlr_data_source_init(struct wlr_data_source *source, + const struct wlr_data_source_impl *impl); +/** + * Finishes the data source. + */ void wlr_data_source_finish(struct wlr_data_source *source); +/** + * Sends the data as the specified MIME type over the passed file descriptor, + * then close it. + */ +void wlr_data_source_send(struct wlr_data_source *source, const char *mime_type, + int32_t fd); + +/** + * Notifies the data source that a target accepts one of the offered MIME types. + * If a target doesn't accept any of the offered types, `mime_type` is NULL. + */ +void wlr_data_source_accept(struct wlr_data_source *source, uint32_t serial, + const char *mime_type); + +/** + * Notifies the data source it is no longer valid and should be destroyed. That + * potentially destroys immediately the data source. + */ +void wlr_data_source_cancel(struct wlr_data_source *source); + +/** + * Notifies the data source that the drop operation was performed. This does not + * indicate acceptance. + * + * The data source may still be used in the future and should not be destroyed + * here. + */ +void wlr_data_source_dnd_drop(struct wlr_data_source *source); + +/** + * Notifies the data source that the drag-and-drop operation concluded. That + * potentially destroys immediately the data source. + */ +void wlr_data_source_dnd_finish(struct wlr_data_source *source); + +/** + * Notifies the data source that a target accepts the drag with the specified + * action. + * + * This shouldn't be called after `wlr_data_source_dnd_drop` unless the + * drag-and-drop operation ended in an "ask" action. + */ +void wlr_data_source_dnd_action(struct wlr_data_source *source, + enum wl_data_device_manager_dnd_action action); + #endif diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index 3e78760b..13b1d08a 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -84,9 +84,7 @@ static void data_offer_update_action(struct wlr_data_offer *offer) { return; } - if (offer->source->dnd_action) { - offer->source->dnd_action(offer->source, action); - } + wlr_data_source_dnd_action(offer->source, action); if (wl_resource_get_version(offer->resource) >= WL_DATA_OFFER_ACTION_SINCE_VERSION) { @@ -104,10 +102,7 @@ static void data_offer_accept(struct wl_client *client, // TODO check that client is currently focused by the input device - if (offer->source->accept) { - offer->source->accept(offer->source, serial, mime_type); - } - offer->source->accepted = (mime_type != NULL); + wlr_data_source_accept(offer->source, serial, mime_type); } static void data_offer_receive(struct wl_client *client, @@ -115,7 +110,7 @@ static void data_offer_receive(struct wl_client *client, struct wlr_data_offer *offer = data_offer_from_resource(resource); if (offer->source && offer == offer->source->offer) { - offer->source->send(offer->source, mime_type, fd); + wlr_data_source_send(offer->source, mime_type, fd); } else { close(fd); } @@ -131,15 +126,12 @@ static void data_source_notify_finish(struct wlr_data_source *source) { return; } - if (source->offer->in_ask && source->dnd_action) { - source->dnd_action(source, source->current_dnd_action); - } - - if (source->dnd_finish) { - source->dnd_finish(source); + if (source->offer->in_ask) { + wlr_data_source_dnd_action(source, source->current_dnd_action); } source->offer = NULL; + wlr_data_source_dnd_finish(source); } static void data_offer_finish(struct wl_client *client, @@ -199,10 +191,10 @@ static void data_offer_resource_destroy(struct wl_resource *resource) { WL_DATA_OFFER_ACTION_SINCE_VERSION) { data_source_notify_finish(offer->source); offer->source->offer = NULL; - } else if (offer->source->dnd_finish) { + } else if (offer->source->impl->dnd_finish) { // source->cancel can free the source offer->source->offer = NULL; - offer->source->cancel(offer->source); + wlr_data_source_cancel(offer->source); } else { offer->source->offer = NULL; } @@ -314,20 +306,15 @@ static void seat_client_selection_source_destroy( void wlr_seat_set_selection(struct wlr_seat *seat, struct wlr_data_source *source, uint32_t serial) { - if (source) { - assert(source->send); - assert(source->cancel); - } - if (seat->selection_source && seat->selection_serial - serial < UINT32_MAX / 2) { return; } if (seat->selection_source) { - seat->selection_source->cancel(seat->selection_source); - seat->selection_source = NULL; wl_list_remove(&seat->selection_source_destroy.link); + wlr_data_source_cancel(seat->selection_source); + seat->selection_source = NULL; } seat->selection_source = source; @@ -533,9 +520,7 @@ static uint32_t pointer_drag_button(struct wlr_seat_pointer_grab *grab, wl_resource_for_each(resource, &drag->focus_client->data_devices) { wl_data_device_send_drop(resource); } - if (drag->source->dnd_drop) { - drag->source->dnd_drop(drag->source); - } + wlr_data_source_dnd_drop(drag->source); if (drag->source->offer != NULL) { drag->source->offer->in_ask = @@ -548,8 +533,8 @@ static uint32_t pointer_drag_button(struct wlr_seat_pointer_grab *grab, .time = time, }; wlr_signal_emit_safe(&drag->events.drop, &event); - } else if (drag->source->dnd_finish) { - drag->source->cancel(drag->source); + } else if (drag->source->impl->dnd_finish) { + wlr_data_source_cancel(drag->source); } } @@ -899,36 +884,51 @@ static void data_device_destroy(struct wl_resource *resource) { struct client_data_source { struct wlr_data_source source; + struct wlr_data_source_impl impl; struct wl_resource *resource; }; +static void client_data_source_accept(struct wlr_data_source *wlr_source, + uint32_t serial, const char *mime_type); + +static struct client_data_source *client_data_source_from_wlr_data_source( + struct wlr_data_source *wlr_source) { + assert(wlr_source->impl->accept == client_data_source_accept); + return (struct client_data_source *)wlr_source; +} + static void client_data_source_accept(struct wlr_data_source *wlr_source, uint32_t serial, const char *mime_type) { - struct client_data_source *source = (struct client_data_source *)wlr_source; + struct client_data_source *source = + client_data_source_from_wlr_data_source(wlr_source); wl_data_source_send_target(source->resource, mime_type); } static void client_data_source_send(struct wlr_data_source *wlr_source, const char *mime_type, int32_t fd) { - struct client_data_source *source = (struct client_data_source *)wlr_source; + struct client_data_source *source = + client_data_source_from_wlr_data_source(wlr_source); wl_data_source_send_send(source->resource, mime_type, fd); close(fd); } static void client_data_source_cancel(struct wlr_data_source *wlr_source) { - struct client_data_source *source = (struct client_data_source *)wlr_source; + struct client_data_source *source = + client_data_source_from_wlr_data_source(wlr_source); wl_data_source_send_cancelled(source->resource); } static void client_data_source_dnd_drop(struct wlr_data_source *wlr_source) { - struct client_data_source *source = (struct client_data_source *)wlr_source; + struct client_data_source *source = + client_data_source_from_wlr_data_source(wlr_source); assert(wl_resource_get_version(source->resource) >= WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION); wl_data_source_send_dnd_drop_performed(source->resource); } static void client_data_source_dnd_finish(struct wlr_data_source *wlr_source) { - struct client_data_source *source = (struct client_data_source *)wlr_source; + struct client_data_source *source = + client_data_source_from_wlr_data_source(wlr_source); assert(wl_resource_get_version(source->resource) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION); wl_data_source_send_dnd_finished(source->resource); @@ -936,7 +936,8 @@ static void client_data_source_dnd_finish(struct wlr_data_source *wlr_source) { static void client_data_source_dnd_action(struct wlr_data_source *wlr_source, enum wl_data_device_manager_dnd_action action) { - struct client_data_source *source = (struct client_data_source *)wlr_source; + struct client_data_source *source = + client_data_source_from_wlr_data_source(wlr_source); assert(wl_resource_get_version(source->resource) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION); wl_data_source_send_action(source->resource, action); @@ -947,6 +948,37 @@ static void data_source_destroy(struct wl_client *client, wl_resource_destroy(resource); } +static struct client_data_source *client_data_source_create( + struct wl_resource *source_resource) { + struct client_data_source *source = + calloc(1, sizeof(struct client_data_source)); + if (source == NULL) { + return NULL; + } + + source->resource = source_resource; + + source->impl.accept = client_data_source_accept; + source->impl.send = client_data_source_send; + source->impl.cancel = client_data_source_cancel; + + if (wl_resource_get_version(source->resource) >= + WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) { + source->impl.dnd_drop = client_data_source_dnd_drop; + } + if (wl_resource_get_version(source->resource) >= + WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { + source->impl.dnd_finish = client_data_source_dnd_finish; + } + if (wl_resource_get_version(source->resource) >= + WL_DATA_SOURCE_ACTION_SINCE_VERSION) { + source->impl.dnd_action = client_data_source_dnd_action; + } + + wlr_data_source_init(&source->source, &source->impl); + return source; +} + static void data_source_set_actions(struct wl_client *client, struct wl_resource *resource, uint32_t dnd_actions) { struct client_data_source *source = @@ -1007,7 +1039,11 @@ static void data_source_resource_handle_destroy(struct wl_resource *resource) { free(source); } -void wlr_data_source_init(struct wlr_data_source *source) { +void wlr_data_source_init(struct wlr_data_source *source, + const struct wlr_data_source_impl *impl) { + assert(impl->send); + + source->impl = impl; wl_array_init(&source->mime_types); wl_signal_init(&source->events.destroy); source->actions = -1; @@ -1027,6 +1063,45 @@ void wlr_data_source_finish(struct wlr_data_source *source) { wl_array_release(&source->mime_types); } +void wlr_data_source_send(struct wlr_data_source *source, const char *mime_type, + int32_t fd) { + source->impl->send(source, mime_type, fd); +} + +void wlr_data_source_accept(struct wlr_data_source *source, uint32_t serial, + const char *mime_type) { + source->accepted = (mime_type != NULL); + if (source->impl->accept) { + source->impl->accept(source, serial, mime_type); + } +} + +void wlr_data_source_cancel(struct wlr_data_source *source) { + if (source->impl->cancel) { + source->impl->cancel(source); + } +} + +void wlr_data_source_dnd_drop(struct wlr_data_source *source) { + if (source->impl->dnd_drop) { + source->impl->dnd_drop(source); + } +} + +void wlr_data_source_dnd_finish(struct wlr_data_source *source) { + if (source->impl->dnd_finish) { + source->impl->dnd_finish(source); + } +} + +void wlr_data_source_dnd_action(struct wlr_data_source *source, + enum wl_data_device_manager_dnd_action action) { + source->current_dnd_action = action; + if (source->impl->dnd_action) { + source->impl->dnd_action(source, action); + } +} + void data_device_manager_get_data_device(struct wl_client *client, struct wl_resource *manager_resource, uint32_t id, @@ -1048,40 +1123,23 @@ void data_device_manager_get_data_device(struct wl_client *client, static void data_device_manager_create_data_source(struct wl_client *client, struct wl_resource *resource, uint32_t id) { - struct client_data_source *source = - calloc(1, sizeof(struct client_data_source)); - if (source == NULL) { + struct wl_resource *source_resource = wl_resource_create(client, + &wl_data_source_interface, wl_resource_get_version(resource), id); + if (source_resource == NULL) { wl_resource_post_no_memory(resource); return; } - wlr_data_source_init(&source->source); - source->resource = wl_resource_create(client, &wl_data_source_interface, - wl_resource_get_version(resource), id); - if (source->resource == NULL) { - free(source); + struct client_data_source *source = + client_data_source_create(source_resource); + if (source == NULL) { + wl_resource_destroy(source_resource); wl_resource_post_no_memory(resource); return; } - wl_resource_set_implementation(source->resource, &data_source_impl, - source, data_source_resource_handle_destroy); - source->source.accept = client_data_source_accept; - source->source.send = client_data_source_send; - source->source.cancel = client_data_source_cancel; - - if (wl_resource_get_version(source->resource) >= - WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) { - source->source.dnd_drop = client_data_source_dnd_drop; - } - if (wl_resource_get_version(source->resource) >= - WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { - source->source.dnd_finish = client_data_source_dnd_finish; - } - if (wl_resource_get_version(source->resource) >= - WL_DATA_SOURCE_ACTION_SINCE_VERSION) { - source->source.dnd_action = client_data_source_dnd_action; - } + wl_resource_set_implementation(source_resource, &data_source_impl, + source, data_source_resource_handle_destroy); } static const struct wl_data_device_manager_interface diff --git a/types/wlr_seat.c b/types/wlr_seat.c index ed7fb470..3c1f17fa 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -366,9 +366,9 @@ void wlr_seat_destroy(struct wlr_seat *seat) { wl_list_remove(&seat->display_destroy.link); if (seat->selection_source) { - seat->selection_source->cancel(seat->selection_source); - seat->selection_source = NULL; wl_list_remove(&seat->selection_source_destroy.link); + wlr_data_source_cancel(seat->selection_source); + seat->selection_source = NULL; } if (seat->primary_selection_source) { seat->primary_selection_source->cancel(seat->primary_selection_source); -- cgit v1.2.3 From cadfccf1fde164646b175401a3fb1d3108b4af9a Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 29 Mar 2018 17:53:47 -0400 Subject: xwayland: use a separate window for drag'n'drop --- include/xwayland/xwm.h | 2 + xwayland/selection.c | 129 +++++++++++++++++++++++++++++++------------------ 2 files changed, 85 insertions(+), 46 deletions(-) (limited to 'include') diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index d36f9ac0..63ee5894 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -115,6 +115,8 @@ struct wlr_xwm { xcb_window_t selection_window; struct wlr_xwm_selection clipboard_selection; struct wlr_xwm_selection primary_selection; + + xcb_window_t dnd_window; struct wlr_xwm_selection dnd_selection; struct wlr_xwayland_surface *focus_surface; diff --git a/xwayland/selection.c b/xwayland/selection.c index d4a8b597..272c10cd 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -50,6 +50,14 @@ static void xwm_selection_send_notify(struct wlr_xwm_selection *selection, .property = property, }; + wlr_log(L_DEBUG, "SendEvent destination=%d SelectionNotify(31) time=%d " + "requestor=%d selection=%d target=%d property=%d", + selection->request.requestor, + selection->request.time, + selection->request.requestor, + selection->request.selection, + selection->request.target, + property); xcb_send_event(selection->xwm->xcb_conn, 0, // propagate selection->request.requestor, @@ -188,7 +196,7 @@ static void xwm_selection_source_send(struct wlr_xwm_selection *selection, struct wlr_data_source *source = selection->xwm->seat->selection_source; if (source != NULL) { - source->send(source, mime_type, fd); + wlr_data_source_send(source, mime_type, fd); return; } } else if (selection == &selection->xwm->primary_selection) { @@ -202,7 +210,7 @@ static void xwm_selection_source_send(struct wlr_xwm_selection *selection, struct wlr_data_source *source = selection->xwm->seat->drag_source; if (source != NULL) { - source->send(source, mime_type, fd); + wlr_data_source_send(source, mime_type, fd); return; } } @@ -282,6 +290,8 @@ static void xwm_selection_send_data(struct wlr_xwm_selection *selection, selection->source_fd, WL_EVENT_READABLE, xwm_data_source_read, selection); + wlr_log(L_DEBUG, "Sending Wayland selection to Xwayland window with " + "MIME type %s", mime_type); xwm_selection_source_send(selection, mime_type, p[1]); close(p[1]); } @@ -345,7 +355,7 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { struct wl_array *mime_types = &drag->source->mime_types; xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_selection.window; + data.data32[0] = xwm->dnd_window; data.data32[1] = XDND_VERSION << 24; // If we have 3 MIME types or less, we can send them directly in the @@ -375,7 +385,7 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->dnd_selection.window, + xwm->dnd_window, xwm->atoms[DND_TYPE_LIST], XCB_ATOM_ATOM, 32, // format @@ -391,7 +401,7 @@ static void xwm_dnd_send_position(struct wlr_xwm *xwm, uint32_t time, int16_t x, assert(drag != NULL); xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_selection.window; + data.data32[0] = xwm->dnd_window; data.data32[2] = (x << 16) | y; data.data32[3] = time; data.data32[4] = @@ -407,7 +417,7 @@ static void xwm_dnd_send_drop(struct wlr_xwm *xwm, uint32_t time) { assert(dest != NULL); xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_selection.window; + data.data32[0] = xwm->dnd_window; data.data32[2] = time; xwm_dnd_send_event(xwm, xwm->atoms[DND_DROP], &data); @@ -420,7 +430,7 @@ static void xwm_dnd_send_leave(struct wlr_xwm *xwm) { assert(dest != NULL); xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_selection.window; + data.data32[0] = xwm->dnd_window; xwm_dnd_send_event(xwm, xwm->atoms[DND_LEAVE], &data); } @@ -432,7 +442,7 @@ static void xwm_dnd_send_leave(struct wlr_xwm *xwm) { assert(dest != NULL); xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_selection.window; + data.data32[0] = xwm->dnd_window; data.data32[1] = drag->source->accepted; if (drag->source->accepted) { @@ -496,8 +506,11 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, xcb_selection_request_event_t *selection_request = (xcb_selection_request_event_t *) event; - wlr_log(L_DEBUG, "XCB_SELECTION_REQUEST (selection=%u, target=%u)", - selection_request->selection, selection_request->target); + wlr_log(L_DEBUG, "XCB_SELECTION_REQUEST (time=%u owner=%u, requestor=%u " + "selection=%u, target=%u, property=%u)", + selection_request->time, selection_request->owner, + selection_request->requestor, selection_request->selection, + selection_request->target, selection_request->property); if (selection_request->selection == xwm->atoms[CLIPBOARD_MANAGER]) { // The wlroots clipboard should already have grabbed the first target, @@ -514,7 +527,12 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, struct wlr_xwm_selection *selection = xwm_get_selection(xwm, selection_request->selection); if (selection == NULL) { - xwm_selection_send_notify(selection, XCB_ATOM_NONE); + wlr_log(L_DEBUG, "received selection request for unknown selection"); + return; + } + + if (selection->window != selection_request->owner) { + wlr_log(L_DEBUG, "received selection request with invalid owner"); return; } @@ -588,8 +606,7 @@ static int xwm_data_source_write(int fd, uint32_t mask, void *data) { xwm_data_source_remove_property_source(selection); if (selection->incr) { - xcb_delete_property(xwm->xcb_conn, - selection->window, + xcb_delete_property(xwm->xcb_conn, selection->window, xwm->atoms[WL_SELECTION]); } else { wlr_log(L_DEBUG, "transfer complete"); @@ -687,27 +704,37 @@ struct x11_data_source { 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 const struct wlr_data_source_impl data_source_impl; + +static struct x11_data_source *data_source_from_wlr_data_source( + struct wlr_data_source *wlr_source) { + assert(wlr_source->impl == &data_source_impl); + return (struct x11_data_source *)wlr_source; } -static void data_source_send(struct wlr_data_source *base, +static void data_source_send(struct wlr_data_source *wlr_source, const char *mime_type, int32_t fd) { - struct x11_data_source *source = (struct x11_data_source *)base; + struct x11_data_source *source = + data_source_from_wlr_data_source(wlr_source); struct wlr_xwm_selection *selection = source->selection; - source_send(selection, &base->mime_types, &source->mime_types_atoms, + source_send(selection, &wlr_source->mime_types, &source->mime_types_atoms, mime_type, fd); } -static void data_source_cancel(struct wlr_data_source *base) { - struct x11_data_source *source = (struct x11_data_source *)base; +static void data_source_cancel(struct wlr_data_source *wlr_source) { + struct x11_data_source *source = + data_source_from_wlr_data_source(wlr_source); wlr_data_source_finish(&source->base); wl_array_release(&source->mime_types_atoms); free(source); } +static const struct wlr_data_source_impl data_source_impl = { + .send = data_source_send, + .cancel = data_source_cancel, +}; + struct x11_primary_selection_source { struct wlr_primary_selection_source base; struct wlr_xwm_selection *selection; @@ -820,10 +847,7 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { 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; + wlr_data_source_init(&source->base, &data_source_impl); source->selection = selection; wl_array_init(&source->mime_types_atoms); @@ -834,7 +858,7 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { wlr_seat_set_selection(xwm->seat, &source->base, wl_display_next_serial(xwm->xwayland->wl_display)); } else { - source->base.cancel(&source->base); + wlr_data_source_cancel(&source->base); } } else if (selection == &xwm->primary_selection) { struct x11_primary_selection_source *source = @@ -1005,12 +1029,9 @@ int xwm_handle_selection_client_message(struct wlr_xwm *xwm, struct wlr_drag *drag = xwm->drag; assert(drag != NULL); - drag->source->accepted = accepted; - drag->source->current_dnd_action = action; - if (drag->source->dnd_action) { - drag->source->dnd_action(drag->source, action); - } + drag->source->accepted = accepted; + wlr_data_source_dnd_action(drag->source, action); wlr_log(L_DEBUG, "DND_STATUS window=%d accepted=%d action=%d", target_window, accepted, action); @@ -1025,6 +1046,8 @@ int xwm_handle_selection_client_message(struct wlr_xwm *xwm, return 1; } + struct wlr_data_source *source = xwm->seat->drag_source; + xcb_client_message_data_t *data = &ev->data; xcb_window_t target_window = data->data32[0]; bool performed = data->data32[1] & 1; @@ -1041,10 +1064,7 @@ int xwm_handle_selection_client_message(struct wlr_xwm *xwm, data_device_manager_dnd_action_from_atom(xwm, action_atom); if (performed) { - struct wlr_data_source *source = xwm->seat->drag_source; - if (source->dnd_finish) { - source->dnd_finish(source); - } + wlr_data_source_dnd_finish(source); } wlr_log(L_DEBUG, "DND_FINISH window=%d performed=%d action=%d", @@ -1072,7 +1092,9 @@ static void selection_init(struct wlr_xwm *xwm, void xwm_selection_init(struct wlr_xwm *xwm) { // Clipboard and primary selection - uint32_t selection_values[] = { XCB_EVENT_MASK_PROPERTY_CHANGE }; + uint32_t selection_values[] = { + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE + }; xwm->selection_window = xcb_generate_id(xwm->xcb_conn); xcb_create_window(xwm->xcb_conn, XCB_COPY_FROM_PARENT, @@ -1094,15 +1116,29 @@ void xwm_selection_init(struct wlr_xwm *xwm) { selection_init(xwm, &xwm->primary_selection, xwm->atoms[PRIMARY]); // Drag'n'drop + uint32_t dnd_values[] = { + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE + }; + xwm->dnd_window = xcb_generate_id(xwm->xcb_conn); + xcb_create_window(xwm->xcb_conn, + XCB_COPY_FROM_PARENT, + xwm->dnd_window, + xwm->screen->root, + 0, 0, + 8192, 8192, + 0, + XCB_WINDOW_CLASS_INPUT_ONLY, + xwm->screen->root_visual, + XCB_CW_EVENT_MASK, dnd_values); + uint32_t version = XDND_VERSION; xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->selection_window, + xwm->dnd_window, xwm->atoms[DND_AWARE], XCB_ATOM_ATOM, - 32, - 1, - &version); + 32, // format + 1, &version); selection_init(xwm, &xwm->dnd_selection, xwm->atoms[DND_SELECTION]); } @@ -1114,9 +1150,12 @@ void xwm_selection_finish(struct wlr_xwm *xwm) { if (xwm->selection_window) { xcb_destroy_window(xwm->xcb_conn, xwm->selection_window); } + if (xwm->dnd_window) { + xcb_destroy_window(xwm->xcb_conn, xwm->dnd_window); + } if (xwm->seat) { if (xwm->seat->selection_source && - xwm->seat->selection_source->cancel == data_source_cancel) { + xwm->seat->selection_source->impl == &data_source_impl) { wlr_seat_set_selection(xwm->seat, NULL, wl_display_next_serial(xwm->xwayland->wl_display)); } @@ -1153,7 +1192,7 @@ static void seat_handle_selection(struct wl_listener *listener, wl_container_of(listener, xwm, seat_selection); struct wlr_data_source *source = seat->selection_source; - if (source != NULL && source->send == data_source_send) { + if (source != NULL && source->impl == &data_source_impl) { return; } @@ -1195,10 +1234,8 @@ static void seat_handle_drag_focus(struct wl_listener *listener, void *data) { } if (xwm->drag_focus != NULL) { - if (drag->source->dnd_action) { - drag->source->dnd_action(drag->source, - WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE); - } + wlr_data_source_dnd_action(drag->source, + WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE); xwm_dnd_send_leave(xwm); } -- cgit v1.2.3 From 92b74071d015759ac0da3ef17d82ae0594818128 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Apr 2018 19:56:41 -0400 Subject: xwayland: support multiple wayland → xwayland selection transfers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes drag'n'drop support for Chromium. --- include/xwayland/xwm.h | 32 ++-- xwayland/selection.c | 408 ++++++++++++++++++++++++++----------------------- xwayland/xwm.c | 1 + 3 files changed, 237 insertions(+), 204 deletions(-) (limited to 'include') diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index 63ee5894..c83dd7c6 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -47,6 +47,7 @@ enum atom_name { INCR, TEXT, TIMESTAMP, + DELETE, NET_WM_WINDOW_TYPE_UTILITY, NET_WM_WINDOW_TYPE_TOOLTIP, NET_WM_WINDOW_TYPE_DND, @@ -80,22 +81,33 @@ enum net_wm_state_action { #define XDND_VERSION 5 +struct wlr_xwm_selection; + +struct wlr_xwm_selection_transfer { + struct wlr_xwm_selection *selection; + bool incr; + bool flush_property_on_delete; + bool property_set; + struct wl_array source_data; + int source_fd; + struct wl_event_source *source; + + // when sending to x11 + xcb_selection_request_event_t request; + + // when receiving from x11 + int property_start; + xcb_get_property_reply_t *property_reply; +}; + 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_selection_transfer incoming; }; struct wlr_xwm { diff --git a/xwayland/selection.c b/xwayland/selection.c index 272c10cd..0905870b 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -38,144 +38,143 @@ static enum wl_data_device_manager_dnd_action return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; } -static void xwm_selection_send_notify(struct wlr_xwm_selection *selection, - xcb_atom_t property) { +static void xwm_selection_send_notify(struct wlr_xwm *xwm, + xcb_selection_request_event_t *req, xcb_atom_t property) { 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, + .time = req->time, + .requestor = req->requestor, + .selection = req->selection, + .target = req->target, .property = property, }; wlr_log(L_DEBUG, "SendEvent destination=%d SelectionNotify(31) time=%d " - "requestor=%d selection=%d target=%d property=%d", - selection->request.requestor, - selection->request.time, - selection->request.requestor, - selection->request.selection, - selection->request.target, - property); - xcb_send_event(selection->xwm->xcb_conn, + "requestor=%d selection=%d target=%d property=%d", req->requestor, + req->time, req->requestor, req->selection, req->target, property); + xcb_send_event(xwm->xcb_conn, 0, // propagate - selection->request.requestor, + req->requestor, XCB_EVENT_MASK_NO_EVENT, (const char *)&selection_notify); - xcb_flush(selection->xwm->xcb_conn); + xcb_flush(xwm->xcb_conn); } -static int xwm_selection_flush_source_data(struct wlr_xwm_selection *selection) { - xcb_change_property(selection->xwm->xcb_conn, +static int xwm_selection_flush_source_data( + struct wlr_xwm_selection_transfer *transfer) { + xcb_change_property(transfer->selection->xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - selection->request.requestor, - selection->request.property, - selection->target, + transfer->request.requestor, + transfer->request.property, + transfer->request.target, 8, // format - selection->source_data.size, - selection->source_data.data); - selection->property_set = true; - int length = selection->source_data.size; - selection->source_data.size = 0; - + transfer->source_data.size, + transfer->source_data.data); + transfer->property_set = true; + size_t length = transfer->source_data.size; + transfer->source_data.size = 0; return length; } -static void xwm_data_source_remove_property_source( - struct wlr_xwm_selection *selection) { - if (selection->property_source) { - wl_event_source_remove(selection->property_source); +static void xwm_data_source_remove_source( + struct wlr_xwm_selection_transfer *transfer) { + if (transfer->source != NULL) { + wl_event_source_remove(transfer->source); + transfer->source = NULL; } - selection->property_source = NULL; } static void xwm_data_source_close_source_fd( - struct wlr_xwm_selection *selection) { - close(selection->source_fd); - selection->source_fd = -1; + struct wlr_xwm_selection_transfer *transfer) { + if (transfer->source_fd >= 0) { + close(transfer->source_fd); + transfer->source_fd = -1; + } } static int xwm_data_source_read(int fd, uint32_t mask, void *data) { - struct wlr_xwm_selection *selection = data; - struct wlr_xwm *xwm = selection->xwm; + struct wlr_xwm_selection_transfer *transfer = data; + struct wlr_xwm *xwm = transfer->selection->xwm; 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); - if (!p){ + size_t current = transfer->source_data.size; + if (transfer->source_data.size < incr_chunk_size) { + p = wl_array_add(&transfer->source_data, incr_chunk_size); + if (p == NULL) { wlr_log(L_ERROR, "Could not allocate selection source_data"); goto error_out; } } else { - p = (char *) selection->source_data.data + selection->source_data.size; + p = (char *)transfer->source_data.data + transfer->source_data.size; } - int available = selection->source_data.alloc - current; - - int len = read(fd, p, available); + size_t available = transfer->source_data.alloc - current; + ssize_t len = read(fd, p, available); if (len == -1) { wlr_log(L_ERROR, "read error from data source: %m"); goto error_out; } - wlr_log(L_DEBUG, "read %d (available %d, mask 0x%x) bytes: \"%.*s\"", - len, available, mask, len, (char *) p); + wlr_log(L_DEBUG, "read %ld (available %zu, mask 0x%x) bytes: \"%.*s\"", + len, available, mask, (int)len, (char *)p); - selection->source_data.size = current + len; - if (selection->source_data.size >= incr_chunk_size) { - if (!selection->incr) { + transfer->source_data.size = current + len; + if (transfer->source_data.size >= incr_chunk_size) { + if (!transfer->incr) { wlr_log(L_DEBUG, "got %zu bytes, starting incr", - selection->source_data.size); - selection->incr = 1; + transfer->source_data.size); + xcb_change_property(xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - selection->request.requestor, - selection->request.property, - xwm->atoms[INCR], - 32, /* format */ - 1, &incr_chunk_size); - selection->property_set = true; - selection->flush_property_on_delete = 1; - xwm_data_source_remove_property_source(selection); - xwm_selection_send_notify(selection, selection->request.property); - } else if (selection->property_set) { + XCB_PROP_MODE_REPLACE, + transfer->request.requestor, + transfer->request.property, + xwm->atoms[INCR], + 32, /* format */ + 1, &incr_chunk_size); + transfer->incr = true; + transfer->property_set = true; + transfer->flush_property_on_delete = true; + xwm_data_source_remove_source(transfer); + xwm_selection_send_notify(xwm, &transfer->request, + transfer->request.property); + } else if (transfer->property_set) { wlr_log(L_DEBUG, "got %zu bytes, waiting for property delete", - selection->source_data.size); + transfer->source_data.size); - selection->flush_property_on_delete = 1; - xwm_data_source_remove_property_source(selection); + transfer->flush_property_on_delete = true; + xwm_data_source_remove_source(transfer); } else { wlr_log(L_DEBUG, "got %zu bytes, property deleted, setting new " - "property", selection->source_data.size); - xwm_selection_flush_source_data(selection); + "property", transfer->source_data.size); + xwm_selection_flush_source_data(transfer); } - } else if (len == 0 && !selection->incr) { + } else if (len == 0 && !transfer->incr) { wlr_log(L_DEBUG, "non-incr transfer complete"); /* Non-incr transfer all done. */ - xwm_selection_flush_source_data(selection); - xwm_selection_send_notify(selection, selection->request.property); + xwm_selection_flush_source_data(transfer); + xwm_selection_send_notify(xwm, &transfer->request, + transfer->request.property); xcb_flush(xwm->xcb_conn); - xwm_data_source_remove_property_source(selection); - xwm_data_source_close_source_fd(selection); - wl_array_release(&selection->source_data); - selection->request.requestor = XCB_NONE; - } else if (len == 0 && selection->incr) { + xwm_data_source_remove_source(transfer); + xwm_data_source_close_source_fd(transfer); + wl_array_release(&transfer->source_data); + transfer->request.requestor = XCB_NONE; + } else if (len == 0 && transfer->incr) { wlr_log(L_DEBUG, "incr transfer complete"); - selection->flush_property_on_delete = 1; - if (selection->property_set) { + transfer->flush_property_on_delete = true; + if (transfer->property_set) { wlr_log(L_DEBUG, "got %zu bytes, waiting for property delete", - selection->source_data.size); + transfer->source_data.size); } else { wlr_log(L_DEBUG, "got %zu bytes, property deleted, setting new " - "property", selection->source_data.size); - xwm_selection_flush_source_data(selection); + "property", transfer->source_data.size); + xwm_selection_flush_source_data(transfer); } xcb_flush(xwm->xcb_conn); - xwm_data_source_remove_property_source(selection); - xwm_data_source_close_source_fd(selection); + xwm_data_source_remove_source(transfer); + xwm_data_source_close_source_fd(transfer); } else { wlr_log(L_DEBUG, "nothing happened, buffered the bytes"); } @@ -183,10 +182,10 @@ static int xwm_data_source_read(int fd, uint32_t mask, void *data) { return 1; error_out: - xwm_selection_send_notify(selection, XCB_ATOM_NONE); - xwm_data_source_remove_property_source(selection); - xwm_data_source_close_source_fd(selection); - wl_array_release(&selection->source_data); + xwm_selection_send_notify(xwm, &transfer->request, XCB_ATOM_NONE); + xwm_data_source_remove_source(transfer); + xwm_data_source_close_source_fd(transfer); + wl_array_release(&transfer->source_data); return 0; } @@ -242,14 +241,17 @@ static struct wl_array *xwm_selection_source_get_mime_types( return NULL; } +/** + * Read the Wayland selection and send it to an Xwayland client. + */ static void xwm_selection_send_data(struct wlr_xwm_selection *selection, - xcb_atom_t target, const char *mime_type) { + xcb_selection_request_event_t *req, const char *mime_type) { // Check MIME type struct wl_array *mime_types = xwm_selection_source_get_mime_types(selection); if (mime_types == NULL) { wlr_log(L_ERROR, "not sending selection: no MIME type list available"); - xwm_selection_send_notify(selection, XCB_ATOM_NONE); + xwm_selection_send_notify(selection->xwm, req, XCB_ATOM_NONE); return; } @@ -265,50 +267,45 @@ static void xwm_selection_send_data(struct wlr_xwm_selection *selection, if (!found) { wlr_log(L_ERROR, "not sending selection: " "requested an unsupported MIME type %s", mime_type); - xwm_selection_send_notify(selection, XCB_ATOM_NONE); + xwm_selection_send_notify(selection->xwm, req, XCB_ATOM_NONE); + return; + } + + struct wlr_xwm_selection_transfer *transfer = + calloc(1, sizeof(struct wlr_xwm_selection_transfer)); + if (transfer == NULL) { + wlr_log(L_ERROR, "Allocation failed"); return; } + transfer->selection = selection; + transfer->request = *req; + wl_array_init(&transfer->source_data); int p[2]; if (pipe(p) == -1) { - wlr_log(L_ERROR, "pipe failed: %m"); - xwm_selection_send_notify(selection, XCB_ATOM_NONE); + wlr_log(L_ERROR, "pipe() failed: %m"); + xwm_selection_send_notify(selection->xwm, req, XCB_ATOM_NONE); return; } - fcntl(p[0], F_SETFD, FD_CLOEXEC); fcntl(p[0], F_SETFL, O_NONBLOCK); fcntl(p[1], F_SETFD, FD_CLOEXEC); fcntl(p[1], F_SETFL, O_NONBLOCK); - wl_array_init(&selection->source_data); - selection->target = target; - selection->source_fd = p[0]; + transfer->source_fd = p[0]; + struct wl_event_loop *loop = 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_data_source_read, - selection); + transfer->source = wl_event_loop_add_fd(loop, transfer->source_fd, + WL_EVENT_READABLE, xwm_data_source_read, transfer); - wlr_log(L_DEBUG, "Sending Wayland selection to Xwayland window with " - "MIME type %s", mime_type); + wlr_log(L_DEBUG, "Sending Wayland selection %u to Xwayland window with " + "MIME type %s", req->target, mime_type); xwm_selection_source_send(selection, mime_type, p[1]); - close(p[1]); + // TODO close(p[1]); } -static void xwm_selection_send_timestamp(struct wlr_xwm_selection *selection) { - xcb_change_property(selection->xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - selection->request.requestor, - selection->request.property, - XCB_ATOM_INTEGER, - 32, // format - 1, &selection->timestamp); - - xwm_selection_send_notify(selection, selection->request.property); -} - -static xcb_atom_t xwm_get_mime_type_atom(struct wlr_xwm *xwm, char *mime_type) { +static xcb_atom_t xwm_mime_type_to_atom(struct wlr_xwm *xwm, char *mime_type) { if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) { return xwm->atoms[UTF8_STRING]; } else if (strcmp(mime_type, "text/plain") == 0) { @@ -366,7 +363,7 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { char **mime_type_ptr; wl_array_for_each(mime_type_ptr, mime_types) { char *mime_type = *mime_type_ptr; - data.data32[2+i] = xwm_get_mime_type_atom(xwm, mime_type); + data.data32[2+i] = xwm_mime_type_to_atom(xwm, mime_type); ++i; } } else { @@ -379,7 +376,7 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { char **mime_type_ptr; wl_array_for_each(mime_type_ptr, mime_types) { char *mime_type = *mime_type_ptr; - targets[i] = xwm_get_mime_type_atom(xwm, mime_type); + targets[i] = xwm_mime_type_to_atom(xwm, mime_type); ++i; } @@ -453,14 +450,16 @@ static void xwm_dnd_send_leave(struct wlr_xwm *xwm) { xwm_dnd_send_event(xwm, xwm->atoms[DND_FINISHED], &data); }*/ -static void xwm_selection_send_targets(struct wlr_xwm_selection *selection) { +static void xwm_selection_send_targets(struct wlr_xwm_selection *selection, + xcb_selection_request_event_t *req) { struct wlr_xwm *xwm = selection->xwm; - struct wl_array *mime_types = xwm_selection_source_get_mime_types(selection); + struct wl_array *mime_types = + xwm_selection_source_get_mime_types(selection); if (mime_types == NULL) { wlr_log(L_ERROR, "not sending selection targets: " "no selection source available"); - xwm_selection_send_notify(selection, XCB_ATOM_NONE); + xwm_selection_send_notify(selection->xwm, req, XCB_ATOM_NONE); return; } @@ -473,19 +472,32 @@ static void xwm_selection_send_targets(struct wlr_xwm_selection *selection) { char **mime_type_ptr; wl_array_for_each(mime_type_ptr, mime_types) { char *mime_type = *mime_type_ptr; - targets[2+i] = xwm_get_mime_type_atom(xwm, mime_type); + targets[2+i] = xwm_mime_type_to_atom(xwm, mime_type); ++i; } xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - selection->request.requestor, - selection->request.property, + req->requestor, + req->property, XCB_ATOM_ATOM, 32, // format n, targets); - xwm_selection_send_notify(selection, selection->request.property); + xwm_selection_send_notify(selection->xwm, req, req->property); +} + +static void xwm_selection_send_timestamp(struct wlr_xwm_selection *selection, + xcb_selection_request_event_t *req) { + xcb_change_property(selection->xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + req->requestor, + req->property, + XCB_ATOM_INTEGER, + 32, // format + 1, &selection->timestamp); + + xwm_selection_send_notify(selection->xwm, req, req->property); } static struct wlr_xwm_selection *xwm_get_selection(struct wlr_xwm *xwm, @@ -501,134 +513,138 @@ static struct wlr_xwm_selection *xwm_get_selection(struct wlr_xwm *xwm, } } +static char *xwm_mime_type_from_atom(struct wlr_xwm *xwm, xcb_atom_t atom) { + if (atom == xwm->atoms[UTF8_STRING]) { + return strdup("text/plain;charset=utf-8"); + } else if (atom == xwm->atoms[TEXT]) { + return strdup("text/plain"); + } else { + return xwm_get_atom_name(xwm, atom); + } +} + 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 *req = (xcb_selection_request_event_t *) event; wlr_log(L_DEBUG, "XCB_SELECTION_REQUEST (time=%u owner=%u, requestor=%u " "selection=%u, target=%u, property=%u)", - selection_request->time, selection_request->owner, - selection_request->requestor, selection_request->selection, - selection_request->target, selection_request->property); + req->time, req->owner, req->requestor, req->selection, req->target, + req->property); - if (selection_request->selection == xwm->atoms[CLIPBOARD_MANAGER]) { + if (req->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. - 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); + xwm_selection_send_notify(xwm, req, req->property); return; } struct wlr_xwm_selection *selection = - xwm_get_selection(xwm, selection_request->selection); + xwm_get_selection(xwm, req->selection); if (selection == NULL) { wlr_log(L_DEBUG, "received selection request for unknown selection"); return; } - if (selection->window != selection_request->owner) { + if (selection->window != req->owner) { wlr_log(L_DEBUG, "received selection request with invalid owner"); 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 && xwm->drag_focus == NULL) { char *selection_name = xwm_get_atom_name(xwm, selection->atom); wlr_log(L_DEBUG, "denying read access to selection %u (%s): " "no xwayland surface focused", selection->atom, selection_name); free(selection_name); - xwm_selection_send_notify(selection, XCB_ATOM_NONE); + xwm_selection_send_notify(xwm, req, XCB_ATOM_NONE); return; } - if (selection_request->target == xwm->atoms[TARGETS]) { - xwm_selection_send_targets(selection); - } else if (selection_request->target == xwm->atoms[TIMESTAMP]) { - xwm_selection_send_timestamp(selection); - } else if (selection_request->target == xwm->atoms[UTF8_STRING]) { - xwm_selection_send_data(selection, selection_request->target, - "text/plain;charset=utf-8"); - } else if (selection_request->target == xwm->atoms[TEXT]) { - xwm_selection_send_data(selection, selection_request->target, - "text/plain"); + if (req->target == xwm->atoms[TARGETS]) { + xwm_selection_send_targets(selection, req); + } else if (req->target == xwm->atoms[TIMESTAMP]) { + xwm_selection_send_timestamp(selection, req); + } else if (req->target == xwm->atoms[DELETE]) { + xwm_selection_send_notify(selection->xwm, req, req->property); } else { - char *mime_type = xwm_get_atom_name(xwm, selection_request->target); + // Send data + char *mime_type = xwm_mime_type_from_atom(xwm, req->target); if (mime_type == NULL) { - wlr_log(L_ERROR, "ignoring selection request: unknown atom"); - xwm_selection_send_notify(selection, XCB_ATOM_NONE); + wlr_log(L_ERROR, "ignoring selection request: unknown atom %u", + req->target); + xwm_selection_send_notify(xwm, req, XCB_ATOM_NONE); return; } - xwm_selection_send_data(selection, selection_request->target, - mime_type); + xwm_selection_send_data(selection, req, mime_type); free(mime_type); } } static void xwm_data_source_destroy_property_reply( - struct wlr_xwm_selection *selection) { - free(selection->property_reply); - selection->property_reply = NULL; + struct wlr_xwm_selection_transfer *transfer) { + free(transfer->property_reply); + transfer->property_reply = NULL; } +/** + * Write the X11 selection to a Wayland client. + */ static int xwm_data_source_write(int fd, uint32_t mask, void *data) { - struct wlr_xwm_selection *selection = data; - struct wlr_xwm *xwm = selection->xwm; + struct wlr_xwm_selection_transfer *transfer = data; + struct wlr_xwm *xwm = transfer->selection->xwm; - unsigned char *property = xcb_get_property_value(selection->property_reply); - int remainder = xcb_get_property_value_length(selection->property_reply) - - selection->property_start; + char *property = xcb_get_property_value(transfer->property_reply); + int remainder = xcb_get_property_value_length(transfer->property_reply) - + transfer->property_start; - int len = write(fd, property + selection->property_start, remainder); + ssize_t len = write(fd, property + transfer->property_start, remainder); if (len == -1) { - xwm_data_source_destroy_property_reply(selection); - xwm_data_source_remove_property_source(selection); - xwm_data_source_close_source_fd(selection); + xwm_data_source_destroy_property_reply(transfer); + xwm_data_source_remove_source(transfer); + xwm_data_source_close_source_fd(transfer); wlr_log(L_ERROR, "write error to target fd: %m"); return 1; } - wlr_log(L_DEBUG, "wrote %d (chunk size %d) of %d bytes", - selection->property_start + len, - len, xcb_get_property_value_length(selection->property_reply)); + wlr_log(L_DEBUG, "wrote %ld (chunk size %ld) of %d bytes", + transfer->property_start + len, + len, xcb_get_property_value_length(transfer->property_reply)); - selection->property_start += len; + transfer->property_start += len; if (len == remainder) { - xwm_data_source_destroy_property_reply(selection); - xwm_data_source_remove_property_source(selection); + xwm_data_source_destroy_property_reply(transfer); + xwm_data_source_remove_source(transfer); - if (selection->incr) { - xcb_delete_property(xwm->xcb_conn, selection->window, + if (transfer->incr) { + xcb_delete_property(xwm->xcb_conn, transfer->selection->window, xwm->atoms[WL_SELECTION]); } else { wlr_log(L_DEBUG, "transfer complete"); - xwm_data_source_close_source_fd(selection); + xwm_data_source_close_source_fd(transfer); } } return 1; } -static void xwm_write_property(struct wlr_xwm_selection *selection, +static void xwm_write_property(struct wlr_xwm_selection_transfer *transfer, xcb_get_property_reply_t *reply) { - selection->property_start = 0; - selection->property_reply = reply; - xwm_data_source_write(selection->source_fd, WL_EVENT_WRITABLE, selection); + struct wlr_xwm *xwm = transfer->selection->xwm; + + transfer->property_start = 0; + transfer->property_reply = reply; - if (selection->property_reply) { + xwm_data_source_write(transfer->source_fd, WL_EVENT_WRITABLE, transfer); + + if (transfer->property_reply != NULL) { struct wl_event_loop *loop = - 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, xwm_data_source_write, - selection); + wl_display_get_event_loop(xwm->xwayland->wl_display); + transfer->source = wl_event_loop_add_fd(loop, + transfer->source_fd, WL_EVENT_WRITABLE, xwm_data_source_write, + transfer); } } @@ -647,17 +663,19 @@ static void xwm_selection_get_data(struct wlr_xwm_selection *selection) { xcb_get_property_reply_t *reply = xcb_get_property_reply(xwm->xcb_conn, cookie, NULL); if (reply == NULL) { + wlr_log(L_ERROR, "Cannot get selection property"); return; } + struct wlr_xwm_selection_transfer *transfer = &selection->incoming; if (reply->type == xwm->atoms[INCR]) { - selection->incr = 1; + transfer->incr = true; free(reply); } else { - selection->incr = 0; + transfer->incr = false; // reply's ownership is transferred to wm, which is responsible // for freeing it - xwm_write_property(selection, reply); + xwm_write_property(transfer, reply); } } @@ -665,6 +683,7 @@ 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 wlr_xwm *xwm = selection->xwm; + struct wlr_xwm_selection_transfer *transfer = &selection->incoming; xcb_atom_t *atoms = mime_types_atoms->data; bool found = false; @@ -681,7 +700,8 @@ static void source_send(struct wlr_xwm_selection *selection, ++i; } if (!found) { - wlr_log(L_DEBUG, "cannot send X11 selection: unsupported MIME type"); + wlr_log(L_DEBUG, "Cannot send X11 selection to Wayland: " + "unsupported MIME type"); return; } @@ -695,7 +715,7 @@ static void source_send(struct wlr_xwm_selection *selection, xcb_flush(xwm->xcb_conn); fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); - selection->source_fd = fd; + transfer->source_fd = fd; } struct x11_data_source { @@ -964,14 +984,14 @@ static int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, return 1; } - selection->incr = 0; + struct wlr_xwm_selection_transfer *transfer = &selection->incoming; + transfer->incr = false; // doing this will give a selection notify where we actually handle the sync xcb_convert_selection(xwm->xcb_conn, selection->window, selection->atom, xwm->atoms[TARGETS], xwm->atoms[WL_SELECTION], xfixes_selection_notify->timestamp); - xcb_flush(xwm->xcb_conn); return 1; @@ -1080,7 +1100,7 @@ static void selection_init(struct wlr_xwm *xwm, selection->xwm = xwm; selection->atom = atom; selection->window = xwm->selection_window; - selection->request.requestor = XCB_NONE; + selection->incoming.selection = selection; uint32_t mask = XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | diff --git a/xwayland/xwm.c b/xwayland/xwm.c index d5501a0a..0b7c62b4 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -50,6 +50,7 @@ const char *atom_map[ATOM_LAST] = { "INCR", "TEXT", "TIMESTAMP", + "DELETE", "_NET_WM_WINDOW_TYPE_UTILITY", "_NET_WM_WINDOW_TYPE_TOOLTIP", "_NET_WM_WINDOW_TYPE_DND", -- cgit v1.2.3 From 8f84c5b05f9911c8b90106856ddc3f31b2c1d0cb Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 3 Apr 2018 00:41:26 -0400 Subject: xwayland: only send one target at a time --- include/xwayland/xwm.h | 3 + xwayland/selection.c | 170 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 117 insertions(+), 56 deletions(-) (limited to 'include') diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index c83dd7c6..c5b1af7e 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -85,6 +85,7 @@ struct wlr_xwm_selection; struct wlr_xwm_selection_transfer { struct wlr_xwm_selection *selection; + bool incr; bool flush_property_on_delete; bool property_set; @@ -94,6 +95,7 @@ struct wlr_xwm_selection_transfer { // when sending to x11 xcb_selection_request_event_t request; + struct wl_list outgoing_link; // when receiving from x11 int property_start; @@ -108,6 +110,7 @@ struct wlr_xwm_selection { xcb_timestamp_t timestamp; struct wlr_xwm_selection_transfer incoming; + struct wl_list outgoing; }; struct wlr_xwm { diff --git a/xwayland/selection.c b/xwayland/selection.c index 03a2eb20..eefd7c13 100644 --- a/xwayland/selection.c +++ b/xwayland/selection.c @@ -79,7 +79,7 @@ static int xwm_selection_flush_source_data( return length; } -static void xwm_data_source_remove_source( +static void xwm_selection_transfer_remove_source( struct wlr_xwm_selection_transfer *transfer) { if (transfer->source != NULL) { wl_event_source_remove(transfer->source); @@ -87,7 +87,7 @@ static void xwm_data_source_remove_source( } } -static void xwm_data_source_close_source_fd( +static void xwm_selection_transfer_close_source_fd( struct wlr_xwm_selection_transfer *transfer) { if (transfer->source_fd >= 0) { close(transfer->source_fd); @@ -95,6 +95,27 @@ static void xwm_data_source_close_source_fd( } } +static void xwm_selection_transfer_start_outgoing( + struct wlr_xwm_selection_transfer *transfer); + +static void xwm_selection_transfer_destroy_outgoing( + struct wlr_xwm_selection_transfer *transfer) { + wl_list_remove(&transfer->outgoing_link); + + // Start next queued transfer + struct wlr_xwm_selection_transfer *first = NULL; + if (!wl_list_empty(&transfer->selection->outgoing)) { + first = wl_container_of(transfer->selection->outgoing.prev, first, + outgoing_link); + xwm_selection_transfer_start_outgoing(first); + } + + xwm_selection_transfer_remove_source(transfer); + xwm_selection_transfer_close_source_fd(transfer); + wl_array_release(&transfer->source_data); + free(transfer); +} + static int xwm_data_source_read(int fd, uint32_t mask, void *data) { struct wlr_xwm_selection_transfer *transfer = data; struct wlr_xwm *xwm = transfer->selection->xwm; @@ -137,14 +158,14 @@ static int xwm_data_source_read(int fd, uint32_t mask, void *data) { transfer->incr = true; transfer->property_set = true; transfer->flush_property_on_delete = true; - xwm_data_source_remove_source(transfer); + xwm_selection_transfer_remove_source(transfer); xwm_selection_send_notify(xwm, &transfer->request, true); } else if (transfer->property_set) { wlr_log(L_DEBUG, "got %zu bytes, waiting for property delete", transfer->source_data.size); transfer->flush_property_on_delete = true; - xwm_data_source_remove_source(transfer); + xwm_selection_transfer_remove_source(transfer); } else { wlr_log(L_DEBUG, "got %zu bytes, property deleted, setting new " "property", transfer->source_data.size); @@ -154,11 +175,7 @@ static int xwm_data_source_read(int fd, uint32_t mask, void *data) { wlr_log(L_DEBUG, "non-incr transfer complete"); xwm_selection_flush_source_data(transfer); xwm_selection_send_notify(xwm, &transfer->request, true); - xcb_flush(xwm->xcb_conn); - xwm_data_source_remove_source(transfer); - xwm_data_source_close_source_fd(transfer); - wl_array_release(&transfer->source_data); - transfer->request.requestor = XCB_NONE; + xwm_selection_transfer_destroy_outgoing(transfer); } else if (len == 0 && transfer->incr) { wlr_log(L_DEBUG, "incr transfer complete"); @@ -171,9 +188,7 @@ static int xwm_data_source_read(int fd, uint32_t mask, void *data) { "property", transfer->source_data.size); xwm_selection_flush_source_data(transfer); } - xcb_flush(xwm->xcb_conn); - xwm_data_source_remove_source(transfer); - xwm_data_source_close_source_fd(transfer); + xwm_selection_transfer_destroy_outgoing(transfer); } else { wlr_log(L_DEBUG, "nothing happened, buffered the bytes"); } @@ -182,9 +197,7 @@ static int xwm_data_source_read(int fd, uint32_t mask, void *data) { error_out: xwm_selection_send_notify(xwm, &transfer->request, false); - xwm_data_source_remove_source(transfer); - xwm_data_source_close_source_fd(transfer); - wl_array_release(&transfer->source_data); + xwm_selection_transfer_destroy_outgoing(transfer); return 0; } @@ -240,6 +253,15 @@ static struct wl_array *xwm_selection_source_get_mime_types( return NULL; } +static void xwm_selection_transfer_start_outgoing( + struct wlr_xwm_selection_transfer *transfer) { + struct wlr_xwm *xwm = transfer->selection->xwm; + struct wl_event_loop *loop = + wl_display_get_event_loop(xwm->xwayland->wl_display); + transfer->source = wl_event_loop_add_fd(loop, transfer->source_fd, + WL_EVENT_READABLE, xwm_data_source_read, transfer); +} + /** * Read the Wayland selection and send it to an Xwayland client. */ @@ -293,15 +315,16 @@ static void xwm_selection_send_data(struct wlr_xwm_selection *selection, transfer->source_fd = p[0]; - struct wl_event_loop *loop = - wl_display_get_event_loop(selection->xwm->xwayland->wl_display); - transfer->source = wl_event_loop_add_fd(loop, transfer->source_fd, - WL_EVENT_READABLE, xwm_data_source_read, transfer); - wlr_log(L_DEBUG, "Sending Wayland selection %u to Xwayland window with " - "MIME type %s", req->target, mime_type); + "MIME type %s, target %u", req->target, mime_type, req->target); xwm_selection_source_send(selection, mime_type, p[1]); - // TODO close(p[1]); + + wl_list_insert(&selection->outgoing, &transfer->outgoing_link); + + // We can only handle one transfer at a time + if (wl_list_length(&selection->outgoing) == 1) { + xwm_selection_transfer_start_outgoing(transfer); + } } static xcb_atom_t xwm_mime_type_to_atom(struct wlr_xwm *xwm, char *mime_type) { @@ -523,10 +546,7 @@ static char *xwm_mime_type_from_atom(struct wlr_xwm *xwm, xcb_atom_t atom) { } static void xwm_handle_selection_request(struct wlr_xwm *xwm, - xcb_generic_event_t *event) { - xcb_selection_request_event_t *req = - (xcb_selection_request_event_t *) event; - + xcb_selection_request_event_t *req) { wlr_log(L_DEBUG, "XCB_SELECTION_REQUEST (time=%u owner=%u, requestor=%u " "selection=%u, target=%u, property=%u)", req->time, req->owner, req->requestor, req->selection, req->target, @@ -582,7 +602,45 @@ static void xwm_handle_selection_request(struct wlr_xwm *xwm, } } -static void xwm_data_source_destroy_property_reply( +static int xwm_handle_selection_property_notify(struct wlr_xwm *xwm, + xcb_property_notify_event_t *event) { + struct wlr_xwm_selection *selections[] = { + &xwm->clipboard_selection, + &xwm->primary_selection, + &xwm->dnd_selection, + }; + + for (size_t i = 0; i < sizeof(selections)/sizeof(selections[0]); ++i) { + struct wlr_xwm_selection *selection = selections[i]; + + if (event->window == xwm->selection_window) { + if (event->state == XCB_PROPERTY_NEW_VALUE && + event->atom == xwm->atoms[WL_SELECTION] && + selection->incoming.incr) { + wlr_log(L_DEBUG, "get incr chunk"); + // TODO + } + return 1; + } + + struct wlr_xwm_selection_transfer *outgoing; + wl_list_for_each(outgoing, &selection->outgoing, outgoing_link) { + if (event->window == outgoing->request.requestor) { + if (event->state == XCB_PROPERTY_DELETE && + event->atom == outgoing->request.property && + outgoing->incr) { + wlr_log(L_DEBUG, "send incr chunk"); + // TODO + } + return 1; + } + } + } + + return 0; +} + +static void xwm_selection_transfer_destroy_property_reply( struct wlr_xwm_selection_transfer *transfer) { free(transfer->property_reply); transfer->property_reply = NULL; @@ -601,9 +659,9 @@ static int xwm_data_source_write(int fd, uint32_t mask, void *data) { ssize_t len = write(fd, property + transfer->property_start, remainder); if (len == -1) { - xwm_data_source_destroy_property_reply(transfer); - xwm_data_source_remove_source(transfer); - xwm_data_source_close_source_fd(transfer); + xwm_selection_transfer_destroy_property_reply(transfer); + xwm_selection_transfer_remove_source(transfer); + xwm_selection_transfer_close_source_fd(transfer); wlr_log(L_ERROR, "write error to target fd: %m"); return 1; } @@ -614,15 +672,15 @@ static int xwm_data_source_write(int fd, uint32_t mask, void *data) { transfer->property_start += len; if (len == remainder) { - xwm_data_source_destroy_property_reply(transfer); - xwm_data_source_remove_source(transfer); + xwm_selection_transfer_destroy_property_reply(transfer); + xwm_selection_transfer_remove_source(transfer); if (transfer->incr) { xcb_delete_property(xwm->xcb_conn, transfer->selection->window, xwm->atoms[WL_SELECTION]); } else { wlr_log(L_DEBUG, "transfer complete"); - xwm_data_source_close_source_fd(transfer); + xwm_selection_transfer_close_source_fd(transfer); } } @@ -906,23 +964,20 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) { } static void xwm_handle_selection_notify(struct wlr_xwm *xwm, - xcb_generic_event_t *event) { - xcb_selection_notify_event_t *selection_notify = - (xcb_selection_notify_event_t *) event; - + xcb_selection_notify_event_t *event) { wlr_log(L_DEBUG, "XCB_SELECTION_NOTIFY (selection=%u, property=%u, target=%u)", - selection_notify->selection, selection_notify->property, - selection_notify->target); + event->selection, event->property, + event->target); struct wlr_xwm_selection *selection = - xwm_get_selection(xwm, selection_notify->selection); + xwm_get_selection(xwm, event->selection); if (selection == NULL) { return; } - if (selection_notify->property == XCB_ATOM_NONE) { + if (event->property == XCB_ATOM_NONE) { wlr_log(L_ERROR, "convert selection failed"); - } else if (selection_notify->target == xwm->atoms[TARGETS]) { + } else if (event->target == xwm->atoms[TARGETS]) { // No xwayland surface focused, deny access to clipboard if (xwm->focus_surface == NULL) { wlr_log(L_DEBUG, "denying write access to clipboard: " @@ -938,20 +993,17 @@ static void xwm_handle_selection_notify(struct wlr_xwm *xwm, } 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) { wlr_log(L_DEBUG, "XCB_XFIXES_SELECTION_NOTIFY (selection=%u, owner=%u)", - xfixes_selection_notify->selection, xfixes_selection_notify->owner); + event->selection, event->owner); struct wlr_xwm_selection *selection = - xwm_get_selection(xwm, xfixes_selection_notify->selection); + xwm_get_selection(xwm, event->selection); if (selection == NULL) { return 0; } - if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) { + if (event->owner == XCB_WINDOW_NONE) { if (selection->owner != selection->window) { // A real X client selection went away, not our // proxy selection @@ -973,13 +1025,13 @@ static int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, return 1; } - selection->owner = xfixes_selection_notify->owner; + selection->owner = event->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 == selection->window) { - selection->timestamp = xfixes_selection_notify->timestamp; + if (event->owner == selection->window) { + selection->timestamp = event->timestamp; return 1; } @@ -990,7 +1042,7 @@ static int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, selection->atom, xwm->atoms[TARGETS], xwm->atoms[WL_SELECTION], - xfixes_selection_notify->timestamp); + event->timestamp); xcb_flush(xwm->xcb_conn); return 1; @@ -1006,17 +1058,22 @@ int xwm_handle_selection_event(struct wlr_xwm *xwm, switch (event->response_type & ~0x80) { case XCB_SELECTION_NOTIFY: - xwm_handle_selection_notify(xwm, event); + xwm_handle_selection_notify(xwm, (xcb_selection_notify_event_t *)event); return 1; + case XCB_PROPERTY_NOTIFY: + return xwm_handle_selection_property_notify(xwm, + (xcb_property_notify_event_t *)event); case XCB_SELECTION_REQUEST: - xwm_handle_selection_request(xwm, event); + xwm_handle_selection_request(xwm, + (xcb_selection_request_event_t *)event); return 1; } switch (event->response_type - xwm->xfixes->first_event) { case XCB_XFIXES_SELECTION_NOTIFY: // an X11 window has copied something to the clipboard - return xwm_handle_xfixes_selection_notify(xwm, event); + return xwm_handle_xfixes_selection_notify(xwm, + (xcb_xfixes_selection_notify_event_t *)event); } return 0; @@ -1100,6 +1157,7 @@ static void selection_init(struct wlr_xwm *xwm, selection->atom = atom; selection->window = xwm->selection_window; selection->incoming.selection = selection; + wl_list_init(&selection->outgoing); uint32_t mask = XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | -- cgit v1.2.3 From 591ea27cf9bab54d966f47b9856a53b523af735a Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 3 Apr 2018 12:12:57 -0400 Subject: xwayland: refactor selection code --- include/xwayland/selection.h | 72 ++ include/xwayland/xwm.h | 52 +- xwayland/meson.build | 5 +- xwayland/selection.c | 1483 ---------------------------------------- xwayland/selection/dnd.c | 340 +++++++++ xwayland/selection/incoming.c | 461 +++++++++++++ xwayland/selection/outgoing.c | 420 ++++++++++++ xwayland/selection/selection.c | 319 +++++++++ xwayland/xwm.c | 4 - 9 files changed, 1626 insertions(+), 1530 deletions(-) create mode 100644 include/xwayland/selection.h delete mode 100644 xwayland/selection.c create mode 100644 xwayland/selection/dnd.c create mode 100644 xwayland/selection/incoming.c create mode 100644 xwayland/selection/outgoing.c create mode 100644 xwayland/selection/selection.c (limited to 'include') diff --git a/include/xwayland/selection.h b/include/xwayland/selection.h new file mode 100644 index 00000000..bb3a894d --- /dev/null +++ b/include/xwayland/selection.h @@ -0,0 +1,72 @@ +#ifndef XWAYLAND_SELECTION_H +#define XWAYLAND_SELECTION_H + +#include + +#define INCR_CHUNK_SIZE (64 * 1024) + +#define XDND_VERSION 5 + +struct wlr_xwm_selection; + +struct wlr_xwm_selection_transfer { + struct wlr_xwm_selection *selection; + + bool incr; + bool flush_property_on_delete; + bool property_set; + struct wl_array source_data; + int source_fd; + struct wl_event_source *source; + + // when sending to x11 + xcb_selection_request_event_t request; + struct wl_list outgoing_link; + + // when receiving from x11 + int property_start; + xcb_get_property_reply_t *property_reply; +}; + +struct wlr_xwm_selection { + struct wlr_xwm *xwm; + xcb_atom_t atom; + xcb_window_t window; + xcb_window_t owner; + xcb_timestamp_t timestamp; + + struct wlr_xwm_selection_transfer incoming; + struct wl_list outgoing; +}; + +void xwm_selection_transfer_remove_source( + struct wlr_xwm_selection_transfer *transfer); +void xwm_selection_transfer_close_source_fd( + struct wlr_xwm_selection_transfer *transfer); +void xwm_selection_transfer_destroy_property_reply( + struct wlr_xwm_selection_transfer *transfer); + +xcb_atom_t xwm_mime_type_to_atom(struct wlr_xwm *xwm, char *mime_type); +char *xwm_mime_type_from_atom(struct wlr_xwm *xwm, xcb_atom_t atom); +struct wlr_xwm_selection *xwm_get_selection(struct wlr_xwm *xwm, + xcb_atom_t selection_atom); + +void xwm_send_incr_chunk(struct wlr_xwm_selection_transfer *transfer); +void xwm_handle_selection_request(struct wlr_xwm *xwm, + xcb_selection_request_event_t *req); + +void xwm_get_incr_chunk(struct wlr_xwm_selection_transfer *transfer); +void xwm_handle_selection_notify(struct wlr_xwm *xwm, + xcb_selection_notify_event_t *event); +int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, + xcb_xfixes_selection_notify_event_t *event); +bool wlr_data_source_is_xwayland_data_source(struct wlr_data_source *wlr_source); +bool wlr_primary_selection_source_is_xwayland_primary_selection_source( + struct wlr_primary_selection_source *wlr_source); + +void xwm_seat_handle_start_drag(struct wlr_xwm *xwm, struct wlr_drag *drag); + +void xwm_selection_init(struct wlr_xwm *xwm); +void xwm_selection_finish(struct wlr_xwm *xwm); + +#endif diff --git a/include/xwayland/xwm.h b/include/xwayland/xwm.h index c5b1af7e..662d00af 100644 --- a/include/xwayland/xwm.h +++ b/include/xwayland/xwm.h @@ -5,13 +5,18 @@ #include #include #include - #ifdef WLR_HAS_XCB_ICCCM - #include +#include #endif #ifdef WLR_HAS_XCB_ERRORS - #include +#include #endif +#include "xwayland/selection.h" + +/* This is in xcb/xcb_event.h, but pulling xcb-util just for a constant + * others redefine anyway is meh + */ +#define XCB_EVENT_RESPONSE_TYPE_MASK (0x7f) enum atom_name { WL_SURFACE_ID, @@ -79,40 +84,6 @@ enum net_wm_state_action { NET_WM_STATE_TOGGLE = 2, }; -#define XDND_VERSION 5 - -struct wlr_xwm_selection; - -struct wlr_xwm_selection_transfer { - struct wlr_xwm_selection *selection; - - bool incr; - bool flush_property_on_delete; - bool property_set; - struct wl_array source_data; - int source_fd; - struct wl_event_source *source; - - // when sending to x11 - xcb_selection_request_event_t request; - struct wl_list outgoing_link; - - // when receiving from x11 - int property_start; - xcb_get_property_reply_t *property_reply; -}; - -struct wlr_xwm_selection { - struct wlr_xwm *xwm; - xcb_atom_t atom; - xcb_window_t window; - xcb_window_t owner; - xcb_timestamp_t timestamp; - - struct wlr_xwm_selection_transfer incoming; - struct wl_list outgoing; -}; - struct wlr_xwm { struct wlr_xwayland *xwayland; struct wl_event_source *event_source; @@ -168,15 +139,12 @@ void xwm_set_cursor(struct wlr_xwm *xwm, const uint8_t *pixels, uint32_t stride, int xwm_handle_selection_event(struct wlr_xwm *xwm, xcb_generic_event_t *event); int xwm_handle_selection_client_message(struct wlr_xwm *xwm, - xcb_client_message_event_t *ev); - -void xwm_selection_init(struct wlr_xwm *xwm); -void xwm_selection_finish(struct wlr_xwm *xwm); + xcb_client_message_event_t *ev); void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat); char *xwm_get_atom_name(struct wlr_xwm *xwm, xcb_atom_t atom); bool xwm_atoms_contains(struct wlr_xwm *xwm, xcb_atom_t *atoms, - size_t num_atoms, enum atom_name needle); + size_t num_atoms, enum atom_name needle); #endif diff --git a/xwayland/meson.build b/xwayland/meson.build index 9d7f3f4a..ec486f58 100644 --- a/xwayland/meson.build +++ b/xwayland/meson.build @@ -1,7 +1,10 @@ lib_wlr_xwayland = static_library( 'wlr_xwayland', files( - 'selection.c', + 'selection/dnd.c', + 'selection/incoming.c', + 'selection/outgoing.c', + 'selection/selection.c', 'sockets.c', 'xwayland.c', 'xwm.c', diff --git a/xwayland/selection.c b/xwayland/selection.c deleted file mode 100644 index 871ce265..00000000 --- a/xwayland/selection.c +++ /dev/null @@ -1,1483 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "xwayland/xwm.h" - -static const size_t incr_chunk_size = 64 * 1024; - -static xcb_atom_t data_device_manager_dnd_action_to_atom( - struct wlr_xwm *xwm, enum wl_data_device_manager_dnd_action action) { - if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) { - return xwm->atoms[DND_ACTION_COPY]; - } else if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE) { - return xwm->atoms[DND_ACTION_MOVE]; - } else if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) { - return xwm->atoms[DND_ACTION_ASK]; - } - return XCB_ATOM_NONE; -} - -static enum wl_data_device_manager_dnd_action - data_device_manager_dnd_action_from_atom(struct wlr_xwm *xwm, - enum atom_name atom) { - if (atom == xwm->atoms[DND_ACTION_COPY] || - atom == xwm->atoms[DND_ACTION_PRIVATE]) { - return WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY; - } else if (atom == xwm->atoms[DND_ACTION_MOVE]) { - return WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE; - } else if (atom == xwm->atoms[DND_ACTION_ASK]) { - return WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK; - } - return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; -} - -static void xwm_selection_send_notify(struct wlr_xwm *xwm, - xcb_selection_request_event_t *req, bool success) { - xcb_selection_notify_event_t selection_notify = { - .response_type = XCB_SELECTION_NOTIFY, - .sequence = 0, - .time = req->time, - .requestor = req->requestor, - .selection = req->selection, - .target = req->target, - .property = success ? req->property : XCB_ATOM_NONE, - }; - - wlr_log(L_DEBUG, "SendEvent destination=%d SelectionNotify(31) time=%d " - "requestor=%d selection=%d target=%d property=%d", req->requestor, - req->time, req->requestor, req->selection, req->target, - selection_notify.property); - xcb_send_event(xwm->xcb_conn, - 0, // propagate - req->requestor, - XCB_EVENT_MASK_NO_EVENT, - (const char *)&selection_notify); - xcb_flush(xwm->xcb_conn); -} - -static int xwm_selection_flush_source_data( - struct wlr_xwm_selection_transfer *transfer) { - xcb_change_property(transfer->selection->xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - transfer->request.requestor, - transfer->request.property, - transfer->request.target, - 8, // format - transfer->source_data.size, - transfer->source_data.data); - xcb_flush(transfer->selection->xwm->xcb_conn); - transfer->property_set = true; - size_t length = transfer->source_data.size; - transfer->source_data.size = 0; - return length; -} - -static void xwm_selection_transfer_remove_source( - struct wlr_xwm_selection_transfer *transfer) { - if (transfer->source != NULL) { - wl_event_source_remove(transfer->source); - transfer->source = NULL; - } -} - -static void xwm_selection_transfer_close_source_fd( - struct wlr_xwm_selection_transfer *transfer) { - if (transfer->source_fd >= 0) { - close(transfer->source_fd); - transfer->source_fd = -1; - } -} - -static void xwm_selection_transfer_start_outgoing( - struct wlr_xwm_selection_transfer *transfer); - -static void xwm_selection_transfer_destroy_outgoing( - struct wlr_xwm_selection_transfer *transfer) { - wl_list_remove(&transfer->outgoing_link); - - // Start next queued transfer - struct wlr_xwm_selection_transfer *first = NULL; - if (!wl_list_empty(&transfer->selection->outgoing)) { - first = wl_container_of(transfer->selection->outgoing.prev, first, - outgoing_link); - xwm_selection_transfer_start_outgoing(first); - } - - xwm_selection_transfer_remove_source(transfer); - xwm_selection_transfer_close_source_fd(transfer); - wl_array_release(&transfer->source_data); - free(transfer); -} - -static int xwm_data_source_read(int fd, uint32_t mask, void *data) { - struct wlr_xwm_selection_transfer *transfer = data; - struct wlr_xwm *xwm = transfer->selection->xwm; - - void *p; - size_t current = transfer->source_data.size; - if (transfer->source_data.size < incr_chunk_size) { - p = wl_array_add(&transfer->source_data, incr_chunk_size); - if (p == NULL) { - wlr_log(L_ERROR, "Could not allocate selection source_data"); - goto error_out; - } - } else { - p = (char *)transfer->source_data.data + transfer->source_data.size; - } - - size_t available = transfer->source_data.alloc - current; - ssize_t len = read(fd, p, available); - if (len == -1) { - wlr_log(L_ERROR, "read error from data source: %m"); - goto error_out; - } - - wlr_log(L_DEBUG, "read %ld bytes (available %zu, mask 0x%x)", len, - available, mask); - - transfer->source_data.size = current + len; - if (transfer->source_data.size >= incr_chunk_size) { - if (!transfer->incr) { - wlr_log(L_DEBUG, "got %zu bytes, starting incr", - transfer->source_data.size); - - xcb_change_property(xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - transfer->request.requestor, - transfer->request.property, - xwm->atoms[INCR], - 32, /* format */ - 1, &incr_chunk_size); - transfer->incr = true; - transfer->property_set = true; - transfer->flush_property_on_delete = true; - xwm_selection_transfer_remove_source(transfer); - xwm_selection_send_notify(xwm, &transfer->request, true); - } else if (transfer->property_set) { - wlr_log(L_DEBUG, "got %zu bytes, waiting for property delete", - transfer->source_data.size); - - transfer->flush_property_on_delete = true; - xwm_selection_transfer_remove_source(transfer); - } else { - wlr_log(L_DEBUG, "got %zu bytes, property deleted, setting new " - "property", transfer->source_data.size); - xwm_selection_flush_source_data(transfer); - } - } else if (len == 0 && !transfer->incr) { - wlr_log(L_DEBUG, "non-incr transfer complete"); - xwm_selection_flush_source_data(transfer); - xwm_selection_send_notify(xwm, &transfer->request, true); - xwm_selection_transfer_destroy_outgoing(transfer); - } else if (len == 0 && transfer->incr) { - wlr_log(L_DEBUG, "incr transfer complete"); - - transfer->flush_property_on_delete = true; - if (transfer->property_set) { - wlr_log(L_DEBUG, "got %zu bytes, waiting for property delete", - transfer->source_data.size); - } else { - wlr_log(L_DEBUG, "got %zu bytes, property deleted, setting new " - "property", transfer->source_data.size); - xwm_selection_flush_source_data(transfer); - } - xwm_selection_transfer_remove_source(transfer); - xwm_selection_transfer_close_source_fd(transfer); - } else { - wlr_log(L_DEBUG, "nothing happened, buffered the bytes"); - } - - return 1; - -error_out: - xwm_selection_send_notify(xwm, &transfer->request, false); - xwm_selection_transfer_destroy_outgoing(transfer); - return 0; -} - -static void xwm_send_incr_chunk(struct wlr_xwm_selection_transfer *transfer) { - wlr_log(L_DEBUG, "property deleted"); - - transfer->property_set = false; - if (transfer->flush_property_on_delete) { - wlr_log(L_DEBUG, "setting new property, %zu bytes", - transfer->source_data.size); - transfer->flush_property_on_delete = false; - int length = xwm_selection_flush_source_data(transfer); - - if (transfer->source_fd >= 0) { - xwm_selection_transfer_start_outgoing(transfer); - } else if (length > 0) { - /* Transfer is all done, but queue a flush for - * the delete of the last chunk so we can set - * the 0 sized property to signal the end of - * the transfer. */ - transfer->flush_property_on_delete = true; - wl_array_release(&transfer->source_data); - wl_array_init(&transfer->source_data); - } else { - xwm_selection_transfer_destroy_outgoing(transfer); - } - } -} - -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) { - wlr_data_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; - } - } else if (selection == &selection->xwm->dnd_selection) { - struct wlr_data_source *source = - selection->xwm->seat->drag_source; - if (source != NULL) { - wlr_data_source_send(source, mime_type, fd); - return; - } - } - - wlr_log(L_DEBUG, "not sending selection: no selection source available"); -} - -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; - } - } else if (selection == &selection->xwm->dnd_selection) { - struct wlr_data_source *source = - selection->xwm->seat->drag_source; - if (source != NULL) { - return &source->mime_types; - } - } - return NULL; -} - -static void xwm_selection_transfer_start_outgoing( - struct wlr_xwm_selection_transfer *transfer) { - struct wlr_xwm *xwm = transfer->selection->xwm; - struct wl_event_loop *loop = - wl_display_get_event_loop(xwm->xwayland->wl_display); - transfer->source = wl_event_loop_add_fd(loop, transfer->source_fd, - WL_EVENT_READABLE, xwm_data_source_read, transfer); -} - -/** - * Read the Wayland selection and send it to an Xwayland client. - */ -static void xwm_selection_send_data(struct wlr_xwm_selection *selection, - xcb_selection_request_event_t *req, const char *mime_type) { - // Check MIME type - struct wl_array *mime_types = - xwm_selection_source_get_mime_types(selection); - if (mime_types == NULL) { - wlr_log(L_ERROR, "not sending selection: no MIME type list available"); - xwm_selection_send_notify(selection->xwm, req, false); - return; - } - - bool found = false; - char **mime_type_ptr; - wl_array_for_each(mime_type_ptr, mime_types) { - char *t = *mime_type_ptr; - if (strcmp(t, mime_type) == 0) { - found = true; - break; - } - } - if (!found) { - wlr_log(L_ERROR, "not sending selection: " - "requested an unsupported MIME type %s", mime_type); - xwm_selection_send_notify(selection->xwm, req, false); - return; - } - - struct wlr_xwm_selection_transfer *transfer = - calloc(1, sizeof(struct wlr_xwm_selection_transfer)); - if (transfer == NULL) { - wlr_log(L_ERROR, "Allocation failed"); - return; - } - transfer->selection = selection; - transfer->request = *req; - wl_array_init(&transfer->source_data); - - int p[2]; - if (pipe(p) == -1) { - wlr_log(L_ERROR, "pipe() failed: %m"); - xwm_selection_send_notify(selection->xwm, req, false); - return; - } - fcntl(p[0], F_SETFD, FD_CLOEXEC); - fcntl(p[0], F_SETFL, O_NONBLOCK); - fcntl(p[1], F_SETFD, FD_CLOEXEC); - fcntl(p[1], F_SETFL, O_NONBLOCK); - - transfer->source_fd = p[0]; - - wlr_log(L_DEBUG, "Sending Wayland selection %u to Xwayland window with " - "MIME type %s, target %u", req->target, mime_type, req->target); - xwm_selection_source_send(selection, mime_type, p[1]); - - wl_list_insert(&selection->outgoing, &transfer->outgoing_link); - - // We can only handle one transfer at a time - if (wl_list_length(&selection->outgoing) == 1) { - xwm_selection_transfer_start_outgoing(transfer); - } -} - -static xcb_atom_t xwm_mime_type_to_atom(struct wlr_xwm *xwm, char *mime_type) { - if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) { - return xwm->atoms[UTF8_STRING]; - } else if (strcmp(mime_type, "text/plain") == 0) { - return xwm->atoms[TEXT]; - } - - xcb_intern_atom_cookie_t cookie = - xcb_intern_atom(xwm->xcb_conn, 0, strlen(mime_type), mime_type); - xcb_intern_atom_reply_t *reply = - xcb_intern_atom_reply(xwm->xcb_conn, cookie, NULL); - if (reply == NULL) { - return XCB_ATOM_NONE; - } - xcb_atom_t atom = reply->atom; - free(reply); - return atom; -} - -static void xwm_dnd_send_event(struct wlr_xwm *xwm, xcb_atom_t type, - xcb_client_message_data_t *data) { - struct wlr_xwayland_surface *dest = xwm->drag_focus; - assert(dest != NULL); - - xcb_client_message_event_t event = { - .response_type = XCB_CLIENT_MESSAGE, - .format = 32, - .sequence = 0, - .window = dest->window_id, - .type = type, - .data = *data, - }; - - xcb_send_event(xwm->xcb_conn, - 0, // propagate - dest->window_id, - XCB_EVENT_MASK_NO_EVENT, - (const char *)&event); - xcb_flush(xwm->xcb_conn); -} - -static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { - struct wlr_drag *drag = xwm->drag; - assert(drag != NULL); - struct wl_array *mime_types = &drag->source->mime_types; - - xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_window; - data.data32[1] = XDND_VERSION << 24; - - // If we have 3 MIME types or less, we can send them directly in the - // DND_ENTER message - size_t n = mime_types->size / sizeof(char *); - if (n <= 3) { - size_t i = 0; - char **mime_type_ptr; - wl_array_for_each(mime_type_ptr, mime_types) { - char *mime_type = *mime_type_ptr; - data.data32[2+i] = xwm_mime_type_to_atom(xwm, mime_type); - ++i; - } - } else { - // Let the client know that targets are not contained in the message - // data and must be retrieved with the DND_TYPE_LIST property - data.data32[1] |= 1; - - xcb_atom_t targets[n]; - size_t i = 0; - char **mime_type_ptr; - wl_array_for_each(mime_type_ptr, mime_types) { - char *mime_type = *mime_type_ptr; - targets[i] = xwm_mime_type_to_atom(xwm, mime_type); - ++i; - } - - xcb_change_property(xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - xwm->dnd_window, - xwm->atoms[DND_TYPE_LIST], - XCB_ATOM_ATOM, - 32, // format - n, targets); - } - - xwm_dnd_send_event(xwm, xwm->atoms[DND_ENTER], &data); -} - -static void xwm_dnd_send_position(struct wlr_xwm *xwm, uint32_t time, int16_t x, - int16_t y) { - struct wlr_drag *drag = xwm->drag; - assert(drag != NULL); - - xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_window; - data.data32[2] = (x << 16) | y; - data.data32[3] = time; - data.data32[4] = - data_device_manager_dnd_action_to_atom(xwm, drag->source->actions); - - xwm_dnd_send_event(xwm, xwm->atoms[DND_POSITION], &data); -} - -static void xwm_dnd_send_drop(struct wlr_xwm *xwm, uint32_t time) { - struct wlr_drag *drag = xwm->drag; - assert(drag != NULL); - struct wlr_xwayland_surface *dest = xwm->drag_focus; - assert(dest != NULL); - - xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_window; - data.data32[2] = time; - - xwm_dnd_send_event(xwm, xwm->atoms[DND_DROP], &data); -} - -static void xwm_dnd_send_leave(struct wlr_xwm *xwm) { - struct wlr_drag *drag = xwm->drag; - assert(drag != NULL); - struct wlr_xwayland_surface *dest = xwm->drag_focus; - assert(dest != NULL); - - xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_window; - - xwm_dnd_send_event(xwm, xwm->atoms[DND_LEAVE], &data); -} - -/*static void xwm_dnd_send_finished(struct wlr_xwm *xwm) { - struct wlr_drag *drag = xwm->drag; - assert(drag != NULL); - struct wlr_xwayland_surface *dest = xwm->drag_focus; - assert(dest != NULL); - - xcb_client_message_data_t data = { 0 }; - data.data32[0] = xwm->dnd_window; - data.data32[1] = drag->source->accepted; - - if (drag->source->accepted) { - data.data32[2] = data_device_manager_dnd_action_to_atom(xwm, - drag->source->current_dnd_action); - } - - xwm_dnd_send_event(xwm, xwm->atoms[DND_FINISHED], &data); -}*/ - -static void xwm_selection_send_targets(struct wlr_xwm_selection *selection, - xcb_selection_request_event_t *req) { - 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_ERROR, "not sending selection targets: " - "no selection source available"); - xwm_selection_send_notify(selection->xwm, req, false); - return; - } - - size_t n = 2 + mime_types->size / sizeof(char *); - xcb_atom_t targets[n]; - targets[0] = xwm->atoms[TIMESTAMP]; - targets[1] = xwm->atoms[TARGETS]; - - size_t i = 0; - char **mime_type_ptr; - wl_array_for_each(mime_type_ptr, mime_types) { - char *mime_type = *mime_type_ptr; - targets[2+i] = xwm_mime_type_to_atom(xwm, mime_type); - ++i; - } - - xcb_change_property(xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - req->requestor, - req->property, - XCB_ATOM_ATOM, - 32, // format - n, targets); - - xwm_selection_send_notify(selection->xwm, req, true); -} - -static void xwm_selection_send_timestamp(struct wlr_xwm_selection *selection, - xcb_selection_request_event_t *req) { - xcb_change_property(selection->xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - req->requestor, - req->property, - XCB_ATOM_INTEGER, - 32, // format - 1, &selection->timestamp); - - xwm_selection_send_notify(selection->xwm, req, true); -} - -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 if (selection_atom == xwm->atoms[DND_SELECTION]) { - return &xwm->dnd_selection; - } else { - return NULL; - } -} - -static char *xwm_mime_type_from_atom(struct wlr_xwm *xwm, xcb_atom_t atom) { - if (atom == xwm->atoms[UTF8_STRING]) { - return strdup("text/plain;charset=utf-8"); - } else if (atom == xwm->atoms[TEXT]) { - return strdup("text/plain"); - } else { - return xwm_get_atom_name(xwm, atom); - } -} - -static void xwm_handle_selection_request(struct wlr_xwm *xwm, - xcb_selection_request_event_t *req) { - wlr_log(L_DEBUG, "XCB_SELECTION_REQUEST (time=%u owner=%u, requestor=%u " - "selection=%u, target=%u, property=%u)", - req->time, req->owner, req->requestor, req->selection, req->target, - req->property); - - if (req->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_selection_send_notify(xwm, req, true); - return; - } - - struct wlr_xwm_selection *selection = - xwm_get_selection(xwm, req->selection); - if (selection == NULL) { - wlr_log(L_DEBUG, "received selection request for unknown selection"); - return; - } - - if (selection->window != req->owner) { - wlr_log(L_DEBUG, "received selection request with invalid owner"); - return; - } - - // No xwayland surface focused, deny access to clipboard - if (xwm->focus_surface == NULL && xwm->drag_focus == NULL) { - char *selection_name = xwm_get_atom_name(xwm, selection->atom); - wlr_log(L_DEBUG, "denying read access to selection %u (%s): " - "no xwayland surface focused", selection->atom, selection_name); - free(selection_name); - xwm_selection_send_notify(xwm, req, false); - return; - } - - if (req->target == xwm->atoms[TARGETS]) { - xwm_selection_send_targets(selection, req); - } else if (req->target == xwm->atoms[TIMESTAMP]) { - xwm_selection_send_timestamp(selection, req); - } else if (req->target == xwm->atoms[DELETE]) { - xwm_selection_send_notify(selection->xwm, req, true); - } else { - // Send data - char *mime_type = xwm_mime_type_from_atom(xwm, req->target); - if (mime_type == NULL) { - wlr_log(L_ERROR, "ignoring selection request: unknown atom %u", - req->target); - xwm_selection_send_notify(xwm, req, false); - return; - } - xwm_selection_send_data(selection, req, mime_type); - free(mime_type); - } -} - -static void xwm_selection_transfer_destroy_property_reply( - struct wlr_xwm_selection_transfer *transfer) { - free(transfer->property_reply); - transfer->property_reply = NULL; -} - -/** - * Write the X11 selection to a Wayland client. - */ -static int xwm_data_source_write(int fd, uint32_t mask, void *data) { - struct wlr_xwm_selection_transfer *transfer = data; - struct wlr_xwm *xwm = transfer->selection->xwm; - - char *property = xcb_get_property_value(transfer->property_reply); - int remainder = xcb_get_property_value_length(transfer->property_reply) - - transfer->property_start; - - ssize_t len = write(fd, property + transfer->property_start, remainder); - if (len == -1) { - xwm_selection_transfer_destroy_property_reply(transfer); - xwm_selection_transfer_remove_source(transfer); - xwm_selection_transfer_close_source_fd(transfer); - wlr_log(L_ERROR, "write error to target fd: %m"); - return 1; - } - - wlr_log(L_DEBUG, "wrote %ld (chunk size %ld) of %d bytes", - transfer->property_start + len, - len, xcb_get_property_value_length(transfer->property_reply)); - - transfer->property_start += len; - if (len == remainder) { - xwm_selection_transfer_destroy_property_reply(transfer); - xwm_selection_transfer_remove_source(transfer); - - if (transfer->incr) { - wlr_log(L_DEBUG, "deleting property"); - xcb_delete_property(xwm->xcb_conn, transfer->selection->window, - xwm->atoms[WL_SELECTION]); - xcb_flush(xwm->xcb_conn); - } else { - wlr_log(L_DEBUG, "transfer complete"); - xwm_selection_transfer_close_source_fd(transfer); - } - } - - return 1; -} - -static void xwm_write_property(struct wlr_xwm_selection_transfer *transfer, - xcb_get_property_reply_t *reply) { - struct wlr_xwm *xwm = transfer->selection->xwm; - - transfer->property_start = 0; - transfer->property_reply = reply; - - xwm_data_source_write(transfer->source_fd, WL_EVENT_WRITABLE, transfer); - - if (transfer->property_reply != NULL) { - struct wl_event_loop *loop = - wl_display_get_event_loop(xwm->xwayland->wl_display); - transfer->source = wl_event_loop_add_fd(loop, - transfer->source_fd, WL_EVENT_WRITABLE, xwm_data_source_write, - transfer); - } -} - -static void xwm_get_incr_chunk(struct wlr_xwm_selection_transfer *transfer) { - struct wlr_xwm *xwm = transfer->selection->xwm; - wlr_log(L_DEBUG, "xwm_get_incr_chunk"); - - xcb_get_property_cookie_t cookie = xcb_get_property(xwm->xcb_conn, - 0, // delete - transfer->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) { - wlr_log(L_ERROR, "cannot get selection property"); - return; - } - //dump_property(xwm, xwm->atoms[WL_SELECTION], reply); - - if (xcb_get_property_value_length(reply) > 0) { - /* Reply's ownership is transferred to xwm, which is responsible - * for freeing it */ - xwm_write_property(transfer, reply); - } else { - wlr_log(L_DEBUG, "transfer complete"); - xwm_selection_transfer_close_source_fd(transfer); - free(reply); - } -} - -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) { - wlr_log(L_ERROR, "Cannot get selection property"); - return; - } - - struct wlr_xwm_selection_transfer *transfer = &selection->incoming; - if (reply->type == xwm->atoms[INCR]) { - transfer->incr = true; - free(reply); - } else { - transfer->incr = false; - // reply's ownership is transferred to wm, which is responsible - // for freeing it - xwm_write_property(transfer, reply); - } -} - -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 wlr_xwm *xwm = selection->xwm; - struct wlr_xwm_selection_transfer *transfer = &selection->incoming; - - 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, mime_types) { - char *mime_type = *mime_type_ptr; - if (strcmp(mime_type, requested_mime_type) == 0) { - found = true; - mime_type_atom = atoms[i]; - break; - } - ++i; - } - if (!found) { - wlr_log(L_DEBUG, "Cannot send X11 selection to Wayland: " - "unsupported MIME type"); - return; - } - - xcb_convert_selection(xwm->xcb_conn, - selection->window, - selection->atom, - mime_type_atom, - xwm->atoms[WL_SELECTION], - XCB_TIME_CURRENT_TIME); - - xcb_flush(xwm->xcb_conn); - - fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); - transfer->source_fd = fd; -} - -struct x11_data_source { - struct wlr_data_source base; - struct wlr_xwm_selection *selection; - struct wl_array mime_types_atoms; -}; - -static const struct wlr_data_source_impl data_source_impl; - -static struct x11_data_source *data_source_from_wlr_data_source( - struct wlr_data_source *wlr_source) { - assert(wlr_source->impl == &data_source_impl); - return (struct x11_data_source *)wlr_source; -} - -static void data_source_send(struct wlr_data_source *wlr_source, - const char *mime_type, int32_t fd) { - struct x11_data_source *source = - data_source_from_wlr_data_source(wlr_source); - struct wlr_xwm_selection *selection = source->selection; - - source_send(selection, &wlr_source->mime_types, &source->mime_types_atoms, - mime_type, fd); -} - -static void data_source_cancel(struct wlr_data_source *wlr_source) { - struct x11_data_source *source = - data_source_from_wlr_data_source(wlr_source); - wlr_data_source_finish(&source->base); - wl_array_release(&source->mime_types_atoms); - free(source); -} - -static const struct wlr_data_source_impl data_source_impl = { - .send = data_source_send, - .cancel = data_source_cancel, -}; - -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 - selection->window, - xwm->atoms[WL_SELECTION], - XCB_GET_PROPERTY_TYPE_ANY, - 0, // offset - 4096 // length - ); - - xcb_get_property_reply_t *reply = - xcb_get_property_reply(xwm->xcb_conn, cookie, NULL); - if (reply == NULL) { - return false; - } - - if (reply->type != XCB_ATOM_ATOM) { - free(reply); - return false; - } - - xcb_atom_t *value = xcb_get_property_value(reply); - for (uint32_t i = 0; i < reply->value_len; i++) { - char *mime_type = NULL; - - if (value[i] == xwm->atoms[UTF8_STRING]) { - mime_type = strdup("text/plain;charset=utf-8"); - } else if (value[i] == xwm->atoms[TEXT]) { - mime_type = strdup("text/plain"); - } else if (value[i] != xwm->atoms[TARGETS] && - value[i] != xwm->atoms[TIMESTAMP]) { - xcb_get_atom_name_cookie_t name_cookie = - xcb_get_atom_name(xwm->xcb_conn, value[i]); - xcb_get_atom_name_reply_t *name_reply = - xcb_get_atom_name_reply(xwm->xcb_conn, name_cookie, NULL); - if (name_reply == NULL) { - continue; - } - size_t len = xcb_get_atom_name_name_length(name_reply); - char *name = xcb_get_atom_name_name(name_reply); // not a C string - if (memchr(name, '/', len) != NULL) { - mime_type = malloc((len + 1) * sizeof(char)); - if (mime_type == NULL) { - free(name_reply); - continue; - } - memcpy(mime_type, name, len); - mime_type[len] = '\0'; - } - free(name_reply); - } - - if (mime_type != NULL) { - char **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(mime_types_atoms, sizeof(*atom_ptr)); - if (atom_ptr == NULL) { - break; - } - *atom_ptr = value[i]; - } - } - - 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, &data_source_impl); - - 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 { - wlr_data_source_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); - } - } else if (selection == &xwm->dnd_selection) { - // TODO - } -} - -static void xwm_handle_selection_notify(struct wlr_xwm *xwm, - xcb_selection_notify_event_t *event) { - wlr_log(L_DEBUG, "XCB_SELECTION_NOTIFY (selection=%u, property=%u, target=%u)", - event->selection, event->property, - event->target); - - struct wlr_xwm_selection *selection = - xwm_get_selection(xwm, event->selection); - if (selection == NULL) { - return; - } - - if (event->property == XCB_ATOM_NONE) { - wlr_log(L_ERROR, "convert selection failed"); - } else if (event->target == xwm->atoms[TARGETS]) { - // No xwayland surface focused, deny access to clipboard - if (xwm->focus_surface == NULL) { - wlr_log(L_DEBUG, "denying write access to clipboard: " - "no xwayland surface focused"); - return; - } - - // This sets the Wayland clipboard (by calling wlr_seat_set_selection) - xwm_selection_get_targets(selection); - } else { - xwm_selection_get_data(selection); - } -} - -static int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, - xcb_xfixes_selection_notify_event_t *event) { - wlr_log(L_DEBUG, "XCB_XFIXES_SELECTION_NOTIFY (selection=%u, owner=%u)", - event->selection, event->owner); - - struct wlr_xwm_selection *selection = - xwm_get_selection(xwm, event->selection); - if (selection == NULL) { - return 0; - } - - if (event->owner == XCB_WINDOW_NONE) { - if (selection->owner != selection->window) { - // A real X client selection went away, not our - // proxy selection - 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)); - } else if (selection == &xwm->dnd_selection) { - // TODO: DND - } else { - wlr_log(L_DEBUG, "X11 selection has been cleared, but cannot " - "clear Wayland selection"); - } - } - - selection->owner = XCB_WINDOW_NONE; - return 1; - } - - selection->owner = event->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 (event->owner == selection->window) { - selection->timestamp = event->timestamp; - return 1; - } - - struct wlr_xwm_selection_transfer *transfer = &selection->incoming; - transfer->incr = false; - // doing this will give a selection notify where we actually handle the sync - xcb_convert_selection(xwm->xcb_conn, selection->window, - selection->atom, - xwm->atoms[TARGETS], - xwm->atoms[WL_SELECTION], - event->timestamp); - xcb_flush(xwm->xcb_conn); - - return 1; -} - -static int xwm_handle_selection_property_notify(struct wlr_xwm *xwm, - xcb_property_notify_event_t *event) { - struct wlr_xwm_selection *selections[] = { - &xwm->clipboard_selection, - &xwm->primary_selection, - &xwm->dnd_selection, - }; - - for (size_t i = 0; i < sizeof(selections)/sizeof(selections[0]); ++i) { - struct wlr_xwm_selection *selection = selections[i]; - - if (event->window == selection->window) { - if (event->state == XCB_PROPERTY_NEW_VALUE && - event->atom == xwm->atoms[WL_SELECTION] && - selection->incoming.incr) { - xwm_get_incr_chunk(&selection->incoming); - } - return 1; - } - - struct wlr_xwm_selection_transfer *outgoing; - wl_list_for_each(outgoing, &selection->outgoing, outgoing_link) { - if (event->window == outgoing->request.requestor) { - if (event->state == XCB_PROPERTY_DELETE && - event->atom == outgoing->request.property && - outgoing->incr) { - xwm_send_incr_chunk(outgoing); - } - return 1; - } - } - } - - return 0; -} - -int xwm_handle_selection_event(struct wlr_xwm *xwm, - xcb_generic_event_t *event) { - if (xwm->seat == NULL) { - wlr_log(L_DEBUG, "not handling selection events:" - "no seat assigned to xwayland"); - return 0; - } - - switch (event->response_type & ~0x80) { - case XCB_SELECTION_NOTIFY: - xwm_handle_selection_notify(xwm, (xcb_selection_notify_event_t *)event); - return 1; - case XCB_PROPERTY_NOTIFY: - return xwm_handle_selection_property_notify(xwm, - (xcb_property_notify_event_t *)event); - case XCB_SELECTION_REQUEST: - xwm_handle_selection_request(xwm, - (xcb_selection_request_event_t *)event); - return 1; - } - - switch (event->response_type - xwm->xfixes->first_event) { - case XCB_XFIXES_SELECTION_NOTIFY: - // an X11 window has copied something to the clipboard - return xwm_handle_xfixes_selection_notify(xwm, - (xcb_xfixes_selection_notify_event_t *)event); - } - - return 0; -} - -int xwm_handle_selection_client_message(struct wlr_xwm *xwm, - xcb_client_message_event_t *ev) { - if (ev->type == xwm->atoms[DND_STATUS]) { - if (xwm->drag == NULL) { - wlr_log(L_DEBUG, "ignoring XdndStatus client message because " - "there's no drag"); - return 1; - } - - xcb_client_message_data_t *data = &ev->data; - xcb_window_t target_window = data->data32[0]; - bool accepted = data->data32[1] & 1; - xcb_atom_t action_atom = data->data32[4]; - - if (xwm->drag_focus == NULL || - target_window != xwm->drag_focus->window_id) { - wlr_log(L_DEBUG, "ignoring XdndStatus client message because " - "it doesn't match the current drag focus window ID"); - return 1; - } - - enum wl_data_device_manager_dnd_action action = - data_device_manager_dnd_action_from_atom(xwm, action_atom); - - struct wlr_drag *drag = xwm->drag; - assert(drag != NULL); - - drag->source->accepted = accepted; - wlr_data_source_dnd_action(drag->source, action); - - wlr_log(L_DEBUG, "DND_STATUS window=%d accepted=%d action=%d", - target_window, accepted, action); - return 1; - } else if (ev->type == xwm->atoms[DND_FINISHED]) { - // This should only happen after the drag has ended, but before the drag - // source is destroyed - if (xwm->seat == NULL || xwm->seat->drag_source == NULL || - xwm->drag != NULL) { - wlr_log(L_DEBUG, "ignoring XdndFinished client message because " - "there's no finished drag"); - return 1; - } - - struct wlr_data_source *source = xwm->seat->drag_source; - - xcb_client_message_data_t *data = &ev->data; - xcb_window_t target_window = data->data32[0]; - bool performed = data->data32[1] & 1; - xcb_atom_t action_atom = data->data32[2]; - - if (xwm->drag_focus == NULL || - target_window != xwm->drag_focus->window_id) { - wlr_log(L_DEBUG, "ignoring XdndFinished client message because " - "it doesn't match the finished drag focus window ID"); - return 1; - } - - enum wl_data_device_manager_dnd_action action = - data_device_manager_dnd_action_from_atom(xwm, action_atom); - - if (performed) { - wlr_data_source_dnd_finish(source); - } - - wlr_log(L_DEBUG, "DND_FINISH window=%d performed=%d action=%d", - target_window, performed, action); - return 1; - } else { - return 0; - } -} - -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->incoming.selection = selection; - wl_list_init(&selection->outgoing); - - 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); -} - -void xwm_selection_init(struct wlr_xwm *xwm) { - // Clipboard and primary selection - uint32_t selection_values[] = { - XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE - }; - xwm->selection_window = xcb_generate_id(xwm->xcb_conn); - xcb_create_window(xwm->xcb_conn, - XCB_COPY_FROM_PARENT, - xwm->selection_window, - xwm->screen->root, - 0, 0, - 10, 10, - 0, - XCB_WINDOW_CLASS_INPUT_OUTPUT, - xwm->screen->root_visual, - XCB_CW_EVENT_MASK, selection_values); - - xcb_set_selection_owner(xwm->xcb_conn, - xwm->selection_window, - xwm->atoms[CLIPBOARD_MANAGER], - XCB_TIME_CURRENT_TIME); - - selection_init(xwm, &xwm->clipboard_selection, xwm->atoms[CLIPBOARD]); - selection_init(xwm, &xwm->primary_selection, xwm->atoms[PRIMARY]); - - // Drag'n'drop - uint32_t dnd_values[] = { - XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE - }; - xwm->dnd_window = xcb_generate_id(xwm->xcb_conn); - xcb_create_window(xwm->xcb_conn, - XCB_COPY_FROM_PARENT, - xwm->dnd_window, - xwm->screen->root, - 0, 0, - 8192, 8192, - 0, - XCB_WINDOW_CLASS_INPUT_ONLY, - xwm->screen->root_visual, - XCB_CW_EVENT_MASK, dnd_values); - - uint32_t version = XDND_VERSION; - xcb_change_property(xwm->xcb_conn, - XCB_PROP_MODE_REPLACE, - xwm->dnd_window, - xwm->atoms[DND_AWARE], - XCB_ATOM_ATOM, - 32, // format - 1, &version); - - selection_init(xwm, &xwm->dnd_selection, xwm->atoms[DND_SELECTION]); -} - -void xwm_selection_finish(struct wlr_xwm *xwm) { - if (!xwm) { - return; - } - if (xwm->selection_window) { - xcb_destroy_window(xwm->xcb_conn, xwm->selection_window); - } - if (xwm->dnd_window) { - xcb_destroy_window(xwm->xcb_conn, xwm->dnd_window); - } - if (xwm->seat) { - if (xwm->seat->selection_source && - xwm->seat->selection_source->impl == &data_source_impl) { - wlr_seat_set_selection(xwm->seat, NULL, - wl_display_next_serial(xwm->xwayland->wl_display)); - } - if (xwm->seat->primary_selection_source && - xwm->seat->primary_selection_source->cancel == primary_selection_source_cancel) { - wlr_seat_set_primary_selection(xwm->seat, NULL, - wl_display_next_serial(xwm->xwayland->wl_display)); - } - wlr_xwayland_set_seat(xwm->xwayland, NULL); - } -} - -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 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); - struct wlr_data_source *source = seat->selection_source; - - if (source != NULL && source->impl == &data_source_impl) { - return; - } - - 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; - } - - xwm_selection_set_owner(&xwm->primary_selection, source != NULL); -} - -static void seat_handle_drag_focus(struct wl_listener *listener, void *data) { - struct wlr_drag *drag = data; - struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_focus); - - struct wlr_xwayland_surface *focus = NULL; - if (drag->focus != NULL) { - // TODO: check for subsurfaces? - struct wlr_xwayland_surface *surface; - wl_list_for_each(surface, &xwm->surfaces, link) { - if (surface->surface == drag->focus) { - focus = surface; - break; - } - } - } - - if (focus == xwm->drag_focus) { - return; - } - - if (xwm->drag_focus != NULL) { - wlr_data_source_dnd_action(drag->source, - WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE); - xwm_dnd_send_leave(xwm); - } - - xwm->drag_focus = focus; - - if (xwm->drag_focus != NULL) { - xwm_dnd_send_enter(xwm); - } -} - -static void seat_handle_drag_motion(struct wl_listener *listener, void *data) { - struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_motion); - struct wlr_drag_motion_event *event = data; - struct wlr_xwayland_surface *surface = xwm->drag_focus; - - if (surface == NULL) { - return; // No xwayland surface focused - } - - xwm_dnd_send_position(xwm, event->time, surface->x + (int16_t)event->sx, - surface->y + (int16_t)event->sy); -} - -static void seat_handle_drag_drop(struct wl_listener *listener, void *data) { - struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_drop); - struct wlr_drag_drop_event *event = data; - - if (xwm->drag_focus == NULL) { - return; // No xwayland surface focused - } - - wlr_log(L_DEBUG, "Wayland drag dropped over an Xwayland window"); - xwm_dnd_send_drop(xwm, event->time); -} - -static void seat_handle_drag_destroy(struct wl_listener *listener, void *data) { - struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_destroy); - - // Don't reset drag focus yet because the target will read the drag source - // right after - if (xwm->drag_focus != NULL && !xwm->drag->source->accepted) { - wlr_log(L_DEBUG, "Wayland drag cancelled over an Xwayland window"); - xwm_dnd_send_leave(xwm); - } - - wl_list_remove(&xwm->seat_drag_focus.link); - wl_list_remove(&xwm->seat_drag_motion.link); - wl_list_remove(&xwm->seat_drag_drop.link); - wl_list_remove(&xwm->seat_drag_destroy.link); - xwm->drag = NULL; -} - -static void seat_handle_drag_source_destroy(struct wl_listener *listener, - void *data) { - struct wlr_xwm *xwm = - wl_container_of(listener, xwm, seat_drag_source_destroy); - - wl_list_remove(&xwm->seat_drag_source_destroy.link); - xwm->drag_focus = NULL; -} - -static void seat_handle_start_drag(struct wl_listener *listener, void *data) { - struct wlr_drag *drag = data; - struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_start_drag); - - xwm_selection_set_owner(&xwm->dnd_selection, drag != NULL); - xwm->drag = drag; - xwm->drag_focus = NULL; - - if (drag != NULL) { - wl_signal_add(&drag->events.focus, &xwm->seat_drag_focus); - xwm->seat_drag_focus.notify = seat_handle_drag_focus; - wl_signal_add(&drag->events.motion, &xwm->seat_drag_motion); - xwm->seat_drag_motion.notify = seat_handle_drag_motion; - wl_signal_add(&drag->events.drop, &xwm->seat_drag_drop); - xwm->seat_drag_drop.notify = seat_handle_drag_drop; - wl_signal_add(&drag->events.destroy, &xwm->seat_drag_destroy); - xwm->seat_drag_destroy.notify = seat_handle_drag_destroy; - - wl_signal_add(&drag->source->events.destroy, - &xwm->seat_drag_source_destroy); - xwm->seat_drag_source_destroy.notify = seat_handle_drag_source_destroy; - } -} - -void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat) { - if (xwm->seat != NULL) { - wl_list_remove(&xwm->seat_selection.link); - wl_list_remove(&xwm->seat_primary_selection.link); - wl_list_remove(&xwm->seat_start_drag.link); - xwm->seat = NULL; - } - - if (seat == NULL) { - return; - } - - xwm->seat = 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; - wl_signal_add(&seat->events.start_drag, &xwm->seat_start_drag); - xwm->seat_start_drag.notify = seat_handle_start_drag; - - seat_handle_selection(&xwm->seat_selection, seat); - seat_handle_primary_selection(&xwm->seat_primary_selection, seat); -} diff --git a/xwayland/selection/dnd.c b/xwayland/selection/dnd.c new file mode 100644 index 00000000..ed265c28 --- /dev/null +++ b/xwayland/selection/dnd.c @@ -0,0 +1,340 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "xwayland/xwm.h" +#include "xwayland/selection.h" + +static xcb_atom_t data_device_manager_dnd_action_to_atom( + struct wlr_xwm *xwm, enum wl_data_device_manager_dnd_action action) { + if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY) { + return xwm->atoms[DND_ACTION_COPY]; + } else if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE) { + return xwm->atoms[DND_ACTION_MOVE]; + } else if (action & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) { + return xwm->atoms[DND_ACTION_ASK]; + } + return XCB_ATOM_NONE; +} + +static enum wl_data_device_manager_dnd_action + data_device_manager_dnd_action_from_atom(struct wlr_xwm *xwm, + enum atom_name atom) { + if (atom == xwm->atoms[DND_ACTION_COPY] || + atom == xwm->atoms[DND_ACTION_PRIVATE]) { + return WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY; + } else if (atom == xwm->atoms[DND_ACTION_MOVE]) { + return WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE; + } else if (atom == xwm->atoms[DND_ACTION_ASK]) { + return WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK; + } + return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; +} + +static void xwm_dnd_send_event(struct wlr_xwm *xwm, xcb_atom_t type, + xcb_client_message_data_t *data) { + struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); + + xcb_client_message_event_t event = { + .response_type = XCB_CLIENT_MESSAGE, + .format = 32, + .sequence = 0, + .window = dest->window_id, + .type = type, + .data = *data, + }; + + xcb_send_event(xwm->xcb_conn, + 0, // propagate + dest->window_id, + XCB_EVENT_MASK_NO_EVENT, + (const char *)&event); + xcb_flush(xwm->xcb_conn); +} + +static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + struct wl_array *mime_types = &drag->source->mime_types; + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_window; + data.data32[1] = XDND_VERSION << 24; + + // If we have 3 MIME types or less, we can send them directly in the + // DND_ENTER message + size_t n = mime_types->size / sizeof(char *); + if (n <= 3) { + size_t i = 0; + char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, mime_types) { + char *mime_type = *mime_type_ptr; + data.data32[2+i] = xwm_mime_type_to_atom(xwm, mime_type); + ++i; + } + } else { + // Let the client know that targets are not contained in the message + // data and must be retrieved with the DND_TYPE_LIST property + data.data32[1] |= 1; + + xcb_atom_t targets[n]; + size_t i = 0; + char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, mime_types) { + char *mime_type = *mime_type_ptr; + targets[i] = xwm_mime_type_to_atom(xwm, mime_type); + ++i; + } + + xcb_change_property(xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + xwm->dnd_window, + xwm->atoms[DND_TYPE_LIST], + XCB_ATOM_ATOM, + 32, // format + n, targets); + } + + xwm_dnd_send_event(xwm, xwm->atoms[DND_ENTER], &data); +} + +static void xwm_dnd_send_position(struct wlr_xwm *xwm, uint32_t time, int16_t x, + int16_t y) { + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_window; + data.data32[2] = (x << 16) | y; + data.data32[3] = time; + data.data32[4] = + data_device_manager_dnd_action_to_atom(xwm, drag->source->actions); + + xwm_dnd_send_event(xwm, xwm->atoms[DND_POSITION], &data); +} + +static void xwm_dnd_send_drop(struct wlr_xwm *xwm, uint32_t time) { + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_window; + data.data32[2] = time; + + xwm_dnd_send_event(xwm, xwm->atoms[DND_DROP], &data); +} + +static void xwm_dnd_send_leave(struct wlr_xwm *xwm) { + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_window; + + xwm_dnd_send_event(xwm, xwm->atoms[DND_LEAVE], &data); +} + +/*static void xwm_dnd_send_finished(struct wlr_xwm *xwm) { + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + struct wlr_xwayland_surface *dest = xwm->drag_focus; + assert(dest != NULL); + + xcb_client_message_data_t data = { 0 }; + data.data32[0] = xwm->dnd_window; + data.data32[1] = drag->source->accepted; + + if (drag->source->accepted) { + data.data32[2] = data_device_manager_dnd_action_to_atom(xwm, + drag->source->current_dnd_action); + } + + xwm_dnd_send_event(xwm, xwm->atoms[DND_FINISHED], &data); +}*/ + +int xwm_handle_selection_client_message(struct wlr_xwm *xwm, + xcb_client_message_event_t *ev) { + if (ev->type == xwm->atoms[DND_STATUS]) { + if (xwm->drag == NULL) { + wlr_log(L_DEBUG, "ignoring XdndStatus client message because " + "there's no drag"); + return 1; + } + + xcb_client_message_data_t *data = &ev->data; + xcb_window_t target_window = data->data32[0]; + bool accepted = data->data32[1] & 1; + xcb_atom_t action_atom = data->data32[4]; + + if (xwm->drag_focus == NULL || + target_window != xwm->drag_focus->window_id) { + wlr_log(L_DEBUG, "ignoring XdndStatus client message because " + "it doesn't match the current drag focus window ID"); + return 1; + } + + enum wl_data_device_manager_dnd_action action = + data_device_manager_dnd_action_from_atom(xwm, action_atom); + + struct wlr_drag *drag = xwm->drag; + assert(drag != NULL); + + drag->source->accepted = accepted; + wlr_data_source_dnd_action(drag->source, action); + + wlr_log(L_DEBUG, "DND_STATUS window=%d accepted=%d action=%d", + target_window, accepted, action); + return 1; + } else if (ev->type == xwm->atoms[DND_FINISHED]) { + // This should only happen after the drag has ended, but before the drag + // source is destroyed + if (xwm->seat == NULL || xwm->seat->drag_source == NULL || + xwm->drag != NULL) { + wlr_log(L_DEBUG, "ignoring XdndFinished client message because " + "there's no finished drag"); + return 1; + } + + struct wlr_data_source *source = xwm->seat->drag_source; + + xcb_client_message_data_t *data = &ev->data; + xcb_window_t target_window = data->data32[0]; + bool performed = data->data32[1] & 1; + xcb_atom_t action_atom = data->data32[2]; + + if (xwm->drag_focus == NULL || + target_window != xwm->drag_focus->window_id) { + wlr_log(L_DEBUG, "ignoring XdndFinished client message because " + "it doesn't match the finished drag focus window ID"); + return 1; + } + + enum wl_data_device_manager_dnd_action action = + data_device_manager_dnd_action_from_atom(xwm, action_atom); + + if (performed) { + wlr_data_source_dnd_finish(source); + } + + wlr_log(L_DEBUG, "DND_FINISH window=%d performed=%d action=%d", + target_window, performed, action); + return 1; + } else { + return 0; + } +} + +static void seat_handle_drag_focus(struct wl_listener *listener, void *data) { + struct wlr_drag *drag = data; + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_focus); + + struct wlr_xwayland_surface *focus = NULL; + if (drag->focus != NULL) { + // TODO: check for subsurfaces? + struct wlr_xwayland_surface *surface; + wl_list_for_each(surface, &xwm->surfaces, link) { + if (surface->surface == drag->focus) { + focus = surface; + break; + } + } + } + + if (focus == xwm->drag_focus) { + return; + } + + if (xwm->drag_focus != NULL) { + wlr_data_source_dnd_action(drag->source, + WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE); + xwm_dnd_send_leave(xwm); + } + + xwm->drag_focus = focus; + + if (xwm->drag_focus != NULL) { + xwm_dnd_send_enter(xwm); + } +} + +static void seat_handle_drag_motion(struct wl_listener *listener, void *data) { + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_motion); + struct wlr_drag_motion_event *event = data; + struct wlr_xwayland_surface *surface = xwm->drag_focus; + + if (surface == NULL) { + return; // No xwayland surface focused + } + + xwm_dnd_send_position(xwm, event->time, surface->x + (int16_t)event->sx, + surface->y + (int16_t)event->sy); +} + +static void seat_handle_drag_drop(struct wl_listener *listener, void *data) { + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_drop); + struct wlr_drag_drop_event *event = data; + + if (xwm->drag_focus == NULL) { + return; // No xwayland surface focused + } + + wlr_log(L_DEBUG, "Wayland drag dropped over an Xwayland window"); + xwm_dnd_send_drop(xwm, event->time); +} + +static void seat_handle_drag_destroy(struct wl_listener *listener, void *data) { + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_drag_destroy); + + // Don't reset drag focus yet because the target will read the drag source + // right after + if (xwm->drag_focus != NULL && !xwm->drag->source->accepted) { + wlr_log(L_DEBUG, "Wayland drag cancelled over an Xwayland window"); + xwm_dnd_send_leave(xwm); + } + + wl_list_remove(&xwm->seat_drag_focus.link); + wl_list_remove(&xwm->seat_drag_motion.link); + wl_list_remove(&xwm->seat_drag_drop.link); + wl_list_remove(&xwm->seat_drag_destroy.link); + xwm->drag = NULL; +} + +static void seat_handle_drag_source_destroy(struct wl_listener *listener, + void *data) { + struct wlr_xwm *xwm = + wl_container_of(listener, xwm, seat_drag_source_destroy); + + wl_list_remove(&xwm->seat_drag_source_destroy.link); + xwm->drag_focus = NULL; +} + +void xwm_seat_handle_start_drag(struct wlr_xwm *xwm, struct wlr_drag *drag) { + xwm->drag = drag; + xwm->drag_focus = NULL; + + if (drag != NULL) { + wl_signal_add(&drag->events.focus, &xwm->seat_drag_focus); + xwm->seat_drag_focus.notify = seat_handle_drag_focus; + wl_signal_add(&drag->events.motion, &xwm->seat_drag_motion); + xwm->seat_drag_motion.notify = seat_handle_drag_motion; + wl_signal_add(&drag->events.drop, &xwm->seat_drag_drop); + xwm->seat_drag_drop.notify = seat_handle_drag_drop; + wl_signal_add(&drag->events.destroy, &xwm->seat_drag_destroy); + xwm->seat_drag_destroy.notify = seat_handle_drag_destroy; + + wl_signal_add(&drag->source->events.destroy, + &xwm->seat_drag_source_destroy); + xwm->seat_drag_source_destroy.notify = seat_handle_drag_source_destroy; + } +} diff --git a/xwayland/selection/incoming.c b/xwayland/selection/incoming.c new file mode 100644 index 00000000..ab06f40d --- /dev/null +++ b/xwayland/selection/incoming.c @@ -0,0 +1,461 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "xwayland/xwm.h" +#include "xwayland/selection.h" + +/** + * Write the X11 selection to a Wayland client. + */ +static int xwm_data_source_write(int fd, uint32_t mask, void *data) { + struct wlr_xwm_selection_transfer *transfer = data; + struct wlr_xwm *xwm = transfer->selection->xwm; + + char *property = xcb_get_property_value(transfer->property_reply); + int remainder = xcb_get_property_value_length(transfer->property_reply) - + transfer->property_start; + + ssize_t len = write(fd, property + transfer->property_start, remainder); + if (len == -1) { + xwm_selection_transfer_destroy_property_reply(transfer); + xwm_selection_transfer_remove_source(transfer); + xwm_selection_transfer_close_source_fd(transfer); + wlr_log(L_ERROR, "write error to target fd: %m"); + return 1; + } + + wlr_log(L_DEBUG, "wrote %ld (chunk size %ld) of %d bytes", + transfer->property_start + len, + len, xcb_get_property_value_length(transfer->property_reply)); + + transfer->property_start += len; + if (len == remainder) { + xwm_selection_transfer_destroy_property_reply(transfer); + xwm_selection_transfer_remove_source(transfer); + + if (transfer->incr) { + wlr_log(L_DEBUG, "deleting property"); + xcb_delete_property(xwm->xcb_conn, transfer->selection->window, + xwm->atoms[WL_SELECTION]); + xcb_flush(xwm->xcb_conn); + } else { + wlr_log(L_DEBUG, "transfer complete"); + xwm_selection_transfer_close_source_fd(transfer); + } + } + + return 1; +} + +static void xwm_write_property(struct wlr_xwm_selection_transfer *transfer, + xcb_get_property_reply_t *reply) { + struct wlr_xwm *xwm = transfer->selection->xwm; + + transfer->property_start = 0; + transfer->property_reply = reply; + + xwm_data_source_write(transfer->source_fd, WL_EVENT_WRITABLE, transfer); + + if (transfer->property_reply != NULL) { + struct wl_event_loop *loop = + wl_display_get_event_loop(xwm->xwayland->wl_display); + transfer->source = wl_event_loop_add_fd(loop, + transfer->source_fd, WL_EVENT_WRITABLE, xwm_data_source_write, + transfer); + } +} + +void xwm_get_incr_chunk(struct wlr_xwm_selection_transfer *transfer) { + struct wlr_xwm *xwm = transfer->selection->xwm; + wlr_log(L_DEBUG, "xwm_get_incr_chunk"); + + xcb_get_property_cookie_t cookie = xcb_get_property(xwm->xcb_conn, + 0, // delete + transfer->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) { + wlr_log(L_ERROR, "cannot get selection property"); + return; + } + //dump_property(xwm, xwm->atoms[WL_SELECTION], reply); + + if (xcb_get_property_value_length(reply) > 0) { + /* Reply's ownership is transferred to xwm, which is responsible + * for freeing it */ + xwm_write_property(transfer, reply); + } else { + wlr_log(L_DEBUG, "transfer complete"); + xwm_selection_transfer_close_source_fd(transfer); + free(reply); + } +} + +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) { + wlr_log(L_ERROR, "Cannot get selection property"); + return; + } + + struct wlr_xwm_selection_transfer *transfer = &selection->incoming; + if (reply->type == xwm->atoms[INCR]) { + transfer->incr = true; + free(reply); + } else { + transfer->incr = false; + // reply's ownership is transferred to wm, which is responsible + // for freeing it + xwm_write_property(transfer, reply); + } +} + +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 wlr_xwm *xwm = selection->xwm; + struct wlr_xwm_selection_transfer *transfer = &selection->incoming; + + 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, mime_types) { + char *mime_type = *mime_type_ptr; + if (strcmp(mime_type, requested_mime_type) == 0) { + found = true; + mime_type_atom = atoms[i]; + break; + } + ++i; + } + if (!found) { + wlr_log(L_DEBUG, "Cannot send X11 selection to Wayland: " + "unsupported MIME type"); + return; + } + + xcb_convert_selection(xwm->xcb_conn, + selection->window, + selection->atom, + mime_type_atom, + xwm->atoms[WL_SELECTION], + XCB_TIME_CURRENT_TIME); + + xcb_flush(xwm->xcb_conn); + + fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); + transfer->source_fd = fd; +} + +struct x11_data_source { + struct wlr_data_source base; + struct wlr_xwm_selection *selection; + struct wl_array mime_types_atoms; +}; + +static const struct wlr_data_source_impl data_source_impl; + +bool wlr_data_source_is_xwayland_data_source( + struct wlr_data_source *wlr_source) { + return wlr_source->impl == &data_source_impl; +} + +static struct x11_data_source *data_source_from_wlr_data_source( + struct wlr_data_source *wlr_source) { + assert(wlr_data_source_is_xwayland_data_source(wlr_source)); + return (struct x11_data_source *)wlr_source; +} + +static void data_source_send(struct wlr_data_source *wlr_source, + const char *mime_type, int32_t fd) { + struct x11_data_source *source = + data_source_from_wlr_data_source(wlr_source); + struct wlr_xwm_selection *selection = source->selection; + + source_send(selection, &wlr_source->mime_types, &source->mime_types_atoms, + mime_type, fd); +} + +static void data_source_cancel(struct wlr_data_source *wlr_source) { + struct x11_data_source *source = + data_source_from_wlr_data_source(wlr_source); + wlr_data_source_finish(&source->base); + wl_array_release(&source->mime_types_atoms); + free(source); +} + +static const struct wlr_data_source_impl data_source_impl = { + .send = data_source_send, + .cancel = data_source_cancel, +}; + +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_cancel( + struct wlr_primary_selection_source *wlr_source); + +bool wlr_primary_selection_source_is_xwayland_primary_selection_source( + struct wlr_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, + int32_t fd) { + struct x11_primary_selection_source *source = + (struct x11_primary_selection_source *)wlr_source; + struct wlr_xwm_selection *selection = source->selection; + + source_send(selection, &wlr_source->mime_types, &source->mime_types_atoms, + mime_type, fd); +} + +static void primary_selection_source_cancel( + struct wlr_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); + 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 + selection->window, + xwm->atoms[WL_SELECTION], + XCB_GET_PROPERTY_TYPE_ANY, + 0, // offset + 4096 // length + ); + + xcb_get_property_reply_t *reply = + xcb_get_property_reply(xwm->xcb_conn, cookie, NULL); + if (reply == NULL) { + return false; + } + + if (reply->type != XCB_ATOM_ATOM) { + free(reply); + return false; + } + + xcb_atom_t *value = xcb_get_property_value(reply); + for (uint32_t i = 0; i < reply->value_len; i++) { + char *mime_type = NULL; + + if (value[i] == xwm->atoms[UTF8_STRING]) { + mime_type = strdup("text/plain;charset=utf-8"); + } else if (value[i] == xwm->atoms[TEXT]) { + mime_type = strdup("text/plain"); + } else if (value[i] != xwm->atoms[TARGETS] && + value[i] != xwm->atoms[TIMESTAMP]) { + xcb_get_atom_name_cookie_t name_cookie = + xcb_get_atom_name(xwm->xcb_conn, value[i]); + xcb_get_atom_name_reply_t *name_reply = + xcb_get_atom_name_reply(xwm->xcb_conn, name_cookie, NULL); + if (name_reply == NULL) { + continue; + } + size_t len = xcb_get_atom_name_name_length(name_reply); + char *name = xcb_get_atom_name_name(name_reply); // not a C string + if (memchr(name, '/', len) != NULL) { + mime_type = malloc((len + 1) * sizeof(char)); + if (mime_type == NULL) { + free(name_reply); + continue; + } + memcpy(mime_type, name, len); + mime_type[len] = '\0'; + } + free(name_reply); + } + + if (mime_type != NULL) { + char **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(mime_types_atoms, sizeof(*atom_ptr)); + if (atom_ptr == NULL) { + break; + } + *atom_ptr = value[i]; + } + } + + 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, &data_source_impl); + + 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 { + wlr_data_source_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); + } + } else if (selection == &xwm->dnd_selection) { + // TODO + } +} + +void xwm_handle_selection_notify(struct wlr_xwm *xwm, + xcb_selection_notify_event_t *event) { + wlr_log(L_DEBUG, "XCB_SELECTION_NOTIFY (selection=%u, property=%u, target=%u)", + event->selection, event->property, + event->target); + + struct wlr_xwm_selection *selection = + xwm_get_selection(xwm, event->selection); + if (selection == NULL) { + return; + } + + if (event->property == XCB_ATOM_NONE) { + wlr_log(L_ERROR, "convert selection failed"); + } else if (event->target == xwm->atoms[TARGETS]) { + // No xwayland surface focused, deny access to clipboard + if (xwm->focus_surface == NULL) { + wlr_log(L_DEBUG, "denying write access to clipboard: " + "no xwayland surface focused"); + return; + } + + // This sets the Wayland clipboard (by calling wlr_seat_set_selection) + xwm_selection_get_targets(selection); + } else { + xwm_selection_get_data(selection); + } +} + +int xwm_handle_xfixes_selection_notify(struct wlr_xwm *xwm, + xcb_xfixes_selection_notify_event_t *event) { + wlr_log(L_DEBUG, "XCB_XFIXES_SELECTION_NOTIFY (selection=%u, owner=%u)", + event->selection, event->owner); + + struct wlr_xwm_selection *selection = + xwm_get_selection(xwm, event->selection); + if (selection == NULL) { + return 0; + } + + if (event->owner == XCB_WINDOW_NONE) { + if (selection->owner != selection->window) { + // A real X client selection went away, not our + // proxy selection + 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)); + } else if (selection == &xwm->dnd_selection) { + // TODO: DND + } else { + wlr_log(L_DEBUG, "X11 selection has been cleared, but cannot " + "clear Wayland selection"); + } + } + + selection->owner = XCB_WINDOW_NONE; + return 1; + } + + selection->owner = event->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 (event->owner == selection->window) { + selection->timestamp = event->timestamp; + return 1; + } + + struct wlr_xwm_selection_transfer *transfer = &selection->incoming; + transfer->incr = false; + // doing this will give a selection notify where we actually handle the sync + xcb_convert_selection(xwm->xcb_conn, selection->window, + selection->atom, + xwm->atoms[TARGETS], + xwm->atoms[WL_SELECTION], + event->timestamp); + xcb_flush(xwm->xcb_conn); + + return 1; +} diff --git a/xwayland/selection/outgoing.c b/xwayland/selection/outgoing.c new file mode 100644 index 00000000..b612b2fb --- /dev/null +++ b/xwayland/selection/outgoing.c @@ -0,0 +1,420 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "xwayland/xwm.h" +#include "xwayland/selection.h" + +static void xwm_selection_send_notify(struct wlr_xwm *xwm, + xcb_selection_request_event_t *req, bool success) { + xcb_selection_notify_event_t selection_notify = { + .response_type = XCB_SELECTION_NOTIFY, + .sequence = 0, + .time = req->time, + .requestor = req->requestor, + .selection = req->selection, + .target = req->target, + .property = success ? req->property : XCB_ATOM_NONE, + }; + + wlr_log(L_DEBUG, "SendEvent destination=%d SelectionNotify(31) time=%d " + "requestor=%d selection=%d target=%d property=%d", req->requestor, + req->time, req->requestor, req->selection, req->target, + selection_notify.property); + xcb_send_event(xwm->xcb_conn, + 0, // propagate + req->requestor, + XCB_EVENT_MASK_NO_EVENT, + (const char *)&selection_notify); + xcb_flush(xwm->xcb_conn); +} + +static int xwm_selection_flush_source_data( + struct wlr_xwm_selection_transfer *transfer) { + xcb_change_property(transfer->selection->xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + transfer->request.requestor, + transfer->request.property, + transfer->request.target, + 8, // format + transfer->source_data.size, + transfer->source_data.data); + xcb_flush(transfer->selection->xwm->xcb_conn); + transfer->property_set = true; + size_t length = transfer->source_data.size; + transfer->source_data.size = 0; + return length; +} + +static void xwm_selection_transfer_start_outgoing( + struct wlr_xwm_selection_transfer *transfer); + +static void xwm_selection_transfer_destroy_outgoing( + struct wlr_xwm_selection_transfer *transfer) { + wl_list_remove(&transfer->outgoing_link); + + // Start next queued transfer + struct wlr_xwm_selection_transfer *first = NULL; + if (!wl_list_empty(&transfer->selection->outgoing)) { + first = wl_container_of(transfer->selection->outgoing.prev, first, + outgoing_link); + xwm_selection_transfer_start_outgoing(first); + } + + xwm_selection_transfer_remove_source(transfer); + xwm_selection_transfer_close_source_fd(transfer); + wl_array_release(&transfer->source_data); + free(transfer); +} + +static int xwm_data_source_read(int fd, uint32_t mask, void *data) { + struct wlr_xwm_selection_transfer *transfer = data; + struct wlr_xwm *xwm = transfer->selection->xwm; + + void *p; + size_t current = transfer->source_data.size; + if (transfer->source_data.size < INCR_CHUNK_SIZE) { + p = wl_array_add(&transfer->source_data, INCR_CHUNK_SIZE); + if (p == NULL) { + wlr_log(L_ERROR, "Could not allocate selection source_data"); + goto error_out; + } + } else { + p = (char *)transfer->source_data.data + transfer->source_data.size; + } + + size_t available = transfer->source_data.alloc - current; + ssize_t len = read(fd, p, available); + if (len == -1) { + wlr_log(L_ERROR, "read error from data source: %m"); + goto error_out; + } + + wlr_log(L_DEBUG, "read %ld bytes (available %zu, mask 0x%x)", len, + available, mask); + + transfer->source_data.size = current + len; + if (transfer->source_data.size >= INCR_CHUNK_SIZE) { + if (!transfer->incr) { + wlr_log(L_DEBUG, "got %zu bytes, starting incr", + transfer->source_data.size); + + size_t incr_chunk_size = INCR_CHUNK_SIZE; + xcb_change_property(xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + transfer->request.requestor, + transfer->request.property, + xwm->atoms[INCR], + 32, /* format */ + 1, &incr_chunk_size); + transfer->incr = true; + transfer->property_set = true; + transfer->flush_property_on_delete = true; + xwm_selection_transfer_remove_source(transfer); + xwm_selection_send_notify(xwm, &transfer->request, true); + } else if (transfer->property_set) { + wlr_log(L_DEBUG, "got %zu bytes, waiting for property delete", + transfer->source_data.size); + + transfer->flush_property_on_delete = true; + xwm_selection_transfer_remove_source(transfer); + } else { + wlr_log(L_DEBUG, "got %zu bytes, property deleted, setting new " + "property", transfer->source_data.size); + xwm_selection_flush_source_data(transfer); + } + } else if (len == 0 && !transfer->incr) { + wlr_log(L_DEBUG, "non-incr transfer complete"); + xwm_selection_flush_source_data(transfer); + xwm_selection_send_notify(xwm, &transfer->request, true); + xwm_selection_transfer_destroy_outgoing(transfer); + } else if (len == 0 && transfer->incr) { + wlr_log(L_DEBUG, "incr transfer complete"); + + transfer->flush_property_on_delete = true; + if (transfer->property_set) { + wlr_log(L_DEBUG, "got %zu bytes, waiting for property delete", + transfer->source_data.size); + } else { + wlr_log(L_DEBUG, "got %zu bytes, property deleted, setting new " + "property", transfer->source_data.size); + xwm_selection_flush_source_data(transfer); + } + xwm_selection_transfer_remove_source(transfer); + xwm_selection_transfer_close_source_fd(transfer); + } else { + wlr_log(L_DEBUG, "nothing happened, buffered the bytes"); + } + + return 1; + +error_out: + xwm_selection_send_notify(xwm, &transfer->request, false); + xwm_selection_transfer_destroy_outgoing(transfer); + return 0; +} + +void xwm_send_incr_chunk(struct wlr_xwm_selection_transfer *transfer) { + wlr_log(L_DEBUG, "property deleted"); + + transfer->property_set = false; + if (transfer->flush_property_on_delete) { + wlr_log(L_DEBUG, "setting new property, %zu bytes", + transfer->source_data.size); + transfer->flush_property_on_delete = false; + int length = xwm_selection_flush_source_data(transfer); + + if (transfer->source_fd >= 0) { + xwm_selection_transfer_start_outgoing(transfer); + } else if (length > 0) { + /* Transfer is all done, but queue a flush for + * the delete of the last chunk so we can set + * the 0 sized property to signal the end of + * the transfer. */ + transfer->flush_property_on_delete = true; + wl_array_release(&transfer->source_data); + wl_array_init(&transfer->source_data); + } else { + xwm_selection_transfer_destroy_outgoing(transfer); + } + } +} + +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) { + wlr_data_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; + } + } else if (selection == &selection->xwm->dnd_selection) { + struct wlr_data_source *source = + selection->xwm->seat->drag_source; + if (source != NULL) { + wlr_data_source_send(source, mime_type, fd); + return; + } + } + + wlr_log(L_DEBUG, "not sending selection: no selection source available"); +} + +static void xwm_selection_transfer_start_outgoing( + struct wlr_xwm_selection_transfer *transfer) { + struct wlr_xwm *xwm = transfer->selection->xwm; + struct wl_event_loop *loop = + wl_display_get_event_loop(xwm->xwayland->wl_display); + transfer->source = wl_event_loop_add_fd(loop, transfer->source_fd, + WL_EVENT_READABLE, xwm_data_source_read, transfer); +} + +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; + } + } else if (selection == &selection->xwm->dnd_selection) { + struct wlr_data_source *source = + selection->xwm->seat->drag_source; + if (source != NULL) { + return &source->mime_types; + } + } + return NULL; +} + +/** + * Read the Wayland selection and send it to an Xwayland client. + */ +static void xwm_selection_send_data(struct wlr_xwm_selection *selection, + xcb_selection_request_event_t *req, const char *mime_type) { + // Check MIME type + struct wl_array *mime_types = + xwm_selection_source_get_mime_types(selection); + if (mime_types == NULL) { + wlr_log(L_ERROR, "not sending selection: no MIME type list available"); + xwm_selection_send_notify(selection->xwm, req, false); + return; + } + + bool found = false; + char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, mime_types) { + char *t = *mime_type_ptr; + if (strcmp(t, mime_type) == 0) { + found = true; + break; + } + } + if (!found) { + wlr_log(L_ERROR, "not sending selection: " + "requested an unsupported MIME type %s", mime_type); + xwm_selection_send_notify(selection->xwm, req, false); + return; + } + + struct wlr_xwm_selection_transfer *transfer = + calloc(1, sizeof(struct wlr_xwm_selection_transfer)); + if (transfer == NULL) { + wlr_log(L_ERROR, "Allocation failed"); + return; + } + transfer->selection = selection; + transfer->request = *req; + wl_array_init(&transfer->source_data); + + int p[2]; + if (pipe(p) == -1) { + wlr_log(L_ERROR, "pipe() failed: %m"); + xwm_selection_send_notify(selection->xwm, req, false); + return; + } + fcntl(p[0], F_SETFD, FD_CLOEXEC); + fcntl(p[0], F_SETFL, O_NONBLOCK); + fcntl(p[1], F_SETFD, FD_CLOEXEC); + fcntl(p[1], F_SETFL, O_NONBLOCK); + + transfer->source_fd = p[0]; + + wlr_log(L_DEBUG, "Sending Wayland selection %u to Xwayland window with " + "MIME type %s, target %u", req->target, mime_type, req->target); + xwm_selection_source_send(selection, mime_type, p[1]); + + wl_list_insert(&selection->outgoing, &transfer->outgoing_link); + + // We can only handle one transfer at a time + if (wl_list_length(&selection->outgoing) == 1) { + xwm_selection_transfer_start_outgoing(transfer); + } +} + +static void xwm_selection_send_targets(struct wlr_xwm_selection *selection, + xcb_selection_request_event_t *req) { + 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_ERROR, "not sending selection targets: " + "no selection source available"); + xwm_selection_send_notify(selection->xwm, req, false); + return; + } + + size_t n = 2 + mime_types->size / sizeof(char *); + xcb_atom_t targets[n]; + targets[0] = xwm->atoms[TIMESTAMP]; + targets[1] = xwm->atoms[TARGETS]; + + size_t i = 0; + char **mime_type_ptr; + wl_array_for_each(mime_type_ptr, mime_types) { + char *mime_type = *mime_type_ptr; + targets[2+i] = xwm_mime_type_to_atom(xwm, mime_type); + ++i; + } + + xcb_change_property(xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + req->requestor, + req->property, + XCB_ATOM_ATOM, + 32, // format + n, targets); + + xwm_selection_send_notify(selection->xwm, req, true); +} + +static void xwm_selection_send_timestamp(struct wlr_xwm_selection *selection, + xcb_selection_request_event_t *req) { + xcb_change_property(selection->xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + req->requestor, + req->property, + XCB_ATOM_INTEGER, + 32, // format + 1, &selection->timestamp); + + xwm_selection_send_notify(selection->xwm, req, true); +} + +void xwm_handle_selection_request(struct wlr_xwm *xwm, + xcb_selection_request_event_t *req) { + wlr_log(L_DEBUG, "XCB_SELECTION_REQUEST (time=%u owner=%u, requestor=%u " + "selection=%u, target=%u, property=%u)", + req->time, req->owner, req->requestor, req->selection, req->target, + req->property); + + if (req->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_selection_send_notify(xwm, req, true); + return; + } + + struct wlr_xwm_selection *selection = + xwm_get_selection(xwm, req->selection); + if (selection == NULL) { + wlr_log(L_DEBUG, "received selection request for unknown selection"); + return; + } + + if (selection->window != req->owner) { + wlr_log(L_DEBUG, "received selection request with invalid owner"); + return; + } + + // No xwayland surface focused, deny access to clipboard + if (xwm->focus_surface == NULL && xwm->drag_focus == NULL) { + char *selection_name = xwm_get_atom_name(xwm, selection->atom); + wlr_log(L_DEBUG, "denying read access to selection %u (%s): " + "no xwayland surface focused", selection->atom, selection_name); + free(selection_name); + xwm_selection_send_notify(xwm, req, false); + return; + } + + if (req->target == xwm->atoms[TARGETS]) { + xwm_selection_send_targets(selection, req); + } else if (req->target == xwm->atoms[TIMESTAMP]) { + xwm_selection_send_timestamp(selection, req); + } else if (req->target == xwm->atoms[DELETE]) { + xwm_selection_send_notify(selection->xwm, req, true); + } else { + // Send data + char *mime_type = xwm_mime_type_from_atom(xwm, req->target); + if (mime_type == NULL) { + wlr_log(L_ERROR, "ignoring selection request: unknown atom %u", + req->target); + xwm_selection_send_notify(xwm, req, false); + return; + } + xwm_selection_send_data(selection, req, mime_type); + free(mime_type); + } +} diff --git a/xwayland/selection/selection.c b/xwayland/selection/selection.c new file mode 100644 index 00000000..39c47c45 --- /dev/null +++ b/xwayland/selection/selection.c @@ -0,0 +1,319 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "xwayland/xwm.h" +#include "xwayland/selection.h" + +void xwm_selection_transfer_remove_source( + struct wlr_xwm_selection_transfer *transfer) { + if (transfer->source != NULL) { + wl_event_source_remove(transfer->source); + transfer->source = NULL; + } +} + +void xwm_selection_transfer_close_source_fd( + struct wlr_xwm_selection_transfer *transfer) { + if (transfer->source_fd >= 0) { + close(transfer->source_fd); + transfer->source_fd = -1; + } +} + +void xwm_selection_transfer_destroy_property_reply( + struct wlr_xwm_selection_transfer *transfer) { + free(transfer->property_reply); + transfer->property_reply = NULL; +} + +xcb_atom_t xwm_mime_type_to_atom(struct wlr_xwm *xwm, char *mime_type) { + if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) { + return xwm->atoms[UTF8_STRING]; + } else if (strcmp(mime_type, "text/plain") == 0) { + return xwm->atoms[TEXT]; + } + + xcb_intern_atom_cookie_t cookie = + xcb_intern_atom(xwm->xcb_conn, 0, strlen(mime_type), mime_type); + xcb_intern_atom_reply_t *reply = + xcb_intern_atom_reply(xwm->xcb_conn, cookie, NULL); + if (reply == NULL) { + return XCB_ATOM_NONE; + } + xcb_atom_t atom = reply->atom; + free(reply); + return atom; +} + +char *xwm_mime_type_from_atom(struct wlr_xwm *xwm, xcb_atom_t atom) { + if (atom == xwm->atoms[UTF8_STRING]) { + return strdup("text/plain;charset=utf-8"); + } else if (atom == xwm->atoms[TEXT]) { + return strdup("text/plain"); + } else { + return xwm_get_atom_name(xwm, atom); + } +} + +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 if (selection_atom == xwm->atoms[DND_SELECTION]) { + return &xwm->dnd_selection; + } else { + return NULL; + } +} + +static int xwm_handle_selection_property_notify(struct wlr_xwm *xwm, + xcb_property_notify_event_t *event) { + struct wlr_xwm_selection *selections[] = { + &xwm->clipboard_selection, + &xwm->primary_selection, + &xwm->dnd_selection, + }; + + for (size_t i = 0; i < sizeof(selections)/sizeof(selections[0]); ++i) { + struct wlr_xwm_selection *selection = selections[i]; + + if (event->window == selection->window) { + if (event->state == XCB_PROPERTY_NEW_VALUE && + event->atom == xwm->atoms[WL_SELECTION] && + selection->incoming.incr) { + xwm_get_incr_chunk(&selection->incoming); + } + return 1; + } + + struct wlr_xwm_selection_transfer *outgoing; + wl_list_for_each(outgoing, &selection->outgoing, outgoing_link) { + if (event->window == outgoing->request.requestor) { + if (event->state == XCB_PROPERTY_DELETE && + event->atom == outgoing->request.property && + outgoing->incr) { + xwm_send_incr_chunk(outgoing); + } + return 1; + } + } + } + + return 0; +} + +int xwm_handle_selection_event(struct wlr_xwm *xwm, + xcb_generic_event_t *event) { + if (xwm->seat == NULL) { + wlr_log(L_DEBUG, "not handling selection events: " + "no seat assigned to xwayland"); + return 0; + } + + switch (event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK) { + case XCB_SELECTION_NOTIFY: + xwm_handle_selection_notify(xwm, (xcb_selection_notify_event_t *)event); + return 1; + case XCB_PROPERTY_NOTIFY: + return xwm_handle_selection_property_notify(xwm, + (xcb_property_notify_event_t *)event); + case XCB_SELECTION_REQUEST: + xwm_handle_selection_request(xwm, + (xcb_selection_request_event_t *)event); + return 1; + } + + switch (event->response_type - xwm->xfixes->first_event) { + case XCB_XFIXES_SELECTION_NOTIFY: + // an X11 window has copied something to the clipboard + return xwm_handle_xfixes_selection_notify(xwm, + (xcb_xfixes_selection_notify_event_t *)event); + } + + return 0; +} + +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->incoming.selection = selection; + wl_list_init(&selection->outgoing); + + 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); +} + +void xwm_selection_init(struct wlr_xwm *xwm) { + // Clipboard and primary selection + uint32_t selection_values[] = { + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE + }; + xwm->selection_window = xcb_generate_id(xwm->xcb_conn); + xcb_create_window(xwm->xcb_conn, + XCB_COPY_FROM_PARENT, + xwm->selection_window, + xwm->screen->root, + 0, 0, + 10, 10, + 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + xwm->screen->root_visual, + XCB_CW_EVENT_MASK, selection_values); + + xcb_set_selection_owner(xwm->xcb_conn, + xwm->selection_window, + xwm->atoms[CLIPBOARD_MANAGER], + XCB_TIME_CURRENT_TIME); + + selection_init(xwm, &xwm->clipboard_selection, xwm->atoms[CLIPBOARD]); + selection_init(xwm, &xwm->primary_selection, xwm->atoms[PRIMARY]); + + // Drag'n'drop + uint32_t dnd_values[] = { + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE + }; + xwm->dnd_window = xcb_generate_id(xwm->xcb_conn); + xcb_create_window(xwm->xcb_conn, + XCB_COPY_FROM_PARENT, + xwm->dnd_window, + xwm->screen->root, + 0, 0, + 8192, 8192, + 0, + XCB_WINDOW_CLASS_INPUT_ONLY, + xwm->screen->root_visual, + XCB_CW_EVENT_MASK, dnd_values); + + uint32_t version = XDND_VERSION; + xcb_change_property(xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + xwm->dnd_window, + xwm->atoms[DND_AWARE], + XCB_ATOM_ATOM, + 32, // format + 1, &version); + + selection_init(xwm, &xwm->dnd_selection, xwm->atoms[DND_SELECTION]); +} + +void xwm_selection_finish(struct wlr_xwm *xwm) { + if (!xwm) { + return; + } + if (xwm->selection_window) { + xcb_destroy_window(xwm->xcb_conn, xwm->selection_window); + } + if (xwm->dnd_window) { + xcb_destroy_window(xwm->xcb_conn, xwm->dnd_window); + } + if (xwm->seat) { + if (xwm->seat->selection_source && + wlr_data_source_is_xwayland_data_source( + xwm->seat->selection_source)) { + wlr_seat_set_selection(xwm->seat, NULL, + wl_display_next_serial(xwm->xwayland->wl_display)); + } + if (xwm->seat->primary_selection_source && + wlr_primary_selection_source_is_xwayland_primary_selection_source( + xwm->seat->primary_selection_source)) { + wlr_seat_set_primary_selection(xwm->seat, NULL, + wl_display_next_serial(xwm->xwayland->wl_display)); + } + wlr_xwayland_set_seat(xwm->xwayland, NULL); + } +} + +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 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); + struct wlr_data_source *source = seat->selection_source; + + if (source != NULL && wlr_data_source_is_xwayland_data_source(source)) { + return; + } + + 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 && + wlr_primary_selection_source_is_xwayland_primary_selection_source( + source)) { + return; + } + + xwm_selection_set_owner(&xwm->primary_selection, source != NULL); +} + +static void seat_handle_start_drag(struct wl_listener *listener, void *data) { + struct wlr_xwm *xwm = wl_container_of(listener, xwm, seat_start_drag); + struct wlr_drag *drag = data; + + xwm_selection_set_owner(&xwm->dnd_selection, drag != NULL); + xwm_seat_handle_start_drag(xwm, drag); +} + +void xwm_set_seat(struct wlr_xwm *xwm, struct wlr_seat *seat) { + if (xwm->seat != NULL) { + wl_list_remove(&xwm->seat_selection.link); + wl_list_remove(&xwm->seat_primary_selection.link); + wl_list_remove(&xwm->seat_start_drag.link); + xwm->seat = NULL; + } + + if (seat == NULL) { + return; + } + + xwm->seat = 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; + wl_signal_add(&seat->events.start_drag, &xwm->seat_start_drag); + xwm->seat_start_drag.notify = seat_handle_start_drag; + + 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 e0bf938c..c2b9bd11 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -1055,10 +1055,6 @@ static void xwm_handle_unhandled_event(struct wlr_xwm *xwm, xcb_generic_event_t #endif } -/* This is in xcb/xcb_event.h, but pulling xcb-util just for a constant - * others redefine anyway is meh - */ -#define XCB_EVENT_RESPONSE_TYPE_MASK (0x7f) static int x11_event_handler(int fd, uint32_t mask, void *data) { int count = 0; xcb_generic_event_t *event; -- cgit v1.2.3