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