Add ObjectView::SetVisibilityState() API.
[platform/core/uifw/pepper-dali.git] / pepper-dali / internal / object-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 <pepper-dali/internal/object-view-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <pepper-dali/internal/shell-client-impl.h>
23 #include <pepper-dali/internal/extensions/tizen-policy.h>
24
25 // EXTERNAL INCLUDES
26 #include <dali/public-api/events/touch-event.h>
27 #include <dali/integration-api/debug.h>
28
29 namespace Dali
30 {
31
32 namespace Pepper
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 #if defined(DEBUG_ENABLED)
42 Integration::Log::Filter* gPepperObjectViewLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_OBJECT_VIEW" );
43 #endif
44
45 } // unnamed namespace
46
47 Dali::Pepper::ObjectView ObjectView::New()
48 {
49   // Create the implementation, temporarily owned on stack
50   IntrusivePtr< ObjectView > internalObjectView = new ObjectView();
51
52   // Pass ownership to CustomActor
53   Dali::Pepper::ObjectView objectView( *internalObjectView );
54
55   // Second-phase init of the implementation
56   // This can only be done after the CustomActor connection has been made...
57   internalObjectView->Initialize();
58
59   return objectView;
60 }
61
62 ObjectView::ObjectView()
63 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS ) ),
64   mWidth( 0 ),
65   mHeight( 0 ),
66   mTouchDown( false ),
67   mSurface( NULL ),
68   mPointer( NULL ),
69   mKeyboard( NULL ),
70   mTouch( NULL )
71 {
72 }
73
74 ObjectView::~ObjectView()
75 {
76 }
77
78 void ObjectView::SetImage( Image image )
79 {
80   mImageView.SetImage( image );
81
82   DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::SetImage: size = %d * %d\n", image.GetWidth(), image.GetHeight() );
83 }
84
85 pid_t ObjectView::GetPid() const
86 {
87   pid_t pid = 0;
88   struct wl_client* client;
89
90   if( mSurface )
91   {
92     client = wl_resource_get_client( pepper_surface_get_resource( mSurface ) );
93     if( client )
94     {
95       wl_client_get_credentials( client, &pid, NULL, NULL );
96     }
97   }
98
99   DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::GetPid: pid = %d\n", pid );
100
101   return pid;
102 }
103
104 std::string ObjectView::GetTitle() const
105 {
106   std::string title;
107
108   if( mSurface )
109   {
110     Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
111     if( shellClient )
112     {
113       title = shellClient->GetTitle();
114     }
115   }
116
117   DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::GetTitle: title = %s\n", title.c_str() );
118
119   return title;
120 }
121
122 std::string ObjectView::GetAppId() const
123 {
124   std::string appId;
125
126   if( mSurface )
127   {
128     Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
129     if( shellClient )
130     {
131       appId = shellClient->GetAppId();
132     }
133   }
134
135   DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::GetAppId: app id = %s\n", appId.c_str() );
136
137   return appId;
138 }
139
140 bool ObjectView::CancelTouchEvent()
141 {
142   if( !mTouchDown )
143   {
144     return false;
145   }
146
147   Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
148   if( !shellClient )
149   {
150     DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::CancelTouchEvent: shellClient is null. mSurface = %p\n", mSurface );
151     return false;
152   }
153
154   pepper_touch_send_cancel( mTouch, shellClient->GetView() );
155   pepper_touch_remove_point( mTouch, 0 );
156
157   mTouchDown = false;
158
159   return true;
160 }
161
162 void ObjectView::Show()
163 {
164   if( mSurface )
165   {
166     Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
167     if( shellClient )
168     {
169       shellClient->MapSurface();
170     }
171   }
172
173   Self().SetVisible( true );
174 }
175
176 void ObjectView::Hide()
177 {
178   if( mSurface )
179   {
180     Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
181     if( shellClient )
182     {
183       shellClient->UnmapSurface();
184     }
185   }
186
187   Self().SetVisible( false );
188 }
189
190 void ObjectView::SetVisibilityState( Pepper::ObjectView::VisibilityState state )
191 {
192   Pepper::Internal::Extension::VisibilityState visibilityState;
193
194   switch( state )
195   {
196     case Pepper::ObjectView::UNOBSCURED:
197     {
198       visibilityState = Pepper::Internal::Extension::UNOBSCURED;
199       break;
200     }
201     case Pepper::ObjectView::PARTIALLY_OBSCURED:
202     {
203       visibilityState = Pepper::Internal::Extension::PARTIALLY_OBSCURED;
204       break;
205     }
206     case Pepper::ObjectView::FULLY_OBSCURED:
207     {
208       visibilityState = Pepper::Internal::Extension::FULLY_OBSCURED;
209       break;
210     }
211   }
212
213   Pepper::Internal::Extension::TizenPolicySetVisibility( mSurface, visibilityState );
214
215   DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::SetVisibilityState: state = %d\n", state );
216 }
217
218 void ObjectView::SetSurface( pepper_surface_t* surface )
219 {
220   mSurface = surface;
221 }
222
223 void ObjectView::SetInput( pepper_pointer_t* pointer, pepper_keyboard_t* keyboard, pepper_touch_t* touch )
224 {
225   mPointer = pointer;
226   mKeyboard = keyboard;
227   mTouch = touch;
228 }
229
230 void ObjectView::OnInitialize()
231 {
232   mImageView = Toolkit::ImageView::New();
233   mImageView.SetParentOrigin( ParentOrigin::CENTER );
234   mImageView.SetAnchorPoint( AnchorPoint::CENTER );
235
236   Self().Add( mImageView );
237   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
238
239   // TODO: support multi touch, focus in/out, mouse in/out
240 }
241
242 bool ObjectView::OnTouchEvent( const TouchEvent& touchEvent )
243 {
244   if( 1 == touchEvent.GetPointCount() )
245   {
246     const TouchPoint& point = touchEvent.GetPoint(0);
247
248     Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
249     if( !shellClient )
250     {
251       DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::OnTouchEvent: shellClient is null. mSurface = %p\n", mSurface );
252       return false;
253     }
254
255     switch( point.state )
256     {
257       case TouchPoint::Down:
258       {
259         mTouchDown = true;
260
261         pepper_touch_add_point( mTouch, point.deviceId, point.local.x, point.local.y );
262         pepper_touch_send_down( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId, point.local.x, point.local.y );
263
264         DALI_LOG_INFO( gPepperObjectViewLogging, Debug::Verbose, "ObjectView::OnTouchEvent: Touch Down (%.2f, %.2f) device = %d surface = %p\n", point.local.x, point.local.y, point.deviceId, mSurface );
265         return true;
266       }
267       case TouchPoint::Up:
268       {
269         if( mTouchDown )
270         {
271           mTouchDown = false;
272
273           pepper_touch_send_up( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId );
274           pepper_touch_remove_point( mTouch, point.deviceId );
275
276           DALI_LOG_INFO( gPepperObjectViewLogging, Debug::Verbose, "ObjectView::OnTouchEvent: Touch Up (%.2f, %.2f) surface = %p\n", point.local.x, point.local.y, mSurface );
277           return true;
278         }
279         break;
280       }
281       case TouchPoint::Motion:
282       {
283         pepper_touch_send_motion( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId, point.local.x, point.local.y );
284
285 //        DALI_LOG_INFO( gPepperObjectViewLogging, Debug::Verbose, "ObjectView::OnTouchEvent: Touch Motion (%.2f, %.2f)\n", point.local.x, point.local.y );
286         return true;
287       }
288       default:
289       {
290         return false;
291       }
292     }
293   }
294
295   return false;
296 }
297
298 void ObjectView::OnSizeSet( const Vector3& targetSize )
299 {
300   if( mSurface )
301   {
302     Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
303     if( shellClient )
304     {
305       shellClient->Configure( static_cast< int >( targetSize.width ), static_cast< int >( targetSize.height ), ObjectView::OnConfigureCallback, this );
306     }
307   }
308
309   DALI_LOG_INFO( gPepperObjectViewLogging, Debug::Verbose, "ObjectView::OnSizeSet:width = %.2f height = %.2f\n", targetSize.width, targetSize.height );
310 }
311
312 void ObjectView::OnConfigureCallback( void* data, int width, int height )
313 {
314   ObjectView* objectView = static_cast< ObjectView* >( data );
315
316   objectView->mImageView.SetSize( static_cast< float >( width ), static_cast< float >( height ) );
317
318   DALI_LOG_INFO( gPepperObjectViewLogging, Debug::Verbose, "ObjectView::OnConfigureCallback:width = %d height = %d\n", width, height );
319 }
320
321 } // namespace Internal
322
323 } // namespace Pepper
324
325 } // namespace Dali