Add ObjectView::CancelTouchEvent()
[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 bool ObjectView::OnTouchEvent( const TouchEvent& touchEvent )
174 {
175   if( 1 == touchEvent.GetPointCount() )
176   {
177     const TouchPoint& point = touchEvent.GetPoint(0);
178
179     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 ) ) );
180     if( !shellClient )
181     {
182       DALI_LOG_INFO( gPepperObjectViewLogging, Debug::General, "ObjectView::OnTouchEvent: shellClient is null. mSurface = %p\n", mSurface );
183       return false;
184     }
185
186     switch( point.state )
187     {
188       case TouchPoint::Down:
189       {
190         mTouchDown = true;
191
192         pepper_touch_add_point( mTouch, point.deviceId, point.local.x, point.local.y );
193         pepper_touch_send_down( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId, point.local.x, point.local.y );
194
195         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 );
196         return true;
197       }
198       case TouchPoint::Up:
199       {
200         if( mTouchDown )
201         {
202           mTouchDown = false;
203
204           pepper_touch_send_up( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId );
205           pepper_touch_remove_point( mTouch, point.deviceId );
206
207           DALI_LOG_INFO( gPepperObjectViewLogging, Debug::Verbose, "ObjectView::OnTouchEvent: Touch Up (%.2f, %.2f) surface = %p\n", point.local.x, point.local.y, mSurface );
208           return true;
209         }
210         break;
211       }
212       case TouchPoint::Motion:
213       {
214         pepper_touch_send_motion( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId, point.local.x, point.local.y );
215
216 //        DALI_LOG_INFO( gPepperObjectViewLogging, Debug::Verbose, "ObjectView::OnTouchEvent: Touch Motion (%.2f, %.2f)\n", point.local.x, point.local.y );
217         return true;
218       }
219       default:
220       {
221         return false;
222       }
223     }
224   }
225
226   return false;
227 }
228
229 void ObjectView::OnInitialize()
230 {
231   mImageView = Toolkit::ImageView::New();
232   mImageView.SetParentOrigin( ParentOrigin::CENTER );
233   mImageView.SetAnchorPoint( AnchorPoint::CENTER );
234
235   Self().Add( mImageView );
236   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
237 }
238
239 } // namespace Internal
240
241 } // namespace Pepper
242
243 } // namespace Dali