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