Set pre multiplied property
[platform/core/uifw/widget-viewer-dali.git] / internal / widget_view / widget_view_impl.cpp
1 /*
2  * Samsung API
3  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Flora License, Version 1.1 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://floralicense.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 // CLASS HEADER
19 #include <internal/widget_view/widget_view_impl.h>
20
21 // INTERNAL INCLUDES
22
23 // EXTERNAL INCLUDES
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/events/touch-data.h>
26 #include <dali/public-api/events/wheel-event.h>
27 #include <dali/public-api/images/native-image.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
32 #include <dali/integration-api/debug.h>
33 #include <string.h>
34 #include <Ecore_Input.h>
35 #include <widget_service.h>
36 #include <widget_instance.h>
37 #include <tzplatform_config.h>
38 #include <wayland-extension/tizen-extension-client-protocol.h>
39
40 namespace Dali
41 {
42
43 namespace WidgetView
44 {
45
46 namespace Internal
47 {
48
49 namespace
50 {
51
52 #define WIDGET_VIEW_RESOURCE_DEFAULT_IMG "/widget_viewer_dali/images/unknown.png"
53
54 #if defined(DEBUG_ENABLED)
55 Integration::Log::Filter* gWidgetViewLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW" );
56 #endif
57
58 BaseHandle Create()
59 {
60   return Dali::BaseHandle();
61 }
62
63 // Setup properties, signals and actions using the type-registry.
64 DALI_TYPE_REGISTRATION_BEGIN( Dali::WidgetView::WidgetView, Toolkit::Control, Create );
65 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "widgetId", BOOLEAN, WIDGET_ID )
66 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "instanceId", BOOLEAN, INSTANCE_ID )
67 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "contentInfo", BOOLEAN, CONTENT_INFO )
68 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "title", BOOLEAN, TITLE )
69 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "updatePeriod", BOOLEAN, UPDATE_PERIOD )
70 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "preview", MAP, PREVIEW )
71 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "loadingText", MAP, LOADING_TEXT )
72 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "widgetStateFaulted", BOOLEAN, WIDGET_STATE_FAULTED )
73 DALI_PROPERTY_REGISTRATION( Dali::WidgetView, WidgetView, "permanentDelete", BOOLEAN, PERMANENT_DELETE )
74
75 // Signals
76 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetAdded", SIGNAL_WIDGET_ADDED )
77 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetDeleted", SIGNAL_WIDGET_DELETED )
78 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetCreationAborted", SIGNAL_WIDGET_CREATION_ABORTED )
79 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetContentUpdated", SIGNAL_WIDGET_CONTENT_UPDATED )
80 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetUpdatePeriodChanged", SIGNAL_WIDGET_UPDATE_PERIOD_CHANGED )
81 DALI_SIGNAL_REGISTRATION( Dali::WidgetView, WidgetView, "widgetFaulted", SIGNAL_WIDGET_FAULTED )
82
83 // Actions
84 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "pauseWidget", ACTION_WIDGETVIEW_PAUSE_WIDGET );
85 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "resumeWidget", ACTION_WIDGETVIEW_RESUME_WIDGET );
86 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "cancelTouchEvent", ACTION_WIDGETVIEW_CANCEL_TOUCH_EVENT );
87 DALI_ACTION_REGISTRATION( Dali::WidgetView, WidgetView, "activateFaultedWidget", ACTION_WIDGETVIEW_ACTIVATE_FAULTED_WIDGET );
88
89 DALI_TYPE_REGISTRATION_END()
90
91 static void OnBufferChanged( struct tizen_remote_surface *surface, uint32_t type, struct wl_buffer *buffer,
92                               int32_t img_file_fd, uint32_t img_file_size, uint32_t time, struct wl_array *keys, void *data)
93 {
94   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
95
96   if( widgetView )
97   {
98     if( !widgetView->IsWidgetImageView() )
99     {
100       tizen_remote_surface_transfer_visibility( surface, TIZEN_REMOTE_SURFACE_VISIBILITY_TYPE_VISIBLE);
101
102       widgetView->CreateWidgetImageView();
103       widgetView->ConnectSignal( surface );
104     }
105
106     //get tbm surface from buffer
107     tbm_surface_h tbmSurface = static_cast< tbm_surface_h >( wl_buffer_get_user_data( buffer ) );
108     widgetView->UpdateImageSource( tbmSurface );
109   }
110 }
111
112 static void OnBufferUpdated( struct tizen_remote_surface* surface, wl_buffer* buffer, uint32_t time, void* data )
113 {
114   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
115
116   if( widgetView )
117   {
118     if( !widgetView->IsWidgetImageView() )
119     {
120       tizen_remote_surface_transfer_visibility( surface, TIZEN_REMOTE_SURFACE_VISIBILITY_TYPE_VISIBLE);
121
122       widgetView->CreateWidgetImageView();
123       widgetView->ConnectSignal( surface );
124     }
125
126     //get tbm surface from buffer
127     tbm_surface_h tbmSurface = static_cast< tbm_surface_h >( wl_buffer_get_user_data( buffer ) );
128     widgetView->UpdateImageSource( tbmSurface );
129   }
130 }
131
132 static void OnSurfaceMissing( struct tizen_remote_surface* surface, void* data )
133 {
134   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
135
136   if( widgetView )
137   {
138     widgetView->CloseRemoteSurface();
139   }
140 }
141
142 } // unnamed namespace
143
144 Dali::WidgetView::WidgetView WidgetView::New( const std::string& widgetId, const std::string& contentInfo, int width, int height, float updatePeriod )
145 {
146   // Create the implementation, temporarily owned on stack
147   IntrusivePtr< WidgetView > internalWidgetView = new WidgetView( widgetId, contentInfo, width, height, updatePeriod );
148
149   // Pass ownership to CustomActor
150   Dali::WidgetView::WidgetView widgetView( *internalWidgetView );
151
152   // Second-phase init of the implementation
153   // This can only be done after the CustomActor connection has been made...
154   internalWidgetView->Initialize();
155
156   return widgetView;
157 }
158
159 WidgetView::WidgetView()
160 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
161   mWidgetId(),
162   mInstanceId(),
163   mContentInfo(),
164   mTitle(),
165   mWidth( 0 ),
166   mHeight( 0 ),
167   mPid( 0 ),
168   mUpdatePeriod( 0.0 ),
169   mPreviewVisible( true ),
170   mStateTextVisible( true ),
171   mPermanentDelete( false ),
172   mWatcherHandle( NULL ),
173   mRemoteSurface( NULL )
174 {
175 }
176
177 WidgetView::WidgetView( const std::string& widgetId, const std::string& contentInfo, int width, int height, float updatePeriod )
178 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
179   mWidgetId( widgetId ),
180   mInstanceId(),
181   mContentInfo( contentInfo ),
182   mTitle(),
183   mWidth( width ),
184   mHeight( height ),
185   mPid( 0 ),
186   mUpdatePeriod( updatePeriod ),
187   mPreviewVisible( true ),
188   mStateTextVisible( true ),
189   mPermanentDelete( false ),
190   mWatcherHandle( NULL ),
191   mRemoteSurface( NULL )
192 {
193 }
194
195 WidgetView::~WidgetView()
196 {
197   if( !mWidgetId.empty() && !mInstanceId.empty() )
198   {
199     widget_instance_terminate( mInstanceId.c_str() );
200
201     if( mPermanentDelete )
202     {
203       widget_instance_destroy( mInstanceId.c_str() );
204     }
205   }
206 }
207
208 bool WidgetView::PauseWidget()
209 {
210   int ret = widget_instance_pause( mInstanceId.c_str() );
211   if( ret < 0 )
212   {
213     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::PauseWidget: Fail to pause widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
214     return false;
215   }
216
217   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::PauseWidget: Widget is paused (%s, %s)\n", mWidgetId.c_str(), mInstanceId.c_str() );
218
219   return true;
220 }
221
222 bool WidgetView::ResumeWidget()
223 {
224   int ret = widget_instance_resume( mInstanceId.c_str() );
225   if( ret < 0 )
226   {
227     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ResumeWidget: Fail to resume widget(%s, %s) [%d]\n", mWidgetId.c_str(), mInstanceId.c_str(), ret );
228     return false;
229   }
230
231   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ResumeWidget: Widget is resumed (%s, %s)\n", mWidgetId.c_str(), mInstanceId.c_str() );
232
233   return true;
234 }
235
236 const std::string& WidgetView::GetWidgetId() const
237 {
238   return mWidgetId;
239 }
240
241 const std::string& WidgetView::GetInstanceId() const
242 {
243   return mInstanceId;
244 }
245
246 const std::string& WidgetView::GetContentInfo()
247 {
248   widget_instance_h instance;
249   char* contentInfo = NULL;
250
251   mContentInfo.clear();
252
253   if( mWidgetId.empty() || mInstanceId.empty() )
254   {
255     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: Widget id (%s) or instance id (%s) is invalid.\n", mWidgetId.c_str(), mInstanceId.c_str() );
256     return mContentInfo;
257   }
258
259   instance = widget_instance_get_instance( mWidgetId.c_str(), mInstanceId.c_str() );
260   if( !instance )
261   {
262     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: widget_instance_get_instance is failed. [%s]\n", mInstanceId.c_str() );
263     return mContentInfo;
264   }
265
266   if( widget_instance_get_content( instance, &contentInfo ) < 0 )
267   {
268     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetContentInfo: Failed to get content of widget. [%s]\n", mInstanceId.c_str() );
269     return mContentInfo;
270   }
271
272   mContentInfo = reinterpret_cast< char* >( contentInfo );
273
274   return mContentInfo;
275 }
276
277 const std::string& WidgetView::GetTitle()
278 {
279   //ToDo: We should add some codes by considering widget_viewer_evas
280   if( mTitle.empty() )
281   {
282     mTitle = widget_service_get_name( mWidgetId.c_str(), NULL );
283   }
284
285   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::GetTitle: title = %s\n", mTitle.c_str() );
286
287   return mTitle;
288 }
289
290 float WidgetView::GetUpdatePeriod() const
291 {
292   return mUpdatePeriod;
293 }
294
295 bool WidgetView::CancelTouchEvent()
296 {
297   if( mRemoteSurface )
298   {
299     tizen_remote_surface_transfer_touch_cancel( mRemoteSurface );
300     return true;
301   }
302
303   return false;
304 }
305
306 void WidgetView::ShowPreview( bool show )
307 {
308   if( mPreviewImage && show != mPreviewVisible )
309   {
310     mPreviewVisible = show;
311     mPreviewImage.SetVisible( show );
312   }
313 }
314
315 bool WidgetView::IsPreviewVisible()
316 {
317   return mPreviewVisible;
318 }
319
320 void WidgetView::ShowStateText( bool show )
321 {
322   if( mStateText && mStateTextVisible != show )
323   {
324     mStateTextVisible = show;
325     mStateText.SetVisible( show );
326   }
327 }
328
329 bool WidgetView::IsStateTextVisible()
330 {
331   return mStateTextVisible;
332 }
333
334 void WidgetView::ActivateFaultedWidget()
335 {
336   if( mPid < 0 )
337   {
338     // Esable preview and text
339     if( mPreviewVisible )
340     {
341       mPreviewImage.SetVisible( true );
342     }
343
344     if( mStateTextVisible )
345     {
346       mStateText.SetVisible( true );
347     }
348
349     // launch widget again
350     mPid = widget_instance_launch( mInstanceId.c_str(), (char *)mContentInfo.c_str(), mWidth, mHeight );
351     if( mPid < 0)
352     {
353       DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
354
355       // Emit signal
356       Dali::WidgetView::WidgetView handle( GetOwner() );
357       mWidgetCreationAbortedSignal.Emit( handle );
358
359       return;
360     }
361
362     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
363   }
364 }
365
366 bool WidgetView::IsWidgetFaulted()
367 {
368   return mPid < 0 ? true : false;
369 }
370
371 void WidgetView::SetPermanentDelete( bool permanentDelete )
372 {
373   mPermanentDelete = permanentDelete;
374 }
375
376 bool WidgetView::IsPermanentDelete()
377 {
378   return mPermanentDelete;
379 }
380
381 void WidgetView::CreateWidgetImageView()
382 {
383   Any source;
384   mImageSource = Dali::NativeImageSource::New( source );
385   Dali::NativeImage image = Dali::NativeImage::New( *mImageSource );
386
387   mWidgetImageView = Dali::Toolkit::ImageView::New( image );
388
389   mWidgetImageView.SetParentOrigin( ParentOrigin::CENTER );
390   mWidgetImageView.SetAnchorPoint( AnchorPoint::CENTER );
391   mWidgetImageView.SetSize( mWidth, mHeight );
392
393   Self().Add( mWidgetImageView );
394
395   //EFL app should be pre multiplied image.
396   mWidgetImageView.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
397
398   // Disable preview and text
399   if( mPreviewVisible )
400   {
401     mPreviewImage.SetVisible( false );
402   }
403
404   if( mStateTextVisible )
405   {
406     mStateText.SetVisible( false );
407   }
408
409   // Emit signal
410   Dali::WidgetView::WidgetView handle( GetOwner() );
411   mWidgetAddedSignal.Emit( handle );
412
413   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::AddObjectView: ObjectView is added.\n" );
414 }
415
416 void WidgetView::RemoveObjectView()
417 {
418   // Enable preview and text
419   if( mPreviewVisible )
420   {
421     mPreviewImage.SetVisible( true );
422   }
423
424   if( mStateTextVisible )
425   {
426     mStateText.SetVisible( true );
427   }
428
429   // Emit signal
430   Dali::WidgetView::WidgetView handle( GetOwner() );
431   mWidgetDeletedSignal.Emit( handle );
432
433   mWidgetImageView.Reset();
434
435   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::RemoveObjectView: ObjectView is removed.\n" );
436 }
437
438 void WidgetView::SendWidgetEvent( int event )
439 {
440   Dali::WidgetView::WidgetView handle( GetOwner() );
441
442   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::SendWidgetEvent: event = %d widget = %s\n", event,  mWidgetId.c_str() );
443
444   // Emit signal
445   switch( event )
446   {
447     case WIDGET_INSTANCE_EVENT_UPDATE:
448     {
449       mWidgetContentUpdatedSignal.Emit( handle );
450       break;
451     }
452     case WIDGET_INSTANCE_EVENT_PERIOD_CHANGED:
453     {
454       mWidgetUpdatePeriodChangedSignal.Emit( handle );
455       break;
456     }
457     case WIDGET_INSTANCE_EVENT_FAULT:
458     {
459       mWidgetFaultedSignal.Emit( handle );
460       break;
461     }
462     default:
463     {
464       break;
465     }
466   }
467 }
468
469 bool WidgetView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
470 {
471   Dali::BaseHandle handle( object );
472
473   bool connected( true );
474   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( handle );
475
476   if( strcmp( signalName.c_str(), SIGNAL_WIDGET_ADDED ) == 0 )
477   {
478     widgetView.WidgetAddedSignal().Connect( tracker, functor );
479   }
480   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_DELETED ) == 0 )
481   {
482     widgetView.WidgetDeletedSignal().Connect( tracker, functor );
483   }
484   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_CREATION_ABORTED ) == 0 )
485   {
486     widgetView.WidgetCreationAbortedSignal().Connect( tracker, functor );
487   }
488   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_CONTENT_UPDATED ) == 0 )
489   {
490     widgetView.WidgetContentUpdatedSignal().Connect( tracker, functor );
491   }
492   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_UPDATE_PERIOD_CHANGED ) == 0 )
493   {
494     widgetView.WidgetUpdatePeriodChangedSignal().Connect( tracker, functor );
495   }
496   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_FAULTED ) == 0 )
497   {
498     widgetView.WidgetFaultedSignal().Connect( tracker, functor );
499   }
500   else
501   {
502     // signalName does not match any signal
503     connected = false;
504   }
505
506   return connected;
507 }
508
509 bool WidgetView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
510 {
511   bool ret = true;
512
513   Dali::BaseHandle handle( object );
514   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( handle );
515
516   if( !widgetView )
517   {
518     return false;
519   }
520
521   WidgetView& impl = GetImplementation( widgetView );
522
523   if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_PAUSE_WIDGET ) == 0 )
524   {
525     impl.PauseWidget();
526   }
527   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_RESUME_WIDGET ) == 0 )
528   {
529     impl.ResumeWidget();
530   }
531   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_CANCEL_TOUCH_EVENT ) == 0 )
532   {
533     impl.CancelTouchEvent();
534   }
535   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_ACTIVATE_FAULTED_WIDGET ) == 0 )
536   {
537     impl.ActivateFaultedWidget();
538   }
539   else
540   {
541     ret = false;
542   }
543
544   return ret;
545 }
546
547 void WidgetView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
548 {
549   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( Dali::BaseHandle( object ) );
550
551   if( widgetView )
552   {
553     WidgetView& impl = GetImplementation( widgetView );
554
555     switch( index )
556     {
557       case Dali::WidgetView::WidgetView::Property::PREVIEW:
558       {
559         bool previewEnabled;
560         if( value.Get( previewEnabled ) )
561         {
562           impl.ShowPreview( previewEnabled );
563         }
564         break;
565       }
566       case Dali::WidgetView::WidgetView::Property::LOADING_TEXT:
567       {
568         bool textEnabled;
569         if( value.Get( textEnabled ) )
570         {
571           impl.ShowStateText( textEnabled );
572         }
573         break;
574       }
575       case Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE:
576       {
577         bool del;
578         if( value.Get( del ) )
579         {
580           impl.SetPermanentDelete( del );
581         }
582         break;
583       }
584     }
585   }
586 }
587
588 Property::Value WidgetView::GetProperty( BaseObject* object, Property::Index index )
589 {
590   Property::Value value;
591   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( Dali::BaseHandle( object ) );
592
593   if( widgetView )
594   {
595     WidgetView& impl = GetImplementation( widgetView );
596
597     switch( index )
598     {
599       case Dali::WidgetView::WidgetView::Property::WIDGET_ID:
600       {
601         value = impl.GetWidgetId();
602         break;
603       }
604       case Dali::WidgetView::WidgetView::Property::INSTANCE_ID:
605       {
606         value = impl.GetInstanceId();
607         break;
608       }
609       case Dali::WidgetView::WidgetView::Property::CONTENT_INFO:
610       {
611         value = impl.GetContentInfo();
612         break;
613       }
614       case Dali::WidgetView::WidgetView::Property::TITLE:
615       {
616         value = impl.GetTitle();
617         break;
618       }
619       case Dali::WidgetView::WidgetView::Property::UPDATE_PERIOD:
620       {
621         value = impl.GetUpdatePeriod();
622         break;
623       }
624       case Dali::WidgetView::WidgetView::Property::PREVIEW:
625       {
626         value = impl.IsPreviewVisible();
627         break;
628       }
629       case Dali::WidgetView::WidgetView::Property::LOADING_TEXT:
630       {
631         value = impl.IsStateTextVisible();
632         break;
633       }
634       case Dali::WidgetView::WidgetView::Property::WIDGET_STATE_FAULTED:
635       {
636         value = impl.IsWidgetFaulted();
637         break;
638       }
639       case Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE:
640       {
641         value = impl.IsPermanentDelete();
642         break;
643       }
644     }
645   }
646
647   return value;
648 }
649
650 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
651 {
652   return mWidgetAddedSignal;
653 }
654
655 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
656 {
657   return mWidgetDeletedSignal;
658 }
659
660 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetCreationAbortedSignal()
661 {
662   return mWidgetCreationAbortedSignal;
663 }
664
665 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetContentUpdatedSignal()
666 {
667   return mWidgetContentUpdatedSignal;
668 }
669
670 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetUpdatePeriodChangedSignal()
671 {
672   return mWidgetUpdatePeriodChangedSignal;
673 }
674
675 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetFaultedSignal()
676 {
677   return mWidgetFaultedSignal;
678 }
679
680 void WidgetView::OnInitialize()
681 {
682   char* instanceId = NULL;
683   char* previewPath = NULL;
684   std::string previewImage;
685   widget_size_type_e sizeType;
686   screen_connector_watcher_ops ops;
687
688   int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
689   if( ret < 0 || !instanceId )
690   {
691     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
692     return;
693   }
694
695   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
696                  mWidgetId.c_str(), instanceId );
697
698   mInstanceId = instanceId;
699
700   // Preview image
701   widget_service_get_size_type( mWidth, mHeight, &sizeType );
702
703   previewPath = widget_service_get_preview_image_path( mWidgetId.c_str(), sizeType );
704   if( previewPath )
705   {
706     previewImage = previewPath;
707     free( previewPath );
708   }
709   else
710   {
711     previewImage = tzplatform_getenv( TZ_SYS_SHARE );
712     previewImage.append( WIDGET_VIEW_RESOURCE_DEFAULT_IMG );
713   }
714
715   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: preview image path = %s\n", previewImage.c_str() );
716
717   mPreviewImage = Toolkit::ImageView::New( previewImage );
718
719   mPreviewImage.SetParentOrigin( ParentOrigin::CENTER );
720   mPreviewImage.SetAnchorPoint( AnchorPoint::CENTER );
721
722   if( !previewPath )
723   {
724     mPreviewImage.SetSize( mWidth, mHeight );
725   }
726
727   Self().SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
728   Self().SetSize( mWidth, mHeight );
729
730   Self().Add( mPreviewImage );
731
732   // State text
733   // TODO: use po files
734   mStateText = Toolkit::TextLabel::New( "Loading..." );
735
736   mStateText.SetParentOrigin( ParentOrigin::CENTER );
737   mStateText.SetAnchorPoint( AnchorPoint::CENTER );
738   mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
739   mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
740
741   mPreviewImage.Add( mStateText );
742
743   // launch widget
744   mPid = widget_instance_launch( instanceId, (char *)mContentInfo.c_str(), mWidth, mHeight );
745   if( mPid < 0)
746   {
747     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
748
749     // Emit signal
750     Dali::WidgetView::WidgetView handle( GetOwner() );
751     mWidgetCreationAbortedSignal.Emit( handle );
752
753     return;
754   }
755
756   ops.change_cb = OnBufferChanged;
757   ops.missing_cb = OnSurfaceMissing;
758   ops.update_cb = OnBufferUpdated;
759   mWatcherHandle = screen_connector_watcher_add(&ops, (char *)mWidgetId.c_str(), this);
760   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
761
762 }
763
764 void WidgetView::OnStageConnection( int depth )
765 {
766   Control::OnStageConnection( depth );
767 }
768
769 void WidgetView::OnStageDisconnection()
770 {
771   Control::OnStageDisconnection();
772 }
773
774 void WidgetView::OnSizeSet( const Vector3& targetSize )
775 {
776   if( mWidgetImageView )
777   {
778     mWidgetImageView.SetSize( targetSize );
779   }
780 }
781
782 bool WidgetView::IsWidgetImageView()
783 {
784   return ( mWidgetImageView )? true: false;
785 }
786
787 void WidgetView::UpdateImageSource( tbm_surface_h source )
788 {
789   mImageSource->SetSource( source );
790   Dali::Stage::GetCurrent().KeepRendering( 0.0f );
791 }
792
793 void WidgetView::ConnectSignal( tizen_remote_surface* surface )
794 {
795   if( mWidgetImageView && surface )
796   {
797     mRemoteSurface = surface;
798
799     Self().TouchSignal().Connect( this, &WidgetView::OnTouch );
800     Self().WheelEventSignal().Connect( this, &WidgetView::OnWheelEvent );
801   }
802
803 }
804
805 bool WidgetView::OnTouch( Dali::Actor actor, const Dali::TouchData& event )
806 {
807   tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
808
809   if( event.GetPointCount() == 0 || mRemoteSurface == NULL )
810   {
811     return false;
812   }
813
814   switch( event.GetState( 0 ) )
815   {
816     case Dali::PointState::UP:
817     {
818       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_UP;
819       break;
820     }
821     case Dali::PointState::DOWN:
822     {
823       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_DOWN;
824       break;
825     }
826     case Dali::PointState::MOTION:
827     {
828       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE;
829       break;
830     }
831     default:
832     {
833       return false;
834     }
835   }
836
837   int button = 1;
838
839   if( type == TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE )
840   {
841     button = 0 ;
842   }
843
844   Vector2 localPos = event.GetLocalPosition( 0 );
845
846   tizen_remote_surface_transfer_mouse_event( mRemoteSurface,
847                                              type,
848                                              0,
849                                              button,
850                                              (int)localPos.x,
851                                              (int)localPos.y,
852                                              wl_fixed_from_double( event.GetEllipseRadius( 0 ).x ),
853                                              wl_fixed_from_double( event.GetEllipseRadius( 0 ).y ),
854                                              wl_fixed_from_double( event.GetPressure( 0 ) ),
855                                              wl_fixed_from_double( event.GetAngle( 0 ).degree ),
856                                              TIZEN_INPUT_DEVICE_CLAS_TOUCHSCREEN,
857                                              TIZEN_INPUT_DEVICE_SUBCLAS_NONE,
858                                              "",
859                                              event.GetTime()
860                                            );
861   return true;
862 }
863
864 bool WidgetView::OnWheelEvent( Dali::Actor actor, const Dali::WheelEvent& event )
865 {
866   if( mRemoteSurface == NULL )
867   {
868     return false;
869   }
870   //ToDo: We should check TIZEN_INPUT_DEVICE_CLAS_MOUSE
871   tizen_remote_surface_transfer_mouse_wheel( mRemoteSurface,
872                                              event.direction,
873                                              event.z,
874                                              TIZEN_INPUT_DEVICE_CLAS_MOUSE,
875                                              TIZEN_INPUT_DEVICE_SUBCLAS_NONE,
876                                              "",
877                                              event.timeStamp
878                                            );
879   return true;
880 }
881
882 Vector3 WidgetView::GetNaturalSize()
883 {
884   Vector3 size;
885   size.x = mWidth;
886   size.y = mHeight;
887
888   if( size.x > 0 && size.y > 0 )
889   {
890     size.z = std::min( size.x, size.y );
891     return size;
892   }
893   else
894   {
895     return Control::GetNaturalSize();
896   }
897 }
898
899 float WidgetView::GetHeightForWidth( float width )
900 {
901   if( mWidth > 0 && mHeight > 0 )
902   {
903     return GetHeightForWidthBase( width );
904   }
905   else
906   {
907     return Control::GetHeightForWidthBase( width );
908   }
909 }
910
911 float WidgetView::GetWidthForHeight( float height )
912 {
913   if( mWidth > 0 && mHeight > 0 )
914   {
915     return GetWidthForHeightBase( height );
916   }
917   else
918   {
919     return Control::GetWidthForHeightBase( height );
920   }
921 }
922
923 void WidgetView::CloseRemoteSurface()
924 {
925   screen_connector_watcher_remove( mWatcherHandle );
926   mRemoteSurface = NULL;
927 }
928
929 } // namespace Internal
930
931 } // namespace WidgetView
932
933 } // namespace Dali