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
73 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
77 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
78 Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
86 mLanguageChangedSignal(),
87 mRegionChangedSignal(),
92 mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
93 mCommandLineOptions( NULL ),
94 mSingletonService( SingletonService::New() ),
97 mWindowMode( windowMode ),
99 mStylesheet( stylesheet ),
100 mEnvironmentOptions(),
101 mWindowPositionSize( positionSize ),
102 mLaunchpadState( Launchpad::NONE ),
103 mSlotDelegate( this )
105 // Get mName from environment options
106 mName = mEnvironmentOptions.GetWindowName();
107 if( mName.empty() && argc && ( *argc > 0 ) )
109 // Set mName from command-line args if environment option not set
113 mCommandLineOptions = new CommandLineOptions(argc, argv);
114 mFramework = new Framework( *this, argc, argv, applicationType );
115 mUseRemoteSurface = (applicationType == Framework::WATCH);
118 Application::~Application()
120 mSingletonService.UnregisterAll();
124 delete mCommandLineOptions;
128 void Application::CreateWindow()
130 if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
132 if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
134 // Command line options override environment options and full screen
135 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
136 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
138 else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
140 // Environment options override full screen functionality if command line arguments not provided
141 mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
142 mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
146 const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
147 mWindow = Dali::Window::New( mWindowPositionSize, mName, windowClassName, mWindowMode == Dali::Application::TRANSPARENT );
149 // Quit the application when the window is closed
150 GetImplementation( mWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
153 void Application::CreateAdaptor()
155 DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
157 mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
159 mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
161 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
164 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
166 mContextLossConfiguration = configuration;
168 // Run the application
172 void Application::Lower()
174 // Lower the application without quitting it.
178 void Application::Quit()
180 // Actually quit the application.
181 AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
184 void Application::QuitFromMainLoop()
189 // This will trigger OnTerminate(), below, after the main loop has completed.
192 void Application::DoInit()
194 // If an application was pre-initialized, a window was made in advance
195 if( mLaunchpadState == Launchpad::NONE )
205 // Check if user requires no vsyncing and set Adaptor
206 if (mCommandLineOptions->noVSyncOnRender)
208 mAdaptor->SetUseHardwareVSync(false);
211 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
212 if( mCommandLineOptions->viewMode != 0 )
214 ViewMode viewMode = MONO;
215 if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
217 viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
219 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
222 if( ! mStylesheet.empty() )
224 Dali::StyleMonitor::Get().SetTheme( mStylesheet );
228 void Application::DoStart()
230 mAdaptor->NotifySceneCreated();
233 void Application::DoTerminate()
237 // Ensure that the render-thread is not using the surface(window) after we delete it
244 void Application::DoPause()
249 void Application::DoResume()
254 void Application::DoLanguageChange()
256 mAdaptor->NotifyLanguageChanged();
259 void Application::OnInit()
261 mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
265 // Wire up the LifecycleController
266 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
268 InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
269 TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
270 PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
271 ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
272 ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
273 ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
274 LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
276 Dali::Application application(this);
277 mInitSignal.Emit( application );
282 void Application::OnTerminate()
284 // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
285 // delete the window as ecore_x has been destroyed by AppCore
287 Dali::Application application(this);
288 mTerminateSignal.Emit( application );
293 void Application::OnPause()
295 // A DALi app should handle Pause/Resume events.
296 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
297 // Pausing DALi core only occurs on the Window Hidden framework event
298 Dali::Application application(this);
299 mPauseSignal.Emit( application );
302 void Application::OnResume()
304 // Emit the signal first so the application can queue any messages before we do an update/render
305 // This ensures we do not just redraw the last frame before pausing if that's not required
306 Dali::Application application(this);
307 mResumeSignal.Emit( application );
309 // DALi just delivers the framework Resume event to the application.
310 // Resuming DALi core only occurs on the Window Show framework event
313 void Application::OnReset()
316 * usually, reset callback was called when a caller request to launch this application via aul.
317 * because Application class already handled initialization in OnInit(), OnReset do nothing.
319 Dali::Application application(this);
320 mResetSignal.Emit( application );
323 void Application::OnAppControl(void *data)
325 Dali::Application application(this);
326 mAppControlSignal.Emit( application , data );
329 void Application::OnLanguageChanged()
332 Dali::Application application(this);
333 mLanguageChangedSignal.Emit( application );
336 void Application::OnRegionChanged()
338 Dali::Application application(this);
339 mRegionChangedSignal.Emit( application );
342 void Application::OnBatteryLow()
344 Dali::Application application(this);
345 mBatteryLowSignal.Emit( application );
348 void Application::OnMemoryLow()
350 Dali::Application application(this);
351 mMemoryLowSignal.Emit( application );
354 void Application::OnResize(Dali::Adaptor& adaptor)
356 Dali::Application application(this);
357 mResizeSignal.Emit( application );
360 bool Application::AddIdle( CallbackBase* callback )
362 return mAdaptor->AddIdle( callback );
365 Dali::Adaptor& Application::GetAdaptor()
370 Dali::Window Application::GetWindow()
377 void Application::SetViewMode( ViewMode viewMode )
379 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
382 ViewMode Application::GetViewMode() const
384 return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
387 void Application::SetStereoBase( float stereoBase )
389 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
392 float Application::GetStereoBase() const
394 return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
398 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
400 Dali::Window newWindow = Dali::Window::New( positionSize, name, mWindowMode == Dali::Application::TRANSPARENT );
401 Window& windowImpl = GetImplementation(newWindow);
402 windowImpl.SetAdaptor(*mAdaptor);
403 newWindow.ShowIndicator(Dali::Window::INVISIBLE);
404 Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
406 Any nativeWindow = newWindow.GetNativeHandle();
407 Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
409 mWindowPositionSize = positionSize;
412 std::string Application::GetResourcePath()
414 return Internal::Adaptor::Framework::GetResourcePath();
417 void Application::SetStyleSheet( const std::string& stylesheet )
419 mStylesheet = stylesheet;
423 ApplicationPtr Application::GetPreInitializedApplication()
425 return gPreInitializedApplication;
428 } // namespace Adaptor
430 } // namespace Internal