Merge "This patch is for refining dali application to support tizen c# application...
[platform/core/uifw/dali-adaptor.git] / adaptors / common / application-impl.cpp
1 /*
2  * Copyright (c) 2016 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 <common/framework.h>
29 #include <singleton-service-impl.h>
30 #include <lifecycle-controller-impl.h>
31
32 namespace Dali
33 {
34
35 namespace TizenPlatform
36 {
37 class TizenPlatformAbstraction;
38 }
39
40 namespace Integration
41 {
42 class Core;
43 }
44
45 namespace Internal
46 {
47
48 namespace Adaptor
49 {
50
51 ApplicationPtr Application::New(
52   int* argc,
53   char **argv[],
54   const std::string& stylesheet,
55   Dali::Application::WINDOW_MODE windowMode,
56   Framework::Type applicationType)
57 {
58   ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, applicationType ) );
59   return application;
60 }
61
62 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
63   Dali::Application::WINDOW_MODE windowMode, Framework::Type applicationType )
64 : mInitSignal(),
65   mTerminateSignal(),
66   mPauseSignal(),
67   mResumeSignal(),
68   mResetSignal(),
69   mResizeSignal(),
70   mAppControlSignal(),
71   mLanguageChangedSignal(),
72   mRegionChangedSignal(),
73   mBatteryLowSignal(),
74   mMemoryLowSignal(),
75   mEventLoop( NULL ),
76   mFramework( NULL ),
77   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
78   mCommandLineOptions( NULL ),
79   mSingletonService( SingletonService::New() ),
80   mAdaptor( NULL ),
81   mWindow(),
82   mWindowMode( windowMode ),
83   mName(),
84   mStylesheet( stylesheet ),
85   mEnvironmentOptions(),
86   mSlotDelegate( this )
87 {
88   // Get mName from environment options
89   mName = mEnvironmentOptions.GetWindowName();
90   if( mName.empty() && argc && ( *argc > 0 ) )
91   {
92     // Set mName from command-line args if environment option not set
93     mName = (*argv)[0];
94   }
95
96   mCommandLineOptions = new CommandLineOptions(argc, argv);
97   mFramework = new Framework( *this, argc, argv, applicationType );
98 }
99
100 Application::~Application()
101 {
102   mSingletonService.UnregisterAll();
103
104   delete mFramework;
105   delete mCommandLineOptions;
106   delete mAdaptor;
107   mWindow.Reset();
108 }
109
110 void Application::CreateWindow()
111 {
112   PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
113
114   if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
115   {
116     // Command line options override environment options and full screen
117     windowPosition = PositionSize( 0, 0, mCommandLineOptions->stageWidth, mCommandLineOptions->stageHeight );
118   }
119   else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
120   {
121     // Environment options override full screen functionality if command line arguments not provided
122     windowPosition = PositionSize( 0, 0, mEnvironmentOptions.GetWindowWidth(), mEnvironmentOptions.GetWindowHeight() );
123   }
124
125   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
126   mWindow = Dali::Window::New( windowPosition, mName, windowClassName, mWindowMode == Dali::Application::TRANSPARENT );
127
128   // Quit the application when the window is closed
129   GetImplementation( mWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
130 }
131
132 void Application::CreateAdaptor()
133 {
134   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
135
136   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
137
138   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
139 }
140
141 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
142 {
143   mContextLossConfiguration = configuration;
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( MakeCallback( this, &Application::QuitFromMainLoop ) );
159 }
160
161 void Application::QuitFromMainLoop()
162 {
163   mAdaptor->Stop();
164
165   mFramework->Quit();
166   // This will trigger OnTerminate(), below, after the main loop has completed.
167 }
168
169 void Application::DoInit()
170 {
171   CreateWindow();
172   CreateAdaptor();
173
174   // Run the adaptor
175   mAdaptor->Start();
176
177   // Check if user requires no vsyncing and set Adaptor
178   if (mCommandLineOptions->noVSyncOnRender)
179   {
180     mAdaptor->SetUseHardwareVSync(false);
181   }
182
183   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
184   if( mCommandLineOptions->viewMode != 0 )
185   {
186     ViewMode viewMode = MONO;
187     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
188     {
189       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
190     }
191     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
192   }
193
194   if( ! mStylesheet.empty() )
195   {
196     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
197   }
198
199   mAdaptor->NotifySceneCreated();
200 }
201
202 void Application::DoTerminate()
203 {
204   if( mAdaptor )
205   {
206     // Ensure that the render-thread is not using the surface(window) after we delete it
207     mAdaptor->Stop();
208   }
209
210   mWindow.Reset();
211 }
212
213 void Application::DoPause()
214 {
215   mAdaptor->Pause();
216 }
217
218 void Application::DoResume()
219 {
220   mAdaptor->Resume();
221 }
222
223 void Application::DoLanguageChange()
224 {
225   mAdaptor->NotifyLanguageChanged();
226 }
227
228 void Application::OnInit()
229 {
230   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
231
232   DoInit();
233
234   // Wire up the LifecycleController
235   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
236
237   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
238   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
239   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
240   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
241   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
242   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
243   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
244
245   Dali::Application application(this);
246   mInitSignal.Emit( application );
247 }
248
249 void Application::OnTerminate()
250 {
251   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
252   // delete the window as ecore_x has been destroyed by AppCore
253
254   Dali::Application application(this);
255   mTerminateSignal.Emit( application );
256
257   DoTerminate();
258 }
259
260 void Application::OnPause()
261 {
262   DoPause();
263   Dali::Application application(this);
264   mPauseSignal.Emit( application );
265 }
266
267 void Application::OnResume()
268 {
269   // Emit the signal first so the application can queue any messages before we do an update/render
270   // This ensures we do not just redraw the last frame before pausing if that's not required
271   Dali::Application application(this);
272   mResumeSignal.Emit( application );
273   DoResume();
274 }
275
276 void Application::OnReset()
277 {
278   /*
279    * usually, reset callback was called when a caller request to launch this application via aul.
280    * because Application class already handled initialization in OnInit(), OnReset do nothing.
281    */
282   Dali::Application application(this);
283   mResetSignal.Emit( application );
284 }
285
286 void Application::OnAppControl(void *data)
287 {
288   Dali::Application application(this);
289   mAppControlSignal.Emit( application , data );
290 }
291
292 void Application::OnLanguageChanged()
293 {
294   DoLanguageChange();
295   Dali::Application application(this);
296   mLanguageChangedSignal.Emit( application );
297 }
298
299 void Application::OnRegionChanged()
300 {
301   Dali::Application application(this);
302   mRegionChangedSignal.Emit( application );
303 }
304
305 void Application::OnBatteryLow()
306 {
307   Dali::Application application(this);
308   mBatteryLowSignal.Emit( application );
309 }
310
311 void Application::OnMemoryLow()
312 {
313   Dali::Application application(this);
314   mMemoryLowSignal.Emit( application );
315 }
316
317 void Application::OnResize(Dali::Adaptor& adaptor)
318 {
319   Dali::Application application(this);
320   mResizeSignal.Emit( application );
321 }
322
323 bool Application::AddIdle( CallbackBase* callback )
324 {
325   return mAdaptor->AddIdle( callback );
326 }
327
328 Dali::Adaptor& Application::GetAdaptor()
329 {
330   return *mAdaptor;
331 }
332
333 Dali::Window Application::GetWindow()
334 {
335   return mWindow;
336 }
337
338 // Stereoscopy
339
340 void Application::SetViewMode( ViewMode viewMode )
341 {
342   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
343 }
344
345 ViewMode Application::GetViewMode() const
346 {
347   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
348 }
349
350 void Application::SetStereoBase( float stereoBase )
351 {
352   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
353 }
354
355 float Application::GetStereoBase() const
356 {
357   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
358 }
359
360
361 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
362 {
363   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
364   Window& windowImpl = GetImplementation(newWindow);
365   windowImpl.SetAdaptor(*mAdaptor);
366   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
367   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
368
369   Any nativeWindow = newWindow.GetNativeHandle();
370   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
371   mWindow = newWindow;
372 }
373
374 std::string Application::GetResourcePath()
375 {
376   return Internal::Adaptor::Framework::GetResourcePath();
377 }
378
379 } // namespace Adaptor
380
381 } // namespace Internal
382
383 } // namespace Dali