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 <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 <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();
198 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, ®ion );
199 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &language );
200 mRegion = std::string( region );
201 mLanguage = std::string( language );
206 delete mAbortCallBack;
208 // we're quiting the main loop so
209 // mCallbackManager->RemoveAllCallBacks() does not need to be called
210 // to delete our abort handler
211 delete mCallbackManager;
218 if (mApplicationType == NORMAL)
220 ret = AppNormalMain();
222 else if(mApplicationType == WIDGET)
224 ret = AppWidgetMain();
228 ret = AppWatchMain();
235 if (mApplicationType == NORMAL)
239 else if(mApplicationType == WIDGET)
249 void SetLanguage( const std::string& language )
251 mLanguage = language;
254 void SetRegion( const std::string& region )
259 std::string GetLanguage() const
264 std::string GetRegion() const
270 Type mApplicationType;
271 CallbackBase* mAbortCallBack;
272 CallbackManager *mCallbackManager;
273 std::string mLanguage;
276 Framework* mFramework;
277 AppCore::AppEventHandlerPtr handlers[5];
278 #ifdef APPCORE_WATCH_AVAILABLE
279 watch_app_lifecycle_callback_s mWatchCallback;
280 app_event_handler_h watchHandlers[5];
283 static int AppCreate(void *data)
285 appcore_ui_base_on_create();
286 return static_cast<int>( static_cast<Framework*>(data)->Create() );
289 static int AppTerminate(void *data)
291 appcore_ui_base_on_terminate();
292 Observer *observer = &static_cast<Framework*>(data)->mObserver;
294 observer->OnTerminate();
299 static int AppPause(void *data)
301 appcore_ui_base_on_pause();
302 Observer *observer = &static_cast<Framework*>(data)->mObserver;
309 static int AppResume(void *data)
311 appcore_ui_base_on_resume();
312 Observer *observer = &static_cast<Framework*>(data)->mObserver;
314 observer->OnResume();
319 static void ProcessBundle(Framework* framework, bundle *bundleData)
321 if(bundleData == NULL)
327 char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
328 if(bundleName != NULL)
330 framework->SetBundleName(bundleName);
334 char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
337 framework->SetBundleId(bundleId);
342 * Called by AppCore when the application is launched from another module (e.g. homescreen).
343 * @param[in] b the bundle data which the launcher module sent
345 static int AppControl(bundle* bundleData, void *data)
347 app_control_h appControl = NULL;
349 appcore_ui_base_on_control(bundleData);
353 if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
355 DALI_LOG_ERROR("Failed to create an app_control handle");
360 if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
362 DALI_LOG_ERROR("Failed to create an app_control handle");
366 Framework* framework = static_cast<Framework*>(data);
367 Observer *observer = &framework->mObserver;
369 ProcessBundle(framework, bundleData);
372 observer->OnAppControl(appControl);
374 app_control_destroy(appControl);
379 static void AppInit(int argc, char **argv, void *data)
381 #pragma GCC diagnostic push
382 #pragma GCC diagnostic ignored "-Wold-style-cast"
385 ecore_app_args_set( argc, (const char **)argv );
387 #pragma GCC diagnostic pop
390 static void AppFinish(void)
394 if(getenv("AUL_LOADER_INIT"))
396 unsetenv("AUL_LOADER_INIT");
401 static void AppRun(void *data)
403 ecore_main_loop_begin();
406 static void AppExit(void *data)
408 ecore_main_loop_quit();
411 static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
413 Framework* framework = static_cast<Framework*>(data);
414 Observer *observer = &framework->mObserver;
416 if( event && event->value )
418 framework->SetLanguage( std::string( static_cast<const char *>(event->value) ) );
419 observer->OnLanguageChanged();
423 DALI_LOG_ERROR( "NULL pointer in Language changed event\n" );
427 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
431 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
433 Framework* framework = static_cast<Framework*>(data);
434 Observer *observer = &framework->mObserver;
436 if( event && event->value )
438 framework->SetRegion( std::string( static_cast<const char *>(event->value) ) );
439 observer->OnRegionChanged();
443 DALI_LOG_ERROR( "NULL pointer in Region changed event\n" );
447 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
449 Observer *observer = &static_cast<Framework*>(data)->mObserver;
450 int status = *static_cast<int *>(event->value);
451 Dali::DeviceStatus::Battery::Status result = Dali::DeviceStatus::Battery::NORMAL;
453 // convert to dali battery status
458 result = Dali::DeviceStatus::Battery::POWER_OFF;
463 result = Dali::DeviceStatus::Battery::CRITICALLY_LOW;
469 observer->OnBatteryLow(result);
472 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
474 Observer *observer = &static_cast<Framework*>(data)->mObserver;
475 int status = *static_cast<int *>(event->value);
476 Dali::DeviceStatus::Memory::Status result = Dali::DeviceStatus::Memory::NORMAL;
478 // convert to dali memmory status
483 result = Dali::DeviceStatus::Memory::NORMAL;
488 result = Dali::DeviceStatus::Memory::LOW;
493 result = Dali::DeviceStatus::Memory::CRITICALLY_LOW;
499 observer->OnMemoryLow(result);
507 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
508 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
509 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
510 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
511 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
513 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
515 /* override methods */
516 ops.base.create = AppCreate;
517 ops.base.control = AppControl;
518 ops.base.terminate = AppTerminate;
519 ops.pause = AppPause;
520 ops.resume = AppResume;
521 ops.base.init = AppInit;
522 ops.base.finish = AppFinish;
523 ops.base.run = AppRun;
524 ops.base.exit = AppExit;
526 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
527 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
528 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
529 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
530 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
532 if (ret != TIZEN_ERROR_NONE)
535 appcore_ui_base_fini();
537 return TIZEN_ERROR_NONE;
542 appcore_ui_base_exit();
550 static int WidgetAppCreate( void *data )
552 widget_base_on_create();
553 return static_cast<int>( static_cast<Framework*>(data)->Create() );
556 static int WidgetAppTerminate( void *data )
558 Observer *observer = &static_cast<Framework*>(data)->mObserver;
559 observer->OnTerminate();
561 widget_base_on_terminate();
567 if( !IsWidgetFeatureEnabled() )
569 DALI_LOG_ERROR("widget feature is not supported");
573 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
574 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
575 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
576 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
577 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
579 widget_base_ops ops = widget_base_get_default_ops();
581 /* override methods */
582 ops.create = WidgetAppCreate;
583 ops.terminate = WidgetAppTerminate;
585 ops.finish = AppFinish;
589 int result = widget_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework);
596 #ifdef APPCORE_WATCH_AVAILABLE
597 static bool WatchAppCreate(int width, int height, void *data)
599 return static_cast<Framework*>(data)->Create();
602 static void WatchAppTimeTick(watch_time_h time, void *data)
604 Observer *observer = &static_cast<Framework*>(data)->mObserver;
605 WatchTime curTime(time);
607 observer->OnTimeTick(curTime);
610 static void WatchAppAmbientTick(watch_time_h time, void *data)
612 Observer *observer = &static_cast<Framework*>(data)->mObserver;
613 WatchTime curTime(time);
615 observer->OnAmbientTick(curTime);
618 static void WatchAppAmbientChanged(bool ambient, void *data)
620 Observer *observer = &static_cast<Framework*>(data)->mObserver;
622 observer->OnAmbientChanged(ambient);
625 static void WatchAppControl(app_control_h app_control, void *data)
627 Framework* framework = static_cast<Framework*>(data);
628 Observer *observer = &framework->mObserver;
629 bundle *bundleData = NULL;
631 app_control_to_bundle(app_control, &bundleData);
632 ProcessBundle(framework, bundleData);
635 observer->OnAppControl(app_control);
638 static void WatchAppTerminate(void *data)
640 Observer *observer = &static_cast<Framework*>(data)->mObserver;
642 observer->OnTerminate();
645 static void WatchAppPause(void *data)
647 Observer *observer = &static_cast<Framework*>(data)->mObserver;
652 static void WatchAppResume(void *data)
654 Observer *observer = &static_cast<Framework*>(data)->mObserver;
656 observer->OnResume();
665 #ifdef APPCORE_WATCH_AVAILABLE
666 mWatchCallback.create = WatchAppCreate;
667 mWatchCallback.app_control = WatchAppControl;
668 mWatchCallback.terminate = WatchAppTerminate;
669 mWatchCallback.pause = WatchAppPause;
670 mWatchCallback.resume = WatchAppResume;
671 mWatchCallback.time_tick = WatchAppTimeTick;
672 mWatchCallback.ambient_tick = WatchAppAmbientTick;
673 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
675 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
676 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
677 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
678 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
680 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
687 #ifdef APPCORE_WATCH_AVAILABLE
694 Impl( const Impl& impl );
697 Impl& operator=( const Impl& impl );
700 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
701 : mObserver(observer),
708 mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
711 bool featureFlag = true;
712 system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
714 if( featureFlag == false )
716 set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
718 #ifdef DALI_ELDBUS_AVAILABLE
719 // Initialize ElDBus.
720 DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
725 mImpl = new Impl(this, type);
728 Framework::~Framework()
735 #ifdef DALI_ELDBUS_AVAILABLE
737 DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
744 bool Framework::Create()
751 void Framework::Run()
756 ret = mImpl->AppMain();
757 if (ret != APP_ERROR_NONE)
759 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
764 void Framework::Quit()
769 bool Framework::IsMainLoopRunning()
774 void Framework::AddAbortCallback( CallbackBase* callback )
776 mImpl->mAbortCallBack = callback;
779 std::string Framework::GetBundleName() const
784 void Framework::SetBundleName(const std::string& name)
789 std::string Framework::GetBundleId() const
794 std::string Framework::GetResourcePath()
796 std::string resourcePath = "";
797 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
798 resourcePath = app_get_resource_path();
799 #else // For backwards compatibility with older Tizen versions
801 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
802 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
803 char* value = getenv( environmentVariable );
806 resourcePath = value;
808 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
813 void Framework::SetBundleId(const std::string& id)
818 void Framework::AbortCallback( )
820 // if an abort call back has been installed run it.
821 if (mImpl->mAbortCallBack)
823 CallbackBase::Execute( *mImpl->mAbortCallBack );
831 void Framework::SetLanguage( const std::string& language )
833 mImpl->SetLanguage( language );
836 void Framework::SetRegion( const std::string& region )
838 mImpl->SetRegion( region );
841 std::string Framework::GetLanguage() const
843 return mImpl->GetLanguage();
846 std::string Framework::GetRegion() const
848 return mImpl->GetRegion();
851 } // namespace Adaptor
853 } // namespace Internal