re-configure notification level whenever window is shown.
[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 #endif /* end of WAYLAND */
629
630 API int
631 efl_util_set_notification_window_level(Evas_Object *window,
632                                        efl_util_notification_level_e level)
633 {
634    Eina_Bool res;
635
636    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
637    EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_NONE) &&
638                                    (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
639                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
640
641 #if X11
642    Ecore_X_Window_Type window_type;
643    Ecore_X_Window xwin;
644
645    res = _x11_init();
646    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
647
648    xwin = elm_win_xwindow_get(window);
649    if (xwin)
650      {
651         if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
652           {
653              // success to get window type
654              if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
655                {
656                   // given EFL window's type is not notification type.
657                   return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
658                }
659           }
660         else
661           return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
662
663         utilx_set_system_notification_level(_eflutil.x11.dpy,
664                                             xwin,
665                                             level);
666         return EFL_UTIL_ERROR_NONE;
667      }
668
669    return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
670 #endif /* end of X11 */
671
672 #if WAYLAND
673    Elm_Win_Type type;
674    Ecore_Wl_Window *wlwin;
675    struct wl_surface *surface;
676    Efl_Util_Wl_Surface_Lv_Info *lv_info;
677    Ecore_Wl_Window_Type wl_type;
678
679    res = _wl_init();
680    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
681
682    wlwin = elm_win_wl_window_get(window);
683    EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
684
685    type = elm_win_type_get(window);
686    if (type != ELM_WIN_NOTIFICATION)
687      {
688         wl_type = ecore_wl_window_type_get(wlwin);
689         EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
690                                         EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
691      }
692
693    while (!_eflutil.wl.policy.proto)
694      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
695
696    surface = ecore_wl_window_surface_get(wlwin);
697    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
698                                   EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
699
700    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
701    if (!lv_info)
702      {
703         lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
704         EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
705
706         lv_info->window = window;
707         lv_info->surface = surface;
708         lv_info->level = (int)level;
709         lv_info->wait_for_done = EINA_TRUE;
710         lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
711         eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
712                       &surface,
713                       lv_info);
714
715         evas_object_event_callback_add(window, EVAS_CALLBACK_SHOW,
716                                        _cb_window_show, lv_info);
717      }
718    else
719      {
720         lv_info->level = (int)level;
721         lv_info->wait_for_done = EINA_TRUE;
722         lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
723      }
724
725
726    tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
727                                        surface, (int)level);
728
729    if (lv_info->wait_for_done)
730      {
731         int count = 0;
732         while (lv_info->wait_for_done && (count < 3))
733           {
734              ecore_wl_flush();
735              wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
736              count++;
737           }
738
739         if (lv_info->wait_for_done)
740           {
741              return EFL_UTIL_ERROR_INVALID_PARAMETER;
742           }
743         else
744           {
745              if (lv_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
746                {
747                   return EFL_UTIL_ERROR_PERMISSION_DENIED;
748                }
749           }
750      }
751
752    return EFL_UTIL_ERROR_NONE;
753 #endif /* end of WAYLAND */
754 }
755
756 API int
757 efl_util_get_notification_window_level(Evas_Object *window,
758                                        efl_util_notification_level_e *level)
759 {
760    Eina_Bool res;
761
762    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
763    EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
764
765 #if X11
766    Ecore_X_Window_Type window_type;
767    Utilx_Notification_Level utilx_level;
768    Ecore_X_Window xwin;
769
770    res = _x11_init();
771    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
772
773    xwin = elm_win_xwindow_get(window);
774    if (xwin)
775      {
776         if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
777           {
778              // success to get window type
779              if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
780                {
781                   // given EFL window's type is not notification type.
782                   return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
783                }
784
785              utilx_level = utilx_get_system_notification_level(_eflutil.x11.dpy, xwin);
786              if (utilx_level == UTILX_NOTIFICATION_LEVEL_LOW)
787                *level = EFL_UTIL_NOTIFICATION_LEVEL_1;
788              else if(utilx_level == UTILX_NOTIFICATION_LEVEL_NORMAL)
789                *level = EFL_UTIL_NOTIFICATION_LEVEL_2;
790              else if(utilx_level == UTILX_NOTIFICATION_LEVEL_HIGH)
791                *level = EFL_UTIL_NOTIFICATION_LEVEL_3;
792              else
793                return EFL_UTIL_ERROR_INVALID_PARAMETER;
794           }
795         else
796           return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
797
798         return EFL_UTIL_ERROR_NONE;
799      }
800
801    return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
802 #endif /* end of X11 */
803
804 #if WAYLAND
805    Elm_Win_Type type;
806    Ecore_Wl_Window *wlwin;
807    struct wl_surface *surface;
808    Efl_Util_Wl_Surface_Lv_Info *lv_info;
809    Ecore_Wl_Window_Type wl_type;
810
811    res = _wl_init();
812    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
813
814    wlwin = elm_win_wl_window_get(window);
815    EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
816
817    type = elm_win_type_get(window);
818    if (type != ELM_WIN_NOTIFICATION)
819      {
820         wl_type = ecore_wl_window_type_get(wlwin);
821         EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
822                                         EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
823      }
824
825    while (!_eflutil.wl.policy.proto)
826      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
827
828    surface = ecore_wl_window_surface_get(wlwin);
829    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
830                                   EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
831
832    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
833    if (lv_info)
834      {
835         if (lv_info->wait_for_done)
836           {
837              int count = 0;
838              while ((lv_info->wait_for_done) && (count < 3))
839                {
840                   ecore_wl_flush();
841                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
842                   count++;
843                }
844
845              if (lv_info->wait_for_done)
846                {
847                   *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
848                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
849                }
850           }
851
852         switch (lv_info->level)
853           {
854            case TIZEN_POLICY_LEVEL_1:       *level = EFL_UTIL_NOTIFICATION_LEVEL_1;       break;
855            case TIZEN_POLICY_LEVEL_2:       *level = EFL_UTIL_NOTIFICATION_LEVEL_2;       break;
856            case TIZEN_POLICY_LEVEL_3:       *level = EFL_UTIL_NOTIFICATION_LEVEL_3;       break;
857            case TIZEN_POLICY_LEVEL_NONE:    *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE;    break;
858            case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
859            case TIZEN_POLICY_LEVEL_MEDIUM:  *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM;  break;
860            case TIZEN_POLICY_LEVEL_HIGH:    *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH;    break;
861            case TIZEN_POLICY_LEVEL_TOP:     *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP;     break;
862            default:                         *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
863             return EFL_UTIL_ERROR_INVALID_PARAMETER;
864           }
865         return EFL_UTIL_ERROR_NONE;
866      }
867    else
868      *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
869
870    return EFL_UTIL_ERROR_NONE;
871 #endif /* end of WAYLAND */
872 }
873
874 API int
875 efl_util_set_notification_window_level_error_cb(Evas_Object *window,
876                                                 efl_util_notification_window_level_error_cb callback,
877                                                 void *user_data)
878 {
879    Eina_Bool ret = EINA_FALSE;
880
881    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
882    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
883
884    ret = _cb_info_add(window,
885                       (Efl_Util_Cb)callback,
886                       user_data,
887                       CBH_NOTI_LEV);
888    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
889
890 #if X11
891    if (!_eflutil.cb_handler[CBH_NOTI_LEV].atom)
892      _eflutil.cb_handler[CBH_NOTI_LEV].atom = ecore_x_atom_get("_E_NOTIFICATION_LEVEL_ACCESS_RESULT");
893 #endif /* end of X11 */
894
895    return EFL_UTIL_ERROR_NONE;
896 }
897
898 API int
899 efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
900 {
901    Eina_Bool ret = EINA_FALSE;
902
903    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
904
905    ret = _cb_info_del_by_win(window, CBH_NOTI_LEV);
906    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
907
908    return EFL_UTIL_ERROR_NONE;
909 }
910
911 API int
912 efl_util_set_window_opaque_state(Evas_Object *window,
913                                  int opaque)
914 {
915    Eina_Bool res;
916
917    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
918    EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
919                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
920
921 #if X11
922    Ecore_X_Window xwin;
923    Utilx_Opaque_State state;
924    int ret;
925
926    res = _x11_init();
927    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
928
929    xwin = elm_win_xwindow_get(window);
930    EINA_SAFETY_ON_FALSE_RETURN_VAL(xwin > 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
931
932    if (opaque)
933      state = UTILX_OPAQUE_STATE_ON;
934    else
935      state = UTILX_OPAQUE_STATE_OFF;
936
937    ret = utilx_set_window_opaque_state(_eflutil.x11.dpy,
938                                        xwin,
939                                        state);
940
941    if (!ret)
942      return EFL_UTIL_ERROR_INVALID_PARAMETER;
943    else
944      return EFL_UTIL_ERROR_NONE;
945 #endif /* end of X11 */
946
947 #if WAYLAND
948    Ecore_Wl_Window *wlwin;
949    struct wl_surface *surface;
950
951    if (!_eflutil.wl.policy.proto)
952      {
953         int ret = 0;
954
955         res = _wl_init();
956         EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
957
958         while (!_eflutil.wl.policy.proto && ret != -1)
959           ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
960
961         EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.policy.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
962      }
963
964    wlwin = elm_win_wl_window_get(window);
965    if (!wlwin)
966       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
967
968    surface  = ecore_wl_window_surface_get(wlwin);
969    if (!surface)
970       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
971
972    tizen_policy_set_opaque_state(_eflutil.wl.policy.proto, surface, opaque);
973
974    return EFL_UTIL_ERROR_NONE;
975 #endif /* end of WAYLAND */
976 }
977
978 API int
979 efl_util_set_window_screen_mode(Evas_Object *window,
980                                 efl_util_screen_mode_e mode)
981 {
982    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
983    EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
984                                     (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
985                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
986
987 #if X11
988    Evas *e;
989    Ecore_Evas *ee;
990    int id;
991
992    e = evas_object_evas_get(window);
993    EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
994
995    ee = ecore_evas_ecore_evas_get(e);
996    EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
997
998    id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
999    if (mode == EFL_UTIL_SCREEN_MODE_ALWAYS_ON)
1000      {
1001         if (id == -1)
1002           ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "1");
1003         else
1004           ecore_evas_aux_hint_val_set(ee, id, "1");
1005      }
1006    else if (mode == EFL_UTIL_SCREEN_MODE_DEFAULT)
1007      {
1008         if (id == -1)
1009           ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "0");
1010         else
1011           ecore_evas_aux_hint_val_set(ee, id, "0");
1012      }
1013    else
1014      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1015
1016    return EFL_UTIL_ERROR_NONE;
1017 #endif /* end of X11 */
1018
1019 #if WAYLAND
1020    Ecore_Wl_Window *wlwin;
1021    struct wl_surface *surface;
1022    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1023    Eina_Bool res;
1024
1025    res = _wl_init();
1026    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1027
1028    wlwin = elm_win_wl_window_get(window);
1029    if (wlwin)
1030      {
1031         while (!_eflutil.wl.policy.proto)
1032           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1033
1034         surface = ecore_wl_window_surface_get(wlwin);
1035         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1036                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
1037
1038         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1039         if (!scr_mode_info)
1040           {
1041              scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
1042              EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
1043
1044              scr_mode_info->surface = surface;
1045              scr_mode_info->mode = (unsigned int)mode;
1046              scr_mode_info->wait_for_done = EINA_TRUE;
1047              scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1048
1049              eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
1050                            &surface,
1051                            scr_mode_info);
1052           }
1053         else
1054           {
1055              scr_mode_info->mode = (unsigned int)mode;
1056              scr_mode_info->wait_for_done = EINA_TRUE;
1057              scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1058           }
1059
1060         tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
1061                                             surface, (unsigned int)mode);
1062         if (scr_mode_info->wait_for_done)
1063           {
1064              int count = 0;
1065              while (scr_mode_info->wait_for_done && (count < 3))
1066                {
1067                   ecore_wl_flush();
1068                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1069                   count++;
1070                }
1071
1072              if (scr_mode_info->wait_for_done)
1073                {
1074                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
1075                }
1076              else
1077                {
1078                   if (scr_mode_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
1079                     {
1080                        return EFL_UTIL_ERROR_PERMISSION_DENIED;
1081                     }
1082                }
1083           }
1084
1085         return EFL_UTIL_ERROR_NONE;
1086      }
1087    else
1088      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1089 #endif /* end of WAYLAND */
1090 }
1091
1092 API int
1093 efl_util_get_window_screen_mode(Evas_Object *window,
1094                                 efl_util_screen_mode_e *mode)
1095 {
1096    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1097    EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
1098
1099 #if X11
1100    Evas *e;
1101    Ecore_Evas *ee;
1102    const char *str;
1103    int id;
1104
1105    e = evas_object_evas_get(window);
1106    EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
1107
1108    ee = ecore_evas_ecore_evas_get(e);
1109    EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
1110
1111    id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
1112    EINA_SAFETY_ON_TRUE_RETURN_VAL((id == -1), EFL_UTIL_ERROR_INVALID_PARAMETER);
1113
1114    str = ecore_evas_aux_hint_val_get(ee, id);
1115    EINA_SAFETY_ON_NULL_RETURN_VAL(str, EFL_UTIL_ERROR_INVALID_PARAMETER);
1116
1117    if (strncmp(str, "1", strlen("1")) == 0)
1118      *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON;
1119    else
1120      *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1121
1122    return EFL_UTIL_ERROR_NONE;
1123 #endif /* end of X11 */
1124
1125 #if WAYLAND
1126    Ecore_Wl_Window *wlwin;
1127    struct wl_surface *surface;
1128    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1129    Eina_Bool res;
1130
1131    res = _wl_init();
1132    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1133
1134    wlwin = elm_win_wl_window_get(window);
1135    if (wlwin)
1136      {
1137         while (!_eflutil.wl.policy.proto)
1138           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1139
1140         surface = ecore_wl_window_surface_get(wlwin);
1141         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1142                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
1143
1144         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1145         if (scr_mode_info)
1146           {
1147              if (scr_mode_info->wait_for_done)
1148                {
1149                   while (scr_mode_info->wait_for_done)
1150                     {
1151                        ecore_wl_flush();
1152                        wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1153                     }
1154                }
1155
1156              switch (scr_mode_info->mode)
1157                {
1158                 case TIZEN_POLICY_MODE_DEFAULT:   *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;   break;
1159                 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
1160                 default:                          *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1161                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
1162                }
1163              return EFL_UTIL_ERROR_NONE;
1164           }
1165         else
1166           {
1167              *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1168              return EFL_UTIL_ERROR_INVALID_PARAMETER;
1169           }
1170      }
1171    else
1172      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1173 #endif /* end of WAYLAND */
1174 }
1175
1176 API int
1177 efl_util_set_window_screen_mode_error_cb(Evas_Object *window,
1178                                          efl_util_window_screen_mode_error_cb callback,
1179                                          void *user_data)
1180 {
1181    Eina_Bool ret = EINA_FALSE;
1182
1183    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1184    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
1185
1186    ret = _cb_info_add(window,
1187                       (Efl_Util_Cb)callback,
1188                       user_data,
1189                       CBH_SCR_MODE);
1190    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1191
1192 #if X11
1193    if (!_eflutil.cb_handler[CBH_SCR_MODE].atom)
1194      _eflutil.cb_handler[CBH_SCR_MODE].atom = ecore_x_atom_get("_E_SCREEN_MODE_ACCESS_RESULT");
1195 #endif /* end of X11 */
1196
1197    return EFL_UTIL_ERROR_NONE;
1198 }
1199
1200 API int
1201 efl_util_unset_window_screen_mode_error_cb(Evas_Object *window)
1202 {
1203    Eina_Bool ret = EINA_FALSE;
1204
1205    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1206
1207    ret = _cb_info_del_by_win(window, CBH_SCR_MODE);
1208    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1209
1210    return EFL_UTIL_ERROR_NONE;
1211 }
1212
1213 API int
1214 efl_util_input_initialize_generator(efl_util_input_device_type_e dev_type)
1215 {
1216    return EFL_UTIL_ERROR_NONE;
1217 }
1218
1219 API void
1220 efl_util_input_deinitialize_generator(void)
1221 {
1222    return;
1223 }
1224
1225 API int
1226 efl_util_input_generate_key(const char *key_name,
1227                             int pressed)
1228 {
1229    return EFL_UTIL_ERROR_NONE;
1230 }
1231
1232 API int
1233 efl_util_input_generate_touch(int idx,
1234                               efl_util_input_touch_type_e touch_type,
1235                               int x,
1236                               int y)
1237 {
1238    return EFL_UTIL_ERROR_NONE;
1239 }
1240
1241 struct _efl_util_screenshot_h
1242 {
1243    int width;
1244    int height;
1245
1246 #if X11
1247    Ecore_X_Display *dpy;
1248    int internal_display;
1249    int screen;
1250    Window root;
1251    Pixmap pixmap;
1252    GC gc;
1253    Atom atom_capture;
1254
1255    /* port */
1256    int port;
1257
1258    /* damage */
1259    Damage   damage;
1260    int      damage_base;
1261
1262    /* dri2 */
1263    int eventBase, errorBase;
1264    int dri2Major, dri2Minor;
1265    char *driver_name, *device_name;
1266    drm_magic_t magic;
1267
1268    /* drm */
1269    int drm_fd;
1270 #endif
1271
1272    Eina_Bool shot_done;
1273
1274    /* tbm bufmgr */
1275    tbm_bufmgr bufmgr;
1276 };
1277
1278 /* scrrenshot handle */
1279 static efl_util_screenshot_h g_screenshot;
1280
1281 #if X11
1282 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1283 #define FOURCC_RGB32    FOURCC('R','G','B','4')
1284 #define TIMEOUT_CAPTURE 3
1285
1286 /* x error handling */
1287 static Bool g_efl_util_x_error_caught;
1288
1289 static int
1290 _efl_util_screenshot_x_error_handle(Display *dpy, XErrorEvent *ev)
1291 {
1292    if (!g_screenshot || (dpy != g_screenshot->dpy))
1293      return 0;
1294
1295    g_efl_util_x_error_caught = True;
1296
1297    return 0;
1298 }
1299
1300 static int
1301 _efl_util_screenshot_get_port(Display *dpy, unsigned int id, Window win)
1302 {
1303    unsigned int ver, rev, req_base, evt_base, err_base;
1304    unsigned int adaptors;
1305    XvAdaptorInfo *ai = NULL;
1306    XvImageFormatValues *fo = NULL;
1307    int formats;
1308    int i, j, p;
1309
1310    if (XvQueryExtension(dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1311      {
1312         fprintf(stderr, "[screenshot] fail: no XV extension. \n");
1313         return -1;
1314      }
1315
1316    if (XvQueryAdaptors(dpy, win, &adaptors, &ai) != Success)
1317      {
1318         fprintf(stderr, "[screenshot] fail: query adaptors. \n");
1319         return -1;
1320      }
1321
1322    EINA_SAFETY_ON_NULL_RETURN_VAL(ai, -1);
1323
1324    for (i = 0; i < adaptors; i++)
1325      {
1326         int support_format = False;
1327
1328         if (!(ai[i].type & XvInputMask) ||
1329             !(ai[i].type & XvStillMask))
1330           continue;
1331
1332         p = ai[i].base_id;
1333
1334         fo = XvListImageFormats(dpy, p, &formats);
1335         for (j = 0; j < formats; j++)
1336           if (fo[j].id == (int)id)
1337             support_format = True;
1338
1339         if (fo)
1340           XFree(fo);
1341
1342         if (!support_format)
1343           continue;
1344
1345         for (; p < ai[i].base_id + ai[i].num_ports; p++)
1346           {
1347              if (XvGrabPort(dpy, p, 0) == Success)
1348                {
1349                   XvFreeAdaptorInfo(ai);
1350                   return p;
1351                }
1352           }
1353      }
1354
1355    XvFreeAdaptorInfo(ai);
1356
1357    XSync(dpy, False);
1358
1359    return -1;
1360 }
1361
1362 static int _efl_util_screenshot_get_best_size(Display *dpy, int port, int width, int height, unsigned int *best_width, unsigned int *best_height)
1363 {
1364    XErrorHandler old_handler = NULL;
1365
1366    Atom atom_capture = XInternAtom(dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1367
1368    g_efl_util_x_error_caught = False;
1369    old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1370
1371    XvSetPortAttribute(dpy, port, atom_capture, 1);
1372    XSync(dpy, False);
1373
1374    g_efl_util_x_error_caught = False;
1375    XSetErrorHandler(old_handler);
1376
1377    XvQueryBestSize(dpy, port, 0, 0, 0, width, height, best_width, best_height);
1378    if (best_width <= 0 || best_height <= 0)
1379      return 0;
1380
1381    return 1;
1382 }
1383 #endif
1384
1385 API efl_util_screenshot_h efl_util_screenshot_initialize(int width, int height)
1386 {
1387 #if X11
1388    efl_util_screenshot_h screenshot = NULL;
1389    int depth = 0;
1390    int damage_err_base = 0;
1391    unsigned int best_width = 0;
1392    unsigned int best_height = 0;
1393
1394    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1395    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1396
1397    if (g_screenshot != NULL)
1398      {
1399         if (g_screenshot->width != width || g_screenshot->height != height)
1400           {
1401              // TODO: recreate pixmap and update information
1402              if (!_efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height))
1403                {
1404                   set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1405                   return NULL;
1406                }
1407
1408              g_screenshot->width = width;
1409              g_screenshot->height = height;
1410           }
1411
1412         return g_screenshot;
1413      }
1414
1415    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1416    EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1417
1418    /* set dpy */
1419    screenshot->dpy = ecore_x_display_get();
1420    if (!screenshot->dpy)
1421      {
1422         screenshot->dpy = XOpenDisplay(0);
1423         EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_init);
1424
1425         /* for XCloseDisplay at denitialization */
1426         screenshot->internal_display = 1;
1427      }
1428
1429    /* set screen */
1430    screenshot->screen = DefaultScreen(screenshot->dpy);
1431
1432    /* set root window */
1433    screenshot->root = DefaultRootWindow(screenshot->dpy);
1434
1435    /* initialize capture adaptor */
1436    screenshot->port = _efl_util_screenshot_get_port(screenshot->dpy, FOURCC_RGB32, screenshot->root);
1437    EINA_SAFETY_ON_FALSE_GOTO(screenshot->port > 0, fail_init);
1438
1439    /* get the best size */
1440    _efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height);
1441    EINA_SAFETY_ON_FALSE_GOTO(best_width > 0, fail_init);
1442    EINA_SAFETY_ON_FALSE_GOTO(best_height > 0, fail_init);
1443
1444    /* set the width and the height */
1445    screenshot->width = best_width;
1446    screenshot->height = best_height;
1447
1448    /* create a pixmap */
1449    depth = DefaultDepth(screenshot->dpy, screenshot->screen);
1450    screenshot->pixmap = XCreatePixmap(screenshot->dpy, screenshot->root, screenshot->width, screenshot->height, depth);
1451    EINA_SAFETY_ON_FALSE_GOTO(screenshot->pixmap > 0, fail_init);
1452
1453    screenshot->gc = XCreateGC(screenshot->dpy, screenshot->pixmap, 0, 0);
1454    EINA_SAFETY_ON_NULL_GOTO(screenshot->gc, fail_init);
1455
1456    XSetForeground(screenshot->dpy, screenshot->gc, 0xFF000000);
1457    XFillRectangle(screenshot->dpy, screenshot->pixmap, screenshot->gc, 0, 0, width, height);
1458
1459    /* initialize damage */
1460    if (!XDamageQueryExtension(screenshot->dpy, &screenshot->damage_base, &damage_err_base))
1461      goto fail_init;
1462
1463    screenshot->damage = XDamageCreate(screenshot->dpy, screenshot->pixmap, XDamageReportNonEmpty);
1464    EINA_SAFETY_ON_FALSE_GOTO(screenshot->damage > 0, fail_init);
1465
1466    /* initialize dri3 and dri2 */
1467    if (!DRI2QueryExtension(screenshot->dpy, &screenshot->eventBase, &screenshot->errorBase))
1468      {
1469         fprintf(stderr, "[screenshot] fail: DRI2QueryExtention\n");
1470         goto fail_init;
1471      }
1472
1473    if (!DRI2QueryVersion(screenshot->dpy, &screenshot->dri2Major, &screenshot->dri2Minor))
1474      {
1475         fprintf(stderr, "[screenshot] fail: DRI2QueryVersion\n");
1476         goto fail_init;
1477      }
1478
1479    if (!DRI2Connect(screenshot->dpy, screenshot->root, &screenshot->driver_name, &screenshot->device_name))
1480      {
1481         fprintf(stderr, "[screenshot] fail: DRI2Connect\n");
1482         goto fail_init;
1483      }
1484
1485    screenshot->drm_fd = open(screenshot->device_name, O_RDWR);
1486    EINA_SAFETY_ON_FALSE_GOTO(screenshot->drm_fd >= 0, fail_init);
1487
1488    if (drmGetMagic(screenshot->drm_fd, &screenshot->magic))
1489      {
1490         fprintf(stderr, "[screenshot] fail: drmGetMagic\n");
1491         goto fail_init;
1492      }
1493
1494    if (!DRI2Authenticate(screenshot->dpy, screenshot->root, screenshot->magic))
1495      {
1496         fprintf(stderr, "[screenshot] fail: DRI2Authenticate\n");
1497         goto fail_init;
1498      }
1499
1500    if (!drmAuthMagic(screenshot->drm_fd, screenshot->magic))
1501      {
1502         fprintf(stderr, "[screenshot] fail: drmAuthMagic\n");
1503         goto fail_init;
1504      }
1505
1506    DRI2CreateDrawable(screenshot->dpy, screenshot->pixmap);
1507
1508    /* tbm bufmgr */
1509    screenshot->bufmgr = tbm_bufmgr_init(screenshot->drm_fd);
1510    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1511
1512    XFlush(screenshot->dpy);
1513
1514    g_screenshot = screenshot;
1515    set_last_result(EFL_UTIL_ERROR_NONE);
1516
1517    return g_screenshot;
1518 #endif
1519
1520 #if WAYLAND
1521    efl_util_screenshot_h screenshot = NULL;
1522
1523    if (!_eflutil.wl.shot.screenshooter)
1524      {
1525         int ret = 0;
1526         _wl_init();
1527         while (!_eflutil.wl.shot.screenshooter && ret != -1)
1528           ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1529         EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.screenshooter, fail_init);
1530
1531        _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
1532        EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_init);
1533      }
1534
1535    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1536    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1537
1538    if (g_screenshot)
1539      {
1540         if (g_screenshot->width != width || g_screenshot->height != height)
1541           {
1542              g_screenshot->width = width;
1543              g_screenshot->height = height;
1544           }
1545
1546         return g_screenshot;
1547      }
1548
1549    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1550    EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1551
1552    screenshot->width = width;
1553    screenshot->height = height;
1554
1555    screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
1556    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1557
1558    g_screenshot = screenshot;
1559    set_last_result(EFL_UTIL_ERROR_NONE);
1560
1561    screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
1562
1563    return g_screenshot;
1564 #endif
1565 fail_param:
1566    if (screenshot)
1567      efl_util_screenshot_deinitialize(screenshot);
1568    set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1569    return NULL;
1570 fail_memory:
1571    if (screenshot)
1572      efl_util_screenshot_deinitialize(screenshot);
1573    set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1574    return NULL;
1575 fail_init:
1576    if (screenshot)
1577      efl_util_screenshot_deinitialize(screenshot);
1578    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1579    return NULL;
1580 }
1581
1582 API int efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
1583 {
1584 #if X11
1585    if (!screenshot)
1586      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1587
1588    /* tbm bufmgr */
1589    if (screenshot->bufmgr)
1590      tbm_bufmgr_deinit(screenshot->bufmgr);
1591
1592    DRI2DestroyDrawable(screenshot->dpy, screenshot->pixmap);
1593
1594    /* dri2 */
1595    if (screenshot->drm_fd)
1596      close(screenshot->drm_fd);
1597    if (screenshot->driver_name)
1598      free(screenshot->driver_name);
1599    if (screenshot->device_name)
1600      free(screenshot->device_name);
1601
1602    /* xv */
1603    if (screenshot->port > 0 && screenshot->pixmap > 0)
1604      XvStopVideo(screenshot->dpy, screenshot->port, screenshot->pixmap);
1605
1606    /* damage */
1607    if (screenshot->damage)
1608      XDamageDestroy(screenshot->dpy, screenshot->damage);
1609
1610    /* gc */
1611    if (screenshot->gc)
1612      XFreeGC(screenshot->dpy, screenshot->gc);
1613
1614    /* pixmap */
1615    if (screenshot->pixmap > 0)
1616      XFreePixmap(screenshot->dpy, screenshot->pixmap);
1617
1618    /* port */
1619    if (screenshot->port > 0)
1620      XvUngrabPort(screenshot->dpy, screenshot->port, 0);
1621
1622    XSync(screenshot->dpy, False);
1623
1624    /* dpy */
1625    if (screenshot->internal_display ==1 && screenshot->dpy)
1626      XCloseDisplay(screenshot->dpy);
1627
1628    free(screenshot);
1629    g_screenshot = NULL;
1630
1631    return EFL_UTIL_ERROR_NONE;
1632 #endif
1633 #if WAYLAND
1634    if (!screenshot)
1635      return EFL_UTIL_ERROR_NONE;
1636
1637    free(screenshot);
1638    g_screenshot = NULL;
1639
1640    if (_eflutil.wl.shot.screenshooter)
1641      screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, NULL);
1642
1643    return EFL_UTIL_ERROR_NONE;
1644 #endif
1645 }
1646
1647
1648 API tbm_surface_h efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
1649 {
1650 #if X11
1651    XEvent ev = {0,};
1652    XErrorHandler old_handler = NULL;
1653    unsigned int attachment = DRI2BufferFrontLeft;
1654    int nbufs = 0;
1655    DRI2Buffer *bufs = NULL;
1656    tbm_bo t_bo = NULL;
1657    tbm_surface_h t_surface = NULL;
1658    int buf_width = 0;
1659    int buf_height = 0;
1660    tbm_surface_info_s surf_info;
1661    int i;
1662
1663    if (screenshot != g_screenshot)
1664      {
1665         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1666         return NULL;
1667      }
1668
1669    /* for flush other pending requests and pending events */
1670    XSync(screenshot->dpy, 0);
1671
1672    g_efl_util_x_error_caught = False;
1673    old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1674
1675    /* dump here */
1676    XvPutStill(screenshot->dpy, screenshot->port, screenshot->pixmap, screenshot->gc,
1677               0, 0, screenshot->width, screenshot->height,
1678               0, 0, screenshot->width, screenshot->height);
1679
1680    XSync(screenshot->dpy, 0);
1681
1682    if (g_efl_util_x_error_caught)
1683      {
1684         g_efl_util_x_error_caught = False;
1685         XSetErrorHandler(old_handler);
1686         goto fail;
1687      }
1688
1689    g_efl_util_x_error_caught = False;
1690    XSetErrorHandler(old_handler);
1691
1692    if (XPending(screenshot->dpy))
1693      XNextEvent(screenshot->dpy, &ev);
1694    else
1695      {
1696         int fd = ConnectionNumber(screenshot->dpy);
1697         fd_set mask;
1698         struct timeval tv;
1699         int ret;
1700
1701         FD_ZERO(&mask);
1702         FD_SET(fd, &mask);
1703
1704         tv.tv_usec = 0;
1705         tv.tv_sec = TIMEOUT_CAPTURE;
1706
1707         ret = select(fd + 1, &mask, 0, 0, &tv);
1708         if (ret < 0)
1709           fprintf(stderr, "[screenshot] fail: select.\n");
1710         else if (ret == 0)
1711           fprintf(stderr, "[screenshot] fail: timeout(%d sec)!\n", TIMEOUT_CAPTURE);
1712         else if (XPending(screenshot->dpy))
1713           XNextEvent(screenshot->dpy, &ev);
1714         else
1715           fprintf(stderr, "[screenshot] fail: not passed a event!\n");
1716      }
1717
1718    /* check if the capture is done by xserver and pixmap has got the captured image */
1719    if (ev.type == (screenshot->damage_base + XDamageNotify))
1720      {
1721         XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1722         if (damage_ev->drawable == screenshot->pixmap)
1723           {
1724              /* Get DRI2 FrontLeft buffer of the pixmap */
1725              bufs = DRI2GetBuffers(screenshot->dpy, screenshot->pixmap, &buf_width, &buf_height, &attachment, 1, &nbufs);
1726              if (!bufs)
1727                {
1728                   fprintf(stderr, "[screenshot] fail: DRI2GetBuffers\n");
1729                   goto fail;
1730                }
1731
1732              t_bo = tbm_bo_import(screenshot->bufmgr, bufs[0].name);
1733              if (!t_bo)
1734                {
1735                   fprintf(stderr, "[screenshot] fail: import tbm_bo!\n");
1736                   goto fail;
1737                }
1738
1739              surf_info.width = buf_width;
1740              surf_info.height = buf_height;
1741              surf_info.format = TBM_FORMAT_XRGB8888;
1742              surf_info.bpp = 32;
1743              surf_info.size = bufs->pitch * surf_info.height;
1744              surf_info.num_planes = 1;
1745              for (i = 0; i < surf_info.num_planes; i++)
1746                {
1747                   surf_info.planes[i].size = bufs->pitch * surf_info.height;
1748                   surf_info.planes[i].stride = bufs->pitch;
1749                   surf_info.planes[i].offset = 0;
1750                }
1751              t_surface = tbm_surface_internal_create_with_bos(&surf_info, &t_bo, 1);
1752              if (!t_surface)
1753                {
1754                   fprintf(stderr, "[screenshot] fail: get tbm_surface!\n");
1755                   goto fail;
1756                }
1757
1758              tbm_bo_unref(t_bo);
1759              free(bufs);
1760
1761              XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1762
1763              set_last_result(EFL_UTIL_ERROR_NONE);
1764
1765              return t_surface;
1766           }
1767
1768         XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1769      }
1770
1771 fail:
1772
1773    if (t_bo)
1774      tbm_bo_unref(t_bo);
1775    if (bufs)
1776      free(bufs);
1777
1778    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1779
1780    return NULL;
1781 #endif
1782
1783 #if WAYLAND
1784    tbm_surface_h t_surface = NULL;
1785    struct wl_buffer *buffer = NULL;
1786    Efl_Util_Wl_Output_Info *output;
1787    int ret = 0;
1788
1789    if (screenshot != g_screenshot)
1790      {
1791         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1792         return NULL;
1793      }
1794
1795    output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
1796    if (!output)
1797      {
1798         fprintf(stderr, "[screenshot] fail: no output for screenshot\n");
1799         goto fail;
1800      }
1801
1802    t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
1803    if (!t_surface)
1804      {
1805         fprintf(stderr, "[screenshot] fail: tbm_surface_create\n");
1806         goto fail;
1807      }
1808
1809    buffer = wayland_tbm_client_create_buffer(_eflutil.wl.shot.tbm_client, t_surface);
1810    if (!buffer)
1811      {
1812         fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n");
1813         goto fail;
1814      }
1815
1816    screenshooter_shoot(_eflutil.wl.shot.screenshooter, output->output, buffer);
1817
1818    screenshot->shot_done = EINA_FALSE;
1819    while (!screenshot->shot_done && ret != -1)
1820      ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1821
1822    if (ret == -1)
1823      {
1824         fprintf(stderr, "[screenshot] fail: screenshooter_shoot\n");
1825         goto fail;
1826      }
1827
1828    wl_buffer_destroy(buffer);
1829
1830    /* reset shot_done for next screenshot */
1831    screenshot->shot_done = EINA_FALSE;
1832
1833    return t_surface;
1834
1835 fail:
1836    if (t_surface)
1837      tbm_surface_destroy(t_surface);
1838    if (buffer)
1839      wl_buffer_destroy(buffer);
1840
1841    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1842
1843    return NULL;
1844 #endif
1845 }