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