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;
384 framework->SetLanguage( std::string( static_cast<const char *>(event->value) ) );
385 observer->OnLanguageChanged();
388 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
392 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
394 Framework* framework = static_cast<Framework*>(data);
395 Observer *observer = &framework->mObserver;
396 framework->SetRegion( std::string( static_cast<const char *>(event->value) ) );
397 observer->OnRegionChanged();
400 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
402 Observer *observer = &static_cast<Framework*>(data)->mObserver;
403 int status = *static_cast<int *>(event->value);
404 Dali::DeviceStatus::Battery::Status result = Dali::DeviceStatus::Battery::NORMAL;
406 // convert to dali battery status
411 result = Dali::DeviceStatus::Battery::POWER_OFF;
416 result = Dali::DeviceStatus::Battery::CRITICALLY_LOW;
422 observer->OnBatteryLow(result);
425 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
427 Observer *observer = &static_cast<Framework*>(data)->mObserver;
428 int status = *static_cast<int *>(event->value);
429 Dali::DeviceStatus::Memory::Status result = Dali::DeviceStatus::Memory::NORMAL;
431 // convert to dali memmory status
436 result = Dali::DeviceStatus::Memory::NORMAL;
441 result = Dali::DeviceStatus::Memory::LOW;
446 result = Dali::DeviceStatus::Memory::CRITICALLY_LOW;
452 observer->OnMemoryLow(result);
460 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
461 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
462 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
463 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
464 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
466 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
468 /* override methods */
469 ops.base.create = AppCreate;
470 ops.base.control = AppControl;
471 ops.base.terminate = AppTerminate;
472 ops.pause = AppPause;
473 ops.resume = AppResume;
474 ops.base.init = AppInit;
475 ops.base.finish = AppFinish;
476 ops.base.run = AppRun;
477 ops.base.exit = AppExit;
479 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
480 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
481 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
482 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
483 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
485 if (ret != TIZEN_ERROR_NONE)
488 appcore_ui_base_fini();
490 return TIZEN_ERROR_NONE;
495 appcore_ui_base_exit();
498 #ifdef APPCORE_WATCH_AVAILABLE
499 static bool WatchAppCreate(int width, int height, void *data)
501 return static_cast<Framework*>(data)->Create();
504 static void WatchAppTimeTick(watch_time_h time, void *data)
506 Observer *observer = &static_cast<Framework*>(data)->mObserver;
507 WatchTime curTime(time);
509 observer->OnTimeTick(curTime);
512 static void WatchAppAmbientTick(watch_time_h time, void *data)
514 Observer *observer = &static_cast<Framework*>(data)->mObserver;
515 WatchTime curTime(time);
517 observer->OnAmbientTick(curTime);
520 static void WatchAppAmbientChanged(bool ambient, void *data)
522 Observer *observer = &static_cast<Framework*>(data)->mObserver;
524 observer->OnAmbientChanged(ambient);
527 static void WatchAppControl(app_control_h app_control, void *data)
529 Framework* framework = static_cast<Framework*>(data);
530 Observer *observer = &framework->mObserver;
531 bundle *bundleData = NULL;
533 app_control_to_bundle(app_control, &bundleData);
534 ProcessBundle(framework, bundleData);
537 observer->OnAppControl(app_control);
540 static void WatchAppTerminate(void *data)
542 Observer *observer = &static_cast<Framework*>(data)->mObserver;
544 observer->OnTerminate();
547 static void WatchAppPause(void *data)
549 Observer *observer = &static_cast<Framework*>(data)->mObserver;
554 static void WatchAppResume(void *data)
556 Observer *observer = &static_cast<Framework*>(data)->mObserver;
558 observer->OnResume();
567 #ifdef APPCORE_WATCH_AVAILABLE
568 mWatchCallback.create = WatchAppCreate;
569 mWatchCallback.app_control = WatchAppControl;
570 mWatchCallback.terminate = WatchAppTerminate;
571 mWatchCallback.pause = WatchAppPause;
572 mWatchCallback.resume = WatchAppResume;
573 mWatchCallback.time_tick = WatchAppTimeTick;
574 mWatchCallback.ambient_tick = WatchAppAmbientTick;
575 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
577 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
578 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
579 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
580 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
582 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
589 #ifdef APPCORE_WATCH_AVAILABLE
596 Impl( const Impl& impl );
599 Impl& operator=( const Impl& impl );
602 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
603 : mObserver(observer),
610 mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
613 bool featureFlag = true;
614 system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
616 if( featureFlag == false )
618 set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
620 #ifdef DALI_ELDBUS_AVAILABLE
621 // Initialize ElDBus.
622 DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
627 mImpl = new Impl(this, type);
630 Framework::~Framework()
637 #ifdef DALI_ELDBUS_AVAILABLE
639 DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
646 bool Framework::Create()
653 void Framework::Run()
658 ret = mImpl->AppMain();
659 if (ret != APP_ERROR_NONE)
661 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
666 void Framework::Quit()
671 bool Framework::IsMainLoopRunning()
676 void Framework::AddAbortCallback( CallbackBase* callback )
678 mImpl->mAbortCallBack = callback;
681 std::string Framework::GetBundleName() const
686 void Framework::SetBundleName(const std::string& name)
691 std::string Framework::GetBundleId() const
696 std::string Framework::GetResourcePath()
698 std::string resourcePath = "";
699 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
700 resourcePath = app_get_resource_path();
701 #else // For backwards compatibility with older Tizen versions
703 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
704 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
705 char* value = getenv( environmentVariable );
708 resourcePath = value;
710 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
715 void Framework::SetBundleId(const std::string& id)
720 void Framework::AbortCallback( )
722 // if an abort call back has been installed run it.
723 if (mImpl->mAbortCallBack)
725 CallbackBase::Execute( *mImpl->mAbortCallBack );
733 void Framework::SetLanguage( const std::string& language )
735 mImpl->SetLanguage( language );
738 void Framework::SetRegion( const std::string& region )
740 mImpl->SetRegion( region );
743 std::string Framework::GetLanguage() const
745 return mImpl->GetLanguage();
748 std::string Framework::GetRegion() const
750 return mImpl->GetRegion();
753 } // namespace Adaptor
755 } // namespace Internal