Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / event / events / event-processor.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/events/event-processor.h>
19
20 // INTERNAL INCLUDES
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>
34
35 using Dali::Integration::Event;
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 namespace // unnamed namespace
44 {
45
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) ) ) );
49
50 static const std::size_t INITIAL_MIN_CAPICITY = 4;
51
52 static const std::size_t INITIAL_BUFFER_SIZE = MAX_MESSAGE_SIZE * INITIAL_MIN_CAPICITY;
53
54 } // unnamed namespace
55
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 )
64 {
65 }
66
67 EventProcessor::~EventProcessor()
68 {
69   for( MessageBuffer::Iterator iter = mEventQueue0.Begin(); iter.IsValid(); iter.Next() )
70   {
71     // Call virtual destructor explictly; since delete will not be called after placement new
72     Event* event = reinterpret_cast< Event* >( iter.Get() );
73     event->~Event();
74   }
75
76   for( MessageBuffer::Iterator iter = mEventQueue1.Begin(); iter.IsValid(); iter.Next() )
77   {
78     // Call virtual destructor explictly; since delete will not be called after placement new
79     Event* event = reinterpret_cast< Event* >( iter.Get() );
80     event->~Event();
81   }
82 }
83
84 void EventProcessor::QueueEvent( const Event& event )
85 {
86   switch( event.type )
87   {
88     case Event::Touch:
89     {
90       typedef Integration::TouchEvent DerivedType;
91
92       // Reserve some memory inside the message queue
93       unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
94
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) );
97
98       break;
99     }
100
101     case Event::Key:
102     {
103       typedef Integration::KeyEvent DerivedType;
104
105       // Reserve some memory inside the message queue
106       unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
107
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) );
110
111       break;
112     }
113
114     case Event::MouseWheel:
115     {
116       typedef Integration::MouseWheelEvent DerivedType;
117
118       // Reserve some memory inside the message queue
119       unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
120
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) );
123
124       break;
125     }
126
127     case Event::Gesture:
128     {
129       QueueGestureEvent( static_cast<const Integration::GestureEvent&>(event) );
130       break;
131     }
132
133   }
134 }
135
136 void EventProcessor::QueueGestureEvent(const Integration::GestureEvent& event)
137 {
138   switch( event.gestureType )
139   {
140     case Gesture::Pinch:
141     {
142       typedef Integration::PinchGestureEvent DerivedType;
143
144       // Reserve some memory inside the message queue
145       unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
146
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) );
149
150       break;
151     }
152
153     case Gesture::Pan:
154     {
155       typedef Integration::PanGestureEvent DerivedType;
156
157       // Reserve some memory inside the message queue
158       unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
159
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) );
162
163       break;
164     }
165
166     case Gesture::Tap:
167     {
168       typedef Integration::TapGestureEvent DerivedType;
169
170       // Reserve some memory inside the message queue
171       unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
172
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) );
175
176       break;
177     }
178
179     case Gesture::LongPress:
180     {
181       typedef Integration::LongPressGestureEvent DerivedType;
182
183       // Reserve some memory inside the message queue
184       unsigned int* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
185
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) );
188
189       break;
190     }
191   }
192 }
193
194 void EventProcessor::ProcessEvents()
195 {
196   MessageBuffer* queueToProcess = mCurrentEventQueue;
197
198   // Switch current queue; events can be added safely while iterating through the other queue.
199   mCurrentEventQueue = (&mEventQueue0 == mCurrentEventQueue) ? &mEventQueue1 : &mEventQueue0;
200
201   for( MessageBuffer::Iterator iter = queueToProcess->Begin(); iter.IsValid(); iter.Next() )
202   {
203     Event* event = reinterpret_cast< Event* >( iter.Get() );
204
205     switch( event->type )
206     {
207       case Event::Touch:
208       {
209         mTouchEventProcessor.ProcessTouchEvent( static_cast<const Integration::TouchEvent&>(*event) );
210         break;
211       }
212
213       case Event::Key:
214       {
215         mKeyEventProcessor.ProcessKeyEvent( static_cast<const Integration::KeyEvent&>(*event) );
216         break;
217       }
218
219       case Event::MouseWheel:
220       {
221         mMouseWheelEventProcessor.ProcessMouseWheelEvent( static_cast<const Integration::MouseWheelEvent&>(*event) );
222         break;
223       }
224
225       case Event::Gesture:
226       {
227         mGestureEventProcessor.ProcessGestureEvent( static_cast<const Integration::GestureEvent&>(*event) );
228         break;
229       }
230
231     }
232     // Call virtual destructor explictly; since delete will not be called after placement new
233     event->~Event();
234   }
235
236   queueToProcess->Reset();
237 }
238
239 } // namespace Internal
240
241 } // namespace Dali