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;
414 framework->SetLanguage( std::string( static_cast<const char *>(event->value) ) );
415 observer->OnLanguageChanged();
418 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
422 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
424 Framework* framework = static_cast<Framework*>(data);
425 Observer *observer = &framework->mObserver;
426 framework->SetRegion( std::string( static_cast<const char *>(event->value) ) );
427 observer->OnRegionChanged();
430 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
432 Observer *observer = &static_cast<Framework*>(data)->mObserver;
433 int status = *static_cast<int *>(event->value);
434 Dali::DeviceStatus::Battery::Status result = Dali::DeviceStatus::Battery::NORMAL;
436 // convert to dali battery status
441 result = Dali::DeviceStatus::Battery::POWER_OFF;
446 result = Dali::DeviceStatus::Battery::CRITICALLY_LOW;
452 observer->OnBatteryLow(result);
455 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
457 Observer *observer = &static_cast<Framework*>(data)->mObserver;
458 int status = *static_cast<int *>(event->value);
459 Dali::DeviceStatus::Memory::Status result = Dali::DeviceStatus::Memory::NORMAL;
461 // convert to dali memmory status
466 result = Dali::DeviceStatus::Memory::NORMAL;
471 result = Dali::DeviceStatus::Memory::LOW;
476 result = Dali::DeviceStatus::Memory::CRITICALLY_LOW;
482 observer->OnMemoryLow(result);
490 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
491 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
492 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
493 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
494 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
496 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
498 /* override methods */
499 ops.base.create = AppCreate;
500 ops.base.control = AppControl;
501 ops.base.terminate = AppTerminate;
502 ops.pause = AppPause;
503 ops.resume = AppResume;
504 ops.base.init = AppInit;
505 ops.base.finish = AppFinish;
506 ops.base.run = AppRun;
507 ops.base.exit = AppExit;
509 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
510 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
511 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
512 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
513 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
515 if (ret != TIZEN_ERROR_NONE)
518 appcore_ui_base_fini();
520 return TIZEN_ERROR_NONE;
525 appcore_ui_base_exit();
533 static int WidgetAppCreate( void *data )
535 dlog_print(DLOG_ERROR,"DALI","WidgetAppCreate!\n");
536 widget_base_on_create();
537 return static_cast<int>( static_cast<Framework*>(data)->Create() );
540 static int WidgetAppTerminate( void *data )
542 Observer *observer = &static_cast<Framework*>(data)->mObserver;
543 observer->OnTerminate();
545 widget_base_on_terminate();
551 if( !IsWidgetFeatureEnabled() )
553 DALI_LOG_ERROR("widget feature is not supported");
557 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
558 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
559 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
560 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
561 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
563 widget_base_ops ops = widget_base_get_default_ops();
565 /* override methods */
566 ops.create = WidgetAppCreate;
567 ops.terminate = WidgetAppTerminate;
569 ops.finish = AppFinish;
573 int result = widget_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework);
580 #ifdef APPCORE_WATCH_AVAILABLE
581 static bool WatchAppCreate(int width, int height, void *data)
583 return static_cast<Framework*>(data)->Create();
586 static void WatchAppTimeTick(watch_time_h time, void *data)
588 Observer *observer = &static_cast<Framework*>(data)->mObserver;
589 WatchTime curTime(time);
591 observer->OnTimeTick(curTime);
594 static void WatchAppAmbientTick(watch_time_h time, void *data)
596 Observer *observer = &static_cast<Framework*>(data)->mObserver;
597 WatchTime curTime(time);
599 observer->OnAmbientTick(curTime);
602 static void WatchAppAmbientChanged(bool ambient, void *data)
604 Observer *observer = &static_cast<Framework*>(data)->mObserver;
606 observer->OnAmbientChanged(ambient);
609 static void WatchAppControl(app_control_h app_control, void *data)
611 Framework* framework = static_cast<Framework*>(data);
612 Observer *observer = &framework->mObserver;
613 bundle *bundleData = NULL;
615 app_control_to_bundle(app_control, &bundleData);
616 ProcessBundle(framework, bundleData);
619 observer->OnAppControl(app_control);
622 static void WatchAppTerminate(void *data)
624 Observer *observer = &static_cast<Framework*>(data)->mObserver;
626 observer->OnTerminate();
629 static void WatchAppPause(void *data)
631 Observer *observer = &static_cast<Framework*>(data)->mObserver;
636 static void WatchAppResume(void *data)
638 Observer *observer = &static_cast<Framework*>(data)->mObserver;
640 observer->OnResume();
649 #ifdef APPCORE_WATCH_AVAILABLE
650 mWatchCallback.create = WatchAppCreate;
651 mWatchCallback.app_control = WatchAppControl;
652 mWatchCallback.terminate = WatchAppTerminate;
653 mWatchCallback.pause = WatchAppPause;
654 mWatchCallback.resume = WatchAppResume;
655 mWatchCallback.time_tick = WatchAppTimeTick;
656 mWatchCallback.ambient_tick = WatchAppAmbientTick;
657 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
659 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
660 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
661 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
662 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
664 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
671 #ifdef APPCORE_WATCH_AVAILABLE
678 Impl( const Impl& impl );
681 Impl& operator=( const Impl& impl );
684 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
685 : mObserver(observer),
692 mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
695 bool featureFlag = true;
696 system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
698 if( featureFlag == false )
700 set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
702 #ifdef DALI_ELDBUS_AVAILABLE
703 // Initialize ElDBus.
704 DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
709 mImpl = new Impl(this, type);
712 Framework::~Framework()
719 #ifdef DALI_ELDBUS_AVAILABLE
721 DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
728 bool Framework::Create()
735 void Framework::Run()
740 ret = mImpl->AppMain();
741 if (ret != APP_ERROR_NONE)
743 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
748 void Framework::Quit()
753 bool Framework::IsMainLoopRunning()
758 void Framework::AddAbortCallback( CallbackBase* callback )
760 mImpl->mAbortCallBack = callback;
763 std::string Framework::GetBundleName() const
768 void Framework::SetBundleName(const std::string& name)
773 std::string Framework::GetBundleId() const
778 std::string Framework::GetResourcePath()
780 std::string resourcePath = "";
781 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
782 resourcePath = app_get_resource_path();
783 #else // For backwards compatibility with older Tizen versions
785 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
786 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
787 char* value = getenv( environmentVariable );
790 resourcePath = value;
792 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
797 void Framework::SetBundleId(const std::string& id)
802 void Framework::AbortCallback( )
804 // if an abort call back has been installed run it.
805 if (mImpl->mAbortCallBack)
807 CallbackBase::Execute( *mImpl->mAbortCallBack );
815 void Framework::SetLanguage( const std::string& language )
817 mImpl->SetLanguage( language );
820 void Framework::SetRegion( const std::string& region )
822 mImpl->SetRegion( region );
825 std::string Framework::GetLanguage() const
827 return mImpl->GetLanguage();
830 std::string Framework::GetRegion() const
832 return mImpl->GetRegion();
835 } // namespace Adaptor
837 } // namespace Internal