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/pinch-gesture-event.h>
29 #include <dali/integration-api/events/pan-gesture-event.h>
30 #include <dali/integration-api/events/tap-gesture-event.h>
31 #include <dali/integration-api/events/long-press-gesture-event.h>
32 #include <dali/internal/event/events/gesture-event-processor.h>
33 #include <dali/internal/common/core-impl.h>
34 #include <dali/internal/event/common/notification-manager.h>
36 using Dali::Integration::Event;
44 namespace // unnamed namespace
47 static const std::size_t MAX_MESSAGE_SIZE = std::max( sizeof(Integration::TouchEvent),
48 std::max( sizeof(Integration::KeyEvent),
49 std::max( sizeof(Integration::MouseWheelEvent), sizeof(Integration::GestureEvent) ) ) );
51 static const std::size_t INITIAL_MIN_CAPICITY = 4;
53 static const std::size_t INITIAL_BUFFER_SIZE = MAX_MESSAGE_SIZE * INITIAL_MIN_CAPICITY;
55 } // unnamed namespace
57 EventProcessor::EventProcessor(Stage& stage, NotificationManager& /* notificationManager */, GestureEventProcessor& gestureEventProcessor)
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) );
130 QueueGestureEvent( static_cast<const Integration::GestureEvent&>(event) );
137 void EventProcessor::QueueGestureEvent(const Integration::GestureEvent& event)
139 switch( event.gestureType )
143 typedef Integration::PinchGestureEvent DerivedType;
145 // Reserve some memory inside the message queue
146 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
148 // Construct message in the message queue memory; note that delete should not be called on the return value
149 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
156 typedef Integration::PanGestureEvent DerivedType;
158 // Reserve some memory inside the message queue
159 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
161 // Construct message in the message queue memory; note that delete should not be called on the return value
162 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
169 typedef Integration::TapGestureEvent DerivedType;
171 // Reserve some memory inside the message queue
172 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
174 // Construct message in the message queue memory; note that delete should not be called on the return value
175 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
180 case Gesture::LongPress:
182 typedef Integration::LongPressGestureEvent DerivedType;
184 // Reserve some memory inside the message queue
185 unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
187 // Construct message in the message queue memory; note that delete should not be called on the return value
188 new (slot) DerivedType( static_cast<const DerivedType&>(event) );
195 void EventProcessor::ProcessEvents()
197 MessageBuffer* queueToProcess = mCurrentEventQueue;
199 // Switch current queue; events can be added safely while iterating through the other queue.
200 mCurrentEventQueue = (&mEventQueue0 == mCurrentEventQueue) ? &mEventQueue1 : &mEventQueue0;
202 for( MessageBuffer::Iterator iter = queueToProcess->Begin(); iter.IsValid(); iter.Next() )
204 Event* event = reinterpret_cast< Event* >( iter.Get() );
206 switch( event->type )
210 mTouchEventProcessor.ProcessTouchEvent( static_cast<const Integration::TouchEvent&>(*event) );
216 mKeyEventProcessor.ProcessKeyEvent( static_cast<const Integration::KeyEvent&>(*event) );
220 case Event::MouseWheel:
222 mMouseWheelEventProcessor.ProcessMouseWheelEvent( static_cast<const Integration::MouseWheelEvent&>(*event) );
228 mGestureEventProcessor.ProcessGestureEvent( static_cast<const Integration::GestureEvent&>(*event) );
233 // Call virtual destructor explictly; since delete will not be called after placement new
237 queueToProcess->Reset();
240 } // namespace Internal