data-device: refactor wlr_drag

This commit is contained in:
emersion 2019-01-30 18:36:19 +01:00
parent 18600f457b
commit 6291e84532
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
10 changed files with 281 additions and 156 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, 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,37 @@ 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);
}
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 +676,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_cursor *cursor = seat->cursor->cursor;
if (wlr_icon->is_pointer) {
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;
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 +772,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

@ -101,33 +101,39 @@ static void drag_icon_set_mapped(struct wlr_drag_icon *icon, bool mapped) {
}
}
static void drag_icon_destroy(struct wlr_drag_icon *icon);
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);
}
wlr_seat_keyboard_end_grab(drag->seat);
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);
free(drag);
if (drag->cancelling) {
return;
}
drag->cancelling = true;
wlr_signal_emit_safe(&drag->events.destroy, drag);
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;
}
if (drag->source) {
wl_list_remove(&drag->source_destroy.link);
}
drag_set_focus(drag, NULL, 0, 0);
drag_icon_destroy(drag->icon);
assert(drag->seat->drag == drag);
drag->seat->drag = NULL;
free(drag);
}
static void drag_handle_pointer_enter(struct wlr_seat_pointer_grab *grab,
@ -318,8 +324,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 +349,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 +366,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 +376,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 +388,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 +410,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);
return drag;
}
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);
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;
}