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