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