2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/event/events/event-processor.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/integration-api/events/event.h>
23 #include <dali/integration-api/events/gesture-event.h>
24 #include <dali/integration-api/events/key-event-integ.h>
25 #include <dali/integration-api/events/mouse-wheel-event-integ.h>
26 #include <dali/integration-api/events/touch-event-integ.h>
27 #include <dali/integration-api/events/pinch-gesture-event.h>
28 #include <dali/integration-api/events/pan-gesture-event.h>
29 #include <dali/integration-api/events/tap-gesture-event.h>
30 #include <dali/integration-api/events/long-press-gesture-event.h>
31 #include <dali/internal/event/events/gesture-event-processor.h>
32 #include <dali/internal/common/core-impl.h>
33 #include <dali/internal/event/common/notification-manager.h>
35 using Dali::Integration::Event;
43 namespace // unnamed namespace
46 static const std::size_t MAX_MESSAGE_SIZE = std::max( sizeof(Integration::TouchEvent),
47 std::max( sizeof(Integration::KeyEvent),
48 std::max( sizeof(Integration::MouseWheelEvent), sizeof(Integration::GestureEvent) ) ) );
50 static const std::size_t INITIAL_MIN_CAPICITY = 4;
52 static const std::size_t INITIAL_BUFFER_SIZE = MAX_MESSAGE_SIZE * INITIAL_MIN_CAPICITY;
54 } // unnamed namespace
56 EventProcessor::EventProcessor(Stage& stage, NotificationManager& notificationManager, GestureEventProcessor& gestureEventProcessor)
57 : mNotificationManager(notificationManager),
58 mTouchEventProcessor(stage),
59 mGestureEventProcessor(gestureEventProcessor),
60 mKeyEventProcessor(stage),
61 mMouseWheelEventProcessor(stage),
62 mEventQueue0( INITIAL_BUFFER_SIZE ),
63 mEventQueue1( INITIAL_BUFFER_SIZE ),
64 mCurrentEventQueue( &mEventQueue0 )
68 EventProcessor::~EventProcessor()
70 for( MessageBuffer::Iterator iter = mEventQueue0.Begin(); iter.IsValid(); iter.Next() )
72 // Call virtual destructor explictly; since delete will not be called after placement new
73 Event* event = reinterpret_cast< Event* >( iter.Get() );
77 for( MessageBuffer::Iterator iter = mEventQueue1.Begin(); iter.IsValid(); iter.Next() )
79 // Call virtual destructor explictly; since delete will not be called after placement new
80 Event* event = reinterpret_cast< Event* >( iter.Get() );
85 void EventProcessor::QueueEvent( const Event& event )
91 typedef Integration::TouchEvent DerivedType;
93 // Reserve some memory inside the message queue
94 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
96 // Construct message in the message queue memory; note that delete should not be called on the return value
97 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
104 typedef Integration::KeyEvent DerivedType;
106 // Reserve some memory inside the message queue
107 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
109 // Construct message in the message queue memory; note that delete should not be called on the return value
110 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
115 case Event::MouseWheel:
117 typedef Integration::MouseWheelEvent DerivedType;
119 // Reserve some memory inside the message queue
120 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
122 // Construct message in the message queue memory; note that delete should not be called on the return value
123 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
128 case Event::Notification:
130 // TODO - Remove this deprecated event
136 QueueGestureEvent( static_cast<const Integration::GestureEvent&>(event) );
142 DALI_ASSERT_ALWAYS( false && "Invalid event sent from Integration\n" );
148 void EventProcessor::QueueGestureEvent(const Integration::GestureEvent& event)
150 switch( event.gestureType )
154 typedef Integration::PinchGestureEvent DerivedType;
156 // Reserve some memory inside the message queue
157 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
159 // Construct message in the message queue memory; note that delete should not be called on the return value
160 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
167 typedef Integration::PanGestureEvent DerivedType;
169 // Reserve some memory inside the message queue
170 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
172 // Construct message in the message queue memory; note that delete should not be called on the return value
173 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
180 typedef Integration::TapGestureEvent DerivedType;
182 // Reserve some memory inside the message queue
183 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
185 // Construct message in the message queue memory; note that delete should not be called on the return value
186 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
191 case Gesture::LongPress:
193 typedef Integration::LongPressGestureEvent DerivedType;
195 // Reserve some memory inside the message queue
196 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
198 // Construct message in the message queue memory; note that delete should not be called on the return value
199 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
206 DALI_ASSERT_ALWAYS( false && "Invalid event sent from Integration\n" );
212 void EventProcessor::ProcessEvents()
214 MessageBuffer* queueToProcess = mCurrentEventQueue;
216 // Switch current queue; events can be added safely while iterating through the other queue.
217 mCurrentEventQueue = (&mEventQueue0 == mCurrentEventQueue) ? &mEventQueue1 : &mEventQueue0;
219 for( MessageBuffer::Iterator iter = queueToProcess->Begin(); iter.IsValid(); iter.Next() )
221 Event* event = reinterpret_cast< Event* >( iter.Get() );
223 switch( event->type )
227 mTouchEventProcessor.ProcessTouchEvent( static_cast<const Integration::TouchEvent&>(*event) );
233 mKeyEventProcessor.ProcessKeyEvent( static_cast<const Integration::KeyEvent&>(*event) );
237 case Event::MouseWheel:
239 mMouseWheelEventProcessor.ProcessMouseWheelEvent( static_cast<const Integration::MouseWheelEvent&>(*event) );
245 mGestureEventProcessor.ProcessGestureEvent( static_cast<const Integration::GestureEvent&>(*event) );
251 DALI_ASSERT_ALWAYS( false && "Invalid event sent from Integration\n" );
255 // Call virtual destructor explictly; since delete will not be called after placement new
260 queueToProcess->Reset();
263 } // namespace Internal