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