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