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