compositor-drm: Work around page flip not setting tiling mode on BYT
[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 void
218 pointer_drag_surface_configure(struct weston_surface *es,
219                                int32_t sx, int32_t sy)
220 {
221         struct weston_pointer_drag *drag = es->configure_private;
222         struct weston_pointer *pointer = drag->grab.pointer;
223
224         assert(es->configure == pointer_drag_surface_configure);
225
226         drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
227 }
228
229 static void
230 touch_drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
231 {
232         struct weston_touch_drag *drag = es->configure_private;
233         struct weston_touch *touch = drag->grab.touch;
234
235         assert(es->configure == touch_drag_surface_configure);
236
237         drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
238 }
239
240 static void
241 destroy_drag_focus(struct wl_listener *listener, void *data)
242 {
243         struct weston_drag *drag =
244                 container_of(listener, struct weston_drag, focus_listener);
245
246         drag->focus_resource = NULL;
247 }
248
249 static void
250 weston_drag_set_focus(struct weston_drag *drag,
251                         struct weston_seat *seat,
252                         struct weston_view *view,
253                         wl_fixed_t sx, wl_fixed_t sy)
254 {
255         struct wl_resource *resource, *offer = NULL;
256         struct wl_display *display = seat->compositor->wl_display;
257         uint32_t serial;
258
259         if (drag->focus && view && drag->focus->surface == view->surface) {
260                 drag->focus = view;
261                 return;
262         }
263
264         if (drag->focus_resource) {
265                 wl_data_device_send_leave(drag->focus_resource);
266                 wl_list_remove(&drag->focus_listener.link);
267                 drag->focus_resource = NULL;
268                 drag->focus = NULL;
269         }
270
271         if (!view || !view->surface->resource)
272                 return;
273
274         if (!drag->data_source &&
275             wl_resource_get_client(view->surface->resource) != drag->client)
276                 return;
277
278         resource = wl_resource_find_for_client(&seat->drag_resource_list,
279                                                wl_resource_get_client(view->surface->resource));
280         if (!resource)
281                 return;
282
283         serial = wl_display_next_serial(display);
284
285         if (drag->data_source) {
286                 offer = weston_data_source_send_offer(drag->data_source,
287                                                       resource);
288                 if (offer == NULL)
289                         return;
290         }
291
292         wl_data_device_send_enter(resource, serial, view->surface->resource,
293                                   sx, sy, offer);
294
295         drag->focus = view;
296         drag->focus_listener.notify = destroy_drag_focus;
297         wl_resource_add_destroy_listener(resource, &drag->focus_listener);
298         drag->focus_resource = resource;
299 }
300
301 static void
302 drag_grab_focus(struct weston_pointer_grab *grab)
303 {
304         struct weston_pointer_drag *drag =
305                 container_of(grab, struct weston_pointer_drag, grab);
306         struct weston_pointer *pointer = grab->pointer;
307         struct weston_view *view;
308         wl_fixed_t sx, sy;
309
310         view = weston_compositor_pick_view(pointer->seat->compositor,
311                                            pointer->x, pointer->y,
312                                            &sx, &sy);
313         if (drag->base.focus != view)
314                 weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
315 }
316
317 static void
318 drag_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
319                  wl_fixed_t x, wl_fixed_t y)
320 {
321         struct weston_pointer_drag *drag =
322                 container_of(grab, struct weston_pointer_drag, grab);
323         struct weston_pointer *pointer = drag->grab.pointer;
324         float fx, fy;
325         wl_fixed_t sx, sy;
326
327         weston_pointer_move(pointer, x, y);
328
329         if (drag->base.icon) {
330                 fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
331                 fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
332                 weston_view_set_position(drag->base.icon, fx, fy);
333                 weston_view_schedule_repaint(drag->base.icon);
334         }
335
336         if (drag->base.focus_resource) {
337                 weston_view_from_global_fixed(drag->base.focus,
338                                               pointer->x, pointer->y,
339                                               &sx, &sy);
340
341                 wl_data_device_send_motion(drag->base.focus_resource, time, sx, sy);
342         }
343 }
344
345 static void
346 data_device_end_drag_grab(struct weston_drag *drag,
347                 struct weston_seat *seat)
348 {
349         if (drag->icon) {
350                 if (weston_view_is_mapped(drag->icon))
351                         weston_view_unmap(drag->icon);
352
353                 drag->icon->surface->configure = NULL;
354                 pixman_region32_clear(&drag->icon->surface->pending.input);
355                 wl_list_remove(&drag->icon_destroy_listener.link);
356                 weston_view_destroy(drag->icon);
357         }
358
359         weston_drag_set_focus(drag, seat, NULL, 0, 0);
360 }
361
362 static void
363 data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
364 {
365         struct weston_pointer *pointer = drag->grab.pointer;
366
367         data_device_end_drag_grab(&drag->base, pointer->seat);
368         weston_pointer_end_grab(pointer);
369         free(drag);
370 }
371
372 static void
373 drag_grab_button(struct weston_pointer_grab *grab,
374                  uint32_t time, uint32_t button, uint32_t state_w)
375 {
376         struct weston_pointer_drag *drag =
377                 container_of(grab, struct weston_pointer_drag, grab);
378         struct weston_pointer *pointer = drag->grab.pointer;
379         enum wl_pointer_button_state state = state_w;
380
381         if (drag->base.focus_resource &&
382             pointer->grab_button == button &&
383             state == WL_POINTER_BUTTON_STATE_RELEASED)
384                 wl_data_device_send_drop(drag->base.focus_resource);
385
386         if (pointer->button_count == 0 &&
387             state == WL_POINTER_BUTTON_STATE_RELEASED) {
388                 if (drag->base.data_source)
389                         wl_list_remove(&drag->base.data_source_listener.link);
390                 data_device_end_pointer_drag_grab(drag);
391         }
392 }
393
394 static void
395 drag_grab_cancel(struct weston_pointer_grab *grab)
396 {
397         struct weston_pointer_drag *drag =
398                 container_of(grab, struct weston_pointer_drag, grab);
399
400         if (drag->base.data_source)
401                 wl_list_remove(&drag->base.data_source_listener.link);
402
403         data_device_end_pointer_drag_grab(drag);
404 }
405
406 static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
407         drag_grab_focus,
408         drag_grab_motion,
409         drag_grab_button,
410         drag_grab_cancel,
411 };
412
413 static void
414 drag_grab_touch_down(struct weston_touch_grab *grab, uint32_t time,
415                 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
416 {
417 }
418
419 static void
420 data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
421 {
422         struct weston_touch *touch = drag->grab.touch;
423
424         data_device_end_drag_grab(&drag->base, touch->seat);
425         weston_touch_end_grab(touch);
426         free(drag);
427 }
428
429 static void
430 drag_grab_touch_up(struct weston_touch_grab *grab,
431                 uint32_t time, int touch_id)
432 {
433         struct weston_touch_drag *touch_drag =
434                 container_of(grab, struct weston_touch_drag, grab);
435         struct weston_touch *touch = grab->touch;
436
437         if (touch_id != touch->grab_touch_id)
438                 return;
439
440         if (touch_drag->base.focus_resource)
441                 wl_data_device_send_drop(touch_drag->base.focus_resource);
442         if (touch_drag->base.data_source)
443                 wl_list_remove(&touch_drag->base.data_source_listener.link);
444         data_device_end_touch_drag_grab(touch_drag);
445 }
446
447 static void
448 drag_grab_touch_focus(struct weston_touch_drag *drag)
449 {
450         struct weston_touch *touch = drag->grab.touch;
451         struct weston_view *view;
452         wl_fixed_t view_x, view_y;
453
454         view = weston_compositor_pick_view(touch->seat->compositor,
455                                 touch->grab_x, touch->grab_y,
456                                 &view_x, &view_y);
457         if (drag->base.focus != view)
458                 weston_drag_set_focus(&drag->base, touch->seat,
459                                 view, view_x, view_y);
460 }
461
462 static void
463 drag_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time,
464                 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
465 {
466         struct weston_touch_drag *touch_drag =
467                 container_of(grab, struct weston_touch_drag, grab);
468         struct weston_touch *touch = grab->touch;
469         wl_fixed_t view_x, view_y;
470         float fx, fy;
471
472         if (touch_id != touch->grab_touch_id)
473                 return;
474
475         drag_grab_touch_focus(touch_drag);
476         if (touch_drag->base.icon) {
477                 fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
478                 fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
479                 weston_view_set_position(touch_drag->base.icon, fx, fy);
480                 weston_view_schedule_repaint(touch_drag->base.icon);
481         }
482
483         if (touch_drag->base.focus_resource) {
484                 weston_view_from_global_fixed(touch_drag->base.focus,
485                                         touch->grab_x, touch->grab_y,
486                                         &view_x, &view_y);
487                 wl_data_device_send_motion(touch_drag->base.focus_resource, time,
488                                         view_x, view_y);
489         }
490 }
491
492 static void
493 drag_grab_touch_frame(struct weston_touch_grab *grab)
494 {
495 }
496
497 static void
498 drag_grab_touch_cancel(struct weston_touch_grab *grab)
499 {
500         struct weston_touch_drag *touch_drag =
501                 container_of(grab, struct weston_touch_drag, grab);
502
503         if (touch_drag->base.data_source)
504                 wl_list_remove(&touch_drag->base.data_source_listener.link);
505         data_device_end_touch_drag_grab(touch_drag);
506 }
507
508 static const struct weston_touch_grab_interface touch_drag_grab_interface = {
509         drag_grab_touch_down,
510         drag_grab_touch_up,
511         drag_grab_touch_motion,
512         drag_grab_touch_frame,
513         drag_grab_touch_cancel
514 };
515
516 static void
517 destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
518 {
519         struct weston_pointer_drag *drag = container_of(listener,
520                         struct weston_pointer_drag, base.data_source_listener);
521
522         data_device_end_pointer_drag_grab(drag);
523 }
524
525 static void
526 handle_drag_icon_destroy(struct wl_listener *listener, void *data)
527 {
528         struct weston_drag *drag = container_of(listener, struct weston_drag,
529                                                 icon_destroy_listener);
530
531         drag->icon = NULL;
532 }
533
534 WL_EXPORT int
535 weston_pointer_start_drag(struct weston_pointer *pointer,
536                        struct weston_data_source *source,
537                        struct weston_surface *icon,
538                        struct wl_client *client)
539 {
540         struct weston_pointer_drag *drag;
541
542         drag = zalloc(sizeof *drag);
543         if (drag == NULL)
544                 return -1;
545
546         drag->grab.interface = &pointer_drag_grab_interface;
547         drag->base.client = client;
548         drag->base.data_source = source;
549
550         if (icon) {
551                 drag->base.icon = weston_view_create(icon);
552                 if (drag->base.icon == NULL) {
553                         free(drag);
554                         return -1;
555                 }
556
557                 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
558                 wl_signal_add(&icon->destroy_signal,
559                               &drag->base.icon_destroy_listener);
560
561                 icon->configure = pointer_drag_surface_configure;
562                 icon->configure_private = drag;
563         } else {
564                 drag->base.icon = NULL;
565         }
566
567         if (source) {
568                 drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
569                 wl_signal_add(&source->destroy_signal,
570                               &drag->base.data_source_listener);
571         }
572
573         weston_pointer_set_focus(pointer, NULL,
574                                  wl_fixed_from_int(0), wl_fixed_from_int(0));
575         weston_pointer_start_grab(pointer, &drag->grab);
576
577         return 0;
578 }
579
580 static void
581 destroy_touch_data_device_source(struct wl_listener *listener, void *data)
582 {
583         struct weston_touch_drag *drag = container_of(listener,
584                         struct weston_touch_drag, base.data_source_listener);
585
586         data_device_end_touch_drag_grab(drag);
587 }
588
589 WL_EXPORT int
590 weston_touch_start_drag(struct weston_touch *touch,
591                        struct weston_data_source *source,
592                        struct weston_surface *icon,
593                        struct wl_client *client)
594 {
595         struct weston_touch_drag *drag;
596
597         drag = zalloc(sizeof *drag);
598         if (drag == NULL)
599                 return -1;
600
601         drag->grab.interface = &touch_drag_grab_interface;
602         drag->base.client = client;
603         drag->base.data_source = source;
604
605         if (icon) {
606                 drag->base.icon = weston_view_create(icon);
607                 if (drag->base.icon == NULL) {
608                         free(drag);
609                         return -1;
610                 }
611
612                 drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
613                 wl_signal_add(&icon->destroy_signal,
614                               &drag->base.icon_destroy_listener);
615
616                 icon->configure = touch_drag_surface_configure;
617                 icon->configure_private = drag;
618         } else {
619                 drag->base.icon = NULL;
620         }
621
622         if (source) {
623                 drag->base.data_source_listener.notify = destroy_touch_data_device_source;
624                 wl_signal_add(&source->destroy_signal,
625                               &drag->base.data_source_listener);
626         }
627
628         weston_touch_start_grab(touch, &drag->grab);
629
630         drag_grab_touch_focus(drag);
631
632         return 0;
633 }
634
635 static void
636 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
637                        struct wl_resource *source_resource,
638                        struct wl_resource *origin_resource,
639                        struct wl_resource *icon_resource, uint32_t serial)
640 {
641         struct weston_seat *seat = wl_resource_get_user_data(resource);
642         struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
643         struct weston_data_source *source = NULL;
644         struct weston_surface *icon = NULL;
645         int is_pointer_grab, is_touch_grab;
646         int32_t ret = 0;
647
648         is_pointer_grab = seat->pointer &&
649                           seat->pointer->button_count == 1 &&
650                           seat->pointer->grab_serial == serial &&
651                           seat->pointer->focus &&
652                           seat->pointer->focus->surface == origin;
653
654         is_touch_grab = seat->touch &&
655                         seat->touch->num_tp == 1 &&
656                         seat->touch->grab_serial == serial &&
657                         seat->touch->focus &&
658                         seat->touch->focus->surface == origin;
659
660         if (!is_pointer_grab && !is_touch_grab)
661                 return;
662
663         /* FIXME: Check that the data source type array isn't empty. */
664
665         if (source_resource)
666                 source = wl_resource_get_user_data(source_resource);
667         if (icon_resource)
668                 icon = wl_resource_get_user_data(icon_resource);
669         if (icon && icon->configure) {
670                 wl_resource_post_error(icon_resource,
671                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
672                                        "surface->configure already set");
673                 return;
674         }
675
676         if (is_pointer_grab)
677                 ret = weston_pointer_start_drag(seat->pointer, source, icon, client);
678         else if (is_touch_grab)
679                 ret = weston_touch_start_drag(seat->touch, source, icon, client);
680
681         if (ret < 0)
682                 wl_resource_post_no_memory(resource);
683 }
684
685 static void
686 destroy_selection_data_source(struct wl_listener *listener, void *data)
687 {
688         struct weston_seat *seat = container_of(listener, struct weston_seat,
689                                                 selection_data_source_listener);
690         struct wl_resource *data_device;
691         struct weston_surface *focus = NULL;
692
693         seat->selection_data_source = NULL;
694
695         if (seat->keyboard)
696                 focus = seat->keyboard->focus;
697         if (focus && focus->resource) {
698                 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
699                                                           wl_resource_get_client(focus->resource));
700                 if (data_device)
701                         wl_data_device_send_selection(data_device, NULL);
702         }
703
704         wl_signal_emit(&seat->selection_signal, seat);
705 }
706
707 WL_EXPORT void
708 weston_seat_set_selection(struct weston_seat *seat,
709                           struct weston_data_source *source, uint32_t serial)
710 {
711         struct wl_resource *data_device, *offer;
712         struct weston_surface *focus = NULL;
713
714         if (seat->selection_data_source &&
715             seat->selection_serial - serial < UINT32_MAX / 2)
716                 return;
717
718         if (seat->selection_data_source) {
719                 seat->selection_data_source->cancel(seat->selection_data_source);
720                 wl_list_remove(&seat->selection_data_source_listener.link);
721                 seat->selection_data_source = NULL;
722         }
723
724         seat->selection_data_source = source;
725         seat->selection_serial = serial;
726
727         if (seat->keyboard)
728                 focus = seat->keyboard->focus;
729         if (focus && focus->resource) {
730                 data_device = wl_resource_find_for_client(&seat->drag_resource_list,
731                                                           wl_resource_get_client(focus->resource));
732                 if (data_device && source) {
733                         offer = weston_data_source_send_offer(seat->selection_data_source,
734                                                               data_device);
735                         wl_data_device_send_selection(data_device, offer);
736                 } else if (data_device) {
737                         wl_data_device_send_selection(data_device, NULL);
738                 }
739         }
740
741         wl_signal_emit(&seat->selection_signal, seat);
742
743         if (source) {
744                 seat->selection_data_source_listener.notify =
745                         destroy_selection_data_source;
746                 wl_signal_add(&source->destroy_signal,
747                               &seat->selection_data_source_listener);
748         }
749 }
750
751 static void
752 data_device_set_selection(struct wl_client *client,
753                           struct wl_resource *resource,
754                           struct wl_resource *source_resource, uint32_t serial)
755 {
756         if (!source_resource)
757                 return;
758
759         /* FIXME: Store serial and check against incoming serial here. */
760         weston_seat_set_selection(wl_resource_get_user_data(resource),
761                                   wl_resource_get_user_data(source_resource),
762                                   serial);
763 }
764
765 static const struct wl_data_device_interface data_device_interface = {
766         data_device_start_drag,
767         data_device_set_selection,
768 };
769
770 static void
771 destroy_data_source(struct wl_resource *resource)
772 {
773         struct weston_data_source *source =
774                 wl_resource_get_user_data(resource);
775         char **p;
776
777         wl_signal_emit(&source->destroy_signal, source);
778
779         wl_array_for_each(p, &source->mime_types)
780                 free(*p);
781
782         wl_array_release(&source->mime_types);
783
784         free(source);
785 }
786
787 static void
788 client_source_accept(struct weston_data_source *source,
789                      uint32_t time, const char *mime_type)
790 {
791         wl_data_source_send_target(source->resource, mime_type);
792 }
793
794 static void
795 client_source_send(struct weston_data_source *source,
796                    const char *mime_type, int32_t fd)
797 {
798         wl_data_source_send_send(source->resource, mime_type, fd);
799         close(fd);
800 }
801
802 static void
803 client_source_cancel(struct weston_data_source *source)
804 {
805         wl_data_source_send_cancelled(source->resource);
806 }
807
808 static void
809 create_data_source(struct wl_client *client,
810                    struct wl_resource *resource, uint32_t id)
811 {
812         struct weston_data_source *source;
813
814         source = malloc(sizeof *source);
815         if (source == NULL) {
816                 wl_resource_post_no_memory(resource);
817                 return;
818         }
819
820         wl_signal_init(&source->destroy_signal);
821         source->accept = client_source_accept;
822         source->send = client_source_send;
823         source->cancel = client_source_cancel;
824
825         wl_array_init(&source->mime_types);
826
827         source->resource =
828                 wl_resource_create(client, &wl_data_source_interface, 1, id);
829         wl_resource_set_implementation(source->resource, &data_source_interface,
830                                        source, destroy_data_source);
831 }
832
833 static void unbind_data_device(struct wl_resource *resource)
834 {
835         wl_list_remove(wl_resource_get_link(resource));
836 }
837
838 static void
839 get_data_device(struct wl_client *client,
840                 struct wl_resource *manager_resource,
841                 uint32_t id, struct wl_resource *seat_resource)
842 {
843         struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
844         struct wl_resource *resource;
845
846         resource = wl_resource_create(client,
847                                       &wl_data_device_interface, 1, id);
848         if (resource == NULL) {
849                 wl_resource_post_no_memory(manager_resource);
850                 return;
851         }
852
853         wl_list_insert(&seat->drag_resource_list,
854                        wl_resource_get_link(resource));
855         wl_resource_set_implementation(resource, &data_device_interface,
856                                        seat, unbind_data_device);
857 }
858
859 static const struct wl_data_device_manager_interface manager_interface = {
860         create_data_source,
861         get_data_device
862 };
863
864 static void
865 bind_manager(struct wl_client *client,
866              void *data, uint32_t version, uint32_t id)
867 {
868         struct wl_resource *resource;
869
870         resource =
871                 wl_resource_create(client,
872                                    &wl_data_device_manager_interface, 1, id);
873         if (resource == NULL) {
874                 wl_client_post_no_memory(client);
875                 return;
876         }
877
878         wl_resource_set_implementation(resource, &manager_interface,
879                                        NULL, NULL);
880 }
881
882 WL_EXPORT void
883 wl_data_device_set_keyboard_focus(struct weston_seat *seat)
884 {
885         struct wl_resource *data_device, *offer;
886         struct weston_data_source *source;
887         struct weston_surface *focus;
888
889         if (!seat->keyboard)
890                 return;
891
892         focus = seat->keyboard->focus;
893         if (!focus || !focus->resource)
894                 return;
895
896         data_device = wl_resource_find_for_client(&seat->drag_resource_list,
897                                                   wl_resource_get_client(focus->resource));
898         if (!data_device)
899                 return;
900
901         source = seat->selection_data_source;
902         if (source) {
903                 offer = weston_data_source_send_offer(source, data_device);
904                 wl_data_device_send_selection(data_device, offer);
905         }
906 }
907
908 WL_EXPORT int
909 wl_data_device_manager_init(struct wl_display *display)
910 {
911         if (wl_global_create(display,
912                              &wl_data_device_manager_interface, 1,
913                              NULL, bind_manager) == NULL)
914                 return -1;
915
916         return 0;
917 }