Code sync
[apps/native/starter.git] / src / starter.c
1  /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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
18
19 #include <Elementary.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <poll.h>
28
29 #include <aul.h>
30 #include <vconf.h>
31 #include <heynoti.h>
32 #include <signal.h>
33
34
35 #include "starter.h"
36 #include "starter-util.h"
37 #include "x11.h"
38 #include "lock-daemon.h"
39 #include "lockd-debug.h"
40 #include "menu_daemon.h"
41
42 #ifndef PACKAGE_NAME
43 #define PACKAGE_NAME "org.tizen.starter"
44 #endif
45
46 #define DEFAULT_THEME "tizen"
47 #define PWLOCK_PATH "/usr/apps/org.tizen.pwlock/bin/pwlock"
48
49 #define DATA_ENCRYPTED "encrypted"
50 #define DATA_MOUNTED "mounted"
51
52 static void lock_menu_screen(void)
53 {
54         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 0);
55 }
56
57 static void unlock_menu_screen(void)
58 {
59         int r;
60         int show_menu;
61
62         show_menu = 0;
63         r = vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &show_menu);
64         if (r || !show_menu) {
65                 vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
66         }
67 }
68
69 static void _set_elm_theme(void)
70 {
71         char *vstr;
72         char *theme;
73         Elm_Theme *th = NULL;
74         vstr = vconf_get_str(VCONFKEY_SETAPPL_WIDGET_THEME_STR);
75         if (vstr == NULL)
76                 theme = DEFAULT_THEME;
77         else
78                 theme = vstr;
79
80         th = elm_theme_new();
81         _DBG("theme vconf[%s]\n set[%s]\n", vstr, theme);
82         elm_theme_set(th, theme);
83
84         if (vstr)
85                 free(vstr);
86 }
87
88 #if 0
89 static void _set_elm_entry(void)
90 {
91         int v;
92         int r;
93
94         r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOCAPITAL_ALLOW_BOOL, &v);
95         if (!r) {
96                 prop_int_set("ENLIGHTENMENT_AUTOCAPITAL_ALLOW", v);
97                 _DBG("vconf autocatipal[%d]", v);
98         }
99
100         r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOPERIOD_ALLOW_BOOL, &v);
101         if (!r) {
102                 prop_int_set("ENLIGHTENMENT_AUTOPERIOD_ALLOW", v);
103                 _DBG("vconf autoperiod[%d]", v);
104         }
105 }
106 #endif
107
108 static int _launch_pwlock(void)
109 {
110         int r;
111         //int i = 0;
112
113         _DBG("%s", __func__);
114
115 #if 1
116         r = aul_launch_app("org.tizen.pwlock", NULL);
117         if (r < 0) {
118                 _ERR("PWLock launch error: error(%d)", r);
119                 if (r == AUL_R_ETIMEOUT) {
120                         _DBG("Launch pwlock is failed for AUL_R_ETIMEOUT, again launch pwlock");
121                         r = aul_launch_app("org.tizen.pwlock", NULL);
122                         if (r < 0) {
123                                 _ERR("2'nd PWLock launch error: error(%d)", r);
124                                 return -1;
125                         } else {
126                                 _DBG("Launch pwlock");
127                                 return 0;
128                         }
129                 } else {
130                         return -1;
131                 }
132         } else {
133                 _DBG("Launch pwlock");
134                 return 0;
135         }
136 #else
137  retry_con:
138         r = aul_launch_app("org.tizen.pwlock", NULL);
139         if (r < 0) {
140                 _ERR("PWLock launch error: error(%d)", r);
141                 if (r == AUL_R_ETIMEOUT) {
142                         i++;
143                         _DBG("Launching pwlock is failed [%d]times for AUL_R_ETIMEOUT ", i);
144                         goto retry_con;
145                 } else {
146                         return -1;
147                 }
148         } else {
149                 _DBG("Launch pwlock");
150                 return 0;
151         }
152 #endif
153 }
154
155 static void _signal_handler(int signum, siginfo_t *info, void *unused)
156 {
157     _DBG("_signal_handler : Terminated...");
158     elm_exit();
159 }
160
161 static void _heynoti_event_power_off(void *data)
162 {
163     _DBG("_heynoti_event_power_off : Terminated...");
164     elm_exit();
165 }
166
167 static Eina_Bool _init_idle(void *data)
168 {
169         _DBG("%s %d\n", __func__, __LINE__);
170         if (_launch_pwlock() < 0) {
171                 _ERR("launch pwlock error");
172         }
173         menu_daemon_init(NULL);
174
175         return ECORE_CALLBACK_CANCEL;
176 }
177
178 static void _lock_state_cb(keynode_t * node, void *data)
179 {
180         _DBG("%s %d\n", __func__, __LINE__);
181         WRITE_FILE_LOG("%s", "Lock state is changed!");
182
183         if (_launch_pwlock() < 0) {
184                 _ERR("launch pwlock error");
185         }
186         menu_daemon_init(NULL);
187         if (vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE,
188              _lock_state_cb) != 0) {
189                 LOCKD_DBG("Fail to unregister");
190         }
191 }
192
193 static void _data_encryption_cb(keynode_t * node, void *data)
194 {
195         int r;
196         char *file = NULL;
197
198         _DBG("%s %d\n", __func__, __LINE__);
199
200         file = vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
201         if (file != NULL) {
202                 _DBG("get %s : %s\n", VCONFKEY_ODE_CRYPTO_STATE, file);
203                 if (!strcmp(file, DATA_MOUNTED)) {
204                         start_lock_daemon(FALSE);
205                         menu_daemon_init(NULL);
206                 }
207                 free(file);
208         }
209 }
210
211 static void _init(struct appdata *ad)
212 {
213         int r;
214         struct sigaction act;
215         char *file = NULL;
216
217         memset(&act,0x00,sizeof(struct sigaction));
218         act.sa_sigaction = _signal_handler;
219         act.sa_flags = SA_SIGINFO;
220
221         int ret = sigemptyset(&act.sa_mask);
222         if (ret < 0) {
223                 _ERR("Failed to sigemptyset[%s]", strerror(errno));
224         }
225         ret = sigaddset(&act.sa_mask, SIGTERM);
226         if (ret < 0) {
227                 _ERR("Failed to sigaddset[%s]", strerror(errno));
228         }
229         ret = sigaction(SIGTERM, &act, NULL);
230         if (ret < 0) {
231                 _ERR("Failed to sigaction[%s]", strerror(errno));
232         }
233
234         memset(ad, 0, sizeof(struct appdata));
235
236         gettimeofday(&ad->tv_start, NULL);
237
238         lock_menu_screen();
239         _set_elm_theme();
240
241         _DBG("%s %d\n", __func__, __LINE__);
242
243         /* Check data encrption */
244         file = vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
245         if (file != NULL) {
246                 _DBG("get %s : %s\n", VCONFKEY_ODE_CRYPTO_STATE, file);
247                 if (!strcmp(file, DATA_ENCRYPTED)) {
248                         if (vconf_notify_key_changed(VCONFKEY_ODE_CRYPTO_STATE,
249                              _data_encryption_cb, NULL) != 0) {
250                                 _ERR("[Error] vconf notify changed : %s", VCONFKEY_ODE_CRYPTO_STATE);
251                         } else {
252                                 _DBG("waiting mount..!!");
253                                 if (_launch_pwlock() < 0) {
254                                         _ERR("launch pwlock error");
255                                 }
256                                 free(file);
257                                 return;
258                         }
259                 }
260                 free(file);
261         }
262
263         /* change the launching order of pwlock and lock mgr in booting time */
264         /* TODO: menu screen is showed before phone lock is showed in booting time */
265         /* FIXME Here lock daemon start..!! */
266         r = start_lock_daemon(TRUE);
267         if (r == 1) {
268                 if (vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
269                      _lock_state_cb, NULL) != 0) {
270                         _ERR("[Error] vconf notify : lock state");
271                         ecore_timer_add(1.5, _init_idle, NULL);
272                 }
273         } else {
274                 if (_launch_pwlock() < 0) {
275                         _ERR("launch pwlock error");
276                 }
277                 menu_daemon_init(NULL);
278         }
279 }
280
281 static void _fini(struct appdata *ad)
282 {
283         struct timeval tv, res;
284
285         if (ad == NULL) {
286                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
287                 return;
288         }
289
290         unlock_menu_screen();
291         menu_daemon_fini();
292
293         gettimeofday(&tv, NULL);
294         timersub(&tv, &ad->tv_start, &res);
295         _DBG("Total time: %d.%06d sec\n", (int)res.tv_sec, (int)res.tv_usec);
296 }
297
298 int main(int argc, char *argv[])
299 {
300         struct appdata ad;
301
302         WRITE_FILE_LOG("%s", "Main function is started in starter");
303 #if 0
304         set_window_scale();     /* not in loop */
305 #endif
306     int heyfd = heynoti_init();
307         if (heyfd < 0) {
308                 _ERR("Failed to heynoti_init[%d]", heyfd);
309         }
310
311         int ret = heynoti_subscribe(heyfd, "power_off_start", _heynoti_event_power_off, NULL);
312         if (ret < 0) {
313                 _ERR("Failed to heynoti_subscribe[%d]", ret);
314         }
315         ret = heynoti_attach_handler(heyfd);
316         if (ret < 0) {
317                 _ERR("Failed to heynoti_attach_handler[%d]", ret);
318         }
319
320         elm_init(argc, argv);
321
322         _init(&ad);
323
324         elm_run();
325
326         _fini(&ad);
327
328         elm_shutdown();
329
330         return 0;
331 }