added pwlock launch
[apps/home/starter.git] / boot-mgr / 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 #include <Elementary.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <poll.h>
26
27 #include <aul.h>
28 #include <vconf.h>
29 #include <heynoti.h>
30 #include <signal.h>
31
32
33 #include "starter.h"
34 #include "x11.h"
35 #include "lock-daemon.h"
36 #include "lockd-debug.h"
37
38 #define DEFAULT_THEME "tizen"
39
40 #define HIB_CAPTURING "/opt/etc/.hib_capturing"
41 #define STR_STARTER_READY "/tmp/hibernation/starter_ready"
42
43 static void lock_menu_screen(void)
44 {
45         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 0);
46 }
47
48 static void unlock_menu_screen(void)
49 {
50         int r;
51         int show_menu;
52
53         show_menu = 0;
54         r = vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &show_menu);
55         if (r || !show_menu) {
56                 vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
57         }
58 }
59
60 static void _set_elm_theme(void)
61 {
62         char *vstr;
63         char *theme;
64         Elm_Theme *th = NULL;
65         vstr = vconf_get_str(VCONFKEY_SETAPPL_WIDGET_THEME_STR);
66         if (vstr == NULL)
67                 theme = DEFAULT_THEME;
68         else
69                 theme = vstr;
70
71         th = elm_theme_new();
72         _DBG("theme vconf[%s]\n set[%s]\n", vstr, theme);
73         elm_theme_set(th, theme);
74
75         if (vstr)
76                 free(vstr);
77 }
78 static int _launch_pwlock(void)
79 {
80         int r;
81
82         _DBG("%s", __func__);
83
84         r = aul_launch_app("org.tizen.pwlock", NULL);
85         if (r < 0) {
86                 _ERR("PWLock launch error: error(%d)", r);
87                 if (r == AUL_R_ETIMEOUT) {
88                         _DBG("Launch pwlock is failed for AUL_R_ETIMEOUT, again launch pwlock");
89                         r = aul_launch_app("org.tizen.pwlock", NULL);
90                         if (r < 0) {
91                                 _ERR("2'nd PWLock launch error: error(%d)", r);
92                                 return -1;
93                         } else {
94                                 _DBG("Launch pwlock");
95                                 return 0;
96                         }
97                 } else {
98                         return -1;
99                 }
100         } else {
101                 _DBG("Launch pwlock");
102                 return 0;
103         }
104
105 }
106
107 static void hib_leave(void *data)
108 {
109         struct appdata *ad = data;
110         if (ad == NULL) {
111                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
112                 return;
113         }
114
115         _DBG("%s", __func__);
116         _set_elm_theme();
117         start_lock_daemon();
118         if (_launch_pwlock() < 0) {
119                 _ERR("launch pwlock error");
120         }
121 }
122
123 static int add_noti(struct appdata *ad)
124 {
125         int fd;
126         int r;
127         _DBG("%s %d\n", __func__, __LINE__);
128
129         if (ad == NULL) {
130                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
131                 return -1;
132         }
133
134         fd = heynoti_init();
135         if (fd == -1) {
136                 _ERR("Noti init error");
137                 return -1;
138         }
139         ad->noti = fd;
140
141         r = heynoti_subscribe(fd, "HIBERNATION_PRELEAVE", hib_leave, ad);
142         if (r == -1) {
143                 _ERR("Noti subs error");
144                 return -1;
145         }
146
147         r = heynoti_attach_handler(fd);
148         if (r == -1) {
149                 _ERR("Noti attach error");
150                 return -1;
151         }
152
153         _DBG("Waiting for hib leave");
154         _DBG("%s %d\n", __func__, __LINE__);
155
156         return 0;
157 }
158
159 static void _signal_handler(int signum, siginfo_t *info, void *unused)
160 {
161     _DBG("_signal_handler : Terminated...");
162     elm_exit();
163 }
164 static void _heynoti_event_power_off(void *data)
165 {
166     _DBG("_heynoti_event_power_off : Terminated...");
167     elm_exit();
168 }
169
170 static int _init(struct appdata *ad)
171 {
172         int fd;
173         int r;
174         int fd1;
175
176         struct sigaction act;
177         act.sa_sigaction = _signal_handler;
178         act.sa_flags = SA_SIGINFO;
179
180         int ret = sigemptyset(&act.sa_mask);
181         if (ret < 0) {
182                 _ERR("Failed to sigemptyset[%s]", strerror(errno));
183         }
184         ret = sigaddset(&act.sa_mask, SIGTERM);
185         if (ret < 0) {
186                 _ERR("Failed to sigaddset[%s]", strerror(errno));
187         }
188         ret = sigaction(SIGTERM, &act, NULL);
189         if (ret < 0) {
190                 _ERR("Failed to sigaction[%s]", strerror(errno));
191         }
192
193         memset(ad, 0, sizeof(struct appdata));
194
195         ad->noti = -1;
196         gettimeofday(&ad->tv_start, NULL);
197
198         lock_menu_screen();
199         _set_elm_theme();
200
201         _DBG("%s %d\n", __func__, __LINE__);
202
203         fd = open(HIB_CAPTURING, O_RDONLY);
204         _DBG("fd = %d\n", fd);
205         if (fd == -1) {
206                 _DBG("fd = %d\n", fd);
207                 start_lock_daemon();
208                 if (_launch_pwlock() < 0) {
209                         _ERR("launch pwlock error");
210                 }
211                 r = 0;
212         } else {
213                 close(fd);
214                 r = add_noti(ad);
215         }
216
217         fd1 = open(STR_STARTER_READY, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
218         if (fd1 > 0) {
219                 _DBG("Hibernation ready.\n");
220                 close(fd1);
221         }
222
223         return r;
224 }
225
226 static void _fini(struct appdata *ad)
227 {
228         struct timeval tv, res;
229
230         if (ad == NULL) {
231                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
232                 return;
233         }
234         if (ad->noti != -1)
235                 heynoti_close(ad->noti);
236
237         unlock_menu_screen();
238
239         gettimeofday(&tv, NULL);
240         timersub(&tv, &ad->tv_start, &res);
241         _DBG("Total time: %d.%06d sec\n", (int)res.tv_sec, (int)res.tv_usec);
242 }
243
244 int main(int argc, char *argv[])
245 {
246         struct appdata ad;
247
248         int heyfd = heynoti_init();
249         if (heyfd < 0) {
250                 _ERR("Failed to heynoti_init[%d]", heyfd);
251         }
252
253         int ret = heynoti_subscribe(heyfd, "power_off_start", _heynoti_event_power_off, NULL);
254         if (ret < 0) {
255                 _ERR("Failed to heynoti_subscribe[%d]", ret);
256         }
257         ret = heynoti_attach_handler(heyfd);
258         if (ret < 0) {
259                 _ERR("Failed to heynoti_attach_handler[%d]", ret);
260         }
261
262         elm_init(argc, argv);
263
264         _init(&ad);
265
266         elm_run();
267
268         _fini(&ad);
269
270         elm_shutdown();
271
272         return 0;
273 }