aboutsummaryrefslogtreecommitdiff
path: root/xwayland
diff options
context:
space:
mode:
Diffstat (limited to 'xwayland')
-rw-r--r--xwayland/selection.c599
-rw-r--r--xwayland/xwm.c47
2 files changed, 556 insertions, 90 deletions
diff --git a/xwayland/selection.c b/xwayland/selection.c
index 72f3de3e..272c10cd 100644
--- a/xwayland/selection.c
+++ b/xwayland/selection.c
@@ -12,6 +12,32 @@
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_selection *selection,
xcb_atom_t property) {
xcb_selection_notify_event_t selection_notify = {
@@ -24,11 +50,20 @@ 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,
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) {
@@ -159,9 +194,9 @@ 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);
+ wlr_data_source_send(source, mime_type, fd);
return;
}
} else if (selection == &selection->xwm->primary_selection) {
@@ -171,13 +206,69 @@ 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) {
+ 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_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");
@@ -199,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]);
}
@@ -215,65 +308,172 @@ static void xwm_selection_send_timestamp(struct wlr_xwm_selection *selection) {
xwm_selection_send_notify(selection, selection->request.property);
}
-static struct wl_array *xwm_selection_source_get_mime_types(
- struct wlr_xwm_selection *selection) {
- if (selection == &selection->xwm->clipboard_selection) {
- struct wlr_data_source *source =
- selection->xwm->seat->selection_data_source;
- if (source != NULL) {
- return &source->mime_types;
+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_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_get_mime_type_atom(xwm, mime_type);
+ ++i;
}
- } 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 {
+ // 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_window,
+ xwm->atoms[DND_TYPE_LIST],
+ XCB_ATOM_ATOM,
+ 32, // format
+ n, targets);
}
- return NULL;
+
+ 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) {
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;
}
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;
}
@@ -286,8 +486,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,
@@ -296,6 +494,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;
}
@@ -306,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,
@@ -324,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;
}
@@ -333,9 +541,11 @@ 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) {
- wlr_log(L_DEBUG, "denying read access to clipboard: "
- "no xwayland surface focused");
+ 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);
return;
}
@@ -351,26 +561,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);
}
}
@@ -407,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");
@@ -506,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;
@@ -639,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);
@@ -653,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 =
@@ -676,6 +881,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
}
}
@@ -735,6 +942,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");
}
}
@@ -791,6 +1003,78 @@ 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]) {
+ 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;
@@ -807,7 +1091,10 @@ static void selection_init(struct wlr_xwm *xwm,
}
void xwm_selection_init(struct wlr_xwm *xwm) {
- uint32_t values[] = { XCB_EVENT_MASK_PROPERTY_CHANGE };
+ // 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,
@@ -818,7 +1105,7 @@ void xwm_selection_init(struct wlr_xwm *xwm) {
0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
xwm->screen->root_visual,
- XCB_CW_EVENT_MASK, values);
+ XCB_CW_EVENT_MASK, selection_values);
xcb_set_selection_owner(xwm->xcb_conn,
xwm->selection_window,
@@ -827,6 +1114,33 @@ void xwm_selection_init(struct wlr_xwm *xwm) {
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) {
@@ -836,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_data_source &&
- xwm->seat->selection_data_source->cancel == data_source_cancel) {
+ 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));
}
@@ -849,7 +1166,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,
@@ -874,9 +1190,9 @@ 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) {
+ if (source != NULL && source->impl == &data_source_impl) {
return;
}
@@ -897,10 +1213,119 @@ 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);
+
+ 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;
}
@@ -914,6 +1339,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);
diff --git a/xwayland/xwm.c b/xwayland/xwm.c
index 6702c3c9..d5501a0a 100644
--- a/xwayland/xwm.c
+++ b/xwayland/xwm.c
@@ -56,9 +56,22 @@ const char *atom_map[ATOM_LAST] = {
"_NET_WM_WINDOW_TYPE_DROPDOWN_MENU",
"_NET_WM_WINDOW_TYPE_POPUP_MENU",
"_NET_WM_WINDOW_TYPE_COMBO",
+ "XdndSelection",
+ "XdndAware",
+ "XdndStatus",
+ "XdndPosition",
+ "XdndEnter",
+ "XdndLeave",
+ "XdndDrop",
+ "XdndFinished",
+ "XdndProxy",
+ "XdndTypeList",
+ "XdndActionMove",
+ "XdndActionCopy",
+ "XdndActionAsk",
+ "XdndActionPrivate",
};
-/* General helpers */
// TODO: replace this with hash table?
static struct wlr_xwayland_surface *lookup_surface(struct wlr_xwm *xwm,
xcb_window_t window_id) {
@@ -493,6 +506,26 @@ static void read_surface_net_wm_state(struct wlr_xwm *xwm,
}
}
+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 =
+ xcb_get_atom_name_reply(xwm->xcb_conn, name_cookie, NULL);
+ if (name_reply == NULL) {
+ return NULL;
+ }
+ size_t len = xcb_get_atom_name_name_length(name_reply);
+ char *buf = xcb_get_atom_name_name(name_reply); // not a C string
+ char *name = malloc((len + 1) * sizeof(char));
+ if (name == NULL) {
+ return NULL;
+ }
+ memcpy(name, buf, len);
+ name[len] = '\0';
+ free(name_reply);
+ return name;
+}
+
static void read_surface_property(struct wlr_xwm *xwm,
struct wlr_xwayland_surface *xsurface, xcb_atom_t property) {
xcb_get_property_cookie_t cookie = xcb_get_property(xwm->xcb_conn, 0,
@@ -525,7 +558,10 @@ 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 {
- wlr_log(L_DEBUG, "unhandled x11 property %u", 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);
}
free(reply);
@@ -930,8 +966,11 @@ 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 {
- wlr_log(L_DEBUG, "unhandled x11 client message %u", ev->type);
+ } else if (!xwm_handle_selection_client_message(xwm, ev)) {
+ 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);
}
}