Added internal api for emitting delete signal
[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 const char* const FONT_STYLE( "fontStyle" );
93 const char* const TEXT_POINT_SIZE( "textPointSize" );
94 const char* const TEXT_COLOR( "textColor" );
95
96 static void OnBufferUpdated( struct tizen_remote_surface *surface, uint32_t type, struct wl_buffer *buffer,
97                               int32_t img_file_fd, uint32_t img_file_size, uint32_t time, struct wl_array *keys,
98                               const char *appid, const char *instance_id, const int pid, void *data)
99 {
100   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
101
102   if( widgetView )
103   {
104     if( !widgetView->IsWidgetImageView() )
105     {
106       tizen_remote_surface_transfer_visibility( surface, TIZEN_REMOTE_SURFACE_VISIBILITY_TYPE_VISIBLE);
107
108       widgetView->CreateWidgetImageView();
109       widgetView->ConnectSignal( surface );
110     }
111
112     //get tbm surface from buffer
113     tbm_surface_h tbmSurface = static_cast< tbm_surface_h >( wl_buffer_get_user_data( buffer ) );
114     widgetView->UpdateImageSource( tbmSurface );
115   }
116
117   if( preBuffer != NULL && tizen_remote_surface_get_version( surface ) >= TIZEN_REMOTE_SURFACE_RELEASE_SINCE_VERSION )
118   {
119     tizen_remote_surface_release( surface, preBuffer );
120   }
121
122   preBuffer = buffer;
123
124   close( img_file_fd );
125 }
126
127 static void OnBufferAdded( const char *appid, const char *instance_id, const int pid, void *data )
128 {
129 }
130
131 static void OnSurfaceRemoved( const char *appid, const char *instance_id, const int pid, void *data )
132 {
133   Dali::WidgetView::Internal::WidgetView* widgetView = static_cast< Dali::WidgetView::Internal::WidgetView* >( data );
134
135   if( widgetView )
136   {
137     widgetView->CloseRemoteSurface();
138     widgetView->RemoveWidgetImage();
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::CreateWidgetImageView: Widget image is added.\n" );
414 }
415
416 void WidgetView::RemoveWidgetImage()
417 {
418   // Emit signal
419   Dali::WidgetView::WidgetView handle( GetOwner() );
420   mWidgetDeletedSignal.Emit( handle );
421
422   mWidgetImageView.Reset();
423
424   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::RemoveWidgetImage: Widget image is removed.\n" );
425 }
426
427 void WidgetView::SendWidgetEvent( int event )
428 {
429   Dali::WidgetView::WidgetView handle( GetOwner() );
430
431   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::SendWidgetEvent: event = %d widget = %s\n", event,  mWidgetId.c_str() );
432
433   // Emit signal
434   switch( event )
435   {
436     case WIDGET_INSTANCE_EVENT_UPDATE:
437     case WIDGET_INSTANCE_EVENT_EXTRA_UPDATED:
438     {
439       mWidgetContentUpdatedSignal.Emit( handle );
440       break;
441     }
442     case WIDGET_INSTANCE_EVENT_PERIOD_CHANGED:
443     {
444       mWidgetUpdatePeriodChangedSignal.Emit( handle );
445       break;
446     }
447     case WIDGET_INSTANCE_EVENT_FAULT:
448     {
449       mWidgetFaultedSignal.Emit( handle );
450       break;
451     }
452     default:
453     {
454       break;
455     }
456   }
457 }
458
459 bool WidgetView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
460 {
461   Dali::BaseHandle handle( object );
462
463   bool connected( true );
464   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( handle );
465
466   if( strcmp( signalName.c_str(), SIGNAL_WIDGET_ADDED ) == 0 )
467   {
468     widgetView.WidgetAddedSignal().Connect( tracker, functor );
469   }
470   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_DELETED ) == 0 )
471   {
472     widgetView.WidgetDeletedSignal().Connect( tracker, functor );
473   }
474   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_CREATION_ABORTED ) == 0 )
475   {
476     widgetView.WidgetCreationAbortedSignal().Connect( tracker, functor );
477   }
478   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_CONTENT_UPDATED ) == 0 )
479   {
480     widgetView.WidgetContentUpdatedSignal().Connect( tracker, functor );
481   }
482   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_UPDATE_PERIOD_CHANGED ) == 0 )
483   {
484     widgetView.WidgetUpdatePeriodChangedSignal().Connect( tracker, functor );
485   }
486   else if( strcmp( signalName.c_str(), SIGNAL_WIDGET_FAULTED ) == 0 )
487   {
488     widgetView.WidgetFaultedSignal().Connect( tracker, functor );
489   }
490   else
491   {
492     // signalName does not match any signal
493     connected = false;
494   }
495
496   return connected;
497 }
498
499 bool WidgetView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
500 {
501   bool ret = true;
502
503   Dali::BaseHandle handle( object );
504   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( handle );
505
506   if( !widgetView )
507   {
508     return false;
509   }
510
511   WidgetView& impl = GetImplementation( widgetView );
512
513   if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_PAUSE_WIDGET ) == 0 )
514   {
515     impl.PauseWidget();
516   }
517   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_RESUME_WIDGET ) == 0 )
518   {
519     impl.ResumeWidget();
520   }
521   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_CANCEL_TOUCH_EVENT ) == 0 )
522   {
523     impl.CancelTouchEvent();
524   }
525   else if( strcmp( actionName.c_str(), ACTION_WIDGETVIEW_ACTIVATE_FAULTED_WIDGET ) == 0 )
526   {
527     impl.ActivateFaultedWidget();
528   }
529   else
530   {
531     ret = false;
532   }
533
534   return ret;
535 }
536
537 void WidgetView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
538 {
539   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( Dali::BaseHandle( object ) );
540
541   if( widgetView )
542   {
543     WidgetView& impl = GetImplementation( widgetView );
544
545     switch( index )
546     {
547       case Dali::WidgetView::WidgetView::Property::PREVIEW:
548       {
549         bool previewEnabled;
550
551         if( value.Get( previewEnabled ) )
552         {
553           impl.ShowPreview( previewEnabled );
554         }
555         break;
556       }
557       case Dali::WidgetView::WidgetView::Property::LOADING_TEXT:
558       {
559         bool textEnabled;
560         Property::Map map;
561
562         if( value.Get( textEnabled ) )
563         {
564           impl.ShowStateText( textEnabled );
565         }
566         else if( value.Get( map ) )
567         {
568           impl.SetLoadingTextPropertyMap( map );
569         }
570         break;
571       }
572       case Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE:
573       {
574         bool del;
575         if( value.Get( del ) )
576         {
577           impl.SetPermanentDelete( del );
578         }
579         break;
580       }
581     }
582   }
583 }
584
585 Property::Value WidgetView::GetProperty( BaseObject* object, Property::Index index )
586 {
587   Property::Value value;
588   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::DownCast( Dali::BaseHandle( object ) );
589
590   if( widgetView )
591   {
592     WidgetView& impl = GetImplementation( widgetView );
593
594     switch( index )
595     {
596       case Dali::WidgetView::WidgetView::Property::WIDGET_ID:
597       {
598         value = impl.GetWidgetId();
599         break;
600       }
601       case Dali::WidgetView::WidgetView::Property::INSTANCE_ID:
602       {
603         value = impl.GetInstanceId();
604         break;
605       }
606       case Dali::WidgetView::WidgetView::Property::CONTENT_INFO:
607       {
608         value = impl.GetContentInfo();
609         break;
610       }
611       case Dali::WidgetView::WidgetView::Property::TITLE:
612       {
613         value = impl.GetTitle();
614         break;
615       }
616       case Dali::WidgetView::WidgetView::Property::UPDATE_PERIOD:
617       {
618         value = impl.GetUpdatePeriod();
619         break;
620       }
621       case Dali::WidgetView::WidgetView::Property::PREVIEW:
622       {
623         value = impl.IsPreviewVisible();
624         break;
625       }
626       case Dali::WidgetView::WidgetView::Property::LOADING_TEXT:
627       {
628         value = impl.IsStateTextVisible();
629         break;
630       }
631       case Dali::WidgetView::WidgetView::Property::WIDGET_STATE_FAULTED:
632       {
633         value = impl.IsWidgetFaulted();
634         break;
635       }
636       case Dali::WidgetView::WidgetView::Property::PERMANENT_DELETE:
637       {
638         value = impl.IsPermanentDelete();
639         break;
640       }
641     }
642   }
643
644   return value;
645 }
646
647 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
648 {
649   return mWidgetAddedSignal;
650 }
651
652 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
653 {
654   return mWidgetDeletedSignal;
655 }
656
657 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetCreationAbortedSignal()
658 {
659   return mWidgetCreationAbortedSignal;
660 }
661
662 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetContentUpdatedSignal()
663 {
664   return mWidgetContentUpdatedSignal;
665 }
666
667 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetUpdatePeriodChangedSignal()
668 {
669   return mWidgetUpdatePeriodChangedSignal;
670 }
671
672 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetFaultedSignal()
673 {
674   return mWidgetFaultedSignal;
675 }
676
677 void WidgetView::OnInitialize()
678 {
679   char* instanceId = NULL;
680   char* previewPath = NULL;
681   std::string previewImage;
682   widget_size_type_e sizeType;
683   screen_connector_toolkit_ops ops;
684
685   int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
686   if( ret < 0 || !instanceId )
687   {
688     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
689     return;
690   }
691
692   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
693                  mWidgetId.c_str(), instanceId );
694
695   mInstanceId = instanceId;
696
697   // Preview image
698   widget_service_get_size_type( mWidth, mHeight, &sizeType );
699
700   previewPath = widget_service_get_preview_image_path( mWidgetId.c_str(), sizeType );
701   if( previewPath )
702   {
703     previewImage = previewPath;
704     free( previewPath );
705   }
706   else
707   {
708     previewImage = WIDGET_VIEW_RESOURCE_DEFAULT_IMG;
709   }
710
711   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: preview image path = %s\n", previewImage.c_str() );
712
713
714   mPreviewActor = Dali::Actor::New();
715   mPreviewActor.SetParentOrigin( ParentOrigin::CENTER );
716   mPreviewActor.SetAnchorPoint( AnchorPoint::CENTER );
717   mPreviewActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
718
719   mPreviewImage = Toolkit::ImageView::New( previewImage );
720   mPreviewImage.SetParentOrigin( ParentOrigin::CENTER );
721   mPreviewImage.SetAnchorPoint( AnchorPoint::CENTER );
722   mPreviewImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
723
724   Self().SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
725   Self().SetSize( mWidth, mHeight );
726
727   Self().Add( mPreviewActor );
728   mPreviewActor.Add( mPreviewImage );
729
730   mStateTextActor = Dali::Actor::New();
731   mStateTextActor.SetParentOrigin( ParentOrigin::CENTER );
732   mStateTextActor.SetAnchorPoint( AnchorPoint::CENTER );
733   mStateTextActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
734
735   // State text
736   // TODO: use po files
737   mStateText = Toolkit::TextLabel::New( "Loading..." );
738   mStateText.SetParentOrigin( ParentOrigin::CENTER );
739   mStateText.SetAnchorPoint( AnchorPoint::CENTER );
740   mStateText.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
741   mStateText.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
742   mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Dali::Color::WHITE );
743   mStateText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, "Bold" );
744
745   mPreviewActor.Add( mStateTextActor );
746   mStateTextActor.Add( mStateText );
747
748   // launch widget
749   mPid = widget_instance_launch( instanceId, (char *)mContentInfo.c_str(), mWidth, mHeight );
750   if( mPid < 0)
751   {
752     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
753
754     // Emit signal
755     Dali::WidgetView::WidgetView handle( GetOwner() );
756     mWidgetCreationAbortedSignal.Emit( handle );
757
758     return;
759   }
760
761   ops.updated_cb = OnBufferUpdated;
762   ops.removed_cb = OnSurfaceRemoved;
763   ops.added_cb = OnBufferAdded;
764   mWatcherHandle = screen_connector_toolkit_add(&ops, (char *)instanceId, SCREEN_CONNECTOR_SCREEEN_TYPE_WIDGET, this);
765   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
766
767 }
768
769 void WidgetView::OnStageConnection( int depth )
770 {
771   Control::OnStageConnection( depth );
772 }
773
774 void WidgetView::OnStageDisconnection()
775 {
776   Control::OnStageDisconnection();
777 }
778
779 void WidgetView::OnSizeSet( const Vector3& targetSize )
780 {
781   if( mWidgetImageView )
782   {
783     mWidgetImageView.SetSize( targetSize );
784   }
785 }
786
787 bool WidgetView::IsWidgetImageView()
788 {
789   return ( mWidgetImageView )? true: false;
790 }
791
792 void WidgetView::UpdateImageSource( tbm_surface_h source )
793 {
794   mImageSource->SetSource( source );
795   Dali::Stage::GetCurrent().KeepRendering( 0.0f );
796 }
797
798 void WidgetView::ConnectSignal( tizen_remote_surface* surface )
799 {
800   if( mWidgetImageView && surface )
801   {
802     mRemoteSurface = surface;
803
804     Self().TouchSignal().Connect( this, &WidgetView::OnTouch );
805     Self().WheelEventSignal().Connect( this, &WidgetView::OnWheelEvent );
806   }
807
808 }
809
810 bool WidgetView::OnTouch( Dali::Actor actor, const Dali::TouchData& event )
811 {
812   tizen_remote_surface_event_type type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_NONE;
813
814   if( event.GetPointCount() == 0 || mRemoteSurface == NULL )
815   {
816     return false;
817   }
818
819   switch( event.GetState( 0 ) )
820   {
821     case Dali::PointState::UP:
822     {
823       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_UP;
824       break;
825     }
826     case Dali::PointState::DOWN:
827     {
828       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_DOWN;
829       break;
830     }
831     case Dali::PointState::MOTION:
832     {
833       type = TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE;
834       break;
835     }
836     default:
837     {
838       return false;
839     }
840   }
841
842   int button = 1;
843
844   if( type == TIZEN_REMOTE_SURFACE_EVENT_TYPE_MOUSE_MOVE )
845   {
846     button = 0 ;
847   }
848
849   Vector2 localPos = event.GetLocalPosition( 0 );
850
851   tizen_remote_surface_transfer_mouse_event( mRemoteSurface,
852                                              type,
853                                              0,
854                                              button,
855                                              (int)localPos.x,
856                                              (int)localPos.y,
857                                              wl_fixed_from_double( event.GetEllipseRadius( 0 ).x ),
858                                              wl_fixed_from_double( event.GetEllipseRadius( 0 ).y ),
859                                              wl_fixed_from_double( event.GetPressure( 0 ) ),
860                                              wl_fixed_from_double( event.GetAngle( 0 ).degree ),
861                                              TIZEN_INPUT_DEVICE_CLAS_TOUCHSCREEN,
862                                              TIZEN_INPUT_DEVICE_SUBCLAS_NONE,
863                                              "",
864                                              event.GetTime()
865                                            );
866   return true;
867 }
868
869 bool WidgetView::OnWheelEvent( Dali::Actor actor, const Dali::WheelEvent& event )
870 {
871   if( mRemoteSurface == NULL )
872   {
873     return false;
874   }
875   //ToDo: We should check TIZEN_INPUT_DEVICE_CLAS_MOUSE
876   tizen_remote_surface_transfer_mouse_wheel( mRemoteSurface,
877                                              event.direction,
878                                              event.z,
879                                              TIZEN_INPUT_DEVICE_CLAS_MOUSE,
880                                              TIZEN_INPUT_DEVICE_SUBCLAS_NONE,
881                                              "",
882                                              event.timeStamp
883                                            );
884   return true;
885 }
886
887 Vector3 WidgetView::GetNaturalSize()
888 {
889   Vector3 size;
890   size.x = mWidth;
891   size.y = mHeight;
892
893   if( size.x > 0 && size.y > 0 )
894   {
895     size.z = std::min( size.x, size.y );
896     return size;
897   }
898   else
899   {
900     return Control::GetNaturalSize();
901   }
902 }
903
904 float WidgetView::GetHeightForWidth( float width )
905 {
906   if( mWidth > 0 && mHeight > 0 )
907   {
908     return GetHeightForWidthBase( width );
909   }
910   else
911   {
912     return Control::GetHeightForWidthBase( width );
913   }
914 }
915
916 float WidgetView::GetWidthForHeight( float height )
917 {
918   if( mWidth > 0 && mHeight > 0 )
919   {
920     return GetWidthForHeightBase( height );
921   }
922   else
923   {
924     return Control::GetWidthForHeightBase( height );
925   }
926 }
927
928 void WidgetView::CloseRemoteSurface()
929 {
930   screen_connector_toolkit_remove( mWatcherHandle );
931   mRemoteSurface = NULL;
932 }
933
934 void WidgetView::SetLoadingTextPropertyMap( Property::Map map )
935 {
936   if( mStateText )
937   {
938     Property::Value* fontStyle = map.Find( FONT_STYLE );
939     std::string style;
940
941     if( fontStyle && fontStyle->Get( style ) )
942     {
943       mStateText.SetProperty( Toolkit::TextLabel::Property::FONT_STYLE, style );
944     }
945
946     Property::Value* textPointSize = map.Find( TEXT_POINT_SIZE );
947     float size = 0.f;
948
949     if( textPointSize && textPointSize->Get( size ) )
950     {
951       mStateText.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, size );
952     }
953
954     Property::Value* textColor = map.Find( TEXT_COLOR );
955     Vector4 color;
956
957     if( textColor && textColor->Get( color ) )
958     {
959       mStateText.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, color );
960     }
961   }
962 }
963
964 } // namespace Internal
965
966 } // namespace WidgetView
967
968 } // namespace Dali