Merge pull request #1517 from emersion/refactor-dnd

Refactor drag-and-drop
This commit is contained in:
Drew DeVault 2019-02-23 13:02:28 -05:00 committed by GitHub
commit e77e53dae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 360 additions and 189 deletions

View File

@ -40,7 +40,8 @@ struct roots_seat {
struct wl_listener request_set_selection;
struct wl_listener request_set_primary_selection;
struct wl_listener new_drag_icon;
struct wl_listener request_start_drag;
struct wl_listener start_drag;
struct wl_listener destroy;
};

View File

@ -30,9 +30,6 @@ void data_source_notify_finish(struct wlr_data_source *source);
struct wlr_seat_client *seat_client_from_data_device_resource(
struct wl_resource *resource);
bool seat_client_start_drag(struct wlr_seat_client *client,
struct wlr_data_source *source, struct wlr_surface *icon_surface,
struct wlr_surface *origin, uint32_t serial);
/**
* Creates a new wl_data_offer if there is a wl_data_source currently set as
* the seat selection and sends it to the seat client, followed by the

View File

@ -89,14 +89,12 @@ struct wlr_data_source {
} events;
};
struct wlr_drag_icon {
struct wlr_surface *surface;
struct wlr_seat_client *client;
struct wl_list link; // wlr_seat::drag_icons
bool mapped;
struct wlr_drag;
bool is_pointer;
int32_t touch_id;
struct wlr_drag_icon {
struct wlr_drag *drag;
struct wlr_surface *surface;
bool mapped;
struct {
struct wl_signal map;
@ -105,40 +103,46 @@ struct wlr_drag_icon {
} events;
struct wl_listener surface_destroy;
struct wl_listener seat_client_destroy;
void *data;
};
enum wlr_drag_grab_type {
WLR_DRAG_GRAB_KEYBOARD,
WLR_DRAG_GRAB_KEYBOARD_POINTER,
WLR_DRAG_GRAB_KEYBOARD_TOUCH,
};
struct wlr_drag {
struct wlr_seat_pointer_grab pointer_grab;
enum wlr_drag_grab_type grab_type;
struct wlr_seat_keyboard_grab keyboard_grab;
struct wlr_seat_pointer_grab pointer_grab;
struct wlr_seat_touch_grab touch_grab;
struct wlr_seat *seat;
struct wlr_seat_client *seat_client;
struct wlr_seat_client *focus_client;
bool is_pointer_grab;
struct wlr_drag_icon *icon; // can be NULL
struct wlr_surface *focus; // can be NULL
struct wlr_data_source *source; // can be NULL
struct wlr_drag_icon *icon;
struct wlr_surface *focus;
struct wlr_data_source *source;
bool started, dropped, cancelling;
int32_t grab_touch_id, touch_id; // if WLR_DRAG_GRAB_TOUCH
bool cancelling;
int32_t grab_touch_id;
struct {
struct wl_signal focus;
struct wl_signal motion; // wlr_drag_motion_event
struct wl_signal drop; // wlr_drag_drop_event
struct wl_signal destroy;
} events;
struct wl_listener point_destroy;
struct wl_listener source_destroy;
struct wl_listener seat_client_destroy;
struct wl_listener icon_destroy;
struct {
struct wl_signal focus;
struct wl_signal motion;
struct wl_signal drop;
struct wl_signal destroy;
} events;
void *data;
};
struct wlr_drag_motion_event {
@ -178,6 +182,40 @@ void wlr_seat_request_set_selection(struct wlr_seat *seat,
void wlr_seat_set_selection(struct wlr_seat *seat,
struct wlr_data_source *source, uint32_t serial);
/**
* Creates a new drag. To request to start the drag, call
* `wlr_seat_request_start_drag`.
*/
struct wlr_drag *wlr_drag_create(struct wlr_seat_client *seat_client,
struct wlr_data_source *source, struct wlr_surface *icon_surface);
/**
* Requests a drag to be started on the seat.
*/
void wlr_seat_request_start_drag(struct wlr_seat *seat, struct wlr_drag *drag,
struct wlr_surface *origin, uint32_t serial);
/**
* Starts a drag on the seat. This starts an implicit keyboard grab, but doesn't
* start a pointer or a touch grab.
*/
void wlr_seat_start_drag(struct wlr_seat *seat, struct wlr_drag *drag,
uint32_t serial);
/**
* Starts a pointer drag on the seat. This starts implicit keyboard and pointer
* grabs.
*/
void wlr_seat_start_pointer_drag(struct wlr_seat *seat, struct wlr_drag *drag,
uint32_t serial);
/**
* Starts a touch drag on the seat. This starts implicit keyboard and touch
* grabs.
*/
void wlr_seat_start_touch_drag(struct wlr_seat *seat, struct wlr_drag *drag,
uint32_t serial, struct wlr_touch_point *point);
/**
* Initializes the data source with the provided implementation.
*/

View File

@ -191,7 +191,6 @@ struct wlr_seat {
struct wl_global *global;
struct wl_display *display;
struct wl_list clients;
struct wl_list drag_icons; // wlr_drag_icon::link
char *name;
uint32_t capabilities;
@ -239,8 +238,9 @@ struct wlr_seat {
struct wl_signal request_set_primary_selection;
struct wl_signal set_primary_selection;
// wlr_seat_request_start_drag_event
struct wl_signal request_start_drag;
struct wl_signal start_drag; // wlr_drag
struct wl_signal new_drag_icon; // wlr_drag_icon
struct wl_signal destroy;
} events;
@ -265,6 +265,12 @@ struct wlr_seat_request_set_primary_selection_event {
uint32_t serial;
};
struct wlr_seat_request_start_drag_event {
struct wlr_drag *drag;
struct wlr_surface *origin;
uint32_t serial;
};
struct wlr_seat_pointer_focus_change_event {
struct wlr_seat *seat;
struct wlr_surface *old_surface, *new_surface;
@ -597,9 +603,30 @@ bool wlr_seat_touch_has_grab(struct wlr_seat *seat);
*/
bool wlr_seat_validate_grab_serial(struct wlr_seat *seat, uint32_t serial);
/**
* Check whether this serial is valid to start a pointer grab action.
*/
bool wlr_seat_validate_pointer_grab_serial(struct wlr_seat *seat,
struct wlr_surface *origin, uint32_t serial);
/**
* Check whether this serial is valid to start a touch grab action. If it's the
* case and point_ptr is non-NULL, *point_ptr is set to the touch point matching
* the serial.
*/
bool wlr_seat_validate_touch_grab_serial(struct wlr_seat *seat,
struct wlr_surface *origin, uint32_t serial,
struct wlr_touch_point **point_ptr);
/**
* Get a seat client from a seat resource. Returns NULL if inert.
*/
struct wlr_seat_client *wlr_seat_client_from_resource(
struct wl_resource *resource);
/**
* Get a seat client from a pointer resource. Returns NULL if inert.
*/
struct wlr_seat_client *wlr_seat_client_from_pointer_resource(
struct wl_resource *resource);

View File

@ -603,10 +603,38 @@ static void roots_drag_icon_handle_destroy(struct wl_listener *listener,
free(icon);
}
static void roots_seat_handle_new_drag_icon(struct wl_listener *listener,
static void roots_seat_handle_request_start_drag(struct wl_listener *listener,
void *data) {
struct roots_seat *seat = wl_container_of(listener, seat, new_drag_icon);
struct wlr_drag_icon *wlr_drag_icon = data;
struct roots_seat *seat =
wl_container_of(listener, seat, request_start_drag);
struct wlr_seat_request_start_drag_event *event = data;
if (wlr_seat_validate_pointer_grab_serial(seat->seat,
event->origin, event->serial)) {
wlr_seat_start_pointer_drag(seat->seat, event->drag, event->serial);
return;
}
struct wlr_touch_point *point;
if (wlr_seat_validate_touch_grab_serial(seat->seat,
event->origin, event->serial, &point)) {
wlr_seat_start_touch_drag(seat->seat, event->drag, event->serial, point);
return;
}
wlr_log(WLR_DEBUG, "Ignoring start_drag request: "
"could not validate pointer or touch serial %" PRIu32, event->serial);
wlr_data_source_destroy(event->drag->source);
}
static void roots_seat_handle_start_drag(struct wl_listener *listener,
void *data) {
struct roots_seat *seat = wl_container_of(listener, seat, start_drag);
struct wlr_drag *wlr_drag = data;
struct wlr_drag_icon *wlr_drag_icon = wlr_drag->icon;
if (wlr_drag_icon == NULL) {
return;
}
struct roots_drag_icon *icon = calloc(1, sizeof(struct roots_drag_icon));
if (icon == NULL) {
@ -649,20 +677,27 @@ static void roots_seat_handle_request_set_primary_selection(
void roots_drag_icon_update_position(struct roots_drag_icon *icon) {
roots_drag_icon_damage_whole(icon);
struct wlr_drag_icon *wlr_icon = icon->wlr_drag_icon;
struct roots_seat *seat = icon->seat;
struct wlr_drag *wlr_drag = icon->wlr_drag_icon->drag;
assert(wlr_drag != NULL);
switch (seat->seat->drag->grab_type) {
case WLR_DRAG_GRAB_KEYBOARD:
assert(false);
case WLR_DRAG_GRAB_KEYBOARD_POINTER:;
struct wlr_cursor *cursor = seat->cursor->cursor;
if (wlr_icon->is_pointer) {
icon->x = cursor->x;
icon->y = cursor->y;
} else {
break;
case WLR_DRAG_GRAB_KEYBOARD_TOUCH:;
struct wlr_touch_point *point =
wlr_seat_touch_get_point(seat->seat, wlr_icon->touch_id);
wlr_seat_touch_get_point(seat->seat, wlr_drag->touch_id);
if (point == NULL) {
return;
}
icon->x = seat->touch_x;
icon->y = seat->touch_y;
break;
}
roots_drag_icon_damage_whole(icon);
@ -738,8 +773,11 @@ struct roots_seat *roots_seat_create(struct roots_input *input, char *name) {
roots_seat_handle_request_set_primary_selection;
wl_signal_add(&seat->seat->events.request_set_primary_selection,
&seat->request_set_primary_selection);
seat->new_drag_icon.notify = roots_seat_handle_new_drag_icon;
wl_signal_add(&seat->seat->events.new_drag_icon, &seat->new_drag_icon);
seat->request_start_drag.notify = roots_seat_handle_request_start_drag;
wl_signal_add(&seat->seat->events.request_start_drag,
&seat->request_start_drag);
seat->start_drag.notify = roots_seat_handle_start_drag;
wl_signal_add(&seat->seat->events.start_drag, &seat->start_drag);
seat->destroy.notify = roots_seat_handle_destroy;
wl_signal_add(&seat->seat->events.destroy, &seat->destroy);

View File

@ -73,8 +73,8 @@ static void data_device_start_drag(struct wl_client *client,
struct wlr_data_source *wlr_source =
source != NULL ? &source->source : NULL;
if (!seat_client_start_drag(seat_client, wlr_source, icon,
origin, serial)) {
struct wlr_drag *drag = wlr_drag_create(seat_client, wlr_source, icon);
if (drag == NULL) {
wl_resource_post_no_memory(device_resource);
return;
}
@ -82,6 +82,8 @@ static void data_device_start_drag(struct wl_client *client,
if (source != NULL) {
source->finalized = true;
}
wlr_seat_request_start_drag(seat_client->seat, drag, origin, serial);
}
static void data_device_release(struct wl_client *client,

View File

@ -111,20 +111,6 @@ static void data_offer_dnd_finish(struct wlr_data_offer *offer) {
static void data_offer_handle_destroy(struct wl_client *client,
struct wl_resource *resource) {
struct wlr_data_offer *offer = data_offer_from_resource(resource);
if (offer == NULL) {
goto out;
}
// If the drag destination has version < 3, wl_data_offer.finish
// won't be called, so do this here as a safety net, because
// we still want the version >= 3 drag source to be happy.
if (wl_resource_get_version(offer->resource) <
WL_DATA_OFFER_ACTION_SINCE_VERSION) {
data_offer_dnd_finish(offer);
}
out:
wl_resource_destroy(resource);
}
@ -135,6 +121,27 @@ static void data_offer_handle_finish(struct wl_client *client,
return;
}
// TODO: also fail while we have a drag-and-drop grab
if (offer->type != WLR_DATA_OFFER_DRAG) {
wl_resource_post_error(offer->resource,
WL_DATA_OFFER_ERROR_INVALID_FINISH, "Offer is not drag-and-drop");
return;
}
if (!offer->source->accepted) {
wl_resource_post_error(offer->resource,
WL_DATA_OFFER_ERROR_INVALID_FINISH, "Premature finish request");
return;
}
enum wl_data_device_manager_dnd_action action =
offer->source->current_dnd_action;
if (action == WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE ||
action == WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK) {
wl_resource_post_error(offer->resource,
WL_DATA_OFFER_ERROR_INVALID_FINISH,
"Offer finished with an invalid action");
return;
}
data_offer_dnd_finish(offer);
data_offer_destroy(offer);
}
@ -183,6 +190,18 @@ void data_offer_destroy(struct wlr_data_offer *offer) {
wl_list_remove(&offer->source_destroy.link);
wl_list_remove(&offer->link);
if (offer->type == WLR_DATA_OFFER_DRAG) {
// If the drag destination has version < 3, wl_data_offer.finish
// won't be called, so do this here as a safety net, because
// we still want the version >= 3 drag source to be happy.
if (wl_resource_get_version(offer->resource) <
WL_DATA_OFFER_ACTION_SINCE_VERSION) {
data_offer_dnd_finish(offer);
} else if (offer->source && offer->source->impl->dnd_finish) {
wlr_data_source_destroy(offer->source);
}
}
// Make the resource inert
wl_resource_set_user_data(offer->resource, NULL);
@ -206,6 +225,8 @@ static void data_offer_handle_source_destroy(struct wl_listener *listener,
void *data) {
struct wlr_data_offer *offer =
wl_container_of(listener, offer, source_destroy);
// Prevent data_offer_destroy from destroying the source again
offer->source = NULL;
data_offer_destroy(offer);
}

View File

@ -28,6 +28,20 @@ static void drag_set_focus(struct wlr_drag *drag,
if (drag->focus_client) {
wl_list_remove(&drag->seat_client_destroy.link);
// If we're switching focus to another client, we want to destroy all
// offers without destroying the source. If the drag operation ends, we
// want to keep the offer around for the data transfer.
struct wlr_data_offer *offer, *tmp;
wl_list_for_each_safe(offer, tmp,
&drag->focus_client->seat->drag_offers, link) {
struct wl_client *client = wl_resource_get_client(offer->resource);
if (!drag->dropped && offer->source == drag->source &&
client == drag->focus_client->client) {
offer->source = NULL;
data_offer_destroy(offer);
}
}
struct wl_resource *resource;
wl_resource_for_each(resource, &drag->focus_client->data_devices) {
wl_data_device_send_leave(resource);
@ -37,20 +51,20 @@ static void drag_set_focus(struct wlr_drag *drag,
drag->focus = NULL;
}
if (!surface || !surface->resource) {
return;
if (!surface) {
goto out;
}
if (!drag->source &&
wl_resource_get_client(surface->resource) !=
drag->seat_client->client) {
return;
goto out;
}
struct wlr_seat_client *focus_client = wlr_seat_client_for_wl_client(
drag->seat_client->seat, wl_resource_get_client(surface->resource));
if (!focus_client) {
return;
goto out;
}
if (drag->source != NULL) {
@ -88,6 +102,7 @@ static void drag_set_focus(struct wlr_drag *drag,
drag->seat_client_destroy.notify = drag_handle_seat_client_destroy;
wl_signal_add(&focus_client->events.destroy, &drag->seat_client_destroy);
out:
wlr_signal_emit_safe(&drag->events.focus, drag);
}
@ -101,34 +116,42 @@ static void drag_icon_set_mapped(struct wlr_drag_icon *icon, bool mapped) {
}
}
static void drag_end(struct wlr_drag *drag) {
if (!drag->cancelling) {
drag->cancelling = true;
if (drag->is_pointer_grab) {
wlr_seat_pointer_end_grab(drag->seat);
} else {
wlr_seat_touch_end_grab(drag->seat);
static void drag_icon_destroy(struct wlr_drag_icon *icon);
static void drag_destroy(struct wlr_drag *drag) {
if (drag->cancelling) {
return;
}
drag->cancelling = true;
wlr_signal_emit_safe(&drag->events.destroy, drag);
if (drag->started) {
wlr_seat_keyboard_end_grab(drag->seat);
switch (drag->grab_type) {
case WLR_DRAG_GRAB_KEYBOARD:
break;
case WLR_DRAG_GRAB_KEYBOARD_POINTER:
wlr_seat_pointer_end_grab(drag->seat);
break;
case WLR_DRAG_GRAB_KEYBOARD_TOUCH:
wlr_seat_touch_end_grab(drag->seat);
break;
}
drag_set_focus(drag, NULL, 0, 0);
assert(drag->seat->drag == drag);
drag->seat->drag = NULL;
}
if (drag->source) {
wl_list_remove(&drag->source_destroy.link);
}
drag_set_focus(drag, NULL, 0, 0);
if (drag->icon) {
wl_list_remove(&drag->icon_destroy.link);
drag_icon_set_mapped(drag->icon, false);
}
assert(drag->seat->drag == drag);
drag->seat->drag = NULL;
wlr_signal_emit_safe(&drag->events.destroy, drag);
drag_icon_destroy(drag->icon);
free(drag);
}
}
static void drag_handle_pointer_enter(struct wlr_seat_pointer_grab *grab,
struct wlr_surface *surface, double sx, double sy) {
@ -156,6 +179,26 @@ static void drag_handle_pointer_motion(struct wlr_seat_pointer_grab *grab,
}
}
static void drag_drop(struct wlr_drag *drag, uint32_t time) {
assert(drag->focus_client);
drag->dropped = true;
struct wl_resource *resource;
wl_resource_for_each(resource, &drag->focus_client->data_devices) {
wl_data_device_send_drop(resource);
}
if (drag->source) {
wlr_data_source_dnd_drop(drag->source);
}
struct wlr_drag_drop_event event = {
.drag = drag,
.time = time,
};
wlr_signal_emit_safe(&drag->events.drop, &event);
}
static uint32_t drag_handle_pointer_button(struct wlr_seat_pointer_grab *grab,
uint32_t time, uint32_t button, uint32_t state) {
struct wlr_drag *drag = grab->data;
@ -165,17 +208,7 @@ static uint32_t drag_handle_pointer_button(struct wlr_seat_pointer_grab *grab,
state == WL_POINTER_BUTTON_STATE_RELEASED) {
if (drag->focus_client && drag->source->current_dnd_action &&
drag->source->accepted) {
struct wl_resource *resource;
wl_resource_for_each(resource, &drag->focus_client->data_devices) {
wl_data_device_send_drop(resource);
}
wlr_data_source_dnd_drop(drag->source);
struct wlr_drag_drop_event event = {
.drag = drag,
.time = time,
};
wlr_signal_emit_safe(&drag->events.drop, &event);
drag_drop(drag, time);
} else if (drag->source->impl->dnd_finish) {
// This will end the grab and free `drag`
wlr_data_source_destroy(drag->source);
@ -185,7 +218,7 @@ static uint32_t drag_handle_pointer_button(struct wlr_seat_pointer_grab *grab,
if (grab->seat->pointer_state.button_count == 0 &&
state == WL_POINTER_BUTTON_STATE_RELEASED) {
drag_end(drag);
drag_destroy(drag);
}
return 0;
@ -199,7 +232,7 @@ static void drag_handle_pointer_axis(struct wlr_seat_pointer_grab *grab,
static void drag_handle_pointer_cancel(struct wlr_seat_pointer_grab *grab) {
struct wlr_drag *drag = grab->data;
drag_end(drag);
drag_destroy(drag);
}
static const struct wlr_pointer_grab_interface
@ -225,13 +258,10 @@ static void drag_handle_touch_up(struct wlr_seat_touch_grab *grab,
}
if (drag->focus_client) {
struct wl_resource *resource;
wl_resource_for_each(resource, &drag->focus_client->data_devices) {
wl_data_device_send_drop(resource);
}
drag_drop(drag, time);
}
drag_end(drag);
drag_destroy(drag);
}
static void drag_handle_touch_motion(struct wlr_seat_touch_grab *grab,
@ -255,7 +285,7 @@ static void drag_handle_touch_enter(struct wlr_seat_touch_grab *grab,
static void drag_handle_touch_cancel(struct wlr_seat_touch_grab *grab) {
struct wlr_drag *drag = grab->data;
drag_end(drag);
drag_destroy(drag);
}
static const struct wlr_touch_grab_interface
@ -287,7 +317,7 @@ static void drag_handle_keyboard_modifiers(struct wlr_seat_keyboard_grab *grab,
static void drag_handle_keyboard_cancel(struct wlr_seat_keyboard_grab *grab) {
struct wlr_drag *drag = grab->data;
drag_end(drag);
drag_destroy(drag);
}
static const struct wlr_keyboard_grab_interface
@ -306,7 +336,7 @@ static void drag_handle_icon_destroy(struct wl_listener *listener, void *data) {
static void drag_handle_drag_source_destroy(struct wl_listener *listener,
void *data) {
struct wlr_drag *drag = wl_container_of(listener, drag, source_destroy);
drag_end(drag);
drag_destroy(drag);
}
@ -318,8 +348,6 @@ static void drag_icon_destroy(struct wlr_drag_icon *icon) {
wlr_signal_emit_safe(&icon->events.destroy, icon);
icon->surface->role_data = NULL;
wl_list_remove(&icon->surface_destroy.link);
wl_list_remove(&icon->seat_client_destroy.link);
wl_list_remove(&icon->link);
free(icon);
}
@ -345,26 +373,15 @@ const struct wlr_surface_role drag_icon_surface_role = {
.commit = drag_icon_surface_role_commit,
};
static void drag_icon_handle_seat_client_destroy(struct wl_listener *listener,
void *data) {
struct wlr_drag_icon *icon =
wl_container_of(listener, icon, seat_client_destroy);
drag_icon_destroy(icon);
}
static struct wlr_drag_icon *drag_icon_create(
struct wlr_surface *icon_surface, struct wlr_seat_client *client,
bool is_pointer, int32_t touch_id) {
static struct wlr_drag_icon *drag_icon_create(struct wlr_drag *drag,
struct wlr_surface *surface) {
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;
icon->drag = drag;
icon->surface = surface;
wl_signal_init(&icon->events.map);
wl_signal_init(&icon->events.unmap);
@ -373,15 +390,9 @@ static struct wlr_drag_icon *drag_icon_create(
wl_signal_add(&icon->surface->events.destroy, &icon->surface_destroy);
icon->surface_destroy.notify = drag_icon_handle_surface_destroy;
wl_signal_add(&client->events.destroy, &icon->seat_client_destroy);
icon->seat_client_destroy.notify = drag_icon_handle_seat_client_destroy;
icon->surface->role_data = icon;
wl_list_insert(&client->seat->drag_icons, &icon->link);
wlr_signal_emit_safe(&client->seat->events.new_drag_icon, icon);
if (wlr_surface_has_buffer(icon_surface)) {
if (wlr_surface_has_buffer(surface)) {
drag_icon_set_mapped(icon, true);
}
@ -389,20 +400,11 @@ static struct wlr_drag_icon *drag_icon_create(
}
static void seat_handle_drag_source_destroy(struct wl_listener *listener,
void *data) {
struct wlr_seat *seat =
wl_container_of(listener, seat, drag_source_destroy);
wl_list_remove(&seat->drag_source_destroy.link);
seat->drag_source = NULL;
}
bool seat_client_start_drag(struct wlr_seat_client *client,
struct wlr_data_source *source, struct wlr_surface *icon_surface,
struct wlr_surface *origin, uint32_t serial) {
struct wlr_drag *wlr_drag_create(struct wlr_seat_client *seat_client,
struct wlr_data_source *source, struct wlr_surface *icon_surface) {
struct wlr_drag *drag = calloc(1, sizeof(struct wlr_drag));
if (drag == NULL) {
return false;
return NULL;
}
wl_signal_init(&drag->events.focus);
@ -410,51 +412,18 @@ bool seat_client_start_drag(struct wlr_seat_client *client,
wl_signal_init(&drag->events.drop);
wl_signal_init(&drag->events.destroy);
struct wlr_seat *seat = client->seat;
drag->seat = seat;
drag->is_pointer_grab = !wl_list_empty(&client->pointers) &&
seat->pointer_state.button_count == 1 &&
seat->pointer_state.grab_serial == serial &&
seat->pointer_state.focused_surface &&
seat->pointer_state.focused_surface == origin;
bool is_touch_grab = !wl_list_empty(&client->touches) &&
wlr_seat_touch_num_points(seat) == 1 &&
seat->touch_state.grab_serial == serial;
// set in the iteration
struct wlr_touch_point *point = NULL;
if (is_touch_grab) {
wl_list_for_each(point, &seat->touch_state.touch_points, link) {
is_touch_grab = point->surface && point->surface == origin;
break;
}
}
if (!drag->is_pointer_grab && !is_touch_grab) {
free(drag);
return true;
}
if (seat->drag != NULL) {
wlr_log(WLR_DEBUG, "Refusing to start drag, "
"another drag is already in progress");
free(drag);
return true;
}
drag->seat = seat_client->seat;
drag->seat_client = seat_client;
if (icon_surface) {
int32_t touch_id = (point ? point->touch_id : 0);
struct wlr_drag_icon *icon =
drag_icon_create(icon_surface, client, drag->is_pointer_grab,
touch_id);
if (!icon) {
struct wlr_drag_icon *icon = drag_icon_create(drag, icon_surface);
if (icon == NULL) {
free(drag);
return false;
return NULL;
}
drag->icon = icon;
drag->icon_destroy.notify = drag_handle_icon_destroy;
wl_signal_add(&icon->events.destroy, &drag->icon_destroy);
}
@ -465,41 +434,85 @@ bool seat_client_start_drag(struct wlr_seat_client *client,
wl_signal_add(&source->events.destroy, &drag->source_destroy);
}
drag->seat_client = client;
drag->pointer_grab.data = drag;
drag->pointer_grab.interface = &data_device_pointer_drag_interface;
drag->touch_grab.data = drag;
drag->touch_grab.interface = &data_device_touch_drag_interface;
drag->grab_touch_id = seat->touch_state.grab_id;
drag->keyboard_grab.data = drag;
drag->keyboard_grab.interface = &data_device_keyboard_drag_interface;
wlr_seat_keyboard_start_grab(seat, &drag->keyboard_grab);
if (drag->is_pointer_grab) {
wlr_seat_pointer_clear_focus(seat);
wlr_seat_pointer_start_grab(seat, &drag->pointer_grab);
} else {
assert(point);
wlr_seat_touch_start_grab(seat, &drag->touch_grab);
drag_set_focus(drag, point->surface, point->sx, point->sy);
return drag;
}
void wlr_seat_request_start_drag(struct wlr_seat *seat, struct wlr_drag *drag,
struct wlr_surface *origin, uint32_t serial) {
assert(drag->seat == seat);
if (seat->drag != NULL) {
wlr_log(WLR_DEBUG, "Rejecting start_drag request, "
"another drag-and-drop operation is already in progress");
return;
}
struct wlr_seat_request_start_drag_event event = {
.drag = drag,
.origin = origin,
.serial = serial,
};
wlr_signal_emit_safe(&seat->events.request_start_drag, &event);
}
static void seat_handle_drag_source_destroy(struct wl_listener *listener,
void *data) {
struct wlr_seat *seat =
wl_container_of(listener, seat, drag_source_destroy);
wl_list_remove(&seat->drag_source_destroy.link);
seat->drag_source = NULL;
}
void wlr_seat_start_drag(struct wlr_seat *seat, struct wlr_drag *drag,
uint32_t serial) {
assert(drag->seat == seat);
assert(!drag->started);
drag->started = true;
wlr_seat_keyboard_start_grab(seat, &drag->keyboard_grab);
seat->drag = drag;
seat->drag_serial = serial;
// We need to destroy the previous source, because listeners only expect one
// active drag source at a time.
wlr_data_source_destroy(seat->drag_source);
seat->drag_source = source;
if (source != NULL) {
seat->drag_source = drag->source;
if (drag->source != NULL) {
seat->drag_source_destroy.notify = seat_handle_drag_source_destroy;
wl_signal_add(&source->events.destroy, &seat->drag_source_destroy);
wl_signal_add(&drag->source->events.destroy, &seat->drag_source_destroy);
}
wlr_signal_emit_safe(&seat->events.start_drag, drag);
return true;
}
void wlr_seat_start_pointer_drag(struct wlr_seat *seat, struct wlr_drag *drag,
uint32_t serial) {
drag->grab_type = WLR_DRAG_GRAB_KEYBOARD_POINTER;
wlr_seat_pointer_clear_focus(seat);
wlr_seat_pointer_start_grab(seat, &drag->pointer_grab);
wlr_seat_start_drag(seat, drag, serial);
}
void wlr_seat_start_touch_drag(struct wlr_seat *seat, struct wlr_drag *drag,
uint32_t serial, struct wlr_touch_point *point) {
drag->grab_type = WLR_DRAG_GRAB_KEYBOARD_TOUCH;
drag->grab_touch_id = seat->touch_state.grab_id;
drag->touch_id = point->touch_id;
wlr_seat_touch_start_grab(seat, &drag->touch_grab);
drag_set_focus(drag, point->surface, point->sx, point->sy);
wlr_seat_start_drag(seat, drag, serial);
}

View File

@ -266,12 +266,11 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) {
seat->display = display;
seat->name = strdup(name);
wl_list_init(&seat->clients);
wl_list_init(&seat->drag_icons);
wl_list_init(&seat->selection_offers);
wl_list_init(&seat->drag_offers);
wl_signal_init(&seat->events.request_start_drag);
wl_signal_init(&seat->events.start_drag);
wl_signal_init(&seat->events.new_drag_icon);
wl_signal_init(&seat->events.request_set_cursor);

View File

@ -402,3 +402,17 @@ void seat_client_destroy_pointer(struct wl_resource *resource) {
}
wl_resource_set_user_data(resource, NULL);
}
bool wlr_seat_validate_pointer_grab_serial(struct wlr_seat *seat,
struct wlr_surface *origin, uint32_t serial) {
if (seat->pointer_state.button_count != 1 ||
seat->pointer_state.grab_serial != serial) {
return false;
}
if (origin != NULL && seat->pointer_state.focused_surface != origin) {
return false;
}
return true;
}

View File

@ -359,3 +359,24 @@ void seat_client_destroy_touch(struct wl_resource *resource) {
}
wl_resource_set_user_data(resource, NULL);
}
bool wlr_seat_validate_touch_grab_serial(struct wlr_seat *seat,
struct wlr_surface *origin, uint32_t serial,
struct wlr_touch_point **point_ptr) {
if (wlr_seat_touch_num_points(seat) != 1 ||
seat->touch_state.grab_serial != serial) {
return false;
}
struct wlr_touch_point *point;
wl_list_for_each(point, &seat->touch_state.touch_points, link) {
if (origin == NULL || point->surface == origin) {
if (point_ptr != NULL) {
*point_ptr = point;
}
return true;
}
}
return false;
}