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] = {
183 ELM_SEL_MARKUP | ELM_SEL_IMAGE,
189 [CNP_ATOM_STRING] = {
191 ELM_SEL_MARKUP | ELM_SEL_IMAGE,
199 ELM_SEL_MARKUP | ELM_SEL_IMAGE,
205 [CNP_ATOM_text_plain_utf8] = {
206 "text/plain;charset=ut-8",
213 [CNP_ATOM_text_plain] = {
223 static struct _elm_cnp_selection selections[ELM_SEL_MAX] = {
224 ARRAYINIT(ELM_SEL_PRIMARY) {
226 .ecore_sel = ECORE_X_SELECTION_PRIMARY,
227 .set = ecore_x_selection_primary_set,
228 .clear = ecore_x_selection_primary_clear,
229 .request = ecore_x_selection_primary_request,
231 ARRAYINIT(ELM_SEL_SECONDARY) {
232 .debug = "Secondary",
233 .ecore_sel = ECORE_X_SELECTION_SECONDARY,
234 .set = ecore_x_selection_secondary_set,
235 .clear = ecore_x_selection_secondary_clear,
236 .request = ecore_x_selection_secondary_request,
238 ARRAYINIT(ELM_SEL_CLIPBOARD) {
239 .debug = "Clipboard",
240 .ecore_sel = ECORE_X_SELECTION_CLIPBOARD,
241 .set = ecore_x_selection_clipboard_set,
242 .clear = ecore_x_selection_clipboard_clear,
243 .request = ecore_x_selection_clipboard_request,
247 static int _elm_cnp_init_count = 0;
248 /* Gah... who left this out of XAtoms.h */
249 static Ecore_X_Atom clipboard_atom;
251 Eina_List *pastedimages;
256 elm_selection_set(enum _elm_sel_type selection, Evas_Object *widget,
257 enum _elm_sel_format format, const char *selbuf)
259 #ifdef HAVE_ELEMENTARY_X
260 struct _elm_cnp_selection *sel;
262 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
263 if (!_elm_cnp_init_count) _elm_cnp_init();
264 if (!selbuf && format != ELM_SEL_IMAGE)
265 return elm_selection_clear(selection, widget);
267 sel = selections + selection;
270 sel->widget = widget;
272 sel->set(elm_win_xwindow_get(widget),&selection,sizeof(enum _elm_sel_type));
273 sel->format = format;
274 sel->selbuf = selbuf ? strdup(selbuf) : NULL;
283 elm_selection_clear(enum _elm_sel_type selection, Evas_Object *widget)
285 #ifdef HAVE_ELEMENTARY_X
286 struct _elm_cnp_selection *sel;
288 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
289 if (!_elm_cnp_init_count) _elm_cnp_init();
291 sel = selections + selection;
293 /* No longer this selection: Consider it gone! */
294 if (!sel->active || sel->widget != widget) return EINA_TRUE;
307 elm_selection_get(enum _elm_sel_type selection, enum _elm_sel_format format,
310 #ifdef HAVE_ELEMENTARY_X
312 struct _elm_cnp_selection *sel;
314 if ((unsigned int)selection >= (unsigned int)ELM_SEL_MAX) return EINA_FALSE;
315 if (!_elm_cnp_init_count) _elm_cnp_init();
317 sel = selections + selection;
318 top = elm_widget_top_get(widget);
319 if (!top) return EINA_FALSE;
321 sel->requestformat = format;
322 sel->requestwidget = widget;
323 sel->request(elm_win_xwindow_get(top), ECORE_X_SELECTION_TARGET_UTF8_STRING);
331 #ifdef HAVE_ELEMENTARY_X
336 if (_elm_cnp_init_count ++) return EINA_TRUE;
338 /* FIXME: Handle XCB */
339 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
341 atoms[i].atom = ecore_x_atom_get(atoms[i].name);
342 ecore_x_selection_converter_atom_add(atoms[i].atom,
345 clipboard_atom = ecore_x_atom_get("CLIPBOARD");
347 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_CLEAR, selection_clear,NULL);
348 ecore_event_handler_add(ECORE_X_EVENT_SELECTION_NOTIFY,selection_notify,NULL);
353 selection_clear(void *udata __UNUSED__, int type __UNUSED__, void *event){
354 Ecore_X_Event_Selection_Clear *ev = event;
355 struct _elm_cnp_selection *sel;
358 for (i = 0 ; i < ELM_SEL_MAX ; i ++)
360 if (selections[i].ecore_sel == ev->selection) break;
362 /* Not me... Don't care */
363 if (i == ELM_SEL_MAX) return ECORE_CALLBACK_PASS_ON;
365 sel = selections + i;
370 return ECORE_CALLBACK_PASS_ON;
375 * Response to a selection request
378 selection_request(void *udata __UNUSED__, int type, void *event){
379 Ecore_X_Event_Selection_Request *ev = event;
380 struct _elm_cnp_selection *sel;
382 printf("selection request callback: %d\n",ev->selection);
383 printf("selection request callback: %d\n",ev->target);
385 if (ev->selection == clipboard_atom){
386 sel = selections + ELM_SEL_CLIPBOARD;
387 } else if (ev->selection == XA_PRIMARY){
388 sel = selections + ELM_SEL_PRIMARY;
389 } else if (ev->selection == XA_SECONDARY){
390 sel = selections + ELM_SEL_SECONDARY;
392 return ECORE_CALLBACK_PASS_ON;
394 return ECORE_CALLBACK_PASS_ON;
396 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
398 if (ev->target == atoms[i].atom)
400 if (atoms[i].response){
401 atoms[i].response(sel, ev);
403 printf("Ignored: No handler!\n");
408 return ECORE_CALLBACK_PASS_ON;
414 * Response to a selection notify:
415 * - So we have asked for the selection list.
416 * - If it's the targets list, parse it, and fire of what we want,
417 * else it's the data we want.
420 selection_notify(void *udata __UNUSED__, int type __UNUSED__, void *event){
421 Ecore_X_Event_Selection_Notify *ev = event;
422 struct _elm_cnp_selection *sel;
423 struct _elm_cnp_selection *bufsel;
426 cnp_debug("selection notify callback: %d\n",ev->selection);
427 switch (ev->selection){
428 case ECORE_X_SELECTION_CLIPBOARD:
429 sel = selections + ELM_SEL_CLIPBOARD;
432 case ECORE_X_SELECTION_PRIMARY:
433 sel = selections + ELM_SEL_PRIMARY;
435 case ECORE_X_SELECTION_SECONDARY:
436 sel = selections + ELM_SEL_SECONDARY;
439 return ECORE_CALLBACK_PASS_ON;
441 cnp_debug("Target is %s\n",ev->target);
443 for (i = 0 ; i < CNP_N_ATOMS ; i ++)
445 if (strcmp(ev->target, atoms[i].name) == 0)
447 if (atoms[i].notify){
448 cnp_debug("Found something: %s\n", atoms[i].name);
450 /* FIXME : it's maybe a bug. more overhaul it!!! */
451 sel->selbuf = bufsel->selbuf;
452 atoms[i].notify(sel, ev);
454 printf("Ignored: No handler!\n");
459 return ECORE_CALLBACK_PASS_ON;
465 targets_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
466 void **data_ret, int *size_ret,
467 Ecore_X_Atom *ttype, int *typesize){
470 struct _elm_cnp_selection *sel;
472 if (!data_ret) return -1;
474 sel = selections + *(int*)data;
476 for (i = 0, count = 0 ; i < CNP_N_ATOMS ; i ++)
477 if (sel->format & atoms[i].formats)
480 aret = malloc(sizeof(Ecore_X_Atom) * count);
481 for (i = 0, count = 0 ; i < CNP_N_ATOMS ; i ++)
482 if (sel->format & atoms[i].formats)
483 aret[count ++] = atoms[i].atom;
486 if (typesize) *typesize = 32 /* urk */;
487 if (ttype) *ttype = XA_ATOM;
488 if (size_ret) *size_ret = count;
494 png_converter(char *target __UNUSED__, void *data __UNUSED__, int size __UNUSED__,
495 void **data_ret __UNUSED__, int *size_ret __UNUSED__,
496 Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
502 * Callback to handle a targets response on a selection request:
503 * So pick the format we'd like; and then request it.
506 notify_handler_targets(struct _elm_cnp_selection *sel,
507 Ecore_X_Event_Selection_Notify *notify)
509 Ecore_X_Selection_Data_Targets *targets;
510 Ecore_X_Atom *atomlist;
513 targets = notify->data;
514 atomlist = (Ecore_X_Atom *)(targets->data.data);
516 for (j = 1; j < CNP_N_ATOMS ; j ++)
518 cnp_debug("\t%s %d\n",atoms[j].name, atoms[j].atom);
519 if (!(atoms[j].formats & sel->requestformat)) continue;
520 for (i = 0 ; i < targets->data.length ; i ++)
522 if (atoms[j].atom == atomlist[i])
524 cnp_debug("Atom %s matches\n",atoms[j].name);
530 cnp_debug("Couldn't find anything that matches\n");
531 return ECORE_CALLBACK_PASS_ON;
534 cnp_debug("Sending request for %s\n",atoms[j].name);
535 sel->request(elm_win_xwindow_get(sel->requestwidget), atoms[j].name);
537 return ECORE_CALLBACK_PASS_ON;
541 response_handler_targets(struct _elm_cnp_selection *sel,
542 Ecore_X_Event_Selection_Notify *notify)
544 Ecore_X_Selection_Data_Targets *targets;
545 Ecore_X_Atom *atomlist;
550 targets = notify->data;
551 atomlist = (Ecore_X_Atom *)(targets->data.data);
555 /* Start from 1: Skip targets */
556 for (j = 1 ; j < CNP_N_ATOMS ; j ++)
558 if (!(atoms[j].formats & sel->requestformat)) continue;
559 for (i = 0 ; i < targets->data.length ; i ++)
561 if (atoms[j].atom == atomlist[i] && atoms[j].response){
562 /* Found a match: Use it */
568 if (j == CNP_N_ATOMS)
570 cnp_debug("No matching type found\n");
574 top = elm_widget_top_get(sel->requestwidget);
577 sel->request(elm_win_xwindow_get(top), atoms[j].name);
583 notify_handler_text(struct _elm_cnp_selection *sel,
584 Ecore_X_Event_Selection_Notify *notify)
586 Ecore_X_Selection_Data *data;
589 fprintf(stderr, "## notify_handler_text selbuf - %s\n", sel->selbuf);
591 // data = notify->data;
592 // str = mark_up((char*)data->data, NULL);
593 str = mark_up(sel->selbuf, NULL);
594 fprintf(stderr, "## notify_handler_text str - %s\n", str);
596 elm_entry_entry_insert(sel->requestwidget, str);
604 * So someone is pasting an image into my entry or widget...
607 notify_handler_uri(struct _elm_cnp_selection *sel,
608 Ecore_X_Event_Selection_Notify *notify)
610 Ecore_X_Selection_Data *data;
611 struct pasteimage *pi;
616 p = (char *)data->data;
617 cnp_debug("Got %s\n",p);
618 if (strncmp(p,"file://",7) != 0){
619 cnp_debug("Doesn't start with ;file; %s\n",p);
623 p += strlen("file://");
625 pi = calloc(1,sizeof(struct pasteimage));
626 snprintf(entrytag, sizeof(entrytag), "pasteimage-%p",pi);
627 pi->tag = strdup(entrytag);
628 pi->file = strndup(p,data->length - strlen("file://"));
630 elm_entry_item_provider_append(sel->requestwidget, image_provider, pi);
631 pastedimages = eina_list_append(pastedimages, pi);
633 snprintf(entrytag, sizeof(entrytag), "<item absize=240x180 href=%s>",pi->tag);
634 elm_entry_entry_insert(sel->requestwidget, entrytag);
640 notify_handler_png(struct _elm_cnp_selection *sel __UNUSED__,
641 Ecore_X_Event_Selection_Notify *notify __UNUSED__)
643 cnp_debug("got a png!\n");
649 text_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
650 void **data_ret, int *size_ret,
651 Ecore_X_Atom *ttype __UNUSED__, int *typesize __UNUSED__)
653 struct _elm_cnp_selection *sel;
655 sel = selections + *(int *)data;
661 if (sel->format == ELM_SEL_MARKUP){
662 *data_ret = remove_tags(sel->selbuf, size_ret);
663 fprintf(stderr, "## text_convert - %s\n", *data_ret);
664 } else if (sel->format == ELM_SEL_IMAGE){
665 cnp_debug("Image %s\n",evas_object_type_get(sel->widget));
666 cnp_debug("Elm type: %s\n",elm_object_widget_type_get(sel->widget));
667 evas_object_image_file_get(elm_photocam_internal_image_get(sel->widget), (const char **)data_ret, NULL);
668 if (!*data_ret) *data_ret = strdup("No file");
669 else *data_ret = strdup(*data_ret);
670 *size_ret = strlen(*data_ret);
676 edje_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
677 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
678 int *typesize __UNUSED__)
680 struct _elm_cnp_selection *sel;
682 sel = selections + *(int *)data;
683 if (data_ret) *data_ret = strdup(sel->selbuf);
684 if (size_ret) *size_ret = strlen(sel->selbuf);
691 html_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
692 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
693 int *typesize __UNUSED__)
695 struct _elm_cnp_selection *sel;
697 sel = selections + *(int *)data;
698 if (data_ret) *data_ret = strdup(sel->selbuf);
699 if (size_ret) *size_ret = strlen(sel->selbuf);
705 uri_converter(char *target __UNUSED__, void *data, int size __UNUSED__,
706 void **data_ret, int *size_ret, Ecore_X_Atom *ttype __UNUSED__,
707 int *typesize __UNUSED__)
709 struct _elm_cnp_selection *sel;
710 sel = selections + *(int *)data;
711 cnp_debug("Uri converter\n");
712 if (data_ret) *data_ret = strdup(sel->selbuf);
713 if (size_ret) *size_ret = strlen(sel->selbuf);
719 * Image paste provide
722 /* FIXME: Should add provider for each pated item: Use data to store it
725 image_provider(void *images __UNUSED__, Evas_Object *entry, const char *item)
727 struct pasteimage *pi;
730 cnp_debug("image provider for %s called\n",item);
731 EINA_LIST_FOREACH(pastedimages, l, pi)
733 cnp_debug("is it %s?\n",pi->tag);
734 if (strcmp(pi->tag,item) == 0){
737 o = evas_object_image_filled_add(evas_object_evas_get(entry));
738 /* FIXME: Handle eets */
739 cnp_debug("file is %s (object is %p)\n",pi->file,o);
740 evas_object_image_file_set(o, pi->file, NULL);
749 entry_deleted(void *images __UNUSED__, Evas *e __UNUSED__, Evas_Object *entry, void *unused __UNUSED__)
751 struct pasteimage *pi;
754 EINA_LIST_FOREACH_SAFE(pastedimages, l, next, pi)
756 if (pi->entry == entry){
757 pastedimages = eina_list_remove_list(pastedimages, l);
764 remove_tags(const char *p, int *len){
769 q = malloc(strlen(p) + 1);
775 if (*p != '<' && *p != '&'){
777 } else if (*p == '<') {
778 if (p[1] == 'b' && p[2] == 'r' &&
779 (p[3] == ' ' || p[3] == '/' || p[3] == '>'))
781 while (*p && *p != '>') p ++;
783 } else if (*p == '&') {
785 for (i = 0 ; i < N_ESCAPES ; i ++){
786 if (!strncmp(p,escapes[i].escape, strlen(escapes[i].escape))){
787 p += strlen(escapes[i].escape);
788 *q = escapes[i].value;
798 if (len) *len = q - ret;
804 mark_up(const char *start, int *lenp){
809 if (!start) return NULL;
810 /* First pass: Count characters */
811 for (l = 0, p = start ; *p ; p ++)
813 for (i = 0 ; i < N_ESCAPES ; i ++)
815 if (*p == escapes[i].value)
817 l += strlen(escapes[i].escape);
825 q = ret = malloc(l + 1);
827 /* Second pass: Change characters */
828 for (p = start ; *p ; )
830 for (i = 0 ; i < N_ESCAPES ; i ++)
832 if (*p == escapes[i].value)
834 strcpy(q, escapes[i].escape);
835 q += strlen(escapes[i].escape);