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