code refactoring
[apps/native/boot-animation.git] / src / boot.c
1 /*
2  * Copyright (c) 2009-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
18 #include <vconf.h>
19 #include <getopt.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22
23 #include "animation.h"
24 #include "boot.h"
25 #include "app_log.h"
26 #include "boot_sound.h"
27
28 #include "test.h"
29
30 extern char *optarg;
31 extern int optind;
32
33 void(*boot_exit)(void);
34
35 static int __check_on_off_type(void *user_data)
36 {
37         int c;
38         int i;
39         int argc;
40         char **argv;
41         int type = TYPE_UNKNOWN;
42         struct args *args = user_data;
43         static struct option long_options[] = {
44                 {"start",       no_argument,            0,      's'     },
45                 {"off",         no_argument,            0,      'o'     },
46                 {"stop",        no_argument,            0,      'p'     },
47                 {"clear",       no_argument,            0,      'c'     },
48                 {0,             0,                      0,      0       },
49         };
50         argc = args->argc;
51         argv = args->argv;
52
53         for (i = 0; i < argc; i++) {
54                 __D("argc %d [%s]", i, argv[i]);
55         }
56
57         while ((c = getopt_long(argc, argv, "sopc", long_options, NULL)) >= 0) {
58
59                 switch (c) {
60                 case 's':
61                         type = TYPE_ON;
62                         break;
63                 case 'p':
64                         type = TYPE_OFF_NOEXIT;
65                         break;
66                 case 'o':
67                         type = TYPE_OFF;
68                         break;
69                 case 'c':
70                         type = TYPE_OFF_NOEXIT;
71                         break;
72                 default:
73                         type = TYPE_UNKNOWN;
74                         __D("[Boot-ani] unknown arg [%s]", optarg);
75                         return EXIT_FAILURE;
76                 }
77         }
78         optind = 0;
79
80         return type;
81 }
82
83 #if (!TEST_MODE)
84 int main(int argc, char *argv[])
85 {
86         int fd = 0;
87         struct args args;
88         setenv("HOME", "/home/root", 1);
89
90         if (argc < 2) {
91                 printf("Usage) %s {--start|--stop}\n", argv[0]);
92                 printf("  Ex:");
93                 printf("    # %s --start\n", argv[0]);
94                 printf("    # %s --stop\n", argv[0]);
95                 printf("    # %s --off\n", argv[0]);
96
97                 return EXIT_FAILURE;
98         }
99
100         args.argc = argc;
101         args.argv = argv;
102
103         static int invoked_flag = 0;
104         if (invoked_flag == 1) {
105                 return -1;
106         }
107         invoked_flag = 1;
108
109         close(1);
110         fd = open("/tmp/myfile.txt", O_CREAT|O_RDWR, S_IRWXU|S_IRWXO);
111         __D("Result of log file open: %d", fd);
112
113         elm_init(argc, argv);
114         boot_exit = elm_exit;
115
116         int boot_type = -1;
117         boot_type = __check_on_off_type(&args);
118         if (boot_type < 0) {
119                 __E("Failed to get on off type or UNKNOWN_TYPE: %d", boot_type);
120                 return 1;
121         }
122
123         if (init_animation(boot_type) != EXIT_SUCCESS) {
124                 __E("Exit boot-animation");
125                 return 1;
126         }
127
128         if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 0) != 0) {
129                 __D("Failed to set finished value to 0\n");
130         }
131
132         play_boot_sound(boot_type);
133
134         elm_run();
135
136         fini_animation();
137
138         return 0;
139 }
140
141 #endif
142
143 #include "test.h"
144 #if TEST_MODE
145 int __t__check_on_off_type(void *user_data)
146 {
147         return __check_on_off_type(user_data);
148 }
149 #endif