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