09b742d0881e180995c1f05a9d327acd4bfcf73d
[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
79 static void hib_leave(void *data)
80 {
81         struct appdata *ad = data;
82         if (ad == NULL) {
83                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
84                 return;
85         }
86
87         _DBG("%s", __func__);
88         _set_elm_theme();
89         start_lock_daemon();
90 }
91
92 static int add_noti(struct appdata *ad)
93 {
94         int fd;
95         int r;
96         _DBG("%s %d\n", __func__, __LINE__);
97
98         if (ad == NULL) {
99                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
100                 return -1;
101         }
102
103         fd = heynoti_init();
104         if (fd == -1) {
105                 _ERR("Noti init error");
106                 return -1;
107         }
108         ad->noti = fd;
109
110         r = heynoti_subscribe(fd, "HIBERNATION_PRELEAVE", hib_leave, ad);
111         if (r == -1) {
112                 _ERR("Noti subs error");
113                 return -1;
114         }
115
116         r = heynoti_attach_handler(fd);
117         if (r == -1) {
118                 _ERR("Noti attach error");
119                 return -1;
120         }
121
122         _DBG("Waiting for hib leave");
123         _DBG("%s %d\n", __func__, __LINE__);
124
125         return 0;
126 }
127
128 static void _signal_handler(int signum, siginfo_t *info, void *unused)
129 {
130     _DBG("_signal_handler : Terminated...");
131     elm_exit();
132 }
133 static void _heynoti_event_power_off(void *data)
134 {
135     _DBG("_heynoti_event_power_off : Terminated...");
136     elm_exit();
137 }
138
139 static int _init(struct appdata *ad)
140 {
141         int fd;
142         int r;
143         int fd1;
144
145         struct sigaction act;
146         act.sa_sigaction = _signal_handler;
147         act.sa_flags = SA_SIGINFO;
148
149         int ret = sigemptyset(&act.sa_mask);
150         if (ret < 0) {
151                 _ERR("Failed to sigemptyset[%s]", strerror(errno));
152         }
153         ret = sigaddset(&act.sa_mask, SIGTERM);
154         if (ret < 0) {
155                 _ERR("Failed to sigaddset[%s]", strerror(errno));
156         }
157         ret = sigaction(SIGTERM, &act, NULL);
158         if (ret < 0) {
159                 _ERR("Failed to sigaction[%s]", strerror(errno));
160         }
161
162         memset(ad, 0, sizeof(struct appdata));
163
164         ad->noti = -1;
165         gettimeofday(&ad->tv_start, NULL);
166
167         lock_menu_screen();
168         _set_elm_theme();
169
170         _DBG("%s %d\n", __func__, __LINE__);
171
172         fd = open(HIB_CAPTURING, O_RDONLY);
173         _DBG("fd = %d\n", fd);
174         if (fd == -1) {
175                 _DBG("fd = %d\n", fd);
176                 start_lock_daemon();
177                 r = 0;
178         } else {
179                 close(fd);
180                 r = add_noti(ad);
181         }
182
183         fd1 = open(STR_STARTER_READY, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
184         if (fd1 > 0) {
185                 _DBG("Hibernation ready.\n");
186                 close(fd1);
187         }
188
189         return r;
190 }
191
192 static void _fini(struct appdata *ad)
193 {
194         struct timeval tv, res;
195
196         if (ad == NULL) {
197                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
198                 return;
199         }
200         if (ad->noti != -1)
201                 heynoti_close(ad->noti);
202
203         unlock_menu_screen();
204
205         gettimeofday(&tv, NULL);
206         timersub(&tv, &ad->tv_start, &res);
207         _DBG("Total time: %d.%06d sec\n", (int)res.tv_sec, (int)res.tv_usec);
208 }
209
210 int main(int argc, char *argv[])
211 {
212         struct appdata ad;
213
214         int heyfd = heynoti_init();
215         if (heyfd < 0) {
216                 _ERR("Failed to heynoti_init[%d]", heyfd);
217         }
218
219         int ret = heynoti_subscribe(heyfd, "power_off_start", _heynoti_event_power_off, NULL);
220         if (ret < 0) {
221                 _ERR("Failed to heynoti_subscribe[%d]", ret);
222         }
223         ret = heynoti_attach_handler(heyfd);
224         if (ret < 0) {
225                 _ERR("Failed to heynoti_attach_handler[%d]", ret);
226         }
227
228         elm_init(argc, argv);
229
230         _init(&ad);
231
232         elm_run();
233
234         _fini(&ad);
235
236         elm_shutdown();
237
238         return 0;
239 }