fix failed to get window notification level before a shell surface was created.
[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 #if X11
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xvlib.h>
37 #include <X11/extensions/Xvproto.h>
38 #include <X11/extensions/Xdamage.h>
39 #include <dri2.h>
40 #include <Ecore_X.h>
41 #include <utilX.h>
42 #endif /* end of X11 */
43
44 #if WAYLAND
45 #include <Ecore_Wayland.h>
46 #include <wayland-client.h>
47 #include <wayland-tbm-client.h>
48 #include <tizen-extension-client-protocol.h>
49 #include <screenshooter-client-protocol.h>
50 #endif /* end of WAYLAND */
51
52 /* callback handler index */
53 #define CBH_NOTI_LEV 0
54 #define CBH_SCR_MODE 1
55 #define CBH_MAX      2
56
57 typedef void (*Efl_Util_Cb)(Evas_Object *, int, void *);
58
59 typedef struct _Efl_Util_Callback_Info
60 {
61    Evas_Object *win;
62    Efl_Util_Cb cb;
63    void *data;
64 } Efl_Util_Callback_Info;
65
66 #if WAYLAND
67 typedef struct _Efl_Util_Wl_Surface_Lv_Info
68 {
69    void *surface; /* wl_surface */
70    int level;
71    Eina_Bool wait_for_done;
72 } Efl_Util_Wl_Surface_Lv_Info;
73
74 typedef struct _Efl_Util_Wl_Surface_Scr_Mode_Info
75 {
76    void *surface; /* wl_surface */
77    unsigned int mode;
78    Eina_Bool wait_for_done;
79 } Efl_Util_Wl_Surface_Scr_Mode_Info;
80
81 typedef struct _Efl_Util_Wl_Output_Info
82 {
83     struct wl_output *output;
84     int offset_x, offset_y, width, height;
85 } Efl_Util_Wl_Output_Info;
86 #endif
87
88 typedef struct _Efl_Util_Data
89 {
90 #if X11
91    /* x11 related stuffs */
92    struct
93    {
94       Eina_Bool init;
95       Ecore_Event_Handler *handler; /* x11 client message handler */
96       Ecore_X_Display *dpy;
97    } x11;
98 #endif /* end of X11 */
99
100 #if WAYLAND
101    /* wayland related stuffs */
102    struct
103    {
104       Eina_Bool init;
105       struct wl_display *dpy;
106       struct wl_event_queue *queue;
107
108       struct
109       {
110          struct tizen_policy *proto;
111          Eina_Hash *hash_noti_lv;
112          Eina_Hash *hash_scr_mode;
113       } policy;
114       struct
115       {
116          struct screenshooter *screenshooter;
117          struct wayland_tbm_client *tbm_client;
118          Eina_List *output_list;
119       } shot;
120    } wl;
121 #endif /* end of WAYLAND */
122
123    struct
124    {
125       Eina_List *info_list; /* list of callback info */
126       unsigned int atom; /* x11 atom */
127    } cb_handler[CBH_MAX];
128 } Efl_Util_Data;
129
130 static Efl_Util_Data _eflutil =
131 {
132 #if X11
133    {
134       EINA_FALSE,
135       NULL,
136       NULL
137    },
138 #endif /* end of X11 */
139 #if WAYLAND
140    {
141       EINA_FALSE,
142       NULL, NULL,
143       { NULL, NULL, NULL }, /* tizen_policy protocol */
144       { NULL, NULL, NULL }  /* screenshooter protocol */
145    },
146 #endif /* end of WAYLAND */
147    {
148       { NULL, 0 }, /* handler for notification level */
149       { NULL, 0 }  /* handler for screen mode */
150    },
151 };
152
153 static Eina_Bool               _cb_info_add(Evas_Object *win, Efl_Util_Cb cb, void *data, int idx);
154 static Eina_Bool               _cb_info_del_by_win(Evas_Object *win, int idx);
155 static Eina_List              *_cb_info_list_get(int idx);
156 static Efl_Util_Callback_Info *_cb_info_find_by_win(Evas_Object *win, int idx);
157 #if X11
158 static Efl_Util_Callback_Info *_cb_info_find_by_xwin(unsigned int xwin, int idx);
159 static Eina_Bool               _cb_x11_client_msg(void *data, int type, void *event);
160 static Eina_Bool               _x11_init(void);
161 #endif /* end of X11 */
162 #if WAYLAND
163 static Eina_Bool               _wl_init(void);
164 static void                    _cb_wl_reg_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
165 static void                    _cb_wl_reg_global_remove(void *data, struct wl_registry *reg, unsigned int name);
166 static Efl_Util_Callback_Info *_cb_info_find_by_wlsurf(void *wlsurf, int idx);
167 static void                    _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t is_conformant);
168 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);
169 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);
170 static void                    _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id);
171 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);
172
173 static const struct wl_registry_listener _wl_reg_listener =
174 {
175    _cb_wl_reg_global,
176    _cb_wl_reg_global_remove
177 };
178
179 struct tizen_policy_listener _wl_tz_policy_listener =
180 {
181    _cb_wl_tz_policy_conformant,
182    _cb_wl_tz_policy_conformant_area,
183    _cb_wl_tz_policy_notification_done,
184    _cb_wl_tz_policy_transient_for_done,
185    _cb_wl_tz_policy_scr_mode_done
186 };
187 #endif /* end of WAYLAND */
188
189 static Eina_Bool
190 _cb_info_add(Evas_Object *win,
191              Efl_Util_Cb cb,
192              void *data,
193              int idx)
194 {
195    Efl_Util_Callback_Info *info;
196
197    info = _cb_info_find_by_win(win, idx);
198    if (info)
199      {
200         _eflutil.cb_handler[idx].info_list
201            = eina_list_remove(_eflutil.cb_handler[idx].info_list,
202                               info);
203         free(info);
204      }
205
206    info = (Efl_Util_Callback_Info *)calloc(1, sizeof(Efl_Util_Callback_Info));
207    if (!info) return EINA_FALSE;
208
209    info->win = win;
210    info->cb = cb;
211    info->data = data;
212
213    _eflutil.cb_handler[idx].info_list
214       = eina_list_append(_eflutil.cb_handler[idx].info_list,
215                          info);
216
217 #if X11
218    if (!_eflutil.x11.handler)
219      _eflutil.x11.handler = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
220                                                     _cb_x11_client_msg,
221                                                     NULL);
222 #endif /* end of X11 */
223
224    return EINA_TRUE;
225 }
226
227 static Eina_Bool
228 _cb_info_del_by_win(Evas_Object *win,
229                     int idx)
230 {
231    Efl_Util_Callback_Info *info;
232
233    info = _cb_info_find_by_win(win, idx);
234    if (!info) return EINA_FALSE;
235
236    _eflutil.cb_handler[idx].info_list
237       = eina_list_remove(_eflutil.cb_handler[idx].info_list,
238                          info);
239    free(info);
240
241 #if X11
242    unsigned int count = eina_list_count(_eflutil.cb_handler[idx].info_list);
243    if ((count == 0) && (_eflutil.x11.handler))
244      {
245         ecore_event_handler_del(_eflutil.x11.handler);
246         _eflutil.x11.handler = NULL;
247      }
248 #endif
249
250    return EINA_TRUE;
251 }
252
253 static Eina_List *
254 _cb_info_list_get(int idx)
255 {
256    return _eflutil.cb_handler[idx].info_list;
257 }
258
259 static Efl_Util_Callback_Info *
260 _cb_info_find_by_win(Evas_Object *win,
261                      int idx)
262 {
263    Eina_List *l, *ll;
264    Efl_Util_Callback_Info *info;
265
266    l = _cb_info_list_get(idx);
267    EINA_LIST_FOREACH(l, ll, info)
268      {
269         if (info->win == win) return info;
270      }
271
272    return NULL;
273 }
274
275 #if X11
276 static Efl_Util_Callback_Info *
277 _cb_info_find_by_xwin(unsigned int xwin,
278                       int idx)
279 {
280    Eina_List *l, *ll;
281    Efl_Util_Callback_Info *info;
282    unsigned int xwin2;
283
284    l = _cb_info_list_get(idx);
285    EINA_LIST_FOREACH(l, ll, info)
286      {
287         xwin2 = elm_win_xwindow_get(info->win);
288         if (xwin == xwin2) return info;
289      }
290
291    return NULL;
292 }
293
294 static Eina_Bool
295 _cb_x11_client_msg(void *data,
296                    int type,
297                    void *event)
298 {
299    Ecore_X_Event_Client_Message *ev;
300    Ecore_X_Window xwin;
301    Efl_Util_Callback_Info *info;
302
303    ev = event;
304    if (!ev) return ECORE_CALLBACK_PASS_ON;
305
306    xwin = ev->win;
307    if (xwin == 0) return ECORE_CALLBACK_PASS_ON;
308
309    if (ev->message_type == _eflutil.cb_handler[CBH_NOTI_LEV].atom)
310      {
311         info = _cb_info_find_by_xwin(xwin, CBH_NOTI_LEV);
312
313         /* permission denied */
314         if ((ev->data.l[1] == 0) &&
315             (info) &&
316             (info->cb))
317           {
318              info->cb(info->win,
319                       EFL_UTIL_ERROR_PERMISSION_DENIED,
320                       info->data);
321           }
322      }
323    else if (ev->message_type == _eflutil.cb_handler[CBH_SCR_MODE].atom)
324      {
325         info = _cb_info_find_by_xwin(xwin, CBH_SCR_MODE);
326
327         /* permission denied */
328         if ((ev->data.l[1] == 0) &&
329             (info) &&
330             (info->cb))
331           {
332              info->cb(info->win,
333                       EFL_UTIL_ERROR_PERMISSION_DENIED,
334                       info->data);
335           }
336      }
337    return ECORE_CALLBACK_PASS_ON;
338 }
339
340 static Eina_Bool
341 _x11_init(void)
342 {
343    if (_eflutil.x11.init) return EINA_TRUE;
344
345    _eflutil.x11.dpy = ecore_x_display_get();
346    EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.x11.dpy, EINA_FALSE);
347
348    _eflutil.x11.init = EINA_TRUE;
349
350    return EINA_TRUE;
351 }
352 #endif /* end of X11 */
353
354 #if WAYLAND
355 static Eina_Bool
356 _wl_init(void)
357 {
358    struct wl_registry *reg = NULL;
359
360    if (_eflutil.wl.init) return EINA_TRUE;
361
362    ecore_wl_init(NULL);
363
364    _eflutil.wl.dpy = ecore_wl_display_get();
365    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.dpy, fail);
366
367    _eflutil.wl.queue = wl_display_create_queue(_eflutil.wl.dpy);
368    EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.queue, fail);
369
370    reg = wl_display_get_registry(_eflutil.wl.dpy);
371    EINA_SAFETY_ON_NULL_GOTO(reg, fail);
372
373    wl_proxy_set_queue((struct wl_proxy*)reg, _eflutil.wl.queue);
374    wl_registry_add_listener(reg, &_wl_reg_listener, NULL);
375
376    _eflutil.wl.init = EINA_TRUE;
377
378    return EINA_TRUE;
379 fail:
380    if (_eflutil.wl.queue)
381      {
382         wl_event_queue_destroy(_eflutil.wl.queue);
383         _eflutil.wl.queue = NULL;
384      }
385
386    if (reg)
387      wl_registry_destroy(reg);
388    ecore_wl_shutdown();
389    return EINA_FALSE;
390 }
391
392 static void
393 _cb_wl_output_geometry(void *data, struct wl_output *wl_output, int x, int y,
394                        int physical_width, int physical_height, int subpixel,
395                        const char *make, const char *model, int transform)
396 {
397    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
398    if (wl_output == output->output)
399      {
400         output->offset_x = x;
401         output->offset_y = y;
402      }
403 }
404
405 static void
406 _cb_wl_output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
407                    int width, int height, int refresh)
408 {
409    Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
410    if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT))
411      {
412         output->width = width;
413         output->height = height;
414      }
415 }
416
417 static void
418 _cb_wl_output_done(void *data, struct wl_output *wl_output)
419 {
420 }
421
422 static void
423 _cb_wl_output_scale(void *data, struct wl_output *wl_output, int32_t factor)
424 {
425 }
426
427 static const struct wl_output_listener output_listener =
428 {
429     _cb_wl_output_geometry,
430     _cb_wl_output_mode,
431     _cb_wl_output_done,
432     _cb_wl_output_scale
433 };
434
435 static void
436 _cb_wl_screenshot_done(void *data, struct screenshooter *screenshooter)
437 {
438    Eina_Bool *shot_done = (Eina_Bool*)data;
439    if (shot_done)
440      *shot_done = EINA_TRUE;
441 }
442
443 static const struct screenshooter_listener screenshooter_listener =
444 {
445     _cb_wl_screenshot_done
446 };
447
448 static void
449 _cb_wl_reg_global(void *data,
450                   struct wl_registry *reg,
451                   unsigned int name,
452                   const char *interface,
453                   unsigned int version)
454 {
455    if (!strcmp(interface, "tizen_policy"))
456      {
457         struct tizen_policy *proto;
458         proto = wl_registry_bind(reg,
459                                   name,
460                                   &tizen_policy_interface,
461                                   1);
462         if (!proto) return;
463
464         tizen_policy_add_listener(proto,
465                                   &_wl_tz_policy_listener,
466                                   NULL);
467
468         _eflutil.wl.policy.hash_noti_lv = eina_hash_pointer_new(free);
469         _eflutil.wl.policy.hash_scr_mode = eina_hash_pointer_new(free);
470         _eflutil.wl.policy.proto = proto;
471      }
472    else if (strcmp(interface, "wl_output") == 0)
473      {
474         Efl_Util_Wl_Output_Info *output = calloc(1, sizeof(Efl_Util_Wl_Output_Info));
475         EINA_SAFETY_ON_NULL_RETURN(output);
476
477         _eflutil.wl.shot.output_list = eina_list_append(_eflutil.wl.shot.output_list, output);
478
479         output->output = wl_registry_bind(reg, name, &wl_output_interface, version);
480         wl_output_add_listener(output->output, &output_listener, output);
481      }
482    else if (strcmp(interface, "screenshooter") == 0)
483      {
484         _eflutil.wl.shot.screenshooter = wl_registry_bind(reg, name, &screenshooter_interface, version);
485         screenshooter_add_listener(_eflutil.wl.shot.screenshooter, &screenshooter_listener, NULL);
486      }
487 }
488
489 static void
490 _cb_wl_reg_global_remove(void *data,
491                          struct wl_registry *reg,
492                          unsigned int name)
493 {
494    _eflutil.wl.policy.proto = NULL;
495    eina_hash_free(_eflutil.wl.policy.hash_noti_lv);
496    eina_hash_free(_eflutil.wl.policy.hash_scr_mode);
497 }
498
499 static Efl_Util_Callback_Info *
500 _cb_info_find_by_wlsurf(void *wlsurf,
501                         int idx)
502 {
503    Eina_List *l, *ll;
504    Efl_Util_Callback_Info *info;
505    Ecore_Wl_Window *wlwin2 = NULL;
506    void *wlsurf2 = NULL;
507
508    l = _cb_info_list_get(idx);
509    EINA_LIST_FOREACH(l, ll, info)
510      {
511         wlwin2 = elm_win_wl_window_get(info->win);
512         wlsurf2 = ecore_wl_window_surface_get(wlwin2);
513         if (wlsurf== wlsurf2) return info;
514      }
515
516    return NULL;
517 }
518
519 static void
520 _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy,
521                             struct wl_surface *surface, uint32_t is_conformant)
522 {
523 }
524
525 static void
526 _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy,
527                                  struct wl_surface *surface, uint32_t conformant_part,
528                                  uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h)
529 {
530 }
531
532 static void
533 _cb_wl_tz_policy_notification_done(void *data,
534                                    struct tizen_policy *tizen_policy,
535                                    struct wl_surface *surface,
536                                    int32_t level,
537                                    uint32_t state)
538 {
539    Efl_Util_Wl_Surface_Lv_Info *lv_info;
540    Efl_Util_Callback_Info *cb_info;
541
542    lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
543    if (lv_info)
544      {
545         lv_info->level = level;
546         lv_info->wait_for_done = EINA_FALSE;
547      }
548
549    if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
550
551    cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_NOTI_LEV);
552    if (!cb_info) return;
553    if (!cb_info->cb) return;
554
555    cb_info->cb(cb_info->win,
556                EFL_UTIL_ERROR_PERMISSION_DENIED,
557                cb_info->data);
558 }
559
560 static void
561 _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id)
562 {
563 }
564
565 static void
566 _cb_wl_tz_policy_scr_mode_done(void *data,
567                                struct tizen_policy *tizen_policy,
568                                struct wl_surface *surface,
569                                uint32_t mode,
570                                uint32_t state)
571 {
572
573    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
574    Efl_Util_Callback_Info *cb_info;
575
576    scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
577    if (scr_mode_info)
578      {
579         scr_mode_info->mode = mode;
580         scr_mode_info->wait_for_done = EINA_FALSE;
581      }
582
583    if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
584
585    cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_SCR_MODE);
586    if (!cb_info) return;
587    if (!cb_info->cb) return;
588
589    cb_info->cb(cb_info->win,
590                EFL_UTIL_ERROR_PERMISSION_DENIED,
591                cb_info->data);
592 }
593 #endif /* end of WAYLAND */
594
595 API int
596 efl_util_set_notification_window_level(Evas_Object *window,
597                                        efl_util_notification_level_e level)
598 {
599    Eina_Bool res;
600
601    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
602    EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_1) &&
603                                    (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
604                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
605
606 #if X11
607    Ecore_X_Window_Type window_type;
608    Ecore_X_Window xwin;
609
610    res = _x11_init();
611    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
612
613    xwin = elm_win_xwindow_get(window);
614    if (xwin)
615      {
616         if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
617           {
618              // success to get window type
619              if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
620                {
621                   // given EFL window's type is not notification type.
622                   return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
623                }
624           }
625         else
626           return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
627
628         utilx_set_system_notification_level(_eflutil.x11.dpy,
629                                             xwin,
630                                             level);
631         return EFL_UTIL_ERROR_NONE;
632      }
633 #endif /* end of X11 */
634
635 #if WAYLAND
636    Elm_Win_Type type;
637    Ecore_Wl_Window *wlwin;
638    struct wl_surface *surface;
639    Efl_Util_Wl_Surface_Lv_Info *lv_info;
640
641    res = _wl_init();
642    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
643
644    type = elm_win_type_get(window);
645    EINA_SAFETY_ON_FALSE_RETURN_VAL((type == ELM_WIN_NOTIFICATION),
646                                    EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
647
648    wlwin = elm_win_wl_window_get(window);
649    if (wlwin)
650      {
651         while (!_eflutil.wl.policy.proto)
652           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
653
654         surface = ecore_wl_window_surface_get(wlwin);
655         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
656                                        EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
657
658         lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
659         if (!lv_info)
660           {
661              lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
662              EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
663
664              lv_info->surface = surface;
665              lv_info->level = (int)level;
666              lv_info->wait_for_done = EINA_TRUE;
667
668              eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
669                            &surface,
670                            lv_info);
671           }
672         else
673           {
674              lv_info->level = (int)level;
675              lv_info->wait_for_done = EINA_TRUE;
676           }
677
678         tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
679                                             surface, (int)level);
680
681         return EFL_UTIL_ERROR_NONE;
682      }
683 #endif /* end of WAYLAND */
684
685    return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
686 }
687
688 API int
689 efl_util_get_notification_window_level(Evas_Object *window,
690                                        efl_util_notification_level_e *level)
691 {
692    Eina_Bool res;
693
694    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
695    EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
696
697 #if X11
698    Ecore_X_Window_Type window_type;
699    Utilx_Notification_Level utilx_level;
700    Ecore_X_Window xwin;
701
702    res = _x11_init();
703    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
704
705    xwin = elm_win_xwindow_get(window);
706    if (xwin)
707      {
708         if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
709           {
710              // success to get window type
711              if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
712                {
713                   // given EFL window's type is not notification type.
714                   return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
715                }
716
717              utilx_level = utilx_get_system_notification_level(_eflutil.x11.dpy, xwin);
718              if (utilx_level == UTILX_NOTIFICATION_LEVEL_LOW)
719                *level = EFL_UTIL_NOTIFICATION_LEVEL_1;
720              else if(utilx_level == UTILX_NOTIFICATION_LEVEL_NORMAL)
721                *level = EFL_UTIL_NOTIFICATION_LEVEL_2;
722              else if(utilx_level == UTILX_NOTIFICATION_LEVEL_HIGH)
723                *level = EFL_UTIL_NOTIFICATION_LEVEL_3;
724              else
725                return EFL_UTIL_ERROR_INVALID_PARAMETER;
726           }
727         else
728           return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
729
730         return EFL_UTIL_ERROR_NONE;
731      }
732 #endif /* end of X11 */
733
734 #if WAYLAND
735    Elm_Win_Type type;
736    Ecore_Wl_Window *wlwin;
737    struct wl_surface *surface;
738    Efl_Util_Wl_Surface_Lv_Info *lv_info;
739
740    res = _wl_init();
741    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
742
743    type = elm_win_type_get(window);
744    EINA_SAFETY_ON_FALSE_RETURN_VAL((type == ELM_WIN_NOTIFICATION),
745                                    EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
746
747    wlwin = elm_win_wl_window_get(window);
748    if (wlwin)
749      {
750         while (!_eflutil.wl.policy.proto)
751           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
752
753         surface = ecore_wl_window_surface_get(wlwin);
754         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
755                                        EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
756
757         lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
758         if (lv_info)
759           {
760              if (lv_info->wait_for_done)
761                {
762                   int count = 0;
763                   while ((lv_info->wait_for_done) && (count < 3))
764                     {
765                        ecore_wl_flush();
766                        wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
767                        count++;
768                     }
769
770                   if (lv_info->wait_for_done)
771                     {
772                        *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
773                        return EFL_UTIL_ERROR_INVALID_PARAMETER;
774                     }
775                }
776
777              switch (lv_info->level)
778                {
779                 case TIZEN_POLICY_LEVEL_1:       *level = EFL_UTIL_NOTIFICATION_LEVEL_1;       break;
780                 case TIZEN_POLICY_LEVEL_2:       *level = EFL_UTIL_NOTIFICATION_LEVEL_2;       break;
781                 case TIZEN_POLICY_LEVEL_3:       *level = EFL_UTIL_NOTIFICATION_LEVEL_3;       break;
782                 case TIZEN_POLICY_LEVEL_NONE:    *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE;    break;
783                 case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
784                 case TIZEN_POLICY_LEVEL_MEDIUM:  *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM;  break;
785                 case TIZEN_POLICY_LEVEL_HIGH:    *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH;    break;
786                 case TIZEN_POLICY_LEVEL_TOP:     *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP;     break;
787                 default:                               *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
788                                                        return EFL_UTIL_ERROR_INVALID_PARAMETER;
789                }
790              return EFL_UTIL_ERROR_NONE;
791           }
792         else
793           *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
794
795         return EFL_UTIL_ERROR_NONE;
796      }
797 #endif /* end of WAYLAND */
798    return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
799 }
800
801 API int
802 efl_util_set_notification_window_level_error_cb(Evas_Object *window,
803                                                 efl_util_notification_window_level_error_cb callback,
804                                                 void *user_data)
805 {
806    Eina_Bool ret = EINA_FALSE;
807
808    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
809    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
810
811    ret = _cb_info_add(window,
812                       (Efl_Util_Cb)callback,
813                       user_data,
814                       CBH_NOTI_LEV);
815    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
816
817 #if X11
818    if (!_eflutil.cb_handler[CBH_NOTI_LEV].atom)
819      _eflutil.cb_handler[CBH_NOTI_LEV].atom = ecore_x_atom_get("_E_NOTIFICATION_LEVEL_ACCESS_RESULT");
820 #endif /* end of X11 */
821
822    return EFL_UTIL_ERROR_NONE;
823 }
824
825 API int
826 efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
827 {
828    Eina_Bool ret = EINA_FALSE;
829
830    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
831
832    ret = _cb_info_del_by_win(window, CBH_NOTI_LEV);
833    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
834
835    return EFL_UTIL_ERROR_NONE;
836 }
837
838 API int
839 efl_util_set_window_opaque_state(Evas_Object *window,
840                                  int opaque)
841 {
842    Eina_Bool res;
843
844    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
845    EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
846                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
847
848 #if X11
849    Ecore_X_Window xwin;
850    Utilx_Opaque_State state;
851    int ret;
852
853    res = _x11_init();
854    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
855
856    xwin = elm_win_xwindow_get(window);
857    EINA_SAFETY_ON_FALSE_RETURN_VAL(xwin > 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
858
859    if (opaque)
860      state = UTILX_OPAQUE_STATE_ON;
861    else
862      state = UTILX_OPAQUE_STATE_OFF;
863
864    ret = utilx_set_window_opaque_state(_eflutil.x11.dpy,
865                                        xwin,
866                                        state);
867
868    if (!ret)
869      return EFL_UTIL_ERROR_INVALID_PARAMETER;
870    else
871      return EFL_UTIL_ERROR_NONE;
872 #endif /* end of X11 */
873
874 #if WAYLAND
875    Ecore_Wl_Window *wlwin;
876    struct wl_surface *surface;
877
878    if (!_eflutil.wl.policy.proto)
879      {
880         int ret = 0;
881
882         res = _wl_init();
883         EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
884
885         while (!_eflutil.wl.policy.proto && ret != -1)
886           ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
887
888         EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.policy.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
889      }
890
891    wlwin = elm_win_wl_window_get(window);
892    if (!wlwin)
893       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
894
895    surface  = ecore_wl_window_surface_get(wlwin);
896    if (!surface)
897       return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
898
899    tizen_policy_set_opaque_state(_eflutil.wl.policy.proto, surface, opaque);
900
901    return EFL_UTIL_ERROR_NONE;
902 #endif /* end of WAYLAND */
903 }
904
905 API int
906 efl_util_set_window_screen_mode(Evas_Object *window,
907                                 efl_util_screen_mode_e mode)
908 {
909    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
910    EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
911                                     (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
912                                    EFL_UTIL_ERROR_INVALID_PARAMETER);
913
914 #if X11
915    Evas *e;
916    Ecore_Evas *ee;
917    int id;
918
919    e = evas_object_evas_get(window);
920    EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
921
922    ee = ecore_evas_ecore_evas_get(e);
923    EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
924
925    id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
926    if (mode == EFL_UTIL_SCREEN_MODE_ALWAYS_ON)
927      {
928         if (id == -1)
929           ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "1");
930         else
931           ecore_evas_aux_hint_val_set(ee, id, "1");
932      }
933    else if (mode == EFL_UTIL_SCREEN_MODE_DEFAULT)
934      {
935         if (id == -1)
936           ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "0");
937         else
938           ecore_evas_aux_hint_val_set(ee, id, "0");
939      }
940    else
941      return EFL_UTIL_ERROR_INVALID_PARAMETER;
942
943    return EFL_UTIL_ERROR_NONE;
944 #endif /* end of X11 */
945
946 #if WAYLAND
947    Ecore_Wl_Window *wlwin;
948    struct wl_surface *surface;
949    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
950    Eina_Bool res;
951
952    res = _wl_init();
953    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
954
955    wlwin = elm_win_wl_window_get(window);
956    if (wlwin)
957      {
958         while (!_eflutil.wl.policy.proto)
959           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
960
961         surface = ecore_wl_window_surface_get(wlwin);
962         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
963                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
964
965         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
966         if (!scr_mode_info)
967           {
968              scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
969              EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
970
971              scr_mode_info->surface = surface;
972              scr_mode_info->mode = (unsigned int)mode;
973              scr_mode_info->wait_for_done = EINA_TRUE;
974
975              eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
976                            &surface,
977                            scr_mode_info);
978           }
979         else
980           {
981              scr_mode_info->mode = (unsigned int)mode;
982              scr_mode_info->wait_for_done = EINA_TRUE;
983           }
984
985         tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
986                                             surface, (unsigned int)mode);
987
988         return EFL_UTIL_ERROR_NONE;
989      }
990    else
991      return EFL_UTIL_ERROR_INVALID_PARAMETER;
992 #endif /* end of WAYLAND */
993 }
994
995 API int
996 efl_util_get_window_screen_mode(Evas_Object *window,
997                                 efl_util_screen_mode_e *mode)
998 {
999    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1000    EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
1001
1002 #if X11
1003    Evas *e;
1004    Ecore_Evas *ee;
1005    const char *str;
1006    int id;
1007
1008    e = evas_object_evas_get(window);
1009    EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
1010
1011    ee = ecore_evas_ecore_evas_get(e);
1012    EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
1013
1014    id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
1015    EINA_SAFETY_ON_TRUE_RETURN_VAL((id == -1), EFL_UTIL_ERROR_INVALID_PARAMETER);
1016
1017    str = ecore_evas_aux_hint_val_get(ee, id);
1018    EINA_SAFETY_ON_NULL_RETURN_VAL(str, EFL_UTIL_ERROR_INVALID_PARAMETER);
1019
1020    if (strncmp(str, "1", strlen("1")) == 0)
1021      *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON;
1022    else
1023      *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1024
1025    return EFL_UTIL_ERROR_NONE;
1026 #endif /* end of X11 */
1027
1028 #if WAYLAND
1029    Ecore_Wl_Window *wlwin;
1030    struct wl_surface *surface;
1031    Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1032    Eina_Bool res;
1033
1034    res = _wl_init();
1035    EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1036
1037    wlwin = elm_win_wl_window_get(window);
1038    if (wlwin)
1039      {
1040         while (!_eflutil.wl.policy.proto)
1041           wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1042
1043         surface = ecore_wl_window_surface_get(wlwin);
1044         EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1045                                        EFL_UTIL_ERROR_INVALID_PARAMETER);
1046
1047         scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1048         if (scr_mode_info)
1049           {
1050              if (scr_mode_info->wait_for_done)
1051                {
1052                   while (scr_mode_info->wait_for_done)
1053                     {
1054                        ecore_wl_flush();
1055                        wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1056                     }
1057                }
1058
1059              switch (scr_mode_info->mode)
1060                {
1061                 case TIZEN_POLICY_MODE_DEFAULT:   *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;   break;
1062                 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
1063                 default:                          *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1064                   return EFL_UTIL_ERROR_INVALID_PARAMETER;
1065                }
1066              return EFL_UTIL_ERROR_NONE;
1067           }
1068         else
1069           {
1070              *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1071              return EFL_UTIL_ERROR_INVALID_PARAMETER;
1072           }
1073      }
1074    else
1075      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1076 #endif /* end of WAYLAND */
1077 }
1078
1079 API int
1080 efl_util_set_window_screen_mode_error_cb(Evas_Object *window,
1081                                          efl_util_window_screen_mode_error_cb callback,
1082                                          void *user_data)
1083 {
1084    Eina_Bool ret = EINA_FALSE;
1085
1086    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1087    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
1088
1089    ret = _cb_info_add(window,
1090                       (Efl_Util_Cb)callback,
1091                       user_data,
1092                       CBH_SCR_MODE);
1093    if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1094
1095 #if X11
1096    if (!_eflutil.cb_handler[CBH_SCR_MODE].atom)
1097      _eflutil.cb_handler[CBH_SCR_MODE].atom = ecore_x_atom_get("_E_SCREEN_MODE_ACCESS_RESULT");
1098 #endif /* end of X11 */
1099
1100    return EFL_UTIL_ERROR_NONE;
1101 }
1102
1103 API int
1104 efl_util_unset_window_screen_mode_error_cb(Evas_Object *window)
1105 {
1106    Eina_Bool ret = EINA_FALSE;
1107
1108    EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1109
1110    ret = _cb_info_del_by_win(window, CBH_SCR_MODE);
1111    if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1112
1113    return EFL_UTIL_ERROR_NONE;
1114 }
1115
1116 API int
1117 efl_util_input_initialize_generator(efl_util_input_device_type_e dev_type)
1118 {
1119    return EFL_UTIL_ERROR_NONE;
1120 }
1121
1122 API void
1123 efl_util_input_deinitialize_generator(void)
1124 {
1125    return;
1126 }
1127
1128 API int
1129 efl_util_input_generate_key(const char *key_name,
1130                             int pressed)
1131 {
1132    return EFL_UTIL_ERROR_NONE;
1133 }
1134
1135 API int
1136 efl_util_input_generate_touch(int idx,
1137                               efl_util_input_touch_type_e touch_type,
1138                               int x,
1139                               int y)
1140 {
1141    return EFL_UTIL_ERROR_NONE;
1142 }
1143
1144 struct _efl_util_screenshot_h
1145 {
1146    int width;
1147    int height;
1148
1149 #if X11
1150    Ecore_X_Display *dpy;
1151    int internal_display;
1152    int screen;
1153    Window root;
1154    Pixmap pixmap;
1155    GC gc;
1156    Atom atom_capture;
1157
1158    /* port */
1159    int port;
1160
1161    /* damage */
1162    Damage   damage;
1163    int      damage_base;
1164
1165    /* dri2 */
1166    int eventBase, errorBase;
1167    int dri2Major, dri2Minor;
1168    char *driver_name, *device_name;
1169    drm_magic_t magic;
1170
1171    /* drm */
1172    int drm_fd;
1173 #endif
1174
1175    Eina_Bool shot_done;
1176
1177    /* tbm bufmgr */
1178    tbm_bufmgr bufmgr;
1179 };
1180
1181 /* scrrenshot handle */
1182 static efl_util_screenshot_h g_screenshot;
1183
1184 #if X11
1185 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1186 #define FOURCC_RGB32    FOURCC('R','G','B','4')
1187 #define TIMEOUT_CAPTURE 3
1188
1189 /* x error handling */
1190 static Bool g_efl_util_x_error_caught;
1191
1192 static int
1193 _efl_util_screenshot_x_error_handle(Display *dpy, XErrorEvent *ev)
1194 {
1195    if (!g_screenshot || (dpy != g_screenshot->dpy))
1196      return 0;
1197
1198    g_efl_util_x_error_caught = True;
1199
1200    return 0;
1201 }
1202
1203 static int
1204 _efl_util_screenshot_get_port(Display *dpy, unsigned int id, Window win)
1205 {
1206    unsigned int ver, rev, req_base, evt_base, err_base;
1207    unsigned int adaptors;
1208    XvAdaptorInfo *ai = NULL;
1209    XvImageFormatValues *fo = NULL;
1210    int formats;
1211    int i, j, p;
1212
1213    if (XvQueryExtension(dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1214      {
1215         fprintf(stderr, "[screenshot] fail: no XV extension. \n");
1216         return -1;
1217      }
1218
1219    if (XvQueryAdaptors(dpy, win, &adaptors, &ai) != Success)
1220      {
1221         fprintf(stderr, "[screenshot] fail: query adaptors. \n");
1222         return -1;
1223      }
1224
1225    EINA_SAFETY_ON_NULL_RETURN_VAL(ai, -1);
1226
1227    for (i = 0; i < adaptors; i++)
1228      {
1229         int support_format = False;
1230
1231         if (!(ai[i].type & XvInputMask) ||
1232             !(ai[i].type & XvStillMask))
1233           continue;
1234
1235         p = ai[i].base_id;
1236
1237         fo = XvListImageFormats(dpy, p, &formats);
1238         for (j = 0; j < formats; j++)
1239           if (fo[j].id == (int)id)
1240             support_format = True;
1241
1242         if (fo)
1243           XFree(fo);
1244
1245         if (!support_format)
1246           continue;
1247
1248         for (; p < ai[i].base_id + ai[i].num_ports; p++)
1249           {
1250              if (XvGrabPort(dpy, p, 0) == Success)
1251                {
1252                   XvFreeAdaptorInfo(ai);
1253                   return p;
1254                }
1255           }
1256      }
1257
1258    XvFreeAdaptorInfo(ai);
1259
1260    XSync(dpy, False);
1261
1262    return -1;
1263 }
1264
1265 static int _efl_util_screenshot_get_best_size(Display *dpy, int port, int width, int height, unsigned int *best_width, unsigned int *best_height)
1266 {
1267    XErrorHandler old_handler = NULL;
1268
1269    Atom atom_capture = XInternAtom(dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1270
1271    g_efl_util_x_error_caught = False;
1272    old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1273
1274    XvSetPortAttribute(dpy, port, atom_capture, 1);
1275    XSync(dpy, False);
1276
1277    g_efl_util_x_error_caught = False;
1278    XSetErrorHandler(old_handler);
1279
1280    XvQueryBestSize(dpy, port, 0, 0, 0, width, height, best_width, best_height);
1281    if (best_width <= 0 || best_height <= 0)
1282      return 0;
1283
1284    return 1;
1285 }
1286 #endif
1287
1288 API efl_util_screenshot_h efl_util_screenshot_initialize(int width, int height)
1289 {
1290 #if X11
1291    efl_util_screenshot_h screenshot = NULL;
1292    int depth = 0;
1293    int damage_err_base = 0;
1294    unsigned int best_width = 0;
1295    unsigned int best_height = 0;
1296
1297    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1298    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1299
1300    if (g_screenshot != NULL)
1301      {
1302         if (g_screenshot->width != width || g_screenshot->height != height)
1303           {
1304              // TODO: recreate pixmap and update information
1305              if (!_efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height))
1306                {
1307                   set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1308                   return NULL;
1309                }
1310
1311              g_screenshot->width = width;
1312              g_screenshot->height = height;
1313           }
1314
1315         return g_screenshot;
1316      }
1317
1318    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1319    EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1320
1321    /* set dpy */
1322    screenshot->dpy = ecore_x_display_get();
1323    if (!screenshot->dpy)
1324      {
1325         screenshot->dpy = XOpenDisplay(0);
1326         EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_init);
1327
1328         /* for XCloseDisplay at denitialization */
1329         screenshot->internal_display = 1;
1330      }
1331
1332    /* set screen */
1333    screenshot->screen = DefaultScreen(screenshot->dpy);
1334
1335    /* set root window */
1336    screenshot->root = DefaultRootWindow(screenshot->dpy);
1337
1338    /* initialize capture adaptor */
1339    screenshot->port = _efl_util_screenshot_get_port(screenshot->dpy, FOURCC_RGB32, screenshot->root);
1340    EINA_SAFETY_ON_FALSE_GOTO(screenshot->port > 0, fail_init);
1341
1342    /* get the best size */
1343    _efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height);
1344    EINA_SAFETY_ON_FALSE_GOTO(best_width > 0, fail_init);
1345    EINA_SAFETY_ON_FALSE_GOTO(best_height > 0, fail_init);
1346
1347    /* set the width and the height */
1348    screenshot->width = best_width;
1349    screenshot->height = best_height;
1350
1351    /* create a pixmap */
1352    depth = DefaultDepth(screenshot->dpy, screenshot->screen);
1353    screenshot->pixmap = XCreatePixmap(screenshot->dpy, screenshot->root, screenshot->width, screenshot->height, depth);
1354    EINA_SAFETY_ON_FALSE_GOTO(screenshot->pixmap > 0, fail_init);
1355
1356    screenshot->gc = XCreateGC(screenshot->dpy, screenshot->pixmap, 0, 0);
1357    EINA_SAFETY_ON_NULL_GOTO(screenshot->gc, fail_init);
1358
1359    XSetForeground(screenshot->dpy, screenshot->gc, 0xFF000000);
1360    XFillRectangle(screenshot->dpy, screenshot->pixmap, screenshot->gc, 0, 0, width, height);
1361
1362    /* initialize damage */
1363    if (!XDamageQueryExtension(screenshot->dpy, &screenshot->damage_base, &damage_err_base))
1364      goto fail_init;
1365
1366    screenshot->damage = XDamageCreate(screenshot->dpy, screenshot->pixmap, XDamageReportNonEmpty);
1367    EINA_SAFETY_ON_FALSE_GOTO(screenshot->damage > 0, fail_init);
1368
1369    /* initialize dri3 and dri2 */
1370    if (!DRI2QueryExtension(screenshot->dpy, &screenshot->eventBase, &screenshot->errorBase))
1371      {
1372         fprintf(stderr, "[screenshot] fail: DRI2QueryExtention\n");
1373         goto fail_init;
1374      }
1375
1376    if (!DRI2QueryVersion(screenshot->dpy, &screenshot->dri2Major, &screenshot->dri2Minor))
1377      {
1378         fprintf(stderr, "[screenshot] fail: DRI2QueryVersion\n");
1379         goto fail_init;
1380      }
1381
1382    if (!DRI2Connect(screenshot->dpy, screenshot->root, &screenshot->driver_name, &screenshot->device_name))
1383      {
1384         fprintf(stderr, "[screenshot] fail: DRI2Connect\n");
1385         goto fail_init;
1386      }
1387
1388    screenshot->drm_fd = open(screenshot->device_name, O_RDWR);
1389    EINA_SAFETY_ON_FALSE_GOTO(screenshot->drm_fd >= 0, fail_init);
1390
1391    if (drmGetMagic(screenshot->drm_fd, &screenshot->magic))
1392      {
1393         fprintf(stderr, "[screenshot] fail: drmGetMagic\n");
1394         goto fail_init;
1395      }
1396
1397    if (!DRI2Authenticate(screenshot->dpy, screenshot->root, screenshot->magic))
1398      {
1399         fprintf(stderr, "[screenshot] fail: DRI2Authenticate\n");
1400         goto fail_init;
1401      }
1402
1403    if (!drmAuthMagic(screenshot->drm_fd, screenshot->magic))
1404      {
1405         fprintf(stderr, "[screenshot] fail: drmAuthMagic\n");
1406         goto fail_init;
1407      }
1408
1409    DRI2CreateDrawable(screenshot->dpy, screenshot->pixmap);
1410
1411    /* tbm bufmgr */
1412    screenshot->bufmgr = tbm_bufmgr_init(screenshot->drm_fd);
1413    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1414
1415    XFlush(screenshot->dpy);
1416
1417    g_screenshot = screenshot;
1418    set_last_result(EFL_UTIL_ERROR_NONE);
1419
1420    return g_screenshot;
1421 #endif
1422
1423 #if WAYLAND
1424    efl_util_screenshot_h screenshot = NULL;
1425
1426    if (!_eflutil.wl.shot.screenshooter)
1427      {
1428         int ret = 0;
1429         _wl_init();
1430         while (!_eflutil.wl.shot.screenshooter && ret != -1)
1431           ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1432         EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.screenshooter, fail_init);
1433
1434        _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
1435        EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_init);
1436      }
1437
1438    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1439    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1440
1441    if (g_screenshot)
1442      {
1443         if (g_screenshot->width != width || g_screenshot->height != height)
1444           {
1445              g_screenshot->width = width;
1446              g_screenshot->height = height;
1447           }
1448
1449         return g_screenshot;
1450      }
1451
1452    screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1453    EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1454
1455    screenshot->width = width;
1456    screenshot->height = height;
1457
1458    screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
1459    EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1460
1461    g_screenshot = screenshot;
1462    set_last_result(EFL_UTIL_ERROR_NONE);
1463
1464    screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
1465
1466    return g_screenshot;
1467 #endif
1468 fail_param:
1469    if (screenshot)
1470      efl_util_screenshot_deinitialize(screenshot);
1471    set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1472    return NULL;
1473 fail_memory:
1474    if (screenshot)
1475      efl_util_screenshot_deinitialize(screenshot);
1476    set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1477    return NULL;
1478 fail_init:
1479    if (screenshot)
1480      efl_util_screenshot_deinitialize(screenshot);
1481    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1482    return NULL;
1483 }
1484
1485 API int efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
1486 {
1487 #if X11
1488    if (!screenshot)
1489      return EFL_UTIL_ERROR_INVALID_PARAMETER;
1490
1491    /* tbm bufmgr */
1492    if (screenshot->bufmgr)
1493      tbm_bufmgr_deinit(screenshot->bufmgr);
1494
1495    DRI2DestroyDrawable(screenshot->dpy, screenshot->pixmap);
1496
1497    /* dri2 */
1498    if (screenshot->drm_fd)
1499      close(screenshot->drm_fd);
1500    if (screenshot->driver_name)
1501      free(screenshot->driver_name);
1502    if (screenshot->device_name)
1503      free(screenshot->device_name);
1504
1505    /* xv */
1506    if (screenshot->port > 0 && screenshot->pixmap > 0)
1507      XvStopVideo(screenshot->dpy, screenshot->port, screenshot->pixmap);
1508
1509    /* damage */
1510    if (screenshot->damage)
1511      XDamageDestroy(screenshot->dpy, screenshot->damage);
1512
1513    /* gc */
1514    if (screenshot->gc)
1515      XFreeGC(screenshot->dpy, screenshot->gc);
1516
1517    /* pixmap */
1518    if (screenshot->pixmap > 0)
1519      XFreePixmap(screenshot->dpy, screenshot->pixmap);
1520
1521    /* port */
1522    if (screenshot->port > 0)
1523      XvUngrabPort(screenshot->dpy, screenshot->port, 0);
1524
1525    XSync(screenshot->dpy, False);
1526
1527    /* dpy */
1528    if (screenshot->internal_display ==1 && screenshot->dpy)
1529      XCloseDisplay(screenshot->dpy);
1530
1531    free(screenshot);
1532    g_screenshot = NULL;
1533
1534    return EFL_UTIL_ERROR_NONE;
1535 #endif
1536 #if WAYLAND
1537    if (!screenshot)
1538      return EFL_UTIL_ERROR_NONE;
1539
1540    free(screenshot);
1541    g_screenshot = NULL;
1542
1543    if (_eflutil.wl.shot.screenshooter)
1544      screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, NULL);
1545
1546    return EFL_UTIL_ERROR_NONE;
1547 #endif
1548 }
1549
1550
1551 API tbm_surface_h efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
1552 {
1553 #if X11
1554    XEvent ev = {0,};
1555    XErrorHandler old_handler = NULL;
1556    unsigned int attachment = DRI2BufferFrontLeft;
1557    int nbufs = 0;
1558    DRI2Buffer *bufs = NULL;
1559    tbm_bo t_bo = NULL;
1560    tbm_surface_h t_surface = NULL;
1561    int buf_width = 0;
1562    int buf_height = 0;
1563    tbm_surface_info_s surf_info;
1564    int i;
1565
1566    if (screenshot != g_screenshot)
1567      {
1568         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1569         return NULL;
1570      }
1571
1572    /* for flush other pending requests and pending events */
1573    XSync(screenshot->dpy, 0);
1574
1575    g_efl_util_x_error_caught = False;
1576    old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1577
1578    /* dump here */
1579    XvPutStill(screenshot->dpy, screenshot->port, screenshot->pixmap, screenshot->gc,
1580               0, 0, screenshot->width, screenshot->height,
1581               0, 0, screenshot->width, screenshot->height);
1582
1583    XSync(screenshot->dpy, 0);
1584
1585    if (g_efl_util_x_error_caught)
1586      {
1587         g_efl_util_x_error_caught = False;
1588         XSetErrorHandler(old_handler);
1589         goto fail;
1590      }
1591
1592    g_efl_util_x_error_caught = False;
1593    XSetErrorHandler(old_handler);
1594
1595    if (XPending(screenshot->dpy))
1596      XNextEvent(screenshot->dpy, &ev);
1597    else
1598      {
1599         int fd = ConnectionNumber(screenshot->dpy);
1600         fd_set mask;
1601         struct timeval tv;
1602         int ret;
1603
1604         FD_ZERO(&mask);
1605         FD_SET(fd, &mask);
1606
1607         tv.tv_usec = 0;
1608         tv.tv_sec = TIMEOUT_CAPTURE;
1609
1610         ret = select(fd + 1, &mask, 0, 0, &tv);
1611         if (ret < 0)
1612           fprintf(stderr, "[screenshot] fail: select.\n");
1613         else if (ret == 0)
1614           fprintf(stderr, "[screenshot] fail: timeout(%d sec)!\n", TIMEOUT_CAPTURE);
1615         else if (XPending(screenshot->dpy))
1616           XNextEvent(screenshot->dpy, &ev);
1617         else
1618           fprintf(stderr, "[screenshot] fail: not passed a event!\n");
1619      }
1620
1621    /* check if the capture is done by xserver and pixmap has got the captured image */
1622    if (ev.type == (screenshot->damage_base + XDamageNotify))
1623      {
1624         XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1625         if (damage_ev->drawable == screenshot->pixmap)
1626           {
1627              /* Get DRI2 FrontLeft buffer of the pixmap */
1628              bufs = DRI2GetBuffers(screenshot->dpy, screenshot->pixmap, &buf_width, &buf_height, &attachment, 1, &nbufs);
1629              if (!bufs)
1630                {
1631                   fprintf(stderr, "[screenshot] fail: DRI2GetBuffers\n");
1632                   goto fail;
1633                }
1634
1635              t_bo = tbm_bo_import(screenshot->bufmgr, bufs[0].name);
1636              if (!t_bo)
1637                {
1638                   fprintf(stderr, "[screenshot] fail: import tbm_bo!\n");
1639                   goto fail;
1640                }
1641
1642              surf_info.width = buf_width;
1643              surf_info.height = buf_height;
1644              surf_info.format = TBM_FORMAT_XRGB8888;
1645              surf_info.bpp = 32;
1646              surf_info.size = bufs->pitch * surf_info.height;
1647              surf_info.num_planes = 1;
1648              for (i = 0; i < surf_info.num_planes; i++)
1649                {
1650                   surf_info.planes[i].size = bufs->pitch * surf_info.height;
1651                   surf_info.planes[i].stride = bufs->pitch;
1652                   surf_info.planes[i].offset = 0;
1653                }
1654              t_surface = tbm_surface_internal_create_with_bos(&surf_info, &t_bo, 1);
1655              if (!t_surface)
1656                {
1657                   fprintf(stderr, "[screenshot] fail: get tbm_surface!\n");
1658                   goto fail;
1659                }
1660
1661              tbm_bo_unref(t_bo);
1662              free(bufs);
1663
1664              XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1665
1666              set_last_result(EFL_UTIL_ERROR_NONE);
1667
1668              return t_surface;
1669           }
1670
1671         XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1672      }
1673
1674 fail:
1675
1676    if (t_bo)
1677      tbm_bo_unref(t_bo);
1678    if (bufs)
1679      free(bufs);
1680
1681    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1682
1683    return NULL;
1684 #endif
1685
1686 #if WAYLAND
1687    tbm_surface_h t_surface = NULL;
1688    struct wl_buffer *buffer = NULL;
1689    Efl_Util_Wl_Output_Info *output;
1690    int ret = 0;
1691
1692    if (screenshot != g_screenshot)
1693      {
1694         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1695         return NULL;
1696      }
1697
1698    output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
1699    if (!output)
1700      {
1701         fprintf(stderr, "[screenshot] fail: no output for screenshot\n");
1702         goto fail;
1703      }
1704
1705    t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
1706    if (!t_surface)
1707      {
1708         fprintf(stderr, "[screenshot] fail: tbm_surface_create\n");
1709         goto fail;
1710      }
1711
1712    buffer = wayland_tbm_client_create_buffer(_eflutil.wl.shot.tbm_client, t_surface);
1713    if (!buffer)
1714      {
1715         fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n");
1716         goto fail;
1717      }
1718
1719    screenshooter_shoot(_eflutil.wl.shot.screenshooter, output->output, buffer);
1720
1721    screenshot->shot_done = EINA_FALSE;
1722    while (!screenshot->shot_done && ret != -1)
1723      ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1724
1725    if (ret == -1)
1726      {
1727         fprintf(stderr, "[screenshot] fail: screenshooter_shoot\n");
1728         goto fail;
1729      }
1730
1731    wl_buffer_destroy(buffer);
1732
1733    /* reset shot_done for next screenshot */
1734    screenshot->shot_done = EINA_FALSE;
1735
1736    return t_surface;
1737
1738 fail:
1739    if (t_surface)
1740      tbm_surface_destroy(t_surface);
1741    if (buffer);
1742      wl_buffer_destroy(buffer);
1743
1744    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1745
1746    return NULL;
1747 #endif
1748 }