[dali_1.0.1] Merge branch '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 #include <style-monitor.h>
24
25 // INTERNAL INCLUDES
26 #include <command-line-options.h>
27 #include <common/adaptor-impl.h>
28
29 namespace Dali
30 {
31
32 namespace SlpPlatform
33 {
34 class SlpPlatformAbstraction;
35 }
36
37 namespace Integration
38 {
39 class Core;
40 }
41
42 namespace Internal
43 {
44
45 namespace Adaptor
46 {
47
48 namespace
49 {
50 // Defaults taken from H2 device
51 const unsigned int DEFAULT_WINDOW_WIDTH   = 480;
52 const unsigned int DEFAULT_WINDOW_HEIGHT  = 800;
53 const float        DEFAULT_HORIZONTAL_DPI = 220;
54 const float        DEFAULT_VERTICAL_DPI   = 217;
55
56 boost::thread_specific_ptr<Application> gThreadLocalApplication;
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(
71   int* argc,
72   char** argv[],
73   const std::string& name,
74   const DeviceLayout& baseLayout,
75   Dali::Application::WINDOW_MODE windowMode)
76 : mFramework(NULL),
77   mCommandLineOptions(NULL),
78   mAdaptor(NULL),
79   mWindow(),
80   mWindowMode( windowMode ),
81   mName(name),
82   mInitialized(false),
83   mBaseLayout(baseLayout),
84   mSlotDelegate( this )
85 {
86   // make sure we don't create the local thread application instance twice
87   DALI_ASSERT_ALWAYS(gThreadLocalApplication.get() == NULL && "Cannot create more than one Application per thread" );
88
89   // reset is used to store a new value associated with this thread
90   gThreadLocalApplication.reset(this);
91
92   mCommandLineOptions = new CommandLineOptions(argc, argv);
93
94   mFramework = new Framework(*this, argc, argv, name);
95 }
96
97 Application::~Application()
98 {
99   delete mFramework;
100   delete mCommandLineOptions;
101   delete mAdaptor;
102   mWindow.Reset();
103   gThreadLocalApplication.release();
104 }
105
106 void Application::CreateWindow()
107 {
108 #ifndef __arm__
109    PositionSize windowPosition(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
110 #else
111    PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
112 #endif
113   if (mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
114   {
115     // let the command line options over ride
116     windowPosition = PositionSize(0,0,mCommandLineOptions->stageWidth,mCommandLineOptions->stageHeight);
117   }
118
119   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
120 }
121
122 void Application::CreateAdaptor()
123 {
124   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
125
126   mAdaptor = &Dali::Adaptor::New( mWindow, mBaseLayout);
127
128   // Allow DPI to be overridden from command line.
129   unsigned int hDPI=DEFAULT_HORIZONTAL_DPI;
130   unsigned int vDPI=DEFAULT_VERTICAL_DPI;
131
132   std::string dpiStr = mCommandLineOptions->stageDPI;
133   if(!dpiStr.empty())
134   {
135     sscanf(dpiStr.c_str(), "%ux%u", &hDPI, &vDPI);
136   }
137   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetDpi(hDPI, vDPI);
138
139   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
140 }
141
142 void Application::MainLoop()
143 {
144   // Run the application
145   mFramework->Run();
146 }
147
148 void Application::Lower()
149 {
150   // Lower the application without quitting it.
151   mWindow.Lower();
152 }
153
154 void Application::Quit()
155 {
156   // Actually quit the application.
157   AddIdle(boost::bind(&Application::QuitFromMainLoop, this));
158 }
159
160 void Application::QuitFromMainLoop()
161 {
162   mAdaptor->Stop();
163
164   Dali::Application application(this);
165   mTerminateSignalV2.Emit( application );
166
167   mFramework->Quit();
168   // This will trigger OnTerminate(), below, after the main loop has completed.
169   mInitialized = false;
170 }
171
172 void Application::OnInit()
173 {
174   mFramework->AddAbortCallback(boost::bind(&Application::QuitFromMainLoop, this));
175
176   CreateWindow();
177   CreateAdaptor();
178
179   // Run the adaptor
180   mAdaptor->Start();
181
182   // Check if user requires no vsyncing and set on X11 Adaptor
183   if (mCommandLineOptions->noVSyncOnRender)
184   {
185     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).DisableVSync();
186   }
187
188   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
189   if( mCommandLineOptions->viewMode != 0 )
190   {
191     ViewMode viewMode = MONO;
192     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
193     {
194       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
195     }
196     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
197   }
198
199   mInitialized = true;
200
201   // in default, auto hide indicator mode
202   mWindow.ShowIndicator(Dali::Window::AUTO);
203
204   Dali::Application application(this);
205   mInitSignalV2.Emit( application );
206 }
207
208 void Application::OnTerminate()
209 {
210   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
211   // delete the window as ecore_x has been destroyed by AppCore
212
213   mWindow.Reset();
214   mInitialized = false;
215 }
216
217 void Application::OnPause()
218 {
219   mAdaptor->Pause();
220   Dali::Application application(this);
221   mPauseSignalV2.Emit( application );
222 }
223
224 void Application::OnResume()
225 {
226   mAdaptor->Resume();
227   Dali::Application application(this);
228   mResumeSignalV2.Emit( application );
229 }
230
231 void Application::OnReset()
232 {
233   /*
234    * usually, reset callback was called when a caller request to launch this application via aul.
235    * because Application class already handled initialization in OnInit(), OnReset do nothing.
236    */
237   Dali::Application application(this);
238   mResetSignalV2.Emit( application );
239
240   mWindow.Raise();
241 }
242
243 void Application::OnLanguageChanged()
244 {
245   mAdaptor->NotifyLanguageChanged();
246 }
247
248 void Application::OnResize(Dali::Adaptor& adaptor)
249 {
250   Dali::Application application(this);
251   mResizeSignalV2.Emit( application );
252 }
253
254 bool Application::AddIdle(boost::function<void(void)> callBack)
255 {
256   return mAdaptor->AddIdle(callBack);
257 }
258
259 Dali::Adaptor& Application::GetAdaptor()
260 {
261   return *mAdaptor;
262 }
263
264 Dali::Window Application::GetWindow()
265 {
266   return mWindow;
267 }
268
269 Dali::Application Application::Get()
270 {
271   DALI_ASSERT_ALWAYS( gThreadLocalApplication.get() != NULL && "Application not instantiated" );
272
273   Dali::Application application(gThreadLocalApplication.get());
274
275   return application;
276 }
277
278 const std::string& Application::GetTheme()
279 {
280   return Dali::StyleMonitor::Get().GetTheme();
281 }
282
283 void Application::SetTheme(const std::string& themeFilePath)
284 {
285   return Dali::StyleMonitor::Get().SetTheme(themeFilePath);
286 }
287
288 // Stereoscopy
289
290 void Application::SetViewMode( ViewMode viewMode )
291 {
292   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
293 }
294
295 ViewMode Application::GetViewMode() const
296 {
297   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
298 }
299
300 void Application::SetStereoBase( float stereoBase )
301 {
302   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
303 }
304
305 float Application::GetStereoBase() const
306 {
307   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
308 }
309
310 } // namespace Adaptor
311
312 } // namespace Internal
313
314 } // namespace Dali