group_fuction_create_window
[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
63         if (type == TYPE_ON) {
64                 _D("EXIT on BOOTING");
65                 if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0) {
66                         _E("Failed to set finished set");
67                 }
68                 elm_exit();
69         } else {
70                 /* Delete Previous Layout */
71                 _D("EXIT on SHUTDOWN");
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 __set_poweroff_message(void)
84 {
85         Evas_Coord w;
86         Evas_Coord h;
87
88         evas_object_size_hint_weight_set(s_animation.txt, EVAS_HINT_EXPAND,     EVAS_HINT_EXPAND);
89         evas_object_size_hint_fill_set(s_animation.txt, EVAS_HINT_FILL, EVAS_HINT_FILL);
90         evas_object_resize(s_animation.txt, s_animation.w, s_animation.h);
91         evas_object_color_set(s_animation.txt, 255, 255, 255, 255);
92         evas_object_text_font_set(s_animation.txt, "SLP:style=medium", 30);
93         evas_object_geometry_get(s_animation.txt, NULL, NULL, &w, &h);
94         evas_object_move(s_animation.txt, (s_animation.w - w) >> 1, (s_animation.h - h) >> 1);
95         evas_object_show(s_animation.txt);
96 }
97
98 static void __edje_cb(void *d, Evas_Object * obj, const char *e, const char *s)
99 {
100         if (s_animation.state == TYPE_OFF || s_animation.state == TYPE_OFF_WITH_MSG) {
101                 _D("TYPE OFF");
102                 if (vconf_set_int(VCONFKEY_BOOT_ANIMATION_FINISHED, 1) != 0) {
103                         _E("Failed to set finished set");
104                 }
105                 if (s_animation.txt) {
106                         __set_poweroff_message();
107                 }
108                 ecore_timer_add(1, __end_cb, (void *)TYPE_OFF);
109         } else {
110                 _D("TYPE_ON");
111                 __end_cb((void *)TYPE_ON);
112         }
113 }
114
115 static Eina_Bool __layout_file_set(Evas_Object *layout, int state)
116 {
117         char file_name[1024];
118         char *on_off;
119         int ret;
120
121         if (state == TYPE_ON) {
122                 snprintf(file_name, sizeof(file_name), FILE_PATH"%dx%d_PowerOn.edj", s_animation.w, s_animation.h);
123                 on_off = GRP_ON;
124         } else {
125                 snprintf(file_name, sizeof(file_name), FILE_PATH"%dx%d_PowerOff.edj", s_animation.w, s_animation.h);
126                 on_off = GRP_OFF;
127         }
128         _D("File name for Animation is: %s", file_name);
129
130         ret = access(file_name, 0);
131         if (ret == 0) {
132                 _D("This Resolution[%d]x[%d] is supported !!", s_animation.w, s_animation.h);
133                 if (!elm_layout_file_set(layout, file_name, on_off)) return EINA_FALSE;
134         } else {
135                 _E("This Resolution[%d]x[%d] is STRANGE !!, Set default image '720x1280'", s_animation.w, s_animation.h);
136                 if (state == TYPE_ON) {
137                         if (!elm_layout_file_set(layout, DEFAULT_ON, on_off)) return EINA_FALSE;
138                 } else {
139                         if (!elm_layout_file_set(layout, DEFAULT_OFF, on_off)) return EINA_FALSE;
140                 }
141         }
142
143         return EINA_TRUE;
144 }
145
146 static Evas_Object *__create_layout(const char *msg)
147 {
148         Eina_Bool ret;
149         Evas_Object *layout = elm_layout_add(s_animation.win);
150         if (!layout) {
151                 _E("Failed to create layout");
152                 return NULL;
153         }
154
155         ret = __layout_file_set(layout, s_animation.state);
156         if (ret != EINA_TRUE) {
157                 _E("Failed to set layout file");
158                 evas_object_del(layout);
159                 return NULL;
160         }
161
162         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
163         elm_win_resize_object_add(s_animation.win, layout);
164         edje_object_signal_callback_add(elm_layout_edje_get(layout), "end", "animation", __edje_cb, NULL);
165         evas_object_show(layout);
166
167         if (msg) {
168                 if (!s_animation.txt) {
169                         s_animation.txt = evas_object_text_add(evas_object_evas_get(s_animation.win));
170                         if (!s_animation.txt) {
171                                 _E("Failed to add text");
172                                 evas_object_del(layout);
173                                 return NULL;
174                         }
175                 }
176
177                 evas_object_text_text_set(s_animation.txt, msg);
178                 evas_object_hide(s_animation.txt);
179         }
180
181         return layout;
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 Evas_Object *__create_window(void)
197 {
198         _D("Create Window");
199         printf("Create Window\n");
200
201         int x, y = 0;
202         Evas_Object *win;
203
204         win = elm_win_add(NULL, "BOOT_ANIMATION", ELM_WIN_NOTIFICATION);
205         elm_win_aux_hint_add(win, "wm.comp.win.always.selective.mode", "1");
206
207         elm_win_role_set(win, "alert");
208         if (!win) {
209                 _E("Failed to create a new window");
210                 printf("Failed to create a new window\n");
211                 return NULL;
212         }
213         if (s_animation.state == TYPE_OFF || s_animation.state == TYPE_OFF_WITH_MSG) {
214                 _D("We are turning off the Tizen");
215         }
216         efl_util_set_notification_window_level(win, EFL_UTIL_NOTIFICATION_LEVEL_HIGH);
217         evas_object_smart_callback_add(win, "delete-request", win_del, NULL);
218
219         elm_win_screen_size_get(win, &x, &y, &s_animation.w, &s_animation.h);
220         _D("Window size is x: %d, y: %d, w: %d, h: %d", x, y, s_animation.w, s_animation.h);
221         elm_win_borderless_set(win, 1);
222         elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_HIDE);
223         evas_object_move(win, 0, 0);
224         evas_object_show(win);
225
226         return win;
227 }
228
229 int init_animation(int state, const char *msg)
230 {
231         _D("Init animation");
232         printf("Init animation\n");
233
234         s_animation.state = state;
235
236         s_animation.win = __create_window();
237         if (!s_animation.win) {
238                 _E("Failed to create a new window");
239                 printf("Failed to create a new window\n");
240                 return EXIT_FAILURE;
241         }
242
243         s_animation.layout = __create_layout(msg);
244         if (!s_animation.layout) {
245                 _E("Failed to create layout");
246                 if (s_animation.txt) {
247                         evas_object_del(s_animation.txt);
248                 }
249                 evas_object_del(s_animation.win);
250                 return EXIT_FAILURE;
251         }
252
253         return EXIT_SUCCESS;
254 }
255
256 int fini_animation(void)
257 {
258         __fini_layout();
259         evas_object_del(s_animation.win);
260         fflush(stdout);
261         close(1);
262         return EXIT_SUCCESS;
263 }
264
265 #if TEST_MODE
266 Evas_Object *__t_create_window(void)
267 {
268         return __create_window();
269 }
270 #endif