diff options
author | Semun Lee <sm79.lee@samsung.com> | 2015-01-09 03:17:47 -0800 |
---|---|---|
committer | Gerrit Code Review <gerrit@review.vlan103.tizen.org> | 2015-01-09 03:17:47 -0800 |
commit | 0fd9f4615f6c95f76cad02828fe7b5bc8bd6e57d (patch) | |
tree | 173629b2301c83a49addb1f50cf1b81053a752f8 /src | |
parent | e3907ebfb0e5f2a9db74df2741402b5c84e05731 (diff) | |
parent | 001fb68b65c4f1c6b93569723c558f687a18f68b (diff) | |
download | application-0fd9f4615f6c95f76cad02828fe7b5bc8bd6e57d.tar.gz application-0fd9f4615f6c95f76cad02828fe7b5bc8bd6e57d.tar.bz2 application-0fd9f4615f6c95f76cad02828fe7b5bc8bd6e57d.zip |
Merge "Add new api for system event handling from tizen_2.3" into tizen
Diffstat (limited to 'src')
-rwxr-xr-x | src/app_device.c | 30 | ||||
-rwxr-xr-x | src/app_event.c | 155 | ||||
-rwxr-xr-x | src/app_main.c | 381 |
3 files changed, 526 insertions, 40 deletions
diff --git a/src/app_device.c b/src/app_device.c index 8b6215d..a28c3f9 100755 --- a/src/app_device.c +++ b/src/app_device.c @@ -38,36 +38,6 @@ #define LOG_TAG "CAPI_APPFW_APPLICATION" -app_device_orientation_e app_convert_appcore_rm(enum appcore_rm rm) -{ - app_device_orientation_e dev_orientation; - - switch (rm) - { - case APPCORE_RM_PORTRAIT_NORMAL: - dev_orientation = APP_DEVICE_ORIENTATION_0; - break; - - case APPCORE_RM_PORTRAIT_REVERSE: - dev_orientation = APP_DEVICE_ORIENTATION_180; - break; - - case APPCORE_RM_LANDSCAPE_NORMAL: - dev_orientation = APP_DEVICE_ORIENTATION_270; - break; - - case APPCORE_RM_LANDSCAPE_REVERSE: - dev_orientation = APP_DEVICE_ORIENTATION_90; - break; - - default: - dev_orientation = APP_DEVICE_ORIENTATION_0; - break; - } - - return dev_orientation; -} - app_device_orientation_e app_get_device_orientation(void) { enum appcore_rm rm; diff --git a/src/app_event.c b/src/app_event.c new file mode 100755 index 0000000..b07645f --- /dev/null +++ b/src/app_event.c @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string.h> + +#include <vconf-internal-keys.h> +#include <app.h> +#include <app_private.h> + +app_device_orientation_e app_convert_appcore_rm(enum appcore_rm rm) +{ + app_device_orientation_e dev_orientation; + + switch (rm) + { + case APPCORE_RM_PORTRAIT_NORMAL: + dev_orientation = APP_DEVICE_ORIENTATION_0; + break; + + case APPCORE_RM_PORTRAIT_REVERSE: + dev_orientation = APP_DEVICE_ORIENTATION_180; + break; + + case APPCORE_RM_LANDSCAPE_NORMAL: + dev_orientation = APP_DEVICE_ORIENTATION_270; + break; + + case APPCORE_RM_LANDSCAPE_REVERSE: + dev_orientation = APP_DEVICE_ORIENTATION_90; + break; + + default: + dev_orientation = APP_DEVICE_ORIENTATION_0; + break; + } + + return dev_orientation; +} + +static int _app_convert_low_memory(void *val) +{ + switch (*(int *)val) { + case VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL: + return APP_EVENT_LOW_MEMORY_NORMAL; + case VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING: + return APP_EVENT_LOW_MEMORY_SOFT_WARNING; + case VCONFKEY_SYSMAN_LOW_MEMORY_HARD_WARNING: + return APP_EVENT_LOW_MEMORY_HARD_WARNING; + default: + return -1; + } +} + +static int _app_convert_low_battery(void *val) +{ + switch (*(int *)val) { + case VCONFKEY_SYSMAN_BAT_POWER_OFF: + return APP_EVENT_LOW_BATTERY_POWER_OFF; + case VCONFKEY_SYSMAN_BAT_CRITICAL_LOW: + return APP_EVENT_LOW_BATTERY_CRITICAL_LOW; + default: + return -1; + } +} + +int app_event_get_low_memory_status(app_event_info_h event_info, app_event_low_memory_status_e *status) +{ + int ret; + + if (event_info == NULL || status == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter"); + + if (event_info->type != APP_EVENT_LOW_MEMORY) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "event type mismatching"); + + ret = _app_convert_low_memory(event_info->value); + if (ret < 0) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "invalid event info"); + + *status = ret; + + return APP_ERROR_NONE; +} + +int app_event_get_low_battery_status(app_event_info_h event_info, app_event_low_battery_status_e *status) +{ + int ret; + + if (event_info == NULL || status == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter"); + + if (event_info->type != APP_EVENT_LOW_BATTERY) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "event type mismatching"); + + ret = _app_convert_low_battery(event_info->value); + if (ret < 0) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "invalid event info"); + + *status = ret; + + return APP_ERROR_NONE; +} + +int app_event_get_language(app_event_info_h event_info, char **lang) +{ + if (event_info == NULL || lang == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter"); + + if (event_info->type != APP_EVENT_LANGUAGE_CHANGED) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "event type mismatching"); + + *lang = strdup(event_info->value); + + return APP_ERROR_NONE; +} + +int app_event_get_region_format(app_event_info_h event_info, char **region) +{ + if (event_info == NULL || region == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter"); + + if (event_info->type != APP_EVENT_REGION_FORMAT_CHANGED) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "event type mismatching"); + + *region = strdup(event_info->value); + + return APP_ERROR_NONE; +} + +int app_event_get_device_orientation(app_event_info_h event_info, app_device_orientation_e *orientation) +{ + if (event_info == NULL || orientation == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter"); + + if (event_info->type != APP_EVENT_DEVICE_ORIENTATION_CHANGED) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "event type mismatching"); + + *orientation = app_convert_appcore_rm(*(enum appcore_rm *)(event_info->value)); + + return APP_ERROR_NONE; +} + diff --git a/src/app_main.c b/src/app_main.c index 1b58f72..24079a2 100755 --- a/src/app_main.c +++ b/src/app_main.c @@ -62,11 +62,11 @@ static int app_appcore_resume(void *data); static int app_appcore_terminate(void *data); static int app_appcore_reset(bundle *appcore_bundle, void *data); -static int app_appcore_low_memory(void *data); -static int app_appcore_low_battery(void *data); -static int app_appcore_rotation_event(enum appcore_rm rm, void *data); -static int app_appcore_lang_changed(void *data); -static int app_appcore_region_changed(void *data); +static int app_appcore_low_memory(void *event, void *data); +static int app_appcore_low_battery(void *evnet, void *data); +static int app_appcore_rotation_event(void *event, enum appcore_rm rm, void *data); +static int app_appcore_lang_changed(void *evnet, void *data); +static int app_appcore_region_changed(void *event, void *data); static void app_set_appcore_event_cb(app_context_h app_context); static void app_unset_appcore_event_cb(void); @@ -260,7 +260,7 @@ int app_appcore_reset(bundle *appcore_bundle, void *data) } -int app_appcore_low_memory(void *data) +int app_appcore_low_memory(void *event_info, void *data) { app_context_h app_context = data; app_low_memory_cb low_memory_cb; @@ -280,7 +280,7 @@ int app_appcore_low_memory(void *data) return APP_ERROR_NONE; } -int app_appcore_low_battery(void *data) +int app_appcore_low_battery(void *event_info, void *data) { app_context_h app_context = data; app_low_battery_cb low_battery_cb; @@ -300,7 +300,7 @@ int app_appcore_low_battery(void *data) return APP_ERROR_NONE; } -int app_appcore_rotation_event(enum appcore_rm rm, void *data) +int app_appcore_rotation_event(void *event_info, enum appcore_rm rm, void *data) { app_context_h app_context = data; app_device_orientation_cb device_orientation_cb; @@ -324,7 +324,7 @@ int app_appcore_rotation_event(enum appcore_rm rm, void *data) return APP_ERROR_NONE; } -int app_appcore_lang_changed(void *data) +int app_appcore_lang_changed(void *event_info, void *data) { app_context_h app_context = data; app_language_changed_cb lang_changed_cb; @@ -344,7 +344,7 @@ int app_appcore_lang_changed(void *data) return APP_ERROR_NONE; } -int app_appcore_region_changed(void *data) +int app_appcore_region_changed(void *event_info, void *data) { app_context_h app_context = data; app_region_format_changed_cb region_changed_cb; @@ -401,3 +401,364 @@ void app_unset_appcore_event_cb(void) appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, NULL, NULL); appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, NULL, NULL); } + +#define UI_APP_EVENT_MAX 5 +static Eina_List *handler_list[UI_APP_EVENT_MAX] = {NULL, }; +static int _initialized = 0; + +struct ui_app_context { + char *package; + char *app_name; + app_state_e state; + ui_app_lifecycle_callback_s *callback; + void *data; +}; + +static void _free_handler_list(void) +{ + int i; + app_event_handler_h handler; + + for (i = 0; i < UI_APP_EVENT_MAX; i++) { + EINA_LIST_FREE(handler_list[i], handler) + free(handler); + } + + eina_shutdown(); +} + +static int _ui_app_appcore_low_memory(void *event_info, void *data) +{ + Eina_List *l; + app_event_handler_h handler; + struct app_event_info event; + + LOGI("_app_appcore_low_memory"); + + event.type = APP_EVENT_LOW_MEMORY; + event.value = event_info; + + EINA_LIST_FOREACH(handler_list[APP_EVENT_LOW_MEMORY], l, handler) { + handler->cb(&event, handler->data); + } + + return APP_ERROR_NONE; +} + +static int _ui_app_appcore_low_battery(void *event_info, void *data) +{ + Eina_List *l; + app_event_handler_h handler; + struct app_event_info event; + + LOGI("_ui_app_appcore_low_battery"); + + event.type = APP_EVENT_LOW_BATTERY; + event.value = event_info; + + EINA_LIST_FOREACH(handler_list[APP_EVENT_LOW_BATTERY], l, handler) { + handler->cb(&event, handler->data); + } + + return APP_ERROR_NONE; +} + +static int _ui_app_appcore_rotation_event(void *event_info, enum appcore_rm rm, void *data) +{ + Eina_List *l; + app_event_handler_h handler; + struct app_event_info event; + + LOGI("_ui_app_appcore_rotation_event"); + + event.type = APP_EVENT_DEVICE_ORIENTATION_CHANGED; + event.value = event_info; + + EINA_LIST_FOREACH(handler_list[APP_EVENT_DEVICE_ORIENTATION_CHANGED], l, handler) { + handler->cb(&event, handler->data); + } + + return APP_ERROR_NONE; +} + +static int _ui_app_appcore_lang_changed(void *event_info, void *data) +{ + Eina_List *l; + app_event_handler_h handler; + struct app_event_info event; + + LOGI("_ui_app_appcore_lang_changed"); + + event.type = APP_EVENT_LANGUAGE_CHANGED; + event.value = event_info; + + EINA_LIST_FOREACH(handler_list[APP_EVENT_LANGUAGE_CHANGED], l, handler) { + handler->cb(&event, handler->data); + } + + return APP_ERROR_NONE; +} + +static int _ui_app_appcore_region_changed(void *event_info, void *data) +{ + Eina_List *l; + app_event_handler_h handler; + struct app_event_info event; + + if (event_info == NULL) { + LOGI("receive empty event, ignore it"); + return APP_ERROR_NONE; + } + + LOGI("_ui_app_appcore_region_changed"); + + event.type = APP_EVENT_REGION_FORMAT_CHANGED; + event.value = event_info; + + EINA_LIST_FOREACH(handler_list[APP_EVENT_REGION_FORMAT_CHANGED], l, handler) { + handler->cb(&event, handler->data); + } + + return APP_ERROR_NONE; +} + + +static void _ui_app_set_appcore_event_cb(void) +{ + appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, _ui_app_appcore_low_memory, NULL); + appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, _ui_app_appcore_low_battery, NULL); + appcore_set_rotation_cb(_ui_app_appcore_rotation_event, NULL); + appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, _ui_app_appcore_lang_changed, NULL); + appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, _ui_app_appcore_region_changed, NULL); +} + +static void _ui_app_unset_appcore_event_cb(void) +{ + appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, NULL, NULL); + appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, NULL, NULL); + appcore_unset_rotation_cb(); + appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, NULL, NULL); + appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, NULL, NULL); +} + +static int _ui_app_appcore_create(void *data) +{ + LOGI("app_appcore_create"); + struct ui_app_context *app_context = data; + app_create_cb create_cb; + + if (app_context == NULL) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL); + + _ui_app_set_appcore_event_cb(); + + create_cb = app_context->callback->create; + + if (create_cb == NULL || create_cb(app_context->data) == false) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "app_create_cb() returns false"); + + app_context->state = APP_STATE_RUNNING; + + return APP_ERROR_NONE; +} + +static int _ui_app_appcore_terminate(void *data) +{ + LOGI("app_appcore_terminate"); + struct ui_app_context *app_context = data; + app_terminate_cb terminate_cb; + + if (app_context == NULL) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL); + + terminate_cb = app_context->callback->terminate; + + if (terminate_cb != NULL) + terminate_cb(app_context->data); + + _ui_app_unset_appcore_event_cb(); + + app_finalizer_execute(); + + if (_initialized) { + _free_handler_list(); + _initialized = 0; + } + + return APP_ERROR_NONE; +} + +static int _ui_app_appcore_pause(void *data) +{ + LOGI("app_appcore_pause"); + struct ui_app_context *app_context = data; + app_pause_cb pause_cb; + + if (app_context == NULL) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL); + + pause_cb = app_context->callback->pause; + + if (pause_cb != NULL) + pause_cb(app_context->data); + + return APP_ERROR_NONE; +} + +static int _ui_app_appcore_resume(void *data) +{ + LOGI("app_appcore_resume"); + struct ui_app_context *app_context = data; + app_resume_cb resume_cb; + + if (app_context == NULL) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL); + + resume_cb = app_context->callback->resume; + + if (resume_cb != NULL) + resume_cb(app_context->data); + + return APP_ERROR_NONE; +} + + +static int _ui_app_appcore_reset(bundle *appcore_bundle, void *data) +{ + LOGI("app_appcore_reset"); + struct ui_app_context *app_context = data; + app_service_cb callback; + service_h service; + + if (app_context == NULL) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL); + + if (service_create_event(appcore_bundle, &service) != APP_ERROR_NONE) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to create a service handle from the bundle"); + + callback = app_context->callback->service; + + if (callback != NULL) + callback(service, app_context->data); + + service_destroy(service); + + return APP_ERROR_NONE; +} + +int ui_app_main(int argc, char **argv, ui_app_lifecycle_callback_s *callback, void *user_data) +{ + struct ui_app_context app_context = { + .package = NULL, + .app_name = NULL, + .state = APP_STATE_NOT_RUNNING, + .callback = callback, + .data = user_data + }; + + struct appcore_ops appcore_context = { + .data = &app_context, + .create = _ui_app_appcore_create, + .terminate = _ui_app_appcore_terminate, + .pause = _ui_app_appcore_pause, + .resume = _ui_app_appcore_resume, + .reset = _ui_app_appcore_reset, + }; + + if (argc < 1 || argv == NULL || callback == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL); + + if (callback->create == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "app_create_cb() callback must be registered"); + + if (app_context.state != APP_STATE_NOT_RUNNING) + return app_error(APP_ERROR_ALREADY_RUNNING, __FUNCTION__, NULL); + + if (app_get_id(&(app_context.package)) != APP_ERROR_NONE) + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package"); + + if (app_get_package_app_name(app_context.package, &(app_context.app_name)) != APP_ERROR_NONE) { + free(app_context.package); + return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the package's app name"); + } + + app_context.state = APP_STATE_CREATING; + + LOGI("app_efl_main"); + appcore_efl_main(app_context.app_name, &argc, &argv, &appcore_context); + + free(app_context.package); + free(app_context.app_name); + + return APP_ERROR_NONE; +} + +void ui_app_exit(void) +{ + app_efl_exit(); +} + +int ui_app_add_event_handler(app_event_handler_h *event_handler, app_event_type_e event_type, app_event_cb callback, void *user_data) +{ + app_event_handler_h handler; + Eina_List *l_itr; + + if (!_initialized) { + eina_init(); + _initialized = 1; + } + + if (event_handler == NULL || callback == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter"); + + if (event_type < APP_EVENT_LOW_MEMORY || event_type > APP_EVENT_REGION_FORMAT_CHANGED) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid event type"); + + EINA_LIST_FOREACH(handler_list[event_type], l_itr, handler) { + if (handler->cb == callback) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "already registered"); + } + + handler = calloc(1, sizeof(struct app_event_handler)); + if (!handler) + return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, "failed to create handler"); + + handler->type = event_type; + handler->cb = callback; + handler->data = user_data; + handler_list[event_type] = eina_list_append(handler_list[event_type], handler); + + *event_handler = handler; + + return APP_ERROR_NONE; +} + +int ui_app_remove_event_handler(app_event_handler_h event_handler) +{ + app_event_handler_h handler; + app_event_type_e type; + Eina_List *l_itr; + Eina_List *l_next; + + if (event_handler == NULL) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "handler is null"); + + if (!_initialized) { + LOGI("handler list is not initialized"); + return APP_ERROR_NONE; + } + + type = event_handler->type; + if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED) + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid handler"); + + EINA_LIST_FOREACH_SAFE(handler_list[type], l_itr, l_next, handler) { + if (handler == event_handler) { + free(handler); + handler_list[type] = eina_list_remove_list(handler_list[type], l_itr); + return APP_ERROR_NONE; + } + } + + return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "cannot find such handler"); +} |