Tizen 2.1 base
[apps/native/boot-animation.git] / src / animation.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   * 
4   * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <unistd.h>
26
27 #include <utilX.h>
28 #include <vconf.h>
29
30 #include <Elementary.h>
31 #include <Ecore_X.h>
32 #include <Ecore_X_Atoms.h>
33
34 #include <boot-animation.h>
35 #include <animation.h>
36
37 static struct animation {
38         Ecore_X_Window focus_win;
39         Ecore_X_Window root_win;
40         Evas_Object *win;
41         Ecore_Window xwin;
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         fprintf(stderr, "Window delete event received\n");
56         elm_exit();
57 }
58
59 static void _edje_cb(void *d, Evas_Object * obj, const char *e, const char *s)
60 {
61         fprintf(stderr, "edje calback is invoked\n");
62
63         if (vconf_set_int("memory/boot-animation/finished", 1) != 0) {
64                 fprintf(stderr, "Failed to set finished set\n");
65         }
66
67         if (s_animation.state == TYPE_OFF || s_animation.state == TYPE_ON) {
68                 elm_exit();
69         } else if (s_animation.state == TYPE_OFF_WITH_MSG) {
70                 if (s_animation.txt) {
71                         Evas_Coord w;
72                         Evas_Coord h;
73
74                         evas_object_size_hint_weight_set(s_animation.txt,
75                                                          EVAS_HINT_EXPAND,
76                                                          EVAS_HINT_EXPAND);
77                         evas_object_size_hint_fill_set(s_animation.txt,
78                                                        EVAS_HINT_FILL,
79                                                        EVAS_HINT_FILL);
80                         evas_object_resize(s_animation.txt, s_animation.w,
81                                            s_animation.h);
82                         evas_object_color_set(s_animation.txt, 255, 255, 255,
83                                               255);
84                         evas_object_text_font_set(s_animation.txt,
85                                                   "SLP:style=medium", 30);
86                         evas_object_geometry_get(s_animation.txt, NULL, NULL,
87                                                  &w, &h);
88                         evas_object_move(s_animation.txt,
89                                          (s_animation.w - w) >> 1,
90                                          (s_animation.h - h) >> 1);
91                         evas_object_show(s_animation.txt);
92                 }
93         }
94 }
95
96 static
97 int init_layout(const char *msg)
98 {
99         s_animation.layout = elm_layout_add(s_animation.win);
100         if (!s_animation.layout) {
101                 fprintf(stderr, "Failed to create layout\n");
102                 return EXIT_FAILURE;
103         }
104
105         if (s_animation.state == TYPE_ON) {
106                 elm_layout_file_set(s_animation.layout, EDJ_ON, GRP_ON);
107         } else {
108                 elm_layout_file_set(s_animation.layout, EDJ_OFF, GRP_OFF);
109         }
110
111         evas_object_size_hint_weight_set(s_animation.layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
112         printf("[Boot-ani] Start animation: %d mode\n", s_animation.state);
113         elm_win_resize_object_add(s_animation.win, s_animation.layout);
114         edje_object_signal_callback_add(elm_layout_edje_get(s_animation.layout),
115                                         "end", "animation", _edje_cb, NULL);
116         evas_object_show(s_animation.layout);
117
118         Ecore_X_Window xwin;
119         xwin = elm_win_xwindow_get(s_animation.win);
120         ecore_x_icccm_hints_set(xwin, 0, 0, 0, 0, 0, 0, 0);
121
122
123         if (msg) {
124                 if (!s_animation.txt) {
125                         s_animation.txt =
126                             evas_object_text_add(s_animation.evas);
127                         if (!s_animation.txt) {
128                                 fprintf(stderr, "Failed to add text\n");
129                                 evas_object_del(s_animation.layout);
130                                 return EXIT_FAILURE;
131                         }
132                 }
133
134                 evas_object_text_text_set(s_animation.txt, msg);
135                 evas_object_hide(s_animation.txt);
136         }
137
138         if (vconf_set_int("memory/boot-animation/finished", 0) != 0) {
139                 fprintf(stderr, "Failed to set finished value to 0\n");
140         }
141
142         return EXIT_SUCCESS;
143 }
144
145 static
146 void fini_layout(void)
147 {
148         if (s_animation.layout) {
149                 evas_object_del(s_animation.layout);
150         }
151
152         if (s_animation.txt) {
153                 evas_object_del(s_animation.txt);
154                 s_animation.txt = NULL;
155         }
156 }
157
158 void restart_animation(int state, const char *msg)
159 {
160         fini_layout();
161
162         s_animation.state = state;
163
164         init_layout(msg);
165
166         evas_object_show(s_animation.win);
167 }
168
169 static
170 int create_window(void)
171 {
172         s_animation.win = elm_win_add(NULL, "", ELM_WIN_BASIC);
173         if (!s_animation.win) {
174                 fprintf(stderr, "Failed to create a new window\n");
175                 return EXIT_FAILURE;
176         }
177         evas_object_smart_callback_add(s_animation.win, "delete-request",
178                                        win_del, NULL);
179         evas_object_resize(s_animation.win, s_animation.w, s_animation.h);
180
181         s_animation.evas = evas_object_evas_get(s_animation.win);
182         if (!s_animation.evas) {
183                 evas_object_del(s_animation.win);
184                 fprintf(stderr, "Failed to get the evas object\n");
185                 return EXIT_FAILURE;
186         }
187         elm_win_borderless_set(s_animation.win, 1);
188         evas_object_move(s_animation.win, 0, 0);
189
190         if (s_animation.w > s_animation.h) {
191                 int t;
192                 elm_win_rotation_with_resize_set(s_animation.win, 90);
193                 t = s_animation.w;
194                 s_animation.w = s_animation.h;
195                 s_animation.h = t;
196         }
197         evas_object_show(s_animation.win);
198
199         s_animation.ee = ecore_evas_ecore_evas_get(s_animation.evas);
200         if (!s_animation.ee) {
201                 evas_object_del(s_animation.win);
202                 fprintf(stderr, "Failed to get the ecore evas object\n");
203                 return EXIT_FAILURE;
204         }
205
206         s_animation.xwin = ecore_evas_window_get(s_animation.ee);
207         ecore_x_netwm_window_type_set(s_animation.xwin,
208                                       ECORE_X_WINDOW_TYPE_NOTIFICATION);
209         utilx_set_system_notification_level(ecore_x_display_get(),
210                                             s_animation.xwin,
211                                             UTILX_NOTIFICATION_LEVEL_HIGH);
212
213         return EXIT_SUCCESS;
214 }
215
216 int init_animation(int state, const char *msg)
217 {
218         s_animation.focus_win = ecore_x_window_focus_get();
219         s_animation.root_win = ecore_x_window_root_get(s_animation.focus_win);
220         s_animation.state = state;
221         ecore_x_window_size_get(s_animation.root_win, &s_animation.w,
222                                 &s_animation.h);
223
224         if (create_window() == EXIT_FAILURE) {
225                 fprintf(stderr, "Failed to create a new window\n");
226                 return EXIT_FAILURE;
227         }
228
229         if (init_layout(msg) == EXIT_FAILURE) {
230                 fprintf(stderr, "Failed to init the layout object\n");
231                 if (msg)
232                         evas_object_del(s_animation.txt);
233                 evas_object_del(s_animation.win);
234                 return EXIT_FAILURE;
235         }
236
237         return EXIT_SUCCESS;
238 }
239
240 int fini_animation(void)
241 {
242         fini_layout();
243         evas_object_del(s_animation.win);
244         return EXIT_SUCCESS;
245 }