don't iconify when first launch
[profile/tv/apps/native/homescreen.git] / src / main.c
1 /*
2  * Copyright (c) 2014 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 #include <app.h>
18 #include <Elementary.h>
19
20 #include "homescreen.h"
21 #include "engine.h"
22 #include "dbg.h"
23
24 struct _appdata {
25         const char *name;
26         Evas_Object *win;
27
28         struct homescreen *hscr;
29         struct engine *eng;
30         int initted;
31 };
32
33 /**
34  * Creates transparent window as BASIC type.
35  *
36  * @param name The name of package
37  * @return window object or NULL otherwise
38  */
39 static Evas_Object *_add_win(const char *name)
40 {
41         Evas_Object *win;
42
43         win = elm_win_add(NULL, name, ELM_WIN_BASIC);
44         if (!win) {
45                 _ERR("failed to create window");
46                 return NULL;
47         }
48
49         elm_win_alpha_set(win, EINA_TRUE);
50         evas_object_show(win);
51
52         return win;
53 }
54
55 /**
56  * Invoked as soon as application is launched according to applcation lifecycle.
57  *
58  * This function creates window object and overlay the theme file.
59  * And also doing following things.
60  *  1. Initializes engine for communicating with several content-providers.
61  *  2. Initializes homescreen UI.
62  *  3. Loads homescreen data.
63  *
64  * @param user_data The function specific data which holds _appdata pointer
65  * @return true If the window and layout object is created successfully;
66  *         false otherwise
67  */
68 static bool _create(void *user_data)
69 {
70         struct _appdata *ad;
71         struct homescreen *hscr;
72         struct engine *eng;
73         Evas_Object *win;
74         int r;
75
76         if (!user_data) {
77                 _ERR("Invalid argument");
78                 return false;
79         }
80
81         ad = user_data;
82
83         elm_theme_overlay_add(NULL, THEMEFILE);
84         elm_config_focus_move_policy_set(ELM_FOCUS_MOVE_POLICY_CLICK);
85
86         eng = engine_init();
87         if (!eng) {
88                 _ERR("failed to initialize engine");
89                 return false;
90         }
91
92         hscr = homescreen_init(eng);
93         if (!hscr) {
94                 _ERR("failed to initialize homescreen");
95                 engine_fini(eng);
96                 return false;
97         }
98
99         win = _add_win(ad->name);
100         if (!win) {
101                 _ERR("failed to create window");
102                 homescreen_fini(hscr);
103                 engine_fini(eng);
104                 return false;
105         }
106
107         r = homescreen_load(hscr, win);
108         if (r != 0) {
109                 _ERR("failed to load homescreen");
110                 homescreen_fini(hscr);
111                 engine_fini(eng);
112                 evas_object_del(win);
113                 return false;
114         }
115
116         ad->win = win;
117         ad->hscr = hscr;
118         ad->eng = eng;
119
120         return true;
121 }
122
123 /**
124  * Invoked before application is terminated according to application lifecycle.
125  *
126  * All Resources will be released.
127  *
128  * @param user_data The function specific data which holds _appdata pointer
129  */
130 static void _terminate(void *user_data)
131 {
132         struct _appdata *ad;
133
134         if (!user_data) {
135                 _ERR("Invalid argument");
136                 return;
137         }
138
139         ad = user_data;
140
141         homescreen_fini(ad->hscr);
142         engine_fini(ad->eng);
143
144         if (ad->win)
145                 evas_object_del(ad->win);
146 }
147
148 /**
149  * Invoked when window of application becomes invisible.
150  *
151  * Hides this applciation.
152  *
153  * @param user_data The function specific data which holds _appdata pointer
154  */
155 static void _pause(void *user_data)
156 {
157         struct _appdata *ad;
158
159         if (!user_data) {
160                 _ERR("Invalid argument");
161                 return;
162         }
163
164         ad = user_data;
165
166         homescreen_hide(ad->hscr);
167 }
168
169 /**
170  * Invoked when window of application becomes visible.
171  *
172  * Shows this application.
173  *
174  * @param user_data The function specific data which holds _appdata pointer
175  */
176 static void _resume(void *user_data)
177 {
178         struct _appdata *ad;
179
180         if (!user_data) {
181                 _ERR("Invalid argument");
182                 return;
183         }
184
185         ad = user_data;
186
187         homescreen_show(ad->hscr);
188 }
189
190 /**
191  * Invoked when application should re-lauched according to application
192  * lifecycle.
193  *
194  * If this function is called at first, set the iconified state of window.
195  * After then, it activates window and shows this application.
196  *
197  * @param service The provided arguments for launching
198  * @param user_data The function specific data which holds _appdata pointer
199  */
200 static void _service(service_h service, void *user_data)
201 {
202         struct _appdata *ad;
203
204         if (!user_data) {
205                 _ERR("Invalid argument");
206                 return;
207         }
208
209         ad = user_data;
210
211         /* FIXME: workaround */
212         homescreen_show(ad->hscr);
213         return;
214
215         if (!ad->initted) {
216                 elm_win_iconified_set(ad->win, EINA_TRUE);
217                 ad->initted = 1;
218         } else {
219                 elm_win_activate(ad->win);
220                 homescreen_show(ad->hscr);
221         }
222 }
223
224 /**
225  * Invoked when language setting changed according to application events.
226  *
227  * There is no action in this applciation.
228  */
229 static void _lang_changed(void *user_data)
230 {
231 }
232
233 /**
234  * Invoked when system region setting changed according to application events.
235  *
236  * There is no action in this application.
237  */
238 static void _region_fmt_changed(void *user_data)
239 {
240 }
241
242 /**
243  * Main function
244  *
245  * This function calls to register the state transition callbacks and system
246  * event callbacks at main loop.
247  *
248  * @param args The argument count
249  * @param argv The argument vector
250  */
251 int main(int argc, char **argv)
252 {
253         struct _appdata ad;
254         app_event_callback_s cbs = {
255                 .create = _create,
256                 .terminate = _terminate,
257                 .pause = _pause,
258                 .resume = _resume,
259                 .service = _service,
260                 .low_memory = NULL,
261                 .low_battery = NULL,
262                 .device_orientation = NULL,
263                 .language_changed = _lang_changed,
264                 .region_format_changed = _region_fmt_changed,
265         };
266
267         memset(&ad, 0x00, sizeof(ad));
268         ad.name = PACKAGE;
269
270         app_efl_main(&argc, &argv, &cbs, &ad);
271
272         return 0;
273 }
274