summaryrefslogtreecommitdiff
path: root/src/data-device.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/data-device.c')
-rw-r--r--src/data-device.c408
1 files changed, 323 insertions, 85 deletions
diff --git a/src/data-device.c b/src/data-device.c
index c3dc0bda..483e22e9 100644
--- a/src/data-device.c
+++ b/src/data-device.c
@@ -26,6 +26,7 @@
#include <string.h>
#include <unistd.h>
#include <stdio.h>
+#include <assert.h>
#include "compositor.h"
@@ -33,15 +34,24 @@ struct weston_drag {
struct wl_client *client;
struct weston_data_source *data_source;
struct wl_listener data_source_listener;
- struct weston_surface *focus;
+ struct weston_view *focus;
struct wl_resource *focus_resource;
struct wl_listener focus_listener;
- struct weston_pointer_grab grab;
- struct weston_surface *icon;
+ struct weston_view *icon;
struct wl_listener icon_destroy_listener;
int32_t dx, dy;
};
+struct weston_pointer_drag {
+ struct weston_drag base;
+ struct weston_pointer_grab grab;
+};
+
+struct weston_touch_drag {
+ struct weston_drag base;
+ struct weston_touch_grab grab;
+};
+
static void
empty_region(pixman_region32_t *region)
{
@@ -171,30 +181,67 @@ static struct wl_data_source_interface data_source_interface = {
};
static void
-drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
+drag_surface_configure(struct weston_drag *drag,
+ struct weston_pointer *pointer,
+ struct weston_touch *touch,
+ struct weston_surface *es,
+ int32_t sx, int32_t sy)
{
- struct weston_drag *drag = es->configure_private;
- struct weston_pointer *pointer = drag->grab.pointer;
struct wl_list *list;
float fx, fy;
+ assert((pointer != NULL && touch == NULL) ||
+ (pointer == NULL && touch != NULL));
+
if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
- if (pointer->sprite && weston_surface_is_mapped(pointer->sprite))
+ if (pointer && pointer->sprite &&
+ weston_view_is_mapped(pointer->sprite))
list = &pointer->sprite->layer_link;
else
- list = &es->compositor->cursor_layer.surface_list;
+ list = &es->compositor->cursor_layer.view_list;
- wl_list_insert(list, &es->layer_link);
- weston_surface_update_transform(es);
+ wl_list_remove(&drag->icon->layer_link);
+ wl_list_insert(list, &drag->icon->layer_link);
+ weston_view_update_transform(drag->icon);
empty_region(&es->pending.input);
}
drag->dx += sx;
drag->dy += sy;
- fx = wl_fixed_to_double(pointer->x) + drag->dx;
- fy = wl_fixed_to_double(pointer->y) + drag->dy;
- weston_surface_configure(es, fx, fy, width, height);
+ /* init to 0 for avoiding a compile warning */
+ fx = fy = 0;
+ if (pointer) {
+ fx = wl_fixed_to_double(pointer->x) + drag->dx;
+ fy = wl_fixed_to_double(pointer->y) + drag->dy;
+ } else if (touch) {
+ fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
+ fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
+ }
+ weston_view_set_position(drag->icon, fx, fy);
+}
+
+static void
+pointer_drag_surface_configure(struct weston_surface *es,
+ int32_t sx, int32_t sy)
+{
+ struct weston_pointer_drag *drag = es->configure_private;
+ struct weston_pointer *pointer = drag->grab.pointer;
+
+ assert(es->configure == pointer_drag_surface_configure);
+
+ drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
+}
+
+static void
+touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
+{
+ struct weston_touch_drag *drag = es->configure_private;
+ struct weston_touch *touch = drag->grab.touch;
+
+ assert(es->configure == touch_drag_surface_configure);
+
+ drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
}
static void
@@ -207,14 +254,20 @@ destroy_drag_focus(struct wl_listener *listener, void *data)
}
static void
-weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface,
- wl_fixed_t sx, wl_fixed_t sy)
+weston_drag_set_focus(struct weston_drag *drag,
+ struct weston_seat *seat,
+ struct weston_view *view,
+ wl_fixed_t sx, wl_fixed_t sy)
{
- struct weston_pointer *pointer = drag->grab.pointer;
struct wl_resource *resource, *offer = NULL;
- struct wl_display *display = pointer->seat->compositor->wl_display;
+ struct wl_display *display = seat->compositor->wl_display;
uint32_t serial;
+ if (drag->focus && view && drag->focus->surface == view->surface) {
+ drag->focus = view;
+ return;
+ }
+
if (drag->focus_resource) {
wl_data_device_send_leave(drag->focus_resource);
wl_list_remove(&drag->focus_listener.link);
@@ -222,15 +275,15 @@ weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface,
drag->focus = NULL;
}
- if (!surface)
+ if (!view || !view->surface->resource)
return;
if (!drag->data_source &&
- wl_resource_get_client(surface->resource) != drag->client)
+ wl_resource_get_client(view->surface->resource) != drag->client)
return;
- resource = wl_resource_find_for_client(&pointer->seat->drag_resource_list,
- wl_resource_get_client(surface->resource));
+ resource = wl_resource_find_for_client(&seat->drag_resource_list,
+ wl_resource_get_client(view->surface->resource));
if (!resource)
return;
@@ -243,10 +296,10 @@ weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface,
return;
}
- wl_data_device_send_enter(resource, serial, surface->resource,
+ wl_data_device_send_enter(resource, serial, view->surface->resource,
sx, sy, offer);
- drag->focus = surface;
+ drag->focus = view;
drag->focus_listener.notify = destroy_drag_focus;
wl_resource_add_destroy_listener(resource, &drag->focus_listener);
drag->focus_resource = resource;
@@ -255,60 +308,71 @@ weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface,
static void
drag_grab_focus(struct weston_pointer_grab *grab)
{
- struct weston_drag *drag =
- container_of(grab, struct weston_drag, grab);
+ struct weston_pointer_drag *drag =
+ container_of(grab, struct weston_pointer_drag, grab);
struct weston_pointer *pointer = grab->pointer;
- struct weston_surface *surface;
+ struct weston_view *view;
wl_fixed_t sx, sy;
- surface = weston_compositor_pick_surface(pointer->seat->compositor,
- pointer->x, pointer->y,
- &sx, &sy);
- if (drag->focus != surface)
- weston_drag_set_focus(drag, surface, sx, sy);
+ view = weston_compositor_pick_view(pointer->seat->compositor,
+ pointer->x, pointer->y,
+ &sx, &sy);
+ if (drag->base.focus != view)
+ weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
}
static void
-drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
+drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
+ wl_fixed_t x, wl_fixed_t y)
{
- struct weston_drag *drag =
- container_of(grab, struct weston_drag, grab);
+ struct weston_pointer_drag *drag =
+ container_of(grab, struct weston_pointer_drag, grab);
struct weston_pointer *pointer = drag->grab.pointer;
float fx, fy;
wl_fixed_t sx, sy;
- if (drag->icon) {
- fx = wl_fixed_to_double(pointer->x) + drag->dx;
- fy = wl_fixed_to_double(pointer->y) + drag->dy;
- weston_surface_set_position(drag->icon, fx, fy);
- weston_surface_schedule_repaint(drag->icon);
+ weston_pointer_move(pointer, x, y);
+
+ if (drag->base.icon) {
+ fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
+ fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
+ weston_view_set_position(drag->base.icon, fx, fy);
+ weston_view_schedule_repaint(drag->base.icon);
}
- if (drag->focus_resource) {
- weston_surface_from_global_fixed(drag->focus,
- pointer->x, pointer->y,
- &sx, &sy);
+ if (drag->base.focus_resource) {
+ weston_view_from_global_fixed(drag->base.focus,
+ pointer->x, pointer->y,
+ &sx, &sy);
- wl_data_device_send_motion(drag->focus_resource, time, sx, sy);
+ wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
}
}
static void
-data_device_end_drag_grab(struct weston_drag *drag)
+data_device_end_drag_grab(struct weston_drag *drag,
+ struct weston_seat *seat)
{
if (drag->icon) {
- if (weston_surface_is_mapped(drag->icon))
- weston_surface_unmap(drag->icon);
+ if (weston_view_is_mapped(drag->icon))
+ weston_view_unmap(drag->icon);
- drag->icon->configure = NULL;
- empty_region(&drag->icon->pending.input);
+ drag->icon->surface->configure = NULL;
+ empty_region(&drag->icon->surface->pending.input);
wl_list_remove(&drag->icon_destroy_listener.link);
+ weston_view_destroy(drag->icon);
}
- weston_drag_set_focus(drag, NULL, 0, 0);
+ weston_drag_set_focus(drag, seat, NULL, 0, 0);
+}
- weston_pointer_end_grab(drag->grab.pointer);
+static void
+data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
+{
+ struct weston_pointer *pointer = drag->grab.pointer;
+ data_device_end_drag_grab(&drag->base, pointer->seat);
+ weston_pointer_end_grab(pointer);
free(drag);
}
@@ -316,37 +380,37 @@ static void
drag_grab_button(struct weston_pointer_grab *grab,
uint32_t time, uint32_t button, uint32_t state_w)
{
- struct weston_drag *drag =
- container_of(grab, struct weston_drag, grab);
+ struct weston_pointer_drag *drag =
+ container_of(grab, struct weston_pointer_drag, grab);
struct weston_pointer *pointer = drag->grab.pointer;
enum wl_pointer_button_state state = state_w;
- if (drag->focus_resource &&
+ if (drag->base.focus_resource &&
pointer->grab_button == button &&
state == WL_POINTER_BUTTON_STATE_RELEASED)
- wl_data_device_send_drop(drag->focus_resource);
+ wl_data_device_send_drop(drag->base.focus_resource);
if (pointer->button_count == 0 &&
state == WL_POINTER_BUTTON_STATE_RELEASED) {
- if (drag->data_source)
- wl_list_remove(&drag->data_source_listener.link);
- data_device_end_drag_grab(drag);
+ if (drag->base.data_source)
+ wl_list_remove(&drag->base.data_source_listener.link);
+ data_device_end_pointer_drag_grab(drag);
}
}
static void
drag_grab_cancel(struct weston_pointer_grab *grab)
{
- struct weston_drag *drag =
- container_of(grab, struct weston_drag, grab);
+ struct weston_pointer_drag *drag =
+ container_of(grab, struct weston_pointer_drag, grab);
- if (drag->data_source)
- wl_list_remove(&drag->data_source_listener.link);
+ if (drag->base.data_source)
+ wl_list_remove(&drag->base.data_source_listener.link);
- data_device_end_drag_grab(drag);
+ data_device_end_pointer_drag_grab(drag);
}
-static const struct weston_pointer_grab_interface drag_grab_interface = {
+static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
drag_grab_focus,
drag_grab_motion,
drag_grab_button,
@@ -354,12 +418,109 @@ static const struct weston_pointer_grab_interface drag_grab_interface = {
};
static void
-destroy_data_device_source(struct wl_listener *listener, void *data)
+drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
+ int touch_id, wl_fixed_t sx, wl_fixed_t sy)
{
- struct weston_drag *drag = container_of(listener, struct weston_drag,
- data_source_listener);
+}
+
+static void
+data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
+{
+ struct weston_touch *touch = drag->grab.touch;
+
+ data_device_end_drag_grab(&drag->base, touch->seat);
+ weston_touch_end_grab(touch);
+ free(drag);
+}
+
+static void
+drag_grab_touch_up(struct weston_touch_grab *grab,
+ uint32_t time, int touch_id)
+{
+ struct weston_touch_drag *touch_drag =
+ container_of(grab, struct weston_touch_drag, grab);
+ struct weston_touch *touch = grab->touch;
+
+ if (touch_id != touch->grab_touch_id)
+ return;
+
+ if (touch_drag->base.focus_resource)
+ wl_data_device_send_drop(touch_drag->base.focus_resource);
+ if (touch_drag->base.data_source)
+ wl_list_remove(&touch_drag->base.data_source_listener.link);
+ data_device_end_touch_drag_grab(touch_drag);
+}
+
+static void
+drag_grab_touch_focus(struct weston_touch_drag *drag)
+{
+ struct weston_touch *touch = drag->grab.touch;
+ struct weston_view *view;
+ wl_fixed_t view_x, view_y;
- data_device_end_drag_grab(drag);
+ view = weston_compositor_pick_view(touch->seat->compositor,
+ touch->grab_x, touch->grab_y,
+ &view_x, &view_y);
+ if (drag->base.focus != view)
+ weston_drag_set_focus(&drag->base, touch->seat,
+ view, view_x, view_y);
+}
+
+static void
+drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
+ int touch_id, wl_fixed_t sx, wl_fixed_t sy)
+{
+ struct weston_touch_drag *touch_drag =
+ container_of(grab, struct weston_touch_drag, grab);
+ struct weston_touch *touch = grab->touch;
+ wl_fixed_t view_x, view_y;
+ float fx, fy;
+
+ if (touch_id != touch->grab_touch_id)
+ return;
+
+ drag_grab_touch_focus(touch_drag);
+ if (touch_drag->base.icon) {
+ fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
+ fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
+ weston_view_set_position(touch_drag->base.icon, fx, fy);
+ weston_view_schedule_repaint(touch_drag->base.icon);
+ }
+
+ if (touch_drag->base.focus_resource) {
+ weston_view_from_global_fixed(touch_drag->base.focus,
+ touch->grab_x, touch->grab_y,
+ &view_x, &view_y);
+ wl_data_device_send_motion(touch_drag->base.focus_resource, time,
+ view_x, view_y);
+ }
+}
+
+static void
+drag_grab_touch_cancel(struct weston_touch_grab *grab)
+{
+ struct weston_touch_drag *touch_drag =
+ container_of(grab, struct weston_touch_drag, grab);
+
+ if (touch_drag->base.data_source)
+ wl_list_remove(&touch_drag->base.data_source_listener.link);
+ data_device_end_touch_drag_grab(touch_drag);
+}
+
+static const struct weston_touch_grab_interface touch_drag_grab_interface = {
+ drag_grab_touch_down,
+ drag_grab_touch_up,
+ drag_grab_touch_motion,
+ drag_grab_touch_cancel
+};
+
+static void
+destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
+{
+ struct weston_pointer_drag *drag = container_of(listener,
+ struct weston_pointer_drag, base.data_source_listener);
+
+ data_device_end_pointer_drag_grab(drag);
}
static void
@@ -372,40 +533,102 @@ handle_drag_icon_destroy(struct wl_listener *listener, void *data)
}
WL_EXPORT int
-weston_seat_start_drag(struct weston_seat *seat,
+weston_pointer_start_drag(struct weston_pointer *pointer,
struct weston_data_source *source,
struct weston_surface *icon,
struct wl_client *client)
{
- struct weston_drag *drag;
+ struct weston_pointer_drag *drag;
drag = zalloc(sizeof *drag);
if (drag == NULL)
return -1;
- drag->grab.interface = &drag_grab_interface;
- drag->client = client;
- drag->data_source = source;
- drag->icon = icon;
+ drag->grab.interface = &pointer_drag_grab_interface;
+ drag->base.client = client;
+ drag->base.data_source = source;
+
+ if (icon) {
+ drag->base.icon = weston_view_create(icon);
+ if (drag->base.icon == NULL) {
+ free(drag);
+ return -1;
+ }
+
+ drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
+ wl_signal_add(&icon->destroy_signal,
+ &drag->base.icon_destroy_listener);
+
+ icon->configure = pointer_drag_surface_configure;
+ icon->configure_private = drag;
+ } else {
+ drag->base.icon = NULL;
+ }
if (source) {
- drag->data_source_listener.notify = destroy_data_device_source;
+ drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
wl_signal_add(&source->destroy_signal,
- &drag->data_source_listener);
+ &drag->base.data_source_listener);
}
+ weston_pointer_set_focus(pointer, NULL,
+ wl_fixed_from_int(0), wl_fixed_from_int(0));
+ weston_pointer_start_grab(pointer, &drag->grab);
+
+ return 0;
+}
+
+static void
+destroy_touch_data_device_source(struct wl_listener *listener, void *data)
+{
+ struct weston_touch_drag *drag = container_of(listener,
+ struct weston_touch_drag, base.data_source_listener);
+
+ data_device_end_touch_drag_grab(drag);
+}
+
+WL_EXPORT int
+weston_touch_start_drag(struct weston_touch *touch,
+ struct weston_data_source *source,
+ struct weston_surface *icon,
+ struct wl_client *client)
+{
+ struct weston_touch_drag *drag;
+
+ drag = zalloc(sizeof *drag);
+ if (drag == NULL)
+ return -1;
+
+ drag->grab.interface = &touch_drag_grab_interface;
+ drag->base.client = client;
+ drag->base.data_source = source;
+
if (icon) {
- drag->icon_destroy_listener.notify = handle_drag_icon_destroy;
+ drag->base.icon = weston_view_create(icon);
+ if (drag->base.icon == NULL) {
+ free(drag);
+ return -1;
+ }
+
+ drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
wl_signal_add(&icon->destroy_signal,
- &drag->icon_destroy_listener);
+ &drag->base.icon_destroy_listener);
- icon->configure = drag_surface_configure;
+ icon->configure = touch_drag_surface_configure;
icon->configure_private = drag;
+ } else {
+ drag->base.icon = NULL;
}
- weston_pointer_set_focus(seat->pointer, NULL,
- wl_fixed_from_int(0), wl_fixed_from_int(0));
- weston_pointer_start_grab(seat->pointer, &drag->grab);
+ if (source) {
+ drag->base.data_source_listener.notify = destroy_touch_data_device_source;
+ wl_signal_add(&source->destroy_signal,
+ &drag->base.data_source_listener);
+ }
+
+ weston_touch_start_grab(touch, &drag->grab);
+
+ drag_grab_touch_focus(drag);
return 0;
}
@@ -419,10 +642,15 @@ data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
struct weston_seat *seat = wl_resource_get_user_data(resource);
struct weston_data_source *source = NULL;
struct weston_surface *icon = NULL;
+ int32_t ret = 0;
- if (seat->pointer->button_count == 0 ||
+ if ((seat->pointer->button_count == 0 ||
seat->pointer->grab_serial != serial ||
- seat->pointer->focus != wl_resource_get_user_data(origin_resource))
+ !seat->pointer->focus ||
+ seat->pointer->focus->surface != wl_resource_get_user_data(origin_resource)) &&
+ (seat->touch->grab_serial != serial ||
+ !seat->touch->focus ||
+ seat->touch->focus->surface != wl_resource_get_user_data(origin_resource)))
return;
/* FIXME: Check that the data source type array isn't empty. */
@@ -438,7 +666,17 @@ data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
return;
}
- if (weston_seat_start_drag(seat, source, icon, client) < 0)
+ if (seat->pointer->button_count == 1 &&
+ seat->pointer->grab_serial == serial &&
+ seat->pointer->focus &&
+ seat->pointer->focus->surface == wl_resource_get_user_data(origin_resource))
+ ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
+ else if (seat->touch->grab_serial != serial ||
+ seat->touch->focus ||
+ seat->touch->focus->surface != wl_resource_get_user_data(origin_resource))
+ ret = weston_touch_start_drag(seat->touch, source, icon, client);
+
+ if (ret < 0)
wl_resource_post_no_memory(resource);
}