2017-10-11 21:48:40 +02:00
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-10-13 13:58:46 +02:00
|
|
|
#include <strings.h>
|
2017-10-11 21:48:40 +02:00
|
|
|
#include <assert.h>
|
2017-10-12 17:41:11 +02:00
|
|
|
#include <unistd.h>
|
2017-10-11 21:48:40 +02:00
|
|
|
#include <wayland-server.h>
|
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include <wlr/types/wlr_seat.h>
|
|
|
|
#include <wlr/types/wlr_data_device.h>
|
|
|
|
|
2017-10-13 13:58:46 +02:00
|
|
|
#define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \
|
|
|
|
WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \
|
|
|
|
WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
|
|
|
|
|
2017-10-14 21:53:30 +02:00
|
|
|
static uint32_t data_offer_choose_action(struct wlr_data_offer *offer) {
|
2017-12-29 22:51:33 +01:00
|
|
|
uint32_t offer_actions, preferred_action = 0;
|
2017-10-14 21:53:30 +02:00
|
|
|
if (wl_resource_get_version(offer->resource) >=
|
|
|
|
WL_DATA_OFFER_ACTION_SINCE_VERSION) {
|
2017-12-29 22:51:33 +01:00
|
|
|
offer_actions = offer->actions;
|
|
|
|
preferred_action = offer->preferred_action;
|
2017-10-14 21:53:30 +02:00
|
|
|
} else {
|
|
|
|
offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
uint32_t source_actions;
|
|
|
|
if (offer->source->actions >= 0) {
|
|
|
|
source_actions = offer->source->actions;
|
2017-10-14 21:53:30 +02:00
|
|
|
} else {
|
|
|
|
source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
uint32_t available_actions = offer_actions & source_actions;
|
2017-10-14 21:53:30 +02:00
|
|
|
if (!available_actions) {
|
|
|
|
return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
if (offer->source->seat_client &&
|
2017-10-14 21:53:30 +02:00
|
|
|
offer->source->compositor_action & available_actions) {
|
|
|
|
return offer->source->compositor_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the dest side has a preferred DnD action, use it
|
|
|
|
if ((preferred_action & available_actions) != 0) {
|
|
|
|
return preferred_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the first found action, in bit order
|
|
|
|
return 1 << (ffs(available_actions) - 1);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
2017-10-14 21:53:30 +02:00
|
|
|
|
|
|
|
static void data_offer_update_action(struct wlr_data_offer *offer) {
|
|
|
|
if (!offer->source) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
uint32_t action = data_offer_choose_action(offer);
|
2017-10-14 21:53:30 +02:00
|
|
|
if (offer->source->current_dnd_action == action) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offer->source->current_dnd_action = action;
|
|
|
|
|
|
|
|
if (offer->in_ask) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (offer->source->dnd_action) {
|
|
|
|
offer->source->dnd_action(offer->source, action);
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wl_resource_get_version(offer->resource) >=
|
|
|
|
WL_DATA_OFFER_ACTION_SINCE_VERSION) {
|
|
|
|
wl_data_offer_send_action(offer->resource, action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:41:11 +02:00
|
|
|
static void data_offer_accept(struct wl_client *client,
|
|
|
|
struct wl_resource *resource, uint32_t serial, const char *mime_type) {
|
|
|
|
struct wlr_data_offer *offer = wl_resource_get_user_data(resource);
|
|
|
|
|
|
|
|
if (!offer->source || offer != offer->source->offer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check that client is currently focused by the input device
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (offer->source->accept) {
|
|
|
|
offer->source->accept(offer->source, serial, mime_type);
|
|
|
|
}
|
2017-10-12 17:41:11 +02:00
|
|
|
offer->source->accepted = (mime_type != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_offer_receive(struct wl_client *client,
|
|
|
|
struct wl_resource *resource, const char *mime_type, int32_t fd) {
|
|
|
|
struct wlr_data_offer *offer = wl_resource_get_user_data(resource);
|
|
|
|
|
|
|
|
if (offer->source && offer == offer->source->offer) {
|
2017-10-15 20:49:43 +02:00
|
|
|
offer->source->send(offer->source, mime_type, fd);
|
2017-10-12 17:41:11 +02:00
|
|
|
} else {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void data_offer_destroy(struct wl_client *client,
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
wl_resource_destroy(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_source_notify_finish(struct wlr_data_source *source) {
|
2017-12-29 22:51:33 +01:00
|
|
|
assert(source->offer);
|
|
|
|
if (source->actions < 0) {
|
2017-10-12 17:41:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (source->offer->in_ask && source->dnd_action) {
|
|
|
|
source->dnd_action(source, source->current_dnd_action);
|
2017-10-12 17:41:11 +02:00
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (source->dnd_finish) {
|
|
|
|
source->dnd_finish(source);
|
2017-10-12 17:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
source->offer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_offer_finish(struct wl_client *client,
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
struct wlr_data_offer *offer = wl_resource_get_user_data(resource);
|
|
|
|
|
|
|
|
if (!offer->source || offer->source->offer != offer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_source_notify_finish(offer->source);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_offer_set_actions(struct wl_client *client,
|
2017-12-29 22:51:33 +01:00
|
|
|
struct wl_resource *resource, uint32_t actions,
|
2017-10-12 17:41:11 +02:00
|
|
|
uint32_t preferred_action) {
|
2017-10-13 13:58:46 +02:00
|
|
|
struct wlr_data_offer *offer = wl_resource_get_user_data(resource);
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (actions & ~ALL_ACTIONS) {
|
2017-10-13 13:58:46 +02:00
|
|
|
wl_resource_post_error(offer->resource,
|
|
|
|
WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK,
|
2017-12-29 22:51:33 +01:00
|
|
|
"invalid action mask %x", actions);
|
2017-10-13 13:58:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (preferred_action && (!(preferred_action & actions) ||
|
|
|
|
__builtin_popcount(preferred_action) > 1)) {
|
2017-10-13 13:58:46 +02:00
|
|
|
wl_resource_post_error(offer->resource,
|
|
|
|
WL_DATA_OFFER_ERROR_INVALID_ACTION,
|
|
|
|
"invalid action %x", preferred_action);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
offer->actions = actions;
|
|
|
|
offer->preferred_action = preferred_action;
|
2017-10-13 13:58:46 +02:00
|
|
|
|
|
|
|
data_offer_update_action(offer);
|
2017-10-12 17:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void data_offer_resource_destroy(struct wl_resource *resource) {
|
|
|
|
struct wlr_data_offer *offer = wl_resource_get_user_data(resource);
|
|
|
|
|
|
|
|
if (!offer->source) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_remove(&offer->source_destroy.link);
|
|
|
|
|
|
|
|
if (offer->source->offer != offer) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-12-30 09:26:48 +01:00
|
|
|
offer->source->offer = NULL;
|
|
|
|
|
2017-10-12 17:41:11 +02:00
|
|
|
// If the drag destination has version < 3, wl_data_offer.finish
|
|
|
|
// won't be called, so do this here as a safety net, because
|
2017-12-29 22:51:33 +01:00
|
|
|
// we still want the version >= 3 drag source to be happy.
|
2017-10-12 17:41:11 +02:00
|
|
|
if (wl_resource_get_version(offer->resource) <
|
|
|
|
WL_DATA_OFFER_ACTION_SINCE_VERSION) {
|
|
|
|
data_source_notify_finish(offer->source);
|
2017-12-29 22:51:33 +01:00
|
|
|
} else if (offer->source->dnd_finish) {
|
|
|
|
offer->source->cancel(offer->source);
|
2017-10-12 17:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(offer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_data_offer_interface data_offer_impl = {
|
|
|
|
.accept = data_offer_accept,
|
|
|
|
.receive = data_offer_receive,
|
|
|
|
.destroy = data_offer_destroy,
|
|
|
|
.finish = data_offer_finish,
|
|
|
|
.set_actions = data_offer_set_actions,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handle_offer_source_destroyed(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct wlr_data_offer *offer =
|
|
|
|
wl_container_of(listener, offer, source_destroy);
|
|
|
|
|
|
|
|
offer->source = NULL;
|
|
|
|
}
|
2017-10-11 21:48:40 +02:00
|
|
|
|
|
|
|
static struct wlr_data_offer *wlr_data_source_send_offer(
|
|
|
|
struct wlr_data_source *source,
|
2017-12-16 14:22:59 +01:00
|
|
|
struct wlr_seat_client *target) {
|
2017-12-16 17:46:07 +01:00
|
|
|
if (wl_list_empty(&target->data_devices)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:41:11 +02:00
|
|
|
struct wlr_data_offer *offer = calloc(1, sizeof(struct wlr_data_offer));
|
2017-12-16 17:46:07 +01:00
|
|
|
if (offer == NULL) {
|
2017-11-02 23:14:24 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-12 17:41:11 +02:00
|
|
|
|
2017-12-16 14:22:59 +01:00
|
|
|
uint32_t version = wl_resource_get_version(
|
|
|
|
wl_resource_from_link(target->data_devices.next));
|
|
|
|
offer->resource = wl_resource_create(target->client,
|
|
|
|
&wl_data_offer_interface, version, 0);
|
2017-10-12 17:41:11 +02:00
|
|
|
if (offer->resource == NULL) {
|
|
|
|
free(offer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wl_resource_set_implementation(offer->resource, &data_offer_impl, offer,
|
|
|
|
data_offer_resource_destroy);
|
|
|
|
|
|
|
|
offer->source_destroy.notify = handle_offer_source_destroyed;
|
|
|
|
wl_signal_add(&source->events.destroy, &offer->source_destroy);
|
|
|
|
|
2017-12-16 14:22:59 +01:00
|
|
|
struct wl_resource *target_resource;
|
|
|
|
wl_resource_for_each(target_resource, &target->data_devices) {
|
|
|
|
wl_data_device_send_data_offer(target_resource, offer->resource);
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:41:11 +02:00
|
|
|
char **p;
|
|
|
|
wl_array_for_each(p, &source->mime_types) {
|
|
|
|
wl_data_offer_send_offer(offer->resource, *p);
|
|
|
|
}
|
|
|
|
|
|
|
|
offer->source = source;
|
|
|
|
source->offer = offer;
|
|
|
|
source->accepted = false;
|
|
|
|
|
|
|
|
return offer;
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 13:44:39 +01:00
|
|
|
void wlr_seat_client_send_selection(struct wlr_seat_client *seat_client) {
|
2017-12-16 16:16:29 +01:00
|
|
|
if (wl_list_empty(&seat_client->data_devices)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-30 09:58:04 +01:00
|
|
|
if (seat_client->seat->selection_data_source) {
|
2017-12-16 14:22:59 +01:00
|
|
|
struct wlr_data_offer *offer = wlr_data_source_send_offer(
|
2017-12-30 09:58:04 +01:00
|
|
|
seat_client->seat->selection_data_source, seat_client);
|
2017-12-16 16:16:29 +01:00
|
|
|
if (offer == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-16 14:22:59 +01:00
|
|
|
|
|
|
|
struct wl_resource *resource;
|
|
|
|
wl_resource_for_each(resource, &seat_client->data_devices) {
|
|
|
|
wl_data_device_send_selection(resource, offer->resource);
|
|
|
|
}
|
2017-10-11 21:48:40 +02:00
|
|
|
} else {
|
2017-12-16 14:22:59 +01:00
|
|
|
struct wl_resource *resource;
|
|
|
|
wl_resource_for_each(resource, &seat_client->data_devices) {
|
|
|
|
wl_data_device_send_selection(resource, NULL);
|
|
|
|
}
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
static void seat_client_selection_data_source_destroy(
|
2017-10-12 17:41:11 +02:00
|
|
|
struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_seat *seat =
|
|
|
|
wl_container_of(listener, seat, selection_data_source_destroy);
|
2017-12-16 14:22:59 +01:00
|
|
|
struct wlr_seat_client *seat_client = seat->keyboard_state.focused_client;
|
2017-10-12 17:41:11 +02:00
|
|
|
|
2017-12-16 14:22:59 +01:00
|
|
|
if (seat_client && seat->keyboard_state.focused_surface) {
|
|
|
|
struct wl_resource *resource;
|
|
|
|
wl_resource_for_each(resource, &seat_client->data_devices) {
|
|
|
|
wl_data_device_send_selection(resource, NULL);
|
|
|
|
}
|
2017-10-16 11:07:09 +02:00
|
|
|
}
|
2017-10-12 17:41:11 +02:00
|
|
|
|
2017-12-30 09:58:04 +01:00
|
|
|
seat->selection_data_source = NULL;
|
2017-10-12 17:41:11 +02:00
|
|
|
|
2017-10-16 11:07:09 +02:00
|
|
|
wl_signal_emit(&seat->events.selection, seat);
|
2017-10-12 17:41:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 20:49:43 +02:00
|
|
|
void wlr_seat_set_selection(struct wlr_seat *seat,
|
2017-10-11 21:48:40 +02:00
|
|
|
struct wlr_data_source *source, uint32_t serial) {
|
2017-12-29 23:23:00 +01:00
|
|
|
if (source) {
|
|
|
|
assert(source->send);
|
|
|
|
assert(source->cancel);
|
|
|
|
}
|
|
|
|
|
2017-12-30 09:58:04 +01:00
|
|
|
if (seat->selection_data_source &&
|
2017-10-11 21:48:40 +02:00
|
|
|
seat->selection_serial - serial < UINT32_MAX / 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-30 09:58:04 +01:00
|
|
|
if (seat->selection_data_source) {
|
|
|
|
seat->selection_data_source->cancel(seat->selection_data_source);
|
|
|
|
seat->selection_data_source = NULL;
|
2017-10-12 17:41:11 +02:00
|
|
|
wl_list_remove(&seat->selection_data_source_destroy.link);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
2017-12-30 09:58:04 +01:00
|
|
|
seat->selection_data_source = source;
|
2017-10-11 21:48:40 +02:00
|
|
|
seat->selection_serial = serial;
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
struct wlr_seat_client *focused_client =
|
|
|
|
seat->keyboard_state.focused_client;
|
2017-10-11 21:48:40 +02:00
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
if (focused_client) {
|
|
|
|
wlr_seat_client_send_selection(focused_client);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 11:07:09 +02:00
|
|
|
wl_signal_emit(&seat->events.selection, seat);
|
2017-10-11 21:48:40 +02:00
|
|
|
|
|
|
|
if (source) {
|
2017-10-12 17:41:11 +02:00
|
|
|
seat->selection_data_source_destroy.notify =
|
2017-10-30 13:37:54 +01:00
|
|
|
seat_client_selection_data_source_destroy;
|
2017-10-12 17:41:11 +02:00
|
|
|
wl_signal_add(&source->events.destroy,
|
|
|
|
&seat->selection_data_source_destroy);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_device_set_selection(struct wl_client *client,
|
2017-10-30 13:37:54 +01:00
|
|
|
struct wl_resource *dd_resource, struct wl_resource *source_resource,
|
2017-10-11 21:48:40 +02:00
|
|
|
uint32_t serial) {
|
2017-12-29 23:01:35 +01:00
|
|
|
struct client_data_source *source = NULL;
|
2017-12-18 00:44:43 +01:00
|
|
|
if (source_resource != NULL) {
|
|
|
|
source = wl_resource_get_user_data(source_resource);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
struct wlr_seat_client *seat_client =
|
|
|
|
wl_resource_get_user_data(dd_resource);
|
2017-10-11 21:48:40 +02:00
|
|
|
|
2017-12-29 23:01:35 +01:00
|
|
|
struct wlr_data_source *wlr_source = (struct wlr_data_source *)source;
|
|
|
|
wlr_seat_set_selection(seat_client->seat, wlr_source, serial);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void data_device_release(struct wl_client *client,
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
wl_resource_destroy(resource);
|
|
|
|
}
|
|
|
|
|
2017-11-19 17:10:27 +01:00
|
|
|
static void handle_drag_seat_client_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
2017-10-30 13:37:54 +01:00
|
|
|
struct wlr_drag *drag =
|
2017-11-19 17:10:27 +01:00
|
|
|
wl_container_of(listener, drag, seat_client_destroy);
|
2017-10-14 21:53:30 +02:00
|
|
|
|
2017-11-19 17:10:27 +01:00
|
|
|
drag->focus_client = NULL;
|
|
|
|
wl_list_remove(&drag->seat_client_destroy.link);
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_drag_set_focus(struct wlr_drag *drag,
|
|
|
|
struct wlr_surface *surface, double sx, double sy) {
|
|
|
|
if (drag->focus == surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-16 14:22:59 +01:00
|
|
|
if (drag->focus_client) {
|
2017-11-19 17:10:27 +01:00
|
|
|
wl_list_remove(&drag->seat_client_destroy.link);
|
2017-12-16 14:22:59 +01:00
|
|
|
|
|
|
|
struct wl_resource *resource;
|
|
|
|
wl_resource_for_each(resource, &drag->focus_client->data_devices) {
|
|
|
|
wl_data_device_send_leave(resource);
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
drag->focus_client = NULL;
|
2017-10-14 21:53:30 +02:00
|
|
|
drag->focus = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!surface || !surface->resource) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!drag->source &&
|
|
|
|
wl_resource_get_client(surface->resource) !=
|
2017-10-30 13:37:54 +01:00
|
|
|
wl_resource_get_client(drag->seat_client->wl_resource)) {
|
2017-10-14 21:53:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drag->source && drag->source->offer) {
|
|
|
|
// unlink the offer from the source
|
|
|
|
wl_list_remove(&drag->source->offer->source_destroy.link);
|
|
|
|
drag->source->offer->source = NULL;
|
|
|
|
drag->source->offer = NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
struct wlr_seat_client *focus_client =
|
2017-10-30 13:44:39 +01:00
|
|
|
wlr_seat_client_for_wl_client(drag->seat_client->seat,
|
2017-10-14 21:53:30 +02:00
|
|
|
wl_resource_get_client(surface->resource));
|
|
|
|
|
2017-12-16 14:22:59 +01:00
|
|
|
if (!focus_client || wl_list_empty(&focus_client->data_devices)) {
|
2017-10-14 21:53:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wl_resource *offer_resource = NULL;
|
|
|
|
if (drag->source) {
|
|
|
|
drag->source->accepted = false;
|
2017-12-16 14:22:59 +01:00
|
|
|
struct wlr_data_offer *offer = wlr_data_source_send_offer(drag->source,
|
|
|
|
focus_client);
|
2017-10-14 21:53:30 +02:00
|
|
|
if (offer == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-12-29 22:51:33 +01:00
|
|
|
drag->source->actions);
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
offer_resource = offer->resource;
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
uint32_t serial =
|
2017-10-30 13:44:39 +01:00
|
|
|
wl_display_next_serial(drag->seat_client->seat->display);
|
2017-12-16 14:22:59 +01:00
|
|
|
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);
|
|
|
|
}
|
2017-10-14 21:53:30 +02:00
|
|
|
|
|
|
|
drag->focus = surface;
|
2017-10-30 13:37:54 +01:00
|
|
|
drag->focus_client = focus_client;
|
2017-11-19 17:10:27 +01:00
|
|
|
drag->seat_client_destroy.notify = handle_drag_seat_client_destroy;
|
|
|
|
wl_signal_add(&focus_client->events.destroy,
|
|
|
|
&drag->seat_client_destroy);
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_drag_end(struct wlr_drag *drag) {
|
2017-11-01 13:05:02 +01:00
|
|
|
if (!drag->cancelling) {
|
|
|
|
drag->cancelling = true;
|
2017-11-13 21:41:48 +01:00
|
|
|
if (drag->is_pointer_grab) {
|
|
|
|
wlr_seat_pointer_end_grab(drag->seat);
|
|
|
|
} else {
|
|
|
|
wlr_seat_touch_end_grab(drag->seat);
|
|
|
|
}
|
|
|
|
wlr_seat_keyboard_end_grab(drag->seat);
|
2017-10-14 21:53:30 +02:00
|
|
|
|
2017-11-01 13:05:02 +01:00
|
|
|
if (drag->source) {
|
|
|
|
wl_list_remove(&drag->source_destroy.link);
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_drag_set_focus(drag, NULL, 0, 0);
|
2017-10-14 21:53:30 +02:00
|
|
|
|
2017-11-01 13:05:02 +01:00
|
|
|
if (drag->icon) {
|
2017-11-19 15:33:55 +01:00
|
|
|
drag->icon->mapped = false;
|
2017-11-01 13:05:02 +01:00
|
|
|
wl_list_remove(&drag->icon_destroy.link);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(drag);
|
|
|
|
}
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
wlr_drag_set_focus(drag, surface, sx, sy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_drag_motion(struct wlr_seat_pointer_grab *grab,
|
|
|
|
uint32_t time, double sx, double sy) {
|
|
|
|
struct wlr_drag *drag = grab->data;
|
2017-12-16 14:22:59 +01:00
|
|
|
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));
|
|
|
|
}
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t pointer_drag_button(struct wlr_seat_pointer_grab *grab,
|
|
|
|
uint32_t time, uint32_t button, uint32_t state) {
|
|
|
|
struct wlr_drag *drag = grab->data;
|
|
|
|
|
2017-10-16 12:43:41 +02:00
|
|
|
if (drag->source &&
|
|
|
|
grab->seat->pointer_state.grab_button == button &&
|
|
|
|
state == WL_POINTER_BUTTON_STATE_RELEASED) {
|
2017-12-16 14:22:59 +01:00
|
|
|
if (drag->focus_client && drag->source->current_dnd_action &&
|
2017-10-16 12:43:41 +02:00
|
|
|
drag->source->accepted) {
|
2017-12-16 14:22:59 +01:00
|
|
|
struct wl_resource *resource;
|
|
|
|
wl_resource_for_each(resource, &drag->focus_client->data_devices) {
|
|
|
|
wl_data_device_send_drop(resource);
|
|
|
|
}
|
2017-12-29 22:51:33 +01:00
|
|
|
if (drag->source->dnd_drop) {
|
|
|
|
drag->source->dnd_drop(drag->source);
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
2017-10-16 12:43:41 +02:00
|
|
|
|
|
|
|
drag->source->offer->in_ask =
|
|
|
|
drag->source->current_dnd_action ==
|
|
|
|
WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
|
2017-12-29 22:51:33 +01:00
|
|
|
} else if (drag->source->dnd_finish) {
|
|
|
|
drag->source->cancel(drag->source);
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
2017-10-16 12:43:41 +02:00
|
|
|
}
|
2017-10-14 21:53:30 +02:00
|
|
|
|
2017-10-16 12:43:41 +02:00
|
|
|
if (grab->seat->pointer_state.button_count == 0 &&
|
|
|
|
state == WL_POINTER_BUTTON_STATE_RELEASED) {
|
2017-10-14 21:53:30 +02:00
|
|
|
wlr_drag_end(drag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_drag_axis(struct wlr_seat_pointer_grab *grab, uint32_t time,
|
|
|
|
enum wlr_axis_orientation orientation, double value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_drag_cancel(struct wlr_seat_pointer_grab *grab) {
|
|
|
|
struct wlr_drag *drag = grab->data;
|
|
|
|
wlr_drag_end(drag);
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:06:03 +02:00
|
|
|
const struct
|
|
|
|
wlr_pointer_grab_interface wlr_data_device_pointer_drag_interface = {
|
2017-10-14 21:53:30 +02:00
|
|
|
.enter = pointer_drag_enter,
|
|
|
|
.motion = pointer_drag_motion,
|
|
|
|
.button = pointer_drag_button,
|
|
|
|
.axis = pointer_drag_axis,
|
|
|
|
.cancel = pointer_drag_cancel,
|
|
|
|
};
|
|
|
|
|
2017-11-16 23:34:38 +01:00
|
|
|
uint32_t touch_drag_down(struct wlr_seat_touch_grab *grab,
|
2017-11-14 01:16:34 +01:00
|
|
|
uint32_t time, struct wlr_touch_point *point) {
|
2017-11-13 21:41:48 +01:00
|
|
|
// eat the event
|
2017-11-16 23:34:38 +01:00
|
|
|
return 0;
|
2017-11-13 14:22:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 00:08:10 +01:00
|
|
|
static void touch_drag_up(struct wlr_seat_touch_grab *grab, uint32_t time,
|
2017-11-14 01:16:34 +01:00
|
|
|
struct wlr_touch_point *point) {
|
2017-11-13 21:41:48 +01:00
|
|
|
struct wlr_drag *drag = grab->data;
|
2017-11-14 01:16:34 +01:00
|
|
|
if (drag->grab_touch_id != point->touch_id) {
|
2017-11-13 21:41:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-16 14:22:59 +01:00
|
|
|
if (drag->focus_client) {
|
|
|
|
struct wl_resource *resource;
|
|
|
|
wl_resource_for_each(resource, &drag->focus_client->data_devices) {
|
|
|
|
wl_data_device_send_drop(resource);
|
|
|
|
}
|
2017-11-13 21:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wlr_drag_end(drag);
|
2017-11-13 14:22:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 00:08:10 +01:00
|
|
|
static void touch_drag_motion(struct wlr_seat_touch_grab *grab, uint32_t time,
|
2017-11-14 01:16:34 +01:00
|
|
|
struct wlr_touch_point *point) {
|
2017-11-13 21:41:48 +01:00
|
|
|
struct wlr_drag *drag = grab->data;
|
2017-12-16 14:22:59 +01:00
|
|
|
if (drag->focus && drag->focus_client) {
|
|
|
|
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(point->sx),
|
|
|
|
wl_fixed_from_double(point->sy));
|
|
|
|
}
|
2017-11-13 21:41:48 +01:00
|
|
|
}
|
2017-11-13 14:22:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 14:51:37 +01:00
|
|
|
static void touch_drag_enter(struct wlr_seat_touch_grab *grab, uint32_t time,
|
|
|
|
struct wlr_touch_point *point) {
|
|
|
|
struct wlr_drag *drag = grab->data;
|
|
|
|
wlr_drag_set_focus(drag, point->focus_surface, point->sx, point->sy);
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:22:04 +01:00
|
|
|
static void touch_drag_cancel(struct wlr_seat_touch_grab *grab) {
|
2017-11-13 21:41:48 +01:00
|
|
|
struct wlr_drag *drag = grab->data;
|
|
|
|
wlr_drag_end(drag);
|
2017-11-13 14:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct wlr_touch_grab_interface wlr_data_device_touch_drag_interface = {
|
|
|
|
.down = touch_drag_down,
|
|
|
|
.up = touch_drag_up,
|
|
|
|
.motion = touch_drag_motion,
|
2017-11-14 14:51:37 +01:00
|
|
|
.enter = touch_drag_enter,
|
2017-11-13 14:22:04 +01:00
|
|
|
.cancel = touch_drag_cancel,
|
|
|
|
};
|
|
|
|
|
2017-10-16 13:29:18 +02:00
|
|
|
static void keyboard_drag_enter(struct wlr_seat_keyboard_grab *grab,
|
2018-01-06 17:06:09 +01:00
|
|
|
struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes,
|
|
|
|
struct wlr_keyboard_modifiers *modifiers) {
|
2017-10-16 13:29:18 +02:00
|
|
|
// nothing has keyboard focus during drags
|
|
|
|
}
|
|
|
|
|
|
|
|
static void keyboard_drag_key(struct wlr_seat_keyboard_grab *grab,
|
|
|
|
uint32_t time, uint32_t key, uint32_t state) {
|
|
|
|
// no keyboard input during drags
|
|
|
|
}
|
|
|
|
|
2018-01-06 14:32:44 +01:00
|
|
|
static void keyboard_drag_modifiers(struct wlr_seat_keyboard_grab *grab,
|
|
|
|
struct wlr_keyboard_modifiers *modifiers) {
|
2017-11-08 19:02:56 +01:00
|
|
|
//struct wlr_keyboard *keyboard = grab->seat->keyboard_state.keyboard;
|
2017-10-16 13:29:18 +02:00
|
|
|
// TODO change the dnd action based on what modifier is pressed on the
|
|
|
|
// keyboard
|
|
|
|
}
|
|
|
|
|
|
|
|
static void keyboard_drag_cancel(struct wlr_seat_keyboard_grab *grab) {
|
|
|
|
struct wlr_drag *drag = grab->data;
|
|
|
|
wlr_drag_end(drag);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct
|
|
|
|
wlr_keyboard_grab_interface wlr_data_device_keyboard_drag_interface = {
|
|
|
|
.enter = keyboard_drag_enter,
|
|
|
|
.key = keyboard_drag_key,
|
|
|
|
.modifiers = keyboard_drag_modifiers,
|
|
|
|
.cancel = keyboard_drag_cancel,
|
|
|
|
};
|
|
|
|
|
2017-10-14 21:53:30 +02:00
|
|
|
static void drag_handle_icon_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_drag *drag = wl_container_of(listener, drag, icon_destroy);
|
|
|
|
drag->icon = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drag_handle_drag_source_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct wlr_drag *drag = wl_container_of(listener, drag, source_destroy);
|
|
|
|
wlr_drag_end(drag);
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:33:55 +01:00
|
|
|
static void wlr_drag_icon_destroy(struct wlr_drag_icon *icon) {
|
|
|
|
if (!icon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wl_signal_emit(&icon->events.destroy, icon);
|
2017-12-27 12:41:14 +01:00
|
|
|
wlr_surface_set_role_committed(icon->surface, NULL, NULL);
|
2017-11-19 15:33:55 +01:00
|
|
|
wl_list_remove(&icon->surface_destroy.link);
|
2017-11-19 17:10:27 +01:00
|
|
|
wl_list_remove(&icon->seat_client_destroy.link);
|
2017-11-19 15:33:55 +01:00
|
|
|
wl_list_remove(&icon->link);
|
|
|
|
free(icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_drag_icon_surface_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct wlr_drag_icon *icon =
|
|
|
|
wl_container_of(listener, icon, surface_destroy);
|
|
|
|
wlr_drag_icon_destroy(icon);
|
|
|
|
}
|
|
|
|
|
2017-12-27 12:41:14 +01:00
|
|
|
static void handle_drag_icon_surface_commit(struct wlr_surface *surface,
|
|
|
|
void *role_data) {
|
|
|
|
struct wlr_drag_icon *icon = role_data;
|
2017-11-19 15:33:55 +01:00
|
|
|
icon->sx += icon->surface->current->sx;
|
|
|
|
icon->sy += icon->surface->current->sy;
|
|
|
|
}
|
|
|
|
|
2017-11-19 17:10:27 +01:00
|
|
|
static void handle_drag_icon_seat_client_destroy(struct wl_listener *listener,
|
2017-11-19 15:33:55 +01:00
|
|
|
void *data) {
|
|
|
|
struct wlr_drag_icon *icon =
|
2017-11-19 17:10:27 +01:00
|
|
|
wl_container_of(listener, icon, seat_client_destroy);
|
2017-11-19 15:33:55 +01:00
|
|
|
|
2017-11-19 17:10:27 +01:00
|
|
|
wlr_drag_icon_destroy(icon);
|
2017-11-19 15:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct wlr_drag_icon *wlr_drag_icon_create(
|
|
|
|
struct wlr_surface *icon_surface, struct wlr_seat_client *client,
|
|
|
|
bool is_pointer, int32_t touch_id) {
|
|
|
|
struct wlr_drag_icon *icon = calloc(1, sizeof(struct wlr_drag_icon));
|
|
|
|
if (!icon) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
icon->surface = icon_surface;
|
|
|
|
icon->client = client;
|
|
|
|
icon->is_pointer = is_pointer;
|
|
|
|
icon->touch_id = touch_id;
|
2017-11-19 16:14:57 +01:00
|
|
|
icon->mapped = true;
|
2017-11-19 15:33:55 +01:00
|
|
|
wl_list_insert(&client->seat->drag_icons, &icon->link);
|
|
|
|
|
|
|
|
wl_signal_init(&icon->events.destroy);
|
|
|
|
|
|
|
|
wl_signal_add(&icon->surface->events.destroy, &icon->surface_destroy);
|
|
|
|
icon->surface_destroy.notify = handle_drag_icon_surface_destroy;
|
|
|
|
|
2017-12-27 12:41:14 +01:00
|
|
|
wlr_surface_set_role_committed(icon->surface,
|
|
|
|
handle_drag_icon_surface_commit, icon);
|
2017-11-19 15:33:55 +01:00
|
|
|
|
2017-11-19 17:10:27 +01:00
|
|
|
wl_signal_add(&client->events.destroy, &icon->seat_client_destroy);
|
|
|
|
icon->seat_client_destroy.notify = handle_drag_icon_seat_client_destroy;
|
2017-11-19 15:33:55 +01:00
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
static bool seat_client_start_drag(struct wlr_seat_client *client,
|
2017-11-19 15:33:55 +01:00
|
|
|
struct wlr_data_source *source, struct wlr_surface *icon_surface,
|
2017-11-13 14:22:04 +01:00
|
|
|
struct wlr_surface *origin, uint32_t serial) {
|
2017-10-14 21:53:30 +02:00
|
|
|
struct wlr_drag *drag = calloc(1, sizeof(struct wlr_drag));
|
|
|
|
if (drag == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-13 21:41:48 +01:00
|
|
|
drag->seat = client->seat;
|
2017-11-13 14:22:04 +01:00
|
|
|
|
2017-12-16 13:53:39 +01:00
|
|
|
drag->is_pointer_grab = !wl_list_empty(&client->pointers) &&
|
2017-11-13 21:41:48 +01:00
|
|
|
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;
|
2017-11-13 14:22:04 +01:00
|
|
|
|
2017-12-16 13:53:39 +01:00
|
|
|
bool is_touch_grab = !wl_list_empty(&client->touches) &&
|
2017-11-16 22:45:56 +01:00
|
|
|
wlr_seat_touch_num_points(client->seat) == 1 &&
|
2017-11-13 21:41:48 +01:00
|
|
|
client->seat->touch_state.grab_serial == serial;
|
|
|
|
|
2017-11-16 22:45:56 +01:00
|
|
|
// set in the iteration
|
2017-11-13 21:41:48 +01:00
|
|
|
struct wlr_touch_point *point = NULL;
|
|
|
|
if (is_touch_grab) {
|
|
|
|
wl_list_for_each(point, &client->seat->touch_state.touch_points, link) {
|
2017-11-16 22:45:56 +01:00
|
|
|
is_touch_grab = point->surface && point->surface == origin;
|
2017-11-13 21:41:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 14:22:04 +01:00
|
|
|
|
2017-11-13 21:41:48 +01:00
|
|
|
if (!drag->is_pointer_grab && !is_touch_grab) {
|
2017-11-18 20:04:20 +01:00
|
|
|
free(drag);
|
2017-11-13 14:22:04 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:33:55 +01:00
|
|
|
if (icon_surface) {
|
|
|
|
int32_t touch_id = (point ? point->touch_id : 0);
|
|
|
|
struct wlr_drag_icon *icon =
|
|
|
|
wlr_drag_icon_create(icon_surface, client, drag->is_pointer_grab,
|
|
|
|
touch_id);
|
|
|
|
if (!icon) {
|
|
|
|
free(drag);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:06:03 +02:00
|
|
|
drag->icon = icon;
|
2017-10-14 21:53:30 +02:00
|
|
|
drag->icon_destroy.notify = drag_handle_icon_destroy;
|
|
|
|
wl_signal_add(&icon->events.destroy, &drag->icon_destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source) {
|
|
|
|
drag->source_destroy.notify = drag_handle_drag_source_destroy;
|
|
|
|
wl_signal_add(&source->events.destroy, &drag->source_destroy);
|
|
|
|
drag->source = source;
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:37:54 +01:00
|
|
|
drag->seat_client = client;
|
2017-10-14 21:53:30 +02:00
|
|
|
drag->pointer_grab.data = drag;
|
2017-10-15 17:06:03 +02:00
|
|
|
drag->pointer_grab.interface = &wlr_data_device_pointer_drag_interface;
|
2017-10-14 21:53:30 +02:00
|
|
|
|
2017-11-13 14:22:04 +01:00
|
|
|
drag->touch_grab.data = drag;
|
|
|
|
drag->touch_grab.interface = &wlr_data_device_touch_drag_interface;
|
2017-11-13 21:41:48 +01:00
|
|
|
drag->grab_touch_id = drag->seat->touch_state.grab_id;
|
2017-11-13 14:22:04 +01:00
|
|
|
|
2017-10-16 13:29:18 +02:00
|
|
|
drag->keyboard_grab.data = drag;
|
|
|
|
drag->keyboard_grab.interface = &wlr_data_device_keyboard_drag_interface;
|
|
|
|
|
2017-11-13 21:41:48 +01:00
|
|
|
wlr_seat_keyboard_start_grab(drag->seat, &drag->keyboard_grab);
|
2017-11-13 14:22:04 +01:00
|
|
|
|
2017-11-13 21:41:48 +01:00
|
|
|
if (drag->is_pointer_grab) {
|
|
|
|
wlr_seat_pointer_clear_focus(drag->seat);
|
|
|
|
wlr_seat_pointer_start_grab(drag->seat, &drag->pointer_grab);
|
2017-11-13 14:22:04 +01:00
|
|
|
} else {
|
2017-11-16 22:45:56 +01:00
|
|
|
assert(point);
|
2017-11-13 21:41:48 +01:00
|
|
|
wlr_seat_touch_start_grab(drag->seat, &drag->touch_grab);
|
|
|
|
wlr_drag_set_focus(drag, point->surface, point->sx, point->sy);
|
2017-11-13 14:22:04 +01:00
|
|
|
}
|
2017-10-14 21:53:30 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_device_start_drag(struct wl_client *client,
|
2017-10-30 13:37:54 +01:00
|
|
|
struct wl_resource *device_resource,
|
2017-10-14 21:53:30 +02:00
|
|
|
struct wl_resource *source_resource,
|
|
|
|
struct wl_resource *origin_resource, struct wl_resource *icon_resource,
|
|
|
|
uint32_t serial) {
|
2017-11-14 00:08:10 +01:00
|
|
|
struct wlr_seat_client *seat_client =
|
|
|
|
wl_resource_get_user_data(device_resource);
|
2017-10-14 21:53:30 +02:00
|
|
|
struct wlr_surface *origin = wl_resource_get_user_data(origin_resource);
|
|
|
|
struct wlr_data_source *source = NULL;
|
|
|
|
struct wlr_surface *icon = NULL;
|
|
|
|
|
|
|
|
if (source_resource) {
|
|
|
|
source = wl_resource_get_user_data(source_resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (icon_resource) {
|
|
|
|
icon = wl_resource_get_user_data(icon_resource);
|
|
|
|
}
|
|
|
|
if (icon) {
|
|
|
|
if (wlr_surface_set_role(icon, "wl_data_device-icon",
|
2017-10-30 13:37:54 +01:00
|
|
|
icon_resource, WL_DATA_DEVICE_ERROR_ROLE) < 0) {
|
2017-10-14 21:53:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:22:04 +01:00
|
|
|
if (!seat_client_start_drag(seat_client, source, icon, origin, serial)) {
|
2017-10-30 13:37:54 +01:00
|
|
|
wl_resource_post_no_memory(device_resource);
|
2017-11-16 10:30:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source) {
|
2017-10-30 13:37:54 +01:00
|
|
|
source->seat_client = seat_client;
|
2017-10-14 21:53:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:48:40 +02:00
|
|
|
static const struct wl_data_device_interface data_device_impl = {
|
|
|
|
.start_drag = data_device_start_drag,
|
|
|
|
.set_selection = data_device_set_selection,
|
|
|
|
.release = data_device_release,
|
|
|
|
};
|
|
|
|
|
2017-12-16 14:22:59 +01:00
|
|
|
static void data_device_destroy(struct wl_resource *resource) {
|
|
|
|
wl_list_remove(wl_resource_get_link(resource));
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:48:40 +02:00
|
|
|
|
2017-12-29 23:23:00 +01:00
|
|
|
struct client_data_source {
|
|
|
|
struct wlr_data_source source;
|
|
|
|
struct wl_resource *resource;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
assert(wl_resource_get_version(source->resource) >=
|
|
|
|
WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION);
|
|
|
|
wl_data_source_send_dnd_finished(source->resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
assert(wl_resource_get_version(source->resource) >=
|
|
|
|
WL_DATA_SOURCE_ACTION_SINCE_VERSION);
|
|
|
|
wl_data_source_send_action(source->resource, action);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void data_source_destroy(struct wl_client *client,
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
wl_resource_destroy(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_source_set_actions(struct wl_client *client,
|
|
|
|
struct wl_resource *resource, uint32_t dnd_actions) {
|
2017-12-29 22:51:33 +01:00
|
|
|
struct client_data_source *source =
|
2017-10-13 14:05:53 +02:00
|
|
|
wl_resource_get_user_data(resource);
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (source->source.actions >= 0) {
|
2017-10-13 14:05:53 +02:00
|
|
|
wl_resource_post_error(source->resource,
|
2017-10-14 21:53:30 +02:00
|
|
|
WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
|
|
|
|
"cannot set actions more than once");
|
2017-10-13 14:05:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dnd_actions & ~ALL_ACTIONS) {
|
|
|
|
wl_resource_post_error(source->resource,
|
2017-10-14 21:53:30 +02:00
|
|
|
WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
|
|
|
|
"invalid action mask %x", dnd_actions);
|
2017-10-13 14:05:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
if (source->source.seat_client) {
|
2017-10-13 14:05:53 +02:00
|
|
|
wl_resource_post_error(source->resource,
|
2017-10-14 21:53:30 +02:00
|
|
|
WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
|
|
|
|
"invalid action change after "
|
|
|
|
"wl_data_device.start_drag");
|
2017-10-13 14:05:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
source->source.actions = dnd_actions;
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void data_source_offer(struct wl_client *client,
|
|
|
|
struct wl_resource *resource, const char *mime_type) {
|
2017-12-29 23:01:35 +01:00
|
|
|
struct client_data_source *source = wl_resource_get_user_data(resource);
|
2017-10-11 21:48:40 +02:00
|
|
|
|
2017-12-29 23:01:35 +01:00
|
|
|
char **p = wl_array_add(&source->source.mime_types, sizeof(*p));
|
2017-10-11 21:48:40 +02:00
|
|
|
if (p) {
|
|
|
|
*p = strdup(mime_type);
|
|
|
|
}
|
2017-12-29 22:51:33 +01:00
|
|
|
if (!p || !*p) {
|
2017-12-28 09:44:35 +01:00
|
|
|
if (p) {
|
2017-12-29 23:01:35 +01:00
|
|
|
source->source.mime_types.size -= sizeof(*p);
|
2017-12-28 09:44:35 +01:00
|
|
|
}
|
2017-10-11 21:48:40 +02:00
|
|
|
wl_resource_post_no_memory(resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wl_data_source_interface data_source_impl = {
|
|
|
|
.offer = data_source_offer,
|
|
|
|
.destroy = data_source_destroy,
|
|
|
|
.set_actions = data_source_set_actions,
|
|
|
|
};
|
|
|
|
|
2017-12-29 23:23:00 +01:00
|
|
|
static void data_source_resource_handle_destroy(struct wl_resource *resource) {
|
|
|
|
struct client_data_source *source = wl_resource_get_user_data(resource);
|
|
|
|
wlr_data_source_finish(&source->source);
|
|
|
|
free(source);
|
|
|
|
}
|
|
|
|
|
2017-12-25 16:10:16 +01:00
|
|
|
void wlr_data_source_init(struct wlr_data_source *source) {
|
|
|
|
wl_array_init(&source->mime_types);
|
|
|
|
wl_signal_init(&source->events.destroy);
|
2017-12-29 22:51:33 +01:00
|
|
|
source->actions = -1;
|
2017-12-25 16:10:16 +01:00
|
|
|
}
|
|
|
|
|
2017-12-25 18:18:26 +01:00
|
|
|
void wlr_data_source_finish(struct wlr_data_source *source) {
|
|
|
|
if (source == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_signal_emit(&source->events.destroy, source);
|
|
|
|
|
|
|
|
char **p;
|
|
|
|
wl_array_for_each(p, &source->mime_types) {
|
|
|
|
free(*p);
|
|
|
|
}
|
|
|
|
wl_array_release(&source->mime_types);
|
|
|
|
}
|
|
|
|
|
2017-12-29 23:23:00 +01:00
|
|
|
|
|
|
|
void data_device_manager_get_data_device(struct wl_client *client,
|
|
|
|
struct wl_resource *manager_resource, uint32_t id,
|
|
|
|
struct wl_resource *seat_resource) {
|
|
|
|
struct wlr_seat_client *seat_client =
|
|
|
|
wl_resource_get_user_data(seat_resource);
|
|
|
|
|
|
|
|
struct wl_resource *resource = wl_resource_create(client,
|
|
|
|
&wl_data_device_interface, wl_resource_get_version(manager_resource),
|
|
|
|
id);
|
|
|
|
if (resource == NULL) {
|
|
|
|
wl_resource_post_no_memory(manager_resource);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wl_resource_set_implementation(resource, &data_device_impl, seat_client,
|
|
|
|
&data_device_destroy);
|
|
|
|
wl_list_insert(&seat_client->data_devices, wl_resource_get_link(resource));
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:48:40 +02:00
|
|
|
static void data_device_manager_create_data_source(struct wl_client *client,
|
|
|
|
struct wl_resource *resource, uint32_t id) {
|
2017-12-29 22:51:33 +01:00
|
|
|
struct client_data_source *source =
|
|
|
|
calloc(1, sizeof(struct client_data_source));
|
2017-10-11 21:48:40 +02:00
|
|
|
if (source == NULL) {
|
|
|
|
wl_resource_post_no_memory(resource);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 22:51:33 +01:00
|
|
|
wlr_data_source_init(&source->source);
|
2017-10-11 21:48:40 +02:00
|
|
|
|
2017-12-25 16:10:16 +01:00
|
|
|
source->resource = wl_resource_create(client, &wl_data_source_interface,
|
|
|
|
wl_resource_get_version(resource), id);
|
2017-10-11 21:48:40 +02:00
|
|
|
if (source->resource == NULL) {
|
|
|
|
free(source);
|
|
|
|
wl_resource_post_no_memory(resource);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-25 16:10:16 +01:00
|
|
|
wl_resource_set_implementation(source->resource, &data_source_impl,
|
2017-12-29 23:23:00 +01:00
|
|
|
source, data_source_resource_handle_destroy);
|
2017-12-29 22:51:33 +01:00
|
|
|
|
|
|
|
source->source.accept = client_data_source_accept;
|
|
|
|
source->source.send = client_data_source_send;
|
|
|
|
source->source.cancel = client_data_source_cancel;
|
2017-10-11 21:48:40 +02:00
|
|
|
|
2017-12-29 22:51:33 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_data_device_manager_interface
|
|
|
|
data_device_manager_impl = {
|
|
|
|
.create_data_source = data_device_manager_create_data_source,
|
|
|
|
.get_data_device = data_device_manager_get_data_device,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void data_device_manager_bind(struct wl_client *client,
|
|
|
|
void *data, uint32_t version, uint32_t id) {
|
2017-11-02 23:14:24 +01:00
|
|
|
struct wl_resource *resource = wl_resource_create(client,
|
|
|
|
&wl_data_device_manager_interface,
|
|
|
|
version, id);
|
2017-10-11 21:48:40 +02:00
|
|
|
if (resource == NULL) {
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-02 23:14:24 +01:00
|
|
|
wl_resource_set_implementation(resource, &data_device_manager_impl,
|
|
|
|
NULL, NULL);
|
2017-10-11 21:48:40 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:50:55 +01:00
|
|
|
void wlr_data_device_manager_destroy(struct wlr_data_device_manager *manager) {
|
|
|
|
if (!manager) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wl_list_remove(&manager->display_destroy.link);
|
2017-12-29 23:23:00 +01:00
|
|
|
// TODO: free wl_resources
|
2017-12-07 00:50:55 +01:00
|
|
|
wl_global_destroy(manager->global);
|
|
|
|
free(manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_data_device_manager *manager =
|
|
|
|
wl_container_of(listener, manager, display_destroy);
|
|
|
|
wlr_data_device_manager_destroy(manager);
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:48:40 +02:00
|
|
|
struct wlr_data_device_manager *wlr_data_device_manager_create(
|
|
|
|
struct wl_display *display) {
|
|
|
|
struct wlr_data_device_manager *manager =
|
|
|
|
calloc(1, sizeof(struct wlr_data_device_manager));
|
|
|
|
if (manager == NULL) {
|
|
|
|
wlr_log(L_ERROR, "could not create data device manager");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
manager->global =
|
|
|
|
wl_global_create(display, &wl_data_device_manager_interface,
|
|
|
|
3, NULL, data_device_manager_bind);
|
|
|
|
if (!manager->global) {
|
|
|
|
wlr_log(L_ERROR, "could not create data device manager wl global");
|
|
|
|
free(manager);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-07 00:50:55 +01:00
|
|
|
manager->display_destroy.notify = handle_display_destroy;
|
|
|
|
wl_display_add_destroy_listener(display, &manager->display_destroy);
|
2017-11-10 03:38:13 +01:00
|
|
|
|
2017-12-07 00:50:55 +01:00
|
|
|
return manager;
|
2017-11-10 03:38:13 +01:00
|
|
|
}
|