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 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "compositor.h" |
| 29 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 30 | struct weston_drag { |
| 31 | struct wl_client *client; |
| 32 | struct wl_data_source *data_source; |
| 33 | struct wl_listener data_source_listener; |
| 34 | struct weston_surface *focus; |
| 35 | struct wl_resource *focus_resource; |
| 36 | struct wl_listener focus_listener; |
| 37 | struct weston_pointer_grab grab; |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 38 | struct weston_surface *icon; |
| 39 | struct wl_listener icon_destroy_listener; |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 40 | int32_t dx, dy; |
| 41 | }; |
| 42 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 43 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 44 | empty_region(pixman_region32_t *region) |
| 45 | { |
| 46 | pixman_region32_fini(region); |
| 47 | pixman_region32_init(region); |
| 48 | } |
| 49 | |
| 50 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 51 | data_offer_accept(struct wl_client *client, struct wl_resource *resource, |
| 52 | uint32_t serial, const char *mime_type) |
| 53 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 54 | struct wl_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 55 | |
| 56 | /* FIXME: Check that client is currently focused by the input |
| 57 | * device that is currently dragging this data source. Should |
| 58 | * this be a wl_data_device request? */ |
| 59 | |
| 60 | if (offer->source) |
| 61 | offer->source->accept(offer->source, serial, mime_type); |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | data_offer_receive(struct wl_client *client, struct wl_resource *resource, |
| 66 | const char *mime_type, int32_t fd) |
| 67 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 68 | struct wl_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 69 | |
| 70 | if (offer->source) |
| 71 | offer->source->send(offer->source, mime_type, fd); |
| 72 | else |
| 73 | close(fd); |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | data_offer_destroy(struct wl_client *client, struct wl_resource *resource) |
| 78 | { |
| 79 | wl_resource_destroy(resource); |
| 80 | } |
| 81 | |
| 82 | static const struct wl_data_offer_interface data_offer_interface = { |
| 83 | data_offer_accept, |
| 84 | data_offer_receive, |
| 85 | data_offer_destroy, |
| 86 | }; |
| 87 | |
| 88 | static void |
| 89 | destroy_data_offer(struct wl_resource *resource) |
| 90 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 91 | struct wl_data_offer *offer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 92 | |
| 93 | if (offer->source) |
| 94 | wl_list_remove(&offer->source_destroy_listener.link); |
| 95 | free(offer); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | destroy_offer_data_source(struct wl_listener *listener, void *data) |
| 100 | { |
| 101 | struct wl_data_offer *offer; |
| 102 | |
| 103 | offer = container_of(listener, struct wl_data_offer, |
| 104 | source_destroy_listener); |
| 105 | |
| 106 | offer->source = NULL; |
| 107 | } |
| 108 | |
| 109 | static struct wl_resource * |
| 110 | wl_data_source_send_offer(struct wl_data_source *source, |
| 111 | struct wl_resource *target) |
| 112 | { |
| 113 | struct wl_data_offer *offer; |
| 114 | char **p; |
| 115 | |
| 116 | offer = malloc(sizeof *offer); |
| 117 | if (offer == NULL) |
| 118 | return NULL; |
| 119 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 120 | offer->resource = wl_client_new_object(wl_resource_get_client(target), |
| 121 | &wl_data_offer_interface, |
| 122 | &data_offer_interface, offer); |
| 123 | wl_resource_set_destructor(offer->resource, destroy_data_offer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 124 | |
| 125 | offer->source = source; |
| 126 | offer->source_destroy_listener.notify = destroy_offer_data_source; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 127 | wl_signal_add(&source->destroy_signal, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 128 | &offer->source_destroy_listener); |
| 129 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 130 | wl_data_device_send_data_offer(target, offer->resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 131 | |
| 132 | wl_array_for_each(p, &source->mime_types) |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 133 | wl_data_offer_send_offer(offer->resource, *p); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 134 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 135 | return offer->resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void |
| 139 | data_source_offer(struct wl_client *client, |
| 140 | struct wl_resource *resource, |
| 141 | const char *type) |
| 142 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 143 | struct wl_data_source *source = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 144 | char **p; |
| 145 | |
| 146 | p = wl_array_add(&source->mime_types, sizeof *p); |
| 147 | if (p) |
| 148 | *p = strdup(type); |
| 149 | if (!p || !*p) |
| 150 | wl_resource_post_no_memory(resource); |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | data_source_destroy(struct wl_client *client, struct wl_resource *resource) |
| 155 | { |
| 156 | wl_resource_destroy(resource); |
| 157 | } |
| 158 | |
| 159 | static struct wl_data_source_interface data_source_interface = { |
| 160 | data_source_offer, |
| 161 | data_source_destroy |
| 162 | }; |
| 163 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 164 | static void |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 165 | drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height) |
| 166 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 167 | struct weston_drag *drag = es->configure_private; |
| 168 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 169 | struct wl_list *list; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 170 | float fx, fy; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 171 | |
| 172 | if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) { |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 173 | if (pointer->sprite && weston_surface_is_mapped(pointer->sprite)) |
| 174 | list = &pointer->sprite->layer_link; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 175 | else |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 176 | list = &es->compositor->cursor_layer.surface_list; |
Kristian Høgsberg | 415f30c | 2013-05-07 22:42:28 -0400 | [diff] [blame] | 177 | |
| 178 | wl_list_insert(list, &es->layer_link); |
| 179 | weston_surface_update_transform(es); |
| 180 | empty_region(&es->pending.input); |
| 181 | } |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 182 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 183 | drag->dx += sx; |
| 184 | drag->dy += sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 185 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 186 | fx = wl_fixed_to_double(pointer->x) + drag->dx; |
| 187 | fy = wl_fixed_to_double(pointer->y) + drag->dy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 188 | weston_surface_configure(es, fx, fy, width, height); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 191 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 192 | destroy_drag_focus(struct wl_listener *listener, void *data) |
| 193 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 194 | struct weston_drag *drag = |
| 195 | container_of(listener, struct weston_drag, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 196 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 197 | drag->focus_resource = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 201 | weston_drag_set_focus(struct weston_drag *drag, struct weston_surface *surface, |
| 202 | wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 203 | { |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 204 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 205 | struct wl_resource *resource, *offer = NULL; |
| 206 | struct wl_display *display; |
| 207 | uint32_t serial; |
| 208 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 209 | if (drag->focus_resource) { |
| 210 | wl_data_device_send_leave(drag->focus_resource); |
| 211 | wl_list_remove(&drag->focus_listener.link); |
| 212 | drag->focus_resource = NULL; |
| 213 | drag->focus = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | if (!surface) |
| 217 | return; |
| 218 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 219 | if (!drag->data_source && |
| 220 | wl_resource_get_client(surface->resource) != drag->client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 221 | return; |
| 222 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 223 | resource = wl_resource_find_for_client(&pointer->seat->drag_resource_list, |
| 224 | wl_resource_get_client(surface->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 225 | if (!resource) |
| 226 | return; |
| 227 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 228 | display = wl_client_get_display(wl_resource_get_client(resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 229 | serial = wl_display_next_serial(display); |
| 230 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 231 | if (drag->data_source) |
| 232 | offer = wl_data_source_send_offer(drag->data_source, resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 233 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 234 | wl_data_device_send_enter(resource, serial, surface->resource, |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 235 | sx, sy, offer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 236 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 237 | drag->focus = surface; |
| 238 | drag->focus_listener.notify = destroy_drag_focus; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 239 | wl_resource_add_destroy_listener(resource, &drag->focus_listener); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 240 | drag->focus_resource = resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 244 | drag_grab_focus(struct weston_pointer_grab *grab) |
| 245 | { |
| 246 | struct weston_drag *drag = |
| 247 | container_of(grab, struct weston_drag, grab); |
| 248 | struct weston_pointer *pointer = grab->pointer; |
| 249 | struct weston_surface *surface; |
| 250 | wl_fixed_t sx, sy; |
| 251 | |
| 252 | surface = weston_compositor_pick_surface(pointer->seat->compositor, |
| 253 | pointer->x, pointer->y, |
| 254 | &sx, &sy); |
| 255 | if (drag->focus != surface) |
| 256 | weston_drag_set_focus(drag, surface, sx, sy); |
| 257 | } |
| 258 | |
| 259 | static void |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 260 | drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 261 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 262 | struct weston_drag *drag = |
| 263 | container_of(grab, struct weston_drag, grab); |
| 264 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 265 | float fx, fy; |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 266 | wl_fixed_t sx, sy; |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 267 | |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 268 | if (drag->icon) { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 269 | fx = wl_fixed_to_double(pointer->x) + drag->dx; |
| 270 | fy = wl_fixed_to_double(pointer->y) + drag->dy; |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 271 | weston_surface_set_position(drag->icon, fx, fy); |
| 272 | weston_surface_schedule_repaint(drag->icon); |
Kristian Høgsberg | aad8099 | 2013-05-07 22:53:43 -0400 | [diff] [blame] | 273 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 274 | |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 275 | if (drag->focus_resource) { |
| 276 | weston_surface_from_global_fixed(drag->focus, |
| 277 | pointer->x, pointer->y, |
| 278 | &sx, &sy); |
| 279 | |
| 280 | wl_data_device_send_motion(drag->focus_resource, time, sx, sy); |
| 281 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 285 | data_device_end_drag_grab(struct weston_drag *drag) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 286 | { |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 287 | if (drag->icon) { |
| 288 | if (weston_surface_is_mapped(drag->icon)) |
| 289 | weston_surface_unmap(drag->icon); |
| 290 | |
| 291 | drag->icon->configure = NULL; |
| 292 | empty_region(&drag->icon->pending.input); |
| 293 | wl_list_remove(&drag->icon_destroy_listener.link); |
| 294 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 295 | |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 296 | weston_drag_set_focus(drag, NULL, 0, 0); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 297 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 298 | weston_pointer_end_grab(drag->grab.pointer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 299 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 300 | free(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 304 | drag_grab_button(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 305 | uint32_t time, uint32_t button, uint32_t state_w) |
| 306 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 307 | struct weston_drag *drag = |
| 308 | container_of(grab, struct weston_drag, grab); |
| 309 | struct weston_pointer *pointer = drag->grab.pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 310 | enum wl_pointer_button_state state = state_w; |
| 311 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 312 | if (drag->focus_resource && |
| 313 | pointer->grab_button == button && |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 314 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 315 | wl_data_device_send_drop(drag->focus_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 316 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 317 | if (pointer->button_count == 0 && |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 318 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 319 | if (drag->data_source) |
| 320 | wl_list_remove(&drag->data_source_listener.link); |
| 321 | data_device_end_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 325 | static const struct weston_pointer_grab_interface drag_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 326 | drag_grab_focus, |
| 327 | drag_grab_motion, |
| 328 | drag_grab_button, |
| 329 | }; |
| 330 | |
| 331 | static void |
| 332 | destroy_data_device_source(struct wl_listener *listener, void *data) |
| 333 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 334 | struct weston_drag *drag = container_of(listener, struct weston_drag, |
| 335 | data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 336 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 337 | data_device_end_drag_grab(drag); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static void |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 341 | handle_drag_icon_destroy(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 342 | { |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 343 | struct weston_drag *drag = container_of(listener, struct weston_drag, |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 344 | icon_destroy_listener); |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 345 | |
Kristian Høgsberg | c43aad1 | 2013-05-08 15:30:42 -0400 | [diff] [blame] | 346 | drag->icon = NULL; |
Kristian Høgsberg | 7848bb6 | 2013-05-07 11:18:46 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static void |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 350 | data_device_start_drag(struct wl_client *client, struct wl_resource *resource, |
| 351 | struct wl_resource *source_resource, |
| 352 | struct wl_resource *origin_resource, |
| 353 | struct wl_resource *icon_resource, uint32_t serial) |
| 354 | { |
Jason Ekstrand | a0d2dde | 2013-06-14 10:08:01 -0500 | [diff] [blame] | 355 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
| 356 | struct weston_drag *drag = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 357 | struct weston_surface *icon = NULL; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 358 | |
Kristian Høgsberg | dba2586 | 2013-05-08 15:53:42 -0400 | [diff] [blame] | 359 | if (seat->pointer->button_count == 0 || |
| 360 | seat->pointer->grab_serial != serial || |
Jason Ekstrand | 0f2ef7e | 2013-06-14 10:07:53 -0500 | [diff] [blame] | 361 | seat->pointer->focus != wl_resource_get_user_data(origin_resource)) |
Kristian Høgsberg | dba2586 | 2013-05-08 15:53:42 -0400 | [diff] [blame] | 362 | return; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 363 | |
| 364 | /* FIXME: Check that the data source type array isn't empty. */ |
| 365 | |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 366 | if (icon_resource) |
Jason Ekstrand | 0f2ef7e | 2013-06-14 10:07:53 -0500 | [diff] [blame] | 367 | icon = wl_resource_get_user_data(icon_resource); |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 368 | if (icon && icon->configure) { |
| 369 | wl_resource_post_error(icon_resource, |
| 370 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 371 | "surface->configure already set"); |
| 372 | return; |
| 373 | } |
| 374 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 375 | drag = malloc(sizeof *drag); |
| 376 | if (drag == NULL) { |
| 377 | wl_resource_post_no_memory(resource); |
| 378 | return; |
| 379 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 380 | |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 381 | memset(drag, 0, sizeof *drag); |
| 382 | drag->grab.interface = &drag_grab_interface; |
| 383 | drag->client = client; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 384 | |
| 385 | if (source_resource) { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 386 | drag->data_source = wl_resource_get_user_data(source_resource); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 387 | drag->data_source_listener.notify = destroy_data_device_source; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 388 | wl_resource_add_destroy_listener(source_resource, |
| 389 | &drag->data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 392 | if (icon) { |
| 393 | drag->icon = icon; |
| 394 | drag->icon_destroy_listener.notify = handle_drag_icon_destroy; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 395 | wl_signal_add(&icon->destroy_signal, |
Kristian Høgsberg | 5a9fb35 | 2013-05-08 15:47:52 -0400 | [diff] [blame] | 396 | &drag->icon_destroy_listener); |
| 397 | |
| 398 | icon->configure = drag_surface_configure; |
| 399 | icon->configure_private = drag; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 402 | weston_pointer_set_focus(seat->pointer, NULL, |
| 403 | wl_fixed_from_int(0), wl_fixed_from_int(0)); |
Kristian Høgsberg | 054c50a | 2013-05-08 15:27:47 -0400 | [diff] [blame] | 404 | weston_pointer_start_grab(seat->pointer, &drag->grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static void |
| 408 | destroy_selection_data_source(struct wl_listener *listener, void *data) |
| 409 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 410 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 411 | selection_data_source_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 412 | struct wl_resource *data_device; |
| 413 | struct wl_resource *focus = NULL; |
| 414 | |
| 415 | seat->selection_data_source = NULL; |
| 416 | |
| 417 | if (seat->keyboard) |
| 418 | focus = seat->keyboard->focus_resource; |
| 419 | if (focus) { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 420 | data_device = wl_resource_find_for_client(&seat->drag_resource_list, |
| 421 | wl_resource_get_client(focus)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 422 | if (data_device) |
| 423 | wl_data_device_send_selection(data_device, NULL); |
| 424 | } |
| 425 | |
| 426 | wl_signal_emit(&seat->selection_signal, seat); |
| 427 | } |
| 428 | |
| 429 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 430 | weston_seat_set_selection(struct weston_seat *seat, |
| 431 | struct wl_data_source *source, uint32_t serial) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 432 | { |
| 433 | struct wl_resource *data_device, *offer; |
| 434 | struct wl_resource *focus = NULL; |
| 435 | |
| 436 | if (seat->selection_data_source && |
| 437 | seat->selection_serial - serial < UINT32_MAX / 2) |
| 438 | return; |
| 439 | |
| 440 | if (seat->selection_data_source) { |
| 441 | seat->selection_data_source->cancel(seat->selection_data_source); |
| 442 | wl_list_remove(&seat->selection_data_source_listener.link); |
| 443 | seat->selection_data_source = NULL; |
| 444 | } |
| 445 | |
| 446 | seat->selection_data_source = source; |
| 447 | seat->selection_serial = serial; |
| 448 | |
| 449 | if (seat->keyboard) |
| 450 | focus = seat->keyboard->focus_resource; |
| 451 | if (focus) { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 452 | data_device = wl_resource_find_for_client(&seat->drag_resource_list, |
| 453 | wl_resource_get_client(focus)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 454 | if (data_device && source) { |
| 455 | offer = wl_data_source_send_offer(seat->selection_data_source, |
| 456 | data_device); |
| 457 | wl_data_device_send_selection(data_device, offer); |
| 458 | } else if (data_device) { |
| 459 | wl_data_device_send_selection(data_device, NULL); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | wl_signal_emit(&seat->selection_signal, seat); |
| 464 | |
| 465 | if (source) { |
| 466 | seat->selection_data_source_listener.notify = |
| 467 | destroy_selection_data_source; |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 468 | wl_signal_add(&source->destroy_signal, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 469 | &seat->selection_data_source_listener); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static void |
| 474 | data_device_set_selection(struct wl_client *client, |
| 475 | struct wl_resource *resource, |
| 476 | struct wl_resource *source_resource, uint32_t serial) |
| 477 | { |
| 478 | if (!source_resource) |
| 479 | return; |
| 480 | |
| 481 | /* FIXME: Store serial and check against incoming serial here. */ |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 482 | weston_seat_set_selection(wl_resource_get_user_data(resource), |
| 483 | wl_resource_get_user_data(source_resource), |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 484 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static const struct wl_data_device_interface data_device_interface = { |
| 488 | data_device_start_drag, |
| 489 | data_device_set_selection, |
| 490 | }; |
| 491 | |
| 492 | static void |
| 493 | destroy_data_source(struct wl_resource *resource) |
| 494 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 495 | struct wl_data_source *source = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 496 | char **p; |
| 497 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 498 | wl_signal_emit(&source->destroy_signal, source); |
| 499 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 500 | wl_array_for_each(p, &source->mime_types) |
| 501 | free(*p); |
| 502 | |
| 503 | wl_array_release(&source->mime_types); |
| 504 | |
Kristian Høgsberg | 489b279 | 2013-06-25 11:26:31 -0400 | [diff] [blame^] | 505 | free(source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void |
| 509 | client_source_accept(struct wl_data_source *source, |
| 510 | uint32_t time, const char *mime_type) |
| 511 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 512 | wl_data_source_send_target(source->resource, mime_type); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static void |
| 516 | client_source_send(struct wl_data_source *source, |
| 517 | const char *mime_type, int32_t fd) |
| 518 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 519 | wl_data_source_send_send(source->resource, mime_type, fd); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 520 | close(fd); |
| 521 | } |
| 522 | |
| 523 | static void |
| 524 | client_source_cancel(struct wl_data_source *source) |
| 525 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 526 | wl_data_source_send_cancelled(source->resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static void |
| 530 | create_data_source(struct wl_client *client, |
| 531 | struct wl_resource *resource, uint32_t id) |
| 532 | { |
| 533 | struct wl_data_source *source; |
| 534 | |
| 535 | source = malloc(sizeof *source); |
| 536 | if (source == NULL) { |
| 537 | wl_resource_post_no_memory(resource); |
| 538 | return; |
| 539 | } |
| 540 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 541 | wl_signal_init(&source->destroy_signal); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 542 | source->accept = client_source_accept; |
| 543 | source->send = client_source_send; |
| 544 | source->cancel = client_source_cancel; |
| 545 | |
| 546 | wl_array_init(&source->mime_types); |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 547 | |
| 548 | source->resource = wl_client_add_object(client, |
| 549 | &wl_data_source_interface, |
| 550 | &data_source_interface, |
| 551 | id, source); |
| 552 | wl_resource_set_destructor(source->resource, destroy_data_source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void unbind_data_device(struct wl_resource *resource) |
| 556 | { |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 557 | wl_list_remove(wl_resource_get_link(resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | static void |
| 561 | get_data_device(struct wl_client *client, |
| 562 | struct wl_resource *manager_resource, |
| 563 | uint32_t id, struct wl_resource *seat_resource) |
| 564 | { |
Jason Ekstrand | a0d2dde | 2013-06-14 10:08:01 -0500 | [diff] [blame] | 565 | struct weston_seat *seat = wl_resource_get_user_data(seat_resource); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 566 | struct wl_resource *resource; |
| 567 | |
| 568 | resource = wl_client_add_object(client, &wl_data_device_interface, |
| 569 | &data_device_interface, id, |
| 570 | seat); |
| 571 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 572 | wl_list_insert(&seat->drag_resource_list, wl_resource_get_link(resource)); |
| 573 | wl_resource_set_destructor(resource, unbind_data_device); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static const struct wl_data_device_manager_interface manager_interface = { |
| 577 | create_data_source, |
| 578 | get_data_device |
| 579 | }; |
| 580 | |
| 581 | static void |
| 582 | bind_manager(struct wl_client *client, |
| 583 | void *data, uint32_t version, uint32_t id) |
| 584 | { |
| 585 | wl_client_add_object(client, &wl_data_device_manager_interface, |
| 586 | &manager_interface, id, NULL); |
| 587 | } |
| 588 | |
| 589 | WL_EXPORT void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 590 | wl_data_device_set_keyboard_focus(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 591 | { |
| 592 | struct wl_resource *data_device, *focus, *offer; |
| 593 | struct wl_data_source *source; |
| 594 | |
| 595 | if (!seat->keyboard) |
| 596 | return; |
| 597 | |
| 598 | focus = seat->keyboard->focus_resource; |
| 599 | if (!focus) |
| 600 | return; |
| 601 | |
Jason Ekstrand | 8a4a9eb | 2013-06-14 10:07:55 -0500 | [diff] [blame] | 602 | data_device = wl_resource_find_for_client(&seat->drag_resource_list, |
| 603 | wl_resource_get_client(focus)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 604 | if (!data_device) |
| 605 | return; |
| 606 | |
| 607 | source = seat->selection_data_source; |
| 608 | if (source) { |
| 609 | offer = wl_data_source_send_offer(source, data_device); |
| 610 | wl_data_device_send_selection(data_device, offer); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | WL_EXPORT int |
| 615 | wl_data_device_manager_init(struct wl_display *display) |
| 616 | { |
| 617 | if (wl_display_add_global(display, |
| 618 | &wl_data_device_manager_interface, |
| 619 | NULL, bind_manager) == NULL) |
| 620 | return -1; |
| 621 | |
| 622 | return 0; |
| 623 | } |