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