Merge branch 'devel/master' into devel/new_mesh
[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   // Get mName from environment options
87   mName = mEnvironmentOptions.GetWindowName();
88   if( mName.empty() && argc && ( *argc > 0 ) )
89   {
90     // Set mName from command-line args if environment option not set
91     mName = (*argv)[0];
92   }
93
94   mCommandLineOptions = new CommandLineOptions(argc, argv);
95
96   mFramework = new Framework( *this, argc, argv );
97 }
98
99 Application::~Application()
100 {
101   mSingletonService.UnregisterAll();
102
103   delete mFramework;
104   delete mCommandLineOptions;
105   delete mAdaptor;
106   mWindow.Reset();
107 }
108
109 void Application::CreateWindow()
110 {
111   PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
112
113   if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
114   {
115     // Command line options override environment options and full screen
116     windowPosition = PositionSize( 0, 0, mCommandLineOptions->stageWidth, mCommandLineOptions->stageHeight );
117   }
118   else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
119   {
120     // Environment options override full screen functionality if command line arguments not provided
121     windowPosition = PositionSize( 0, 0, mEnvironmentOptions.GetWindowWidth(), mEnvironmentOptions.GetWindowHeight() );
122   }
123
124   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
125
126   // Set the window class name if available
127   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
128   if( ! windowClassName.empty() )
129   {
130     mWindow.SetClass( mName, windowClassName );
131   }
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
143 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
144 {
145   mContextLossConfiguration = configuration;
146
147   // Run the application
148   mFramework->Run();
149 }
150
151 void Application::Lower()
152 {
153   // Lower the application without quitting it.
154   mWindow.Lower();
155 }
156
157 void Application::Quit()
158 {
159   // Actually quit the application.
160   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
161 }
162
163 void Application::QuitFromMainLoop()
164 {
165   mAdaptor->Stop();
166
167   Dali::Application application(this);
168   mTerminateSignal.Emit( application );
169
170   mFramework->Quit();
171   // This will trigger OnTerminate(), below, after the main loop has completed.
172   mInitialized = false;
173 }
174
175 void Application::OnInit()
176 {
177   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
178
179   CreateWindow();
180   CreateAdaptor();
181
182   // Run the adaptor
183   mAdaptor->Start();
184
185   // Check if user requires no vsyncing and set on X11 Adaptor
186   if (mCommandLineOptions->noVSyncOnRender)
187   {
188     mAdaptor->SetUseHardwareVSync(false);
189   }
190
191   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
192   if( mCommandLineOptions->viewMode != 0 )
193   {
194     ViewMode viewMode = MONO;
195     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
196     {
197       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
198     }
199     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
200   }
201
202   if( ! mStylesheet.empty() )
203   {
204     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
205   }
206
207   mInitialized = true;
208
209   // Wire up the LifecycleController
210   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
211
212   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
213   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
214   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
215   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
216   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
217   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
218   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
219
220   Dali::Application application(this);
221   mInitSignal.Emit( application );
222
223   mAdaptor->NotifySceneCreated();
224 }
225
226 void Application::OnTerminate()
227 {
228   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
229   // delete the window as ecore_x has been destroyed by AppCore
230
231   mWindow.Reset();
232   mInitialized = false;
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   mAdaptor->Resume();
245   Dali::Application application(this);
246   mResumeSignal.Emit( application );
247 }
248
249 void Application::OnReset()
250 {
251   /*
252    * usually, reset callback was called when a caller request to launch this application via aul.
253    * because Application class already handled initialization in OnInit(), OnReset do nothing.
254    */
255   Dali::Application application(this);
256   mResetSignal.Emit( application );
257
258   mWindow.Raise();
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 } // namespace Adaptor
350
351 } // namespace Internal
352
353 } // namespace Dali