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