1 #include <Elementary.h>
4 #ifdef HAVE_ELEMENTARY_X
10 * @defgroup CBHM_helper CBHM_helper
13 * retrieving date from Clipboard History Manager
14 * CBHM_helper supports to get CBHM's contents
17 #define ATOM_CLIPBOARD_NAME "CLIPBOARD"
18 #define ATOM_CLIPBOARD_MANGER_NAME "CLIPBOARD_MANAGER"
19 #define CLIPBOARD_MANAGER_WINDOW_TITLE_STRING "X11_CLIPBOARD_HISTORY_MANAGER"
20 #define ATOM_CBHM_WINDOW_NAME "CBHM_XWIN"
22 #ifdef HAVE_ELEMENTARY_X
23 static Ecore_X_Display *cbhm_disp = NULL;
24 static Ecore_X_Window cbhm_win = None;
25 static Ecore_X_Window self_win = None;
27 static Eina_Bool init_flag = EINA_FALSE;
29 void _get_clipboard_window();
30 unsigned int _get_cbhm_serial_number();
31 void _search_clipboard_window(Ecore_X_Window w);
32 int _send_clipboard_events(char *cmd);
33 #ifdef HAVE_ELEMENTARY_X
34 int _get_clipboard_data(Atom datom, char **datomptr);
37 void _get_clipboard_window()
39 #ifdef HAVE_ELEMENTARY_X
42 unsigned long nitems, bytes_after;
43 unsigned char *prop_return = NULL;
44 Atom atomCbhmWin = XInternAtom(cbhm_disp, ATOM_CBHM_WINDOW_NAME, False);
46 XGetWindowProperty(cbhm_disp, DefaultRootWindow(cbhm_disp), atomCbhmWin,
47 0, sizeof(Ecore_X_Window), False, XA_WINDOW,
48 &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
51 cbhm_win = *(Ecore_X_Window*)prop_return;
53 fprintf(stderr, "## find clipboard history manager at root\n");
58 unsigned int _get_cbhm_serial_number()
60 unsigned int senum = 0;
61 #ifdef HAVE_ELEMENTARY_X
64 unsigned long nitems, bytes_after;
65 unsigned char *prop_return = NULL;
66 Atom atomCbhmSN = XInternAtom(cbhm_disp, "CBHM_SERIAL_NUMBER", False);
68 // FIXME : is it really needed?
69 XSync(cbhm_disp, EINA_FALSE);
72 XGetWindowProperty(cbhm_disp, cbhm_win, atomCbhmSN,
73 0, sizeof(Ecore_X_Window), False, XA_INTEGER,
74 &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
77 senum = *(unsigned int*)prop_return;
80 fprintf(stderr, "## chbm_serial = %d\n", senum);
85 void _search_clipboard_window(Ecore_X_Window w)
87 #ifdef HAVE_ELEMENTARY_X
88 // Get the PID for the current Window.
89 Atom atomWMName = XInternAtom(cbhm_disp, "_NET_WM_NAME", False);
90 Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
94 unsigned long bytes_after;
95 unsigned long nsize = 0;
96 unsigned char *propName = 0;
98 XGetWindowProperty(cbhm_disp, w, atomWMName, 0, (long)nsize, False,
99 atomUTF8String, &type, &format, &nitems, &bytes_after, &propName))
104 if (strcmp((const char *)CLIPBOARD_MANAGER_WINDOW_TITLE_STRING,(const char *)propName) == 0)
110 // Recurse into child windows.
116 if(0 != XQueryTree(cbhm_disp, w, &wroot, &wparent, &wchild, &nchildren))
118 for(i = 0; i < nchildren; i++)
119 _search_clipboard_window(wchild[i]);
124 int _send_clipboard_events(char *cmd)
129 #ifdef HAVE_ELEMENTARY_X
130 Atom atomCBHM_MSG = XInternAtom(cbhm_disp, "CBHM_MSG", False);
132 XClientMessageEvent m;
133 memset(&m, sizeof(m), 0);
134 m.type = ClientMessage;
135 m.display = cbhm_disp;
137 m.message_type = atomCBHM_MSG;
139 sprintf(m.data.b, "%s", cmd);
141 XSendEvent(cbhm_disp, cbhm_win, False, NoEventMask, (XEvent*)&m);
146 #ifdef HAVE_ELEMENTARY_X
147 int _get_clipboard_data(Atom datom, char **datomptr)
149 // Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
152 unsigned long nitems;
154 unsigned char *propname = NULL;
156 // FIXME : is it really needed?
157 XSync(cbhm_disp, EINA_FALSE);
160 XGetWindowProperty(cbhm_disp, self_win, datom, 0, 0, False,
161 AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
167 fprintf(stderr, "## format = %d\n", format);
168 fprintf(stderr, "## nsize = %d\n", nsize);
175 XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
176 AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
179 XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
180 AnyPropertyType, &type, &format, &nitems, &nsize, &propname);
184 // fprintf(stderr, "## get data(0x%x) : %s\n", propname, propname);
185 // fprintf(stderr, "## after nsize = %d\n", nsize);
186 *datomptr = (char*)propname;
190 XDeleteProperty(cbhm_disp, self_win, datom);
194 if (propname != NULL)
202 void free_clipboard_data(char *dptr)
204 #ifdef HAVE_ELEMENTARY_X
212 * initalizing CBHM_helper
214 * @param self The self window object which receive events
215 * @return return TRUE or FALSE if it cannot be created
217 * @ingroup CBHM_helper
220 elm_cbhm_helper_init(Evas_Object *self)
222 init_flag = EINA_FALSE;
224 #ifdef HAVE_ELEMENTARY_X
225 cbhm_disp = ecore_x_display_get();
226 if (cbhm_disp == NULL)
228 if (cbhm_win == None)
229 _get_clipboard_window();
230 if (cbhm_win == None)
231 _search_clipboard_window(DefaultRootWindow(cbhm_disp));
232 if (self_win == None)
233 self_win = ecore_evas_software_x11_window_get(ecore_evas_ecore_evas_get(evas_object_evas_get(self)));
235 if (cbhm_disp && cbhm_win && self_win)
236 init_flag = EINA_TRUE;
242 * getting serial number of CBHM
244 * @return return serial number of clipboard history manager
246 * @ingroup CBHM_helper
249 elm_cbhm_get_serial_number()
251 if (init_flag == EINA_FALSE)
254 unsigned int num = 0;
255 num = _get_cbhm_serial_number();
260 * getting count of CBHM's contents
262 * @return return count of history contents
264 * @ingroup CBHM_helper
269 if (init_flag == EINA_FALSE)
275 _send_clipboard_events("get count");
277 #ifdef HAVE_ELEMENTARY_X
278 Atom atomCBHM_cCOUNT = XInternAtom(cbhm_disp, "CBHM_cCOUNT", False);
280 _get_clipboard_data(atomCBHM_cCOUNT, &retptr);
285 fprintf(stderr, "## c get retptr : %s\n", retptr);
286 count = atoi(retptr);
288 free_clipboard_data(retptr);
296 * getting raw data of CBHM's contents
298 * @return return raw data of history contents
300 * @ingroup CBHM_helper
303 elm_cbhm_get_raw_data()
305 if (init_flag == EINA_FALSE)
310 _send_clipboard_events("get raw");
312 #ifdef HAVE_ELEMENTARY_X
313 Atom atomCBHM_cRAW = XInternAtom(cbhm_disp, "CBHM_cRAW", False);
315 _get_clipboard_data(atomCBHM_cRAW, &retptr);
320 free_clipboard_data(retptr);
328 * sending raw command to CBHM
332 * @ingroup CBHM_helper
335 elm_cbhm_send_raw_data(char *cmd)
337 if (init_flag == EINA_FALSE)
343 _send_clipboard_events(cmd);
344 fprintf(stderr, "## cbhm - send raw cmd = %s\n", cmd);
350 * getting data by history position of CBHM's contents
351 * 0 is current content.
353 * @return return data pointer of position of history contents
355 * @ingroup CBHM_helper
358 elm_cbhm_get_data_by_position(int pos, char **dataptr)
360 if (init_flag == EINA_FALSE)
365 sprintf(reqbuf, "get #%d", pos);
367 _send_clipboard_events(reqbuf);
369 sprintf(reqbuf, "CBHM_c%d", pos);
371 #ifdef HAVE_ELEMENTARY_X
372 Atom atomCBHM_cPOS = XInternAtom(cbhm_disp, reqbuf, False);
374 ret = _get_clipboard_data(atomCBHM_cPOS, dataptr);
377 if (ret >= 0 && *dataptr != NULL)
379 // fprintf(stderr, "## d get retptr : %s\n", *dataptr);
380 // fprintf(stderr, "## dptr = 0x%x\n", *dataptr);
389 * free data by history position of CBHM's contents
393 * @ingroup CBHM_helper
396 elm_cbhm_free_data(char *dptr)
399 free_clipboard_data(dptr);