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 mMainWindowReplaced( false ),
113 mStylesheet( stylesheet ),
114 mEnvironmentOptions(),
115 mWindowPositionSize( positionSize ),
116 mLaunchpadState( Launchpad::NONE ),
117 mSlotDelegate( this ),
119 mStereoBase( DEFAULT_STEREO_BASE )
121 // Get mName from environment options
122 mMainWindowName = mEnvironmentOptions.GetWindowName();
123 if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
125 // Set mName from command-line args if environment option not set
126 mMainWindowName = (*argv)[0];
129 mCommandLineOptions = new CommandLineOptions(argc, argv);
130 mFramework = new Framework( *this, argc, argv, applicationType );
131 mUseRemoteSurface = (applicationType == Framework::WATCH);
134 Application::~Application()
136 mSingletonService.UnregisterAll();
140 delete mAdaptorBuilder;
141 delete mCommandLineOptions;
145 void Application::CreateWindow()
147 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
149 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
151 // Command line options override environment options and full screen
152 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
153 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
155 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
157 // Environment options override full screen functionality if command line arguments not provided
158 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
159 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
163 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
164 mMainWindow = Dali::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
166 // Quit the application when the window is closed
167 GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
170 void Application::CreateAdaptor()
172 DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
174 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
176 mAdaptor = Dali::Internal::Adaptor::Adaptor::New( graphicsFactory, mMainWindow, mContextLossConfiguration, &mEnvironmentOptions );
178 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
180 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
183 void Application::CreateAdaptorBuilder()
185 mAdaptorBuilder = new AdaptorBuilder();
188 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
190 mContextLossConfiguration = configuration;
192 // Run the application
196 void Application::Lower()
198 // Lower the application without quitting it.
202 void Application::Quit()
204 // Actually quit the application.
205 // Force a call to Quit even if adaptor is not running.
206 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
209 void Application::QuitFromMainLoop()
214 // This will trigger OnTerminate(), below, after the main loop has completed.
217 void Application::OnInit()
219 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
221 CreateAdaptorBuilder();
223 // If an application was pre-initialized, a window was made in advance
224 if( mLaunchpadState == Launchpad::NONE )
234 // Check if user requires no vsyncing and set Adaptor
235 if (mCommandLineOptions->noVSyncOnRender)
237 mAdaptor->SetUseHardwareVSync(false);
240 if( ! mStylesheet.empty() )
242 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
245 // Wire up the LifecycleController
246 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
248 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
249 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
250 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
251 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
252 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
253 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
254 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
256 Dali::Application application(this);
257 mInitSignal.Emit( application );
259 mAdaptor->NotifySceneCreated();
262 void Application::OnTerminate()
264 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
265 // delete the window as ecore_x has been destroyed by AppCore
267 Dali::Application application(this);
268 mTerminateSignal.Emit( application );
272 // Ensure that the render-thread is not using the surface(window) after we delete it
276 mMainWindow.Reset(); // This only resets (clears) the default Window
279 void Application::OnPause()
281 // A DALi app should handle Pause/Resume events.
282 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
283 // Pausing DALi core only occurs on the Window Hidden framework event
284 Dali::Application application(this);
285 mPauseSignal.Emit( application );
288 void Application::OnResume()
290 // Emit the signal first so the application can queue any messages before we do an update/render
291 // This ensures we do not just redraw the last frame before pausing if that's not required
292 Dali::Application application(this);
293 mResumeSignal.Emit( application );
295 // DALi just delivers the framework Resume event to the application.
296 // Resuming DALi core only occurs on the Window Show framework event
298 // Trigger processing of events queued up while paused
299 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
300 coreEventInterface.ProcessCoreEvents();
303 void Application::OnReset()
306 * usually, reset callback was called when a caller request to launch this application via aul.
307 * because Application class already handled initialization in OnInit(), OnReset do nothing.
309 Dali::Application application(this);
310 mResetSignal.Emit( application );
313 void Application::OnAppControl(void *data)
315 Dali::Application application(this);
316 mAppControlSignal.Emit( application , data );
319 void Application::OnLanguageChanged()
321 mAdaptor->NotifyLanguageChanged();
322 Dali::Application application(this);
323 mLanguageChangedSignal.Emit( application );
326 void Application::OnRegionChanged()
328 Dali::Application application(this);
329 mRegionChangedSignal.Emit( application );
332 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
334 Dali::Application application(this);
335 mBatteryLowSignal.Emit( application );
337 mLowBatterySignal.Emit( status );
340 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
342 Dali::Application application(this);
343 mMemoryLowSignal.Emit( application );
345 mLowMemorySignal.Emit( status );
348 void Application::OnResize(Dali::Adaptor& adaptor)
350 Dali::Application application(this);
351 mResizeSignal.Emit( application );
354 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
356 return mAdaptor->AddIdle( callback, hasReturnValue );
359 std::string Application::GetRegion() const
361 return mFramework->GetRegion();
364 std::string Application::GetLanguage() const
366 return mFramework->GetLanguage();
369 Dali::Adaptor& Application::GetAdaptor()
374 Dali::Window Application::GetWindow()
376 // Changed to return a different window handle after ReplaceWindow is called
377 // just for backward compatibility to make the test case pass
378 return mMainWindowReplaced ? Dali::Window() : mMainWindow;
383 void Application::SetViewMode( ViewMode viewMode )
385 mViewMode = viewMode;
388 ViewMode Application::GetViewMode() const
393 void Application::SetStereoBase( float stereoBase )
395 mStereoBase = stereoBase;
398 float Application::GetStereoBase() const
403 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
405 // This API is kept just for backward compatibility to make the test case pass.
407 mMainWindowReplaced = true;
408 OnResize( *mAdaptor );
411 std::string Application::GetResourcePath()
413 return Internal::Adaptor::Framework::GetResourcePath();
416 std::string Application::GetDataPath()
418 return Internal::Adaptor::Framework::GetDataPath();
421 void Application::SetStyleSheet( const std::string& stylesheet )
423 mStylesheet = stylesheet;
426 ApplicationPtr Application::GetPreInitializedApplication()
428 return gPreInitializedApplication;
431 } // namespace Adaptor
433 } // namespace Internal