Initialize Tizen 2.3
[framework/api/application.git] / src / app_main.c
1 /*
2  * Copyright (c) 2011 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 <unistd.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <bundle.h>
27 #include <appcore-common.h>
28 #include <appcore-efl.h>
29 #include <aul.h>
30 #include <dlog.h>
31
32 #include <Elementary.h>
33
34 #include <app_private.h>
35 #include <app_service_private.h>
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40
41 #define LOG_TAG "CAPI_APPFW_APPLICATION"
42
43 typedef enum {
44         APP_STATE_NOT_RUNNING, // The application has been launched or was running but was terminated
45         APP_STATE_CREATING, // The application is initializing the resources on app_create_cb callback
46         APP_STATE_RUNNING, // The application is running in the foreground and background
47 } app_state_e;
48
49 typedef struct {
50         char *package;
51         char *app_name;
52         app_state_e state;
53         app_event_callback_s *callback;
54         void *data;
55 } app_context_s;
56
57 typedef app_context_s *app_context_h;
58
59 static int app_appcore_create(void *data);
60 static int app_appcore_pause(void *data);
61 static int app_appcore_resume(void *data);
62 static int app_appcore_terminate(void *data);
63 static int app_appcore_reset(bundle *appcore_bundle, void *data);
64
65 static int app_appcore_low_memory(void *data);
66 static int app_appcore_low_battery(void *data);
67 static int app_appcore_rotation_event(enum appcore_rm rm, void *data);
68 static int app_appcore_lang_changed(void *data);
69 static int app_appcore_region_changed(void *data);
70
71 static void app_set_appcore_event_cb(app_context_h app_context);
72 static void app_unset_appcore_event_cb(void);
73
74
75 int app_efl_main(int *argc, char ***argv, app_event_callback_s *callback, void *user_data)
76 {
77         app_context_s app_context = {
78                 .package = NULL,
79                 .app_name = NULL,
80                 .state = APP_STATE_NOT_RUNNING,
81                 .callback = callback,
82                 .data = user_data
83         };
84
85         struct appcore_ops appcore_context = {
86                 .data = &app_context,
87                 .create = app_appcore_create,
88                 .terminate = app_appcore_terminate,
89                 .pause = app_appcore_pause,
90                 .resume = app_appcore_resume,
91                 .reset = app_appcore_reset,
92         };
93
94         if (argc == NULL || argv == NULL || callback == NULL)
95         {
96                 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
97         }
98
99         if (callback->create == NULL)
100         {
101                 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "app_create_cb() callback must be registered");
102         }
103
104         if (app_context.state != APP_STATE_NOT_RUNNING)
105         {
106                 return app_error(APP_ERROR_ALREADY_RUNNING, __FUNCTION__, NULL);
107         }
108
109         if (app_get_id(&(app_context.package)) != APP_ERROR_NONE)
110         {
111                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package");
112         }
113         
114         if (app_get_package_app_name(app_context.package, &(app_context.app_name)) != APP_ERROR_NONE)
115         {
116                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package's app name");
117         }
118
119         app_context.state = APP_STATE_CREATING;
120
121         appcore_efl_main(app_context.app_name, argc, argv, &appcore_context);
122
123         free(app_context.package);
124         free(app_context.app_name);
125
126         return APP_ERROR_NONE;
127 }
128
129
130 void app_efl_exit(void)
131 {
132         elm_exit();
133 }
134
135
136 int app_appcore_create(void *data)
137 {
138         app_context_h app_context = data;
139         app_create_cb create_cb;
140         char locale_dir[TIZEN_PATH_MAX] = {0, };
141
142         if (app_context == NULL)
143         {
144                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
145         }
146
147         app_set_appcore_event_cb(app_context);
148
149         snprintf(locale_dir, TIZEN_PATH_MAX, PATH_FMT_LOCALE_DIR, app_context->package);
150         if (access(locale_dir, R_OK) != 0) {
151                 snprintf(locale_dir, TIZEN_PATH_MAX, PATH_FMT_RO_LOCALE_DIR, app_context->package);
152         }
153         appcore_set_i18n(app_context->app_name, locale_dir);
154
155         create_cb = app_context->callback->create;
156
157         if (create_cb == NULL || create_cb(app_context->data) == false)
158         {
159                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "app_create_cb() returns false");
160         }
161
162         app_context->state = APP_STATE_RUNNING;
163
164         return APP_ERROR_NONE;
165 }
166
167 int app_appcore_terminate(void *data)
168 {
169         app_context_h app_context = data;
170         app_terminate_cb terminate_cb;
171
172         if (app_context == NULL)
173         {
174                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
175         }
176
177         terminate_cb = app_context->callback->terminate;
178
179         if (terminate_cb != NULL)
180         {
181                 terminate_cb(app_context->data);
182         }
183
184         app_unset_appcore_event_cb();   
185
186         app_finalizer_execute();
187
188         return APP_ERROR_NONE;
189 }
190
191 int app_appcore_pause(void *data)
192 {
193         app_context_h app_context = data;
194         app_pause_cb pause_cb;
195
196         if (app_context == NULL)
197         {
198                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
199         }
200
201         pause_cb = app_context->callback->pause;
202
203         if (pause_cb != NULL)
204         {
205                 pause_cb(app_context->data);
206         }
207
208         return APP_ERROR_NONE;
209 }
210
211 int app_appcore_resume(void *data)
212 {
213         app_context_h app_context = data;
214         app_resume_cb resume_cb;
215
216         if (app_context == NULL)
217         {
218                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
219         }
220
221         resume_cb = app_context->callback->resume;
222
223         if (resume_cb != NULL)
224         {
225                 resume_cb(app_context->data);
226         }
227
228         return APP_ERROR_NONE;
229 }
230
231
232 int app_appcore_reset(bundle *appcore_bundle, void *data)
233 {
234         app_context_h app_context = data;
235         app_service_cb service_cb;
236         service_h service;
237
238         if (app_context == NULL)
239         {
240                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
241         }
242
243         if (service_create_event(appcore_bundle, &service) != APP_ERROR_NONE)
244         {
245                 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to create a service handle from the bundle");
246         }
247
248         service_cb = app_context->callback->service;
249
250         if (service_cb != NULL)
251         {
252                 service_cb(service, app_context->data);
253         }
254
255         service_destroy(service);
256
257         return APP_ERROR_NONE;
258 }
259
260
261 int app_appcore_low_memory(void *data)
262 {
263         app_context_h app_context = data;
264         app_low_memory_cb low_memory_cb;
265
266         if (app_context == NULL)
267         {
268                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
269         }
270
271         low_memory_cb = app_context->callback->low_memory;
272
273         if (low_memory_cb != NULL)
274         {
275                 low_memory_cb(app_context->data);
276         }
277
278         return APP_ERROR_NONE;
279 }
280
281 int app_appcore_low_battery(void *data)
282 {
283         app_context_h app_context = data;
284         app_low_battery_cb low_battery_cb;
285
286         if (app_context == NULL)
287         {
288                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
289         }
290
291         low_battery_cb = app_context->callback->low_battery;
292
293         if (low_battery_cb != NULL)
294         {
295                 low_battery_cb(app_context->data);
296         }
297
298         return APP_ERROR_NONE;
299 }
300
301 int app_appcore_rotation_event(enum appcore_rm rm, void *data)
302 {
303         app_context_h app_context = data;
304         app_device_orientation_cb device_orientation_cb;
305
306         if (app_context == NULL)
307         {
308                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
309         }
310
311         device_orientation_cb = app_context->callback->device_orientation;
312
313         if (device_orientation_cb != NULL)
314         {
315                 app_device_orientation_e dev_orientation;
316
317                 dev_orientation = app_convert_appcore_rm(rm);
318
319                 device_orientation_cb(dev_orientation, app_context->data);
320         }
321
322         return APP_ERROR_NONE;
323 }
324
325 int app_appcore_lang_changed(void *data)
326 {
327         app_context_h app_context = data;
328         app_language_changed_cb lang_changed_cb;
329
330         if (app_context == NULL)
331         {
332                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
333         }
334
335         lang_changed_cb = app_context->callback->language_changed;
336
337         if (lang_changed_cb != NULL)
338         {
339                 lang_changed_cb(app_context->data);
340         }
341
342         return APP_ERROR_NONE;
343 }
344
345 int app_appcore_region_changed(void *data)
346 {
347         app_context_h app_context = data;
348         app_region_format_changed_cb region_changed_cb;
349
350         if (app_context == NULL)
351         {
352                 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
353         }
354
355         region_changed_cb = app_context->callback->region_format_changed;
356
357         if (region_changed_cb != NULL)
358         {
359                 region_changed_cb(app_context->data);
360         }
361
362         return APP_ERROR_NONE;
363 }
364
365
366 void app_set_appcore_event_cb(app_context_h app_context)
367 {
368         if (app_context->callback->low_memory != NULL)
369         {
370                 appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, app_appcore_low_memory, app_context);
371         }
372
373         if (app_context->callback->low_battery != NULL)
374         {
375                 appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, app_appcore_low_battery, app_context);
376         }
377
378         if (app_context->callback->device_orientation != NULL)
379         {
380                 appcore_set_rotation_cb(app_appcore_rotation_event, app_context);
381         }
382
383         if (app_context->callback->language_changed != NULL)
384         {
385                 appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, app_appcore_lang_changed, app_context);
386         }
387
388         if (app_context->callback->region_format_changed != NULL)
389         {
390                 appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, app_appcore_region_changed, app_context);
391         }
392 }
393
394 void app_unset_appcore_event_cb(void)
395 {
396         appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, NULL, NULL);
397         appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, NULL, NULL);
398         appcore_unset_rotation_cb();
399         appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, NULL, NULL);
400         appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, NULL, NULL);
401 }