[mobile] Remove password lockscreen
[apps/native/starter.git] / src / common / window_mgr.c
1 /*
2  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Elementary.h>
18 #include <vconf.h>
19 #include <bundle.h>
20 #include <appcore-efl.h>
21 #include <app.h>
22
23 #ifdef HAVE_X11
24 #include <Ecore_X.h>
25 #include <utilX.h>
26 #include <ui-gadget.h>
27 #endif
28
29 #include "window_mgr.h"
30 #include "util.h"
31
32 #define STR_ATOM_PANEL_SCROLLABLE_STATE         "_E_MOVE_PANEL_SCROLLABLE_STATE"
33
34
35
36 #ifdef HAVE_X11
37 struct _lockw_data {
38         Eina_Bool is_registered;
39         Ecore_X_Window lock_x_window;
40
41         Ecore_Event_Handler *h_wincreate;
42         Ecore_Event_Handler *h_winshow;
43         Ecore_Event_Handler *h_winhide;
44 };
45
46
47
48 static int _is_on_screen(Ecore_X_Display * dpy, Ecore_X_Window window)
49 {
50         Ecore_X_Window root;
51         Window child;
52         Window win;
53
54         int rel_x = 0;
55         int rel_y = 0;
56         int abs_x = 0;
57         int abs_y = 0;
58
59         unsigned int width = 0;
60         unsigned int height = 0;
61         unsigned int border = 0;
62         unsigned int depth = 0;
63         unsigned int root_w = 0;
64         unsigned int root_h = 0;
65
66         Eina_Bool ret = FALSE;
67
68         root = ecore_x_window_root_first_get();
69         XGetGeometry(dpy, root, &win, &rel_x, &rel_y, &root_w, &root_h, &border, &depth);
70         _D("root rel_x[%d] rel_y[%d] border[%d] width[%d] height[%d]", rel_x, rel_y, border, root_w, root_h);
71
72         if (XGetGeometry(dpy, window, &win, &rel_x, &rel_y, &width, &height, &border, &depth)) {
73                 if (XTranslateCoordinates(dpy, window, root, 0, 0, &abs_x, &abs_y, &child)) {
74                     _D("abs_x[%d] abs_y[%d] border[%d] width[%d] height[%d]", abs_x, abs_y, border, width, height);
75                         if ((abs_x - border) >= root_w
76                                 || (abs_y - border) >= root_h
77                             || (width + abs_x) <= 0 || (height + abs_y) <= 0)
78                         {
79                                 ret = FALSE;
80                         } else {
81                                 ret = (width == root_w) && (height == root_h);
82                         }
83                 }
84         }
85
86         return ret;
87 }
88
89
90
91 static Window _get_user_created_window(Window win)
92 {
93         Atom type_ret = 0;
94         int ret, size_ret = 0;
95         unsigned long num_ret = 0, bytes = 0;
96         unsigned char *prop_ret = NULL;
97         unsigned int xid;
98         Atom prop_user_created_win;
99
100         prop_user_created_win = XInternAtom(ecore_x_display_get(), "_E_USER_CREATED_WINDOW", False);
101
102         ret = XGetWindowProperty(ecore_x_display_get()
103                                                         , win, prop_user_created_win
104                                                         , 0L, 1L
105                                                         , False, 0
106                                                         , &type_ret, &size_ret
107                                                         , &num_ret, &bytes
108                                                         , &prop_ret);
109         if (ret != Success) {
110                 if (prop_ret)
111                         XFree((void *) prop_ret);
112                 return win;
113         } else if (!prop_ret) {
114                 return win;
115         }
116
117         memcpy(&xid, prop_ret, sizeof(unsigned int));
118         XFree((void *)prop_ret);
119
120         return xid;
121
122 }
123
124
125
126 int window_mgr_get_focus_window_pid(void)
127 {
128         Ecore_X_Window x_win_focused = 0;
129         int pid = 0;
130         int ret = -1;
131
132         _D("%s, %d", __func__, __LINE__);
133
134         x_win_focused = ecore_x_window_focus_get();
135         ret = ecore_x_netwm_pid_get(x_win_focused, &pid);
136         if(ret != 1) {
137                 _E("Can't get pid for focus x window (%x)\n", x_win_focused);
138                 return -1;
139         }
140         _D("PID(%d) for focus x window (%x)\n", pid, x_win_focused);
141
142         return pid;
143 }
144
145
146
147 Eina_Bool window_mgr_set_prop(lockw_data * data, int lock_app_pid, void *event)
148 {
149         Ecore_X_Event_Window_Create *e = event;
150         Ecore_X_Window user_window = 0;
151         lockw_data *lockw = (lockw_data *) data;
152         int pid = 0;
153         int ret = 0;
154
155         retv_if(!lockw, EINA_FALSE);
156
157         user_window = _get_user_created_window((Window) (e->win));
158
159         ret = ecore_x_netwm_pid_get(user_window, &pid);
160         retv_if(ret != 1, EINA_FALSE);
161
162         _D("Check PID(%d) window. (lock_app_pid : %d)", pid, lock_app_pid);
163
164         if (lock_app_pid == pid) {
165                 if (_is_on_screen(ecore_x_display_get(), user_window) == TRUE) {
166                         lockw->lock_x_window = user_window;
167                         /* window effect : fade in /out */
168                         ecore_x_icccm_name_class_set(user_window, "LOCK_SCREEN", "LOCK_SCREEN");
169                         ecore_x_netwm_window_type_set(user_window, ECORE_X_WINDOW_TYPE_NOTIFICATION);
170                         utilx_set_system_notification_level(ecore_x_display_get(), user_window, UTILX_NOTIFICATION_LEVEL_NORMAL);
171                         utilx_set_window_opaque_state(ecore_x_display_get(), user_window, UTILX_OPAQUE_STATE_ON);
172                         return EINA_TRUE;
173                 }
174         }
175         return EINA_FALSE;
176 }
177
178
179
180 Eina_Bool window_mgr_set_effect(lockw_data * data, int lock_app_pid, void *event)
181 {
182         Ecore_X_Event_Window_Create *e = event;
183         Ecore_X_Window user_window = 0;
184         int pid = 0;
185         int ret = 0;
186
187         user_window = _get_user_created_window((Window) (e->win));
188         ret = ecore_x_netwm_pid_get(user_window, &pid);
189         retv_if(ret != 1, EINA_FALSE);
190
191         if (lock_app_pid == pid) {
192                 if (_is_on_screen(ecore_x_display_get(), user_window) == TRUE) {
193                         Ecore_X_Atom ATOM_WINDOW_EFFECT_ENABLE = 0;
194                         unsigned int effect_state = 0;
195
196                         ATOM_WINDOW_EFFECT_ENABLE = ecore_x_atom_get("_NET_CM_WINDOW_EFFECT_ENABLE");
197                         if (ATOM_WINDOW_EFFECT_ENABLE) {
198                                 ecore_x_window_prop_card32_set(user_window, ATOM_WINDOW_EFFECT_ENABLE, &effect_state, 1);
199                         } else {
200                                 _E("ecore_x_atom_get() failed");
201                         }
202                         return EINA_TRUE;
203                 }
204         }
205         return EINA_FALSE;
206 }
207
208
209
210 void window_mgr_set_scroll_prop(lockw_data *data, int lock_type)
211 {
212         lockw_data *lockw = (lockw_data *) data;
213         Ecore_X_Atom ATOM_PANEL_SCROLLABLE_STATE = 0;
214         unsigned int val[3] = { 0, };
215
216         ret_if(!lockw);
217
218         ATOM_PANEL_SCROLLABLE_STATE = ecore_x_atom_get(STR_ATOM_PANEL_SCROLLABLE_STATE);
219         if (lock_type == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD ||
220                         lock_type == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
221                 val[0] = 0; // always enable F
222                 val[1] = 0; // quickpanel enable F
223                 val[2] = 0; // apptray enable F
224         } else {
225                 val[0] = 0; // always enable F
226                 val[1] = 1; // quickpanel enable T
227                 val[2] = 0; // apptray enable F
228         }
229         ecore_x_window_prop_card32_set(lockw->lock_x_window, ATOM_PANEL_SCROLLABLE_STATE, val, 3);
230 }
231
232
233
234 void window_mgr_register_event(void *data, lockw_data * lockw,
235                             Eina_Bool (*create_cb) (void *, int, void *),
236                             Eina_Bool (*show_cb) (void *, int, void *))
237 {
238         Ecore_X_Window root_window;
239
240         ret_if(!lockw);
241
242         if (lockw->is_registered) {
243                 _E("Already register event cb");
244                 return;
245         }
246
247         /* For getting window x event */
248         root_window = ecore_x_window_root_first_get();
249         ecore_x_window_client_sniff(root_window);
250
251         lockw->h_wincreate = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_CREATE, create_cb, data);
252         lockw->h_winshow = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, show_cb, data);
253
254         lockw->is_registered = EINA_TRUE;
255 }
256
257
258
259 static inline void _unregister_event(lockw_data *lockw)
260 {
261         Ecore_X_Window root_window;
262
263         /* delete getting window x event */
264         root_window = ecore_x_window_root_first_get();
265         ecore_x_window_client_sniff(root_window);
266
267         /* delete window create event handler */
268         if (lockw->h_wincreate) {
269                 ecore_event_handler_del(lockw->h_wincreate);
270                 lockw->h_wincreate = NULL;
271         }
272         if (lockw->h_winshow) {
273                 ecore_event_handler_del(lockw->h_winshow);
274                 lockw->h_winshow = NULL;
275         }
276         if (lockw->h_winhide) {
277                 ecore_event_handler_del(lockw->h_winhide);
278                 lockw->h_winhide = NULL;
279         }
280
281         ecore_x_pointer_ungrab();
282
283         lockw->is_registered = EINA_FALSE;
284 }
285
286
287
288 void window_mgr_unregister_event(lockw_data *lockw)
289 {
290         ret_if(!lockw);
291
292         if (!lockw->is_registered) {
293                 _E("event cb is not registered");
294                 return;
295         }
296
297         _unregister_event(lockw);
298 }
299
300
301
302 lockw_data *window_mgr_init(void)
303 {
304         lockw_data *lockw = NULL;
305
306         lockw = calloc(1, sizeof(*lockw));
307
308         return lockw;
309 }
310
311
312
313 void window_mgr_fini(lockw_data *lockw)
314 {
315         ret_if(!lockw);
316
317         if (lockw->is_registered) {
318                 _unregister_event(lockw);
319         }
320
321         free(lockw);
322 }
323 #endif