2 * Copyright (c) 2011 - 2016 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.
21 #include <sys/types.h>
26 #include <appcore_efl_base.h>
29 #include <app_internal.h>
30 #include <app_control_internal.h>
31 #include <app_common_internal.h>
32 #include <tizen_error.h>
34 #include "app_extension.h"
40 #define LOG_TAG "CAPI_APPFW_APPLICATION"
42 #define UI_APP_EVENT_MAX 7
45 APP_STATE_NOT_RUNNING, /* The application has been launched or was running but was terminated */
46 APP_STATE_CREATING, /* The application is initializing the resources on app_create_cb callback */
47 APP_STATE_RUNNING, /* The application is running in the foreground and background */
50 struct app_event_handler {
51 app_event_type_e type;
57 struct app_event_info {
58 app_event_type_e type;
62 struct ui_app_context {
63 ui_app_lifecycle_callback_s callback;
67 static struct ui_app_context __context;
68 static app_state_e __app_state = APP_STATE_NOT_RUNNING;
70 static int __app_event_converter[APPCORE_BASE_EVENT_MAX] = {
71 [APP_EVENT_LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
72 [APP_EVENT_LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
73 [APP_EVENT_LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
74 [APP_EVENT_DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
75 [APP_EVENT_REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
76 [APP_EVENT_SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
77 [APP_EVENT_UPDATE_REQUESTED] = APPCORE_BASE_EVENT_UPDATE_REQUESTED,
80 static int __ui_app_create(void *data)
82 appcore_efl_base_on_create();
83 if (__context.callback.create == NULL ||
84 __context.callback.create(__context.data) == false)
85 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "app_create_cb() returns false");
87 return APP_ERROR_NONE;
90 static int __ui_app_terminate(void *data)
92 appcore_efl_base_on_terminate();
94 if (__context.callback.terminate)
95 __context.callback.terminate(__context.data);
97 return APP_ERROR_NONE;
100 static int __ui_app_control(bundle *b, void *data)
102 app_control_h app_control = NULL;
104 appcore_efl_base_on_control(b);
107 if (app_control_create_event(b, &app_control) != APP_ERROR_NONE)
108 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "Failed to create an app_control handle");
110 if (app_control_create(&app_control) != APP_ERROR_NONE)
111 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, "Failed to create an app_control handle");
114 if (__context.callback.app_control)
115 __context.callback.app_control(app_control, __context.data);
117 app_control_destroy(app_control);
119 return APP_ERROR_NONE;
122 static int __ui_app_pause(void *data)
124 appcore_efl_base_on_pause();
125 if (__context.callback.pause)
126 __context.callback.pause(__context.data);
127 return APP_ERROR_NONE;
130 static int __ui_app_resume(void *data)
132 appcore_efl_base_on_resume();
133 if (__context.callback.resume)
134 __context.callback.resume(__context.data);
135 return APP_ERROR_NONE;
138 static int __app_init(int argc, char **argv, ui_app_lifecycle_callback_s *callback, void *user_data, appcore_efl_base_ops ops)
142 if (argc < 1 || argv == NULL || callback == NULL)
143 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
145 if (callback->create == NULL)
146 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "app_create_cb() callback must be registered");
148 if (__app_state != APP_STATE_NOT_RUNNING)
149 return app_error(APP_ERROR_ALREADY_RUNNING, __FUNCTION__, NULL);
151 __context.callback = *callback;
152 __context.data = user_data;
153 __app_state = APP_STATE_CREATING;
155 ret = appcore_efl_base_init(ops, argc, argv, NULL,
156 APPCORE_EFL_BASE_HINT_WINDOW_GROUP_CONTROL |
157 APPCORE_EFL_BASE_HINT_WINDOW_STACK_CONTROL |
158 APPCORE_EFL_BASE_HINT_BG_LAUNCH_CONTROL |
159 APPCORE_EFL_BASE_HINT_HW_ACC_CONTROL |
160 APPCORE_EFL_BASE_HINT_WINDOW_AUTO_CONTROL |
161 APPCORE_EFL_BASE_HINT_LEGACY_CONTROL);
164 __app_state = APP_STATE_NOT_RUNNING;
165 return app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);
168 return APP_ERROR_NONE;
171 static void __app_fini(void)
173 appcore_efl_base_fini();
174 __app_state = APP_STATE_NOT_RUNNING;
178 int ui_app_init(int argc, char **argv, ui_app_lifecycle_callback_s *callback, void *user_data, appcore_context_h *handle)
180 appcore_efl_base_ops ops = appcore_efl_base_get_default_ops();
182 /* override methods */
183 ops.ui_base.base.create = __ui_app_create;
184 ops.ui_base.base.control = __ui_app_control;
185 ops.ui_base.base.terminate = __ui_app_terminate;
186 ops.ui_base.pause = __ui_app_pause;
187 ops.ui_base.resume = __ui_app_resume;
188 ops.ui_base.base.run = NULL;
189 ops.ui_base.base.exit = NULL;
191 return __app_init(argc, argv, callback, user_data, ops);
194 void ui_app_fini(appcore_context_h handle)
199 int ui_app_main(int argc, char **argv, ui_app_lifecycle_callback_s *callback, void *user_data)
202 appcore_efl_base_ops ops = appcore_efl_base_get_default_ops();
204 /* override methods */
205 ops.ui_base.base.create = __ui_app_create;
206 ops.ui_base.base.control = __ui_app_control;
207 ops.ui_base.base.terminate = __ui_app_terminate;
208 ops.ui_base.pause = __ui_app_pause;
209 ops.ui_base.resume = __ui_app_resume;
211 ret = __app_init(argc, argv, callback, user_data, ops);
212 if (ret != APP_ERROR_NONE)
217 return APP_ERROR_NONE;
220 void ui_app_exit(void)
222 appcore_efl_base_exit();
225 int __event_cb(void *event, void *data)
227 app_event_handler_h handler = data;
229 struct app_event_info app_event;
231 app_event.type = handler->type;
232 app_event.value = event;
235 handler->cb(&app_event, handler->data);
240 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)
242 app_event_handler_h handler;
244 if (event_handler == NULL || callback == NULL)
245 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");
247 if (event_type < APP_EVENT_LOW_MEMORY || event_type > APP_EVENT_UPDATE_REQUESTED)
248 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid event type");
250 handler = calloc(1, sizeof(struct app_event_handler));
252 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, "failed to create handler");
254 handler->type = event_type;
255 handler->cb = callback;
256 handler->data = user_data;
257 handler->raw = appcore_base_add_event(__app_event_converter[event_type], __event_cb, handler);
259 *event_handler = handler;
261 return APP_ERROR_NONE;
264 int ui_app_remove_event_handler(app_event_handler_h event_handler)
267 app_event_type_e type;
269 if (event_handler == NULL)
270 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "handler is null");
272 type = event_handler->type;
273 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_UPDATE_REQUESTED)
274 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid handler");
277 ret = appcore_base_remove_event(event_handler->raw);
279 return app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid raw handler");
283 return APP_ERROR_NONE;