Merge "systemd: Add new EnvironmentFile for XDG_RUNTIME_DIR variable" into tizen
[apps/native/boot-animation.git] / src / animation.c
1 /*
2  *  boot-animation
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32 #include <dirent.h>
33
34 #include <vconf.h>
35
36 #include <Elementary.h>
37
38 #include <boot.h>
39 #include <animation.h>
40
41 #include "log.h"
42
43 #define OVER_COUNT 19
44
45 static struct animation {
46         Evas_Object *win;
47         Evas_Coord w;
48         Evas_Coord h;
49         int t;
50         Evas *evas;
51         Ecore_Evas *ee;
52         Evas_Object *layout;
53         int state;
54         Evas_Object *txt;
55 } s_animation = {
56         .txt = NULL,};
57
58 static void win_del(void *data, Evas_Object * obj, void *event_info)
59 {
60         _D("Window delete event received");
61         elm_exit();
62 }
63
64 static Eina_Bool _end_cb(void *data)
65 {
66         _D("_end_cb is invoked");
67         printf("_end_cb is invoked\n");
68         if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0)
69                 _E("Failed to set finished set");
70         elm_exit();
71         return ECORE_CALLBACK_CANCEL;
72 }
73
74 static void _edje_cb(void *d, Evas_Object * obj, const char *e, const char *s)
75 {
76         _D("edje callback is invoked");
77
78         if (s_animation.state == TYPE_OFF || s_animation.state == TYPE_OFF_WITH_MSG) {
79                 _D("TYPE OFF");
80                 if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0)
81                         _E("Failed to set finished set");
82                 if (s_animation.txt) {
83                         Evas_Coord w;
84                         Evas_Coord h;
85
86                         evas_object_size_hint_weight_set(s_animation.txt,
87                                                          EVAS_HINT_EXPAND,
88                                                          EVAS_HINT_EXPAND);
89                         evas_object_size_hint_fill_set(s_animation.txt,
90                                                        EVAS_HINT_FILL,
91                                                        EVAS_HINT_FILL);
92                         evas_object_resize(s_animation.txt, s_animation.w,
93                                            s_animation.h);
94                         evas_object_color_set(s_animation.txt, 255, 255, 255,
95                                               255);
96                         evas_object_text_font_set(s_animation.txt,
97                                                   "SLP:style=medium", 30);
98                         evas_object_geometry_get(s_animation.txt, NULL, NULL,
99                                                  &w, &h);
100                         evas_object_move(s_animation.txt,
101                                          (s_animation.w - w) >> 1,
102                                          (s_animation.h - h) >> 1);
103                         evas_object_show(s_animation.txt);
104                 }
105                 ecore_timer_add(1, _end_cb, NULL);
106         } else
107                 _D("TYPE_ON");
108                 _end_cb(NULL);
109 }
110
111 #define DEFAULT_W 480
112 static void layout_file_set(int state)
113 {
114         _D("Layout file set according to resolution");
115         _D("Screen Width: %d, DEFAULT_WIDTH: %d", s_animation.w, DEFAULT_W);
116
117         if (s_animation.w < DEFAULT_W) {
118                 if (state == TYPE_ON) {
119                         _D("Set file: %s", WEARABLE_EDJ_ON);
120                         elm_layout_file_set(s_animation.layout, WEARABLE_EDJ_ON, GRP_ON);
121                 } else {
122                         _D("Set file: %s", WEARABLE_EDJ_OFF);
123                         elm_layout_file_set(s_animation.layout, WEARABLE_EDJ_OFF, GRP_OFF);
124                 }
125         } else if (s_animation.w > DEFAULT_W) {
126                 if (state == TYPE_ON) {
127                         _D("Set file: %s", MOBILE_EDJ_ON);
128                         elm_layout_file_set(s_animation.layout, MOBILE_EDJ_ON, GRP_ON);
129                 } else {
130                         _D("Set file: %s", MOBILE_EDJ_OFF);
131                         elm_layout_file_set(s_animation.layout, MOBILE_EDJ_OFF, GRP_OFF);
132                 }
133         } else {
134                 if (state == TYPE_ON) {
135                         _D("Set file: %s", DEFAULT_EDJ_ON);
136                         elm_layout_file_set(s_animation.layout, DEFAULT_EDJ_ON, GRP_ON);
137                 } else {
138                         _D("Set file: %s", DEFAULT_EDJ_OFF);
139                         elm_layout_file_set(s_animation.layout, DEFAULT_EDJ_OFF, GRP_OFF);
140                 }
141         }
142 }
143
144 static int init_layout(const char *msg)
145 {
146         s_animation.layout = elm_layout_add(s_animation.win);
147         if (!s_animation.layout) {
148                 _E("Failed to create layout");
149                 return EXIT_FAILURE;
150         }
151
152         layout_file_set(s_animation.state);
153
154         evas_object_size_hint_weight_set(s_animation.layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
155         _D("[Boot-ani] Start animation: %d mode", s_animation.state);
156         elm_win_resize_object_add(s_animation.win, s_animation.layout);
157         edje_object_signal_callback_add(elm_layout_edje_get(s_animation.layout), "end", "animation", _edje_cb, NULL);
158         evas_object_show(s_animation.layout);
159
160         if (msg) {
161                 if (!s_animation.txt) {
162                         s_animation.txt = evas_object_text_add(s_animation.evas);
163                         if (!s_animation.txt) {
164                                 _E("Failed to add text");
165                                 evas_object_del(s_animation.layout);
166                                 return EXIT_FAILURE;
167                         }
168                 }
169
170                 evas_object_text_text_set(s_animation.txt, msg);
171                 evas_object_hide(s_animation.txt);
172         }
173
174         return EXIT_SUCCESS;
175 }
176
177 static void fini_layout(void)
178 {
179         if (s_animation.layout) {
180                 evas_object_del(s_animation.layout);
181         }
182
183         if (s_animation.txt) {
184                 evas_object_del(s_animation.txt);
185                 s_animation.txt = NULL;
186         }
187 }
188
189 static int create_window(void)
190 {
191         _D("Create Window");
192         printf("Create Window\n");
193
194         int x, y = 0;
195
196         s_animation.win = elm_win_add(NULL, "BOOT_ANIMATION", ELM_WIN_NOTIFICATION);
197         elm_win_role_set(s_animation.win, "alert");
198         if (!s_animation.win) {
199                 _E("Failed to create a new window");
200                 printf("Failed to create a new window\n");
201                 return EXIT_FAILURE;
202         }
203         if (s_animation.state == TYPE_OFF || s_animation.state == TYPE_OFF_WITH_MSG) {
204                 _D("We are turning off the Tizen");
205                 elm_win_alpha_set(s_animation.win, EINA_TRUE);
206         }
207         evas_object_smart_callback_add(s_animation.win, "delete-request", win_del, NULL);
208
209         s_animation.evas = evas_object_evas_get(s_animation.win);
210         if (!s_animation.evas) {
211                 evas_object_del(s_animation.win);
212                 _E("Failed to get the evas object");
213                 return EXIT_FAILURE;
214         }
215         elm_win_screen_size_get(s_animation.win, &x, &y, &s_animation.w, &s_animation.h);
216         _D("Window size is x: %d, y: %d, w: %d, h: %d", x, y, s_animation.w, s_animation.h);
217         elm_win_borderless_set(s_animation.win, 1);
218         elm_win_indicator_mode_set(s_animation.win, ELM_WIN_INDICATOR_HIDE);
219         evas_object_move(s_animation.win, 0, 0);
220
221         if (s_animation.w > s_animation.h) {
222                 int t;
223                 elm_win_rotation_with_resize_set(s_animation.win, 90);
224                 /* Update geometry infomration */
225                 t = s_animation.w;
226                 s_animation.w = s_animation.h;
227                 s_animation.h = t;
228         }
229
230         evas_object_show(s_animation.win);
231
232         s_animation.ee = ecore_evas_ecore_evas_get(s_animation.evas);
233         if (!s_animation.ee) {
234                 evas_object_del(s_animation.win);
235                 _E("Failed to get the ecore evas object");
236                 return EXIT_FAILURE;
237         }
238
239         return EXIT_SUCCESS;
240 }
241
242 int init_animation(int state, const char *msg)
243 {
244         _D("Init animation");
245         printf("Init animation\n");
246
247         Ecore_Timer *timer = NULL;
248
249         s_animation.state = state;
250
251         if (create_window() == EXIT_FAILURE) {
252                 _E("Failed to create a new window");
253                 printf("Failed to create a new window\n");
254                 return EXIT_FAILURE;
255         }
256
257         if (init_layout(msg) == EXIT_FAILURE) {
258                 _E("Failed to init the layout object");
259                 if (msg) {
260                         evas_object_del(s_animation.txt);
261                 }
262                 evas_object_del(s_animation.win);
263         }
264
265         return EXIT_SUCCESS;
266 }
267
268 int fini_animation(void)
269 {
270         fini_layout();
271         evas_object_del(s_animation.win);
272         fflush(stdout);
273         close(1);
274         return EXIT_SUCCESS;
275 }