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-impl.h>
31 #include <dali/internal/window-system/common/window-render-surface.h>
32 #include <dali/internal/window-system/common/render-surface-factory.h>
34 // To disable a macro with the same name from one of OpenGL headers
40 namespace TizenPlatform
42 class TizenPlatformAbstraction;
59 const float DEFAULT_STEREO_BASE( 65.0f );
61 } // unnamed namespace
63 ApplicationPtr Application::gPreInitializedApplication( NULL );
65 ApplicationPtr Application::New(
68 const std::string& stylesheet,
69 Dali::Application::WINDOW_MODE windowMode,
70 const PositionSize& positionSize,
71 Framework::Type applicationType)
73 ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
77 void Application::PreInitialize( int* argc, char** argv[] )
79 if( !gPreInitializedApplication )
81 gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
83 gPreInitializedApplication->CreateWindow(); // Only create window
85 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
89 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
90 Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
98 mLanguageChangedSignal(),
99 mRegionChangedSignal(),
102 mEventLoop( nullptr ),
103 mFramework( nullptr ),
104 mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
105 mCommandLineOptions( nullptr ),
106 mSingletonService( SingletonService::New() ),
107 mAdaptorBuilder( nullptr ),
110 mMainWindowMode( windowMode ),
112 mStylesheet( stylesheet ),
113 mEnvironmentOptions(),
114 mWindowPositionSize( positionSize ),
115 mLaunchpadState( Launchpad::NONE ),
116 mSlotDelegate( this ),
118 mStereoBase( DEFAULT_STEREO_BASE )
120 // Get mName from environment options
121 mMainWindowName = mEnvironmentOptions.GetWindowName();
122 if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
124 // Set mName from command-line args if environment option not set
125 mMainWindowName = (*argv)[0];
128 mCommandLineOptions = new CommandLineOptions(argc, argv);
129 mFramework = new Framework( *this, argc, argv, applicationType );
130 mUseRemoteSurface = (applicationType == Framework::WATCH);
133 Application::~Application()
135 mSingletonService.UnregisterAll();
139 delete mAdaptorBuilder;
140 delete mCommandLineOptions;
144 void Application::CreateWindow()
146 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
148 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
150 // Command line options override environment options and full screen
151 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
152 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
154 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
156 // Environment options override full screen functionality if command line arguments not provided
157 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
158 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
162 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
163 mMainWindow = Dali::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
165 // Quit the application when the window is closed
166 GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
169 void Application::CreateAdaptor()
171 DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
173 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
175 mAdaptor = Dali::Internal::Adaptor::Adaptor::New( graphicsFactory, mMainWindow, mContextLossConfiguration, &mEnvironmentOptions );
177 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
179 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
182 void Application::CreateAdaptorBuilder()
184 mAdaptorBuilder = new AdaptorBuilder();
187 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
189 mContextLossConfiguration = configuration;
191 // Run the application
195 void Application::Lower()
197 // Lower the application without quitting it.
201 void Application::Quit()
203 // Actually quit the application.
204 // Force a call to Quit even if adaptor is not running.
205 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
208 void Application::QuitFromMainLoop()
213 // This will trigger OnTerminate(), below, after the main loop has completed.
216 void Application::OnInit()
218 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
220 CreateAdaptorBuilder();
222 // If an application was pre-initialized, a window was made in advance
223 if( mLaunchpadState == Launchpad::NONE )
233 // Check if user requires no vsyncing and set Adaptor
234 if (mCommandLineOptions->noVSyncOnRender)
236 mAdaptor->SetUseHardwareVSync(false);
239 if( ! mStylesheet.empty() )
241 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
244 // Wire up the LifecycleController
245 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
247 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
248 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
249 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
250 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
251 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
252 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
253 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
255 Dali::Application application(this);
256 mInitSignal.Emit( application );
258 mAdaptor->NotifySceneCreated();
261 void Application::OnTerminate()
263 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
264 // delete the window as ecore_x has been destroyed by AppCore
266 Dali::Application application(this);
267 mTerminateSignal.Emit( application );
271 // Ensure that the render-thread is not using the surface(window) after we delete it
275 mMainWindow.Reset(); // This only resets (clears) the default Window
278 void Application::OnPause()
280 // A DALi app should handle Pause/Resume events.
281 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
282 // Pausing DALi core only occurs on the Window Hidden framework event
283 Dali::Application application(this);
284 mPauseSignal.Emit( application );
287 void Application::OnResume()
289 // Emit the signal first so the application can queue any messages before we do an update/render
290 // This ensures we do not just redraw the last frame before pausing if that's not required
291 Dali::Application application(this);
292 mResumeSignal.Emit( application );
294 // DALi just delivers the framework Resume event to the application.
295 // Resuming DALi core only occurs on the Window Show framework event
297 // Trigger processing of events queued up while paused
298 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
299 coreEventInterface.ProcessCoreEvents();
302 void Application::OnReset()
305 * usually, reset callback was called when a caller request to launch this application via aul.
306 * because Application class already handled initialization in OnInit(), OnReset do nothing.
308 Dali::Application application(this);
309 mResetSignal.Emit( application );
312 void Application::OnAppControl(void *data)
314 Dali::Application application(this);
315 mAppControlSignal.Emit( application , data );
318 void Application::OnLanguageChanged()
320 mAdaptor->NotifyLanguageChanged();
321 Dali::Application application(this);
322 mLanguageChangedSignal.Emit( application );
325 void Application::OnRegionChanged()
327 Dali::Application application(this);
328 mRegionChangedSignal.Emit( application );
331 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
333 Dali::Application application(this);
334 mBatteryLowSignal.Emit( application );
336 mLowBatterySignal.Emit( status );
339 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
341 Dali::Application application(this);
342 mMemoryLowSignal.Emit( application );
344 mLowMemorySignal.Emit( status );
346 void Application::OnResize(Dali::Adaptor& adaptor)
348 Dali::Application application(this);
349 mResizeSignal.Emit( application );
352 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
354 return mAdaptor->AddIdle( callback, hasReturnValue );
357 std::string Application::GetRegion() const
359 return mFramework->GetRegion();
362 std::string Application::GetLanguage() const
364 return mFramework->GetLanguage();
367 Dali::Adaptor& Application::GetAdaptor()
372 Dali::Window Application::GetWindow()
379 void Application::SetViewMode( ViewMode viewMode )
381 mViewMode = viewMode;
384 ViewMode Application::GetViewMode() const
389 void Application::SetStereoBase( float stereoBase )
391 mStereoBase = stereoBase;
394 float Application::GetStereoBase() const
399 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
402 auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
403 std::unique_ptr<Internal::Adaptor::WindowRenderSurface> renderSurface =
404 renderSurfaceFactory->CreateWindowRenderSurface( positionSize, surface, false );
406 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface( mMainWindow, *renderSurface.release() );
407 mWindowPositionSize = positionSize;
410 std::string Application::GetResourcePath()
412 return Internal::Adaptor::Framework::GetResourcePath();
415 std::string Application::GetDataPath()
417 return Internal::Adaptor::Framework::GetDataPath();
420 void Application::SetStyleSheet( const std::string& stylesheet )
422 mStylesheet = stylesheet;
425 ApplicationPtr Application::GetPreInitializedApplication()
427 return gPreInitializedApplication;
430 } // namespace Adaptor
432 } // namespace Internal