2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
31 #include "menu_daemon.h"
33 #include "starter-util.h"
39 Ecore_Event_Handler *create_handler;
40 Ecore_Event_Handler *destroy_handler;
41 Ecore_Event_Handler *focus_in_handler;
42 Ecore_Event_Handler *focus_out_handler;
45 .create_handler = NULL,
46 .destroy_handler = NULL,
47 .focus_in_handler = NULL,
48 .focus_out_handler = NULL,
49 .is_top = VCONFKEY_IDLE_SCREEN_TOP_FALSE,
58 static inline int _get_pid(Ecore_X_Window win)
62 unsigned char *in_pid = NULL;
65 atom = ecore_x_atom_get("X_CLIENT_PID");
66 if (ecore_x_window_prop_property_get(win, atom, ECORE_X_ATOM_CARDINAL,
67 sizeof(int), &in_pid, &num) == EINA_FALSE) {
72 if (ecore_x_netwm_pid_get(win, &pid) == EINA_FALSE) {
73 _E("Failed to get PID from a window 0x%X", win);
86 bool _set_idlescreen_top(void)
94 win = ecore_x_window_focus_get();
95 focused_pid = _get_pid(win);
96 retv_if(focused_pid <= 0, false);
98 is_top = menu_daemon_is_homescreen(focused_pid)?
99 VCONFKEY_IDLE_SCREEN_TOP_TRUE : VCONFKEY_IDLE_SCREEN_TOP_FALSE;
101 if (is_top != xmonitor_info.is_top) {
102 ret = vconf_set_int(VCONFKEY_IDLE_SCREEN_TOP, is_top);
103 retv_if(0 != ret, false);
104 xmonitor_info.is_top = is_top;
105 _D("set the key of idlescreen_is_top as %d", is_top);
113 static Eina_Bool _create_cb(void *data, int type, void *event)
115 Ecore_X_Event_Window_Create *info = event;
117 _D("Create a window[%x]", info->win);
119 ecore_x_window_client_sniff(info->win);
121 return ECORE_CALLBACK_PASS_ON;
126 static Eina_Bool _destroy_cb(void *data, int type, void *event)
128 return ECORE_CALLBACK_PASS_ON;
133 static Eina_Bool _focus_in_cb(void *data, int type, void *event)
135 Ecore_X_Event_Window_Focus_In *info = event;
137 _D("Focus in a window[%x]", info->win);
139 retv_if(false == _set_idlescreen_top(), ECORE_CALLBACK_PASS_ON);
141 return ECORE_CALLBACK_PASS_ON;
146 static Eina_Bool _focus_out_cb(void *data, int type, void *event)
148 Ecore_X_Event_Window_Focus_Out *info = event;
150 _D("Focus out a window[%x]", info->win);
152 return ECORE_CALLBACK_PASS_ON;
157 static inline void _sniff_all_windows(void)
161 struct stack_item *new_item;
162 struct stack_item *item;
163 Eina_List *win_stack = NULL;
165 Ecore_X_Window *wins;
170 root = ecore_x_window_root_first_get();
171 ecore_x_window_sniff(root);
173 new_item = malloc(sizeof(*new_item));
175 _E("Error(%s)\n", strerror(errno));
179 new_item->nr_of_wins = 0;
181 ecore_x_window_children_get(root, &new_item->nr_of_wins);
185 win_stack = eina_list_append(win_stack, new_item);
189 while ((item = eina_list_nth(win_stack, 0))) {
190 win_stack = eina_list_remove(win_stack, item);
197 while (item->i < item->nr_of_wins) {
198 ret = item->wins[item->i];
200 ecore_x_window_client_sniff(ret);
202 new_item = malloc(sizeof(*new_item));
204 _E("Error %s\n", strerror(errno));
210 new_item->nr_of_wins = 0;
212 ecore_x_window_children_get(ret,
213 &new_item->nr_of_wins);
214 if (new_item->wins) {
216 eina_list_append(win_stack, new_item);
233 int xmonitor_init(void)
235 if (ecore_x_composite_query() == EINA_FALSE)
236 _D("====> COMPOSITOR IS NOT ENABLED");
238 xmonitor_info.create_handler =
239 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_CREATE, _create_cb, NULL);
240 goto_if(NULL == xmonitor_info.create_handler, Error);
242 xmonitor_info.destroy_handler =
243 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_DESTROY, _destroy_cb, NULL);
244 goto_if(NULL == xmonitor_info.destroy_handler, Error);
246 xmonitor_info.focus_in_handler =
247 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_FOCUS_IN, _focus_in_cb, NULL);
248 goto_if(NULL == xmonitor_info.focus_in_handler, Error);
250 xmonitor_info.focus_out_handler =
251 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_FOCUS_OUT, _focus_out_cb, NULL);
252 goto_if(NULL == xmonitor_info.focus_out_handler, Error);
254 _sniff_all_windows();
255 if (false == _set_idlescreen_top()) _E("cannot set idlescreen_is_top");
260 if (xmonitor_info.create_handler) {
261 ecore_event_handler_del(xmonitor_info.create_handler);
262 xmonitor_info.create_handler = NULL;
263 } else return -EFAULT;
265 if (xmonitor_info.destroy_handler) {
266 ecore_event_handler_del(xmonitor_info.destroy_handler);
267 xmonitor_info.destroy_handler = NULL;
268 } else return -EFAULT;
270 if (xmonitor_info.focus_in_handler) {
271 ecore_event_handler_del(xmonitor_info.focus_in_handler);
272 xmonitor_info.focus_in_handler = NULL;
273 } else return -EFAULT;
275 if (xmonitor_info.focus_out_handler) {
276 ecore_event_handler_del(xmonitor_info.focus_out_handler);
277 xmonitor_info.focus_out_handler = NULL;
278 } else return -EFAULT;
283 void xmonitor_fini(void)
285 ecore_event_handler_del(xmonitor_info.create_handler);
286 xmonitor_info.create_handler = NULL;
288 ecore_event_handler_del(xmonitor_info.destroy_handler);
289 xmonitor_info.destroy_handler = NULL;
291 ecore_event_handler_del(xmonitor_info.focus_in_handler);
292 xmonitor_info.focus_in_handler = NULL;
294 ecore_event_handler_del(xmonitor_info.focus_out_handler);
295 xmonitor_info.focus_out_handler = NULL;