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