Fix bug that is occurred by changing user
[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         int seq = status_active_get()->starter_sequence;
57         ret_if(seq == 1);
58
59         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 0);
60 }
61
62
63
64 static void _show_home(void)
65 {
66         int show_menu = 0;
67
68         if (status_active_get()->starter_sequence || !show_menu) {
69                 vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
70         }
71 }
72
73
74
75 static Eina_Bool _finish_boot_animation(void *data)
76 {
77         if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0) {
78                 _E("Failed to set boot animation finished set");
79         }
80         _show_home();
81
82         return ECORE_CALLBACK_CANCEL;
83 }
84
85
86
87 static int _fail_to_launch_pwlock(const char *appid, const char *key, const char *value, void *cfn, void *afn)
88 {
89         _finish_boot_animation(NULL);
90         return 0;
91 }
92
93
94
95 static void _after_launch_pwlock(int pid)
96 {
97         process_mgr_set_pwlock_priority(pid);
98         ecore_timer_add(0.5, _finish_boot_animation, NULL);
99 }
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                 lock_mgr_daemon_start();
134                 _show_home();
135         }
136
137         return 1;
138 }
139
140
141
142 static void _language_changed_cb(keynode_t *node, void *data)
143 {
144         char *lang = NULL;
145
146         ret_if(!node);
147
148         lang = vconf_keynode_get_str(node);
149         ret_if(!lang);
150
151         _D("language is changed : %s", lang);
152
153         elm_language_set(lang);
154
155         lock_pwd_util_view_init();
156         lock_pwd_control_panel_emg_btn_text_update();
157 }
158
159
160
161 static int _set_i18n(const char *domain, const char *dir)
162 {
163         char *r = NULL;
164
165         if (domain == NULL) {
166                 errno = EINVAL;
167                 return -1;
168         }
169
170         char *lang = vconf_get_str(VCONFKEY_LANGSET);
171         r = setlocale(LC_ALL, lang);
172         if (!r) {
173                 _E("setlocale() error");
174         }
175         if (lang) {
176                 free(lang);
177         }
178
179         r = bindtextdomain(domain, dir);
180         if (!r) {
181                 _E("bindtextdomain() error");
182         }
183
184         r = textdomain(domain);
185         if (!r) {
186                 _E("textdomain() error");
187         }
188
189         if (vconf_notify_key_changed(VCONFKEY_LANGSET, _language_changed_cb, NULL) < 0) {
190                 _E("Failed to register changed cb : %s", VCONFKEY_LANGSET);
191         }
192
193         return 0;
194 }
195
196
197
198 static int _check_dead_signal(int pid, void *data)
199 {
200         int home_pid = 0;
201         int volume_pid = 0;
202         int lock_pid = 0;
203
204         _D("Process %d is termianted", pid);
205
206         if (pid < 0) {
207                 _E("pid : %d", pid);
208                 return 0;
209         }
210
211         home_pid = home_mgr_get_home_pid();
212         volume_pid = home_mgr_get_volume_pid();
213         lock_pid = lock_mgr_get_lock_pid();
214
215         if (pid == home_pid) {
216                 _D("Homescreen is dead");
217                 home_mgr_relaunch_homescreen();
218         } else if (pid == volume_pid) {
219                 _D("volume is dead");
220                 home_mgr_relaunch_volume();
221         } else if (pid == lock_pid) {
222                 _D("lockscreen is dead");
223                 lock_mgr_unlock();
224         } else {
225                 _D("Unknown process, ignore it");
226         }
227
228         return 0;
229 }
230
231
232
233 static void _init(struct appdata *ad)
234 {
235         struct sigaction act;
236
237         memset(&act,0x00,sizeof(struct sigaction));
238         act.sa_sigaction = _signal_handler;
239         act.sa_flags = SA_SIGINFO;
240
241         int ret = sigemptyset(&act.sa_mask);
242         if (ret < 0) {
243                 _E("Failed to sigemptyset[%s]", strerror(errno));
244         }
245         ret = sigaddset(&act.sa_mask, SIGTERM);
246         if (ret < 0) {
247                 _E("Failed to sigaddset[%s]", strerror(errno));
248         }
249         ret = sigaction(SIGTERM, &act, NULL);
250         if (ret < 0) {
251                 _E("Failed to sigaction[%s]", strerror(errno));
252         }
253
254         _set_i18n(PACKAGE, LOCALEDIR);
255
256         status_register();
257         status_active_register_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb, NULL);
258         status_active_register_cb(STATUS_ACTIVE_KEY_BOOT_ANIMATION_FINISHED, _boot_animation_finished_cb, NULL);
259
260         /* Ordering : _hide_home -> process_mgr_must_launch(pwlock) -> _show_home */
261         _hide_home();
262 #if 0
263         process_mgr_must_launch(PWLOCK_LITE_PKG_NAME, NULL, NULL, _fail_to_launch_pwlock, _after_launch_pwlock);
264 #endif
265
266 #ifdef HAVE_X11
267         hw_key_create_window();
268 #endif
269         home_mgr_init(NULL);
270
271         aul_listen_app_dead_signal(_check_dead_signal, NULL);
272 }
273
274
275
276 static void _fini(struct appdata *ad)
277 {
278         home_mgr_fini();
279 #ifdef HAVE_X11
280         hw_key_destroy_window();
281 #endif
282         lock_mgr_daemon_end();
283
284         status_active_unregister_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb);
285         status_active_unregister_cb(STATUS_ACTIVE_KEY_BOOT_ANIMATION_FINISHED, _boot_animation_finished_cb);
286         status_unregister();
287
288         if (vconf_ignore_key_changed(VCONFKEY_LANGSET, _language_changed_cb) < 0) {
289                 _E("Failed to unregister changed cb : %s", VCONFKEY_LANGSET);
290         }
291 }
292
293
294
295 int main(int argc, char *argv[])
296 {
297         struct appdata ad;
298
299         _D("starter is launched..!!");
300
301         elm_init(argc, argv);
302         _init(&ad);
303
304         elm_run();
305
306         _fini(&ad);
307         elm_shutdown();
308
309         return 0;
310 }