Merge "Add API to notify adaptor that scene has been created" into devel/master
[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 #include <app_control_internal.h>
29 #endif
30
31 #include <dali/integration-api/debug.h>
32
33 // INTERNAL INCLUDES
34 #include <callback-manager.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace Adaptor
43 {
44
45 namespace
46 {
47
48 /// Application Status Enum
49 enum
50 {
51   APP_CREATE,
52   APP_TERMINATE,
53   APP_PAUSE,
54   APP_RESUME,
55   APP_RESET,
56   APP_CONTROL,
57   APP_LANGUAGE_CHANGE,
58   APP_DEVICE_ROTATED,
59   APP_REGION_CHANGED,
60   APP_BATTERY_LOW,
61   APP_MEMORY_LOW
62 };
63
64 } // Unnamed namespace
65
66 /**
67  * Impl to hide EFL data members
68  */
69 struct Framework::Impl
70 {
71   // Constructor
72
73   Impl(void* data)
74   : mAbortCallBack( NULL ),
75     mCallbackManager( NULL )
76   {
77     mEventCallback.create = AppCreate;
78     mEventCallback.terminate = AppTerminate;
79     mEventCallback.pause = AppPause;
80     mEventCallback.resume = AppResume;
81 #ifndef OVER_TIZEN_SDK_2_2
82     mEventCallback.service = AppService;
83
84     mEventCallback.low_memory = NULL;
85     mEventCallback.low_battery = NULL;
86     mEventCallback.device_orientation = AppDeviceRotated;
87     mEventCallback.language_changed = AppLanguageChanged;
88     mEventCallback.region_format_changed = NULL;
89
90 #else
91     mEventCallback.app_control = AppControl;
92
93     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, AppBatteryLow, data);
94     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, AppMemoryLow, data);
95     ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, data);
96     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, AppLanguageChanged, data);
97     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, AppRegionChanged, data);
98
99 #endif
100
101     mCallbackManager = CallbackManager::New();
102   }
103
104   ~Impl()
105   {
106     delete mAbortCallBack;
107
108     // we're quiting the main loop so
109     // mCallbackManager->RemoveAllCallBacks() does not need to be called
110     // to delete our abort handler
111     delete mCallbackManager;
112   }
113
114   // Data
115
116   CallbackBase* mAbortCallBack;
117   CallbackManager *mCallbackManager;
118
119 #ifndef OVER_TIZEN_SDK_2_2
120   app_event_callback_s mEventCallback;
121 #else
122   ui_app_lifecycle_callback_s mEventCallback;
123   app_event_handler_h handlers[5];
124 #endif
125
126   /**
127    * Called by AppCore on application creation.
128    */
129   static bool AppCreate(void *data)
130   {
131     return static_cast<Framework*>(data)->AppStatusHandler(APP_CREATE, NULL);
132   }
133
134   /**
135    * Called by AppCore when the application should terminate.
136    */
137   static void AppTerminate(void *data)
138   {
139     static_cast<Framework*>(data)->AppStatusHandler(APP_TERMINATE, NULL);
140   }
141
142   /**
143    * Called by AppCore when the application is paused.
144    */
145   static void AppPause(void *data)
146   {
147     static_cast<Framework*>(data)->AppStatusHandler(APP_PAUSE, NULL);
148   }
149
150   /**
151    * Called by AppCore when the application is resumed.
152    */
153   static void AppResume(void *data)
154   {
155     static_cast<Framework*>(data)->AppStatusHandler(APP_RESUME, NULL);
156   }
157
158   static void ProcessBundle(Framework* framework, bundle *bundleData)
159   {
160     if(bundleData == NULL)
161     {
162       return;
163     }
164
165     // get bundle name
166     char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
167     if(bundleName != NULL)
168     {
169       framework->SetBundleName(bundleName);
170     }
171
172     // get bundle id
173     char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
174     if(bundleId != NULL)
175     {
176       framework->SetBundleId(bundleId);
177     }
178   }
179
180 #ifndef OVER_TIZEN_SDK_2_2
181   /**
182    * Called by AppCore when the application is launched from another module (e.g. homescreen).
183    * @param[in] b the bundle data which the launcher module sent
184    */
185   static void AppService(service_h service, void *data)
186   {
187     Framework* framework = static_cast<Framework*>(data);
188
189     if(framework == NULL)
190     {
191       return;
192     }
193     bundle *bundleData = NULL;
194
195     service_to_bundle(service, &bundleData);
196     ProcessBundle(framework, bundleData);
197
198     framework->AppStatusHandler(APP_RESET, NULL);
199   }
200
201   static void AppLanguageChanged(void* user_data)
202   {
203     static_cast<Framework*>(user_data)->AppStatusHandler(APP_LANGUAGE_CHANGE, NULL);
204   }
205
206   static void AppDeviceRotated(app_device_orientation_e orientation, void *user_data)
207   {
208     static_cast<Framework*>(user_data)->AppStatusHandler(APP_DEVICE_ROTATED, NULL);
209   }
210
211 #else
212
213   /**
214    * Called by AppCore when the application is launched from another module (e.g. homescreen).
215    * @param[in] b the bundle data which the launcher module sent
216    */
217   static void AppControl(app_control_h app_control, void *data)
218   {
219     Framework* framework = static_cast<Framework*>(data);
220     if(framework == NULL)
221     {
222       return;
223     }
224     bundle *bundleData = NULL;
225
226     app_control_to_bundle(app_control, &bundleData);
227     ProcessBundle(framework, bundleData);
228
229     framework->AppStatusHandler(APP_RESET, NULL);
230     framework->AppStatusHandler(APP_CONTROL, app_control);
231   }
232
233   static void AppLanguageChanged(app_event_info_h event_info, void *user_data)
234   {
235     static_cast<Framework*>(user_data)->AppStatusHandler(APP_LANGUAGE_CHANGE, NULL);
236   }
237
238   static void AppDeviceRotated(app_event_info_h event_info, void *user_data)
239   {
240     static_cast<Framework*>(user_data)->AppStatusHandler(APP_DEVICE_ROTATED, NULL);
241   }
242
243   static void AppRegionChanged(app_event_info_h event_info, void *user_data)
244   {
245     static_cast<Framework*>(user_data)->AppStatusHandler(APP_REGION_CHANGED, NULL);
246   }
247
248   static void AppBatteryLow(app_event_info_h event_info, void *user_data)
249   {
250     static_cast<Framework*>(user_data)->AppStatusHandler(APP_BATTERY_LOW, NULL);
251   }
252
253   static void AppMemoryLow(app_event_info_h event_info, void *user_data)
254   {
255     static_cast<Framework*>(user_data)->AppStatusHandler(APP_MEMORY_LOW, NULL);
256   }
257
258 #endif
259
260 };
261
262 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv )
263 : mObserver(observer),
264   mInitialised(false),
265   mRunning(false),
266   mArgc(argc),
267   mArgv(argv),
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