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