2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/events/event-processor.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/events/event.h>
24 #include <dali/integration-api/events/gesture-event.h>
25 #include <dali/integration-api/events/key-event-integ.h>
26 #include <dali/integration-api/events/mouse-wheel-event-integ.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/integration-api/events/hover-event-integ.h>
29 #include <dali/integration-api/events/pinch-gesture-event.h>
30 #include <dali/integration-api/events/pan-gesture-event.h>
31 #include <dali/integration-api/events/tap-gesture-event.h>
32 #include <dali/integration-api/events/long-press-gesture-event.h>
33 #include <dali/internal/event/events/gesture-event-processor.h>
34 #include <dali/internal/common/core-impl.h>
35 #include <dali/internal/event/common/notification-manager.h>
37 using Dali::Integration::Event;
45 namespace // unnamed namespace
48 static const std::size_t MAX_MESSAGE_SIZE = std::max( sizeof(Integration::TouchEvent),
49 std::max( sizeof(Integration::KeyEvent),
50 std::max( sizeof(Integration::MouseWheelEvent), sizeof(Integration::GestureEvent) ) ) );
52 static const std::size_t INITIAL_MIN_CAPACITY = 4;
54 static const std::size_t INITIAL_BUFFER_SIZE = MAX_MESSAGE_SIZE * INITIAL_MIN_CAPACITY;
56 } // unnamed namespace
58 EventProcessor::EventProcessor(Stage& stage, NotificationManager& /* notificationManager */, GestureEventProcessor& gestureEventProcessor)
59 : mTouchEventProcessor(stage),
60 mHoverEventProcessor(stage),
61 mGestureEventProcessor(gestureEventProcessor),
62 mKeyEventProcessor(stage),
63 mMouseWheelEventProcessor(stage),
64 mEventQueue0( INITIAL_BUFFER_SIZE ),
65 mEventQueue1( INITIAL_BUFFER_SIZE ),
66 mCurrentEventQueue( &mEventQueue0 )
70 EventProcessor::~EventProcessor()
72 for( MessageBuffer::Iterator iter = mEventQueue0.Begin(); iter.IsValid(); iter.Next() )
74 // Call virtual destructor explictly; since delete will not be called after placement new
75 Event* event = reinterpret_cast< Event* >( iter.Get() );
79 for( MessageBuffer::Iterator iter = mEventQueue1.Begin(); iter.IsValid(); iter.Next() )
81 // Call virtual destructor explictly; since delete will not be called after placement new
82 Event* event = reinterpret_cast< Event* >( iter.Get() );
87 void EventProcessor::QueueEvent( const Event& event )
93 typedef Integration::TouchEvent DerivedType;
95 // Reserve some memory inside the message queue
96 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
98 // Construct message in the message queue memory; note that delete should not be called on the return value
99 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
106 typedef Integration::HoverEvent DerivedType;
108 // Reserve some memory inside the message queue
109 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
111 // Construct message in the message queue memory; note that delete should not be called on the return value
112 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
119 typedef Integration::KeyEvent DerivedType;
121 // Reserve some memory inside the message queue
122 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
124 // Construct message in the message queue memory; note that delete should not be called on the return value
125 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
130 case Event::MouseWheel:
132 typedef Integration::MouseWheelEvent DerivedType;
134 // Reserve some memory inside the message queue
135 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
137 // Construct message in the message queue memory; note that delete should not be called on the return value
138 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
145 QueueGestureEvent( static_cast<const Integration::GestureEvent&>(event) );
152 void EventProcessor::QueueGestureEvent(const Integration::GestureEvent& event)
154 switch( event.gestureType )
158 typedef Integration::PinchGestureEvent DerivedType;
160 // Reserve some memory inside the message queue
161 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
163 // Construct message in the message queue memory; note that delete should not be called on the return value
164 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
171 typedef Integration::PanGestureEvent DerivedType;
173 // Reserve some memory inside the message queue
174 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
176 // Construct message in the message queue memory; note that delete should not be called on the return value
177 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
184 typedef Integration::TapGestureEvent DerivedType;
186 // Reserve some memory inside the message queue
187 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
189 // Construct message in the message queue memory; note that delete should not be called on the return value
190 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
195 case Gesture::LongPress:
197 typedef Integration::LongPressGestureEvent DerivedType;
199 // Reserve some memory inside the message queue
200 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
202 // Construct message in the message queue memory; note that delete should not be called on the return value
203 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
210 void EventProcessor::ProcessEvents()
212 MessageBuffer* queueToProcess = mCurrentEventQueue;
214 // Switch current queue; events can be added safely while iterating through the other queue.
215 mCurrentEventQueue = (&mEventQueue0 == mCurrentEventQueue) ? &mEventQueue1 : &mEventQueue0;
217 for( MessageBuffer::Iterator iter = queueToProcess->Begin(); iter.IsValid(); iter.Next() )
219 Event* event = reinterpret_cast< Event* >( iter.Get() );
221 switch( event->type )
225 mTouchEventProcessor.ProcessTouchEvent( static_cast<const Integration::TouchEvent&>(*event) );
231 mHoverEventProcessor.ProcessHoverEvent( static_cast<const Integration::HoverEvent&>(*event) );
237 mKeyEventProcessor.ProcessKeyEvent( static_cast<const Integration::KeyEvent&>(*event) );
241 case Event::MouseWheel:
243 mMouseWheelEventProcessor.ProcessMouseWheelEvent( static_cast<const Integration::MouseWheelEvent&>(*event) );
249 mGestureEventProcessor.ProcessGestureEvent( static_cast<const Integration::GestureEvent&>(*event) );
254 // Call virtual destructor explictly; since delete will not be called after placement new
258 queueToProcess->Reset();
261 } // namespace Internal