Added comments for utc Coverage
[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 #include <Ecore_Wayland.h>
35 #include <wayland-client.h>
36 #include <wayland-tbm-client.h>
37 #include <tizen-extension-client-protocol.h>
38 #include <screenshooter-client-protocol.h>
39
40 /* callback handler index */
41 #define CBH_NOTI_LEV 0
42 #define CBH_SCR_MODE 1
43 #define CBH_MAX      2
44
45 typedef void (*Efl_Util_Cb)(Evas_Object *, int, void *);
46
47 typedef struct _Efl_Util_Callback_Info
48 {
49    Evas_Object *win;
50    Efl_Util_Cb cb;
51    void *data;
52 } Efl_Util_Callback_Info;
53
54 typedef struct _Efl_Util_Wl_Surface_Lv_Info
55 {
56    void *surface; /* wl_surface */
57    int level;
58    Eina_Bool wait_for_done;
59    uint32_t state;
60 } Efl_Util_Wl_Surface_Lv_Info;
61
62 typedef struct _Efl_Util_Wl_Surface_Scr_Mode_Info
63 {
64    void *surface; /* wl_surface */
65    unsigned int mode;
66    Eina_Bool wait_for_done;
67    uint32_t state;
68 } Efl_Util_Wl_Surface_Scr_Mode_Info;
69
70 typedef struct _Efl_Util_Wl_Surface_Brightness_Info
71 {
72    void *surface; /* wl_surface */
73    int brightness;
74    Eina_Bool wait_for_done;
75    uint32_t state;
76 } Efl_Util_Wl_Surface_Brightness_Info;
77
78 typedef struct _Efl_Util_Wl_Output_Info
79 {
80     struct wl_output *output;
81     int offset_x, offset_y, width, height;
82 } Efl_Util_Wl_Output_Info;
83
84 typedef struct _Efl_Util_Data
85 {
86    /* wayland related stuffs */
87    struct
88    {
89       Eina_Bool init;
90       struct wl_display *dpy;
91       struct wl_event_queue *queue;
92
93       struct
94       {
95          struct tizen_policy *proto;
96          Eina_Hash *hash_noti_lv;
97          Eina_Hash *hash_scr_mode;
98       } policy;
99       struct
100       {
101          struct screenshooter *screenshooter;
102          struct wayland_tbm_client *tbm_client;
103          Eina_List *output_list;
104       } shot;
105       struct
106       {
107          struct tizen_input_device_manager *devicemgr;
108          int request_notified;
109       } devmgr;
110       struct
111       {
112          struct tizen_display_policy *proto;
113          Eina_Hash *hash_brightness;
114       } display_policy;
115    } wl;
116
117    struct
118    {
119       Eina_List *info_list; /* list of callback info */
120       unsigned int atom; /* x11 atom */
121    } cb_handler[CBH_MAX];
122 } Efl_Util_Data;
123
124 static Efl_Util_Data _eflutil =
125 {
126    {
127       EINA_FALSE,
128       NULL, NULL,
129       { NULL, NULL, NULL }, /* tizen_policy protocol */
130       { NULL, NULL, NULL }, /* screenshooter protocol */
131       { NULL, -1 } /* tizen_input_device_manager protocol */
132    },
133    {
134       { NULL, 0 }, /* handler for notification level */
135       { NULL, 0 }  /* handler for screen mode */
136    },
137 };
138
139 static Eina_Bool               _cb_info_add(Evas_Object *win, Efl_Util_Cb cb, void *data, int idx);
140 static Eina_Bool               _cb_info_del_by_win(Evas_Object *win, int idx);
141 static Eina_List              *_cb_info_list_get(int idx);
142 static Efl_Util_Callback_Info *_cb_info_find_by_win(Evas_Object *win, int idx);
143 static Eina_Bool               _wl_init(void);
144 static void                    _cb_wl_reg_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
145 static void                    _cb_wl_reg_global_remove(void *data, struct wl_registry *reg, unsigned int name);
146 static Efl_Util_Callback_Info *_cb_info_find_by_wlsurf(void *wlsurf, int idx);
147 static void                    _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t is_conformant);
148 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);
149 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);
150 static void                    _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id);
151 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);
152 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);
153 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);
154 static void                    _cb_wl_tz_policy_allowed_aux_hint(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, int id);
155
156 static void                    _cb_wl_tz_display_policy_brightness_done(void *data, struct tizen_display_policy *tizen_display_policy, struct wl_surface *surface_resource, int32_t brightness, uint32_t state);
157
158 static void                    _cb_device_add(void *data EINA_UNUSED, struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED, uint32_t serial  EINA_UNUSED, const char *identifier  EINA_UNUSED, struct tizen_input_device *device  EINA_UNUSED, struct wl_seat *seat EINA_UNUSED);
159 static void                    _cb_device_remove(void *data EINA_UNUSED, struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED, uint32_t serial EINA_UNUSED, const char *identifier  EINA_UNUSED, struct tizen_input_device *device EINA_UNUSED, struct wl_seat *seat EINA_UNUSED);
160 static void                    _cb_error(void *data EINA_UNUSED, struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED, uint32_t errorcode);
161 static void                    _cb_block_expired(void *data EINA_UNUSED, struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED);
162
163 static const struct wl_registry_listener _wl_reg_listener =
164 {
165    _cb_wl_reg_global,
166    _cb_wl_reg_global_remove
167 };
168
169 struct tizen_policy_listener _wl_tz_policy_listener =
170 {
171    _cb_wl_tz_policy_conformant,
172    _cb_wl_tz_policy_conformant_area,
173    _cb_wl_tz_policy_notification_done,
174    _cb_wl_tz_policy_transient_for_done,
175    _cb_wl_tz_policy_scr_mode_done,
176    _cb_wl_tz_policy_iconify_state_changed,
177    _cb_wl_tz_policy_supported_aux_hints,
178    _cb_wl_tz_policy_allowed_aux_hint,
179 };
180
181 struct tizen_input_device_manager_listener _wl_tz_devmgr_listener =
182 {
183    _cb_device_add,
184    _cb_device_remove,
185    _cb_error,
186    _cb_block_expired
187 };
188
189 struct tizen_display_policy_listener _wl_tz_display_policy_listener =
190 {
191    _cb_wl_tz_display_policy_brightness_done,
192 };
193
194 static Eina_Bool
195 _cb_info_add(Evas_Object *win,
196              Efl_Util_Cb cb,
197              void *data,
198              int idx)
199 {
200    Efl_Util_Callback_Info *info;
201
202    info = _cb_info_find_by_win(win, idx);
203    if (info)
204      {
205         _eflutil.cb_handler[idx].info_list
206            = eina_list_remove(_eflutil.cb_handler[idx].info_list,
207                               info);
208         free(info);
209      }
210
211    info = (Efl_Util_Callback_Info *)calloc(1, sizeof(Efl_Util_Callback_Info));
212    if (!info) return EINA_FALSE;
213
214    info->win = win;
215    info->cb = cb;
216    info->data = data;
217
218    _eflutil.cb_handler[idx].info_list
219       = eina_list_append(_eflutil.cb_handler[idx].info_list,
220                          info);
221
222    return EINA_TRUE;
223 }
224
225 static Eina_Bool
226 _cb_info_del_by_win(Evas_Object *win,
227                     int idx)
228 {
229    Efl_Util_Callback_Info *info;
230
231    info = _cb_info_find_by_win(win, idx);
232    if (!info) return EINA_FALSE;
233
234    _eflutil.cb_handler[idx].info_list
235       = eina_list_remove(_eflutil.cb_handler[idx].info_list,
236                          info);
237    free(info);
238
239    return EINA_TRUE;
240 }
241
242 static Eina_List *
243 _cb_info_list_get(int idx)
244 {
245    return _eflutil.cb_handler[idx].info_list;
246 }
247
248 static Efl_Util_Callback_Info *
249 _cb_info_find_by_win(Evas_Object *win,
250                      int idx)
251 {
252    Eina_List *l, *ll;
253    Efl_Util_Callback_Info *info;
254
255    l = _cb_info_list_get(idx);
256    EINA_LIST_FOREACH(l, ll, info)
257      {
258         if (info->win == win) return info;
259      }
260
261    return NULL;
262 }
263
264 static Eina_Bool
265 _wl_init(void)
266 {
267    struct wl_registry *reg = NULL;
268
269    if (_eflutil.wl.init) return EINA_TRUE;
270
271    ecore_wl_init(NULL);
272
273    _eflutil.wl.dpy = ecore_wl_display_get();
274    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.dpy, fail);
275
276    _eflutil.wl.queue = wl_display_create_queue(_eflutil.wl.dpy);
277    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.queue, fail);
278
279    reg = wl_display_get_registry(_eflutil.wl.dpy);
280    EINA_SAFETY_ON_NULL_GOTO(reg, fail);
281
282    wl_proxy_set_queue((struct wl_proxy*)reg, _eflutil.wl.queue);
283    wl_registry_add_listener(reg, &_wl_reg_listener, NULL);
284
285    _eflutil.wl.init = EINA_TRUE;
286
287    return EINA_TRUE;
288 fail:
289    if (_eflutil.wl.queue)
290      {
291         wl_event_queue_destroy(_eflutil.wl.queue);
292         _eflutil.wl.queue = NULL;
293      }
294
295    if (reg)
296      wl_registry_destroy(reg);
297    ecore_wl_shutdown();
298    return EINA_FALSE;
299 }
300
301 static void
302 _cb_wl_output_geometry(void *data, struct wl_output *wl_output, int x, int y,
303                        int physical_width, int physical_height, int subpixel,
304                        const char *make, const char *model, int transform)
305 {
306    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
307    if (wl_output == output->output)
308      {
309         output->offset_x = x;
310         output->offset_y = y;
311      }
312 }
313
314 static void
315 _cb_wl_output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
316                    int width, int height, int refresh)
317 {
318    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
319    if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT))
320      {
321         output->width = width;
322         output->height = height;
323      }
324 }
325
326 static void
327 _cb_wl_output_done(void *data, struct wl_output *wl_output)
328 {
329 }
330
331 static void
332 _cb_wl_output_scale(void *data, struct wl_output *wl_output, int32_t factor)
333 {
334 }
335
336 static const struct wl_output_listener output_listener =
337 {
338     _cb_wl_output_geometry,
339     _cb_wl_output_mode,
340     _cb_wl_output_done,
341     _cb_wl_output_scale
342 };
343
344 static void
345 _cb_wl_screenshot_done(void *data, struct screenshooter *screenshooter)
346 {
347    Eina_Bool *shot_done = (Eina_Bool*)data;
348    if (shot_done)
349      *shot_done = EINA_TRUE;
350 }
351
352 static const struct screenshooter_listener screenshooter_listener =
353 {
354     _cb_wl_screenshot_done
355 };
356
357 static void
358 _cb_wl_reg_global(void *data,
359                   struct wl_registry *reg,
360                   unsigned int name,
361                   const char *interface,
362                   unsigned int version)
363 {
364    if (!strcmp(interface, "tizen_policy"))
365      {
366         struct tizen_policy *proto;
367         proto = wl_registry_bind(reg,
368                                   name,
369                                   &tizen_policy_interface,
370                                   1);
371         if (!proto) return;
372
373         tizen_policy_add_listener(proto,
374                                   &_wl_tz_policy_listener,
375                                   NULL);
376
377         _eflutil.wl.policy.hash_noti_lv = eina_hash_pointer_new(free);
378         _eflutil.wl.policy.hash_scr_mode = eina_hash_pointer_new(free);
379         _eflutil.wl.policy.proto = proto;
380      }
381    else if (strcmp(interface, "wl_output") == 0)
382      {
383         Efl_Util_Wl_Output_Info *output = calloc(1, sizeof(Efl_Util_Wl_Output_Info));
384         EINA_SAFETY_ON_NULL_RETURN(output);
385
386         _eflutil.wl.shot.output_list = eina_list_append(_eflutil.wl.shot.output_list, output);
387
388         output->output = wl_registry_bind(reg, name, &wl_output_interface, version);
389         wl_output_add_listener(output->output, &output_listener, output);
390      }
391    else if (strcmp(interface, "screenshooter") == 0)
392      {
393         _eflutil.wl.shot.screenshooter = wl_registry_bind(reg, name, &screenshooter_interface, version);
394         screenshooter_add_listener(_eflutil.wl.shot.screenshooter, &screenshooter_listener, NULL);
395      }
396    else if (strcmp(interface, "tizen_input_device_manager") == 0)
397      {
398         _eflutil.wl.devmgr.devicemgr = wl_registry_bind(reg, name, &tizen_input_device_manager_interface, version);
399         tizen_input_device_manager_add_listener(_eflutil.wl.devmgr.devicemgr, &_wl_tz_devmgr_listener, NULL);
400      }
401    else if (!strcmp(interface, "tizen_display_policy"))
402      {
403         _eflutil.wl.display_policy.proto = wl_registry_bind(reg, name, &tizen_display_policy_interface, version);
404         if (!_eflutil.wl.display_policy.proto) return;
405
406         tizen_display_policy_add_listener(_eflutil.wl.display_policy.proto,
407                                           &_wl_tz_display_policy_listener,
408                                           NULL);
409
410         _eflutil.wl.display_policy.hash_brightness = eina_hash_pointer_new(free);
411      }
412
413 }
414 /* LCOV_EXCL_START */
415 static void
416 _cb_wl_reg_global_remove(void *data,
417                          struct wl_registry *reg,
418                          unsigned int name)
419 {
420    _eflutil.wl.policy.proto = NULL;
421    eina_hash_free(_eflutil.wl.policy.hash_noti_lv);
422    eina_hash_free(_eflutil.wl.policy.hash_scr_mode);
423
424    _eflutil.wl.display_policy.proto = NULL;
425    eina_hash_free(_eflutil.wl.display_policy.hash_brightness);
426 }
427
428 static Efl_Util_Callback_Info *
429 _cb_info_find_by_wlsurf(void *wlsurf,
430                         int idx)
431 {
432    Eina_List *l, *ll;
433    Efl_Util_Callback_Info *info;
434    Ecore_Wl_Window *wlwin2 = NULL;
435    void *wlsurf2 = NULL;
436
437    l = _cb_info_list_get(idx);
438    EINA_LIST_FOREACH(l, ll, info)
439      {
440         wlwin2 = elm_win_wl_window_get(info->win);
441         wlsurf2 = ecore_wl_window_surface_get(wlwin2);
442         if (wlsurf== wlsurf2) return info;
443      }
444
445    return NULL;
446 }
447
448 static void
449 _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy,
450                             struct wl_surface *surface, uint32_t is_conformant)
451 {
452 }
453
454 static void
455 _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy,
456                                  struct wl_surface *surface, uint32_t conformant_part,
457                                  uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h)
458 {
459 }
460 /* LCOV_EXCL_STOP */
461
462 static void
463 _cb_wl_tz_policy_notification_done(void *data,
464                                    struct tizen_policy *tizen_policy,
465                                    struct wl_surface *surface,
466                                    int32_t level,
467                                    uint32_t state)
468 {
469    Efl_Util_Wl_Surface_Lv_Info *lv_info;
470    Efl_Util_Callback_Info *cb_info;
471
472    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
473    if (lv_info)
474      {
475         lv_info->level = level;
476         lv_info->wait_for_done = EINA_FALSE;
477         lv_info->state = state;
478      }
479
480    if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
481
482    cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_NOTI_LEV);
483    if (!cb_info) return;
484    if (!cb_info->cb) return;
485
486    cb_info->cb(cb_info->win,
487                EFL_UTIL_ERROR_PERMISSION_DENIED,
488                cb_info->data);
489 }
490
491 /* LCOV_EXCL_START */
492 static void
493 _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id)
494 {
495 }
496 /* LCOV_EXCL_STOP */
497
498 static void
499 _cb_wl_tz_policy_scr_mode_done(void *data,
500                                struct tizen_policy *tizen_policy,
501                                struct wl_surface *surface,
502                                uint32_t mode,
503                                uint32_t state)
504 {
505
506    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
507    Efl_Util_Callback_Info *cb_info;
508
509    scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
510    if (scr_mode_info)
511      {
512         scr_mode_info->mode = mode;
513         scr_mode_info->wait_for_done = EINA_FALSE;
514         scr_mode_info->state = state;
515      }
516
517    if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
518
519    cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_SCR_MODE);
520    if (!cb_info) return;
521    if (!cb_info->cb) return;
522
523    cb_info->cb(cb_info->win,
524                EFL_UTIL_ERROR_PERMISSION_DENIED,
525                cb_info->data);
526 }
527
528 /* LCOV_EXCL_START */
529 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)
530 {
531 }
532
533 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)
534 {
535 }
536
537 static void                    _cb_wl_tz_policy_allowed_aux_hint(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, int id)
538 {
539 }
540 /* LCOV_EXCL_STOP */
541
542 static void
543 _cb_wl_tz_display_policy_brightness_done(void *data,
544                                  struct tizen_display_policy *tizen_display_policy,
545                                  struct wl_surface *surface,
546                                  int32_t brightness,
547                                  uint32_t state)
548 {
549    Efl_Util_Wl_Surface_Brightness_Info *brightness_info;
550
551    brightness_info = eina_hash_find(_eflutil.wl.display_policy.hash_brightness, &surface);
552    if (brightness_info)
553      {
554         brightness_info->brightness = brightness;
555         brightness_info->wait_for_done = EINA_FALSE;
556         brightness_info->state = state;
557      }
558 }
559
560 static void
561 _cb_window_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
562 {
563    Efl_Util_Wl_Surface_Lv_Info *lv_info;
564
565    lv_info = data;
566    if (EINA_UNLIKELY(!lv_info))
567      return;
568
569    eina_hash_del(_eflutil.wl.policy.hash_noti_lv, &lv_info->surface, lv_info);
570 }
571
572 API int
573 efl_util_set_notification_window_level(Evas_Object *window,
574                                        efl_util_notification_level_e level)
575 {
576    Eina_Bool res;
577
578    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
579    EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_NONE) &&
580                                    (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
581                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
582
583    Elm_Win_Type type;
584    Ecore_Wl_Window *wlwin;
585    struct wl_surface *surface;
586    Efl_Util_Wl_Surface_Lv_Info *lv_info;
587    Ecore_Wl_Window_Type wl_type;
588
589    res = _wl_init();
590    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
591
592    wlwin = elm_win_wl_window_get(window);
593    EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
594
595    type = elm_win_type_get(window);
596    if (type != ELM_WIN_NOTIFICATION)
597      {
598         wl_type = ecore_wl_window_type_get(wlwin);
599         EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
600                                         EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
601      }
602
603    while (!_eflutil.wl.policy.proto)
604      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
605
606    surface = ecore_wl_window_surface_get(wlwin);
607    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
608                                   EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
609
610    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
611    if (!lv_info)
612      {
613         lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
614         EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
615
616         lv_info->surface = surface;
617         lv_info->level = (int)level;
618         lv_info->wait_for_done = EINA_TRUE;
619         lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
620         eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
621                       &surface,
622                       lv_info);
623
624         evas_object_event_callback_add(window, EVAS_CALLBACK_DEL,
625                                        _cb_window_del, lv_info);
626      }
627    else
628      {
629         lv_info->level = (int)level;
630         lv_info->wait_for_done = EINA_TRUE;
631         lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
632      }
633
634
635    tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
636                                        surface, (int)level);
637
638    if (lv_info->wait_for_done)
639      {
640         int count = 0;
641         while (lv_info->wait_for_done && (count < 3))
642           {
643              ecore_wl_flush();
644              wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
645              count++;
646           }
647
648         if (lv_info->wait_for_done)
649           {
650              return EFL_UTIL_ERROR_INVALID_PARAMETER;
651           }
652         else
653           {
654              if (lv_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
655                {
656                   return EFL_UTIL_ERROR_PERMISSION_DENIED;
657                }
658           }
659      }
660
661    return EFL_UTIL_ERROR_NONE;
662 }
663
664 API int
665 efl_util_get_notification_window_level(Evas_Object *window,
666                                        efl_util_notification_level_e *level)
667 {
668    Eina_Bool res;
669
670    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
671    EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
672
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         if (lv_info->wait_for_done)
704           {
705              int count = 0;
706              while ((lv_info->wait_for_done) && (count < 3))
707                {
708                   ecore_wl_flush();
709                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
710                   count++;
711                }
712
713              if (lv_info->wait_for_done)
714                {
715                   *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
716                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
717                }
718           }
719
720         switch (lv_info->level)
721           {
722            case TIZEN_POLICY_LEVEL_1:       *level = EFL_UTIL_NOTIFICATION_LEVEL_1;       break;
723            case TIZEN_POLICY_LEVEL_2:       *level = EFL_UTIL_NOTIFICATION_LEVEL_2;       break;
724            case TIZEN_POLICY_LEVEL_3:       *level = EFL_UTIL_NOTIFICATION_LEVEL_3;       break;
725            case TIZEN_POLICY_LEVEL_NONE:    *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE;    break;
726            case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
727            case TIZEN_POLICY_LEVEL_MEDIUM:  *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM;  break;
728            case TIZEN_POLICY_LEVEL_HIGH:    *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH;    break;
729            case TIZEN_POLICY_LEVEL_TOP:     *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP;     break;
730            default:                         *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
731             return EFL_UTIL_ERROR_INVALID_PARAMETER;
732           }
733         return EFL_UTIL_ERROR_NONE;
734      }
735    else
736      *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
737
738    return EFL_UTIL_ERROR_NONE;
739 }
740
741 API int
742 efl_util_set_notification_window_level_error_cb(Evas_Object *window,
743                                                 efl_util_notification_window_level_error_cb callback,
744                                                 void *user_data)
745 {
746    Eina_Bool ret = EINA_FALSE;
747
748    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
749    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
750
751    ret = _cb_info_add(window,
752                       (Efl_Util_Cb)callback,
753                       user_data,
754                       CBH_NOTI_LEV);
755    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
756
757    return EFL_UTIL_ERROR_NONE;
758 }
759
760 API int
761 efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
762 {
763    Eina_Bool ret = EINA_FALSE;
764
765    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
766
767    ret = _cb_info_del_by_win(window, CBH_NOTI_LEV);
768    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
769
770    return EFL_UTIL_ERROR_NONE;
771 }
772
773 API int
774 efl_util_set_window_opaque_state(Evas_Object *window,
775                                  int opaque)
776 {
777    Eina_Bool res;
778
779    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
780    EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
781                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
782
783    Ecore_Wl_Window *wlwin;
784    struct wl_surface *surface;
785
786    if (!_eflutil.wl.policy.proto)
787      {
788         int ret = 0;
789
790         res = _wl_init();
791         EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
792
793         while (!_eflutil.wl.policy.proto && ret != -1)
794           ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
795
796         EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.policy.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
797      }
798
799    wlwin = elm_win_wl_window_get(window);
800    if (!wlwin)
801       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
802
803    surface  = ecore_wl_window_surface_get(wlwin);
804    if (!surface)
805       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
806
807    tizen_policy_set_opaque_state(_eflutil.wl.policy.proto, surface, opaque);
808
809    return EFL_UTIL_ERROR_NONE;
810 }
811
812 API int
813 efl_util_set_window_screen_mode(Evas_Object *window,
814                                 efl_util_screen_mode_e mode)
815 {
816    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
817    EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
818                                     (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
819                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
820
821    Ecore_Wl_Window *wlwin;
822    struct wl_surface *surface;
823    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
824    Eina_Bool res;
825
826    res = _wl_init();
827    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
828
829    wlwin = elm_win_wl_window_get(window);
830    if (wlwin)
831      {
832         while (!_eflutil.wl.policy.proto)
833           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
834
835         surface = ecore_wl_window_surface_get(wlwin);
836         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
837                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
838
839         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
840         if (!scr_mode_info)
841           {
842              scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
843              EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
844
845              scr_mode_info->surface = surface;
846              scr_mode_info->mode = (unsigned int)mode;
847              scr_mode_info->wait_for_done = EINA_TRUE;
848              scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
849
850              eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
851                            &surface,
852                            scr_mode_info);
853           }
854         else
855           {
856              scr_mode_info->mode = (unsigned int)mode;
857              scr_mode_info->wait_for_done = EINA_TRUE;
858              scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
859           }
860
861         tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
862                                             surface, (unsigned int)mode);
863         if (scr_mode_info->wait_for_done)
864           {
865              int count = 0;
866              while (scr_mode_info->wait_for_done && (count < 3))
867                {
868                   ecore_wl_flush();
869                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
870                   count++;
871                }
872
873              if (scr_mode_info->wait_for_done)
874                {
875                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
876                }
877              else
878                {
879                   if (scr_mode_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
880                     {
881                        return EFL_UTIL_ERROR_PERMISSION_DENIED;
882                     }
883                }
884           }
885
886         return EFL_UTIL_ERROR_NONE;
887      }
888    else
889      return EFL_UTIL_ERROR_INVALID_PARAMETER;
890 }
891
892 API int
893 efl_util_get_window_screen_mode(Evas_Object *window,
894                                 efl_util_screen_mode_e *mode)
895 {
896    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
897    EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
898
899    Ecore_Wl_Window *wlwin;
900    struct wl_surface *surface;
901    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
902    Eina_Bool res;
903
904    res = _wl_init();
905    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
906
907    wlwin = elm_win_wl_window_get(window);
908    if (wlwin)
909      {
910         while (!_eflutil.wl.policy.proto)
911           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
912
913         surface = ecore_wl_window_surface_get(wlwin);
914         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
915                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
916
917         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
918         if (scr_mode_info)
919           {
920              if (scr_mode_info->wait_for_done)
921                {
922                   while (scr_mode_info->wait_for_done)
923                     {
924                        ecore_wl_flush();
925                        wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
926                     }
927                }
928
929              switch (scr_mode_info->mode)
930                {
931                 case TIZEN_POLICY_MODE_DEFAULT:   *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;   break;
932                 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
933                 default:                          *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
934                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
935                }
936              return EFL_UTIL_ERROR_NONE;
937           }
938         else
939           {
940              *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
941              return EFL_UTIL_ERROR_INVALID_PARAMETER;
942           }
943      }
944    else
945      return EFL_UTIL_ERROR_INVALID_PARAMETER;
946 }
947
948 #ifndef TIZEN_WEARABLE
949 API int
950 efl_util_set_window_screen_mode_error_cb(Evas_Object *window,
951                                          efl_util_window_screen_mode_error_cb callback,
952                                          void *user_data)
953 {
954    Eina_Bool ret = EINA_FALSE;
955
956    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
957    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
958
959    ret = _cb_info_add(window,
960                       (Efl_Util_Cb)callback,
961                       user_data,
962                       CBH_SCR_MODE);
963    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
964
965    return EFL_UTIL_ERROR_NONE;
966 }
967
968 API int
969 efl_util_unset_window_screen_mode_error_cb(Evas_Object *window)
970 {
971    Eina_Bool ret = EINA_FALSE;
972
973    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
974
975    ret = _cb_info_del_by_win(window, CBH_SCR_MODE);
976    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
977
978    return EFL_UTIL_ERROR_NONE;
979 }
980 #endif
981
982 API int
983 efl_util_set_window_brightness(Evas_Object *window, int brightness)
984 {
985    Ecore_Wl_Window *wlwin;
986    struct wl_surface *surface;
987    Efl_Util_Wl_Surface_Brightness_Info *brightness_info;
988    Eina_Bool res;
989
990    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
991    EINA_SAFETY_ON_FALSE_RETURN_VAL(brightness <= 100, EFL_UTIL_ERROR_INVALID_PARAMETER);
992
993    res = _wl_init();
994    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
995
996    wlwin = elm_win_wl_window_get(window);
997    if (wlwin)
998      {
999         while (!_eflutil.wl.display_policy.proto)
1000           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1001
1002         surface = ecore_wl_window_surface_get(wlwin);
1003         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1004                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
1005
1006         brightness_info = eina_hash_find(_eflutil.wl.display_policy.hash_brightness, &surface);
1007         if (!brightness_info)
1008           {
1009              brightness_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Brightness_Info));
1010              EINA_SAFETY_ON_NULL_RETURN_VAL(brightness_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
1011
1012              brightness_info->surface = surface;
1013              brightness_info->brightness = brightness;
1014              brightness_info->wait_for_done = EINA_TRUE;
1015              brightness_info->state = TIZEN_DISPLAY_POLICY_ERROR_STATE_NONE;
1016
1017              eina_hash_add(_eflutil.wl.display_policy.hash_brightness,
1018                            &surface,
1019                            brightness_info);
1020            }
1021          else
1022            {
1023               brightness_info->brightness = brightness;
1024               brightness_info->wait_for_done = EINA_TRUE;
1025               brightness_info->state = TIZEN_DISPLAY_POLICY_ERROR_STATE_NONE;
1026            }
1027
1028          tizen_display_policy_set_window_brightness(_eflutil.wl.display_policy.proto,
1029                                                     surface, brightness);
1030          if (brightness_info->wait_for_done)
1031            {
1032               int count = 0;
1033               while (brightness_info->wait_for_done && (count < 3))
1034                 {
1035                    ecore_wl_flush();
1036                    wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1037                    count++;
1038                 }
1039
1040               if (brightness_info->wait_for_done)
1041                 {
1042                    return EFL_UTIL_ERROR_INVALID_PARAMETER;
1043                 }
1044               else
1045                 {
1046                    if (brightness_info->state == TIZEN_DISPLAY_POLICY_ERROR_STATE_PERMISSION_DENIED)
1047                      {
1048                         return EFL_UTIL_ERROR_PERMISSION_DENIED;
1049                      }
1050                 }
1051            }
1052         return EFL_UTIL_ERROR_NONE;
1053      }
1054    else
1055      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1056 }
1057
1058 API int
1059 efl_util_get_window_brightness(Evas_Object *window, int *brightness)
1060 {
1061    Ecore_Wl_Window *wlwin;
1062    struct wl_surface *surface;
1063    Efl_Util_Wl_Surface_Brightness_Info *brightness_info;
1064    Eina_Bool res;
1065
1066    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1067    EINA_SAFETY_ON_NULL_RETURN_VAL(brightness, EFL_UTIL_ERROR_INVALID_PARAMETER);
1068
1069    res = _wl_init();
1070    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1071
1072    wlwin = elm_win_wl_window_get(window);
1073    if (!wlwin) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1074
1075    while (!_eflutil.wl.display_policy.proto)
1076      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1077
1078    surface = ecore_wl_window_surface_get(wlwin);
1079    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1080                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
1081
1082    brightness_info = eina_hash_find(_eflutil.wl.display_policy.hash_brightness, &surface);
1083    if (brightness_info)
1084      {
1085         if (brightness_info->wait_for_done)
1086           {
1087              while (brightness_info->wait_for_done)
1088                {
1089                   ecore_wl_flush();
1090                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1091                }
1092           }
1093          *brightness = brightness_info->brightness;
1094      }
1095    else
1096      *brightness = -1;
1097
1098    return EFL_UTIL_ERROR_NONE;
1099 }
1100
1101
1102 struct _efl_util_inputgen_h
1103 {
1104    unsigned int init_type;
1105 };
1106
1107 static void
1108 _cb_device_add(void *data EINA_UNUSED,
1109                struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED,
1110                uint32_t serial EINA_UNUSED,
1111                const char *identifier EINA_UNUSED,
1112                struct tizen_input_device *device EINA_UNUSED,
1113                struct wl_seat *seat EINA_UNUSED)
1114 {
1115    ;
1116 }
1117
1118 /* LCOV_EXCL_START */
1119 static void
1120 _cb_device_remove(void *data EINA_UNUSED,
1121                struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED,
1122                uint32_t serial  EINA_UNUSED,
1123                const char *identifier  EINA_UNUSED,
1124                struct tizen_input_device *device  EINA_UNUSED,
1125                struct wl_seat *seat  EINA_UNUSED)
1126 {
1127    ;
1128 }
1129 /* LCOV_EXCL_STOP */
1130
1131 static void
1132 _cb_error(void *data EINA_UNUSED,
1133           struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED,
1134           uint32_t errorcode)
1135 {
1136    _eflutil.wl.devmgr.request_notified = errorcode;
1137 }
1138
1139 /* LCOV_EXCL_START */
1140 static void
1141 _cb_block_expired(void *data EINA_UNUSED,
1142                   struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED)
1143 {
1144    ;
1145 }
1146 /* LCOV_EXCL_STOP */
1147
1148 static efl_util_error_e
1149 _efl_util_input_convert_input_generator_error(int ret)
1150 {
1151    switch (ret)
1152      {
1153         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE:
1154            return EFL_UTIL_ERROR_NONE;
1155         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION:
1156            return EFL_UTIL_ERROR_PERMISSION_DENIED;
1157         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES:
1158            return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1159         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER:
1160            return EFL_UTIL_ERROR_INVALID_PARAMETER;
1161         default :
1162            return EFL_UTIL_ERROR_NONE;
1163      }
1164 }
1165
1166 API efl_util_inputgen_h
1167 efl_util_input_initialize_generator(efl_util_input_device_type_e dev_type)
1168 {
1169    int ret = EFL_UTIL_ERROR_NONE;
1170    efl_util_inputgen_h inputgen_h = NULL;
1171
1172    if ((dev_type <= EFL_UTIL_INPUT_DEVTYPE_NONE) ||
1173        (dev_type >= EFL_UTIL_INPUT_DEVTYPE_MAX))
1174      {
1175         set_last_result(EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1176         goto out;
1177      }
1178
1179    inputgen_h = (efl_util_inputgen_h)calloc(1, sizeof(struct _efl_util_inputgen_h));
1180    if (!inputgen_h)
1181      {
1182         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1183         goto out;
1184      }
1185
1186    inputgen_h->init_type |= dev_type;
1187
1188    ret = _wl_init();
1189    if (ret == (int)EINA_FALSE)
1190      {
1191         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1192         goto out;
1193      }
1194
1195    while (!_eflutil.wl.devmgr.devicemgr)
1196      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1197
1198    tizen_input_device_manager_init_generator(_eflutil.wl.devmgr.devicemgr);
1199
1200    while (_eflutil.wl.devmgr.request_notified == -1)
1201      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1202
1203    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1204    _eflutil.wl.devmgr.request_notified = -1;
1205
1206    set_last_result(ret);
1207    if (ret != TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE)
1208      goto out;
1209
1210    return inputgen_h;
1211
1212 out:
1213    if (inputgen_h)
1214      {
1215         free(inputgen_h);
1216         inputgen_h = NULL;
1217      }
1218    return NULL;
1219 }
1220
1221 API int
1222 efl_util_input_deinitialize_generator(efl_util_inputgen_h inputgen_h)
1223 {
1224    int ret = EFL_UTIL_ERROR_NONE;
1225    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1226
1227    free(inputgen_h);
1228    inputgen_h = NULL;
1229
1230    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1231
1232    tizen_input_device_manager_deinit_generator(_eflutil.wl.devmgr.devicemgr);
1233
1234    while (_eflutil.wl.devmgr.request_notified == -1)
1235      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1236
1237    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1238    _eflutil.wl.devmgr.request_notified = -1;
1239
1240    return ret;
1241 }
1242
1243 API int
1244 efl_util_input_generate_key(efl_util_inputgen_h inputgen_h, const char *key_name, int pressed)
1245 {
1246    int ret = EFL_UTIL_ERROR_NONE;
1247
1248    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1249    EINA_SAFETY_ON_NULL_RETURN_VAL(key_name, EFL_UTIL_ERROR_INVALID_PARAMETER);
1250    EINA_SAFETY_ON_FALSE_RETURN_VAL(pressed == 0 || pressed == 1, EFL_UTIL_ERROR_INVALID_PARAMETER);
1251    EINA_SAFETY_ON_FALSE_RETURN_VAL(inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_KEYBOARD, EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1252
1253    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1254
1255    tizen_input_device_manager_generate_key(_eflutil.wl.devmgr.devicemgr, key_name, pressed);
1256
1257    while (_eflutil.wl.devmgr.request_notified == -1)
1258      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1259
1260    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1261    _eflutil.wl.devmgr.request_notified = -1;
1262
1263    return ret;
1264 }
1265
1266 API int
1267 efl_util_input_generate_touch(efl_util_inputgen_h inputgen_h, int idx,
1268                               efl_util_input_touch_type_e touch_type, int x, int y)
1269 {
1270    int ret;
1271    enum tizen_input_device_manager_pointer_event_type type;
1272
1273    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1274    EINA_SAFETY_ON_FALSE_RETURN_VAL(idx >= 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
1275    EINA_SAFETY_ON_FALSE_RETURN_VAL((x > 0 && y > 0), EFL_UTIL_ERROR_INVALID_PARAMETER);
1276    EINA_SAFETY_ON_FALSE_RETURN_VAL(inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN, EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1277
1278    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1279
1280    switch(touch_type)
1281      {
1282         case EFL_UTIL_INPUT_TOUCH_BEGIN:
1283            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN;
1284            break;
1285         case EFL_UTIL_INPUT_TOUCH_UPDATE:
1286            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE;
1287            break;
1288         case EFL_UTIL_INPUT_TOUCH_END:
1289            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END;
1290            break;
1291         default:
1292            return EFL_UTIL_ERROR_INVALID_PARAMETER;
1293      }
1294
1295    tizen_input_device_manager_generate_touch(_eflutil.wl.devmgr.devicemgr, type, x, y, idx);
1296
1297    while (_eflutil.wl.devmgr.request_notified == -1)
1298      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1299
1300    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1301    _eflutil.wl.devmgr.request_notified = -1;
1302
1303    return ret;
1304 }
1305
1306 struct _efl_util_screenshot_h
1307 {
1308    int width;
1309    int height;
1310
1311    Eina_Bool shot_done;
1312
1313    /* tbm bufmgr */
1314    tbm_bufmgr bufmgr;
1315 };
1316
1317 /* scrrenshot handle */
1318 static efl_util_screenshot_h g_screenshot;
1319
1320 API efl_util_screenshot_h
1321 efl_util_screenshot_initialize(int width, int height)
1322 {
1323    efl_util_screenshot_h screenshot = NULL;
1324
1325    if (!_eflutil.wl.shot.screenshooter)
1326      {
1327         int ret = 0;
1328         _wl_init();
1329         while (!_eflutil.wl.shot.screenshooter && ret != -1)
1330           ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1331         EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.screenshooter, fail_init);
1332
1333        _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
1334        EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_init);
1335      }
1336
1337    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1338    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1339
1340    if (g_screenshot)
1341      {
1342         if (g_screenshot->width != width || g_screenshot->height != height)
1343           {
1344              g_screenshot->width = width;
1345              g_screenshot->height = height;
1346           }
1347
1348         return g_screenshot;
1349      }
1350
1351    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1352    EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1353
1354    screenshot->width = width;
1355    screenshot->height = height;
1356
1357    screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
1358    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1359
1360    g_screenshot = screenshot;
1361    set_last_result(EFL_UTIL_ERROR_NONE);
1362
1363    screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
1364
1365    return g_screenshot;
1366
1367 fail_param:
1368    if (screenshot)
1369      efl_util_screenshot_deinitialize(screenshot);
1370    set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1371    return NULL;
1372 fail_memory:
1373 /* LCOV_EXCL_START */
1374    if (screenshot)
1375      efl_util_screenshot_deinitialize(screenshot);
1376    set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1377    return NULL;
1378 /* LCOV_EXCL_STOP */
1379 fail_init:
1380    if (screenshot)
1381      efl_util_screenshot_deinitialize(screenshot);
1382    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1383    return NULL;
1384 }
1385
1386 API int
1387 efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
1388 {
1389    if (!screenshot)
1390      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1391
1392    free(screenshot);
1393    g_screenshot = NULL;
1394
1395    if (_eflutil.wl.shot.screenshooter)
1396      screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, NULL);
1397
1398    return EFL_UTIL_ERROR_NONE;
1399 }
1400
1401
1402 API tbm_surface_h
1403 efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
1404 {
1405    tbm_surface_h t_surface = NULL;
1406    struct wl_buffer *buffer = NULL;
1407    Efl_Util_Wl_Output_Info *output;
1408    int ret = 0;
1409
1410    if (screenshot != g_screenshot)
1411      {
1412         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1413         return NULL;
1414      }
1415
1416    output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
1417    if (!output)
1418      {
1419         fprintf(stderr, "[screenshot] fail: no output for screenshot\n"); /* LCOV_EXCL_LINE */
1420         goto fail;
1421      }
1422
1423    t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
1424    if (!t_surface)
1425      {
1426         fprintf(stderr, "[screenshot] fail: tbm_surface_create\n"); /* LCOV_EXCL_LINE */
1427         goto fail;
1428      }
1429
1430    buffer = wayland_tbm_client_create_buffer(_eflutil.wl.shot.tbm_client, t_surface);
1431    if (!buffer)
1432      {
1433         fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n"); /* LCOV_EXCL_LINE */
1434         goto fail;
1435      }
1436
1437    screenshooter_shoot(_eflutil.wl.shot.screenshooter, output->output, buffer);
1438
1439    screenshot->shot_done = EINA_FALSE;
1440    while (!screenshot->shot_done && ret != -1)
1441      ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1442
1443    if (ret == -1)
1444      {
1445         fprintf(stderr, "[screenshot] fail: screenshooter_shoot\n"); /* LCOV_EXCL_LINE */
1446         goto fail;
1447      }
1448
1449    wl_buffer_destroy(buffer);
1450
1451    /* reset shot_done for next screenshot */
1452    screenshot->shot_done = EINA_FALSE;
1453
1454    return t_surface;
1455
1456 fail:
1457    if (t_surface)
1458      tbm_surface_destroy(t_surface);
1459    if (buffer)
1460      wl_buffer_destroy(buffer);
1461
1462    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1463
1464    return NULL;
1465 }