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