diff options
author | Tudor Brindus <me@tbrindus.ca> | 2021-02-03 00:16:15 -0500 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2021-02-04 17:06:14 +0100 |
commit | 2fa257313ac888f8b61fba8e7c457eac9e1973c3 (patch) | |
tree | efa2eff824f4207770ee97b28e4d02e097d65000 /xwayland/selection | |
parent | 7964a313e8c59ed664235884d9b612273b12fc57 (diff) |
xwayland/selection: use one target window per selection
Previously, the clipboard and primary selections shared the same window.
This was racey, and could have led to pasting failures.
On xfixes selection owner change notification, the logic for requesting
the supported mimetypes of the new owner's selection looks like:
xcb_convert_selection(
xwm->xcb_conn,
selection->window,
selection->atom,
xwm->atoms[TARGETS],
xwm->atoms[WL_SELECTION],
selection->timestamp
);
This means ask the selection owner to write its TARGETS for the
`selection->atom` selection (one of PRIMARY, CLIPBOARD, DND_SELECTION)
to `selection->window`'s WL_SELECTION atom.
However, `selection->window` is shared for both PRIMARY and CLIPBOARD
selections, and WL_SELECTION is used as the target atom in both cases.
So, there's a race when both selections change at the same time.
The CLIPBOARD selection might support mimetypes {A, B, C}, and the
PRIMARY only {A, B}. If the ConvertSelection requests/responses "cross
on the wire", so to speak, wlroots can end up believing that the PRIMARY
selection also supports C.
A Wayland client may then ask for the PRIMARY selection in C format,
which will fail with "convert selection failed".
This commit fixes this by using a separate window for PRIMARY and
CLIPBOARD target requests, so that WL_SELECTION can be used as the
target atom in both cases.
Diffstat (limited to 'xwayland/selection')
-rw-r--r-- | xwayland/selection/dnd.c | 12 | ||||
-rw-r--r-- | xwayland/selection/selection.c | 61 |
2 files changed, 64 insertions, 9 deletions
diff --git a/xwayland/selection/dnd.c b/xwayland/selection/dnd.c index 16941558..ee9a23a6 100644 --- a/xwayland/selection/dnd.c +++ b/xwayland/selection/dnd.c @@ -64,7 +64,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_window; + 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 @@ -94,7 +94,7 @@ static void xwm_dnd_send_enter(struct wlr_xwm *xwm) { xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, - xwm->dnd_window, + xwm->dnd_selection.window, xwm->atoms[DND_TYPE_LIST], XCB_ATOM_ATOM, 32, // format @@ -110,7 +110,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_window; + data.data32[0] = xwm->dnd_selection.window; data.data32[2] = (x << 16) | y; data.data32[3] = time; data.data32[4] = @@ -126,7 +126,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_window; + data.data32[0] = xwm->dnd_selection.window; data.data32[2] = time; xwm_dnd_send_event(xwm, xwm->atoms[DND_DROP], &data); @@ -139,7 +139,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_window; + data.data32[0] = xwm->dnd_selection.window; xwm_dnd_send_event(xwm, xwm->atoms[DND_LEAVE], &data); } @@ -151,7 +151,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_window; + data.data32[0] = xwm->dnd_selection.window; data.data32[1] = drag->source->accepted; if (drag->source->accepted) { diff --git a/xwayland/selection/selection.c b/xwayland/selection/selection.c index 0996f94d..9cb8c455 100644 --- a/xwayland/selection/selection.c +++ b/xwayland/selection/selection.c @@ -174,12 +174,65 @@ int xwm_handle_selection_event(struct wlr_xwm *xwm, void xwm_selection_init(struct wlr_xwm_selection *selection, struct wlr_xwm *xwm, xcb_atom_t atom) { - selection->xwm = xwm; - selection->atom = atom; - selection->window = xwm->selection_window; wl_list_init(&selection->incoming); wl_list_init(&selection->outgoing); + selection->xwm = xwm; + selection->atom = atom; + selection->window = xcb_generate_id(xwm->xcb_conn); + + if (atom == xwm->atoms[DND_SELECTION]) { + xcb_create_window( + xwm->xcb_conn, + XCB_COPY_FROM_PARENT, + selection->window, + xwm->screen->root, + 0, 0, + 8192, 8192, + 0, + XCB_WINDOW_CLASS_INPUT_ONLY, + xwm->screen->root_visual, + XCB_CW_EVENT_MASK, + (uint32_t[]){ + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE + } + ); + + xcb_change_property( + xwm->xcb_conn, + XCB_PROP_MODE_REPLACE, + selection->window, + xwm->atoms[DND_AWARE], + XCB_ATOM_ATOM, + 32, // format + 1, + &(uint32_t){XDND_VERSION} + ); + } else { + xcb_create_window( + xwm->xcb_conn, + XCB_COPY_FROM_PARENT, + selection->window, + xwm->screen->root, + 0, 0, + 10, 10, + 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + xwm->screen->root_visual, + XCB_CW_EVENT_MASK, + (uint32_t[]){ + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE + } + ); + + if (atom == xwm->atoms[CLIPBOARD]) { + xcb_set_selection_owner(xwm->xcb_conn, selection->window, + xwm->atoms[CLIPBOARD_MANAGER], XCB_TIME_CURRENT_TIME); + } else { + assert(atom == xwm->atoms[PRIMARY]); + } + } + uint32_t mask = XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | @@ -203,6 +256,8 @@ void xwm_selection_finish(struct wlr_xwm_selection *selection) { wl_list_for_each_safe(incoming, tmp, &selection->incoming, link) { xwm_selection_transfer_destroy(incoming); } + + xcb_destroy_window(selection->xwm->xcb_conn, selection->window); } static void xwm_selection_set_owner(struct wlr_xwm_selection *selection, |