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