summaryrefslogtreecommitdiff
path: root/src/lib/ecore_wayland/ecore_wl_dnd.c
blob: 1104f5bd0ab31a2cf3a577edc8b28c81d1681ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <fcntl.h>
#include <sys/epoll.h>
#include "ecore_wl_private.h"

/* local structures */
struct _dnd_task
{
   void *data;
   Ecore_Fd_Cb cb;
};

struct _dnd_read_ctx
{
   int epoll_fd;
   struct epoll_event *ep;
};

/* local function prototypes */
static void _ecore_wl_dnd_selection_data_receive(Ecore_Wl_Dnd_Source *source, const char *type);
static Eina_Bool _ecore_wl_dnd_selection_data_read(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED);
static void _ecore_wl_dnd_selection_data_ready_cb_free(void *data EINA_UNUSED, void *event);
static Eina_Bool _ecore_wl_dnd_selection_cb_idle(void *data);

static void _ecore_wl_dnd_source_cb_target(void *data, struct wl_data_source *source EINA_UNUSED, const char *mime_type EINA_UNUSED);
static void _ecore_wl_dnd_source_cb_send(void *data, struct wl_data_source *source EINA_UNUSED, const char *mime_type, int32_t fd);
static void _ecore_wl_dnd_source_cb_send_free(void *data EINA_UNUSED, void *event);
static void _ecore_wl_dnd_source_cb_cancelled(void *data EINA_UNUSED, struct wl_data_source *source);

static void _ecore_wl_dnd_offer_cb_offer(void *data, struct wl_data_offer *data_offer EINA_UNUSED, const char *type);

/* local wayland interfaces */
static const struct wl_data_source_listener 
_ecore_wl_dnd_source_listener = 
{
   _ecore_wl_dnd_source_cb_target,
   _ecore_wl_dnd_source_cb_send,
   _ecore_wl_dnd_source_cb_cancelled
};

static const struct wl_data_offer_listener 
_ecore_wl_dnd_offer_listener = 
{
   _ecore_wl_dnd_offer_cb_offer
};

/**
 * @deprecated use ecore_wl_dnd_selection_set
 * @since 1.7 
*/
EINA_DEPRECATED EAPI Eina_Bool 
ecore_wl_dnd_set_selection(Ecore_Wl_Dnd *dnd, const char **types_offered)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   return ecore_wl_dnd_selection_set(dnd->input, types_offered);
}

/**
 * @deprecated use ecore_wl_dnd_selection_get
 * @since 1.7 
*/
EINA_DEPRECATED EAPI Eina_Bool 
ecore_wl_dnd_get_selection(Ecore_Wl_Dnd *dnd, const char *type)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   return ecore_wl_dnd_selection_get(dnd->input, type);
}

/**
 * @deprecated Do Not Use
 * @since 1.7
 */
EINA_DEPRECATED EAPI Ecore_Wl_Dnd *
ecore_wl_dnd_get(void)
{
   return NULL;
}

/**
 * @deprecated use ecore_wl_dnd_drag_start
 * @since 1.7
 */
EINA_DEPRECATED EAPI Eina_Bool 
ecore_wl_dnd_start_drag(Ecore_Wl_Dnd *dnd EINA_UNUSED)
{
   return EINA_FALSE;
}

/**
 * @deprecated use ecore_wl_dnd_selection_owner_has
 * @since 1.7
 */
EINA_DEPRECATED EAPI Eina_Bool 
ecore_wl_dnd_selection_has_owner(Ecore_Wl_Dnd *dnd)
{
   return ecore_wl_dnd_selection_owner_has(dnd->input);
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI Eina_Bool 
ecore_wl_dnd_selection_set(Ecore_Wl_Input *input, const char **types_offered)
{
   struct wl_data_device_manager *man;
   const char **type;
   char **t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!input) return EINA_FALSE;

   man = input->display->wl.data_device_manager;

   /* free any old types offered */
   if (input->data_types.data)
     {
        wl_array_for_each(t, &input->data_types)
          free(*t);
        wl_array_release(&input->data_types);
        wl_array_init(&input->data_types);
     }

   /* destroy any existing data source */
   if (input->data_source) wl_data_source_destroy(input->data_source);
   input->data_source = NULL;

   if (!types_offered[0]) return EINA_FALSE;

   /* try to create a new data source */
   if (!(input->data_source = wl_data_device_manager_create_data_source(man)))
     return EINA_FALSE;

   /* add these types to the data source */
   for (type = types_offered; *type; type++)
     {
        t = wl_array_add(&input->data_types, sizeof(*t));
        if (t) *t = strdup(*type);
        wl_data_source_offer(input->data_source, *t);
     }

   /* add a listener for data source events */
   wl_data_source_add_listener(input->data_source, 
                               &_ecore_wl_dnd_source_listener, input);

   /* set the selection */
   wl_data_device_set_selection(input->data_device, input->data_source, 
                                input->display->serial);

   return EINA_TRUE;
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI Eina_Bool 
ecore_wl_dnd_selection_get(Ecore_Wl_Input *input, const char *type)
{
   char **t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* check for valid input and selection source */
   if ((!input) || (!input->selection_source)) return EINA_FALSE;

   for (t = input->selection_source->types.data; *t; t++)
     {
        if (!strcmp(type, *t)) break;
     }

   if (!*t) return EINA_FALSE;

   _ecore_wl_dnd_selection_data_receive(input->selection_source, type);

   return EINA_TRUE;
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI Eina_Bool 
ecore_wl_dnd_selection_owner_has(Ecore_Wl_Input *input)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!input) return EINA_FALSE;
   return (input->selection_source != NULL);
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI Eina_Bool 
ecore_wl_dnd_selection_clear(Ecore_Wl_Input *input)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* check for valid input */
   if (!input) return EINA_FALSE;

   /* set the selection to NULL */
   wl_data_device_set_selection(input->data_device, NULL, 
                                input->display->serial);

   return EINA_TRUE;
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI void 
ecore_wl_dnd_drag_start(Ecore_Wl_Input *input, Ecore_Wl_Window *win, Ecore_Wl_Window *dragwin, int x EINA_UNUSED, int y EINA_UNUSED, int w EINA_UNUSED, int h EINA_UNUSED)
{
   struct wl_surface *drag_surface;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* check for valid input. if not, get the default one */
   if (!input) input = _ecore_wl_disp->input;

   /* check for valid data source */
   if (!input->data_source) return;

   /* get the surface from this drag window */
   drag_surface = ecore_wl_window_surface_get(dragwin);

   /* release any existing grabs */
   ecore_wl_input_ungrab(input);

   /* add a listener for data source events */
   wl_data_source_add_listener(input->data_source, 
                               &_ecore_wl_dnd_source_listener, input);

   /* start the drag */
   wl_data_device_start_drag(input->data_device, input->data_source, 
                             ecore_wl_window_surface_get(win), 
                             drag_surface, input->display->serial);

   /* set pointer image */
   ecore_wl_input_cursor_from_name_set(input, "move");

   /* NB: Below code disabled for now
    * 
    * This Was for adjusting the "drag icon" to be centered on the mouse 
    * based on the hotspot, but it crashes for some reason :(
    */

   /* struct wl_buffer *drag_buffer; */
   /* struct wl_cursor_image *cursor; */
   /* int cx = 0, cy = 0; */
   /* drag_buffer = wl_surface_get_user_data(drag_surface); */
   /* cursor = input->cursor->images[input->cursor_current_index]; */
   /* cx = cursor->hotspot_x - x; */
   /* cy = cursor->hotspot_y - y; */
   /* wl_surface_attach(drag_surface, drag_buffer, cx, cy); */
   /* wl_surface_damage(drag_surface, 0, 0, w, h); */
   /* wl_surface_commit(drag_surface); */
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI void 
ecore_wl_dnd_drag_end(Ecore_Wl_Input *input)
{
   Ecore_Wl_Event_Dnd_End *ev;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* check for valid input. if not, get the default one */
   if (!input) input = _ecore_wl_disp->input;

   if (input->data_types.data)
     {
        char **t;

        wl_array_for_each(t, &input->data_types)
          free(*t);
        wl_array_release(&input->data_types);
        wl_array_init(&input->data_types);
     }

   /* if (input->drag_source) _ecore_wl_dnd_del(input->drag_source); */
   /* input->drag_source = NULL; */

   /* destroy any existing data source */
   if (input->data_source) wl_data_source_destroy(input->data_source);
   input->data_source = NULL;

   if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Dnd_End)))) return;

   if (input->pointer_focus)
     ev->win = input->pointer_focus->id;

   if (input->keyboard_focus)
     ev->source = input->keyboard_focus->id;

   ecore_event_add(ECORE_WL_EVENT_DND_END, ev, NULL, NULL);
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI Eina_Bool 
ecore_wl_dnd_drag_get(Ecore_Wl_Input *input, const char *type)
{
   char **t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* check for valid input and drag source */
   if ((!input) || (!input->drag_source)) return EINA_FALSE;

   wl_array_for_each(t, &input->drag_source->types)
     if (!strcmp(type, *t)) break;

   if (!*t) return EINA_FALSE;

   _ecore_wl_dnd_selection_data_receive(input->drag_source, type);

   return EINA_TRUE;
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI void 
ecore_wl_dnd_drag_types_set(Ecore_Wl_Input *input, const char **types_offered)
{
   struct wl_data_device_manager *man;
   const char **type;
   char **t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* check for valid input. if not, get the default one */
   if (!input) input = _ecore_wl_disp->input;

   man = input->display->wl.data_device_manager;

   /* free any old types offered */
   if (input->data_types.data)
     {
        wl_array_for_each(t, &input->data_types)
          free(*t);
        wl_array_release(&input->data_types);
        wl_array_init(&input->data_types);
     }

   /* destroy any existing data source */
   if (input->data_source) wl_data_source_destroy(input->data_source);
   input->data_source = NULL;

   /* try to create a new data source */
   if (!(input->data_source = wl_data_device_manager_create_data_source(man)))
     {
        printf("Failed to create new data source for drag\n");
        return;
     }

   /* add these types to the data source */
   for (type = types_offered; *type; type++)
     {
        if (!*type) continue;
        t = wl_array_add(&input->data_types, sizeof(*t));
        if (t) 
          {
             *t = strdup(*type);
             wl_data_source_offer(input->data_source, *t);
          }
     }
}

/**
 * @ingroup Ecore_Wl_Dnd_Group
 * @since 1.8
 */
EAPI struct wl_array *
ecore_wl_dnd_drag_types_get(Ecore_Wl_Input *input)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* check for valid input. if not, get the default one */
   if (!input) input = _ecore_wl_disp->input;

   return &input->data_types;
}

/* private functions */
void 
_ecore_wl_dnd_add(Ecore_Wl_Input *input, struct wl_data_device *data_device EINA_UNUSED, struct wl_data_offer *offer)
{
   Ecore_Wl_Dnd_Source *source;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(source = malloc(sizeof(Ecore_Wl_Dnd_Source))))
     return;

   wl_array_init(&source->types);
   source->refcount = 1;
   source->input = input;
   source->data_offer = offer;

   wl_data_offer_add_listener(source->data_offer, 
                              &_ecore_wl_dnd_offer_listener, source);
}

void 
_ecore_wl_dnd_enter(void *data, struct wl_data_device *data_device EINA_UNUSED, unsigned int timestamp, struct wl_surface *surface, int x, int y, struct wl_data_offer *offer)
{
   Ecore_Wl_Event_Dnd_Enter *ev;
   Ecore_Wl_Window *win;
   Ecore_Wl_Input *input;
   char **types;
   int num = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(input = data)) return;

   win = ecore_wl_window_surface_find(surface);

   input->pointer_enter_serial = timestamp;
   input->pointer_focus = win;

   if (offer)
     {
        input->drag_source = wl_data_offer_get_user_data(offer);

        num = (input->drag_source->types.size / sizeof(char *));
        types = input->drag_source->types.data;
     }
   else
     {
        input->drag_source = NULL;
        types = NULL;
     }

   if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Dnd_Enter)))) return;

   if (win) 
     ev->win = win->id;

   if (input->keyboard_focus)
     ev->source = input->keyboard_focus->id;

   ev->offer = offer;
   ev->serial = timestamp;
   ev->position.x = wl_fixed_to_int(x);
   ev->position.y = wl_fixed_to_int(y);
   ev->num_types = num;
   ev->types = types;

   ecore_event_add(ECORE_WL_EVENT_DND_ENTER, ev, NULL, NULL);
}

void 
_ecore_wl_dnd_leave(void *data, struct wl_data_device *data_device EINA_UNUSED)
{
   Ecore_Wl_Event_Dnd_Leave *ev;
   Ecore_Wl_Input *input;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(input = data)) return;

   if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Dnd_Leave)))) return;

   if (input->pointer_focus)
     ev->win = input->pointer_focus->id;

   if (input->keyboard_focus)
     ev->source = input->keyboard_focus->id;

   ecore_event_add(ECORE_WL_EVENT_DND_LEAVE, ev, NULL, NULL);
}

void 
_ecore_wl_dnd_motion(void *data, struct wl_data_device *data_device EINA_UNUSED, unsigned int timestamp EINA_UNUSED, int x, int y)
{
   Ecore_Wl_Event_Dnd_Position *ev;
   Ecore_Wl_Input *input;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(input = data)) return;

   input->sx = wl_fixed_to_int(x);
   input->sy = wl_fixed_to_int(y);

   if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Dnd_Position)))) return;

   if (input->pointer_focus)
     ev->win = input->pointer_focus->id;

   if (input->keyboard_focus)
     ev->source = input->keyboard_focus->id;

   ev->position.x = input->sx;
   ev->position.y = input->sy;

   ecore_event_add(ECORE_WL_EVENT_DND_POSITION, ev, NULL, NULL);
}

void 
_ecore_wl_dnd_drop(void *data, struct wl_data_device *data_device EINA_UNUSED)
{
   Ecore_Wl_Event_Dnd_Drop *ev;
   Ecore_Wl_Input *input;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(input = data)) return;

   if (!(ev = calloc(1, sizeof(Ecore_Wl_Event_Dnd_Drop)))) return;

   if (input->drag_source)
     {
        if (input->pointer_focus)
          ev->win = input->pointer_focus->id;
        if (input->keyboard_focus)
          ev->source = input->keyboard_focus->id;
     }

   ev->position.x = input->sx;
   ev->position.y = input->sy;

   ecore_event_add(ECORE_WL_EVENT_DND_DROP, ev, NULL, NULL);
}

void 
_ecore_wl_dnd_selection(void *data, struct wl_data_device *data_device EINA_UNUSED, struct wl_data_offer *offer)
{
   Ecore_Wl_Input *input;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(input = data)) return;

   if (input->selection_source) _ecore_wl_dnd_del(input->selection_source);
   input->selection_source = NULL;

   if (offer)
     {
        char **t;

        input->selection_source = wl_data_offer_get_user_data(offer);
        t = wl_array_add(&input->selection_source->types, sizeof(*t));
        *t = NULL;
     }
}

void 
_ecore_wl_dnd_del(Ecore_Wl_Dnd_Source *source)
{
   char **t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!source) return;
   source->refcount--;
   if (source->refcount == 0)
     {
        wl_data_offer_destroy(source->data_offer);
        for (t = source->types.data; *t; t++)
          free(*t);
        wl_array_release(&source->types);
        free(source);
     }
}

/* local functions */
static void 
_ecore_wl_dnd_selection_data_receive(Ecore_Wl_Dnd_Source *source, const char *type)
{
   int epoll_fd;
   struct epoll_event *ep = NULL;
   struct _dnd_task *task = NULL;
   struct _dnd_read_ctx *read_ctx = NULL;
   int p[2];

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (pipe2(p, O_CLOEXEC) == -1)
     return;

   wl_data_offer_receive(source->data_offer, type, p[1]);
   close(p[1]);

   /* Due to http://trac.enlightenment.org/e/ticket/1208,
    * use epoll and idle handler instead of ecore_main_fd_handler_add() */

   ep = calloc(1, sizeof(struct epoll_event));
   if (!ep) goto err;

   task = calloc(1, sizeof(struct _dnd_task));
   if (!task) goto err;

   read_ctx = calloc(1, sizeof(struct _dnd_read_ctx));
   if (!read_ctx) goto err;

   epoll_fd  = epoll_create1(0);
   if (epoll_fd < 0) goto err;

   task->data = source;
   task->cb = _ecore_wl_dnd_selection_data_read;
   ep->events = EPOLLIN;
   ep->data.ptr = task;

   if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, p[0], ep) < 0) goto err;

   read_ctx->epoll_fd = epoll_fd;
   read_ctx->ep = ep;

   if (!ecore_idler_add(_ecore_wl_dnd_selection_cb_idle, read_ctx)) goto err;

   source->refcount++;
   source->fd = p[0];

   return;

err:
   if (ep) free(ep);
   if (task) free(task);
   if (read_ctx) free(read_ctx);
   close(p[0]);
   return;
}

static Eina_Bool 
_ecore_wl_dnd_selection_data_read(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
{
   int len;
   char buffer[PATH_MAX];
   Ecore_Wl_Dnd_Source *source;
   Ecore_Wl_Event_Selection_Data_Ready *event;
   Eina_Bool ret;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   source = data;

   len = read(source->fd, buffer, sizeof buffer);

   if (!(event = calloc(1, sizeof(Ecore_Wl_Event_Selection_Data_Ready)))) 
     return ECORE_CALLBACK_CANCEL;

   if (len <= 0)
     {
        close(source->fd);
        _ecore_wl_dnd_del(source);
        event->done = EINA_TRUE;
        event->data = NULL;
        event->len = 0;
        ret = ECORE_CALLBACK_CANCEL;
     }
   else
     {
        event->data = malloc(len + 1);
        if (!event->data)
          {
             free(event);
             return ECORE_CALLBACK_CANCEL;
          }
        strncpy(event->data, buffer, len);
        event->data[len] = '\0';
        event->len = len;
        event->done = EINA_FALSE;
        ret = ECORE_CALLBACK_RENEW;
     }

   ecore_event_add(ECORE_WL_EVENT_SELECTION_DATA_READY, event, 
                   _ecore_wl_dnd_selection_data_ready_cb_free, NULL);

   return ret;
}

static void 
_ecore_wl_dnd_selection_data_ready_cb_free(void *data EINA_UNUSED, void *event)
{
   Ecore_Wl_Event_Selection_Data_Ready *ev;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(ev = event)) return;

   free(ev->data);
   free(ev);
}

static Eina_Bool
_ecore_wl_dnd_selection_cb_idle(void *data)
{
   struct _dnd_read_ctx *ctx;
   struct _dnd_task *task;
   int count, i;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   ctx = data;
   count = epoll_wait(ctx->epoll_fd, ctx->ep, 1, 0);
   for (i = 0; i < count; i++)
     {
        task = ctx->ep->data.ptr;
        if (task->cb(task->data, NULL) == ECORE_CALLBACK_CANCEL)
          {
             free(ctx->ep);
             free(task);
             free(ctx);
             return ECORE_CALLBACK_CANCEL;
          }
     } 
   return ECORE_CALLBACK_RENEW;
}

static void 
_ecore_wl_dnd_source_cb_target(void *data, struct wl_data_source *source EINA_UNUSED, const char *mime_type EINA_UNUSED)
{
   Ecore_Wl_Input *input;

   if (!(input = data)) return;

   printf("Dnd Source Target\n");
}

static void 
_ecore_wl_dnd_source_cb_send(void *data, struct wl_data_source *source EINA_UNUSED, const char *mime_type, int32_t fd)
{
   Ecore_Wl_Event_Data_Source_Send *event;
   Ecore_Wl_Input *input;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(input = data)) return;

   if (!(event = calloc(1, sizeof(Ecore_Wl_Event_Data_Source_Send)))) return;

   event->type = strdup(mime_type);
   event->fd = fd;

   ecore_event_add(ECORE_WL_EVENT_DATA_SOURCE_SEND, event, 
                   _ecore_wl_dnd_source_cb_send_free, NULL);
}

static void 
_ecore_wl_dnd_source_cb_send_free(void *data EINA_UNUSED, void *event)
{
   Ecore_Wl_Event_Data_Source_Send *ev;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(ev = event)) return;

   free(ev->type);
   free(ev);
}

static void 
_ecore_wl_dnd_source_cb_cancelled(void *data EINA_UNUSED, struct wl_data_source *source)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   /* FIXME: Raise an Ecore_Wl_Event here */
   wl_data_source_destroy(source);
}

static void 
_ecore_wl_dnd_offer_cb_offer(void *data, struct wl_data_offer *data_offer EINA_UNUSED, const char *type)
{
   Ecore_Wl_Dnd_Source *source;
   char **t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!(source = data)) return;
   if (!type) return;

   t = wl_array_add(&source->types, sizeof(*t));
   if (t) *t = strdup(type);
}