create a event queue for efl-util
[platform/core/api/efl-util.git] / src / efl_util.c
1 /*
2  * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define LOG_TAG "TIZEN_N_EFL_UTIL"
18
19 #include <efl_util.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <xf86drm.h>
28 #include <tbm_bufmgr.h>
29 #include <tbm_surface.h>
30 #include <tbm_surface_internal.h>
31 #include <Elementary.h>
32 #include <Ecore_Evas.h>
33
34 #if X11
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xvlib.h>
37 #include <X11/extensions/Xvproto.h>
38 #include <X11/extensions/Xdamage.h>
39 #include <dri2.h>
40 #include <Ecore_X.h>
41 #include <utilX.h>
42 #endif /* end of X11 */
43
44 #if WAYLAND
45 #include <Ecore_Wayland.h>
46 #include <wayland-client.h>
47 #include <tizen-extension-client-protocol.h>
48 #include <screenshooter-client-protocol.h>
49 #endif /* end of WAYLAND */
50
51 /* callback handler index */
52 #define CBH_NOTI_LEV 0
53 #define CBH_SCR_MODE 1
54 #define CBH_MAX      2
55
56 typedef void (*Efl_Util_Cb)(Evas_Object *, int, void *);
57
58 typedef struct _Efl_Util_Callback_Info
59 {
60    Evas_Object *win;
61    Efl_Util_Cb cb;
62    void *data;
63 } Efl_Util_Callback_Info;
64
65 #if WAYLAND
66 typedef struct _Efl_Util_Wl_Surface_Lv_Info
67 {
68    void *surface; /* wl_surface */
69    int level;
70    Eina_Bool wait_for_done;
71 } Efl_Util_Wl_Surface_Lv_Info;
72
73 typedef struct _Efl_Util_Wl_Surface_Scr_Mode_Info
74 {
75    void *surface; /* wl_surface */
76    unsigned int mode;
77    Eina_Bool wait_for_done;
78 } Efl_Util_Wl_Surface_Scr_Mode_Info;
79
80 typedef struct _Efl_Util_Wl_Output_Info
81 {
82     struct wl_output *output;
83     int offset_x, offset_y, width, height;
84 } Efl_Util_Wl_Output_Info;
85 #endif
86
87 typedef struct _Efl_Util_Data
88 {
89 #if X11
90    /* x11 related stuffs */
91    struct
92    {
93       Eina_Bool init;
94       Ecore_Event_Handler *handler; /* x11 client message handler */
95       Ecore_X_Display *dpy;
96    } x11;
97 #endif /* end of X11 */
98
99 #if WAYLAND
100    /* wayland related stuffs */
101    struct
102    {
103       Eina_Bool init;
104       struct wl_display *dpy;
105       struct wl_event_queue *queue;
106
107       struct
108       {
109          struct tizen_policy *proto;
110          Eina_Hash *hash_noti_lv;
111          Eina_Hash *hash_scr_mode;
112       } policy;
113       struct
114       {
115          struct screenshooter *screenshooter;
116          struct tizen_buffer_pool *buffer_pool;
117          Eina_List *output_list;
118       } shot;
119    } wl;
120 #endif /* end of WAYLAND */
121
122    struct
123    {
124       Eina_List *info_list; /* list of callback info */
125       unsigned int atom; /* x11 atom */
126    } cb_handler[CBH_MAX];
127 } Efl_Util_Data;
128
129 static Efl_Util_Data _eflutil =
130 {
131 #if X11
132    {
133       EINA_FALSE,
134       NULL,
135       NULL
136    },
137 #endif /* end of X11 */
138 #if WAYLAND
139    {
140       EINA_FALSE,
141       NULL, NULL,
142       { NULL, NULL, NULL }, /* tizen_policy protocol */
143       { NULL, NULL, NULL }  /* screenshooter protocol */
144    },
145 #endif /* end of WAYLAND */
146    {
147       { NULL, 0 }, /* handler for notification level */
148       { NULL, 0 }  /* handler for screen mode */
149    },
150 };
151
152 static Eina_Bool               _cb_info_add(Evas_Object *win, Efl_Util_Cb cb, void *data, int idx);
153 static Eina_Bool               _cb_info_del_by_win(Evas_Object *win, int idx);
154 static Eina_List              *_cb_info_list_get(int idx);
155 static Efl_Util_Callback_Info *_cb_info_find_by_win(Evas_Object *win, int idx);
156 #if X11
157 static Efl_Util_Callback_Info *_cb_info_find_by_xwin(unsigned int xwin, int idx);
158 static Eina_Bool               _cb_x11_client_msg(void *data, int type, void *event);
159 static Eina_Bool               _x11_init(void);
160 #endif /* end of X11 */
161 #if WAYLAND
162 static Eina_Bool               _wl_init(void);
163 static void                    _cb_wl_reg_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
164 static void                    _cb_wl_reg_global_remove(void *data, struct wl_registry *reg, unsigned int name);
165 static Efl_Util_Callback_Info *_cb_info_find_by_wlsurf(void *wlsurf, int idx);
166 static void                    _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t is_conformant);
167 static void                    _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t conformant_part, uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h);
168 static void                    _cb_wl_tz_policy_notification_done(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, int32_t level, uint32_t state);
169 static void                    _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id);
170 static void                    _cb_wl_tz_policy_scr_mode_done(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t mode, uint32_t state);
171
172 static const struct wl_registry_listener _wl_reg_listener =
173 {
174    _cb_wl_reg_global,
175    _cb_wl_reg_global_remove
176 };
177
178 struct tizen_policy_listener _wl_tz_policy_listener =
179 {
180    _cb_wl_tz_policy_conformant,
181    _cb_wl_tz_policy_conformant_area,
182    _cb_wl_tz_policy_notification_done,
183    _cb_wl_tz_policy_transient_for_done,
184    _cb_wl_tz_policy_scr_mode_done
185 };
186 #endif /* end of WAYLAND */
187
188 static Eina_Bool
189 _cb_info_add(Evas_Object *win,
190              Efl_Util_Cb cb,
191              void *data,
192              int idx)
193 {
194    Efl_Util_Callback_Info *info;
195
196    info = _cb_info_find_by_win(win, idx);
197    if (info)
198      {
199         _eflutil.cb_handler[idx].info_list
200            = eina_list_remove(_eflutil.cb_handler[idx].info_list,
201                               info);
202         free(info);
203      }
204
205    info = (Efl_Util_Callback_Info *)calloc(1, sizeof(Efl_Util_Callback_Info));
206    if (!info) return EINA_FALSE;
207
208    info->win = win;
209    info->cb = cb;
210    info->data = data;
211
212    _eflutil.cb_handler[idx].info_list
213       = eina_list_append(_eflutil.cb_handler[idx].info_list,
214                          info);
215
216 #if X11
217    if (!_eflutil.x11.handler)
218      _eflutil.x11.handler = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
219                                                     _cb_x11_client_msg,
220                                                     NULL);
221 #endif /* end of X11 */
222
223    return EINA_TRUE;
224 }
225
226 static Eina_Bool
227 _cb_info_del_by_win(Evas_Object *win,
228                     int idx)
229 {
230    Efl_Util_Callback_Info *info;
231
232    info = _cb_info_find_by_win(win, idx);
233    if (!info) return EINA_FALSE;
234
235    _eflutil.cb_handler[idx].info_list
236       = eina_list_remove(_eflutil.cb_handler[idx].info_list,
237                          info);
238    free(info);
239
240 #if X11
241    unsigned int count = eina_list_count(_eflutil.cb_handler[idx].info_list);
242    if ((count == 0) && (_eflutil.x11.handler))
243      {
244         ecore_event_handler_del(_eflutil.x11.handler);
245         _eflutil.x11.handler = NULL;
246      }
247 #endif
248
249    return EINA_TRUE;
250 }
251
252 static Eina_List *
253 _cb_info_list_get(int idx)
254 {
255    return _eflutil.cb_handler[idx].info_list;
256 }
257
258 static Efl_Util_Callback_Info *
259 _cb_info_find_by_win(Evas_Object *win,
260                      int idx)
261 {
262    Eina_List *l, *ll;
263    Efl_Util_Callback_Info *info;
264
265    l = _cb_info_list_get(idx);
266    EINA_LIST_FOREACH(l, ll, info)
267      {
268         if (info->win == win) return info;
269      }
270
271    return NULL;
272 }
273
274 #if X11
275 static Efl_Util_Callback_Info *
276 _cb_info_find_by_xwin(unsigned int xwin,
277                       int idx)
278 {
279    Eina_List *l, *ll;
280    Efl_Util_Callback_Info *info;
281    unsigned int xwin2;
282
283    l = _cb_info_list_get(idx);
284    EINA_LIST_FOREACH(l, ll, info)
285      {
286         xwin2 = elm_win_xwindow_get(info->win);
287         if (xwin == xwin2) return info;
288      }
289
290    return NULL;
291 }
292
293 static Eina_Bool
294 _cb_x11_client_msg(void *data,
295                    int type,
296                    void *event)
297 {
298    Ecore_X_Event_Client_Message *ev;
299    Ecore_X_Window xwin;
300    Efl_Util_Callback_Info *info;
301
302    ev = event;
303    if (!ev) return ECORE_CALLBACK_PASS_ON;
304
305    xwin = ev->win;
306    if (xwin == 0) return ECORE_CALLBACK_PASS_ON;
307
308    if (ev->message_type == _eflutil.cb_handler[CBH_NOTI_LEV].atom)
309      {
310         info = _cb_info_find_by_xwin(xwin, CBH_NOTI_LEV);
311
312         /* permission denied */
313         if ((ev->data.l[1] == 0) &&
314             (info) &&
315             (info->cb))
316           {
317              info->cb(info->win,
318                       EFL_UTIL_ERROR_PERMISSION_DENIED,
319                       info->data);
320           }
321      }
322    else if (ev->message_type == _eflutil.cb_handler[CBH_SCR_MODE].atom)
323      {
324         info = _cb_info_find_by_xwin(xwin, CBH_SCR_MODE);
325
326         /* permission denied */
327         if ((ev->data.l[1] == 0) &&
328             (info) &&
329             (info->cb))
330           {
331              info->cb(info->win,
332                       EFL_UTIL_ERROR_PERMISSION_DENIED,
333                       info->data);
334           }
335      }
336    return ECORE_CALLBACK_PASS_ON;
337 }
338
339 static Eina_Bool
340 _x11_init(void)
341 {
342    if (_eflutil.x11.init) return EINA_TRUE;
343
344    _eflutil.x11.dpy = ecore_x_display_get();
345    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.x11.dpy, EINA_FALSE);
346
347    _eflutil.x11.init = EINA_TRUE;
348
349    return EINA_TRUE;
350 }
351 #endif /* end of X11 */
352
353 #if WAYLAND
354 static Eina_Bool
355 _wl_init(void)
356 {
357    struct wl_registry *reg = NULL;
358
359    if (_eflutil.wl.init) return EINA_TRUE;
360
361    ecore_wl_init(NULL);
362
363    _eflutil.wl.dpy = ecore_wl_display_get();
364    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.dpy, fail);
365
366    _eflutil.wl.queue = wl_display_create_queue(_eflutil.wl.dpy);
367    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.queue, fail);
368
369    reg = wl_display_get_registry(_eflutil.wl.dpy);
370    EINA_SAFETY_ON_NULL_GOTO(reg, fail);
371
372    wl_proxy_set_queue((struct wl_proxy*)reg, _eflutil.wl.queue);
373    wl_registry_add_listener(reg, &_wl_reg_listener, NULL);
374
375    _eflutil.wl.init = EINA_TRUE;
376
377    return EINA_TRUE;
378 fail:
379    if (_eflutil.wl.queue)
380      {
381         wl_event_queue_destroy(_eflutil.wl.queue);
382         _eflutil.wl.queue = NULL;
383      }
384
385    if (reg)
386      wl_registry_destroy(reg);
387    ecore_wl_shutdown();
388    return EINA_FALSE;
389 }
390
391 static void
392 _cb_wl_output_geometry(void *data, struct wl_output *wl_output, int x, int y,
393                        int physical_width, int physical_height, int subpixel,
394                        const char *make, const char *model, int transform)
395 {
396    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
397    if (wl_output == output->output)
398      {
399         output->offset_x = x;
400         output->offset_y = y;
401      }
402 }
403
404 static void
405 _cb_wl_output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
406                    int width, int height, int refresh)
407 {
408    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
409    if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT))
410      {
411         output->width = width;
412         output->height = height;
413      }
414 }
415
416 static void
417 _cb_wl_output_done(void *data, struct wl_output *wl_output)
418 {
419 }
420
421 static void
422 _cb_wl_output_scale(void *data, struct wl_output *wl_output, int32_t factor)
423 {
424 }
425
426 static const struct wl_output_listener output_listener =
427 {
428     _cb_wl_output_geometry,
429     _cb_wl_output_mode,
430     _cb_wl_output_done,
431     _cb_wl_output_scale
432 };
433
434 static void
435 _cb_wl_screenshot_done(void *data, struct screenshooter *screenshooter)
436 {
437    Eina_Bool *shot_done = (Eina_Bool*)data;
438    if (shot_done)
439      *shot_done = EINA_TRUE;
440 }
441
442 static const struct screenshooter_listener screenshooter_listener =
443 {
444     _cb_wl_screenshot_done
445 };
446
447 static void
448 _cb_wl_reg_global(void *data,
449                   struct wl_registry *reg,
450                   unsigned int name,
451                   const char *interface,
452                   unsigned int version)
453 {
454    if (!strcmp(interface, "tizen_policy"))
455      {
456         struct tizen_policy *proto;
457         proto = wl_registry_bind(reg,
458                                   name,
459                                   &tizen_policy_interface,
460                                   1);
461         if (!proto) return;
462
463         tizen_policy_add_listener(proto,
464                                   &_wl_tz_policy_listener,
465                                   NULL);
466
467         _eflutil.wl.policy.hash_noti_lv = eina_hash_pointer_new(free);
468         _eflutil.wl.policy.hash_scr_mode = eina_hash_pointer_new(free);
469         _eflutil.wl.policy.proto = proto;
470      }
471    else if (strcmp(interface, "wl_output") == 0)
472      {
473         Efl_Util_Wl_Output_Info *output = calloc(1, sizeof(Efl_Util_Wl_Output_Info));
474         EINA_SAFETY_ON_NULL_RETURN(output);
475
476         _eflutil.wl.shot.output_list = eina_list_append(_eflutil.wl.shot.output_list, output);
477
478         output->output = wl_registry_bind(reg, name, &wl_output_interface, version);
479         wl_output_add_listener(output->output, &output_listener, output);
480      }
481    else if (strcmp(interface, "tizen_buffer_pool") == 0)
482      {
483         _eflutil.wl.shot.buffer_pool = wl_registry_bind(reg, name, &tizen_buffer_pool_interface, 1);
484      }
485    else if (strcmp(interface, "screenshooter") == 0)
486      {
487         _eflutil.wl.shot.screenshooter = wl_registry_bind(reg, name, &screenshooter_interface, version);
488         screenshooter_add_listener(_eflutil.wl.shot.screenshooter, &screenshooter_listener, NULL);
489      }
490 }
491
492 static void
493 _cb_wl_reg_global_remove(void *data,
494                          struct wl_registry *reg,
495                          unsigned int name)
496 {
497    _eflutil.wl.policy.proto = NULL;
498    eina_hash_free(_eflutil.wl.policy.hash_noti_lv);
499    eina_hash_free(_eflutil.wl.policy.hash_scr_mode);
500 }
501
502 static Efl_Util_Callback_Info *
503 _cb_info_find_by_wlsurf(void *wlsurf,
504                         int idx)
505 {
506    Eina_List *l, *ll;
507    Efl_Util_Callback_Info *info;
508    Ecore_Wl_Window *wlwin2 = NULL;
509    void *wlsurf2 = NULL;
510
511    l = _cb_info_list_get(idx);
512    EINA_LIST_FOREACH(l, ll, info)
513      {
514         wlwin2 = elm_win_wl_window_get(info->win);
515         wlsurf2 = ecore_wl_window_surface_get(wlwin2);
516         if (wlsurf== wlsurf2) return info;
517      }
518
519    return NULL;
520 }
521
522 static void
523 _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy,
524                             struct wl_surface *surface, uint32_t is_conformant)
525 {
526 }
527
528 static void
529 _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy,
530                                  struct wl_surface *surface, uint32_t conformant_part,
531                                  uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h)
532 {
533 }
534
535 static void
536 _cb_wl_tz_policy_notification_done(void *data,
537                                    struct tizen_policy *tizen_policy,
538                                    struct wl_surface *surface,
539                                    int32_t level,
540                                    uint32_t state)
541 {
542    Efl_Util_Wl_Surface_Lv_Info *lv_info;
543    Efl_Util_Callback_Info *cb_info;
544
545    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
546    if (lv_info)
547      {
548         lv_info->level = level;
549         lv_info->wait_for_done = EINA_FALSE;
550      }
551
552    if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
553
554    cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_NOTI_LEV);
555    if (!cb_info) return;
556    if (!cb_info->cb) return;
557
558    cb_info->cb(cb_info->win,
559                EFL_UTIL_ERROR_PERMISSION_DENIED,
560                cb_info->data);
561 }
562
563 static void
564 _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id)
565 {
566 }
567
568 static void
569 _cb_wl_tz_policy_scr_mode_done(void *data,
570                                struct tizen_policy *tizen_policy,
571                                struct wl_surface *surface,
572                                uint32_t mode,
573                                uint32_t state)
574 {
575
576    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
577    Efl_Util_Callback_Info *cb_info;
578
579    scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
580    if (scr_mode_info)
581      {
582         scr_mode_info->mode = mode;
583         scr_mode_info->wait_for_done = EINA_FALSE;
584      }
585
586    if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
587
588    cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_SCR_MODE);
589    if (!cb_info) return;
590    if (!cb_info->cb) return;
591
592    cb_info->cb(cb_info->win,
593                EFL_UTIL_ERROR_PERMISSION_DENIED,
594                cb_info->data);
595 }
596 #endif /* end of WAYLAND */
597
598 API int
599 efl_util_set_notification_window_level(Evas_Object *window,
600                                        efl_util_notification_level_e level)
601 {
602    Eina_Bool res;
603
604    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
605    EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_1) &&
606                                    (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
607                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
608
609 #if X11
610    Ecore_X_Window_Type window_type;
611    Ecore_X_Window xwin;
612
613    res = _x11_init();
614    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
615
616    xwin = elm_win_xwindow_get(window);
617    if (xwin)
618      {
619         if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
620           {
621              // success to get window type
622              if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
623                {
624                   // given EFL window's type is not notification type.
625                   return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
626                }
627           }
628         else
629           return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
630
631         utilx_set_system_notification_level(_eflutil.x11.dpy,
632                                             xwin,
633                                             level);
634         return EFL_UTIL_ERROR_NONE;
635      }
636 #endif /* end of X11 */
637
638 #if WAYLAND
639    Elm_Win_Type type;
640    Ecore_Wl_Window *wlwin;
641    struct wl_surface *surface;
642    Efl_Util_Wl_Surface_Lv_Info *lv_info;
643
644    res = _wl_init();
645    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
646
647    type = elm_win_type_get(window);
648    EINA_SAFETY_ON_FALSE_RETURN_VAL((type == ELM_WIN_NOTIFICATION),
649                                    EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
650
651    wlwin = elm_win_wl_window_get(window);
652    if (wlwin)
653      {
654         while (!_eflutil.wl.policy.proto)
655           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
656
657         surface = ecore_wl_window_surface_get(wlwin);
658         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
659                                        EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
660
661         lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
662         if (!lv_info)
663           {
664              lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
665              EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
666
667              lv_info->surface = surface;
668              lv_info->level = (int)level;
669              lv_info->wait_for_done = EINA_TRUE;
670
671              eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
672                            &surface,
673                            lv_info);
674           }
675         else
676           {
677              lv_info->level = (int)level;
678              lv_info->wait_for_done = EINA_TRUE;
679           }
680
681         tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
682                                             surface, (int)level);
683
684         return EFL_UTIL_ERROR_NONE;
685      }
686 #endif /* end of WAYLAND */
687
688    return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
689 }
690
691 API int
692 efl_util_get_notification_window_level(Evas_Object *window,
693                                        efl_util_notification_level_e *level)
694 {
695    Eina_Bool res;
696
697    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
698    EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
699
700 #if X11
701    Ecore_X_Window_Type window_type;
702    Utilx_Notification_Level utilx_level;
703    Ecore_X_Window xwin;
704
705    res = _x11_init();
706    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
707
708    xwin = elm_win_xwindow_get(window);
709    if (xwin)
710      {
711         if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
712           {
713              // success to get window type
714              if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
715                {
716                   // given EFL window's type is not notification type.
717                   return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
718                }
719
720              utilx_level = utilx_get_system_notification_level(_eflutil.x11.dpy, xwin);
721              if (utilx_level == UTILX_NOTIFICATION_LEVEL_LOW)
722                *level = EFL_UTIL_NOTIFICATION_LEVEL_1;
723              else if(utilx_level == UTILX_NOTIFICATION_LEVEL_NORMAL)
724                *level = EFL_UTIL_NOTIFICATION_LEVEL_2;
725              else if(utilx_level == UTILX_NOTIFICATION_LEVEL_HIGH)
726                *level = EFL_UTIL_NOTIFICATION_LEVEL_3;
727              else
728                return EFL_UTIL_ERROR_INVALID_PARAMETER;
729           }
730         else
731           return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
732
733         return EFL_UTIL_ERROR_NONE;
734      }
735 #endif /* end of X11 */
736
737 #if WAYLAND
738    Elm_Win_Type type;
739    Ecore_Wl_Window *wlwin;
740    struct wl_surface *surface;
741    Efl_Util_Wl_Surface_Lv_Info *lv_info;
742
743    res = _wl_init();
744    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
745
746    type = elm_win_type_get(window);
747    EINA_SAFETY_ON_FALSE_RETURN_VAL((type == ELM_WIN_NOTIFICATION),
748                                    EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
749
750    wlwin = elm_win_wl_window_get(window);
751    if (wlwin)
752      {
753         while (!_eflutil.wl.policy.proto)
754           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
755
756         surface = ecore_wl_window_surface_get(wlwin);
757         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
758                                        EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
759
760         lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
761         if (lv_info)
762           {
763              if (lv_info->wait_for_done)
764                {
765                   if (ecore_wl_window_shell_surface_get(wlwin) ||
766                       ecore_wl_window_xdg_surface_get(wlwin))
767                     {
768                        while (lv_info->wait_for_done)
769                          {
770                             ecore_wl_flush();
771                             wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
772                          }
773                     }
774                   else
775                     {
776                        *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
777                        return EFL_UTIL_ERROR_INVALID_PARAMETER;
778                     }
779                }
780
781              switch (lv_info->level)
782                {
783                 case TIZEN_POLICY_LEVEL_1:       *level = EFL_UTIL_NOTIFICATION_LEVEL_1;       break;
784                 case TIZEN_POLICY_LEVEL_2:       *level = EFL_UTIL_NOTIFICATION_LEVEL_2;       break;
785                 case TIZEN_POLICY_LEVEL_3:       *level = EFL_UTIL_NOTIFICATION_LEVEL_3;       break;
786                 case TIZEN_POLICY_LEVEL_NONE:    *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE;    break;
787                 case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
788                 case TIZEN_POLICY_LEVEL_MEDIUM:  *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM;  break;
789                 case TIZEN_POLICY_LEVEL_HIGH:    *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH;    break;
790                 case TIZEN_POLICY_LEVEL_TOP:     *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP;     break;
791                 default:                               *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
792                                                        return EFL_UTIL_ERROR_INVALID_PARAMETER;
793                }
794              return EFL_UTIL_ERROR_NONE;
795           }
796         else
797           *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
798
799         return EFL_UTIL_ERROR_NONE;
800      }
801 #endif /* end of WAYLAND */
802    return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
803 }
804
805 API int
806 efl_util_set_notification_window_level_error_cb(Evas_Object *window,
807                                                 efl_util_notification_window_level_error_cb callback,
808                                                 void *user_data)
809 {
810    Eina_Bool ret = EINA_FALSE;
811
812    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
813    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
814
815    ret = _cb_info_add(window,
816                       (Efl_Util_Cb)callback,
817                       user_data,
818                       CBH_NOTI_LEV);
819    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
820
821 #if X11
822    if (!_eflutil.cb_handler[CBH_NOTI_LEV].atom)
823      _eflutil.cb_handler[CBH_NOTI_LEV].atom = ecore_x_atom_get("_E_NOTIFICATION_LEVEL_ACCESS_RESULT");
824 #endif /* end of X11 */
825
826    return EFL_UTIL_ERROR_NONE;
827 }
828
829 API int
830 efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
831 {
832    Eina_Bool ret = EINA_FALSE;
833
834    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
835
836    ret = _cb_info_del_by_win(window, CBH_NOTI_LEV);
837    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
838
839    return EFL_UTIL_ERROR_NONE;
840 }
841
842 API int
843 efl_util_set_window_opaque_state(Evas_Object *window,
844                                  int opaque)
845 {
846    Eina_Bool res;
847
848    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
849    EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
850                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
851
852 #if X11
853    Ecore_X_Window xwin;
854    Utilx_Opaque_State state;
855    int ret;
856
857    res = _x11_init();
858    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
859
860    xwin = elm_win_xwindow_get(window);
861    EINA_SAFETY_ON_FALSE_RETURN_VAL(xwin > 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
862
863    if (opaque)
864      state = UTILX_OPAQUE_STATE_ON;
865    else
866      state = UTILX_OPAQUE_STATE_OFF;
867
868    ret = utilx_set_window_opaque_state(_eflutil.x11.dpy,
869                                        xwin,
870                                        state);
871
872    if (!ret)
873      return EFL_UTIL_ERROR_INVALID_PARAMETER;
874    else
875      return EFL_UTIL_ERROR_NONE;
876 #endif /* end of X11 */
877
878 #if WAYLAND
879    Ecore_Wl_Window *wlwin;
880    int x, y, w, h;
881
882    res = _wl_init();
883    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
884
885    wlwin = elm_win_wl_window_get(window);
886    if (wlwin)
887      {
888         evas_object_geometry_get(window, &x, &y, &w, &h);
889
890         if (opaque)
891           ecore_wl_window_opaque_region_set(wlwin, x, y, w, h);
892         else
893           ecore_wl_window_opaque_region_set(wlwin, 0, 0, 0, 0);
894
895         return EFL_UTIL_ERROR_NONE;
896      }
897
898    return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
899 #endif /* end of WAYLAND */
900 }
901
902 API int
903 efl_util_set_window_screen_mode(Evas_Object *window,
904                                 efl_util_screen_mode_e mode)
905 {
906    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
907    EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
908                                     (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
909                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
910
911 #if X11
912    Evas *e;
913    Ecore_Evas *ee;
914    int id;
915
916    e = evas_object_evas_get(window);
917    EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
918
919    ee = ecore_evas_ecore_evas_get(e);
920    EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
921
922    id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
923    if (mode == EFL_UTIL_SCREEN_MODE_ALWAYS_ON)
924      {
925         if (id == -1)
926           ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "1");
927         else
928           ecore_evas_aux_hint_val_set(ee, id, "1");
929      }
930    else if (mode == EFL_UTIL_SCREEN_MODE_DEFAULT)
931      {
932         if (id == -1)
933           ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "0");
934         else
935           ecore_evas_aux_hint_val_set(ee, id, "0");
936      }
937    else
938      return EFL_UTIL_ERROR_INVALID_PARAMETER;
939
940    return EFL_UTIL_ERROR_NONE;
941 #endif /* end of X11 */
942
943 #if WAYLAND
944    Ecore_Wl_Window *wlwin;
945    struct wl_surface *surface;
946    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
947    Eina_Bool res;
948
949    res = _wl_init();
950    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
951
952    wlwin = elm_win_wl_window_get(window);
953    if (wlwin)
954      {
955         while (!_eflutil.wl.policy.proto)
956           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
957
958         surface = ecore_wl_window_surface_get(wlwin);
959         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
960                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
961
962         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
963         if (!scr_mode_info)
964           {
965              scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
966              EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
967
968              scr_mode_info->surface = surface;
969              scr_mode_info->mode = (unsigned int)mode;
970              scr_mode_info->wait_for_done = EINA_TRUE;
971
972              eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
973                            &surface,
974                            scr_mode_info);
975           }
976         else
977           {
978              scr_mode_info->mode = (unsigned int)mode;
979              scr_mode_info->wait_for_done = EINA_TRUE;
980           }
981
982         tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
983                                             surface, (unsigned int)mode);
984
985         return EFL_UTIL_ERROR_NONE;
986      }
987    else
988      return EFL_UTIL_ERROR_INVALID_PARAMETER;
989 #endif /* end of WAYLAND */
990 }
991
992 API int
993 efl_util_get_window_screen_mode(Evas_Object *window,
994                                 efl_util_screen_mode_e *mode)
995 {
996    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
997    EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
998
999 #if X11
1000    Evas *e;
1001    Ecore_Evas *ee;
1002    const char *str;
1003    int id;
1004
1005    e = evas_object_evas_get(window);
1006    EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
1007
1008    ee = ecore_evas_ecore_evas_get(e);
1009    EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
1010
1011    id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
1012    EINA_SAFETY_ON_TRUE_RETURN_VAL((id == -1), EFL_UTIL_ERROR_INVALID_PARAMETER);
1013
1014    str = ecore_evas_aux_hint_val_get(ee, id);
1015    EINA_SAFETY_ON_NULL_RETURN_VAL(str, EFL_UTIL_ERROR_INVALID_PARAMETER);
1016
1017    if (strncmp(str, "1", strlen("1")) == 0)
1018      *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON;
1019    else
1020      *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1021
1022    return EFL_UTIL_ERROR_NONE;
1023 #endif /* end of X11 */
1024
1025 #if WAYLAND
1026    Ecore_Wl_Window *wlwin;
1027    struct wl_surface *surface;
1028    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1029    Eina_Bool res;
1030
1031    res = _wl_init();
1032    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1033
1034    wlwin = elm_win_wl_window_get(window);
1035    if (wlwin)
1036      {
1037         while (!_eflutil.wl.policy.proto)
1038           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1039
1040         surface = ecore_wl_window_surface_get(wlwin);
1041         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1042                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
1043
1044         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1045         if (scr_mode_info)
1046           {
1047              if (scr_mode_info->wait_for_done)
1048                {
1049                   while (scr_mode_info->wait_for_done)
1050                     {
1051                        ecore_wl_flush();
1052                        wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1053                     }
1054                }
1055
1056              switch (scr_mode_info->mode)
1057                {
1058                 case TIZEN_POLICY_MODE_DEFAULT:   *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;   break;
1059                 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
1060                 default:                          *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1061                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
1062                }
1063              return EFL_UTIL_ERROR_NONE;
1064           }
1065         else
1066           {
1067              *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1068              return EFL_UTIL_ERROR_INVALID_PARAMETER;
1069           }
1070      }
1071    else
1072      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1073 #endif /* end of WAYLAND */
1074 }
1075
1076 API int
1077 efl_util_set_window_screen_mode_error_cb(Evas_Object *window,
1078                                          efl_util_window_screen_mode_error_cb callback,
1079                                          void *user_data)
1080 {
1081    Eina_Bool ret = EINA_FALSE;
1082
1083    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1084    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
1085
1086    ret = _cb_info_add(window,
1087                       (Efl_Util_Cb)callback,
1088                       user_data,
1089                       CBH_SCR_MODE);
1090    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1091
1092 #if X11
1093    if (!_eflutil.cb_handler[CBH_SCR_MODE].atom)
1094      _eflutil.cb_handler[CBH_SCR_MODE].atom = ecore_x_atom_get("_E_SCREEN_MODE_ACCESS_RESULT");
1095 #endif /* end of X11 */
1096
1097    return EFL_UTIL_ERROR_NONE;
1098 }
1099
1100 API int
1101 efl_util_unset_window_screen_mode_error_cb(Evas_Object *window)
1102 {
1103    Eina_Bool ret = EINA_FALSE;
1104
1105    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1106
1107    ret = _cb_info_del_by_win(window, CBH_SCR_MODE);
1108    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1109
1110    return EFL_UTIL_ERROR_NONE;
1111 }
1112
1113 API int
1114 efl_util_input_initialize_generator(efl_util_input_device_type_e dev_type)
1115 {
1116    return EFL_UTIL_ERROR_NONE;
1117 }
1118
1119 API void
1120 efl_util_input_deinitialize_generator(void)
1121 {
1122    return;
1123 }
1124
1125 API int
1126 efl_util_input_generate_key(const char *key_name,
1127                             int pressed)
1128 {
1129    return EFL_UTIL_ERROR_NONE;
1130 }
1131
1132 API int
1133 efl_util_input_generate_touch(int idx,
1134                               efl_util_input_touch_type_e touch_type,
1135                               int x,
1136                               int y)
1137 {
1138    return EFL_UTIL_ERROR_NONE;
1139 }
1140
1141 struct _efl_util_screenshot_h
1142 {
1143    int width;
1144    int height;
1145
1146 #if X11
1147    Ecore_X_Display *dpy;
1148    int internal_display;
1149    int screen;
1150    Window root;
1151    Pixmap pixmap;
1152    GC gc;
1153    Atom atom_capture;
1154
1155    /* port */
1156    int port;
1157
1158    /* damage */
1159    Damage   damage;
1160    int      damage_base;
1161
1162    /* dri2 */
1163    int eventBase, errorBase;
1164    int dri2Major, dri2Minor;
1165    char *driver_name, *device_name;
1166    drm_magic_t magic;
1167
1168    /* drm */
1169    int drm_fd;
1170 #endif
1171
1172    Eina_Bool shot_done;
1173
1174    /* tbm bufmgr */
1175    tbm_bufmgr bufmgr;
1176 };
1177
1178 /* scrrenshot handle */
1179 static efl_util_screenshot_h g_screenshot;
1180
1181 #if X11
1182 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1183 #define FOURCC_RGB32    FOURCC('R','G','B','4')
1184 #define TIMEOUT_CAPTURE 3
1185
1186 /* x error handling */
1187 static Bool g_efl_util_x_error_caught;
1188
1189 static int
1190 _efl_util_screenshot_x_error_handle(Display *dpy, XErrorEvent *ev)
1191 {
1192    if (!g_screenshot || (dpy != g_screenshot->dpy))
1193      return 0;
1194
1195    g_efl_util_x_error_caught = True;
1196
1197    return 0;
1198 }
1199
1200 static int
1201 _efl_util_screenshot_get_port(Display *dpy, unsigned int id, Window win)
1202 {
1203    unsigned int ver, rev, req_base, evt_base, err_base;
1204    unsigned int adaptors;
1205    XvAdaptorInfo *ai = NULL;
1206    XvImageFormatValues *fo = NULL;
1207    int formats;
1208    int i, j, p;
1209
1210    if (XvQueryExtension(dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1211      {
1212         fprintf(stderr, "[screenshot] fail: no XV extension. \n");
1213         return -1;
1214      }
1215
1216    if (XvQueryAdaptors(dpy, win, &adaptors, &ai) != Success)
1217      {
1218         fprintf(stderr, "[screenshot] fail: query adaptors. \n");
1219         return -1;
1220      }
1221
1222    EINA_SAFETY_ON_NULL_RETURN_VAL(ai, -1);
1223
1224    for (i = 0; i < adaptors; i++)
1225      {
1226         int support_format = False;
1227
1228         if (!(ai[i].type & XvInputMask) ||
1229             !(ai[i].type & XvStillMask))
1230           continue;
1231
1232         p = ai[i].base_id;
1233
1234         fo = XvListImageFormats(dpy, p, &formats);
1235         for (j = 0; j < formats; j++)
1236           if (fo[j].id == (int)id)
1237             support_format = True;
1238
1239         if (fo)
1240           XFree(fo);
1241
1242         if (!support_format)
1243           continue;
1244
1245         for (; p < ai[i].base_id + ai[i].num_ports; p++)
1246           {
1247              if (XvGrabPort(dpy, p, 0) == Success)
1248                {
1249                   XvFreeAdaptorInfo(ai);
1250                   return p;
1251                }
1252           }
1253      }
1254
1255    XvFreeAdaptorInfo(ai);
1256
1257    XSync(dpy, False);
1258
1259    return -1;
1260 }
1261
1262 static int _efl_util_screenshot_get_best_size(Display *dpy, int port, int width, int height, unsigned int *best_width, unsigned int *best_height)
1263 {
1264    XErrorHandler old_handler = NULL;
1265
1266    Atom atom_capture = XInternAtom(dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1267
1268    g_efl_util_x_error_caught = False;
1269    old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1270
1271    XvSetPortAttribute(dpy, port, atom_capture, 1);
1272    XSync(dpy, False);
1273
1274    g_efl_util_x_error_caught = False;
1275    XSetErrorHandler(old_handler);
1276
1277    XvQueryBestSize(dpy, port, 0, 0, 0, width, height, best_width, best_height);
1278    if (best_width <= 0 || best_height <= 0)
1279      return 0;
1280
1281    return 1;
1282 }
1283 #endif
1284
1285 API efl_util_screenshot_h efl_util_screenshot_initialize(int width, int height)
1286 {
1287 #if X11
1288    efl_util_screenshot_h screenshot = NULL;
1289    int depth = 0;
1290    int damage_err_base = 0;
1291    unsigned int best_width = 0;
1292    unsigned int best_height = 0;
1293
1294    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1295    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1296
1297    if (g_screenshot != NULL)
1298      {
1299         if (g_screenshot->width != width || g_screenshot->height != height)
1300           {
1301              // TODO: recreate pixmap and update information
1302              if (!_efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height))
1303                {
1304                   set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1305                   return NULL;
1306                }
1307
1308              g_screenshot->width = width;
1309              g_screenshot->height = height;
1310           }
1311
1312         return g_screenshot;
1313      }
1314
1315    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1316    EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1317
1318    /* set dpy */
1319    screenshot->dpy = ecore_x_display_get();
1320    if (!screenshot->dpy)
1321      {
1322         screenshot->dpy = XOpenDisplay(0);
1323         EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_init);
1324
1325         /* for XCloseDisplay at denitialization */
1326         screenshot->internal_display = 1;
1327      }
1328
1329    /* set screen */
1330    screenshot->screen = DefaultScreen(screenshot->dpy);
1331
1332    /* set root window */
1333    screenshot->root = DefaultRootWindow(screenshot->dpy);
1334
1335    /* initialize capture adaptor */
1336    screenshot->port = _efl_util_screenshot_get_port(screenshot->dpy, FOURCC_RGB32, screenshot->root);
1337    EINA_SAFETY_ON_FALSE_GOTO(screenshot->port > 0, fail_init);
1338
1339    /* get the best size */
1340    _efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height);
1341    EINA_SAFETY_ON_FALSE_GOTO(best_width > 0, fail_init);
1342    EINA_SAFETY_ON_FALSE_GOTO(best_height > 0, fail_init);
1343
1344    /* set the width and the height */
1345    screenshot->width = best_width;
1346    screenshot->height = best_height;
1347
1348    /* create a pixmap */
1349    depth = DefaultDepth(screenshot->dpy, screenshot->screen);
1350    screenshot->pixmap = XCreatePixmap(screenshot->dpy, screenshot->root, screenshot->width, screenshot->height, depth);
1351    EINA_SAFETY_ON_FALSE_GOTO(screenshot->pixmap > 0, fail_init);
1352
1353    screenshot->gc = XCreateGC(screenshot->dpy, screenshot->pixmap, 0, 0);
1354    EINA_SAFETY_ON_NULL_GOTO(screenshot->gc, fail_init);
1355
1356    XSetForeground(screenshot->dpy, screenshot->gc, 0xFF000000);
1357    XFillRectangle(screenshot->dpy, screenshot->pixmap, screenshot->gc, 0, 0, width, height);
1358
1359    /* initialize damage */
1360    if (!XDamageQueryExtension(screenshot->dpy, &screenshot->damage_base, &damage_err_base))
1361      goto fail_init;
1362
1363    screenshot->damage = XDamageCreate(screenshot->dpy, screenshot->pixmap, XDamageReportNonEmpty);
1364    EINA_SAFETY_ON_FALSE_GOTO(screenshot->damage > 0, fail_init);
1365
1366    /* initialize dri3 and dri2 */
1367    if (!DRI2QueryExtension(screenshot->dpy, &screenshot->eventBase, &screenshot->errorBase))
1368      {
1369         fprintf(stderr, "[screenshot] fail: DRI2QueryExtention\n");
1370         goto fail_init;
1371      }
1372
1373    if (!DRI2QueryVersion(screenshot->dpy, &screenshot->dri2Major, &screenshot->dri2Minor))
1374      {
1375         fprintf(stderr, "[screenshot] fail: DRI2QueryVersion\n");
1376         goto fail_init;
1377      }
1378
1379    if (!DRI2Connect(screenshot->dpy, screenshot->root, &screenshot->driver_name, &screenshot->device_name))
1380      {
1381         fprintf(stderr, "[screenshot] fail: DRI2Connect\n");
1382         goto fail_init;
1383      }
1384
1385    screenshot->drm_fd = open(screenshot->device_name, O_RDWR);
1386    EINA_SAFETY_ON_FALSE_GOTO(screenshot->drm_fd >= 0, fail_init);
1387
1388    if (drmGetMagic(screenshot->drm_fd, &screenshot->magic))
1389      {
1390         fprintf(stderr, "[screenshot] fail: drmGetMagic\n");
1391         goto fail_init;
1392      }
1393
1394    if (!DRI2Authenticate(screenshot->dpy, screenshot->root, screenshot->magic))
1395      {
1396         fprintf(stderr, "[screenshot] fail: DRI2Authenticate\n");
1397         goto fail_init;
1398      }
1399
1400    if (!drmAuthMagic(screenshot->drm_fd, screenshot->magic))
1401      {
1402         fprintf(stderr, "[screenshot] fail: drmAuthMagic\n");
1403         goto fail_init;
1404      }
1405
1406    DRI2CreateDrawable(screenshot->dpy, screenshot->pixmap);
1407
1408    /* tbm bufmgr */
1409    screenshot->bufmgr = tbm_bufmgr_init(screenshot->drm_fd);
1410    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1411
1412    XFlush(screenshot->dpy);
1413
1414    g_screenshot = screenshot;
1415    set_last_result(EFL_UTIL_ERROR_NONE);
1416
1417    return g_screenshot;
1418 #endif
1419
1420 #if WAYLAND
1421    efl_util_screenshot_h screenshot = NULL;
1422
1423    if (!_eflutil.wl.shot.screenshooter)
1424      {
1425         int ret = 0;
1426         _wl_init();
1427         while (!_eflutil.wl.shot.screenshooter && ret != -1)
1428           ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1429         EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.screenshooter, fail_init);
1430         EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.buffer_pool, fail_init);
1431      }
1432
1433    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1434    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1435
1436    if (g_screenshot)
1437      {
1438         if (g_screenshot->width != width || g_screenshot->height != height)
1439           {
1440              g_screenshot->width = width;
1441              g_screenshot->height = height;
1442           }
1443
1444         return g_screenshot;
1445      }
1446
1447    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1448    EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1449
1450    screenshot->width = width;
1451    screenshot->height = height;
1452
1453    screenshot->bufmgr = tbm_bufmgr_init(-1);
1454    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1455
1456    g_screenshot = screenshot;
1457    set_last_result(EFL_UTIL_ERROR_NONE);
1458
1459    screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
1460
1461    return g_screenshot;
1462 #endif
1463 fail_param:
1464    if (screenshot)
1465      efl_util_screenshot_deinitialize(screenshot);
1466    set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1467    return NULL;
1468 fail_memory:
1469    if (screenshot)
1470      efl_util_screenshot_deinitialize(screenshot);
1471    set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1472    return NULL;
1473 fail_init:
1474    if (screenshot)
1475      efl_util_screenshot_deinitialize(screenshot);
1476    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1477    return NULL;
1478 }
1479
1480 API int efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
1481 {
1482 #if X11
1483    if (!screenshot)
1484      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1485
1486    /* tbm bufmgr */
1487    if (screenshot->bufmgr)
1488      tbm_bufmgr_deinit(screenshot->bufmgr);
1489
1490    DRI2DestroyDrawable(screenshot->dpy, screenshot->pixmap);
1491
1492    /* dri2 */
1493    if (screenshot->drm_fd)
1494      close(screenshot->drm_fd);
1495    if (screenshot->driver_name)
1496      free(screenshot->driver_name);
1497    if (screenshot->device_name)
1498      free(screenshot->device_name);
1499
1500    /* xv */
1501    if (screenshot->port > 0 && screenshot->pixmap > 0)
1502      XvStopVideo(screenshot->dpy, screenshot->port, screenshot->pixmap);
1503
1504    /* damage */
1505    if (screenshot->damage)
1506      XDamageDestroy(screenshot->dpy, screenshot->damage);
1507
1508    /* gc */
1509    if (screenshot->gc)
1510      XFreeGC(screenshot->dpy, screenshot->gc);
1511
1512    /* pixmap */
1513    if (screenshot->pixmap > 0)
1514      XFreePixmap(screenshot->dpy, screenshot->pixmap);
1515
1516    /* port */
1517    if (screenshot->port > 0)
1518      XvUngrabPort(screenshot->dpy, screenshot->port, 0);
1519
1520    XSync(screenshot->dpy, False);
1521
1522    /* dpy */
1523    if (screenshot->internal_display ==1 && screenshot->dpy)
1524      XCloseDisplay(screenshot->dpy);
1525
1526    free(screenshot);
1527    g_screenshot = NULL;
1528
1529    return EFL_UTIL_ERROR_NONE;
1530 #endif
1531 #if WAYLAND
1532    if (!screenshot)
1533      return EFL_UTIL_ERROR_NONE;
1534
1535    if (screenshot->bufmgr)
1536      tbm_bufmgr_deinit(screenshot->bufmgr);
1537
1538    free(screenshot);
1539    g_screenshot = NULL;
1540
1541    if (_eflutil.wl.shot.screenshooter)
1542      screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, NULL);
1543
1544    return EFL_UTIL_ERROR_NONE;
1545 #endif
1546 }
1547
1548
1549 API tbm_surface_h efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
1550 {
1551 #if X11
1552    XEvent ev = {0,};
1553    XErrorHandler old_handler = NULL;
1554    unsigned int attachment = DRI2BufferFrontLeft;
1555    int nbufs = 0;
1556    DRI2Buffer *bufs = NULL;
1557    tbm_bo t_bo = NULL;
1558    tbm_surface_h t_surface = NULL;
1559    int buf_width = 0;
1560    int buf_height = 0;
1561    tbm_surface_info_s surf_info;
1562    int i;
1563
1564    if (screenshot != g_screenshot)
1565      {
1566         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1567         return NULL;
1568      }
1569
1570    /* for flush other pending requests and pending events */
1571    XSync(screenshot->dpy, 0);
1572
1573    g_efl_util_x_error_caught = False;
1574    old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1575
1576    /* dump here */
1577    XvPutStill(screenshot->dpy, screenshot->port, screenshot->pixmap, screenshot->gc,
1578               0, 0, screenshot->width, screenshot->height,
1579               0, 0, screenshot->width, screenshot->height);
1580
1581    XSync(screenshot->dpy, 0);
1582
1583    if (g_efl_util_x_error_caught)
1584      {
1585         g_efl_util_x_error_caught = False;
1586         XSetErrorHandler(old_handler);
1587         goto fail;
1588      }
1589
1590    g_efl_util_x_error_caught = False;
1591    XSetErrorHandler(old_handler);
1592
1593    if (XPending(screenshot->dpy))
1594      XNextEvent(screenshot->dpy, &ev);
1595    else
1596      {
1597         int fd = ConnectionNumber(screenshot->dpy);
1598         fd_set mask;
1599         struct timeval tv;
1600         int ret;
1601
1602         FD_ZERO(&mask);
1603         FD_SET(fd, &mask);
1604
1605         tv.tv_usec = 0;
1606         tv.tv_sec = TIMEOUT_CAPTURE;
1607
1608         ret = select(fd + 1, &mask, 0, 0, &tv);
1609         if (ret < 0)
1610           fprintf(stderr, "[screenshot] fail: select.\n");
1611         else if (ret == 0)
1612           fprintf(stderr, "[screenshot] fail: timeout(%d sec)!\n", TIMEOUT_CAPTURE);
1613         else if (XPending(screenshot->dpy))
1614           XNextEvent(screenshot->dpy, &ev);
1615         else
1616           fprintf(stderr, "[screenshot] fail: not passed a event!\n");
1617      }
1618
1619    /* check if the capture is done by xserver and pixmap has got the captured image */
1620    if (ev.type == (screenshot->damage_base + XDamageNotify))
1621      {
1622         XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1623         if (damage_ev->drawable == screenshot->pixmap)
1624           {
1625              /* Get DRI2 FrontLeft buffer of the pixmap */
1626              bufs = DRI2GetBuffers(screenshot->dpy, screenshot->pixmap, &buf_width, &buf_height, &attachment, 1, &nbufs);
1627              if (!bufs)
1628                {
1629                   fprintf(stderr, "[screenshot] fail: DRI2GetBuffers\n");
1630                   goto fail;
1631                }
1632
1633              t_bo = tbm_bo_import(screenshot->bufmgr, bufs[0].name);
1634              if (!t_bo)
1635                {
1636                   fprintf(stderr, "[screenshot] fail: import tbm_bo!\n");
1637                   goto fail;
1638                }
1639
1640              surf_info.width = buf_width;
1641              surf_info.height = buf_height;
1642              surf_info.format = TBM_FORMAT_XRGB8888;
1643              surf_info.bpp = 32;
1644              surf_info.size = bufs->pitch * surf_info.height;
1645              surf_info.num_planes = 1;
1646              for (i = 0; i < surf_info.num_planes; i++)
1647                {
1648                   surf_info.planes[i].size = bufs->pitch * surf_info.height;
1649                   surf_info.planes[i].stride = bufs->pitch;
1650                   surf_info.planes[i].offset = 0;
1651                }
1652              t_surface = tbm_surface_internal_create_with_bos(&surf_info, &t_bo, 1);
1653              if (!t_surface)
1654                {
1655                   fprintf(stderr, "[screenshot] fail: get tbm_surface!\n");
1656                   goto fail;
1657                }
1658
1659              tbm_bo_unref(t_bo);
1660              free(bufs);
1661
1662              XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1663
1664              set_last_result(EFL_UTIL_ERROR_NONE);
1665
1666              return t_surface;
1667           }
1668
1669         XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1670      }
1671
1672 fail:
1673
1674    if (t_bo)
1675      tbm_bo_unref(t_bo);
1676    if (bufs)
1677      free(bufs);
1678
1679    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1680
1681    return NULL;
1682 #endif
1683
1684 #if WAYLAND
1685    tbm_bo t_bo = NULL;
1686    tbm_surface_h t_surface = NULL;
1687    struct wl_buffer *buffer = NULL;
1688    tbm_surface_info_s info;
1689    Efl_Util_Wl_Output_Info *output;
1690    int ret = 0;
1691
1692    if (screenshot != g_screenshot)
1693      {
1694         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1695         return NULL;
1696      }
1697
1698    output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
1699    if (!output)
1700      {
1701         fprintf(stderr, "[screenshot] fail: no output for screenshot\n");
1702         goto fail;
1703      }
1704
1705    t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
1706    if (!t_surface)
1707      {
1708         fprintf(stderr, "[screenshot] fail: tbm_surface_create\n");
1709         goto fail;
1710      }
1711
1712    t_bo = tbm_surface_internal_get_bo(t_surface, 0);
1713    if (!t_bo)
1714      {
1715         fprintf(stderr, "[screenshot] fail: no tbm_bo for screenshot\n");
1716         goto fail;
1717      }
1718
1719    tbm_surface_get_info(t_surface, &info);
1720
1721    buffer =
1722      tizen_buffer_pool_create_buffer(_eflutil.wl.shot.buffer_pool,
1723                                      tbm_bo_export(t_bo),
1724                                      info.width, info.height,
1725                                      info.planes[0].stride,
1726                                      TIZEN_BUFFER_POOL_FORMAT_XRGB8888);
1727    if (!buffer)
1728      {
1729         fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n");
1730         goto fail;
1731      }
1732
1733    screenshooter_shoot(_eflutil.wl.shot.screenshooter, output->output, buffer);
1734
1735    screenshot->shot_done = EINA_FALSE;
1736    while (!screenshot->shot_done && ret != -1)
1737      ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1738
1739    if (ret == -1)
1740      {
1741         fprintf(stderr, "[screenshot] fail: screenshooter_shoot\n");
1742         goto fail;
1743      }
1744
1745    wl_buffer_destroy(buffer);
1746
1747    return t_surface;
1748
1749 fail:
1750    if (t_surface)
1751      tbm_surface_destroy(t_surface);
1752    if (buffer);
1753      wl_buffer_destroy(buffer);
1754
1755    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1756
1757    return NULL;
1758 #endif
1759 }