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