2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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.
19 #include "framework.h"
22 #include <appcore_ui_base.h>
23 #include <app_control_internal.h>
24 #include <app_common.h>
28 #include <system_info.h>
29 #include <bundle_internal.h>
31 // CONDITIONAL INCLUDES
32 #ifdef APPCORE_WATCH_AVAILABLE
33 #include <appcore-watch/watch_app.h>
35 #ifdef DALI_ELDBUS_AVAILABLE
37 #endif // DALI_ELDBUS_AVAILABLE
39 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
40 #include <tzplatform_config.h>
41 #endif // TIZEN_PLATFORM_CONFIG_SUPPORTED
43 #include <dali/integration-api/debug.h>
46 #include <callback-manager.h>
57 #if defined(DEBUG_ENABLED)
60 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS" );
61 } // anonymous namespace
69 LOW_MEMORY, //< The low memory event
70 LOW_BATTERY, //< The low battery event
71 LANGUAGE_CHANGED, //< The system language changed event
72 DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
73 REGION_FORMAT_CHANGED, //< The region format changed event
74 SUSPENDED_STATE_CHANGED, //< The suspended state changed event of the application
75 UPDATE_REQUESTED, //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
78 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
80 [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
81 [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
82 [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
83 [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
84 [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
85 [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
94 typedef struct AppEventInfo *AppEventInfoPtr;
96 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void *userData);
98 struct AppEventHandler
106 typedef struct AppEventHandler *AppEventHandlerPtr;
108 int EventCallback(void *event, void *data)
110 AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
112 struct AppEventInfo appEvent;
114 appEvent.type = handler->type;
115 appEvent.value = event;
118 handler->cb(&appEvent, handler->data);
123 int AppAddEventHandler(AppEventHandlerPtr *eventHandler, AppEventType eventType, AppEventCallback callback, void *userData)
125 AppEventHandlerPtr handler;
127 handler = static_cast<AppEventHandlerPtr>( calloc(1, sizeof(struct AppEventHandler)) );
130 DALI_LOG_ERROR( "failed to create handler" );
131 return TIZEN_ERROR_UNKNOWN;
135 handler->type = eventType;
136 handler->cb = callback;
137 handler->data = userData;
138 handler->raw = appcore_base_add_event( static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
140 *eventHandler = handler;
142 return TIZEN_ERROR_NONE;
146 } // namespace Appcore
149 * Impl to hide EFL data members
151 struct Framework::Impl
154 Impl(void* data, Type type )
155 : mAbortCallBack( NULL ),
156 mCallbackManager( NULL )
157 #ifdef APPCORE_WATCH_AVAILABLE
161 mFramework = static_cast<Framework*>(data);
163 #ifndef APPCORE_WATCH_AVAILABLE
166 throw Dali::DaliException( "", "Watch Application is not supported." );
169 mApplicationType = type;
170 mCallbackManager = CallbackManager::New();
175 delete mAbortCallBack;
177 // we're quiting the main loop so
178 // mCallbackManager->RemoveAllCallBacks() does not need to be called
179 // to delete our abort handler
180 delete mCallbackManager;
187 if (mApplicationType == NORMAL)
189 ret = AppNormalMain();
193 ret = AppWatchMain();
200 if (mApplicationType == NORMAL)
212 Type mApplicationType;
213 CallbackBase* mAbortCallBack;
214 CallbackManager *mCallbackManager;
216 Framework* mFramework;
217 AppCore::AppEventHandlerPtr handlers[5];
218 #ifdef APPCORE_WATCH_AVAILABLE
219 watch_app_lifecycle_callback_s mWatchCallback;
220 app_event_handler_h watchHandlers[5];
223 static int AppCreate(void *data)
225 appcore_ui_base_on_create();
226 return static_cast<int>( static_cast<Framework*>(data)->Create() );
229 static int AppTerminate(void *data)
231 appcore_ui_base_on_terminate();
232 Observer *observer = &static_cast<Framework*>(data)->mObserver;
234 observer->OnTerminate();
239 static int AppPause(void *data)
241 appcore_ui_base_on_pause();
242 Observer *observer = &static_cast<Framework*>(data)->mObserver;
249 static int AppResume(void *data)
251 appcore_ui_base_on_resume();
252 Observer *observer = &static_cast<Framework*>(data)->mObserver;
254 observer->OnResume();
259 static void ProcessBundle(Framework* framework, bundle *bundleData)
261 if(bundleData == NULL)
267 char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
268 if(bundleName != NULL)
270 framework->SetBundleName(bundleName);
274 char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
277 framework->SetBundleId(bundleId);
282 * Called by AppCore when the application is launched from another module (e.g. homescreen).
283 * @param[in] b the bundle data which the launcher module sent
285 static int AppControl(bundle* bundleData, void *data)
287 app_control_h appControl = NULL;
289 appcore_ui_base_on_control(bundleData);
293 if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
295 DALI_LOG_ERROR("Failed to create an app_control handle");
300 if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
302 DALI_LOG_ERROR("Failed to create an app_control handle");
306 Framework* framework = static_cast<Framework*>(data);
307 Observer *observer = &framework->mObserver;
309 ProcessBundle(framework, bundleData);
312 observer->OnAppControl(appControl);
314 app_control_destroy(appControl);
319 static int AppInit(int argc, char **argv, void *data)
321 #pragma GCC diagnostic push
322 #pragma GCC diagnostic ignored "-Wold-style-cast"
325 ecore_app_args_set( argc, (const char **)argv );
327 #pragma GCC diagnostic pop
331 static void AppFinish(void)
335 if(getenv("AUL_LOADER_INIT"))
337 unsetenv("AUL_LOADER_INIT");
342 static void AppRun(void *data)
344 ecore_main_loop_begin();
347 static void AppExit(void *data)
349 ecore_main_loop_quit();
352 static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
354 Observer *observer = &static_cast<Framework*>(data)->mObserver;
356 observer->OnLanguageChanged();
359 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
363 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
365 Observer *observer = &static_cast<Framework*>(data)->mObserver;
367 observer->OnRegionChanged();
370 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
372 Observer *observer = &static_cast<Framework*>(data)->mObserver;
374 observer->OnBatteryLow();
377 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
379 Observer *observer = &static_cast<Framework*>(data)->mObserver;
381 observer->OnMemoryLow();
389 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
390 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
391 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
392 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
393 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
395 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
397 /* override methods */
398 ops.base.create = AppCreate;
399 ops.base.control = AppControl;
400 ops.base.terminate = AppTerminate;
401 ops.pause = AppPause;
402 ops.resume = AppResume;
403 ops.base.init = AppInit;
404 ops.base.finish = AppFinish;
405 ops.base.run = AppRun;
406 ops.base.exit = AppExit;
408 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
409 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
410 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
411 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
412 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
414 if (ret != TIZEN_ERROR_NONE)
417 appcore_ui_base_fini();
419 return TIZEN_ERROR_NONE;
424 appcore_ui_base_exit();
427 #ifdef APPCORE_WATCH_AVAILABLE
428 static bool WatchAppCreate(int width, int height, void *data)
430 return static_cast<Framework*>(data)->Create();
433 static void WatchAppTimeTick(watch_time_h time, void *data)
435 Observer *observer = &static_cast<Framework*>(data)->mObserver;
436 WatchTime curTime(time);
438 observer->OnTimeTick(curTime);
441 static void WatchAppAmbientTick(watch_time_h time, void *data)
443 Observer *observer = &static_cast<Framework*>(data)->mObserver;
444 WatchTime curTime(time);
446 observer->OnAmbientTick(curTime);
449 static void WatchAppAmbientChanged(bool ambient, void *data)
451 Observer *observer = &static_cast<Framework*>(data)->mObserver;
453 observer->OnAmbientChanged(ambient);
456 static void WatchAppLanguageChanged(app_event_info_h event, void *data)
458 Observer *observer = &static_cast<Framework*>(data)->mObserver;
460 observer->OnLanguageChanged();
463 static void WatchAppRegionChanged(app_event_info_h event, void *data)
465 Observer *observer = &static_cast<Framework*>(data)->mObserver;
467 observer->OnRegionChanged();
470 static void WatchAppBatteryLow(app_event_info_h event, void *data)
472 Observer *observer = &static_cast<Framework*>(data)->mObserver;
474 observer->OnBatteryLow();
477 static void WatchAppMemoryLow(app_event_info_h event, void *data)
479 Observer *observer = &static_cast<Framework*>(data)->mObserver;
481 observer->OnMemoryLow();
484 static void WatchAppControl(app_control_h app_control, void *data)
486 Framework* framework = static_cast<Framework*>(data);
487 Observer *observer = &framework->mObserver;
488 bundle *bundleData = NULL;
490 app_control_to_bundle(app_control, &bundleData);
491 ProcessBundle(framework, bundleData);
494 observer->OnAppControl(app_control);
497 static void WatchAppTerminate(void *data)
499 Observer *observer = &static_cast<Framework*>(data)->mObserver;
501 observer->OnTerminate();
504 static void WatchAppPause(void *data)
506 Observer *observer = &static_cast<Framework*>(data)->mObserver;
511 static void WatchAppResume(void *data)
513 Observer *observer = &static_cast<Framework*>(data)->mObserver;
515 observer->OnResume();
524 #ifdef APPCORE_WATCH_AVAILABLE
525 mWatchCallback.create = WatchAppCreate;
526 mWatchCallback.app_control = WatchAppControl;
527 mWatchCallback.terminate = WatchAppTerminate;
528 mWatchCallback.pause = WatchAppPause;
529 mWatchCallback.resume = WatchAppResume;
530 mWatchCallback.time_tick = WatchAppTimeTick;
531 mWatchCallback.ambient_tick = WatchAppAmbientTick;
532 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
534 watch_app_add_event_handler(&watchHandlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, WatchAppBatteryLow, mFramework);
535 watch_app_add_event_handler(&watchHandlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, WatchAppMemoryLow, mFramework);
536 watch_app_add_event_handler(&watchHandlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, WatchAppLanguageChanged, mFramework);
537 watch_app_add_event_handler(&watchHandlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, WatchAppRegionChanged, mFramework);
539 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
546 #ifdef APPCORE_WATCH_AVAILABLE
554 Impl( const Impl& impl );
557 Impl& operator=( const Impl& impl );
560 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
561 : mObserver(observer),
568 mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
571 bool featureFlag = true;
572 system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
574 if( featureFlag == false )
576 set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
578 #ifdef DALI_ELDBUS_AVAILABLE
579 // Initialize ElDBus.
580 DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
585 mImpl = new Impl(this, type);
588 Framework::~Framework()
595 #ifdef DALI_ELDBUS_AVAILABLE
597 DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
604 bool Framework::Create()
611 void Framework::Run()
616 ret = mImpl->AppMain();
617 if (ret != APP_ERROR_NONE)
619 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
624 void Framework::Quit()
629 bool Framework::IsMainLoopRunning()
634 void Framework::AddAbortCallback( CallbackBase* callback )
636 mImpl->mAbortCallBack = callback;
639 std::string Framework::GetBundleName() const
644 void Framework::SetBundleName(const std::string& name)
649 std::string Framework::GetBundleId() const
654 std::string Framework::GetResourcePath()
656 std::string resourcePath = "";
657 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
658 resourcePath = app_get_resource_path();
659 #else // For backwards compatibility with older Tizen versions
661 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
662 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
663 char* value = getenv( environmentVariable );
666 resourcePath = value;
668 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
673 void Framework::SetBundleId(const std::string& id)
678 void Framework::AbortCallback( )
680 // if an abort call back has been installed run it.
681 if (mImpl->mAbortCallBack)
683 CallbackBase::Execute( *mImpl->mAbortCallBack );
691 } // namespace Adaptor
693 } // namespace Internal