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