2 * Copyright (c) 2019 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 <dali/internal/adaptor/common/framework.h>
22 #include <appcore_ui_base.h>
23 #include <app_control_internal.h>
24 #include <app_common.h>
26 #include <dali/internal/system/linux/dali-ecore.h>
28 #include <system_info.h>
29 #include <system_settings.h>
30 #include <bundle_internal.h>
31 #include <widget_base.h>
32 // CONDITIONAL INCLUDES
33 #ifdef APPCORE_WATCH_AVAILABLE
34 #include <appcore-watch/watch_app.h>
36 #ifdef DALI_ELDBUS_AVAILABLE
38 #endif // DALI_ELDBUS_AVAILABLE
40 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
41 #include <tzplatform_config.h>
42 #endif // TIZEN_PLATFORM_CONFIG_SUPPORTED
44 #include <dali/integration-api/debug.h>
47 #include <dali/internal/system/common/callback-manager.h>
60 #if defined(DEBUG_ENABLED)
61 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS" );
64 bool IsWidgetFeatureEnabled()
66 static bool feature = false;
67 static bool retrieved = false;
75 ret = system_info_get_platform_bool("http://tizen.org/feature/shell.appwidget", &feature);
76 if(ret != SYSTEM_INFO_ERROR_NONE)
78 DALI_LOG_ERROR("failed to get system info");
86 } // anonymous namespace
93 LOW_MEMORY, //< The low memory event
94 LOW_BATTERY, //< The low battery event
95 LANGUAGE_CHANGED, //< The system language changed event
96 DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
97 REGION_FORMAT_CHANGED, //< The region format changed event
98 SUSPENDED_STATE_CHANGED, //< The suspended state changed event of the application
99 UPDATE_REQUESTED, //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
102 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
104 [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
105 [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
106 [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
107 [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
108 [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
109 [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
118 typedef struct AppEventInfo *AppEventInfoPtr;
120 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void *userData);
122 struct AppEventHandler
130 typedef struct AppEventHandler *AppEventHandlerPtr;
132 int EventCallback(void *event, void *data)
134 AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
136 struct AppEventInfo appEvent;
138 appEvent.type = handler->type;
139 appEvent.value = event;
142 handler->cb(&appEvent, handler->data);
147 int AppAddEventHandler(AppEventHandlerPtr *eventHandler, AppEventType eventType, AppEventCallback callback, void *userData)
149 AppEventHandlerPtr handler;
151 handler = static_cast<AppEventHandlerPtr>( calloc(1, sizeof(struct AppEventHandler)) );
154 DALI_LOG_ERROR( "failed to create handler" );
155 return TIZEN_ERROR_UNKNOWN;
159 handler->type = eventType;
160 handler->cb = callback;
161 handler->data = userData;
162 handler->raw = appcore_base_add_event( static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
164 *eventHandler = handler;
166 return TIZEN_ERROR_NONE;
170 } // namespace Appcore
173 * Impl to hide EFL data members
175 struct Framework::Impl
178 Impl(void* data, Type type )
179 : mAbortCallBack( NULL ),
180 mCallbackManager( NULL )
181 #ifdef APPCORE_WATCH_AVAILABLE
185 mFramework = static_cast<Framework*>(data);
187 #ifndef APPCORE_WATCH_AVAILABLE
190 throw Dali::DaliException( "", "Watch Application is not supported." );
193 mApplicationType = type;
194 mCallbackManager = CallbackManager::New();
196 char* region = nullptr;
197 char* language = nullptr;
198 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, ®ion );
199 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &language );
201 if (region != nullptr)
203 mRegion = std::string( region );
206 if ( language != nullptr)
208 mLanguage = std::string( language );
214 delete mAbortCallBack;
216 // we're quiting the main loop so
217 // mCallbackManager->RemoveAllCallBacks() does not need to be called
218 // to delete our abort handler
219 delete mCallbackManager;
226 if (mApplicationType == NORMAL)
228 ret = AppNormalMain();
230 else if(mApplicationType == WIDGET)
232 ret = AppWidgetMain();
236 ret = AppWatchMain();
243 if (mApplicationType == NORMAL)
247 else if(mApplicationType == WIDGET)
257 void SetLanguage( const std::string& language )
259 mLanguage = language;
262 void SetRegion( const std::string& region )
267 std::string GetLanguage() const
272 std::string GetRegion() const
278 Type mApplicationType;
279 CallbackBase* mAbortCallBack;
280 CallbackManager *mCallbackManager;
281 std::string mLanguage;
284 Framework* mFramework;
285 AppCore::AppEventHandlerPtr handlers[5];
286 #ifdef APPCORE_WATCH_AVAILABLE
287 watch_app_lifecycle_callback_s mWatchCallback;
288 app_event_handler_h watchHandlers[5];
291 static int AppCreate(void *data)
293 appcore_ui_base_on_create();
294 return static_cast<int>( static_cast<Framework*>(data)->Create() );
297 static int AppTerminate(void *data)
299 appcore_ui_base_on_terminate();
300 Observer *observer = &static_cast<Framework*>(data)->mObserver;
302 observer->OnTerminate();
307 static int AppPause(void *data)
309 appcore_ui_base_on_pause();
310 Observer *observer = &static_cast<Framework*>(data)->mObserver;
317 static int AppResume(void *data)
319 appcore_ui_base_on_resume();
320 Observer *observer = &static_cast<Framework*>(data)->mObserver;
322 observer->OnResume();
327 static void ProcessBundle(Framework* framework, bundle *bundleData)
329 if(bundleData == NULL)
335 char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
336 if(bundleName != NULL)
338 framework->SetBundleName(bundleName);
342 char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
345 framework->SetBundleId(bundleId);
350 * Called by AppCore when the application is launched from another module (e.g. homescreen).
351 * @param[in] b the bundle data which the launcher module sent
353 static int AppControl(bundle* bundleData, void *data)
355 app_control_h appControl = NULL;
357 appcore_ui_base_on_control(bundleData);
361 if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
363 DALI_LOG_ERROR("Failed to create an app_control handle");
368 if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
370 DALI_LOG_ERROR("Failed to create an app_control handle");
374 Framework* framework = static_cast<Framework*>(data);
375 Observer *observer = &framework->mObserver;
377 ProcessBundle(framework, bundleData);
380 observer->OnAppControl(appControl);
382 app_control_destroy(appControl);
387 static void AppInit(int argc, char **argv, void *data)
389 #pragma GCC diagnostic push
390 #pragma GCC diagnostic ignored "-Wold-style-cast"
393 ecore_app_args_set( argc, (const char **)argv );
395 #pragma GCC diagnostic pop
398 static void AppFinish(void)
402 if(getenv("AUL_LOADER_INIT"))
404 unsetenv("AUL_LOADER_INIT");
409 static void AppRun(void *data)
411 ecore_main_loop_begin();
414 static void AppExit(void *data)
416 ecore_main_loop_quit();
419 static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
421 Framework* framework = static_cast<Framework*>(data);
422 Observer *observer = &framework->mObserver;
424 if( event && event->value )
426 framework->SetLanguage( std::string( static_cast<const char *>(event->value) ) );
427 observer->OnLanguageChanged();
431 DALI_LOG_ERROR( "NULL pointer in Language changed event\n" );
435 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
439 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
441 Framework* framework = static_cast<Framework*>(data);
442 Observer *observer = &framework->mObserver;
444 if( event && event->value )
446 framework->SetRegion( std::string( static_cast<const char *>(event->value) ) );
447 observer->OnRegionChanged();
451 DALI_LOG_ERROR( "NULL pointer in Region changed event\n" );
455 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
457 Observer *observer = &static_cast<Framework*>(data)->mObserver;
458 int status = *static_cast<int *>(event->value);
459 Dali::DeviceStatus::Battery::Status result = Dali::DeviceStatus::Battery::NORMAL;
461 // convert to dali battery status
466 result = Dali::DeviceStatus::Battery::POWER_OFF;
471 result = Dali::DeviceStatus::Battery::CRITICALLY_LOW;
477 observer->OnBatteryLow(result);
480 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
482 Observer *observer = &static_cast<Framework*>(data)->mObserver;
483 int status = *static_cast<int *>(event->value);
484 Dali::DeviceStatus::Memory::Status result = Dali::DeviceStatus::Memory::NORMAL;
486 // convert to dali memmory status
491 result = Dali::DeviceStatus::Memory::NORMAL;
496 result = Dali::DeviceStatus::Memory::LOW;
501 result = Dali::DeviceStatus::Memory::CRITICALLY_LOW;
507 observer->OnMemoryLow(result);
515 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
516 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
517 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
518 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
519 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
521 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
523 /* override methods */
524 ops.base.create = AppCreate;
525 ops.base.control = AppControl;
526 ops.base.terminate = AppTerminate;
527 ops.pause = AppPause;
528 ops.resume = AppResume;
529 ops.base.init = AppInit;
530 ops.base.finish = AppFinish;
531 ops.base.run = AppRun;
532 ops.base.exit = AppExit;
534 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
535 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
536 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
537 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
538 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
540 if (ret != TIZEN_ERROR_NONE)
543 appcore_ui_base_fini();
545 return TIZEN_ERROR_NONE;
550 appcore_ui_base_exit();
558 static int WidgetAppCreate( void *data )
560 widget_base_on_create();
561 return static_cast<int>( static_cast<Framework*>(data)->Create() );
564 static int WidgetAppTerminate( void *data )
566 Observer *observer = &static_cast<Framework*>(data)->mObserver;
567 observer->OnTerminate();
569 widget_base_on_terminate();
575 if( !IsWidgetFeatureEnabled() )
577 DALI_LOG_ERROR("widget feature is not supported");
581 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
582 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
583 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
584 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
585 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
587 widget_base_ops ops = widget_base_get_default_ops();
589 /* override methods */
590 ops.create = WidgetAppCreate;
591 ops.terminate = WidgetAppTerminate;
593 ops.finish = AppFinish;
597 int result = widget_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework);
604 #ifdef APPCORE_WATCH_AVAILABLE
605 static bool WatchAppCreate(int width, int height, void *data)
607 return static_cast<Framework*>(data)->Create();
610 static void WatchAppTimeTick(watch_time_h time, void *data)
612 Observer *observer = &static_cast<Framework*>(data)->mObserver;
613 WatchTime curTime(time);
615 observer->OnTimeTick(curTime);
618 static void WatchAppAmbientTick(watch_time_h time, void *data)
620 Observer *observer = &static_cast<Framework*>(data)->mObserver;
621 WatchTime curTime(time);
623 observer->OnAmbientTick(curTime);
626 static void WatchAppAmbientChanged(bool ambient, void *data)
628 Observer *observer = &static_cast<Framework*>(data)->mObserver;
630 observer->OnAmbientChanged(ambient);
633 static void WatchAppControl(app_control_h app_control, void *data)
635 Framework* framework = static_cast<Framework*>(data);
636 Observer *observer = &framework->mObserver;
637 bundle *bundleData = NULL;
639 app_control_to_bundle(app_control, &bundleData);
640 ProcessBundle(framework, bundleData);
643 observer->OnAppControl(app_control);
646 static void WatchAppTerminate(void *data)
648 Observer *observer = &static_cast<Framework*>(data)->mObserver;
650 observer->OnTerminate();
653 static void WatchAppPause(void *data)
655 Observer *observer = &static_cast<Framework*>(data)->mObserver;
660 static void WatchAppResume(void *data)
662 Observer *observer = &static_cast<Framework*>(data)->mObserver;
664 observer->OnResume();
673 #ifdef APPCORE_WATCH_AVAILABLE
674 mWatchCallback.create = WatchAppCreate;
675 mWatchCallback.app_control = WatchAppControl;
676 mWatchCallback.terminate = WatchAppTerminate;
677 mWatchCallback.pause = WatchAppPause;
678 mWatchCallback.resume = WatchAppResume;
679 mWatchCallback.time_tick = WatchAppTimeTick;
680 mWatchCallback.ambient_tick = WatchAppAmbientTick;
681 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
683 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
684 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
685 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
686 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
688 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
695 #ifdef APPCORE_WATCH_AVAILABLE
702 Impl( const Impl& impl );
705 Impl& operator=( const Impl& impl );
708 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
709 : mObserver(observer),
717 mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
720 bool featureFlag = true;
721 system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
723 if( featureFlag == false )
725 set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
727 #ifdef DALI_ELDBUS_AVAILABLE
728 // Initialize ElDBus.
729 DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
734 mImpl = new Impl(this, type);
737 Framework::~Framework()
744 #ifdef DALI_ELDBUS_AVAILABLE
746 DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
753 bool Framework::Create()
760 void Framework::Run()
765 ret = mImpl->AppMain();
766 if (ret != APP_ERROR_NONE)
768 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
773 void Framework::Quit()
778 bool Framework::IsMainLoopRunning()
783 void Framework::AddAbortCallback( CallbackBase* callback )
785 mImpl->mAbortCallBack = callback;
788 std::string Framework::GetBundleName() const
793 void Framework::SetBundleName(const std::string& name)
798 std::string Framework::GetBundleId() const
803 std::string Framework::GetResourcePath()
805 std::string resourcePath = "";
806 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
807 char* app_rsc_path = app_get_resource_path();
810 resourcePath = app_rsc_path;
813 #else // For backwards compatibility with older Tizen versions
815 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
816 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
817 char* value = getenv( environmentVariable );
820 resourcePath = value;
823 if( resourcePath.back() != '/' )
828 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
833 std::string Framework::GetDataPath()
836 char* dataPath = app_get_data_path();
845 void Framework::SetBundleId(const std::string& id)
850 void Framework::AbortCallback( )
852 // if an abort call back has been installed run it.
853 if (mImpl->mAbortCallBack)
855 CallbackBase::Execute( *mImpl->mAbortCallBack );
863 void Framework::InitThreads()
867 void Framework::SetLanguage( const std::string& language )
869 mImpl->SetLanguage( language );
872 void Framework::SetRegion( const std::string& region )
874 mImpl->SetRegion( region );
877 std::string Framework::GetLanguage() const
879 return mImpl->GetLanguage();
882 std::string Framework::GetRegion() const
884 return mImpl->GetRegion();
887 } // namespace Adaptor
889 } // namespace Internal