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