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