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);
175 static const struct wl_registry_listener _wl_reg_listener =
178 _cb_wl_reg_global_remove
181 struct tizen_policy_listener _wl_tz_policy_listener =
183 _cb_wl_tz_policy_conformant,
184 _cb_wl_tz_policy_conformant_area,
185 _cb_wl_tz_policy_notification_done,
186 _cb_wl_tz_policy_transient_for_done,
187 _cb_wl_tz_policy_scr_mode_done
189 #endif /* end of WAYLAND */
192 _cb_info_add(Evas_Object *win,
197 Efl_Util_Callback_Info *info;
199 info = _cb_info_find_by_win(win, idx);
202 _eflutil.cb_handler[idx].info_list
203 = eina_list_remove(_eflutil.cb_handler[idx].info_list,
208 info = (Efl_Util_Callback_Info *)calloc(1, sizeof(Efl_Util_Callback_Info));
209 if (!info) return EINA_FALSE;
215 _eflutil.cb_handler[idx].info_list
216 = eina_list_append(_eflutil.cb_handler[idx].info_list,
220 if (!_eflutil.x11.handler)
221 _eflutil.x11.handler = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
224 #endif /* end of X11 */
230 _cb_info_del_by_win(Evas_Object *win,
233 Efl_Util_Callback_Info *info;
235 info = _cb_info_find_by_win(win, idx);
236 if (!info) return EINA_FALSE;
238 _eflutil.cb_handler[idx].info_list
239 = eina_list_remove(_eflutil.cb_handler[idx].info_list,
244 unsigned int count = eina_list_count(_eflutil.cb_handler[idx].info_list);
245 if ((count == 0) && (_eflutil.x11.handler))
247 ecore_event_handler_del(_eflutil.x11.handler);
248 _eflutil.x11.handler = NULL;
256 _cb_info_list_get(int idx)
258 return _eflutil.cb_handler[idx].info_list;
261 static Efl_Util_Callback_Info *
262 _cb_info_find_by_win(Evas_Object *win,
266 Efl_Util_Callback_Info *info;
268 l = _cb_info_list_get(idx);
269 EINA_LIST_FOREACH(l, ll, info)
271 if (info->win == win) return info;
278 static Efl_Util_Callback_Info *
279 _cb_info_find_by_xwin(unsigned int xwin,
283 Efl_Util_Callback_Info *info;
286 l = _cb_info_list_get(idx);
287 EINA_LIST_FOREACH(l, ll, info)
289 xwin2 = elm_win_xwindow_get(info->win);
290 if (xwin == xwin2) return info;
297 _cb_x11_client_msg(void *data,
301 Ecore_X_Event_Client_Message *ev;
303 Efl_Util_Callback_Info *info;
306 if (!ev) return ECORE_CALLBACK_PASS_ON;
309 if (xwin == 0) return ECORE_CALLBACK_PASS_ON;
311 if (ev->message_type == _eflutil.cb_handler[CBH_NOTI_LEV].atom)
313 info = _cb_info_find_by_xwin(xwin, CBH_NOTI_LEV);
315 /* permission denied */
316 if ((ev->data.l[1] == 0) &&
321 EFL_UTIL_ERROR_PERMISSION_DENIED,
325 else if (ev->message_type == _eflutil.cb_handler[CBH_SCR_MODE].atom)
327 info = _cb_info_find_by_xwin(xwin, CBH_SCR_MODE);
329 /* permission denied */
330 if ((ev->data.l[1] == 0) &&
335 EFL_UTIL_ERROR_PERMISSION_DENIED,
339 return ECORE_CALLBACK_PASS_ON;
345 if (_eflutil.x11.init) return EINA_TRUE;
347 _eflutil.x11.dpy = ecore_x_display_get();
348 EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.x11.dpy, EINA_FALSE);
350 _eflutil.x11.init = EINA_TRUE;
354 #endif /* end of X11 */
360 struct wl_registry *reg = NULL;
362 if (_eflutil.wl.init) return EINA_TRUE;
366 _eflutil.wl.dpy = ecore_wl_display_get();
367 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.dpy, fail);
369 _eflutil.wl.queue = wl_display_create_queue(_eflutil.wl.dpy);
370 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.queue, fail);
372 reg = wl_display_get_registry(_eflutil.wl.dpy);
373 EINA_SAFETY_ON_NULL_GOTO(reg, fail);
375 wl_proxy_set_queue((struct wl_proxy*)reg, _eflutil.wl.queue);
376 wl_registry_add_listener(reg, &_wl_reg_listener, NULL);
378 _eflutil.wl.init = EINA_TRUE;
382 if (_eflutil.wl.queue)
384 wl_event_queue_destroy(_eflutil.wl.queue);
385 _eflutil.wl.queue = NULL;
389 wl_registry_destroy(reg);
395 _cb_wl_output_geometry(void *data, struct wl_output *wl_output, int x, int y,
396 int physical_width, int physical_height, int subpixel,
397 const char *make, const char *model, int transform)
399 Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
400 if (wl_output == output->output)
402 output->offset_x = x;
403 output->offset_y = y;
408 _cb_wl_output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
409 int width, int height, int refresh)
411 Efl_Util_Wl_Output_Info *output = wl_output_get_user_data(wl_output);
412 if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT))
414 output->width = width;
415 output->height = height;
420 _cb_wl_output_done(void *data, struct wl_output *wl_output)
425 _cb_wl_output_scale(void *data, struct wl_output *wl_output, int32_t factor)
429 static const struct wl_output_listener output_listener =
431 _cb_wl_output_geometry,
438 _cb_wl_screenshot_done(void *data, struct screenshooter *screenshooter)
440 Eina_Bool *shot_done = (Eina_Bool*)data;
442 *shot_done = EINA_TRUE;
445 static const struct screenshooter_listener screenshooter_listener =
447 _cb_wl_screenshot_done
451 _cb_wl_reg_global(void *data,
452 struct wl_registry *reg,
454 const char *interface,
455 unsigned int version)
457 if (!strcmp(interface, "tizen_policy"))
459 struct tizen_policy *proto;
460 proto = wl_registry_bind(reg,
462 &tizen_policy_interface,
466 tizen_policy_add_listener(proto,
467 &_wl_tz_policy_listener,
470 _eflutil.wl.policy.hash_noti_lv = eina_hash_pointer_new(free);
471 _eflutil.wl.policy.hash_scr_mode = eina_hash_pointer_new(free);
472 _eflutil.wl.policy.proto = proto;
474 else if (strcmp(interface, "wl_output") == 0)
476 Efl_Util_Wl_Output_Info *output = calloc(1, sizeof(Efl_Util_Wl_Output_Info));
477 EINA_SAFETY_ON_NULL_RETURN(output);
479 _eflutil.wl.shot.output_list = eina_list_append(_eflutil.wl.shot.output_list, output);
481 output->output = wl_registry_bind(reg, name, &wl_output_interface, version);
482 wl_output_add_listener(output->output, &output_listener, output);
484 else if (strcmp(interface, "screenshooter") == 0)
486 _eflutil.wl.shot.screenshooter = wl_registry_bind(reg, name, &screenshooter_interface, version);
487 screenshooter_add_listener(_eflutil.wl.shot.screenshooter, &screenshooter_listener, NULL);
492 _cb_wl_reg_global_remove(void *data,
493 struct wl_registry *reg,
496 _eflutil.wl.policy.proto = NULL;
497 eina_hash_free(_eflutil.wl.policy.hash_noti_lv);
498 eina_hash_free(_eflutil.wl.policy.hash_scr_mode);
501 static Efl_Util_Callback_Info *
502 _cb_info_find_by_wlsurf(void *wlsurf,
506 Efl_Util_Callback_Info *info;
507 Ecore_Wl_Window *wlwin2 = NULL;
508 void *wlsurf2 = NULL;
510 l = _cb_info_list_get(idx);
511 EINA_LIST_FOREACH(l, ll, info)
513 wlwin2 = elm_win_wl_window_get(info->win);
514 wlsurf2 = ecore_wl_window_surface_get(wlwin2);
515 if (wlsurf== wlsurf2) return info;
522 _cb_wl_tz_policy_conformant(void *data, struct tizen_policy *tizen_policy,
523 struct wl_surface *surface, uint32_t is_conformant)
528 _cb_wl_tz_policy_conformant_area(void *data, struct tizen_policy *tizen_policy,
529 struct wl_surface *surface, uint32_t conformant_part,
530 uint32_t state, int32_t x, int32_t y, int32_t w, int32_t h)
535 _cb_wl_tz_policy_notification_done(void *data,
536 struct tizen_policy *tizen_policy,
537 struct wl_surface *surface,
541 Efl_Util_Wl_Surface_Lv_Info *lv_info;
542 Efl_Util_Callback_Info *cb_info;
544 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
547 lv_info->level = level;
548 lv_info->wait_for_done = EINA_FALSE;
549 lv_info->state = state;
552 if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
554 cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_NOTI_LEV);
555 if (!cb_info) return;
556 if (!cb_info->cb) return;
558 cb_info->cb(cb_info->win,
559 EFL_UTIL_ERROR_PERMISSION_DENIED,
564 _cb_wl_tz_policy_transient_for_done(void *data, struct tizen_policy *tizen_policy, uint32_t child_id)
569 _cb_wl_tz_policy_scr_mode_done(void *data,
570 struct tizen_policy *tizen_policy,
571 struct wl_surface *surface,
576 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
577 Efl_Util_Callback_Info *cb_info;
579 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
582 scr_mode_info->mode = mode;
583 scr_mode_info->wait_for_done = EINA_FALSE;
584 scr_mode_info->state = state;
587 if (state != TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED) return;
589 cb_info = _cb_info_find_by_wlsurf((void *)surface, CBH_SCR_MODE);
590 if (!cb_info) return;
591 if (!cb_info->cb) return;
593 cb_info->cb(cb_info->win,
594 EFL_UTIL_ERROR_PERMISSION_DENIED,
599 _cb_window_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
601 Efl_Util_Wl_Surface_Lv_Info *lv_info;
604 if (EINA_UNLIKELY(!lv_info))
607 eina_hash_del(_eflutil.wl.policy.hash_noti_lv, &lv_info->surface, lv_info);
609 #endif /* end of WAYLAND */
612 efl_util_set_notification_window_level(Evas_Object *window,
613 efl_util_notification_level_e level)
617 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
618 EINA_SAFETY_ON_FALSE_RETURN_VAL((level >= EFL_UTIL_NOTIFICATION_LEVEL_NONE) &&
619 (level <= EFL_UTIL_NOTIFICATION_LEVEL_TOP),
620 EFL_UTIL_ERROR_INVALID_PARAMETER);
623 Ecore_X_Window_Type window_type;
627 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
629 xwin = elm_win_xwindow_get(window);
632 if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
634 // success to get window type
635 if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
637 // given EFL window's type is not notification type.
638 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
642 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
644 utilx_set_system_notification_level(_eflutil.x11.dpy,
647 return EFL_UTIL_ERROR_NONE;
650 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
651 #endif /* end of X11 */
655 Ecore_Wl_Window *wlwin;
656 struct wl_surface *surface;
657 Efl_Util_Wl_Surface_Lv_Info *lv_info;
658 Ecore_Wl_Window_Type wl_type;
661 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
663 wlwin = elm_win_wl_window_get(window);
664 EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
666 type = elm_win_type_get(window);
667 if (type != ELM_WIN_NOTIFICATION)
669 wl_type = ecore_wl_window_type_get(wlwin);
670 EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
671 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
674 while (!_eflutil.wl.policy.proto)
675 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
677 surface = ecore_wl_window_surface_get(wlwin);
678 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
679 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
681 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
684 lv_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Lv_Info));
685 EINA_SAFETY_ON_NULL_RETURN_VAL(lv_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
687 lv_info->surface = surface;
688 lv_info->level = (int)level;
689 lv_info->wait_for_done = EINA_TRUE;
690 lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
691 eina_hash_add(_eflutil.wl.policy.hash_noti_lv,
695 evas_object_event_callback_add(window, EVAS_CALLBACK_DEL,
696 _cb_window_del, lv_info);
700 lv_info->level = (int)level;
701 lv_info->wait_for_done = EINA_TRUE;
702 lv_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
706 tizen_policy_set_notification_level(_eflutil.wl.policy.proto,
707 surface, (int)level);
709 if (lv_info->wait_for_done)
712 while (lv_info->wait_for_done && (count < 3))
715 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
719 if (lv_info->wait_for_done)
721 return EFL_UTIL_ERROR_INVALID_PARAMETER;
725 if (lv_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
727 return EFL_UTIL_ERROR_PERMISSION_DENIED;
732 return EFL_UTIL_ERROR_NONE;
733 #endif /* end of WAYLAND */
737 efl_util_get_notification_window_level(Evas_Object *window,
738 efl_util_notification_level_e *level)
742 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
743 EINA_SAFETY_ON_NULL_RETURN_VAL(level, EFL_UTIL_ERROR_INVALID_PARAMETER);
746 Ecore_X_Window_Type window_type;
747 Utilx_Notification_Level utilx_level;
751 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
753 xwin = elm_win_xwindow_get(window);
756 if (ecore_x_netwm_window_type_get(xwin, &window_type) == EINA_TRUE)
758 // success to get window type
759 if (window_type != ECORE_X_WINDOW_TYPE_NOTIFICATION)
761 // given EFL window's type is not notification type.
762 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
765 utilx_level = utilx_get_system_notification_level(_eflutil.x11.dpy, xwin);
766 if (utilx_level == UTILX_NOTIFICATION_LEVEL_LOW)
767 *level = EFL_UTIL_NOTIFICATION_LEVEL_1;
768 else if(utilx_level == UTILX_NOTIFICATION_LEVEL_NORMAL)
769 *level = EFL_UTIL_NOTIFICATION_LEVEL_2;
770 else if(utilx_level == UTILX_NOTIFICATION_LEVEL_HIGH)
771 *level = EFL_UTIL_NOTIFICATION_LEVEL_3;
773 return EFL_UTIL_ERROR_INVALID_PARAMETER;
776 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
778 return EFL_UTIL_ERROR_NONE;
781 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
782 #endif /* end of X11 */
786 Ecore_Wl_Window *wlwin;
787 struct wl_surface *surface;
788 Efl_Util_Wl_Surface_Lv_Info *lv_info;
789 Ecore_Wl_Window_Type wl_type;
792 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
794 wlwin = elm_win_wl_window_get(window);
795 EINA_SAFETY_ON_NULL_RETURN_VAL(wlwin, EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
797 type = elm_win_type_get(window);
798 if (type != ELM_WIN_NOTIFICATION)
800 wl_type = ecore_wl_window_type_get(wlwin);
801 EINA_SAFETY_ON_FALSE_RETURN_VAL((wl_type == ECORE_WL_WINDOW_TYPE_NOTIFICATION),
802 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
805 while (!_eflutil.wl.policy.proto)
806 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
808 surface = ecore_wl_window_surface_get(wlwin);
809 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
810 EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE);
812 lv_info = eina_hash_find(_eflutil.wl.policy.hash_noti_lv, &surface);
815 if (lv_info->wait_for_done)
818 while ((lv_info->wait_for_done) && (count < 3))
821 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
825 if (lv_info->wait_for_done)
827 *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
828 return EFL_UTIL_ERROR_INVALID_PARAMETER;
832 switch (lv_info->level)
834 case TIZEN_POLICY_LEVEL_1: *level = EFL_UTIL_NOTIFICATION_LEVEL_1; break;
835 case TIZEN_POLICY_LEVEL_2: *level = EFL_UTIL_NOTIFICATION_LEVEL_2; break;
836 case TIZEN_POLICY_LEVEL_3: *level = EFL_UTIL_NOTIFICATION_LEVEL_3; break;
837 case TIZEN_POLICY_LEVEL_NONE: *level = EFL_UTIL_NOTIFICATION_LEVEL_NONE; break;
838 case TIZEN_POLICY_LEVEL_DEFAULT: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT; break;
839 case TIZEN_POLICY_LEVEL_MEDIUM: *level = EFL_UTIL_NOTIFICATION_LEVEL_MEDIUM; break;
840 case TIZEN_POLICY_LEVEL_HIGH: *level = EFL_UTIL_NOTIFICATION_LEVEL_HIGH; break;
841 case TIZEN_POLICY_LEVEL_TOP: *level = EFL_UTIL_NOTIFICATION_LEVEL_TOP; break;
842 default: *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
843 return EFL_UTIL_ERROR_INVALID_PARAMETER;
845 return EFL_UTIL_ERROR_NONE;
848 *level = EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT;
850 return EFL_UTIL_ERROR_NONE;
851 #endif /* end of WAYLAND */
855 efl_util_set_notification_window_level_error_cb(Evas_Object *window,
856 efl_util_notification_window_level_error_cb callback,
859 Eina_Bool ret = EINA_FALSE;
861 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
862 EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
864 ret = _cb_info_add(window,
865 (Efl_Util_Cb)callback,
868 if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
871 if (!_eflutil.cb_handler[CBH_NOTI_LEV].atom)
872 _eflutil.cb_handler[CBH_NOTI_LEV].atom = ecore_x_atom_get("_E_NOTIFICATION_LEVEL_ACCESS_RESULT");
873 #endif /* end of X11 */
875 return EFL_UTIL_ERROR_NONE;
879 efl_util_unset_notification_window_level_error_cb(Evas_Object *window)
881 Eina_Bool ret = EINA_FALSE;
883 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
885 ret = _cb_info_del_by_win(window, CBH_NOTI_LEV);
886 if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
888 return EFL_UTIL_ERROR_NONE;
892 efl_util_set_window_opaque_state(Evas_Object *window,
897 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
898 EINA_SAFETY_ON_FALSE_RETURN_VAL(((opaque >= 0) && (opaque <= 1)),
899 EFL_UTIL_ERROR_INVALID_PARAMETER);
903 Utilx_Opaque_State state;
907 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
909 xwin = elm_win_xwindow_get(window);
910 EINA_SAFETY_ON_FALSE_RETURN_VAL(xwin > 0, EFL_UTIL_ERROR_INVALID_PARAMETER);
913 state = UTILX_OPAQUE_STATE_ON;
915 state = UTILX_OPAQUE_STATE_OFF;
917 ret = utilx_set_window_opaque_state(_eflutil.x11.dpy,
922 return EFL_UTIL_ERROR_INVALID_PARAMETER;
924 return EFL_UTIL_ERROR_NONE;
925 #endif /* end of X11 */
928 Ecore_Wl_Window *wlwin;
929 struct wl_surface *surface;
931 if (!_eflutil.wl.policy.proto)
936 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
938 while (!_eflutil.wl.policy.proto && ret != -1)
939 ret = wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
941 EINA_SAFETY_ON_NULL_RETURN_VAL(_eflutil.wl.policy.proto, EFL_UTIL_ERROR_INVALID_PARAMETER);
944 wlwin = elm_win_wl_window_get(window);
946 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
948 surface = ecore_wl_window_surface_get(wlwin);
950 return EFL_UTIL_ERROR_NOT_SUPPORTED_WINDOW_TYPE;
952 tizen_policy_set_opaque_state(_eflutil.wl.policy.proto, surface, opaque);
954 return EFL_UTIL_ERROR_NONE;
955 #endif /* end of WAYLAND */
959 efl_util_set_window_screen_mode(Evas_Object *window,
960 efl_util_screen_mode_e mode)
962 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
963 EINA_SAFETY_ON_FALSE_RETURN_VAL(((mode >= EFL_UTIL_SCREEN_MODE_DEFAULT) &&
964 (mode <= EFL_UTIL_SCREEN_MODE_ALWAYS_ON)),
965 EFL_UTIL_ERROR_INVALID_PARAMETER);
972 e = evas_object_evas_get(window);
973 EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
975 ee = ecore_evas_ecore_evas_get(e);
976 EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
978 id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
979 if (mode == EFL_UTIL_SCREEN_MODE_ALWAYS_ON)
982 ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "1");
984 ecore_evas_aux_hint_val_set(ee, id, "1");
986 else if (mode == EFL_UTIL_SCREEN_MODE_DEFAULT)
989 ecore_evas_aux_hint_add(ee, "wm.policy.win.lcd.lock", "0");
991 ecore_evas_aux_hint_val_set(ee, id, "0");
994 return EFL_UTIL_ERROR_INVALID_PARAMETER;
996 return EFL_UTIL_ERROR_NONE;
997 #endif /* end of X11 */
1000 Ecore_Wl_Window *wlwin;
1001 struct wl_surface *surface;
1002 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1006 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1008 wlwin = elm_win_wl_window_get(window);
1011 while (!_eflutil.wl.policy.proto)
1012 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1014 surface = ecore_wl_window_surface_get(wlwin);
1015 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1016 EFL_UTIL_ERROR_INVALID_PARAMETER);
1018 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1021 scr_mode_info = calloc(1, sizeof(Efl_Util_Wl_Surface_Scr_Mode_Info));
1022 EINA_SAFETY_ON_NULL_RETURN_VAL(scr_mode_info, EFL_UTIL_ERROR_OUT_OF_MEMORY);
1024 scr_mode_info->surface = surface;
1025 scr_mode_info->mode = (unsigned int)mode;
1026 scr_mode_info->wait_for_done = EINA_TRUE;
1027 scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1029 eina_hash_add(_eflutil.wl.policy.hash_scr_mode,
1035 scr_mode_info->mode = (unsigned int)mode;
1036 scr_mode_info->wait_for_done = EINA_TRUE;
1037 scr_mode_info->state = TIZEN_POLICY_ERROR_STATE_NONE;
1040 tizen_policy_set_window_screen_mode(_eflutil.wl.policy.proto,
1041 surface, (unsigned int)mode);
1042 if (scr_mode_info->wait_for_done)
1045 while (scr_mode_info->wait_for_done && (count < 3))
1048 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1052 if (scr_mode_info->wait_for_done)
1054 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1058 if (scr_mode_info->state == TIZEN_POLICY_ERROR_STATE_PERMISSION_DENIED)
1060 return EFL_UTIL_ERROR_PERMISSION_DENIED;
1065 return EFL_UTIL_ERROR_NONE;
1068 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1069 #endif /* end of WAYLAND */
1073 efl_util_get_window_screen_mode(Evas_Object *window,
1074 efl_util_screen_mode_e *mode)
1076 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1077 EINA_SAFETY_ON_NULL_RETURN_VAL(mode, EFL_UTIL_ERROR_INVALID_PARAMETER);
1085 e = evas_object_evas_get(window);
1086 EINA_SAFETY_ON_NULL_RETURN_VAL(e, EFL_UTIL_ERROR_INVALID_PARAMETER);
1088 ee = ecore_evas_ecore_evas_get(e);
1089 EINA_SAFETY_ON_NULL_RETURN_VAL(ee, EFL_UTIL_ERROR_INVALID_PARAMETER);
1091 id = ecore_evas_aux_hint_id_get(ee, "wm.policy.win.lcd.lock");
1092 EINA_SAFETY_ON_TRUE_RETURN_VAL((id == -1), EFL_UTIL_ERROR_INVALID_PARAMETER);
1094 str = ecore_evas_aux_hint_val_get(ee, id);
1095 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EFL_UTIL_ERROR_INVALID_PARAMETER);
1097 if (strncmp(str, "1", strlen("1")) == 0)
1098 *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON;
1100 *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1102 return EFL_UTIL_ERROR_NONE;
1103 #endif /* end of X11 */
1106 Ecore_Wl_Window *wlwin;
1107 struct wl_surface *surface;
1108 Efl_Util_Wl_Surface_Scr_Mode_Info *scr_mode_info;
1112 EINA_SAFETY_ON_FALSE_RETURN_VAL(res, EFL_UTIL_ERROR_INVALID_PARAMETER);
1114 wlwin = elm_win_wl_window_get(window);
1117 while (!_eflutil.wl.policy.proto)
1118 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1120 surface = ecore_wl_window_surface_get(wlwin);
1121 EINA_SAFETY_ON_NULL_RETURN_VAL(surface,
1122 EFL_UTIL_ERROR_INVALID_PARAMETER);
1124 scr_mode_info = eina_hash_find(_eflutil.wl.policy.hash_scr_mode, &surface);
1127 if (scr_mode_info->wait_for_done)
1129 while (scr_mode_info->wait_for_done)
1132 wl_display_dispatch_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1136 switch (scr_mode_info->mode)
1138 case TIZEN_POLICY_MODE_DEFAULT: *mode = EFL_UTIL_SCREEN_MODE_DEFAULT; break;
1139 case TIZEN_POLICY_MODE_ALWAYS_ON: *mode = EFL_UTIL_SCREEN_MODE_ALWAYS_ON; break;
1140 default: *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1141 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1143 return EFL_UTIL_ERROR_NONE;
1147 *mode = EFL_UTIL_SCREEN_MODE_DEFAULT;
1148 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1152 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1153 #endif /* end of WAYLAND */
1157 efl_util_set_window_screen_mode_error_cb(Evas_Object *window,
1158 efl_util_window_screen_mode_error_cb callback,
1161 Eina_Bool ret = EINA_FALSE;
1163 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1164 EINA_SAFETY_ON_NULL_RETURN_VAL(callback, EFL_UTIL_ERROR_INVALID_PARAMETER);
1166 ret = _cb_info_add(window,
1167 (Efl_Util_Cb)callback,
1170 if (!ret) return EFL_UTIL_ERROR_OUT_OF_MEMORY;
1173 if (!_eflutil.cb_handler[CBH_SCR_MODE].atom)
1174 _eflutil.cb_handler[CBH_SCR_MODE].atom = ecore_x_atom_get("_E_SCREEN_MODE_ACCESS_RESULT");
1175 #endif /* end of X11 */
1177 return EFL_UTIL_ERROR_NONE;
1181 efl_util_unset_window_screen_mode_error_cb(Evas_Object *window)
1183 Eina_Bool ret = EINA_FALSE;
1185 EINA_SAFETY_ON_NULL_RETURN_VAL(window, EFL_UTIL_ERROR_INVALID_PARAMETER);
1187 ret = _cb_info_del_by_win(window, CBH_SCR_MODE);
1188 if (!ret) return EFL_UTIL_ERROR_INVALID_PARAMETER;
1190 return EFL_UTIL_ERROR_NONE;
1194 efl_util_input_initialize_generator(efl_util_input_device_type_e dev_type)
1196 return EFL_UTIL_ERROR_NONE;
1200 efl_util_input_deinitialize_generator(void)
1206 efl_util_input_generate_key(const char *key_name,
1209 return EFL_UTIL_ERROR_NONE;
1213 efl_util_input_generate_touch(int idx,
1214 efl_util_input_touch_type_e touch_type,
1218 return EFL_UTIL_ERROR_NONE;
1221 struct _efl_util_screenshot_h
1227 Ecore_X_Display *dpy;
1228 int internal_display;
1243 int eventBase, errorBase;
1244 int dri2Major, dri2Minor;
1245 char *driver_name, *device_name;
1252 Eina_Bool shot_done;
1258 /* scrrenshot handle */
1259 static efl_util_screenshot_h g_screenshot;
1262 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1263 #define FOURCC_RGB32 FOURCC('R','G','B','4')
1264 #define TIMEOUT_CAPTURE 3
1266 /* x error handling */
1267 static Bool g_efl_util_x_error_caught;
1270 _efl_util_screenshot_x_error_handle(Display *dpy, XErrorEvent *ev)
1272 if (!g_screenshot || (dpy != g_screenshot->dpy))
1275 g_efl_util_x_error_caught = True;
1281 _efl_util_screenshot_get_port(Display *dpy, unsigned int id, Window win)
1283 unsigned int ver, rev, req_base, evt_base, err_base;
1284 unsigned int adaptors;
1285 XvAdaptorInfo *ai = NULL;
1286 XvImageFormatValues *fo = NULL;
1290 if (XvQueryExtension(dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1292 fprintf(stderr, "[screenshot] fail: no XV extension. \n");
1296 if (XvQueryAdaptors(dpy, win, &adaptors, &ai) != Success)
1298 fprintf(stderr, "[screenshot] fail: query adaptors. \n");
1302 EINA_SAFETY_ON_NULL_RETURN_VAL(ai, -1);
1304 for (i = 0; i < adaptors; i++)
1306 int support_format = False;
1308 if (!(ai[i].type & XvInputMask) ||
1309 !(ai[i].type & XvStillMask))
1314 fo = XvListImageFormats(dpy, p, &formats);
1315 for (j = 0; j < formats; j++)
1316 if (fo[j].id == (int)id)
1317 support_format = True;
1322 if (!support_format)
1325 for (; p < ai[i].base_id + ai[i].num_ports; p++)
1327 if (XvGrabPort(dpy, p, 0) == Success)
1329 XvFreeAdaptorInfo(ai);
1335 XvFreeAdaptorInfo(ai);
1342 static int _efl_util_screenshot_get_best_size(Display *dpy, int port, int width, int height, unsigned int *best_width, unsigned int *best_height)
1344 XErrorHandler old_handler = NULL;
1346 Atom atom_capture = XInternAtom(dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1348 g_efl_util_x_error_caught = False;
1349 old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1351 XvSetPortAttribute(dpy, port, atom_capture, 1);
1354 g_efl_util_x_error_caught = False;
1355 XSetErrorHandler(old_handler);
1357 XvQueryBestSize(dpy, port, 0, 0, 0, width, height, best_width, best_height);
1358 if (best_width <= 0 || best_height <= 0)
1365 API efl_util_screenshot_h efl_util_screenshot_initialize(int width, int height)
1368 efl_util_screenshot_h screenshot = NULL;
1370 int damage_err_base = 0;
1371 unsigned int best_width = 0;
1372 unsigned int best_height = 0;
1374 EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1375 EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1377 if (g_screenshot != NULL)
1379 if (g_screenshot->width != width || g_screenshot->height != height)
1381 // TODO: recreate pixmap and update information
1382 if (!_efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height))
1384 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1388 g_screenshot->width = width;
1389 g_screenshot->height = height;
1392 return g_screenshot;
1395 screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1396 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1399 screenshot->dpy = ecore_x_display_get();
1400 if (!screenshot->dpy)
1402 screenshot->dpy = XOpenDisplay(0);
1403 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_init);
1405 /* for XCloseDisplay at denitialization */
1406 screenshot->internal_display = 1;
1410 screenshot->screen = DefaultScreen(screenshot->dpy);
1412 /* set root window */
1413 screenshot->root = DefaultRootWindow(screenshot->dpy);
1415 /* initialize capture adaptor */
1416 screenshot->port = _efl_util_screenshot_get_port(screenshot->dpy, FOURCC_RGB32, screenshot->root);
1417 EINA_SAFETY_ON_FALSE_GOTO(screenshot->port > 0, fail_init);
1419 /* get the best size */
1420 _efl_util_screenshot_get_best_size(screenshot->dpy, screenshot->port, width, height, &best_width, &best_height);
1421 EINA_SAFETY_ON_FALSE_GOTO(best_width > 0, fail_init);
1422 EINA_SAFETY_ON_FALSE_GOTO(best_height > 0, fail_init);
1424 /* set the width and the height */
1425 screenshot->width = best_width;
1426 screenshot->height = best_height;
1428 /* create a pixmap */
1429 depth = DefaultDepth(screenshot->dpy, screenshot->screen);
1430 screenshot->pixmap = XCreatePixmap(screenshot->dpy, screenshot->root, screenshot->width, screenshot->height, depth);
1431 EINA_SAFETY_ON_FALSE_GOTO(screenshot->pixmap > 0, fail_init);
1433 screenshot->gc = XCreateGC(screenshot->dpy, screenshot->pixmap, 0, 0);
1434 EINA_SAFETY_ON_NULL_GOTO(screenshot->gc, fail_init);
1436 XSetForeground(screenshot->dpy, screenshot->gc, 0xFF000000);
1437 XFillRectangle(screenshot->dpy, screenshot->pixmap, screenshot->gc, 0, 0, width, height);
1439 /* initialize damage */
1440 if (!XDamageQueryExtension(screenshot->dpy, &screenshot->damage_base, &damage_err_base))
1443 screenshot->damage = XDamageCreate(screenshot->dpy, screenshot->pixmap, XDamageReportNonEmpty);
1444 EINA_SAFETY_ON_FALSE_GOTO(screenshot->damage > 0, fail_init);
1446 /* initialize dri3 and dri2 */
1447 if (!DRI2QueryExtension(screenshot->dpy, &screenshot->eventBase, &screenshot->errorBase))
1449 fprintf(stderr, "[screenshot] fail: DRI2QueryExtention\n");
1453 if (!DRI2QueryVersion(screenshot->dpy, &screenshot->dri2Major, &screenshot->dri2Minor))
1455 fprintf(stderr, "[screenshot] fail: DRI2QueryVersion\n");
1459 if (!DRI2Connect(screenshot->dpy, screenshot->root, &screenshot->driver_name, &screenshot->device_name))
1461 fprintf(stderr, "[screenshot] fail: DRI2Connect\n");
1465 screenshot->drm_fd = open(screenshot->device_name, O_RDWR);
1466 EINA_SAFETY_ON_FALSE_GOTO(screenshot->drm_fd >= 0, fail_init);
1468 if (drmGetMagic(screenshot->drm_fd, &screenshot->magic))
1470 fprintf(stderr, "[screenshot] fail: drmGetMagic\n");
1474 if (!DRI2Authenticate(screenshot->dpy, screenshot->root, screenshot->magic))
1476 fprintf(stderr, "[screenshot] fail: DRI2Authenticate\n");
1480 if (!drmAuthMagic(screenshot->drm_fd, screenshot->magic))
1482 fprintf(stderr, "[screenshot] fail: drmAuthMagic\n");
1486 DRI2CreateDrawable(screenshot->dpy, screenshot->pixmap);
1489 screenshot->bufmgr = tbm_bufmgr_init(screenshot->drm_fd);
1490 EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1492 XFlush(screenshot->dpy);
1494 g_screenshot = screenshot;
1495 set_last_result(EFL_UTIL_ERROR_NONE);
1497 return g_screenshot;
1501 efl_util_screenshot_h screenshot = NULL;
1503 if (!_eflutil.wl.shot.screenshooter)
1507 while (!_eflutil.wl.shot.screenshooter && ret != -1)
1508 ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1509 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.screenshooter, fail_init);
1511 _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
1512 EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_init);
1515 EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
1516 EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
1520 if (g_screenshot->width != width || g_screenshot->height != height)
1522 g_screenshot->width = width;
1523 g_screenshot->height = height;
1526 return g_screenshot;
1529 screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
1530 EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
1532 screenshot->width = width;
1533 screenshot->height = height;
1535 screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
1536 EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
1538 g_screenshot = screenshot;
1539 set_last_result(EFL_UTIL_ERROR_NONE);
1541 screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
1543 return g_screenshot;
1547 efl_util_screenshot_deinitialize(screenshot);
1548 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1552 efl_util_screenshot_deinitialize(screenshot);
1553 set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
1557 efl_util_screenshot_deinitialize(screenshot);
1558 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
1562 API int efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
1566 return EFL_UTIL_ERROR_INVALID_PARAMETER;
1569 if (screenshot->bufmgr)
1570 tbm_bufmgr_deinit(screenshot->bufmgr);
1572 DRI2DestroyDrawable(screenshot->dpy, screenshot->pixmap);
1575 if (screenshot->drm_fd)
1576 close(screenshot->drm_fd);
1577 if (screenshot->driver_name)
1578 free(screenshot->driver_name);
1579 if (screenshot->device_name)
1580 free(screenshot->device_name);
1583 if (screenshot->port > 0 && screenshot->pixmap > 0)
1584 XvStopVideo(screenshot->dpy, screenshot->port, screenshot->pixmap);
1587 if (screenshot->damage)
1588 XDamageDestroy(screenshot->dpy, screenshot->damage);
1592 XFreeGC(screenshot->dpy, screenshot->gc);
1595 if (screenshot->pixmap > 0)
1596 XFreePixmap(screenshot->dpy, screenshot->pixmap);
1599 if (screenshot->port > 0)
1600 XvUngrabPort(screenshot->dpy, screenshot->port, 0);
1602 XSync(screenshot->dpy, False);
1605 if (screenshot->internal_display ==1 && screenshot->dpy)
1606 XCloseDisplay(screenshot->dpy);
1609 g_screenshot = NULL;
1611 return EFL_UTIL_ERROR_NONE;
1615 return EFL_UTIL_ERROR_NONE;
1618 g_screenshot = NULL;
1620 if (_eflutil.wl.shot.screenshooter)
1621 screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, NULL);
1623 return EFL_UTIL_ERROR_NONE;
1628 API tbm_surface_h efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
1632 XErrorHandler old_handler = NULL;
1633 unsigned int attachment = DRI2BufferFrontLeft;
1635 DRI2Buffer *bufs = NULL;
1637 tbm_surface_h t_surface = NULL;
1640 tbm_surface_info_s surf_info;
1643 if (screenshot != g_screenshot)
1645 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1649 /* for flush other pending requests and pending events */
1650 XSync(screenshot->dpy, 0);
1652 g_efl_util_x_error_caught = False;
1653 old_handler = XSetErrorHandler(_efl_util_screenshot_x_error_handle);
1656 XvPutStill(screenshot->dpy, screenshot->port, screenshot->pixmap, screenshot->gc,
1657 0, 0, screenshot->width, screenshot->height,
1658 0, 0, screenshot->width, screenshot->height);
1660 XSync(screenshot->dpy, 0);
1662 if (g_efl_util_x_error_caught)
1664 g_efl_util_x_error_caught = False;
1665 XSetErrorHandler(old_handler);
1669 g_efl_util_x_error_caught = False;
1670 XSetErrorHandler(old_handler);
1672 if (XPending(screenshot->dpy))
1673 XNextEvent(screenshot->dpy, &ev);
1676 int fd = ConnectionNumber(screenshot->dpy);
1685 tv.tv_sec = TIMEOUT_CAPTURE;
1687 ret = select(fd + 1, &mask, 0, 0, &tv);
1689 fprintf(stderr, "[screenshot] fail: select.\n");
1691 fprintf(stderr, "[screenshot] fail: timeout(%d sec)!\n", TIMEOUT_CAPTURE);
1692 else if (XPending(screenshot->dpy))
1693 XNextEvent(screenshot->dpy, &ev);
1695 fprintf(stderr, "[screenshot] fail: not passed a event!\n");
1698 /* check if the capture is done by xserver and pixmap has got the captured image */
1699 if (ev.type == (screenshot->damage_base + XDamageNotify))
1701 XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1702 if (damage_ev->drawable == screenshot->pixmap)
1704 /* Get DRI2 FrontLeft buffer of the pixmap */
1705 bufs = DRI2GetBuffers(screenshot->dpy, screenshot->pixmap, &buf_width, &buf_height, &attachment, 1, &nbufs);
1708 fprintf(stderr, "[screenshot] fail: DRI2GetBuffers\n");
1712 t_bo = tbm_bo_import(screenshot->bufmgr, bufs[0].name);
1715 fprintf(stderr, "[screenshot] fail: import tbm_bo!\n");
1719 surf_info.width = buf_width;
1720 surf_info.height = buf_height;
1721 surf_info.format = TBM_FORMAT_XRGB8888;
1723 surf_info.size = bufs->pitch * surf_info.height;
1724 surf_info.num_planes = 1;
1725 for (i = 0; i < surf_info.num_planes; i++)
1727 surf_info.planes[i].size = bufs->pitch * surf_info.height;
1728 surf_info.planes[i].stride = bufs->pitch;
1729 surf_info.planes[i].offset = 0;
1731 t_surface = tbm_surface_internal_create_with_bos(&surf_info, &t_bo, 1);
1734 fprintf(stderr, "[screenshot] fail: get tbm_surface!\n");
1741 XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1743 set_last_result(EFL_UTIL_ERROR_NONE);
1748 XDamageSubtract(screenshot->dpy, screenshot->damage, None, None );
1758 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
1764 tbm_surface_h t_surface = NULL;
1765 struct wl_buffer *buffer = NULL;
1766 Efl_Util_Wl_Output_Info *output;
1769 if (screenshot != g_screenshot)
1771 set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
1775 output = eina_list_nth(_eflutil.wl.shot.output_list, 0);
1778 fprintf(stderr, "[screenshot] fail: no output for screenshot\n");
1782 t_surface = tbm_surface_create(screenshot->width, screenshot->height, TBM_FORMAT_XRGB8888);
1785 fprintf(stderr, "[screenshot] fail: tbm_surface_create\n");
1789 buffer = wayland_tbm_client_create_buffer(_eflutil.wl.shot.tbm_client, t_surface);
1792 fprintf(stderr, "[screenshot] fail: create wl_buffer for screenshot\n");
1796 screenshooter_shoot(_eflutil.wl.shot.screenshooter, output->output, buffer);
1798 screenshot->shot_done = EINA_FALSE;
1799 while (!screenshot->shot_done && ret != -1)
1800 ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
1804 fprintf(stderr, "[screenshot] fail: screenshooter_shoot\n");
1808 wl_buffer_destroy(buffer);
1810 /* reset shot_done for next screenshot */
1811 screenshot->shot_done = EINA_FALSE;
1817 tbm_surface_destroy(t_surface);
1819 wl_buffer_destroy(buffer);
1821 set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);