tizen 2.3.1 release
[apps/home/starter.git] / src / 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 <poll.h>
26
27 #include <aul.h>
28 #include <vconf.h>
29 #include <signal.h>
30
31 #include "starter.h"
32 #include "starter-util.h"
33 #include "x11.h"
34 #include "lock-daemon.h"
35 #include "lockd-debug.h"
36 #include "menu_daemon.h"
37
38 #ifndef PACKAGE_NAME
39 #define PACKAGE_NAME "com.samsung.starter"
40 #endif
41
42 #define DEFAULT_THEME "tizen"
43 #define PWLOCK_PATH "/usr/apps/com.samsung.pwlock/bin/pwlock"
44 #define PWLOCK_PKG_NAME "com.samsung.pwlock"
45 #define PWLOCK_LITE_PKG_NAME "com.samsung.pwlock-lite"
46
47 #define DATA_UNENCRYPTED "unencrypted"
48 #define DATA_MOUNTED "mounted"
49 #define SD_DATA_ENCRYPTED "encrypted"
50 #define SD_CRYPT_META_FILE ".MetaEcfsFile"
51 #define MMC_MOUNT_POINT "/opt/storage/sdcard"
52
53 #ifdef FEATURE_LITE
54 #define _FIRST_HOME 1
55 #else
56 #define _FIRST_HOME 1
57 #endif
58
59 static int _check_encrypt_sdcard()
60 {
61         const char *crypt_file = SD_CRYPT_META_FILE;
62         char *meta_data_file = NULL;
63         int ret = -1;
64         int length = 0;
65
66         length = strlen(MMC_MOUNT_POINT) + strlen(crypt_file) + 2;
67
68         meta_data_file = malloc(length);
69         if (!meta_data_file) {
70                 _DBG("malloc error");
71                 return -1;
72         }
73
74         snprintf(meta_data_file, length, "%s/%s", MMC_MOUNT_POINT, crypt_file);
75         if (access(meta_data_file, F_OK) == 0) {
76                 ret = 0;
77         }
78         free(meta_data_file);
79
80         _DBG("Check sd card ecryption : %d", ret);
81
82         return ret;
83 }
84
85 static void lock_menu_screen(void)
86 {
87         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 0);
88 }
89
90 static void unlock_menu_screen(void)
91 {
92         int r;
93         int show_menu;
94
95         show_menu = 0;
96         r = vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &show_menu);
97         if (r || !show_menu) {
98                 vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
99         }
100 }
101
102 static void _set_elm_theme(void)
103 {
104         char *vstr;
105         char *theme;
106         Elm_Theme *th = NULL;
107         vstr = vconf_get_str(VCONFKEY_SETAPPL_WIDGET_THEME_STR);
108         if (vstr == NULL)
109                 theme = DEFAULT_THEME;
110         else
111                 theme = vstr;
112
113         th = elm_theme_new();
114         _DBG("theme vconf[%s]\n set[%s]\n", vstr, theme);
115         elm_theme_set(th, theme);
116
117         if (vstr)
118                 free(vstr);
119 }
120
121 #if 0
122 static void _set_elm_entry(void)
123 {
124         int v;
125         int r;
126
127         r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOCAPITAL_ALLOW_BOOL, &v);
128         if (!r) {
129                 prop_int_set("ENLIGHTENMENT_AUTOCAPITAL_ALLOW", v);
130                 _DBG("vconf autocatipal[%d]", v);
131         }
132
133         r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOPERIOD_ALLOW_BOOL, &v);
134         if (!r) {
135                 prop_int_set("ENLIGHTENMENT_AUTOPERIOD_ALLOW", v);
136                 _DBG("vconf autoperiod[%d]", v);
137         }
138 }
139 #endif
140
141 static int _launch_pwlock(void)
142 {
143         int r;
144         //int i = 0;
145
146         _DBG("%s", __func__);
147
148 #ifdef FEATURE_LITE
149         r = aul_launch_app(PWLOCK_LITE_PKG_NAME, NULL);
150         if (r < 0) {
151                 _ERR("PWLock launch error: error(%d)", r);
152                 if (r == AUL_R_ETIMEOUT) {
153                         _DBG("Launch pwlock is failed for AUL_R_ETIMEOUT, again launch pwlock-lite");
154                         r = aul_launch_app(PWLOCK_LITE_PKG_NAME, NULL);
155                         if (r < 0) {
156                                 _ERR("2'nd PWLock launch error: error(%d)", r);
157                                 return -1;
158                         } else {
159                                 _DBG("Launch pwlock-lite");
160                                 return r;
161                         }
162                 } else {
163                         return -1;
164                 }
165         } else {
166                 _DBG("Launch pwlock-lite");
167                 return r;
168         }
169 #else
170         r = aul_launch_app(PWLOCK_LITE_PKG_NAME, NULL);
171         if (r < 0) {
172                 _ERR("PWLock launch error: error(%d)", r);
173                 if (r == AUL_R_ETIMEOUT) {
174                         _DBG("Launch pwlock is failed for AUL_R_ETIMEOUT, again launch pwlock");
175                         r = aul_launch_app(PWLOCK_LITE_PKG_NAME, NULL);
176                         if (r < 0) {
177                                 _ERR("2'nd PWLock launch error: error(%d)", r);
178                                 return -1;
179                         } else {
180                                 _DBG("Launch pwlock");
181                                 return r;
182                         }
183                 } else {
184                         return -1;
185                 }
186         } else {
187                 _DBG("Launch pwlock");
188                 return r;
189         }
190 #endif
191 #if 0
192  retry_con:
193         r = aul_launch_app("com.samsung.pwlock", NULL);
194         if (r < 0) {
195                 _ERR("PWLock launch error: error(%d)", r);
196                 if (r == AUL_R_ETIMEOUT) {
197                         i++;
198                         _DBG("Launching pwlock is failed [%d]times for AUL_R_ETIMEOUT ", i);
199                         goto retry_con;
200                 } else {
201                         return -1;
202                 }
203         } else {
204                 _DBG("Launch pwlock");
205                 return 0;
206         }
207 #endif
208 }
209
210 static void _signal_handler(int signum, siginfo_t *info, void *unused)
211 {
212     _DBG("_signal_handler : Terminated...");
213     elm_exit();
214 }
215
216 #if 0
217 static void _heynoti_event_power_off(void *data)
218 {
219     _DBG("_heynoti_event_power_off : Terminated...");
220     elm_exit();
221 }
222 #endif
223
224 static void _poweroff_control_cb(keynode_t *in_key, void *data)
225 {
226         int val;
227         if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) == 0 &&
228                 (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART)) {
229             _DBG("_poweroff_control_cb : Terminated...");
230             elm_exit();
231         }
232 }
233
234 #define APP_ID_SPLIT_LAUNCHER "com.samsung.split-launcher"
235 static Eina_Bool _fini_boot(void *data)
236 {
237         _DBG("%s %d\n", __func__, __LINE__);
238
239         int multiwindow_enabled = 0;
240         int val = 0;
241
242         if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0) {
243                 _ERR("Failed to set boot animation finished set");
244         }
245
246         if (vconf_get_bool(VCONFKEY_QUICKSETTING_MULTIWINDOW_ENABLED, &multiwindow_enabled) < 0) {
247                 _ERR("Cannot get VCONFKEY");
248                 multiwindow_enabled = 0;
249         }
250
251         if (vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &val) < 0) {
252                 _DBG("Cannot get VCONFKEY");
253                 val = 0;
254         }
255
256         if ((val == 1) || (multiwindow_enabled == 0)) {
257                 _DBG("TTS : %d, Multiwindow enabled : %d", val, multiwindow_enabled);
258                 return ECORE_CALLBACK_CANCEL;
259         }
260
261         _DBG("Launch the split-launcher");
262
263         int ret = aul_launch_app(APP_ID_SPLIT_LAUNCHER, NULL);
264         if (0 > ret) _ERR("cannot launch the split-launcher (%d)", ret);
265
266         return ECORE_CALLBACK_CANCEL;
267 }
268
269
270 static Eina_Bool _init_idle(void *data)
271 {
272         _DBG("%s %d\n", __func__, __LINE__);
273         int pwlock_pid = 0;
274         if ((pwlock_pid = _launch_pwlock()) < 0) {
275                 _ERR("launch pwlock error");
276         }
277         else{
278                 lockd_process_mgr_set_pwlock_priority(pwlock_pid);
279         }
280         _fini_boot(NULL);
281         return ECORE_CALLBACK_CANCEL;
282 }
283
284 static void _lock_state_cb(keynode_t * node, void *data)
285 {
286         _DBG("%s %d\n", __func__, __LINE__);
287 #if 0
288         if (_launch_pwlock() < 0) {
289                 _ERR("launch pwlock error");
290         }
291         menu_daemon_init(NULL);
292 #else
293         _fini_boot(NULL);
294 #endif
295         if (vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE,
296              _lock_state_cb) != 0) {
297                 LOCKD_DBG("Fail to unregister");
298         }
299 }
300
301 static Eina_Bool _init_lock_lite(void *data)
302 {
303         char *file = NULL;
304         int pwlock_pid = 0;
305
306         _DBG("%s %d\n", __func__, __LINE__);
307
308         /* Check SD card encription */
309         file = vconf_get_str(VCONFKEY_SDE_CRYPTO_STATE);
310         if (file && !strncmp(SD_DATA_ENCRYPTED, file, strlen(file)) && _check_encrypt_sdcard() == 0) {
311                 _ERR("SD card is encripted");
312                 if (start_lock_daemon_lite(FALSE, FALSE) == 1) {
313                         if ((pwlock_pid = _launch_pwlock()) < 0) {
314                                 _ERR("launch pwlock error");
315                         }
316                         else{
317                                 lockd_process_mgr_set_pwlock_priority(pwlock_pid);
318                         }
319                         ecore_timer_add(0.5, _fini_boot, NULL);
320                 } else {
321                         _init_idle(NULL);
322                 }
323         } else {
324                 if (start_lock_daemon_lite(TRUE, FALSE) == 1) {
325                         if ((pwlock_pid = _launch_pwlock()) < 0) {
326                                 _ERR("launch pwlock error");
327                         }
328                         else{
329                                 lockd_process_mgr_set_pwlock_priority(pwlock_pid);
330                         }
331                         if (vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
332                                                 _lock_state_cb, NULL) != 0) {
333                                 _ERR("[Error] vconf notify : lock state");
334                                 ecore_timer_add(0.5, _fini_boot, NULL);
335                         }
336                 } else {
337                         _init_idle(NULL);
338                 }
339         }
340         free(file);
341         return ECORE_CALLBACK_CANCEL;
342 }
343
344 static Eina_Bool _init_lock(void *data)
345 {
346         char *file = NULL;
347
348         _DBG("%s %d\n", __func__, __LINE__);
349
350         /* Check SD card encription */
351         file = vconf_get_str(VCONFKEY_SDE_CRYPTO_STATE);
352         if (file && !strncmp(SD_DATA_ENCRYPTED, file, strlen(file)) && _check_encrypt_sdcard() == 0) {
353                 _ERR("SD card is encripted");
354                 if (start_lock_daemon(FALSE, FALSE) == 1) {
355                         if (_launch_pwlock() < 0) {
356                                 _ERR("launch pwlock error");
357                         }
358                         ecore_timer_add(0.5, _fini_boot, NULL);
359                 } else {
360                         _init_idle(NULL);
361                 }
362         } else {
363                 if (start_lock_daemon(TRUE, FALSE) == 1) {
364                         if (_launch_pwlock() < 0) {
365                                 _ERR("launch pwlock error");
366                         }
367                         if (vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
368                                                 _lock_state_cb, NULL) != 0) {
369                                 _ERR("[Error] vconf notify : lock state");
370                                 ecore_timer_add(0.5, _fini_boot, NULL);
371                         }
372                 } else {
373                         _init_idle(NULL);
374                 }
375         }
376         free(file);
377         return ECORE_CALLBACK_CANCEL;
378 }
379
380 static void _data_encryption_cb(keynode_t * node, void *data)
381 {
382         char *file = NULL;
383
384         _DBG("%s %d\n", __func__, __LINE__);
385
386         file = vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
387         if (file != NULL) {
388                 _DBG("get VCONFKEY : %s\n",  file);
389                 if (!strcmp(file, DATA_MOUNTED)) {
390 #ifdef FEATURE_LITE
391                         start_lock_daemon_lite(FALSE, FALSE);
392 #else
393                         start_lock_daemon(FALSE, FALSE);
394 #endif
395                         menu_daemon_init(NULL);
396                 }
397                 free(file);
398         }
399 }
400
401 static Eina_Bool _start_sequence_cb(void *data)
402 {
403         _DBG("%s, %d", __func__, __LINE__);
404
405         unlock_menu_screen();
406
407         return ECORE_CALLBACK_CANCEL;
408 }
409
410 static void _init(struct appdata *ad)
411 {
412         int r;
413         struct sigaction act;
414         char *file = NULL;
415         int first = -1;
416         int pwlock_pid = 0;
417
418         memset(&act,0x00,sizeof(struct sigaction));
419         act.sa_sigaction = _signal_handler;
420         act.sa_flags = SA_SIGINFO;
421
422         int ret = sigemptyset(&act.sa_mask);
423         if (ret < 0) {
424                 _ERR("Failed to sigemptyset[%s]", strerror(errno));
425         }
426         ret = sigaddset(&act.sa_mask, SIGTERM);
427         if (ret < 0) {
428                 _ERR("Failed to sigaddset[%s]", strerror(errno));
429         }
430         ret = sigaction(SIGTERM, &act, NULL);
431         if (ret < 0) {
432                 _ERR("Failed to sigaction[%s]", strerror(errno));
433         }
434
435         memset(ad, 0, sizeof(struct appdata));
436
437         gettimeofday(&ad->tv_start, NULL);
438
439 /*      if (bincfg_is_factory_binary() == 1) {
440                 _DBG("Factory binary..!!");
441                 _set_elm_theme();
442                 unlock_menu_screen();
443                 menu_daemon_init(NULL);
444         } else {
445 */              _DBG("%s %d\n", __func__, __LINE__);
446                 lock_menu_screen();
447                 _set_elm_theme();
448
449                 /* Check data encrption */
450                 file = vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
451                 if (file != NULL) {
452                         _DBG("get VCONFKEY : %s\n",  file);
453                         if (strncmp(DATA_UNENCRYPTED, file, strlen(file))) {
454                                 if (vconf_notify_key_changed(VCONFKEY_ODE_CRYPTO_STATE,
455                                                         _data_encryption_cb, NULL) != 0) {
456                                         _ERR("[Error] vconf notify changed is failed: %s", VCONFKEY_ODE_CRYPTO_STATE);
457                                 } else {
458                                         _DBG("waiting mount..!!");
459                                         if ((pwlock_pid = _launch_pwlock()) < 0) {
460                                                 _ERR("launch pwlock error");
461                                         }
462                                         else{
463                                                 lockd_process_mgr_set_pwlock_priority(pwlock_pid);
464                                         }
465                                         free(file);
466                                         return;
467                                 }
468                         }
469                         free(file);
470                 }
471
472 #if 0
473                 r = start_lock_daemon(TRUE);
474                 if (r == 1) {
475                         if (vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
476                                                 _lock_state_cb, NULL) != 0) {
477                                 _ERR("[Error] vconf notify : lock state");
478                                 ecore_timer_add(1.5, _init_idle, NULL);
479                         }
480                 } else {
481                         if (_launch_pwlock() < 0) {
482                                 _ERR("launch pwlock error");
483                         }
484                         menu_daemon_init(NULL);
485                 }
486 #else
487
488                 r = vconf_get_bool(VCONFKEY_PWLOCK_FIRST_BOOT, &first);
489                 _ERR("vconf get First boot result:%d, get_value:%d", r, first);
490                 if (r == 0 && first == 0){
491                         _DBG("Not first booting time");
492
493 #if (!_FIRST_HOME)
494                         _DBG("Not First home");
495                         unlock_menu_screen();
496                         menu_daemon_init(NULL);
497 #ifdef FEATURE_LITE
498                         _init_lock_lite(NULL);
499 #else
500                         _init_lock(NULL);
501 #endif
502 #else //_FIRST_HOME
503                         _DBG("First home");
504 #ifdef FEATURE_LITE
505                         _init_lock_lite(NULL);
506 #else
507                         _init_lock(NULL);
508 #endif
509                         menu_daemon_init(NULL);
510                         ecore_idler_add(_start_sequence_cb, NULL);
511 #endif
512                 } else {
513                         _ERR("First booting time");
514                         menu_daemon_init(NULL);
515 #ifdef FEATURE_LITE
516                         r = start_lock_daemon_lite(TRUE, TRUE);
517 #else
518                         r = start_lock_daemon(TRUE, TRUE);
519 #endif
520                         _DBG("start_lock_daemon ret:%d", r);
521                         if ((pwlock_pid = _launch_pwlock()) < 0) {
522                                 _ERR("launch pwlock error");
523                                 if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0) {
524                                         _ERR("Failed to set boot animation finished set");
525                                 }
526                                 unlock_menu_screen();
527                         } else {
528                                 lockd_process_mgr_set_pwlock_priority(pwlock_pid);
529                                 ecore_timer_add(1, _fini_boot, NULL);
530                         }
531                 }
532 #endif
533 //      }
534 }
535
536 static void _fini(struct appdata *ad)
537 {
538         struct timeval tv, res;
539
540         if (ad == NULL) {
541                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
542                 return;
543         }
544
545         unlock_menu_screen();
546         menu_daemon_fini();
547
548         gettimeofday(&tv, NULL);
549         timersub(&tv, &ad->tv_start, &res);
550         _DBG("Total time: %d.%06d sec\n", (int)res.tv_sec, (int)res.tv_usec);
551 }
552
553 int main(int argc, char *argv[])
554 {
555         struct appdata ad;
556
557         _DBG("starter is launched..!!");
558 #if 0
559         set_window_scale();     /* not in loop */
560 #endif
561
562 #if 0
563     int heyfd = heynoti_init();
564         if (heyfd < 0) {
565                 _ERR("Failed to heynoti_init[%d]", heyfd);
566                 return -1;
567         }
568
569         int ret = heynoti_subscribe(heyfd, "power_off_start", _heynoti_event_power_off, NULL);
570         if (ret < 0) {
571                 _ERR("Failed to heynoti_subscribe[%d]", ret);
572         }
573         ret = heynoti_attach_handler(heyfd);
574         if (ret < 0) {
575                 _ERR("Failed to heynoti_attach_handler[%d]", ret);
576         }
577 #else
578         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, (void *)_poweroff_control_cb, NULL) < 0) {
579                 _ERR("Vconf notify key chaneged failed: VCONFKEY_SYSMAN_POWER_OFF_STATUS");
580         }
581 #endif
582
583         elm_init(argc, argv);
584
585         _init(&ad);
586
587         elm_run();
588
589         _fini(&ad);
590
591         elm_shutdown();
592
593         return 0;
594 }