785602025ec3b15ec6e2b035577d58bac7ca48cc
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / application-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/adaptor/common/application-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
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-render-surface.h>
31
32 namespace Dali
33 {
34
35 namespace TizenPlatform
36 {
37 class TizenPlatformAbstraction;
38 }
39
40 namespace Integration
41 {
42 class Core;
43 }
44
45 namespace Internal
46 {
47
48 namespace Adaptor
49 {
50
51 namespace
52 {
53
54 const float DEFAULT_STEREO_BASE( 65.0f );
55
56 } // unnamed namespace
57
58 ApplicationPtr Application::gPreInitializedApplication( NULL );
59
60 ApplicationPtr Application::New(
61   int* argc,
62   char **argv[],
63   const std::string& stylesheet,
64   Dali::Application::WINDOW_MODE windowMode,
65   const PositionSize& positionSize,
66   Framework::Type applicationType)
67 {
68   ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
69   return application;
70 }
71
72 void Application::PreInitialize( int* argc, char** argv[] )
73 {
74   if( !gPreInitializedApplication )
75   {
76     gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
77
78     gPreInitializedApplication->CreateWindow();    // Only create window
79
80     gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
81   }
82 }
83
84 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
85   Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
86 : mInitSignal(),
87   mTerminateSignal(),
88   mPauseSignal(),
89   mResumeSignal(),
90   mResetSignal(),
91   mResizeSignal(),
92   mAppControlSignal(),
93   mLanguageChangedSignal(),
94   mRegionChangedSignal(),
95   mBatteryLowSignal(),
96   mMemoryLowSignal(),
97   mEventLoop( nullptr ),
98   mFramework( nullptr ),
99   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
100   mCommandLineOptions( nullptr ),
101   mSingletonService( SingletonService::New() ),
102   mAdaptorBuilder( nullptr ),
103   mAdaptor( nullptr ),
104   mMainWindow(),
105   mMainWindowMode( windowMode ),
106   mMainWindowName(),
107   mStylesheet( stylesheet ),
108   mEnvironmentOptions(),
109   mWindowPositionSize( positionSize ),
110   mLaunchpadState( Launchpad::NONE ),
111   mSlotDelegate( this ),
112   mViewMode( MONO ),
113   mStereoBase( DEFAULT_STEREO_BASE )
114 {
115   // Get mName from environment options
116   mMainWindowName = mEnvironmentOptions.GetWindowName();
117   if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
118   {
119     // Set mName from command-line args if environment option not set
120     mMainWindowName = (*argv)[0];
121   }
122
123   mCommandLineOptions = new CommandLineOptions(argc, argv);
124   mFramework = new Framework( *this, argc, argv, applicationType );
125   mUseRemoteSurface = (applicationType == Framework::WATCH);
126 }
127
128 Application::~Application()
129 {
130   mSingletonService.UnregisterAll();
131
132   mMainWindow.Reset();
133   delete mAdaptor;
134   delete mAdaptorBuilder;
135   delete mCommandLineOptions;
136   delete mFramework;
137 }
138
139 void Application::CreateWindow()
140 {
141   if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
142   {
143     if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
144     {
145       // Command line options override environment options and full screen
146       mWindowPositionSize.width = mCommandLineOptions->stageWidth;
147       mWindowPositionSize.height = mCommandLineOptions->stageHeight;
148     }
149     else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
150     {
151       // Environment options override full screen functionality if command line arguments not provided
152       mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
153       mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
154     }
155   }
156
157   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
158   mMainWindow = Dali::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
159
160   int indicatorVisibleMode = mEnvironmentOptions.GetIndicatorVisibleMode();
161   if( indicatorVisibleMode >= Dali::Window::INVISIBLE && indicatorVisibleMode <= Dali::Window::AUTO )
162   {
163     GetImplementation( mMainWindow ).SetIndicatorVisibleMode( static_cast< Dali::Window::IndicatorVisibleMode >( indicatorVisibleMode ) );
164   }
165
166   // Quit the application when the window is closed
167   GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
168 }
169
170 void Application::CreateAdaptor()
171 {
172   DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
173
174   auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
175
176   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( graphicsFactory, mMainWindow, mContextLossConfiguration, &mEnvironmentOptions );
177
178   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
179
180   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
181 }
182
183 void Application::CreateAdaptorBuilder()
184 {
185   mAdaptorBuilder = new AdaptorBuilder();
186 }
187
188 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
189 {
190   mContextLossConfiguration = configuration;
191
192   // Run the application
193   mFramework->Run();
194 }
195
196 void Application::Lower()
197 {
198   // Lower the application without quitting it.
199   mMainWindow.Lower();
200 }
201
202 void Application::Quit()
203 {
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 );
207 }
208
209 void Application::QuitFromMainLoop()
210 {
211   mAdaptor->Stop();
212
213   mFramework->Quit();
214   // This will trigger OnTerminate(), below, after the main loop has completed.
215 }
216
217 void Application::DoInit()
218 {
219   CreateAdaptorBuilder();
220
221   // If an application was pre-initialized, a window was made in advance
222   if( mLaunchpadState == Launchpad::NONE )
223   {
224     CreateWindow();
225   }
226
227   CreateAdaptor();
228
229   // Run the adaptor
230   mAdaptor->Start();
231
232   // Check if user requires no vsyncing and set Adaptor
233   if (mCommandLineOptions->noVSyncOnRender)
234   {
235     mAdaptor->SetUseHardwareVSync(false);
236   }
237
238   if( ! mStylesheet.empty() )
239   {
240     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
241   }
242 }
243
244 void Application::DoStart()
245 {
246   mAdaptor->NotifySceneCreated();
247 }
248
249 void Application::DoTerminate()
250 {
251   if( mAdaptor )
252   {
253     // Ensure that the render-thread is not using the surface(window) after we delete it
254     mAdaptor->Stop();
255   }
256
257   mMainWindow.Reset(); // This only resets (clears) the default Window
258 }
259
260 void Application::DoPause()
261 {
262   mAdaptor->Pause();
263 }
264
265 void Application::DoResume()
266 {
267   mAdaptor->Resume();
268 }
269
270 void Application::DoLanguageChange()
271 {
272   mAdaptor->NotifyLanguageChanged();
273 }
274
275 void Application::OnInit()
276 {
277   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
278
279   DoInit();
280
281   // Wire up the LifecycleController
282   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
283
284   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
285   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
286   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
287   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
288   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
289   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
290   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
291
292   Dali::Application application(this);
293   mInitSignal.Emit( application );
294
295   DoStart();
296 }
297
298 void Application::OnTerminate()
299 {
300   // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
301   // delete the window as ecore_x has been destroyed by AppCore
302
303   Dali::Application application(this);
304   mTerminateSignal.Emit( application );
305
306   DoTerminate();
307 }
308
309 void Application::OnPause()
310 {
311   // A DALi app should handle Pause/Resume events.
312   // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
313   // Pausing DALi core only occurs on the Window Hidden framework event
314   Dali::Application application(this);
315   mPauseSignal.Emit( application );
316 }
317
318 void Application::OnResume()
319 {
320   // Emit the signal first so the application can queue any messages before we do an update/render
321   // This ensures we do not just redraw the last frame before pausing if that's not required
322   Dali::Application application(this);
323   mResumeSignal.Emit( application );
324
325   // DALi just delivers the framework Resume event to the application.
326   // Resuming DALi core only occurs on the Window Show framework event
327
328   // Trigger processing of events queued up while paused
329   CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
330   coreEventInterface.ProcessCoreEvents();
331 }
332
333 void Application::OnReset()
334 {
335   /*
336    * usually, reset callback was called when a caller request to launch this application via aul.
337    * because Application class already handled initialization in OnInit(), OnReset do nothing.
338    */
339   Dali::Application application(this);
340   mResetSignal.Emit( application );
341 }
342
343 void Application::OnAppControl(void *data)
344 {
345   Dali::Application application(this);
346   mAppControlSignal.Emit( application , data );
347 }
348
349 void Application::OnLanguageChanged()
350 {
351   DoLanguageChange();
352   Dali::Application application(this);
353   mLanguageChangedSignal.Emit( application );
354 }
355
356 void Application::OnRegionChanged()
357 {
358   Dali::Application application(this);
359   mRegionChangedSignal.Emit( application );
360 }
361
362 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
363 {
364   Dali::Application application(this);
365   mBatteryLowSignal.Emit( application );
366
367   mLowBatterySignal.Emit( status );
368 }
369
370 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
371 {
372   Dali::Application application(this);
373   mMemoryLowSignal.Emit( application );
374
375   mLowMemorySignal.Emit( status );
376 }
377 void Application::OnResize(Dali::Adaptor& adaptor)
378 {
379   Dali::Application application(this);
380   mResizeSignal.Emit( application );
381 }
382
383 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
384 {
385   return mAdaptor->AddIdle( callback, hasReturnValue );
386 }
387
388 std::string Application::GetRegion() const
389 {
390   return mFramework->GetRegion();
391 }
392
393 std::string Application::GetLanguage() const
394 {
395   return mFramework->GetLanguage();
396 }
397
398 Dali::Adaptor& Application::GetAdaptor()
399 {
400   return *mAdaptor;
401 }
402
403 Dali::Window Application::GetWindow()
404 {
405   return mMainWindow;
406 }
407
408 // Stereoscopy
409
410 void Application::SetViewMode( ViewMode viewMode )
411 {
412   mViewMode = viewMode;
413 }
414
415 ViewMode Application::GetViewMode() const
416 {
417   return mViewMode;
418 }
419
420 void Application::SetStereoBase( float stereoBase )
421 {
422   mStereoBase = stereoBase;
423 }
424
425 float Application::GetStereoBase() const
426 {
427   return mStereoBase;
428 }
429
430 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
431 {
432   Dali::Window newWindow = Dali::Window::New( positionSize, name, mMainWindowMode == Dali::Application::TRANSPARENT );
433   Window& windowImpl = GetImplementation(newWindow);
434   windowImpl.SetAdaptor(*mAdaptor);
435
436   int indicatorVisibleMode = mEnvironmentOptions.GetIndicatorVisibleMode();
437   if( indicatorVisibleMode >= Dali::Window::INVISIBLE && indicatorVisibleMode <= Dali::Window::AUTO )
438   {
439     GetImplementation( newWindow ).SetIndicatorVisibleMode( static_cast< Dali::Window::IndicatorVisibleMode >( indicatorVisibleMode ) );
440   }
441
442   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
443
444   Any nativeWindow = newWindow.GetNativeHandle();
445   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
446   mMainWindow = newWindow;
447   mWindowPositionSize = positionSize;
448 }
449
450 std::string Application::GetResourcePath()
451 {
452   return Internal::Adaptor::Framework::GetResourcePath();
453 }
454
455 std::string Application::GetDataPath()
456 {
457   return Internal::Adaptor::Framework::GetDataPath();
458 }
459
460 void Application::SetStyleSheet( const std::string& stylesheet )
461 {
462   mStylesheet = stylesheet;
463 }
464
465 ApplicationPtr Application::GetPreInitializedApplication()
466 {
467   return gPreInitializedApplication;
468 }
469
470 } // namespace Adaptor
471
472 } // namespace Internal
473
474 } // namespace Dali