[Copy&Paste] Fix Windows Build Error
[framework/uifw/elementary.git] / src / modules / ctxpopup_copypasteUI / cbhm_helper.c
1 #include "cbhm_helper.h"
2
3 #ifdef HAVE_ELEMENTARY_X
4 #include <X11/Xlib.h>
5 #include <X11/Xatom.h>
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_SERIAL_NUMBER "CBHM_SERIAL_NUMBER"
10 #define MSG_CBHM_COUNT_GET "get count"
11 #define ATOM_CBHM_ERROR "CBHM_ERROR"
12 #define ATOM_CBHM_ITEM "CBHM_ITEM"
13 #endif
14
15 #ifdef HAVE_ELEMENTARY_X
16 Ecore_X_Window
17 _cbhm_window_get()
18 {
19    Ecore_X_Atom x_atom_cbhm = ecore_x_atom_get(ATOM_CBHM_WINDOW_NAME);
20    Ecore_X_Window x_cbhm_win = 0;
21    unsigned char *buf = NULL;
22    int num = 0;
23    int ret = ecore_x_window_prop_property_get(0, x_atom_cbhm, XA_WINDOW, 0, &buf, &num);
24    DMSG("ret: %d, num: %d\n", ret, num);
25    if (ret && num)
26      memcpy(&x_cbhm_win, buf, sizeof(Ecore_X_Window));
27    if (buf)
28      free(buf);
29    return x_cbhm_win;
30 }
31 #endif
32
33 Eina_Bool
34 _cbhm_msg_send(Evas_Object *obj, char *msg)
35 {
36 #ifdef HAVE_ELEMENTARY_X
37    Ecore_X_Window x_cbhm_win = _cbhm_window_get();
38    Ecore_X_Atom x_atom_cbhm_msg = ecore_x_atom_get(ATOM_CBHM_MSG);
39    Ecore_X_Window xwin = ecore_evas_software_x11_window_get(
40       ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
41
42    DMSG("x_cbhm: 0x%x\n", x_cbhm_win);
43    if (!x_cbhm_win || !x_atom_cbhm_msg)
44      return EINA_FALSE;
45
46    XClientMessageEvent m;
47    memset(&m, 0, sizeof(m));
48    m.type = ClientMessage;
49    m.display = ecore_x_display_get();
50    m.window = xwin;
51    m.message_type = x_atom_cbhm_msg;
52    m.format = 8;
53    snprintf(m.data.b, 20, "%s", msg);
54
55    XSendEvent(ecore_x_display_get(), x_cbhm_win, False, NoEventMask, (XEvent*)&m);
56
57    ecore_x_sync();
58    return EINA_TRUE;
59 #else
60    return EINA_FALSE;
61 #endif
62 }
63
64 #ifdef HAVE_ELEMENTARY_X
65 void *
66 _cbhm_reply_get(Ecore_X_Window xwin, Ecore_X_Atom property, Ecore_X_Atom *x_data_type, int *num)
67 {
68    unsigned char *data = NULL;
69    if (x_data_type)
70      *x_data_type = 0;
71    if (!property)
72       return NULL;
73    ecore_x_sync();
74    if (num)
75      *num = 0;
76
77    long unsigned int num_ret = 0, bytes = 0;
78    int ret = 0, size_ret;
79    unsigned int i;
80    unsigned char *prop_ret;
81    Ecore_X_Atom type_ret;
82    ret = XGetWindowProperty(ecore_x_display_get(), xwin, property, 0, LONG_MAX,
83                             False, ecore_x_window_prop_any_type(), (Atom *)&type_ret, &size_ret,
84                             &num_ret, &bytes, &prop_ret);
85    if (ret != Success)
86      return NULL;
87    if (!num_ret)
88      {
89         XFree(prop_ret);
90         return NULL;
91      }
92
93    if (!(data = malloc(num_ret * size_ret / 8)))
94      {
95         XFree(prop_ret);
96         return NULL;
97      }
98
99    switch (size_ret) {
100       case 8:
101         for (i = 0; i < num_ret; i++)
102           (data)[i] = prop_ret[i];
103         break;
104
105       case 16:
106         for (i = 0; i < num_ret; i++)
107           ((unsigned short *)data)[i] = ((unsigned short *)prop_ret)[i];
108         break;
109
110       case 32:
111         for (i = 0; i < num_ret; i++)
112           ((unsigned int *)data)[i] = ((unsigned long *)prop_ret)[i];
113         break;
114      }
115
116    XFree(prop_ret);
117
118    if (num)
119      *num = num_ret;
120    if (x_data_type)
121      *x_data_type = type_ret;
122
123    return data;
124 }
125 #endif
126
127 int
128 _cbhm_item_count_get(Evas_Object *obj)
129 {
130 #ifdef HAVE_ELEMENTARY_X
131    char *ret, count;
132    if(_cbhm_msg_send(obj, MSG_CBHM_COUNT_GET))
133      {
134         DMSG("message send success\n");
135         Ecore_X_Atom x_atom_cbhm_count_get = ecore_x_atom_get(ATOM_CBHM_COUNT_GET);
136         Ecore_X_Window xwin = ecore_evas_software_x11_window_get(
137            ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
138         DMSG("x_win: 0x%x, x_atom: %d\n", xwin, x_atom_cbhm_count_get);
139         ret = _cbhm_reply_get(xwin, x_atom_cbhm_count_get, NULL, NULL);
140         if (ret)
141           {
142              count = atoi(ret);
143              DMSG("count: %d\n", count);
144              free(ret);
145              return count;
146           }
147         DMSG("ret: 0x%x\n", ret);
148      }
149 #endif
150    return -1;
151 }
152
153 #ifdef HAVE_ELEMENTARY_X
154 Eina_Bool
155 _cbhm_item_get(Evas_Object *obj, int index, Ecore_X_Atom *data_type, char **buf)
156 #else
157 Eina_Bool
158 _cbhm_item_get(Evas_Object *obj, int index, void *data_type, char **buf)
159 #endif
160
161 {
162    if (buf)
163      *buf = NULL;
164    if (data_type)
165      *(int *)data_type = 0;
166
167 #ifdef HAVE_ELEMENTARY_X
168    Ecore_X_Window xwin = ecore_evas_software_x11_window_get(
169       ecore_evas_ecore_evas_get(evas_object_evas_get(obj)));
170    char send_buf[20];
171    char *ret;
172
173    snprintf(send_buf, 20, "GET_ITEM%d", index);
174    if (_cbhm_msg_send(obj, send_buf))
175      {
176         DMSG("message send success\n");
177         Ecore_X_Atom x_atom_cbhm_item = ecore_x_atom_get(ATOM_CBHM_ITEM);
178         Ecore_X_Atom x_atom_item_type = 0;
179
180         DMSG("x_win: 0x%x, x_atom: %d\n", xwin, x_atom_cbhm_item);
181         ret = _cbhm_reply_get(xwin, x_atom_cbhm_item, &x_atom_item_type, NULL);
182         if (ret)
183           {
184              DMSG("data_type: %d, buf: %s\n", x_atom_item_type, ret);
185              if (buf)
186                *buf = ret;
187              else
188                free(ret);
189              if (data_type)
190                *data_type = x_atom_item_type;
191
192              Ecore_X_Atom x_atom_cbhm_error = ecore_x_atom_get(ATOM_CBHM_ERROR);
193              if (x_atom_item_type == x_atom_cbhm_error)
194                return EINA_FALSE;
195           }
196         DMSG("ret: 0x%x\n", ret);
197      }
198 #endif
199    return EINA_FALSE;
200 }
201
202 unsigned int
203 _cbhm_serial_number_get()
204 {
205    unsigned int senum = 0;
206 #ifdef HAVE_ELEMENTARY_X
207    unsigned char *buf = NULL;
208    Ecore_X_Atom x_atom_cbhm_SN = ecore_x_atom_get(ATOM_CBHM_SERIAL_NUMBER);
209    Ecore_X_Window x_cbhm_win = _cbhm_window_get();
210    buf = _cbhm_reply_get(x_cbhm_win, x_atom_cbhm_SN, NULL, NULL);
211    if (buf)
212      {
213         memcpy(&senum, buf, sizeof(senum));
214         free(buf);
215      }
216 #endif
217    return senum;
218 }
219
220 //////////////////////////////////////////////////////////////////////////////
221 // old elm_cbmh_helper                                                      //
222 //////////////////////////////////////////////////////////////////////////////
223 //#include <Elementary.h>
224 //#include "elm_priv.h"
225 //
226 //#ifdef HAVE_ELEMENTARY_X
227 //#include <X11/Xlib.h>
228 //#include <X11/Xatom.h>
229 //#endif
230 //
231 ///**
232 // * @defgroup CBHM_helper CBHM_helper
233 // * @ingroup Elementary
234 // *
235 // * retrieving date from Clipboard History Manager
236 // * CBHM_helper supports to get CBHM's contents
237 // */
238 //
239 //
240 //#define ATOM_CLIPBOARD_NAME "CLIPBOARD"
241 //#define ATOM_CLIPBOARD_MANGER_NAME "CLIPBOARD_MANAGER"
242 //#define CLIPBOARD_MANAGER_WINDOW_TITLE_STRING "X11_CLIPBOARD_HISTORY_MANAGER"
243 //#define ATOM_CBHM_WINDOW_NAME "CBHM_XWIN"
244 //
245 //#ifdef HAVE_ELEMENTARY_X
246 //static Ecore_X_Display *cbhm_disp = NULL;
247 //static Ecore_X_Window cbhm_win = None;
248 //static Ecore_X_Window self_win = None;
249 //#endif
250 //static Eina_Bool init_flag = EINA_FALSE;
251 //
252 //void _get_clipboard_window();
253 //unsigned int _get_cbhm_serial_number();
254 //void _search_clipboard_window(Ecore_X_Window w);
255 //int _send_clipboard_events(char *cmd);
256 //#ifdef HAVE_ELEMENTARY_X
257 //int _get_clipboard_data(Atom datom, char **datomptr);
258 //#endif
259 //
260 //void _get_clipboard_window()
261 //{
262 //#ifdef HAVE_ELEMENTARY_X
263 //   Atom actual_type;
264 //   int actual_format;
265 //   unsigned long nitems, bytes_after;
266 //   unsigned char *prop_return = NULL;
267 //   Atom atomCbhmWin = XInternAtom(cbhm_disp, ATOM_CBHM_WINDOW_NAME, False);
268 //   if(Success ==
269 //      XGetWindowProperty(cbhm_disp, DefaultRootWindow(cbhm_disp), atomCbhmWin,
270 //                         0, sizeof(Ecore_X_Window), False, XA_WINDOW,
271 //                         &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
272 //      prop_return)
273 //     {
274 //        cbhm_win = *(Ecore_X_Window*)prop_return;
275 //        XFree(prop_return);
276 //        fprintf(stderr, "## find clipboard history manager at root\n");
277 //     }
278 //#endif
279 //}
280 //
281 //unsigned int _get_cbhm_serial_number()
282 //{
283 //   unsigned int senum = 0;
284 //#ifdef HAVE_ELEMENTARY_X
285 //   Atom actual_type;
286 //   int actual_format;
287 //   unsigned long nitems, bytes_after;
288 //   unsigned char *prop_return = NULL;
289 //   Atom atomCbhmSN = XInternAtom(cbhm_disp, "CBHM_SERIAL_NUMBER", False);
290 //
291 //   // FIXME : is it really needed?
292 //   XSync(cbhm_disp, EINA_FALSE);
293 //
294 //   if(Success ==
295 //      XGetWindowProperty(cbhm_disp, cbhm_win, atomCbhmSN,
296 //                         0, sizeof(Ecore_X_Window), False, XA_INTEGER,
297 //                         &actual_type, &actual_format, &nitems, &bytes_after, &prop_return) &&
298 //      prop_return)
299 //     {
300 //        senum = *(unsigned int*)prop_return;
301 //        XFree(prop_return);
302 //     }
303 //   fprintf(stderr, "## chbm_serial = %d\n", senum);
304 //#endif
305 //   return senum;
306 //}
307 //
308 //void _search_clipboard_window(Ecore_X_Window w)
309 //{
310 //#ifdef HAVE_ELEMENTARY_X
311 //   // Get the PID for the current Window.
312 //   Atom atomWMName = XInternAtom(cbhm_disp, "_NET_WM_NAME", False);
313 //   Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
314 //   Atom type;
315 //   int format;
316 //   unsigned long nitems;
317 //   unsigned long bytes_after;
318 //   unsigned long nsize = 0;
319 //   unsigned char *propName = 0;
320 //   if(Success ==
321 //      XGetWindowProperty(cbhm_disp, w, atomWMName, 0, (long)nsize, False,
322 //                         atomUTF8String, &type, &format, &nitems, &bytes_after, &propName))
323 //
324 //     {
325 //        if(propName != 0)
326 //          {
327 //             if (strcmp((const char *)CLIPBOARD_MANAGER_WINDOW_TITLE_STRING,(const char *)propName) == 0)
328 //               cbhm_win = w;
329 //             XFree(propName);
330 //          }
331 //     }
332 //
333 //   // Recurse into child windows.
334 //   Window wroot;
335 //   Window wparent;
336 //   Window *wchild;
337 //   unsigned nchildren;
338 //   int i;
339 //   if(0 != XQueryTree(cbhm_disp, w, &wroot, &wparent, &wchild, &nchildren))
340 //     {
341 //        for(i = 0; i < nchildren; i++)
342 //          _search_clipboard_window(wchild[i]);
343 //        XFree(wchild);
344 //     }
345 //#endif
346 //}
347 //
348 //int _send_clipboard_events(char *cmd)
349 //{
350 //   if (cmd == NULL)
351 //     return -1;
352 //
353 //#ifdef HAVE_ELEMENTARY_X
354 //   Atom atomCBHM_MSG = XInternAtom(cbhm_disp, "CBHM_MSG", False);
355 //
356 //   XClientMessageEvent m;
357 //   memset(&m, 0, sizeof(m));
358 //   m.type = ClientMessage;
359 //   m.display = cbhm_disp;
360 //   m.window = self_win;
361 //   m.message_type = atomCBHM_MSG;
362 //   m.format = 8;
363 //   sprintf(m.data.b, "%s", cmd);
364 //
365 //   XSendEvent(cbhm_disp, cbhm_win, False, NoEventMask, (XEvent*)&m);
366 //#endif
367 //   return 0;
368 //}
369 //
370 //#ifdef HAVE_ELEMENTARY_X
371 //int _get_clipboard_data(Atom datom, char **datomptr)
372 //{
373 //   // Atom atomUTF8String = XInternAtom(cbhm_disp, "UTF8_STRING", False);
374 //   Atom type;
375 //   int format;
376 //   unsigned long nitems;
377 //   unsigned long nsize;
378 //   unsigned char *propname = NULL;
379 //
380 //   // FIXME : is it really needed?
381 //   XSync(cbhm_disp, EINA_FALSE);
382 //
383 //   if (Success ==
384 //       XGetWindowProperty(cbhm_disp, self_win, datom, 0, 0, False,
385 //                          AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
386 //     {
387 //        XFree(propname);
388 //     }
389 //   else
390 //     return -1;
391 //
392 //   /*
393 //      fprintf(stderr, "## format = %d\n", format);
394 //      fprintf(stderr, "## nsize = %d\n", nsize);
395 //    */
396 //
397 //   if (format != 8)
398 //     return -1;
399 //
400 //   if (Success ==
401 //       XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
402 //                          AnyPropertyType, &type, &format, &nitems, &nsize, &propname))
403 //     {
404 //        if (nsize != 0)
405 //          {
406 //             XGetWindowProperty(cbhm_disp, self_win, datom, 0, (long)nsize, False,
407 //                                AnyPropertyType, &type, &format, &nitems, &nsize, &propname);
408 //          }
409 //
410 //        if(propname != NULL)
411 //          {
412 //             //                       fprintf(stderr, "## get data(0x%x) : %s\n", propname, propname);
413 //             //                       fprintf(stderr, "## after nsize = %d\n", nsize);
414 //             *datomptr = (char*)propname;
415 //             //                       XFree(propName);
416 //          }
417 //
418 //        XDeleteProperty(cbhm_disp, self_win, datom);
419 //        XFlush(cbhm_disp);
420 //     }
421 //
422 //   if (propname != NULL)
423 //     return 0;
424 //
425 //   *datomptr = NULL;
426 //   return -1;
427 //}
428 //#endif
429 //
430 //void free_clipboard_data(char *dptr)
431 //{
432 //#ifdef HAVE_ELEMENTARY_X
433 //   XFree(dptr);
434 //   return;
435 //#endif
436 //}
437 //
438 //
439 ///**
440 // * initalizing CBHM_helper
441 // *
442 // * @param self The self window object which receive events
443 // * @return return TRUE or FALSE if it cannot be created
444 // *
445 // * @ingroup CBHM_helper
446 // */
447 //EAPI Eina_Bool
448 //elm_cbhm_helper_init(Evas_Object *self)
449 //{
450 //   init_flag = EINA_FALSE;
451 //
452 //#ifdef HAVE_ELEMENTARY_X
453 //   cbhm_disp = ecore_x_display_get();
454 //   if (cbhm_disp == NULL)
455 //     return init_flag;
456 //   if (cbhm_win == None)
457 //     _get_clipboard_window();
458 //   if (cbhm_win == None)
459 //     _search_clipboard_window(DefaultRootWindow(cbhm_disp));
460 //   if (self_win == None)
461 //     self_win = ecore_evas_software_x11_window_get(ecore_evas_ecore_evas_get(evas_object_evas_get(self)));
462 //
463 //   if (cbhm_disp && cbhm_win && self_win)
464 //     init_flag = EINA_TRUE;
465 //#endif
466 //   return init_flag;
467 //}
468 //
469 ///**
470 // * getting serial number of CBHM
471 // *
472 // * @return return serial number of clipboard history manager
473 // *
474 // * @ingroup CBHM_helper
475 // */
476 //EAPI unsigned int
477 //elm_cbhm_get_serial_number()
478 //{
479 //   if (init_flag == EINA_FALSE)
480 //     return 0;
481 //
482 //   unsigned int num = 0;
483 //   num = _get_cbhm_serial_number();
484 //   return num;
485 //}
486 //
487 ///**
488 // * getting count of CBHM's contents
489 // *
490 // * @return return count of history contents
491 // *
492 // * @ingroup CBHM_helper
493 // */
494 //EAPI int
495 //elm_cbhm_get_count()
496 //{
497 //   if (init_flag == EINA_FALSE)
498 //     return -1;
499 //
500 //   char *retptr = NULL;
501 //   int count = 0;
502 //
503 //   _send_clipboard_events("get count");
504 //
505 //#ifdef HAVE_ELEMENTARY_X
506 //   Atom atomCBHM_cCOUNT = XInternAtom(cbhm_disp, "CBHM_cCOUNT", False);
507 //
508 //   _get_clipboard_data(atomCBHM_cCOUNT, &retptr);
509 //#endif
510 //
511 //   if (retptr != NULL)
512 //     {
513 //        fprintf(stderr, "## c get retptr : %s\n", retptr);
514 //        count = atoi(retptr);
515 //
516 //        free_clipboard_data(retptr);
517 //        retptr = NULL;
518 //     }
519 //
520 //   return count;
521 //}
522 //
523 ///**
524 // * getting raw data of CBHM's contents
525 // *
526 // * @return return raw data of history contents
527 // *
528 // * @ingroup CBHM_helper
529 // */
530 //EAPI int
531 //elm_cbhm_get_raw_data()
532 //{
533 //   if (init_flag == EINA_FALSE)
534 //     return -1;
535 //
536 //   char *retptr = NULL;
537 //
538 //   _send_clipboard_events("get raw");
539 //
540 //#ifdef HAVE_ELEMENTARY_X
541 //   Atom atomCBHM_cRAW = XInternAtom(cbhm_disp, "CBHM_cRAW", False);
542 //
543 //   _get_clipboard_data(atomCBHM_cRAW, &retptr);
544 //#endif
545 //
546 //   if (retptr != NULL)
547 //     {
548 //        free_clipboard_data(retptr);
549 //        retptr = NULL;
550 //     }
551 //
552 //   return 0;
553 //}
554 //
555 ///**
556 // * sending raw command to CBHM
557 // *
558 // * @return void
559 // *
560 // * @ingroup CBHM_helper
561 // */
562 //EAPI void
563 //elm_cbhm_send_raw_data(char *cmd)
564 //{
565 //   if (init_flag == EINA_FALSE)
566 //     return;
567 //
568 //   if (cmd == NULL)
569 //     return;
570 //
571 //   _send_clipboard_events(cmd);
572 //   fprintf(stderr, "## cbhm - send raw cmd = %s\n", cmd);
573 //
574 //   return;
575 //}
576 //
577 ///**
578 // * getting data by history position of CBHM's contents
579 // * 0 is current content.
580 // *
581 // * @return return data pointer of position of history contents
582 // *
583 // * @ingroup CBHM_helper
584 // */
585 //EAPI int
586 //elm_cbhm_get_data_by_position(int pos, char **dataptr)
587 //{
588 //   if (init_flag == EINA_FALSE)
589 //     return -1;
590 //
591 //   char reqbuf[16];
592 //   int ret = 0;
593 //   sprintf(reqbuf, "get #%d", pos);
594 //
595 //   _send_clipboard_events(reqbuf);
596 //
597 //   sprintf(reqbuf, "CBHM_c%d", pos);
598 //
599 //#ifdef HAVE_ELEMENTARY_X
600 //   Atom atomCBHM_cPOS = XInternAtom(cbhm_disp, reqbuf, False);
601 //
602 //   ret = _get_clipboard_data(atomCBHM_cPOS, dataptr);
603 //#endif
604 //
605 //   if (ret >= 0 && *dataptr != NULL)
606 //     {
607 //        //            fprintf(stderr, "## d get retptr : %s\n", *dataptr);
608 //        //            fprintf(stderr, "## dptr = 0x%x\n", *dataptr);
609 //
610 //        return 0;
611 //     }
612 //
613 //   return -1;
614 //}
615 //
616 ///**
617 // * free data by history position of CBHM's contents
618 // *
619 // * @return None
620 // *
621 // * @ingroup CBHM_helper
622 // */
623 //EAPI void
624 //elm_cbhm_free_data(char *dptr)
625 //{
626 //   if (dptr != NULL)
627 //     free_clipboard_data(dptr);
628 //}