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