2 * Copyright (c) 2022 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 <app_common.h>
23 #include <app_control_internal.h>
24 #include <appcore_ui_base.h>
26 #include <dali/internal/system/linux/dali-ecore.h>
28 #include <bundle_internal.h>
29 #include <system_info.h>
30 #include <system_settings.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 #ifdef COMPONENT_APPLICATION_SUPPORT
45 #include <component_based_app_base.h>
48 #include <dali/integration-api/debug.h>
49 #include <dali/integration-api/trace.h>
52 #include <dali/internal/system/common/callback-manager.h>
62 #if defined(DEBUG_ENABLED)
63 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS");
65 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_FRAMEWORK, true);
67 bool IsWidgetFeatureEnabled()
69 static bool feature = false;
70 static bool retrieved = false;
78 ret = system_info_get_platform_bool("http://tizen.org/feature/shell.appwidget", &feature);
79 if(ret != SYSTEM_INFO_ERROR_NONE)
81 DALI_LOG_ERROR("failed to get system info");
89 } // anonymous namespace
95 LOW_MEMORY, //< The low memory event
96 LOW_BATTERY, //< The low battery event
97 LANGUAGE_CHANGED, //< The system language changed event
98 DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
99 REGION_FORMAT_CHANGED, //< The region format changed event
100 SUSPENDED_STATE_CHANGED, //< The suspended state changed event of the application
101 UPDATE_REQUESTED, //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
104 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
106 [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
107 [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
108 [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
109 [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
110 [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
111 [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
120 typedef struct AppEventInfo* AppEventInfoPtr;
122 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void* userData);
124 struct AppEventHandler
132 typedef struct AppEventHandler* AppEventHandlerPtr;
134 int EventCallback(void* event, void* data)
136 AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
138 struct AppEventInfo appEvent;
140 appEvent.type = handler->type;
141 appEvent.value = event;
144 handler->cb(&appEvent, handler->data);
149 int AppAddEventHandler(AppEventHandlerPtr* eventHandler, AppEventType eventType, AppEventCallback callback, void* userData)
151 AppEventHandlerPtr handler;
153 handler = static_cast<AppEventHandlerPtr>(calloc(1, sizeof(struct AppEventHandler)));
156 DALI_LOG_ERROR("failed to create handler");
157 return TIZEN_ERROR_UNKNOWN;
161 handler->type = eventType;
162 handler->cb = callback;
163 handler->data = userData;
164 handler->raw = appcore_base_add_event(static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
166 *eventHandler = handler;
168 return TIZEN_ERROR_NONE;
172 } // namespace AppCore
175 * Impl to hide EFL data members
177 struct Framework::Impl
180 Impl(void* data, Type type)
181 : mAbortCallBack(NULL),
182 mCallbackManager(NULL)
183 #ifdef APPCORE_WATCH_AVAILABLE
188 mFramework = static_cast<Framework*>(data);
190 #ifndef APPCORE_WATCH_AVAILABLE
193 throw Dali::DaliException("", "Watch Application is not supported.");
196 mApplicationType = type;
197 mCallbackManager = CallbackManager::New();
199 char* region = nullptr;
200 char* language = nullptr;
201 system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, ®ion);
202 system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &language);
204 if(region != nullptr)
206 mRegion = std::string(region);
210 if(language != nullptr)
212 mLanguage = std::string(language);
219 delete mAbortCallBack;
221 // we're quiting the main loop so
222 // mCallbackManager->RemoveAllCallBacks() does not need to be called
223 // to delete our abort handler
224 delete mCallbackManager;
230 switch(mApplicationType)
234 ret = AppNormalMain();
239 ret = AppWidgetMain();
244 ret = AppWatchMain();
247 #ifdef COMPONENT_APPLICATION_SUPPORT
250 ret = AppComponentMain();
260 switch(mApplicationType)
277 #ifdef COMPONENT_APPLICATION_SUPPORT
287 void SetLanguage(const std::string& language)
289 mLanguage = language;
292 void SetRegion(const std::string& region)
297 std::string GetLanguage() const
302 std::string GetRegion() const
308 Type mApplicationType;
309 CallbackBase* mAbortCallBack;
310 CallbackManager* mCallbackManager;
311 std::string mLanguage;
314 Framework* mFramework;
315 AppCore::AppEventHandlerPtr handlers[5];
316 #ifdef APPCORE_WATCH_AVAILABLE
317 watch_app_lifecycle_callback_s mWatchCallback;
318 app_event_handler_h watchHandlers[5];
321 static int AppCreate(void* data)
323 appcore_ui_base_on_create();
324 return static_cast<int>(static_cast<Framework*>(data)->Create());
327 static int AppTerminate(void* data)
329 appcore_ui_base_on_terminate();
330 Observer* observer = &static_cast<Framework*>(data)->mObserver;
332 observer->OnTerminate();
337 static int AppPause(void* data)
339 appcore_ui_base_on_pause();
340 Observer* observer = &static_cast<Framework*>(data)->mObserver;
347 static int AppResume(void* data)
349 appcore_ui_base_on_resume();
350 Observer* observer = &static_cast<Framework*>(data)->mObserver;
352 observer->OnResume();
357 static void ProcessBundle(Framework* framework, bundle* bundleData)
359 if(bundleData == NULL)
365 char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
366 if(bundleName != NULL)
368 framework->SetBundleName(bundleName);
372 char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
375 framework->SetBundleId(bundleId);
380 * Called by AppCore when the application is launched from another module (e.g. homescreen).
381 * @param[in] b the bundle data which the launcher module sent
383 static int AppControl(bundle* bundleData, void* data)
385 app_control_h appControl = NULL;
387 appcore_ui_base_on_control(bundleData);
391 if(app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
393 DALI_LOG_ERROR("Failed to create an app_control handle");
398 if(app_control_create(&appControl) != TIZEN_ERROR_NONE)
400 DALI_LOG_ERROR("Failed to create an app_control handle");
404 Framework* framework = static_cast<Framework*>(data);
405 Observer* observer = &framework->mObserver;
407 ProcessBundle(framework, bundleData);
410 observer->OnAppControl(appControl);
412 app_control_destroy(appControl);
417 static void AppInit(int argc, char** argv, void* data)
419 #pragma GCC diagnostic push
420 #pragma GCC diagnostic ignored "-Wold-style-cast"
423 ecore_app_args_set(argc, (const char**)argv);
425 #pragma GCC diagnostic pop
428 static void AppFinish(void)
432 if(getenv("AUL_LOADER_INIT"))
434 setenv("AUL_LOADER_INIT", "0", 1);
439 static void AppRun(void* data)
441 ecore_main_loop_begin();
444 static void AppExit(void* data)
446 ecore_main_loop_quit();
449 static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void* data)
451 Framework* framework = static_cast<Framework*>(data);
452 Observer* observer = &framework->mObserver;
454 if(event && event->value)
456 framework->SetLanguage(std::string(static_cast<const char*>(event->value)));
457 observer->OnLanguageChanged();
461 DALI_LOG_ERROR("NULL pointer in Language changed event\n");
465 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void* data)
469 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void* data)
471 Framework* framework = static_cast<Framework*>(data);
472 Observer* observer = &framework->mObserver;
474 if(event && event->value)
476 framework->SetRegion(std::string(static_cast<const char*>(event->value)));
477 observer->OnRegionChanged();
481 DALI_LOG_ERROR("NULL pointer in Region changed event\n");
485 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void* data)
487 Observer* observer = &static_cast<Framework*>(data)->mObserver;
488 int status = *static_cast<int*>(event->value);
489 Dali::DeviceStatus::Battery::Status result = Dali::DeviceStatus::Battery::NORMAL;
491 // convert to dali battery status
496 result = Dali::DeviceStatus::Battery::POWER_OFF;
501 result = Dali::DeviceStatus::Battery::CRITICALLY_LOW;
507 observer->OnBatteryLow(result);
510 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void* data)
512 Observer* observer = &static_cast<Framework*>(data)->mObserver;
513 int status = *static_cast<int*>(event->value);
514 Dali::DeviceStatus::Memory::Status result = Dali::DeviceStatus::Memory::NORMAL;
516 // convert to dali memmory status
521 result = Dali::DeviceStatus::Memory::NORMAL;
526 result = Dali::DeviceStatus::Memory::LOW;
531 result = Dali::DeviceStatus::Memory::CRITICALLY_LOW;
537 observer->OnMemoryLow(result);
544 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
545 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
546 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
547 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
548 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
550 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
552 /* override methods */
553 ops.base.create = AppCreate;
554 ops.base.control = AppControl;
555 ops.base.terminate = AppTerminate;
556 ops.pause = AppPause;
557 ops.resume = AppResume;
558 ops.base.init = AppInit;
559 ops.base.finish = AppFinish;
560 ops.base.run = AppRun;
561 ops.base.exit = AppExit;
563 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL | APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL | APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL | APPCORE_UI_BASE_HINT_HW_ACC_CONTROL | APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL);
565 if(ret != TIZEN_ERROR_NONE)
568 appcore_ui_base_fini();
570 return TIZEN_ERROR_NONE;
575 appcore_ui_base_exit();
583 static int WidgetAppCreate(void* data)
585 widget_base_on_create();
586 return static_cast<int>(static_cast<Framework*>(data)->Create());
589 static int WidgetAppTerminate(void* data)
591 Observer* observer = &static_cast<Framework*>(data)->mObserver;
592 observer->OnTerminate();
594 widget_base_on_terminate();
600 if(!IsWidgetFeatureEnabled())
602 DALI_LOG_ERROR("widget feature is not supported");
606 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
607 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
608 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
609 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
610 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
612 widget_base_ops ops = widget_base_get_default_ops();
614 /* override methods */
615 ops.create = WidgetAppCreate;
616 ops.terminate = WidgetAppTerminate;
618 ops.finish = AppFinish;
622 int result = widget_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework);
629 #ifdef APPCORE_WATCH_AVAILABLE
630 static bool WatchAppCreate(int width, int height, void* data)
632 return static_cast<Framework*>(data)->Create();
635 static void WatchAppTimeTick(watch_time_h time, void* data)
637 Observer* observer = &static_cast<Framework*>(data)->mObserver;
638 WatchTime curTime(time);
640 observer->OnTimeTick(curTime);
643 static void WatchAppAmbientTick(watch_time_h time, void* data)
645 Observer* observer = &static_cast<Framework*>(data)->mObserver;
646 WatchTime curTime(time);
648 observer->OnAmbientTick(curTime);
651 static void WatchAppAmbientChanged(bool ambient, void* data)
653 Observer* observer = &static_cast<Framework*>(data)->mObserver;
655 observer->OnAmbientChanged(ambient);
658 static void WatchAppControl(app_control_h app_control, void* data)
660 Framework* framework = static_cast<Framework*>(data);
661 Observer* observer = &framework->mObserver;
662 bundle* bundleData = NULL;
664 app_control_to_bundle(app_control, &bundleData);
665 ProcessBundle(framework, bundleData);
668 observer->OnAppControl(app_control);
671 static void WatchAppTerminate(void* data)
673 Observer* observer = &static_cast<Framework*>(data)->mObserver;
675 observer->OnTerminate();
678 static void WatchAppPause(void* data)
680 Observer* observer = &static_cast<Framework*>(data)->mObserver;
685 static void WatchAppResume(void* data)
687 Observer* observer = &static_cast<Framework*>(data)->mObserver;
689 observer->OnResume();
698 #ifdef APPCORE_WATCH_AVAILABLE
699 mWatchCallback.create = WatchAppCreate;
700 mWatchCallback.app_control = WatchAppControl;
701 mWatchCallback.terminate = WatchAppTerminate;
702 mWatchCallback.pause = WatchAppPause;
703 mWatchCallback.resume = WatchAppResume;
704 mWatchCallback.time_tick = WatchAppTimeTick;
705 mWatchCallback.ambient_tick = WatchAppAmbientTick;
706 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
708 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
709 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
710 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
711 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
713 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
720 #ifdef APPCORE_WATCH_AVAILABLE
725 #ifdef COMPONENT_APPLICATION_SUPPORT
726 int AppComponentMain()
728 /*Crate component_based_app_base_lifecycle_callback*/
729 component_based_app_base_lifecycle_callback_s callback;
730 callback.init = AppInit;
731 callback.run = AppRun;
732 callback.exit = AppExit;
733 callback.create = ComponentAppCreate;
734 callback.terminate = ComponentAppTerminate;
735 callback.fini = ComponentAppFinish;
737 return component_based_app_base_main(*mFramework->mArgc, *mFramework->mArgv, &callback, mFramework);
741 static void* ComponentAppCreate(void* data)
743 Framework* framework = static_cast<Framework*>(data);
744 Observer* observer = &framework->mObserver;
747 return Dali::AnyCast<void*>(observer->OnCreate());
750 static void ComponentAppTerminate(void* data)
752 Observer* observer = &static_cast<Framework*>(data)->mObserver;
753 observer->OnTerminate();
756 static void ComponentAppFinish(void* data)
760 if(getenv("AUL_LOADER_INIT"))
762 setenv("AUL_LOADER_INIT", "0", 1);
767 void AppComponentExit()
769 component_based_app_base_exit();
776 Impl(const Impl& impl);
779 Impl& operator=(const Impl& impl);
782 Framework::Framework(Framework::Observer& observer, int* argc, char*** argv, Type type)
783 : mObserver(observer),
791 mAbortHandler(MakeCallback(this, &Framework::AbortCallback)),
794 bool featureFlag = true;
795 system_info_get_platform_bool("tizen.org/feature/opengles.version.2_0", &featureFlag);
797 if(featureFlag == false)
799 set_last_result(TIZEN_ERROR_NOT_SUPPORTED);
801 #ifdef DALI_ELDBUS_AVAILABLE
802 // Initialize ElDBus.
803 DALI_LOG_INFO(gDBusLogging, Debug::General, "Starting DBus Initialization\n");
808 mImpl = new Impl(this, type);
811 Framework::~Framework()
818 #ifdef DALI_ELDBUS_AVAILABLE
820 DALI_LOG_INFO(gDBusLogging, Debug::General, "Shutting down DBus\n");
827 bool Framework::Create()
834 void Framework::Run()
839 DALI_TRACE_BEGIN(gTraceFilter, "DALI_APPMAIN");
840 ret = mImpl->AppMain();
841 DALI_TRACE_END(gTraceFilter, "DALI_APPMAIN");
842 if(ret != APP_ERROR_NONE)
844 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
849 void Framework::Quit()
854 bool Framework::IsMainLoopRunning()
859 void Framework::AddAbortCallback(CallbackBase* callback)
861 mImpl->mAbortCallBack = callback;
864 std::string Framework::GetBundleName() const
869 void Framework::SetBundleName(const std::string& name)
874 std::string Framework::GetBundleId() const
879 std::string Framework::GetResourcePath()
881 std::string resourcePath = "";
882 #if defined(TIZEN_PLATFORM_CONFIG_SUPPORTED) && TIZEN_PLATFORM_CONFIG_SUPPORTED
883 char* app_rsc_path = app_get_resource_path();
886 resourcePath = app_rsc_path;
889 #else // For backwards compatibility with older Tizen versions
891 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
892 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
893 char* value = getenv(environmentVariable);
896 resourcePath = value;
899 if(resourcePath.back() != '/')
904 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
909 std::string Framework::GetDataPath()
912 char* dataPath = app_get_data_path();
921 void Framework::SetBundleId(const std::string& id)
926 void Framework::AbortCallback()
928 // if an abort call back has been installed run it.
929 if(mImpl->mAbortCallBack)
931 CallbackBase::Execute(*mImpl->mAbortCallBack);
939 void Framework::InitThreads()
943 void Framework::SetLanguage(const std::string& language)
945 mImpl->SetLanguage(language);
948 void Framework::SetRegion(const std::string& region)
950 mImpl->SetRegion(region);
953 std::string Framework::GetLanguage() const
955 return mImpl->GetLanguage();
958 std::string Framework::GetRegion() const
960 return mImpl->GetRegion();
963 } // namespace Adaptor
965 } // namespace Internal