[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / application-impl.h
1 #ifndef DALI_INTERNAL_APPLICATION_H
2 #define DALI_INTERNAL_APPLICATION_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/math/rect.h>
23 #include <dali/public-api/object/base-object.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/devel-api/common/singleton-service.h>
27 #include <dali/public-api/adaptor-framework/application.h>
28
29 #include <dali/internal/adaptor/common/adaptor-builder-impl.h>
30 #include <dali/internal/adaptor/common/framework.h>
31 #include <dali/internal/system/common/environment-options.h>
32
33 namespace Dali
34 {
35 class Adaptor;
36 class Window;
37
38 namespace Internal
39 {
40 namespace Adaptor
41 {
42 namespace Launchpad
43 {
44 /**
45  * @brief Launchpad is used to improve application launch performance.
46  * When an application is pre-initialized, so files are preloaded, some functions are initialized and a window is made in advance.
47  */
48 enum State
49 {
50   NONE,           ///< The default state
51   PRE_INITIALIZED ///< Application is pre-initialized.
52 };
53
54 } // namespace Launchpad
55
56 class CommandLineOptions;
57 class FrameworkFactory;
58
59 typedef Dali::Rect<int> PositionSize;
60
61 class Application;
62 typedef IntrusivePtr<Application> ApplicationPtr;
63
64 /**
65  * Implementation of the Application class.
66  */
67 class Application : public BaseObject, public Framework::Observer, public Framework::TaskObserver
68 {
69 public:
70   typedef Dali::Application::LowBatterySignalType               LowBatterySignalType;
71   typedef Dali::Application::LowMemorySignalType                LowMemorySignalType;
72   typedef Dali::Application::DeviceOrientationChangedSignalType DeviceOrientationChangedSignalType;
73   typedef Dali::Application::AppSignalType                      AppSignalType;
74   typedef Dali::Application::AppControlSignalType               AppControlSignalType;
75   typedef Dali::Application::WINDOW_MODE                        WINDOW_MODE;
76
77   /**
78    * Create a new application
79    * @param[in]  argc              A pointer to the number of arguments
80    * @param[in]  argv              A pointer to the argument list
81    * @param[in]  stylesheet        The path to user defined theme file
82    * @param[in]  applicationType   A member of Dali::Framework::Type
83    * @param[in]  useUiThread       True if the application would create a UI thread
84    * @param[in]  windowData        The window data
85    *
86    */
87   static ApplicationPtr New(int* argc, char** argv[], const std::string& stylesheet, Framework::Type applicationType, bool useUiThread, const WindowData& windowData);
88
89   /**
90    * @copydoc Dali::DevelApplication::PreInitialize()
91    */
92   static void PreInitialize(int* argc, char** argv[]);
93
94 public:
95   /**
96    * @copydoc Dali::Application::MainLoop()
97    */
98   void MainLoop();
99
100   /**
101    * @copydoc Dali::Application::Lower()
102    */
103   void Lower();
104
105   /**
106    * @copydoc Dali::Application::Quit()
107    */
108   void Quit();
109
110   /**
111    * @copydoc Dali::Application::AddIdle()
112    */
113   bool AddIdle(CallbackBase* callback, bool hasReturnValue);
114
115   /**
116    * @copydoc Dali::Application::GetAdaptor();
117    */
118   Dali::Adaptor& GetAdaptor();
119
120   /**
121    * @copydoc Dali::Application::GetWindow();
122    */
123   Dali::Window GetWindow();
124
125   /**
126    * @copydoc Dali::Application::GetRegion();
127    */
128   std::string GetRegion() const;
129
130   /**
131    * @copydoc Dali::Application::GetLanguage();
132    */
133   std::string GetLanguage() const;
134
135   /**
136    * @copydoc Dali::Application::GetObjectRegistry();
137    */
138   Dali::ObjectRegistry GetObjectRegistry() const;
139
140   /**
141    * @copydoc Dali::Application::GetResourcePath();
142    */
143   static std::string GetResourcePath();
144
145   /**
146    * @copydoc Dali::DevelApplication::GetDataPath()
147    */
148   static std::string GetDataPath();
149
150   /**
151    * Retrieves the pre-initialized application.
152    *
153    * @return A pointer to the pre-initialized application
154    */
155   static ApplicationPtr GetPreInitializedApplication();
156
157   /**
158    * Stores PositionSize of window
159    */
160   void StoreWindowPositionSize(PositionSize positionSize);
161
162   /**
163    * Stores the front buffer rendering status of the window.
164    */
165   void StoreFrontBufferRendering(bool enable);
166
167   /**
168    * @copydoc Dali::DevelApplication::GetRenderThreadId()
169    */
170   int32_t GetRenderThreadId() const;
171
172 public: // From Framework::Observer
173   /**
174    * Called when the framework is initialised.
175    */
176   void OnInit() override;
177
178   /**
179    * Called when the framework is terminated.
180    */
181   void OnTerminate() override;
182
183   /**
184    * Called when the framework is paused.
185    */
186   void OnPause() override;
187
188   /**
189    * Called when the framework resumes from a paused state.
190    */
191   void OnResume() override;
192
193   /**
194    * Called when the framework received AppControlSignal.
195    * @param[in] The bundle data of AppControl event.
196    */
197   void OnAppControl(void* data) override;
198
199   /**
200    * Called when the framework informs the application that it should reset itself.
201    */
202   void OnReset() override;
203
204   /**
205    * Called when the framework informs the application that the language of the device has changed.
206    */
207   void OnLanguageChanged() override;
208
209   /**
210    * Called when the framework informs the application that the region of the device has changed.
211    */
212   void OnRegionChanged() override;
213
214   /**
215    * Called when the framework informs the application that the battery level of the device is low.
216    */
217   void OnBatteryLow(Dali::DeviceStatus::Battery::Status status) override;
218
219   /**
220    * Called when the framework informs the application that the memory level of the device is low.
221    */
222   void OnMemoryLow(Dali::DeviceStatus::Memory::Status status) override;
223
224   /**
225    * Called when the framework informs the application that device orientation is changed.
226    */
227   void OnDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status) override;
228
229   /**
230    * Called when the framework informs the application that the platform surface is created.
231    */
232   void OnSurfaceCreated(Any newSurface) override;
233
234   /**
235    * Called when the framework informs the application that the platform surface is destroyed.
236    */
237   void OnSurfaceDestroyed(Any newSurface) override;
238
239 public: // From Framework::TaskObserver
240   /**
241    * Called when the framework is initialised.
242    */
243   void OnTaskInit() override;
244
245   /**
246    * Called when the framework is terminated.
247    */
248   void OnTaskTerminate() override;
249
250   /**
251    * Called when the framework received AppControlSignal.
252    * @param[in] The bundle data of AppControl event.
253    */
254   void OnTaskAppControl(void* data) override;
255
256   /**
257    * Called when the framework informs the application that the language of the device has changed.
258    */
259   void OnTaskLanguageChanged() override;
260
261   /**
262    * Called when the framework informs the application that the region of the device has changed.
263    */
264   void OnTaskRegionChanged() override;
265
266   /**
267    * Called when the framework informs the application that the battery level of the device is low.
268    */
269   void OnTaskBatteryLow(Dali::DeviceStatus::Battery::Status status) override;
270
271   /**
272    * Called when the framework informs the application that the memory level of the device is low.
273    */
274   void OnTaskMemoryLow(Dali::DeviceStatus::Memory::Status status) override;
275
276   /**
277    * Called when the framework informs the application that the device orientation is changed.
278    *
279    * Device orientation changed event is from Application Framework(Sensor Framework), it means it is system event.
280    * If UIThreading is enable, DALI application has the main thread and UI thread.
281    * This event is emitted in main thread, then it is posted to the UI thread in this callback function.
282    */
283   void OnTaskDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status) override;
284
285 public:
286   /**
287    * Sets a user defined theme file.
288    * This should be called before initialization.
289    * @param[in] stylesheet The path to user defined theme file
290    */
291   void SetStyleSheet(const std::string& stylesheet);
292
293   /**
294    * Sets a command line options.
295    * This is used in case of the preinitialized application.
296    * @param[in] argc A pointer to the number of arguments
297    * @param[in] argv A pointer to the argument list
298    */
299   void SetCommandLineOptions(int* argc, char** argv[]);
300
301   /**
302    * Sets default window type.
303    * This is used in case of the preinitialized application.
304    * @param[in] type the window type for default window
305    */
306   void SetDefaultWindowType(WindowType type);
307
308   /**
309    * @brief Relayout the application and ensure all pending operations are flushed to the update thread.
310    */
311   void FlushUpdateMessages();
312
313 public: // Signals
314   /**
315    * @copydoc Dali::Application::InitSignal()
316    */
317   Dali::Application::AppSignalType& InitSignal()
318   {
319     return mInitSignal;
320   }
321
322   /**
323    * @copydoc Dali::Application::TerminateSignal()
324    */
325   Dali::Application::AppSignalType& TerminateSignal()
326   {
327     return mTerminateSignal;
328   }
329
330   /**
331    * @copydoc Dali::Application::PauseSignal()
332    */
333   Dali::Application::AppSignalType& PauseSignal()
334   {
335     return mPauseSignal;
336   }
337
338   /**
339    * @copydoc Dali::Application::ResumeSignal()
340    */
341   Dali::Application::AppSignalType& ResumeSignal()
342   {
343     return mResumeSignal;
344   }
345
346   /**
347    * @copydoc Dali::Application::ResetSignal()
348    */
349   Dali::Application::AppSignalType& ResetSignal()
350   {
351     return mResetSignal;
352   }
353
354   /**
355    * @copydoc Dali::Application::AppControlSignal()
356    */
357   Dali::Application::AppControlSignalType& AppControlSignal()
358   {
359     return mAppControlSignal;
360   }
361
362   /**
363    * @copydoc Dali::Application::LanguageChangedSignal()
364    */
365   Dali::Application::AppSignalType& LanguageChangedSignal()
366   {
367     return mLanguageChangedSignal;
368   }
369
370   /**
371    * @copydoc Dali::Application::RegionChangedSignal()
372    */
373   Dali::Application::AppSignalType& RegionChangedSignal()
374   {
375     return mRegionChangedSignal;
376   }
377
378   /**
379    * @copydoc Dali::Application::LowBatterySignal()
380    */
381   Dali::Application::LowBatterySignalType& LowBatterySignal()
382   {
383     return mLowBatterySignal;
384   }
385
386   /**
387    * @copydoc Dali::Application:::LowMemorySignal()
388    */
389   Dali::Application::LowMemorySignalType& LowMemorySignal()
390   {
391     return mLowMemorySignal;
392   }
393
394   /**
395    * @copydoc Dali::Application:::DeviceOrientationChangedSignalType()
396    */
397   Dali::Application::DeviceOrientationChangedSignalType& DeviceOrientationChangedSignal()
398   {
399     return mDeviceOrientationChangedSignal;
400   }
401
402   /**
403    * @copydoc Dali::Application::TaskInitSignal()
404    */
405   Dali::Application::AppSignalType& TaskInitSignal()
406   {
407     return mTaskInitSignal;
408   }
409
410   /**
411    * @copydoc Dali::Application::TaskTerminateSignal()
412    */
413   Dali::Application::AppSignalType& TaskTerminateSignal()
414   {
415     return mTaskTerminateSignal;
416   }
417
418   /**
419    * @copydoc Dali::Application::TaskAppControlSignal()
420    */
421   Dali::Application::AppControlSignalType& TaskAppControlSignal()
422   {
423     return mTaskAppControlSignal;
424   }
425
426   /**
427    * @copydoc Dali::Application::TaskLanguageChangedSignal()
428    */
429   Dali::Application::AppSignalType& TaskLanguageChangedSignal()
430   {
431     return mTaskLanguageChangedSignal;
432   }
433
434   /**
435    * @copydoc Dali::Application::TaskRegionChangedSignal()
436    */
437   Dali::Application::AppSignalType& TaskRegionChangedSignal()
438   {
439     return mTaskRegionChangedSignal;
440   }
441
442   /**
443    * @copydoc Dali::Application::TaskLowBatterySignal()
444    */
445   Dali::Application::LowBatterySignalType& TaskLowBatterySignal()
446   {
447     return mTaskLowBatterySignal;
448   }
449
450   /**
451    * @copydoc Dali::Application::TaskLowMemorySignal()
452    */
453   Dali::Application::LowMemorySignalType& TaskLowMemorySignal()
454   {
455     return mTaskLowMemorySignal;
456   }
457
458   /**
459    * @copydoc Dali::Application::TaskDeviceOrientationChangedSignal()
460    */
461   Dali::Application::DeviceOrientationChangedSignalType& TaskDeviceOrientationChangedSignal()
462   {
463     return mTaskDeviceOrientationChangedSignal;
464   }
465
466 protected:
467   /**
468    * Private Constructor
469    * @param[in]  argc               A pointer to the number of arguments
470    * @param[in]  argv               A pointer to the argument list
471    * @param[in]  stylesheet         The path to user defined theme file
472    * @param[in]  applicationType    A member of Dali::Framework::Type
473    * @param[in]  useUiThread        True if the application would create UI thread
474    * @param[in]  windowData         The WindowData
475    */
476   Application(int* argc, char** argv[], const std::string& stylesheet, Framework::Type applicationType, bool useUiThread, const WindowData& windowData);
477
478   /**
479    * Destructor
480    */
481   virtual ~Application() override;
482
483   // Undefined
484   Application(const Application&);
485   Application& operator=(Application&);
486
487   /**
488    * Creates the default window
489    */
490   void CreateWindow();
491
492   /**
493    * Creates the adaptor
494    */
495   void CreateAdaptor();
496
497   /**
498    * Creates the adaptor builder
499    */
500   void CreateAdaptorBuilder();
501
502   /**
503    * Quits from the main loop
504    */
505   void QuitFromMainLoop();
506
507   /**
508    * Changes information of preInitialized window
509    */
510   void ChangePreInitializedWindowInfo();
511
512 private:
513   AppSignalType                      mInitSignal;
514   AppSignalType                      mTerminateSignal;
515   AppSignalType                      mPauseSignal;
516   AppSignalType                      mResumeSignal;
517   AppSignalType                      mResetSignal;
518   AppControlSignalType               mAppControlSignal;
519   AppSignalType                      mLanguageChangedSignal;
520   AppSignalType                      mRegionChangedSignal;
521   LowBatterySignalType               mLowBatterySignal;
522   LowMemorySignalType                mLowMemorySignal;
523   DeviceOrientationChangedSignalType mDeviceOrientationChangedSignal;
524
525   AppSignalType                      mTaskInitSignal;
526   AppSignalType                      mTaskTerminateSignal;
527   AppControlSignalType               mTaskAppControlSignal;
528   AppSignalType                      mTaskLanguageChangedSignal;
529   AppSignalType                      mTaskRegionChangedSignal;
530   LowBatterySignalType               mTaskLowBatterySignal;
531   LowMemorySignalType                mTaskLowMemorySignal;
532   DeviceOrientationChangedSignalType mTaskDeviceOrientationChangedSignal;
533
534   std::unique_ptr<Framework>        mFramework;
535   std::unique_ptr<FrameworkFactory> mFrameworkFactory;
536
537   CommandLineOptions* mCommandLineOptions;
538
539   Dali::Internal::Adaptor::AdaptorBuilder* mAdaptorBuilder; ///< The adaptor builder
540   Dali::Adaptor*                           mAdaptor;
541   std::unique_ptr<EnvironmentOptions>      mEnvironmentOptions;
542
543   // The Main Window is that window created by the Application during initial startup
544   // (previously this was the only window)
545   Dali::Window                   mMainWindow;                       ///< Main Window instance
546   Dali::Application::WINDOW_MODE mMainWindowMode;                   ///< Window mode of the main window
547   std::string                    mMainWindowName;                   ///< Name of the main window as obtained from environment options
548   bool                           mIsMainWindowFrontBufferRendering; ///< Whether front buffer rendering of the main window is enabled
549
550   std::string      mStylesheet;
551   PositionSize     mWindowPositionSize;
552   Launchpad::State mLaunchpadState;
553   WindowType       mDefaultWindowType; ///< Default window's type. It is used when Application is created.
554   bool             mUseRemoteSurface;
555   bool             mUseUiThread;
556   bool             mIsSystemInitialized;
557
558   SlotDelegate<Application> mSlotDelegate;
559
560   UIThreadLoader*       mUIThreadLoader;
561   static ApplicationPtr gPreInitializedApplication;
562 };
563
564 inline Application& GetImplementation(Dali::Application& application)
565 {
566   DALI_ASSERT_ALWAYS(application && "application handle is empty");
567
568   BaseObject& handle = application.GetBaseObject();
569
570   return static_cast<Internal::Adaptor::Application&>(handle);
571 }
572
573 inline const Application& GetImplementation(const Dali::Application& application)
574 {
575   DALI_ASSERT_ALWAYS(application && "application handle is empty");
576
577   const BaseObject& handle = application.GetBaseObject();
578
579   return static_cast<const Internal::Adaptor::Application&>(handle);
580 }
581
582 } // namespace Adaptor
583
584 } // namespace Internal
585
586 } // namespace Dali
587
588 #endif // DALI_INTERNAL_APPLICATION_H