Add widget_viewer_dali
[platform/core/uifw/widget-viewer-dali.git] / internal / widget_view / widget_view_impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
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/integration-api/debug.h>
25 #include <string.h>
26 #include <widget_service.h>
27 #include <widget_instance.h>
28
29 namespace Dali
30 {
31
32 namespace WidgetView
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 #if defined(DEBUG_ENABLED)
42 Integration::Log::Filter* gWidgetViewLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW" );
43 #endif
44
45 } // unnamed namespace
46
47 Dali::WidgetView::WidgetView WidgetView::New( const std::string& widgetId, const std::string& contentInfo, int width, int height, double period )
48 {
49   // Create the implementation, temporarily owned on stack
50   IntrusivePtr< WidgetView > internalWidgetView = new WidgetView( widgetId, contentInfo, width, height, period );
51
52   // Pass ownership to CustomActor
53   Dali::WidgetView::WidgetView widgetView( *internalWidgetView );
54
55   // Second-phase init of the implementation
56   // This can only be done after the CustomActor connection has been made...
57   internalWidgetView->Initialize();
58
59   return widgetView;
60 }
61
62 WidgetView::WidgetView()
63 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
64   mWidgetId(),
65   mInstanceId(),
66   mContentInfo(),
67   mTitle(),
68   mBundle( NULL ),
69   mWidth( 0 ),
70   mHeight( 0 ),
71   mPid( 0 ),
72   mPeriod( 0.0 ),
73   mPermanentDelete( true )
74 {
75 }
76
77 WidgetView::WidgetView( const std::string& widgetId, const std::string& contentInfo, int width, int height, double period )
78 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
79   mWidgetId( widgetId ),
80   mInstanceId(),
81   mContentInfo( contentInfo ),
82   mTitle(),
83   mBundle( NULL ),
84   mWidth( width ),
85   mHeight( height ),
86   mPid( 0 ),
87   mPeriod( period ),
88   mPermanentDelete( true )
89 {
90 }
91
92 WidgetView::~WidgetView()
93 {
94   if( !mWidgetId.empty() && !mInstanceId.empty() )
95   {
96     widget_instance_terminate( mWidgetId.c_str(), mInstanceId.c_str() );
97
98     if( mPermanentDelete )
99     {
100       widget_instance_destroy( mWidgetId.c_str(), mInstanceId.c_str() );
101     }
102   }
103
104   if( mBundle )
105   {
106     bundle_free( mBundle );
107   }
108 }
109
110 const std::string& WidgetView::GetWidgetId() const
111 {
112   return mWidgetId;
113 }
114
115 const std::string& WidgetView::GetInstanceId() const
116 {
117   return mInstanceId;
118 }
119
120 const std::string& WidgetView::GetContentInfo() const
121 {
122   return mContentInfo;
123 }
124
125 const std::string& WidgetView::GetTitle() const
126 {
127   return mTitle;
128 }
129
130 double WidgetView::GetPeriod() const
131 {
132   return mPeriod;
133 }
134
135 void WidgetView::ActivateFaultedWidget()
136 {
137   if( mPid < 0 )
138   {
139     // launch widget again
140     mPid = widget_instance_launch( mWidgetId.c_str(), mInstanceId.c_str(), mBundle, mWidth, mHeight );
141     if( mPid < 0)
142     {
143       DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
144       return;
145     }
146
147     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::ActivateFaultedWidget: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
148   }
149 }
150
151 bool WidgetView::IsWidgetFaulted()
152 {
153   return mPid < 0 ? true : false;
154 }
155
156 void WidgetView::SetPermanentDelete( bool permanentDelete )
157 {
158   mPermanentDelete = permanentDelete;
159 }
160
161 void WidgetView::AddObjectView( Pepper::ObjectView objectView )
162 {
163   mObjectView = objectView;
164
165   mObjectView.SetParentOrigin( ParentOrigin::CENTER );
166   mObjectView.SetAnchorPoint( AnchorPoint::CENTER );
167
168   Self().Add( mObjectView );
169   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
170
171   mTitle = mObjectView.GetTitle();
172
173   // Emit signal
174   Dali::WidgetView::WidgetView handle( GetOwner() );
175   mWidgetAddedSignal.Emit( handle );
176 }
177
178 void WidgetView::RemoveObjectView()
179 {
180   // Emit signal
181   Dali::WidgetView::WidgetView handle( GetOwner() );
182   mWidgetDeletedSignal.Emit( handle );
183
184   mObjectView.Reset();
185 }
186
187 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetAddedSignal()
188 {
189   return mWidgetAddedSignal;
190 }
191
192 Dali::WidgetView::WidgetView::WidgetViewSignalType& WidgetView::WidgetDeletedSignal()
193 {
194   return mWidgetDeletedSignal;
195 }
196
197 void WidgetView::OnInitialize()
198 {
199   char* instanceId = NULL;
200
201   if( !mContentInfo.empty() )
202   {
203     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: decode bundle\n" );
204
205     mBundle = bundle_decode( reinterpret_cast< const bundle_raw* >( mContentInfo.c_str() ), mContentInfo.length() );
206     if( !mBundle )
207     {
208       DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: Invalid bundle data.\n" );
209       return;
210     }
211
212     bundle_get_str( mBundle, WIDGET_K_INSTANCE, &instanceId );
213   }
214
215   if( !instanceId )
216   {
217     int ret = widget_instance_create( mWidgetId.c_str(), &instanceId );
218     if( ret < 0 || !instanceId )
219     {
220       DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is failed [%s].\n", mWidgetId.c_str() );
221       return;
222     }
223
224     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_create is called. [widget id = %s, instance id = %s]\n",
225                    mWidgetId.c_str(), instanceId );
226   }
227
228   mInstanceId = instanceId;
229
230   // launch widget
231   mPid = widget_instance_launch( mWidgetId.c_str(), instanceId, mBundle, mWidth, mHeight );
232   if( mPid < 0)
233   {
234     DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is failed. [%s]\n", mWidgetId.c_str() );
235     return;
236   }
237
238   DALI_LOG_INFO( gWidgetViewLogging, Debug::Verbose, "WidgetView::OnInitialize: widget_instance_launch is called. [%s, mPid = %d]\n", mWidgetId.c_str(), mPid );
239 }
240
241 } // namespace Internal
242
243 } // namespace WidgetView
244
245 } // namespace Dali