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