Merge remote-tracking branch 'origin/tizen' into new_text
[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 SlpPlatform
35 {
36 class SlpPlatformAbstraction;
37 }
38
39 namespace Integration
40 {
41 class Core;
42 }
43
44 namespace Internal
45 {
46
47 namespace Adaptor
48 {
49
50 namespace
51 {
52 // Defaults taken from H2 device
53 const unsigned int DEFAULT_WINDOW_WIDTH   = 480;
54 const unsigned int DEFAULT_WINDOW_HEIGHT  = 800;
55 }
56
57 ApplicationPtr Application::New(
58   int* argc,
59   char **argv[],
60   const std::string& name,
61   const DeviceLayout& baseLayout,
62   Dali::Application::WINDOW_MODE windowMode)
63 {
64   ApplicationPtr application ( new Application (argc, argv, name, baseLayout, windowMode ) );
65   return application;
66 }
67
68 Application::Application( int* argc, char** argv[], const std::string& name, const DeviceLayout& baseLayout, Dali::Application::WINDOW_MODE windowMode)
69 : mInitSignal(),
70   mTerminateSignal(),
71   mPauseSignal(),
72   mResumeSignal(),
73   mResetSignal(),
74   mResizeSignal(),
75   mAppControlSignal(),
76   mLanguageChangedSignal(),
77   mRegionChangedSignal(),
78   mBatteryLowSignal(),
79   mMemoryLowSignal(),
80   mEventLoop( NULL ),
81   mFramework( NULL ),
82   mCommandLineOptions( NULL ),
83   mSingletonService( SingletonService::New() ),
84   mAdaptor( NULL ),
85   mWindow(),
86   mWindowMode( windowMode ),
87   mName( name ),
88   mInitialized( false ),
89   mBaseLayout( baseLayout ),
90   mSlotDelegate( this )
91 {
92   mCommandLineOptions = new CommandLineOptions(argc, argv);
93
94   mFramework = new Framework(*this, argc, argv, name);
95 }
96
97 Application::~Application()
98 {
99   mSingletonService.UnregisterAll();
100
101   delete mFramework;
102   delete mCommandLineOptions;
103   delete mAdaptor;
104   mWindow.Reset();
105 }
106
107 void Application::CreateWindow()
108 {
109 #ifndef __arm__
110    PositionSize windowPosition(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
111 #else
112    PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
113 #endif
114   if (mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
115   {
116     // let the command line options over ride
117     windowPosition = PositionSize(0,0,mCommandLineOptions->stageWidth,mCommandLineOptions->stageHeight);
118   }
119
120   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
121 }
122
123 void Application::CreateAdaptor()
124 {
125   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
126
127   mAdaptor = &Dali::Adaptor::New( mWindow, mBaseLayout, mContextLossConfiguration );
128
129   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
130 }
131
132 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
133 {
134   mContextLossConfiguration = configuration;
135
136   // Run the application
137   mFramework->Run();
138 }
139
140 void Application::Lower()
141 {
142   // Lower the application without quitting it.
143   mWindow.Lower();
144 }
145
146 void Application::Quit()
147 {
148   // Actually quit the application.
149   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
150 }
151
152 void Application::QuitFromMainLoop()
153 {
154   mAdaptor->Stop();
155
156   Dali::Application application(this);
157   mTerminateSignal.Emit( application );
158
159   mFramework->Quit();
160   // This will trigger OnTerminate(), below, after the main loop has completed.
161   mInitialized = false;
162 }
163
164 void Application::OnInit()
165 {
166   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
167
168   CreateWindow();
169   CreateAdaptor();
170
171   // Run the adaptor
172   mAdaptor->Start();
173
174   // Check if user requires no vsyncing and set on X11 Adaptor
175   if (mCommandLineOptions->noVSyncOnRender)
176   {
177     mAdaptor->SetUseHardwareVSync(false);
178   }
179
180   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
181   if( mCommandLineOptions->viewMode != 0 )
182   {
183     ViewMode viewMode = MONO;
184     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
185     {
186       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
187     }
188     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
189   }
190
191   mInitialized = true;
192
193   // Wire up the LifecycleController
194   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
195
196   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
197   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
198   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
199   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
200   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
201   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
202   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
203
204   Dali::Application application(this);
205   mInitSignal.Emit( application );
206
207   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetCore().SceneCreated();
208 }
209
210 void Application::OnTerminate()
211 {
212   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
213   // delete the window as ecore_x has been destroyed by AppCore
214
215   mWindow.Reset();
216   mInitialized = false;
217 }
218
219 void Application::OnPause()
220 {
221   mAdaptor->Pause();
222   Dali::Application application(this);
223   mPauseSignal.Emit( application );
224 }
225
226 void Application::OnResume()
227 {
228   mAdaptor->Resume();
229   Dali::Application application(this);
230   mResumeSignal.Emit( application );
231 }
232
233 void Application::OnReset()
234 {
235   /*
236    * usually, reset callback was called when a caller request to launch this application via aul.
237    * because Application class already handled initialization in OnInit(), OnReset do nothing.
238    */
239   Dali::Application application(this);
240   mResetSignal.Emit( application );
241
242   mWindow.Raise();
243 }
244
245 void Application::OnAppControl(void *data)
246 {
247   Dali::Application application(this);
248   mAppControlSignal.Emit( application , data );
249 }
250
251 void Application::OnLanguageChanged()
252 {
253   mAdaptor->NotifyLanguageChanged();
254 }
255
256 void Application::OnRegionChanged()
257 {
258   Dali::Application application(this);
259   mRegionChangedSignal.Emit( application );
260 }
261
262 void Application::OnBatteryLow()
263 {
264   Dali::Application application(this);
265   mBatteryLowSignal.Emit( application );
266 }
267
268 void Application::OnMemoryLow()
269 {
270   Dali::Application application(this);
271   mMemoryLowSignal.Emit( application );
272 }
273
274 void Application::OnResize(Dali::Adaptor& adaptor)
275 {
276   Dali::Application application(this);
277   mResizeSignal.Emit( application );
278 }
279
280 bool Application::AddIdle( CallbackBase* callback )
281 {
282   return mAdaptor->AddIdle( callback );
283 }
284
285 Dali::Adaptor& Application::GetAdaptor()
286 {
287   return *mAdaptor;
288 }
289
290 Dali::Window Application::GetWindow()
291 {
292   return mWindow;
293 }
294
295 const std::string& Application::GetTheme()
296 {
297   return Dali::StyleMonitor::Get().GetTheme();
298 }
299
300 void Application::SetTheme(const std::string& themeFilePath)
301 {
302   return Dali::StyleMonitor::Get().SetTheme(themeFilePath);
303 }
304
305 // Stereoscopy
306
307 void Application::SetViewMode( ViewMode viewMode )
308 {
309   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
310 }
311
312 ViewMode Application::GetViewMode() const
313 {
314   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
315 }
316
317 void Application::SetStereoBase( float stereoBase )
318 {
319   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
320 }
321
322 float Application::GetStereoBase() const
323 {
324   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
325 }
326
327
328 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
329 {
330   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
331   Window& windowImpl = GetImplementation(newWindow);
332   windowImpl.SetAdaptor(*mAdaptor);
333   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
334   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
335   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(*renderSurface);
336   mWindow = newWindow;
337 }
338
339 } // namespace Adaptor
340
341 } // namespace Internal
342
343 } // namespace Dali