Fix build error when using --enable-debug option
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / event-handler-wl.cpp
1 /*
2  * Copyright (c) 2015 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 <events/event-handler.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/events/touch-point.h>
26 #include <dali/public-api/events/key-event.h>
27 #include <dali/public-api/events/wheel-event.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/integration-api/events/key-event-integ.h>
30 #include <dali/integration-api/events/touch-event-integ.h>
31 #include <dali/integration-api/events/hover-event-integ.h>
32 #include <dali/integration-api/events/wheel-event-integ.h>
33
34
35 // INTERNAL INCLUDES
36 #include <render-surface/render-surface-wl.h>
37 #include <events/gesture-manager.h>
38 #include <key-impl.h>
39 #include <clipboard.h>
40 #include <physical-keyboard-impl.h>
41 #include <style-monitor-impl.h>
42 #include <base/core-event-interface.h>
43 #include <base/interfaces/window-event-interface.h>
44
45
46
47 namespace Dali
48 {
49
50 namespace Internal
51 {
52
53 namespace Adaptor
54 {
55
56 #if defined(DEBUG_ENABLED)
57 namespace
58 {
59   Integration::Log::Filter* gTouchEventLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
60 } // unnamed namespace
61 #endif
62
63 struct EventHandler::Impl : public WindowEventInterface
64 {
65   // Construction & Destruction
66
67   /**
68    * Constructor
69    */
70   Impl( EventHandler* handler )
71   : mHandler( handler ),
72     mPaused( false )
73   {
74   }
75   /**
76    * Destructor
77    */
78   ~Impl()
79   {
80   }
81   // @todo Consider allowing the EventHandler class to inherit from WindowEventInterface directly
82   virtual void TouchEvent( Dali::Integration::Point& point, unsigned long timeStamp )
83   {
84     mHandler->SendEvent( point, timeStamp );
85   }
86   virtual void KeyEvent( Dali::KeyEvent& keyEvent )
87   {
88     mHandler->SendEvent( keyEvent );
89   }
90   virtual void WheelEvent( Dali::WheelEvent& wheelEvent )
91   {
92     mHandler->SendWheelEvent( wheelEvent );
93   }
94   virtual void DamageEvent( Rect<int>& damageArea )
95   {
96     mHandler->SendEvent( damageArea );
97   }
98   virtual void WindowFocusOut( )
99   {
100     // used to do some work with ime
101   }
102   virtual void WindowFocusIn()
103   {
104     // used to do some work with ime
105   }
106   // Data
107   EventHandler* mHandler;
108   bool mPaused;
109 };
110
111 /**
112  * @TODO the event handler code seems to be common across all adaptors, could do with moving into common
113  *
114  */
115 EventHandler::EventHandler( RenderSurface* surface, CoreEventInterface& coreEventInterface, GestureManager& gestureManager, DamageObserver& damageObserver, DragAndDropDetectorPtr dndDetector )
116 : mCoreEventInterface(coreEventInterface),
117   mGestureManager( gestureManager ),
118   mStyleMonitor( StyleMonitor::Get() ),
119   mDamageObserver( damageObserver ),
120   mRotationObserver( NULL ),
121   mDragAndDropDetector( dndDetector ),
122   mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
123   mClipboard( Dali::Clipboard::Get()),
124   mImpl( NULL )
125 {
126
127
128   // this code only works with the wayland RenderSurface so need to downcast
129   Wayland::RenderSurface* waylandSurface = dynamic_cast< Wayland::RenderSurface* >( surface );
130
131   mImpl = new Impl(this );
132
133   if( waylandSurface )
134   {
135     waylandSurface->AssignWindowEventInterface( mImpl );
136   }
137 }
138
139 EventHandler::~EventHandler()
140 {
141   if(mImpl)
142   {
143     delete mImpl;
144   }
145
146   mGestureManager.Stop();
147 }
148
149 void EventHandler::SendEvent(Integration::Point& point, unsigned long timeStamp)
150 {
151
152   Integration::TouchEvent touchEvent;
153   Integration::HoverEvent hoverEvent;
154   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
155   if(type != Integration::TouchEventCombiner::DispatchNone )
156   {
157     DALI_LOG_INFO(gTouchEventLogFilter, Debug::General, "%d: Device %d: Button state %d (%.2f, %.2f)\n", timeStamp, point.GetDeviceId(), point.GetState(), point.GetLocalPosition().x, point.GetLocalPosition().y);
158
159     // First the touch and/or hover event & related gesture events are queued
160     if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth)
161     {
162       mCoreEventInterface.QueueCoreEvent( touchEvent );
163       mGestureManager.SendEvent(touchEvent);
164     }
165
166     if(type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth)
167     {
168       mCoreEventInterface.QueueCoreEvent( hoverEvent );
169     }
170
171     // Next the events are processed with a single call into Core
172     mCoreEventInterface.ProcessCoreEvents();
173   }
174 }
175
176 void EventHandler::SendEvent(KeyEvent& keyEvent)
177 {
178   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
179   if ( physicalKeyboard )
180   {
181     if ( ! KeyLookup::IsDeviceButton( keyEvent.keyPressedName.c_str() ) )
182     {
183       GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
184     }
185   }
186
187   // Create KeyEvent and send to Core.
188   Integration::KeyEvent event(keyEvent.keyPressedName, keyEvent.keyPressed, keyEvent.keyCode,
189   keyEvent.keyModifier, keyEvent.time, static_cast<Integration::KeyEvent::State>(keyEvent.state));
190   mCoreEventInterface.QueueCoreEvent( event );
191   mCoreEventInterface.ProcessCoreEvents();
192 }
193
194 void EventHandler::SendWheelEvent( WheelEvent& wheelEvent )
195 {
196   // Create WheelEvent and send to Core.
197   Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
198   mCoreEventInterface.QueueCoreEvent( event );
199   mCoreEventInterface.ProcessCoreEvents();
200 }
201
202 void EventHandler::SendEvent( StyleChange::Type styleChange )
203 {
204   DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
205   GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
206 }
207
208 void EventHandler::SendEvent( const DamageArea& area )
209 {
210   mDamageObserver.OnDamaged( area );
211 }
212
213 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
214 {
215   if( mRotationObserver != NULL )
216   {
217     mRotationObserver->OnRotationPrepare( event );
218   }
219 }
220
221 void EventHandler::SendRotationRequestEvent( )
222 {
223   if( mRotationObserver != NULL )
224   {
225     mRotationObserver->OnRotationRequest( );
226   }
227 }
228
229 void EventHandler::FeedTouchPoint( TouchPoint& point, int timeStamp)
230 {
231   Integration::Point convertedPoint( point );
232   SendEvent(convertedPoint, timeStamp);
233 }
234
235 void EventHandler::FeedWheelEvent( WheelEvent& wheelEvent )
236 {
237   SendWheelEvent( wheelEvent );
238 }
239
240 void EventHandler::FeedKeyEvent( KeyEvent& event )
241 {
242   SendEvent( event );
243 }
244
245 void EventHandler::FeedEvent( Integration::Event& event )
246 {
247   mCoreEventInterface.QueueCoreEvent( event );
248   mCoreEventInterface.ProcessCoreEvents();
249 }
250
251 void EventHandler::Reset()
252 {
253   mCombiner.Reset();
254
255   // Any touch listeners should be told of the interruption.
256   Integration::TouchEvent event;
257   Integration::Point point;
258   point.SetState( PointState::INTERRUPTED );
259   event.AddPoint( point );
260
261   // First the touch event & related gesture events are queued
262   mCoreEventInterface.QueueCoreEvent( event );
263   mGestureManager.SendEvent( event );
264
265   // Next the events are processed with a single call into Core
266   mCoreEventInterface.ProcessCoreEvents();
267 }
268
269 void EventHandler::Pause()
270 {
271   mPaused = true;
272   Reset();
273 }
274
275 void EventHandler::Resume()
276 {
277   mPaused = false;
278   Reset();
279 }
280
281 void EventHandler::SetDragAndDropDetector( DragAndDropDetectorPtr detector )
282 {
283   mDragAndDropDetector = detector;
284 }
285
286 void EventHandler::SetRotationObserver( RotationObserver* observer )
287 {
288   mRotationObserver = observer;
289 }
290
291 } // namespace Adaptor
292
293 } // namespace Internal
294
295 } // namespace Dali