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