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 <bundle_internal.h>
31 // CONDITIONAL INCLUDES
32 #ifdef APPCORE_WATCH_AVAILABLE
33 #include <appcore-watch/watch_app.h>
35 #ifdef DALI_ELDBUS_AVAILABLE
37 #endif // DALI_ELDBUS_AVAILABLE
39 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
40 #include <tzplatform_config.h>
41 #endif // TIZEN_PLATFORM_CONFIG_SUPPORTED
43 #include <dali/integration-api/debug.h>
46 #include <callback-manager.h>
57 #if defined(DEBUG_ENABLED)
60 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS" );
61 } // anonymous namespace
69 LOW_MEMORY, //< The low memory event
70 LOW_BATTERY, //< The low battery event
71 LANGUAGE_CHANGED, //< The system language changed event
72 DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
73 REGION_FORMAT_CHANGED, //< The region format changed event
74 SUSPENDED_STATE_CHANGED, //< The suspended state changed event of the application
75 UPDATE_REQUESTED, //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
78 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
80 [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
81 [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
82 [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
83 [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
84 [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
85 [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
94 typedef struct AppEventInfo *AppEventInfoPtr;
96 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void *userData);
98 struct AppEventHandler
106 typedef struct AppEventHandler *AppEventHandlerPtr;
108 int EventCallback(void *event, void *data)
110 AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
112 struct AppEventInfo appEvent;
114 appEvent.type = handler->type;
115 appEvent.value = event;
118 handler->cb(&appEvent, handler->data);
123 int AppAddEventHandler(AppEventHandlerPtr *eventHandler, AppEventType eventType, AppEventCallback callback, void *userData)
125 AppEventHandlerPtr handler;
127 handler = static_cast<AppEventHandlerPtr>( calloc(1, sizeof(struct AppEventHandler)) );
130 DALI_LOG_ERROR( "failed to create handler" );
131 return TIZEN_ERROR_UNKNOWN;
135 handler->type = eventType;
136 handler->cb = callback;
137 handler->data = userData;
138 handler->raw = appcore_base_add_event( static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
140 *eventHandler = handler;
142 return TIZEN_ERROR_NONE;
146 } // namespace Appcore
149 * Impl to hide EFL data members
151 struct Framework::Impl
154 Impl(void* data, Type type )
155 : mAbortCallBack( NULL ),
156 mCallbackManager( NULL )
157 #ifdef APPCORE_WATCH_AVAILABLE
161 mFramework = static_cast<Framework*>(data);
163 #ifndef APPCORE_WATCH_AVAILABLE
166 throw Dali::DaliException( "", "Watch Application is not supported." );
169 mApplicationType = type;
170 mCallbackManager = CallbackManager::New();
175 delete mAbortCallBack;
177 // we're quiting the main loop so
178 // mCallbackManager->RemoveAllCallBacks() does not need to be called
179 // to delete our abort handler
180 delete mCallbackManager;
187 if (mApplicationType == NORMAL)
189 ret = AppNormalMain();
193 ret = AppWatchMain();
200 if (mApplicationType == NORMAL)
212 Type mApplicationType;
213 CallbackBase* mAbortCallBack;
214 CallbackManager *mCallbackManager;
216 Framework* mFramework;
217 AppCore::AppEventHandlerPtr handlers[5];
218 #ifdef APPCORE_WATCH_AVAILABLE
219 watch_app_lifecycle_callback_s mWatchCallback;
220 app_event_handler_h watchHandlers[5];
223 static int AppCreate(void *data)
225 appcore_ui_base_on_create();
226 return static_cast<int>( static_cast<Framework*>(data)->Create() );
229 static int AppTerminate(void *data)
231 appcore_ui_base_on_terminate();
232 Observer *observer = &static_cast<Framework*>(data)->mObserver;
234 observer->OnTerminate();
239 static int AppPause(void *data)
241 appcore_ui_base_on_pause();
242 Observer *observer = &static_cast<Framework*>(data)->mObserver;
249 static int AppResume(void *data)
251 appcore_ui_base_on_resume();
252 Observer *observer = &static_cast<Framework*>(data)->mObserver;
254 observer->OnResume();
259 static void ProcessBundle(Framework* framework, bundle *bundleData)
261 if(bundleData == NULL)
267 char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
268 if(bundleName != NULL)
270 framework->SetBundleName(bundleName);
274 char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
277 framework->SetBundleId(bundleId);
282 * Called by AppCore when the application is launched from another module (e.g. homescreen).
283 * @param[in] b the bundle data which the launcher module sent
285 static int AppControl(bundle* bundleData, void *data)
287 app_control_h appControl = NULL;
289 appcore_ui_base_on_control(bundleData);
293 if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
295 DALI_LOG_ERROR("Failed to create an app_control handle");
300 if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
302 DALI_LOG_ERROR("Failed to create an app_control handle");
306 Framework* framework = static_cast<Framework*>(data);
307 Observer *observer = &framework->mObserver;
309 ProcessBundle(framework, bundleData);
312 observer->OnAppControl(appControl);
314 app_control_destroy(appControl);
319 static void AppInit(int argc, char **argv, void *data)
321 #pragma GCC diagnostic push
322 #pragma GCC diagnostic ignored "-Wold-style-cast"
325 ecore_app_args_set( argc, (const char **)argv );
327 #pragma GCC diagnostic pop
330 static void AppFinish(void)
334 if(getenv("AUL_LOADER_INIT"))
336 unsetenv("AUL_LOADER_INIT");
341 static void AppRun(void *data)
343 ecore_main_loop_begin();
346 static void AppExit(void *data)
348 ecore_main_loop_quit();
351 static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
353 Observer *observer = &static_cast<Framework*>(data)->mObserver;
355 observer->OnLanguageChanged();
358 static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
362 static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
364 Observer *observer = &static_cast<Framework*>(data)->mObserver;
366 observer->OnRegionChanged();
369 static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
371 Observer *observer = &static_cast<Framework*>(data)->mObserver;
373 observer->OnBatteryLow();
376 static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
378 Observer *observer = &static_cast<Framework*>(data)->mObserver;
380 observer->OnMemoryLow();
388 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
389 AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
390 AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
391 AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
392 AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
394 appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
396 /* override methods */
397 ops.base.create = AppCreate;
398 ops.base.control = AppControl;
399 ops.base.terminate = AppTerminate;
400 ops.pause = AppPause;
401 ops.resume = AppResume;
402 ops.base.init = AppInit;
403 ops.base.finish = AppFinish;
404 ops.base.run = AppRun;
405 ops.base.exit = AppExit;
407 ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
408 APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
409 APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
410 APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
411 APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
413 if (ret != TIZEN_ERROR_NONE)
416 appcore_ui_base_fini();
418 return TIZEN_ERROR_NONE;
423 appcore_ui_base_exit();
426 #ifdef APPCORE_WATCH_AVAILABLE
427 static bool WatchAppCreate(int width, int height, void *data)
429 return static_cast<Framework*>(data)->Create();
432 static void WatchAppTimeTick(watch_time_h time, void *data)
434 Observer *observer = &static_cast<Framework*>(data)->mObserver;
435 WatchTime curTime(time);
437 observer->OnTimeTick(curTime);
440 static void WatchAppAmbientTick(watch_time_h time, void *data)
442 Observer *observer = &static_cast<Framework*>(data)->mObserver;
443 WatchTime curTime(time);
445 observer->OnAmbientTick(curTime);
448 static void WatchAppAmbientChanged(bool ambient, void *data)
450 Observer *observer = &static_cast<Framework*>(data)->mObserver;
452 observer->OnAmbientChanged(ambient);
455 static void WatchAppLanguageChanged(app_event_info_h event, void *data)
457 Observer *observer = &static_cast<Framework*>(data)->mObserver;
459 observer->OnLanguageChanged();
462 static void WatchAppRegionChanged(app_event_info_h event, void *data)
464 Observer *observer = &static_cast<Framework*>(data)->mObserver;
466 observer->OnRegionChanged();
469 static void WatchAppBatteryLow(app_event_info_h event, void *data)
471 Observer *observer = &static_cast<Framework*>(data)->mObserver;
473 observer->OnBatteryLow();
476 static void WatchAppMemoryLow(app_event_info_h event, void *data)
478 Observer *observer = &static_cast<Framework*>(data)->mObserver;
480 observer->OnMemoryLow();
483 static void WatchAppControl(app_control_h app_control, void *data)
485 Framework* framework = static_cast<Framework*>(data);
486 Observer *observer = &framework->mObserver;
487 bundle *bundleData = NULL;
489 app_control_to_bundle(app_control, &bundleData);
490 ProcessBundle(framework, bundleData);
493 observer->OnAppControl(app_control);
496 static void WatchAppTerminate(void *data)
498 Observer *observer = &static_cast<Framework*>(data)->mObserver;
500 observer->OnTerminate();
503 static void WatchAppPause(void *data)
505 Observer *observer = &static_cast<Framework*>(data)->mObserver;
510 static void WatchAppResume(void *data)
512 Observer *observer = &static_cast<Framework*>(data)->mObserver;
514 observer->OnResume();
523 #ifdef APPCORE_WATCH_AVAILABLE
524 mWatchCallback.create = WatchAppCreate;
525 mWatchCallback.app_control = WatchAppControl;
526 mWatchCallback.terminate = WatchAppTerminate;
527 mWatchCallback.pause = WatchAppPause;
528 mWatchCallback.resume = WatchAppResume;
529 mWatchCallback.time_tick = WatchAppTimeTick;
530 mWatchCallback.ambient_tick = WatchAppAmbientTick;
531 mWatchCallback.ambient_changed = WatchAppAmbientChanged;
533 watch_app_add_event_handler(&watchHandlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, WatchAppBatteryLow, mFramework);
534 watch_app_add_event_handler(&watchHandlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, WatchAppMemoryLow, mFramework);
535 watch_app_add_event_handler(&watchHandlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, WatchAppLanguageChanged, mFramework);
536 watch_app_add_event_handler(&watchHandlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, WatchAppRegionChanged, mFramework);
538 ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
545 #ifdef APPCORE_WATCH_AVAILABLE
553 Impl( const Impl& impl );
556 Impl& operator=( const Impl& impl );
559 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
560 : mObserver(observer),
567 mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
570 bool featureFlag = true;
571 system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
573 if( featureFlag == false )
575 set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
577 #ifdef DALI_ELDBUS_AVAILABLE
578 // Initialize ElDBus.
579 DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
584 mImpl = new Impl(this, type);
587 Framework::~Framework()
594 #ifdef DALI_ELDBUS_AVAILABLE
596 DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
603 bool Framework::Create()
610 void Framework::Run()
615 ret = mImpl->AppMain();
616 if (ret != APP_ERROR_NONE)
618 DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
623 void Framework::Quit()
628 bool Framework::IsMainLoopRunning()
633 void Framework::AddAbortCallback( CallbackBase* callback )
635 mImpl->mAbortCallBack = callback;
638 std::string Framework::GetBundleName() const
643 void Framework::SetBundleName(const std::string& name)
648 std::string Framework::GetBundleId() const
653 std::string Framework::GetResourcePath()
655 std::string resourcePath = "";
656 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
657 resourcePath = app_get_resource_path();
658 #else // For backwards compatibility with older Tizen versions
660 // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
661 const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
662 char* value = getenv( environmentVariable );
665 resourcePath = value;
667 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
672 void Framework::SetBundleId(const std::string& id)
677 void Framework::AbortCallback( )
679 // if an abort call back has been installed run it.
680 if (mImpl->mAbortCallBack)
682 CallbackBase::Execute( *mImpl->mAbortCallBack );
690 } // namespace Adaptor
692 } // namespace Internal