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 );
84 gPreInitializedApplication->CreateWindow(); // Only create window
85 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
87 //Make DefaultFontDescription cached
88 Dali::TextAbstraction::FontClient fontClient = Dali::TextAbstraction::FontClient::Get();
89 Dali::TextAbstraction::FontDescription defaultFontDescription;
90 fontClient.GetDefaultPlatformFontDescription( defaultFontDescription );
94 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
95 Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
103 mLanguageChangedSignal(),
104 mRegionChangedSignal(),
107 mEventLoop( nullptr ),
108 mFramework( nullptr ),
109 mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
110 mCommandLineOptions( nullptr ),
111 mSingletonService( SingletonService::New() ),
112 mAdaptorBuilder( nullptr ),
115 mMainWindowMode( windowMode ),
117 mMainWindowReplaced( false ),
118 mStylesheet( stylesheet ),
119 mEnvironmentOptions(),
120 mWindowPositionSize( positionSize ),
121 mLaunchpadState( Launchpad::NONE ),
122 mSlotDelegate( this ),
124 mStereoBase( DEFAULT_STEREO_BASE )
126 // Get mName from environment options
127 mMainWindowName = mEnvironmentOptions.GetWindowName();
128 if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
130 // Set mName from command-line args if environment option not set
131 mMainWindowName = (*argv)[0];
134 mCommandLineOptions = new CommandLineOptions(argc, argv);
135 mFramework = new Framework( *this, argc, argv, applicationType );
136 mUseRemoteSurface = (applicationType == Framework::WATCH);
139 Application::~Application()
141 mSingletonService.UnregisterAll();
145 delete mAdaptorBuilder;
146 delete mCommandLineOptions;
150 void Application::CreateWindow()
152 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
154 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
156 // Command line options override environment options and full screen
157 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
158 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
160 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
162 // Environment options override full screen functionality if command line arguments not provided
163 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
164 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
168 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
170 Internal::Adaptor::Window* window = Internal::Adaptor::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
171 mMainWindow = Dali::Window( window );
173 // Quit the application when the window is closed
174 GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
177 void Application::CreateAdaptor()
179 DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
181 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
183 Integration::SceneHolder sceneHolder = Integration::SceneHolder( &Dali::GetImplementation( mMainWindow ) );
185 mAdaptor = Adaptor::New( graphicsFactory, sceneHolder, mContextLossConfiguration, &mEnvironmentOptions );
187 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
189 Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
192 void Application::CreateAdaptorBuilder()
194 mAdaptorBuilder = new AdaptorBuilder();
197 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
199 mContextLossConfiguration = configuration;
201 // Run the application
205 void Application::Lower()
207 // Lower the application without quitting it.
211 void Application::Quit()
213 // Actually quit the application.
214 // Force a call to Quit even if adaptor is not running.
215 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
218 void Application::QuitFromMainLoop()
223 // This will trigger OnTerminate(), below, after the main loop has completed.
226 void Application::OnInit()
228 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
230 CreateAdaptorBuilder();
231 // If an application was pre-initialized, a window was made in advance
232 if( mLaunchpadState == Launchpad::NONE )
242 if( ! mStylesheet.empty() )
244 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
247 // Wire up the LifecycleController
248 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
250 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
251 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
252 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
253 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
254 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
255 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
256 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
258 Dali::Application application(this);
259 mInitSignal.Emit( application );
261 mAdaptor->NotifySceneCreated();
264 void Application::OnTerminate()
266 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
267 // delete the window as ecore_x has been destroyed by AppCore
269 Dali::Application application(this);
270 mTerminateSignal.Emit( application );
274 // Ensure that the render-thread is not using the surface(window) after we delete it
278 mMainWindow.Reset(); // This only resets (clears) the default Window
281 void Application::OnPause()
283 // A DALi app should handle Pause/Resume events.
284 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
285 // Pausing DALi core only occurs on the Window Hidden framework event
286 Dali::Application application(this);
287 mPauseSignal.Emit( application );
290 void Application::OnResume()
292 // Emit the signal first so the application can queue any messages before we do an update/render
293 // This ensures we do not just redraw the last frame before pausing if that's not required
294 Dali::Application application(this);
295 mResumeSignal.Emit( application );
297 // DALi just delivers the framework Resume event to the application.
298 // Resuming DALi core only occurs on the Window Show framework event
300 // Trigger processing of events queued up while paused
301 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
302 coreEventInterface.ProcessCoreEvents();
305 void Application::OnReset()
308 * usually, reset callback was called when a caller request to launch this application via aul.
309 * because Application class already handled initialization in OnInit(), OnReset do nothing.
311 Dali::Application application(this);
312 mResetSignal.Emit( application );
315 void Application::OnAppControl(void *data)
317 Dali::Application application(this);
318 mAppControlSignal.Emit( application , data );
321 void Application::OnLanguageChanged()
323 mAdaptor->NotifyLanguageChanged();
324 Dali::Application application(this);
325 mLanguageChangedSignal.Emit( application );
328 void Application::OnRegionChanged()
330 Dali::Application application(this);
331 mRegionChangedSignal.Emit( application );
334 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
336 Dali::Application application(this);
337 mBatteryLowSignal.Emit( application );
339 mLowBatterySignal.Emit( status );
342 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
344 Dali::Application application(this);
345 mMemoryLowSignal.Emit( application );
347 mLowMemorySignal.Emit( status );
350 void Application::OnSurfaceCreated( Any newSurface )
352 void* newWindow = AnyCast< void* >( newSurface );
353 void* oldWindow = AnyCast< void* >( mMainWindow.GetNativeHandle() );
354 if( oldWindow != newWindow )
356 auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
357 std::unique_ptr< WindowRenderSurface > newSurfacePtr
358 = renderSurfaceFactory->CreateWindowRenderSurface( PositionSize(), newSurface, true );
360 mAdaptor->ReplaceSurface( mMainWindow, *newSurfacePtr.release() );
364 void Application::OnSurfaceDestroyed( Any surface )
366 void* windowToDelete = AnyCast< void* >( surface );
367 void* oldWindow = AnyCast< void* >( mMainWindow.GetNativeHandle() );
368 if( oldWindow == windowToDelete )
370 mAdaptor->DeleteSurface( mAdaptor->GetSurface() );
374 void Application::OnResize(Dali::Adaptor& adaptor)
376 Dali::Application application(this);
377 mResizeSignal.Emit( application );
380 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
382 return mAdaptor->AddIdle( callback, hasReturnValue );
385 std::string Application::GetRegion() const
387 return mFramework->GetRegion();
390 std::string Application::GetLanguage() const
392 return mFramework->GetLanguage();
395 Dali::Adaptor& Application::GetAdaptor()
400 Dali::Window Application::GetWindow()
402 // Changed to return a different window handle after ReplaceWindow is called
403 // just for backward compatibility to make the test case pass
404 if ( mMainWindowReplaced )
406 Internal::Adaptor::Window* window = Internal::Adaptor::Window::New( PositionSize(), "ReplacedWindow", "", false );
407 return Dali::Window( window );
417 void Application::SetViewMode( ViewMode viewMode )
419 mViewMode = viewMode;
422 ViewMode Application::GetViewMode() const
427 void Application::SetStereoBase( float stereoBase )
429 mStereoBase = stereoBase;
432 float Application::GetStereoBase() const
437 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
439 // This API is kept just for backward compatibility to make the test case pass.
441 mMainWindowReplaced = true;
442 OnResize( *mAdaptor );
445 std::string Application::GetResourcePath()
447 return Internal::Adaptor::Framework::GetResourcePath();
450 std::string Application::GetDataPath()
452 return Internal::Adaptor::Framework::GetDataPath();
455 void Application::SetStyleSheet( const std::string& stylesheet )
457 mStylesheet = stylesheet;
460 void Application::SetCommandLineOptions( int* argc, char **argv[] )
462 delete mCommandLineOptions;
464 mCommandLineOptions = new CommandLineOptions( argc, argv );
466 mFramework->SetCommandLineOptions( argc, argv );
469 ApplicationPtr Application::GetPreInitializedApplication()
471 return gPreInitializedApplication;
474 } // namespace Adaptor
476 } // namespace Internal