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