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