[3.0] Fix build error
[platform/core/uifw/dali-adaptor.git] / adaptors / common / application-impl.cpp
1 /*
2  * Copyright (c) 2017 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   mWindow.Reset();
105   delete mAdaptor;
106   delete mCommandLineOptions;
107   delete mFramework;
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   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
141 }
142
143 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
144 {
145   mContextLossConfiguration = configuration;
146
147   // Run the application
148   mFramework->Run();
149 }
150
151 void Application::Lower()
152 {
153   // Lower the application without quitting it.
154   mWindow.Lower();
155 }
156
157 void Application::Quit()
158 {
159   // Actually quit the application.
160   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
161 }
162
163 void Application::QuitFromMainLoop()
164 {
165   mAdaptor->Stop();
166
167   mFramework->Quit();
168   // This will trigger OnTerminate(), below, after the main loop has completed.
169 }
170
171 void Application::OnInit()
172 {
173   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
174
175   CreateWindow();
176
177   // Start the adaptor
178   CreateAdaptor();
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   if( ! mStylesheet.empty() )
199   {
200     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
201   }
202
203   // Wire up the LifecycleController
204   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
205
206   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
207   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
208   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
209   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
210   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
211   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
212   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
213
214   Dali::Application application(this);
215   mInitSignal.Emit( application );
216
217   mAdaptor->NotifySceneCreated();
218 }
219
220 void Application::OnTerminate()
221 {
222   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
223   // delete the window as ecore_x has been destroyed by AppCore
224
225   Dali::Application application(this);
226   mTerminateSignal.Emit( application );
227
228   if( mAdaptor )
229   {
230     // Ensure that the render-thread is not using the surface(window) after we delete it
231     mAdaptor->Stop();
232   }
233
234   mWindow.Reset();
235 }
236
237 void Application::OnPause()
238 {
239   // A DALi app should handle Pause/Resume events.
240   // DALi just delivers the framework Pause event to the application, but not actually pause DALi core
241   // Pausing DALi core only occurs on the Window Hidden framework event
242   Dali::Application application(this);
243   mPauseSignal.Emit( application );
244 }
245
246 void Application::OnResume()
247 {
248   // Emit the signal first so the application can queue any messages before we do an update/render
249   // This ensures we do not just redraw the last frame before pausing if that's not required
250   Dali::Application application(this);
251   mResumeSignal.Emit( application );
252
253   // DALi just delivers the framework Resume event to the application.
254   // Resuming DALi core only occurs on the Window Show framework event
255 }
256
257 void Application::OnReset()
258 {
259   /*
260    * usually, reset callback was called when a caller request to launch this application via aul.
261    * because Application class already handled initialization in OnInit(), OnReset do nothing.
262    */
263   Dali::Application application(this);
264   mResetSignal.Emit( application );
265 }
266
267 void Application::OnAppControl(void *data)
268 {
269   Dali::Application application(this);
270   mAppControlSignal.Emit( application , data );
271 }
272
273 void Application::OnLanguageChanged()
274 {
275   mAdaptor->NotifyLanguageChanged();
276   Dali::Application application(this);
277   mLanguageChangedSignal.Emit( application );
278 }
279
280 void Application::OnRegionChanged()
281 {
282   Dali::Application application(this);
283   mRegionChangedSignal.Emit( application );
284 }
285
286 void Application::OnBatteryLow()
287 {
288   Dali::Application application(this);
289   mBatteryLowSignal.Emit( application );
290 }
291
292 void Application::OnMemoryLow()
293 {
294   Dali::Application application(this);
295   mMemoryLowSignal.Emit( application );
296 }
297
298 void Application::OnResize(Dali::Adaptor& adaptor)
299 {
300   Dali::Application application(this);
301   mResizeSignal.Emit( application );
302 }
303
304 bool Application::AddIdle( CallbackBase* callback )
305 {
306   return mAdaptor->AddIdle( callback );
307 }
308
309 Dali::Adaptor& Application::GetAdaptor()
310 {
311   return *mAdaptor;
312 }
313
314 Dali::Window Application::GetWindow()
315 {
316   return mWindow;
317 }
318
319 // Stereoscopy
320
321 void Application::SetViewMode( ViewMode viewMode )
322 {
323   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
324 }
325
326 ViewMode Application::GetViewMode() const
327 {
328   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
329 }
330
331 void Application::SetStereoBase( float stereoBase )
332 {
333   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
334 }
335
336 float Application::GetStereoBase() const
337 {
338   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
339 }
340
341
342 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
343 {
344   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
345   Window& windowImpl = GetImplementation(newWindow);
346   windowImpl.SetAdaptor(*mAdaptor);
347   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
348   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
349
350   Any nativeWindow = newWindow.GetNativeHandle();
351   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
352   mWindow = newWindow;
353 }
354
355 } // namespace Adaptor
356
357 } // namespace Internal
358
359 } // namespace Dali