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