Gesture event refactor
[platform/core/uifw/dali-core.git] / dali / internal / event / events / event-processor.cpp
1 /*
2  * Copyright (c) 2019 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/key-event-integ.h>
25 #include <dali/integration-api/events/wheel-event-integ.h>
26 #include <dali/integration-api/events/touch-event-integ.h>
27 #include <dali/integration-api/events/hover-event-integ.h>
28 #include <dali/internal/event/events/gesture-event-processor.h>
29 #include <dali/internal/common/core-impl.h>
30 #include <dali/internal/event/common/notification-manager.h>
31
32 using Dali::Integration::Event;
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace // unnamed namespace
41 {
42
43 static const std::size_t MAX_MESSAGE_SIZE = std::max( sizeof(Integration::TouchEvent),
44                                                       std::max( sizeof(Integration::KeyEvent), sizeof(Integration::WheelEvent) ) );
45
46 static const std::size_t INITIAL_MIN_CAPACITY = 4;
47
48 static const std::size_t INITIAL_BUFFER_SIZE = MAX_MESSAGE_SIZE * INITIAL_MIN_CAPACITY;
49
50 } // unnamed namespace
51
52 EventProcessor::EventProcessor( Scene& scene, GestureEventProcessor& gestureEventProcessor )
53 : mScene( scene ),
54   mTouchEventProcessor( scene ),
55   mHoverEventProcessor( scene ),
56   mGestureEventProcessor( gestureEventProcessor ),
57   mKeyEventProcessor( scene ),
58   mWheelEventProcessor( scene ),
59   mEventQueue0( INITIAL_BUFFER_SIZE ),
60   mEventQueue1( INITIAL_BUFFER_SIZE ),
61   mCurrentEventQueue( &mEventQueue0 )
62 {
63 }
64
65 EventProcessor::~EventProcessor()
66 {
67   for( MessageBuffer::Iterator iter = mEventQueue0.Begin(); iter.IsValid(); iter.Next() )
68   {
69     // Call virtual destructor explictly; since delete will not be called after placement new
70     Event* event = reinterpret_cast< Event* >( iter.Get() );
71     event->~Event();
72   }
73
74   for( MessageBuffer::Iterator iter = mEventQueue1.Begin(); iter.IsValid(); iter.Next() )
75   {
76     // Call virtual destructor explictly; since delete will not be called after placement new
77     Event* event = reinterpret_cast< Event* >( iter.Get() );
78     event->~Event();
79   }
80 }
81
82 void EventProcessor::QueueEvent( const Event& event )
83 {
84   switch( event.type )
85   {
86     case Event::Touch:
87     {
88       typedef Integration::TouchEvent DerivedType;
89
90       // Reserve some memory inside the message queue
91       uint32_t* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
92
93       // Construct message in the message queue memory; note that delete should not be called on the return value
94       new (slot) DerivedType( static_cast<const DerivedType&>(event) );
95
96       break;
97     }
98
99     case Event::Hover:
100     {
101       typedef Integration::HoverEvent DerivedType;
102
103       // Reserve some memory inside the message queue
104       uint32_t* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
105
106       // Construct message in the message queue memory; note that delete should not be called on the return value
107       new (slot) DerivedType( static_cast<const DerivedType&>(event) );
108
109       break;
110     }
111
112     case Event::Key:
113     {
114       typedef Integration::KeyEvent DerivedType;
115
116       // Reserve some memory inside the message queue
117       uint32_t* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
118
119       // Construct message in the message queue memory; note that delete should not be called on the return value
120       new (slot) DerivedType( static_cast<const DerivedType&>(event) );
121
122       break;
123     }
124
125     case Event::Wheel:
126     {
127       typedef Integration::WheelEvent DerivedType;
128
129       // Reserve some memory inside the message queue
130       uint32_t* slot = mCurrentEventQueue->ReserveMessageSlot( sizeof( DerivedType ) );
131
132       // Construct message in the message queue memory; note that delete should not be called on the return value
133       new (slot) DerivedType( static_cast<const DerivedType&>(event) );
134
135       break;
136     }
137   }
138 }
139
140 void EventProcessor::ProcessEvents()
141 {
142   MessageBuffer* queueToProcess = mCurrentEventQueue;
143
144   // Switch current queue; events can be added safely while iterating through the other queue.
145   mCurrentEventQueue = ( &mEventQueue0 == mCurrentEventQueue ) ? &mEventQueue1 : &mEventQueue0;
146
147   for( MessageBuffer::Iterator iter = queueToProcess->Begin(); iter.IsValid(); iter.Next() )
148   {
149     Event* event = reinterpret_cast< Event* >( iter.Get() );
150
151     switch( event->type )
152     {
153       case Event::Touch:
154       {
155         mTouchEventProcessor.ProcessTouchEvent( static_cast<const Integration::TouchEvent&>(*event) );
156         mGestureEventProcessor.ProcessTouchEvent(mScene, static_cast<const Integration::TouchEvent&>(*event));
157         break;
158       }
159
160       case Event::Hover:
161       {
162         mHoverEventProcessor.ProcessHoverEvent( static_cast<const Integration::HoverEvent&>(*event) );
163         break;
164       }
165
166       case Event::Key:
167       {
168         mKeyEventProcessor.ProcessKeyEvent( static_cast<const Integration::KeyEvent&>(*event) );
169         break;
170       }
171
172       case Event::Wheel:
173       {
174         mWheelEventProcessor.ProcessWheelEvent( static_cast<const Integration::WheelEvent&>(*event) );
175         break;
176       }
177     }
178     // Call virtual destructor explictly; since delete will not be called after placement new
179     event->~Event();
180   }
181
182   queueToProcess->Reset();
183 }
184
185 } // namespace Internal
186
187 } // namespace Dali