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