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