Merge "Remove the wearable definition in Makefile.am" into devel/master
[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   mPaused( false )
126 {
127
128
129   // this code only works with the wayland RenderSurface so need to downcast
130   Wayland::RenderSurface* waylandSurface = dynamic_cast< Wayland::RenderSurface* >( surface );
131
132   mImpl = new Impl(this );
133
134   if( waylandSurface )
135   {
136     waylandSurface->AssignWindowEventInterface( mImpl );
137   }
138 }
139
140 EventHandler::~EventHandler()
141 {
142   if(mImpl)
143   {
144     delete mImpl;
145   }
146
147   mGestureManager.Stop();
148 }
149
150 void EventHandler::SendEvent(Integration::Point& point, unsigned long timeStamp)
151 {
152
153   Integration::TouchEvent touchEvent;
154   Integration::HoverEvent hoverEvent;
155   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
156   if(type != Integration::TouchEventCombiner::DispatchNone )
157   {
158     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);
159
160     // First the touch and/or hover event & related gesture events are queued
161     if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth)
162     {
163       mCoreEventInterface.QueueCoreEvent( touchEvent );
164       mGestureManager.SendEvent(touchEvent);
165     }
166
167     if(type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth)
168     {
169       mCoreEventInterface.QueueCoreEvent( hoverEvent );
170     }
171
172     // Next the events are processed with a single call into Core
173     mCoreEventInterface.ProcessCoreEvents();
174   }
175 }
176
177 void EventHandler::SendEvent(KeyEvent& keyEvent)
178 {
179   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
180   if ( physicalKeyboard )
181   {
182     if ( ! KeyLookup::IsDeviceButton( keyEvent.keyPressedName.c_str() ) )
183     {
184       GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
185     }
186   }
187
188   // Create KeyEvent and send to Core.
189   Integration::KeyEvent event(keyEvent.keyPressedName, keyEvent.keyPressed, keyEvent.keyCode,
190   keyEvent.keyModifier, keyEvent.time, static_cast<Integration::KeyEvent::State>(keyEvent.state));
191   mCoreEventInterface.QueueCoreEvent( event );
192   mCoreEventInterface.ProcessCoreEvents();
193 }
194
195 void EventHandler::SendWheelEvent( WheelEvent& wheelEvent )
196 {
197   // Create WheelEvent and send to Core.
198   Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
199   mCoreEventInterface.QueueCoreEvent( event );
200   mCoreEventInterface.ProcessCoreEvents();
201 }
202
203 void EventHandler::SendEvent( StyleChange::Type styleChange )
204 {
205   DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
206   GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
207 }
208
209 void EventHandler::SendEvent( const DamageArea& area )
210 {
211   mDamageObserver.OnDamaged( area );
212 }
213
214 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
215 {
216   if( mRotationObserver != NULL )
217   {
218     mRotationObserver->OnRotationPrepare( event );
219   }
220 }
221
222 void EventHandler::SendRotationRequestEvent( )
223 {
224   if( mRotationObserver != NULL )
225   {
226     mRotationObserver->OnRotationRequest( );
227   }
228 }
229
230 void EventHandler::FeedTouchPoint( TouchPoint& point, int timeStamp)
231 {
232   Integration::Point convertedPoint( point );
233   SendEvent(convertedPoint, timeStamp);
234 }
235
236 void EventHandler::FeedWheelEvent( WheelEvent& wheelEvent )
237 {
238   SendWheelEvent( wheelEvent );
239 }
240
241 void EventHandler::FeedKeyEvent( KeyEvent& event )
242 {
243   SendEvent( event );
244 }
245
246 void EventHandler::FeedEvent( Integration::Event& event )
247 {
248   mCoreEventInterface.QueueCoreEvent( event );
249   mCoreEventInterface.ProcessCoreEvents();
250 }
251
252 void EventHandler::Reset()
253 {
254   mCombiner.Reset();
255
256   // Any touch listeners should be told of the interruption.
257   Integration::TouchEvent event;
258   Integration::Point point;
259   point.SetState( PointState::INTERRUPTED );
260   event.AddPoint( point );
261
262   // First the touch event & related gesture events are queued
263   mCoreEventInterface.QueueCoreEvent( event );
264   mGestureManager.SendEvent( event );
265
266   // Next the events are processed with a single call into Core
267   mCoreEventInterface.ProcessCoreEvents();
268 }
269
270 void EventHandler::Pause()
271 {
272   mPaused = true;
273   Reset();
274 }
275
276 void EventHandler::Resume()
277 {
278   mPaused = false;
279   Reset();
280 }
281
282 void EventHandler::SetDragAndDropDetector( DragAndDropDetectorPtr detector )
283 {
284   mDragAndDropDetector = detector;
285 }
286
287 void EventHandler::SetRotationObserver( RotationObserver* observer )
288 {
289   mRotationObserver = observer;
290 }
291
292 } // namespace Adaptor
293
294 } // namespace Internal
295
296 } // namespace Dali