Fix bug in dead timer cb
[apps/native/starter.git] / src / mobile / starter.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 <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
26 #include <aul.h>
27 #include <vconf.h>
28 #include <signal.h>
29
30 #include "starter.h"
31 #include "lock_mgr.h"
32 #include "lock_pwd_util.h"
33 #include "lock_pwd_control_panel.h"
34 #include "home_mgr.h"
35 #include "hw_key.h"
36 #include "process_mgr.h"
37 #include "util.h"
38 #include "status.h"
39
40 #ifdef HAVE_X11
41 #include "hw_key.h"
42 #endif
43
44 #define PWLOCK_LITE_PKG_NAME "org.tizen.pwlock-lite"
45
46 #define DATA_UNENCRYPTED "unencrypted"
47 #define DATA_MOUNTED "mounted"
48 #define SD_DATA_ENCRYPTED "encrypted"
49 #define SD_CRYPT_META_FILE ".MetaEcfsFile"
50 #define MMC_MOUNT_POINT "/opt/storage/sdcard"
51
52
53
54 static void _hide_home(void)
55 {
56         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 0);
57 }
58
59
60
61 static void _show_home(void)
62 {
63         int show_menu = 0;
64
65         if (status_active_get()->starter_sequence || !show_menu) {
66                 vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
67         }
68 }
69
70
71
72 static Eina_Bool _finish_boot_animation(void *data)
73 {
74         if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0) {
75                 _E("Failed to set boot animation finished set");
76         }
77         _show_home();
78
79         return ECORE_CALLBACK_CANCEL;
80 }
81
82
83
84 static int _fail_to_launch_pwlock(const char *appid, const char *key, const char *value, void *cfn, void *afn)
85 {
86         _finish_boot_animation(NULL);
87         return 0;
88 }
89
90
91
92 static void _after_launch_pwlock(int pid)
93 {
94         process_mgr_set_pwlock_priority(pid);
95         ecore_timer_add(0.5, _finish_boot_animation, NULL);
96 }
97
98
99
100 static void _signal_handler(int signum, siginfo_t *info, void *unused)
101 {
102     _D("_signal_handler : Terminated...");
103     elm_exit();
104 }
105
106
107
108 static int _power_off_cb(status_active_key_e key, void *data)
109 {
110         int val = status_active_get()->sysman_power_off_status;
111
112         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT
113                 || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART)
114         {
115             _D("_power_off_cb : Terminated...");
116             elm_exit();
117         }
118
119         return 1;
120 }
121
122
123
124 static void _language_changed_cb(keynode_t *node, void *data)
125 {
126         char *lang = NULL;
127
128         ret_if(!node);
129
130         lang = vconf_keynode_get_str(node);
131         ret_if(!lang);
132
133         _D("language is changed : %s", lang);
134
135         elm_language_set(lang);
136
137         lock_pwd_util_view_init();
138         lock_pwd_control_panel_emg_btn_text_update();
139 }
140
141
142
143 static int _set_i18n(const char *domain, const char *dir)
144 {
145         char *r = NULL;
146
147         if (domain == NULL) {
148                 errno = EINVAL;
149                 return -1;
150         }
151
152         char *lang = vconf_get_str(VCONFKEY_LANGSET);
153         r = setlocale(LC_ALL, lang);
154         if (!r) {
155                 _E("setlocale() error");
156         }
157         if (lang) {
158                 free(lang);
159         }
160
161         r = bindtextdomain(domain, dir);
162         if (!r) {
163                 _E("bindtextdomain() error");
164         }
165
166         r = textdomain(domain);
167         if (!r) {
168                 _E("textdomain() error");
169         }
170
171         if (vconf_notify_key_changed(VCONFKEY_LANGSET, _language_changed_cb, NULL) < 0) {
172                 _E("Failed to register changed cb : %s", VCONFKEY_LANGSET);
173         }
174
175         return 0;
176 }
177
178
179
180 static int _check_dead_signal(int pid, void *data)
181 {
182         int home_pid = 0;
183         int volume_pid = 0;
184         int lock_pid = 0;
185
186         _D("Process %d is termianted", pid);
187
188         if (pid < 0) {
189                 _E("pid : %d", pid);
190                 return 0;
191         }
192
193         home_pid = home_mgr_get_home_pid();
194         volume_pid = home_mgr_get_volume_pid();
195         lock_pid = lock_mgr_get_lock_pid();
196
197         if (pid == home_pid) {
198                 _D("Homescreen is dead");
199                 home_mgr_relaunch_homescreen();
200         } else if (pid == volume_pid) {
201                 _D("volume is dead");
202                 home_mgr_relaunch_volume();
203         } else if (pid == lock_pid) {
204                 _D("lockscreen is dead");
205                 lock_mgr_unlock();
206         } else {
207                 _D("Unknown process, ignore it");
208         }
209
210         return 0;
211 }
212
213
214
215 static void _init(struct appdata *ad)
216 {
217         struct sigaction act;
218
219         memset(&act,0x00,sizeof(struct sigaction));
220         act.sa_sigaction = _signal_handler;
221         act.sa_flags = SA_SIGINFO;
222
223         int ret = sigemptyset(&act.sa_mask);
224         if (ret < 0) {
225                 _E("Failed to sigemptyset[%s]", strerror(errno));
226         }
227         ret = sigaddset(&act.sa_mask, SIGTERM);
228         if (ret < 0) {
229                 _E("Failed to sigaddset[%s]", strerror(errno));
230         }
231         ret = sigaction(SIGTERM, &act, NULL);
232         if (ret < 0) {
233                 _E("Failed to sigaction[%s]", strerror(errno));
234         }
235
236         _set_i18n(PACKAGE, LOCALEDIR);
237
238         status_register();
239         status_active_register_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb, NULL);
240
241         /* Ordering : _hide_home -> process_mgr_must_launch(pwlock) -> _show_home */
242         _hide_home();
243         process_mgr_must_launch(PWLOCK_LITE_PKG_NAME, NULL, NULL, _fail_to_launch_pwlock, _after_launch_pwlock);
244
245         lock_mgr_daemon_start();
246 #ifdef HAVE_X11
247         hw_key_create_window();
248 #endif
249         home_mgr_init(NULL);
250
251         aul_listen_app_dead_signal(_check_dead_signal, NULL);
252 }
253
254
255
256 static void _fini(struct appdata *ad)
257 {
258         home_mgr_fini();
259 #ifdef HAVE_X11
260         hw_key_destroy_window();
261 #endif
262         lock_mgr_daemon_end();
263
264         status_active_unregister_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb);
265         status_unregister();
266
267         if (vconf_ignore_key_changed(VCONFKEY_LANGSET, _language_changed_cb) < 0) {
268                 _E("Failed to unregister changed cb : %s", VCONFKEY_LANGSET);
269         }
270 }
271
272
273
274 int main(int argc, char *argv[])
275 {
276         struct appdata ad;
277
278         _D("starter is launched..!!");
279
280         elm_init(argc, argv);
281         _init(&ad);
282
283         elm_run();
284
285         _fini(&ad);
286         elm_shutdown();
287
288         return 0;
289 }