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