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 : mTouchEventProcessor(stage),
58 mGestureEventProcessor(gestureEventProcessor),
59 mKeyEventProcessor(stage),
60 mMouseWheelEventProcessor(stage),
61 mEventQueue0( INITIAL_BUFFER_SIZE ),
62 mEventQueue1( INITIAL_BUFFER_SIZE ),
63 mCurrentEventQueue( &mEventQueue0 )
67 EventProcessor::~EventProcessor()
69 for( MessageBuffer::Iterator iter = mEventQueue0.Begin(); iter.IsValid(); iter.Next() )
71 // Call virtual destructor explictly; since delete will not be called after placement new
72 Event* event = reinterpret_cast< Event* >( iter.Get() );
76 for( MessageBuffer::Iterator iter = mEventQueue1.Begin(); iter.IsValid(); iter.Next() )
78 // Call virtual destructor explictly; since delete will not be called after placement new
79 Event* event = reinterpret_cast< Event* >( iter.Get() );
84 void EventProcessor::QueueEvent( const Event& event )
90 typedef Integration::TouchEvent DerivedType;
92 // Reserve some memory inside the message queue
93 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
95 // Construct message in the message queue memory; note that delete should not be called on the return value
96 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
103 typedef Integration::KeyEvent DerivedType;
105 // Reserve some memory inside the message queue
106 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
108 // Construct message in the message queue memory; note that delete should not be called on the return value
109 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
114 case Event::MouseWheel:
116 typedef Integration::MouseWheelEvent DerivedType;
118 // Reserve some memory inside the message queue
119 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
121 // Construct message in the message queue memory; note that delete should not be called on the return value
122 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
129 QueueGestureEvent( static_cast<const Integration::GestureEvent&>(event) );
136 void EventProcessor::QueueGestureEvent(const Integration::GestureEvent& event)
138 switch( event.gestureType )
142 typedef Integration::PinchGestureEvent DerivedType;
144 // Reserve some memory inside the message queue
145 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
147 // Construct message in the message queue memory; note that delete should not be called on the return value
148 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
155 typedef Integration::PanGestureEvent DerivedType;
157 // Reserve some memory inside the message queue
158 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
160 // Construct message in the message queue memory; note that delete should not be called on the return value
161 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
168 typedef Integration::TapGestureEvent DerivedType;
170 // Reserve some memory inside the message queue
171 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
173 // Construct message in the message queue memory; note that delete should not be called on the return value
174 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
179 case Gesture::LongPress:
181 typedef Integration::LongPressGestureEvent DerivedType;
183 // Reserve some memory inside the message queue
184 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
186 // Construct message in the message queue memory; note that delete should not be called on the return value
187 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
194 void EventProcessor::ProcessEvents()
196 MessageBuffer* queueToProcess = mCurrentEventQueue;
198 // Switch current queue; events can be added safely while iterating through the other queue.
199 mCurrentEventQueue = (&mEventQueue0 == mCurrentEventQueue) ? &mEventQueue1 : &mEventQueue0;
201 for( MessageBuffer::Iterator iter = queueToProcess->Begin(); iter.IsValid(); iter.Next() )
203 Event* event = reinterpret_cast< Event* >( iter.Get() );
205 switch( event->type )
209 mTouchEventProcessor.ProcessTouchEvent( static_cast<const Integration::TouchEvent&>(*event) );
215 mKeyEventProcessor.ProcessKeyEvent( static_cast<const Integration::KeyEvent&>(*event) );
219 case Event::MouseWheel:
221 mMouseWheelEventProcessor.ProcessMouseWheelEvent( static_cast<const Integration::MouseWheelEvent&>(*event) );
227 mGestureEventProcessor.ProcessGestureEvent( static_cast<const Integration::GestureEvent&>(*event) );
232 // Call virtual destructor explictly; since delete will not be called after placement new
236 queueToProcess->Reset();
239 } // namespace Internal