Fix coding rule disobey
[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 <vconf.h>
19 #include <efl_util.h>
20
21 #include "boot.h"
22 #include "animation.h"
23 #include "app_log.h"
24
25 static struct animation {
26         Evas_Object *win;
27         Evas_Object *layout;
28         Evas_Object *display_block;
29         Evas_Coord win_w;
30         Evas_Coord win_h;
31         int animation_type;
32 } s_animation = {
33         .win = NULL,
34         .layout = NULL,
35         .display_block = NULL,
36         .win_w = 0,
37         .win_h = 0,
38         .animation_type = 0,
39 };
40
41 static void __win_del_cb(void *data, Evas_Object * obj, void *event_info)
42 {
43         __D("Window delete event received");
44         elm_exit();
45 }
46
47 static Evas_Object *__create_window(void)
48 {
49         __D("Create Window");
50
51         Evas_Object *win;
52
53         win = elm_win_add(NULL, "BOOT_ANIMATION", ELM_WIN_NOTIFICATION);
54         if (!win) {
55                 __E("Failed to create a new window");
56                 return NULL;
57         }
58         elm_win_aux_hint_add(win, "wm.comp.win.always.selective.mode", "1");
59         elm_win_role_set(win, "alert");
60         efl_util_set_notification_window_level(win, EFL_UTIL_NOTIFICATION_LEVEL_HIGH);
61         evas_object_smart_callback_add(win, "delete-request", __win_del_cb, NULL);
62
63         elm_win_screen_size_get(win, NULL, NULL, &s_animation.win_w, &s_animation.win_h);
64         elm_win_borderless_set(win, EINA_TRUE);
65         elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_HIDE);
66         evas_object_move(win, 0, 0);
67         evas_object_show(win);
68
69         return win;
70 }
71
72 static void __block_display(void)
73 {
74         Evas_Object *display_block = NULL;
75         display_block = evas_object_rectangle_add(evas_object_evas_get(s_animation.win));
76         elm_win_resize_object_add(s_animation.win, display_block);
77         evas_object_color_set(display_block, 0, 0, 0, 255);
78         evas_object_show(display_block);
79
80         s_animation.display_block = display_block;
81 }
82
83 static Eina_Bool __end_cb(void *data)
84 {
85         int type = (int) data;
86
87         if (type == TYPE_OFF) {
88                 __D("EXIT on SHUTDOWN");
89                 /* Delete Previous Layout */
90                 if (s_animation.layout) {
91                         evas_object_del(s_animation.layout);
92                         s_animation.layout = NULL;
93                 }
94
95                 /* boot-animation do not terminate on shutdown, so flush log now */
96                 fflush(stdout);
97                 close(1);
98
99                 __block_display();
100         } else {
101                 __D("EXIT on BOOTING");
102                 if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0)
103                         __E("Failed to set finished set");
104
105                 boot_exit();
106         }
107         return ECORE_CALLBACK_CANCEL;
108 }
109
110 static void __animation_finished_cb(void *d, Evas_Object * obj, const char *e, const char *s)
111 {
112         if (s_animation.animation_type == TYPE_OFF) {
113                 __D("TYPE OFF");
114                 if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0)
115                         __E("Failed to set finished set");
116
117                 ecore_timer_add(1, __end_cb, (void *)TYPE_OFF);
118         } else {
119                 __D("TYPE_ON");
120                 __end_cb((void *)TYPE_ON);
121         }
122 }
123
124 static Evas_Object * __create_layout(Evas_Object *win, const char *file_name)
125 {
126         Evas_Object *layout = elm_layout_add(win);
127         if (!layout) {
128                 __E("Failed to create layout");
129                 return NULL;
130         }
131         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
132         elm_win_resize_object_add(s_animation.win, layout);
133         elm_layout_signal_callback_add(layout, "end", "animation", __animation_finished_cb, NULL);
134
135         if (!elm_layout_file_set(layout, file_name, GRP_ANIM)) {
136                 __E("Failed to layout file set");
137                 return NULL;
138         }
139
140         evas_object_show(layout);
141
142         return layout;
143 }
144
145 static void __destroy_layout(void)
146 {
147         if (s_animation.display_block) {
148                 evas_object_del(s_animation.display_block);
149                 s_animation.display_block = NULL;
150         }
151
152         if (s_animation.layout) {
153                 evas_object_del(s_animation.layout);
154                 s_animation.layout = NULL;
155         }
156 }
157
158 static char * __get_layout_file_name(int animation_type, int w, int h)
159 {
160         char file_name[1024];
161         int ret = 0;
162
163         if (animation_type == TYPE_OFF)
164                 snprintf(file_name, sizeof(file_name), FILE_PATH"%dx%d_PowerOff.edj", w, h);
165         else
166                 snprintf(file_name, sizeof(file_name), FILE_PATH"%dx%d_PowerOn.edj", w, h);
167
168         __D("File name for Animation is: %s", file_name);
169
170         ret = access(file_name, 0);
171         if (ret != 0) {
172                 __E("There's no Image same with [%s], So set Default[720x1280] image", file_name);
173                 if (animation_type == TYPE_OFF)
174                         snprintf(file_name, sizeof(file_name), "%s", DEFAULT_OFF);
175                 else
176                         snprintf(file_name, sizeof(file_name), "%s", DEFAULT_ON);
177
178                 ret = access(file_name, 0);
179                 if (ret) {
180                         __E("There's no Image file[%s] on target", file_name);
181                         return NULL;
182                 }
183         }
184
185         return strdup(file_name);
186 }
187
188 static Eina_Bool __is_layout_file_exist(const char *file_name)
189 {
190         int ret = 0;
191
192         ret = access(file_name, 0);
193         if (ret == 0)
194                 return EINA_TRUE;
195         else
196                 return EINA_FALSE;
197 }
198
199 int animation_init(int animation_type)
200 {
201         __D("Init animation");
202
203         s_animation.animation_type = animation_type;
204
205         s_animation.win = __create_window();
206         if (!s_animation.win) {
207                 __E("Failed to create a new window");
208                 return EXIT_FAILURE;
209         }
210
211         char *layout_file_name = NULL;
212         layout_file_name = __get_layout_file_name(animation_type, s_animation.win_w, s_animation.win_h);
213         if (!layout_file_name) {
214                 __E("Failed to get layout file name");
215                 return EXIT_FAILURE;
216         }
217
218         if (!__is_layout_file_exist(layout_file_name)) {
219                 __E("This file is not exist");
220                 free(layout_file_name);
221                 return EXIT_FAILURE;
222         }
223
224         s_animation.layout = __create_layout(s_animation.win, layout_file_name);
225         if (!s_animation.layout) {
226                 __E("Failed to create layout");
227                 free(layout_file_name);
228                 evas_object_del(s_animation.win);
229                 return EXIT_FAILURE;
230         }
231
232         free(layout_file_name);
233
234         return EXIT_SUCCESS;
235 }
236
237 int animation_fini(void)
238 {
239         __destroy_layout();
240
241         evas_object_del(s_animation.win);
242         fflush(stdout);
243         close(1);
244         return EXIT_SUCCESS;
245 }
246
247 #include "test.h"
248 #if TEST_MODE
249 Evas_Object *__t__get_window(void)
250 {
251         return s_animation.win;
252 }
253
254 Evas_Object *__t__get_layout(void)
255 {
256         return s_animation.layout;
257 }
258
259 Evas_Object *__t__get_display_block(void)
260 {
261         return s_animation.display_block;
262 }
263
264 Evas_Object * __t__animation_create_layout(Evas_Object *win, const char *file_name)
265 {
266         return __create_layout(win, file_name);
267 }
268
269 Evas_Object *__t__animation_create_window(void)
270 {
271         return __create_window();
272 }
273
274 char * __t__get_layout_file_name(int animation_type, int w, int h)
275 {
276         return __get_layout_file_name(animation_type, w, h);
277 }
278
279 Eina_Bool __t__is_layout_file_exist(const char *file_name)
280 {
281         return __is_layout_file_exist(file_name);
282 }
283 #endif