7ff0c3292dd0e704846a52196f197b3c8c7ce72a
[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 <aul.h>
23 #include <aul_app_com.h>
24 #include <appcore_ui_base.h>
25 #include <appcore_multiwindow_base.h>
26 #include <app_control_internal.h>
27 #include <app_common.h>
28 #include <bundle.h>
29 #include <bundle_internal.h>
30 #include <Ecore.h>
31 #include <screen_connector_provider.h>
32 #include <system_info.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <vconf.h>
36 #include <vconf-internal-keys.h>
37
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wold-style-cast"
40 #include <glib.h>
41 #pragma GCC diagnostic pop
42
43 // CONDITIONAL INCLUDES
44 #ifdef APPCORE_WATCH_AVAILABLE
45 #include <appcore-watch/watch_app.h>
46 #endif
47 #ifdef DALI_ELDBUS_AVAILABLE
48 #include <Eldbus.h>
49 #endif // DALI_ELDBUS_AVAILABLE
50
51 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
52 #include <tzplatform_config.h>
53 #endif // TIZEN_PLATFORM_CONFIG_SUPPORTED
54
55 #include <dali/integration-api/debug.h>
56
57 // INTERNAL INCLUDES
58 #include <callback-manager.h>
59 #include <window.h>
60 #include <widget-impl.h>
61 #include <widget-data.h>
62
63 namespace Dali
64 {
65
66 namespace Internal
67 {
68
69 namespace Adaptor
70 {
71
72 namespace
73 {
74 #if defined(DEBUG_ENABLED)
75 Integration::Log::Filter* gDBusLogging = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DBUS" );
76 #endif
77
78 // TODO: remove these global variables
79 static bool gForegroundState;
80 static char* gAppId;
81 static char* gPackageId;
82 static char* gViewerEndpoint;
83
84 } // anonymous namespace
85
86 namespace AppCore
87 {
88
89 typedef enum
90 {
91   LOW_MEMORY,                 //< The low memory event
92   LOW_BATTERY,                //< The low battery event
93   LANGUAGE_CHANGED,           //< The system language changed event
94   DEVICE_ORIENTATION_CHANGED, //< The device orientation changed event
95   REGION_FORMAT_CHANGED,      //< The region format changed event
96   SUSPENDED_STATE_CHANGED,    //< The suspended state changed event of the application
97   UPDATE_REQUESTED,           //< The update requested event. This event can occur when an app needs to be updated. It is dependent on target devices.
98 } AppEventType;
99
100 static int AppEventConverter[APPCORE_BASE_EVENT_MAX] =
101 {
102   [LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
103   [LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
104   [LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
105   [DEVICE_ORIENTATION_CHANGED] = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
106   [REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
107   [SUSPENDED_STATE_CHANGED] = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
108 };
109
110 struct AppEventInfo
111 {
112   AppEventType type;
113   void *value;
114 };
115
116 typedef struct AppEventInfo *AppEventInfoPtr;
117
118 typedef void (*AppEventCallback)(AppEventInfoPtr eventInfo, void *userData);
119
120 struct AppEventHandler
121 {
122   AppEventType type;
123   AppEventCallback cb;
124   void *data;
125   void *raw;
126 };
127
128 typedef struct AppEventHandler *AppEventHandlerPtr;
129
130 int EventCallback(void *event, void *data)
131 {
132   AppEventHandlerPtr handler = static_cast<AppEventHandlerPtr>(data);
133
134   struct AppEventInfo appEvent;
135
136   appEvent.type = handler->type;
137   appEvent.value = event;
138
139   if (handler->cb)
140     handler->cb(&appEvent, handler->data);
141
142   return 0;
143 }
144
145 int AppAddEventHandler(AppEventHandlerPtr *eventHandler, AppEventType eventType, AppEventCallback callback, void *userData)
146 {
147   AppEventHandlerPtr handler;
148
149   handler = static_cast<AppEventHandlerPtr>( calloc(1, sizeof(struct AppEventHandler)) );
150   if (!handler)
151   {
152     DALI_LOG_ERROR( "failed to create handler" );
153     return TIZEN_ERROR_UNKNOWN;
154   }
155   else
156   {
157     handler->type = eventType;
158     handler->cb = callback;
159     handler->data = userData;
160     handler->raw = appcore_base_add_event( static_cast<appcore_base_event>(AppEventConverter[static_cast<int>(eventType)]), EventCallback, handler);
161
162     *eventHandler = handler;
163
164     return TIZEN_ERROR_NONE;
165   }
166 }
167
168 } // namespace Appcore
169
170 /**
171  * Impl to hide EFL data members
172  */
173 struct Framework::Impl
174 {
175 // Constructor
176   Impl(void* data, Type type )
177   : mAbortCallBack( NULL ),
178     mCallbackManager( NULL )
179 #ifdef APPCORE_WATCH_AVAILABLE
180     , mWatchCallback()
181 #endif
182   {
183     mFramework = static_cast<Framework*>(data);
184     gForegroundState = false;
185
186 #ifndef APPCORE_WATCH_AVAILABLE
187     if ( type == WATCH )
188     {
189       throw Dali::DaliException( "", "Watch Application is not supported." );
190     }
191 #endif
192     mApplicationType = type;
193     mCallbackManager = CallbackManager::New();
194   }
195
196   ~Impl()
197   {
198     delete mAbortCallBack;
199
200     // we're quiting the main loop so
201     // mCallbackManager->RemoveAllCallBacks() does not need to be called
202     // to delete our abort handler
203     delete mCallbackManager;
204   }
205
206   int AppMain()
207   {
208     int ret;
209
210     if (mApplicationType == NORMAL)
211     {
212       ret = AppNormalMain();
213     }
214     else if(mApplicationType == WIDGET)
215     {
216       ret = AppWidgetMain();
217     }
218     else
219     {
220       ret = AppWatchMain();
221     }
222     return ret;
223   }
224
225   void AppExit()
226   {
227     if (mApplicationType == NORMAL)
228     {
229       AppNormalExit();
230     }
231     else if(mApplicationType == WIDGET)
232     {
233       AppWidgetExit();
234     }
235     else
236     {
237       AppWatchExit();
238     }
239   }
240
241
242   // Data
243   Type mApplicationType;
244   CallbackBase* mAbortCallBack;
245   CallbackManager *mCallbackManager;
246
247   Framework* mFramework;
248   AppCore::AppEventHandlerPtr handlers[5];
249
250 #ifdef APPCORE_WATCH_AVAILABLE
251   watch_app_lifecycle_callback_s mWatchCallback;
252   app_event_handler_h watchHandlers[5];
253 #endif
254
255   static int AppCreate(void *data)
256   {
257     appcore_ui_base_on_create();
258     return static_cast<int>( static_cast<Framework*>(data)->Create() );
259   }
260
261   static int AppTerminate(void *data)
262   {
263     appcore_ui_base_on_terminate();
264     Observer *observer = &static_cast<Framework*>(data)->mObserver;
265
266     observer->OnTerminate();
267
268     return 0;
269   }
270
271   static int AppPause(void *data)
272   {
273     appcore_ui_base_on_pause();
274     Observer *observer = &static_cast<Framework*>(data)->mObserver;
275
276     observer->OnPause();
277
278     return 0;
279   }
280
281   static int AppResume(void *data)
282   {
283     appcore_ui_base_on_resume();
284     Observer *observer = &static_cast<Framework*>(data)->mObserver;
285
286     observer->OnResume();
287
288     return 0;
289   }
290
291   static void ProcessBundle(Framework* framework, bundle *bundleData)
292   {
293     if(bundleData == NULL)
294     {
295       return;
296     }
297
298     // get bundle name
299     char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
300     if(bundleName != NULL)
301     {
302       framework->SetBundleName(bundleName);
303     }
304
305     // get bundle? id
306     char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
307     if(bundleId != NULL)
308     {
309       framework->SetBundleId(bundleId);
310     }
311   }
312
313   /**
314    * Called by AppCore when the application is launched from another module (e.g. homescreen).
315    * @param[in] b the bundle data which the launcher module sent
316    */
317   static int AppControl(bundle* bundleData, void *data)
318   {
319     app_control_h appControl = NULL;
320
321     appcore_ui_base_on_control(bundleData);
322
323     if (bundleData)
324     {
325       if (app_control_create_event(bundleData, &appControl) != TIZEN_ERROR_NONE)
326       {
327         DALI_LOG_ERROR("Failed to create an app_control handle");
328       }
329     }
330     else
331     {
332       if (app_control_create(&appControl) != TIZEN_ERROR_NONE)
333       {
334         DALI_LOG_ERROR("Failed to create an app_control handle");
335       }
336     }
337
338     Framework* framework = static_cast<Framework*>(data);
339     Observer *observer = &framework->mObserver;
340
341     ProcessBundle(framework, bundleData);
342
343     observer->OnReset();
344     observer->OnAppControl(appControl);
345
346     app_control_destroy(appControl);
347
348     return 0;
349   }
350
351   static void AppInit(int argc, char **argv, void *data)
352   {
353 #pragma GCC diagnostic push
354 #pragma GCC diagnostic ignored "-Wold-style-cast"
355
356     ecore_init();
357     ecore_app_args_set( argc, (const char **)argv );
358
359 #pragma GCC diagnostic pop
360   }
361
362   static void AppFinish(void)
363   {
364     ecore_shutdown();
365
366     if(getenv("AUL_LOADER_INIT"))
367     {
368       unsetenv("AUL_LOADER_INIT");
369       ecore_shutdown();
370     }
371   }
372
373   static void AppRun(void *data)
374   {
375     ecore_main_loop_begin();
376   }
377
378   static void AppExit(void *data)
379   {
380     ecore_main_loop_quit();
381   }
382
383   static void AppLanguageChanged(AppCore::AppEventInfoPtr event, void *data)
384   {
385     Observer *observer = &static_cast<Framework*>(data)->mObserver;
386     std::string language( static_cast<const char *>(event->value) );
387     observer->OnLanguageChanged(language);
388   }
389
390   static void AppDeviceRotated(AppCore::AppEventInfoPtr event_info, void *data)
391   {
392   }
393
394   static void AppRegionChanged(AppCore::AppEventInfoPtr event, void *data)
395   {
396     Observer *observer = &static_cast<Framework*>(data)->mObserver;
397     std::string region( static_cast<const char *>(event->value) );
398     observer->OnRegionChanged(region);
399   }
400
401   static void AppBatteryLow(AppCore::AppEventInfoPtr event, void *data)
402   {
403     Observer *observer = &static_cast<Framework*>(data)->mObserver;
404     int status = *static_cast<int *>(event->value);
405     Dali::DevelApplication::BatteryStatus::Type result = Dali::DevelApplication::BatteryStatus::NORMAL;
406
407     // convert to dali battery status
408     switch( status )
409     {
410       case 1:
411       {
412         result = Dali::DevelApplication::BatteryStatus::POWER_OFF;
413         break;
414       }
415       case 2:
416       {
417         result = Dali::DevelApplication::BatteryStatus::CRITICAL_LOW;
418         break;
419       }
420       default :
421         break;
422     }
423     observer->OnBatteryLow(result);
424   }
425
426   static void AppMemoryLow(AppCore::AppEventInfoPtr event, void *data)
427   {
428     Observer *observer = &static_cast<Framework*>(data)->mObserver;
429     int status = *static_cast<int *>(event->value);
430     Dali::DevelApplication::MemoryStatus::Type result = Dali::DevelApplication::MemoryStatus::NORMAL;
431     // convert to dali memmory status
432     switch( status )
433     {
434       case 1:
435       {
436         result = Dali::DevelApplication::MemoryStatus::NORMAL;
437         break;
438       }
439       case 2:
440       {
441         result = Dali::DevelApplication::MemoryStatus::SOFT_WARNING;
442         break;
443       }
444       case 4:
445       {
446         result = Dali::DevelApplication::MemoryStatus::HARD_WARNING;
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   void AppWidgetExit()
499   {
500     if( !IsWidgetFeatureEnabled() )
501     {
502       DALI_LOG_ERROR("widget feature is not supported");
503       return;
504     }
505
506     appcore_multiwindow_base_exit();
507     aul_notify_exit();
508   }
509
510   int AppWidgetMain()
511   {
512     if( !IsWidgetFeatureEnabled() )
513     {
514       DALI_LOG_ERROR("widget feature is not supported");
515       return 0;
516     }
517
518     AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
519     AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
520     AppCore::AppAddEventHandler(&handlers[AppCore::DEVICE_ORIENTATION_CHANGED], AppCore::DEVICE_ORIENTATION_CHANGED, AppDeviceRotated, mFramework);
521     AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
522     AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
523
524     appcore_multiwindow_base_ops ops = appcore_multiwindow_base_get_default_ops();
525
526     /* override methods */
527     ops.base.create = WidgetAppCreate;
528     ops.base.control = WidgetAppControl;
529     ops.base.terminate = WidgetAppTerminate;
530     ops.base.receive = WidgetAppReceive;
531     ops.base.init = AppInit;
532     ops.base.finish = AppFinish;
533     ops.base.run = AppRun;
534     ops.base.exit = AppExit;
535
536     bundle *bundleFromArgv = bundle_import_from_argv(*mFramework->mArgc, *mFramework->mArgv);
537
538     char* viewerEndpoint = NULL;
539
540     if (bundleFromArgv)
541     {
542       bundle_get_str(bundleFromArgv, "__WIDGET_ENDPOINT__", &viewerEndpoint);
543       if (viewerEndpoint)
544       {
545         gViewerEndpoint = strdup(viewerEndpoint);
546       }
547       else
548       {
549         DALI_LOG_ERROR("endpoint is missing");
550         return 0;
551       }
552
553       bundle_free(bundleFromArgv);
554     }
555     else
556     {
557       DALI_LOG_ERROR("failed to get launch argv");
558       return 0;
559     }
560
561     appcore_multiwindow_base_init(ops, *mFramework->mArgc, *mFramework->mArgv, mFramework);
562     appcore_multiwindow_base_fini();
563     return TIZEN_ERROR_NONE;
564   }
565
566   static void WidgetAppPoweroff(keynode_t *key, void *data)
567   {
568     int val;
569
570     val = vconf_keynode_get_int(key);
571     switch (val) {
572       case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
573       case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
574       {
575         static_cast<Internal::Adaptor::Framework::Impl*>(data)->AppWidgetExit();
576         break;
577       }
578       case VCONFKEY_SYSMAN_POWER_OFF_NONE:
579       case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
580       default:
581         break;
582     }
583   }
584
585   static int WidgetAppCreate(void *data)
586   {
587     char pkgid[256] = {0, };
588
589     appcore_multiwindow_base_on_create();
590     app_get_id(&gAppId);
591
592     if(aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
593     {
594       gPackageId = strdup(pkgid);
595     }
596
597     if(!gPackageId || !gAppId)
598     {
599       DALI_LOG_ERROR("package_id is NULL");
600       return -1;
601     }
602
603     screen_connector_provider_init();
604     vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, WidgetAppPoweroff, data);
605
606     return static_cast<int>( static_cast<Framework*>(data)->Create() );
607   }
608
609   static int WidgetAppTerminate(void *data)
610   {
611     Observer *observer = &static_cast<Framework*>(data)->mObserver;
612
613     observer->OnTerminate();
614
615     vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, WidgetAppPoweroff);
616     screen_connector_provider_fini();
617
618     appcore_multiwindow_base_on_terminate();
619     return 0;
620   }
621
622   static void WidgetAppInstResume(const char* classId, const char* id, appcore_multiwindow_base_instance_h context, void* data)
623   {
624     WidgetInstanceResume(classId, id, static_cast<bundle*>(data));
625   }
626
627   static void WidgetInstanceResume(const char* classId, const char* id, bundle* bundleData)
628   {
629     appcore_multiwindow_base_instance_h context;
630
631     context = appcore_multiwindow_base_instance_find(id);
632
633     if(!context)
634     {
635       DALI_LOG_ERROR("context not found: %s", id);
636       return;
637     }
638
639     appcore_multiwindow_base_instance_resume(context);
640
641     SendUpdateStatus(classId, id, AUL_WIDGET_INSTANCE_EVENT_RESUME, NULL);
642
643     if(!gForegroundState)
644     {
645       aul_send_app_status_change_signal( getpid(), gAppId, gPackageId, "fg", "widgetapp" );
646       gForegroundState = true;
647     }
648
649     return;
650   }
651
652   static int SendUpdateStatus(const char* classId, const char* instanceId, int status, bundle* extra )
653   {
654     bundle* bundleData;
655     int lifecycle = -1;
656     bundle_raw *raw = NULL;
657     int length;
658
659     bundleData = bundle_create();
660     if(!bundleData)
661     {
662       DALI_LOG_ERROR("out of memory");
663       return -1;
664     }
665
666     bundle_add_str(bundleData, AUL_K_WIDGET_ID, classId);
667     bundle_add_str(bundleData, AUL_K_WIDGET_INSTANCE_ID, instanceId);
668     bundle_add_byte(bundleData, AUL_K_WIDGET_STATUS, &status, sizeof(int));
669
670     if(extra)
671     {
672       bundle_encode(extra, &raw, &length);
673 #pragma GCC diagnostic push
674 #pragma GCC diagnostic ignored "-Wold-style-cast"
675       bundle_add_str(bundleData, "__WIDGET_CONTENT_INFO__", (const char*)raw);
676 #pragma GCC diagnostic pop
677
678       aul_widget_instance_add(classId, instanceId);
679     }
680
681     aul_app_com_send(gViewerEndpoint, bundleData);
682
683     switch(status)
684     {
685       case AUL_WIDGET_INSTANCE_EVENT_CREATE:
686         lifecycle = Dali::Widget::WidgetLifecycleEventType::CREATE;
687         break;
688       case AUL_WIDGET_INSTANCE_EVENT_DESTROY:
689         lifecycle = Dali::Widget::WidgetLifecycleEventType::DESTROY;
690         break;
691       case AUL_WIDGET_INSTANCE_EVENT_PAUSE:
692         lifecycle = Dali::Widget::WidgetLifecycleEventType::PAUSE;
693         break;
694       case AUL_WIDGET_INSTANCE_EVENT_RESUME:
695         lifecycle = Dali::Widget::WidgetLifecycleEventType::RESUME;
696         break;
697     }
698
699     if (lifecycle > -1)
700       SendLifecycleEvent(classId, instanceId, lifecycle);
701
702     bundle_free(bundleData);
703     if (raw)
704       free(raw);
705
706     return 0;
707   }
708
709   static int SendLifecycleEvent(const char* classId, const char* instanceId, int status)
710   {
711     bundle* bundleData = bundle_create();
712     int ret;
713
714     if (bundleData == NULL)
715     {
716       DALI_LOG_ERROR("out of memory");
717       return -1;
718     }
719
720     bundle_add_str(bundleData, AUL_K_WIDGET_ID, classId);
721     bundle_add_str(bundleData, AUL_K_WIDGET_INSTANCE_ID, instanceId);
722     bundle_add_byte(bundleData, AUL_K_WIDGET_STATUS, &status, sizeof(int));
723     bundle_add_str(bundleData, AUL_K_PKGID, gPackageId);
724
725     ret = aul_app_com_send("widget.status", bundleData);
726
727     if (ret < 0)
728       DALI_LOG_ERROR("send lifecycle error:%d", ret);
729
730     bundle_free(bundleData);
731
732     return ret;
733   }
734
735   static int WidgetAppReceive(aul_type type, bundle *bundleData, void *data)
736   {
737     appcore_multiwindow_base_on_receive(type, bundleData);
738
739     switch(type)
740     {
741       case AUL_RESUME:
742       {
743         appcore_multiwindow_base_instance_foreach_full(WidgetAppInstResume, bundleData);
744         break;
745       }
746       case AUL_TERMINATE:
747       {
748         static_cast<Internal::Adaptor::Framework::Impl*>(data)->AppWidgetExit();
749         break;
750       }
751       case AUL_WIDGET_CONTENT:
752       {
753         GetContent(bundleData);
754         break;
755       }
756       default:
757         break;
758     }
759     return 0;
760   }
761
762   static void GetContent( bundle* bundleData )
763   {
764     char* instanceId = NULL;
765     appcore_multiwindow_base_instance_h context;
766     const appcore_multiwindow_base_class *cls;
767     Internal::Adaptor::Widget *widgetInstance;
768
769     bundle_get_str(bundleData, AUL_K_WIDGET_INSTANCE_ID, &instanceId);
770     if(!instanceId)
771     {
772       DALI_LOG_ERROR("instance id is NULL");
773       return;
774     }
775
776     context = static_cast<appcore_multiwindow_base_instance_h>(appcore_multiwindow_base_instance_find(instanceId));
777     if(!context)
778     {
779       DALI_LOG_ERROR("could not find widget obj: %s", instanceId);
780       return;
781     }
782
783     cls = appcore_multiwindow_base_instance_get_class(context);
784     if(!cls)
785     {
786       DALI_LOG_ERROR("widget class is NULL");
787       return;
788     }
789
790     widgetInstance = static_cast<Internal::Adaptor::Widget*>(cls->data);
791     if(!widgetInstance)
792     {
793       DALI_LOG_ERROR("widget instance is NULL");
794       return;
795     }
796
797     Dali::WidgetData *widgetData = widgetInstance->FindWidgetData( instanceId );
798     if(!widgetData)
799     {
800       DALI_LOG_ERROR("widget extra is NULL");
801       return;
802     }
803
804     char* widgetContent = widgetData->GetContent();
805     if(widgetContent)
806     {
807       bundle_add_str(bundleData, AUL_K_WIDGET_CONTENT_INFO, widgetContent);
808     }
809     else
810     {
811       bundle_add_str(bundleData, AUL_K_WIDGET_CONTENT_INFO, "");
812     }
813   }
814
815   /**
816    * Called by AppCore when the application is launched from another module (e.g. homescreen).
817    * @param[in] b the bundle data which the launcher module sent
818    */
819   static int WidgetAppControl(bundle* bundleData, void *data)
820   {
821     char *classId = NULL;
822     char *id = NULL;
823     char *operation = NULL;
824
825     appcore_multiwindow_base_on_control(bundleData);
826
827     bundle_get_str(bundleData, AUL_K_WIDGET_ID, &classId);
828     bundle_get_str(bundleData, AUL_K_WIDGET_INSTANCE_ID, &id);
829     bundle_get_str(bundleData, "__WIDGET_OP__", &operation);
830
831     if(!operation)
832     {
833       DALI_LOG_ERROR("operation is NULL");
834       return 0;
835     }
836
837     if(strcmp(operation, "create") == 0)
838     {
839       InstanceCreate( classId, id, bundleData );
840     }
841     else if (strcmp(operation, "resize") == 0)
842     {
843       InstanceResize( classId, id, bundleData );
844     }
845     else if (strcmp(operation, "update") == 0)
846     {
847       InstanceUpdate( classId, id, bundleData );
848     }
849     else if (strcmp(operation, "destroy") == 0)
850     {
851       InstanceDestroy( classId, id, bundleData, data );
852     }
853     else if (strcmp(operation, "resume") == 0)
854     {
855       InstanceResume( classId, id, bundleData );
856     }
857     else if (strcmp(operation, "pause") == 0)
858     {
859       InstancePause( classId, id, bundleData );
860     }
861     else if (strcmp(operation, "terminate") == 0)
862     {
863       InstanceDestroy( classId, id, bundleData, data );
864     }
865
866     return 0;
867   }
868
869   static void InstanceCreate(const char* classId, const char* id, bundle* bundleData)
870   {
871     appcore_multiwindow_base_instance_run(classId, id, bundle_dup(bundleData));
872   }
873
874   static void InstanceResize(const char *classId, const char *id, bundle *bundleData)
875   {
876     appcore_multiwindow_base_instance_h context;
877     Internal::Adaptor::Widget *widgetInstance;
878     const appcore_multiwindow_base_class *cls;
879     char *remain = NULL;
880     char *widthStr = NULL;
881     char *heightStr = NULL;
882     uint32_t width = 0;
883     uint32_t height = 0;
884
885     context = appcore_multiwindow_base_instance_find(id);
886
887     if(!context)
888     {
889       DALI_LOG_ERROR("context not found: %s", id);
890       return;
891     }
892
893     cls = appcore_multiwindow_base_instance_get_class(context);
894     if(!cls)
895     {
896       DALI_LOG_ERROR("widget class is NULL");
897       return;
898     }
899
900     widgetInstance = static_cast<Internal::Adaptor::Widget*>(cls->data);
901     if(!widgetInstance)
902     {
903       DALI_LOG_ERROR("widget instance is NULL");
904       return;
905     }
906
907     bundle_get_str(bundleData, "__WIDGET_WIDTH__", &widthStr);
908     bundle_get_str(bundleData, "__WIDGET_HEIGHT__", &heightStr);
909
910     if(widthStr)
911       width = static_cast<uint32_t>(g_ascii_strtoll(widthStr, &remain, 10));
912
913     if(heightStr)
914       height = static_cast<uint32_t>(g_ascii_strtoll(heightStr, &remain, 10));
915
916     widgetInstance->OnResize( context, Dali::Widget::WindowSize(width,height) );
917   }
918
919   static void InstanceUpdate(const char* classId, const char* id, bundle* bundleData)
920   {
921     appcore_multiwindow_base_instance_h context;
922
923     if(!id)
924     {
925       appcore_multiwindow_base_instance_foreach(classId, UpdateCallback, bundleData);
926       return;
927     }
928
929     context = appcore_multiwindow_base_instance_find(id);
930
931     if(!context)
932     {
933       DALI_LOG_ERROR("context not found: %s", id);
934       return;
935     }
936
937     UpdateCallback(classId, id, context, bundleData);
938   }
939
940   static void UpdateCallback(const char* classId, const char* id, appcore_multiwindow_base_instance_h context, void* data)
941   {
942     Internal::Adaptor::Widget *widgetInstance;
943     const appcore_multiwindow_base_class *cls;
944     bundle* content = NULL;
945     char* contentRaw = NULL;
946     char* forceStr = NULL;
947     int force;
948     bundle* bundleData = static_cast<bundle*>(data);
949
950     if(!bundleData)
951     {
952       DALI_LOG_ERROR("bundle is NULL");
953       return;
954     }
955
956     cls = appcore_multiwindow_base_instance_get_class(context);
957     if(!cls)
958     {
959       DALI_LOG_ERROR("class is NULL");
960       return;
961     }
962
963     widgetInstance = static_cast<Internal::Adaptor::Widget*>(cls->data);
964     if(!widgetInstance)
965     {
966       DALI_LOG_ERROR("widget instance is NULL");
967       return;
968     }
969
970     bundle_get_str(bundleData, "__WIDGET_FORCE__", &forceStr);
971
972     if(forceStr && strcmp(forceStr, "true") == 0)
973     {
974       force = 1;
975     }
976     else
977     {
978       force = 0;
979     }
980
981     bundle_get_str(bundleData, "__WIDGET_CONTENT_INFO__", &contentRaw);
982
983     if(contentRaw)
984     {
985 #pragma GCC diagnostic push
986 #pragma GCC diagnostic ignored "-Wold-style-cast"
987       content = bundle_decode((const bundle_raw *)contentRaw, strlen(contentRaw));
988 #pragma GCC diagnostic pop
989
990     }
991
992     widgetInstance->OnUpdate(context, content, force);
993
994     if(content)
995     {
996       bundle_free(content);
997     }
998   }
999
1000   static void InstanceDestroy(const char* classId, const char* id, bundle* bundleData, void* data)
1001   {
1002     appcore_multiwindow_base_instance_h context;
1003
1004     Internal::Adaptor::Widget *widgetInstance;
1005     const appcore_multiwindow_base_class *cls;
1006
1007     context = appcore_multiwindow_base_instance_find(id);
1008
1009     if(!context)
1010     {
1011       DALI_LOG_ERROR("could not find widget obj: %s, clear amd info", id);
1012       aul_widget_instance_del(classId, id);
1013       return;
1014     }
1015
1016     cls = appcore_multiwindow_base_instance_get_class(context);
1017     if(!cls)
1018     {
1019       DALI_LOG_ERROR("widget class is NULL");
1020       return;
1021     }
1022
1023     widgetInstance = static_cast<Internal::Adaptor::Widget*>(cls->data);
1024     if(!widgetInstance)
1025     {
1026       DALI_LOG_ERROR("widget instance is NULL");
1027       return;
1028     }
1029
1030     Dali::WidgetData *widgetData  = widgetInstance->FindWidgetData(id);
1031
1032     widgetData->SetArgs( bundleData );
1033     appcore_multiwindow_base_instance_exit(context);
1034     CheckEmptyInstance(data);
1035   }
1036
1037   static void CheckEmptyInstance(void* data)
1038   {
1039     int cnt = appcore_multiwindow_base_instance_get_cnt();
1040
1041     if(cnt == 0)
1042     {
1043       static_cast<Internal::Adaptor::Framework::Impl*>(data)->AppWidgetExit();
1044     }
1045   }
1046
1047   static void InstanceResume(const char* classId, const char* id, bundle* bundleData)
1048   {
1049     appcore_multiwindow_base_instance_h context;
1050
1051     context = appcore_multiwindow_base_instance_find(id);
1052
1053     if(!context)
1054     {
1055       DALI_LOG_ERROR("context not found: %s", id);
1056       return;
1057     }
1058
1059     appcore_multiwindow_base_instance_resume(context);
1060
1061     SendUpdateStatus(classId, id, AUL_WIDGET_INSTANCE_EVENT_RESUME, NULL);
1062     if(!gForegroundState)
1063     {
1064       aul_send_app_status_change_signal(getpid(), gAppId, gPackageId, "fg", "widgetapp" );
1065       gForegroundState = true;
1066     }
1067   }
1068
1069   static void InstancePause(const char* classId, const char* id, bundle* bundleData)
1070   {
1071     appcore_multiwindow_base_instance_h context;
1072
1073     context = appcore_multiwindow_base_instance_find(id);
1074
1075     if(!context)
1076     {
1077       DALI_LOG_ERROR("context not found: %s", id);
1078       return;
1079     }
1080
1081     appcore_multiwindow_base_instance_pause(context);
1082
1083     if(gForegroundState)
1084     {
1085       aul_send_app_status_change_signal(getpid(), gAppId, gPackageId, "bg", "widgetapp" );
1086       gForegroundState = false;
1087     }
1088   }
1089
1090   static bool IsWidgetFeatureEnabled()
1091   {
1092     static bool feature = false;
1093     static bool retrieved = false;
1094     int ret;
1095
1096     if(retrieved == true)
1097       return feature;
1098
1099     ret = system_info_get_platform_bool("http://tizen.org/feature/shell.appwidget", &feature);
1100     if(ret != SYSTEM_INFO_ERROR_NONE)
1101     {
1102       DALI_LOG_ERROR("failed to get system info"); /* LCOV_EXCL_LINE */
1103       return false; /* LCOV_EXCL_LINE */
1104     }
1105
1106     retrieved = true;
1107     return feature;
1108   }
1109
1110
1111 #ifdef APPCORE_WATCH_AVAILABLE
1112   static bool WatchAppCreate(int width, int height, void *data)
1113   {
1114     return static_cast<Framework*>(data)->Create();
1115   }
1116
1117   static void WatchAppTimeTick(watch_time_h time, void *data)
1118   {
1119     Observer *observer = &static_cast<Framework*>(data)->mObserver;
1120     WatchTime curTime(time);
1121
1122     observer->OnTimeTick(curTime);
1123   }
1124
1125   static void WatchAppAmbientTick(watch_time_h time, void *data)
1126   {
1127     Observer *observer = &static_cast<Framework*>(data)->mObserver;
1128     WatchTime curTime(time);
1129
1130     observer->OnAmbientTick(curTime);
1131   }
1132
1133   static void WatchAppAmbientChanged(bool ambient, void *data)
1134   {
1135     Observer *observer = &static_cast<Framework*>(data)->mObserver;
1136
1137     observer->OnAmbientChanged(ambient);
1138   }
1139
1140   static void WatchAppControl(app_control_h app_control, void *data)
1141   {
1142     Framework* framework = static_cast<Framework*>(data);
1143     Observer *observer = &framework->mObserver;
1144     bundle *bundleData = NULL;
1145
1146     app_control_to_bundle(app_control, &bundleData);
1147     ProcessBundle(framework, bundleData);
1148
1149     observer->OnReset();
1150     observer->OnAppControl(app_control);
1151   }
1152
1153   static void WatchAppTerminate(void *data)
1154   {
1155     Observer *observer = &static_cast<Framework*>(data)->mObserver;
1156
1157     observer->OnTerminate();
1158   }
1159
1160   static void WatchAppPause(void *data)
1161   {
1162     Observer *observer = &static_cast<Framework*>(data)->mObserver;
1163
1164     observer->OnPause();
1165   }
1166
1167   static void WatchAppResume(void *data)
1168   {
1169     Observer *observer = &static_cast<Framework*>(data)->mObserver;
1170
1171     observer->OnResume();
1172   }
1173 #endif
1174
1175   int AppWatchMain()
1176   {
1177     int ret = true;
1178
1179 #ifdef APPCORE_WATCH_AVAILABLE
1180     mWatchCallback.create = WatchAppCreate;
1181     mWatchCallback.app_control = WatchAppControl;
1182     mWatchCallback.terminate = WatchAppTerminate;
1183     mWatchCallback.pause = WatchAppPause;
1184     mWatchCallback.resume = WatchAppResume;
1185     mWatchCallback.time_tick = WatchAppTimeTick;
1186     mWatchCallback.ambient_tick = WatchAppAmbientTick;
1187     mWatchCallback.ambient_changed = WatchAppAmbientChanged;
1188
1189     AppCore::AppAddEventHandler(&handlers[AppCore::LOW_BATTERY], AppCore::LOW_BATTERY, AppBatteryLow, mFramework);
1190     AppCore::AppAddEventHandler(&handlers[AppCore::LOW_MEMORY], AppCore::LOW_MEMORY, AppMemoryLow, mFramework);
1191     AppCore::AppAddEventHandler(&handlers[AppCore::LANGUAGE_CHANGED], AppCore::LANGUAGE_CHANGED, AppLanguageChanged, mFramework);
1192     AppCore::AppAddEventHandler(&handlers[AppCore::REGION_FORMAT_CHANGED], AppCore::REGION_FORMAT_CHANGED, AppRegionChanged, mFramework);
1193
1194     ret = watch_app_main(*mFramework->mArgc, *mFramework->mArgv, &mWatchCallback, mFramework);
1195 #endif
1196     return ret;
1197   }
1198
1199   void AppWatchExit()
1200   {
1201 #ifdef APPCORE_WATCH_AVAILABLE
1202     watch_app_exit();
1203 #endif
1204   }
1205
1206
1207 private:
1208   // Undefined
1209   Impl( const Impl& impl );
1210
1211   // Undefined
1212   Impl& operator=( const Impl& impl );
1213 };
1214
1215 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
1216 : mObserver(observer),
1217   mInitialised(false),
1218   mRunning(false),
1219   mArgc(argc),
1220   mArgv(argv),
1221   mBundleName(""),
1222   mBundleId(""),
1223   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
1224   mImpl(NULL)
1225 {
1226   bool featureFlag = true;
1227   system_info_get_platform_bool( "tizen.org/feature/opengles.version.2_0", &featureFlag );
1228
1229   if( featureFlag == false )
1230   {
1231     set_last_result( TIZEN_ERROR_NOT_SUPPORTED );
1232   }
1233 #ifdef DALI_ELDBUS_AVAILABLE
1234   // Initialize ElDBus.
1235   eldbus_init();
1236 #endif
1237   InitThreads();
1238
1239   mImpl = new Impl(this, type);
1240 }
1241
1242 Framework::~Framework()
1243 {
1244   if (mRunning)
1245   {
1246     Quit();
1247   }
1248
1249 #ifdef DALI_ELDBUS_AVAILABLE
1250   // Shutdown ELDBus.
1251   eldbus_shutdown();
1252 #endif
1253
1254   delete mImpl;
1255 }
1256
1257 bool Framework::Create()
1258 {
1259   mInitialised = true;
1260   mObserver.OnInit();
1261   return true;
1262 }
1263
1264 void Framework::Run()
1265 {
1266   mRunning = true;
1267   int ret;
1268
1269   ret = mImpl->AppMain();
1270   if (ret != APP_ERROR_NONE)
1271   {
1272     DALI_LOG_ERROR("Framework::Run(), ui_app_main() is failed. err = %d\n", ret);
1273   }
1274   mRunning = false;
1275 }
1276
1277 void Framework::Quit()
1278 {
1279   mImpl->AppExit();
1280 }
1281
1282 bool Framework::IsMainLoopRunning()
1283 {
1284   return mRunning;
1285 }
1286
1287 void Framework::AddAbortCallback( CallbackBase* callback )
1288 {
1289   mImpl->mAbortCallBack = callback;
1290 }
1291
1292 std::string Framework::GetBundleName() const
1293 {
1294   return mBundleName;
1295 }
1296
1297 void Framework::SetBundleName(const std::string& name)
1298 {
1299   mBundleName = name;
1300 }
1301
1302 std::string Framework::GetBundleId() const
1303 {
1304   return mBundleId;
1305 }
1306
1307 std::string Framework::GetResourcePath()
1308 {
1309   std::string resourcePath = "";
1310 #if defined( TIZEN_PLATFORM_CONFIG_SUPPORTED ) && TIZEN_PLATFORM_CONFIG_SUPPORTED
1311   resourcePath = app_get_resource_path();
1312 #else // For backwards compatibility with older Tizen versions
1313
1314   // "DALI_APPLICATION_PACKAGE" is used to get the already configured Application package path.
1315   const char* environmentVariable = "DALI_APPLICATION_PACKAGE";
1316   char* value = getenv( environmentVariable );
1317   if ( value != NULL )
1318   {
1319     resourcePath = value;
1320   }
1321 #endif //TIZEN_PLATFORM_CONFIG_SUPPORTED
1322
1323   return resourcePath;
1324 }
1325
1326 void Framework::SetBundleId(const std::string& id)
1327 {
1328   mBundleId = id;
1329 }
1330
1331 void Framework::AbortCallback( )
1332 {
1333   // if an abort call back has been installed run it.
1334   if (mImpl->mAbortCallBack)
1335   {
1336     CallbackBase::Execute( *mImpl->mAbortCallBack );
1337   }
1338   else
1339   {
1340     Quit();
1341   }
1342 }
1343
1344 } // namespace Adaptor
1345
1346 } // namespace Internal
1347
1348 } // namespace Dali