Merge branch 'clipboard' into 'master'

selection: allows using a wrapper source to filter data

See merge request wlroots/wlroots!5153
This commit is contained in:
JiDe Zhang 2026-02-02 23:54:17 +08:00
commit d1526c3a10
18 changed files with 2723 additions and 108 deletions

1996
examples/clipboard-control.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -47,6 +47,11 @@ compositors = {
],
'dep': [wayland_client, wayland_egl, egl, glesv2],
},
'clipboard-control': {
'src': 'clipboard-control.c',
'dep': [cairo],
'proto': ['xdg-shell'],
},
}
foreach name, info : compositors

View file

@ -11,6 +11,7 @@
#include <wayland-server-core.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_data_receiver.h>
struct wlr_data_device_manager {
struct wl_global *global;
@ -42,6 +43,9 @@ struct wlr_data_offer {
enum wl_data_device_manager_dnd_action preferred_action;
bool in_ask;
/* Data receiver for this offer */
struct wlr_data_receiver receiver;
struct {
struct wl_listener source_destroy;
} WLR_PRIVATE;
@ -53,15 +57,22 @@ struct wlr_data_offer {
*/
struct wlr_data_source_impl {
void (*send)(struct wlr_data_source *source, const char *mime_type,
int32_t fd);
struct wlr_data_receiver *receiver);
void (*accept)(struct wlr_data_source *source, uint32_t serial,
const char *mime_type);
const char *mime_type, struct wlr_data_receiver *receiver);
void (*cancelled)(struct wlr_data_source *source);
void (*destroy)(struct wlr_data_source *source);
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);
/**
* Returns the unwrapped source object. This source object maybe is a
* wrapper, its a proxy for the others source.
*/
struct wlr_data_source *(*get_original)(struct wlr_data_source *source);
};
struct wlr_data_source {
@ -74,6 +85,10 @@ struct wlr_data_source {
// source status
bool accepted;
// source information
struct wl_client *client;
pid_t pid; // PID of the source process (for XWayland, X11 client PID)
// drag'n'drop status
enum wl_data_device_manager_dnd_action current_dnd_action;
uint32_t compositor_action;
@ -217,18 +232,23 @@ void wlr_data_source_init(struct wlr_data_source *source,
const struct wlr_data_source_impl *impl);
/**
* Sends the data as the specified MIME type over the passed file descriptor,
* then close it.
* Sends the data as the specified MIME type to the receiver,
* with receiver managing the file descriptor and callbacks.
*/
void wlr_data_source_send(struct wlr_data_source *source, const char *mime_type,
int32_t fd);
void wlr_data_source_send(struct wlr_data_source *source,
const char *mime_type, struct wlr_data_receiver *receiver);
/**
* 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);
void wlr_data_source_accept(struct wlr_data_source *source,
uint32_t serial, const char *mime_type, struct wlr_data_receiver *receiver);
/**
* Notifies the data source that the operation was cancelled.
*/
void wlr_data_source_cancelled(struct wlr_data_source *source);
/**
* Notifies the data source it is no longer valid and should be destroyed. That
@ -261,4 +281,17 @@ void wlr_data_source_dnd_finish(struct wlr_data_source *source);
void wlr_data_source_dnd_action(struct wlr_data_source *source,
enum wl_data_device_manager_dnd_action action);
/**
* Copy metadata from one data source to another. This is useful for implementing
* wrapper data sources that can filter MIME types or other metadata.
*/
void wlr_data_source_copy(struct wlr_data_source *dest, struct wlr_data_source *src);
/**
* Returns the original source object, it isn't NULL if the source argument
* isn't NULL.
*/
struct wlr_data_source *
wlr_data_source_get_original(struct wlr_data_source *source);
#endif

View file

@ -0,0 +1,95 @@
/*
* This an unstable interface of wlroots. No guarantees are made regarding the
* future consistency of this API.
*/
#ifndef WLR_USE_UNSTABLE
#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features"
#endif
#ifndef WLR_TYPES_WLR_DATA_RECEIVER_H
#define WLR_TYPES_WLR_DATA_RECEIVER_H
#include <wayland-server-core.h>
#include <unistd.h>
struct wlr_data_receiver;
/**
* A data receiver implementation. All callbacks are optional.
*/
struct wlr_data_receiver_impl {
/**
* Called when the transfer is cancelled before completion. This should
* clean up any ongoing transfer state. The fd will be automatically
* closed after this callback returns.
*/
void (*cancelled)(struct wlr_data_receiver *receiver);
/**
* Called when the receiver is being destroyed. This should free any
* resources allocated for the receiver implementation.
*/
void (*destroy)(struct wlr_data_receiver *receiver);
};
/**
* A receiver is the receiving side of a data transfer. It represents the
* target of clipboard, primary selection, or drag-and-drop operations.
*
* This abstraction unifies the handling of data transfers across different
* protocols (Wayland native, XWayland, etc.) and provides a consistent
* interface for permission systems and transfer tracking.
*/
struct wlr_data_receiver {
const struct wlr_data_receiver_impl *impl;
/**
* File descriptor for data transfer. This is typically a pipe write end
* that will be passed to the data source for writing clipboard data.
*/
int fd;
/**
* Process ID of the receiving client. For XWayland windows, this is the
* X11 client PID, not the XWayland server PID. For native Wayland clients,
* this is the client process PID.
*/
pid_t pid;
/**
* The Wayland client associated with this receiver. For XWayland windows,
* this refers to the XWayland server client, not the X11 application.
* For native Wayland clients, this is the actual client.
*/
struct wl_client *client;
struct {
struct wl_signal destroy;
} events;
};
/**
* Initialize a data receiver with the given implementation. The receiver
* should be embedded in a larger structure (e.g., wlr_data_offer) to manage
* its lifecycle properly.
*/
void wlr_data_receiver_init(struct wlr_data_receiver *receiver,
const struct wlr_data_receiver_impl *impl);
/**
* Destroy a data receiver. This will emit the destroy signal, call the
* implementation's destroy callback if present, and close any open file
* descriptor. After calling this function, the receiver should not be used.
*/
void wlr_data_receiver_destroy(struct wlr_data_receiver *receiver);
/**
* Notify the receiver that the transfer has been cancelled. This calls the
* cancelled callback if implemented, then automatically closes the file
* descriptor. Should be called when a transfer is aborted before completion,
* such as when a drag operation is cancelled or a selection is cleared.
*/
void wlr_data_receiver_cancelled(struct wlr_data_receiver *receiver);
#endif

View file

@ -11,6 +11,7 @@
#include <wayland-server-core.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_data_receiver.h>
struct wlr_primary_selection_source;
@ -19,8 +20,14 @@ struct wlr_primary_selection_source;
*/
struct wlr_primary_selection_source_impl {
void (*send)(struct wlr_primary_selection_source *source,
const char *mime_type, int fd);
const char *mime_type, struct wlr_data_receiver *receiver);
void (*destroy)(struct wlr_primary_selection_source *source);
/**
* Returns the unwrapped source object. This source object maybe is a
* wrapper, its a proxy for the others source.
*/
struct wlr_primary_selection_source *(*get_original)(struct wlr_primary_selection_source *source);
};
/**
@ -32,6 +39,10 @@ struct wlr_primary_selection_source {
// source metadata
struct wl_array mime_types;
// source information
struct wl_client *client;
pid_t pid; // PID of the source process (for XWayland, X11 client PID)
struct {
struct wl_signal destroy;
} events;
@ -45,8 +56,22 @@ void wlr_primary_selection_source_init(
void wlr_primary_selection_source_destroy(
struct wlr_primary_selection_source *source);
void wlr_primary_selection_source_send(
struct wlr_primary_selection_source *source, const char *mime_type,
int fd);
struct wlr_primary_selection_source *source,
const char *mime_type, struct wlr_data_receiver *receiver);
/**
* Copy metadata from one primary selection source to another. This is useful for implementing
* wrapper primary selection sources that can filter MIME types or other metadata.
*/
void wlr_primary_selection_source_copy(struct wlr_primary_selection_source *dest,
struct wlr_primary_selection_source *src);
/**
* Returns the original primary selection source object, it isn't NULL if the source argument
* isn't NULL.
*/
struct wlr_primary_selection_source *
wlr_primary_selection_source_get_original(struct wlr_primary_selection_source *source);
/**
* Request setting the primary selection. If `client` is not null, then the

View file

@ -4,6 +4,7 @@
#include <stdbool.h>
#include <xcb/xfixes.h>
#include <wayland-util.h>
#include <wlr/types/wlr_data_receiver.h>
#define INCR_CHUNK_SIZE (64 * 1024)
@ -34,6 +35,12 @@ struct wlr_xwm_selection_transfer {
int property_start;
xcb_get_property_reply_t *property_reply;
xcb_window_t incoming_window;
// Data receiver reference for Wayland client (may be NULL)
struct wlr_data_receiver *wl_client_receiver;
// Listener for receiver destruction
struct wl_listener receiver_destroy;
};
struct wlr_xwm_selection {
@ -60,7 +67,7 @@ void xwm_selection_transfer_destroy_property_reply(
struct wlr_xwm_selection_transfer *transfer);
void xwm_selection_transfer_init(struct wlr_xwm_selection_transfer *transfer,
struct wlr_xwm_selection *selection);
void xwm_selection_transfer_destroy(
void xwm_selection_transfer_destroy_incoming(
struct wlr_xwm_selection_transfer *transfer);
void xwm_selection_transfer_destroy_outgoing(
@ -88,6 +95,8 @@ bool primary_selection_source_is_xwayland(
void xwm_seat_handle_start_drag(struct wlr_xwm *xwm, struct wlr_drag *drag);
pid_t get_x11_window_pid(xcb_connection_t *conn, xcb_window_t window);
void xwm_selection_init(struct wlr_xwm_selection *selection,
struct wlr_xwm *xwm, xcb_atom_t atom);
void xwm_selection_finish(struct wlr_xwm_selection *selection);

View file

@ -37,6 +37,7 @@ static void data_device_set_selection(struct wl_client *client,
if (source != NULL) {
source->finalized = true;
assert(client == source->source.client);
}
struct wlr_data_source *wlr_source =

View file

@ -6,10 +6,21 @@
#include <unistd.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/util/log.h>
#include "types/wlr_data_device.h"
// Empty destroy function for simple data receiver implementation
static void handle_data_receiver_simple_destroy(struct wlr_data_receiver *receiver) {
// No-op: receiver is embedded in another structure and will be freed with it
}
// Simple data receiver implementation with no callbacks
static const struct wlr_data_receiver_impl wlr_data_receiver_simple_impl = {
.destroy = handle_data_receiver_simple_destroy,
};
static const struct wl_data_offer_interface data_offer_impl;
static struct wlr_data_offer *data_offer_from_resource(
@ -88,7 +99,7 @@ static void data_offer_handle_accept(struct wl_client *client,
return;
}
wlr_data_source_accept(offer->source, serial, mime_type);
wlr_data_source_accept(offer->source, serial, mime_type, &offer->receiver);
}
static void data_offer_handle_receive(struct wl_client *client,
@ -99,7 +110,11 @@ static void data_offer_handle_receive(struct wl_client *client,
return;
}
wlr_data_source_send(offer->source, mime_type, fd);
// Set receiver fd for this transfer
offer->receiver.fd = fd;
assert(client == offer->receiver.client);
wlr_data_source_send(offer->source, mime_type, &offer->receiver);
}
static void data_offer_source_dnd_finish(struct wlr_data_offer *offer) {
@ -208,6 +223,8 @@ void data_offer_destroy(struct wlr_data_offer *offer) {
}
}
wlr_data_receiver_destroy(&offer->receiver);
// Make the resource inert
wl_resource_set_user_data(offer->resource, NULL);
@ -261,6 +278,10 @@ struct wlr_data_offer *data_offer_create(struct wl_resource *device_resource,
wl_resource_set_implementation(offer->resource, &data_offer_impl, offer,
data_offer_handle_resource_destroy);
wlr_data_receiver_init(&offer->receiver, &wlr_data_receiver_simple_impl);
offer->receiver.client = client;
wl_client_get_credentials(client, &offer->receiver.pid, NULL, NULL);
switch (type) {
case WLR_DATA_OFFER_SELECTION:
wl_list_insert(&seat_client->seat->selection_offers, &offer->link);

View file

@ -5,6 +5,7 @@
#include <unistd.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/util/log.h>
#include "types/wlr_data_device.h"
@ -16,21 +17,28 @@ void wlr_data_source_init(struct wlr_data_source *source,
*source = (struct wlr_data_source){
.impl = impl,
.actions = -1,
.pid = 0,
};
wl_array_init(&source->mime_types);
wl_signal_init(&source->events.destroy);
}
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_send(struct wlr_data_source *source,
const char *mime_type, struct wlr_data_receiver *receiver) {
source->impl->send(source, mime_type, receiver);
}
void wlr_data_source_accept(struct wlr_data_source *source, uint32_t serial,
const char *mime_type) {
void wlr_data_source_accept(struct wlr_data_source *source,
uint32_t serial, const char *mime_type, struct wlr_data_receiver *receiver) {
source->accepted = (mime_type != NULL);
if (source->impl->accept) {
source->impl->accept(source, serial, mime_type);
source->impl->accept(source, serial, mime_type, receiver);
}
}
void wlr_data_source_cancelled(struct wlr_data_source *source) {
if (source->impl->cancelled) {
source->impl->cancelled(source);
}
}
@ -76,6 +84,61 @@ void wlr_data_source_dnd_action(struct wlr_data_source *source,
}
}
void wlr_data_source_copy(struct wlr_data_source *dest, struct wlr_data_source *src) {
if (dest == NULL || src == NULL) {
return;
}
// Copy basic metadata
dest->client = src->client;
dest->pid = src->pid;
dest->accepted = src->accepted;
dest->actions = src->actions;
dest->current_dnd_action = src->current_dnd_action;
dest->compositor_action = src->compositor_action;
/* Clear any existing mime types before copying */
if (dest->mime_types.size > 0) {
char **p;
wl_array_for_each(p, &dest->mime_types) {
free(*p);
}
wl_array_release(&dest->mime_types);
wl_array_init(&dest->mime_types);
}
// Copy MIME types from source
char **p;
wl_array_for_each(p, &src->mime_types) {
char **dest_p = wl_array_add(&dest->mime_types, sizeof(*dest_p));
if (dest_p == NULL) {
wlr_log(WLR_ERROR, "Failed to add MIME type to destination");
continue;
}
char *mime_type_copy = strdup(*p);
if (mime_type_copy == NULL) {
wlr_log(WLR_ERROR, "Failed to copy MIME type");
continue;
}
*dest_p = mime_type_copy;
}
}
struct wlr_data_source *wlr_data_source_get_original(
struct wlr_data_source *source) {
if (!source) {
return NULL;
}
if (source->impl && source->impl->get_original) {
return source->impl->get_original(source);
}
return source;
}
static const struct wl_data_source_interface data_source_impl;
@ -87,7 +150,7 @@ struct wlr_client_data_source *client_data_source_from_resource(
}
static void client_data_source_accept(struct wlr_data_source *wlr_source,
uint32_t serial, const char *mime_type);
uint32_t serial, const char *mime_type, struct wlr_data_receiver *receiver);
static struct wlr_client_data_source *client_data_source_from_wlr_data_source(
struct wlr_data_source *wlr_source) {
@ -97,18 +160,26 @@ static struct wlr_client_data_source *client_data_source_from_wlr_data_source(
}
static void client_data_source_accept(struct wlr_data_source *wlr_source,
uint32_t serial, const char *mime_type) {
uint32_t serial, const char *mime_type, struct wlr_data_receiver *receiver) {
struct wlr_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) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct wlr_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);
wl_data_source_send_send(source->resource, mime_type, receiver->fd);
close(receiver->fd);
receiver->fd = -1;
}
static void client_data_source_cancelled(struct wlr_data_source *wlr_source) {
struct wlr_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_destroy(struct wlr_data_source *wlr_source) {
@ -254,6 +325,7 @@ struct wlr_client_data_source *client_data_source_create(
source->impl.accept = client_data_source_accept;
source->impl.send = client_data_source_send;
source->impl.cancelled = client_data_source_cancelled;
source->impl.destroy = client_data_source_destroy;
if (wl_resource_get_version(source->resource) >=
@ -270,5 +342,8 @@ struct wlr_client_data_source *client_data_source_create(
}
wlr_data_source_init(&source->source, &source->impl);
source->source.client = client;
wl_client_get_credentials(client, &source->source.pid, NULL, NULL);
return source;
}

View file

@ -3,6 +3,7 @@ wlr_files += files(
'data_device/wlr_data_offer.c',
'data_device/wlr_data_source.c',
'data_device/wlr_drag.c',
'wlr_data_receiver.c',
'ext_image_capture_source_v1/base.c',
'ext_image_capture_source_v1/output.c',
'ext_image_capture_source_v1/foreign_toplevel.c',

View file

@ -4,12 +4,23 @@
#include <unistd.h>
#include <wlr/types/wlr_data_control_v1.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/util/log.h>
#include "wlr-data-control-unstable-v1-protocol.h"
#define DATA_CONTROL_MANAGER_VERSION 2
// Empty destroy function for simple data receiver implementation
static void handle_data_receiver_simple_destroy(struct wlr_data_receiver *receiver) {
// No-op: receiver is embedded in another structure and will be freed with it
}
// Simple data receiver implementation with no callbacks
static const struct wlr_data_receiver_impl wlr_data_receiver_simple_impl = {
.destroy = handle_data_receiver_simple_destroy,
};
struct data_control_source {
struct wl_resource *resource;
struct wl_array mime_types;
@ -124,11 +135,13 @@ static struct client_data_source *
}
static void client_source_send(struct wlr_data_source *wlr_source,
const char *mime_type, int fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct client_data_source *source =
client_data_source_from_source(wlr_source);
zwlr_data_control_source_v1_send_send(source->resource, mime_type, fd);
close(fd);
zwlr_data_control_source_v1_send_send(source->resource, mime_type, receiver->fd);
close(receiver->fd);
receiver->fd = -1;
}
static void client_source_destroy(struct wlr_data_source *wlr_source) {
@ -172,11 +185,13 @@ static struct client_primary_selection_source *
static void client_primary_selection_source_send(
struct wlr_primary_selection_source *wlr_source,
const char *mime_type, int fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct client_primary_selection_source *source =
client_primary_selection_source_from_source(wlr_source);
zwlr_data_control_source_v1_send_send(source->resource, mime_type, fd);
close(fd);
zwlr_data_control_source_v1_send_send(source->resource, mime_type, receiver->fd);
close(receiver->fd);
receiver->fd = -1;
}
static void client_primary_selection_source_destroy(
@ -208,6 +223,9 @@ struct data_offer {
struct wl_resource *resource;
struct wlr_data_control_device_v1 *device;
bool is_primary;
// Data receiver for this offer
struct wlr_data_receiver receiver;
};
static void data_offer_destroy(struct data_offer *offer) {
@ -225,6 +243,8 @@ static void data_offer_destroy(struct data_offer *offer) {
}
wl_resource_set_user_data(offer->resource, NULL);
wlr_data_receiver_destroy(&offer->receiver);
free(offer);
}
@ -251,20 +271,27 @@ static void offer_handle_receive(struct wl_client *client,
return;
}
// Set receiver fd for this transfer
offer->receiver.fd = fd;
assert(offer->receiver.client == client);
if (offer->is_primary) {
if (device->seat->primary_selection_source == NULL) {
close(fd);
return;
}
wlr_primary_selection_source_send(
device->seat->primary_selection_source,
mime_type, fd);
mime_type, &offer->receiver);
} else {
if (device->seat->selection_source == NULL) {
close(fd);
return;
}
wlr_data_source_send(device->seat->selection_source, mime_type, fd);
wlr_data_source_send(device->seat->selection_source,
mime_type, &offer->receiver);
}
}
@ -306,6 +333,10 @@ static struct wl_resource *create_offer(struct wlr_data_control_device_v1 *devic
offer->resource = resource;
wlr_data_receiver_init(&offer->receiver, &wlr_data_receiver_simple_impl);
offer->receiver.client = client;
wl_client_get_credentials(client, &offer->receiver.pid, NULL, NULL);
wl_resource_set_implementation(resource, &offer_impl, offer,
offer_handle_resource_destroy);
@ -369,6 +400,8 @@ static void control_handle_set_selection(struct wl_client *client,
struct wlr_data_source *wlr_source = &client_source->source;
wlr_data_source_init(wlr_source, &client_source_impl);
wlr_source->client = client;
wl_client_get_credentials(client, &wlr_source->pid, NULL, NULL);
source->active_source = wlr_source;
wl_array_release(&wlr_source->mime_types);
@ -421,6 +454,8 @@ static void control_handle_set_primary_selection(struct wl_client *client,
struct wlr_primary_selection_source *wlr_source = &client_source->source;
wlr_primary_selection_source_init(wlr_source, &client_primary_selection_source_impl);
wlr_source->client = client;
wl_client_get_credentials(client, &wlr_source->pid, NULL, NULL);
source->active_primary_source = wlr_source;
wl_array_release(&wlr_source->mime_types);

55
types/wlr_data_receiver.c Normal file
View file

@ -0,0 +1,55 @@
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/util/log.h>
void wlr_data_receiver_init(struct wlr_data_receiver *receiver,
const struct wlr_data_receiver_impl *impl) {
assert(receiver && impl);
*receiver = (struct wlr_data_receiver){
.impl = impl,
.fd = -1,
.pid = 0,
.client = NULL,
};
wl_signal_init(&receiver->events.destroy);
}
void wlr_data_receiver_destroy(struct wlr_data_receiver *receiver) {
if (!receiver) {
return;
}
wl_signal_emit_mutable(&receiver->events.destroy, receiver);
assert(wl_list_empty(&receiver->events.destroy.listener_list));
int fd = receiver->fd;
if (receiver->impl && receiver->impl->destroy) {
receiver->impl->destroy(receiver);
} else {
free(receiver);
}
if (fd >= 0 && fcntl(fd, F_GETFD) != -1) {
close(fd);
}
}
void wlr_data_receiver_cancelled(struct wlr_data_receiver *receiver) {
if (!receiver) {
return;
}
if (receiver->impl && receiver->impl->cancelled) {
receiver->impl->cancelled(receiver);
}
// Close fd if still valid
if (receiver->fd >= 0 && fcntl(receiver->fd, F_GETFD) != -1) {
close(receiver->fd);
receiver->fd = -1;
}
}

View file

@ -4,12 +4,23 @@
#include <unistd.h>
#include <wlr/types/wlr_ext_data_control_v1.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/util/log.h>
#include "ext-data-control-v1-protocol.h"
#define EXT_DATA_CONTROL_MANAGER_VERSION 1
// Empty destroy function for simple data receiver implementation
static void handle_data_receiver_simple_destroy(struct wlr_data_receiver *receiver) {
// No-op: receiver is embedded in another structure and will be freed with it
}
// Simple data receiver implementation with no callbacks
static const struct wlr_data_receiver_impl wlr_data_receiver_simple_impl = {
.destroy = handle_data_receiver_simple_destroy,
};
struct data_control_source {
struct wl_resource *resource;
struct wl_array mime_types;
@ -124,11 +135,11 @@ static struct client_data_source *
}
static void client_source_send(struct wlr_data_source *wlr_source,
const char *mime_type, int fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct client_data_source *source =
client_data_source_from_source(wlr_source);
ext_data_control_source_v1_send_send(source->resource, mime_type, fd);
close(fd);
ext_data_control_source_v1_send_send(source->resource, mime_type, receiver->fd);
close(receiver->fd);
}
static void client_source_destroy(struct wlr_data_source *wlr_source) {
@ -172,11 +183,11 @@ static struct client_primary_selection_source *
static void client_primary_selection_source_send(
struct wlr_primary_selection_source *wlr_source,
const char *mime_type, int fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct client_primary_selection_source *source =
client_primary_selection_source_from_source(wlr_source);
ext_data_control_source_v1_send_send(source->resource, mime_type, fd);
close(fd);
ext_data_control_source_v1_send_send(source->resource, mime_type, receiver->fd);
close(receiver->fd);
}
static void client_primary_selection_source_destroy(
@ -208,6 +219,9 @@ struct data_offer {
struct wl_resource *resource;
struct wlr_ext_data_control_device_v1 *device;
bool is_primary;
// Data receiver for this offer
struct wlr_data_receiver receiver;
};
static void data_offer_destroy(struct data_offer *offer) {
@ -225,6 +239,8 @@ static void data_offer_destroy(struct data_offer *offer) {
}
wl_resource_set_user_data(offer->resource, NULL);
wlr_data_receiver_destroy(&offer->receiver);
free(offer);
}
@ -251,6 +267,9 @@ static void offer_handle_receive(struct wl_client *client,
return;
}
offer->receiver.fd = fd;
assert(offer->receiver.client == client);
if (offer->is_primary) {
if (device->seat->primary_selection_source == NULL) {
close(fd);
@ -258,13 +277,14 @@ static void offer_handle_receive(struct wl_client *client,
}
wlr_primary_selection_source_send(
device->seat->primary_selection_source,
mime_type, fd);
mime_type, &offer->receiver);
} else {
if (device->seat->selection_source == NULL) {
close(fd);
return;
}
wlr_data_source_send(device->seat->selection_source, mime_type, fd);
wlr_data_source_send(device->seat->selection_source,
mime_type, &offer->receiver);
}
}
@ -305,6 +325,9 @@ static struct wl_resource *create_offer(struct wlr_ext_data_control_device_v1 *d
}
offer->resource = resource;
wlr_data_receiver_init(&offer->receiver, &wlr_data_receiver_simple_impl);
offer->receiver.client = client;
wl_client_get_credentials(client, &offer->receiver.pid, NULL, NULL);
wl_resource_set_implementation(resource, &offer_impl, offer,
offer_handle_resource_destroy);
@ -369,6 +392,8 @@ static void control_handle_set_selection(struct wl_client *client,
struct wlr_data_source *wlr_source = &client_source->source;
wlr_data_source_init(wlr_source, &client_source_impl);
wlr_source->client = client;
wl_client_get_credentials(client, &wlr_source->pid, NULL, NULL);
source->active_source = wlr_source;
wl_array_release(&wlr_source->mime_types);
@ -421,6 +446,8 @@ static void control_handle_set_primary_selection(struct wl_client *client,
struct wlr_primary_selection_source *wlr_source = &client_source->source;
wlr_primary_selection_source_init(wlr_source, &client_primary_selection_source_impl);
wlr_source->client = client;
wl_client_get_credentials(client, &wlr_source->pid, NULL, NULL);
source->active_primary_source = wlr_source;
wl_array_release(&wlr_source->mime_types);

View file

@ -39,11 +39,61 @@ void wlr_primary_selection_source_destroy(
}
void wlr_primary_selection_source_send(
struct wlr_primary_selection_source *source, const char *mime_type,
int32_t fd) {
source->impl->send(source, mime_type, fd);
struct wlr_primary_selection_source *source,
const char *mime_type, struct wlr_data_receiver *receiver) {
source->impl->send(source, mime_type, receiver);
}
void wlr_primary_selection_source_copy(struct wlr_primary_selection_source *dest,
struct wlr_primary_selection_source *src) {
if (dest == NULL || src == NULL) {
return;
}
dest->client = src->client;
dest->pid = src->pid;
/* Clear any existing mime types before copying */
if (dest->mime_types.size > 0) {
char **p;
wl_array_for_each(p, &dest->mime_types) {
free(*p);
}
wl_array_release(&dest->mime_types);
wl_array_init(&dest->mime_types);
}
// Copy MIME types from source
char **p;
wl_array_for_each(p, &src->mime_types) {
char **dest_p = wl_array_add(&dest->mime_types, sizeof(*dest_p));
if (dest_p == NULL) {
wlr_log(WLR_ERROR, "Failed to add MIME type to destination");
continue;
}
char *mime_type_copy = strdup(*p);
if (mime_type_copy == NULL) {
wlr_log(WLR_ERROR, "Failed to copy MIME type");
continue;
}
*dest_p = mime_type_copy;
}
}
struct wlr_primary_selection_source *wlr_primary_selection_source_get_original(
struct wlr_primary_selection_source *source) {
if (!source) {
return NULL;
}
if (source->impl && source->impl->get_original) {
return source->impl->get_original(source);
}
return source;
}
void wlr_seat_request_set_primary_selection(struct wlr_seat *seat,
struct wlr_seat_client *client,

View file

@ -5,15 +5,33 @@
#include <unistd.h>
#include <wlr/types/wlr_primary_selection_v1.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/util/log.h>
#include "primary-selection-unstable-v1-protocol.h"
#define DEVICE_MANAGER_VERSION 1
// Primary selection offer structure
struct primary_selection_offer {
struct wlr_primary_selection_v1_device *device;
// Data receiver for this offer
struct wlr_data_receiver receiver;
};
// Primary selection receiver implementation
static void handle_primary_selection_receiver_destroy(struct wlr_data_receiver *receiver) {
// The receiver is embedded in primary_selection_offer, so don't free it
}
static const struct wlr_data_receiver_impl primary_selection_receiver_impl = {
.destroy = handle_primary_selection_receiver_destroy,
};
static const struct zwp_primary_selection_offer_v1_interface offer_impl;
static struct wlr_primary_selection_v1_device *device_from_offer_resource(
static struct primary_selection_offer *offer_from_resource(
struct wl_resource *resource) {
assert(wl_resource_instance_of(resource,
&zwp_primary_selection_offer_v1_interface, &offer_impl));
@ -22,15 +40,19 @@ static struct wlr_primary_selection_v1_device *device_from_offer_resource(
static void offer_handle_receive(struct wl_client *client,
struct wl_resource *resource, const char *mime_type, int32_t fd) {
struct wlr_primary_selection_v1_device *device =
device_from_offer_resource(resource);
if (device == NULL || device->seat->primary_selection_source == NULL) {
struct primary_selection_offer *offer = offer_from_resource(resource);
if (offer == NULL || offer->device == NULL ||
offer->device->seat->primary_selection_source == NULL) {
close(fd);
return;
}
wlr_primary_selection_source_send(device->seat->primary_selection_source,
mime_type, fd);
// Set receiver fd for this transfer
offer->receiver.fd = fd;
assert(offer->receiver.client == client);
wlr_primary_selection_source_send(offer->device->seat->primary_selection_source,
mime_type, &offer->receiver);
}
static void offer_handle_destroy(struct wl_client *client,
@ -45,6 +67,11 @@ static const struct zwp_primary_selection_offer_v1_interface offer_impl = {
static void offer_handle_resource_destroy(struct wl_resource *resource) {
wl_list_remove(wl_resource_get_link(resource));
struct primary_selection_offer *offer = offer_from_resource(resource);
if (offer) {
wlr_data_receiver_destroy(&offer->receiver);
free(offer);
}
}
static struct wlr_primary_selection_v1_device *device_from_resource(
@ -64,7 +91,22 @@ static void create_offer(struct wl_resource *device_resource,
wl_resource_post_no_memory(device_resource);
return;
}
wl_resource_set_implementation(resource, &offer_impl, device,
// Create the offer structure with embedded receiver
struct primary_selection_offer *offer = calloc(1, sizeof(*offer));
if (offer == NULL) {
wl_resource_destroy(resource);
wl_resource_post_no_memory(device_resource);
return;
}
offer->device = device;
wlr_data_receiver_init(&offer->receiver, &primary_selection_receiver_impl);
offer->receiver.client = client;
wl_client_get_credentials(client, &offer->receiver.pid, NULL, NULL);
wl_resource_set_implementation(resource, &offer_impl, offer,
offer_handle_resource_destroy);
wl_list_insert(&device->offers, wl_resource_get_link(resource));
@ -80,7 +122,8 @@ static void create_offer(struct wl_resource *device_resource,
}
static void destroy_offer(struct wl_resource *resource) {
if (device_from_offer_resource(resource) == NULL) {
struct primary_selection_offer *offer = offer_from_resource(resource);
if (offer == NULL) {
return;
}
@ -101,10 +144,12 @@ struct client_data_source {
static void client_source_send(
struct wlr_primary_selection_source *wlr_source,
const char *mime_type, int fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct client_data_source *source = wl_container_of(wlr_source, source, source);
zwp_primary_selection_source_v1_send_send(source->resource, mime_type, fd);
close(fd);
zwp_primary_selection_source_v1_send_send(source->resource, mime_type, receiver->fd);
close(receiver->fd);
receiver->fd = -1;
}
static void client_source_destroy(
@ -374,6 +419,8 @@ static void device_manager_handle_create_source(struct wl_client *client,
return;
}
wlr_primary_selection_source_init(&source->source, &client_source_impl);
source->source.client = client;
wl_client_get_credentials(client, &source->source.pid, NULL, NULL);
uint32_t version = wl_resource_get_version(manager_resource);
source->resource = wl_resource_create(client,

View file

@ -7,6 +7,7 @@
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/util/log.h>
#include <xcb/xfixes.h>
#include "wlr/types/wlr_data_receiver.h"
#include "xwayland/selection.h"
#include "xwayland/xwm.h"
@ -42,6 +43,39 @@ xwm_selection_transfer_create_incoming(struct wlr_xwm_selection *selection) {
return transfer;
}
void xwm_selection_transfer_destroy_incoming(
struct wlr_xwm_selection_transfer *transfer) {
if (!transfer) {
return;
}
xwm_selection_transfer_destroy_property_reply(transfer);
xwm_selection_transfer_remove_event_source(transfer);
xwm_selection_transfer_close_wl_client_fd(transfer);
if (transfer->incoming_window) {
struct wlr_xwm *xwm = transfer->selection->xwm;
xcb_destroy_window(xwm->xcb_conn, transfer->incoming_window);
xwm_schedule_flush(xwm);
}
if (transfer->wl_client_receiver) {
wl_list_remove(&transfer->receiver_destroy.link);
wlr_data_receiver_destroy(transfer->wl_client_receiver);
}
wl_list_remove(&transfer->link);
free(transfer);
}
static void handle_incoming_receiver_destroy(struct wl_listener *listener, void *data) {
struct wlr_xwm_selection_transfer *transfer =
wl_container_of(listener, transfer, receiver_destroy);
transfer->wl_client_receiver = NULL;
wl_list_remove(&transfer->receiver_destroy.link);
}
struct wlr_xwm_selection_transfer *
xwm_selection_find_incoming_transfer_by_window(
struct wlr_xwm_selection *selection, xcb_window_t window) {
@ -110,7 +144,7 @@ static int write_selection_property_to_wl_client(int fd, uint32_t mask,
ssize_t len = write(fd, property + transfer->property_start, remainder);
if (len == -1) {
wlr_log_errno(WLR_ERROR, "write error to target fd %d", fd);
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
return 0;
}
@ -126,7 +160,7 @@ static int write_selection_property_to_wl_client(int fd, uint32_t mask,
xwm_notify_ready_for_next_incr_chunk(transfer);
} else {
wlr_log(WLR_DEBUG, "transfer complete");
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
}
return 0;
@ -171,7 +205,7 @@ void xwm_get_incr_chunk(struct wlr_xwm_selection_transfer *transfer) {
xwm_write_selection_property_to_wl_client(transfer);
} else {
wlr_log(WLR_DEBUG, "incremental transfer complete");
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
}
}
@ -195,7 +229,7 @@ static void xwm_selection_transfer_get_data(
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, int fd) {
const char *requested_mime_type, struct wlr_data_receiver *receiver) {
struct wlr_xwm *xwm = selection->xwm;
xcb_atom_t *atoms = mime_types_atoms->data;
@ -215,7 +249,7 @@ static void source_send(struct wlr_xwm_selection *selection,
if (!found) {
wlr_log(WLR_DEBUG, "Cannot send X11 selection to Wayland: "
"unsupported MIME type");
close(fd);
wlr_data_receiver_cancelled(receiver);
return;
}
@ -223,10 +257,14 @@ static void source_send(struct wlr_xwm_selection *selection,
xwm_selection_transfer_create_incoming(selection);
if (!transfer) {
wlr_log(WLR_ERROR, "Cannot create transfer");
close(fd);
wlr_data_receiver_cancelled(receiver);
return;
}
// Listen for receiver destruction to clean up the reference
transfer->receiver_destroy.notify = handle_incoming_receiver_destroy;
wl_signal_add(&receiver->events.destroy, &transfer->receiver_destroy);
xcb_convert_selection(xwm->xcb_conn,
transfer->incoming_window,
selection->atom,
@ -236,8 +274,9 @@ static void source_send(struct wlr_xwm_selection *selection,
xwm_schedule_flush(xwm);
fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
transfer->wl_client_fd = fd;
fcntl(receiver->fd, F_SETFL, O_WRONLY | O_NONBLOCK);
transfer->wl_client_fd = receiver->fd;
transfer->wl_client_receiver = receiver;
}
struct x11_data_source {
@ -250,24 +289,26 @@ static const struct wlr_data_source_impl data_source_impl;
bool data_source_is_xwayland(
struct wlr_data_source *wlr_source) {
return wlr_source->impl == &data_source_impl;
struct wlr_data_source *original = wlr_data_source_get_original(wlr_source);
return original->impl == &data_source_impl;
}
static struct x11_data_source *data_source_from_wlr_data_source(
struct wlr_data_source *wlr_source) {
assert(data_source_is_xwayland(wlr_source));
struct x11_data_source *source = wl_container_of(wlr_source, source, base);
struct wlr_data_source *original = wlr_data_source_get_original(wlr_source);
struct x11_data_source *source = wl_container_of(original, source, base);
return source;
}
static void data_source_send(struct wlr_data_source *wlr_source,
const char *mime_type, int32_t fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
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);
mime_type, receiver);
}
static void data_source_destroy(struct wlr_data_source *wlr_source) {
@ -293,17 +334,18 @@ static const struct wlr_primary_selection_source_impl
bool primary_selection_source_is_xwayland(
struct wlr_primary_selection_source *wlr_source) {
return wlr_source->impl == &primary_selection_source_impl;
struct wlr_primary_selection_source *original = wlr_primary_selection_source_get_original(wlr_source);
return original->impl == &primary_selection_source_impl;
}
static void primary_selection_source_send(
struct wlr_primary_selection_source *wlr_source,
const char *mime_type, int fd) {
const char *mime_type, struct wlr_data_receiver *receiver) {
struct x11_primary_selection_source *source = wl_container_of(wlr_source, source, base);
struct wlr_xwm_selection *selection = source->selection;
source_send(selection, &wlr_source->mime_types, &source->mime_types_atoms,
mime_type, fd);
mime_type, receiver);
}
static void primary_selection_source_destroy(
@ -407,6 +449,10 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) {
}
wlr_data_source_init(&source->base, &data_source_impl);
// Set client and PID for XWayland data sources
source->base.client = xwm->xwayland->server->client;
source->base.pid = get_x11_window_pid(xwm->xcb_conn, selection->owner);
source->selection = selection;
wl_array_init(&source->mime_types_atoms);
@ -426,6 +472,10 @@ static void xwm_selection_get_targets(struct wlr_xwm_selection *selection) {
wlr_primary_selection_source_init(&source->base,
&primary_selection_source_impl);
// Set client and PID for XWayland primary selection sources
source->base.client = xwm->xwayland->server->client;
source->base.pid = get_x11_window_pid(xwm->xcb_conn, selection->owner);
source->selection = selection;
wl_array_init(&source->mime_types_atoms);
@ -460,7 +510,7 @@ void xwm_handle_selection_notify(struct wlr_xwm *xwm,
if (event->property == XCB_ATOM_NONE) {
if (transfer) {
wlr_log(WLR_ERROR, "convert selection failed");
xwm_selection_transfer_destroy(transfer);
xwm_selection_transfer_destroy_incoming(transfer);
}
} else if (event->target == xwm->atoms[TARGETS]) {
// No xwayland surface focused, deny access to clipboard

View file

@ -5,6 +5,7 @@
#include <unistd.h>
#include <wayland-util.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_data_receiver.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/util/log.h>
#include <xcb/xfixes.h>
@ -56,11 +57,22 @@ static int xwm_selection_flush_source_data(
static void xwm_selection_transfer_start_outgoing(
struct wlr_xwm_selection_transfer *transfer);
// Structure to contain wlr_data_receiver and transfer pointer for outgoing transfers
struct xwm_outgoing_receiver {
struct wlr_data_receiver receiver;
struct wlr_xwm_selection_transfer *transfer;
};
void xwm_selection_transfer_destroy_outgoing(
struct wlr_xwm_selection_transfer *transfer) {
wl_list_remove(&transfer->link);
wlr_log(WLR_DEBUG, "Destroying transfer %p", transfer);
// Destroy the receiver if it exists
if (transfer->wl_client_receiver) {
wlr_data_receiver_destroy(transfer->wl_client_receiver);
}
xwm_selection_transfer_remove_event_source(transfer);
xwm_selection_transfer_close_wl_client_fd(transfer);
wl_array_release(&transfer->source_data);
@ -180,32 +192,54 @@ void xwm_send_incr_chunk(struct wlr_xwm_selection_transfer *transfer) {
}
}
static void xwm_selection_source_send(struct wlr_xwm_selection *selection,
const char *mime_type, int32_t fd) {
static void handle_data_receiver_cancelled(struct wlr_data_receiver *receiver) {
struct xwm_outgoing_receiver *outgoing_receiver =
wl_container_of(receiver, outgoing_receiver, receiver);
struct wlr_xwm_selection_transfer *transfer = outgoing_receiver->transfer;
// Handle failure by destroying the transfer
xwm_selection_transfer_destroy_outgoing(transfer);
}
static void handle_data_receiver_destroy(struct wlr_data_receiver *receiver) {
struct xwm_outgoing_receiver *outgoing_receiver =
wl_container_of(receiver, outgoing_receiver, receiver);
outgoing_receiver->transfer->wl_client_receiver = NULL;
free(outgoing_receiver);
}
static const struct wlr_data_receiver_impl outgoing_receiver_impl = {
.cancelled = handle_data_receiver_cancelled,
.destroy = handle_data_receiver_destroy,
};
static bool xwm_selection_source_send(struct wlr_xwm_selection *selection,
const char *mime_type, struct wlr_data_receiver *receiver) {
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;
wlr_data_source_send(source, mime_type, receiver);
return true;
}
} else if (selection == &selection->xwm->primary_selection) {
struct wlr_primary_selection_source *source =
selection->xwm->seat->primary_selection_source;
if (source != NULL) {
wlr_primary_selection_source_send(source, mime_type, fd);
return;
wlr_primary_selection_source_send(source, mime_type, receiver);
return true;
}
} 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_data_source_send(source, mime_type, receiver);
return true;
}
}
wlr_log(WLR_DEBUG, "not sending selection: no selection source available");
return false;
}
static void xwm_selection_transfer_start_outgoing(
@ -276,16 +310,17 @@ static bool xwm_selection_send_data(struct wlr_xwm_selection *selection,
return false;
}
xwm_selection_transfer_init(transfer, selection);
transfer->request = *req;
wl_array_init(&transfer->source_data);
int p[2];
if (pipe(p) == -1) {
wlr_log_errno(WLR_ERROR, "pipe() failed");
free(transfer);
return false;
}
xwm_selection_transfer_init(transfer, selection);
transfer->request = *req;
wl_array_init(&transfer->source_data);
fcntl(p[0], F_SETFD, FD_CLOEXEC);
fcntl(p[0], F_SETFL, O_NONBLOCK);
fcntl(p[1], F_SETFD, FD_CLOEXEC);
@ -293,10 +328,31 @@ static bool xwm_selection_send_data(struct wlr_xwm_selection *selection,
transfer->wl_client_fd = p[0];
wlr_log(WLR_DEBUG, "Sending Wayland selection %u to Xwayland window with "
"MIME type %s, target %u, transfer %p", req->target, mime_type,
req->target, transfer);
xwm_selection_source_send(selection, mime_type, p[1]);
// Create and initialize the outgoing receiver
struct xwm_outgoing_receiver *outgoing_receiver = calloc(1, sizeof(*outgoing_receiver));
if (!outgoing_receiver) {
wlr_log(WLR_ERROR, "Failed to allocate outgoing receiver");
close(p[0]);
close(p[1]);
free(transfer);
return false;
}
outgoing_receiver->transfer = transfer;
struct wlr_data_receiver *receiver = &outgoing_receiver->receiver;
wlr_data_receiver_init(receiver, &outgoing_receiver_impl);
receiver->fd = p[1];
receiver->client = selection->xwm->xwayland->server->client;
// Get X11 window PID
if (transfer->request.requestor != XCB_WINDOW_NONE) {
receiver->pid = get_x11_window_pid(selection->xwm->xcb_conn, transfer->request.requestor);
} else {
receiver->pid = 0;
}
transfer->wl_client_receiver = receiver;
// It seems that if we ever try to reply to a selection request after
// another has been sent by the same requestor, the requestor never reads
@ -317,6 +373,14 @@ static bool xwm_selection_send_data(struct wlr_xwm_selection *selection,
xwm_selection_transfer_start_outgoing(transfer);
wlr_log(WLR_DEBUG, "Sending Wayland selection %u to Xwayland window with "
"MIME type %s, target %u, transfer %p", req->target, mime_type,
req->target, transfer);
// Maybe the transfer object is destroyed following the receiver object afeter
// xwm_selection_source_send, so don't access any member of transfer after this call.
transfer = NULL;
xwm_selection_source_send(selection, mime_type, receiver);
return true;
}

View file

@ -3,12 +3,58 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-server-protocol.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/util/log.h>
#include <xcb/xfixes.h>
#include <xcb/res.h>
#include "xwayland/selection.h"
#include "xwayland/xwm.h"
/* Get X11 window PID using XRes extension */
pid_t get_x11_window_pid(xcb_connection_t *conn, xcb_window_t window) {
if (!conn || window == XCB_WINDOW_NONE) {
return 0;
}
// Check if XRes extension is available
xcb_query_extension_cookie_t ext_cookie = xcb_query_extension(conn,
strlen("X-Resource"), "X-Resource");
xcb_query_extension_reply_t *ext_reply = xcb_query_extension_reply(conn, ext_cookie, NULL);
if (!ext_reply || !ext_reply->present) {
free(ext_reply);
return 0;
}
free(ext_reply);
// Create client ID spec for the window
xcb_res_client_id_spec_t spec;
spec.client = window;
spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
// Query client IDs for the window
xcb_res_query_client_ids_cookie_t cookie = xcb_res_query_client_ids(conn, 1, &spec);
xcb_res_query_client_ids_reply_t *reply = xcb_res_query_client_ids_reply(conn, cookie, NULL);
if (!reply) {
return 0;
}
pid_t pid = 0;
xcb_res_client_id_value_iterator_t iter = xcb_res_query_client_ids_ids_iterator(reply);
for (; iter.rem; xcb_res_client_id_value_next(&iter)) {
if (iter.data->spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
uint32_t *value = xcb_res_client_id_value_value(iter.data);
if (value && iter.data->length >= sizeof(uint32_t)) {
pid = *value;
break;
}
}
}
free(reply);
return pid;
}
void xwm_selection_transfer_remove_event_source(
struct wlr_xwm_selection_transfer *transfer) {
if (transfer->event_source != NULL) {
@ -39,26 +85,6 @@ void xwm_selection_transfer_init(struct wlr_xwm_selection_transfer *transfer,
};
}
void xwm_selection_transfer_destroy(
struct wlr_xwm_selection_transfer *transfer) {
if (!transfer) {
return;
}
xwm_selection_transfer_destroy_property_reply(transfer);
xwm_selection_transfer_remove_event_source(transfer);
xwm_selection_transfer_close_wl_client_fd(transfer);
if (transfer->incoming_window) {
struct wlr_xwm *xwm = transfer->selection->xwm;
xcb_destroy_window(xwm->xcb_conn, transfer->incoming_window);
xwm_schedule_flush(xwm);
}
wl_list_remove(&transfer->link);
free(transfer);
}
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];
@ -256,7 +282,7 @@ void xwm_selection_finish(struct wlr_xwm_selection *selection) {
struct wlr_xwm_selection_transfer *incoming;
wl_list_for_each_safe(incoming, tmp, &selection->incoming, link) {
xwm_selection_transfer_destroy(incoming);
xwm_selection_transfer_destroy_incoming(incoming);
}
xcb_destroy_window(selection->xwm->xcb_conn, selection->window);