4e07eeddbd4c67cf20d78dbe1beb52fdeee27cbf
[apps/home/starter.git] / boot-mgr / starter.c
1 /*
2  *  starter
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <Elementary.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <poll.h>
31
32 #include <aul.h>
33 #include <vconf.h>
34 #include <heynoti.h>
35
36 #include "starter.h"
37 #include "x11.h"
38 #include "lock-daemon.h"
39 #include "lockd-debug.h"
40
41 #ifndef PACKAGE_NAME
42 #define PACKAGE_NAME "org.tizen.starter"
43 #endif
44
45 #define PWLOCK_PKG "org.tizen.pwlock"
46 #define VCONFKEY_START "memory/startapps/sequence"
47 #define PWLOCK_FIRST_BOOT "db/setting/pwlock_boot"
48 #define DEFAULT_THEME "tizen"
49 #define PWLOCK_PATH "/opt/apps/org.tizen.pwlock/bin/pwlock"
50 #define HIB_CAPTURING "/opt/etc/.hib_capturing"
51
52 static void lock_menu_screen(void)
53 {
54         vconf_set_int(VCONFKEY_START, 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_START, &show_menu);
64         if (r || !show_menu) {
65                 vconf_set_int(VCONFKEY_START, 1);
66         }
67 }
68
69 static void _set_elm_theme(void)
70 {
71         char *vstr;
72         char *theme;
73         vstr = vconf_get_str(VCONFKEY_SETAPPL_WIDGET_THEME_STR);
74         if (vstr == NULL)
75                 theme = DEFAULT_THEME;
76         else
77                 theme = vstr;
78
79         _DBG("theme vconf[%s]\n set[%s]\n", vstr, theme);
80         elm_theme_all_set(theme);
81
82         if (vstr)
83                 free(vstr);
84 }
85
86 static void _set_elm_entry(void)
87 {
88         int v;
89         int r;
90
91         r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOCAPITAL_ALLOW_BOOL, &v);
92         if (!r) {
93                 prop_int_set("ENLIGHTENMENT_AUTOCAPITAL_ALLOW", v);
94                 _DBG("vconf autocatipal[%d]", v);
95         }
96
97         r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOPERIOD_ALLOW_BOOL, &v);
98         if (!r) {
99                 prop_int_set("ENLIGHTENMENT_AUTOPERIOD_ALLOW", v);
100                 _DBG("vconf autoperiod[%d]", v);
101         }
102 }
103
104 static int _launch_pwlock(void)
105 {
106         int r;
107
108         _DBG("%s", __func__);
109
110         r = aul_launch_app(PWLOCK_PKG, NULL);
111         if (r < 0) {
112                 _ERR("PWLock launch error: error(%d)", r);
113                 if (r == AUL_R_ETIMEOUT) {
114                         _DBG("Launch pwlock is failed for AUL_R_ETIMEOUT, again launch pwlock");
115                         r = aul_launch_app(PWLOCK_PKG, NULL);
116                         if (r < 0) {
117                                 _ERR("2'nd PWLock launch error: error(%d)", r);
118                                 return -1;
119                         } else {
120                                 _DBG("Launch pwlock");
121                                 return 0;
122                         }
123                 } else {
124                         return -1;
125                 }
126         } else {
127                 _DBG("Launch pwlock");
128                 return 0;
129         }
130 }
131
132 static void hib_leave(void *data)
133 {
134         struct appdata *ad = data;
135         if (ad == NULL) {
136                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
137                 return;
138         }
139
140         _DBG("%s", __func__);
141         _set_elm_theme();
142         start_lock_daemon();
143         if (_launch_pwlock() < 0) {
144                 _ERR("launch pwlock error");
145         }
146 }
147
148 static int add_noti(struct appdata *ad)
149 {
150         int fd;
151         int r;
152         _DBG("%s %d\n", __func__, __LINE__);
153
154         if (ad == NULL) {
155                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
156                 return -1;
157         }
158
159         fd = heynoti_init();
160         if (fd == -1) {
161                 _ERR("Noti init error");
162                 return -1;
163         }
164         ad->noti = fd;
165
166         r = heynoti_subscribe(fd, "HIBERNATION_PRELEAVE", hib_leave, ad);
167         if (r == -1) {
168                 _ERR("Noti subs error");
169                 return -1;
170         }
171
172         r = heynoti_attach_handler(fd);
173         if (r == -1) {
174                 _ERR("Noti attach error");
175                 return -1;
176         }
177
178         _DBG("Waiting for hib leave");
179         _DBG("%s %d\n", __func__, __LINE__);
180
181         return 0;
182 }
183
184 static int _init(struct appdata *ad)
185 {
186         int fd;
187         int r;
188
189         memset(ad, 0, sizeof(struct appdata));
190
191         ad->noti = -1;
192         gettimeofday(&ad->tv_start, NULL);
193
194         lock_menu_screen();
195         _set_elm_theme();
196         _set_elm_entry();
197
198         _DBG("%s %d\n", __func__, __LINE__);
199         fd = open(HIB_CAPTURING, O_RDONLY);
200         _DBG("fd = %d\n", fd);
201         if (fd == -1) {
202                 _DBG("fd = %d\n", fd);
203                 start_lock_daemon();
204                 if (_launch_pwlock() < 0) {
205                         _ERR("launch pwlock error");
206                 }
207                 r = 0;
208         } else {
209                 close(fd);
210                 r = add_noti(ad);
211         }
212
213         if (vconf_set_int("memory/hibernation/starter_ready", 1))
214                 _ERR("vconf_set_int FAIL");
215         else
216                 _ERR("vconf_set_int OK\n");
217
218         return r;
219 }
220
221 static void _fini(struct appdata *ad)
222 {
223         struct timeval tv, res;
224
225         if (ad == NULL) {
226                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
227                 return;
228         }
229         if (ad->noti != -1)
230                 heynoti_close(ad->noti);
231
232         unlock_menu_screen();
233
234         gettimeofday(&tv, NULL);
235         timersub(&tv, &ad->tv_start, &res);
236         _DBG("Total time: %d.%06d sec\n", (int)res.tv_sec, (int)res.tv_usec);
237 }
238
239 int main(int argc, char *argv[])
240 {
241         struct appdata ad;
242
243         set_window_scale();
244
245         elm_init(argc, argv);
246
247         _init(&ad);
248
249         elm_run();
250
251         _fini(&ad);
252
253         elm_shutdown();
254
255         return 0;
256 }