Change Application to use NotifySceneCreated from Adaptor
[platform/core/uifw/dali-adaptor.git] / adaptors / common / application-impl.cpp
1 /*
2  * Copyright (c) 2014 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 <singleton-service-impl.h>
29 #include <lifecycle-controller-impl.h>
30
31 namespace Dali
32 {
33
34 namespace TizenPlatform
35 {
36 class TizenPlatformAbstraction;
37 }
38
39 namespace Integration
40 {
41 class Core;
42 }
43
44 namespace Internal
45 {
46
47 namespace Adaptor
48 {
49
50 ApplicationPtr Application::New(
51   int* argc,
52   char **argv[],
53   const std::string& stylesheet,
54   Dali::Application::WINDOW_MODE windowMode)
55 {
56   ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode ) );
57   return application;
58 }
59
60 Application::Application( int* argc, char** argv[], const std::string& stylesheet, Dali::Application::WINDOW_MODE windowMode )
61 : mInitSignal(),
62   mTerminateSignal(),
63   mPauseSignal(),
64   mResumeSignal(),
65   mResetSignal(),
66   mResizeSignal(),
67   mAppControlSignal(),
68   mLanguageChangedSignal(),
69   mRegionChangedSignal(),
70   mBatteryLowSignal(),
71   mMemoryLowSignal(),
72   mEventLoop( NULL ),
73   mFramework( NULL ),
74   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
75   mCommandLineOptions( NULL ),
76   mSingletonService( SingletonService::New() ),
77   mAdaptor( NULL ),
78   mWindow(),
79   mWindowMode( windowMode ),
80   mName(),
81   mStylesheet( stylesheet ),
82   mEnvironmentOptions(),
83   mInitialized( false ),
84   mSlotDelegate( this )
85 {
86   // Copy mName from command-line args
87   if( argc && ( *argc > 0 ) )
88   {
89     mName = (*argv)[0];
90   }
91
92   mCommandLineOptions = new CommandLineOptions(argc, argv);
93
94   mFramework = new Framework( *this, argc, argv );
95 }
96
97 Application::~Application()
98 {
99   mSingletonService.UnregisterAll();
100
101   delete mFramework;
102   delete mCommandLineOptions;
103   delete mAdaptor;
104   mWindow.Reset();
105 }
106
107 void Application::CreateWindow()
108 {
109   PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
110
111   if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
112   {
113     // Command line options override environment options and full screen
114     windowPosition = PositionSize( 0, 0, mCommandLineOptions->stageWidth, mCommandLineOptions->stageHeight );
115   }
116   else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
117   {
118     // Environment options override full screen functionality if command line arguments not provided
119     windowPosition = PositionSize( 0, 0, mEnvironmentOptions.GetWindowWidth(), mEnvironmentOptions.GetWindowHeight() );
120   }
121
122   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
123 }
124
125 void Application::CreateAdaptor()
126 {
127   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
128
129   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
130
131   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
132 }
133
134 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
135 {
136   mContextLossConfiguration = configuration;
137
138   // Run the application
139   mFramework->Run();
140 }
141
142 void Application::Lower()
143 {
144   // Lower the application without quitting it.
145   mWindow.Lower();
146 }
147
148 void Application::Quit()
149 {
150   // Actually quit the application.
151   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
152 }
153
154 void Application::QuitFromMainLoop()
155 {
156   mAdaptor->Stop();
157
158   Dali::Application application(this);
159   mTerminateSignal.Emit( application );
160
161   mFramework->Quit();
162   // This will trigger OnTerminate(), below, after the main loop has completed.
163   mInitialized = false;
164 }
165
166 void Application::OnInit()
167 {
168   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
169
170   CreateWindow();
171   CreateAdaptor();
172
173   // Run the adaptor
174   mAdaptor->Start();
175
176   // Check if user requires no vsyncing and set on X11 Adaptor
177   if (mCommandLineOptions->noVSyncOnRender)
178   {
179     mAdaptor->SetUseHardwareVSync(false);
180   }
181
182   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
183   if( mCommandLineOptions->viewMode != 0 )
184   {
185     ViewMode viewMode = MONO;
186     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
187     {
188       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
189     }
190     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
191   }
192
193   if( ! mStylesheet.empty() )
194   {
195     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
196   }
197
198   mInitialized = true;
199
200   // Wire up the LifecycleController
201   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
202
203   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
204   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
205   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
206   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
207   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
208   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
209   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
210
211   Dali::Application application(this);
212   mInitSignal.Emit( application );
213
214   mAdaptor->NotifySceneCreated();
215 }
216
217 void Application::OnTerminate()
218 {
219   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
220   // delete the window as ecore_x has been destroyed by AppCore
221
222   mWindow.Reset();
223   mInitialized = false;
224 }
225
226 void Application::OnPause()
227 {
228   mAdaptor->Pause();
229   Dali::Application application(this);
230   mPauseSignal.Emit( application );
231 }
232
233 void Application::OnResume()
234 {
235   mAdaptor->Resume();
236   Dali::Application application(this);
237   mResumeSignal.Emit( application );
238 }
239
240 void Application::OnReset()
241 {
242   /*
243    * usually, reset callback was called when a caller request to launch this application via aul.
244    * because Application class already handled initialization in OnInit(), OnReset do nothing.
245    */
246   Dali::Application application(this);
247   mResetSignal.Emit( application );
248
249   mWindow.Raise();
250 }
251
252 void Application::OnAppControl(void *data)
253 {
254   Dali::Application application(this);
255   mAppControlSignal.Emit( application , data );
256 }
257
258 void Application::OnLanguageChanged()
259 {
260   mAdaptor->NotifyLanguageChanged();
261   Dali::Application application(this);
262   mLanguageChangedSignal.Emit( application );
263 }
264
265 void Application::OnRegionChanged()
266 {
267   Dali::Application application(this);
268   mRegionChangedSignal.Emit( application );
269 }
270
271 void Application::OnBatteryLow()
272 {
273   Dali::Application application(this);
274   mBatteryLowSignal.Emit( application );
275 }
276
277 void Application::OnMemoryLow()
278 {
279   Dali::Application application(this);
280   mMemoryLowSignal.Emit( application );
281 }
282
283 void Application::OnResize(Dali::Adaptor& adaptor)
284 {
285   Dali::Application application(this);
286   mResizeSignal.Emit( application );
287 }
288
289 bool Application::AddIdle( CallbackBase* callback )
290 {
291   return mAdaptor->AddIdle( callback );
292 }
293
294 Dali::Adaptor& Application::GetAdaptor()
295 {
296   return *mAdaptor;
297 }
298
299 Dali::Window Application::GetWindow()
300 {
301   return mWindow;
302 }
303
304 // Stereoscopy
305
306 void Application::SetViewMode( ViewMode viewMode )
307 {
308   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
309 }
310
311 ViewMode Application::GetViewMode() const
312 {
313   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
314 }
315
316 void Application::SetStereoBase( float stereoBase )
317 {
318   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
319 }
320
321 float Application::GetStereoBase() const
322 {
323   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
324 }
325
326
327 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
328 {
329   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
330   Window& windowImpl = GetImplementation(newWindow);
331   windowImpl.SetAdaptor(*mAdaptor);
332   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
333   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
334
335   Any nativeWindow = newWindow.GetNativeHandle();
336   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
337   mWindow = newWindow;
338 }
339
340 } // namespace Adaptor
341
342 } // namespace Internal
343
344 } // namespace Dali