Release free memory from the top of the heap(using malloc_trim())
[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         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                 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 indicator_pid = 0;
203         int quickpanel_pid = 0;
204         int lock_pid = 0;
205
206         _D("Process %d is termianted", pid);
207
208         if (pid < 0) {
209                 _E("pid : %d", pid);
210                 return 0;
211         }
212
213         home_pid = home_mgr_get_home_pid();
214         volume_pid = home_mgr_get_volume_pid();
215         indicator_pid = home_mgr_get_indicator_pid();
216         quickpanel_pid = home_mgr_get_quickpanel_pid();
217         lock_pid = lock_mgr_get_lock_pid();
218
219         if (pid == home_pid) {
220                 _D("Homescreen is dead");
221                 home_mgr_relaunch_homescreen();
222         } else if (pid == volume_pid) {
223                 _D("volume is dead");
224                 home_mgr_relaunch_volume();
225         } else if (pid == indicator_pid) {
226                 _D("indicator is dead");
227                 home_mgr_relaunch_indicator();
228         } else if (pid == quickpanel_pid) {
229                 _D("quickpanel is dead");
230                 home_mgr_relaunch_quickpanel();
231         } else if (pid == lock_pid) {
232                 _D("lockscreen is dead");
233                 lock_mgr_unlock();
234         } else {
235                 _D("Unknown process, ignore it");
236         }
237
238         return 0;
239 }
240
241
242
243 static void _init(struct appdata *ad)
244 {
245         struct sigaction act;
246         char err_buf[128] = {0,};
247
248         memset(&act,0x00,sizeof(struct sigaction));
249         act.sa_sigaction = _signal_handler;
250         act.sa_flags = SA_SIGINFO;
251
252         int ret = sigemptyset(&act.sa_mask);
253         if (ret < 0) {
254                 if (strerror_r(errno, err_buf, sizeof(err_buf)) == 0) {
255                         _E("Failed to sigemptyset[%d / %s]", errno, err_buf);
256                 }
257         }
258         ret = sigaddset(&act.sa_mask, SIGTERM);
259         if (ret < 0) {
260                 if (strerror_r(errno, err_buf, sizeof(err_buf)) == 0) {
261                         _E("Failed to sigaddset[%d / %s]", errno, err_buf);
262                 }
263         }
264         ret = sigaction(SIGTERM, &act, NULL);
265         if (ret < 0) {
266                 if (strerror_r(errno, err_buf, sizeof(err_buf)) == 0) {
267                         _E("Failed to sigaction[%d / %s]", errno, err_buf);
268                 }
269         }
270
271         _set_i18n(PACKAGE, LOCALEDIR);
272
273         status_register();
274         status_active_register_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb, NULL);
275         status_active_register_cb(STATUS_ACTIVE_KEY_BOOT_ANIMATION_FINISHED, _boot_animation_finished_cb, NULL);
276
277         /* Ordering : _hide_home -> process_mgr_must_launch(pwlock) -> _show_home */
278         _hide_home();
279 #if 0
280         process_mgr_must_launch(PWLOCK_LITE_PKG_NAME, NULL, NULL, _fail_to_launch_pwlock, _after_launch_pwlock);
281 #endif
282
283         hw_key_create_window();
284         home_mgr_init(NULL);
285
286         aul_listen_app_dead_signal(_check_dead_signal, NULL);
287 }
288
289
290
291 static void _fini(struct appdata *ad)
292 {
293         home_mgr_fini();
294         hw_key_destroy_window();
295         lock_mgr_daemon_end();
296
297         status_active_unregister_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb);
298         status_active_unregister_cb(STATUS_ACTIVE_KEY_BOOT_ANIMATION_FINISHED, _boot_animation_finished_cb);
299         status_unregister();
300
301         if (vconf_ignore_key_changed(VCONFKEY_LANGSET, _language_changed_cb) < 0) {
302                 _E("Failed to unregister changed cb : %s", VCONFKEY_LANGSET);
303         }
304 }
305
306
307
308 int main(int argc, char *argv[])
309 {
310         struct appdata ad;
311
312         _D("starter is launched..!!");
313
314         elm_init(argc, argv);
315         _init(&ad);
316
317         malloc_trim(0);
318         elm_run();
319
320         _fini(&ad);
321         elm_shutdown();
322
323         return 0;
324 }