Elm glayer: Workaround a bug causing a delay with N_TAPS events.
[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    char *mkupstr;
817
818    data = notify->data;
819    str = malloc(sizeof(char) * (data->length + 1));
820    if (str)
821      {
822         strncpy(str, (char *)data->data, data->length);
823         str[data->length] = '\0';
824      }
825    else
826      str = data->data;
827
828    if (sel->datacb)
829      {
830         Elm_Selection_Data ddata;
831
832         ddata.x = ddata.y = 0;
833         ddata.format = ELM_SEL_FORMAT_TEXT;
834         ddata.data = str;
835         ddata.len = data->length;
836         sel->datacb(sel->udata, sel->widget, &ddata);
837      }
838    else
839      {
840         cnp_debug("Notify handler text %d %d %p\n", data->format, data->length, data->data);
841         mkupstr = _elm_util_text_to_mkup((const char *) str);
842         if (mkupstr)
843           {
844              entry_insert_filter(sel->requestwidget, mkupstr);
845              free(mkupstr);
846           }
847         else
848           entry_insert_filter(sel->requestwidget, str);
849
850         cnp_debug("String is %s (from %s)\n", str, data->data);
851         //_elm_entry_entry_paste(sel->requestwidget, str);
852      }
853
854    if (str != data->data)
855      free(str);
856    return 0;
857 }
858
859
860 /**
861  * So someone is pasting an image into my entry or widget...
862  */
863 static int
864 notify_handler_uri(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
865 {
866    Ecore_X_Selection_Data *data;
867    Ecore_X_Selection_Data_Files *files;
868    char *p, *stripstr;
869
870    data = notify->data;
871    cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
872    if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
873      {
874         cnp_debug("got a files list\n");
875         files = notify->data;
876         if (files->num_files > 1)
877           {
878              /* Don't handle many items */
879              cnp_debug("more then one file: Bailing\n");
880              return 0;
881           }
882         stripstr = p = strdup(files->files[0]);
883      }
884    else
885      {
886         stripstr = p = malloc(data->length * sizeof(char) + 1);
887         memcpy(stripstr, data->data, data->length);
888         stripstr[data->length] = 0;
889      }
890
891    if (!p)
892      {
893         cnp_debug("Couldn't find a file\n");
894         return 0;
895      }
896    cnp_debug("Got %s\n",p);
897    if (sel->datacb)
898      {
899         Elm_Selection_Data ddata;
900
901         ddata.x = ddata.y = 0;
902         ddata.format = ELM_SEL_FORMAT_IMAGE;
903         ddata.data = p;
904         ddata.len = data->length;
905         sel->datacb(sel->udata, sel->widget, &ddata);
906         free(p);
907         return 0;
908      }
909    if (strncmp(p, "file://", 7))
910      {
911         /* Try and continue if it looks sane */
912         if (*p != '/')
913           {
914              free(p);
915              return 0;
916           }
917      }
918    else
919      {
920         p += strlen("file://");
921      }
922
923    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
924    if (savedtypes.textreq)
925      {
926         savedtypes.textreq = 0;
927         savedtypes.imgfile = strdup(p);
928      }
929    else
930      {
931         pasteimage_append(p, sel->requestwidget);
932      }
933    free(stripstr);
934    return 0;
935 }
936
937 /**
938  * Just receieved an vcard, either through cut and paste, or dnd.
939  */
940 static int
941 vcard_receive(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
942 {
943    Dropable *dropable;
944    Eina_List *l;
945    Ecore_X_Selection_Data *data;
946
947    data = notify->data;
948    cnp_debug("vcard receive\n");
949
950    if (sel == (selections + ELM_SEL_TYPE_XDND))
951      {
952         Elm_Selection_Data ddata;
953
954         cnp_debug("drag & drop\n");
955         /* FIXME: this needs to be generic: Used for all receives */
956         EINA_LIST_FOREACH(drops, l, dropable)
957           {
958              if (dropable->obj == sel->requestwidget) break;
959           }
960         if (!dropable)
961           {
962              cnp_debug("Unable to find drop object");
963              ecore_x_dnd_send_finished();
964              return 0;
965           }
966         dropable = eina_list_data_get(l);
967         ddata.x = savedtypes.x;
968         ddata.y = savedtypes.y;
969         ddata.format = ELM_SEL_FORMAT_VCARD;
970         ddata.data = data->data;
971         ddata.len = data->length;
972         dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
973         ecore_x_dnd_send_finished();
974      }
975    else if (sel->datacb)
976      {
977         Elm_Selection_Data ddata;
978         ddata.x = ddata.y = 0;
979         ddata.format = ELM_SEL_FORMAT_IMAGE;
980         ddata.data = data->data;
981         ddata.len = data->length;
982         sel->datacb(sel->udata, sel->widget, &ddata);
983      }
984    else
985      {
986         cnp_debug("Paste request\n");
987      }
988
989    return 0;
990
991 }
992
993
994 static int
995 notify_handler_image(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
996 {
997    Ecore_X_Selection_Data *data;
998    Tmp_Info *tmp;
999
1000    cnp_debug("got a image file!\n");
1001    data = notify->data;
1002
1003    cnp_debug("Size if %d\n", data->length);
1004
1005    if (sel->datacb)
1006      {
1007         Elm_Selection_Data ddata;
1008
1009         ddata.x = ddata.y = 0;
1010         ddata.format = ELM_SEL_FORMAT_IMAGE;
1011         ddata.data = data->data;
1012         ddata.len = data->length;
1013         sel->datacb(sel->udata, sel->widget, &ddata);
1014         return 0;
1015      }
1016
1017    /* generate tmp name */
1018    tmp = elm_cnp_tempfile_create(data->length);
1019    memcpy(tmp->map, data->data, data->length);
1020    munmap(tmp->map, data->length);
1021
1022    /* FIXME: Add to paste image data to clean up */
1023    pasteimage_append(tmp->filename, sel->requestwidget);
1024
1025    tmpinfo_free(tmp);
1026    return 0;
1027 }
1028
1029 static int
1030 notify_handler_edje(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1031 {
1032    Ecore_X_Selection_Data *data;
1033
1034    data = notify->data;
1035
1036    char *stripstr = NULL;
1037    stripstr = malloc(sizeof(char) * (data->length + 1));
1038    strncpy(stripstr, (char *)data->data, data->length);
1039    stripstr[data->length] = '\0';
1040
1041    if (sel->datacb)
1042      {
1043         Elm_Selection_Data ddata;
1044         ddata.x = ddata.y = 0;
1045         ddata.format = ELM_SEL_FORMAT_MARKUP;
1046         ddata.data = stripstr;
1047         ddata.len = data->length;
1048         sel->datacb(sel->udata, sel->widget, &ddata);
1049      }
1050    else
1051      entry_insert_filter(sel->requestwidget, stripstr);
1052      //_elm_entry_entry_paste(sel->requestwidget, stripstr);
1053
1054    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1055    free(stripstr);
1056    return 0;
1057 }
1058
1059 /**
1060  *    Warning: Generic text/html can';t handle it sanely.
1061  *    Firefox sends ucs2 (i think).
1062  *       chrome sends utf8... blerg
1063  */
1064 static int
1065 notify_handler_html(Cnp_Selection *sel, Ecore_X_Event_Selection_Notify *notify)
1066 {
1067    Ecore_X_Selection_Data *data;
1068
1069    cnp_debug("Got some HTML: Checking encoding is useful\n");
1070    data = notify->data;
1071
1072    char *stripstr = NULL;
1073    stripstr = malloc(sizeof(char) * (data->length + 1));
1074    strncpy(stripstr, (char *)data->data, data->length);
1075    stripstr[data->length] = '\0';
1076
1077    if (sel->datacb)
1078      {
1079         Elm_Selection_Data ddata;
1080         ddata.x = ddata.y = 0;
1081         ddata.format = ELM_SEL_FORMAT_HTML;
1082         ddata.data = stripstr;
1083         ddata.len = data->length;
1084         sel->datacb(sel->udata, sel->widget, &ddata);
1085      }
1086    else
1087      entry_insert_filter(sel->requestwidget, stripstr);
1088    //_elm_entry_entry_paste(sel->requestwidget, stripstr);
1089
1090    cnp_debug("String is %s (%d bytes)\n", stripstr, data->length);
1091    free(stripstr);
1092    return 0;
1093 }
1094
1095
1096 static Eina_Bool
1097 text_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1098 {
1099    Cnp_Selection *sel;
1100
1101    cnp_debug("text converter\n");
1102    if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
1103      return EINA_FALSE;
1104    if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1105      {
1106         if (data_ret)
1107           {
1108              *data_ret = malloc(size * sizeof(char) + 1);
1109              memcpy(*data_ret, data, size);
1110              ((char**)(data_ret))[0][size] = 0;
1111           }
1112         if (size_ret) *size_ret = size;
1113         return EINA_TRUE;
1114      }
1115    sel = selections + *((int *)data);
1116    if (!sel->active) return EINA_TRUE;
1117
1118    if (sel->format & ELM_SEL_FORMAT_MARKUP)
1119      *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1120    else if (sel->format & ELM_SEL_FORMAT_HTML)
1121      {
1122         char *text = NULL;
1123         Eina_Strbuf *buf = eina_strbuf_new();
1124         if (buf)
1125           {
1126              eina_strbuf_append(buf, sel->selbuf);
1127              eina_strbuf_replace_all(buf, "&nbsp;", " ");
1128              text = eina_strbuf_string_steal(buf);
1129              eina_strbuf_free(buf);
1130              *data_ret = _elm_util_mkup_to_text(text);
1131              free(text);
1132           }
1133         else
1134           *data_ret = _elm_util_mkup_to_text(sel->selbuf);
1135      }
1136    else if (sel->format & ELM_SEL_FORMAT_TEXT)
1137      {
1138         *data_ret = strdup(sel->selbuf);
1139         *size_ret = strlen(sel->selbuf);
1140      }
1141    else if (sel->format & ELM_SEL_FORMAT_IMAGE)
1142      {
1143         cnp_debug("Image %s\n", evas_object_type_get(sel->widget));
1144         cnp_debug("Elm type: %s\n", elm_object_widget_type_get(sel->widget));
1145         evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget),
1146                                    (const char **)data_ret, NULL);
1147         if (!*data_ret) *data_ret = strdup("No file");
1148         else *data_ret = strdup(*data_ret);
1149         *size_ret = strlen(*data_ret);
1150      }
1151    return EINA_TRUE;
1152 }
1153
1154 static Eina_Bool
1155 general_converter(char *target __UNUSED__, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
1156 {
1157    if (_get_selection_type(data, size) == ELM_SEL_FORMAT_NONE)
1158      {
1159         if (data_ret)
1160           {
1161              *data_ret = malloc(size * sizeof(char) + 1);
1162              memcpy(*data_ret, data, size);
1163              ((char**)(data_ret))[0][size] = 0;
1164           }
1165         if (size_ret) *size_ret = size;
1166      }
1167    else
1168      {
1169         Cnp_Selection *sel;
1170         if (!data || (*((unsigned int *)data) > ELM_SEL_TYPE_CLIPBOARD))
1171           return EINA_FALSE;
1172
1173         sel = selections + *((int *)data);
1174         if (data_ret) *data_ret = strdup(sel->selbuf);
1175         if (size_ret) *size_ret = strlen(sel->selbuf);
1176      }
1177    return EINA_TRUE;
1178 }
1179
1180 static Eina_Bool
1181 pasteimage_append(char *file, Evas_Object *entry)
1182 {
1183    char *entrytag;
1184    int len;
1185    static const char *tagstring = "<item absize=240x180 href=file://%s></item>";
1186
1187    if ((!file) || (!entry)) return EINA_FALSE;
1188
1189    len = strlen(tagstring)+strlen(file);
1190
1191    entrytag = alloca(len + 1);
1192    snprintf(entrytag, len + 1, tagstring, file);
1193    _elm_entry_entry_paste(entry, entrytag);
1194
1195    return EINA_TRUE;
1196 }
1197
1198 static Eina_Bool
1199 _dnd_enter(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1200 {
1201    Ecore_X_Event_Xdnd_Enter *enter = ev;
1202    int i;
1203
1204    /* Skip it */
1205    if ((!enter) || (!enter->num_types) || (!enter->types)) return EINA_TRUE;
1206
1207    cnp_debug("Types\n");
1208    savedtypes.ntypes = enter->num_types;
1209    if (savedtypes.types) free(savedtypes.types);
1210    savedtypes.types = malloc(sizeof(char *) * enter->num_types);
1211    if (!savedtypes.types) return EINA_FALSE;
1212
1213    for (i = 0; i < enter->num_types; i++)
1214      {
1215         savedtypes.types[i] = eina_stringshare_add(enter->types[i]);
1216         cnp_debug("Type is %s %p %p\n", enter->types[i],
1217                   savedtypes.types[i],text_uri);
1218         if (savedtypes.types[i] == text_uri)
1219           {
1220              /* Request it, so we know what it is */
1221              cnp_debug("Sending uri request\n");
1222              savedtypes.textreq = 1;
1223              if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1224              savedtypes.imgfile = NULL;
1225              ecore_x_selection_xdnd_request(enter->win, text_uri);
1226           }
1227      }
1228
1229    /* FIXME: Find an object and make it current */
1230    return EINA_TRUE;
1231 }
1232
1233 static Eina_Bool
1234 _dnd_drop(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1235 {
1236    struct _Ecore_X_Event_Xdnd_Drop *drop;
1237    Dropable *dropable;
1238    Eina_List *l;
1239    Ecore_Evas *ee;
1240    Ecore_X_Window xwin;
1241    Elm_Selection_Data ddata;
1242    int x, y, w, h;
1243    int i, j;
1244
1245    drop = ev;
1246
1247    // check we still have something to drop
1248    if (!drops) return EINA_TRUE;
1249
1250    /* Find any widget in our window; then work out geometry rel to our window */
1251    for (l = drops; l; l = l->next)
1252      {
1253         dropable = l->data;
1254         xwin = (Ecore_X_Window)ecore_evas_window_get
1255            (ecore_evas_ecore_evas_get(evas_object_evas_get
1256                                       (dropable->obj)));
1257         if (xwin == drop->win) break;
1258      }
1259    /* didn't find a window */
1260    if (!l) return EINA_TRUE;
1261
1262    /* Calculate real (widget relative) position */
1263    // - window position
1264    // - widget position
1265    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(dropable->obj));
1266    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1267    savedtypes.x = drop->position.x - x;
1268    savedtypes.y = drop->position.y - y;
1269
1270    cnp_debug("Drop position is %d,%d\n", savedtypes.x, savedtypes.y);
1271
1272    for (; l; l = l->next)
1273      {
1274         dropable = l->data;
1275         evas_object_geometry_get(dropable->obj, &x, &y, &w, &h);
1276         if ((savedtypes.x >= x) && (savedtypes.y >= y) &&
1277             (savedtypes.x < x + w) && (savedtypes.y < y + h))
1278           break; /* found! */
1279      }
1280
1281    if (!l) return EINA_TRUE; /* didn't find one */
1282
1283    evas_object_geometry_get(dropable->obj, &x, &y, NULL, NULL);
1284    savedtypes.x -= x;
1285    savedtypes.y -= y;
1286
1287    /* Find our type from the previous list */
1288    for (i = 0; i < CNP_N_ATOMS; i++)
1289      {
1290         for (j = 0; j < savedtypes.ntypes; j++)
1291           {
1292              if (!strcmp(savedtypes.types[j], atoms[i].name)) goto found;
1293           }
1294      }
1295
1296    cnp_debug("Didn't find a target\n");
1297    return EINA_TRUE;
1298
1299 found:
1300    cnp_debug("Found a target we'd like: %s\n", atoms[i].name);
1301    cnp_debug("0x%x\n",xwin);
1302
1303    if (i == CNP_ATOM_text_urilist)
1304      {
1305         cnp_debug("We found a URI... (%scached) %s\n",
1306                   savedtypes.imgfile ? "" : "not ",
1307                   savedtypes.imgfile);
1308         if (savedtypes.imgfile)
1309           {
1310              char *entrytag;
1311              static const char *tagstring = "<item absize=240x180 href="
1312                 "file://%s></item>";
1313              ddata.x = savedtypes.x;
1314              ddata.y = savedtypes.y;
1315
1316              /* If it's markup that also supports images */
1317              if ((dropable->types & ELM_SEL_FORMAT_MARKUP) &&
1318                  (dropable->types & ELM_SEL_FORMAT_IMAGE))
1319                {
1320                   int len;
1321                   ddata.format = ELM_SEL_FORMAT_MARKUP;
1322
1323                   len = strlen(tagstring) + strlen(savedtypes.imgfile);
1324                   entrytag = alloca(len + 1);
1325                   snprintf(entrytag, len + 1, tagstring, savedtypes.imgfile);
1326                   ddata.data = entrytag;
1327                   cnp_debug("Insert %s\n", (char *)ddata.data);
1328                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1329                   ecore_x_dnd_send_finished();
1330                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1331                   savedtypes.imgfile = NULL;
1332
1333                   return EINA_TRUE;
1334                }
1335              else if (dropable->types & ELM_SEL_FORMAT_IMAGE)
1336                {
1337                   cnp_debug("Doing image insert (%s)\n", savedtypes.imgfile);
1338                   ddata.format = ELM_SEL_FORMAT_IMAGE;
1339                   ddata.data = (char *)savedtypes.imgfile;
1340                   dropable->dropcb(dropable->cbdata, dropable->obj, &ddata);
1341                   ecore_x_dnd_send_finished();
1342                   if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1343                   savedtypes.imgfile = NULL;
1344
1345                   return EINA_TRUE;
1346                }
1347              else
1348                {
1349                   cnp_debug("Item doesn't support images... passing\n");
1350                   return EINA_TRUE;
1351                }
1352           }
1353         else if (savedtypes.textreq)
1354           {
1355              /* Already asked: Pretend we asked now, and paste immediately when
1356               * it comes in */
1357              savedtypes.textreq = 0;
1358              ecore_x_dnd_send_finished();
1359              return EINA_TRUE;
1360           }
1361      }
1362
1363    cnp_debug("doing a request then\n");
1364    selections[ELM_SEL_TYPE_XDND].requestwidget = dropable->obj;
1365    selections[ELM_SEL_TYPE_XDND].requestformat = ELM_SEL_FORMAT_MARKUP;
1366    selections[ELM_SEL_TYPE_XDND].active = EINA_TRUE;
1367
1368    ecore_x_selection_xdnd_request(xwin, atoms[i].name);
1369
1370    return EINA_TRUE;
1371 }
1372 static Eina_Bool
1373 _dnd_position(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1374 {
1375    struct _Ecore_X_Event_Xdnd_Position *pos;
1376    Ecore_X_Rectangle rect;
1377
1378    pos = ev;
1379
1380    /* Need to send a status back */
1381    /* FIXME: Should check I can drop here */
1382    /* FIXME: Should highlight widget */
1383    rect.x = pos->position.x - 5;
1384    rect.y = pos->position.y - 5;
1385    rect.width = 10;
1386    rect.height = 10;
1387    ecore_x_dnd_send_status(EINA_TRUE, EINA_FALSE, rect, pos->action);
1388
1389    return EINA_TRUE;
1390 }
1391
1392 /**
1393  * When dragging this is callback response from the destination.
1394  * The important thing we care about: Can we drop; thus update cursor
1395  * appropriately.
1396  */
1397 static Eina_Bool
1398 _dnd_status(void *data __UNUSED__, int etype __UNUSED__, void *ev)
1399 {
1400    struct _Ecore_X_Event_Xdnd_Status *status = ev;
1401
1402    if (!status) return EINA_TRUE;
1403
1404    /* Only thing we care about: will accept */
1405    if (status->will_accept)
1406      {
1407         cnp_debug("Will accept\n");
1408      }
1409    else
1410      { /* Won't accept */
1411         cnp_debug("Won't accept accept\n");
1412      }
1413    return EINA_TRUE;
1414 }
1415
1416 /**
1417  * Add a widget as drop target.
1418  */
1419 Eina_Bool
1420 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1421 {
1422    Dropable *drop;
1423    Ecore_X_Window xwin;
1424    Eina_List *item;
1425    int first;
1426    Evas_Object *top;
1427
1428    if (!obj) return EINA_FALSE;
1429    top = elm_widget_top_get(obj);
1430    if (!top || !elm_win_xwindow_get(top)) return EINA_FALSE;
1431    if (!_elm_cnp_init_count) _elm_cnp_init();
1432
1433    /* Is this the first? */
1434    first = (!drops) ? 1 : 0;
1435
1436    EINA_LIST_FOREACH(drops, item, drop)
1437      {
1438         if (drop->obj == obj)
1439           {
1440              /* Update: Not a new one */
1441              drop->dropcb = dropcb;
1442              drop->cbdata = cbdata;
1443              drop->types = format;
1444              return EINA_TRUE;
1445           }
1446      }
1447
1448    /* Create new drop */
1449    drop = calloc(1, sizeof(Dropable));
1450    if (!drop) return EINA_FALSE;
1451    /* FIXME: Check for eina's deranged error method */
1452    drops = eina_list_append(drops, drop);
1453
1454    if (!drops/* || or other error */)
1455      {
1456         free(drop);
1457         return EINA_FALSE;
1458      }
1459    drop->dropcb = dropcb;
1460    drop->cbdata = cbdata;
1461    drop->types = format;
1462    drop->obj = obj;
1463
1464    evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL,
1465                                   /* I love C and varargs */
1466                                   (Evas_Object_Event_Cb)elm_drop_target_del,
1467                                   obj);
1468    /* FIXME: Handle resizes */
1469
1470    /* If not the first: We're done */
1471    if (!first) return EINA_TRUE;
1472
1473    xwin = (Ecore_X_Window)ecore_evas_window_get
1474       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1475
1476    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1477
1478    cnp_debug("Adding drop target calls\n");
1479    handler_enter = ecore_event_handler_add(ECORE_X_EVENT_XDND_ENTER,
1480                                            _dnd_enter, NULL);
1481    handler_pos = ecore_event_handler_add(ECORE_X_EVENT_XDND_POSITION,
1482                                          _dnd_position, NULL);
1483    handler_drop = ecore_event_handler_add(ECORE_X_EVENT_XDND_DROP,
1484                                           _dnd_drop, NULL);
1485
1486    return EINA_TRUE;
1487 }
1488
1489 Eina_Bool
1490 elm_drop_target_del(Evas_Object *obj)
1491 {
1492    Dropable *drop,*del;
1493    Eina_List *item;
1494    Ecore_X_Window xwin;
1495
1496    del = NULL;
1497    EINA_LIST_FOREACH(drops, item, drop)
1498      {
1499         if (drop->obj == obj)
1500           {
1501              drops = eina_list_remove_list(drops, item);
1502              del = drop;
1503              break;
1504           }
1505      }
1506    if (!del) return EINA_FALSE;
1507
1508    evas_object_event_callback_del(obj, EVAS_CALLBACK_FREE,
1509                                   (Evas_Object_Event_Cb)elm_drop_target_del);
1510    free(drop);
1511    /* If still drops there: All fine.. continue */
1512    if (drops) return EINA_TRUE;
1513
1514    cnp_debug("Disabling DND\n");
1515    xwin = (Ecore_X_Window)ecore_evas_window_get
1516       (ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
1517    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1518
1519    ecore_event_handler_del(handler_pos);
1520    ecore_event_handler_del(handler_drop);
1521    ecore_event_handler_del(handler_enter);
1522
1523    if (savedtypes.imgfile) free((void*)savedtypes.imgfile);
1524    savedtypes.imgfile = NULL;
1525
1526    return EINA_TRUE;
1527 }
1528
1529
1530 static void
1531 _drag_mouse_up(void *un __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *data)
1532 {
1533    Ecore_X_Window xwin = *((Ecore_X_Window *)data);
1534    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP, _drag_mouse_up);
1535    ecore_x_dnd_drop();
1536    ecore_x_dnd_aware_set(xwin, EINA_FALSE);
1537    if (dragdonecb)
1538      {
1539         dragdonecb(dragdonedata, selections[ELM_SEL_TYPE_XDND].widget);
1540         dragdonecb = NULL;
1541      }
1542    if (dragwin)
1543      {
1544         evas_object_del(dragwin);
1545         dragwin = NULL;
1546      }
1547 }
1548
1549 static void
1550 _drag_move(void *data __UNUSED__, Ecore_X_Xdnd_Position *pos)
1551 {
1552    evas_object_move(dragwin,
1553                     pos->position.x - _dragx,
1554                     pos->position.y - _dragy);
1555 }
1556
1557
1558 Eina_Bool
1559 elm_drag_start(Evas_Object *obj, Elm_Sel_Format format, const char *data, void (*dragdone) (void *data, Evas_Object *), void *donecbdata)
1560 {
1561    Ecore_X_Window xwin;
1562    Cnp_Selection *sel;
1563    Elm_Sel_Type xdnd = ELM_SEL_TYPE_XDND;
1564    Ecore_Evas *ee;
1565    int x, y, x2, y2, x3, y3;
1566    Evas_Object *icon;
1567    int w, h;
1568
1569    if (!_elm_cnp_init_count) _elm_cnp_init();
1570
1571    xwin = elm_win_xwindow_get(obj);
1572
1573    cnp_debug("starting drag...\n");
1574
1575    if (dragwin)
1576      {
1577         cnp_debug("another obj is dragging...\n");
1578         return EINA_FALSE;
1579      }
1580
1581    ecore_x_dnd_type_set(xwin, "text/uri-list", EINA_TRUE);
1582    sel = selections + ELM_SEL_TYPE_XDND;
1583    sel->active = EINA_TRUE;
1584    sel->widget = obj;
1585    sel->format = format;
1586    sel->selbuf = data ? strdup(data) : NULL;
1587    dragdonecb = dragdone;
1588    dragdonedata = donecbdata;
1589
1590    ecore_x_dnd_aware_set(xwin, EINA_TRUE);
1591    ecore_x_dnd_callback_pos_update_set(_drag_move, NULL);
1592    ecore_x_dnd_begin(xwin, (unsigned char *)&xdnd, sizeof(Elm_Sel_Type));
1593    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
1594                                   _drag_mouse_up, (void *)(long)xwin);
1595
1596    handler_status = ecore_event_handler_add(ECORE_X_EVENT_XDND_STATUS,
1597                                             _dnd_status, NULL);
1598
1599    dragwin = elm_win_add(NULL, "Elm Drag Object", ELM_WIN_UTILITY);
1600    elm_win_override_set(dragwin, EINA_TRUE);
1601
1602    /* FIXME: Images only */
1603    icon = elm_icon_add(dragwin);
1604    elm_icon_file_set(icon, data + 7, NULL); /* 7!? "file://" */
1605    elm_win_resize_object_add(dragwin, icon);
1606    evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1607    evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
1608
1609    /* Position subwindow appropriately */
1610    ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1611    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
1612    evas_object_geometry_get(obj, &x2, &y2, &w, &h);
1613    x += x2;
1614    y += y2;
1615    evas_object_move(dragwin, x, y);
1616    evas_object_resize(icon, w, h);
1617    evas_object_resize(dragwin, w, h);
1618
1619    evas_object_show(icon);
1620    evas_object_show(dragwin);
1621
1622    evas_pointer_canvas_xy_get(evas_object_evas_get(obj), &x3, &y3);
1623    _dragx = x3 - x2;
1624    _dragy = y3 - y2;
1625
1626    return EINA_TRUE;
1627 }
1628
1629 static Tmp_Info *
1630 elm_cnp_tempfile_create(int size)
1631 {
1632 #ifdef HAVE_MMAN_H
1633    Tmp_Info *info;
1634    const char *tmppath;
1635    int len;
1636
1637    info = malloc(sizeof(Tmp_Info));
1638    if (!info) return NULL;
1639
1640    tmppath = getenv("TMP");
1641    if (!tmppath) tmppath = P_tmpdir;
1642    len = snprintf(NULL, 0, "%s/%sXXXXXX", tmppath, "elmcnpitem-");
1643    if (len < 0)
1644      {
1645         free(info);
1646         return NULL;
1647      }
1648    len++;
1649    info->filename = malloc(len);
1650    if (!info->filename)
1651      {
1652         free(info);
1653         return NULL;
1654      }
1655    snprintf(info->filename,len,"%s/%sXXXXXX", tmppath, "elmcnpitem-");
1656
1657    info->fd = mkstemp(info->filename);
1658
1659 # ifdef __linux__
1660      {
1661         char *tmp;
1662         /* And before someone says anything see POSIX 1003.1-2008 page 400 */
1663         long pid;
1664
1665         pid = (long)getpid();
1666         /* Use pid instead of /proc/self: That way if can be passed around */
1667         len = snprintf(NULL,0,"/proc/%li/fd/%i", pid, info->fd);
1668         len++;
1669         tmp = malloc(len);
1670         if (tmp)
1671           {
1672              snprintf(tmp,len, "/proc/%li/fd/%i", pid, info->fd);
1673              unlink(info->filename);
1674              free(info->filename);
1675              info->filename = tmp;
1676           }
1677      }
1678 # endif
1679
1680    cnp_debug("filename is %s\n", info->filename);
1681    if (size < 1)
1682      {
1683         /* Set map to NULL and return */
1684         info->map = NULL;
1685         info->len = 0;
1686         return info;
1687      }
1688
1689    /* Map it in */
1690    if (ftruncate(info->fd, size))
1691      {
1692         perror("ftruncate");
1693         info->map = NULL;
1694         info->len = 0;
1695         return info;
1696      }
1697
1698    eina_mmap_safety_enabled_set(EINA_TRUE);
1699
1700    info->map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, info->fd, 0);
1701    if (info->map == MAP_FAILED)
1702      {
1703         perror("mmap");
1704         info->map = NULL;
1705         info->len = 0;
1706      }
1707
1708    return info;
1709 #else
1710    (void) size;
1711    return NULL;
1712 #endif
1713 }
1714
1715
1716 static int
1717 tmpinfo_free(Tmp_Info *info)
1718 {
1719    if (!info) return 0;
1720    free(info->filename);
1721    free(info);
1722    return 0;
1723 }
1724
1725 #else
1726 /* Stubs for windows */
1727 Eina_Bool
1728 elm_drag_start(Evas_Object *o, Elm_Sel_Format f, const char *d, void (*donecb)(void *, Evas_Object *),void *cbdata)
1729 {
1730    return EINA_FALSE;
1731 }
1732
1733 Eina_Bool
1734 elm_drop_target_add(Evas_Object *obj, Elm_Sel_Type format, Elm_Drop_Cb dropcb, void *cbdata)
1735 {
1736    return EINA_FALSE;
1737 }
1738
1739 Eina_Bool
1740 elm_drop_target_del(Evas_Object *o)
1741 {
1742    return EINA_TRUE;
1743 }
1744 #endif
1745
1746 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/