[mobile] Remove password lockscreen
[apps/native/starter.git] / src / ivi / 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
173                         return EINA_TRUE;
174                 }
175         }
176         return EINA_FALSE;
177 }
178
179
180
181 Eina_Bool window_mgr_set_effect(lockw_data * data, int lock_app_pid, void *event)
182 {
183         Ecore_X_Event_Window_Create *e = event;
184         Ecore_X_Window user_window = 0;
185         int pid = 0;
186         int ret = 0;
187
188         user_window = _get_user_created_window((Window) (e->win));
189         ret = ecore_x_netwm_pid_get(user_window, &pid);
190         retv_if(ret != 1, EINA_FALSE);
191
192         if (lock_app_pid == pid) {
193                 if (_is_on_screen(ecore_x_display_get(), user_window) == TRUE) {
194                         Ecore_X_Atom ATOM_WINDOW_EFFECT_ENABLE = 0;
195                         unsigned int effect_state = 0;
196
197                         ATOM_WINDOW_EFFECT_ENABLE = ecore_x_atom_get("_NET_CM_WINDOW_EFFECT_ENABLE");
198                         if (ATOM_WINDOW_EFFECT_ENABLE) {
199                                 ecore_x_window_prop_card32_set(user_window, ATOM_WINDOW_EFFECT_ENABLE, &effect_state, 1);
200                         } else {
201                                 _E("ecore_x_atom_get() failed");
202                         }
203                         return EINA_TRUE;
204                 }
205         }
206         return EINA_FALSE;
207 }
208
209
210
211 void window_mgr_set_scroll_prop(lockw_data *data, int lock_type)
212 {
213         lockw_data *lockw = (lockw_data *) data;
214         Ecore_X_Atom ATOM_PANEL_SCROLLABLE_STATE = 0;
215         unsigned int val[3] = { 0, };
216
217         ret_if(!lockw);
218
219         ATOM_PANEL_SCROLLABLE_STATE = ecore_x_atom_get(STR_ATOM_PANEL_SCROLLABLE_STATE);
220         if (lock_type == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD ||
221                         lock_type == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
222                 val[0] = 0; // always enable F
223                 val[1] = 0; // quickpanel enable F
224                 val[2] = 0; // apptray enable F
225         } else {
226                 val[0] = 0; // always enable F
227                 val[1] = 1; // quickpanel enable T
228                 val[2] = 0; // apptray enable F
229         }
230         ecore_x_window_prop_card32_set(lockw->lock_x_window, ATOM_PANEL_SCROLLABLE_STATE, val, 3);
231 }
232
233
234
235 void window_mgr_register_event(void *data, lockw_data * lockw,
236                             Eina_Bool (*create_cb) (void *, int, void *),
237                             Eina_Bool (*show_cb) (void *, int, void *))
238 {
239         Ecore_X_Window root_window;
240
241         ret_if(!lockw);
242
243         if (lockw->is_registered) {
244                 _E("Already register event cb");
245                 return;
246         }
247
248         /* For getting window x event */
249         root_window = ecore_x_window_root_first_get();
250         ecore_x_window_client_sniff(root_window);
251
252         lockw->h_wincreate = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_CREATE, create_cb, data);
253         lockw->h_winshow = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, show_cb, data);
254
255         lockw->is_registered = EINA_TRUE;
256 }
257
258
259
260 static inline void _unregister_event(lockw_data *lockw)
261 {
262         Ecore_X_Window root_window;
263
264         /* delete getting window x event */
265         root_window = ecore_x_window_root_first_get();
266         ecore_x_window_client_sniff(root_window);
267
268         /* delete window create event handler */
269         if (lockw->h_wincreate) {
270                 ecore_event_handler_del(lockw->h_wincreate);
271                 lockw->h_wincreate = NULL;
272         }
273         if (lockw->h_winshow) {
274                 ecore_event_handler_del(lockw->h_winshow);
275                 lockw->h_winshow = NULL;
276         }
277         if (lockw->h_winhide) {
278                 ecore_event_handler_del(lockw->h_winhide);
279                 lockw->h_winhide = NULL;
280         }
281
282         ecore_x_pointer_ungrab();
283
284         lockw->is_registered = EINA_FALSE;
285 }
286
287
288
289 void window_mgr_unregister_event(lockw_data *lockw)
290 {
291         ret_if(!lockw);
292
293         if (!lockw->is_registered) {
294                 _E("event cb is not registered");
295                 return;
296         }
297
298         _unregister_event(lockw);
299 }
300
301
302
303 lockw_data *window_mgr_init(void)
304 {
305         lockw_data *lockw = NULL;
306
307         lockw = calloc(1, sizeof(*lockw));
308
309         return lockw;
310 }
311
312
313
314 void window_mgr_fini(lockw_data *lockw)
315 {
316         ret_if(!lockw);
317
318         if (lockw->is_registered) {
319                 _unregister_event(lockw);
320         }
321
322         free(lockw);
323 }
324 #endif