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