Direct Rendering
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / framework.h
1 #ifndef DALI_INTERNAL_FRAMEWORK_H
2 #define DALI_INTERNAL_FRAMEWORK_H
3
4 /*
5  * Copyright (c) 2021 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/signals/callback.h>
23 #include <string>
24 #ifdef APPCORE_WATCH_AVAILABLE
25 #include <dali/public-api/watch/watch-application.h>
26 #endif
27
28 // INTERNAL INCLUDES
29 #include <dali/internal/system/common/abort-handler.h>
30 #include <dali/public-api/adaptor-framework/device-status.h>
31 #ifdef COMPONENT_APPLICATION_SUPPORT
32 #include <dali/devel-api/adaptor-framework/component-application.h>
33 #endif
34
35 namespace Dali
36 {
37 namespace Internal
38 {
39 namespace Adaptor
40 {
41 /**
42  * The Framework class is ideally placed to provide key API required by Applications.
43  *
44  * The class is also used to register callbacks with the TIZEN platform so that
45  * we know when any of the application lifecycle events occur.  This includes events
46  * like when our application is to be initialised, terminated, paused, resumed etc.
47  *
48  */
49 class Framework
50 {
51 public:
52   enum Type
53   {
54     NORMAL,   ///< normal appFramework
55     WATCH,    ///< watch appFramework
56     WIDGET,   ///< widget appFramework
57     COMPONENT ///< component appFramework
58   };
59
60   /**
61    * Observer class for the framework.
62    */
63   class Observer
64   {
65   public:
66     /**
67      * Invoked when the application is to be initialised.
68      */
69     virtual void OnInit()
70     {
71     }
72
73     /**
74      * Invoked when the application is to be terminated.
75      */
76     virtual void OnTerminate()
77     {
78     }
79
80     /**
81      * Invoked when the application is to be paused.
82      */
83     virtual void OnPause()
84     {
85     }
86
87     /**
88      * Invoked when the application is to be resumed.
89      */
90     virtual void OnResume()
91     {
92     }
93
94     /**
95      * Invoked when the application is to be reset.
96      */
97     virtual void OnReset()
98     {
99     }
100
101     /**
102     * Invoked when the AppControl message is received.
103     * @param[in] The bundle data of AppControl message.
104     */
105     virtual void OnAppControl(void*)
106     {
107     }
108
109 #ifdef APPCORE_WATCH_AVAILABLE
110     /**
111      * Invoked at every second
112      */
113     virtual void OnTimeTick(WatchTime&)
114     {
115     }
116
117     /**
118      * Invoked at every second in ambient mode
119      */
120     virtual void OnAmbientTick(WatchTime&)
121     {
122     }
123
124     /**
125      * Invoked when the device enters or exits ambient mode
126      */
127     virtual void OnAmbientChanged(bool ambient)
128     {
129     }
130 #endif
131
132     /**
133      * Invoked when the language of the device is changed.
134      */
135     virtual void OnLanguageChanged()
136     {
137     }
138
139     /**
140      * Invoked when the region is changed.
141      */
142     virtual void OnRegionChanged()
143     {
144     }
145
146     /**
147      * Invoked when the battery level of the device is low.
148      */
149     virtual void OnBatteryLow(Dali::DeviceStatus::Battery::Status status)
150     {
151     }
152
153     /**
154      * Invoked when the memory level of the device is low.
155      */
156     virtual void OnMemoryLow(Dali::DeviceStatus::Memory::Status status)
157     {
158     }
159
160     /**
161      * Invoked when the platform surface is created.
162      */
163     virtual void OnSurfaceCreated(Any newSurface)
164     {
165     }
166
167     /**
168      * Invoked when the platform surface is destroyed.
169      */
170     virtual void OnSurfaceDestroyed(Any newSurface)
171     {
172     }
173
174 #ifdef COMPONENT_APPLICATION_SUPPORT
175     /**
176      * Invoked when the component application is created.
177      */
178     virtual Any OnCreate()
179     {
180       return nullptr;
181     }
182 #endif
183   };
184
185 public:
186   /**
187    * Constructor
188    * @param[in]  observer  The observer of the Framework.
189    * @param[in]  argc      A pointer to the number of arguments.
190    * @param[in]  argv      A pointer the the argument list.
191    * @param[in]  type      The type of application
192    */
193   Framework(Observer& observer, int* argc, char*** argv, Type type = NORMAL);
194
195   /**
196    * Destructor
197    */
198   ~Framework();
199
200 public:
201   /**
202    * Runs the main loop of framework
203    */
204   void Run();
205
206   /**
207    * Quits the main loop
208    */
209   void Quit();
210
211   /**
212    * Checks whether the main loop of the framework is running.
213    * @return true, if the main loop is running, false otherwise.
214    */
215   bool IsMainLoopRunning();
216
217   /**
218    * If the main loop aborts unexpectedly, then the connected callback function is called.
219    * @param[in]  callBack  The function to call.
220    * @note Only one callback can be registered.  The last callback to be set will be called on abort.
221    * @note The ownership of callback is passed onto this class.
222    */
223   void AddAbortCallback(CallbackBase* callback);
224
225   /**
226    * Gets bundle name which was passed in app_reset callback.
227    */
228   std::string GetBundleName() const;
229
230   /**
231    * Gets bundle id which was passed in app_reset callback.
232    */
233   std::string GetBundleId() const;
234
235   /**
236    * Sets a command line options.
237    * This is used in case of the preinitialized application.
238    * @param[in] argc A pointer to the number of arguments
239    * @param[in] argv A pointer to the argument list
240    */
241   void SetCommandLineOptions(int* argc, char** argv[])
242   {
243     mArgc = argc;
244     mArgv = argv;
245   }
246
247   /**
248    *  Gets the path at which application resources are stored.
249    */
250   static std::string GetResourcePath();
251
252   /**
253    *  Gets the path at which application data are stored.
254    */
255   static std::string GetDataPath();
256
257   /**
258    * Sets system language.
259    */
260   void SetLanguage(const std::string& language);
261
262   /**
263    * Sets system region.
264    */
265   void SetRegion(const std::string& region);
266
267   /**
268    * Gets system language.
269    */
270   std::string GetLanguage() const;
271
272   /**
273    * Gets system region.
274    */
275   std::string GetRegion() const;
276
277   /**
278    * Called by the App framework when an application lifecycle event occurs.
279    * @param[in] type The type of event occurred.
280    * @param[in] data The data of event occurred.
281    */
282   bool AppStatusHandler(int type, void* data);
283
284   /**
285    * Called by the adaptor when an idle callback is added.
286    * @param[in] timeout The timeout of the callback.
287    * @param[in] data The data of of the callback.
288    * @param[in] callback The callback.
289    * @return The callback id.
290    */
291   unsigned int AddIdle(int timeout, void* data, bool (*callback)(void* data));
292
293   /**
294    * Called by the adaptor when an idle callback is removed.
295    * @param[in] id The callback id.
296    */
297   void RemoveIdle(unsigned int id);
298
299 private:
300   // Undefined
301   Framework(const Framework&);
302   Framework& operator=(Framework&);
303
304 private:
305   /**
306    * Called when the application is created.
307    */
308   bool Create();
309
310   /**
311    * Called app_reset callback was called with bundle.
312    */
313   void SetBundleName(const std::string& name);
314
315   /**
316    * Called app_reset callback was called with bundle.
317    */
318   void SetBundleId(const std::string& id);
319
320   /**
321    * Called if the application is aborted.
322    */
323   void AbortCallback();
324
325   /**
326    * Called for initializing on specified backend. (X11 or Wayland)
327    */
328   void InitThreads();
329
330 private:
331   Observer&    mObserver;
332   bool         mInitialised;
333   bool         mPaused;
334   bool         mRunning;
335   int*         mArgc;
336   char***      mArgv;
337   std::string  mBundleName;
338   std::string  mBundleId;
339   AbortHandler mAbortHandler;
340
341 private: // impl members
342   struct Impl;
343   Impl* mImpl;
344 };
345
346 } // namespace Adaptor
347
348 } // namespace Internal
349
350 } // namespace Dali
351
352 #endif // DALI_INTERNAL_FRAMEWORK_H