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>
61 #if defined(DEBUG_ENABLED)
62 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS" );
65 bool IsWidgetFeatureEnabled()
67 static bool feature = false;
68 static bool retrieved = false;
74 ret = system_info_get_platform_bool("http://tizen.org/feature/shell.appwidget", &feature);
75 if(ret != SYSTEM_INFO_ERROR_NONE)
77 DALI_LOG_ERROR("failed to get system info"); /* LCOV_EXCL_LINE */
78 return false; /* LCOV_EXCL_LINE */
85 } // anonymous namespace
92 LOW_MEMORY, //< The low memory event
93 LOW_BATTERY, //< The low battery event
94 LANGUAGE_CHANGED, //< The system language changed event
95 DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
96 REGION_FORMAT_CHANGED, //< The region format changed event
97 SUSPENDED_STATE_CHANGED, //< The suspended state changed event of the application
98 UPDATE_REQUESTED, //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
101 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
103 [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
104 [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
105 [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
106 [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
107 [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
108 [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
117 typedef struct AppEventInfo *AppEventInfoPtr;
119 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void *userData);
121 struct AppEventHandler
129 typedef struct AppEventHandler *AppEventHandlerPtr;
131 int EventCallback(void *event, void *data)
133 AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
135 struct AppEventInfo appEvent;
137 appEvent.type = handler->type;
138 appEvent.value = event;
141 handler->cb(&appEvent, handler->data);
146 int AppAddEventHandler(AppEventHandlerPtr *eventHandler, AppEventType eventType, AppEventCallback callback, void *userData)
148 AppEventHandlerPtr handler;
150 handler = static_cast<AppEventHandlerPtr>( calloc(1, sizeof(struct AppEventHandler)) );
153 DALI_LOG_ERROR( "failed to create handler" );
154 return TIZEN_ERROR_UNKNOWN;
158 handler->type = eventType;
159 handler->cb = callback;
160 handler->data = userData;
161 handler->raw = appcore_base_add_event( static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
163 *eventHandler = handler;
165 return TIZEN_ERROR_NONE;
169 } // namespace Appcore
172 * Impl to hide EFL data members
174 struct Framework::Impl
177 Impl(void* data, Type type )
178 : mAbortCallBack( NULL ),
179 mCallbackManager( NULL )
180 #ifdef APPCORE_WATCH_AVAILABLE
184 mFramework = static_cast<Framework*>(data);
186 #ifndef APPCORE_WATCH_AVAILABLE
189 throw Dali::DaliException( "", "Watch Application is not supported." );
192 mApplicationType = type;
193 mCallbackManager = CallbackManager::New();
197 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, ®ion );
198 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &language );
199 mRegion = std::string( region );
200 mLanguage = std::string( language );
205 delete mAbortCallBack;
207 // we're quiting the main loop so
208 // mCallbackManager->RemoveAllCallBacks() does not need to be called
209 // to delete our abort handler
210 delete mCallbackManager;
217 if (mApplicationType == NORMAL)
219 ret = AppNormalMain();
221 else if(mApplicationType == WIDGET)
223 ret = AppWidgetMain();
227 ret = AppWatchMain();
234 if (mApplicationType == NORMAL)
238 else if(mApplicationType == WIDGET)
248 void SetLanguage( const std::string& language )
250 mLanguage = language;
253 void SetRegion( const std::string& region )
258 std::string GetLanguage() const
263 std::string GetRegion() const
269 Type mApplicationType;
270 CallbackBase* mAbortCallBack;
271 CallbackManager *mCallbackManager;
272 std::string mLanguage;
275 Framework* mFramework;
276 AppCore::AppEventHandlerPtr handlers[5];
277 #ifdef APPCORE_WATCH_AVAILABLE
278 watch_app_lifecycle_callback_s mWatchCallback;
279 app_event_handler_h watchHandlers[5];
282 static int AppCreate(void *data)
284 appcore_ui_base_on_create();
285 return static_cast<int>( static_cast<Framework*>(data)->Create() );
288 static int AppTerminate(void *data)
290 appcore_ui_base_on_terminate();
291 Observer *observer = &static_cast<Framework*>(data)->mObserver;
293 observer->OnTerminate();
298 static int AppPause(void *data)
300 appcore_ui_base_on_pause();
301 Observer *observer = &static_cast<Framework*>(data)->mObserver;
308 static int AppResume(void *data)
310 appcore_ui_base_on_resume();
311 Observer *observer = &static_cast<Framework*>(data)->mObserver;
313 observer->OnResume();
318 static void ProcessBundle(Framework* framework, bundle *bundleData)
320 if(bundleData == NULL)
326 char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
327 if(bundleName != NULL)
329 framework->SetBundleName(bundleName);
333 char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
336 framework->SetBundleId(bundleId);
341 * Called by AppCore when the application is launched from another module (e.g. homescreen).
342 * @param[in] b the bundle data which the launcher module sent
344 static int AppControl(bundle* bundleData, void *data)
346 app_control_h appControl = NULL;
348 appcore_ui_base_on_control(bundleData);
352 if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
354 DALI_LOG_ERROR("Failed to create an app_control handle");
359 if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
361 DALI_LOG_ERROR("Failed to create an app_control handle");
365 Framework* framework = static_cast<Framework*>(data);
366 Observer *observer = &framework->mObserver;
368 ProcessBundle(framework, bundleData);
371 observer->OnAppControl(appControl);
373 app_control_destroy(appControl);
378 static void AppInit(int argc, char **argv, void *data)
380 #pragma GCC diagnostic push
381 #pragma GCC diagnostic ignored "-Wold-style-cast"
384 ecore_app_args_set( argc, (const char **)argv );
386 #pragma GCC diagnostic pop
389 static void AppFinish(void)
393 if(getenv("AUL_LOADER_INIT"))
395 unsetenv("AUL_LOADER_INIT");
400 static void AppRun(void *data)
402 ecore_main_loop_begin();
405 static void AppExit(void *data)
407 ecore_main_loop_quit();
410 static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
412 Framework* framework = static_cast<Framework*>(data);
413 Observer *observer = &framework->mObserver;
415 if( event && event->value )
417 framework->SetLanguage( std::string( static_cast<const char *>(event->value) ) );
418 observer->OnLanguageChanged();
422 DALI_LOG_ERROR( "NULL pointer in Language changed event\n" );
426 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
430 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
432 Framework* framework = static_cast<Framework*>(data);
433 Observer *observer = &framework->mObserver;
435 if( event && event->value )
437 framework->SetRegion( std::string( static_cast<const char *>(event->value) ) );
438 observer->OnRegionChanged();
442 DALI_LOG_ERROR( "NULL pointer in Region changed event\n" );
446 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
448 Observer *observer = &static_cast<Framework*>(data)->mObserver;
449 int status = *static_cast<int *>(event->value);
450 Dali::DeviceStatus::Battery::Status result = Dali::DeviceStatus::Battery::NORMAL;
452 // convert to dali battery status
457 result = Dali::DeviceStatus::Battery::POWER_OFF;
462 result = Dali::DeviceStatus::Battery::CRITICALLY_LOW;
468 observer->OnBatteryLow(result);
471 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
473 Observer *observer = &static_cast<Framework*>(data)->mObserver;
474 int status = *static_cast<int *>(event->value);
475 Dali::DeviceStatus::Memory::Status result = Dali::DeviceStatus::Memory::NORMAL;
477 // convert to dali memmory status
482 result = Dali::DeviceStatus::Memory::NORMAL;
487 result = Dali::DeviceStatus::Memory::LOW;
492 result = Dali::DeviceStatus::Memory::CRITICALLY_LOW;
498 observer->OnMemoryLow(result);
506 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
507 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
508 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
509 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
510 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
512 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
514 /* override methods */
515 ops.base.create = AppCreate;
516 ops.base.control = AppControl;
517 ops.base.terminate = AppTerminate;
518 ops.pause = AppPause;
519 ops.resume = AppResume;
520 ops.base.init = AppInit;
521 ops.base.finish = AppFinish;
522 ops.base.run = AppRun;
523 ops.base.exit = AppExit;
525 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
526 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
527 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
528 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
529 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
531 if (ret != TIZEN_ERROR_NONE)
534 appcore_ui_base_fini();
536 return TIZEN_ERROR_NONE;
541 appcore_ui_base_exit();
549 static int WidgetAppCreate( void *data )
551 dlog_print(DLOG_ERROR,"DALI","WidgetAppCreate!\n");
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