Revert "[Tizen](ATSPI) squashed implementation"
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / application-impl.cpp
1 /*
2  * Copyright (c) 2018 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 <dali/internal/adaptor/common/application-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/adaptor-framework/style-monitor.h>
26 #include <dali/devel-api/text-abstraction/font-client.h>
27 #include <dali/internal/adaptor/common/adaptor-impl.h>
28 #include <dali/internal/system/common/command-line-options.h>
29 #include <dali/internal/adaptor/common/framework.h>
30 #include <dali/internal/system/common/singleton-service-impl.h>
31 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
32 #include <dali/internal/window-system/common/window-impl.h>
33 #include <dali/internal/window-system/common/window-render-surface.h>
34 #include <dali/internal/window-system/common/render-surface-factory.h>
35
36 // To disable a macro with the same name from one of OpenGL headers
37 #undef Status
38
39 namespace Dali
40 {
41
42 namespace TizenPlatform
43 {
44 class TizenPlatformAbstraction;
45 }
46
47 namespace Integration
48 {
49 class Core;
50 }
51
52 namespace Internal
53 {
54
55 namespace Adaptor
56 {
57
58 namespace
59 {
60
61 const float DEFAULT_STEREO_BASE( 65.0f );
62
63 } // unnamed namespace
64
65 ApplicationPtr Application::gPreInitializedApplication( NULL );
66
67 ApplicationPtr Application::New(
68   int* argc,
69   char **argv[],
70   const std::string& stylesheet,
71   Dali::Application::WINDOW_MODE windowMode,
72   const PositionSize& positionSize,
73   Framework::Type applicationType)
74 {
75   ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
76   return application;
77 }
78
79 void Application::PreInitialize( int* argc, char** argv[] )
80 {
81   if( !gPreInitializedApplication )
82   {
83     gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
84
85     gPreInitializedApplication->CreateWindow();    // Only create window
86
87     gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
88
89     //Make DefaultFontDescription cached
90     Dali::TextAbstraction::FontClient fontClient = Dali::TextAbstraction::FontClient::Get();
91     Dali::TextAbstraction::FontDescription defaultFontDescription;
92     fontClient.GetDefaultPlatformFontDescription( defaultFontDescription );
93   }
94 }
95
96 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
97   Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
98 : mInitSignal(),
99   mTerminateSignal(),
100   mPauseSignal(),
101   mResumeSignal(),
102   mResetSignal(),
103   mResizeSignal(),
104   mAppControlSignal(),
105   mLanguageChangedSignal(),
106   mRegionChangedSignal(),
107   mBatteryLowSignal(),
108   mMemoryLowSignal(),
109   mEventLoop( nullptr ),
110   mFramework( nullptr ),
111   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
112   mCommandLineOptions( nullptr ),
113   mSingletonService( SingletonService::New() ),
114   mAdaptorBuilder( nullptr ),
115   mAdaptor( nullptr ),
116   mMainWindow(),
117   mMainWindowMode( windowMode ),
118   mMainWindowName(),
119   mMainWindowReplaced( false ),
120   mStylesheet( stylesheet ),
121   mEnvironmentOptions(),
122   mWindowPositionSize( positionSize ),
123   mLaunchpadState( Launchpad::NONE ),
124   mSlotDelegate( this ),
125   mViewMode( MONO ),
126   mStereoBase( DEFAULT_STEREO_BASE )
127 {
128   // Get mName from environment options
129   mMainWindowName = mEnvironmentOptions.GetWindowName();
130   if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
131   {
132     // Set mName from command-line args if environment option not set
133     mMainWindowName = (*argv)[0];
134   }
135
136   mCommandLineOptions = new CommandLineOptions(argc, argv);
137   mFramework = new Framework( *this, argc, argv, applicationType );
138   mUseRemoteSurface = (applicationType == Framework::WATCH);
139 }
140
141 Application::~Application()
142 {
143   mSingletonService.UnregisterAll();
144
145   mMainWindow.Reset();
146   delete mAdaptor;
147   delete mAdaptorBuilder;
148   delete mCommandLineOptions;
149   delete mFramework;
150 }
151
152 void Application::CreateWindow()
153 {
154   if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
155   {
156     if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
157     {
158       // Command line options override environment options and full screen
159       mWindowPositionSize.width = mCommandLineOptions->stageWidth;
160       mWindowPositionSize.height = mCommandLineOptions->stageHeight;
161     }
162     else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
163     {
164       // Environment options override full screen functionality if command line arguments not provided
165       mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
166       mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
167     }
168   }
169
170   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
171
172   Internal::Adaptor::Window* window = Internal::Adaptor::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
173   mMainWindow = Dali::Window( window );
174
175   // Quit the application when the window is closed
176   GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
177 }
178
179 void Application::CreateAdaptor()
180 {
181   DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
182
183   auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
184
185   Integration::SceneHolder sceneHolder = Integration::SceneHolder( &Dali::GetImplementation( mMainWindow ) );
186
187   mAdaptor = Adaptor::New( graphicsFactory, sceneHolder, mContextLossConfiguration, &mEnvironmentOptions );
188
189   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
190
191   Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
192 }
193
194 void Application::CreateAdaptorBuilder()
195 {
196   mAdaptorBuilder = new AdaptorBuilder();
197 }
198
199 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
200 {
201   mContextLossConfiguration = configuration;
202
203   // Run the application
204   mFramework->Run();
205 }
206
207 void Application::Lower()
208 {
209   // Lower the application without quitting it.
210   mMainWindow.Lower();
211 }
212
213 void Application::Quit()
214 {
215   // Actually quit the application.
216   // Force a call to Quit even if adaptor is not running.
217   Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
218 }
219
220 void Application::QuitFromMainLoop()
221 {
222   mAdaptor->Stop();
223
224   mFramework->Quit();
225   // This will trigger OnTerminate(), below, after the main loop has completed.
226 }
227
228 void Application::OnInit()
229 {
230   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
231
232   CreateAdaptorBuilder();
233
234   // If an application was pre-initialized, a window was made in advance
235   if( mLaunchpadState == Launchpad::NONE )
236   {
237     CreateWindow();
238   }
239
240   CreateAdaptor();
241
242   // Run the adaptor
243   mAdaptor->Start();
244
245   if( ! mStylesheet.empty() )
246   {
247     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
248   }
249
250   // Wire up the LifecycleController
251   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
252
253   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
254   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
255   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
256   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
257   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
258   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
259   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
260
261   Dali::Application application(this);
262   mInitSignal.Emit( application );
263
264   mAdaptor->NotifySceneCreated();
265 }
266
267 void Application::OnTerminate()
268 {
269   // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
270   // delete the window as ecore_x has been destroyed by AppCore
271
272   Dali::Application application(this);
273   mTerminateSignal.Emit( application );
274
275   if( mAdaptor )
276   {
277     // Ensure that the render-thread is not using the surface(window) after we delete it
278     mAdaptor->Stop();
279   }
280
281   mMainWindow.Reset(); // This only resets (clears) the default Window
282 }
283
284 void Application::OnPause()
285 {
286   // A DALi app should handle Pause/Resume events.
287   // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
288   // Pausing DALi core only occurs on the Window Hidden framework event
289   Dali::Application application(this);
290   mPauseSignal.Emit( application );
291 }
292
293 void Application::OnResume()
294 {
295   // Emit the signal first so the application can queue any messages before we do an update/render
296   // This ensures we do not just redraw the last frame before pausing if that's not required
297   Dali::Application application(this);
298   mResumeSignal.Emit( application );
299
300   // DALi just delivers the framework Resume event to the application.
301   // Resuming DALi core only occurs on the Window Show framework event
302
303   // Trigger processing of events queued up while paused
304   CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
305   coreEventInterface.ProcessCoreEvents();
306 }
307
308 void Application::OnReset()
309 {
310   /*
311    * usually, reset callback was called when a caller request to launch this application via aul.
312    * because Application class already handled initialization in OnInit(), OnReset do nothing.
313    */
314   Dali::Application application(this);
315   mResetSignal.Emit( application );
316 }
317
318 void Application::OnAppControl(void *data)
319 {
320   Dali::Application application(this);
321   mAppControlSignal.Emit( application , data );
322 }
323
324 void Application::OnLanguageChanged()
325 {
326   mAdaptor->NotifyLanguageChanged();
327   Dali::Application application(this);
328   mLanguageChangedSignal.Emit( application );
329 }
330
331 void Application::OnRegionChanged()
332 {
333   Dali::Application application(this);
334   mRegionChangedSignal.Emit( application );
335 }
336
337 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
338 {
339   Dali::Application application(this);
340   mBatteryLowSignal.Emit( application );
341
342   mLowBatterySignal.Emit( status );
343 }
344
345 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
346 {
347   Dali::Application application(this);
348   mMemoryLowSignal.Emit( application );
349
350   mLowMemorySignal.Emit( status );
351 }
352
353 void Application::OnResize(Dali::Adaptor& adaptor)
354 {
355   Dali::Application application(this);
356   mResizeSignal.Emit( application );
357 }
358
359 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
360 {
361   return mAdaptor->AddIdle( callback, hasReturnValue );
362 }
363
364 std::string Application::GetRegion() const
365 {
366   return mFramework->GetRegion();
367 }
368
369 std::string Application::GetLanguage() const
370 {
371   return mFramework->GetLanguage();
372 }
373
374 Dali::Adaptor& Application::GetAdaptor()
375 {
376   return *mAdaptor;
377 }
378
379 Dali::Window Application::GetWindow()
380 {
381   // Changed to return a different window handle after ReplaceWindow is called
382   // just for backward compatibility to make the test case pass
383   if ( mMainWindowReplaced )
384   {
385     Internal::Adaptor::Window* window = Internal::Adaptor::Window::New( PositionSize(), "ReplacedWindow", "", false );
386     return Dali::Window( window );
387   }
388   else
389   {
390     return mMainWindow;
391   }
392 }
393
394 // Stereoscopy
395
396 void Application::SetViewMode( ViewMode viewMode )
397 {
398   mViewMode = viewMode;
399 }
400
401 ViewMode Application::GetViewMode() const
402 {
403   return mViewMode;
404 }
405
406 void Application::SetStereoBase( float stereoBase )
407 {
408   mStereoBase = stereoBase;
409 }
410
411 float Application::GetStereoBase() const
412 {
413   return mStereoBase;
414 }
415
416 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
417 {
418   // This API is kept just for backward compatibility to make the test case pass.
419
420   mMainWindowReplaced = true;
421   OnResize( *mAdaptor );
422 }
423
424 std::string Application::GetResourcePath()
425 {
426   return Internal::Adaptor::Framework::GetResourcePath();
427 }
428
429 std::string Application::GetDataPath()
430 {
431   return Internal::Adaptor::Framework::GetDataPath();
432 }
433
434 void Application::SetStyleSheet( const std::string& stylesheet )
435 {
436   mStylesheet = stylesheet;
437 }
438
439 void Application::SetCommandLineOptions( int* argc, char **argv[] )
440 {
441   delete mCommandLineOptions;
442
443   mCommandLineOptions = new CommandLineOptions( argc, argv );
444
445   mFramework->SetCommandLineOptions( argc, argv );
446 }
447
448 ApplicationPtr Application::GetPreInitializedApplication()
449 {
450   return gPreInitializedApplication;
451 }
452
453 } // namespace Adaptor
454
455 } // namespace Internal
456
457 } // namespace Dali