elementary: support environment without sys/mman.h.
[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) -1, // everything
172           targets_converter,
173           response_handler_targets,
174           notify_handler_targets,
175           0
176      },
177      [CNP_ATOM_ATOM] = {
178           "ATOM",
179           (Elm_Sel_Format) -1, // everything
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_MAX] = {
346      ARRAYINIT(ELM_SEL_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_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_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_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_selection_set(Elm_Sel_Type selection, Evas_Object *widget, Elm_Sel_Format format, const char *selbuf)
412 {
413 #ifdef HAVE_ELEMENTARY_X
414    Evas_Object *top = elm_widget_top_get(widget);
415    Ecore_X_Window xwin;
416    Cnp_Selection *sel;
417
418    if (top) xwin = elm_win_xwindow_get(top);
419    else xwin = elm_win_xwindow_get(widget);
420    if (!xwin) return EINA_FALSE;
421    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
422    if (!_elm_cnp_init_count) _elm_cnp_init();
423    if ((!selbuf) && (format != ELM_SEL_FORMAT_IMAGE))
424      return elm_selection_clear(selection, widget);
425
426    sel = selections + selection;
427
428    sel->active = 1;
429    sel->widget = widget;
430
431    sel->set(xwin, &selection, sizeof(Elm_Sel_Type));
432    sel->format = format;
433    sel->selbuf = selbuf ? strdup(selbuf) : NULL;
434
435    return EINA_TRUE;
436 #else
437    return EINA_FALSE;
438 #endif
439 }
440
441 Eina_Bool
442 elm_selection_clear(Elm_Sel_Type selection, Evas_Object *widget)
443 {
444 #ifdef HAVE_ELEMENTARY_X
445    Cnp_Selection *sel;
446
447    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
448    if (!_elm_cnp_init_count) _elm_cnp_init();
449
450    sel = selections + selection;
451
452    /* No longer this selection: Consider it gone! */
453    if ((!sel->active) || (sel->widget != widget)) return EINA_TRUE;
454
455    sel->active = 0;
456    sel->widget = NULL;
457    sel->clear();
458
459    return EINA_TRUE;
460 #else
461    return EINA_FALSE;
462 #endif
463 }
464
465 Eina_Bool
466 elm_selection_get(Elm_Sel_Type selection, Elm_Sel_Format format,
467                   Evas_Object *widget, Elm_Drop_Cb datacb, void *udata)
468 {
469 #ifdef HAVE_ELEMENTARY_X
470    Evas_Object *top;
471    Cnp_Selection *sel;
472
473    if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
474    if (!_elm_cnp_init_count) _elm_cnp_init();
475
476    sel = selections + selection;
477    top = elm_widget_top_get(widget);
478    if (!top) return EINA_FALSE;
479
480    sel->requestformat = format;
481    sel->requestwidget = widget;
482    sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_TARGETS);
483    sel->datacb = datacb;
484    sel->udata = udata;
485
486    return EINA_TRUE;
487 #else
488    return EINA_FALSE;
489 #endif
490 }
491
492 #ifdef HAVE_ELEMENTARY_X
493
494 static Eina_Bool
495 _elm_cnp_init(void)
496 {
497    int i;
498
499    if (_elm_cnp_init_count++) return EINA_TRUE;
500    for (i = 0; i < CNP_N_ATOMS; i++)
501      {
502         atoms[i].atom = ecore_x_atom_get(atoms[i].name);
503         ecore_x_selection_converter_atom_add(atoms[i].atom,
504                                              atoms[i].converter);
505      }
506
507    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear, NULL);
508    ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY, selection_notify, NULL);
509
510    text_uri = eina_stringshare_add("text/uri-list");
511    return EINA_TRUE;
512 }
513
514 static Eina_Bool
515 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event)
516 {
517    Ecore_X_Event_Selection_Clear *ev = event;
518    Cnp_Selection *sel;
519    int i;
520
521    for (i = 0; i < ELM_SEL_MAX; i++)
522      {
523         if (selections[i].ecore_sel == ev->selection) break;
524      }
525    cnp_debug("selection %d clear\n", i);
526    /* Not me... Don't care */
527    if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
528
529    sel = selections + i;
530    sel->active = 0;
531    sel->widget = NULL;
532    sel->selbuf = NULL;
533
534    return ECORE_CALLBACK_PASS_ON;
535 }
536
537
538 /*
539  * Response to a selection notify:
540  *      - So we have asked for the selection list.
541  *      - If it's the targets list, parse it, and fire of what we want,
542  *      else it's the data we want.
543  */
544 static Eina_Bool
545 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event)
546 {
547    Ecore_X_Event_Selection_Notify *ev = event;
548    Cnp_Selection *sel;
549    int i;
550
551    cnp_debug("selection notify callback: %d\n",ev->selection);
552    switch (ev->selection)
553      {
554       case ECORE_X_SELECTION_CLIPBOARD:
555          sel = selections + ELM_SEL_CLIPBOARD;
556          break;
557       case ECORE_X_SELECTION_PRIMARY:
558          sel = selections + ELM_SEL_PRIMARY;
559          break;
560       case ECORE_X_SELECTION_SECONDARY:
561          sel = selections + ELM_SEL_SECONDARY;
562          break;
563       case ECORE_X_SELECTION_XDND:
564          sel = selections + ELM_SEL_XDND;
565          break;
566       default:
567          return ECORE_CALLBACK_PASS_ON;
568      }
569    cnp_debug("Target is %s\n", ev->target);
570
571    for (i = 0; i < CNP_N_ATOMS; i++)
572      {
573         if (!strcmp(ev->target, atoms[i].name))
574           {
575              if (atoms[i].notify)
576                {
577                   cnp_debug("Found something: %s\n", atoms[i].name);
578                   atoms[i].notify(sel, ev);
579                }
580              else
581                {
582                   cnp_debug("Ignored: No handler!\n");
583                }
584           }
585      }
586
587    return ECORE_CALLBACK_PASS_ON;
588 }
589
590
591
592 static Eina_Bool
593 targets_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize)
594 {
595    int i,count;
596    Ecore_X_Atom *aret;
597    Cnp_Selection *sel;
598
599    if (!data_ret) return EINA_FALSE;
600
601    if (size != sizeof(int))
602      {
603         if (data_ret)
604           {
605              *data_ret = malloc(size * sizeof(char) + 1);
606              memcpy(*data_ret, data, size);
607              ((char**)(data_ret))[0][size] = 0;
608           }
609         if (size_ret) *size_ret = size;
610         return EINA_TRUE;
611      }
612
613    sel = selections + *((int *)data);
614
615    for (i = 0, count = 0; i < CNP_N_ATOMS ; i++)
616      {
617         if (sel->format & atoms[i].formats) count++;
618      }
619
620    aret = malloc(sizeof(Ecore_X_Atom) * count);
621    for (i = 0, count = 0; i < CNP_N_ATOMS; i++)
622      {
623         if (sel->format & atoms[i].formats) aret[count ++] = atoms[i].atom;
624      }
625
626    *data_ret = aret;
627    if (typesize) *typesize = 32 /* urk */;
628    if (ttype) *ttype = ECORE_X_ATOM_ATOM;
629    if (size_ret) *size_ret = count;
630
631    return EINA_TRUE;
632 }
633
634 static Eina_Bool
635 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__)
636 {
637    cnp_debug("Image converter called\n");
638    return EINA_TRUE;
639 }
640
641 static Eina_Bool
642 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__)
643 {
644    Cnp_Selection *sel;
645
646    cnp_debug("Vcard send called\n");
647
648    sel = selections + *((int *)data);
649
650    if (data_ret) *data_ret = strdup(sel->selbuf);
651    if (size_ret) *size_ret = strlen(sel->selbuf);
652
653    return EINA_TRUE;
654 }
655
656 static Eina_Bool
657 is_uri_type_data(Cnp_Selection *sel __UNUSED__, Ecore_X_Event_Selection_Notify *notify)
658 {
659    Ecore_X_Selection_Data *data;
660    char *p;
661
662    data = notify->data;
663    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
664    if (data->content == ECORE_X_SELECTION_CONTENT_FILES) return EINA_TRUE;
665    else p = (char *)data->data;
666
667    if (!p) return EINA_TRUE;
668    cnp_debug("Got %s\n", p);
669    if (strncmp(p, "file://", 7))
670      {
671         if (*p != '/') return EINA_FALSE;
672      }
673
674    return EINA_TRUE;
675 }
676
677 /*
678  * Callback to handle a targets response on a selection request:
679  * So pick the format we'd like; and then request it.
680  */
681 static int
682 notify_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
683 {
684    Ecore_X_Selection_Data_Targets *targets;
685    Ecore_X_Atom *atomlist;
686    Evas_Object *top;
687    int i, j;
688
689    targets = notify->data;
690    atomlist = (Ecore_X_Atom *)(targets->data.data);
691
692    for (j = (CNP_ATOM_LISTING_ATOMS+1); j < CNP_N_ATOMS; j++)
693      {
694         cnp_debug("\t%s %d\n", atoms[j].name, atoms[j].atom);
695         if (!(atoms[j].formats & sel->requestformat)) continue;
696         for (i = 0; i < targets->data.length; i++)
697           {
698              if ((atoms[j].atom == atomlist[i]) && (atoms[j].notify))
699                {
700                   if ((j == CNP_ATOM_text_uri) ||
701                       (j == CNP_ATOM_text_urilist))
702                     {
703                       if(!is_uri_type_data(sel, notify)) continue;
704                     }
705                   cnp_debug("Atom %s matches\n",atoms[j].name);
706                   goto done;
707                }
708           }
709      }
710
711    cnp_debug("Couldn't find anything that matches\n");
712    return ECORE_CALLBACK_PASS_ON;
713
714 done:
715    top = elm_widget_top_get(sel->requestwidget);
716    if (!top) top = sel->requestwidget;
717    cnp_debug("Sending request for %s\n", atoms[j].name);
718    sel->request(elm_win_xwindow_get(top), atoms[j].name);
719
720    return ECORE_CALLBACK_PASS_ON;
721 }
722
723 static int
724 response_handler_targets(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
725 {
726    Ecore_X_Selection_Data_Targets *targets;
727    Ecore_X_Atom *atomlist;
728    Evas_Object *top;
729    int i,j;
730
731    targets = notify->data;
732    atomlist = (Ecore_X_Atom *)(targets->data.data);
733
734    /* Start from 1: Skip targets */
735    for (j = 1 ; j < CNP_N_ATOMS ; j ++)
736      {
737         if (!(atoms[j].formats & sel->requestformat)) continue;
738         for (i = 0 ; i < targets->data.length ; i ++)
739           {
740              if ((atoms[j].atom == atomlist[i]) && (atoms[j].response))
741                {
742                   /* Found a match: Use it */
743                   goto found;
744                }
745           }
746      }
747 found:
748    if (j == CNP_N_ATOMS)
749      {
750         cnp_debug("No matching type found\n");
751         return 0;
752      }
753
754    top = elm_widget_top_get(sel->requestwidget);
755    if (!top) return 0;
756
757    sel->request(elm_win_xwindow_get(top), atoms[j].name);
758    return 0;
759 }
760
761
762 static int
763 notify_handler_text(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
764 {
765    Ecore_X_Selection_Data *data;
766    char *str;
767
768    data = notify->data;
769
770    if (sel->datacb)
771      {
772         Elm_Selection_Data ddata;
773
774         ddata.x = ddata.y = 0;
775         ddata.format = ELM_SEL_FORMAT_TEXT;
776         ddata.data = data->data;
777         ddata.len = data->length;
778         sel->datacb(sel->udata, sel->widget, &ddata);
779         return 0;
780      }
781
782    cnp_debug("Notify handler text %d %d %p\n", data->format,data->length, data->data);
783    str = mark_up((char *)data->data, data->length, NULL);
784    cnp_debug("String is %s (from %s)\n", str, data->data);
785    _elm_entry_entry_paste(sel->requestwidget, str);
786    free(str);
787    return 0;
788 }
789
790
791 /**
792  * So someone is pasting an image into my entry or widget...
793  */
794 static int
795 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
796 {
797    Ecore_X_Selection_Data *data;
798    Ecore_X_Selection_Data_Files *files;
799    char *p, *stripstr;
800
801    data = notify->data;
802    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
803    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
804      {
805         cnp_debug("got a files list\n");
806         files = notify->data;
807         if (files->num_files > 1)
808           {
809              /* Don't handle many items */
810              cnp_debug("more then one file: Bailing\n");
811              return 0;
812           }
813         stripstr = p = strdup(files->files[0]);
814      }
815    else
816      {
817         stripstr = p = malloc(data->length * sizeof(char) + 1);
818         memcpy(stripstr, data->data, data->length);
819         stripstr[data->length] = 0;
820      }
821
822    if (!p)
823      {
824         cnp_debug("Couldn't find a file\n");
825         return 0;
826      }
827    cnp_debug("Got %s\n",p);
828    if (strncmp(p, "file://", 7))
829      {
830         /* Try and continue if it looks sane */
831         if (*p != '/')
832           {
833              free(p);
834              return 0;
835           }
836      }
837    else
838      {
839         p += strlen("file://");
840      }
841
842    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
843    if (savedtypes.textreq)
844      {
845         savedtypes.textreq = 0;
846         savedtypes.imgfile = strdup(p);
847      }
848    else
849      {
850         pasteimage_append(p, sel->requestwidget);
851      }
852    free(stripstr);
853    return 0;
854 }
855
856 /**
857  * Just receieved an vcard, either through cut and paste, or dnd.
858  */
859 static int
860 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
861 {
862    Dropable *dropable;
863    Eina_List *l;
864    Ecore_X_Selection_Data *data;
865
866    data = notify->data;
867    cnp_debug("vcard receive\n");
868
869    if (sel == (selections + ELM_SEL_XDND))
870      {
871         Elm_Selection_Data ddata;
872
873         cnp_debug("drag & drop\n");
874         /* FIXME: this needs to be generic: Used for all receives */
875         EINA_LIST_FOREACH(drops, l, dropable)
876           {
877              if (dropable->obj == sel->requestwidget) break;
878           }
879         if (!dropable)
880           {
881              cnp_debug("Unable to find drop object");
882              ecore_x_dnd_send_finished();
883              return 0;
884           }
885         dropable = eina_list_data_get(l);
886         ddata.x = savedtypes.x;
887         ddata.y = savedtypes.y;
888         ddata.format = ELM_SEL_FORMAT_VCARD;
889         ddata.data = data->data;
890         ddata.len = data->length;
891         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
892         ecore_x_dnd_send_finished();
893      }
894    else if (sel->datacb)
895      {
896         Elm_Selection_Data ddata;
897         ddata.x = ddata.y = 0;
898         ddata.format = ELM_SEL_FORMAT_IMAGE;
899         ddata.data = data->data;
900         ddata.len = data->length;
901         sel->datacb(sel->udata, sel->widget, &ddata);
902      }
903    else
904      {
905         cnp_debug("Paste request\n");
906      }
907
908    return 0;
909
910 }
911
912
913 static int
914 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
915 {
916    Ecore_X_Selection_Data *data;
917    Tmp_Info *tmp;
918
919    cnp_debug("got a png (or a jpeg)!\n");
920    data = notify->data;
921
922    cnp_debug("Size if %d\n", data->length);
923
924    if (sel->datacb)
925      {
926         Elm_Selection_Data ddata;
927
928         ddata.x = ddata.y = 0;
929         ddata.format = ELM_SEL_FORMAT_IMAGE;
930         ddata.data = data->data;
931         ddata.len = data->length;
932         sel->datacb(sel->udata, sel->widget, &ddata);
933         return 0;
934      }
935
936    /* generate tmp name */
937    tmp = elm_cnp_tempfile_create(data->length);
938    memcpy(tmp->map, data->data, data->length);
939    munmap(tmp->map,data->length);
940
941    /* FIXME: Add to paste image data to clean up */
942    pasteimage_append(tmp->filename, sel->requestwidget);
943
944    tmpinfo_free(tmp);
945    return 0;
946 }
947
948
949 /**
950  *    Warning: Generic text/html can';t handle it sanely.
951  *    Firefox sends ucs2 (i think).
952  *       chrome sends utf8... blerg
953  */
954 static int
955 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
956 {
957    Ecore_X_Selection_Data *data;
958
959    cnp_debug("Got some HTML: Checking encoding is useful\n");
960    data = notify->data;
961
962    if (sel->datacb)
963      {
964         Elm_Selection_Data ddata;
965         ddata.x = ddata.y = 0;
966         ddata.format = ELM_SEL_FORMAT_HTML;
967         ddata.data = data->data;
968         ddata.len = data->length;
969         sel->datacb(sel->udata, sel->widget, &ddata);
970         return 0;
971      }
972
973    char *stripstr = NULL;
974    stripstr = malloc(sizeof(char) * (data->length + 1));
975    strncpy(stripstr, (char *)data->data, data->length);
976    stripstr[data->length] = '\0';
977    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
978    _elm_entry_entry_paste(sel->requestwidget, stripstr);
979    free(stripstr);
980    return 0;
981 }
982
983
984 static Eina_Bool
985 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
986 {
987    Cnp_Selection *sel;
988
989    cnp_debug("text converter\n");
990    if (size != sizeof(int))
991      {
992         if (data_ret)
993           {
994              *data_ret = malloc(size * sizeof(char) + 1);
995              memcpy(*data_ret, data, size);
996              ((char**)(data_ret))[0][size] = 0;
997           }
998         if (size_ret) *size_ret = size;
999         return EINA_TRUE;
1000      }
1001    sel = selections + *((int *)data);
1002    if (!sel->active) return EINA_TRUE;
1003
1004    if ((sel->format & ELM_SEL_FORMAT_MARKUP) ||
1005        (sel->format & ELM_SEL_FORMAT_HTML))
1006      {
1007         *data_ret = remove_tags(sel->selbuf, size_ret);
1008      }
1009    else if (sel->format & ELM_SEL_FORMAT_TEXT)
1010      {
1011         *data_ret = strdup(sel->selbuf);
1012         *size_ret = strlen(sel->selbuf);
1013      }
1014    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1015      {
1016         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1017         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1018         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
1019         if (!*data_ret) *data_ret = strdup("No file");
1020         else *data_ret = strdup(*data_ret);
1021         *size_ret = strlen(*data_ret);
1022      }
1023    return EINA_TRUE;
1024 }
1025
1026 static Eina_Bool
1027 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1028 {
1029    if (size == sizeof(int))
1030      {
1031         Cnp_Selection *sel;
1032
1033         sel = selections + *((int *)data);
1034         if (data_ret) *data_ret = strdup(sel->selbuf);
1035         if (size_ret) *size_ret = strlen(sel->selbuf);
1036      }
1037    else if (size)
1038      {
1039         if (data_ret)
1040           {
1041              *data_ret = malloc(size * sizeof(char) + 1);
1042              memcpy(*data_ret, data, size);
1043              ((char**)(data_ret))[0][size] = 0;
1044           }
1045         if (size_ret) *size_ret = size;
1046      }
1047    return EINA_TRUE;
1048 }
1049
1050 static Eina_Bool
1051 pasteimage_append(char *file, Evas_Object *entry)
1052 {
1053    char *entrytag;
1054    int len;
1055    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1056
1057    if (!file) return EINA_FALSE;
1058    if (!entry) return EINA_FALSE;
1059
1060    len = strlen(tagstring)+strlen(file);
1061
1062    entrytag = alloca(len + 1);
1063    snprintf(entrytag, len + 1, tagstring, file);
1064    _elm_entry_entry_paste(entry, entrytag);
1065
1066    return EINA_TRUE;
1067 }
1068
1069 static char *
1070 remove_tags(const char *p, int *len)
1071 {
1072    char *q,*ret;
1073    int i;
1074    if (!p) return NULL;
1075
1076    q = malloc(strlen(p) + 1);
1077    if (!q) return NULL;
1078    ret = q;
1079
1080    while (*p)
1081      {
1082         Eina_Bool esc = EINA_TRUE;
1083         const char *x;
1084         switch (p[0])
1085           {
1086            case '<':
1087              x = strchr(p + 3, '>');
1088              if (!x)
1089                {
1090                   strcpy(q, p);
1091                   if (len) *len = strlen(ret);
1092                   return ret;
1093                }
1094              if (memcmp(p + 1, "br", 2) && memcmp(p + 1, "ps", 2))
1095                {
1096                   strncpy(q, p, x - p + 1);
1097                   p = x + 1;
1098                   break;
1099                }
1100              i = x - p - 1;
1101              if (p[i] == '/') i--;
1102              for (; i > 2; i++)
1103                {
1104                   if (p[i] != ' ')
1105                     {
1106                        esc = EINA_FALSE;
1107                        break;
1108                     }
1109                }
1110              if (!esc)
1111                {
1112                   strncpy(q, p, x - p + 1);
1113                   p = x + 1;
1114                   break;
1115                }
1116              if (p[1] == 'b')
1117                *q++ = '\n';
1118              else
1119                {
1120                   strcpy(q, _PARAGRAPH_SEPARATOR);
1121                   q += sizeof(_PARAGRAPH_SEPARATOR) - 1;
1122                }
1123              p = x + 1;
1124              break;
1125            case '&':
1126              for (i = 3 ; i < N_ESCAPES ; i++)
1127                {
1128                   if (strncmp(p, escapes[i].escape, strlen(escapes[i].escape)))
1129                     continue;
1130                   p += strlen(escapes[i].escape);
1131                   strcpy(q, escapes[i].value);
1132                   q += strlen(escapes[i].value);
1133                   break;
1134                }
1135              if (i == N_ESCAPES) *q ++= '&';
1136              break;
1137            default:
1138              *q++ = *p++;
1139           }
1140      }
1141    *q = 0;
1142    if (len) *len = q - ret;
1143    return ret;
1144 }
1145
1146 /* Mark up */
1147 static char *
1148 mark_up(const char *start, int inlen, int *lenp)
1149 {
1150    int l, i;
1151    const char *p;
1152    char *q, *ret;
1153    const char *endp = NULL;
1154
1155    if (!start) return NULL;
1156    if (inlen >= 0) endp = start + inlen;
1157    /* First pass: Count characters */
1158    for (l = 0, p = start; ((!endp) || (p < endp)) && (*p); p++)
1159      {
1160         for (i = 0 ; i < N_ESCAPES ; i ++)
1161           {
1162              if (*p == escapes[i].value[0])
1163                {
1164                   if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1165                     l += strlen(escapes[i].escape);
1166                   break;
1167                }
1168           }
1169         if (i == N_ESCAPES) l++;
1170      }
1171
1172    q = ret = malloc(l + 1);
1173
1174    /* Second pass: Change characters */
1175    for (p = start; ((!endp) || (p < endp)) && (*p); )
1176      {
1177         for (i = 0; i < N_ESCAPES; i++)
1178           {
1179              if (*p == escapes[i].value[0])
1180                {
1181                   if (!strncmp(p, escapes[i].value, strlen(escapes[i].value)))
1182                     {
1183                        strcpy(q, escapes[i].escape);
1184                        q += strlen(escapes[i].escape);
1185                        p += strlen(escapes[i].value);
1186                     }
1187                   break;
1188                }
1189           }
1190         if (i == N_ESCAPES) *q++ = *p++;
1191      }
1192    *q = 0;
1193
1194    if (lenp) *lenp = l;
1195    return ret;
1196 }
1197
1198
1199 static Eina_Bool
1200 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1201 {
1202    Ecore_X_Event_Xdnd_Enter *enter = ev;
1203    int i;
1204
1205    /* Skip it */
1206    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1207
1208    cnp_debug("Types\n");
1209    savedtypes.ntypes = enter->num_types;
1210    if (savedtypes.types) free(savedtypes.types);
1211    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1212    if (!savedtypes.types) return EINA_FALSE;
1213
1214    for (i = 0; i < enter->num_types; i++)
1215      {
1216         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1217         cnp_debug("Type is %s %p %p\n", enter->types[i],
1218                   savedtypes.types[i],text_uri);
1219         if (savedtypes.types[i] == text_uri)
1220           {
1221              /* Request it, so we know what it is */
1222              cnp_debug("Sending uri request\n");
1223              savedtypes.textreq = 1;
1224              if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1225              savedtypes.imgfile = NULL;
1226              ecore_x_selection_xdnd_request(enter->win, text_uri);
1227           }
1228      }
1229
1230    /* FIXME: Find an object and make it current */
1231    return EINA_TRUE;
1232 }
1233
1234 static Eina_Bool
1235 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1236 {
1237    struct _Ecore_X_Event_Xdnd_Drop *drop;
1238    Dropable *dropable;
1239    Eina_List *l;
1240    Ecore_Evas *ee;
1241    Ecore_X_Window xwin;
1242    Elm_Selection_Data ddata;
1243    int x, y, w, h;
1244    int i, j;
1245
1246    drop = ev;
1247
1248    // check we still have something to drop
1249    if (!drops) return EINA_TRUE;
1250
1251    /* Find any widget in our window; then work out geometry rel to our window */
1252    for (l = drops; l; l = l->next)
1253      {
1254         dropable = l->data;
1255         xwin = (Ecore_X_Window)ecore_evas_window_get
1256            (ecore_evas_ecore_evas_get(evas_object_evas_get
1257                                       (dropable->obj)));
1258         if (xwin == drop->win) break;
1259      }
1260    /* didn't find a window */
1261    if (!l) return EINA_TRUE;
1262
1263    /* Calculate real (widget relative) position */
1264    // - window position
1265    // - widget position
1266    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1267    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1268    savedtypes.x = drop->position.x - x;
1269    savedtypes.y = drop->position.y - y;
1270
1271    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1272
1273    for (; l; l = l->next)
1274      {
1275         dropable = l->data;
1276         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1277         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1278             (savedtypes.x < x + w) && (savedtypes.y < y + h))
1279           break; /* found! */
1280      }
1281
1282    if (!l) return EINA_TRUE; /* didn't find one */
1283
1284    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1285    savedtypes.x -= x;
1286    savedtypes.y -= y;
1287
1288    /* Find our type from the previous list */
1289    for (i = 0; i < CNP_N_ATOMS; i++)
1290      {
1291         for (j = 0; j < savedtypes.ntypes; j++)
1292           {
1293              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1294           }
1295      }
1296
1297    cnp_debug("Didn't find a target\n");
1298    return EINA_TRUE;
1299
1300 found:
1301    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1302    cnp_debug("0x%x\n",xwin);
1303
1304    if (i == CNP_ATOM_text_urilist)
1305      {
1306         cnp_debug("We found a URI... (%scached) %s\n",
1307                   savedtypes.imgfile ? "" : "not ",
1308                   savedtypes.imgfile);
1309         if (savedtypes.imgfile)
1310           {
1311              char *entrytag;
1312              static const char *tagstring = "<item absize=240x180 href="
1313                 "file://%s></item>";
1314              ddata.x = savedtypes.x;
1315              ddata.y = savedtypes.y;
1316
1317              /* If it's markup that also supports images */
1318              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1319                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
1320                {
1321                   int len;
1322                   ddata.format = ELM_SEL_FORMAT_MARKUP;
1323
1324                   len = strlen(tagstring) + strlen(savedtypes.imgfile);
1325                   entrytag = alloca(len + 1);
1326                   snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1327                   ddata.data = entrytag;
1328                   cnp_debug("Insert %s\n", (char *)ddata.data);
1329                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1330                   ecore_x_dnd_send_finished();
1331                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1332                   savedtypes.imgfile = NULL;
1333
1334                   return EINA_TRUE;
1335                }
1336              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1337                {
1338                   cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1339                   ddata.format = ELM_SEL_FORMAT_IMAGE;
1340                   ddata.data = (char *)savedtypes.imgfile;
1341                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1342                   ecore_x_dnd_send_finished();
1343                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1344                   savedtypes.imgfile = NULL;
1345
1346                   return EINA_TRUE;
1347                }
1348              else
1349                {
1350                   cnp_debug("Item doesn't support images... passing\n");
1351                   return EINA_TRUE;
1352                }
1353           }
1354         else if (savedtypes.textreq)
1355           {
1356              /* Already asked: Pretend we asked now, and paste immediately when
1357               * it comes in */
1358              savedtypes.textreq = 0;
1359              ecore_x_dnd_send_finished();
1360              return EINA_TRUE;
1361           }
1362      }
1363
1364    cnp_debug("doing a request then\n");
1365    selections[ELM_SEL_XDND].requestwidget = dropable->obj;
1366    selections[ELM_SEL_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1367    selections[ELM_SEL_XDND].active = EINA_TRUE;
1368
1369    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1370
1371    return EINA_TRUE;
1372 }
1373 static Eina_Bool
1374 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1375 {
1376    struct _Ecore_X_Event_Xdnd_Position *pos;
1377    Ecore_X_Rectangle rect;
1378
1379    pos = ev;
1380
1381    /* Need to send a status back */
1382    /* FIXME: Should check I can drop here */
1383    /* FIXME: Should highlight widget */
1384    rect.x = pos->position.x - 5;
1385    rect.y = pos->position.y - 5;
1386    rect.width = 10;
1387    rect.height = 10;
1388    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1389
1390    return EINA_TRUE;
1391 }
1392
1393 /**
1394  * When dragging this is callback response from the destination.
1395  * The important thing we care about: Can we drop; thus update cursor
1396  * appropriately.
1397  */
1398 static Eina_Bool
1399 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1400 {
1401    struct _Ecore_X_Event_Xdnd_Status *status = ev;
1402
1403    if (!status) return EINA_TRUE;
1404
1405    /* Only thing we care about: will accept */
1406    if (status->will_accept)
1407      {
1408         cnp_debug("Will accept\n");
1409      }
1410    else
1411      { /* Won't accept */
1412         cnp_debug("Won't accept accept\n");
1413      }
1414    return EINA_TRUE;
1415 }
1416
1417 /**
1418  * Add a widget as drop target.
1419  */
1420 Eina_Bool
1421 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1422 {
1423    Dropable *drop;
1424    Ecore_X_Window xwin;
1425    Eina_List *item;
1426    int first;
1427    Evas_Object *top;
1428
1429    if (!obj) return EINA_FALSE;
1430    top = elm_widget_top_get(obj);
1431    if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1432    if (!_elm_cnp_init_count) _elm_cnp_init();
1433
1434    /* Is this the first? */
1435    first = (!drops) ? 1 : 0;
1436
1437    EINA_LIST_FOREACH(drops, item, drop)
1438      {
1439         if (drop->obj == obj)
1440           {
1441              /* Update: Not a new one */
1442              drop->dropcb = dropcb;
1443              drop->cbdata = cbdata;
1444              drop->types = format;
1445              return EINA_TRUE;
1446           }
1447      }
1448
1449    /* Create new drop */
1450    drop = calloc(1, sizeof(Dropable));
1451    if (!drop) return EINA_FALSE;
1452    /* FIXME: Check for eina's deranged error method */
1453    drops = eina_list_append(drops, drop);
1454
1455    if (!drops/* || or other error */)
1456      {
1457         free(drop);
1458         return EINA_FALSE;
1459      }
1460    drop->dropcb = dropcb;
1461    drop->cbdata = cbdata;
1462    drop->types = format;
1463    drop->obj = obj;
1464
1465    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1466                                   /* I love C and varargs */
1467                                   (Evas_Object_Event_Cb)elm_drop_target_del,
1468                                   obj);
1469    /* FIXME: Handle resizes */
1470
1471    /* If not the first: We're done */
1472    if (!first) return EINA_TRUE;
1473
1474    xwin = (Ecore_X_Window)ecore_evas_window_get
1475       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1476
1477    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1478
1479    cnp_debug("Adding drop target calls\n");
1480    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1481                                            _dnd_enter, NULL);
1482    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1483                                          _dnd_position, NULL);
1484    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1485                                           _dnd_drop, NULL);
1486
1487    return EINA_TRUE;
1488 }
1489
1490 Eina_Bool
1491 elm_drop_target_del(Evas_Object *obj)
1492 {
1493    Dropable *drop,*del;
1494    Eina_List *item;
1495    Ecore_X_Window xwin;
1496
1497    del = NULL;
1498    EINA_LIST_FOREACH(drops, item, drop)
1499      {
1500         if (drop->obj == obj)
1501           {
1502              drops = eina_list_remove_list(drops, item);
1503              del = drop;
1504              break;
1505           }
1506      }
1507    if (!del) return EINA_FALSE;
1508
1509    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1510                                   (Evas_Object_Event_Cb)elm_drop_target_del);
1511    free(drop);
1512    /* If still drops there: All fine.. continue */
1513    if (drops) return EINA_TRUE;
1514
1515    cnp_debug("Disabling DND\n");
1516    xwin = (Ecore_X_Window)ecore_evas_window_get
1517       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1518    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1519
1520    ecore_event_handler_del(handler_pos);
1521    ecore_event_handler_del(handler_drop);
1522    ecore_event_handler_del(handler_enter);
1523
1524    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1525    savedtypes.imgfile = NULL;
1526
1527    return EINA_TRUE;
1528 }
1529
1530
1531 static void
1532 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data __UNUSED__)
1533 {
1534    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1535    ecore_x_dnd_drop();
1536    if (dragdonecb)
1537      {
1538         dragdonecb(dragdonecb,selections[ELM_SEL_XDND].widget);
1539         dragdonecb = NULL;
1540      }
1541    if (dragwin)
1542      {
1543         evas_object_del(dragwin);
1544         dragwin = NULL;
1545      }
1546 }
1547
1548 static void
1549 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1550 {
1551    evas_object_move(dragwin,
1552                     pos->position.x - _dragx,
1553                     pos->position.y - _dragy);
1554 }
1555
1556
1557 Eina_Bool
1558 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1559 {
1560    Ecore_X_Window xwin;
1561    Cnp_Selection *sel;
1562    Elm_Sel_Type xdnd = ELM_SEL_XDND;
1563    Ecore_Evas *ee;
1564    int x, y, x2, y2, x3, y3;
1565    Evas_Object *icon;
1566    int w, h;
1567
1568    if (!_elm_cnp_init_count) _elm_cnp_init();
1569
1570    xwin = elm_win_xwindow_get(obj);
1571
1572    cnp_debug("starting drag...\n");
1573
1574    ecore_x_dnd_type_set(xwin, "text/uri-list", 1);
1575    sel = selections + ELM_SEL_XDND;
1576    sel->active = 1;
1577    sel->widget = obj;
1578    sel->format = format;
1579    sel->selbuf = data ? strdup(data) : NULL;
1580    dragdonecb = dragdone;
1581    dragdonedata = donecbdata;
1582
1583    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1584    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1585    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1586                                   _drag_mouse_up, NULL);
1587
1588    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1589                                             _dnd_status, NULL);
1590
1591    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1592    elm_win_override_set(dragwin, 1);
1593
1594    /* FIXME: Images only */
1595    icon = elm_icon_add(dragwin);
1596    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1597    elm_win_resize_object_add(dragwin,icon);
1598    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1599    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1600
1601    /* Position subwindow appropriately */
1602    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1603    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1604    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1605    x += x2;
1606    y += y2;
1607    evas_object_move(dragwin, x, y);
1608    evas_object_resize(icon, w, h);
1609    evas_object_resize(dragwin, w, h);
1610
1611    evas_object_show(icon);
1612    evas_object_show(dragwin);
1613
1614    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1615    _dragx = x3 - x2;
1616    _dragy = y3 - y2;
1617
1618    return EINA_TRUE;
1619 }
1620
1621 static Tmp_Info *
1622 elm_cnp_tempfile_create(int size)
1623 {
1624 #ifdef HAVE_MMAN_H
1625    Tmp_Info *info;
1626    const char *tmppath;
1627    int len;
1628
1629    info = malloc(sizeof(Tmp_Info));
1630    if (!info) return NULL;
1631
1632    tmppath = getenv("TMP");
1633    if (!tmppath) tmppath = P_tmpdir;
1634    if (!tmppath) tmppath = "/tmp";
1635    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1636    if (len < 0)
1637      {
1638         free(info);
1639         return NULL;
1640      }
1641    len++;
1642    info->filename = malloc(len);
1643    if (!info->filename)
1644      {
1645         free(info);
1646         return NULL;
1647      }
1648    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1649
1650    info->fd = mkstemp(info->filename);
1651
1652 # ifdef __linux__
1653      {
1654         char *tmp;
1655         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1656         long pid;
1657
1658         pid = (long)getpid();
1659         /* Use pid instead of /proc/self: That way if can be passed around */
1660         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1661         len++;
1662         tmp = malloc(len);
1663         if (tmp)
1664           {
1665              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1666              unlink(info->filename);
1667              free(info->filename);
1668              info->filename = tmp;
1669           }
1670      }
1671 # endif
1672
1673    cnp_debug("filename is %s\n", info->filename);
1674    if (size < 1)
1675      {
1676         /* Set map to NULL and return */
1677         info->map = NULL;
1678         info->len = 0;
1679         return info;
1680      }
1681
1682    /* Map it in */
1683    if (ftruncate(info->fd, size))
1684      {
1685         perror("ftruncate");
1686         info->map = NULL;
1687         info->len = 0;
1688         return info;
1689      }
1690
1691    eina_mmap_safety_enabled_set(EINA_TRUE);
1692
1693    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1694    if (info->map == MAP_FAILED)
1695      {
1696         perror("mmap");
1697         info->map = NULL;
1698         info->len = 0;
1699      }
1700
1701    return info;
1702 #else
1703    (void) size;
1704    return NULL;
1705 #endif
1706 }
1707
1708
1709 static int
1710 tmpinfo_free(Tmp_Info *info)
1711 {
1712    if (!info) return 0;
1713    free(info->filename);
1714    free(info);
1715    return 0;
1716 }
1717
1718 #else
1719 /* Stubs for windows */
1720 Eina_Bool
1721 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1722 {
1723    return EINA_FALSE;
1724 }
1725
1726 Eina_Bool
1727 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1728 {
1729    return EINA_FALSE;
1730 }
1731
1732 Eina_Bool
1733 elm_drop_target_del(Evas_Object *o)
1734 {
1735    return EINA_TRUE;
1736 }
1737 #endif
1738
1739 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/