2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "TIZEN_N_EFL_UTIL"
24 #include <sys/types.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>
36 #include <X11/extensions/Xvlib.h>
37 #include <X11/extensions/Xvproto.h>
38 #include <X11/extensions/Xdamage.h>
42 #endif /* end of X11 */
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 */
52 /* callback handler index */
53 #define CBH_NOTI_LEV 0
54 #define CBH_SCR_MODE 1
57 typedef void (*Efl_Util_Cb)(Evas_Object *, int, void *);
59 typedef struct _Efl_Util_Callback_Info
64 } Efl_Util_Callback_Info;
67 typedef struct _Efl_Util_Wl_Surface_Lv_Info
70 void *surface; /* wl_surface */
72 Eina_Bool wait_for_done;
74 } Efl_Util_Wl_Surface_Lv_Info;
76 typedef struct _Efl_Util_Wl_Surface_Scr_Mode_Info
78 void *surface; /* wl_surface */
80 Eina_Bool wait_for_done;
82 } Efl_Util_Wl_Surface_Scr_Mode_Info;
84 typedef struct _Efl_Util_Wl_Output_Info
86 struct wl_output *output;
87 int offset_x, offset_y, width, height;
88 } Efl_Util_Wl_Output_Info;
91 typedef struct _Efl_Util_Data
94 /* x11 related stuffs */
98 Ecore_Event_Handler *handler; /* x11 client message handler */
101 #endif /* end of X11 */
104 /* wayland related stuffs */
108 struct wl_display *dpy;
109 struct wl_event_queue *queue;
113 struct tizen_policy *proto;
114 Eina_Hash *hash_noti_lv;
115 Eina_Hash *hash_scr_mode;
119 struct screenshooter *screenshooter;
120 struct wayland_tbm_client *tbm_client;
121 Eina_List *output_list;
124 #endif /* end of WAYLAND */
128 Eina_List *info_list; /* list of callback info */
129 unsigned int atom; /* x11 atom */
130 } cb_handler[CBH_MAX];
133 static Efl_Util_Data _eflutil =
141 #endif /* end of X11 */
146 { NULL, NULL, NULL }, /* tizen_policy protocol */
147 { NULL, NULL, NULL } /* screenshooter protocol */
149 #endif /* end of WAYLAND */
151 { NULL, 0 }, /* handler for notification level */
152 { NULL, 0 } /* handler for screen mode */
156 static Eina_Bool _cb_info_add(Evas_Object *win, Efl_Util_Cb cb, void *data, int idx);
157 static Eina_Bool _cb_info_del_by_win(Evas_Object *win, int idx);
158 static Eina_List *_cb_info_list_get(int idx);
159 static Efl_Util_Callback_Info *_cb_info_find_by_win(Evas_Object *win, int idx);
161 static Efl_Util_Callback_Info *_cb_info_find_by_xwin(unsigned int xwin, int idx);
162 static Eina_Bool _cb_x11_client_msg(void *data, int type, void *event);
163 static Eina_Bool _x11_init(void);
164 #endif /* end of X11 */
166 static Eina_Bool _wl_init(void);
167 static void _cb_wl_reg_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
168 static void _cb_wl_reg_global_remove(void *data, struct wl_registry *reg, unsigned int name);
169 static Efl_Util_Callback_Info *_cb_info_find_by_wlsurf(void *wlsurf, int idx);
170 static void _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t is_conformant);
171 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);
172 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);
173 static void _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id);
174 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);
176 static const struct wl_registry_listener _wl_reg_listener =
179 _cb_wl_reg_global_remove
182 struct tizen_policy_listener _wl_tz_policy_listener =
184 _cb_wl_tz_policy_conformant,
185 _cb_wl_tz_policy_conformant_area,
186 _cb_wl_tz_policy_notification_done,
187 _cb_wl_tz_policy_transient_for_done,
188 _cb_wl_tz_policy_scr_mode_done
190 #endif /* end of WAYLAND */
193 _cb_info_add(Evas_Object *win,
198 Efl_Util_Callback_Info *info;
200 info = _cb_info_find_by_win(win, idx);
203 _eflutil.cb_handler[idx].info_list
204 = eina_list_remove(_eflutil.cb_handler[idx].info_list,
209 info = (Efl_Util_Callback_Info *)calloc(1, sizeof(Efl_Util_Callback_Info));
210 if (!info) return EINA_FALSE;
216 _eflutil.cb_handler[idx].info_list
217 = eina_list_append(_eflutil.cb_handler[idx].info_list,
221 if (!_eflutil.x11.handler)
222 _eflutil.x11.handler = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
225 #endif /* end of X11 */
231 _cb_info_del_by_win(Evas_Object *win,
234 Efl_Util_Callback_Info *info;
236 info = _cb_info_find_by_win(win, idx);
237 if (!info) return EINA_FALSE;
239 _eflutil.cb_handler[idx].info_list
240 = eina_list_remove(_eflutil.cb_handler[idx].info_list,
245 unsigned int count = eina_list_count(_eflutil.cb_handler[idx].info_list);
246 if ((count == 0) && (_eflutil.x11.handler))
248 ecore_event_handler_del(_eflutil.x11.handler);
249 _eflutil.x11.handler = NULL;
257 _cb_info_list_get(int idx)
259 return _eflutil.cb_handler[idx].info_list;
262 static Efl_Util_Callback_Info *
263 _cb_info_find_by_win(Evas_Object *win,
267 Efl_Util_Callback_Info *info;
269 l = _cb_info_list_get(idx);
270 EINA_LIST_FOREACH(l, ll, info)
272 if (info->win == win) return info;
279 static Efl_Util_Callback_Info *
280 _cb_info_find_by_xwin(unsigned int xwin,
284 Efl_Util_Callback_Info *info;
287 l = _cb_info_list_get(idx);
288 EINA_LIST_FOREACH(l, ll, info)
290 xwin2 = elm_win_xwindow_get(info->win);
291 if (xwin == xwin2) return info;
298 _cb_x11_client_msg(void *data,
302 Ecore_X_Event_Client_Message *ev;
304 Efl_Util_Callback_Info *info;
307 if (!ev) return ECORE_CALLBACK_PASS_ON;
310 if (xwin == 0) return ECORE_CALLBACK_PASS_ON;
312 if (ev->message_type == _eflutil.cb_handler[CBH_NOTI_LEV].atom)
314 info = _cb_info_find_by_xwin(xwin, CBH_NOTI_LEV);
316 /* permission denied */
317 if ((ev->data.l[1] == 0) &&
322 EFL_UTIL_ERROR_PERMISSION_DENIED,
326 else if (ev->message_type == _eflutil.cb_handler[CBH_SCR_MODE].atom)
328 info = _cb_info_find_by_xwin(xwin, CBH_SCR_MODE);
330 /* permission denied */
331 if ((ev->data.l[1] == 0) &&
336 EFL_UTIL_ERROR_PERMISSION_DENIED,
340 return ECORE_CALLBACK_PASS_ON;
346 if (_eflutil.x11.init) return EINA_TRUE;
348 _eflutil.x11.dpy = ecore_x_display_get();
349 EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.x11.dpy, EINA_FALSE);
351 _eflutil.x11.init = EINA_TRUE;
355 #endif /* end of X11 */
361 struct wl_registry *reg = NULL;
363 if (_eflutil.wl.init) return EINA_TRUE;
367 _eflutil.wl.dpy = ecore_wl_display_get();
368 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.dpy, fail);
370 _eflutil.wl.queue = wl_display_create_queue(_eflutil.wl.dpy);
371 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.queue, fail);
373 reg = wl_display_get_registry(_eflutil.wl.dpy);
374 EINA_SAFETY_ON_NULL_GOTO(reg, fail);
376 wl_proxy_set_queue((struct wl_proxy*)reg, _eflutil.wl.queue);
377 wl_registry_add_listener(reg, &_wl_reg_listener, NULL);
379 _eflutil.wl.init = EINA_TRUE;
383 if (_eflutil.wl.queue)
385 wl_event_queue_destroy(_eflutil.wl.queue);
386 _eflutil.wl.queue = NULL;
390 wl_registry_destroy(reg);
396 _cb_wl_output_geometry(void *data, struct wl_output *wl_output, int x, int y,
397 int physical_width, int physical_height, int subpixel,
398 const char *make, const char *model, int transform)
400 Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
401 if (wl_output == output->output)
403 output->offset_x = x;
404 output->offset_y = y;
409 _cb_wl_output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
410 int width, int height, int refresh)
412 Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
413 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT))
415 output->width = width;
416 output->height = height;
421 _cb_wl_output_done(void *data, struct wl_output *wl_output)
426 _cb_wl_output_scale(void *data, struct wl_output *wl_output, int32_t factor)
430 static const struct wl_output_listener output_listener =
432 _cb_wl_output_geometry,
439 _cb_wl_screenshot_done(void *data, struct screenshooter *screenshooter)
441 Eina_Bool *shot_done = (Eina_Bool*)data;
443 *shot_done = EINA_TRUE;
446 static const struct screenshooter_listener screenshooter_listener =
448 _cb_wl_screenshot_done
452 _cb_wl_reg_global(void *data,
453 struct wl_registry *reg,
455 const char *interface,
456 unsigned int version)
458 if (!strcmp(interface, "tizen_policy"))
460 struct tizen_policy *proto;
461 proto = wl_registry_bind(reg,
463 &tizen_policy_interface,
467 tizen_policy_add_listener(proto,
468 &_wl_tz_policy_listener,
471 _eflutil.wl.policy.hash_noti_lv = eina_hash_pointer_new(free);
472 _eflutil.wl.policy.hash_scr_mode = eina_hash_pointer_new(free);
473 _eflutil.wl.policy.proto = proto;
475 else if (strcmp(interface, "wl_output") == 0)
477 Efl_Util_Wl_Output_Info *output = calloc(1, sizeof(Efl_Util_Wl_Output_Info));
478 EINA_SAFETY_ON_NULL_RETURN(output);
480 _eflutil.wl.shot.output_list = eina_list_append(_eflutil.wl.shot.output_list, output);
482 output->output = wl_registry_bind(reg, name, &wl_output_interface, version);
483 wl_output_add_listener(output->output, &output_listener, output);
485 else if (strcmp(interface, "screenshooter") == 0)
487 _eflutil.wl.shot.screenshooter = wl_registry_bind(reg, name, &screenshooter_interface, version);
488 screenshooter_add_listener(_eflutil.wl.shot.screenshooter, &screenshooter_listener, NULL);
493 _cb_wl_reg_global_remove(void *data,
494 struct wl_registry *reg,
497 _eflutil.wl.policy.proto = NULL;
498 eina_hash_free(_eflutil.wl.policy.hash_noti_lv);
499 eina_hash_free(_eflutil.wl.policy.hash_scr_mode);
502 static Efl_Util_Callback_Info *
503 _cb_info_find_by_wlsurf(void *wlsurf,
507 Efl_Util_Callback_Info *info;
508 Ecore_Wl_Window *wlwin2 = NULL;
509 void *wlsurf2 = NULL;
511 l = _cb_info_list_get(idx);
512 EINA_LIST_FOREACH(l, ll, info)
514 wlwin2 = elm_win_wl_window_get(info->win);
515 wlsurf2 = ecore_wl_window_surface_get(wlwin2);
516 if (wlsurf== wlsurf2) return info;
523 _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy,
524 struct wl_surface *surface, uint32_t is_conformant)
529 _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy,
530 struct wl_surface *surface, uint32_t conformant_part,
531 uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h)
536 _cb_wl_tz_policy_notification_done(void *data,
537 struct tizen_policy *tizen_policy,
538 struct wl_surface *surface,
542 Efl_Util_Wl_Surface_Lv_Info *lv_info;
543 Efl_Util_Callback_Info *cb_info;
545 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
548 lv_info->level = level;
549 lv_info->wait_for_done = EINA_FALSE;
550 lv_info->state = state;
553 if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
555 cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_NOTI_LEV);
556 if (!cb_info) return;
557 if (!cb_info->cb) return;
559 cb_info->cb(cb_info->win,
560 EFL_UTIL_ERROR_PERMISSION_DENIED,
565 _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id)
570 _cb_wl_tz_policy_scr_mode_done(void *data,
571 struct tizen_policy *tizen_policy,
572 struct wl_surface *surface,
577 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
578 Efl_Util_Callback_Info *cb_info;
580 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
583 scr_mode_info->mode = mode;
584 scr_mode_info->wait_for_done = EINA_FALSE;
585 scr_mode_info->state = state;
588 if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
590 cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_SCR_MODE);
591 if (!cb_info) return;
592 if (!cb_info->cb) return;
594 cb_info->cb(cb_info->win,
595 EFL_UTIL_ERROR_PERMISSION_DENIED,
600 _cb_window_show(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
602 Efl_Util_Wl_Surface_Lv_Info *lv_info;
603 Ecore_Wl_Window *wlwin;
604 struct wl_surface *surface;
607 if (EINA_UNLIKELY(!lv_info))
610 wlwin = elm_win_wl_window_get(lv_info->window);
611 if (EINA_UNLIKELY(!wlwin))
614 surface = ecore_wl_window_surface_get(wlwin);
615 if (EINA_UNLIKELY(!surface))
618 eina_hash_free_cb_set(_eflutil.wl.policy.hash_noti_lv, NULL);
619 eina_hash_del_by_key(_eflutil.wl.policy.hash_noti_lv, &lv_info->surface);
621 lv_info->surface = surface;
622 eina_hash_add(_eflutil.wl.policy.hash_noti_lv, &surface, lv_info);
623 eina_hash_free_cb_set(_eflutil.wl.policy.hash_noti_lv, free);
625 tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
626 surface, (int)lv_info->level);
628 #endif /* end of WAYLAND */
631 efl_util_set_notification_window_level(Evas_Object *window,
632 efl_util_notification_level_e level)
636 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
637 EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_NONE) &&
638 (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
639 EFL_UTIL_ERROR_INVALID_PARAMETER);
642 Ecore_X_Window_Type window_type;
646 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
648 xwin = elm_win_xwindow_get(window);
651 if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
653 // success to get window type
654 if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
656 // given EFL window's type is not notification type.
657 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
661 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
663 utilx_set_system_notification_level(_eflutil.x11.dpy,
666 return EFL_UTIL_ERROR_NONE;
669 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
670 #endif /* end of X11 */
674 Ecore_Wl_Window *wlwin;
675 struct wl_surface *surface;
676 Efl_Util_Wl_Surface_Lv_Info *lv_info;
677 Ecore_Wl_Window_Type wl_type;
680 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
682 wlwin = elm_win_wl_window_get(window);
683 EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
685 type = elm_win_type_get(window);
686 if (type != ELM_WIN_NOTIFICATION)
688 wl_type = ecore_wl_window_type_get(wlwin);
689 EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
690 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
693 while (!_eflutil.wl.policy.proto)
694 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
696 surface = ecore_wl_window_surface_get(wlwin);
697 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
698 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
700 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
703 lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
704 EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
706 lv_info->window = window;
707 lv_info->surface = surface;
708 lv_info->level = (int)level;
709 lv_info->wait_for_done = EINA_TRUE;
710 lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
711 eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
715 evas_object_event_callback_add(window, EVAS_CALLBACK_SHOW,
716 _cb_window_show, lv_info);
720 lv_info->level = (int)level;
721 lv_info->wait_for_done = EINA_TRUE;
722 lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
726 tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
727 surface, (int)level);
729 if (lv_info->wait_for_done)
732 while (lv_info->wait_for_done && (count < 3))
735 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
739 if (lv_info->wait_for_done)
741 return EFL_UTIL_ERROR_INVALID_PARAMETER;
745 if (lv_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
747 return EFL_UTIL_ERROR_PERMISSION_DENIED;
752 return EFL_UTIL_ERROR_NONE;
753 #endif /* end of WAYLAND */
757 efl_util_get_notification_window_level(Evas_Object *window,
758 efl_util_notification_level_e *level)
762 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
763 EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
766 Ecore_X_Window_Type window_type;
767 Utilx_Notification_Level utilx_level;
771 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
773 xwin = elm_win_xwindow_get(window);
776 if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
778 // success to get window type
779 if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
781 // given EFL window's type is not notification type.
782 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
785 utilx_level = utilx_get_system_notification_level(_eflutil.x11.dpy, xwin);
786 if (utilx_level == UTILX_NOTIFICATION_LEVEL_LOW)
787 *level = EFL_UTIL_NOTIFICATION_LEVEL_1;
788 else if(utilx_level == UTILX_NOTIFICATION_LEVEL_NORMAL)
789 *level = EFL_UTIL_NOTIFICATION_LEVEL_2;
790 else if(utilx_level == UTILX_NOTIFICATION_LEVEL_HIGH)
791 *level = EFL_UTIL_NOTIFICATION_LEVEL_3;
793 return EFL_UTIL_ERROR_INVALID_PARAMETER;
796 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
798 return EFL_UTIL_ERROR_NONE;
801 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
802 #endif /* end of X11 */
806 Ecore_Wl_Window *wlwin;
807 struct wl_surface *surface;
808 Efl_Util_Wl_Surface_Lv_Info *lv_info;
809 Ecore_Wl_Window_Type wl_type;
812 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
814 wlwin = elm_win_wl_window_get(window);
815 EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
817 type = elm_win_type_get(window);
818 if (type != ELM_WIN_NOTIFICATION)
820 wl_type = ecore_wl_window_type_get(wlwin);
821 EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
822 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
825 while (!_eflutil.wl.policy.proto)
826 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
828 surface = ecore_wl_window_surface_get(wlwin);
829 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
830 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
832 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
835 if (lv_info->wait_for_done)
838 while ((lv_info->wait_for_done) && (count < 3))
841 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
845 if (lv_info->wait_for_done)
847 *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
848 return EFL_UTIL_ERROR_INVALID_PARAMETER;
852 switch (lv_info->level)
854 case TIZEN_POLICY_LEVEL_1: *level = EFL_UTIL_NOTIFICATION_LEVEL_1; break;
855 case TIZEN_POLICY_LEVEL_2: *level = EFL_UTIL_NOTIFICATION_LEVEL_2; break;
856 case TIZEN_POLICY_LEVEL_3: *level = EFL_UTIL_NOTIFICATION_LEVEL_3; break;
857 case TIZEN_POLICY_LEVEL_NONE: *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE; break;
858 case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
859 case TIZEN_POLICY_LEVEL_MEDIUM: *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM; break;
860 case TIZEN_POLICY_LEVEL_HIGH: *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH; break;
861 case TIZEN_POLICY_LEVEL_TOP: *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP; break;
862 default: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
863 return EFL_UTIL_ERROR_INVALID_PARAMETER;
865 return EFL_UTIL_ERROR_NONE;
868 *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
870 return EFL_UTIL_ERROR_NONE;
871 #endif /* end of WAYLAND */
875 efl_util_set_notification_window_level_error_cb(Evas_Object *window,
876 efl_util_notification_window_level_error_cb callback,
879 Eina_Bool ret = EINA_FALSE;
881 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
882 EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
884 ret = _cb_info_add(window,
885 (Efl_Util_Cb)callback,
888 if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
891 if (!_eflutil.cb_handler[CBH_NOTI_LEV].atom)
892 _eflutil.cb_handler[CBH_NOTI_LEV].atom = ecore_x_atom_get("_E_NOTIFICATION_LEVEL_ACCESS_RESULT");
893 #endif /* end of X11 */
895 return EFL_UTIL_ERROR_NONE;
899 efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
901 Eina_Bool ret = EINA_FALSE;
903 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
905 ret = _cb_info_del_by_win(window, CBH_NOTI_LEV);
906 if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
908 return EFL_UTIL_ERROR_NONE;
912 efl_util_set_window_opaque_state(Evas_Object *window,
917 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
918 EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
919 EFL_UTIL_ERROR_INVALID_PARAMETER);
923 Utilx_Opaque_State state;
927 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
929 xwin = elm_win_xwindow_get(window);
930 EINA_SAFETY_ON_FALSE_RETURN_VAL(xwin > 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
933 state = UTILX_OPAQUE_STATE_ON;
935 state = UTILX_OPAQUE_STATE_OFF;
937 ret = utilx_set_window_opaque_state(_eflutil.x11.dpy,
942 return EFL_UTIL_ERROR_INVALID_PARAMETER;
944 return EFL_UTIL_ERROR_NONE;
945 #endif /* end of X11 */
948 Ecore_Wl_Window *wlwin;
949 struct wl_surface *surface;
951 if (!_eflutil.wl.policy.proto)
956 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
958 while (!_eflutil.wl.policy.proto && ret != -1)
959 ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
961 EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.policy.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
964 wlwin = elm_win_wl_window_get(window);
966 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
968 surface = ecore_wl_window_surface_get(wlwin);
970 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
972 tizen_policy_set_opaque_state(_eflutil.wl.policy.proto, surface, opaque);
974 return EFL_UTIL_ERROR_NONE;
975 #endif /* end of WAYLAND */
979 efl_util_set_window_screen_mode(Evas_Object *window,
980 efl_util_screen_mode_e mode)
982 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
983 EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
984 (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
985 EFL_UTIL_ERROR_INVALID_PARAMETER);
992 e = evas_object_evas_get(window);
993 EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
995 ee = ecore_evas_ecore_evas_get(e);
996 EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
998 id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
999 if (mode == EFL_UTIL_SCREEN_MODE_ALWAYS_ON)
1002 ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "1");
1004 ecore_evas_aux_hint_val_set(ee, id, "1");
1006 else if (mode == EFL_UTIL_SCREEN_MODE_DEFAULT)
1009 ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "0");
1011 ecore_evas_aux_hint_val_set(ee, id, "0");
1014 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1016 return EFL_UTIL_ERROR_NONE;
1017 #endif /* end of X11 */
1020 Ecore_Wl_Window *wlwin;
1021 struct wl_surface *surface;
1022 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1026 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1028 wlwin = elm_win_wl_window_get(window);
1031 while (!_eflutil.wl.policy.proto)
1032 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1034 surface = ecore_wl_window_surface_get(wlwin);
1035 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1036 EFL_UTIL_ERROR_INVALID_PARAMETER);
1038 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1041 scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
1042 EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
1044 scr_mode_info->surface = surface;
1045 scr_mode_info->mode = (unsigned int)mode;
1046 scr_mode_info->wait_for_done = EINA_TRUE;
1047 scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1049 eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
1055 scr_mode_info->mode = (unsigned int)mode;
1056 scr_mode_info->wait_for_done = EINA_TRUE;
1057 scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1060 tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
1061 surface, (unsigned int)mode);
1062 if (scr_mode_info->wait_for_done)
1065 while (scr_mode_info->wait_for_done && (count < 3))
1068 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1072 if (scr_mode_info->wait_for_done)
1074 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1078 if (scr_mode_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
1080 return EFL_UTIL_ERROR_PERMISSION_DENIED;
1085 return EFL_UTIL_ERROR_NONE;
1088 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1089 #endif /* end of WAYLAND */
1093 efl_util_get_window_screen_mode(Evas_Object *window,
1094 efl_util_screen_mode_e *mode)
1096 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1097 EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
1105 e = evas_object_evas_get(window);
1106 EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
1108 ee = ecore_evas_ecore_evas_get(e);
1109 EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
1111 id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
1112 EINA_SAFETY_ON_TRUE_RETURN_VAL((id == -1), EFL_UTIL_ERROR_INVALID_PARAMETER);
1114 str = ecore_evas_aux_hint_val_get(ee, id);
1115 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EFL_UTIL_ERROR_INVALID_PARAMETER);
1117 if (strncmp(str, "1", strlen("1")) == 0)
1118 *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON;
1120 *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1122 return EFL_UTIL_ERROR_NONE;
1123 #endif /* end of X11 */
1126 Ecore_Wl_Window *wlwin;
1127 struct wl_surface *surface;
1128 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1132 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1134 wlwin = elm_win_wl_window_get(window);
1137 while (!_eflutil.wl.policy.proto)
1138 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1140 surface = ecore_wl_window_surface_get(wlwin);
1141 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1142 EFL_UTIL_ERROR_INVALID_PARAMETER);
1144 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1147 if (scr_mode_info->wait_for_done)
1149 while (scr_mode_info->wait_for_done)
1152 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1156 switch (scr_mode_info->mode)
1158 case TIZEN_POLICY_MODE_DEFAULT: *mode = EFL_UTIL_SCREEN_MODE_DEFAULT; break;
1159 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
1160 default: *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1161 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1163 return EFL_UTIL_ERROR_NONE;
1167 *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1168 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1172 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1173 #endif /* end of WAYLAND */
1177 efl_util_set_window_screen_mode_error_cb(Evas_Object *window,
1178 efl_util_window_screen_mode_error_cb callback,
1181 Eina_Bool ret = EINA_FALSE;
1183 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1184 EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
1186 ret = _cb_info_add(window,
1187 (Efl_Util_Cb)callback,
1190 if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1193 if (!_eflutil.cb_handler[CBH_SCR_MODE].atom)
1194 _eflutil.cb_handler[CBH_SCR_MODE].atom = ecore_x_atom_get("_E_SCREEN_MODE_ACCESS_RESULT");
1195 #endif /* end of X11 */
1197 return EFL_UTIL_ERROR_NONE;
1201 efl_util_unset_window_screen_mode_error_cb(Evas_Object *window)
1203 Eina_Bool ret = EINA_FALSE;
1205 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1207 ret = _cb_info_del_by_win(window, CBH_SCR_MODE);
1208 if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1210 return EFL_UTIL_ERROR_NONE;
1214 efl_util_input_initialize_generator(efl_util_input_device_type_e dev_type)
1216 return EFL_UTIL_ERROR_NONE;
1220 efl_util_input_deinitialize_generator(void)
1226 efl_util_input_generate_key(const char *key_name,
1229 return EFL_UTIL_ERROR_NONE;
1233 efl_util_input_generate_touch(int idx,
1234 efl_util_input_touch_type_e touch_type,
1238 return EFL_UTIL_ERROR_NONE;
1241 struct _efl_util_screenshot_h
1247 Ecore_X_Display *dpy;
1248 int internal_display;
1263 int eventBase, errorBase;
1264 int dri2Major, dri2Minor;
1265 char *driver_name, *device_name;
1272 Eina_Bool shot_done;
1278 /* scrrenshot handle */
1279 static efl_util_screenshot_h g_screenshot;
1282 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1283 #define FOURCC_RGB32 FOURCC('R','G','B','4')
1284 #define TIMEOUT_CAPTURE 3
1286 /* x error handling */
1287 static Bool g_efl_util_x_error_caught;
1290 _efl_util_screenshot_x_error_handle(Display *dpy, XErrorEvent *ev)
1292 if (!g_screenshot || (dpy != g_screenshot->dpy))
1295 g_efl_util_x_error_caught = True;
1301 _efl_util_screenshot_get_port(Display *dpy, unsigned int id, Window win)
1303 unsigned int ver, rev, req_base, evt_base, err_base;
1304 unsigned int adaptors;
1305 XvAdaptorInfo *ai = NULL;
1306 XvImageFormatValues *fo = NULL;
1310 if (XvQueryExtension(dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1312 fprintf(stderr, "[screenshot] fail: no XV extension. \n");
1316 if (XvQueryAdaptors(dpy, win, &adaptors, &ai) != Success)
1318 fprintf(stderr, "[screenshot] fail: query adaptors. \n");
1322 EINA_SAFETY_ON_NULL_RETURN_VAL(ai, -1);
1324 for (i = 0; i < adaptors; i++)
1326 int support_format = False;
1328 if (!(ai[i].type & XvInputMask) ||
1329 !(ai[i].type & XvStillMask))
1334 fo = XvListImageFormats(dpy, p, &formats);
1335 for (j = 0; j < formats; j++)
1336 if (fo[j].id == (int)id)
1337 support_format = True;
1342 if (!support_format)
1345 for (; p < ai[i].base_id + ai[i].num_ports; p++)
1347 if (XvGrabPort(dpy, p, 0) == Success)
1349 XvFreeAdaptorInfo(ai);
1355 XvFreeAdaptorInfo(ai);
1362 static int _efl_util_screenshot_get_best_size(Display *dpy, int port, int width, int height, unsigned int *best_width, unsigned int *best_height)
1364 XErrorHandler old_handler = NULL;
1366 Atom atom_capture = XInternAtom(dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1368 g_efl_util_x_error_caught = False;
1369 old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1371 XvSetPortAttribute(dpy, port, atom_capture, 1);
1374 g_efl_util_x_error_caught = False;
1375 XSetErrorHandler(old_handler);
1377 XvQueryBestSize(dpy, port, 0, 0, 0, width, height, best_width, best_height);
1378 if (best_width <= 0 || best_height <= 0)
1385 API efl_util_screenshot_h efl_util_screenshot_initialize(int width, int height)
1388 efl_util_screenshot_h screenshot = NULL;
1390 int damage_err_base = 0;
1391 unsigned int best_width = 0;
1392 unsigned int best_height = 0;
1394 EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1395 EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1397 if (g_screenshot != NULL)
1399 if (g_screenshot->width != width || g_screenshot->height != height)
1401 // TODO: recreate pixmap and update information
1402 if (!_efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height))
1404 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1408 g_screenshot->width = width;
1409 g_screenshot->height = height;
1412 return g_screenshot;
1415 screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1416 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1419 screenshot->dpy = ecore_x_display_get();
1420 if (!screenshot->dpy)
1422 screenshot->dpy = XOpenDisplay(0);
1423 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_init);
1425 /* for XCloseDisplay at denitialization */
1426 screenshot->internal_display = 1;
1430 screenshot->screen = DefaultScreen(screenshot->dpy);
1432 /* set root window */
1433 screenshot->root = DefaultRootWindow(screenshot->dpy);
1435 /* initialize capture adaptor */
1436 screenshot->port = _efl_util_screenshot_get_port(screenshot->dpy, FOURCC_RGB32, screenshot->root);
1437 EINA_SAFETY_ON_FALSE_GOTO(screenshot->port > 0, fail_init);
1439 /* get the best size */
1440 _efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height);
1441 EINA_SAFETY_ON_FALSE_GOTO(best_width > 0, fail_init);
1442 EINA_SAFETY_ON_FALSE_GOTO(best_height > 0, fail_init);
1444 /* set the width and the height */
1445 screenshot->width = best_width;
1446 screenshot->height = best_height;
1448 /* create a pixmap */
1449 depth = DefaultDepth(screenshot->dpy, screenshot->screen);
1450 screenshot->pixmap = XCreatePixmap(screenshot->dpy, screenshot->root, screenshot->width, screenshot->height, depth);
1451 EINA_SAFETY_ON_FALSE_GOTO(screenshot->pixmap > 0, fail_init);
1453 screenshot->gc = XCreateGC(screenshot->dpy, screenshot->pixmap, 0, 0);
1454 EINA_SAFETY_ON_NULL_GOTO(screenshot->gc, fail_init);
1456 XSetForeground(screenshot->dpy, screenshot->gc, 0xFF000000);
1457 XFillRectangle(screenshot->dpy, screenshot->pixmap, screenshot->gc, 0, 0, width, height);
1459 /* initialize damage */
1460 if (!XDamageQueryExtension(screenshot->dpy, &screenshot->damage_base, &damage_err_base))
1463 screenshot->damage = XDamageCreate(screenshot->dpy, screenshot->pixmap, XDamageReportNonEmpty);
1464 EINA_SAFETY_ON_FALSE_GOTO(screenshot->damage > 0, fail_init);
1466 /* initialize dri3 and dri2 */
1467 if (!DRI2QueryExtension(screenshot->dpy, &screenshot->eventBase, &screenshot->errorBase))
1469 fprintf(stderr, "[screenshot] fail: DRI2QueryExtention\n");
1473 if (!DRI2QueryVersion(screenshot->dpy, &screenshot->dri2Major, &screenshot->dri2Minor))
1475 fprintf(stderr, "[screenshot] fail: DRI2QueryVersion\n");
1479 if (!DRI2Connect(screenshot->dpy, screenshot->root, &screenshot->driver_name, &screenshot->device_name))
1481 fprintf(stderr, "[screenshot] fail: DRI2Connect\n");
1485 screenshot->drm_fd = open(screenshot->device_name, O_RDWR);
1486 EINA_SAFETY_ON_FALSE_GOTO(screenshot->drm_fd >= 0, fail_init);
1488 if (drmGetMagic(screenshot->drm_fd, &screenshot->magic))
1490 fprintf(stderr, "[screenshot] fail: drmGetMagic\n");
1494 if (!DRI2Authenticate(screenshot->dpy, screenshot->root, screenshot->magic))
1496 fprintf(stderr, "[screenshot] fail: DRI2Authenticate\n");
1500 if (!drmAuthMagic(screenshot->drm_fd, screenshot->magic))
1502 fprintf(stderr, "[screenshot] fail: drmAuthMagic\n");
1506 DRI2CreateDrawable(screenshot->dpy, screenshot->pixmap);
1509 screenshot->bufmgr = tbm_bufmgr_init(screenshot->drm_fd);
1510 EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1512 XFlush(screenshot->dpy);
1514 g_screenshot = screenshot;
1515 set_last_result(EFL_UTIL_ERROR_NONE);
1517 return g_screenshot;
1521 efl_util_screenshot_h screenshot = NULL;
1523 if (!_eflutil.wl.shot.screenshooter)
1527 while (!_eflutil.wl.shot.screenshooter && ret != -1)
1528 ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1529 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.screenshooter, fail_init);
1531 _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
1532 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_init);
1535 EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1536 EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1540 if (g_screenshot->width != width || g_screenshot->height != height)
1542 g_screenshot->width = width;
1543 g_screenshot->height = height;
1546 return g_screenshot;
1549 screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1550 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1552 screenshot->width = width;
1553 screenshot->height = height;
1555 screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
1556 EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1558 g_screenshot = screenshot;
1559 set_last_result(EFL_UTIL_ERROR_NONE);
1561 screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
1563 return g_screenshot;
1567 efl_util_screenshot_deinitialize(screenshot);
1568 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1572 efl_util_screenshot_deinitialize(screenshot);
1573 set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1577 efl_util_screenshot_deinitialize(screenshot);
1578 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1582 API int efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
1586 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1589 if (screenshot->bufmgr)
1590 tbm_bufmgr_deinit(screenshot->bufmgr);
1592 DRI2DestroyDrawable(screenshot->dpy, screenshot->pixmap);
1595 if (screenshot->drm_fd)
1596 close(screenshot->drm_fd);
1597 if (screenshot->driver_name)
1598 free(screenshot->driver_name);
1599 if (screenshot->device_name)
1600 free(screenshot->device_name);
1603 if (screenshot->port > 0 && screenshot->pixmap > 0)
1604 XvStopVideo(screenshot->dpy, screenshot->port, screenshot->pixmap);
1607 if (screenshot->damage)
1608 XDamageDestroy(screenshot->dpy, screenshot->damage);
1612 XFreeGC(screenshot->dpy, screenshot->gc);
1615 if (screenshot->pixmap > 0)
1616 XFreePixmap(screenshot->dpy, screenshot->pixmap);
1619 if (screenshot->port > 0)
1620 XvUngrabPort(screenshot->dpy, screenshot->port, 0);
1622 XSync(screenshot->dpy, False);
1625 if (screenshot->internal_display ==1 && screenshot->dpy)
1626 XCloseDisplay(screenshot->dpy);
1629 g_screenshot = NULL;
1631 return EFL_UTIL_ERROR_NONE;
1635 return EFL_UTIL_ERROR_NONE;
1638 g_screenshot = NULL;
1640 if (_eflutil.wl.shot.screenshooter)
1641 screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, NULL);
1643 return EFL_UTIL_ERROR_NONE;
1648 API tbm_surface_h efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
1652 XErrorHandler old_handler = NULL;
1653 unsigned int attachment = DRI2BufferFrontLeft;
1655 DRI2Buffer *bufs = NULL;
1657 tbm_surface_h t_surface = NULL;
1660 tbm_surface_info_s surf_info;
1663 if (screenshot != g_screenshot)
1665 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1669 /* for flush other pending requests and pending events */
1670 XSync(screenshot->dpy, 0);
1672 g_efl_util_x_error_caught = False;
1673 old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1676 XvPutStill(screenshot->dpy, screenshot->port, screenshot->pixmap, screenshot->gc,
1677 0, 0, screenshot->width, screenshot->height,
1678 0, 0, screenshot->width, screenshot->height);
1680 XSync(screenshot->dpy, 0);
1682 if (g_efl_util_x_error_caught)
1684 g_efl_util_x_error_caught = False;
1685 XSetErrorHandler(old_handler);
1689 g_efl_util_x_error_caught = False;
1690 XSetErrorHandler(old_handler);
1692 if (XPending(screenshot->dpy))
1693 XNextEvent(screenshot->dpy, &ev);
1696 int fd = ConnectionNumber(screenshot->dpy);
1705 tv.tv_sec = TIMEOUT_CAPTURE;
1707 ret = select(fd + 1, &mask, 0, 0, &tv);
1709 fprintf(stderr, "[screenshot] fail: select.\n");
1711 fprintf(stderr, "[screenshot] fail: timeout(%d sec)!\n", TIMEOUT_CAPTURE);
1712 else if (XPending(screenshot->dpy))
1713 XNextEvent(screenshot->dpy, &ev);
1715 fprintf(stderr, "[screenshot] fail: not passed a event!\n");
1718 /* check if the capture is done by xserver and pixmap has got the captured image */
1719 if (ev.type == (screenshot->damage_base + XDamageNotify))
1721 XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1722 if (damage_ev->drawable == screenshot->pixmap)
1724 /* Get DRI2 FrontLeft buffer of the pixmap */
1725 bufs = DRI2GetBuffers(screenshot->dpy, screenshot->pixmap, &buf_width, &buf_height, &attachment, 1, &nbufs);
1728 fprintf(stderr, "[screenshot] fail: DRI2GetBuffers\n");
1732 t_bo = tbm_bo_import(screenshot->bufmgr, bufs[0].name);
1735 fprintf(stderr, "[screenshot] fail: import tbm_bo!\n");
1739 surf_info.width = buf_width;
1740 surf_info.height = buf_height;
1741 surf_info.format = TBM_FORMAT_XRGB8888;
1743 surf_info.size = bufs->pitch * surf_info.height;
1744 surf_info.num_planes = 1;
1745 for (i = 0; i < surf_info.num_planes; i++)
1747 surf_info.planes[i].size = bufs->pitch * surf_info.height;
1748 surf_info.planes[i].stride = bufs->pitch;
1749 surf_info.planes[i].offset = 0;
1751 t_surface = tbm_surface_internal_create_with_bos(&surf_info, &t_bo, 1);
1754 fprintf(stderr, "[screenshot] fail: get tbm_surface!\n");
1761 XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1763 set_last_result(EFL_UTIL_ERROR_NONE);
1768 XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1778 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1784 tbm_surface_h t_surface = NULL;
1785 struct wl_buffer *buffer = NULL;
1786 Efl_Util_Wl_Output_Info *output;
1789 if (screenshot != g_screenshot)
1791 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1795 output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
1798 fprintf(stderr, "[screenshot] fail: no output for screenshot\n");
1802 t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
1805 fprintf(stderr, "[screenshot] fail: tbm_surface_create\n");
1809 buffer = wayland_tbm_client_create_buffer(_eflutil.wl.shot.tbm_client, t_surface);
1812 fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n");
1816 screenshooter_shoot(_eflutil.wl.shot.screenshooter, output->output, buffer);
1818 screenshot->shot_done = EINA_FALSE;
1819 while (!screenshot->shot_done && ret != -1)
1820 ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1824 fprintf(stderr, "[screenshot] fail: screenshooter_shoot\n");
1828 wl_buffer_destroy(buffer);
1830 /* reset shot_done for next screenshot */
1831 screenshot->shot_done = EINA_FALSE;
1837 tbm_surface_destroy(t_surface);
1839 wl_buffer_destroy(buffer);
1841 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);