Merge "Revert "recover PixmapImage::GetPixmap() without any parameter"" into 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 #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   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetCore().SceneCreated();
215 }
216
217 void Application::OnTerminate()
218 {
219   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
220   // delete the window as ecore_x has been destroyed by AppCore
221
222   mWindow.Reset();
223   mInitialized = false;
224 }
225
226 void Application::OnPause()
227 {
228   mAdaptor->Pause();
229   Dali::Application application(this);
230   mPauseSignalV2.Emit( application );
231 }
232
233 void Application::OnResume()
234 {
235   mAdaptor->Resume();
236   Dali::Application application(this);
237   mResumeSignalV2.Emit( application );
238 }
239
240 void Application::OnReset()
241 {
242   /*
243    * usually, reset callback was called when a caller request to launch this application via aul.
244    * because Application class already handled initialization in OnInit(), OnReset do nothing.
245    */
246   Dali::Application application(this);
247   mResetSignalV2.Emit( application );
248
249   mWindow.Raise();
250 }
251
252 void Application::OnLanguageChanged()
253 {
254   mAdaptor->NotifyLanguageChanged();
255 }
256
257 void Application::OnResize(Dali::Adaptor& adaptor)
258 {
259   Dali::Application application(this);
260   mResizeSignalV2.Emit( application );
261 }
262
263 bool Application::AddIdle(boost::function<void(void)> callBack)
264 {
265   return mAdaptor->AddIdle(callBack);
266 }
267
268 Dali::Adaptor& Application::GetAdaptor()
269 {
270   return *mAdaptor;
271 }
272
273 Dali::Window Application::GetWindow()
274 {
275   return mWindow;
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
311 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
312 {
313   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
314   Window& windowImpl = GetImplementation(newWindow);
315   windowImpl.SetAdaptor(*mAdaptor);
316   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
317   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
318   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(*renderSurface);
319   mWindow = newWindow;
320 }
321
322 } // namespace Adaptor
323
324 } // namespace Internal
325
326 } // namespace Dali