Merge "Use environment option to set the name of the window" into devel/master
[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
127 void Application::CreateAdaptor()
128 {
129   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
130
131   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
132
133   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
134 }
135
136 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
137 {
138   mContextLossConfiguration = configuration;
139
140   // Run the application
141   mFramework->Run();
142 }
143
144 void Application::Lower()
145 {
146   // Lower the application without quitting it.
147   mWindow.Lower();
148 }
149
150 void Application::Quit()
151 {
152   // Actually quit the application.
153   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
154 }
155
156 void Application::QuitFromMainLoop()
157 {
158   mAdaptor->Stop();
159
160   Dali::Application application(this);
161   mTerminateSignal.Emit( application );
162
163   mFramework->Quit();
164   // This will trigger OnTerminate(), below, after the main loop has completed.
165   mInitialized = false;
166 }
167
168 void Application::OnInit()
169 {
170   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
171
172   CreateWindow();
173   CreateAdaptor();
174
175   // Run the adaptor
176   mAdaptor->Start();
177
178   // Check if user requires no vsyncing and set on X11 Adaptor
179   if (mCommandLineOptions->noVSyncOnRender)
180   {
181     mAdaptor->SetUseHardwareVSync(false);
182   }
183
184   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
185   if( mCommandLineOptions->viewMode != 0 )
186   {
187     ViewMode viewMode = MONO;
188     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
189     {
190       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
191     }
192     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
193   }
194
195   if( ! mStylesheet.empty() )
196   {
197     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
198   }
199
200   mInitialized = true;
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   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetCore().SceneCreated();
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   mWindow.Reset();
225   mInitialized = false;
226 }
227
228 void Application::OnPause()
229 {
230   mAdaptor->Pause();
231   Dali::Application application(this);
232   mPauseSignal.Emit( application );
233 }
234
235 void Application::OnResume()
236 {
237   mAdaptor->Resume();
238   Dali::Application application(this);
239   mResumeSignal.Emit( application );
240 }
241
242 void Application::OnReset()
243 {
244   /*
245    * usually, reset callback was called when a caller request to launch this application via aul.
246    * because Application class already handled initialization in OnInit(), OnReset do nothing.
247    */
248   Dali::Application application(this);
249   mResetSignal.Emit( application );
250
251   mWindow.Raise();
252 }
253
254 void Application::OnAppControl(void *data)
255 {
256   Dali::Application application(this);
257   mAppControlSignal.Emit( application , data );
258 }
259
260 void Application::OnLanguageChanged()
261 {
262   mAdaptor->NotifyLanguageChanged();
263   Dali::Application application(this);
264   mLanguageChangedSignal.Emit( application );
265 }
266
267 void Application::OnRegionChanged()
268 {
269   Dali::Application application(this);
270   mRegionChangedSignal.Emit( application );
271 }
272
273 void Application::OnBatteryLow()
274 {
275   Dali::Application application(this);
276   mBatteryLowSignal.Emit( application );
277 }
278
279 void Application::OnMemoryLow()
280 {
281   Dali::Application application(this);
282   mMemoryLowSignal.Emit( application );
283 }
284
285 void Application::OnResize(Dali::Adaptor& adaptor)
286 {
287   Dali::Application application(this);
288   mResizeSignal.Emit( application );
289 }
290
291 bool Application::AddIdle( CallbackBase* callback )
292 {
293   return mAdaptor->AddIdle( callback );
294 }
295
296 Dali::Adaptor& Application::GetAdaptor()
297 {
298   return *mAdaptor;
299 }
300
301 Dali::Window Application::GetWindow()
302 {
303   return mWindow;
304 }
305
306 // Stereoscopy
307
308 void Application::SetViewMode( ViewMode viewMode )
309 {
310   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
311 }
312
313 ViewMode Application::GetViewMode() const
314 {
315   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
316 }
317
318 void Application::SetStereoBase( float stereoBase )
319 {
320   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
321 }
322
323 float Application::GetStereoBase() const
324 {
325   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
326 }
327
328
329 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
330 {
331   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
332   Window& windowImpl = GetImplementation(newWindow);
333   windowImpl.SetAdaptor(*mAdaptor);
334   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
335   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
336
337   Any nativeWindow = newWindow.GetNativeHandle();
338   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
339   mWindow = newWindow;
340 }
341
342 } // namespace Adaptor
343
344 } // namespace Internal
345
346 } // namespace Dali