Merge branch '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 #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   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   // Allow DPI to be overridden from command line.
126   unsigned int hDPI=DEFAULT_HORIZONTAL_DPI;
127   unsigned int vDPI=DEFAULT_VERTICAL_DPI;
128
129   std::string dpiStr = mCommandLineOptions->stageDPI;
130   if(!dpiStr.empty())
131   {
132     sscanf(dpiStr.c_str(), "%ux%u", &hDPI, &vDPI);
133   }
134   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetDpi(hDPI, vDPI);
135
136   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
137 }
138
139 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
140 {
141   mContextLossConfiguration = configuration;
142
143   // Run the application
144   mFramework->Run();
145 }
146
147 void Application::Lower()
148 {
149   // Lower the application without quitting it.
150   mWindow.Lower();
151 }
152
153 void Application::Quit()
154 {
155   // Actually quit the application.
156   AddIdle(boost::bind(&Application::QuitFromMainLoop, this));
157 }
158
159 void Application::QuitFromMainLoop()
160 {
161   mAdaptor->Stop();
162
163   Dali::Application application(this);
164   mTerminateSignalV2.Emit( application );
165
166   mFramework->Quit();
167   // This will trigger OnTerminate(), below, after the main loop has completed.
168   mInitialized = false;
169 }
170
171 void Application::OnInit()
172 {
173   mFramework->AddAbortCallback(boost::bind(&Application::QuitFromMainLoop, this));
174
175   CreateWindow();
176   CreateAdaptor();
177
178   // Run the adaptor
179   mAdaptor->Start();
180
181   // Check if user requires no vsyncing and set on X11 Adaptor
182   if (mCommandLineOptions->noVSyncOnRender)
183   {
184     mAdaptor->SetUseHardwareVSync(false);
185   }
186
187   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
188   if( mCommandLineOptions->viewMode != 0 )
189   {
190     ViewMode viewMode = MONO;
191     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
192     {
193       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
194     }
195     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
196   }
197
198   mInitialized = true;
199
200   // Wire up the LifecycleController
201   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
202
203   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
204   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
205   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
206   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
207   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
208   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
209   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
210
211   Dali::Application application(this);
212   mInitSignalV2.Emit( application );
213 }
214
215 void Application::OnTerminate()
216 {
217   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
218   // delete the window as ecore_x has been destroyed by AppCore
219
220   mWindow.Reset();
221   mInitialized = false;
222 }
223
224 void Application::OnPause()
225 {
226   mAdaptor->Pause();
227   Dali::Application application(this);
228   mPauseSignalV2.Emit( application );
229 }
230
231 void Application::OnResume()
232 {
233   mAdaptor->Resume();
234   Dali::Application application(this);
235   mResumeSignalV2.Emit( application );
236 }
237
238 void Application::OnReset()
239 {
240   /*
241    * usually, reset callback was called when a caller request to launch this application via aul.
242    * because Application class already handled initialization in OnInit(), OnReset do nothing.
243    */
244   Dali::Application application(this);
245   mResetSignalV2.Emit( application );
246
247   mWindow.Raise();
248 }
249
250 void Application::OnLanguageChanged()
251 {
252   mAdaptor->NotifyLanguageChanged();
253 }
254
255 void Application::OnResize(Dali::Adaptor& adaptor)
256 {
257   Dali::Application application(this);
258   mResizeSignalV2.Emit( application );
259 }
260
261 bool Application::AddIdle(boost::function<void(void)> callBack)
262 {
263   return mAdaptor->AddIdle(callBack);
264 }
265
266 Dali::Adaptor& Application::GetAdaptor()
267 {
268   return *mAdaptor;
269 }
270
271 Dali::Window Application::GetWindow()
272 {
273   return mWindow;
274 }
275
276 const std::string& Application::GetTheme()
277 {
278   return Dali::StyleMonitor::Get().GetTheme();
279 }
280
281 void Application::SetTheme(const std::string& themeFilePath)
282 {
283   return Dali::StyleMonitor::Get().SetTheme(themeFilePath);
284 }
285
286 // Stereoscopy
287
288 void Application::SetViewMode( ViewMode viewMode )
289 {
290   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
291 }
292
293 ViewMode Application::GetViewMode() const
294 {
295   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
296 }
297
298 void Application::SetStereoBase( float stereoBase )
299 {
300   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
301 }
302
303 float Application::GetStereoBase() const
304 {
305   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
306 }
307
308
309 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
310 {
311   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
312   Window& windowImpl = GetImplementation(newWindow);
313   windowImpl.SetAdaptor(*mAdaptor);
314   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
315   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
316   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(*renderSurface);
317   mWindow = newWindow;
318 }
319
320 } // namespace Adaptor
321
322 } // namespace Internal
323
324 } // namespace Dali