2 * Copyright (c) 2017 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 "application-impl.h"
22 #include <dali/integration-api/debug.h>
25 #include <style-monitor.h>
26 #include <command-line-options.h>
27 #include <common/adaptor-impl.h>
28 #include <common/framework.h>
29 #include <singleton-service-impl.h>
30 #include <lifecycle-controller-impl.h>
35 namespace TizenPlatform
37 class TizenPlatformAbstraction;
51 ApplicationPtr Application::gPreInitializedApplication( NULL );
53 ApplicationPtr Application::New(
56 const std::string& stylesheet,
57 Dali::Application::WINDOW_MODE windowMode,
58 const PositionSize& positionSize,
59 Framework::Type applicationType)
61 ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
65 void Application::PreInitialize( int* argc, char** argv[] )
67 if( !gPreInitializedApplication )
69 gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
71 gPreInitializedApplication->CreateWindow(); // Only create window
75 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
76 Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
84 mLanguageChangedSignal(),
85 mRegionChangedSignal(),
90 mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
91 mCommandLineOptions( NULL ),
92 mSingletonService( SingletonService::New() ),
95 mWindowMode( windowMode ),
97 mStylesheet( stylesheet ),
98 mEnvironmentOptions(),
99 mWindowPositionSize( positionSize ),
100 mLaunchpadState( Launchpad::NONE ),
101 mSlotDelegate( this )
103 // Get mName from environment options
104 mName = mEnvironmentOptions.GetWindowName();
105 if( mName.empty() && argc && ( *argc > 0 ) )
107 // Set mName from command-line args if environment option not set
111 mCommandLineOptions = new CommandLineOptions(argc, argv);
112 mFramework = new Framework( *this, argc, argv, applicationType );
113 mUseRemoteSurface = (applicationType == Framework::WATCH);
116 Application::~Application()
118 mSingletonService.UnregisterAll();
122 delete mCommandLineOptions;
126 void Application::CreateWindow()
128 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
130 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
132 // Command line options override environment options and full screen
133 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
134 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
136 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
138 // Environment options override full screen functionality if command line arguments not provided
139 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
140 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
144 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
145 mWindow = Dali::Window::New( mWindowPositionSize, mName, windowClassName, mWindowMode == Dali::Application::TRANSPARENT );
147 // Quit the application when the window is closed
148 GetImplementation( mWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
151 void Application::CreateAdaptor()
153 DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
155 mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
157 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
159 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
162 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
164 mContextLossConfiguration = configuration;
166 // Run the application
170 void Application::Lower()
172 // Lower the application without quitting it.
176 void Application::Quit()
178 // Actually quit the application.
179 AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
182 void Application::QuitFromMainLoop()
187 // This will trigger OnTerminate(), below, after the main loop has completed.
190 void Application::DoInit()
192 // If an application was pre-initialized, a window was made in advance
193 if( mLaunchpadState == Launchpad::NONE )
203 // Check if user requires no vsyncing and set Adaptor
204 if (mCommandLineOptions->noVSyncOnRender)
206 mAdaptor->SetUseHardwareVSync(false);
209 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
210 if( mCommandLineOptions->viewMode != 0 )
212 ViewMode viewMode = MONO;
213 if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
215 viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
217 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
220 if( ! mStylesheet.empty() )
222 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
226 void Application::DoStart()
228 mAdaptor->NotifySceneCreated();
231 void Application::DoTerminate()
235 // Ensure that the render-thread is not using the surface(window) after we delete it
242 void Application::DoPause()
247 void Application::DoResume()
252 void Application::DoLanguageChange()
254 mAdaptor->NotifyLanguageChanged();
257 void Application::OnInit()
259 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
263 // Wire up the LifecycleController
264 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
266 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
267 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
268 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
269 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
270 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
271 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
272 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
274 Dali::Application application(this);
275 mInitSignal.Emit( application );
280 void Application::OnTerminate()
282 // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
283 // delete the window as ecore_x has been destroyed by AppCore
285 Dali::Application application(this);
286 mTerminateSignal.Emit( application );
291 void Application::OnPause()
293 // A DALi app should handle Pause/Resume events.
294 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
295 // Pausing DALi core only occurs on the Window Hidden framework event
296 Dali::Application application(this);
297 mPauseSignal.Emit( application );
300 void Application::OnResume()
302 // Emit the signal first so the application can queue any messages before we do an update/render
303 // This ensures we do not just redraw the last frame before pausing if that's not required
304 Dali::Application application(this);
305 mResumeSignal.Emit( application );
307 // DALi just delivers the framework Resume event to the application.
308 // Resuming DALi core only occurs on the Window Show framework event
311 void Application::OnReset()
314 * usually, reset callback was called when a caller request to launch this application via aul.
315 * because Application class already handled initialization in OnInit(), OnReset do nothing.
317 Dali::Application application(this);
318 mResetSignal.Emit( application );
321 void Application::OnAppControl(void *data)
323 Dali::Application application(this);
324 mAppControlSignal.Emit( application , data );
327 void Application::OnLanguageChanged()
330 Dali::Application application(this);
331 mLanguageChangedSignal.Emit( application );
334 void Application::OnRegionChanged()
336 Dali::Application application(this);
337 mRegionChangedSignal.Emit( application );
340 void Application::OnBatteryLow()
342 Dali::Application application(this);
343 mBatteryLowSignal.Emit( application );
346 void Application::OnMemoryLow()
348 Dali::Application application(this);
349 mMemoryLowSignal.Emit( application );
352 void Application::OnResize(Dali::Adaptor& adaptor)
354 Dali::Application application(this);
355 mResizeSignal.Emit( application );
358 bool Application::AddIdle( CallbackBase* callback )
360 return mAdaptor->AddIdle( callback );
363 Dali::Adaptor& Application::GetAdaptor()
368 Dali::Window Application::GetWindow()
375 void Application::SetViewMode( ViewMode viewMode )
377 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
380 ViewMode Application::GetViewMode() const
382 return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
385 void Application::SetStereoBase( float stereoBase )
387 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
390 float Application::GetStereoBase() const
392 return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
396 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
398 Dali::Window newWindow = Dali::Window::New( positionSize, name, mWindowMode == Dali::Application::TRANSPARENT );
399 Window& windowImpl = GetImplementation(newWindow);
400 windowImpl.SetAdaptor(*mAdaptor);
401 newWindow.ShowIndicator(Dali::Window::INVISIBLE);
402 Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
404 Any nativeWindow = newWindow.GetNativeHandle();
405 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
407 mWindowPositionSize = positionSize;
410 std::string Application::GetResourcePath()
412 return Internal::Adaptor::Framework::GetResourcePath();
415 void Application::SetStyleSheet( const std::string& stylesheet )
417 mStylesheet = stylesheet;
421 ApplicationPtr Application::GetPreInitializedApplication()
423 return gPreInitializedApplication;
426 } // namespace Adaptor
428 } // namespace Internal