add mmap safety enable in elm too
[framework/uifw/elementary.git] / src / lib / elm_cnp_helper.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4 #include <Elementary.h>
5 #include "elm_priv.h"
6
7 #include <sys/mman.h>
8
9 #ifdef HAVE_ELEMENTARY_X
10
11 #define ARRAYINIT(foo)  [foo] =
12
13 //#define DEBUGON 1
14
15 #ifdef DEBUGON
16 # define cnp_debug(x...) fprintf(stderr, __FILE__": " x)
17 #else
18 # define cnp_debug(x...)
19 #endif
20
21 typedef struct _Cnp_Selection Cnp_Selection;
22 typedef struct _Escape        Escape;
23 typedef struct _Tmp_Info      Tmp_Info;
24 typedef struct _Cnp_Atom      Cnp_Atom;
25 typedef struct _Saved_Type    Saved_Type;
26 typedef struct _Dropable      Dropable;
27
28 typedef Eina_Bool (*Converter_Fn_Cb)     (char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
29 typedef int       (*Response_Handler_Cb) (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
30 typedef int       (*Notify_Handler_Cb)   (Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
31
32 enum
33 {
34    CNP_ATOM_TARGETS = 0,
35    CNP_ATOM_text_uri,
36    CNP_ATOM_text_urilist,
37    CNP_ATOM_text_x_vcard,
38    CNP_ATOM_image_png,
39    CNP_ATOM_image_jpeg,
40    CNP_ATOM_image_bmp,
41    CNP_ATOM_image_gif,
42    CNP_ATOM_image_tiff,
43    CNP_ATOM_image_svg,
44    CNP_ATOM_image_xpm,
45    CNP_ATOM_image_tga,
46    CNP_ATOM_image_ppm,
47    CNP_ATOM_XELM,
48    CNP_ATOM_text_html_utf8,
49    CNP_ATOM_text_html,
50    CNP_ATOM_UTF8STRING,
51    CNP_ATOM_STRING,
52    CNP_ATOM_TEXT,
53    CNP_ATOM_text_plain_utf8,
54    CNP_ATOM_text_plain,
55
56    CNP_N_ATOMS,
57 };
58
59 struct _Cnp_Selection
60 {
61    const char      *debug;
62    Evas_Object     *widget;
63    char            *selbuf;
64    Evas_Object     *requestwidget;
65    void            *udata;
66    Elm_Sel_Format   requestformat;
67    Elm_Drop_Cb      datacb;
68    Eina_Bool      (*set)     (Ecore_X_Window, const void *data, int size);
69    Eina_Bool      (*clear)   (void);
70    void           (*request) (Ecore_X_Window, const char *target);
71
72    Elm_Sel_Format    format;
73    Ecore_X_Selection ecore_sel;
74
75    Eina_Bool         active : 1;
76 };
77
78 struct _Escape
79 {
80    const char *escape;
81    const char  value;
82 };
83
84 struct _Tmp_Info
85 {
86    char *filename;
87    void *map;
88    int   fd;
89    int   len;
90 };
91
92 struct _Cnp_Atom
93 {
94    const char          *name;
95    Elm_Sel_Format       formats;
96    /* Called by ecore to do conversion */
97    Converter_Fn_Cb      converter;
98    Response_Handler_Cb  response;
99    Notify_Handler_Cb    notify;
100    /* Atom */
101    Ecore_X_Atom         atom;
102 };
103
104 struct _Saved_Type
105 {
106    const char  **types;
107    char         *imgfile;
108    int           ntypes;
109    int           x, y;
110    Eina_Bool     textreq: 1;
111 };
112
113 struct _Dropable
114 {
115    Evas_Object     *obj;
116    /* FIXME: Cache window */
117    Elm_Sel_Format   types;
118    Elm_Drop_Cb      dropcb;
119    void            *cbdata;
120 };
121
122 static Tmp_Info *elm_cnp_tempfile_create(int size);
123 static int tmpinfo_free(Tmp_Info *tmp);
124
125 static Eina_Bool _elm_cnp_init(void);
126 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
127 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
128 static char *remove_tags(const char *p, int *len);
129 static char *mark_up(const char *start, int inlen, int *lenp);
130
131 static Eina_Bool targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
132 static Eina_Bool text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
133 static Eina_Bool html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
134 static Eina_Bool edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
135 static Eina_Bool uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
136 static Eina_Bool image_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
137 static Eina_Bool vcard_send(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
138
139 static int response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *);
140
141 static int notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
142 static int notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
143 static int notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
144 static int notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
145 static int notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify);
146 static int vcard_receive(Cnp_Selection *sed, Ecore_X_Event_Selection_Notify *notify);
147
148 static Eina_Bool pasteimage_append(char *file, Evas_Object *entry);
149
150 /* Optimisation: Turn this into a 256 byte table:
151  *      then can lookup in one index, not N checks */
152 static const Escape escapes[] = {
153        { "<br>",   '\n' },
154        { "<\t>",   '\t' },
155        { "gt;",    '>'  },
156        { "lt;",    '<'  },
157        { "amp;",   '&'  },
158        { "quot;",  '\'' },
159        { "dquot;", '"'  }
160 };
161 #define N_ESCAPES ((int)(sizeof(escapes) / sizeof(escapes[0])))
162
163 static Cnp_Atom atoms[CNP_N_ATOMS] = {
164      [CNP_ATOM_TARGETS] = {
165           "TARGETS",
166           (Elm_Sel_Format) -1, // everything
167           targets_converter,
168           response_handler_targets,
169           notify_handler_targets,
170           0
171      },
172      [CNP_ATOM_XELM] =  {
173           "application/x-elementary-markup",
174           ELM_SEL_FORMAT_MARKUP,
175           edje_converter,
176           NULL,
177           NULL,
178           0
179      },
180      [CNP_ATOM_text_uri] = {
181           "text/uri",
182           ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_IMAGE, /* Either images or entries */
183           uri_converter,
184           NULL,
185           notify_handler_uri,
186           0
187      },
188      [CNP_ATOM_text_urilist] = {
189           "text/uri-list",
190           ELM_SEL_FORMAT_IMAGE,
191           uri_converter,
192           NULL,
193           notify_handler_uri,
194           0
195      },
196      [CNP_ATOM_text_x_vcard] = {
197           "text/x-vcard",
198           ELM_SEL_FORMAT_VCARD,
199           vcard_send, NULL,
200           vcard_receive, 0
201      },
202      [CNP_ATOM_image_png] = {
203           "image/png",
204           ELM_SEL_FORMAT_IMAGE,
205           image_converter,
206           NULL,
207           notify_handler_image,
208           0
209      },
210      [CNP_ATOM_image_jpeg] = {
211           "image/jpeg",
212           ELM_SEL_FORMAT_IMAGE,
213           image_converter,
214           NULL,
215           notify_handler_image,/* Raw image data is the same */
216           0
217      },
218      [CNP_ATOM_image_bmp] = {
219           "image/x-ms-bmp",
220           ELM_SEL_FORMAT_IMAGE,
221           image_converter,
222           NULL,
223           notify_handler_image,/* Raw image data is the same */
224           0
225      },
226      [CNP_ATOM_image_gif] = {
227           "image/gif",
228           ELM_SEL_FORMAT_IMAGE,
229           image_converter,
230           NULL,
231           notify_handler_image,/* Raw image data is the same */
232           0
233      },
234      [CNP_ATOM_image_tiff] = {
235           "image/tiff",
236           ELM_SEL_FORMAT_IMAGE,
237           image_converter,
238           NULL,
239           notify_handler_image,/* Raw image data is the same */
240           0
241      },
242      [CNP_ATOM_image_svg] = {
243           "image/svg+xml",
244           ELM_SEL_FORMAT_IMAGE,
245           image_converter,
246           NULL,
247           notify_handler_image,/* Raw image data is the same */
248           0
249      },
250      [CNP_ATOM_image_xpm] = {
251           "image/x-xpixmap",
252           ELM_SEL_FORMAT_IMAGE,
253           image_converter,
254           NULL,
255           notify_handler_image,/* Raw image data is the same */
256           0
257      },
258      [CNP_ATOM_image_tga] = {
259           "image/x-tga",
260           ELM_SEL_FORMAT_IMAGE,
261           image_converter,
262           NULL,
263           notify_handler_image,/* Raw image data is the same */
264           0
265      },
266      [CNP_ATOM_image_ppm] = {
267           "image/x-portable-pixmap",
268           ELM_SEL_FORMAT_IMAGE,
269           image_converter,
270           NULL,
271           notify_handler_image,/* Raw image data is the same */
272           0
273      },
274      [CNP_ATOM_text_html_utf8] = {
275           "text/html;charset=utf-8",
276           ELM_SEL_FORMAT_HTML,
277           html_converter,
278           NULL,
279           notify_handler_html,
280           0
281      },
282      [CNP_ATOM_text_html] = {
283           "text/html",
284           ELM_SEL_FORMAT_HTML,
285           html_converter,
286           NULL,
287           notify_handler_html, /* No encoding: Webkit only */
288           0
289      },
290      [CNP_ATOM_UTF8STRING] = {
291           "UTF8_STRING",
292           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
293           text_converter,
294           NULL,
295           notify_handler_text,
296           0
297      },
298      [CNP_ATOM_STRING] = {
299           "STRING",
300           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
301           text_converter,
302           NULL,
303           notify_handler_text,
304           0
305      },
306      [CNP_ATOM_TEXT] = {
307           "TEXT",
308           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
309           text_converter,
310           NULL,
311           NULL,
312           0
313      },
314      [CNP_ATOM_text_plain_utf8] = {
315           "text/plain;charset=utf-8",
316           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
317           text_converter,
318           NULL,
319           NULL,
320           0
321      },
322      [CNP_ATOM_text_plain] = {
323           "text/plain",
324           ELM_SEL_FORMAT_TEXT | ELM_SEL_FORMAT_MARKUP | ELM_SEL_FORMAT_HTML,
325           text_converter,
326           NULL,
327           NULL,
328           0
329      },
330 };
331
332 static Cnp_Selection selections[ELM_SEL_MAX] = {
333      ARRAYINIT(ELM_SEL_PRIMARY) {
334           .debug = "Primary",
335           .ecore_sel = ECORE_X_SELECTION_PRIMARY,
336           .set = ecore_x_selection_primary_set,
337           .clear = ecore_x_selection_primary_clear,
338           .request = ecore_x_selection_primary_request,
339      },
340      ARRAYINIT(ELM_SEL_SECONDARY) {
341           .debug = "Secondary",
342           .ecore_sel = ECORE_X_SELECTION_SECONDARY,
343           .set = ecore_x_selection_secondary_set,
344           .clear = ecore_x_selection_secondary_clear,
345           .request = ecore_x_selection_secondary_request,
346      },
347      ARRAYINIT(ELM_SEL_CLIPBOARD) {
348           .debug = "Clipboard",
349           .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
350           .set = ecore_x_selection_clipboard_set,
351           .clear = ecore_x_selection_clipboard_clear,
352           .request = ecore_x_selection_clipboard_request,
353      },
354      ARRAYINIT(ELM_SEL_XDND) {
355           .debug = "XDnD",
356           .ecore_sel = ECORE_X_SELECTION_XDND,
357           .request = ecore_x_selection_xdnd_request,
358      },
359 };
360
361 /* Data for DND in progress */
362 static Saved_Type savedtypes =  { NULL, NULL, 0, 0, 0, EINA_FALSE };
363
364 static void (*dragdonecb) (void *data, Evas_Object *obj) = NULL;
365 static void *dragdonedata = NULL;
366
367 static int _elm_cnp_init_count = 0;
368 /* FIXME: who left this out of XAtoms.h */
369 static Ecore_X_Atom clipboard_atom;
370
371 /**
372  * Drag & Drop functions
373  */
374
375 /* FIXME: Way too many globals */
376 static Eina_List *drops = NULL;
377 static Evas_Object *dragwin = NULL;
378 static int _dragx = 0, _dragy = 0;
379 static Ecore_Event_Handler *handler_pos = NULL;
380 static Ecore_Event_Handler *handler_drop = NULL;
381 static Ecore_Event_Handler *handler_enter = NULL;
382 static Ecore_Event_Handler *handler_status = NULL;
383
384 #endif
385
386 /* Stringshared, so I can just compare pointers later */
387 static const char *text_uri;
388
389 Eina_Bool
390 elm_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
391 {
392 #ifdef HAVE_ELEMENTARY_X
393    Evas_Object *top = elm_widget_top_get(widget);
394    Ecore_X_Window xwin;
395    Cnp_Selection *sel;
396
397    if (top) xwin = elm_win_xwindow_get(top);
398    else xwin = elm_win_xwindow_get(widget);
399    if (!xwin) return EINA_FALSE;
400    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
401    if (!_elm_cnp_init_count) _elm_cnp_init();
402    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
403      return elm_selection_clear(selection, widget);
404
405    sel = selections + selection;
406
407    sel->active = 1;
408    sel->widget = widget;
409
410    sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
411    sel->format = format;
412    sel->selbuf = selbuf ? strdup(selbuf) : NULL;
413
414    return EINA_TRUE;
415 #else
416    return EINA_FALSE;
417 #endif
418 }
419
420 Eina_Bool
421 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
422 {
423 #ifdef HAVE_ELEMENTARY_X
424    Cnp_Selection *sel;
425
426    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
427    if (!_elm_cnp_init_count) _elm_cnp_init();
428
429    sel = selections + selection;
430
431    /* No longer this selection: Consider it gone! */
432    if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
433
434    sel->active = 0;
435    sel->widget = NULL;
436    sel->clear();
437
438    return EINA_TRUE;
439 #else
440    return EINA_FALSE;
441 #endif
442 }
443
444 Eina_Bool
445 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
446                   Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
447 {
448 #ifdef HAVE_ELEMENTARY_X
449    Evas_Object *top;
450    Cnp_Selection *sel;
451
452    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
453    if (!_elm_cnp_init_count) _elm_cnp_init();
454
455    sel = selections + selection;
456    top = elm_widget_top_get(widget);
457    if (!top) return EINA_FALSE;
458
459    sel->requestformat = format;
460    sel->requestwidget = widget;
461    sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
462    sel->datacb = datacb;
463    sel->udata = udata;
464
465    return EINA_TRUE;
466 #else
467    return EINA_FALSE;
468 #endif
469 }
470
471 #ifdef HAVE_ELEMENTARY_X
472
473 static Eina_Bool
474 _elm_cnp_init(void)
475 {
476    int i;
477
478    if (_elm_cnp_init_count++) return EINA_TRUE;
479    for (i = 0; i < CNP_N_ATOMS; i++)
480      {
481         atoms[i].atom = ecore_x_atom_get(atoms[i].name);
482         ecore_x_selection_converter_atom_add(atoms[i].atom,
483                                              atoms[i].converter);
484      }
485    clipboard_atom = ecore_x_atom_get("CLIPBOARD");
486
487    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
488    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
489
490    text_uri = eina_stringshare_add("text/uri-list");
491    return EINA_TRUE;
492 }
493
494 static Eina_Bool
495 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
496 {
497    Ecore_X_Event_Selection_Clear *ev = event;
498    Cnp_Selection *sel;
499    int i;
500
501    for (i = 0; i < ELM_SEL_MAX; i++)
502      {
503         if (selections[i].ecore_sel == ev->selection) break;
504      }
505    cnp_debug("selection %d clear\n", i);
506    /* Not me... Don't care */
507    if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
508
509    sel = selections + i;
510    sel->active = 0;
511    sel->widget = NULL;
512    sel->selbuf = NULL;
513
514    return ECORE_CALLBACK_PASS_ON;
515 }
516
517
518 /*
519  * Response to a selection notify:
520  *      - So we have asked for the selection list.
521  *      - If it's the targets list, parse it, and fire of what we want,
522  *      else it's the data we want.
523  */
524 static Eina_Bool
525 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
526 {
527    Ecore_X_Event_Selection_Notify *ev = event;
528    Cnp_Selection *sel;
529    int i;
530
531    cnp_debug("selection notify callback: %d\n",ev->selection);
532    switch (ev->selection)
533      {
534       case ECORE_X_SELECTION_CLIPBOARD:
535          sel = selections + ELM_SEL_CLIPBOARD;
536          break;
537       case ECORE_X_SELECTION_PRIMARY:
538          sel = selections + ELM_SEL_PRIMARY;
539          break;
540       case ECORE_X_SELECTION_SECONDARY:
541          sel = selections + ELM_SEL_SECONDARY;
542          break;
543       case ECORE_X_SELECTION_XDND:
544          sel = selections + ELM_SEL_XDND;
545          break;
546       default:
547          return ECORE_CALLBACK_PASS_ON;
548      }
549    cnp_debug("Target is %s\n", ev->target);
550
551    for (i = 0; i < CNP_N_ATOMS; i++)
552      {
553         if (!strcmp(ev->target, atoms[i].name))
554           {
555              if (atoms[i].notify)
556                {
557                   cnp_debug("Found something: %s\n", atoms[i].name);
558                   atoms[i].notify(sel, ev);
559                }
560              else
561                {
562                   cnp_debug("Ignored: No handler!\n");
563                }
564           }
565      }
566
567    return ECORE_CALLBACK_PASS_ON;
568 }
569
570
571
572 static Eina_Bool
573 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
574 {
575    int i,count;
576    Ecore_X_Atom *aret;
577    Cnp_Selection *sel;
578
579    if (!data_ret) return EINA_FALSE;
580
581    sel = selections + *((int *)data);
582
583    for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
584      {
585         if (sel->format & atoms[i].formats) count++;
586      }
587
588    aret = malloc(sizeof(Ecore_X_Atom) * count);
589    for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
590      {
591         if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
592      }
593
594    *data_ret = aret;
595    if (typesize) *typesize = 32 /* urk */;
596    if (ttype) *ttype = ECORE_X_ATOM_ATOM;
597    if (size_ret) *size_ret = count;
598
599    return EINA_TRUE;
600 }
601
602 static Eina_Bool
603 image_converter(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret __UNUSED__, int *size_ret __UNUSED__, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
604 {
605    cnp_debug("Image converter called\n");
606    return EINA_TRUE;
607 }
608
609 static Eina_Bool
610 vcard_send(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
611 {
612    Cnp_Selection *sel;
613
614    cnp_debug("Vcard send called\n");
615
616    sel = selections + *((int *)data);
617
618    if (data_ret) *data_ret = strdup(sel->selbuf);
619    if (size_ret) *size_ret = strlen(sel->selbuf);
620
621    return EINA_TRUE;
622 }
623
624 static Eina_Bool
625 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
626 {
627    Ecore_X_Selection_Data *data;
628    char *p;
629
630    data = notify->data;
631    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
632    if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
633    else p = (char *)data->data;
634
635    if (!p) return EINA_TRUE;
636    cnp_debug("Got %s\n", p);
637    if (strncmp(p, "file://", 7))
638      {
639         if (*p != '/') return EINA_FALSE;
640      }
641
642    return EINA_TRUE;
643 }
644
645 /*
646  * Callback to handle a targets response on a selection request:
647  * So pick the format we'd like; and then request it.
648  */
649 static int
650 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
651 {
652    Ecore_X_Selection_Data_Targets *targets;
653    Ecore_X_Atom *atomlist;
654    Evas_Object *top;
655    int i, j;
656
657    targets = notify->data;
658    atomlist = (Ecore_X_Atom *)(targets->data.data);
659
660    for (j = 1; j < CNP_N_ATOMS; j++)
661      {
662         cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
663         if (!(atoms[j].formats & sel->requestformat)) continue;
664         for (i = 0; i < targets->data.length; i++)
665           {
666              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
667                {
668                   if ((j == CNP_ATOM_text_uri) ||
669                       (j == CNP_ATOM_text_urilist))
670                     {
671                       if(!is_uri_type_data(sel, notify)) continue;
672                     }
673                   cnp_debug("Atom %s matches\n",atoms[j].name);
674                   goto done;
675                }
676           }
677      }
678
679    cnp_debug("Couldn't find anything that matches\n");
680    return ECORE_CALLBACK_PASS_ON;
681
682 done:
683    top = elm_widget_top_get(sel->requestwidget);
684    if (!top) top = sel->requestwidget;
685    cnp_debug("Sending request for %s\n", atoms[j].name);
686    sel->request(elm_win_xwindow_get(top), atoms[j].name);
687
688    return ECORE_CALLBACK_PASS_ON;
689 }
690
691 static int
692 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
693 {
694    Ecore_X_Selection_Data_Targets *targets;
695    Ecore_X_Atom *atomlist;
696    Evas_Object *top;
697    int i,j;
698
699    targets = notify->data;
700    atomlist = (Ecore_X_Atom *)(targets->data.data);
701
702    /* Start from 1: Skip targets */
703    for (j = 1 ; j < CNP_N_ATOMS ; j ++)
704      {
705         if (!(atoms[j].formats & sel->requestformat)) continue;
706         for (i = 0 ; i < targets->data.length ; i ++)
707           {
708              if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
709                {
710                   /* Found a match: Use it */
711                   goto found;
712                }
713           }
714      }
715 found:
716    if (j == CNP_N_ATOMS)
717      {
718         cnp_debug("No matching type found\n");
719         return 0;
720      }
721
722    top = elm_widget_top_get(sel->requestwidget);
723    if (!top) return 0;
724
725    sel->request(elm_win_xwindow_get(top), atoms[j].name);
726    return 0;
727 }
728
729
730 static int
731 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
732 {
733    Ecore_X_Selection_Data *data;
734    char *str;
735
736    data = notify->data;
737
738    if (sel->datacb)
739      {
740         Elm_Selection_Data ddata;
741
742         ddata.x = ddata.y = 0;
743         ddata.format = ELM_SEL_FORMAT_TEXT;
744         ddata.data = data->data;
745         ddata.len = data->length;
746         sel->datacb(sel->udata, sel->widget, &ddata);
747         return 0;
748      }
749
750    cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
751    str = mark_up((char *)data->data, data->length, NULL);
752    cnp_debug("String is %s (from %s)\n", str, data->data);
753    elm_entry_entry_insert(sel->requestwidget, str);
754    free(str);
755    return 0;
756 }
757
758
759 /**
760  * So someone is pasting an image into my entry or widget...
761  */
762 static int
763 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
764 {
765    Ecore_X_Selection_Data *data;
766    Ecore_X_Selection_Data_Files *files;
767    char *p;
768
769    data = notify->data;
770    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
771    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
772      {
773         cnp_debug("got a files list\n");
774         files = notify->data;
775         if (files->num_files > 1)
776           {
777              /* Don't handle many items */
778              cnp_debug("more then one file: Bailing\n");
779              return 0;
780           }
781         p = files->files[0];
782      }
783    else
784      {
785         p = (char *)data->data;
786      }
787
788    if (!p)
789      {
790         cnp_debug("Couldn't find a file\n");
791         return 0;
792      }
793    cnp_debug("Got %s\n",p);
794    if (strncmp(p, "file://", 7))
795      {
796         /* Try and continue if it looks sane */
797         if (*p != '/') return 0;
798      }
799    else
800      {
801         p += strlen("file://");
802      }
803
804    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
805    if (savedtypes.textreq)
806      {
807         savedtypes.textreq = 0;
808         savedtypes.imgfile = strdup(p);
809      }
810    else
811      {
812         pasteimage_append(p, sel->requestwidget);
813      }
814    return 0;
815 }
816
817 /**
818  * Just receieved an vcard, either through cut and paste, or dnd.
819  */
820 static int
821 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
822 {
823    Dropable *dropable;
824    Eina_List *l;
825    Ecore_X_Selection_Data *data;
826
827    data = notify->data;
828    cnp_debug("vcard receive\n");
829
830    if (sel == (selections + ELM_SEL_XDND))
831      {
832         Elm_Selection_Data ddata;
833
834         cnp_debug("drag & drop\n");
835         /* FIXME: this needs to be generic: Used for all receives */
836         EINA_LIST_FOREACH(drops, l, dropable)
837           {
838              if (dropable->obj == sel->requestwidget) break;
839           }
840         if (!dropable)
841           {
842              cnp_debug("Unable to find drop object");
843              ecore_x_dnd_send_finished();
844              return 0;
845           }
846         dropable = eina_list_data_get(l);
847         ddata.x = savedtypes.x;
848         ddata.y = savedtypes.y;
849         ddata.format = ELM_SEL_FORMAT_VCARD;
850         ddata.data = data->data;
851         ddata.len = data->length;
852         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
853         ecore_x_dnd_send_finished();
854      }
855    else if (sel->datacb)
856      {
857         Elm_Selection_Data ddata;
858         ddata.x = ddata.y = 0;
859         ddata.format = ELM_SEL_FORMAT_IMAGE;
860         ddata.data = data->data;
861         ddata.len = data->length;
862         sel->datacb(sel->udata, sel->widget, &ddata);
863      }
864    else
865      {
866         cnp_debug("Paste request\n");
867      }
868
869    return 0;
870
871 }
872
873
874 static int
875 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
876 {
877    Ecore_X_Selection_Data *data;
878    Tmp_Info *tmp;
879
880    cnp_debug("got a png (or a jpeg)!\n");
881    data = notify->data;
882
883    cnp_debug("Size if %d\n", data->length);
884
885    if (sel->datacb)
886      {
887         Elm_Selection_Data ddata;
888
889         ddata.x = ddata.y = 0;
890         ddata.format = ELM_SEL_FORMAT_IMAGE;
891         ddata.data = data->data;
892         ddata.len = data->length;
893         sel->datacb(sel->udata, sel->widget, &ddata);
894         return 0;
895      }
896
897    /* generate tmp name */
898    tmp = elm_cnp_tempfile_create(data->length);
899    memcpy(tmp->map, data->data, data->length);
900    munmap(tmp->map,data->length);
901
902    /* FIXME: Add to paste image data to clean up */
903    pasteimage_append(tmp->filename, sel->requestwidget);
904
905    tmpinfo_free(tmp);
906    return 0;
907 }
908
909
910 /**
911  *    Warning: Generic text/html can';t handle it sanely.
912  *    Firefox sends ucs2 (i think).
913  *       chrome sends utf8... blerg
914  */
915 static int
916 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
917 {
918    Ecore_X_Selection_Data *data;
919
920    cnp_debug("Got some HTML: Checking encoding is useful\n");
921    data = notify->data;
922
923    if (sel->datacb)
924      {
925         Elm_Selection_Data ddata;
926         ddata.x = ddata.y = 0;
927         ddata.format = ELM_SEL_FORMAT_HTML;
928         ddata.data = data->data;
929         ddata.len = data->length;
930         sel->datacb(sel->udata, sel->widget, &ddata);
931         return 0;
932      }
933
934    char *stripstr = NULL;
935    stripstr = malloc(sizeof(char) * (data->length + 1));
936    strncpy(stripstr, (char *)data->data, data->length);
937    stripstr[data->length] = '\0';
938    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
939    elm_entry_entry_insert(sel->requestwidget, stripstr);
940    free(stripstr);
941    return 0;
942 }
943
944
945 static Eina_Bool
946 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
947 {
948    Cnp_Selection *sel;
949
950    cnp_debug("text converter\n");
951    sel = selections + *((int *)data);
952    if (!sel->active) return EINA_TRUE;
953
954    if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
955        (sel->format & ELM_SEL_FORMAT_HTML))
956      {
957         *data_ret = remove_tags(sel->selbuf, size_ret);
958      }
959    else if (sel->format & ELM_SEL_FORMAT_TEXT)
960      {
961         *data_ret = strdup(sel->selbuf);
962         *size_ret = strlen(sel->selbuf);
963      }
964    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
965      {
966         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
967         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
968         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
969         if (!*data_ret) *data_ret = strdup("No file");
970         else *data_ret = strdup(*data_ret);
971         *size_ret = strlen(*data_ret);
972      }
973    return EINA_TRUE;
974 }
975
976 static Eina_Bool
977 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
978 {
979    Cnp_Selection *sel;
980
981    sel = selections + *((int *)data);
982    if (data_ret) *data_ret = strdup(sel->selbuf);
983    if (size_ret) *size_ret = strlen(sel->selbuf);
984
985    return EINA_TRUE;
986 }
987
988
989 static Eina_Bool
990 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
991 {
992    Cnp_Selection *sel;
993
994    sel = selections + *(int *)data;
995    if (data_ret) *data_ret = strdup(sel->selbuf);
996    if (size_ret) *size_ret = strlen(sel->selbuf);
997
998    return EINA_TRUE;
999 }
1000
1001 static Eina_Bool
1002 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1003 {
1004    Cnp_Selection *sel;
1005    sel = selections + *((int *)data);
1006    cnp_debug("Uri converter\n");
1007    if (data_ret) *data_ret = strdup(sel->selbuf);
1008    if (size_ret) *size_ret = strlen(sel->selbuf);
1009    return EINA_TRUE;
1010 }
1011
1012 static Eina_Bool
1013 pasteimage_append(char *file, Evas_Object *entry)
1014 {
1015    char *entrytag;
1016    int len;
1017    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1018
1019    if (!file) return EINA_FALSE;
1020    if (!entry) return EINA_FALSE;
1021
1022    len = strlen(tagstring)+strlen(file);
1023
1024    entrytag = alloca(len + 1);
1025    snprintf(entrytag, len + 1, tagstring, file);
1026    elm_entry_entry_insert(entry, entrytag);
1027
1028    return EINA_TRUE;
1029 }
1030
1031 static char *
1032 remove_tags(const char *p, int *len)
1033 {
1034    char *q,*ret;
1035    int i;
1036    if (!p) return NULL;
1037
1038    q = malloc(strlen(p) + 1);
1039    if (!q) return NULL;
1040    ret = q;
1041
1042    while (*p)
1043      {
1044         if ((*p != '<') && (*p != '&')) *q++ = *p++;
1045         else if (*p == '<')
1046           {
1047              if ((p[1] == 'b') && (p[2] == 'r') &&
1048                  ((p[3] == ' ') || (p[3] == '/') || (p[3] == '>')))
1049                *q++ = '\n';
1050              while ((*p) && (*p != '>')) p++;
1051              p++;
1052           }
1053         else if (*p == '&')
1054           {
1055              p++;
1056              for (i = 0 ; i < N_ESCAPES ; i++)
1057                {
1058                   if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape)))
1059                     {
1060                        p += strlen(escapes[i].escape);
1061                        *q = escapes[i].value;
1062                        q++;
1063                        break;
1064                     }
1065                }
1066              if (i == N_ESCAPES) *q ++= '&';
1067           }
1068      }
1069    *q = 0;
1070    if (len) *len = q - ret;
1071    return ret;
1072 }
1073
1074 /* Mark up */
1075 static char *
1076 mark_up(const char *start, int inlen, int *lenp)
1077 {
1078    int l, i;
1079    const char *p;
1080    char *q, *ret;
1081    const char *endp = NULL;
1082
1083    if (!start) return NULL;
1084    if (inlen >= 0) endp = start + inlen;
1085    /* First pass: Count characters */
1086    for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1087      {
1088         for (i = 0 ; i < N_ESCAPES ; i ++)
1089           {
1090              if (*p == escapes[i].value)
1091                {
1092                   l += strlen(escapes[i].escape);
1093                   break;
1094                }
1095           }
1096         if (i == N_ESCAPES) l++;
1097      }
1098
1099    q = ret = malloc(l + 1);
1100
1101    /* Second pass: Change characters */
1102    for (p = start; ((!endp) || (p < endp)) && (*p); )
1103      {
1104         for (i = 0; i < N_ESCAPES; i++)
1105           {
1106              if (*p == escapes[i].value)
1107                {
1108                   strcpy(q, escapes[i].escape);
1109                   q += strlen(escapes[i].escape);
1110                   p ++;
1111                   break;
1112                }
1113           }
1114         if (i == N_ESCAPES) *q++ = *p++;
1115      }
1116    *q = 0;
1117
1118    if (lenp) *lenp = l;
1119    return ret;
1120 }
1121
1122
1123 static Eina_Bool
1124 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1125 {
1126    Ecore_X_Event_Xdnd_Enter *enter = ev;
1127    int i;
1128
1129    /* Skip it */
1130    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1131
1132    cnp_debug("Types\n");
1133    savedtypes.ntypes = enter->num_types;
1134    if (savedtypes.types) free(savedtypes.types);
1135    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1136    if (!savedtypes.types) return EINA_FALSE;
1137
1138    for (i = 0; i < enter->num_types; i++)
1139      {
1140         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1141         cnp_debug("Type is %s %p %p\n", enter->types[i],
1142                   savedtypes.types[i],text_uri);
1143         if (savedtypes.types[i] == text_uri)
1144           {
1145              /* Request it, so we know what it is */
1146              cnp_debug("Sending uri request\n");
1147              savedtypes.textreq = 1;
1148              if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1149              savedtypes.imgfile = NULL;
1150              ecore_x_selection_xdnd_request(enter->win, text_uri);
1151           }
1152      }
1153
1154    /* FIXME: Find an object and make it current */
1155    return EINA_TRUE;
1156 }
1157
1158 static Eina_Bool
1159 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1160 {
1161    struct _Ecore_X_Event_Xdnd_Drop *drop;
1162    Dropable *dropable;
1163    Eina_List *l;
1164    Ecore_Evas *ee;
1165    Ecore_X_Window xwin;
1166    Elm_Selection_Data ddata;
1167    int x, y, w, h;
1168    int i, j;
1169
1170    drop = ev;
1171
1172    // check we still have something to drop
1173    if (!drops) return EINA_TRUE;
1174
1175    /* Find any widget in our window; then work out geometry rel to our window */
1176    for (l = drops; l; l = l->next)
1177      {
1178         dropable = l->data;
1179         xwin = (Ecore_X_Window)ecore_evas_window_get
1180            (ecore_evas_ecore_evas_get(evas_object_evas_get
1181                                       (dropable->obj)));
1182         if (xwin == drop->win) break;
1183      }
1184    /* didn't find a window */
1185    if (!l) return EINA_TRUE;
1186
1187    /* Calculate real (widget relative) position */
1188    // - window position
1189    // - widget position
1190    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1191    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1192    savedtypes.x = drop->position.x - x;
1193    savedtypes.y = drop->position.y - y;
1194
1195    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1196
1197    for (; l; l = l->next)
1198      {
1199         dropable = l->data;
1200         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1201         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1202             (savedtypes.x < x + w) && (savedtypes.y < y + h))
1203           break; /* found! */
1204      }
1205
1206    if (!l) return EINA_TRUE; /* didn't find one */
1207
1208    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1209    savedtypes.x -= x;
1210    savedtypes.y -= y;
1211
1212    /* Find our type from the previous list */
1213    for (i = 0; i < CNP_N_ATOMS; i++)
1214      {
1215         for (j = 0; j < savedtypes.ntypes; j++)
1216           {
1217              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1218           }
1219      }
1220
1221    cnp_debug("Didn't find a target\n");
1222    return EINA_TRUE;
1223
1224 found:
1225    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1226    cnp_debug("0x%x\n",xwin);
1227
1228    if (i == CNP_ATOM_text_urilist)
1229      {
1230         cnp_debug("We found a URI... (%scached) %s\n",
1231                   savedtypes.imgfile ? "" : "not ",
1232                   savedtypes.imgfile);
1233         if (savedtypes.imgfile)
1234           {
1235              char *entrytag;
1236              static const char *tagstring = "<item absize=240x180 href="
1237                 "file://%s></item>";
1238              ddata.x = savedtypes.x;
1239              ddata.y = savedtypes.y;
1240
1241              /* If it's markup that also supports images */
1242              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1243                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
1244                {
1245                   int len;
1246                   ddata.format = ELM_SEL_FORMAT_MARKUP;
1247
1248                   len = strlen(tagstring) + strlen(savedtypes.imgfile);
1249                   entrytag = alloca(len + 1);
1250                   snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1251                   ddata.data = entrytag;
1252                   cnp_debug("Insert %s\n", (char *)ddata.data);
1253                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1254                   ecore_x_dnd_send_finished();
1255                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1256                   savedtypes.imgfile = NULL;
1257
1258                   return EINA_TRUE;
1259                }
1260              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1261                {
1262                   cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1263                   ddata.format = ELM_SEL_FORMAT_IMAGE;
1264                   ddata.data = (char *)savedtypes.imgfile;
1265                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1266                   ecore_x_dnd_send_finished();
1267                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1268                   savedtypes.imgfile = NULL;
1269
1270                   return EINA_TRUE;
1271                }
1272              else
1273                {
1274                   cnp_debug("Item doesn't support images... passing\n");
1275                   return EINA_TRUE;
1276                }
1277           }
1278         else if (savedtypes.textreq)
1279           {
1280              /* Already asked: Pretend we asked now, and paste immediately when
1281               * it comes in */
1282              savedtypes.textreq = 0;
1283              ecore_x_dnd_send_finished();
1284              return EINA_TRUE;
1285           }
1286      }
1287
1288    cnp_debug("doing a request then\n");
1289    selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1290    selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1291    selections[ELM_SEL_XDND].active = EINA_TRUE;
1292
1293    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1294
1295    return EINA_TRUE;
1296 }
1297 static Eina_Bool
1298 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1299 {
1300    struct _Ecore_X_Event_Xdnd_Position *pos;
1301    Ecore_X_Rectangle rect;
1302
1303    pos = ev;
1304
1305    /* Need to send a status back */
1306    /* FIXME: Should check I can drop here */
1307    /* FIXME: Should highlight widget */
1308    rect.x = pos->position.x - 5;
1309    rect.y = pos->position.y - 5;
1310    rect.width = 10;
1311    rect.height = 10;
1312    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1313
1314    return EINA_TRUE;
1315 }
1316
1317 /**
1318  * When dragging this is callback response from the destination.
1319  * The important thing we care about: Can we drop; thus update cursor
1320  * appropriately.
1321  */
1322 static Eina_Bool
1323 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1324 {
1325    struct _Ecore_X_Event_Xdnd_Status *status = ev;
1326
1327    if (!status) return EINA_TRUE;
1328
1329    /* Only thing we care about: will accept */
1330    if (status->will_accept)
1331      {
1332         cnp_debug("Will accept\n");
1333      }
1334    else
1335      { /* Won't accept */
1336         cnp_debug("Won't accept accept\n");
1337      }
1338    return EINA_TRUE;
1339 }
1340
1341 /**
1342  * Add a widget as drop target.
1343  */
1344 Eina_Bool
1345 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1346 {
1347    Dropable *drop;
1348    Ecore_X_Window xwin;
1349    Eina_List *item;
1350    int first;
1351
1352    if (!obj) return EINA_FALSE;
1353    if (!_elm_cnp_init_count) _elm_cnp_init();
1354
1355    /* Is this the first? */
1356    first = (!drops) ? 1 : 0;
1357
1358    EINA_LIST_FOREACH(drops, item, drop)
1359      {
1360         if (drop->obj == obj)
1361           {
1362              /* Update: Not a new one */
1363              drop->dropcb = dropcb;
1364              drop->cbdata = cbdata;
1365              drop->types = format;
1366              return EINA_TRUE;
1367           }
1368      }
1369
1370    /* Create new drop */
1371    drop = calloc(1, sizeof(Dropable));
1372    if (!drop) return EINA_FALSE;
1373    /* FIXME: Check for eina's deranged error method */
1374    drops = eina_list_append(drops, drop);
1375
1376    if (!drops/* || or other error */)
1377      {
1378         free(drop);
1379         return EINA_FALSE;
1380      }
1381    drop->dropcb = dropcb;
1382    drop->cbdata = cbdata;
1383    drop->types = format;
1384    drop->obj = obj;
1385
1386    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1387                                   /* I love C and varargs */
1388                                   (Evas_Object_Event_Cb)elm_drop_target_del,
1389                                   obj);
1390    /* FIXME: Handle resizes */
1391
1392    /* If not the first: We're done */
1393    if (!first) return EINA_TRUE;
1394
1395    xwin = (Ecore_X_Window)ecore_evas_window_get
1396       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1397
1398    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1399
1400    cnp_debug("Adding drop target calls\n");
1401    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1402                                            _dnd_enter, NULL);
1403    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1404                                          _dnd_position, NULL);
1405    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1406                                           _dnd_drop, NULL);
1407
1408    return EINA_TRUE;
1409 }
1410
1411 Eina_Bool
1412 elm_drop_target_del(Evas_Object *obj)
1413 {
1414    Dropable *drop,*del;
1415    Eina_List *item;
1416    Ecore_X_Window xwin;
1417
1418    del = NULL;
1419    EINA_LIST_FOREACH(drops, item, drop)
1420      {
1421         if (drop->obj == obj)
1422           {
1423              drops = eina_list_remove_list(drops, item);
1424              del = drop;
1425              break;
1426           }
1427      }
1428    if (!del) return EINA_FALSE;
1429
1430    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1431                                   (Evas_Object_Event_Cb)elm_drop_target_del);
1432    free(drop);
1433    /* If still drops there: All fine.. continue */
1434    if (drops) return EINA_TRUE;
1435
1436    cnp_debug("Disabling DND\n");
1437    xwin = (Ecore_X_Window)ecore_evas_window_get
1438       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1439    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1440
1441    ecore_event_handler_del(handler_pos);
1442    ecore_event_handler_del(handler_drop);
1443    ecore_event_handler_del(handler_enter);
1444
1445    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1446    savedtypes.imgfile = NULL;
1447
1448    return EINA_TRUE;
1449 }
1450
1451
1452 static void
1453 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1454 {
1455    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1456    ecore_x_dnd_drop();
1457    if (dragdonecb)
1458      {
1459         dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1460         dragdonecb = NULL;
1461      }
1462    if (dragwin)
1463      {
1464         evas_object_del(dragwin);
1465         dragwin = NULL;
1466      }
1467 }
1468
1469 static void
1470 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1471 {
1472    evas_object_move(dragwin,
1473                     pos->position.x - _dragx,
1474                     pos->position.y - _dragy);
1475 }
1476
1477
1478 Eina_Bool
1479 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1480 {
1481    Ecore_X_Window xwin;
1482    Cnp_Selection *sel;
1483    Elm_Sel_Type xdnd = ELM_SEL_XDND;
1484    Ecore_Evas *ee;
1485    int x, y, x2, y2, x3, y3;
1486    Evas_Object *icon;
1487    int w, h;
1488
1489    if (!_elm_cnp_init_count) _elm_cnp_init();
1490
1491    xwin = elm_win_xwindow_get(obj);
1492
1493    cnp_debug("starting drag...\n");
1494
1495    ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1496    sel = selections + ELM_SEL_XDND;
1497    sel->active = 1;
1498    sel->widget = obj;
1499    sel->format = format;
1500    sel->selbuf = data ? strdup(data) : NULL;
1501    dragdonecb = dragdone;
1502    dragdonedata = donecbdata;
1503
1504    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1505    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1506    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1507                                   _drag_mouse_up, NULL);
1508
1509    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1510                                             _dnd_status, NULL);
1511
1512    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1513    elm_win_override_set(dragwin, 1);
1514
1515    /* FIXME: Images only */
1516    icon = elm_icon_add(dragwin);
1517    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1518    elm_win_resize_object_add(dragwin,icon);
1519    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1520    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1521
1522    /* Position subwindow appropriately */
1523    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1524    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1525    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1526    x += x2;
1527    y += y2;
1528    evas_object_move(dragwin, x, y);
1529    evas_object_resize(icon, w, h);
1530    evas_object_resize(dragwin, w, h);
1531
1532    evas_object_show(icon);
1533    evas_object_show(dragwin);
1534
1535    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1536    _dragx = x3 - x2;
1537    _dragy = y3 - y2;
1538
1539    return EINA_TRUE;
1540 }
1541
1542 static Tmp_Info *
1543 elm_cnp_tempfile_create(int size)
1544 {
1545    Tmp_Info *info;
1546    const char *tmppath;
1547    int len;
1548
1549    info = malloc(sizeof(Tmp_Info));
1550    if (!info) return NULL;
1551
1552    tmppath = getenv("TMP");
1553    if (!tmppath) tmppath = P_tmpdir;
1554    if (!tmppath) tmppath = "/tmp";
1555    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1556    if (len < 0)
1557      {
1558         free(info);
1559         return NULL;
1560      }
1561    len++;
1562    info->filename = malloc(len);
1563    if (!info->filename)
1564      {
1565         free(info);
1566         return NULL;
1567      }
1568    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1569
1570    info->fd = mkstemp(info->filename);
1571
1572 # ifdef __linux__
1573      {
1574         char *tmp;
1575         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1576         long pid;
1577
1578         pid = (long)getpid();
1579         /* Use pid instead of /proc/self: That way if can be passed around */
1580         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1581         len++;
1582         tmp = malloc(len);
1583         if (tmp)
1584           {
1585              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1586              unlink(info->filename);
1587              free(info->filename);
1588              info->filename = tmp;
1589           }
1590      }
1591 # endif
1592
1593    cnp_debug("filename is %s\n", info->filename);
1594    if (size < 1)
1595      {
1596         /* Set map to NULL and return */
1597         info->map = NULL;
1598         info->len = 0;
1599         return info;
1600      }
1601
1602    /* Map it in */
1603    if (ftruncate(info->fd, size))
1604      {
1605         perror("ftruncate");
1606         info->map = NULL;
1607         info->len = 0;
1608         return info;
1609      }
1610
1611    eina_mmap_safety_enabled_set(EINA_TRUE);
1612    
1613    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1614    if (info->map == MAP_FAILED)
1615      {
1616         perror("mmap");
1617         info->map = NULL;
1618         info->len = 0;
1619      }
1620
1621    return info;
1622 }
1623
1624
1625 static int
1626 tmpinfo_free(Tmp_Info *info)
1627 {
1628    if (!info) return 0;
1629    free(info->filename);
1630    free(info);
1631    return 0;
1632 }
1633
1634 #else
1635 /* Stubs for windows */
1636 Eina_Bool
1637 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1638 {
1639    return EINA_FALSE;
1640 }
1641
1642 Eina_Bool
1643 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1644 {
1645    return EINA_FALSE;
1646 }
1647
1648 Eina_Bool
1649 elm_drop_target_del(Evas_Object *o)
1650 {
1651    return EINA_TRUE;
1652 }
1653 #endif
1654
1655 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/