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>
38 namespace TizenPlatform
40 class TizenPlatformAbstraction;
57 const float DEFAULT_STEREO_BASE( 65.0f );
59 } // unnamed namespace
61 ApplicationPtr Application::gPreInitializedApplication( NULL );
63 ApplicationPtr Application::New(
66 const std::string& stylesheet,
67 Dali::Application::WINDOW_MODE windowMode,
68 const PositionSize& positionSize,
69 Framework::Type applicationType)
71 ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
75 void Application::PreInitialize( int* argc, char** argv[] )
77 if( !gPreInitializedApplication )
79 gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
81 gPreInitializedApplication->CreateWindow(); // Only create window
83 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
87 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
88 Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
96 mLanguageChangedSignal(),
97 mRegionChangedSignal(),
100 mEventLoop( nullptr ),
101 mFramework( nullptr ),
102 mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
103 mCommandLineOptions( nullptr ),
104 mSingletonService( SingletonService::New() ),
105 mAdaptorBuilder( nullptr ),
108 mMainWindowMode( windowMode ),
110 mStylesheet( stylesheet ),
111 mEnvironmentOptions(),
112 mWindowPositionSize( positionSize ),
113 mLaunchpadState( Launchpad::NONE ),
114 mSlotDelegate( this ),
116 mStereoBase( DEFAULT_STEREO_BASE )
118 // Get mName from environment options
119 mMainWindowName = mEnvironmentOptions.GetWindowName();
120 if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
122 // Set mName from command-line args if environment option not set
123 mMainWindowName = (*argv)[0];
126 mCommandLineOptions = new CommandLineOptions(argc, argv);
127 mFramework = new Framework( *this, argc, argv, applicationType );
128 mUseRemoteSurface = (applicationType == Framework::WATCH);
131 Application::~Application()
133 mSingletonService.UnregisterAll();
137 delete mAdaptorBuilder;
138 delete mCommandLineOptions;
142 void Application::CreateWindow()
144 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
146 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
148 // Command line options override environment options and full screen
149 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
150 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
152 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
154 // Environment options override full screen functionality if command line arguments not provided
155 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
156 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
160 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
161 mMainWindow = Dali::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
163 int indicatorVisibleMode = mEnvironmentOptions.GetIndicatorVisibleMode();
164 if( indicatorVisibleMode >= Dali::Window::INVISIBLE && indicatorVisibleMode <= Dali::Window::AUTO )
166 GetImplementation( mMainWindow ).SetIndicatorVisibleMode( static_cast< Dali::Window::IndicatorVisibleMode >( indicatorVisibleMode ) );
169 // Quit the application when the window is closed
170 GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
173 void Application::CreateAdaptor()
175 DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
177 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
179 mAdaptor = Dali::Internal::Adaptor::Adaptor::New( graphicsFactory, mMainWindow, mContextLossConfiguration, &mEnvironmentOptions );
181 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
183 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
186 void Application::CreateAdaptorBuilder()
188 mAdaptorBuilder = new AdaptorBuilder();
191 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
193 mContextLossConfiguration = configuration;
195 // Run the application
199 void Application::Lower()
201 // Lower the application without quitting it.
205 void Application::Quit()
207 // Actually quit the application.
208 // Force a call to Quit even if adaptor is not running.
209 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
212 void Application::QuitFromMainLoop()
217 // This will trigger OnTerminate(), below, after the main loop has completed.
220 void Application::OnInit()
222 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
224 CreateAdaptorBuilder();
226 // If an application was pre-initialized, a window was made in advance
227 if( mLaunchpadState == Launchpad::NONE )
237 // Check if user requires no vsyncing and set Adaptor
238 if (mCommandLineOptions->noVSyncOnRender)
240 mAdaptor->SetUseHardwareVSync(false);
243 if( ! mStylesheet.empty() )
245 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
248 // Wire up the LifecycleController
249 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
251 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
252 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
253 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
254 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
255 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
256 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
257 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
259 Dali::Application application(this);
260 mInitSignal.Emit( application );
262 mAdaptor->NotifySceneCreated();
265 void Application::OnTerminate()
267 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
268 // delete the window as ecore_x has been destroyed by AppCore
270 Dali::Application application(this);
271 mTerminateSignal.Emit( application );
275 // Ensure that the render-thread is not using the surface(window) after we delete it
279 mMainWindow.Reset(); // This only resets (clears) the default Window
282 void Application::OnPause()
284 // A DALi app should handle Pause/Resume events.
285 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
286 // Pausing DALi core only occurs on the Window Hidden framework event
287 Dali::Application application(this);
288 mPauseSignal.Emit( application );
291 void Application::OnResume()
293 // Emit the signal first so the application can queue any messages before we do an update/render
294 // This ensures we do not just redraw the last frame before pausing if that's not required
295 Dali::Application application(this);
296 mResumeSignal.Emit( application );
298 // DALi just delivers the framework Resume event to the application.
299 // Resuming DALi core only occurs on the Window Show framework event
301 // Trigger processing of events queued up while paused
302 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
303 coreEventInterface.ProcessCoreEvents();
306 void Application::OnReset()
309 * usually, reset callback was called when a caller request to launch this application via aul.
310 * because Application class already handled initialization in OnInit(), OnReset do nothing.
312 Dali::Application application(this);
313 mResetSignal.Emit( application );
316 void Application::OnAppControl(void *data)
318 Dali::Application application(this);
319 mAppControlSignal.Emit( application , data );
322 void Application::OnLanguageChanged()
324 mAdaptor->NotifyLanguageChanged();
325 Dali::Application application(this);
326 mLanguageChangedSignal.Emit( application );
329 void Application::OnRegionChanged()
331 Dali::Application application(this);
332 mRegionChangedSignal.Emit( application );
335 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
337 Dali::Application application(this);
338 mBatteryLowSignal.Emit( application );
340 mLowBatterySignal.Emit( status );
343 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
345 Dali::Application application(this);
346 mMemoryLowSignal.Emit( application );
348 mLowMemorySignal.Emit( status );
350 void Application::OnResize(Dali::Adaptor& adaptor)
352 Dali::Application application(this);
353 mResizeSignal.Emit( application );
356 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
358 return mAdaptor->AddIdle( callback, hasReturnValue );
361 std::string Application::GetRegion() const
363 return mFramework->GetRegion();
366 std::string Application::GetLanguage() const
368 return mFramework->GetLanguage();
371 Dali::Adaptor& Application::GetAdaptor()
376 Dali::Window Application::GetWindow()
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 Dali::Window newWindow = Dali::Window::New( positionSize, name, mMainWindowMode == Dali::Application::TRANSPARENT );
406 Window& windowImpl = GetImplementation(newWindow);
407 windowImpl.SetAdaptor(*mAdaptor);
409 int indicatorVisibleMode = mEnvironmentOptions.GetIndicatorVisibleMode();
410 if( indicatorVisibleMode >= Dali::Window::INVISIBLE && indicatorVisibleMode <= Dali::Window::AUTO )
412 GetImplementation( newWindow ).SetIndicatorVisibleMode( static_cast< Dali::Window::IndicatorVisibleMode >( indicatorVisibleMode ) );
415 Internal::Adaptor::WindowRenderSurface* renderSurface = windowImpl.GetSurface();
417 Any nativeWindow = newWindow.GetNativeHandle();
418 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
419 mMainWindow = newWindow;
420 mWindowPositionSize = positionSize;
423 std::string Application::GetResourcePath()
425 return Internal::Adaptor::Framework::GetResourcePath();
428 std::string Application::GetDataPath()
430 return Internal::Adaptor::Framework::GetDataPath();
433 void Application::SetStyleSheet( const std::string& stylesheet )
435 mStylesheet = stylesheet;
438 ApplicationPtr Application::GetPreInitializedApplication()
440 return gPreInitializedApplication;
443 } // namespace Adaptor
445 } // namespace Internal