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