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