3 #include <stdio.h> // debug
5 #include <Elementary.h>
9 #ifdef HAVE_ELEMENTARY_X
12 # include <X11/Xatom.h>
15 # define ARRAYINIT(foo) [foo]=
20 #define cnp_debug(x...) printf(__FILE__": " x)
22 #define cnp_debug(x...)
31 CNP_ATOM_text_html_utf8,
36 CNP_ATOM_text_plain_utf8,
49 struct _elm_cnp_selection {
53 enum _elm_sel_format format;
56 unsigned int active : 1;
58 Ecore_X_Selection ecore_sel;
60 Evas_Object *requestwidget;
61 enum _elm_sel_format requestformat;
63 int (*set)(Ecore_X_Window, const void *data, int size);
65 void (*request)(Ecore_X_Window, const char *target);
68 /* Optimisation: Turn this into a 256 byte table:
69 * then can lookup in one index, not N checks */
70 static const struct escapes {
82 #define N_ESCAPES ((int)(sizeof(escapes)/sizeof(escapes[0])))
85 static Eina_Bool _elm_cnp_init(void);
86 static Eina_Bool selection_clear(void *udata __UNUSED__, int type, void *event);
87 //static Eina_Bool selection_request(void *udata __UNUSED__, int type, void *event);
88 static Eina_Bool selection_notify(void *udata __UNUSED__, int type, void *event);
89 static char *remove_tags(const char *p, int *len);
90 static char *mark_up(const char *start, int *lenp);
92 static Evas_Object *image_provider(void *images, Evas_Object *entry, const char *item);
95 typedef int (*converter_fn)(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
97 static int targets_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
98 static int text_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
99 static int html_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
100 static int edje_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
101 static int uri_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
102 static int png_converter(char *target, void *data, int size, void **data_ret, int *size_ret, Ecore_X_Atom *ttype, int *typesize);
104 /* FIXME: Which way should this be: Notify or response */
105 typedef int (*response_handler)(struct _elm_cnp_selection *sel,
106 Ecore_X_Event_Selection_Notify *);
107 static int response_handler_targets(struct _elm_cnp_selection *sel,
108 Ecore_X_Event_Selection_Notify *);
110 typedef int (*notify_handler)(struct _elm_cnp_selection *sel,
111 Ecore_X_Event_Selection_Notify *);
112 static int notify_handler_targets(struct _elm_cnp_selection *sel,
113 Ecore_X_Event_Selection_Notify *notify);
114 static int notify_handler_text(struct _elm_cnp_selection *sel,
115 Ecore_X_Event_Selection_Notify *notify);
116 static int notify_handler_png(struct _elm_cnp_selection *sel,
117 Ecore_X_Event_Selection_Notify *notify);
118 static int notify_handler_uri(struct _elm_cnp_selection *sel,
119 Ecore_X_Event_Selection_Notify *notify);
124 enum _elm_sel_format formats;
125 /* Called by ecore to do conversion */
126 converter_fn converter;
127 response_handler response;
128 notify_handler notify;
131 } atoms[CNP_N_ATOMS] = {
132 [CNP_ATOM_TARGETS] = {
134 (enum _elm_sel_format)-1,
136 response_handler_targets,
137 notify_handler_targets,
141 "application/x-elementary-markup",
148 [CNP_ATOM_text_uri] = {
150 ELM_SEL_MARKUP | ELM_SEL_IMAGE, /* Either images or entries */
156 [CNP_ATOM_image_png] = {
158 ELM_SEL_IMAGE | ELM_SEL_IMAGE,
164 [CNP_ATOM_text_html_utf8] = {
165 "text/html;charset=utf-8",
172 [CNP_ATOM_text_html] = {
180 [CNP_ATOM_UTF8STRING] = {
188 [CNP_ATOM_STRING] = {
190 ELM_SEL_MARKUP | ELM_SEL_IMAGE,
198 ELM_SEL_MARKUP | ELM_SEL_IMAGE,
204 [CNP_ATOM_text_plain_utf8] = {
205 "text/plain;charset=ut-8",
212 [CNP_ATOM_text_plain] = {
222 static struct _elm_cnp_selection selections[ELM_SEL_MAX] = {
223 ARRAYINIT(ELM_SEL_PRIMARY) {
225 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
226 .set = ecore_x_selection_primary_set,
227 .clear = ecore_x_selection_primary_clear,
228 .request = ecore_x_selection_primary_request,
230 ARRAYINIT(ELM_SEL_SECONDARY) {
231 .debug = "Secondary",
232 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
233 .set = ecore_x_selection_secondary_set,
234 .clear = ecore_x_selection_secondary_clear,
235 .request = ecore_x_selection_secondary_request,
237 ARRAYINIT(ELM_SEL_CLIPBOARD) {
238 .debug = "Clipboard",
239 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
240 .set = ecore_x_selection_clipboard_set,
241 .clear = ecore_x_selection_clipboard_clear,
242 .request = ecore_x_selection_clipboard_request,
246 static int _elm_cnp_init_count = 0;
247 /* Gah... who left this out of XAtoms.h */
248 static Ecore_X_Atom clipboard_atom;
250 Eina_List *pastedimages;
255 elm_selection_set(enum _elm_sel_type selection, Evas_Object *widget,
256 enum _elm_sel_format format, const char *selbuf)
258 #ifdef HAVE_ELEMENTARY_X
259 struct _elm_cnp_selection *sel;
261 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
262 if (!_elm_cnp_init_count) _elm_cnp_init();
263 if (!selbuf && format != ELM_SEL_IMAGE)
264 return elm_selection_clear(selection, widget);
266 sel = selections + selection;
269 sel->widget = widget;
271 sel->set(elm_win_xwindow_get(widget),&selection,sizeof(enum _elm_sel_type));
272 sel->format = format;
273 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
282 elm_selection_clear(enum _elm_sel_type selection, Evas_Object *widget)
284 #ifdef HAVE_ELEMENTARY_X
285 struct _elm_cnp_selection *sel;
287 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
288 if (!_elm_cnp_init_count) _elm_cnp_init();
290 sel = selections + selection;
292 /* No longer this selection: Consider it gone! */
293 if (!sel->active || sel->widget != widget) return EINA_TRUE;
306 elm_selection_get(enum _elm_sel_type selection, enum _elm_sel_format format,
309 #ifdef HAVE_ELEMENTARY_X
311 struct _elm_cnp_selection *sel;
313 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
314 if (!_elm_cnp_init_count) _elm_cnp_init();
316 sel = selections + selection;
317 top = elm_widget_top_get(widget);
318 if (!top) return EINA_FALSE;
320 sel->requestformat = format;
321 sel->requestwidget = widget;
322 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_UTF8_STRING);
330 #ifdef HAVE_ELEMENTARY_X
335 if (_elm_cnp_init_count ++) return EINA_TRUE;
337 /* FIXME: Handle XCB */
338 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
340 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
341 ecore_x_selection_converter_atom_add(atoms[i].atom,
344 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
346 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear,NULL);
347 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY,selection_notify,NULL);
352 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event){
353 Ecore_X_Event_Selection_Clear *ev = event;
354 struct _elm_cnp_selection *sel;
357 for (i = 0 ; i < ELM_SEL_MAX ; i ++)
359 if (selections[i].ecore_sel == ev->selection) break;
361 /* Not me... Don't care */
362 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
364 sel = selections + i;
369 return ECORE_CALLBACK_PASS_ON;
374 * Response to a selection request
377 selection_request(void *udata __UNUSED__, int type, void *event){
378 Ecore_X_Event_Selection_Request *ev = event;
379 struct _elm_cnp_selection *sel;
381 printf("selection request callback: %d\n",ev->selection);
382 printf("selection request callback: %d\n",ev->target);
384 if (ev->selection == clipboard_atom){
385 sel = selections + ELM_SEL_CLIPBOARD;
386 } else if (ev->selection == XA_PRIMARY){
387 sel = selections + ELM_SEL_PRIMARY;
388 } else if (ev->selection == XA_SECONDARY){
389 sel = selections + ELM_SEL_SECONDARY;
391 return ECORE_CALLBACK_PASS_ON;
393 return ECORE_CALLBACK_PASS_ON;
395 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
397 if (ev->target == atoms[i].atom)
399 if (atoms[i].response){
400 atoms[i].response(sel, ev);
402 printf("Ignored: No handler!\n");
407 return ECORE_CALLBACK_PASS_ON;
413 * Response to a selection notify:
414 * - So we have asked for the selection list.
415 * - If it's the targets list, parse it, and fire of what we want,
416 * else it's the data we want.
419 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event){
420 Ecore_X_Event_Selection_Notify *ev = event;
421 struct _elm_cnp_selection *sel;
424 cnp_debug("selection notify callback: %d\n",ev->selection);
425 switch (ev->selection){
426 case ECORE_X_SELECTION_CLIPBOARD:
427 sel = selections + ELM_SEL_CLIPBOARD;
430 case ECORE_X_SELECTION_PRIMARY:
431 sel = selections + ELM_SEL_PRIMARY;
433 case ECORE_X_SELECTION_SECONDARY:
434 sel = selections + ELM_SEL_SECONDARY;
437 return ECORE_CALLBACK_PASS_ON;
439 cnp_debug("Target is %s\n",ev->target);
441 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
443 if (strcmp(ev->target, atoms[i].name) == 0)
445 if (atoms[i].notify){
446 cnp_debug("Found something: %s\n", atoms[i].name);
447 atoms[i].notify(sel, ev);
449 printf("Ignored: No handler!\n");
454 return ECORE_CALLBACK_PASS_ON;
460 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
461 void **data_ret, int *size_ret,
462 Ecore_X_Atom *ttype, int *typesize){
465 struct _elm_cnp_selection *sel;
467 if (!data_ret) return -1;
469 sel = selections + *(int*)data;
471 for (i = 0, count = 0 ; i < CNP_N_ATOMS ; i ++)
472 if (sel->format & atoms[i].formats)
475 aret = malloc(sizeof(Ecore_X_Atom) * count);
476 for (i = 0, count = 0 ; i < CNP_N_ATOMS ; i ++)
477 if (sel->format & atoms[i].formats)
478 aret[count ++] = atoms[i].atom;
481 if (typesize) *typesize = 32 /* urk */;
482 if (ttype) *ttype = XA_ATOM;
483 if (size_ret) *size_ret = count;
489 png_converter(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__,
490 void **data_ret __UNUSED__, int *size_ret __UNUSED__,
491 Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
497 * Callback to handle a targets response on a selection request:
498 * So pick the format we'd like; and then request it.
501 notify_handler_targets(struct _elm_cnp_selection *sel,
502 Ecore_X_Event_Selection_Notify *notify)
504 Ecore_X_Selection_Data_Targets *targets;
505 Ecore_X_Atom *atomlist;
508 targets = notify->data;
509 atomlist = (Ecore_X_Atom *)(targets->data.data);
511 for (j = 1; j < CNP_N_ATOMS ; j ++)
513 cnp_debug("\t%s %d\n",atoms[j].name, atoms[j].atom);
514 if (!(atoms[j].formats & sel->requestformat)) continue;
515 for (i = 0 ; i < targets->data.length ; i ++)
517 if (atoms[j].atom == atomlist[i])
519 cnp_debug("Atom %s matches\n",atoms[j].name);
525 cnp_debug("Couldn't find anything that matches\n");
526 return ECORE_CALLBACK_PASS_ON;
529 cnp_debug("Sending request for %s\n",atoms[j].name);
530 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
532 return ECORE_CALLBACK_PASS_ON;
536 response_handler_targets(struct _elm_cnp_selection *sel,
537 Ecore_X_Event_Selection_Notify *notify)
539 Ecore_X_Selection_Data_Targets *targets;
540 Ecore_X_Atom *atomlist;
545 targets = notify->data;
546 atomlist = (Ecore_X_Atom *)(targets->data.data);
550 /* Start from 1: Skip targets */
551 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
553 if (!(atoms[j].formats & sel->requestformat)) continue;
554 for (i = 0 ; i < targets->data.length ; i ++)
556 if (atoms[j].atom == atomlist[i] && atoms[j].response){
557 /* Found a match: Use it */
563 if (j == CNP_N_ATOMS)
565 cnp_debug("No matching type found\n");
569 top = elm_widget_top_get(sel->requestwidget);
572 sel->request(elm_win_xwindow_get(top), atoms[j].name);
578 notify_handler_text(struct _elm_cnp_selection *sel,
579 Ecore_X_Event_Selection_Notify *notify)
581 Ecore_X_Selection_Data *data;
585 str = mark_up((char*)data->data, NULL);
586 elm_entry_entry_insert(sel->requestwidget, str);
594 * So someone is pasting an image into my entry or widget...
597 notify_handler_uri(struct _elm_cnp_selection *sel,
598 Ecore_X_Event_Selection_Notify *notify)
600 Ecore_X_Selection_Data *data;
601 struct pasteimage *pi;
606 p = (char *)data->data;
607 cnp_debug("Got %s\n",p);
608 if (strncmp(p,"file://",7) != 0){
609 cnp_debug("Doesn't start with ;file; %s\n",p);
613 p += strlen("file://");
615 pi = calloc(1,sizeof(struct pasteimage));
616 snprintf(entrytag, sizeof(entrytag), "pasteimage-%p",pi);
617 pi->tag = strdup(entrytag);
618 pi->file = strndup(p,data->length - strlen("file://"));
620 elm_entry_item_provider_append(sel->requestwidget, image_provider, pi);
621 pastedimages = eina_list_append(pastedimages, pi);
623 snprintf(entrytag, sizeof(entrytag), "<item absize=240x180 href=%s>",pi->tag);
624 elm_entry_entry_insert(sel->requestwidget, entrytag);
630 notify_handler_png(struct _elm_cnp_selection *sel __UNUSED__,
631 Ecore_X_Event_Selection_Notify *notify __UNUSED__)
633 cnp_debug("got a png!\n");
639 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
640 void **data_ret, int *size_ret,
641 Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
643 struct _elm_cnp_selection *sel;
645 sel = selections + *(int *)data;
651 if (sel->format == ELM_SEL_MARKUP){
652 *data_ret = remove_tags(sel->selbuf, size_ret);
653 } else if (sel->format == ELM_SEL_IMAGE){
654 cnp_debug("Image %s\n",evas_object_type_get(sel->widget));
655 cnp_debug("Elm type: %s\n",elm_object_widget_type_get(sel->widget));
656 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
657 if (!*data_ret) *data_ret = strdup("No file");
658 else *data_ret = strdup(*data_ret);
659 *size_ret = strlen(*data_ret);
665 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
666 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
667 int *typesize __UNUSED__)
669 struct _elm_cnp_selection *sel;
671 sel = selections + *(int *)data;
672 if (data_ret) *data_ret = strdup(sel->selbuf);
673 if (size_ret) *size_ret = strlen(sel->selbuf);
680 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
681 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
682 int *typesize __UNUSED__)
684 struct _elm_cnp_selection *sel;
686 sel = selections + *(int *)data;
687 if (data_ret) *data_ret = strdup(sel->selbuf);
688 if (size_ret) *size_ret = strlen(sel->selbuf);
694 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
695 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
696 int *typesize __UNUSED__)
698 struct _elm_cnp_selection *sel;
699 sel = selections + *(int *)data;
700 cnp_debug("Uri converter\n");
701 if (data_ret) *data_ret = strdup(sel->selbuf);
702 if (size_ret) *size_ret = strlen(sel->selbuf);
708 * Image paste provide
711 /* FIXME: Should add provider for each pated item: Use data to store it
714 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
716 struct pasteimage *pi;
719 cnp_debug("image provider for %s called\n",item);
720 EINA_LIST_FOREACH(pastedimages, l, pi)
722 cnp_debug("is it %s?\n",pi->tag);
723 if (strcmp(pi->tag,item) == 0){
726 o = evas_object_image_filled_add(evas_object_evas_get(entry));
727 /* FIXME: Handle eets */
728 cnp_debug("file is %s (object is %p)\n",pi->file,o);
729 evas_object_image_file_set(o, pi->file, NULL);
738 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
740 struct pasteimage *pi;
743 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
745 if (pi->entry == entry){
746 pastedimages = eina_list_remove_list(pastedimages, l);
753 remove_tags(const char *p, int *len){
758 q = malloc(strlen(p) + 1);
764 if (*p != '<' && *p != '&'){
766 } else if (*p == '<') {
767 if (p[1] == 'b' && p[2] == 'r' &&
768 (p[3] == ' ' || p[3] == '/' || p[3] == '>'))
770 while (*p && *p != '>') p ++;
772 } else if (*p == '&') {
774 for (i = 0 ; i < N_ESCAPES ; i ++){
775 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape))){
776 p += strlen(escapes[i].escape);
777 *q = escapes[i].value;
787 if (len) *len = q - ret;
793 mark_up(const char *start, int *lenp){
798 if (!start) return NULL;
799 /* First pass: Count characters */
800 for (l = 0, p = start ; *p ; p ++)
802 for (i = 0 ; i < N_ESCAPES ; i ++)
804 if (*p == escapes[i].value)
806 l += strlen(escapes[i].escape);
814 q = ret = malloc(l + 1);
816 /* Second pass: Change characters */
817 for (p = start ; *p ; )
819 for (i = 0 ; i < N_ESCAPES ; i ++)
821 if (*p == escapes[i].value)
823 strcpy(q, escapes[i].escape);
824 q += strlen(escapes[i].escape);