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