Merge "Merge remote-tracking branch 'origin/tizen' into new_text" 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 #include <style-monitor.h>
24
25 // INTERNAL INCLUDES
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 const float        DEFAULT_HORIZONTAL_DPI = 220;
56 const float        DEFAULT_VERTICAL_DPI   = 217;
57 }
58
59 ApplicationPtr Application::New(
60   int* argc,
61   char **argv[],
62   const std::string& name,
63   const DeviceLayout& baseLayout,
64   Dali::Application::WINDOW_MODE windowMode)
65 {
66   ApplicationPtr application ( new Application (argc, argv, name, baseLayout, windowMode ) );
67   return application;
68 }
69
70 Application::Application( int* argc, char** argv[], const std::string& name, const DeviceLayout& baseLayout, Dali::Application::WINDOW_MODE windowMode)
71 : mInitSignalV2(),
72   mTerminateSignalV2(),
73   mPauseSignalV2(),
74   mResumeSignalV2(),
75   mResetSignalV2(),
76   mResizeSignalV2(),
77   mLanguageChangedSignalV2(),
78   mEventLoop( NULL ),
79   mFramework( NULL ),
80   mCommandLineOptions( NULL ),
81   mSingletonService( SingletonService::New() ),
82   mAdaptor( NULL ),
83   mWindow(),
84   mWindowMode( windowMode ),
85   mName( name ),
86   mInitialized( false ),
87   mBaseLayout( baseLayout ),
88   mSlotDelegate( this )
89 {
90   mCommandLineOptions = new CommandLineOptions(argc, argv);
91
92   mFramework = new Framework(*this, argc, argv, name);
93 }
94
95 Application::~Application()
96 {
97   mSingletonService.UnregisterAll();
98
99   delete mFramework;
100   delete mCommandLineOptions;
101   delete mAdaptor;
102   mWindow.Reset();
103 }
104
105 void Application::CreateWindow()
106 {
107 #ifndef __arm__
108    PositionSize windowPosition(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
109 #else
110    PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
111 #endif
112   if (mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
113   {
114     // let the command line options over ride
115     windowPosition = PositionSize(0,0,mCommandLineOptions->stageWidth,mCommandLineOptions->stageHeight);
116   }
117
118   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
119 }
120
121 void Application::CreateAdaptor()
122 {
123   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
124
125   mAdaptor = &Dali::Adaptor::New( mWindow, mBaseLayout, mContextLossConfiguration );
126
127   // Allow DPI to be overridden from command line.
128   unsigned int hDPI=DEFAULT_HORIZONTAL_DPI;
129   unsigned int vDPI=DEFAULT_VERTICAL_DPI;
130
131   std::string dpiStr = mCommandLineOptions->stageDPI;
132   if(!dpiStr.empty())
133   {
134     sscanf(dpiStr.c_str(), "%ux%u", &hDPI, &vDPI);
135   }
136   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetDpi(hDPI, vDPI);
137
138   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
139 }
140
141 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
142 {
143   mContextLossConfiguration = configuration;
144
145   // Run the application
146   mFramework->Run();
147 }
148
149 void Application::Lower()
150 {
151   // Lower the application without quitting it.
152   mWindow.Lower();
153 }
154
155 void Application::Quit()
156 {
157   // Actually quit the application.
158   AddIdle(boost::bind(&Application::QuitFromMainLoop, this));
159 }
160
161 void Application::QuitFromMainLoop()
162 {
163   mAdaptor->Stop();
164
165   Dali::Application application(this);
166   mTerminateSignalV2.Emit( application );
167
168   mFramework->Quit();
169   // This will trigger OnTerminate(), below, after the main loop has completed.
170   mInitialized = false;
171 }
172
173 void Application::OnInit()
174 {
175   mFramework->AddAbortCallback(boost::bind(&Application::QuitFromMainLoop, this));
176
177   CreateWindow();
178   CreateAdaptor();
179
180   // Run the adaptor
181   mAdaptor->Start();
182
183   // Check if user requires no vsyncing and set on X11 Adaptor
184   if (mCommandLineOptions->noVSyncOnRender)
185   {
186     mAdaptor->SetUseHardwareVSync(false);
187   }
188
189   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
190   if( mCommandLineOptions->viewMode != 0 )
191   {
192     ViewMode viewMode = MONO;
193     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
194     {
195       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
196     }
197     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
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   mInitSignalV2.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   mPauseSignalV2.Emit( application );
233 }
234
235 void Application::OnResume()
236 {
237   mAdaptor->Resume();
238   Dali::Application application(this);
239   mResumeSignalV2.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   mResetSignalV2.Emit( application );
250
251   mWindow.Raise();
252 }
253
254 void Application::OnLanguageChanged()
255 {
256   mAdaptor->NotifyLanguageChanged();
257 }
258
259 void Application::OnResize(Dali::Adaptor& adaptor)
260 {
261   Dali::Application application(this);
262   mResizeSignalV2.Emit( application );
263 }
264
265 bool Application::AddIdle(boost::function<void(void)> callBack)
266 {
267   return mAdaptor->AddIdle(callBack);
268 }
269
270 Dali::Adaptor& Application::GetAdaptor()
271 {
272   return *mAdaptor;
273 }
274
275 Dali::Window Application::GetWindow()
276 {
277   return mWindow;
278 }
279
280 const std::string& Application::GetTheme()
281 {
282   return Dali::StyleMonitor::Get().GetTheme();
283 }
284
285 void Application::SetTheme(const std::string& themeFilePath)
286 {
287   return Dali::StyleMonitor::Get().SetTheme(themeFilePath);
288 }
289
290 // Stereoscopy
291
292 void Application::SetViewMode( ViewMode viewMode )
293 {
294   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
295 }
296
297 ViewMode Application::GetViewMode() const
298 {
299   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
300 }
301
302 void Application::SetStereoBase( float stereoBase )
303 {
304   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
305 }
306
307 float Application::GetStereoBase() const
308 {
309   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
310 }
311
312
313 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
314 {
315   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
316   Window& windowImpl = GetImplementation(newWindow);
317   windowImpl.SetAdaptor(*mAdaptor);
318   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
319   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
320   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(*renderSurface);
321   mWindow = newWindow;
322 }
323
324 } // namespace Adaptor
325
326 } // namespace Internal
327
328 } // namespace Dali