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