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