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
69 void *surface; /* wl_surface */
71 Eina_Bool wait_for_done;
73 } Efl_Util_Wl_Surface_Lv_Info;
75 typedef struct _Efl_Util_Wl_Surface_Scr_Mode_Info
77 void *surface; /* wl_surface */
79 Eina_Bool wait_for_done;
81 } Efl_Util_Wl_Surface_Scr_Mode_Info;
83 typedef struct _Efl_Util_Wl_Output_Info
85 struct wl_output *output;
86 int offset_x, offset_y, width, height;
87 } Efl_Util_Wl_Output_Info;
90 typedef struct _Efl_Util_Data
93 /* x11 related stuffs */
97 Ecore_Event_Handler *handler; /* x11 client message handler */
100 #endif /* end of X11 */
103 /* wayland related stuffs */
107 struct wl_display *dpy;
108 struct wl_event_queue *queue;
112 struct tizen_policy *proto;
113 Eina_Hash *hash_noti_lv;
114 Eina_Hash *hash_scr_mode;
118 struct screenshooter *screenshooter;
119 struct wayland_tbm_client *tbm_client;
120 Eina_List *output_list;
123 #endif /* end of WAYLAND */
127 Eina_List *info_list; /* list of callback info */
128 unsigned int atom; /* x11 atom */
129 } cb_handler[CBH_MAX];
132 static Efl_Util_Data _eflutil =
140 #endif /* end of X11 */
145 { NULL, NULL, NULL }, /* tizen_policy protocol */
146 { NULL, NULL, NULL } /* screenshooter protocol */
148 #endif /* end of WAYLAND */
150 { NULL, 0 }, /* handler for notification level */
151 { NULL, 0 } /* handler for screen mode */
155 static Eina_Bool _cb_info_add(Evas_Object *win, Efl_Util_Cb cb, void *data, int idx);
156 static Eina_Bool _cb_info_del_by_win(Evas_Object *win, int idx);
157 static Eina_List *_cb_info_list_get(int idx);
158 static Efl_Util_Callback_Info *_cb_info_find_by_win(Evas_Object *win, int idx);
160 static Efl_Util_Callback_Info *_cb_info_find_by_xwin(unsigned int xwin, int idx);
161 static Eina_Bool _cb_x11_client_msg(void *data, int type, void *event);
162 static Eina_Bool _x11_init(void);
163 #endif /* end of X11 */
165 static Eina_Bool _wl_init(void);
166 static void _cb_wl_reg_global(void *data, struct wl_registry *reg, unsigned int name, const char *interface, unsigned int version);
167 static void _cb_wl_reg_global_remove(void *data, struct wl_registry *reg, unsigned int name);
168 static Efl_Util_Callback_Info *_cb_info_find_by_wlsurf(void *wlsurf, int idx);
169 static void _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface, uint32_t is_conformant);
170 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);
171 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);
172 static void _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id);
173 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);
174 static void _cb_wl_tz_policy_iconify_state_changed(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, uint32_t iconified, uint32_t force);
175 static void _cb_wl_tz_policy_supported_aux_hints(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, struct wl_array *hints, uint32_t num_hints);
176 static void _cb_wl_tz_policy_allowed_aux_hint(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, int id);
178 static const struct wl_registry_listener _wl_reg_listener =
181 _cb_wl_reg_global_remove
184 struct tizen_policy_listener _wl_tz_policy_listener =
186 _cb_wl_tz_policy_conformant,
187 _cb_wl_tz_policy_conformant_area,
188 _cb_wl_tz_policy_notification_done,
189 _cb_wl_tz_policy_transient_for_done,
190 _cb_wl_tz_policy_scr_mode_done,
191 _cb_wl_tz_policy_iconify_state_changed,
192 _cb_wl_tz_policy_supported_aux_hints,
193 _cb_wl_tz_policy_allowed_aux_hint,
195 #endif /* end of WAYLAND */
198 _cb_info_add(Evas_Object *win,
203 Efl_Util_Callback_Info *info;
205 info = _cb_info_find_by_win(win, idx);
208 _eflutil.cb_handler[idx].info_list
209 = eina_list_remove(_eflutil.cb_handler[idx].info_list,
214 info = (Efl_Util_Callback_Info *)calloc(1, sizeof(Efl_Util_Callback_Info));
215 if (!info) return EINA_FALSE;
221 _eflutil.cb_handler[idx].info_list
222 = eina_list_append(_eflutil.cb_handler[idx].info_list,
226 if (!_eflutil.x11.handler)
227 _eflutil.x11.handler = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
230 #endif /* end of X11 */
236 _cb_info_del_by_win(Evas_Object *win,
239 Efl_Util_Callback_Info *info;
241 info = _cb_info_find_by_win(win, idx);
242 if (!info) return EINA_FALSE;
244 _eflutil.cb_handler[idx].info_list
245 = eina_list_remove(_eflutil.cb_handler[idx].info_list,
250 unsigned int count = eina_list_count(_eflutil.cb_handler[idx].info_list);
251 if ((count == 0) && (_eflutil.x11.handler))
253 ecore_event_handler_del(_eflutil.x11.handler);
254 _eflutil.x11.handler = NULL;
262 _cb_info_list_get(int idx)
264 return _eflutil.cb_handler[idx].info_list;
267 static Efl_Util_Callback_Info *
268 _cb_info_find_by_win(Evas_Object *win,
272 Efl_Util_Callback_Info *info;
274 l = _cb_info_list_get(idx);
275 EINA_LIST_FOREACH(l, ll, info)
277 if (info->win == win) return info;
284 static Efl_Util_Callback_Info *
285 _cb_info_find_by_xwin(unsigned int xwin,
289 Efl_Util_Callback_Info *info;
292 l = _cb_info_list_get(idx);
293 EINA_LIST_FOREACH(l, ll, info)
295 xwin2 = elm_win_xwindow_get(info->win);
296 if (xwin == xwin2) return info;
303 _cb_x11_client_msg(void *data,
307 Ecore_X_Event_Client_Message *ev;
309 Efl_Util_Callback_Info *info;
312 if (!ev) return ECORE_CALLBACK_PASS_ON;
315 if (xwin == 0) return ECORE_CALLBACK_PASS_ON;
317 if (ev->message_type == _eflutil.cb_handler[CBH_NOTI_LEV].atom)
319 info = _cb_info_find_by_xwin(xwin, CBH_NOTI_LEV);
321 /* permission denied */
322 if ((ev->data.l[1] == 0) &&
327 EFL_UTIL_ERROR_PERMISSION_DENIED,
331 else if (ev->message_type == _eflutil.cb_handler[CBH_SCR_MODE].atom)
333 info = _cb_info_find_by_xwin(xwin, CBH_SCR_MODE);
335 /* permission denied */
336 if ((ev->data.l[1] == 0) &&
341 EFL_UTIL_ERROR_PERMISSION_DENIED,
345 return ECORE_CALLBACK_PASS_ON;
351 if (_eflutil.x11.init) return EINA_TRUE;
353 _eflutil.x11.dpy = ecore_x_display_get();
354 EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.x11.dpy, EINA_FALSE);
356 _eflutil.x11.init = EINA_TRUE;
360 #endif /* end of X11 */
366 struct wl_registry *reg = NULL;
368 if (_eflutil.wl.init) return EINA_TRUE;
372 _eflutil.wl.dpy = ecore_wl_display_get();
373 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.dpy, fail);
375 _eflutil.wl.queue = wl_display_create_queue(_eflutil.wl.dpy);
376 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.queue, fail);
378 reg = wl_display_get_registry(_eflutil.wl.dpy);
379 EINA_SAFETY_ON_NULL_GOTO(reg, fail);
381 wl_proxy_set_queue((struct wl_proxy*)reg, _eflutil.wl.queue);
382 wl_registry_add_listener(reg, &_wl_reg_listener, NULL);
384 _eflutil.wl.init = EINA_TRUE;
388 if (_eflutil.wl.queue)
390 wl_event_queue_destroy(_eflutil.wl.queue);
391 _eflutil.wl.queue = NULL;
395 wl_registry_destroy(reg);
401 _cb_wl_output_geometry(void *data, struct wl_output *wl_output, int x, int y,
402 int physical_width, int physical_height, int subpixel,
403 const char *make, const char *model, int transform)
405 Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
406 if (wl_output == output->output)
408 output->offset_x = x;
409 output->offset_y = y;
414 _cb_wl_output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
415 int width, int height, int refresh)
417 Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
418 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT))
420 output->width = width;
421 output->height = height;
426 _cb_wl_output_done(void *data, struct wl_output *wl_output)
431 _cb_wl_output_scale(void *data, struct wl_output *wl_output, int32_t factor)
435 static const struct wl_output_listener output_listener =
437 _cb_wl_output_geometry,
444 _cb_wl_screenshot_done(void *data, struct screenshooter *screenshooter)
446 Eina_Bool *shot_done = (Eina_Bool*)data;
448 *shot_done = EINA_TRUE;
451 static const struct screenshooter_listener screenshooter_listener =
453 _cb_wl_screenshot_done
457 _cb_wl_reg_global(void *data,
458 struct wl_registry *reg,
460 const char *interface,
461 unsigned int version)
463 if (!strcmp(interface, "tizen_policy"))
465 struct tizen_policy *proto;
466 proto = wl_registry_bind(reg,
468 &tizen_policy_interface,
472 tizen_policy_add_listener(proto,
473 &_wl_tz_policy_listener,
476 _eflutil.wl.policy.hash_noti_lv = eina_hash_pointer_new(free);
477 _eflutil.wl.policy.hash_scr_mode = eina_hash_pointer_new(free);
478 _eflutil.wl.policy.proto = proto;
480 else if (strcmp(interface, "wl_output") == 0)
482 Efl_Util_Wl_Output_Info *output = calloc(1, sizeof(Efl_Util_Wl_Output_Info));
483 EINA_SAFETY_ON_NULL_RETURN(output);
485 _eflutil.wl.shot.output_list = eina_list_append(_eflutil.wl.shot.output_list, output);
487 output->output = wl_registry_bind(reg, name, &wl_output_interface, version);
488 wl_output_add_listener(output->output, &output_listener, output);
490 else if (strcmp(interface, "screenshooter") == 0)
492 _eflutil.wl.shot.screenshooter = wl_registry_bind(reg, name, &screenshooter_interface, version);
493 screenshooter_add_listener(_eflutil.wl.shot.screenshooter, &screenshooter_listener, NULL);
498 _cb_wl_reg_global_remove(void *data,
499 struct wl_registry *reg,
502 _eflutil.wl.policy.proto = NULL;
503 eina_hash_free(_eflutil.wl.policy.hash_noti_lv);
504 eina_hash_free(_eflutil.wl.policy.hash_scr_mode);
507 static Efl_Util_Callback_Info *
508 _cb_info_find_by_wlsurf(void *wlsurf,
512 Efl_Util_Callback_Info *info;
513 Ecore_Wl_Window *wlwin2 = NULL;
514 void *wlsurf2 = NULL;
516 l = _cb_info_list_get(idx);
517 EINA_LIST_FOREACH(l, ll, info)
519 wlwin2 = elm_win_wl_window_get(info->win);
520 wlsurf2 = ecore_wl_window_surface_get(wlwin2);
521 if (wlsurf== wlsurf2) return info;
528 _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy,
529 struct wl_surface *surface, uint32_t is_conformant)
534 _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy,
535 struct wl_surface *surface, uint32_t conformant_part,
536 uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h)
541 _cb_wl_tz_policy_notification_done(void *data,
542 struct tizen_policy *tizen_policy,
543 struct wl_surface *surface,
547 Efl_Util_Wl_Surface_Lv_Info *lv_info;
548 Efl_Util_Callback_Info *cb_info;
550 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
553 lv_info->level = level;
554 lv_info->wait_for_done = EINA_FALSE;
555 lv_info->state = state;
558 if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
560 cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_NOTI_LEV);
561 if (!cb_info) return;
562 if (!cb_info->cb) return;
564 cb_info->cb(cb_info->win,
565 EFL_UTIL_ERROR_PERMISSION_DENIED,
570 _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id)
575 _cb_wl_tz_policy_scr_mode_done(void *data,
576 struct tizen_policy *tizen_policy,
577 struct wl_surface *surface,
582 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
583 Efl_Util_Callback_Info *cb_info;
585 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
588 scr_mode_info->mode = mode;
589 scr_mode_info->wait_for_done = EINA_FALSE;
590 scr_mode_info->state = state;
593 if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
595 cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_SCR_MODE);
596 if (!cb_info) return;
597 if (!cb_info->cb) return;
599 cb_info->cb(cb_info->win,
600 EFL_UTIL_ERROR_PERMISSION_DENIED,
604 static void _cb_wl_tz_policy_iconify_state_changed(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, uint32_t iconified, uint32_t force)
608 static void _cb_wl_tz_policy_supported_aux_hints(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, struct wl_array *hints, uint32_t num_hints)
612 static void _cb_wl_tz_policy_allowed_aux_hint(void *data, struct tizen_policy *tizen_policy, struct wl_surface *surface_resource, int id)
617 _cb_window_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
619 Efl_Util_Wl_Surface_Lv_Info *lv_info;
622 if (EINA_UNLIKELY(!lv_info))
625 eina_hash_del(_eflutil.wl.policy.hash_noti_lv, &lv_info->surface, lv_info);
627 #endif /* end of WAYLAND */
630 efl_util_set_notification_window_level(Evas_Object *window,
631 efl_util_notification_level_e level)
635 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
636 EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_NONE) &&
637 (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
638 EFL_UTIL_ERROR_INVALID_PARAMETER);
641 Ecore_X_Window_Type window_type;
645 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
647 xwin = elm_win_xwindow_get(window);
650 if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
652 // success to get window type
653 if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
655 // given EFL window's type is not notification type.
656 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
660 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
662 utilx_set_system_notification_level(_eflutil.x11.dpy,
665 return EFL_UTIL_ERROR_NONE;
668 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
669 #endif /* end of X11 */
673 Ecore_Wl_Window *wlwin;
674 struct wl_surface *surface;
675 Efl_Util_Wl_Surface_Lv_Info *lv_info;
676 Ecore_Wl_Window_Type wl_type;
679 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
681 wlwin = elm_win_wl_window_get(window);
682 EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
684 type = elm_win_type_get(window);
685 if (type != ELM_WIN_NOTIFICATION)
687 wl_type = ecore_wl_window_type_get(wlwin);
688 EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
689 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
692 while (!_eflutil.wl.policy.proto)
693 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
695 surface = ecore_wl_window_surface_get(wlwin);
696 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
697 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
699 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
702 lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
703 EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
705 lv_info->surface = surface;
706 lv_info->level = (int)level;
707 lv_info->wait_for_done = EINA_TRUE;
708 lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
709 eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
713 evas_object_event_callback_add(window, EVAS_CALLBACK_DEL,
714 _cb_window_del, lv_info);
718 lv_info->level = (int)level;
719 lv_info->wait_for_done = EINA_TRUE;
720 lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
724 tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
725 surface, (int)level);
727 if (lv_info->wait_for_done)
730 while (lv_info->wait_for_done && (count < 3))
733 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
737 if (lv_info->wait_for_done)
739 return EFL_UTIL_ERROR_INVALID_PARAMETER;
743 if (lv_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
745 return EFL_UTIL_ERROR_PERMISSION_DENIED;
750 return EFL_UTIL_ERROR_NONE;
751 #endif /* end of WAYLAND */
755 efl_util_get_notification_window_level(Evas_Object *window,
756 efl_util_notification_level_e *level)
760 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
761 EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
764 Ecore_X_Window_Type window_type;
765 Utilx_Notification_Level utilx_level;
769 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
771 xwin = elm_win_xwindow_get(window);
774 if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
776 // success to get window type
777 if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
779 // given EFL window's type is not notification type.
780 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
783 utilx_level = utilx_get_system_notification_level(_eflutil.x11.dpy, xwin);
784 if (utilx_level == UTILX_NOTIFICATION_LEVEL_LOW)
785 *level = EFL_UTIL_NOTIFICATION_LEVEL_1;
786 else if(utilx_level == UTILX_NOTIFICATION_LEVEL_NORMAL)
787 *level = EFL_UTIL_NOTIFICATION_LEVEL_2;
788 else if(utilx_level == UTILX_NOTIFICATION_LEVEL_HIGH)
789 *level = EFL_UTIL_NOTIFICATION_LEVEL_3;
791 return EFL_UTIL_ERROR_INVALID_PARAMETER;
794 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
796 return EFL_UTIL_ERROR_NONE;
799 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
800 #endif /* end of X11 */
804 Ecore_Wl_Window *wlwin;
805 struct wl_surface *surface;
806 Efl_Util_Wl_Surface_Lv_Info *lv_info;
807 Ecore_Wl_Window_Type wl_type;
810 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
812 wlwin = elm_win_wl_window_get(window);
813 EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
815 type = elm_win_type_get(window);
816 if (type != ELM_WIN_NOTIFICATION)
818 wl_type = ecore_wl_window_type_get(wlwin);
819 EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
820 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
823 while (!_eflutil.wl.policy.proto)
824 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
826 surface = ecore_wl_window_surface_get(wlwin);
827 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
828 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
830 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
833 if (lv_info->wait_for_done)
836 while ((lv_info->wait_for_done) && (count < 3))
839 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
843 if (lv_info->wait_for_done)
845 *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
846 return EFL_UTIL_ERROR_INVALID_PARAMETER;
850 switch (lv_info->level)
852 case TIZEN_POLICY_LEVEL_1: *level = EFL_UTIL_NOTIFICATION_LEVEL_1; break;
853 case TIZEN_POLICY_LEVEL_2: *level = EFL_UTIL_NOTIFICATION_LEVEL_2; break;
854 case TIZEN_POLICY_LEVEL_3: *level = EFL_UTIL_NOTIFICATION_LEVEL_3; break;
855 case TIZEN_POLICY_LEVEL_NONE: *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE; break;
856 case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
857 case TIZEN_POLICY_LEVEL_MEDIUM: *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM; break;
858 case TIZEN_POLICY_LEVEL_HIGH: *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH; break;
859 case TIZEN_POLICY_LEVEL_TOP: *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP; break;
860 default: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
861 return EFL_UTIL_ERROR_INVALID_PARAMETER;
863 return EFL_UTIL_ERROR_NONE;
866 *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
868 return EFL_UTIL_ERROR_NONE;
869 #endif /* end of WAYLAND */
873 efl_util_set_notification_window_level_error_cb(Evas_Object *window,
874 efl_util_notification_window_level_error_cb callback,
877 Eina_Bool ret = EINA_FALSE;
879 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
880 EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
882 ret = _cb_info_add(window,
883 (Efl_Util_Cb)callback,
886 if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
889 if (!_eflutil.cb_handler[CBH_NOTI_LEV].atom)
890 _eflutil.cb_handler[CBH_NOTI_LEV].atom = ecore_x_atom_get("_E_NOTIFICATION_LEVEL_ACCESS_RESULT");
891 #endif /* end of X11 */
893 return EFL_UTIL_ERROR_NONE;
897 efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
899 Eina_Bool ret = EINA_FALSE;
901 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
903 ret = _cb_info_del_by_win(window, CBH_NOTI_LEV);
904 if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
906 return EFL_UTIL_ERROR_NONE;
910 efl_util_set_window_opaque_state(Evas_Object *window,
915 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
916 EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
917 EFL_UTIL_ERROR_INVALID_PARAMETER);
921 Utilx_Opaque_State state;
925 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
927 xwin = elm_win_xwindow_get(window);
928 EINA_SAFETY_ON_FALSE_RETURN_VAL(xwin > 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
931 state = UTILX_OPAQUE_STATE_ON;
933 state = UTILX_OPAQUE_STATE_OFF;
935 ret = utilx_set_window_opaque_state(_eflutil.x11.dpy,
940 return EFL_UTIL_ERROR_INVALID_PARAMETER;
942 return EFL_UTIL_ERROR_NONE;
943 #endif /* end of X11 */
946 Ecore_Wl_Window *wlwin;
947 struct wl_surface *surface;
949 if (!_eflutil.wl.policy.proto)
954 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
956 while (!_eflutil.wl.policy.proto && ret != -1)
957 ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
959 EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.policy.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
962 wlwin = elm_win_wl_window_get(window);
964 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
966 surface = ecore_wl_window_surface_get(wlwin);
968 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
970 tizen_policy_set_opaque_state(_eflutil.wl.policy.proto, surface, opaque);
972 return EFL_UTIL_ERROR_NONE;
973 #endif /* end of WAYLAND */
977 efl_util_set_window_screen_mode(Evas_Object *window,
978 efl_util_screen_mode_e mode)
980 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
981 EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
982 (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
983 EFL_UTIL_ERROR_INVALID_PARAMETER);
990 e = evas_object_evas_get(window);
991 EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
993 ee = ecore_evas_ecore_evas_get(e);
994 EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
996 id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
997 if (mode == EFL_UTIL_SCREEN_MODE_ALWAYS_ON)
1000 ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "1");
1002 ecore_evas_aux_hint_val_set(ee, id, "1");
1004 else if (mode == EFL_UTIL_SCREEN_MODE_DEFAULT)
1007 ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "0");
1009 ecore_evas_aux_hint_val_set(ee, id, "0");
1012 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1014 return EFL_UTIL_ERROR_NONE;
1015 #endif /* end of X11 */
1018 Ecore_Wl_Window *wlwin;
1019 struct wl_surface *surface;
1020 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1024 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1026 wlwin = elm_win_wl_window_get(window);
1029 while (!_eflutil.wl.policy.proto)
1030 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1032 surface = ecore_wl_window_surface_get(wlwin);
1033 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1034 EFL_UTIL_ERROR_INVALID_PARAMETER);
1036 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1039 scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
1040 EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
1042 scr_mode_info->surface = surface;
1043 scr_mode_info->mode = (unsigned int)mode;
1044 scr_mode_info->wait_for_done = EINA_TRUE;
1045 scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1047 eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
1053 scr_mode_info->mode = (unsigned int)mode;
1054 scr_mode_info->wait_for_done = EINA_TRUE;
1055 scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1058 tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
1059 surface, (unsigned int)mode);
1060 if (scr_mode_info->wait_for_done)
1063 while (scr_mode_info->wait_for_done && (count < 3))
1066 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1070 if (scr_mode_info->wait_for_done)
1072 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1076 if (scr_mode_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
1078 return EFL_UTIL_ERROR_PERMISSION_DENIED;
1083 return EFL_UTIL_ERROR_NONE;
1086 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1087 #endif /* end of WAYLAND */
1091 efl_util_get_window_screen_mode(Evas_Object *window,
1092 efl_util_screen_mode_e *mode)
1094 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1095 EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
1103 e = evas_object_evas_get(window);
1104 EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
1106 ee = ecore_evas_ecore_evas_get(e);
1107 EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
1109 id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
1110 EINA_SAFETY_ON_TRUE_RETURN_VAL((id == -1), EFL_UTIL_ERROR_INVALID_PARAMETER);
1112 str = ecore_evas_aux_hint_val_get(ee, id);
1113 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EFL_UTIL_ERROR_INVALID_PARAMETER);
1115 if (strncmp(str, "1", strlen("1")) == 0)
1116 *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON;
1118 *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1120 return EFL_UTIL_ERROR_NONE;
1121 #endif /* end of X11 */
1124 Ecore_Wl_Window *wlwin;
1125 struct wl_surface *surface;
1126 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1130 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1132 wlwin = elm_win_wl_window_get(window);
1135 while (!_eflutil.wl.policy.proto)
1136 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1138 surface = ecore_wl_window_surface_get(wlwin);
1139 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1140 EFL_UTIL_ERROR_INVALID_PARAMETER);
1142 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1145 if (scr_mode_info->wait_for_done)
1147 while (scr_mode_info->wait_for_done)
1150 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1154 switch (scr_mode_info->mode)
1156 case TIZEN_POLICY_MODE_DEFAULT: *mode = EFL_UTIL_SCREEN_MODE_DEFAULT; break;
1157 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
1158 default: *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1159 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1161 return EFL_UTIL_ERROR_NONE;
1165 *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1166 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1170 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1171 #endif /* end of WAYLAND */
1175 efl_util_set_window_screen_mode_error_cb(Evas_Object *window,
1176 efl_util_window_screen_mode_error_cb callback,
1179 Eina_Bool ret = EINA_FALSE;
1181 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1182 EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
1184 ret = _cb_info_add(window,
1185 (Efl_Util_Cb)callback,
1188 if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1191 if (!_eflutil.cb_handler[CBH_SCR_MODE].atom)
1192 _eflutil.cb_handler[CBH_SCR_MODE].atom = ecore_x_atom_get("_E_SCREEN_MODE_ACCESS_RESULT");
1193 #endif /* end of X11 */
1195 return EFL_UTIL_ERROR_NONE;
1199 efl_util_unset_window_screen_mode_error_cb(Evas_Object *window)
1201 Eina_Bool ret = EINA_FALSE;
1203 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1205 ret = _cb_info_del_by_win(window, CBH_SCR_MODE);
1206 if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1208 return EFL_UTIL_ERROR_NONE;
1212 efl_util_input_initialize_generator(efl_util_input_device_type_e dev_type)
1214 return EFL_UTIL_ERROR_NONE;
1218 efl_util_input_deinitialize_generator(void)
1224 efl_util_input_generate_key(const char *key_name,
1227 return EFL_UTIL_ERROR_NONE;
1231 efl_util_input_generate_touch(int idx,
1232 efl_util_input_touch_type_e touch_type,
1236 return EFL_UTIL_ERROR_NONE;
1239 struct _efl_util_screenshot_h
1245 Ecore_X_Display *dpy;
1246 int internal_display;
1261 int eventBase, errorBase;
1262 int dri2Major, dri2Minor;
1263 char *driver_name, *device_name;
1270 Eina_Bool shot_done;
1276 /* scrrenshot handle */
1277 static efl_util_screenshot_h g_screenshot;
1280 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1281 #define FOURCC_RGB32 FOURCC('R','G','B','4')
1282 #define TIMEOUT_CAPTURE 3
1284 /* x error handling */
1285 static Bool g_efl_util_x_error_caught;
1288 _efl_util_screenshot_x_error_handle(Display *dpy, XErrorEvent *ev)
1290 if (!g_screenshot || (dpy != g_screenshot->dpy))
1293 g_efl_util_x_error_caught = True;
1299 _efl_util_screenshot_get_port(Display *dpy, unsigned int id, Window win)
1301 unsigned int ver, rev, req_base, evt_base, err_base;
1302 unsigned int adaptors;
1303 XvAdaptorInfo *ai = NULL;
1304 XvImageFormatValues *fo = NULL;
1308 if (XvQueryExtension(dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1310 fprintf(stderr, "[screenshot] fail: no XV extension. \n");
1314 if (XvQueryAdaptors(dpy, win, &adaptors, &ai) != Success)
1316 fprintf(stderr, "[screenshot] fail: query adaptors. \n");
1320 EINA_SAFETY_ON_NULL_RETURN_VAL(ai, -1);
1322 for (i = 0; i < adaptors; i++)
1324 int support_format = False;
1326 if (!(ai[i].type & XvInputMask) ||
1327 !(ai[i].type & XvStillMask))
1332 fo = XvListImageFormats(dpy, p, &formats);
1333 for (j = 0; j < formats; j++)
1334 if (fo[j].id == (int)id)
1335 support_format = True;
1340 if (!support_format)
1343 for (; p < ai[i].base_id + ai[i].num_ports; p++)
1345 if (XvGrabPort(dpy, p, 0) == Success)
1347 XvFreeAdaptorInfo(ai);
1353 XvFreeAdaptorInfo(ai);
1360 static int _efl_util_screenshot_get_best_size(Display *dpy, int port, int width, int height, unsigned int *best_width, unsigned int *best_height)
1362 XErrorHandler old_handler = NULL;
1364 Atom atom_capture = XInternAtom(dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1366 g_efl_util_x_error_caught = False;
1367 old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1369 XvSetPortAttribute(dpy, port, atom_capture, 1);
1372 g_efl_util_x_error_caught = False;
1373 XSetErrorHandler(old_handler);
1375 XvQueryBestSize(dpy, port, 0, 0, 0, width, height, best_width, best_height);
1376 if (best_width <= 0 || best_height <= 0)
1383 API efl_util_screenshot_h efl_util_screenshot_initialize(int width, int height)
1386 efl_util_screenshot_h screenshot = NULL;
1388 int damage_err_base = 0;
1389 unsigned int best_width = 0;
1390 unsigned int best_height = 0;
1392 EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1393 EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1395 if (g_screenshot != NULL)
1397 if (g_screenshot->width != width || g_screenshot->height != height)
1399 // TODO: recreate pixmap and update information
1400 if (!_efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height))
1402 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1406 g_screenshot->width = width;
1407 g_screenshot->height = height;
1410 return g_screenshot;
1413 screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1414 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1417 screenshot->dpy = ecore_x_display_get();
1418 if (!screenshot->dpy)
1420 screenshot->dpy = XOpenDisplay(0);
1421 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_init);
1423 /* for XCloseDisplay at denitialization */
1424 screenshot->internal_display = 1;
1428 screenshot->screen = DefaultScreen(screenshot->dpy);
1430 /* set root window */
1431 screenshot->root = DefaultRootWindow(screenshot->dpy);
1433 /* initialize capture adaptor */
1434 screenshot->port = _efl_util_screenshot_get_port(screenshot->dpy, FOURCC_RGB32, screenshot->root);
1435 EINA_SAFETY_ON_FALSE_GOTO(screenshot->port > 0, fail_init);
1437 /* get the best size */
1438 _efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height);
1439 EINA_SAFETY_ON_FALSE_GOTO(best_width > 0, fail_init);
1440 EINA_SAFETY_ON_FALSE_GOTO(best_height > 0, fail_init);
1442 /* set the width and the height */
1443 screenshot->width = best_width;
1444 screenshot->height = best_height;
1446 /* create a pixmap */
1447 depth = DefaultDepth(screenshot->dpy, screenshot->screen);
1448 screenshot->pixmap = XCreatePixmap(screenshot->dpy, screenshot->root, screenshot->width, screenshot->height, depth);
1449 EINA_SAFETY_ON_FALSE_GOTO(screenshot->pixmap > 0, fail_init);
1451 screenshot->gc = XCreateGC(screenshot->dpy, screenshot->pixmap, 0, 0);
1452 EINA_SAFETY_ON_NULL_GOTO(screenshot->gc, fail_init);
1454 XSetForeground(screenshot->dpy, screenshot->gc, 0xFF000000);
1455 XFillRectangle(screenshot->dpy, screenshot->pixmap, screenshot->gc, 0, 0, width, height);
1457 /* initialize damage */
1458 if (!XDamageQueryExtension(screenshot->dpy, &screenshot->damage_base, &damage_err_base))
1461 screenshot->damage = XDamageCreate(screenshot->dpy, screenshot->pixmap, XDamageReportNonEmpty);
1462 EINA_SAFETY_ON_FALSE_GOTO(screenshot->damage > 0, fail_init);
1464 /* initialize dri3 and dri2 */
1465 if (!DRI2QueryExtension(screenshot->dpy, &screenshot->eventBase, &screenshot->errorBase))
1467 fprintf(stderr, "[screenshot] fail: DRI2QueryExtention\n");
1471 if (!DRI2QueryVersion(screenshot->dpy, &screenshot->dri2Major, &screenshot->dri2Minor))
1473 fprintf(stderr, "[screenshot] fail: DRI2QueryVersion\n");
1477 if (!DRI2Connect(screenshot->dpy, screenshot->root, &screenshot->driver_name, &screenshot->device_name))
1479 fprintf(stderr, "[screenshot] fail: DRI2Connect\n");
1483 screenshot->drm_fd = open(screenshot->device_name, O_RDWR);
1484 EINA_SAFETY_ON_FALSE_GOTO(screenshot->drm_fd >= 0, fail_init);
1486 if (drmGetMagic(screenshot->drm_fd, &screenshot->magic))
1488 fprintf(stderr, "[screenshot] fail: drmGetMagic\n");
1492 if (!DRI2Authenticate(screenshot->dpy, screenshot->root, screenshot->magic))
1494 fprintf(stderr, "[screenshot] fail: DRI2Authenticate\n");
1498 if (!drmAuthMagic(screenshot->drm_fd, screenshot->magic))
1500 fprintf(stderr, "[screenshot] fail: drmAuthMagic\n");
1504 DRI2CreateDrawable(screenshot->dpy, screenshot->pixmap);
1507 screenshot->bufmgr = tbm_bufmgr_init(screenshot->drm_fd);
1508 EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1510 XFlush(screenshot->dpy);
1512 g_screenshot = screenshot;
1513 set_last_result(EFL_UTIL_ERROR_NONE);
1515 return g_screenshot;
1519 efl_util_screenshot_h screenshot = NULL;
1521 if (!_eflutil.wl.shot.screenshooter)
1525 while (!_eflutil.wl.shot.screenshooter && ret != -1)
1526 ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1527 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.screenshooter, fail_init);
1529 _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
1530 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_init);
1533 EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1534 EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1538 if (g_screenshot->width != width || g_screenshot->height != height)
1540 g_screenshot->width = width;
1541 g_screenshot->height = height;
1544 return g_screenshot;
1547 screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1548 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1550 screenshot->width = width;
1551 screenshot->height = height;
1553 screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
1554 EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1556 g_screenshot = screenshot;
1557 set_last_result(EFL_UTIL_ERROR_NONE);
1559 screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
1561 return g_screenshot;
1565 efl_util_screenshot_deinitialize(screenshot);
1566 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1570 efl_util_screenshot_deinitialize(screenshot);
1571 set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1575 efl_util_screenshot_deinitialize(screenshot);
1576 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1580 API int efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
1584 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1587 if (screenshot->bufmgr)
1588 tbm_bufmgr_deinit(screenshot->bufmgr);
1590 DRI2DestroyDrawable(screenshot->dpy, screenshot->pixmap);
1593 if (screenshot->drm_fd)
1594 close(screenshot->drm_fd);
1595 if (screenshot->driver_name)
1596 free(screenshot->driver_name);
1597 if (screenshot->device_name)
1598 free(screenshot->device_name);
1601 if (screenshot->port > 0 && screenshot->pixmap > 0)
1602 XvStopVideo(screenshot->dpy, screenshot->port, screenshot->pixmap);
1605 if (screenshot->damage)
1606 XDamageDestroy(screenshot->dpy, screenshot->damage);
1610 XFreeGC(screenshot->dpy, screenshot->gc);
1613 if (screenshot->pixmap > 0)
1614 XFreePixmap(screenshot->dpy, screenshot->pixmap);
1617 if (screenshot->port > 0)
1618 XvUngrabPort(screenshot->dpy, screenshot->port, 0);
1620 XSync(screenshot->dpy, False);
1623 if (screenshot->internal_display ==1 && screenshot->dpy)
1624 XCloseDisplay(screenshot->dpy);
1627 g_screenshot = NULL;
1629 return EFL_UTIL_ERROR_NONE;
1633 return EFL_UTIL_ERROR_NONE;
1636 g_screenshot = NULL;
1638 if (_eflutil.wl.shot.screenshooter)
1639 screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, NULL);
1641 return EFL_UTIL_ERROR_NONE;
1646 API tbm_surface_h efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
1650 XErrorHandler old_handler = NULL;
1651 unsigned int attachment = DRI2BufferFrontLeft;
1653 DRI2Buffer *bufs = NULL;
1655 tbm_surface_h t_surface = NULL;
1658 tbm_surface_info_s surf_info;
1661 if (screenshot != g_screenshot)
1663 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1667 /* for flush other pending requests and pending events */
1668 XSync(screenshot->dpy, 0);
1670 g_efl_util_x_error_caught = False;
1671 old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1674 XvPutStill(screenshot->dpy, screenshot->port, screenshot->pixmap, screenshot->gc,
1675 0, 0, screenshot->width, screenshot->height,
1676 0, 0, screenshot->width, screenshot->height);
1678 XSync(screenshot->dpy, 0);
1680 if (g_efl_util_x_error_caught)
1682 g_efl_util_x_error_caught = False;
1683 XSetErrorHandler(old_handler);
1687 g_efl_util_x_error_caught = False;
1688 XSetErrorHandler(old_handler);
1690 if (XPending(screenshot->dpy))
1691 XNextEvent(screenshot->dpy, &ev);
1694 int fd = ConnectionNumber(screenshot->dpy);
1703 tv.tv_sec = TIMEOUT_CAPTURE;
1705 ret = select(fd + 1, &mask, 0, 0, &tv);
1707 fprintf(stderr, "[screenshot] fail: select.\n");
1709 fprintf(stderr, "[screenshot] fail: timeout(%d sec)!\n", TIMEOUT_CAPTURE);
1710 else if (XPending(screenshot->dpy))
1711 XNextEvent(screenshot->dpy, &ev);
1713 fprintf(stderr, "[screenshot] fail: not passed a event!\n");
1716 /* check if the capture is done by xserver and pixmap has got the captured image */
1717 if (ev.type == (screenshot->damage_base + XDamageNotify))
1719 XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1720 if (damage_ev->drawable == screenshot->pixmap)
1722 /* Get DRI2 FrontLeft buffer of the pixmap */
1723 bufs = DRI2GetBuffers(screenshot->dpy, screenshot->pixmap, &buf_width, &buf_height, &attachment, 1, &nbufs);
1726 fprintf(stderr, "[screenshot] fail: DRI2GetBuffers\n");
1730 t_bo = tbm_bo_import(screenshot->bufmgr, bufs[0].name);
1733 fprintf(stderr, "[screenshot] fail: import tbm_bo!\n");
1737 surf_info.width = buf_width;
1738 surf_info.height = buf_height;
1739 surf_info.format = TBM_FORMAT_XRGB8888;
1741 surf_info.size = bufs->pitch * surf_info.height;
1742 surf_info.num_planes = 1;
1743 for (i = 0; i < surf_info.num_planes; i++)
1745 surf_info.planes[i].size = bufs->pitch * surf_info.height;
1746 surf_info.planes[i].stride = bufs->pitch;
1747 surf_info.planes[i].offset = 0;
1749 t_surface = tbm_surface_internal_create_with_bos(&surf_info, &t_bo, 1);
1752 fprintf(stderr, "[screenshot] fail: get tbm_surface!\n");
1759 XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1761 set_last_result(EFL_UTIL_ERROR_NONE);
1766 XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1776 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1782 tbm_surface_h t_surface = NULL;
1783 struct wl_buffer *buffer = NULL;
1784 Efl_Util_Wl_Output_Info *output;
1787 if (screenshot != g_screenshot)
1789 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1793 output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
1796 fprintf(stderr, "[screenshot] fail: no output for screenshot\n");
1800 t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
1803 fprintf(stderr, "[screenshot] fail: tbm_surface_create\n");
1807 buffer = wayland_tbm_client_create_buffer(_eflutil.wl.shot.tbm_client, t_surface);
1810 fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n");
1814 screenshooter_shoot(_eflutil.wl.shot.screenshooter, output->output, buffer);
1816 screenshot->shot_done = EINA_FALSE;
1817 while (!screenshot->shot_done && ret != -1)
1818 ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1822 fprintf(stderr, "[screenshot] fail: screenshooter_shoot\n");
1826 wl_buffer_destroy(buffer);
1828 /* reset shot_done for next screenshot */
1829 screenshot->shot_done = EINA_FALSE;
1835 tbm_surface_destroy(t_surface);
1837 wl_buffer_destroy(buffer);
1839 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);