2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
27 #include <appcore-common.h>
28 #include <appcore-efl.h>
32 #include <Elementary.h>
34 #include <app_private.h>
35 #include <app_service_private.h>
41 #define LOG_TAG "CAPI_APPFW_APPLICATION"
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
53 app_event_callback_s *callback;
57 typedef app_context_s *app_context_h;
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);
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);
71 static void app_set_appcore_event_cb(app_context_h app_context);
72 static void app_unset_appcore_event_cb(void);
75 int app_efl_main(int *argc, char ***argv, app_event_callback_s *callback, void *user_data)
77 app_context_s app_context = {
80 .state = APP_STATE_NOT_RUNNING,
85 struct appcore_ops appcore_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,
94 if (argc == NULL || argv == NULL || callback == NULL)
96 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
99 if (callback->create == NULL)
101 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "app_create_cb() callback must be registered");
104 if (app_context.state != APP_STATE_NOT_RUNNING)
106 return app_error(APP_ERROR_ALREADY_RUNNING, __FUNCTION__, NULL);
109 if (app_get_id(&(app_context.package)) != APP_ERROR_NONE)
111 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package");
114 if (app_get_package_app_name(app_context.package, &(app_context.app_name)) != APP_ERROR_NONE)
116 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package's app name");
119 app_context.state = APP_STATE_CREATING;
121 appcore_efl_main(app_context.app_name, argc, argv, &appcore_context);
123 free(app_context.package);
124 free(app_context.app_name);
126 return APP_ERROR_NONE;
130 void app_efl_exit(void)
136 int app_appcore_create(void *data)
138 app_context_h app_context = data;
139 app_create_cb create_cb;
140 char locale_dir[TIZEN_PATH_MAX] = {0, };
142 if (app_context == NULL)
144 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
147 app_set_appcore_event_cb(app_context);
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);
153 appcore_set_i18n(app_context->app_name, locale_dir);
155 create_cb = app_context->callback->create;
157 if (create_cb == NULL || create_cb(app_context->data) == false)
159 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "app_create_cb() returns false");
162 app_context->state = APP_STATE_RUNNING;
164 return APP_ERROR_NONE;
167 int app_appcore_terminate(void *data)
169 app_context_h app_context = data;
170 app_terminate_cb terminate_cb;
172 if (app_context == NULL)
174 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
177 terminate_cb = app_context->callback->terminate;
179 if (terminate_cb != NULL)
181 terminate_cb(app_context->data);
184 app_unset_appcore_event_cb();
186 app_finalizer_execute();
188 return APP_ERROR_NONE;
191 int app_appcore_pause(void *data)
193 app_context_h app_context = data;
194 app_pause_cb pause_cb;
196 if (app_context == NULL)
198 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
201 pause_cb = app_context->callback->pause;
203 if (pause_cb != NULL)
205 pause_cb(app_context->data);
208 return APP_ERROR_NONE;
211 int app_appcore_resume(void *data)
213 app_context_h app_context = data;
214 app_resume_cb resume_cb;
216 if (app_context == NULL)
218 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
221 resume_cb = app_context->callback->resume;
223 if (resume_cb != NULL)
225 resume_cb(app_context->data);
228 return APP_ERROR_NONE;
232 int app_appcore_reset(bundle *appcore_bundle, void *data)
234 app_context_h app_context = data;
235 app_service_cb service_cb;
238 if (app_context == NULL)
240 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
243 if (service_create_event(appcore_bundle, &service) != APP_ERROR_NONE)
245 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to create a service handle from the bundle");
248 service_cb = app_context->callback->service;
250 if (service_cb != NULL)
252 service_cb(service, app_context->data);
255 service_destroy(service);
257 return APP_ERROR_NONE;
261 int app_appcore_low_memory(void *data)
263 app_context_h app_context = data;
264 app_low_memory_cb low_memory_cb;
266 if (app_context == NULL)
268 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
271 low_memory_cb = app_context->callback->low_memory;
273 if (low_memory_cb != NULL)
275 low_memory_cb(app_context->data);
278 return APP_ERROR_NONE;
281 int app_appcore_low_battery(void *data)
283 app_context_h app_context = data;
284 app_low_battery_cb low_battery_cb;
286 if (app_context == NULL)
288 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
291 low_battery_cb = app_context->callback->low_battery;
293 if (low_battery_cb != NULL)
295 low_battery_cb(app_context->data);
298 return APP_ERROR_NONE;
301 int app_appcore_rotation_event(enum appcore_rm rm, void *data)
303 app_context_h app_context = data;
304 app_device_orientation_cb device_orientation_cb;
306 if (app_context == NULL)
308 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
311 device_orientation_cb = app_context->callback->device_orientation;
313 if (device_orientation_cb != NULL)
315 app_device_orientation_e dev_orientation;
317 dev_orientation = app_convert_appcore_rm(rm);
319 device_orientation_cb(dev_orientation, app_context->data);
322 return APP_ERROR_NONE;
325 int app_appcore_lang_changed(void *data)
327 app_context_h app_context = data;
328 app_language_changed_cb lang_changed_cb;
330 if (app_context == NULL)
332 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
335 lang_changed_cb = app_context->callback->language_changed;
337 if (lang_changed_cb != NULL)
339 lang_changed_cb(app_context->data);
342 return APP_ERROR_NONE;
345 int app_appcore_region_changed(void *data)
347 app_context_h app_context = data;
348 app_region_format_changed_cb region_changed_cb;
350 if (app_context == NULL)
352 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
355 region_changed_cb = app_context->callback->region_format_changed;
357 if (region_changed_cb != NULL)
359 region_changed_cb(app_context->data);
362 return APP_ERROR_NONE;
366 void app_set_appcore_event_cb(app_context_h app_context)
368 if (app_context->callback->low_memory != NULL)
370 appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, app_appcore_low_memory, app_context);
373 if (app_context->callback->low_battery != NULL)
375 appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, app_appcore_low_battery, app_context);
378 if (app_context->callback->device_orientation != NULL)
380 appcore_set_rotation_cb(app_appcore_rotation_event, app_context);
383 if (app_context->callback->language_changed != NULL)
385 appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, app_appcore_lang_changed, app_context);
388 if (app_context->callback->region_format_changed != NULL)
390 appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, app_appcore_region_changed, app_context);
394 void app_unset_appcore_event_cb(void)
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);