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