Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Daniel Stone | 8e7a8bd | 2013-08-15 01:10:24 +0100 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdio.h> |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 29 | #include <assert.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 30 | |
| 31 | #include "compositor.h" |
| 32 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 33 | struct weston_drag { |
| 34 | struct wl_client *client; |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 35 | struct weston_data_source *data_source; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 36 | struct wl_listener data_source_listener; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 37 | struct weston_view *focus; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 38 | struct wl_resource *focus_resource; |
| 39 | struct wl_listener focus_listener; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 40 | struct weston_view *icon; |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 41 | struct wl_listener icon_destroy_listener; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 42 | int32_t dx, dy; |
| 43 | }; |
| 44 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 45 | struct weston_pointer_drag { |
| 46 | struct weston_drag base; |
| 47 | struct weston_pointer_grab grab; |
| 48 | }; |
| 49 | |
| 50 | struct weston_touch_drag { |
| 51 | struct weston_drag base; |
| 52 | struct weston_touch_grab grab; |
| 53 | }; |
| 54 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 55 | static void |
| 56 | data_offer_accept(struct wl_client *client, struct wl_resource *resource, |
| 57 | uint32_t serial, const char *mime_type) |
| 58 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 59 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 60 | |
| 61 | /* FIXME: Check that client is currently focused by the input |
| 62 | * device that is currently dragging this data source. Should |
| 63 | * this be a wl_data_device request? */ |
| 64 | |
| 65 | if (offer->source) |
| 66 | offer->source->accept(offer->source, serial, mime_type); |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | data_offer_receive(struct wl_client *client, struct wl_resource *resource, |
| 71 | const char *mime_type, int32_t fd) |
| 72 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 73 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 74 | |
| 75 | if (offer->source) |
| 76 | offer->source->send(offer->source, mime_type, fd); |
| 77 | else |
| 78 | close(fd); |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | data_offer_destroy(struct wl_client *client, struct wl_resource *resource) |
| 83 | { |
| 84 | wl_resource_destroy(resource); |
| 85 | } |
| 86 | |
| 87 | static const struct wl_data_offer_interface data_offer_interface = { |
| 88 | data_offer_accept, |
| 89 | data_offer_receive, |
| 90 | data_offer_destroy, |
| 91 | }; |
| 92 | |
| 93 | static void |
| 94 | destroy_data_offer(struct wl_resource *resource) |
| 95 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 96 | struct weston_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 97 | |
| 98 | if (offer->source) |
| 99 | wl_list_remove(&offer->source_destroy_listener.link); |
| 100 | free(offer); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | destroy_offer_data_source(struct wl_listener *listener, void *data) |
| 105 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 106 | struct weston_data_offer *offer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 107 | |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 108 | offer = container_of(listener, struct weston_data_offer, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 109 | source_destroy_listener); |
| 110 | |
| 111 | offer->source = NULL; |
| 112 | } |
| 113 | |
| 114 | static struct wl_resource * |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 115 | weston_data_source_send_offer(struct weston_data_source *source, |
| 116 | struct wl_resource *target) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 117 | { |
Kristian Høgsberg | 5e76a49 | 2013-07-25 16:09:37 -0700 | [diff] [blame] | 118 | struct weston_data_offer *offer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 119 | char **p; |
| 120 | |
| 121 | offer = malloc(sizeof *offer); |
| 122 | if (offer == NULL) |
| 123 | return NULL; |
| 124 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 125 | offer->resource = |
| 126 | wl_resource_create(wl_resource_get_client(target), |
| 127 | &wl_data_offer_interface, 1, 0); |
Kristian Høgsberg | 3c30f0f | 2013-08-06 10:24:04 -0700 | [diff] [blame] | 128 | if (offer->resource == NULL) { |
| 129 | free(offer); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 133 | wl_resource_set_implementation(offer->resource, &data_offer_interface, |
| 134 | offer, destroy_data_offer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 135 | |
| 136 | offer->source = source; |
| 137 | offer->source_destroy_listener.notify = destroy_offer_data_source; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 138 | wl_signal_add(&source->destroy_signal, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 139 | &offer->source_destroy_listener); |
| 140 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 141 | wl_data_device_send_data_offer(target, offer->resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 142 | |
| 143 | wl_array_for_each(p, &source->mime_types) |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 144 | wl_data_offer_send_offer(offer->resource, *p); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 145 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 146 | return offer->resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void |
| 150 | data_source_offer(struct wl_client *client, |
| 151 | struct wl_resource *resource, |
| 152 | const char *type) |
| 153 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 154 | struct weston_data_source *source = |
| 155 | wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 156 | char **p; |
| 157 | |
| 158 | p = wl_array_add(&source->mime_types, sizeof *p); |
| 159 | if (p) |
| 160 | *p = strdup(type); |
| 161 | if (!p || !*p) |
| 162 | wl_resource_post_no_memory(resource); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | data_source_destroy(struct wl_client *client, struct wl_resource *resource) |
| 167 | { |
| 168 | wl_resource_destroy(resource); |
| 169 | } |
| 170 | |
| 171 | static struct wl_data_source_interface data_source_interface = { |
| 172 | data_source_offer, |
| 173 | data_source_destroy |
| 174 | }; |
| 175 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 176 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 177 | drag_surface_configure(struct weston_drag *drag, |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 178 | struct weston_pointer *pointer, |
| 179 | struct weston_touch *touch, |
| 180 | struct weston_surface *es, |
| 181 | int32_t sx, int32_t sy) |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 182 | { |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 183 | struct weston_layer_entry *list; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 184 | float fx, fy; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 185 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 186 | assert((pointer != NULL && touch == NULL) || |
| 187 | (pointer == NULL && touch != NULL)); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 188 | |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 189 | if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 190 | if (pointer && pointer->sprite && |
| 191 | weston_view_is_mapped(pointer->sprite)) |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 192 | list = &pointer->sprite->layer_link; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 193 | else |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 194 | list = &es->compositor->cursor_layer.view_list; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 195 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 196 | weston_layer_entry_remove(&drag->icon->layer_link); |
| 197 | weston_layer_entry_insert(list, &drag->icon->layer_link); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 198 | weston_view_update_transform(drag->icon); |
Jason Ekstrand | ef54008 | 2014-06-26 10:37:36 -0700 | [diff] [blame] | 199 | pixman_region32_clear(&es->pending.input); |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 200 | } |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 201 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 202 | drag->dx += sx; |
| 203 | drag->dy += sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 204 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 205 | /* init to 0 for avoiding a compile warning */ |
| 206 | fx = fy = 0; |
| 207 | if (pointer) { |
| 208 | fx = wl_fixed_to_double(pointer->x) + drag->dx; |
| 209 | fy = wl_fixed_to_double(pointer->y) + drag->dy; |
| 210 | } else if (touch) { |
| 211 | fx = wl_fixed_to_double(touch->grab_x) + drag->dx; |
| 212 | fy = wl_fixed_to_double(touch->grab_y) + drag->dy; |
| 213 | } |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 214 | weston_view_set_position(drag->icon, fx, fy); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 217 | static void |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 218 | pointer_drag_surface_configure(struct weston_surface *es, |
| 219 | int32_t sx, int32_t sy) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 220 | { |
| 221 | struct weston_pointer_drag *drag = es->configure_private; |
| 222 | struct weston_pointer *pointer = drag->grab.pointer; |
| 223 | |
| 224 | assert(es->configure == pointer_drag_surface_configure); |
| 225 | |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 226 | drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 230 | touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 231 | { |
| 232 | struct weston_touch_drag *drag = es->configure_private; |
| 233 | struct weston_touch *touch = drag->grab.touch; |
| 234 | |
| 235 | assert(es->configure == touch_drag_surface_configure); |
| 236 | |
Jonas Ådahl | 767d891 | 2013-12-03 22:30:17 +0100 | [diff] [blame] | 237 | drag_surface_configure(&drag->base, NULL, touch, es, sx, sy); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 241 | destroy_drag_focus(struct wl_listener *listener, void *data) |
| 242 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 243 | struct weston_drag *drag = |
| 244 | container_of(listener, struct weston_drag, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 245 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 246 | drag->focus_resource = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 250 | weston_drag_set_focus(struct weston_drag *drag, |
| 251 | struct weston_seat *seat, |
| 252 | struct weston_view *view, |
| 253 | wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 254 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 255 | struct wl_resource *resource, *offer = NULL; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 256 | struct wl_display *display = seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 257 | uint32_t serial; |
| 258 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 259 | if (drag->focus && view && drag->focus->surface == view->surface) { |
| 260 | drag->focus = view; |
| 261 | return; |
| 262 | } |
| 263 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 264 | if (drag->focus_resource) { |
| 265 | wl_data_device_send_leave(drag->focus_resource); |
| 266 | wl_list_remove(&drag->focus_listener.link); |
| 267 | drag->focus_resource = NULL; |
| 268 | drag->focus = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 271 | if (!view || !view->surface->resource) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 272 | return; |
| 273 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 274 | if (!drag->data_source && |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 275 | wl_resource_get_client(view->surface->resource) != drag->client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 276 | return; |
| 277 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 278 | resource = wl_resource_find_for_client(&seat->drag_resource_list, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 279 | wl_resource_get_client(view->surface->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 280 | if (!resource) |
| 281 | return; |
| 282 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 283 | serial = wl_display_next_serial(display); |
| 284 | |
Kristian Høgsberg | a34e2f2 | 2013-08-20 15:58:25 -0700 | [diff] [blame] | 285 | if (drag->data_source) { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 286 | offer = weston_data_source_send_offer(drag->data_source, |
| 287 | resource); |
Kristian Høgsberg | a34e2f2 | 2013-08-20 15:58:25 -0700 | [diff] [blame] | 288 | if (offer == NULL) |
| 289 | return; |
| 290 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 291 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 292 | wl_data_device_send_enter(resource, serial, view->surface->resource, |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 293 | sx, sy, offer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 294 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 295 | drag->focus = view; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 296 | drag->focus_listener.notify = destroy_drag_focus; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 297 | wl_resource_add_destroy_listener(resource, &drag->focus_listener); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 298 | drag->focus_resource = resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static void |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 302 | drag_grab_focus(struct weston_pointer_grab *grab) |
| 303 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 304 | struct weston_pointer_drag *drag = |
| 305 | container_of(grab, struct weston_pointer_drag, grab); |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 306 | struct weston_pointer *pointer = grab->pointer; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 307 | struct weston_view *view; |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 308 | wl_fixed_t sx, sy; |
| 309 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 310 | view = weston_compositor_pick_view(pointer->seat->compositor, |
| 311 | pointer->x, pointer->y, |
| 312 | &sx, &sy); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 313 | if (drag->base.focus != view) |
| 314 | weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy); |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 318 | drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time, |
| 319 | wl_fixed_t x, wl_fixed_t y) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 320 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 321 | struct weston_pointer_drag *drag = |
| 322 | container_of(grab, struct weston_pointer_drag, grab); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 323 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 324 | float fx, fy; |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 325 | wl_fixed_t sx, sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 326 | |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 327 | weston_pointer_move(pointer, x, y); |
| 328 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 329 | if (drag->base.icon) { |
| 330 | fx = wl_fixed_to_double(pointer->x) + drag->base.dx; |
| 331 | fy = wl_fixed_to_double(pointer->y) + drag->base.dy; |
| 332 | weston_view_set_position(drag->base.icon, fx, fy); |
| 333 | weston_view_schedule_repaint(drag->base.icon); |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 334 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 335 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 336 | if (drag->base.focus_resource) { |
| 337 | weston_view_from_global_fixed(drag->base.focus, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 338 | pointer->x, pointer->y, |
| 339 | &sx, &sy); |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 340 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 341 | wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy); |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 342 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 346 | data_device_end_drag_grab(struct weston_drag *drag, |
| 347 | struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 348 | { |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 349 | if (drag->icon) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 350 | if (weston_view_is_mapped(drag->icon)) |
| 351 | weston_view_unmap(drag->icon); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 352 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 353 | drag->icon->surface->configure = NULL; |
Jason Ekstrand | ef54008 | 2014-06-26 10:37:36 -0700 | [diff] [blame] | 354 | pixman_region32_clear(&drag->icon->surface->pending.input); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 355 | wl_list_remove(&drag->icon_destroy_listener.link); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 356 | weston_view_destroy(drag->icon); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 357 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 358 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 359 | weston_drag_set_focus(drag, seat, NULL, 0, 0); |
| 360 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 361 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 362 | static void |
| 363 | data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag) |
| 364 | { |
| 365 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 366 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 367 | data_device_end_drag_grab(&drag->base, pointer->seat); |
| 368 | weston_pointer_end_grab(pointer); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 369 | free(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 373 | drag_grab_button(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 374 | uint32_t time, uint32_t button, uint32_t state_w) |
| 375 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 376 | struct weston_pointer_drag *drag = |
| 377 | container_of(grab, struct weston_pointer_drag, grab); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 378 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 379 | enum wl_pointer_button_state state = state_w; |
| 380 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 381 | if (drag->base.focus_resource && |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 382 | pointer->grab_button == button && |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 383 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 384 | wl_data_device_send_drop(drag->base.focus_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 385 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 386 | if (pointer->button_count == 0 && |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 387 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 388 | if (drag->base.data_source) |
| 389 | wl_list_remove(&drag->base.data_source_listener.link); |
| 390 | data_device_end_pointer_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 394 | static void |
| 395 | drag_grab_cancel(struct weston_pointer_grab *grab) |
| 396 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 397 | struct weston_pointer_drag *drag = |
| 398 | container_of(grab, struct weston_pointer_drag, grab); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 399 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 400 | if (drag->base.data_source) |
| 401 | wl_list_remove(&drag->base.data_source_listener.link); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 402 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 403 | data_device_end_pointer_drag_grab(drag); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 406 | static const struct weston_pointer_grab_interface pointer_drag_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 407 | drag_grab_focus, |
| 408 | drag_grab_motion, |
| 409 | drag_grab_button, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 410 | drag_grab_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 414 | drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time, |
| 415 | int touch_id, wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 416 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 417 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 418 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 419 | static void |
| 420 | data_device_end_touch_drag_grab(struct weston_touch_drag *drag) |
| 421 | { |
| 422 | struct weston_touch *touch = drag->grab.touch; |
| 423 | |
| 424 | data_device_end_drag_grab(&drag->base, touch->seat); |
| 425 | weston_touch_end_grab(touch); |
| 426 | free(drag); |
| 427 | } |
| 428 | |
| 429 | static void |
| 430 | drag_grab_touch_up(struct weston_touch_grab *grab, |
| 431 | uint32_t time, int touch_id) |
| 432 | { |
| 433 | struct weston_touch_drag *touch_drag = |
| 434 | container_of(grab, struct weston_touch_drag, grab); |
| 435 | struct weston_touch *touch = grab->touch; |
| 436 | |
| 437 | if (touch_id != touch->grab_touch_id) |
| 438 | return; |
| 439 | |
| 440 | if (touch_drag->base.focus_resource) |
| 441 | wl_data_device_send_drop(touch_drag->base.focus_resource); |
| 442 | if (touch_drag->base.data_source) |
| 443 | wl_list_remove(&touch_drag->base.data_source_listener.link); |
| 444 | data_device_end_touch_drag_grab(touch_drag); |
| 445 | } |
| 446 | |
| 447 | static void |
| 448 | drag_grab_touch_focus(struct weston_touch_drag *drag) |
| 449 | { |
| 450 | struct weston_touch *touch = drag->grab.touch; |
| 451 | struct weston_view *view; |
| 452 | wl_fixed_t view_x, view_y; |
| 453 | |
| 454 | view = weston_compositor_pick_view(touch->seat->compositor, |
| 455 | touch->grab_x, touch->grab_y, |
| 456 | &view_x, &view_y); |
| 457 | if (drag->base.focus != view) |
| 458 | weston_drag_set_focus(&drag->base, touch->seat, |
| 459 | view, view_x, view_y); |
| 460 | } |
| 461 | |
| 462 | static void |
| 463 | drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time, |
| 464 | int touch_id, wl_fixed_t sx, wl_fixed_t sy) |
| 465 | { |
| 466 | struct weston_touch_drag *touch_drag = |
| 467 | container_of(grab, struct weston_touch_drag, grab); |
| 468 | struct weston_touch *touch = grab->touch; |
| 469 | wl_fixed_t view_x, view_y; |
| 470 | float fx, fy; |
| 471 | |
| 472 | if (touch_id != touch->grab_touch_id) |
| 473 | return; |
| 474 | |
| 475 | drag_grab_touch_focus(touch_drag); |
| 476 | if (touch_drag->base.icon) { |
| 477 | fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx; |
| 478 | fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy; |
| 479 | weston_view_set_position(touch_drag->base.icon, fx, fy); |
| 480 | weston_view_schedule_repaint(touch_drag->base.icon); |
| 481 | } |
| 482 | |
| 483 | if (touch_drag->base.focus_resource) { |
| 484 | weston_view_from_global_fixed(touch_drag->base.focus, |
| 485 | touch->grab_x, touch->grab_y, |
| 486 | &view_x, &view_y); |
| 487 | wl_data_device_send_motion(touch_drag->base.focus_resource, time, |
| 488 | view_x, view_y); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static void |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 493 | drag_grab_touch_frame(struct weston_touch_grab *grab) |
| 494 | { |
| 495 | } |
| 496 | |
| 497 | static void |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 498 | drag_grab_touch_cancel(struct weston_touch_grab *grab) |
| 499 | { |
| 500 | struct weston_touch_drag *touch_drag = |
| 501 | container_of(grab, struct weston_touch_drag, grab); |
| 502 | |
| 503 | if (touch_drag->base.data_source) |
| 504 | wl_list_remove(&touch_drag->base.data_source_listener.link); |
| 505 | data_device_end_touch_drag_grab(touch_drag); |
| 506 | } |
| 507 | |
| 508 | static const struct weston_touch_grab_interface touch_drag_grab_interface = { |
| 509 | drag_grab_touch_down, |
| 510 | drag_grab_touch_up, |
| 511 | drag_grab_touch_motion, |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 512 | drag_grab_touch_frame, |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 513 | drag_grab_touch_cancel |
| 514 | }; |
| 515 | |
| 516 | static void |
| 517 | destroy_pointer_data_device_source(struct wl_listener *listener, void *data) |
| 518 | { |
| 519 | struct weston_pointer_drag *drag = container_of(listener, |
| 520 | struct weston_pointer_drag, base.data_source_listener); |
| 521 | |
| 522 | data_device_end_pointer_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static void |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 526 | handle_drag_icon_destroy(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 527 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 528 | struct weston_drag *drag = container_of(listener, struct weston_drag, |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 529 | icon_destroy_listener); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 530 | |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 531 | drag->icon = NULL; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 534 | WL_EXPORT int |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 535 | weston_pointer_start_drag(struct weston_pointer *pointer, |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 536 | struct weston_data_source *source, |
| 537 | struct weston_surface *icon, |
| 538 | struct wl_client *client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 539 | { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 540 | struct weston_pointer_drag *drag; |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 541 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 542 | drag = zalloc(sizeof *drag); |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 543 | if (drag == NULL) |
| 544 | return -1; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 545 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 546 | drag->grab.interface = &pointer_drag_grab_interface; |
| 547 | drag->base.client = client; |
| 548 | drag->base.data_source = source; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 549 | |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 550 | if (icon) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 551 | drag->base.icon = weston_view_create(icon); |
| 552 | if (drag->base.icon == NULL) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 553 | free(drag); |
| 554 | return -1; |
| 555 | } |
| 556 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 557 | drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 558 | wl_signal_add(&icon->destroy_signal, |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 559 | &drag->base.icon_destroy_listener); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 560 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 561 | icon->configure = pointer_drag_surface_configure; |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 562 | icon->configure_private = drag; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 563 | } else { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 564 | drag->base.icon = NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | if (source) { |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 568 | drag->base.data_source_listener.notify = destroy_pointer_data_device_source; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 569 | wl_signal_add(&source->destroy_signal, |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 570 | &drag->base.data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 571 | } |
| 572 | |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 573 | weston_pointer_set_focus(pointer, NULL, |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 574 | wl_fixed_from_int(0), wl_fixed_from_int(0)); |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 575 | weston_pointer_start_grab(pointer, &drag->grab); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | destroy_touch_data_device_source(struct wl_listener *listener, void *data) |
| 582 | { |
| 583 | struct weston_touch_drag *drag = container_of(listener, |
| 584 | struct weston_touch_drag, base.data_source_listener); |
| 585 | |
| 586 | data_device_end_touch_drag_grab(drag); |
| 587 | } |
| 588 | |
| 589 | WL_EXPORT int |
| 590 | weston_touch_start_drag(struct weston_touch *touch, |
| 591 | struct weston_data_source *source, |
| 592 | struct weston_surface *icon, |
| 593 | struct wl_client *client) |
| 594 | { |
| 595 | struct weston_touch_drag *drag; |
| 596 | |
| 597 | drag = zalloc(sizeof *drag); |
| 598 | if (drag == NULL) |
| 599 | return -1; |
| 600 | |
| 601 | drag->grab.interface = &touch_drag_grab_interface; |
| 602 | drag->base.client = client; |
| 603 | drag->base.data_source = source; |
| 604 | |
| 605 | if (icon) { |
| 606 | drag->base.icon = weston_view_create(icon); |
| 607 | if (drag->base.icon == NULL) { |
| 608 | free(drag); |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy; |
| 613 | wl_signal_add(&icon->destroy_signal, |
| 614 | &drag->base.icon_destroy_listener); |
| 615 | |
| 616 | icon->configure = touch_drag_surface_configure; |
| 617 | icon->configure_private = drag; |
| 618 | } else { |
| 619 | drag->base.icon = NULL; |
| 620 | } |
| 621 | |
| 622 | if (source) { |
| 623 | drag->base.data_source_listener.notify = destroy_touch_data_device_source; |
| 624 | wl_signal_add(&source->destroy_signal, |
| 625 | &drag->base.data_source_listener); |
| 626 | } |
| 627 | |
| 628 | weston_touch_start_grab(touch, &drag->grab); |
| 629 | |
| 630 | drag_grab_touch_focus(drag); |
Kristian Høgsberg | 0abad07 | 2013-09-11 09:42:26 -0700 | [diff] [blame] | 631 | |
| 632 | return 0; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static void |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 636 | data_device_start_drag(struct wl_client *client, struct wl_resource *resource, |
| 637 | struct wl_resource *source_resource, |
| 638 | struct wl_resource *origin_resource, |
| 639 | struct wl_resource *icon_resource, uint32_t serial) |
| 640 | { |
| 641 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 642 | struct weston_surface *origin = wl_resource_get_user_data(origin_resource); |
Kristian Høgsberg | 1702d4c | 2013-09-11 09:45:03 -0700 | [diff] [blame] | 643 | struct weston_data_source *source = NULL; |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 644 | struct weston_surface *icon = NULL; |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 645 | int is_pointer_grab, is_touch_grab; |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 646 | int32_t ret = 0; |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 647 | |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 648 | is_pointer_grab = seat->pointer && |
| 649 | seat->pointer->button_count == 1 && |
| 650 | seat->pointer->grab_serial == serial && |
| 651 | seat->pointer->focus && |
| 652 | seat->pointer->focus->surface == origin; |
| 653 | |
| 654 | is_touch_grab = seat->touch && |
| 655 | seat->touch->num_tp == 1 && |
| 656 | seat->touch->grab_serial == serial && |
| 657 | seat->touch->focus && |
| 658 | seat->touch->focus->surface == origin; |
| 659 | |
| 660 | if (!is_pointer_grab && !is_touch_grab) |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 661 | return; |
| 662 | |
| 663 | /* FIXME: Check that the data source type array isn't empty. */ |
| 664 | |
| 665 | if (source_resource) |
| 666 | source = wl_resource_get_user_data(source_resource); |
| 667 | if (icon_resource) |
| 668 | icon = wl_resource_get_user_data(icon_resource); |
Pekka Paalanen | 50b6747 | 2014-10-01 15:02:41 +0300 | [diff] [blame^] | 669 | |
| 670 | if (icon) { |
| 671 | if (weston_surface_set_role(icon, "wl_data_device-icon", |
| 672 | resource, |
| 673 | WL_DATA_DEVICE_ERROR_ROLE) < 0) |
| 674 | return; |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 677 | if (is_pointer_grab) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 678 | ret = weston_pointer_start_drag(seat->pointer, source, icon, client); |
Jason Ekstrand | 8202d72 | 2014-06-24 21:19:24 -0700 | [diff] [blame] | 679 | else if (is_touch_grab) |
Xiong Zhang | fd51e7b | 2013-11-25 18:42:49 +0800 | [diff] [blame] | 680 | ret = weston_touch_start_drag(seat->touch, source, icon, client); |
| 681 | |
| 682 | if (ret < 0) |
Kristian Høgsberg | 85de9c2 | 2013-09-04 20:44:26 -0700 | [diff] [blame] | 683 | wl_resource_post_no_memory(resource); |
| 684 | } |
| 685 | |
| 686 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 687 | destroy_selection_data_source(struct wl_listener *listener, void *data) |
| 688 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 689 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 690 | selection_data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 691 | struct wl_resource *data_device; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 692 | struct weston_surface *focus = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 693 | |
| 694 | seat->selection_data_source = NULL; |
| 695 | |
| 696 | if (seat->keyboard) |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 697 | focus = seat->keyboard->focus; |
| 698 | if (focus && focus->resource) { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 699 | data_device = wl_resource_find_for_client(&seat->drag_resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 700 | wl_resource_get_client(focus->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 701 | if (data_device) |
| 702 | wl_data_device_send_selection(data_device, NULL); |
| 703 | } |
| 704 | |
| 705 | wl_signal_emit(&seat->selection_signal, seat); |
| 706 | } |
| 707 | |
| 708 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 709 | weston_seat_set_selection(struct weston_seat *seat, |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 710 | struct weston_data_source *source, uint32_t serial) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 711 | { |
| 712 | struct wl_resource *data_device, *offer; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 713 | struct weston_surface *focus = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 714 | |
| 715 | if (seat->selection_data_source && |
| 716 | seat->selection_serial - serial < UINT32_MAX / 2) |
| 717 | return; |
| 718 | |
| 719 | if (seat->selection_data_source) { |
| 720 | seat->selection_data_source->cancel(seat->selection_data_source); |
| 721 | wl_list_remove(&seat->selection_data_source_listener.link); |
| 722 | seat->selection_data_source = NULL; |
| 723 | } |
| 724 | |
| 725 | seat->selection_data_source = source; |
| 726 | seat->selection_serial = serial; |
| 727 | |
| 728 | if (seat->keyboard) |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 729 | focus = seat->keyboard->focus; |
| 730 | if (focus && focus->resource) { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 731 | data_device = wl_resource_find_for_client(&seat->drag_resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 732 | wl_resource_get_client(focus->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 733 | if (data_device && source) { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 734 | offer = weston_data_source_send_offer(seat->selection_data_source, |
| 735 | data_device); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 736 | wl_data_device_send_selection(data_device, offer); |
| 737 | } else if (data_device) { |
| 738 | wl_data_device_send_selection(data_device, NULL); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | wl_signal_emit(&seat->selection_signal, seat); |
| 743 | |
| 744 | if (source) { |
| 745 | seat->selection_data_source_listener.notify = |
| 746 | destroy_selection_data_source; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 747 | wl_signal_add(&source->destroy_signal, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 748 | &seat->selection_data_source_listener); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | static void |
| 753 | data_device_set_selection(struct wl_client *client, |
| 754 | struct wl_resource *resource, |
| 755 | struct wl_resource *source_resource, uint32_t serial) |
| 756 | { |
| 757 | if (!source_resource) |
| 758 | return; |
| 759 | |
| 760 | /* FIXME: Store serial and check against incoming serial here. */ |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 761 | weston_seat_set_selection(wl_resource_get_user_data(resource), |
| 762 | wl_resource_get_user_data(source_resource), |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 763 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static const struct wl_data_device_interface data_device_interface = { |
| 767 | data_device_start_drag, |
| 768 | data_device_set_selection, |
| 769 | }; |
| 770 | |
| 771 | static void |
| 772 | destroy_data_source(struct wl_resource *resource) |
| 773 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 774 | struct weston_data_source *source = |
| 775 | wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 776 | char **p; |
| 777 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 778 | wl_signal_emit(&source->destroy_signal, source); |
| 779 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 780 | wl_array_for_each(p, &source->mime_types) |
| 781 | free(*p); |
| 782 | |
| 783 | wl_array_release(&source->mime_types); |
| 784 | |
Kristian Høgsberg | 489b279 | 2013-06-25 11:26:31 -0400 | [diff] [blame] | 785 | free(source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 789 | client_source_accept(struct weston_data_source *source, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 790 | uint32_t time, const char *mime_type) |
| 791 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 792 | wl_data_source_send_target(source->resource, mime_type); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 796 | client_source_send(struct weston_data_source *source, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 797 | const char *mime_type, int32_t fd) |
| 798 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 799 | wl_data_source_send_send(source->resource, mime_type, fd); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 800 | close(fd); |
| 801 | } |
| 802 | |
| 803 | static void |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 804 | client_source_cancel(struct weston_data_source *source) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 805 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 806 | wl_data_source_send_cancelled(source->resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static void |
| 810 | create_data_source(struct wl_client *client, |
| 811 | struct wl_resource *resource, uint32_t id) |
| 812 | { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 813 | struct weston_data_source *source; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 814 | |
| 815 | source = malloc(sizeof *source); |
| 816 | if (source == NULL) { |
| 817 | wl_resource_post_no_memory(resource); |
| 818 | return; |
| 819 | } |
| 820 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 821 | wl_signal_init(&source->destroy_signal); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 822 | source->accept = client_source_accept; |
| 823 | source->send = client_source_send; |
| 824 | source->cancel = client_source_cancel; |
| 825 | |
| 826 | wl_array_init(&source->mime_types); |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 827 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 828 | source->resource = |
| 829 | wl_resource_create(client, &wl_data_source_interface, 1, id); |
| 830 | wl_resource_set_implementation(source->resource, &data_source_interface, |
| 831 | source, destroy_data_source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static void unbind_data_device(struct wl_resource *resource) |
| 835 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 836 | wl_list_remove(wl_resource_get_link(resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static void |
| 840 | get_data_device(struct wl_client *client, |
| 841 | struct wl_resource *manager_resource, |
| 842 | uint32_t id, struct wl_resource *seat_resource) |
| 843 | { |
Jason Ekstrand | a0d2dde | 2013-06-14 10:08:01 -0500 | [diff] [blame] | 844 | struct weston_seat *seat = wl_resource_get_user_data(seat_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 845 | struct wl_resource *resource; |
| 846 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 847 | resource = wl_resource_create(client, |
| 848 | &wl_data_device_interface, 1, id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 849 | if (resource == NULL) { |
| 850 | wl_resource_post_no_memory(manager_resource); |
| 851 | return; |
| 852 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 853 | |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 854 | wl_list_insert(&seat->drag_resource_list, |
| 855 | wl_resource_get_link(resource)); |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 856 | wl_resource_set_implementation(resource, &data_device_interface, |
| 857 | seat, unbind_data_device); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | static const struct wl_data_device_manager_interface manager_interface = { |
| 861 | create_data_source, |
| 862 | get_data_device |
| 863 | }; |
| 864 | |
| 865 | static void |
| 866 | bind_manager(struct wl_client *client, |
| 867 | void *data, uint32_t version, uint32_t id) |
| 868 | { |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 869 | struct wl_resource *resource; |
| 870 | |
| 871 | resource = |
| 872 | wl_resource_create(client, |
| 873 | &wl_data_device_manager_interface, 1, id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 874 | if (resource == NULL) { |
| 875 | wl_client_post_no_memory(client); |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | wl_resource_set_implementation(resource, &manager_interface, |
| 880 | NULL, NULL); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 884 | wl_data_device_set_keyboard_focus(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 885 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 886 | struct wl_resource *data_device, *offer; |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 887 | struct weston_data_source *source; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 888 | struct weston_surface *focus; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 889 | |
| 890 | if (!seat->keyboard) |
| 891 | return; |
| 892 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 893 | focus = seat->keyboard->focus; |
| 894 | if (!focus || !focus->resource) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 895 | return; |
| 896 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 897 | data_device = wl_resource_find_for_client(&seat->drag_resource_list, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 898 | wl_resource_get_client(focus->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 899 | if (!data_device) |
| 900 | return; |
| 901 | |
| 902 | source = seat->selection_data_source; |
| 903 | if (source) { |
Kristian Høgsberg | 7ff3bdb | 2013-07-25 15:52:14 -0700 | [diff] [blame] | 904 | offer = weston_data_source_send_offer(source, data_device); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 905 | wl_data_device_send_selection(data_device, offer); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | WL_EXPORT int |
| 910 | wl_data_device_manager_init(struct wl_display *display) |
| 911 | { |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 912 | if (wl_global_create(display, |
| 913 | &wl_data_device_manager_interface, 1, |
| 914 | NULL, bind_manager) == NULL) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 915 | return -1; |
| 916 | |
| 917 | return 0; |
| 918 | } |