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