1 #include <Elementary.h>
9 * @defgroup CBHM_helper CBHM_helper
12 * retrieving date from Clipboard History Manager
13 * CBHM_helper supports to get CBHM's contents
16 #define ATOM_CLIPBOARD_NAME "CLIPBOARD"
17 #define ATOM_CLIPBOARD_MANGER_NAME "CLIPBOARD_MANAGER"
18 #define CLIPBOARD_MANAGER_WINDOW_TITLE_STRING "X11_CLIPBOARD_HISTORY_MANAGER"
19 #define ATOM_CBHM_WINDOW_NAME "CBHM_XWIN"
21 static Ecore_X_Display *cbhm_disp = NULL;
22 static Ecore_X_Window cbhm_win = None;
23 static Ecore_X_Window self_win = None;
24 static Eina_Bool init_flag = EINA_FALSE;
26 void _get_clipboard_window();
27 unsigned int _get_cbhm_serial_number();
28 void _search_clipboard_window(Ecore_X_Window w);
29 int _send_clipboard_events(char *cmd);
30 int _get_clipboard_data(Atom datom, char **datomptr);
32 void _get_clipboard_window()
36 unsigned long nitems, bytes_after;
37 unsigned char *prop_return = NULL;
38 Atom atomCbhmWin = XInternAtom(cbhm_disp, ATOM_CBHM_WINDOW_NAME, False);
40 XGetWindowProperty(cbhm_disp, DefaultRootWindow(cbhm_disp), atomCbhmWin,
41 0, sizeof(Ecore_X_Window), False, XA_WINDOW,
42 &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
45 cbhm_win = *(Ecore_X_Window*)prop_return;
47 fprintf(stderr, "## find clipboard history manager at root\n");
51 unsigned int _get_cbhm_serial_number()
55 unsigned long nitems, bytes_after;
56 unsigned char *prop_return = NULL;
57 unsigned int senum = 0;
58 Atom atomCbhmSN = XInternAtom(cbhm_disp, "CBHM_SERIAL_NUMBER", False);
60 // FIXME : is it really needed?
61 XSync(cbhm_disp, EINA_FALSE);
64 XGetWindowProperty(cbhm_disp, cbhm_win, atomCbhmSN,
65 0, sizeof(Ecore_X_Window), False, XA_INTEGER,
66 &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
69 senum = *(unsigned int*)prop_return;
72 fprintf(stderr, "## chbm_serial = %d\n", senum);
76 void _search_clipboard_window(Ecore_X_Window w)
78 // Get the PID for the current Window.
79 Atom atomWMName = XInternAtom(cbhm_disp, "_NET_WM_NAME", False);
80 Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
84 unsigned long bytes_after;
86 unsigned char *propName = 0;
88 XGetWindowProperty(cbhm_disp, w, atomWMName, 0, (long)nsize, False,
89 atomUTF8String, &type, &format, &nitems, &bytes_after, &propName))
94 if (strcmp(CLIPBOARD_MANAGER_WINDOW_TITLE_STRING, propName) == 0)
100 // Recurse into child windows.
106 if(0 != XQueryTree(cbhm_disp, w, &wroot, &wparent, &wchild, &nchildren))
108 for(i = 0; i < nchildren; i++)
109 _search_clipboard_window(wchild[i]);
113 int _send_clipboard_events(char *cmd)
118 Atom atomCBHM_MSG = XInternAtom(cbhm_disp, "CBHM_MSG", False);
120 XClientMessageEvent m;
121 memset(&m, sizeof(m), 0);
122 m.type = ClientMessage;
123 m.display = cbhm_disp;
125 m.message_type = atomCBHM_MSG;
127 sprintf(m.data.b, "%s", cmd);
129 XSendEvent(cbhm_disp, cbhm_win, False, NoEventMask, (XEvent*)&m);
134 int _get_clipboard_data(Atom datom, char **datomptr)
136 Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
139 unsigned long nitems;
141 unsigned char *propname = NULL;
143 // FIXME : is it really needed?
144 XSync(cbhm_disp, EINA_FALSE);
147 XGetWindowProperty(cbhm_disp, self_win, datom, 0, 0, False,
148 AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
153 fprintf(stderr, "## format = %d\n", format);
154 fprintf(stderr, "## nsize = %d\n", nsize);
160 XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
161 AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
164 XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
165 AnyPropertyType, &type, &format, &nitems, &nsize, &propname);
169 fprintf(stderr, "## get data(0x%x) : %s\n", propname, propname);
170 fprintf(stderr, "## after nsize = %d\n", nsize);
171 *datomptr = propname;
175 XDeleteProperty(cbhm_disp, self_win, datom);
179 if (propname != NULL)
187 void free_clipboard_data(char *dptr)
194 * initalizing CBHM_helper
196 * @param self The self window object which receive events
197 * @return return TRUE or FALSE if it cannot be created
199 * @ingroup CBHM_helper
202 elm_cbhm_helper_init(Evas_Object *self)
204 init_flag = EINA_FALSE;
206 cbhm_disp = ecore_x_display_get();
207 if (cbhm_disp == NULL)
209 if (cbhm_win == None)
210 _get_clipboard_window();
211 if (cbhm_win == None)
212 _search_clipboard_window(DefaultRootWindow(cbhm_disp));
213 if (self_win == None)
214 self_win = ecore_evas_software_x11_window_get(ecore_evas_ecore_evas_get(evas_object_evas_get(self)));
216 if (cbhm_disp && cbhm_win && self_win)
217 init_flag = EINA_TRUE;
223 * getting serial number of CBHM
225 * @return return serial number of clipboard history manager
227 * @ingroup CBHM_helper
230 elm_cbhm_get_serial_number()
232 if (init_flag == EINA_FALSE)
235 unsigned int num = 0;
236 num = _get_cbhm_serial_number();
241 * getting count of CBHM's contents
243 * @return return count of history contents
245 * @ingroup CBHM_helper
250 if (init_flag == EINA_FALSE)
256 _send_clipboard_events("get count");
258 Atom atomCBHM_cCOUNT = XInternAtom(cbhm_disp, "CBHM_cCOUNT", False);
260 _get_clipboard_data(atomCBHM_cCOUNT, &retptr);
264 fprintf(stderr, "## c get retptr : %s\n", retptr);
265 count = atoi(retptr);
267 free_clipboard_data(retptr);
275 * getting raw data of CBHM's contents
277 * @return return raw data of history contents
279 * @ingroup CBHM_helper
282 elm_cbhm_get_raw_data()
284 if (init_flag == EINA_FALSE)
289 _send_clipboard_events("get raw");
291 Atom atomCBHM_cRAW = XInternAtom(cbhm_disp, "CBHM_cRAW", False);
293 _get_clipboard_data(atomCBHM_cRAW, &retptr);
297 free_clipboard_data(retptr);
305 * sending raw command to CBHM
309 * @ingroup CBHM_helper
312 elm_cbhm_send_raw_data(char *cmd)
314 if (init_flag == EINA_FALSE)
320 _send_clipboard_events(cmd);
321 fprintf(stderr, "## cbhm - send raw cmd = %s\n", cmd);
327 * getting data by history position of CBHM's contents
328 * 0 is current content.
330 * @return return data pointer of position of history contents
332 * @ingroup CBHM_helper
335 elm_cbhm_get_data_by_position(int pos, char **dataptr)
337 if (init_flag == EINA_FALSE)
342 sprintf(reqbuf, "get #%d", pos);
344 _send_clipboard_events(reqbuf);
346 sprintf(reqbuf, "CBHM_c%d", pos);
348 Atom atomCBHM_cPOS = XInternAtom(cbhm_disp, reqbuf, False);
350 ret = _get_clipboard_data(atomCBHM_cPOS, dataptr);
352 if (ret >= 0 && *dataptr != NULL)
354 fprintf(stderr, "## d get retptr : %s\n", *dataptr);
355 fprintf(stderr, "## dptr = 0x%x\n", *dataptr);
364 * free data by history position of CBHM's contents
368 * @ingroup CBHM_helper
371 elm_cbhm_free_data(char *dptr)
374 free_clipboard_data(dptr);