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>
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>
58 #if defined(DEBUG_ENABLED)
61 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS" );
62 } // anonymous namespace
70 LOW_MEMORY, //< The low memory event
71 LOW_BATTERY, //< The low battery event
72 LANGUAGE_CHANGED, //< The system language changed event
73 DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
74 REGION_FORMAT_CHANGED, //< The region format changed event
75 SUSPENDED_STATE_CHANGED, //< The suspended state changed event of the application
76 UPDATE_REQUESTED, //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
79 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
81 [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
82 [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
83 [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
84 [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
85 [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
86 [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
95 typedef struct AppEventInfo *AppEventInfoPtr;
97 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void *userData);
99 struct AppEventHandler
107 typedef struct AppEventHandler *AppEventHandlerPtr;
109 int EventCallback(void *event, void *data)
111 AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
113 struct AppEventInfo appEvent;
115 appEvent.type = handler->type;
116 appEvent.value = event;
119 handler->cb(&appEvent, handler->data);
124 int AppAddEventHandler(AppEventHandlerPtr *eventHandler, AppEventType eventType, AppEventCallback callback, void *userData)
126 AppEventHandlerPtr handler;
128 handler = static_cast<AppEventHandlerPtr>( calloc(1, sizeof(struct AppEventHandler)) );
131 DALI_LOG_ERROR( "failed to create handler" );
132 return TIZEN_ERROR_UNKNOWN;
136 handler->type = eventType;
137 handler->cb = callback;
138 handler->data = userData;
139 handler->raw = appcore_base_add_event( static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
141 *eventHandler = handler;
143 return TIZEN_ERROR_NONE;
147 } // namespace Appcore
150 * Impl to hide EFL data members
152 struct Framework::Impl
155 Impl(void* data, Type type )
156 : mAbortCallBack( NULL ),
157 mCallbackManager( NULL )
158 #ifdef APPCORE_WATCH_AVAILABLE
162 mFramework = static_cast<Framework*>(data);
164 #ifndef APPCORE_WATCH_AVAILABLE
167 throw Dali::DaliException( "", "Watch Application is not supported." );
170 mApplicationType = type;
171 mCallbackManager = CallbackManager::New();
175 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, ®ion );
176 system_settings_get_value_string( SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &language );
177 mRegion = std::string( region );
178 mLanguage = std::string( language );
183 delete mAbortCallBack;
185 // we're quiting the main loop so
186 // mCallbackManager->RemoveAllCallBacks() does not need to be called
187 // to delete our abort handler
188 delete mCallbackManager;
195 if (mApplicationType == NORMAL)
197 ret = AppNormalMain();
201 ret = AppWatchMain();
208 if (mApplicationType == NORMAL)
218 void SetLanguage( const std::string& language )
220 mLanguage = language;
223 void SetRegion( const std::string& region )
228 std::string GetLanguage() const
233 std::string GetRegion() const
239 Type mApplicationType;
240 CallbackBase* mAbortCallBack;
241 CallbackManager *mCallbackManager;
242 std::string mLanguage;
245 Framework* mFramework;
246 AppCore::AppEventHandlerPtr handlers[5];
247 #ifdef APPCORE_WATCH_AVAILABLE
248 watch_app_lifecycle_callback_s mWatchCallback;
249 app_event_handler_h watchHandlers[5];
252 static int AppCreate(void *data)
254 appcore_ui_base_on_create();
255 return static_cast<int>( static_cast<Framework*>(data)->Create() );
258 static int AppTerminate(void *data)
260 appcore_ui_base_on_terminate();
261 Observer *observer = &static_cast<Framework*>(data)->mObserver;
263 observer->OnTerminate();
268 static int AppPause(void *data)
270 appcore_ui_base_on_pause();
271 Observer *observer = &static_cast<Framework*>(data)->mObserver;
278 static int AppResume(void *data)
280 appcore_ui_base_on_resume();
281 Observer *observer = &static_cast<Framework*>(data)->mObserver;
283 observer->OnResume();
288 static void ProcessBundle(Framework* framework, bundle *bundleData)
290 if(bundleData == NULL)
296 char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
297 if(bundleName != NULL)
299 framework->SetBundleName(bundleName);
303 char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
306 framework->SetBundleId(bundleId);
311 * Called by AppCore when the application is launched from another module (e.g. homescreen).
312 * @param[in] b the bundle data which the launcher module sent
314 static int AppControl(bundle* bundleData, void *data)
316 app_control_h appControl = NULL;
318 appcore_ui_base_on_control(bundleData);
322 if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
324 DALI_LOG_ERROR("Failed to create an app_control handle");
329 if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
331 DALI_LOG_ERROR("Failed to create an app_control handle");
335 Framework* framework = static_cast<Framework*>(data);
336 Observer *observer = &framework->mObserver;
338 ProcessBundle(framework, bundleData);
341 observer->OnAppControl(appControl);
343 app_control_destroy(appControl);
348 static void AppInit(int argc, char **argv, void *data)
350 #pragma GCC diagnostic push
351 #pragma GCC diagnostic ignored "-Wold-style-cast"
354 ecore_app_args_set( argc, (const char **)argv );
356 #pragma GCC diagnostic pop
359 static void AppFinish(void)
363 if(getenv("AUL_LOADER_INIT"))
365 unsetenv("AUL_LOADER_INIT");
370 static void AppRun(void *data)
372 ecore_main_loop_begin();
375 static void AppExit(void *data)
377 ecore_main_loop_quit();
380 static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
382 Framework* framework = static_cast<Framework*>(data);
383 Observer *observer = &framework->mObserver;
385 if( event && event->value )
387 framework->SetLanguage( std::string( static_cast<const char *>(event->value) ) );
388 observer->OnLanguageChanged();
392 DALI_LOG_ERROR( "NULL pointer in Language changed event\n" );
396 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
400 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
402 Framework* framework = static_cast<Framework*>(data);
403 Observer *observer = &framework->mObserver;
405 if( event && event->value )
407 framework->SetRegion( std::string( static_cast<const char *>(event->value) ) );
408 observer->OnRegionChanged();
412 DALI_LOG_ERROR( "NULL pointer in Region changed event\n" );
416 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
418 Observer *observer = &static_cast<Framework*>(data)->mObserver;
419 int status = *static_cast<int *>(event->value);
420 Dali::DeviceStatus::Battery::Status result = Dali::DeviceStatus::Battery::NORMAL;
422 // convert to dali battery status
427 result = Dali::DeviceStatus::Battery::POWER_OFF;
432 result = Dali::DeviceStatus::Battery::CRITICALLY_LOW;
438 observer->OnBatteryLow(result);
441 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
443 Observer *observer = &static_cast<Framework*>(data)->mObserver;
444 int status = *static_cast<int *>(event->value);
445 Dali::DeviceStatus::Memory::Status result = Dali::DeviceStatus::Memory::NORMAL;
447 // convert to dali memmory status
452 result = Dali::DeviceStatus::Memory::NORMAL;
457 result = Dali::DeviceStatus::Memory::LOW;
462 result = Dali::DeviceStatus::Memory::CRITICALLY_LOW;
468 observer->OnMemoryLow(result);
476 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
477 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
478 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
479 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
480 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
482 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
484 /* override methods */
485 ops.base.create = AppCreate;
486 ops.base.control = AppControl;
487 ops.base.terminate = AppTerminate;
488 ops.pause = AppPause;
489 ops.resume = AppResume;
490 ops.base.init = AppInit;
491 ops.base.finish = AppFinish;
492 ops.base.run = AppRun;
493 ops.base.exit = AppExit;
495 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
496 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
497 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
498 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
499 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
501 if (ret != TIZEN_ERROR_NONE)
504 appcore_ui_base_fini();
506 return TIZEN_ERROR_NONE;
511 appcore_ui_base_exit();
514 #ifdef APPCORE_WATCH_AVAILABLE
515 static bool WatchAppCreate(int width, int height, void *data)
517 return static_cast<Framework*>(data)->Create();
520 static void WatchAppTimeTick(watch_time_h time, void *data)
522 Observer *observer = &static_cast<Framework*>(data)->mObserver;
523 WatchTime curTime(time);
525 observer->OnTimeTick(curTime);
528 static void WatchAppAmbientTick(watch_time_h time, void *data)
530 Observer *observer = &static_cast<Framework*>(data)->mObserver;
531 WatchTime curTime(time);
533 observer->OnAmbientTick(curTime);
536 static void WatchAppAmbientChanged(bool ambient, void *data)
538 Observer *observer = &static_cast<Framework*>(data)->mObserver;
540 observer->OnAmbientChanged(ambient);
543 static void WatchAppControl(app_control_h app_control, void *data)
545 Framework* framework = static_cast<Framework*>(data);
546 Observer *observer = &framework->mObserver;
547 bundle *bundleData = NULL;
549 app_control_to_bundle(app_control, &bundleData);
550 ProcessBundle(framework, bundleData);
553 observer->OnAppControl(app_control);
556 static void WatchAppTerminate(void *data)
558 Observer *observer = &static_cast<Framework*>(data)->mObserver;
560 observer->OnTerminate();
563 static void WatchAppPause(void *data)
565 Observer *observer = &static_cast<Framework*>(data)->mObserver;
570 static void WatchAppResume(void *data)
572 Observer *observer = &static_cast<Framework*>(data)->mObserver;
574 observer->OnResume();
583 #ifdef APPCORE_WATCH_AVAILABLE
584 mWatchCallback.create = WatchAppCreate;
585 mWatchCallback.app_control = WatchAppControl;
586 mWatchCallback.terminate = WatchAppTerminate;
587 mWatchCallback.pause = WatchAppPause;
588 mWatchCallback.resume = WatchAppResume;
589 mWatchCallback.time_tick = WatchAppTimeTick;
590 mWatchCallback.ambient_tick = WatchAppAmbientTick;
591 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
593 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
594 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
595 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
596 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
598 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
605 #ifdef APPCORE_WATCH_AVAILABLE
612 Impl( const Impl& impl );
615 Impl& operator=( const Impl& impl );
618 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
619 : mObserver(observer),
626 mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
629 bool featureFlag = true;
630 system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
632 if( featureFlag == false )
634 set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
636 #ifdef DALI_ELDBUS_AVAILABLE
637 // Initialize ElDBus.
638 DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
643 mImpl = new Impl(this, type);
646 Framework::~Framework()
653 #ifdef DALI_ELDBUS_AVAILABLE
655 DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
662 bool Framework::Create()
669 void Framework::Run()
674 ret = mImpl->AppMain();
675 if (ret != APP_ERROR_NONE)
677 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
682 void Framework::Quit()
687 bool Framework::IsMainLoopRunning()
692 void Framework::AddAbortCallback( CallbackBase* callback )
694 mImpl->mAbortCallBack = callback;
697 std::string Framework::GetBundleName() const
702 void Framework::SetBundleName(const std::string& name)
707 std::string Framework::GetBundleId() const
712 std::string Framework::GetResourcePath()
714 std::string resourcePath = "";
715 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
716 resourcePath = app_get_resource_path();
717 #else // For backwards compatibility with older Tizen versions
719 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
720 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
721 char* value = getenv( environmentVariable );
724 resourcePath = value;
726 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
731 void Framework::SetBundleId(const std::string& id)
736 void Framework::AbortCallback( )
738 // if an abort call back has been installed run it.
739 if (mImpl->mAbortCallBack)
741 CallbackBase::Execute( *mImpl->mAbortCallBack );
749 void Framework::SetLanguage( const std::string& language )
751 mImpl->SetLanguage( language );
754 void Framework::SetRegion( const std::string& region )
756 mImpl->SetRegion( region );
759 std::string Framework::GetLanguage() const
761 return mImpl->GetLanguage();
764 std::string Framework::GetRegion() const
766 return mImpl->GetRegion();
769 } // namespace Adaptor
771 } // namespace Internal