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