Merge "Change to use new AppFW APi for TV profile" 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
24 // INTERNAL INCLUDES
25 #include <style-monitor.h>
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 : mInitSignal(),
72   mTerminateSignal(),
73   mPauseSignal(),
74   mResumeSignal(),
75   mResetSignal(),
76   mResizeSignal(),
77   mAppControlSignal(),
78   mLanguageChangedSignal(),
79   mRegionChangedSignal(),
80   mBatteryLowSignal(),
81   mMemoryLowSignal(),
82   mEventLoop( NULL ),
83   mFramework( NULL ),
84   mCommandLineOptions( NULL ),
85   mSingletonService( SingletonService::New() ),
86   mAdaptor( NULL ),
87   mWindow(),
88   mWindowMode( windowMode ),
89   mName( name ),
90   mInitialized( false ),
91   mBaseLayout( baseLayout ),
92   mSlotDelegate( this )
93 {
94   mCommandLineOptions = new CommandLineOptions(argc, argv);
95
96   mFramework = new Framework(*this, argc, argv, name);
97 }
98
99 Application::~Application()
100 {
101   mSingletonService.UnregisterAll();
102
103   delete mFramework;
104   delete mCommandLineOptions;
105   delete mAdaptor;
106   mWindow.Reset();
107 }
108
109 void Application::CreateWindow()
110 {
111 #ifndef __arm__
112    PositionSize windowPosition(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
113 #else
114    PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
115 #endif
116   if (mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
117   {
118     // let the command line options over ride
119     windowPosition = PositionSize(0,0,mCommandLineOptions->stageWidth,mCommandLineOptions->stageHeight);
120   }
121
122   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
123 }
124
125 void Application::CreateAdaptor()
126 {
127   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
128
129   mAdaptor = &Dali::Adaptor::New( mWindow, mBaseLayout, mContextLossConfiguration );
130
131   // Allow DPI to be overridden from command line.
132   unsigned int hDPI=DEFAULT_HORIZONTAL_DPI;
133   unsigned int vDPI=DEFAULT_VERTICAL_DPI;
134
135   std::string dpiStr = mCommandLineOptions->stageDPI;
136   if(!dpiStr.empty())
137   {
138     sscanf(dpiStr.c_str(), "%ux%u", &hDPI, &vDPI);
139   }
140   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetDpi(hDPI, vDPI);
141
142   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
143 }
144
145 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
146 {
147   mContextLossConfiguration = configuration;
148
149   // Run the application
150   mFramework->Run();
151 }
152
153 void Application::Lower()
154 {
155   // Lower the application without quitting it.
156   mWindow.Lower();
157 }
158
159 void Application::Quit()
160 {
161   // Actually quit the application.
162   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
163 }
164
165 void Application::QuitFromMainLoop()
166 {
167   mAdaptor->Stop();
168
169   Dali::Application application(this);
170   mTerminateSignal.Emit( application );
171
172   mFramework->Quit();
173   // This will trigger OnTerminate(), below, after the main loop has completed.
174   mInitialized = false;
175 }
176
177 void Application::OnInit()
178 {
179   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
180
181   CreateWindow();
182   CreateAdaptor();
183
184   // Run the adaptor
185   mAdaptor->Start();
186
187   // Check if user requires no vsyncing and set on X11 Adaptor
188   if (mCommandLineOptions->noVSyncOnRender)
189   {
190     mAdaptor->SetUseHardwareVSync(false);
191   }
192
193   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
194   if( mCommandLineOptions->viewMode != 0 )
195   {
196     ViewMode viewMode = MONO;
197     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
198     {
199       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
200     }
201     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
202   }
203
204   mInitialized = true;
205
206   // Wire up the LifecycleController
207   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
208
209   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
210   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
211   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
212   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
213   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
214   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
215   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
216
217   Dali::Application application(this);
218   mInitSignal.Emit( application );
219
220   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetCore().SceneCreated();
221 }
222
223 void Application::OnTerminate()
224 {
225   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
226   // delete the window as ecore_x has been destroyed by AppCore
227
228   mWindow.Reset();
229   mInitialized = false;
230 }
231
232 void Application::OnPause()
233 {
234   mAdaptor->Pause();
235   Dali::Application application(this);
236   mPauseSignal.Emit( application );
237 }
238
239 void Application::OnResume()
240 {
241   mAdaptor->Resume();
242   Dali::Application application(this);
243   mResumeSignal.Emit( application );
244 }
245
246 void Application::OnReset()
247 {
248   /*
249    * usually, reset callback was called when a caller request to launch this application via aul.
250    * because Application class already handled initialization in OnInit(), OnReset do nothing.
251    */
252   Dali::Application application(this);
253   mResetSignal.Emit( application );
254
255   mWindow.Raise();
256 }
257
258 void Application::OnAppControl(void *data)
259 {
260   Dali::Application application(this);
261   mAppControlSignal.Emit( application , data );
262 }
263
264 void Application::OnLanguageChanged()
265 {
266   mAdaptor->NotifyLanguageChanged();
267 }
268
269 void Application::OnRegionChanged()
270 {
271   Dali::Application application(this);
272   mRegionChangedSignal.Emit( application );
273 }
274
275 void Application::OnBatteryLow()
276 {
277   Dali::Application application(this);
278   mBatteryLowSignal.Emit( application );
279 }
280
281 void Application::OnMemoryLow()
282 {
283   Dali::Application application(this);
284   mMemoryLowSignal.Emit( application );
285 }
286
287 void Application::OnResize(Dali::Adaptor& adaptor)
288 {
289   Dali::Application application(this);
290   mResizeSignal.Emit( application );
291 }
292
293 bool Application::AddIdle( CallbackBase* callback )
294 {
295   return mAdaptor->AddIdle( callback );
296 }
297
298 Dali::Adaptor& Application::GetAdaptor()
299 {
300   return *mAdaptor;
301 }
302
303 Dali::Window Application::GetWindow()
304 {
305   return mWindow;
306 }
307
308 const std::string& Application::GetTheme()
309 {
310   return Dali::StyleMonitor::Get().GetTheme();
311 }
312
313 void Application::SetTheme(const std::string& themeFilePath)
314 {
315   return Dali::StyleMonitor::Get().SetTheme(themeFilePath);
316 }
317
318 // Stereoscopy
319
320 void Application::SetViewMode( ViewMode viewMode )
321 {
322   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
323 }
324
325 ViewMode Application::GetViewMode() const
326 {
327   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
328 }
329
330 void Application::SetStereoBase( float stereoBase )
331 {
332   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
333 }
334
335 float Application::GetStereoBase() const
336 {
337   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
338 }
339
340
341 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
342 {
343   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
344   Window& windowImpl = GetImplementation(newWindow);
345   windowImpl.SetAdaptor(*mAdaptor);
346   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
347   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
348   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(*renderSurface);
349   mWindow = newWindow;
350 }
351
352 } // namespace Adaptor
353
354 } // namespace Internal
355
356 } // namespace Dali