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