Revert "[4.0] Create Widget Application"
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / framework-tizen.cpp
1 /*
2  * Copyright (c) 2017 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 <appcore_ui_base.h>
23 #include <app_control_internal.h>
24 #include <app_common.h>
25 #include <bundle.h>
26 #include <Ecore.h>
27
28 #include <system_info.h>
29 #include <bundle_internal.h>
30
31 // CONDITIONAL INCLUDES
32 #ifdef APPCORE_WATCH_AVAILABLE
33 #include <appcore-watch/watch_app.h>
34 #endif
35 #ifdef DALI_ELDBUS_AVAILABLE
36 #include <Eldbus.h>
37 #endif // DALI_ELDBUS_AVAILABLE
38
39 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
40 #include <tzplatform_config.h>
41 #endif // TIZEN_PLATFORM_CONFIG_SUPPORTED
42
43 #include <dali/integration-api/debug.h>
44
45 // INTERNAL INCLUDES
46 #include <callback-manager.h>
47
48 namespace Dali
49 {
50
51 namespace Internal
52 {
53
54 namespace Adaptor
55 {
56
57 #if defined(DEBUG_ENABLED)
58 namespace
59 {
60 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS" );
61 } // anonymous namespace
62 #endif
63
64 namespace AppCore
65 {
66
67 typedef enum
68 {
69   LOW_MEMORY,                 //< The low memory event
70   LOW_BATTERY,                //< The low battery event
71   LANGUAGE_CHANGED,           //< The system language changed event
72   DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
73   REGION_FORMAT_CHANGED,      //< The region format changed event
74   SUSPENDED_STATE_CHANGED,    //< The suspended state changed event of the application
75   UPDATE_REQUESTED,           //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
76 } AppEventType;
77
78 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
79 {
80   [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
81   [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
82   [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
83   [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
84   [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
85   [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
86 };
87
88 struct AppEventInfo
89 {
90   AppEventType type;
91   void *value;
92 };
93
94 typedef struct AppEventInfo *AppEventInfoPtr;
95
96 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void *userData);
97
98 struct AppEventHandler
99 {
100   AppEventType type;
101   AppEventCallback cb;
102   void *data;
103   void *raw;
104 };
105
106 typedef struct AppEventHandler *AppEventHandlerPtr;
107
108 int EventCallback(void *event, void *data)
109 {
110   AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
111
112   struct AppEventInfo appEvent;
113
114   appEvent.type = handler->type;
115   appEvent.value = event;
116
117   if (handler->cb)
118     handler->cb(&appEvent, handler->data);
119
120   return 0;
121 }
122
123 int AppAddEventHandler(AppEventHandlerPtr *eventHandler, AppEventType eventType, AppEventCallback callback, void *userData)
124 {
125   AppEventHandlerPtr handler;
126
127   handler = static_cast<AppEventHandlerPtr>( calloc(1, sizeof(struct AppEventHandler)) );
128   if (!handler)
129   {
130     DALI_LOG_ERROR( "failed to create handler" );
131     return TIZEN_ERROR_UNKNOWN;
132   }
133   else
134   {
135     handler->type = eventType;
136     handler->cb = callback;
137     handler->data = userData;
138     handler->raw = appcore_base_add_event( static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
139
140     *eventHandler = handler;
141
142     return TIZEN_ERROR_NONE;
143   }
144 }
145
146 } // namespace Appcore
147
148 /**
149  * Impl to hide EFL data members
150  */
151 struct Framework::Impl
152 {
153 // Constructor
154   Impl(void* data, Type type )
155   : mAbortCallBack( NULL ),
156     mCallbackManager( NULL )
157 #ifdef APPCORE_WATCH_AVAILABLE
158     , mWatchCallback()
159 #endif
160   {
161     mFramework = static_cast<Framework*>(data);
162
163 #ifndef APPCORE_WATCH_AVAILABLE
164     if ( type == WATCH )
165     {
166       throw Dali::DaliException( "", "Watch Application is not supported." );
167     }
168 #endif
169     mApplicationType = type;
170     mCallbackManager = CallbackManager::New();
171   }
172
173   ~Impl()
174   {
175     delete mAbortCallBack;
176
177     // we're quiting the main loop so
178     // mCallbackManager->RemoveAllCallBacks() does not need to be called
179     // to delete our abort handler
180     delete mCallbackManager;
181   }
182
183   int AppMain()
184   {
185     int ret;
186
187     if (mApplicationType == NORMAL)
188     {
189       ret = AppNormalMain();
190     }
191     else
192     {
193       ret = AppWatchMain();
194     }
195     return ret;
196   }
197
198   void AppExit()
199   {
200     if (mApplicationType == NORMAL)
201     {
202       AppNormalExit();
203     }
204     else
205     {
206       AppWatchExit();
207     }
208   }
209
210
211   // Data
212   Type mApplicationType;
213   CallbackBase* mAbortCallBack;
214   CallbackManager *mCallbackManager;
215
216   Framework* mFramework;
217   AppCore::AppEventHandlerPtr handlers[5];
218 #ifdef APPCORE_WATCH_AVAILABLE
219   watch_app_lifecycle_callback_s mWatchCallback;
220   app_event_handler_h watchHandlers[5];
221 #endif
222
223   static int AppCreate(void *data)
224   {
225     appcore_ui_base_on_create();
226     return static_cast<int>( static_cast<Framework*>(data)->Create() );
227   }
228
229   static int AppTerminate(void *data)
230   {
231     appcore_ui_base_on_terminate();
232     Observer *observer = &static_cast<Framework*>(data)->mObserver;
233
234     observer->OnTerminate();
235
236     return 0;
237   }
238
239   static int AppPause(void *data)
240   {
241     appcore_ui_base_on_pause();
242     Observer *observer = &static_cast<Framework*>(data)->mObserver;
243
244     observer->OnPause();
245
246     return 0;
247   }
248
249   static int AppResume(void *data)
250   {
251     appcore_ui_base_on_resume();
252     Observer *observer = &static_cast<Framework*>(data)->mObserver;
253
254     observer->OnResume();
255
256     return 0;
257   }
258
259   static void ProcessBundle(Framework* framework, bundle *bundleData)
260   {
261     if(bundleData == NULL)
262     {
263       return;
264     }
265
266     // get bundle name
267     char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
268     if(bundleName != NULL)
269     {
270       framework->SetBundleName(bundleName);
271     }
272
273     // get bundle? id
274     char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
275     if(bundleId != NULL)
276     {
277       framework->SetBundleId(bundleId);
278     }
279   }
280
281   /**
282    * Called by AppCore when the application is launched from another module (e.g. homescreen).
283    * @param[in] b the bundle data which the launcher module sent
284    */
285   static int AppControl(bundle* bundleData, void *data)
286   {
287     app_control_h appControl = NULL;
288
289     appcore_ui_base_on_control(bundleData);
290
291     if (bundleData)
292     {
293       if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
294       {
295         DALI_LOG_ERROR("Failed to create an app_control handle");
296       }
297     }
298     else
299     {
300       if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
301       {
302         DALI_LOG_ERROR("Failed to create an app_control handle");
303       }
304     }
305
306     Framework* framework = static_cast<Framework*>(data);
307     Observer *observer = &framework->mObserver;
308
309     ProcessBundle(framework, bundleData);
310
311     observer->OnReset();
312     observer->OnAppControl(appControl);
313
314     app_control_destroy(appControl);
315
316     return 0;
317   }
318
319   static void AppInit(int argc, char **argv, void *data)
320   {
321 #pragma GCC diagnostic push
322 #pragma GCC diagnostic ignored "-Wold-style-cast"
323
324     ecore_init();
325     ecore_app_args_set( argc, (const char **)argv );
326
327 #pragma GCC diagnostic pop
328   }
329
330   static void AppFinish(void)
331   {
332     ecore_shutdown();
333
334     if(getenv("AUL_LOADER_INIT"))
335     {
336       unsetenv("AUL_LOADER_INIT");
337       ecore_shutdown();
338     }
339   }
340
341   static void AppRun(void *data)
342   {
343     ecore_main_loop_begin();
344   }
345
346   static void AppExit(void *data)
347   {
348     ecore_main_loop_quit();
349   }
350
351   static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
352   {
353     Observer *observer = &static_cast<Framework*>(data)->mObserver;
354
355     observer->OnLanguageChanged();
356   }
357
358   static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
359   {
360   }
361
362   static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
363   {
364     Observer *observer = &static_cast<Framework*>(data)->mObserver;
365
366     observer->OnRegionChanged();
367   }
368
369   static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
370   {
371     Observer *observer = &static_cast<Framework*>(data)->mObserver;
372
373     observer->OnBatteryLow();
374   }
375
376   static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
377   {
378     Observer *observer = &static_cast<Framework*>(data)->mObserver;
379
380     observer->OnMemoryLow();
381   }
382
383
384   int AppNormalMain()
385   {
386     int ret;
387
388     AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
389     AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
390     AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
391     AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
392     AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
393
394     appcore_ui_base_ops ops = appcore_ui_base_get_default_ops();
395
396     /* override methods */
397     ops.base.create = AppCreate;
398     ops.base.control = AppControl;
399     ops.base.terminate = AppTerminate;
400     ops.pause = AppPause;
401     ops.resume = AppResume;
402     ops.base.init = AppInit;
403     ops.base.finish = AppFinish;
404     ops.base.run = AppRun;
405     ops.base.exit = AppExit;
406
407     ret = appcore_ui_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework, APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL |
408                                                                                         APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL |
409                                                                                         APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL |
410                                                                                         APPCORE_UI_BASE_HINT_HW_ACC_CONTROL |
411                                                                                         APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL );
412
413     if (ret != TIZEN_ERROR_NONE)
414       return ret;
415
416     appcore_ui_base_fini();
417
418     return TIZEN_ERROR_NONE;
419   }
420
421   void AppNormalExit()
422   {
423     appcore_ui_base_exit();
424   }
425
426 #ifdef APPCORE_WATCH_AVAILABLE
427   static bool WatchAppCreate(int width, int height, void *data)
428   {
429     return static_cast<Framework*>(data)->Create();
430   }
431
432   static void WatchAppTimeTick(watch_time_h time, void *data)
433   {
434     Observer *observer = &static_cast<Framework*>(data)->mObserver;
435     WatchTime curTime(time);
436
437     observer->OnTimeTick(curTime);
438   }
439
440   static void WatchAppAmbientTick(watch_time_h time, void *data)
441   {
442     Observer *observer = &static_cast<Framework*>(data)->mObserver;
443     WatchTime curTime(time);
444
445     observer->OnAmbientTick(curTime);
446   }
447
448   static void WatchAppAmbientChanged(bool ambient, void *data)
449   {
450     Observer *observer = &static_cast<Framework*>(data)->mObserver;
451
452     observer->OnAmbientChanged(ambient);
453   }
454
455   static void WatchAppLanguageChanged(app_event_info_h event, void *data)
456   {
457     Observer *observer = &static_cast<Framework*>(data)->mObserver;
458
459     observer->OnLanguageChanged();
460   }
461
462   static void WatchAppRegionChanged(app_event_info_h event, void *data)
463   {
464     Observer *observer = &static_cast<Framework*>(data)->mObserver;
465
466     observer->OnRegionChanged();
467   }
468
469   static void WatchAppBatteryLow(app_event_info_h event, void *data)
470   {
471     Observer *observer = &static_cast<Framework*>(data)->mObserver;
472
473     observer->OnBatteryLow();
474   }
475
476   static void WatchAppMemoryLow(app_event_info_h event, void *data)
477   {
478     Observer *observer = &static_cast<Framework*>(data)->mObserver;
479
480     observer->OnMemoryLow();
481   }
482
483   static void WatchAppControl(app_control_h app_control, void *data)
484   {
485     Framework* framework = static_cast<Framework*>(data);
486     Observer *observer = &framework->mObserver;
487     bundle *bundleData = NULL;
488
489     app_control_to_bundle(app_control, &bundleData);
490     ProcessBundle(framework, bundleData);
491
492     observer->OnReset();
493     observer->OnAppControl(app_control);
494   }
495
496   static void WatchAppTerminate(void *data)
497   {
498     Observer *observer = &static_cast<Framework*>(data)->mObserver;
499
500     observer->OnTerminate();
501   }
502
503   static void WatchAppPause(void *data)
504   {
505     Observer *observer = &static_cast<Framework*>(data)->mObserver;
506
507     observer->OnPause();
508   }
509
510   static void WatchAppResume(void *data)
511   {
512     Observer *observer = &static_cast<Framework*>(data)->mObserver;
513
514     observer->OnResume();
515   }
516
517 #endif
518
519   int AppWatchMain()
520   {
521     int ret = true;
522
523 #ifdef APPCORE_WATCH_AVAILABLE
524     mWatchCallback.create = WatchAppCreate;
525     mWatchCallback.app_control = WatchAppControl;
526     mWatchCallback.terminate = WatchAppTerminate;
527     mWatchCallback.pause = WatchAppPause;
528     mWatchCallback.resume = WatchAppResume;
529     mWatchCallback.time_tick = WatchAppTimeTick;
530     mWatchCallback.ambient_tick = WatchAppAmbientTick;
531     mWatchCallback.ambient_changed = WatchAppAmbientChanged;
532
533     watch_app_add_event_handler(&watchHandlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, WatchAppBatteryLow, mFramework);
534     watch_app_add_event_handler(&watchHandlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, WatchAppMemoryLow, mFramework);
535     watch_app_add_event_handler(&watchHandlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, WatchAppLanguageChanged, mFramework);
536     watch_app_add_event_handler(&watchHandlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, WatchAppRegionChanged, mFramework);
537
538     ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
539 #endif
540     return ret;
541   }
542
543   void AppWatchExit()
544   {
545 #ifdef APPCORE_WATCH_AVAILABLE
546     watch_app_exit();
547 #endif
548   }
549
550
551 private:
552   // Undefined
553   Impl( const Impl& impl );
554
555   // Undefined
556   Impl& operator=( const Impl& impl );
557 };
558
559 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
560 : mObserver(observer),
561   mInitialised(false),
562   mRunning(false),
563   mArgc(argc),
564   mArgv(argv),
565   mBundleName(""),
566   mBundleId(""),
567   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
568   mImpl(NULL)
569 {
570   bool featureFlag = true;
571   system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
572
573   if( featureFlag == false )
574   {
575     set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
576   }
577 #ifdef DALI_ELDBUS_AVAILABLE
578   // Initialize ElDBus.
579   DALI_LOG_INFO( gDBusLogging, Debug::General, "Starting DBus Initialization\n" );
580   eldbus_init();
581 #endif
582   InitThreads();
583
584   mImpl = new Impl(this, type);
585 }
586
587 Framework::~Framework()
588 {
589   if (mRunning)
590   {
591     Quit();
592   }
593
594 #ifdef DALI_ELDBUS_AVAILABLE
595   // Shutdown ELDBus.
596   DALI_LOG_INFO( gDBusLogging, Debug::General, "Shutting down DBus\n" );
597   eldbus_shutdown();
598 #endif
599
600   delete mImpl;
601 }
602
603 bool Framework::Create()
604 {
605   mInitialised = true;
606   mObserver.OnInit();
607   return true;
608 }
609
610 void Framework::Run()
611 {
612   mRunning = true;
613   int ret;
614
615   ret = mImpl->AppMain();
616   if (ret != APP_ERROR_NONE)
617   {
618     DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
619   }
620   mRunning = false;
621 }
622
623 void Framework::Quit()
624 {
625   mImpl->AppExit();
626 }
627
628 bool Framework::IsMainLoopRunning()
629 {
630   return mRunning;
631 }
632
633 void Framework::AddAbortCallback( CallbackBase* callback )
634 {
635   mImpl->mAbortCallBack = callback;
636 }
637
638 std::string Framework::GetBundleName() const
639 {
640   return mBundleName;
641 }
642
643 void Framework::SetBundleName(const std::string& name)
644 {
645   mBundleName = name;
646 }
647
648 std::string Framework::GetBundleId() const
649 {
650   return mBundleId;
651 }
652
653 std::string Framework::GetResourcePath()
654 {
655   std::string resourcePath = "";
656 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
657   resourcePath = app_get_resource_path();
658 #else // For backwards compatibility with older Tizen versions
659
660   // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
661   const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
662   char* value = getenv( environmentVariable );
663   if ( value != NULL )
664   {
665     resourcePath = value;
666   }
667 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
668
669   return resourcePath;
670 }
671
672 void Framework::SetBundleId(const std::string& id)
673 {
674   mBundleId = id;
675 }
676
677 void Framework::AbortCallback( )
678 {
679   // if an abort call back has been installed run it.
680   if (mImpl->mAbortCallBack)
681   {
682     CallbackBase::Execute( *mImpl->mAbortCallBack );
683   }
684   else
685   {
686     Quit();
687   }
688 }
689
690 } // namespace Adaptor
691
692 } // namespace Internal
693
694 } // namespace Dali