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