screenshot: refactoring screenshot init
[platform/core/api/efl-util.git] / src / efl_util.c
1 /*
2  * Copyright (c) 2011-2017 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 #include <pthread.h>
34
35 #include <Ecore_Wl2.h>
36 #include <wayland-client.h>
37 #include <wayland-tbm-client.h>
38 #include <tizen-extension-client-protocol.h>
39
40 #include <efl_util_screenshot_extension.h>
41
42 #include <dlog.h>
43 #ifdef LOG_TAG
44 #undef LOG_TAG
45 #endif
46
47 #define LOG_TAG "TIZEN_N_EFL_UTIL"
48 #define EFL_UTIL_INPUT_GENERATOR_DEFAULT_TIME_OUT 1000
49 #define EFL_UTIL_INPUT_GENERATOR_DEFAULT_DISPATCH_TIME_OUT 10
50 #define EFL_UTIL_INPUT_GENERATOR_TOUCH_MAX_FINGER 10
51
52 typedef struct _Efl_Util_Wl_Surface_Lv_Info
53 {
54    void *surface; /* wl_surface */
55    int level;
56    Eina_Bool wait_for_done;
57    uint32_t state;
58 } Efl_Util_Wl_Surface_Lv_Info;
59
60 typedef struct _Efl_Util_Wl_Surface_Scr_Mode_Info
61 {
62    void *surface; /* wl_surface */
63    unsigned int mode;
64    Eina_Bool wait_for_done;
65    uint32_t state;
66 } Efl_Util_Wl_Surface_Scr_Mode_Info;
67
68 typedef struct _Efl_Util_Wl_Surface_Brightness_Info
69 {
70    void *surface; /* wl_surface */
71    int brightness;
72    Eina_Bool wait_for_done;
73    uint32_t state;
74 } Efl_Util_Wl_Surface_Brightness_Info;
75
76 typedef struct _Efl_Util_Wl_Output_Info
77 {
78     struct wl_output *output;
79     int offset_x, offset_y, width, height;
80 } Efl_Util_Wl_Output_Info;
81
82 typedef struct _Efl_Util_Gesture_Common_Grab_Data
83 {
84    int type;
85 } Efl_Util_Gesture_Common_Grab_Data;
86
87 typedef struct _Efl_Util_Gesture_Edge_Swipe_Grab_Data
88 {
89    Efl_Util_Gesture_Common_Grab_Data base;
90
91    unsigned int fingers;
92    efl_util_gesture_edge_e edge;
93    efl_util_gesture_edge_size_e edge_size;
94    unsigned int start_point;
95    unsigned int end_point;
96 } Efl_Util_Gesture_Edge_Swipe_Grab_Data;
97
98 typedef struct _Efl_Util_Gesture_Edge_Drag_Grab_Data
99 {
100    Efl_Util_Gesture_Common_Grab_Data base;
101
102    unsigned int fingers;
103    efl_util_gesture_edge_e edge;
104    efl_util_gesture_edge_size_e edge_size;
105    unsigned int start_point;
106    unsigned int end_point;
107 } Efl_Util_Gesture_Edge_Drag_Grab_Data;
108
109 typedef struct _Efl_Util_Gesture_Tap_Grab_Data
110 {
111    Efl_Util_Gesture_Common_Grab_Data base;
112
113    unsigned int fingers;
114    unsigned int repeats;
115 } Efl_Util_Gesture_Tap_Grab_Data;
116
117 typedef struct _Efl_Util_Gesture_Palm_Cover_Grab_Data
118 {
119    Efl_Util_Gesture_Common_Grab_Data base;
120 } Efl_Util_Gesture_Palm_Cover_Grab_Data;
121
122 typedef struct _Efl_Util_Device_Info
123 {
124    struct tizen_input_device *device;
125    Ecore_Device_Class clas;
126    Eina_Stringshare *name;
127 } Efl_Util_Device_Info;
128
129 typedef struct _E_Devicemgr_Inputgen_Touch_Axis
130 {
131    double radius_x;
132    double radius_y;
133    double pressure;
134    double angle;
135    double palm;
136 } E_Devicemgr_Inputgen_Touch_Axis;
137
138 typedef struct _Efl_Util_Data
139 {
140    /* wayland related stuffs */
141    struct
142    {
143       Eina_Bool init;
144       Ecore_Wl2_Display *wl2_display;
145       struct wl_display *dpy;
146       struct wl_event_queue *queue;
147
148       struct
149       {
150          unsigned int id;
151          struct tizen_policy *proto;
152          Eina_Hash *hash_noti_lv;
153          Eina_Hash *hash_scr_mode;
154       } policy;
155       struct
156       {
157          struct wl_event_queue *queue;
158          struct tizen_screenshooter *tz_screenshooter;
159          struct wayland_tbm_client *tbm_client;
160          Eina_List *output_list;
161          uint32_t noti;
162       } shot;
163       struct
164       {
165          struct tizen_input_device_manager *devicemgr;
166          int request_notified;
167          Eina_List *devices;
168          Eina_List *wait_devices;
169          struct wl_event_source *wait_timer;
170          int max_touch_count;
171          int request_touch_count;
172       } devmgr;
173       struct wl_seat *seat;
174       struct
175       {
176          unsigned int id;
177          struct tizen_display_policy *proto;
178          Eina_Hash *hash_brightness;
179       } display_policy;
180    } wl;
181 } Efl_Util_Data;
182
183 typedef struct _Efl_Util_Data_Default_Queue
184 {
185    /* wayland related stuffs */
186    struct
187    {
188       Eina_Bool init;
189       Ecore_Wl2_Display *wl2_display;
190       struct wl_display *dpy;
191
192       struct
193       {
194          unsigned int id;
195          struct tizen_gesture *proto;
196          int request_notified;
197          int event_init;
198       } gesture;
199    } wl;
200 } Efl_Util_Data_Default_Queue;
201
202
203 static Efl_Util_Data _eflutil =
204 {
205    {
206       EINA_FALSE,
207       NULL, NULL, NULL,
208       { 0, NULL, NULL, NULL }, /* tizen_policy protocol */
209       { NULL, NULL, NULL, NULL, 0 }, /* screenshooter protocol */
210       { NULL, -1, NULL, NULL, NULL, 0, 0 }, /* tizen_input_device_manager protocol */
211       NULL, /* wl_seat protocol */
212       { 0, NULL, NULL } /* display_policy protocol */
213    },
214 };
215
216 static Efl_Util_Data_Default_Queue _eflutil_defaultqueue =
217 {
218    {
219       EINA_FALSE,
220       NULL, NULL,
221       { 0, NULL, -1, 0 } /* gesture protocol */
222    },
223 };
224
225
226 static Eina_Bool               _wl_init(void);
227 static Eina_Bool               _wl_init_default_queue(void);
228 static void                    _cb_wl_reg_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
229 static void                    _cb_wl_reg_global_remove(void *data, struct wl_registry *reg, unsigned int name);
230 static void                    _cb_wl_default_queue_reg_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
231 static void                    _cb_wl_default_queue_reg_global_remove(void *data, struct wl_registry *reg, unsigned int name);
232 static void                    _cb_wl_reg_screenshooter_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
233 static void                    _cb_wl_reg_screenshooter_global_remove(void *data, struct wl_registry *reg, unsigned int name);
234 static void                    _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t is_conformant);
235 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);
236 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);
237 static void                    _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id);
238 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);
239 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);
240 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);
241 static void                    _cb_wl_tz_policy_allowed_aux_hint(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, int id);
242 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);
243 static void                    _cb_wl_conformant_region(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, uint32_t serial);
244
245 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);
246
247 static void                    _cb_device_info(void *data, struct tizen_input_device *tizen_input_device EINA_UNUSED, const char *name, uint32_t clas, uint32_t subclas EINA_UNUSED, struct wl_array *axes EINA_UNUSED);
248 static void                    _cb_event_device(void *data EINA_UNUSED, struct tizen_input_device *tizen_input_device EINA_UNUSED, unsigned int serial EINA_UNUSED, const char *name EINA_UNUSED, uint32_t time EINA_UNUSED);
249 static void                    _cb_axis(void *data EINA_UNUSED, struct tizen_input_device *tizen_input_device EINA_UNUSED, uint32_t axis_type EINA_UNUSED, wl_fixed_t value EINA_UNUSED);
250
251 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);
252 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);
253 static void                    _cb_error(void *data EINA_UNUSED, struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED, uint32_t errorcode);
254 static void                    _cb_block_expired(void *data EINA_UNUSED, struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED);
255 static void                    _cb_max_touch_count(void *data EINA_UNUSED, struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED, uint32_t serial EINA_UNUSED, int32_t max_count EINA_UNUSED, struct wl_seat *seat EINA_UNUSED);
256
257 static void                    _cb_gesture_edge_swipe_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t fingers EINA_UNUSED, uint32_t edge EINA_UNUSED, uint32_t edge_size EINA_UNUSED, uint32_t start_point EINA_UNUSED, uint32_t end_point EINA_UNUSED, uint32_t error);
258 static void                    _cb_gesture_edge_swipe(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t mode, uint32_t fingers, int sx, int sy, uint32_t edge);
259 static void                    _cb_gesture_edge_drag_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t fingers EINA_UNUSED, uint32_t edge EINA_UNUSED, uint32_t edge_size EINA_UNUSED, uint32_t start_point EINA_UNUSED, uint32_t end_point EINA_UNUSED, uint32_t error);
260 static void                    _cb_gesture_edge_drag(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t mode, uint32_t fingers, int cx, int cy, uint32_t edge);
261 static void                    _cb_gesture_tap_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t fingers EINA_UNUSED, uint32_t repeats EINA_UNUSED, uint32_t error);
262 static void                    _cb_gesture_tap(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t mode, uint32_t fingers, uint32_t repeats);
263 static void                    _cb_gesture_palm_cover_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, struct wl_surface *surface EINA_UNUSED, uint32_t error);
264 static void                    _cb_gesture_palm_cover(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, struct wl_surface *surface EINA_UNUSED, uint32_t mode, uint32_t duration, int cx, int cy, uint32_t size, wl_fixed_t pressure);
265 static void                    _cb_gesture_activate_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, struct wl_surface *surface EINA_UNUSED, uint32_t type EINA_UNUSED, uint32_t active EINA_UNUSED, uint32_t error);
266
267
268 static const struct wl_registry_listener _wl_reg_listener =
269 {
270    _cb_wl_reg_global,
271    _cb_wl_reg_global_remove
272 };
273
274 static const struct wl_registry_listener _wl_reg_screenshooter_listener =
275 {
276    _cb_wl_reg_screenshooter_global,
277    _cb_wl_reg_screenshooter_global_remove
278 };
279
280 struct tizen_policy_listener _wl_tz_policy_listener =
281 {
282    _cb_wl_tz_policy_conformant,
283    _cb_wl_tz_policy_conformant_area,
284    _cb_wl_tz_policy_notification_done,
285    _cb_wl_tz_policy_transient_for_done,
286    _cb_wl_tz_policy_scr_mode_done,
287    _cb_wl_tz_policy_iconify_state_changed,
288    _cb_wl_tz_policy_supported_aux_hints,
289    _cb_wl_tz_policy_allowed_aux_hint,
290    _cb_wl_tz_policy_aux_message,
291    _cb_wl_conformant_region,
292 };
293
294 static const struct tizen_input_device_listener _wl_tz_input_device_listener =
295 {
296    _cb_device_info,
297    _cb_event_device,
298    _cb_axis,
299 };
300
301 struct tizen_input_device_manager_listener _wl_tz_devmgr_listener =
302 {
303    _cb_device_add,
304    _cb_device_remove,
305    _cb_error,
306    _cb_block_expired,
307    _cb_max_touch_count
308 };
309
310 struct tizen_display_policy_listener _wl_tz_display_policy_listener =
311 {
312    _cb_wl_tz_display_policy_brightness_done,
313 };
314
315 static const struct wl_registry_listener _wl_default_queue_reg_listener =
316 {
317    _cb_wl_default_queue_reg_global,
318    _cb_wl_default_queue_reg_global_remove
319 };
320
321 struct tizen_gesture_listener _wl_tz_gesture_listener =
322 {
323    _cb_gesture_edge_swipe_notify,
324    _cb_gesture_edge_swipe,
325    _cb_gesture_edge_drag_notify,
326    _cb_gesture_edge_drag,
327    _cb_gesture_tap_notify,
328    _cb_gesture_tap,
329    _cb_gesture_palm_cover_notify,
330    _cb_gesture_palm_cover,
331    _cb_gesture_activate_notify
332 };
333
334 static Eina_Bool
335 _wl_init(void)
336 {
337    struct wl_display *display_wrapper = NULL;
338    struct wl_registry *reg = NULL;
339
340    if (_eflutil.wl.init) return EINA_TRUE;
341
342    ecore_wl2_init();
343
344    _eflutil.wl.wl2_display = ecore_wl2_display_connect(NULL);
345    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.wl2_display, fail);
346    _eflutil.wl.dpy = ecore_wl2_display_get(_eflutil.wl.wl2_display);
347    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.dpy, fail);
348
349    display_wrapper = wl_proxy_create_wrapper(_eflutil.wl.dpy);
350    EINA_SAFETY_ON_NULL_GOTO(display_wrapper, fail);
351
352    _eflutil.wl.queue = wl_display_create_queue(_eflutil.wl.dpy);
353    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.queue, fail);
354
355    wl_proxy_set_queue((struct wl_proxy *)display_wrapper, _eflutil.wl.queue);
356
357    reg = wl_display_get_registry(display_wrapper);
358    wl_proxy_wrapper_destroy(display_wrapper);
359    display_wrapper = NULL;
360    EINA_SAFETY_ON_NULL_GOTO(reg, fail);
361
362    wl_registry_add_listener(reg, &_wl_reg_listener, NULL);
363
364    _eflutil.wl.init = EINA_TRUE;
365
366    return EINA_TRUE;
367 fail:
368    if (display_wrapper)
369      wl_proxy_wrapper_destroy(display_wrapper);
370
371    if (_eflutil.wl.queue)
372      {
373         wl_event_queue_destroy(_eflutil.wl.queue);
374         _eflutil.wl.queue = NULL;
375      }
376
377    ecore_wl2_shutdown();
378    return EINA_FALSE;
379 }
380
381 static Eina_Bool
382 _wl_init_default_queue(void)
383 {
384    struct wl_display *display_wrapper = NULL;
385    struct wl_registry *reg = NULL;
386
387    if (_eflutil_defaultqueue.wl.init) return EINA_TRUE;
388
389    if (ecore_wl2_init() <= 0) return EINA_FALSE;
390
391    _eflutil_defaultqueue.wl.wl2_display = ecore_wl2_connected_display_get(NULL);
392    EINA_SAFETY_ON_NULL_GOTO(_eflutil_defaultqueue.wl.wl2_display, fail);
393    _eflutil_defaultqueue.wl.dpy = ecore_wl2_display_get(_eflutil_defaultqueue.wl.wl2_display);
394    EINA_SAFETY_ON_NULL_GOTO(_eflutil_defaultqueue.wl.dpy, fail);
395
396    display_wrapper = wl_proxy_create_wrapper(_eflutil_defaultqueue.wl.dpy);
397    EINA_SAFETY_ON_NULL_GOTO(display_wrapper, fail);
398
399    reg = wl_display_get_registry(display_wrapper);
400    wl_proxy_wrapper_destroy(display_wrapper);
401    display_wrapper = NULL;
402    EINA_SAFETY_ON_NULL_GOTO(reg, fail);
403
404    wl_registry_add_listener(reg, &_wl_default_queue_reg_listener, NULL);
405
406    _eflutil_defaultqueue.wl.init = EINA_TRUE;
407
408    return EINA_TRUE;
409 fail:
410
411    ecore_wl2_shutdown();
412    return EINA_FALSE;
413 }
414
415 static void
416 _cb_wl_output_geometry(void *data, struct wl_output *wl_output, int x, int y,
417                        int physical_width, int physical_height, int subpixel,
418                        const char *make, const char *model, int transform)
419 {
420    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
421    if (wl_output == output->output)
422      {
423         output->offset_x = x;
424         output->offset_y = y;
425      }
426 }
427
428 static void
429 _cb_wl_output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
430                    int width, int height, int refresh)
431 {
432    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
433    if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT))
434      {
435         output->width = width;
436         output->height = height;
437      }
438 }
439
440 static void
441 _cb_wl_output_done(void *data, struct wl_output *wl_output)
442 {
443 }
444
445 static void
446 _cb_wl_output_scale(void *data, struct wl_output *wl_output, int32_t factor)
447 {
448 }
449
450 static const struct wl_output_listener output_listener =
451 {
452     _cb_wl_output_geometry,
453     _cb_wl_output_mode,
454     _cb_wl_output_done,
455     _cb_wl_output_scale
456 };
457
458 static void
459 _cb_tz_screenshot_format(void *data, struct tizen_screenshooter *tz_screenshooter, uint32_t format)
460 {
461 }
462
463 static void
464 _cb_tz_screenshot_noti(void *data, struct tizen_screenshooter *tz_screenshooter, uint32_t noti)
465 {
466    _eflutil.wl.shot.noti = noti;
467 }
468
469 static void
470 _cb_tz_screenshot_done(void *data, struct tizen_screenshooter *tz_screenshooter)
471 {
472    Eina_Bool *shot_done = (Eina_Bool*)data;
473    if (shot_done)
474      *shot_done = EINA_TRUE;
475 }
476
477 static const struct tizen_screenshooter_listener tz_screenshooter_listener =
478 {
479     _cb_tz_screenshot_format,
480     _cb_tz_screenshot_noti,
481     _cb_tz_screenshot_done,
482 };
483
484 static void
485 _cb_seat_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps)
486 {
487 }
488
489 static void
490 _cb_seat_name(void *data, struct wl_seat *seat EINA_UNUSED, const char *name)
491 {
492 }
493
494 static const struct wl_seat_listener _seat_listener =
495 {
496    _cb_seat_capabilities,
497    _cb_seat_name,
498 };
499
500 static void
501 _cb_wl_reg_global(void *data,
502                   struct wl_registry *reg,
503                   unsigned int id,
504                   const char *interface,
505                   unsigned int version)
506 {
507    if (!strcmp(interface, "tizen_policy"))
508      {
509         struct tizen_policy *proto;
510         proto = wl_registry_bind(reg,
511                                   id,
512                                   &tizen_policy_interface,
513                                   7);
514         if (!proto) return;
515
516         tizen_policy_add_listener(proto,
517                                   &_wl_tz_policy_listener,
518                                   NULL);
519
520         _eflutil.wl.policy.hash_noti_lv = eina_hash_pointer_new(free);
521         _eflutil.wl.policy.hash_scr_mode = eina_hash_pointer_new(free);
522         _eflutil.wl.policy.proto = proto;
523         _eflutil.wl.policy.id = id;
524      }
525    else if (strcmp(interface, "wl_output") == 0)
526      {
527         Efl_Util_Wl_Output_Info *output = calloc(1, sizeof(Efl_Util_Wl_Output_Info));
528         EINA_SAFETY_ON_NULL_RETURN(output);
529
530         _eflutil.wl.shot.output_list = eina_list_append(_eflutil.wl.shot.output_list, output);
531
532         output->output = wl_registry_bind(reg, id, &wl_output_interface, version);
533         wl_output_add_listener(output->output, &output_listener, output);
534      }
535    else if(strcmp(interface, "wl_seat") == 0)
536      {
537         _eflutil.wl.seat = wl_registry_bind(reg, id, &wl_seat_interface, version);
538         wl_seat_add_listener(_eflutil.wl.seat, &_seat_listener, NULL);
539      }
540    else if (strcmp(interface, "tizen_input_device_manager") == 0)
541      {
542         _eflutil.wl.devmgr.devicemgr = wl_registry_bind(reg, id, &tizen_input_device_manager_interface, version);
543         tizen_input_device_manager_add_listener(_eflutil.wl.devmgr.devicemgr, &_wl_tz_devmgr_listener, NULL);
544      }
545    else if (!strcmp(interface, "tizen_display_policy"))
546      {
547         _eflutil.wl.display_policy.proto = wl_registry_bind(reg, id, &tizen_display_policy_interface, version);
548         if (!_eflutil.wl.display_policy.proto) return;
549
550         tizen_display_policy_add_listener(_eflutil.wl.display_policy.proto,
551                                           &_wl_tz_display_policy_listener,
552                                           NULL);
553
554         _eflutil.wl.display_policy.hash_brightness = eina_hash_pointer_new(free);
555         _eflutil.wl.display_policy.id = id;
556      }
557 }
558 /* LCOV_EXCL_START */
559 static void
560 _cb_wl_reg_global_remove(void *data,
561                          struct wl_registry *reg,
562                          unsigned int id)
563 {
564    /* unset each global id number to 0 since global id is started
565     * from number 1 on server side display structure
566     */
567    if (id == _eflutil.wl.policy.id)
568      {
569         _eflutil.wl.policy.id = 0;
570         _eflutil.wl.policy.proto = NULL;
571         eina_hash_free(_eflutil.wl.policy.hash_noti_lv);
572         eina_hash_free(_eflutil.wl.policy.hash_scr_mode);
573      }
574    else if (id == _eflutil.wl.display_policy.id)
575      {
576         _eflutil.wl.display_policy.id = 0;
577         _eflutil.wl.display_policy.proto = NULL;
578         eina_hash_free(_eflutil.wl.display_policy.hash_brightness);
579      }
580 }
581 /* LCOV_EXCL_STOP */
582
583 static void
584 _cb_wl_default_queue_reg_global(void *data,
585                                 struct wl_registry *reg,
586                                 unsigned int id,
587                                 const char *interface,
588                                 unsigned int version)
589 {
590    if (strcmp(interface, "tizen_gesture") == 0)
591      {
592         _eflutil_defaultqueue.wl.gesture.id = id;
593         _eflutil_defaultqueue.wl.gesture.proto = wl_registry_bind(reg, id, &tizen_gesture_interface, version);
594         tizen_gesture_add_listener(_eflutil_defaultqueue.wl.gesture.proto, &_wl_tz_gesture_listener, NULL);
595      }
596 }
597 /* LCOV_EXCL_START */
598 static void
599 _cb_wl_default_queue_reg_global_remove(void *data,
600                                        struct wl_registry *reg,
601                                        unsigned int id)
602 {
603    if (id == _eflutil_defaultqueue.wl.gesture.id)
604      {
605         _eflutil_defaultqueue.wl.gesture.id = 0;
606         _eflutil_defaultqueue.wl.gesture.proto = NULL;
607      }
608 }
609 /* LCOV_EXCL_STOP */
610
611
612 static void
613 _cb_wl_reg_screenshooter_global(void *data,
614                   struct wl_registry *reg,
615                   unsigned int name,
616                   const char *interface,
617                   unsigned int version)
618 {
619    if (strcmp(interface, "tizen_screenshooter") == 0)
620      {
621         _eflutil.wl.shot.tz_screenshooter = wl_registry_bind(reg, name, &tizen_screenshooter_interface, version);
622         tizen_screenshooter_add_listener(_eflutil.wl.shot.tz_screenshooter, &tz_screenshooter_listener, NULL);
623
624         wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.shot.queue);
625      }
626 }
627
628 /* LCOV_EXCL_START */
629 static void
630 _cb_wl_reg_screenshooter_global_remove(void *data,
631                          struct wl_registry *reg,
632                          unsigned int name)
633 {
634 }
635
636 static void
637 _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy,
638                             struct wl_surface *surface, uint32_t is_conformant)
639 {
640 }
641
642 static void
643 _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy,
644                                  struct wl_surface *surface, uint32_t conformant_part,
645                                  uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h)
646 {
647 }
648 /* LCOV_EXCL_STOP */
649
650 static void
651 _cb_wl_tz_policy_notification_done(void *data,
652                                    struct tizen_policy *tizen_policy,
653                                    struct wl_surface *surface,
654                                    int32_t level,
655                                    uint32_t state)
656 {
657    Efl_Util_Wl_Surface_Lv_Info *lv_info;
658
659    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
660    if (lv_info)
661      {
662         lv_info->level = level;
663         lv_info->wait_for_done = EINA_FALSE;
664         lv_info->state = state;
665      }
666 }
667
668 /* LCOV_EXCL_START */
669 static void
670 _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id)
671 {
672 }
673 /* LCOV_EXCL_STOP */
674
675 static void
676 _cb_wl_tz_policy_scr_mode_done(void *data,
677                                struct tizen_policy *tizen_policy,
678                                struct wl_surface *surface,
679                                uint32_t mode,
680                                uint32_t state)
681 {
682    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
683
684    scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
685    if (scr_mode_info)
686      {
687         scr_mode_info->mode = mode;
688         scr_mode_info->wait_for_done = EINA_FALSE;
689         scr_mode_info->state = state;
690      }
691 }
692
693 /* LCOV_EXCL_START */
694 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)
695 {
696 }
697
698 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)
699 {
700 }
701
702 static void                    _cb_wl_tz_policy_allowed_aux_hint(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, int id)
703 {
704 }
705
706 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)
707 {
708 }
709 static void                    _cb_wl_conformant_region(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, uint32_t serial)
710 {
711 }
712 /* LCOV_EXCL_STOP */
713
714 static void
715 _cb_wl_tz_display_policy_brightness_done(void *data,
716                                  struct tizen_display_policy *tizen_display_policy,
717                                  struct wl_surface *surface,
718                                  int32_t brightness,
719                                  uint32_t state)
720 {
721    Efl_Util_Wl_Surface_Brightness_Info *brightness_info;
722
723    brightness_info = eina_hash_find(_eflutil.wl.display_policy.hash_brightness, &surface);
724    if (brightness_info)
725      {
726         brightness_info->brightness = brightness;
727         brightness_info->wait_for_done = EINA_FALSE;
728         brightness_info->state = state;
729      }
730 }
731
732 static void
733 _cb_window_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
734 {
735    Efl_Util_Wl_Surface_Lv_Info *lv_info;
736
737    lv_info = data;
738    if (EINA_UNLIKELY(!lv_info))
739      return;
740
741    eina_hash_del(_eflutil.wl.policy.hash_noti_lv, &lv_info->surface, lv_info);
742 }
743
744 API int
745 efl_util_set_notification_window_level(Evas_Object *window,
746                                        efl_util_notification_level_e level)
747 {
748    Eina_Bool res;
749
750    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
751    EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_NONE) &&
752                                    (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
753                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
754
755    Elm_Win_Type type;
756    Ecore_Wl2_Window *wlwin;
757    struct wl_surface *surface;
758    Efl_Util_Wl_Surface_Lv_Info *lv_info;
759    Ecore_Wl2_Window_Type wl_type;
760
761    res = _wl_init();
762    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
763
764    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
765    EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
766
767    type = elm_win_type_get(window);
768    if (type != ELM_WIN_NOTIFICATION)
769      {
770         wl_type = ecore_wl2_window_type_get(wlwin);
771         EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL2_WINDOW_TYPE_NOTIFICATION),
772                                         EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
773      }
774
775    while (!_eflutil.wl.policy.proto)
776      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
777
778    surface = ecore_wl2_window_surface_get(wlwin);
779    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
780                                   EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
781
782    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
783    if (!lv_info)
784      {
785         lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
786         EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
787
788         lv_info->surface = surface;
789         lv_info->level = (int)level;
790         lv_info->wait_for_done = EINA_TRUE;
791         lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
792         eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
793                       &surface,
794                       lv_info);
795
796         evas_object_event_callback_add(window, EVAS_CALLBACK_DEL,
797                                        _cb_window_del, lv_info);
798      }
799    else
800      {
801         lv_info->level = (int)level;
802         lv_info->wait_for_done = EINA_TRUE;
803         lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
804      }
805
806
807    tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
808                                        surface, (int)level);
809
810    if (lv_info->wait_for_done)
811      {
812         int count = 0;
813         while (lv_info->wait_for_done && (count < 3))
814           {
815              ecore_wl2_display_flush(_eflutil.wl.wl2_display);
816              wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
817              count++;
818           }
819
820         if (lv_info->wait_for_done)
821           {
822              return EFL_UTIL_ERROR_INVALID_PARAMETER;
823           }
824         else
825           {
826              if (lv_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
827                {
828                   return EFL_UTIL_ERROR_PERMISSION_DENIED;
829                }
830           }
831      }
832
833    return EFL_UTIL_ERROR_NONE;
834 }
835
836 API int
837 efl_util_get_notification_window_level(Evas_Object *window,
838                                        efl_util_notification_level_e *level)
839 {
840    Eina_Bool res;
841
842    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
843    EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
844
845    Elm_Win_Type type;
846    Ecore_Wl2_Window *wlwin;
847    struct wl_surface *surface;
848    Efl_Util_Wl_Surface_Lv_Info *lv_info;
849    Ecore_Wl2_Window_Type wl_type;
850
851    res = _wl_init();
852    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
853
854    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
855    EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
856
857    type = elm_win_type_get(window);
858    if (type != ELM_WIN_NOTIFICATION)
859      {
860         wl_type = ecore_wl2_window_type_get(wlwin);
861         EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL2_WINDOW_TYPE_NOTIFICATION),
862                                         EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
863      }
864
865    while (!_eflutil.wl.policy.proto)
866      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
867
868    surface = ecore_wl2_window_surface_get(wlwin);
869    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
870                                   EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
871
872    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
873    if (lv_info)
874      {
875         if (lv_info->wait_for_done)
876           {
877              int count = 0;
878              while ((lv_info->wait_for_done) && (count < 3))
879                {
880                   ecore_wl2_display_flush(_eflutil.wl.wl2_display);
881                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
882                   count++;
883                }
884
885              if (lv_info->wait_for_done)
886                {
887                   *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
888                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
889                }
890           }
891
892         switch (lv_info->level)
893           {
894            case TIZEN_POLICY_LEVEL_NONE:    *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE;    break;
895            case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
896            case TIZEN_POLICY_LEVEL_MEDIUM:  *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM;  break;
897            case TIZEN_POLICY_LEVEL_HIGH:    *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH;    break;
898            case TIZEN_POLICY_LEVEL_TOP:     *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP;     break;
899            default:                         *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
900             return EFL_UTIL_ERROR_INVALID_PARAMETER;
901           }
902         return EFL_UTIL_ERROR_NONE;
903      }
904    else
905      *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
906
907    return EFL_UTIL_ERROR_NONE;
908 }
909
910 API int
911 efl_util_set_window_opaque_state(Evas_Object *window,
912                                  int opaque)
913 {
914    Eina_Bool res;
915
916    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
917    EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
918                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
919
920    Ecore_Wl2_Window *wlwin;
921    struct wl_surface *surface;
922
923    if (!_eflutil.wl.policy.proto)
924      {
925         int ret = 0;
926
927         res = _wl_init();
928         EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
929
930         while (!_eflutil.wl.policy.proto && ret != -1)
931           ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
932
933         EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.policy.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
934      }
935
936    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
937    if (!wlwin)
938       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
939
940    surface  = ecore_wl2_window_surface_get(wlwin);
941    if (!surface)
942       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
943
944    tizen_policy_set_opaque_state(_eflutil.wl.policy.proto, surface, opaque);
945
946    return EFL_UTIL_ERROR_NONE;
947 }
948
949 API int
950 efl_util_set_window_screen_mode(Evas_Object *window,
951                                 efl_util_screen_mode_e mode)
952 {
953    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
954    EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
955                                     (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
956                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
957
958    Ecore_Wl2_Window *wlwin;
959    struct wl_surface *surface;
960    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
961    Eina_Bool res;
962
963    res = _wl_init();
964    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
965
966    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
967    if (wlwin)
968      {
969         while (!_eflutil.wl.policy.proto)
970           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
971
972         surface = ecore_wl2_window_surface_get(wlwin);
973         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
974                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
975
976         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
977         if (!scr_mode_info)
978           {
979              scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
980              EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
981
982              scr_mode_info->surface = surface;
983              scr_mode_info->mode = (unsigned int)mode;
984              scr_mode_info->wait_for_done = EINA_TRUE;
985              scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
986
987              eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
988                            &surface,
989                            scr_mode_info);
990           }
991         else
992           {
993              scr_mode_info->mode = (unsigned int)mode;
994              scr_mode_info->wait_for_done = EINA_TRUE;
995              scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
996           }
997
998         tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
999                                             surface, (unsigned int)mode);
1000         if (scr_mode_info->wait_for_done)
1001           {
1002              int count = 0;
1003              while (scr_mode_info->wait_for_done && (count < 3))
1004                {
1005                   ecore_wl2_display_flush(_eflutil.wl.wl2_display);
1006                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1007                   count++;
1008                }
1009
1010              if (scr_mode_info->wait_for_done)
1011                {
1012                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
1013                }
1014              else
1015                {
1016                   if (scr_mode_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
1017                     {
1018                        return EFL_UTIL_ERROR_PERMISSION_DENIED;
1019                     }
1020                }
1021           }
1022
1023         return EFL_UTIL_ERROR_NONE;
1024      }
1025    else
1026      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1027 }
1028
1029 API int
1030 efl_util_get_window_screen_mode(Evas_Object *window,
1031                                 efl_util_screen_mode_e *mode)
1032 {
1033    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1034    EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
1035
1036    Ecore_Wl2_Window *wlwin;
1037    struct wl_surface *surface;
1038    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1039    Eina_Bool res;
1040
1041    res = _wl_init();
1042    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1043
1044    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
1045    if (wlwin)
1046      {
1047         while (!_eflutil.wl.policy.proto)
1048           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1049
1050         surface = ecore_wl2_window_surface_get(wlwin);
1051         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1052                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
1053
1054         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1055         if (scr_mode_info)
1056           {
1057              if (scr_mode_info->wait_for_done)
1058                {
1059                   while (scr_mode_info->wait_for_done)
1060                     {
1061                        ecore_wl2_display_flush(_eflutil.wl.wl2_display);
1062                        wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1063                     }
1064                }
1065
1066              switch (scr_mode_info->mode)
1067                {
1068                 case TIZEN_POLICY_MODE_DEFAULT:   *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;   break;
1069                 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
1070                 default:                          *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1071                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
1072                }
1073              return EFL_UTIL_ERROR_NONE;
1074           }
1075         else
1076           {
1077              *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1078              return EFL_UTIL_ERROR_INVALID_PARAMETER;
1079           }
1080      }
1081    else
1082      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1083 }
1084
1085 API int
1086 efl_util_set_window_brightness(Evas_Object *window, int brightness)
1087 {
1088    Ecore_Wl2_Window *wlwin;
1089    struct wl_surface *surface;
1090    Efl_Util_Wl_Surface_Brightness_Info *brightness_info;
1091    Eina_Bool res;
1092
1093    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1094    EINA_SAFETY_ON_FALSE_RETURN_VAL(brightness <= 100, EFL_UTIL_ERROR_INVALID_PARAMETER);
1095
1096    res = _wl_init();
1097    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1098
1099    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
1100    if (wlwin)
1101      {
1102         while (!_eflutil.wl.display_policy.proto)
1103           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1104
1105         surface = ecore_wl2_window_surface_get(wlwin);
1106         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1107                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
1108
1109         brightness_info = eina_hash_find(_eflutil.wl.display_policy.hash_brightness, &surface);
1110         if (!brightness_info)
1111           {
1112              brightness_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Brightness_Info));
1113              EINA_SAFETY_ON_NULL_RETURN_VAL(brightness_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
1114
1115              brightness_info->surface = surface;
1116              brightness_info->brightness = brightness;
1117              brightness_info->wait_for_done = EINA_TRUE;
1118              brightness_info->state = TIZEN_DISPLAY_POLICY_ERROR_STATE_NONE;
1119
1120              eina_hash_add(_eflutil.wl.display_policy.hash_brightness,
1121                            &surface,
1122                            brightness_info);
1123            }
1124          else
1125            {
1126               brightness_info->brightness = brightness;
1127               brightness_info->wait_for_done = EINA_TRUE;
1128               brightness_info->state = TIZEN_DISPLAY_POLICY_ERROR_STATE_NONE;
1129            }
1130
1131          tizen_display_policy_set_window_brightness(_eflutil.wl.display_policy.proto,
1132                                                     surface, brightness);
1133          if (brightness_info->wait_for_done)
1134            {
1135               int count = 0;
1136               while (brightness_info->wait_for_done && (count < 3))
1137                 {
1138                    ecore_wl2_display_flush(_eflutil.wl.wl2_display);
1139                    wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1140                    count++;
1141                 }
1142
1143               if (brightness_info->wait_for_done)
1144                 {
1145                    return EFL_UTIL_ERROR_INVALID_PARAMETER;
1146                 }
1147               else
1148                 {
1149                    if (brightness_info->state == TIZEN_DISPLAY_POLICY_ERROR_STATE_PERMISSION_DENIED)
1150                      {
1151                         return EFL_UTIL_ERROR_PERMISSION_DENIED;
1152                      }
1153                 }
1154            }
1155         return EFL_UTIL_ERROR_NONE;
1156      }
1157    else
1158      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1159 }
1160
1161 API int
1162 efl_util_get_window_brightness(Evas_Object *window, int *brightness)
1163 {
1164    Ecore_Wl2_Window *wlwin;
1165    struct wl_surface *surface;
1166    Efl_Util_Wl_Surface_Brightness_Info *brightness_info;
1167    Eina_Bool res;
1168
1169    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1170    EINA_SAFETY_ON_NULL_RETURN_VAL(brightness, EFL_UTIL_ERROR_INVALID_PARAMETER);
1171
1172    res = _wl_init();
1173    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1174
1175    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
1176    if (!wlwin) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1177
1178    while (!_eflutil.wl.display_policy.proto)
1179      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1180
1181    surface = ecore_wl2_window_surface_get(wlwin);
1182    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1183                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
1184
1185    brightness_info = eina_hash_find(_eflutil.wl.display_policy.hash_brightness, &surface);
1186    if (brightness_info)
1187      {
1188         if (brightness_info->wait_for_done)
1189           {
1190              while (brightness_info->wait_for_done)
1191                {
1192                   ecore_wl2_display_flush(_eflutil.wl.wl2_display);
1193                   wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1194                }
1195           }
1196          *brightness = brightness_info->brightness;
1197      }
1198    else
1199      *brightness = -1;
1200
1201    return EFL_UTIL_ERROR_NONE;
1202 }
1203
1204
1205 struct _efl_util_inputgen_h
1206 {
1207    unsigned int init_type;
1208    char name[32];
1209    E_Devicemgr_Inputgen_Touch_Axis *axis_info;
1210 };
1211
1212 static Eina_Bool
1213 _efl_util_input_check_wait_device_full(void)
1214 {
1215    Eina_List *l, *ll, *l_next;
1216    Efl_Util_Device_Info *dev, *wait_dev;
1217    int wait_cnt = 0;
1218
1219    wait_cnt = eina_list_count(_eflutil.wl.devmgr.wait_devices);
1220    if (wait_cnt <= 0) return EINA_TRUE;
1221
1222    EINA_LIST_FOREACH(_eflutil.wl.devmgr.devices, l, dev)
1223      {
1224         EINA_LIST_FOREACH_SAFE(_eflutil.wl.devmgr.wait_devices, ll, l_next, wait_dev)
1225           {
1226              if ((dev->clas == wait_dev->clas) &&
1227                  (!strncmp(dev->name, wait_dev->name, eina_stringshare_strlen(wait_dev->name))))
1228                {
1229                   eina_stringshare_del(wait_dev->name);
1230                   _eflutil.wl.devmgr.wait_devices = eina_list_remove_list(_eflutil.wl.devmgr.wait_devices, ll);
1231                   free(wait_dev);
1232
1233                   wait_cnt--;
1234                   if (wait_cnt <= 0) return EINA_TRUE;
1235
1236                   break;
1237                }
1238           }
1239      }
1240
1241    return EINA_FALSE;
1242 }
1243
1244 static Eina_Bool
1245 _efl_util_input_check_wait_device(const char *name, unsigned int type)
1246 {
1247    Eina_List *l, *l_next;
1248    Efl_Util_Device_Info *wait_dev;
1249    int wait_cnt = 0;
1250
1251    wait_cnt = eina_list_count(_eflutil.wl.devmgr.wait_devices);
1252    if (wait_cnt <= 0) return EINA_TRUE;
1253
1254    EINA_LIST_FOREACH_SAFE(_eflutil.wl.devmgr.wait_devices, l, l_next, wait_dev)
1255      {
1256         if ((type == wait_dev->clas) &&
1257             (!strncmp(name, wait_dev->name, eina_stringshare_strlen(wait_dev->name))))
1258           {
1259              eina_stringshare_del(wait_dev->name);
1260              _eflutil.wl.devmgr.wait_devices = eina_list_remove_list(_eflutil.wl.devmgr.wait_devices, l);
1261              free(wait_dev);
1262
1263              wait_cnt--;
1264              if (wait_cnt <= 0) return EINA_TRUE;
1265
1266              break;
1267           }
1268      }
1269
1270    return EINA_FALSE;
1271 }
1272
1273 static void
1274 _cb_device_info(void *data, struct tizen_input_device *tizen_input_device EINA_UNUSED, const char *name, uint32_t clas, uint32_t subclas EINA_UNUSED, struct wl_array *axes EINA_UNUSED)
1275 {
1276    Efl_Util_Device_Info *dev;
1277
1278    if (!(dev = data)) return;
1279    dev->clas = (Ecore_Device_Class)clas;
1280    dev->name = eina_stringshare_add(name);
1281
1282    if (_eflutil.wl.devmgr.wait_timer &&
1283        _efl_util_input_check_wait_device(name, clas))
1284      {
1285         wl_event_source_remove(_eflutil.wl.devmgr.wait_timer);
1286         _eflutil.wl.devmgr.wait_timer = NULL;
1287      }
1288 }
1289
1290 /* LCOV_EXCL_START */
1291 static void
1292 _cb_event_device(void *data EINA_UNUSED, struct tizen_input_device *tizen_input_device EINA_UNUSED, unsigned int serial EINA_UNUSED, const char *name EINA_UNUSED, uint32_t time EINA_UNUSED)
1293 {
1294    ;
1295 }
1296
1297 static void
1298 _cb_axis(void *data EINA_UNUSED, struct tizen_input_device *tizen_input_device EINA_UNUSED, uint32_t axis_type EINA_UNUSED, wl_fixed_t value EINA_UNUSED)
1299 {
1300    ;
1301 }
1302 /* LCOV_EXCL_STOP */
1303
1304 static void
1305 _cb_device_add(void *data EINA_UNUSED,
1306                struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED,
1307                uint32_t serial EINA_UNUSED,
1308                const char *identifier EINA_UNUSED,
1309                struct tizen_input_device *device,
1310                struct wl_seat *seat EINA_UNUSED)
1311 {
1312    Efl_Util_Device_Info *dev;
1313
1314    EINA_SAFETY_ON_NULL_RETURN(device);
1315
1316    dev = (Efl_Util_Device_Info *)calloc(1, sizeof(Efl_Util_Device_Info));
1317    EINA_SAFETY_ON_NULL_RETURN(dev);
1318
1319    dev->device = device;
1320    tizen_input_device_add_listener(device, &_wl_tz_input_device_listener, dev);
1321
1322    _eflutil.wl.devmgr.devices = eina_list_append(_eflutil.wl.devmgr.devices, dev);
1323 }
1324
1325 static void
1326 _cb_device_remove(void *data EINA_UNUSED,
1327                struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED,
1328                uint32_t serial  EINA_UNUSED,
1329                const char *identifier  EINA_UNUSED,
1330                struct tizen_input_device *device,
1331                struct wl_seat *seat  EINA_UNUSED)
1332 {
1333    Eina_List *l, *l_next;
1334    Efl_Util_Device_Info *dev;
1335
1336    EINA_LIST_FOREACH_SAFE(_eflutil.wl.devmgr.devices, l, l_next, dev)
1337      {
1338         if (dev->device == device)
1339           {
1340              if (dev->name) eina_stringshare_del(dev->name);
1341              tizen_input_device_release(dev->device);
1342
1343              _eflutil.wl.devmgr.devices = eina_list_remove_list(_eflutil.wl.devmgr.devices, l);
1344              free(dev);
1345
1346              break;
1347           }
1348      }
1349 }
1350
1351 static void
1352 _cb_error(void *data EINA_UNUSED,
1353           struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED,
1354           uint32_t errorcode)
1355 {
1356    _eflutil.wl.devmgr.request_notified = errorcode;
1357 }
1358
1359 /* LCOV_EXCL_START */
1360 static void
1361 _cb_block_expired(void *data EINA_UNUSED,
1362                   struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED)
1363 {
1364    ;
1365 }
1366 /* LCOV_EXCL_STOP */
1367
1368 static void
1369 _cb_max_touch_count(void *data EINA_UNUSED,
1370                     struct tizen_input_device_manager *tizen_input_device_manager EINA_UNUSED,
1371                     uint32_t serial EINA_UNUSED,
1372                     int32_t max_count,
1373                     struct wl_seat *seat EINA_UNUSED)
1374 {
1375    _eflutil.wl.devmgr.max_touch_count = max_count;
1376 }
1377
1378 static efl_util_error_e
1379 _efl_util_input_convert_input_generator_error(int ret)
1380 {
1381    switch (ret)
1382      {
1383         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE:
1384            return EFL_UTIL_ERROR_NONE;
1385         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_PERMISSION:
1386            return EFL_UTIL_ERROR_PERMISSION_DENIED;
1387         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES:
1388            return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1389         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_PARAMETER:
1390            return EFL_UTIL_ERROR_INVALID_PARAMETER;
1391         case TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED:
1392            return EFL_UTIL_ERROR_NO_RESOURCE_AVAILABLE;
1393         default :
1394            return EFL_UTIL_ERROR_NONE;
1395      }
1396 }
1397
1398 /* LCOV_EXCL_START */
1399 static int
1400 _timer_wait(void *data)
1401 {
1402    Efl_Util_Device_Info *dev;
1403
1404    _eflutil.wl.devmgr.request_notified = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_SYSTEM_RESOURCES;
1405
1406    if (eina_list_count(_eflutil.wl.devmgr.wait_devices) > 0)
1407      {
1408         EINA_LIST_FREE(_eflutil.wl.devmgr.wait_devices, dev)
1409           {
1410              eina_stringshare_del(dev->name);
1411              dev->name = NULL;
1412           }
1413      }
1414
1415    wl_event_source_remove(_eflutil.wl.devmgr.wait_timer);
1416    _eflutil.wl.devmgr.wait_timer = NULL;
1417
1418    return 1;
1419 }
1420 /* LCOV_EXCL_STOP */
1421
1422 static void
1423 _efl_util_input_initialize_wait_device(void)
1424 {
1425    struct wl_event_loop *loop;
1426    int ret = -1;
1427
1428    if (_efl_util_input_check_wait_device_full()) return;
1429
1430    loop = wl_event_loop_create();
1431    _eflutil.wl.devmgr.wait_timer = wl_event_loop_add_timer(loop, _timer_wait, NULL);
1432    if (_eflutil.wl.devmgr.wait_timer)
1433      {
1434         ret = wl_event_source_timer_update(_eflutil.wl.devmgr.wait_timer,
1435                                            EFL_UTIL_INPUT_GENERATOR_DEFAULT_TIME_OUT);
1436         if (ret != 0)
1437           {
1438              _timer_wait(NULL);
1439              return;
1440           }
1441      }
1442
1443    while (_eflutil.wl.devmgr.wait_timer)
1444      {
1445         wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1446         ret = wl_event_loop_dispatch(loop, EFL_UTIL_INPUT_GENERATOR_DEFAULT_DISPATCH_TIME_OUT);
1447         if (ret != 0) _timer_wait(NULL);
1448      }
1449 }
1450
1451 static void
1452 _efl_util_input_initialize_append_device(const char *name, Ecore_Device_Class clas)
1453 {
1454    Efl_Util_Device_Info *dev;
1455
1456    dev = (Efl_Util_Device_Info *)calloc(1, sizeof(Efl_Util_Device_Info));
1457    EINA_SAFETY_ON_NULL_RETURN(dev);
1458
1459    dev->name = eina_stringshare_add(name);
1460    dev->clas = clas;
1461
1462    _eflutil.wl.devmgr.wait_devices = eina_list_append(_eflutil.wl.devmgr.wait_devices, dev);
1463 }
1464
1465 static void
1466 _efl_util_input_initialize_add_wait_device(const char *name, unsigned int dev_type)
1467 {
1468    EINA_SAFETY_ON_NULL_RETURN(name);
1469
1470    if (dev_type & EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN)
1471      _efl_util_input_initialize_append_device(name, ECORE_DEVICE_CLASS_TOUCH);
1472
1473    if (dev_type & EFL_UTIL_INPUT_DEVTYPE_KEYBOARD)
1474      _efl_util_input_initialize_append_device(name, ECORE_DEVICE_CLASS_KEYBOARD);
1475
1476    if (dev_type & EFL_UTIL_INPUT_DEVTYPE_POINTER)
1477      _efl_util_input_initialize_append_device(name, ECORE_DEVICE_CLASS_MOUSE);
1478 }
1479
1480 static efl_util_inputgen_h
1481 _efl_util_input_create_inputgen(unsigned int dev_type, const char *name, int *ret, int with_name)
1482 {
1483    efl_util_inputgen_h inputgen_h = NULL;
1484    unsigned int clas = 0x0;
1485
1486    if (!dev_type ||
1487         dev_type & ~(EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN
1488                     | EFL_UTIL_INPUT_DEVTYPE_KEYBOARD
1489                     | EFL_UTIL_INPUT_DEVTYPE_POINTER))
1490      {
1491         set_last_result(EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1492         goto out;
1493      }
1494
1495    inputgen_h = (efl_util_inputgen_h)calloc(1, sizeof(struct _efl_util_inputgen_h));
1496    if (!inputgen_h)
1497      {
1498         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1499         goto out;
1500      }
1501
1502    inputgen_h->init_type |= dev_type;
1503    if (with_name)
1504      {
1505         if (name) strncpy(inputgen_h->name, name, 31);
1506         else strncpy(inputgen_h->name, "Input Generator", 31);
1507      }
1508
1509    *ret = _wl_init();
1510    if (*ret == (int)EINA_FALSE)
1511      {
1512         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1513         goto out;
1514      }
1515
1516    if (dev_type & EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN)
1517      clas |= TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN;
1518    if (dev_type & EFL_UTIL_INPUT_DEVTYPE_KEYBOARD)
1519      clas |= TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD;
1520    if (dev_type & EFL_UTIL_INPUT_DEVTYPE_POINTER)
1521      clas |= TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE;
1522
1523    while (!_eflutil.wl.devmgr.devicemgr)
1524      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1525
1526    if (with_name)
1527       tizen_input_device_manager_init_generator_with_name(_eflutil.wl.devmgr.devicemgr, clas, inputgen_h->name);
1528    else
1529       tizen_input_device_manager_init_generator(_eflutil.wl.devmgr.devicemgr, clas);
1530
1531
1532    while (_eflutil.wl.devmgr.request_notified == -1)
1533      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1534
1535    *ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1536    _eflutil.wl.devmgr.request_notified = -1;
1537
1538    set_last_result(*ret);
1539    if (*ret != TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE)
1540      goto out;
1541
1542    return inputgen_h;
1543
1544 out:
1545    if (inputgen_h)
1546      {
1547         free(inputgen_h);
1548         inputgen_h = NULL;
1549      }
1550    return NULL;
1551 }
1552
1553 API efl_util_inputgen_h
1554 efl_util_input_initialize_generator(unsigned int dev_type)
1555 {
1556    int ret = EFL_UTIL_ERROR_NONE;
1557    efl_util_inputgen_h inputgen_h = NULL;
1558
1559    inputgen_h = _efl_util_input_create_inputgen(dev_type, NULL, &ret, 0);
1560    if (!inputgen_h)
1561       return NULL;
1562
1563    return inputgen_h;
1564 }
1565
1566 API efl_util_inputgen_h
1567 efl_util_input_initialize_generator_with_name(unsigned int dev_type, const char *name)
1568 {
1569    int ret = EFL_UTIL_ERROR_NONE;
1570    efl_util_inputgen_h inputgen_h = NULL;
1571
1572    inputgen_h = _efl_util_input_create_inputgen(dev_type, name, &ret, 1);
1573    if (!inputgen_h)
1574       return NULL;
1575
1576    return inputgen_h;
1577 }
1578
1579 API efl_util_inputgen_h
1580 efl_util_input_initialize_generator_with_sync(unsigned int dev_type, const char *name)
1581 {
1582    int ret = EFL_UTIL_ERROR_NONE;
1583    efl_util_inputgen_h inputgen_h = NULL;
1584
1585    inputgen_h = efl_util_input_initialize_generator_with_name(dev_type, name);
1586    if (!inputgen_h)
1587       return NULL;
1588
1589    _efl_util_input_initialize_add_wait_device(inputgen_h->name, dev_type);
1590    _efl_util_input_initialize_wait_device();
1591    if (_eflutil.wl.devmgr.request_notified != -1)
1592       {
1593          ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1594          _eflutil.wl.devmgr.request_notified = -1;
1595       }
1596
1597    set_last_result(ret);
1598    if (ret != TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE)
1599      goto out;
1600
1601    return inputgen_h;
1602
1603 out:
1604    if (inputgen_h)
1605      {
1606         free(inputgen_h);
1607         inputgen_h = NULL;
1608      }
1609
1610    return NULL;
1611 }
1612
1613 API int
1614 efl_util_input_set_touch_count(int max_count)
1615 {
1616    int ret = EFL_UTIL_ERROR_NONE;
1617
1618    ret = _wl_init();
1619    if (ret == (int)EINA_FALSE)
1620      {
1621         return EFL_UTIL_ERROR_INVALID_PARAMETER;
1622      }
1623
1624    while (!_eflutil.wl.devmgr.devicemgr)
1625      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1626
1627    if (_eflutil.wl.devmgr.max_touch_count >= max_count)
1628      return EFL_UTIL_ERROR_NONE;
1629
1630    tizen_input_device_manager_set_touch_count(_eflutil.wl.devmgr.devicemgr, max_count);
1631
1632    while (_eflutil.wl.devmgr.request_notified == -1)
1633      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1634
1635    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1636    _eflutil.wl.devmgr.request_notified = -1;
1637
1638    if (ret == EFL_UTIL_ERROR_NONE)
1639      {
1640         _eflutil.wl.devmgr.request_touch_count = max_count;
1641      }
1642
1643    return ret;
1644 }
1645
1646 API int
1647 efl_util_input_deinitialize_generator(efl_util_inputgen_h inputgen_h)
1648 {
1649    int ret = EFL_UTIL_ERROR_NONE;
1650    unsigned int clas = 0x0;
1651    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1652
1653    if (inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN)
1654      clas |= TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN;
1655    if (inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_KEYBOARD)
1656      clas |= TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD;
1657    if (inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_POINTER)
1658      clas |= TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE;
1659
1660    if (inputgen_h->axis_info)
1661      free(inputgen_h->axis_info);
1662
1663    free(inputgen_h);
1664    inputgen_h = NULL;
1665
1666    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1667
1668    tizen_input_device_manager_deinit_generator(_eflutil.wl.devmgr.devicemgr, clas);
1669
1670    while (_eflutil.wl.devmgr.request_notified == -1)
1671      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1672
1673    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1674    _eflutil.wl.devmgr.request_notified = -1;
1675
1676    return ret;
1677 }
1678
1679 API int
1680 efl_util_input_generate_key(efl_util_inputgen_h inputgen_h, const char *key_name, int pressed)
1681 {
1682    int ret = EFL_UTIL_ERROR_NONE;
1683
1684    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1685    EINA_SAFETY_ON_NULL_RETURN_VAL(key_name, EFL_UTIL_ERROR_INVALID_PARAMETER);
1686    EINA_SAFETY_ON_FALSE_RETURN_VAL(pressed == 0 || pressed == 1, EFL_UTIL_ERROR_INVALID_PARAMETER);
1687    EINA_SAFETY_ON_FALSE_RETURN_VAL(inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_KEYBOARD, EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1688
1689    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1690
1691    tizen_input_device_manager_generate_key(_eflutil.wl.devmgr.devicemgr, key_name, pressed);
1692
1693    while (_eflutil.wl.devmgr.request_notified == -1)
1694      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1695
1696    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1697    _eflutil.wl.devmgr.request_notified = -1;
1698
1699    return ret;
1700 }
1701
1702 API int
1703 efl_util_input_generate_touch(efl_util_inputgen_h inputgen_h, int idx,
1704                               efl_util_input_touch_type_e touch_type, int x, int y)
1705 {
1706    int ret;
1707    enum tizen_input_device_manager_pointer_event_type type;
1708
1709    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1710    EINA_SAFETY_ON_FALSE_RETURN_VAL(idx >= 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
1711    EINA_SAFETY_ON_FALSE_RETURN_VAL((x > 0 && y > 0), EFL_UTIL_ERROR_INVALID_PARAMETER);
1712    EINA_SAFETY_ON_FALSE_RETURN_VAL(inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN, EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1713
1714    if (idx >= _eflutil.wl.devmgr.max_touch_count)
1715      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1716
1717    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1718
1719    switch(touch_type)
1720      {
1721         case EFL_UTIL_INPUT_TOUCH_BEGIN:
1722            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN;
1723            break;
1724         case EFL_UTIL_INPUT_TOUCH_UPDATE:
1725            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE;
1726            break;
1727         case EFL_UTIL_INPUT_TOUCH_END:
1728            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END;
1729            break;
1730         default:
1731            return EFL_UTIL_ERROR_INVALID_PARAMETER;
1732      }
1733
1734    tizen_input_device_manager_generate_touch(_eflutil.wl.devmgr.devicemgr, type, x, y, idx);
1735
1736    while (_eflutil.wl.devmgr.request_notified == -1)
1737      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1738
1739    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1740    _eflutil.wl.devmgr.request_notified = -1;
1741
1742    return ret;
1743 }
1744
1745 static int
1746 _efl_util_input_generate_touch_axis_send(unsigned int type, double value)
1747 {
1748    int ret;
1749
1750    tizen_input_device_manager_generate_axis(_eflutil.wl.devmgr.devicemgr, type, wl_fixed_from_double(value));
1751
1752    while (_eflutil.wl.devmgr.request_notified == -1)
1753      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1754
1755    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1756    _eflutil.wl.devmgr.request_notified = -1;
1757
1758    return ret;
1759 }
1760
1761 static void
1762 _efl_util_input_generate_touch_axis_cleanup(efl_util_inputgen_h inputgen_h, int idx)
1763 {
1764    int i;
1765    if (idx >= 0)
1766      {
1767         inputgen_h->axis_info[idx].radius_x = 1.0;
1768         inputgen_h->axis_info[idx].radius_y = 1.0;
1769         inputgen_h->axis_info[idx].pressure = 1.0;
1770         inputgen_h->axis_info[idx].angle = 0.0;
1771         inputgen_h->axis_info[idx].palm = 0.0;
1772      }
1773    else
1774      {
1775         for (i = 0; i < EFL_UTIL_INPUT_GENERATOR_TOUCH_MAX_FINGER; i++)
1776           {
1777              inputgen_h->axis_info[i].radius_x = 1.0;
1778              inputgen_h->axis_info[i].radius_y = 1.0;
1779              inputgen_h->axis_info[i].pressure = 1.0;
1780              inputgen_h->axis_info[i].angle = 0.0;
1781              inputgen_h->axis_info[i].palm = 0.0;
1782           }
1783      }
1784 }
1785
1786 static int
1787 _efl_util_input_generate_touch_axis_process(efl_util_inputgen_h inputgen_h, int idx, double radius_x,
1788                                             double radius_y, double pressure, double angle, double palm)
1789 {
1790    int ret = EFL_UTIL_ERROR_NONE;
1791
1792    if (!inputgen_h->axis_info)
1793      {
1794         inputgen_h->axis_info = calloc(EFL_UTIL_INPUT_GENERATOR_TOUCH_MAX_FINGER,
1795                                        sizeof(E_Devicemgr_Inputgen_Touch_Axis));
1796         _efl_util_input_generate_touch_axis_cleanup(inputgen_h, -1);
1797      }
1798
1799    if (inputgen_h->axis_info[idx].radius_x != radius_x)
1800      {
1801         ret = _efl_util_input_generate_touch_axis_send(TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_RADIUS_X, radius_x);
1802         if (ret != EFL_UTIL_ERROR_NONE) return ret;
1803         inputgen_h->axis_info[idx].radius_x = radius_x;
1804      }
1805    if (inputgen_h->axis_info[idx].radius_y != radius_y)
1806      {
1807         ret = _efl_util_input_generate_touch_axis_send(TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_RADIUS_Y, radius_y);
1808         if (ret != EFL_UTIL_ERROR_NONE) return ret;
1809         inputgen_h->axis_info[idx].radius_y = radius_y;
1810      }
1811    if (inputgen_h->axis_info[idx].pressure != pressure)
1812      {
1813         ret = _efl_util_input_generate_touch_axis_send(TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_PRESSURE, pressure);
1814         if (ret != EFL_UTIL_ERROR_NONE) return ret;
1815         inputgen_h->axis_info[idx].pressure = pressure;
1816      }
1817    if (inputgen_h->axis_info[idx].angle != angle)
1818      {
1819         ret = _efl_util_input_generate_touch_axis_send(TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_ANGLE, angle);
1820         if (ret != EFL_UTIL_ERROR_NONE) return ret;
1821         inputgen_h->axis_info[idx].angle = angle;
1822      }
1823    if (inputgen_h->axis_info[idx].palm != palm)
1824      {
1825         ret = _efl_util_input_generate_touch_axis_send(TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_PALM, palm);
1826         if (ret != EFL_UTIL_ERROR_NONE) return ret;
1827         inputgen_h->axis_info[idx].palm = palm;
1828      }
1829
1830    return ret;
1831 }
1832
1833 API int
1834 efl_util_input_generate_touch_axis(efl_util_inputgen_h inputgen_h, int idx,
1835                                    efl_util_input_touch_type_e touch_type, int x, int y,
1836                                    double radius_x, double radius_y,
1837                                    double pressure, double angle, double palm)
1838 {
1839    int ret, version;
1840    enum tizen_input_device_manager_pointer_event_type type;
1841
1842    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1843    EINA_SAFETY_ON_FALSE_RETURN_VAL(idx >= 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
1844    EINA_SAFETY_ON_FALSE_RETURN_VAL((x >= 0 && y >= 0), EFL_UTIL_ERROR_INVALID_PARAMETER);
1845    EINA_SAFETY_ON_FALSE_RETURN_VAL(inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN, EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1846    EINA_SAFETY_ON_FALSE_RETURN_VAL((radius_x >= 0.0 && radius_y >= 0.0), EFL_UTIL_ERROR_INVALID_PARAMETER);
1847    EINA_SAFETY_ON_FALSE_RETURN_VAL((pressure >= 0.0 && palm >= 0.0), EFL_UTIL_ERROR_INVALID_PARAMETER);
1848
1849    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1850    version = tizen_input_device_manager_get_version(_eflutil.wl.devmgr.devicemgr);
1851    EINA_SAFETY_ON_FALSE_RETURN_VAL((version >= 3), EFL_UTIL_ERROR_NOT_SUPPORTED);
1852
1853    switch(touch_type)
1854      {
1855         case EFL_UTIL_INPUT_TOUCH_BEGIN:
1856            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN;
1857            break;
1858         case EFL_UTIL_INPUT_TOUCH_UPDATE:
1859            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE;
1860            break;
1861         case EFL_UTIL_INPUT_TOUCH_END:
1862            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END;
1863            break;
1864         default:
1865            return EFL_UTIL_ERROR_INVALID_PARAMETER;
1866      }
1867
1868    if (touch_type != EFL_UTIL_INPUT_TOUCH_END)
1869      _efl_util_input_generate_touch_axis_process(inputgen_h, idx, radius_x, radius_y, pressure, angle, palm);
1870    else
1871      _efl_util_input_generate_touch_axis_cleanup(inputgen_h, idx);
1872
1873    tizen_input_device_manager_generate_touch(_eflutil.wl.devmgr.devicemgr, type, x, y, idx);
1874
1875    while (_eflutil.wl.devmgr.request_notified == -1)
1876      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1877
1878    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1879    _eflutil.wl.devmgr.request_notified = -1;
1880
1881    return ret;
1882 }
1883
1884
1885 API int
1886 efl_util_input_generate_pointer(efl_util_inputgen_h inputgen_h, int buttons, efl_util_input_pointer_type_e pointer_type, int x, int y)
1887 {
1888    int ret;
1889    enum tizen_input_device_manager_pointer_event_type type;
1890
1891    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1892    EINA_SAFETY_ON_FALSE_RETURN_VAL(buttons > 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
1893    EINA_SAFETY_ON_FALSE_RETURN_VAL((x >= 0 && y >= 0), EFL_UTIL_ERROR_INVALID_PARAMETER);
1894    EINA_SAFETY_ON_FALSE_RETURN_VAL(inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_POINTER, EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1895
1896    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1897
1898    switch(pointer_type)
1899      {
1900         case EFL_UTIL_INPUT_POINTER_BUTTON_DOWN:
1901            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_BEGIN;
1902            break;
1903         case EFL_UTIL_INPUT_POINTER_MOVE:
1904            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_UPDATE;
1905            break;
1906         case EFL_UTIL_INPUT_POINTER_BUTTON_UP:
1907            type = TIZEN_INPUT_DEVICE_MANAGER_POINTER_EVENT_TYPE_END;
1908            break;
1909         default:
1910            return EFL_UTIL_ERROR_INVALID_PARAMETER;
1911      }
1912
1913    tizen_input_device_manager_generate_pointer(_eflutil.wl.devmgr.devicemgr, type, x, y, buttons);
1914
1915    while (_eflutil.wl.devmgr.request_notified == -1)
1916      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1917
1918    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1919    _eflutil.wl.devmgr.request_notified = -1;
1920
1921    return ret;
1922 }
1923
1924 API int
1925 efl_util_input_generate_wheel(efl_util_inputgen_h inputgen_h, efl_util_input_pointer_wheel_type_e wheel_type, int value)
1926 {
1927    int ret, version;
1928    enum tizen_input_device_manager_pointer_event_type type;
1929
1930    EINA_SAFETY_ON_NULL_RETURN_VAL(inputgen_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
1931    EINA_SAFETY_ON_FALSE_RETURN_VAL(inputgen_h->init_type & EFL_UTIL_INPUT_DEVTYPE_POINTER, EFL_UTIL_ERROR_NO_SUCH_DEVICE);
1932
1933    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.devmgr.devicemgr, EFL_UTIL_ERROR_INVALID_PARAMETER);
1934    version = tizen_input_device_manager_get_version(_eflutil.wl.devmgr.devicemgr);
1935    EINA_SAFETY_ON_FALSE_RETURN_VAL((version >= 3), EFL_UTIL_ERROR_NOT_SUPPORTED);
1936
1937    switch(wheel_type)
1938      {
1939         case EFL_UTIL_INPUT_POINTER_WHEEL_VERT:
1940            type = TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_WHEEL;
1941            break;
1942         case EFL_UTIL_INPUT_POINTER_WHEEL_HORZ:
1943            type = TIZEN_INPUT_DEVICE_MANAGER_AXIS_TYPE_HWHEEL;
1944            break;
1945         default:
1946            return EFL_UTIL_ERROR_INVALID_PARAMETER;
1947      }
1948
1949    tizen_input_device_manager_generate_axis(_eflutil.wl.devmgr.devicemgr, type, wl_fixed_from_int(value));
1950
1951    while (_eflutil.wl.devmgr.request_notified == -1)
1952      wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1953
1954    ret = _efl_util_input_convert_input_generator_error(_eflutil.wl.devmgr.request_notified);
1955    _eflutil.wl.devmgr.request_notified = -1;
1956
1957    return ret;
1958 }
1959
1960
1961
1962 struct _efl_util_screenshot_h
1963 {
1964    int width;
1965    int height;
1966
1967    Eina_Bool shot_done;
1968
1969    /* tbm bufmgr */
1970    tbm_bufmgr bufmgr;
1971
1972    Eina_Bool auto_rotation;
1973 };
1974
1975 /* scrrenshot handle */
1976 static efl_util_screenshot_h g_screenshot;
1977 static Eina_Bool shot_mutex_init;
1978 static pthread_mutex_t shot_lock;
1979
1980 static Eina_Bool
1981 _screenshot_mutex_init(void)
1982 {
1983    if (shot_mutex_init)
1984      return EINA_TRUE;
1985
1986    if (pthread_mutex_init(&shot_lock, NULL))
1987      {
1988         fprintf(stderr, "[screenshot] fail: mutex init"); /*LCOV_EXCL_LINE*/
1989         return EINA_FALSE; /*LCOV_EXCL_LINE*/
1990      }
1991
1992    shot_mutex_init = EINA_TRUE;
1993
1994    return EINA_TRUE;
1995 }
1996
1997 static Eina_Bool
1998 _screenshot_mutex_destory(void)
1999 {
2000    if (!shot_mutex_init)
2001      return EINA_TRUE;
2002
2003    if (pthread_mutex_destroy(&shot_lock))
2004      {
2005         fprintf(stderr, "[screenshot] fail: mutex destory"); /*LCOV_EXCL_LINE*/
2006         return EINA_FALSE; /*LCOV_EXCL_LINE*/
2007      }
2008
2009    shot_mutex_init = EINA_FALSE;
2010
2011    return EINA_TRUE;
2012 }
2013
2014 void
2015 _screenshot_mutex_lock(void)
2016 {
2017    if (!_screenshot_mutex_init())
2018      return;
2019
2020    pthread_mutex_lock(&shot_lock);
2021 }
2022
2023 void
2024 _screenshot_mutex_unlock(void)
2025 {
2026    pthread_mutex_unlock(&shot_lock);
2027 }
2028
2029 static Eina_Bool
2030 _efl_util_wl_screenshooter_init()
2031 {
2032    struct wl_display *display_wrapper = NULL;
2033    struct wl_registry *registry = NULL;
2034    int ret = 0;
2035
2036    if (_wl_init() == EINA_FALSE)
2037      return EINA_FALSE;
2038
2039    wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
2040
2041    display_wrapper = wl_proxy_create_wrapper(_eflutil.wl.dpy);
2042    EINA_SAFETY_ON_NULL_RETURN_VAL(display_wrapper, EINA_FALSE);
2043
2044    _eflutil.wl.shot.queue = wl_display_create_queue(_eflutil.wl.dpy);
2045    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.queue, fail_create_queue);
2046
2047    wl_proxy_set_queue((struct wl_proxy *)display_wrapper, _eflutil.wl.shot.queue);
2048
2049    registry = wl_display_get_registry(display_wrapper);
2050    EINA_SAFETY_ON_NULL_GOTO(registry, fail_get_registry);
2051
2052    wl_registry_add_listener(registry, &_wl_reg_screenshooter_listener, NULL);
2053
2054    ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.shot.queue);
2055    EINA_SAFETY_ON_TRUE_GOTO(ret == -1, fail_roundtrip);
2056    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tz_screenshooter, fail_roundtrip);
2057
2058    _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
2059    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_tbm_client_init);
2060
2061    wl_registry_destroy(registry);
2062    wl_proxy_wrapper_destroy(display_wrapper);
2063
2064    if (_eflutil.wl.shot.noti == 0)
2065      {
2066         fprintf(stderr, "[screenshot] fail: privilege error\n"); /* LCOV_EXCL_LINE */
2067         return EINA_FALSE;
2068      }
2069
2070    return EINA_TRUE;
2071
2072 fail_tbm_client_init:
2073    tizen_screenshooter_destroy(_eflutil.wl.shot.tz_screenshooter);
2074    _eflutil.wl.shot.tz_screenshooter = NULL;
2075 fail_roundtrip:
2076    wl_registry_destroy(registry);
2077 fail_get_registry:
2078    wl_event_queue_destroy(_eflutil.wl.shot.queue);
2079    _eflutil.wl.shot.queue = NULL;
2080 fail_create_queue:
2081    wl_proxy_wrapper_destroy(display_wrapper);
2082
2083    return EINA_FALSE;
2084 }
2085
2086 static void
2087 _efl_util_wl_screenshooter_deinit()
2088 {
2089    if (_eflutil.wl.shot.tbm_client)
2090      {
2091         wayland_tbm_client_deinit(_eflutil.wl.shot.tbm_client);
2092         _eflutil.wl.shot.tbm_client = NULL;
2093      }
2094
2095    if (_eflutil.wl.shot.tz_screenshooter)
2096      {
2097         tizen_screenshooter_destroy(_eflutil.wl.shot.tz_screenshooter);
2098         _eflutil.wl.shot.tz_screenshooter = NULL;
2099      }
2100
2101    if (_eflutil.wl.shot.queue)
2102      {
2103         wl_event_queue_destroy(_eflutil.wl.shot.queue);
2104         _eflutil.wl.shot.queue = NULL;
2105      }
2106 }
2107
2108 API efl_util_screenshot_h
2109 efl_util_screenshot_initialize(int width, int height)
2110 {
2111    efl_util_screenshot_h screenshot = NULL;
2112
2113    if (width <= 0 || height <= 0)
2114      {
2115         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2116         return NULL;
2117      }
2118
2119    _screenshot_mutex_lock();
2120
2121    if (g_screenshot)
2122      {
2123         if (g_screenshot->width != width || g_screenshot->height != height)
2124           {
2125              g_screenshot->width = width;
2126              g_screenshot->height = height;
2127           }
2128         set_last_result(EFL_UTIL_ERROR_NONE);
2129
2130         _screenshot_mutex_unlock();
2131
2132         return g_screenshot;
2133      }
2134
2135    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
2136    if (screenshot == NULL)
2137      {
2138         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
2139
2140         _screenshot_mutex_unlock();
2141
2142         return NULL;
2143      }
2144
2145    if (!_eflutil.wl.shot.tz_screenshooter)
2146      {
2147         if (_efl_util_wl_screenshooter_init() == EINA_FALSE)
2148           {
2149              if (_eflutil.wl.shot.noti == 0)
2150                set_last_result(EFL_UTIL_ERROR_PERMISSION_DENIED);
2151              else
2152                set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
2153
2154              free(screenshot);
2155
2156              _screenshot_mutex_unlock();
2157
2158              return NULL;
2159           }
2160      }
2161
2162    screenshot->width = width;
2163    screenshot->height = height;
2164    screenshot->auto_rotation = EINA_TRUE;
2165
2166    screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
2167    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_get_bufmgr);
2168
2169    g_screenshot = screenshot;
2170    set_last_result(EFL_UTIL_ERROR_NONE);
2171
2172    tizen_screenshooter_set_user_data(_eflutil.wl.shot.tz_screenshooter, &screenshot->shot_done);
2173
2174    _screenshot_mutex_unlock();
2175
2176    return g_screenshot;
2177
2178 /* LCOV_EXCL_START */
2179 fail_get_bufmgr:
2180    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
2181    _screenshot_mutex_unlock();
2182    efl_util_screenshot_deinitialize(screenshot);
2183
2184    return NULL;
2185 /* LCOV_EXCL_STOP */
2186 }
2187
2188 API int
2189 efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
2190 {
2191    _screenshot_mutex_lock();
2192
2193    if (!screenshot)
2194      {
2195         _screenshot_mutex_unlock();
2196         _screenshot_mutex_destory();
2197         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2198      }
2199
2200    free(screenshot);
2201    g_screenshot = NULL;
2202
2203    _efl_util_wl_screenshooter_deinit();
2204
2205    _screenshot_mutex_unlock();
2206    _screenshot_mutex_destory();
2207
2208    return EFL_UTIL_ERROR_NONE;
2209 }
2210
2211
2212 API tbm_surface_h
2213 efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
2214 {
2215    tbm_surface_h t_surface = NULL;
2216    struct wl_buffer *buffer = NULL;
2217    Efl_Util_Wl_Output_Info *output;
2218    int ret = 0;
2219
2220    _screenshot_mutex_lock();
2221
2222    if (!screenshot || (screenshot != g_screenshot))
2223      {
2224         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2225         _screenshot_mutex_unlock();
2226         return NULL;
2227      }
2228
2229    output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
2230    if (!output)
2231      {
2232         fprintf(stderr, "[screenshot] fail: no output for screenshot\n"); /* LCOV_EXCL_LINE */
2233         goto fail;
2234      }
2235
2236    t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
2237    if (!t_surface)
2238      {
2239         fprintf(stderr, "[screenshot] fail: tbm_surface_create\n"); /* LCOV_EXCL_LINE */
2240         goto fail;
2241      }
2242
2243    buffer = wayland_tbm_client_create_buffer(_eflutil.wl.shot.tbm_client, t_surface);
2244    if (!buffer)
2245      {
2246         fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n"); /* LCOV_EXCL_LINE */
2247         goto fail;
2248      }
2249
2250    tizen_screenshooter_shoot(_eflutil.wl.shot.tz_screenshooter, output->output, buffer);
2251
2252    screenshot->shot_done = EINA_FALSE;
2253
2254    while (!screenshot->shot_done && ret != -1)
2255      ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.shot.queue);
2256
2257    if (ret == -1)
2258      {
2259         fprintf(stderr, "[screenshot] fail: tizen_screenshooter_shoot\n"); /* LCOV_EXCL_LINE */
2260         goto fail;
2261      }
2262
2263    wayland_tbm_client_destroy_buffer(_eflutil.wl.shot.tbm_client, buffer);
2264
2265    /* reset shot_done for next screenshot */
2266    screenshot->shot_done = EINA_FALSE;
2267
2268    set_last_result(EFL_UTIL_ERROR_NONE);
2269
2270    _screenshot_mutex_unlock();
2271
2272    return t_surface;
2273
2274 fail:
2275    if (t_surface)
2276      tbm_surface_destroy(t_surface);
2277    if (buffer)
2278      wayland_tbm_client_destroy_buffer(_eflutil.wl.shot.tbm_client, buffer);
2279
2280    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
2281
2282    _screenshot_mutex_unlock();
2283
2284    return NULL;
2285 }
2286
2287 API int
2288 efl_util_screenshot_set_auto_rotation(efl_util_screenshot_h screenshot, int set)
2289 {
2290    if (!screenshot || (screenshot != g_screenshot))
2291      return EFL_UTIL_ERROR_INVALID_PARAMETER;
2292
2293    if (!(set == 0 || set == 1))
2294      return EFL_UTIL_ERROR_INVALID_PARAMETER;
2295
2296    if (set)
2297      g_screenshot->auto_rotation = EINA_TRUE;
2298    else
2299      g_screenshot->auto_rotation = EINA_FALSE;
2300
2301    tizen_screenshooter_set_oneshot_auto_rotation(_eflutil.wl.shot.tz_screenshooter, g_screenshot->auto_rotation);
2302
2303    return EFL_UTIL_ERROR_NONE;
2304 }
2305
2306 API int
2307 efl_util_screenshot_get_auto_rotation(efl_util_screenshot_h screenshot, int *set)
2308 {
2309    if (!screenshot || (screenshot != g_screenshot) || !set)
2310      return EFL_UTIL_ERROR_INVALID_PARAMETER;
2311
2312    *set = g_screenshot->auto_rotation;
2313
2314    return EFL_UTIL_ERROR_NONE;
2315 }
2316
2317 struct _efl_util_gesture_h
2318 {
2319    Eina_Bool init;
2320 };
2321
2322 API int EFL_UTIL_EVENT_GESTURE_EDGE_SWIPE = 0;
2323 API int EFL_UTIL_EVENT_GESTURE_EDGE_DRAG = 0;
2324 API int EFL_UTIL_EVENT_GESTURE_TAP = 0;
2325 API int EFL_UTIL_EVENT_GESTURE_PALM_COVER = 0;
2326
2327 /* LCOV_EXCL_START */
2328 static void
2329 _cb_gesture_edge_swipe_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t fingers EINA_UNUSED, uint32_t edge EINA_UNUSED, uint32_t edge_size EINA_UNUSED, uint32_t start_point EINA_UNUSED, uint32_t end_point EINA_UNUSED, uint32_t error)
2330 {
2331    _eflutil_defaultqueue.wl.gesture.request_notified = error;
2332 }
2333
2334 static void
2335 _cb_gesture_edge_swipe(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t mode, uint32_t fingers, int sx, int sy, uint32_t edge)
2336 {
2337    efl_util_event_gesture_edge_swipe_s *ev = NULL;
2338
2339    ev = (efl_util_event_gesture_edge_swipe_s *)calloc(1, sizeof(*ev));
2340    if (!ev) return;
2341
2342    ev->mode = mode;
2343
2344    ev->fingers = fingers;
2345    ev->sx = sx;
2346    ev->sy = sy;
2347    ev->edge = edge;
2348
2349    ecore_event_add(EFL_UTIL_EVENT_GESTURE_EDGE_SWIPE, ev, NULL, NULL);
2350 }
2351
2352 static void
2353 _cb_gesture_edge_drag_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t fingers EINA_UNUSED, uint32_t edge EINA_UNUSED, uint32_t edge_size EINA_UNUSED, uint32_t start_point EINA_UNUSED, uint32_t end_point EINA_UNUSED, uint32_t error)
2354 {
2355    _eflutil_defaultqueue.wl.gesture.request_notified = error;
2356 }
2357
2358 static void
2359 _cb_gesture_edge_drag(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t mode, uint32_t fingers, int cx, int cy, uint32_t edge)
2360 {
2361    efl_util_event_gesture_edge_drag_s *ev = NULL;
2362
2363    ev = (efl_util_event_gesture_edge_drag_s *)calloc(1, sizeof(*ev));
2364    if (!ev) return;
2365
2366    ev->mode = mode;
2367
2368    ev->fingers = fingers;
2369    ev->cx = cx;
2370    ev->cy = cy;
2371    ev->edge = edge;
2372
2373    ecore_event_add(EFL_UTIL_EVENT_GESTURE_EDGE_DRAG, ev, NULL, NULL);
2374 }
2375
2376 static void
2377 _cb_gesture_tap_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t fingers EINA_UNUSED, uint32_t repeat EINA_UNUSED, uint32_t error)
2378 {
2379    _eflutil_defaultqueue.wl.gesture.request_notified = error;
2380 }
2381
2382 static void
2383 _cb_gesture_tap(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, uint32_t mode, uint32_t fingers, uint32_t repeats)
2384 {
2385    efl_util_event_gesture_tap_s *ev = NULL;
2386
2387    ev = (efl_util_event_gesture_tap_s *)calloc(1, sizeof(*ev));
2388    if (!ev) return;
2389
2390    ev->mode = mode;
2391
2392    ev->fingers = fingers;
2393    ev->repeats = repeats;
2394
2395    ecore_event_add(EFL_UTIL_EVENT_GESTURE_TAP, ev, NULL, NULL);
2396 }
2397 /* LCOV_EXCL_STOP */
2398
2399 static void
2400 _cb_gesture_palm_cover_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, struct wl_surface *surface EINA_UNUSED, uint32_t error)
2401 {
2402    _eflutil_defaultqueue.wl.gesture.request_notified = error;
2403 }
2404
2405 /* LCOV_EXCL_START */
2406 static void
2407 _cb_gesture_palm_cover(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, struct wl_surface *surface, uint32_t mode, uint32_t duration, int cx, int cy, uint32_t size, wl_fixed_t pressure)
2408 {
2409    efl_util_event_gesture_palm_cover_s *ev = NULL;
2410
2411    ev = (efl_util_event_gesture_palm_cover_s *)calloc(1, sizeof(*ev));
2412    if (!ev) return;
2413
2414    ev->mode = mode;
2415
2416    ev->duration = duration;
2417    ev->cx = cx;
2418    ev->cy = cy;
2419    ev->size = size;
2420    ev->pressure = wl_fixed_to_int(pressure);
2421
2422    ecore_event_add(EFL_UTIL_EVENT_GESTURE_PALM_COVER, ev, NULL, NULL);
2423 }
2424 /* LCOV_EXCL_STOP */
2425
2426 static void
2427 _cb_gesture_activate_notify(void *data EINA_UNUSED, struct tizen_gesture *tizen_gesture EINA_UNUSED, struct wl_surface *surface EINA_UNUSED, uint32_t type EINA_UNUSED, uint32_t active EINA_UNUSED, uint32_t error)
2428 {
2429    _eflutil_defaultqueue.wl.gesture.request_notified = error;
2430 }
2431
2432 static efl_util_error_e
2433 _efl_util_gesture_convert_error(int ret)
2434 {
2435    switch (ret)
2436      {
2437         case TIZEN_GESTURE_ERROR_NONE:
2438            return EFL_UTIL_ERROR_NONE;
2439         case TIZEN_GESTURE_ERROR_INVALID_DATA:
2440            return EFL_UTIL_ERROR_INVALID_PARAMETER;
2441         case TIZEN_GESTURE_ERROR_NO_PERMISSION:
2442            return EFL_UTIL_ERROR_PERMISSION_DENIED;
2443         case TIZEN_GESTURE_ERROR_NO_SYSTEM_RESOURCES:
2444            return EFL_UTIL_ERROR_OUT_OF_MEMORY;
2445         case TIZEN_GESTURE_ERROR_GRABBED_ALREADY:
2446            return EFL_UTIL_ERROR_NO_RESOURCE_AVAILABLE;
2447         case TIZEN_GESTURE_ERROR_NOT_SUPPORTED:
2448            return EFL_UTIL_ERROR_NOT_SUPPORTED;
2449         default :
2450            return EFL_UTIL_ERROR_NONE;
2451      }
2452 }
2453
2454 /* LCOV_EXCL_START */
2455 static efl_util_error_e
2456 _efl_util_gesture_verify_request_notified()
2457 {
2458    int ret = EFL_UTIL_ERROR_NONE;
2459
2460    while (_eflutil_defaultqueue.wl.gesture.request_notified == -1)
2461      wl_display_dispatch(_eflutil_defaultqueue.wl.dpy);
2462
2463    ret = _efl_util_gesture_convert_error(_eflutil_defaultqueue.wl.gesture.request_notified);
2464    _eflutil_defaultqueue.wl.gesture.request_notified = -1;
2465
2466    return ret;
2467 }
2468
2469 static int
2470 _efl_util_gesture_grab_edge_swipe(efl_util_gesture_data data, Eina_Bool grabbed)
2471 {
2472    int ret = EFL_UTIL_ERROR_NONE;
2473    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
2474    Efl_Util_Gesture_Edge_Swipe_Grab_Data *edge_swipe_data = NULL;
2475    unsigned int fingers = 0;
2476    unsigned int edge = 0;
2477    unsigned int edge_size = 0;
2478    unsigned int start_point = 0;
2479    unsigned int end_point = 0;
2480
2481    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
2482
2483    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil_defaultqueue.wl.gesture.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
2484    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2485    EINA_SAFETY_ON_FALSE_RETURN_VAL(base_data->type == TIZEN_GESTURE_TYPE_EDGE_SWIPE,
2486                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
2487
2488    edge_swipe_data = (Efl_Util_Gesture_Edge_Swipe_Grab_Data *)data;
2489
2490    fingers = edge_swipe_data->fingers;
2491    edge = edge_swipe_data->edge;
2492    edge_size = edge_swipe_data->edge_size;
2493    start_point = edge_swipe_data->start_point;
2494    end_point = edge_swipe_data->end_point;
2495
2496    if (grabbed)
2497       tizen_gesture_grab_edge_swipe(_eflutil_defaultqueue.wl.gesture.proto, fingers, edge, edge_size, start_point, end_point);
2498    else
2499       tizen_gesture_ungrab_edge_swipe(_eflutil_defaultqueue.wl.gesture.proto, fingers, edge, edge_size, start_point, end_point);
2500
2501    ret = _efl_util_gesture_verify_request_notified();
2502
2503    return ret;
2504 }
2505
2506 static int
2507 _efl_util_gesture_grab_edge_drag(efl_util_gesture_data data, Eina_Bool grabbed)
2508 {
2509    int ret = EFL_UTIL_ERROR_NONE;
2510    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
2511    Efl_Util_Gesture_Edge_Drag_Grab_Data *edge_drag_data = NULL;
2512    unsigned int fingers = 0;
2513    unsigned int edge = 0;
2514    unsigned int edge_size = 0;
2515    unsigned int start_point = 0;
2516    unsigned int end_point = 0;
2517
2518    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
2519
2520    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil_defaultqueue.wl.gesture.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
2521    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2522    EINA_SAFETY_ON_FALSE_RETURN_VAL(base_data->type == TIZEN_GESTURE_TYPE_EDGE_DRAG,
2523                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
2524
2525    edge_drag_data = (Efl_Util_Gesture_Edge_Drag_Grab_Data *)data;
2526
2527    fingers = edge_drag_data->fingers;
2528    edge = edge_drag_data->edge;
2529    edge_size = edge_drag_data->edge_size;
2530    start_point = edge_drag_data->start_point;
2531    end_point = edge_drag_data->end_point;
2532
2533    if (grabbed)
2534       tizen_gesture_grab_edge_drag(_eflutil_defaultqueue.wl.gesture.proto, fingers, edge, edge_size, start_point, end_point);
2535    else
2536       tizen_gesture_ungrab_edge_drag(_eflutil_defaultqueue.wl.gesture.proto, fingers, edge, edge_size, start_point, end_point);
2537
2538    ret = _efl_util_gesture_verify_request_notified();
2539
2540    return ret;
2541 }
2542
2543 static int
2544 _efl_util_gesture_grab_tap(efl_util_gesture_data data, Eina_Bool grabbed)
2545 {
2546    int ret = EFL_UTIL_ERROR_NONE;
2547    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
2548    Efl_Util_Gesture_Tap_Grab_Data *tap_data = NULL;
2549    unsigned int fingers = 0;
2550    unsigned int repeats = 0;
2551
2552    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
2553
2554    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil_defaultqueue.wl.gesture.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
2555    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2556    EINA_SAFETY_ON_FALSE_RETURN_VAL(base_data->type == TIZEN_GESTURE_TYPE_TAP,
2557                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
2558
2559    tap_data = (Efl_Util_Gesture_Tap_Grab_Data *)data;
2560
2561    fingers = tap_data->fingers;
2562    repeats = tap_data->repeats;
2563
2564    if (grabbed)
2565       tizen_gesture_grab_tap(_eflutil_defaultqueue.wl.gesture.proto, fingers, repeats);
2566    else
2567       tizen_gesture_ungrab_tap(_eflutil_defaultqueue.wl.gesture.proto, fingers, repeats);
2568
2569    ret = _efl_util_gesture_verify_request_notified();
2570
2571    return ret;
2572 }
2573
2574 static int
2575 _efl_util_gesture_grab_palm_cover(efl_util_gesture_data data, Eina_Bool grabbed)
2576 {
2577    int ret = EFL_UTIL_ERROR_NONE;
2578    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
2579
2580    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
2581
2582    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil_defaultqueue.wl.gesture.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
2583    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2584    EINA_SAFETY_ON_FALSE_RETURN_VAL(base_data->type == TIZEN_GESTURE_TYPE_PALM_COVER,
2585                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
2586
2587    if (grabbed)
2588       tizen_gesture_grab_palm_cover(_eflutil_defaultqueue.wl.gesture.proto);
2589    else
2590       tizen_gesture_ungrab_palm_cover(_eflutil_defaultqueue.wl.gesture.proto);
2591
2592    ret = _efl_util_gesture_verify_request_notified();
2593
2594    return ret;
2595 }
2596 /* LCOV_EXCL_STOP */
2597
2598 API efl_util_gesture_h
2599 efl_util_gesture_initialize(void)
2600 {
2601    efl_util_gesture_h gesture_h = NULL;
2602
2603    gesture_h = (efl_util_gesture_h)calloc(1, sizeof(struct _efl_util_gesture_h));
2604    if (!gesture_h)
2605      {
2606         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
2607         goto out; /* LCOV_EXCL_LINE */
2608      }
2609
2610    if (_wl_init_default_queue() == (int)EINA_FALSE)
2611      {
2612         set_last_result(EFL_UTIL_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
2613         goto out; /* LCOV_EXCL_LINE */
2614      }
2615
2616    while (!_eflutil_defaultqueue.wl.gesture.proto)
2617      wl_display_dispatch(_eflutil_defaultqueue.wl.dpy); /* LCOV_EXCL_LINE */
2618
2619    if (_eflutil_defaultqueue.wl.gesture.event_init <= 0)
2620      {
2621         if (ecore_event_init() <= 0)
2622           {
2623              set_last_result(EFL_UTIL_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
2624              goto out; /* LCOV_EXCL_LINE */
2625           }
2626         EFL_UTIL_EVENT_GESTURE_EDGE_SWIPE = ecore_event_type_new();
2627         EFL_UTIL_EVENT_GESTURE_EDGE_DRAG = ecore_event_type_new();
2628         EFL_UTIL_EVENT_GESTURE_TAP = ecore_event_type_new();
2629         EFL_UTIL_EVENT_GESTURE_PALM_COVER = ecore_event_type_new();
2630      }
2631    _eflutil_defaultqueue.wl.gesture.event_init++;
2632    gesture_h->init = EINA_TRUE;
2633
2634    set_last_result(EFL_UTIL_ERROR_NONE);
2635    return gesture_h;
2636
2637 out:
2638 /* LCOV_EXCL_START */
2639    if (gesture_h)
2640      {
2641         free(gesture_h);
2642         gesture_h = NULL;
2643      }
2644    return gesture_h;
2645 /* LCOV_EXCL_STOP */
2646 }
2647
2648 API int
2649 efl_util_gesture_deinitialize(efl_util_gesture_h gesture_h)
2650 {
2651    EINA_SAFETY_ON_NULL_RETURN_VAL(gesture_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
2652    EINA_SAFETY_ON_FALSE_RETURN_VAL(gesture_h->init, EFL_UTIL_ERROR_INVALID_PARAMETER);
2653
2654    free(gesture_h);
2655    gesture_h = NULL;
2656
2657    _eflutil_defaultqueue.wl.gesture.event_init--;
2658
2659    if (_eflutil_defaultqueue.wl.gesture.event_init <= 0)
2660      {
2661         _eflutil_defaultqueue.wl.gesture.event_init = 0;
2662         ecore_event_shutdown();
2663         EFL_UTIL_EVENT_GESTURE_EDGE_SWIPE = 0;
2664         EFL_UTIL_EVENT_GESTURE_EDGE_DRAG = 0;
2665         EFL_UTIL_EVENT_GESTURE_TAP = 0;
2666         EFL_UTIL_EVENT_GESTURE_PALM_COVER = 0;
2667      }
2668
2669    return EFL_UTIL_ERROR_NONE;
2670 }
2671
2672 API efl_util_gesture_data
2673 efl_util_gesture_edge_swipe_new(efl_util_gesture_h gesture_h, unsigned int fingers, efl_util_gesture_edge_e edge)
2674 {
2675    Efl_Util_Gesture_Edge_Swipe_Grab_Data *data;
2676
2677    if (!gesture_h || gesture_h->init == EINA_FALSE)
2678      {
2679         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2680         return NULL;
2681      }
2682
2683    if (edge <= EFL_UTIL_GESTURE_EDGE_NONE || edge > EFL_UTIL_GESTURE_EDGE_LEFT)
2684      {
2685         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2686         return NULL;
2687      }
2688
2689    data = (Efl_Util_Gesture_Edge_Swipe_Grab_Data *)calloc(1, sizeof(Efl_Util_Gesture_Edge_Swipe_Grab_Data));
2690    if (!data)
2691      {
2692         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
2693         return NULL; /* LCOV_EXCL_LINE */
2694      }
2695
2696    data->base.type = TIZEN_GESTURE_TYPE_EDGE_SWIPE;
2697    data->fingers = fingers;
2698    data->edge = edge;
2699    data->edge_size = EFL_UTIL_GESTURE_EDGE_SIZE_FULL;
2700
2701    set_last_result(EFL_UTIL_ERROR_NONE);
2702
2703    return (void *)data;
2704 }
2705
2706 API int
2707 efl_util_gesture_edge_swipe_free(efl_util_gesture_h gesture_h, efl_util_gesture_data data)
2708 {
2709    if (!gesture_h || gesture_h->init == EINA_FALSE)
2710      {
2711         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2712      }
2713
2714    if (!data)
2715      {
2716         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2717      }
2718
2719    free(data);
2720    data = NULL;
2721
2722    return EFL_UTIL_ERROR_NONE;
2723 }
2724
2725 API int
2726 efl_util_gesture_edge_swipe_size_set(efl_util_gesture_data data, efl_util_gesture_edge_size_e edge_size, unsigned int start_point, unsigned int end_point)
2727 {
2728    Efl_Util_Gesture_Edge_Swipe_Grab_Data *edge_swipe_data = data;
2729
2730    EINA_SAFETY_ON_NULL_RETURN_VAL(edge_swipe_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2731    EINA_SAFETY_ON_FALSE_RETURN_VAL(edge_swipe_data->base.type == TIZEN_GESTURE_TYPE_EDGE_SWIPE,
2732                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
2733    EINA_SAFETY_ON_FALSE_RETURN_VAL(edge_size == EFL_UTIL_GESTURE_EDGE_SIZE_PARTIAL,
2734                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
2735    EINA_SAFETY_ON_FALSE_RETURN_VAL(end_point > start_point, EFL_UTIL_ERROR_INVALID_PARAMETER);
2736
2737    edge_swipe_data->edge_size = edge_size;
2738    edge_swipe_data->start_point = start_point;
2739    edge_swipe_data->end_point = end_point;
2740
2741    return EFL_UTIL_ERROR_NONE;
2742 }
2743
2744 API efl_util_gesture_data
2745 efl_util_gesture_edge_drag_new(efl_util_gesture_h gesture_h, unsigned int fingers, efl_util_gesture_edge_e edge)
2746 {
2747    Efl_Util_Gesture_Edge_Drag_Grab_Data *data;
2748
2749    if (!gesture_h || gesture_h->init == EINA_FALSE)
2750      {
2751         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2752         return NULL;
2753      }
2754
2755    if (edge <= EFL_UTIL_GESTURE_EDGE_NONE || edge > EFL_UTIL_GESTURE_EDGE_LEFT)
2756      {
2757         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2758         return NULL;
2759      }
2760
2761    data = (Efl_Util_Gesture_Edge_Drag_Grab_Data *)calloc(1, sizeof(Efl_Util_Gesture_Edge_Drag_Grab_Data));
2762    if (!data)
2763      {
2764         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
2765         return NULL; /* LCOV_EXCL_LINE */
2766      }
2767
2768    data->base.type = TIZEN_GESTURE_TYPE_EDGE_DRAG;
2769    data->fingers = fingers;
2770    data->edge = edge;
2771    data->edge_size = EFL_UTIL_GESTURE_EDGE_SIZE_FULL;
2772
2773    set_last_result(EFL_UTIL_ERROR_NONE);
2774
2775    return (void *)data;
2776 }
2777
2778 API int
2779 efl_util_gesture_edge_drag_free(efl_util_gesture_h gesture_h, efl_util_gesture_data data)
2780 {
2781    if (!gesture_h || gesture_h->init == EINA_FALSE)
2782      {
2783         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2784      }
2785
2786    if (!data)
2787      {
2788         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2789      }
2790
2791    free(data);
2792    data = NULL;
2793
2794    return EFL_UTIL_ERROR_NONE;
2795 }
2796
2797 API int
2798 efl_util_gesture_edge_drag_size_set(efl_util_gesture_data data, efl_util_gesture_edge_size_e edge_size, unsigned int start_point, unsigned int end_point)
2799 {
2800    Efl_Util_Gesture_Edge_Drag_Grab_Data *edge_drag_data = data;
2801
2802    EINA_SAFETY_ON_NULL_RETURN_VAL(edge_drag_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2803    EINA_SAFETY_ON_FALSE_RETURN_VAL(edge_drag_data->base.type == TIZEN_GESTURE_TYPE_EDGE_DRAG,
2804                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
2805    EINA_SAFETY_ON_FALSE_RETURN_VAL(edge_size == EFL_UTIL_GESTURE_EDGE_SIZE_PARTIAL,
2806                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
2807    EINA_SAFETY_ON_FALSE_RETURN_VAL(end_point > start_point, EFL_UTIL_ERROR_INVALID_PARAMETER);
2808
2809    edge_drag_data->edge_size = edge_size;
2810    edge_drag_data->start_point = start_point;
2811    edge_drag_data->end_point = end_point;
2812
2813    return EFL_UTIL_ERROR_NONE;
2814 }
2815
2816 API efl_util_gesture_data
2817 efl_util_gesture_tap_new(efl_util_gesture_h gesture_h, unsigned int fingers, unsigned int repeats)
2818 {
2819    Efl_Util_Gesture_Tap_Grab_Data *data;
2820
2821    if (!gesture_h || gesture_h->init == EINA_FALSE)
2822      {
2823         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2824         return NULL;
2825      }
2826
2827    if (fingers <= 1 || repeats <= 1)
2828      {
2829         set_last_result(EFL_UTIL_ERROR_NOT_SUPPORTED);
2830         return NULL;
2831      }
2832
2833    data = (Efl_Util_Gesture_Tap_Grab_Data *)calloc(1, sizeof(Efl_Util_Gesture_Tap_Grab_Data));
2834    if (!data)
2835      {
2836         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
2837         return NULL; /* LCOV_EXCL_LINE */
2838      }
2839
2840    data->base.type = TIZEN_GESTURE_TYPE_TAP;
2841    data->fingers = fingers;
2842    data->repeats = repeats;
2843
2844    set_last_result(EFL_UTIL_ERROR_NONE);
2845
2846    return (void *)data;
2847 }
2848
2849 API int
2850 efl_util_gesture_tap_free(efl_util_gesture_h gesture_h, efl_util_gesture_data data)
2851 {
2852    if (!gesture_h || gesture_h->init == EINA_FALSE)
2853      {
2854         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2855      }
2856
2857    if (!data)
2858      {
2859         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2860      }
2861
2862    free(data);
2863    data = NULL;
2864
2865    return EFL_UTIL_ERROR_NONE;
2866 }
2867
2868 API efl_util_gesture_data
2869 efl_util_gesture_palm_cover_new(efl_util_gesture_h gesture_h)
2870 {
2871    Efl_Util_Gesture_Palm_Cover_Grab_Data *data;
2872
2873    if (!gesture_h || gesture_h->init == EINA_FALSE)
2874      {
2875         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
2876         return NULL;
2877      }
2878
2879    data = (Efl_Util_Gesture_Palm_Cover_Grab_Data *)calloc(1, sizeof(Efl_Util_Gesture_Palm_Cover_Grab_Data));
2880    if (!data)
2881      {
2882         set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
2883         return NULL; /* LCOV_EXCL_LINE */
2884      }
2885
2886    data->base.type = TIZEN_GESTURE_TYPE_PALM_COVER;
2887
2888    set_last_result(EFL_UTIL_ERROR_NONE);
2889
2890    return (void *)data;
2891 }
2892
2893 API int
2894 efl_util_gesture_palm_cover_free(efl_util_gesture_h gesture_h, efl_util_gesture_data data)
2895 {
2896    if (!gesture_h || gesture_h->init == EINA_FALSE)
2897      {
2898         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2899      }
2900
2901    if (!data)
2902      {
2903         return EFL_UTIL_ERROR_INVALID_PARAMETER;
2904      }
2905
2906    free(data);
2907    data = NULL;
2908
2909    return EFL_UTIL_ERROR_NONE;
2910 }
2911
2912 /* LCOV_EXCL_START */
2913 API int
2914 efl_util_gesture_grab(efl_util_gesture_h gesture_h, efl_util_gesture_data data)
2915 {
2916    int ret = EFL_UTIL_ERROR_NONE;
2917    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
2918
2919    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
2920
2921    EINA_SAFETY_ON_NULL_RETURN_VAL(gesture_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
2922    EINA_SAFETY_ON_FALSE_RETURN_VAL(gesture_h->init, EFL_UTIL_ERROR_INVALID_PARAMETER);
2923    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2924
2925    switch (base_data->type)
2926      {
2927         case TIZEN_GESTURE_TYPE_EDGE_SWIPE:
2928            ret = _efl_util_gesture_grab_edge_swipe(data, EINA_TRUE);
2929            break;
2930         case TIZEN_GESTURE_TYPE_EDGE_DRAG:
2931            ret = _efl_util_gesture_grab_edge_drag(data, EINA_TRUE);
2932            break;
2933         case TIZEN_GESTURE_TYPE_TAP:
2934            ret = _efl_util_gesture_grab_tap(data, EINA_TRUE);
2935            break;
2936         case TIZEN_GESTURE_TYPE_PALM_COVER:
2937            ret = _efl_util_gesture_grab_palm_cover(data, EINA_TRUE);
2938            break;
2939         default:
2940            return EFL_UTIL_ERROR_INVALID_PARAMETER;
2941      }
2942
2943    return ret;
2944 }
2945
2946 API int
2947 efl_util_gesture_ungrab(efl_util_gesture_h gesture_h, efl_util_gesture_data data)
2948 {
2949    int ret = EFL_UTIL_ERROR_NONE;
2950    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
2951
2952    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
2953
2954    EINA_SAFETY_ON_NULL_RETURN_VAL(gesture_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
2955    EINA_SAFETY_ON_FALSE_RETURN_VAL(gesture_h->init, EFL_UTIL_ERROR_INVALID_PARAMETER);
2956    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2957
2958    switch (base_data->type)
2959      {
2960         case TIZEN_GESTURE_TYPE_EDGE_SWIPE:
2961            ret = _efl_util_gesture_grab_edge_swipe(data, EINA_FALSE);
2962            break;
2963         case TIZEN_GESTURE_TYPE_EDGE_DRAG:
2964            ret = _efl_util_gesture_grab_edge_drag(data, EINA_FALSE);
2965            break;
2966         case TIZEN_GESTURE_TYPE_TAP:
2967            ret = _efl_util_gesture_grab_tap(data, EINA_FALSE);
2968            break;
2969         case TIZEN_GESTURE_TYPE_PALM_COVER:
2970            ret = _efl_util_gesture_grab_palm_cover(data, EINA_FALSE);
2971            break;
2972         default:
2973            return EFL_UTIL_ERROR_INVALID_PARAMETER;
2974      }
2975
2976    return ret;
2977 }
2978 /* LCOV_EXCL_STOP */
2979
2980 API int
2981 efl_util_gesture_select(efl_util_gesture_h gesture_h, Evas_Object *window, efl_util_gesture_data data)
2982 {
2983    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
2984    Ecore_Wl2_Window *wlwin;
2985    struct wl_surface *surface;
2986    int ret;
2987
2988    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
2989
2990    EINA_SAFETY_ON_NULL_RETURN_VAL(gesture_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
2991    EINA_SAFETY_ON_FALSE_RETURN_VAL(gesture_h->init, EFL_UTIL_ERROR_INVALID_PARAMETER);
2992    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
2993    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
2994
2995    if (base_data->type != TIZEN_GESTURE_TYPE_PALM_COVER)
2996      return EFL_UTIL_ERROR_NOT_SUPPORTED;
2997
2998    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
2999    if (!wlwin) return EFL_UTIL_ERROR_INVALID_PARAMETER;
3000
3001    surface = ecore_wl2_window_surface_get(wlwin);
3002    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
3003                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
3004
3005    tizen_gesture_select_palm_cover(_eflutil_defaultqueue.wl.gesture.proto, surface);
3006
3007    ret = _efl_util_gesture_verify_request_notified();
3008
3009    return ret;
3010 }
3011
3012 API int
3013 efl_util_gesture_deselect(efl_util_gesture_h gesture_h, Evas_Object *window, efl_util_gesture_data data)
3014 {
3015    Efl_Util_Gesture_Common_Grab_Data *base_data = NULL;
3016    Ecore_Wl2_Window *wlwin;
3017    struct wl_surface *surface;
3018    int ret;
3019
3020    base_data = (Efl_Util_Gesture_Common_Grab_Data *)data;
3021
3022    EINA_SAFETY_ON_NULL_RETURN_VAL(gesture_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
3023    EINA_SAFETY_ON_FALSE_RETURN_VAL(gesture_h->init, EFL_UTIL_ERROR_INVALID_PARAMETER);
3024    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
3025    EINA_SAFETY_ON_NULL_RETURN_VAL(base_data, EFL_UTIL_ERROR_INVALID_PARAMETER);
3026
3027    if (base_data->type != TIZEN_GESTURE_TYPE_PALM_COVER)
3028      return EFL_UTIL_ERROR_NOT_SUPPORTED;
3029
3030    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
3031    if (!wlwin) return EFL_UTIL_ERROR_INVALID_PARAMETER;
3032
3033    surface = ecore_wl2_window_surface_get(wlwin);
3034    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
3035                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
3036
3037    tizen_gesture_deselect_palm_cover(_eflutil_defaultqueue.wl.gesture.proto, surface);
3038
3039    ret = _efl_util_gesture_verify_request_notified();
3040
3041    return ret;
3042 }
3043
3044 /* LCOV_EXCL_START */
3045 API int
3046 efl_util_gesture_activate_set(efl_util_gesture_h gesture_h, unsigned int type, Eina_Bool active)
3047 {
3048    int ret;
3049
3050    EINA_SAFETY_ON_NULL_RETURN_VAL(gesture_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
3051    EINA_SAFETY_ON_FALSE_RETURN_VAL(gesture_h->init, EFL_UTIL_ERROR_INVALID_PARAMETER);
3052    EINA_SAFETY_ON_TRUE_RETURN_VAL(type == EFL_UTIL_GESTURE_TYPE_NONE, EFL_UTIL_ERROR_INVALID_PARAMETER);
3053
3054    tizen_gesture_activate_set(_eflutil_defaultqueue.wl.gesture.proto, NULL, type, active);
3055
3056    ret = _efl_util_gesture_verify_request_notified();
3057
3058    return ret;
3059 }
3060 /* LCOV_EXCL_STOP */
3061
3062 API int
3063 efl_util_gesture_activate_set_on_window(efl_util_gesture_h gesture_h, Evas_Object *window, unsigned int type, Eina_Bool active)
3064 {
3065    Ecore_Wl2_Window *wlwin;
3066    struct wl_surface *surface;
3067    int ret;
3068
3069    EINA_SAFETY_ON_NULL_RETURN_VAL(gesture_h, EFL_UTIL_ERROR_INVALID_PARAMETER);
3070    EINA_SAFETY_ON_FALSE_RETURN_VAL(gesture_h->init, EFL_UTIL_ERROR_INVALID_PARAMETER);
3071    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
3072    EINA_SAFETY_ON_TRUE_RETURN_VAL(type == EFL_UTIL_GESTURE_TYPE_NONE, EFL_UTIL_ERROR_INVALID_PARAMETER);
3073
3074    wlwin = (Ecore_Wl2_Window *)elm_win_wl_window_get(window);
3075    if (!wlwin) return EFL_UTIL_ERROR_INVALID_PARAMETER;
3076
3077    surface = ecore_wl2_window_surface_get(wlwin);
3078    EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
3079                                   EFL_UTIL_ERROR_INVALID_PARAMETER);
3080
3081    tizen_gesture_activate_set(_eflutil_defaultqueue.wl.gesture.proto, surface, type, active);
3082
3083    ret = _efl_util_gesture_verify_request_notified();
3084
3085    return ret;
3086 }