1 #include "cbhm_helper.h"
3 #ifdef HAVE_ELEMENTARY_X
6 #define ATOM_CBHM_WINDOW_NAME "CBHM_XWIN"
7 #define ATOM_CBHM_MSG "CBHM_MSG"
8 #define ATOM_CBHM_COUNT_GET "CBHM_cCOUNT"
9 #define ATOM_CBHM_TEXT_COUNT_GET "CBHM_TEXT_cCOUNT"
10 #define ATOM_CBHM_IMAGE_COUNT_GET "CBHM_IMAGE_cCOUNT"
11 #define ATOM_CBHM_SERIAL_NUMBER "CBHM_SERIAL_NUMBER"
12 #define MSG_CBHM_COUNT_GET "get count"
13 #define ATOM_CBHM_ERROR "CBHM_ERROR"
14 #define ATOM_CBHM_ITEM "CBHM_ITEM"
17 #ifdef HAVE_ELEMENTARY_X
21 Ecore_X_Atom x_atom_cbhm = ecore_x_atom_get(ATOM_CBHM_WINDOW_NAME);
22 Ecore_X_Window x_cbhm_win = 0;
23 unsigned char *buf = NULL;
25 int ret = ecore_x_window_prop_property_get(0, x_atom_cbhm, XA_WINDOW, 0, &buf, &num);
26 DMSG("ret: %d, num: %d\n", ret, num);
28 memcpy(&x_cbhm_win, buf, sizeof(Ecore_X_Window));
36 _cbhm_msg_send(Evas_Object *obj, char *msg)
38 #ifdef HAVE_ELEMENTARY_X
39 Ecore_X_Window x_cbhm_win = _cbhm_window_get();
40 Ecore_X_Atom x_atom_cbhm_msg = ecore_x_atom_get(ATOM_CBHM_MSG);
41 Ecore_Evas *ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
42 Ecore_X_Window xwin = ecore_evas_software_x11_window_get(ee);
45 xwin = ecore_evas_gl_x11_window_get(ee);
48 DMSG("ERROR: can't get x window\n");
53 DMSG("x_cbhm: 0x%x\n", x_cbhm_win);
54 if (!x_cbhm_win || !x_atom_cbhm_msg)
57 XClientMessageEvent m;
58 memset(&m, 0, sizeof(m));
59 m.type = ClientMessage;
60 m.display = ecore_x_display_get();
62 m.message_type = x_atom_cbhm_msg;
64 snprintf(m.data.b, 20, "%s", msg);
66 XSendEvent(ecore_x_display_get(), x_cbhm_win, False, NoEventMask, (XEvent*)&m);
75 #ifdef HAVE_ELEMENTARY_X
77 _cbhm_reply_get(Ecore_X_Window xwin, Ecore_X_Atom property, Ecore_X_Atom *x_data_type, int *num)
79 unsigned char *data = NULL;
88 long unsigned int num_ret = 0, bytes = 0;
89 int ret = 0, size_ret;
91 unsigned char *prop_ret;
92 Ecore_X_Atom type_ret;
93 ret = XGetWindowProperty(ecore_x_display_get(), xwin, property, 0, LONG_MAX,
94 False, ecore_x_window_prop_any_type(), (Atom *)&type_ret, &size_ret,
95 &num_ret, &bytes, &prop_ret);
104 if (!(data = malloc(num_ret * size_ret / 8)))
112 for (i = 0; i < num_ret; i++)
113 (data)[i] = prop_ret[i];
117 for (i = 0; i < num_ret; i++)
118 ((unsigned short *)data)[i] = ((unsigned short *)prop_ret)[i];
122 for (i = 0; i < num_ret; i++)
123 ((unsigned int *)data)[i] = ((unsigned long *)prop_ret)[i];
132 *x_data_type = type_ret;
139 _cbhm_item_count_get(Evas_Object *obj __UNUSED__, int atom_index)
141 #ifdef HAVE_ELEMENTARY_X
143 Ecore_X_Atom x_atom_cbhm_count_get = 0;
145 if (atom_index == ATOM_INDEX_CBHM_COUNT_ALL)
146 x_atom_cbhm_count_get = ecore_x_atom_get(ATOM_CBHM_COUNT_GET);
147 else if (atom_index == ATOM_INDEX_CBHM_COUNT_TEXT)
148 x_atom_cbhm_count_get = ecore_x_atom_get(ATOM_CBHM_TEXT_COUNT_GET);
149 else if (atom_index == ATOM_INDEX_CBHM_COUNT_IMAGE)
150 x_atom_cbhm_count_get = ecore_x_atom_get(ATOM_CBHM_IMAGE_COUNT_GET);
152 Ecore_X_Window cbhm_xwin = _cbhm_window_get();
153 DMSG("x_win: 0x%x, x_atom: %d\n", cbhm_xwin, x_atom_cbhm_count_get);
154 ret = _cbhm_reply_get(cbhm_xwin, x_atom_cbhm_count_get, NULL, NULL);
158 DMSG("count: %d\n", count);
162 DMSG("ret: 0x%x\n", ret);
168 _cbhm_item_count_get(Evas_Object *obj)
170 #ifdef HAVE_ELEMENTARY_X
172 if(_cbhm_msg_send(obj, MSG_CBHM_COUNT_GET))
174 DMSG("message send success\n");
175 Ecore_X_Atom x_atom_cbhm_count_get = ecore_x_atom_get(ATOM_CBHM_COUNT_GET);
176 Ecore_X_Window xwin = ecore_evas_software_x11_window_get(
177 ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
178 DMSG("x_win: 0x%x, x_atom: %d\n", xwin, x_atom_cbhm_count_get);
179 ret = _cbhm_reply_get(xwin, x_atom_cbhm_count_get, NULL, NULL);
183 DMSG("count: %d\n", count);
187 DMSG("ret: 0x%x\n", ret);
194 #ifdef HAVE_ELEMENTARY_X
196 _cbhm_item_get(Evas_Object *obj __UNUSED__, int idx, Ecore_X_Atom *data_type, char **buf)
199 _cbhm_item_get(Evas_Object *obj, int idx, void *data_type, char **buf)
206 *(int *)data_type = 0;
208 #ifdef HAVE_ELEMENTARY_X
209 Ecore_X_Window cbhm_xwin = _cbhm_window_get();
213 snprintf(send_buf, 20, "CBHM_ITEM%d", idx);
214 Ecore_X_Atom x_atom_cbhm_item = ecore_x_atom_get(send_buf);
215 Ecore_X_Atom x_atom_item_type = 0;
217 DMSG("x_win: 0x%x, x_atom: %d\n", cbhm_xwin, x_atom_cbhm_item);
218 ret = _cbhm_reply_get(cbhm_xwin, x_atom_cbhm_item, &x_atom_item_type, NULL);
221 DMSG("data_type: %d, buf: %s\n", x_atom_item_type, ret);
228 *data_type = x_atom_item_type;
230 Ecore_X_Atom x_atom_cbhm_error = ecore_x_atom_get(ATOM_CBHM_ERROR);
231 if (x_atom_item_type == x_atom_cbhm_error)
234 DMSG("ret: 0x%x\n", ret);
236 Ecore_X_Window xwin = ecore_evas_software_x11_window_get(
237 ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
241 snprintf(send_buf, 20, "GET_ITEM%d", idx);
242 if (_cbhm_msg_send(obj, send_buf))
244 DMSG("message send success\n");
245 Ecore_X_Atom x_atom_cbhm_item = ecore_x_atom_get(ATOM_CBHM_ITEM);
246 Ecore_X_Atom x_atom_item_type = 0;
248 DMSG("x_win: 0x%x, x_atom: %d\n", xwin, x_atom_cbhm_item);
249 ret = _cbhm_reply_get(xwin, x_atom_cbhm_item, &x_atom_item_type, NULL);
252 DMSG("data_type: %d, buf: %s\n", x_atom_item_type, ret);
258 *data_type = x_atom_item_type;
260 Ecore_X_Atom x_atom_cbhm_error = ecore_x_atom_get(ATOM_CBHM_ERROR);
261 if (x_atom_item_type == x_atom_cbhm_error)
264 DMSG("ret: 0x%x\n", ret);
271 #ifdef HAVE_ELEMENTARY_X
273 _cbhm_item_set(Evas_Object *obj, Ecore_X_Atom data_type, char *item_data)
275 Ecore_X_Window x_cbhm_win = _cbhm_window_get();
276 Ecore_X_Atom x_atom_cbhm_item = ecore_x_atom_get(ATOM_CBHM_ITEM);
277 ecore_x_window_prop_property_set(x_cbhm_win, x_atom_cbhm_item, data_type, 8, item_data, strlen(item_data) + 1);
279 if (_cbhm_msg_send(obj, "SET_ITEM"))
281 DMSG("message send success\n");
289 _cbhm_serial_number_get()
291 unsigned int senum = 0;
292 #ifdef HAVE_ELEMENTARY_X
293 unsigned char *buf = NULL;
294 Ecore_X_Atom x_atom_cbhm_SN = ecore_x_atom_get(ATOM_CBHM_SERIAL_NUMBER);
295 Ecore_X_Window x_cbhm_win = _cbhm_window_get();
296 buf = _cbhm_reply_get(x_cbhm_win, x_atom_cbhm_SN, NULL, NULL);
299 memcpy(&senum, buf, sizeof(senum));
306 //////////////////////////////////////////////////////////////////////////////
307 // old elm_cbmh_helper //
308 //////////////////////////////////////////////////////////////////////////////
309 //#include <Elementary.h>
310 //#include "elm_priv.h"
312 //#ifdef HAVE_ELEMENTARY_X
313 //#include <X11/Xlib.h>
314 //#include <X11/Xatom.h>
318 // * @defgroup CBHM_helper CBHM_helper
319 // * @ingroup Elementary
321 // * retrieving date from Clipboard History Manager
322 // * CBHM_helper supports to get CBHM's contents
326 //#define ATOM_CLIPBOARD_NAME "CLIPBOARD"
327 //#define ATOM_CLIPBOARD_MANGER_NAME "CLIPBOARD_MANAGER"
328 //#define CLIPBOARD_MANAGER_WINDOW_TITLE_STRING "X11_CLIPBOARD_HISTORY_MANAGER"
329 //#define ATOM_CBHM_WINDOW_NAME "CBHM_XWIN"
331 //#ifdef HAVE_ELEMENTARY_X
332 //static Ecore_X_Display *cbhm_disp = NULL;
333 //static Ecore_X_Window cbhm_win = None;
334 //static Ecore_X_Window self_win = None;
336 //static Eina_Bool init_flag = EINA_FALSE;
338 //void _get_clipboard_window();
339 //unsigned int _get_cbhm_serial_number();
340 //void _search_clipboard_window(Ecore_X_Window w);
341 //int _send_clipboard_events(char *cmd);
342 //#ifdef HAVE_ELEMENTARY_X
343 //int _get_clipboard_data(Atom datom, char **datomptr);
346 //void _get_clipboard_window()
348 //#ifdef HAVE_ELEMENTARY_X
350 // int actual_format;
351 // unsigned long nitems, bytes_after;
352 // unsigned char *prop_return = NULL;
353 // Atom atomCbhmWin = XInternAtom(cbhm_disp, ATOM_CBHM_WINDOW_NAME, False);
355 // XGetWindowProperty(cbhm_disp, DefaultRootWindow(cbhm_disp), atomCbhmWin,
356 // 0, sizeof(Ecore_X_Window), False, XA_WINDOW,
357 // &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
360 // cbhm_win = *(Ecore_X_Window*)prop_return;
361 // XFree(prop_return);
362 // fprintf(stderr, "## find clipboard history manager at root\n");
367 //unsigned int _get_cbhm_serial_number()
369 // unsigned int senum = 0;
370 //#ifdef HAVE_ELEMENTARY_X
372 // int actual_format;
373 // unsigned long nitems, bytes_after;
374 // unsigned char *prop_return = NULL;
375 // Atom atomCbhmSN = XInternAtom(cbhm_disp, "CBHM_SERIAL_NUMBER", False);
377 // // FIXME : is it really needed?
378 // XSync(cbhm_disp, EINA_FALSE);
381 // XGetWindowProperty(cbhm_disp, cbhm_win, atomCbhmSN,
382 // 0, sizeof(Ecore_X_Window), False, XA_INTEGER,
383 // &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
386 // senum = *(unsigned int*)prop_return;
387 // XFree(prop_return);
389 // fprintf(stderr, "## chbm_serial = %d\n", senum);
394 //void _search_clipboard_window(Ecore_X_Window w)
396 //#ifdef HAVE_ELEMENTARY_X
397 // // Get the PID for the current Window.
398 // Atom atomWMName = XInternAtom(cbhm_disp, "_NET_WM_NAME", False);
399 // Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
402 // unsigned long nitems;
403 // unsigned long bytes_after;
404 // unsigned long nsize = 0;
405 // unsigned char *propName = 0;
407 // XGetWindowProperty(cbhm_disp, w, atomWMName, 0, (long)nsize, False,
408 // atomUTF8String, &type, &format, &nitems, &bytes_after, &propName))
413 // if (strcmp((const char *)CLIPBOARD_MANAGER_WINDOW_TITLE_STRING,(const char *)propName) == 0)
419 // // Recurse into child windows.
423 // unsigned nchildren;
425 // if(0 != XQueryTree(cbhm_disp, w, &wroot, &wparent, &wchild, &nchildren))
427 // for(i = 0; i < nchildren; i++)
428 // _search_clipboard_window(wchild[i]);
434 //int _send_clipboard_events(char *cmd)
439 //#ifdef HAVE_ELEMENTARY_X
440 // Atom atomCBHM_MSG = XInternAtom(cbhm_disp, "CBHM_MSG", False);
442 // XClientMessageEvent m;
443 // memset(&m, 0, sizeof(m));
444 // m.type = ClientMessage;
445 // m.display = cbhm_disp;
446 // m.window = self_win;
447 // m.message_type = atomCBHM_MSG;
449 // sprintf(m.data.b, "%s", cmd);
451 // XSendEvent(cbhm_disp, cbhm_win, False, NoEventMask, (XEvent*)&m);
456 //#ifdef HAVE_ELEMENTARY_X
457 //int _get_clipboard_data(Atom datom, char **datomptr)
459 // // Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
462 // unsigned long nitems;
463 // unsigned long nsize;
464 // unsigned char *propname = NULL;
466 // // FIXME : is it really needed?
467 // XSync(cbhm_disp, EINA_FALSE);
470 // XGetWindowProperty(cbhm_disp, self_win, datom, 0, 0, False,
471 // AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
479 // fprintf(stderr, "## format = %d\n", format);
480 // fprintf(stderr, "## nsize = %d\n", nsize);
487 // XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
488 // AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
492 // XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
493 // AnyPropertyType, &type, &format, &nitems, &nsize, &propname);
496 // if(propname != NULL)
498 // // fprintf(stderr, "## get data(0x%x) : %s\n", propname, propname);
499 // // fprintf(stderr, "## after nsize = %d\n", nsize);
500 // *datomptr = (char*)propname;
501 // // XFree(propName);
504 // XDeleteProperty(cbhm_disp, self_win, datom);
505 // XFlush(cbhm_disp);
508 // if (propname != NULL)
516 //void free_clipboard_data(char *dptr)
518 //#ifdef HAVE_ELEMENTARY_X
526 // * initalizing CBHM_helper
528 // * @param self The self window object which receive events
529 // * @return return TRUE or FALSE if it cannot be created
531 // * @ingroup CBHM_helper
534 //elm_cbhm_helper_init(Evas_Object *self)
536 // init_flag = EINA_FALSE;
538 //#ifdef HAVE_ELEMENTARY_X
539 // cbhm_disp = ecore_x_display_get();
540 // if (cbhm_disp == NULL)
542 // if (cbhm_win == None)
543 // _get_clipboard_window();
544 // if (cbhm_win == None)
545 // _search_clipboard_window(DefaultRootWindow(cbhm_disp));
546 // if (self_win == None)
547 // self_win = ecore_evas_software_x11_window_get(ecore_evas_ecore_evas_get(evas_object_evas_get(self)));
549 // if (cbhm_disp && cbhm_win && self_win)
550 // init_flag = EINA_TRUE;
556 // * getting serial number of CBHM
558 // * @return return serial number of clipboard history manager
560 // * @ingroup CBHM_helper
563 //elm_cbhm_get_serial_number()
565 // if (init_flag == EINA_FALSE)
568 // unsigned int num = 0;
569 // num = _get_cbhm_serial_number();
574 // * getting count of CBHM's contents
576 // * @return return count of history contents
578 // * @ingroup CBHM_helper
581 //elm_cbhm_get_count()
583 // if (init_flag == EINA_FALSE)
586 // char *retptr = NULL;
589 // _send_clipboard_events("get count");
591 //#ifdef HAVE_ELEMENTARY_X
592 // Atom atomCBHM_cCOUNT = XInternAtom(cbhm_disp, "CBHM_cCOUNT", False);
594 // _get_clipboard_data(atomCBHM_cCOUNT, &retptr);
597 // if (retptr != NULL)
599 // fprintf(stderr, "## c get retptr : %s\n", retptr);
600 // count = atoi(retptr);
602 // free_clipboard_data(retptr);
610 // * getting raw data of CBHM's contents
612 // * @return return raw data of history contents
614 // * @ingroup CBHM_helper
617 //elm_cbhm_get_raw_data()
619 // if (init_flag == EINA_FALSE)
622 // char *retptr = NULL;
624 // _send_clipboard_events("get raw");
626 //#ifdef HAVE_ELEMENTARY_X
627 // Atom atomCBHM_cRAW = XInternAtom(cbhm_disp, "CBHM_cRAW", False);
629 // _get_clipboard_data(atomCBHM_cRAW, &retptr);
632 // if (retptr != NULL)
634 // free_clipboard_data(retptr);
642 // * sending raw command to CBHM
646 // * @ingroup CBHM_helper
649 //elm_cbhm_send_raw_data(char *cmd)
651 // if (init_flag == EINA_FALSE)
657 // _send_clipboard_events(cmd);
658 // fprintf(stderr, "## cbhm - send raw cmd = %s\n", cmd);
664 // * getting data by history position of CBHM's contents
665 // * 0 is current content.
667 // * @return return data pointer of position of history contents
669 // * @ingroup CBHM_helper
672 //elm_cbhm_get_data_by_position(int pos, char **dataptr)
674 // if (init_flag == EINA_FALSE)
679 // sprintf(reqbuf, "get #%d", pos);
681 // _send_clipboard_events(reqbuf);
683 // sprintf(reqbuf, "CBHM_c%d", pos);
685 //#ifdef HAVE_ELEMENTARY_X
686 // Atom atomCBHM_cPOS = XInternAtom(cbhm_disp, reqbuf, False);
688 // ret = _get_clipboard_data(atomCBHM_cPOS, dataptr);
691 // if (ret >= 0 && *dataptr != NULL)
693 // // fprintf(stderr, "## d get retptr : %s\n", *dataptr);
694 // // fprintf(stderr, "## dptr = 0x%x\n", *dataptr);
703 // * free data by history position of CBHM's contents
707 // * @ingroup CBHM_helper
710 //elm_cbhm_free_data(char *dptr)
713 // free_clipboard_data(dptr);