Merge "Add test unit" into tizen
[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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <linux/fb.h>
29 #include <stddef.h>
30 #include <unistd.h>
31
32 #include <pthread.h>
33
34 #include <Elementary.h>
35
36 #include <getopt.h>
37 #include <system_settings.h>
38
39 #include <vconf.h>
40
41 #include "animation.h"
42 #include "boot.h"
43 #include "log.h"
44 #include "test.h"
45
46 #define XRGB8888 4
47
48 extern char *optarg;
49 extern int optind, opterr, optopt;
50
51 struct args {
52         int argc;
53         char **argv;
54         char *msg;
55 };
56
57 //#define SOUND_PROFILE
58 #ifdef SOUND_PROFILE
59 #include <mm_sound_private.h>
60 #endif
61
62 static void print_usages(char *argv0)
63 {
64         printf("Usage) %s {--start|--stop}\n"
65                "  Ex:"
66                "    # %s --start\n"
67                "    # %s --stop\n"
68                "    # %s --off\n"
69                "    # %s --offmsg YOUR_MESSAGE\n", argv0, argv0, argv0, argv0,
70                argv0);
71 }
72
73
74 #ifdef SOUND_PROFILE
75 /* Sound is not supported Since Tizen 4.0 */
76 static int get_wav_file(int state, char *wavpath)
77 {
78         _D("Get wav file");
79         if (state == TYPE_ON) {
80                 snprintf(wavpath, FILE_PATH_MAX-1, "%s", DEFAULT_POWERON_WAV);
81                 _D("Wav file: %s", wavpath);
82         } else
83                 return -1;
84
85         return 0;
86 }
87 #endif
88
89 static int xready_cb(keynode_t * node, void *user_data)
90 {
91         int c;
92         int argc;
93         char **argv;
94         int type = TYPE_UNKNOWN;
95         struct args *args = user_data;
96         static struct option long_options[] = {
97                 {"start",       no_argument,            0,      's'     },
98                 {"stop",        no_argument,            0,      'p'     },
99                 {"off",         no_argument,            0,      'o'     },
100                 {"offmsg",      required_argument,      0,      'm'     },
101                 {"clear",       no_argument,            0,      'c'     },
102                 {0,             0,                      0,      0       },
103         };
104         static int invoked_flag = 0;
105
106         _D("xready_cb");
107         printf("xready_cb\n");
108
109         if (invoked_flag == 1) {
110                 _E("Already launched");
111                 printf("Error Already launched\n");
112                 return EXIT_FAILURE;
113         }
114
115         invoked_flag = 1;
116
117         argc = args->argc;
118         argv = args->argv;
119
120         int i;
121         for (i = 0; i < argc; i++) {
122             _D("argc %d [%s]", i, argv[i]);
123             printf("argc %d [%s]\n", i, argv[i]);
124         }
125         while ((c = getopt_long(argc, argv, "spom:c", long_options, NULL)) >= 0) {
126
127                 switch (c) {
128                 case 's':
129                         type = TYPE_ON;
130                         continue;
131                 case 'p':
132                         type = TYPE_OFF;
133                         continue;
134                 case 'o':
135                         type = TYPE_OFF_NOEXIT;
136                         continue;
137                 case 'm':
138                         if (args->msg) continue;
139                         type = TYPE_OFF_WITH_MSG;
140                         args->msg = strdup(optarg);
141                         if (!args->msg)
142                                 perror("strdup");
143                         continue;
144                 case 'c':
145                         continue;
146                 default:
147                         type = TYPE_UNKNOWN;
148                         _D("[Boot-ani] unknown arg [%s]", optarg);
149                         printf("[Boot-anim] unknown arg [%s]\n", optarg);
150                         return EXIT_FAILURE;
151                 }
152         }
153
154 #ifdef SOUND_PROFILE
155         /* Sound is not supported Since Tizen 4.0 */
156         /* check sound profile */
157         char wav_path[256];
158         int soundon = 1;        /* default sound on */
159         if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &soundon) < 0) {
160                 _D("VCONFKEY_SETAPPL_SOUND_STATUS_BOOL ==> FAIL!!");
161                 printf("VCONFKEY_SETAPPL_SOUND_STATUS_BOOL ==> FAIL!!\n");
162         }
163
164         _D("Sound status: %d", soundon);
165         printf("Sound status: %d\n", soundon);
166 #endif
167
168         if (init_animation(type, args->msg) != EXIT_SUCCESS) {
169                 _D("Exit boot-animation");
170                 printf("Exit boot-animation\n");
171                 return EXIT_FAILURE;
172         }
173
174 #ifdef SOUND_PROFILE
175         if (soundon) {
176                 _D("Sound on!!");
177                 printf("Sound on!!\n");
178                 if (!get_wav_file(type, wav_path)) {
179                         _D("File path: %s", wav_path);
180                         printf("File path: %s\n", wav_path);
181                         mm_sound_boot_ready(3);
182                         mm_sound_boot_play_sound(wav_path);
183                 }
184         }
185 #endif
186         _D("EXIT SUCESS");
187
188         return EXIT_SUCCESS;
189 }
190
191 #if 0
192 static void _boot_ani_ui_set_scale(void)
193 {
194         double root_height = 0.0;
195         double root_width = 0.0;
196         char buf[128] = { 0, };
197         Display *disp;
198         int screen_num;
199
200         disp = XOpenDisplay(NULL);
201         if (disp == NULL)
202                 return;
203
204         screen_num = DefaultScreen(disp);
205
206         root_height = DisplayHeight(disp, screen_num);
207         root_width = DisplayWidth(disp, screen_num);
208
209         XCloseDisplay(disp);
210
211         snprintf(buf, sizeof(buf), "%lf", root_height / BA_DEFAULT_WINDOW_H);
212         //snprintf(buf, sizeof(buf), "%lf", root_width / BA_DEFAULT_WINDOW_W);
213         _D("Boot animation scale : [%s]", buf);
214
215         setenv("ELM_SCALE", buf, 1);
216         setenv("SCALE_FACTOR", buf, 1);
217 }
218 #endif
219
220 #if (!TEST_MODE)
221 int main(int argc, char *argv[])
222 {
223         int fd = 0;
224         struct args args;
225         setenv("HOME", "/home/root", 1);
226
227         if (argc < 2) {
228                 print_usages(argv[0]);
229                 return EXIT_FAILURE;
230         }
231
232         args.argc = argc;
233         args.argv = argv;
234         args.msg = NULL;
235
236 #if 0
237         _boot_ani_ui_set_scale();
238 #endif
239
240         close(1);
241         fd = open("/tmp/myfile.txt", O_CREAT|O_RDWR, S_IRWXU|S_IRWXO);
242         _D("result of open: %d", fd);
243         printf("[%s/%s/%d] fd == %d\n", __FILE__, __func__, __LINE__, fd);
244
245         elm_init(argc, argv);
246
247         if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 0) != 0) {
248                 _D("Failed to set finished value to 0\n");
249                 printf("[%s/%s/%d] Failed to set finished value to 0\n", __FILE__, __func__, __LINE__);
250         }
251         if (xready_cb(NULL, &args) != EXIT_SUCCESS) {
252                 vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1);
253                 return 1;
254         }
255
256         elm_run();
257
258         fini_animation();
259
260         if (args.msg)
261                 free(args.msg);
262         return 0;
263 }
264 #endif
265 //ELM_MAIN()