b9a6603bd8f7082752e2d413f95bf109cfe45834
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / internal / application-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include "application-impl.h"
19
20 // EXTERNAL INCLUDES
21 #include <Ecore_X.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/adaptor-framework/common/style-monitor.h>
24
25 // INTERNAL INCLUDES
26 #include <internal/command-line-options.h>
27 #include <internal/common/adaptor-impl.h>
28 #include <internal/common/ecore-x/ecore-x-render-surface.h>
29
30 namespace Dali
31 {
32
33 namespace SlpPlatform
34 {
35 class SlpPlatformAbstraction;
36 }
37
38 namespace Integration
39 {
40 class Core;
41 }
42
43 namespace Internal
44 {
45
46 namespace Adaptor
47 {
48
49 namespace
50 {
51 // Defaults taken from H2 device
52 const unsigned int DEFAULT_WINDOW_WIDTH   = 480;
53 const unsigned int DEFAULT_WINDOW_HEIGHT  = 800;
54 const float        DEFAULT_HORIZONTAL_DPI = 220;
55 const float        DEFAULT_VERTICAL_DPI   = 217;
56
57 boost::thread_specific_ptr<Application> gThreadLocalApplication;
58 }
59
60 ApplicationPtr Application::New(
61   int* argc,
62   char **argv[],
63   const std::string& name,
64   const DeviceLayout& baseLayout,
65   Dali::Application::WINDOW_MODE windowMode)
66 {
67   ApplicationPtr application ( new Application (argc, argv, name, baseLayout, windowMode ) );
68   return application;
69 }
70
71 Application::Application(
72   int* argc,
73   char** argv[],
74   const std::string& name,
75   const DeviceLayout& baseLayout,
76   Dali::Application::WINDOW_MODE windowMode)
77 : mFramework(NULL),
78   mCommandLineOptions(NULL),
79   mAdaptor(NULL),
80   mWindow(),
81   mWindowMode( windowMode ),
82   mName(name),
83   mInitialized(false),
84   mBaseLayout(baseLayout),
85   mSlotDelegate( this )
86 {
87   // make sure we don't create the local thread application instance twice
88   DALI_ASSERT_ALWAYS(gThreadLocalApplication.get() == NULL && "Cannot create more than one Application per thread" );
89
90   // reset is used to store a new value associated with this thread
91   gThreadLocalApplication.reset(this);
92
93   mCommandLineOptions = new CommandLineOptions(argc, argv);
94
95   mFramework = new Framework(*this, argc, argv, name);
96 }
97
98 Application::~Application()
99 {
100   delete mFramework;
101   delete mCommandLineOptions;
102   delete mAdaptor;
103   mWindow.Reset();
104   gThreadLocalApplication.release();
105 }
106
107 void Application::CreateWindow()
108 {
109 #ifndef __arm__
110    PositionSize windowPosition(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
111 #else
112    PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
113 #endif
114   if (mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
115   {
116     // let the command line options over ride
117     windowPosition = PositionSize(0,0,mCommandLineOptions->stageWidth,mCommandLineOptions->stageHeight);
118   }
119
120   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
121 }
122
123 void Application::CreateAdaptor()
124 {
125   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
126
127   mAdaptor = &Dali::Adaptor::New( mWindow, mBaseLayout);
128
129   // Allow DPI to be overridden from command line.
130   unsigned int hDPI=DEFAULT_HORIZONTAL_DPI;
131   unsigned int vDPI=DEFAULT_VERTICAL_DPI;
132
133   std::string dpiStr = mCommandLineOptions->stageDPI;
134   if(!dpiStr.empty())
135   {
136     sscanf(dpiStr.c_str(), "%ux%u", &hDPI, &vDPI);
137   }
138   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetDpi(hDPI, vDPI);
139
140   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
141 }
142
143 void Application::MainLoop()
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     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).DisableVSync();
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   Dali::Application application(this);
203   mInitSignalV2.Emit( application );
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   mPauseSignalV2.Emit( application );
220 }
221
222 void Application::OnResume()
223 {
224   mAdaptor->Resume();
225   Dali::Application application(this);
226   mResumeSignalV2.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   mResetSignalV2.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   mResizeSignalV2.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 Dali::Application Application::Get()
268 {
269   DALI_ASSERT_ALWAYS( gThreadLocalApplication.get() != NULL && "Application not instantiated" );
270
271   Dali::Application application(gThreadLocalApplication.get());
272
273   return application;
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 } // namespace Adaptor
309
310 } // namespace Internal
311
312 } // namespace Dali