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