Fixed Svace
[apps/native/boot-animation.git] / src / animation.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/types.h>
24 #include <sys/stat.h>
25 #include <sys/wait.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28 #include <dirent.h>
29
30 #include <vconf.h>
31
32 #include <Elementary.h>
33
34 #include <boot.h>
35 #include <animation.h>
36
37 #include "log.h"
38
39 #define OVER_COUNT 19
40
41 static struct animation {
42         Evas_Object *win;
43         Evas_Coord w;
44         Evas_Coord h;
45         int t;
46         Evas *evas;
47         Ecore_Evas *ee;
48         Evas_Object *layout;
49         int state;
50         Evas_Object *txt;
51 } s_animation = {
52         .txt = NULL,};
53
54 static void win_del(void *data, Evas_Object * obj, void *event_info)
55 {
56         _D("Window delete event received");
57         elm_exit();
58 }
59
60 static Eina_Bool _end_cb(void *data)
61 {
62         _D("_end_cb is invoked");
63         printf("_end_cb is invoked\n");
64         if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0)
65                 _E("Failed to set finished set");
66         elm_exit();
67         return ECORE_CALLBACK_CANCEL;
68 }
69
70 static void _edje_cb(void *d, Evas_Object * obj, const char *e, const char *s)
71 {
72         _D("edje callback is invoked");
73
74         if (s_animation.state == TYPE_OFF || s_animation.state == TYPE_OFF_WITH_MSG) {
75                 _D("TYPE OFF");
76                 if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0)
77                         _E("Failed to set finished set");
78                 if (s_animation.txt) {
79                         Evas_Coord w;
80                         Evas_Coord h;
81
82                         evas_object_size_hint_weight_set(s_animation.txt,
83                                                          EVAS_HINT_EXPAND,
84                                                          EVAS_HINT_EXPAND);
85                         evas_object_size_hint_fill_set(s_animation.txt,
86                                                        EVAS_HINT_FILL,
87                                                        EVAS_HINT_FILL);
88                         evas_object_resize(s_animation.txt, s_animation.w,
89                                            s_animation.h);
90                         evas_object_color_set(s_animation.txt, 255, 255, 255,
91                                               255);
92                         evas_object_text_font_set(s_animation.txt,
93                                                   "SLP:style=medium", 30);
94                         evas_object_geometry_get(s_animation.txt, NULL, NULL,
95                                                  &w, &h);
96                         evas_object_move(s_animation.txt,
97                                          (s_animation.w - w) >> 1,
98                                          (s_animation.h - h) >> 1);
99                         evas_object_show(s_animation.txt);
100                 }
101                 ecore_timer_add(1, _end_cb, NULL);
102         } else {
103                 _D("TYPE_ON");
104                 _end_cb(NULL);
105         }
106 }
107
108 #define DEFAULT_W 480
109 static void layout_file_set(int state)
110 {
111         _D("Layout file set according to resolution");
112         _D("Screen Width: %d, DEFAULT_WIDTH: %d", s_animation.w, DEFAULT_W);
113
114         switch(s_animation.w)
115         {
116                 case 360:
117                         _D("Resolution is %dx%d", s_animation.w, s_animation.h);
118                         if (state == TYPE_ON) {
119                                 elm_layout_file_set(s_animation.layout, WEARABLE_POWER_ON, GRP_ON);
120                         } else {
121                                 elm_layout_file_set(s_animation.layout, WEARABLE_POWER_OFF, GRP_OFF);
122                         }
123                         break;
124                 case 480:
125                         _D("Resolution is %dx%d", s_animation.w, s_animation.h);
126                         if (state == TYPE_ON) {
127                                 elm_layout_file_set(s_animation.layout, WVGA_POWER_ON, GRP_ON);
128                         } else {
129                                 elm_layout_file_set(s_animation.layout, WVGA_POWER_OFF, GRP_OFF);
130                         }
131                         break;
132                 case 720:
133                         _D("Resolution is %dx%d", s_animation.w, s_animation.h);
134                         if (state == TYPE_ON) {
135                                 elm_layout_file_set(s_animation.layout, HD_POWER_ON, GRP_ON);
136                         } else {
137                                 elm_layout_file_set(s_animation.layout, HD_POWER_OFF, GRP_OFF);
138                         }
139                         break;
140                 default:
141                         _D("This is Strange resolution, Plz check, %dx%d", s_animation.w, s_animation.h);
142                         if (state == TYPE_ON) {
143                                 elm_layout_file_set(s_animation.layout, HD_POWER_ON, GRP_ON);
144                         } else {
145                                 elm_layout_file_set(s_animation.layout, HD_POWER_ON, GRP_OFF);
146                         }
147                         break;
148         }
149 }
150
151 static int init_layout(const char *msg)
152 {
153         s_animation.layout = elm_layout_add(s_animation.win);
154         if (!s_animation.layout) {
155                 _E("Failed to create layout");
156                 return EXIT_FAILURE;
157         }
158
159         layout_file_set(s_animation.state);
160
161         evas_object_size_hint_weight_set(s_animation.layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
162         _D("[Boot-ani] Start animation: %d mode", s_animation.state);
163         elm_win_resize_object_add(s_animation.win, s_animation.layout);
164         edje_object_signal_callback_add(elm_layout_edje_get(s_animation.layout), "end", "animation", _edje_cb, NULL);
165         evas_object_show(s_animation.layout);
166
167         if (msg) {
168                 if (!s_animation.txt) {
169                         s_animation.txt = evas_object_text_add(s_animation.evas);
170                         if (!s_animation.txt) {
171                                 _E("Failed to add text");
172                                 evas_object_del(s_animation.layout);
173                                 return EXIT_FAILURE;
174                         }
175                 }
176
177                 evas_object_text_text_set(s_animation.txt, msg);
178                 evas_object_hide(s_animation.txt);
179         }
180
181         return EXIT_SUCCESS;
182 }
183
184 static void fini_layout(void)
185 {
186         if (s_animation.layout) {
187                 evas_object_del(s_animation.layout);
188         }
189
190         if (s_animation.txt) {
191                 evas_object_del(s_animation.txt);
192                 s_animation.txt = NULL;
193         }
194 }
195
196 static int create_window(void)
197 {
198         _D("Create Window");
199         printf("Create Window\n");
200
201         int x, y = 0;
202
203         s_animation.win = elm_win_add(NULL, "BOOT_ANIMATION", ELM_WIN_NOTIFICATION);
204         elm_win_role_set(s_animation.win, "alert");
205         if (!s_animation.win) {
206                 _E("Failed to create a new window");
207                 printf("Failed to create a new window\n");
208                 return EXIT_FAILURE;
209         }
210         if (s_animation.state == TYPE_OFF || s_animation.state == TYPE_OFF_WITH_MSG) {
211                 _D("We are turning off the Tizen");
212                 elm_win_alpha_set(s_animation.win, EINA_TRUE);
213         }
214         evas_object_smart_callback_add(s_animation.win, "delete-request", win_del, NULL);
215
216         s_animation.evas = evas_object_evas_get(s_animation.win);
217         if (!s_animation.evas) {
218                 evas_object_del(s_animation.win);
219                 _E("Failed to get the evas object");
220                 return EXIT_FAILURE;
221         }
222         elm_win_screen_size_get(s_animation.win, &x, &y, &s_animation.w, &s_animation.h);
223         _D("Window size is x: %d, y: %d, w: %d, h: %d", x, y, s_animation.w, s_animation.h);
224         elm_win_borderless_set(s_animation.win, 1);
225         elm_win_indicator_mode_set(s_animation.win, ELM_WIN_INDICATOR_HIDE);
226         evas_object_move(s_animation.win, 0, 0);
227
228 /*      if (s_animation.w > s_animation.h) {
229                 int t;
230                 elm_win_rotation_with_resize_set(s_animation.win, 90);
231                 t = s_animation.w;
232                 s_animation.w = s_animation.h;
233                 s_animation.h = t;
234         }*/
235
236         evas_object_show(s_animation.win);
237
238         s_animation.ee = ecore_evas_ecore_evas_get(s_animation.evas);
239         if (!s_animation.ee) {
240                 evas_object_del(s_animation.win);
241                 _E("Failed to get the ecore evas object");
242                 return EXIT_FAILURE;
243         }
244
245         return EXIT_SUCCESS;
246 }
247
248 int init_animation(int state, const char *msg)
249 {
250         _D("Init animation");
251         printf("Init animation\n");
252
253         Ecore_Timer *timer = NULL;
254
255         s_animation.state = state;
256
257         if (create_window() == EXIT_FAILURE) {
258                 _E("Failed to create a new window");
259                 printf("Failed to create a new window\n");
260                 return EXIT_FAILURE;
261         }
262
263         if (init_layout(msg) == EXIT_FAILURE) {
264                 _E("Failed to init the layout object");
265                 if (msg) {
266                         evas_object_del(s_animation.txt);
267                 }
268                 evas_object_del(s_animation.win);
269         }
270
271         return EXIT_SUCCESS;
272 }
273
274 int fini_animation(void)
275 {
276         fini_layout();
277         evas_object_del(s_animation.win);
278         fflush(stdout);
279         close(1);
280         return EXIT_SUCCESS;
281 }