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/devel-api/text-abstraction/font-client.h>
27 #include <dali/internal/adaptor/common/adaptor-impl.h>
28 #include <dali/internal/system/common/command-line-options.h>
29 #include <dali/internal/adaptor/common/framework.h>
30 #include <dali/internal/system/common/singleton-service-impl.h>
31 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
32 #include <dali/internal/window-system/common/window-impl.h>
33 #include <dali/internal/window-system/common/window-render-surface.h>
34 #include <dali/internal/window-system/common/render-surface-factory.h>
36 // To disable a macro with the same name from one of OpenGL headers
42 namespace TizenPlatform
44 class TizenPlatformAbstraction;
61 const float DEFAULT_STEREO_BASE( 65.0f );
63 } // unnamed namespace
65 ApplicationPtr Application::gPreInitializedApplication( NULL );
67 ApplicationPtr Application::New(
70 const std::string& stylesheet,
71 Dali::Application::WINDOW_MODE windowMode,
72 const PositionSize& positionSize,
73 Framework::Type applicationType)
75 ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
79 void Application::PreInitialize( int* argc, char** argv[] )
81 if( !gPreInitializedApplication )
83 gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
85 gPreInitializedApplication->CreateWindow(); // Only create window
87 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
89 //Make DefaultFontDescription cached
90 Dali::TextAbstraction::FontClient fontClient = Dali::TextAbstraction::FontClient::Get();
91 Dali::TextAbstraction::FontDescription defaultFontDescription;
92 fontClient.GetDefaultPlatformFontDescription( defaultFontDescription );
96 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
97 Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
105 mLanguageChangedSignal(),
106 mRegionChangedSignal(),
109 mEventLoop( nullptr ),
110 mFramework( nullptr ),
111 mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
112 mCommandLineOptions( nullptr ),
113 mSingletonService( SingletonService::New() ),
114 mAdaptorBuilder( nullptr ),
117 mMainWindowMode( windowMode ),
119 mMainWindowReplaced( false ),
120 mStylesheet( stylesheet ),
121 mEnvironmentOptions(),
122 mWindowPositionSize( positionSize ),
123 mLaunchpadState( Launchpad::NONE ),
124 mSlotDelegate( this ),
126 mStereoBase( DEFAULT_STEREO_BASE )
128 // Get mName from environment options
129 mMainWindowName = mEnvironmentOptions.GetWindowName();
130 if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
132 // Set mName from command-line args if environment option not set
133 mMainWindowName = (*argv)[0];
136 mCommandLineOptions = new CommandLineOptions(argc, argv);
137 mFramework = new Framework( *this, argc, argv, applicationType );
138 mUseRemoteSurface = (applicationType == Framework::WATCH);
141 Application::~Application()
143 mSingletonService.UnregisterAll();
147 delete mAdaptorBuilder;
148 delete mCommandLineOptions;
152 void Application::CreateWindow()
154 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
156 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
158 // Command line options override environment options and full screen
159 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
160 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
162 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
164 // Environment options override full screen functionality if command line arguments not provided
165 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
166 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
170 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
172 Internal::Adaptor::Window* window = Internal::Adaptor::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
173 mMainWindow = Dali::Window( window );
175 // Quit the application when the window is closed
176 GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
179 void Application::CreateAdaptor()
181 DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
183 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
185 Integration::SceneHolder sceneHolder = Integration::SceneHolder( &Dali::GetImplementation( mMainWindow ) );
187 mAdaptor = Adaptor::New( graphicsFactory, sceneHolder, mContextLossConfiguration, &mEnvironmentOptions );
189 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
191 Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
194 void Application::CreateAdaptorBuilder()
196 mAdaptorBuilder = new AdaptorBuilder();
199 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
201 mContextLossConfiguration = configuration;
203 // Run the application
207 void Application::Lower()
209 // Lower the application without quitting it.
213 void Application::Quit()
215 // Actually quit the application.
216 // Force a call to Quit even if adaptor is not running.
217 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
220 void Application::QuitFromMainLoop()
225 // This will trigger OnTerminate(), below, after the main loop has completed.
228 void Application::OnInit()
230 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
232 CreateAdaptorBuilder();
234 // If an application was pre-initialized, a window was made in advance
235 if( mLaunchpadState == Launchpad::NONE )
245 if( ! mStylesheet.empty() )
247 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
250 // Wire up the LifecycleController
251 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
253 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
254 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
255 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
256 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
257 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
258 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
259 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
261 Dali::Application application(this);
262 mInitSignal.Emit( application );
264 mAdaptor->NotifySceneCreated();
267 void Application::OnTerminate()
269 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
270 // delete the window as ecore_x has been destroyed by AppCore
272 Dali::Application application(this);
273 mTerminateSignal.Emit( application );
277 // Ensure that the render-thread is not using the surface(window) after we delete it
281 mMainWindow.Reset(); // This only resets (clears) the default Window
284 void Application::OnPause()
286 // A DALi app should handle Pause/Resume events.
287 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
288 // Pausing DALi core only occurs on the Window Hidden framework event
289 Dali::Application application(this);
290 mPauseSignal.Emit( application );
293 void Application::OnResume()
295 // Emit the signal first so the application can queue any messages before we do an update/render
296 // This ensures we do not just redraw the last frame before pausing if that's not required
297 Dali::Application application(this);
298 mResumeSignal.Emit( application );
300 // DALi just delivers the framework Resume event to the application.
301 // Resuming DALi core only occurs on the Window Show framework event
303 // Trigger processing of events queued up while paused
304 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
305 coreEventInterface.ProcessCoreEvents();
308 void Application::OnReset()
311 * usually, reset callback was called when a caller request to launch this application via aul.
312 * because Application class already handled initialization in OnInit(), OnReset do nothing.
314 Dali::Application application(this);
315 mResetSignal.Emit( application );
318 void Application::OnAppControl(void *data)
320 Dali::Application application(this);
321 mAppControlSignal.Emit( application , data );
324 void Application::OnLanguageChanged()
326 mAdaptor->NotifyLanguageChanged();
327 Dali::Application application(this);
328 mLanguageChangedSignal.Emit( application );
331 void Application::OnRegionChanged()
333 Dali::Application application(this);
334 mRegionChangedSignal.Emit( application );
337 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
339 Dali::Application application(this);
340 mBatteryLowSignal.Emit( application );
342 mLowBatterySignal.Emit( status );
345 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
347 Dali::Application application(this);
348 mMemoryLowSignal.Emit( application );
350 mLowMemorySignal.Emit( status );
353 void Application::OnResize(Dali::Adaptor& adaptor)
355 Dali::Application application(this);
356 mResizeSignal.Emit( application );
359 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
361 return mAdaptor->AddIdle( callback, hasReturnValue );
364 std::string Application::GetRegion() const
366 return mFramework->GetRegion();
369 std::string Application::GetLanguage() const
371 return mFramework->GetLanguage();
374 Dali::Adaptor& Application::GetAdaptor()
379 Dali::Window Application::GetWindow()
381 // Changed to return a different window handle after ReplaceWindow is called
382 // just for backward compatibility to make the test case pass
383 if ( mMainWindowReplaced )
385 Internal::Adaptor::Window* window = Internal::Adaptor::Window::New( PositionSize(), "ReplacedWindow", "", false );
386 return Dali::Window( window );
396 void Application::SetViewMode( ViewMode viewMode )
398 mViewMode = viewMode;
401 ViewMode Application::GetViewMode() const
406 void Application::SetStereoBase( float stereoBase )
408 mStereoBase = stereoBase;
411 float Application::GetStereoBase() const
416 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
418 // This API is kept just for backward compatibility to make the test case pass.
420 mMainWindowReplaced = true;
421 OnResize( *mAdaptor );
424 std::string Application::GetResourcePath()
426 return Internal::Adaptor::Framework::GetResourcePath();
429 std::string Application::GetDataPath()
431 return Internal::Adaptor::Framework::GetDataPath();
434 void Application::SetStyleSheet( const std::string& stylesheet )
436 mStylesheet = stylesheet;
439 void Application::SetCommandLineOptions( int* argc, char **argv[] )
441 delete mCommandLineOptions;
443 mCommandLineOptions = new CommandLineOptions( argc, argv );
445 mFramework->SetCommandLineOptions( argc, argv );
448 ApplicationPtr Application::GetPreInitializedApplication()
450 return gPreInitializedApplication;
453 } // namespace Adaptor
455 } // namespace Internal