2 * Copyright (c) 2018 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 <dali/internal/adaptor/common/application-impl.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali/devel-api/adaptor-framework/style-monitor.h>
26 #include <dali/internal/system/common/command-line-options.h>
27 #include <dali/internal/adaptor/common/framework.h>
28 #include <dali/internal/system/common/singleton-service-impl.h>
29 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
30 #include <dali/internal/window-system/common/window-render-surface.h>
35 namespace TizenPlatform
37 class TizenPlatformAbstraction;
54 const float DEFAULT_STEREO_BASE( 65.0f );
56 } // unnamed namespace
58 ApplicationPtr Application::gPreInitializedApplication( NULL );
60 ApplicationPtr Application::New(
63 const std::string& stylesheet,
64 Dali::Application::WINDOW_MODE windowMode,
65 const PositionSize& positionSize,
66 Framework::Type applicationType)
68 ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
72 void Application::PreInitialize( int* argc, char** argv[] )
74 if( !gPreInitializedApplication )
76 gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
78 gPreInitializedApplication->CreateWindow(); // Only create window
80 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
84 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
85 Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
93 mLanguageChangedSignal(),
94 mRegionChangedSignal(),
97 mEventLoop( nullptr ),
98 mFramework( nullptr ),
99 mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
100 mCommandLineOptions( nullptr ),
101 mSingletonService( SingletonService::New() ),
102 mAdaptorBuilder( nullptr ),
105 mMainWindowMode( windowMode ),
107 mStylesheet( stylesheet ),
108 mEnvironmentOptions(),
109 mWindowPositionSize( positionSize ),
110 mLaunchpadState( Launchpad::NONE ),
111 mSlotDelegate( this ),
113 mStereoBase( DEFAULT_STEREO_BASE )
115 // Get mName from environment options
116 mMainWindowName = mEnvironmentOptions.GetWindowName();
117 if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
119 // Set mName from command-line args if environment option not set
120 mMainWindowName = (*argv)[0];
123 mCommandLineOptions = new CommandLineOptions(argc, argv);
124 mFramework = new Framework( *this, argc, argv, applicationType );
125 mUseRemoteSurface = (applicationType == Framework::WATCH);
128 Application::~Application()
130 mSingletonService.UnregisterAll();
134 delete mAdaptorBuilder;
135 delete mCommandLineOptions;
139 void Application::CreateWindow()
141 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
143 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
145 // Command line options override environment options and full screen
146 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
147 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
149 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
151 // Environment options override full screen functionality if command line arguments not provided
152 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
153 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
157 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
158 mMainWindow = Dali::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
160 // Quit the application when the window is closed
161 GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
164 void Application::CreateAdaptor()
166 DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
168 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
170 mAdaptor = Dali::Internal::Adaptor::Adaptor::New( graphicsFactory, mMainWindow, mContextLossConfiguration, &mEnvironmentOptions );
172 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
174 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
177 void Application::CreateAdaptorBuilder()
179 mAdaptorBuilder = new AdaptorBuilder();
182 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
184 mContextLossConfiguration = configuration;
186 // Run the application
190 void Application::Lower()
192 // Lower the application without quitting it.
196 void Application::Quit()
198 // Actually quit the application.
199 // Force a call to Quit even if adaptor is not running.
200 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
203 void Application::QuitFromMainLoop()
208 // This will trigger OnTerminate(), below, after the main loop has completed.
211 void Application::OnInit()
213 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
215 CreateAdaptorBuilder();
217 // If an application was pre-initialized, a window was made in advance
218 if( mLaunchpadState == Launchpad::NONE )
228 // Check if user requires no vsyncing and set Adaptor
229 if (mCommandLineOptions->noVSyncOnRender)
231 mAdaptor->SetUseHardwareVSync(false);
234 if( ! mStylesheet.empty() )
236 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
239 // Wire up the LifecycleController
240 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
242 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
243 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
244 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
245 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
246 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
247 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
248 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
250 Dali::Application application(this);
251 mInitSignal.Emit( application );
253 mAdaptor->NotifySceneCreated();
256 void Application::OnTerminate()
258 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
259 // delete the window as ecore_x has been destroyed by AppCore
261 Dali::Application application(this);
262 mTerminateSignal.Emit( application );
266 // Ensure that the render-thread is not using the surface(window) after we delete it
270 mMainWindow.Reset(); // This only resets (clears) the default Window
273 void Application::OnPause()
275 // A DALi app should handle Pause/Resume events.
276 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
277 // Pausing DALi core only occurs on the Window Hidden framework event
278 Dali::Application application(this);
279 mPauseSignal.Emit( application );
282 void Application::OnResume()
284 // Emit the signal first so the application can queue any messages before we do an update/render
285 // This ensures we do not just redraw the last frame before pausing if that's not required
286 Dali::Application application(this);
287 mResumeSignal.Emit( application );
289 // DALi just delivers the framework Resume event to the application.
290 // Resuming DALi core only occurs on the Window Show framework event
292 // Trigger processing of events queued up while paused
293 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
294 coreEventInterface.ProcessCoreEvents();
297 void Application::OnReset()
300 * usually, reset callback was called when a caller request to launch this application via aul.
301 * because Application class already handled initialization in OnInit(), OnReset do nothing.
303 Dali::Application application(this);
304 mResetSignal.Emit( application );
307 void Application::OnAppControl(void *data)
309 Dali::Application application(this);
310 mAppControlSignal.Emit( application , data );
313 void Application::OnLanguageChanged()
315 mAdaptor->NotifyLanguageChanged();
316 Dali::Application application(this);
317 mLanguageChangedSignal.Emit( application );
320 void Application::OnRegionChanged()
322 Dali::Application application(this);
323 mRegionChangedSignal.Emit( application );
326 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
328 Dali::Application application(this);
329 mBatteryLowSignal.Emit( application );
331 mLowBatterySignal.Emit( status );
334 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
336 Dali::Application application(this);
337 mMemoryLowSignal.Emit( application );
339 mLowMemorySignal.Emit( status );
341 void Application::OnResize(Dali::Adaptor& adaptor)
343 Dali::Application application(this);
344 mResizeSignal.Emit( application );
347 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
349 return mAdaptor->AddIdle( callback, hasReturnValue );
352 std::string Application::GetRegion() const
354 return mFramework->GetRegion();
357 std::string Application::GetLanguage() const
359 return mFramework->GetLanguage();
362 Dali::Adaptor& Application::GetAdaptor()
367 Dali::Window Application::GetWindow()
374 void Application::SetViewMode( ViewMode viewMode )
376 mViewMode = viewMode;
379 ViewMode Application::GetViewMode() const
384 void Application::SetStereoBase( float stereoBase )
386 mStereoBase = stereoBase;
389 float Application::GetStereoBase() const
394 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
396 Dali::Window newWindow = Dali::Window::New( positionSize, name, mMainWindowMode == Dali::Application::TRANSPARENT );
397 Window& windowImpl = GetImplementation(newWindow);
398 windowImpl.SetAdaptor(*mAdaptor);
400 Internal::Adaptor::WindowRenderSurface* renderSurface = windowImpl.GetSurface();
402 Any nativeWindow = newWindow.GetNativeHandle();
403 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
404 mMainWindow = newWindow;
405 mWindowPositionSize = positionSize;
408 std::string Application::GetResourcePath()
410 return Internal::Adaptor::Framework::GetResourcePath();
413 std::string Application::GetDataPath()
415 return Internal::Adaptor::Framework::GetDataPath();
418 void Application::SetStyleSheet( const std::string& stylesheet )
420 mStylesheet = stylesheet;
423 ApplicationPtr Application::GetPreInitializedApplication()
425 return gPreInitializedApplication;
428 } // namespace Adaptor
430 } // namespace Internal