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