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