Change to use new AppFW APi for TV profile
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / framework-tizen.cpp
1 /*
2  * Copyright (c) 2014 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 "framework.h"
20
21 // EXTERNAL INCLUDES
22 #include <app.h>
23 #include <bundle.h>
24 #include <Ecore.h>
25
26 #include <dali/integration-api/debug.h>
27
28 // INTERNAL INCLUDES
29 #include <callback-manager.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42
43 /// Application Status Enum
44 enum
45 {
46   APP_CREATE,
47   APP_TERMINATE,
48   APP_PAUSE,
49   APP_RESUME,
50   APP_RESET,
51   APP_CONTROL,
52   APP_LANGUAGE_CHANGE,
53   APP_DEVICE_ROTATED,
54   APP_REGION_CHANGED,
55   APP_BATTERY_LOW,
56   APP_MEMORY_LOW
57 };
58
59 } // Unnamed namespace
60
61 /**
62  * Impl to hide EFL data members
63  */
64 struct Framework::Impl
65 {
66   // Constructor
67
68   Impl(void* data)
69   : mAbortCallBack( NULL ),
70     mCallbackManager( NULL )
71   {
72     mEventCallback.create = AppCreate;
73     mEventCallback.terminate = AppTerminate;
74     mEventCallback.pause = AppPause;
75     mEventCallback.resume = AppResume;
76 #ifndef OVER_TIZEN_SDK_2_2
77     mEventCallback.service = AppService;
78
79     mEventCallback.low_memory = NULL;
80     mEventCallback.low_battery = NULL;
81     mEventCallback.device_orientation = AppDeviceRotated;
82     mEventCallback.language_changed = AppLanguageChanged;
83     mEventCallback.region_format_changed = NULL;
84
85 #else
86     mEventCallback.app_control = AppControl;
87
88     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, AppBatteryLow, data);
89     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, AppMemoryLow, data);
90     ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, data);
91     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, AppLanguageChanged, data);
92     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, AppRegionChanged, data);
93
94 #endif
95
96     mCallbackManager = CallbackManager::New();
97   }
98
99   ~Impl()
100   {
101     delete mAbortCallBack;
102
103     // we're quiting the main loop so
104     // mCallbackManager->RemoveAllCallBacks() does not need to be called
105     // to delete our abort handler
106     delete mCallbackManager;
107   }
108
109   // Data
110
111   CallbackBase* mAbortCallBack;
112   CallbackManager *mCallbackManager;
113
114 #ifndef OVER_TIZEN_SDK_2_2
115   app_event_callback_s mEventCallback;
116 #else
117   ui_app_lifecycle_callback_s mEventCallback;
118   app_event_handler_h handlers[5];
119 #endif
120
121   /**
122    * Called by AppCore on application creation.
123    */
124   static bool AppCreate(void *data)
125   {
126     return static_cast<Framework*>(data)->AppStatusHandler(APP_CREATE, NULL);
127   }
128
129   /**
130    * Called by AppCore when the application should terminate.
131    */
132   static void AppTerminate(void *data)
133   {
134     static_cast<Framework*>(data)->AppStatusHandler(APP_TERMINATE, NULL);
135   }
136
137   /**
138    * Called by AppCore when the application is paused.
139    */
140   static void AppPause(void *data)
141   {
142     static_cast<Framework*>(data)->AppStatusHandler(APP_PAUSE, NULL);
143   }
144
145   /**
146    * Called by AppCore when the application is resumed.
147    */
148   static void AppResume(void *data)
149   {
150     static_cast<Framework*>(data)->AppStatusHandler(APP_RESUME, NULL);
151   }
152
153   static void ProcessBundle(Framework* framework, bundle *bundleData)
154   {
155     if(bundleData == NULL)
156     {
157       return;
158     }
159
160     // get bundle name
161     char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
162     if(bundleName != NULL)
163     {
164       framework->SetBundleName(bundleName);
165     }
166
167     // get bundle id
168     char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
169     if(bundleId != NULL)
170     {
171       framework->SetBundleId(bundleId);
172     }
173   }
174
175 #ifndef OVER_TIZEN_SDK_2_2
176   /**
177    * Called by AppCore when the application is launched from another module (e.g. homescreen).
178    * @param[in] b the bundle data which the launcher module sent
179    */
180   static void AppService(service_h service, void *data)
181   {
182     Framework* framework = static_cast<Framework*>(data);
183
184     if(framework == NULL)
185     {
186       return;
187     }
188     bundle *bundleData = NULL;
189
190     service_to_bundle(service, &bundleData);
191     ProcessBundle(framework, bundleData);
192
193     framework->AppStatusHandler(APP_RESET, NULL);
194   }
195
196   static void AppLanguageChanged(void* user_data)
197   {
198     static_cast<Framework*>(user_data)->AppStatusHandler(APP_LANGUAGE_CHANGE, NULL);
199   }
200
201   static void AppDeviceRotated(app_device_orientation_e orientation, void *user_data)
202   {
203     static_cast<Framework*>(user_data)->AppStatusHandler(APP_DEVICE_ROTATED, NULL);
204   }
205
206 #else
207
208   /**
209    * Called by AppCore when the application is launched from another module (e.g. homescreen).
210    * @param[in] b the bundle data which the launcher module sent
211    */
212   static void AppControl(app_control_h app_control, void *data)
213   {
214     Framework* framework = static_cast<Framework*>(data);
215     if(framework == NULL)
216     {
217       return;
218     }
219     bundle *bundleData = NULL;
220
221     app_control_to_bundle(app_control, &bundleData);
222     ProcessBundle(framework, bundleData);
223
224     framework->AppStatusHandler(APP_RESET, NULL);
225     framework->AppStatusHandler(APP_CONTROL, app_control);
226   }
227
228   static void AppLanguageChanged(app_event_info_h event_info, void *user_data)
229   {
230     static_cast<Framework*>(user_data)->AppStatusHandler(APP_LANGUAGE_CHANGE, NULL);
231   }
232
233   static void AppDeviceRotated(app_event_info_h event_info, void *user_data)
234   {
235     static_cast<Framework*>(user_data)->AppStatusHandler(APP_DEVICE_ROTATED, NULL);
236   }
237
238   static void AppRegionChanged(app_event_info_h event_info, void *user_data)
239   {
240     static_cast<Framework*>(user_data)->AppStatusHandler(APP_REGION_CHANGED, NULL);
241   }
242
243   static void AppBatteryLow(app_event_info_h event_info, void *user_data)
244   {
245     static_cast<Framework*>(user_data)->AppStatusHandler(APP_BATTERY_LOW, NULL);
246   }
247
248   static void AppMemoryLow(app_event_info_h event_info, void *user_data)
249   {
250     static_cast<Framework*>(user_data)->AppStatusHandler(APP_MEMORY_LOW, NULL);
251   }
252
253 #endif
254
255 };
256
257 Framework::Framework(Framework::Observer& observer, int *argc, char ***argv, const std::string& name)
258 : mObserver(observer),
259   mInitialised(false),
260   mRunning(false),
261   mArgc(argc),
262   mArgv(argv),
263   mName(name),
264   mBundleName(""),
265   mBundleId(""),
266   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
267   mImpl(NULL)
268 {
269   InitThreads();
270   mImpl = new Impl(this);
271 }
272
273 Framework::~Framework()
274 {
275   if (mRunning)
276   {
277     Quit();
278   }
279
280   delete mImpl;
281 }
282
283 void Framework::Run()
284 {
285   mRunning = true;
286
287 #ifndef OVER_TIZEN_SDK_2_2
288   app_efl_main(mArgc, mArgv, &mImpl->mEventCallback, this);
289
290 #else
291   int ret = ui_app_main(*mArgc, *mArgv, &mImpl->mEventCallback, this);
292   if (ret != APP_ERROR_NONE)
293   {
294     DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d", ret);
295   }
296 #endif
297
298   mRunning = false;
299 }
300
301 void Framework::Quit()
302 {
303   app_efl_exit();
304 }
305
306 bool Framework::IsMainLoopRunning()
307 {
308   return mRunning;
309 }
310
311 void Framework::AddAbortCallback( CallbackBase* callback )
312 {
313   mImpl->mAbortCallBack = callback;
314 }
315
316 std::string Framework::GetBundleName() const
317 {
318   return mBundleName;
319 }
320
321 void Framework::SetBundleName(const std::string& name)
322 {
323   mBundleName = name;
324 }
325
326 std::string Framework::GetBundleId() const
327 {
328   return mBundleId;
329 }
330
331 void Framework::SetBundleId(const std::string& id)
332 {
333   mBundleId = id;
334 }
335
336 void Framework::AbortCallback( )
337 {
338   // if an abort call back has been installed run it.
339   if (mImpl->mAbortCallBack)
340   {
341     CallbackBase::Execute( *mImpl->mAbortCallBack );
342   }
343   else
344   {
345     Quit();
346   }
347 }
348
349 bool Framework::AppStatusHandler(int type, void *bundleData)
350 {
351   switch (type)
352   {
353     case APP_CREATE:
354     {
355       mInitialised = true;
356
357       // Connect to abnormal exit signals
358       mAbortHandler.AbortOnSignal( SIGINT );
359       mAbortHandler.AbortOnSignal( SIGQUIT );
360       mAbortHandler.AbortOnSignal( SIGKILL );
361
362       mObserver.OnInit();
363       break;
364     }
365
366     case APP_RESET:
367     {
368       mObserver.OnReset();
369       break;
370     }
371
372     case APP_RESUME:
373     {
374       mObserver.OnResume();
375       break;
376     }
377
378     case APP_TERMINATE:
379     {
380       mObserver.OnTerminate();
381       break;
382     }
383
384     case APP_PAUSE:
385     {
386       mObserver.OnPause();
387       break;
388     }
389
390     case APP_CONTROL:
391     {
392       mObserver.OnAppControl(bundleData);
393       break;
394     }
395
396     case APP_LANGUAGE_CHANGE:
397     {
398       mObserver.OnLanguageChanged();
399       break;
400     }
401
402     case APP_REGION_CHANGED:
403     {
404       mObserver.OnRegionChanged();
405       break;
406     }
407
408     case APP_BATTERY_LOW:
409     {
410       mObserver.OnBatteryLow();
411       break;
412     }
413
414     case APP_MEMORY_LOW:
415     {
416       mObserver.OnMemoryLow();
417       break;
418     }
419
420     default:
421       break;
422   }
423
424   return true;
425 }
426
427 } // namespace Adaptor
428
429 } // namespace Internal
430
431 } // namespace Dali