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