Wayland DPI and Multi-threading Wayland support
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / input-manager.cpp
1 /*
2  * Copyright (c) 2015 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 "input-manager.h"
20
21 // INTERNAL INCLUDES
22 #include <input/input-listeners.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace Adaptor
31 {
32
33 namespace
34 {
35
36 const unsigned int POINTER_DEVICE_ID = 2;
37 const unsigned int TOUCH_DEVICE_ID = 3;
38
39 } // unnamed namespace
40
41 InputManager::InputManager()
42 :mWindowEventInterface( NULL )
43 {
44
45 }
46 InputManager::~InputManager()
47 {
48   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
49   {
50     delete (*iter);
51   }
52   mSeats.Clear();
53 }
54
55 void InputManager::AssignWindowEventInterface( WindowEventInterface* eventInterface)
56 {
57   mWindowEventInterface = eventInterface;
58 }
59
60 void InputManager::AddSeatListener( Dali::WlSeat* seatInterface )
61 {
62   Seat* seat = new Seat( this, seatInterface );
63
64   AddSeat( seat );
65
66   // listen to seat events
67   wl_seat_add_listener( seatInterface, Wayland::GetSeatListener(), this );
68
69 }
70 void InputManager::PointerEnter( Seat* seat, unsigned int serial, WlSurface* surface, float x, float y )
71 {
72   if( mWindowEventInterface )
73   {
74     mWindowEventInterface->WindowFocusIn();
75   }
76 }
77
78 void InputManager::PointerLeave( Seat* seat, unsigned int serial, WlSurface* surface )
79 {
80   if( mWindowEventInterface )
81   {
82     mWindowEventInterface->WindowFocusOut();
83   }
84
85 }
86
87 void InputManager::PointerMotion( Seat* seat, unsigned int timestamp, float x, float y )
88 {
89   if( mWindowEventInterface )
90   {
91     TouchPoint point ( POINTER_DEVICE_ID, TouchPoint::Motion, x , y);
92     mWindowEventInterface->TouchEvent( point, timestamp );
93   }
94 }
95
96 void InputManager::PointerButton( Seat* seat, unsigned int serial, unsigned int timestamp, unsigned int button, unsigned int state )
97 {
98   // think about handling multiple pointer button states, if DALi starts to support them
99   if( mWindowEventInterface )
100   {
101     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
102
103     TouchPoint point ( POINTER_DEVICE_ID, TouchPoint::Up,  pointer.x,  pointer.y );
104     if( state == 1)
105     {
106       point.state = TouchPoint::Down;
107     }
108     mWindowEventInterface->TouchEvent( point, timestamp );
109   }
110 }
111
112 void InputManager::PointerAxis( Seat* seat, unsigned int timestamp, unsigned int axis, float value )
113 {
114
115   if( mWindowEventInterface )
116   {
117     WheelEvent wheelEvent( WheelEvent::MOUSE_WHEEL,
118                            axis,
119                            static_cast< int >( seat->GetDepressedKeyboardModifiers() ),
120                            seat->GetLastPointerPosition(),
121                            value,
122                            timestamp );
123
124     mWindowEventInterface->WheelEvent( wheelEvent );
125   }
126 }
127
128 void InputManager::KeyboardKeymap( Seat* seat, unsigned int format, int fd, unsigned int size )
129 {
130   seat->KeyboardKeymap( format, fd, size );
131 }
132
133 void InputManager::KeyFocusEnter( Seat* seat, unsigned int serial, WlSurface* surface, WlArray* keys )
134 {
135   // ignore for now
136 }
137
138 void InputManager::KeyFocusLeave( Seat* seat, unsigned int serial, WlSurface* surface )
139 {
140   // ignore for now
141 }
142
143 void InputManager::KeyEvent( Seat* seat, unsigned int serial, unsigned int timestamp, unsigned int keycode, unsigned int state )
144 {
145   Dali::KeyEvent keyEvent = seat->GetDALiKeyEvent( serial, timestamp, keycode, state );
146
147   mWindowEventInterface->KeyEvent( keyEvent);
148
149 }
150
151
152 void InputManager::KeyModifiers( Seat* seat,
153                           unsigned int serial,
154                           unsigned int depressed,
155                           unsigned int latched,
156                           unsigned int locked,
157                           unsigned int group )
158 {
159   seat->SetDepressedKeyboardModifiers( depressed );
160 }
161
162 void InputManager::KeyRepeatInfo( Seat* seat, int32_t rate, int32_t delay)
163 {
164   if(( rate >= 0 ) && ( delay >= 0))
165   {
166     seat->SetKeyRepeatInfo( static_cast< unsigned int >( rate) , static_cast< unsigned int >(delay ));
167   }
168 }
169
170 void InputManager::TouchDown( Seat* seat, unsigned int serial, unsigned int timestamp, WlSurface* surface, int touchId, float x, float y)
171 {
172   // think about handling multiple pointer button states, if DALi starts to support them
173   if( mWindowEventInterface )
174   {
175     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
176
177     TouchPoint point ( touchId, TouchPoint::Down,  pointer.x,  pointer.y );
178     mWindowEventInterface->TouchEvent( point, timestamp );
179   }
180 }
181
182
183 void InputManager::TouchUp( Seat* seat, unsigned int serial, unsigned int timestamp, int touchId )
184 {
185   if( mWindowEventInterface )
186   {
187     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
188
189     TouchPoint point ( touchId, TouchPoint::Up,  pointer.x,  pointer.y );
190     mWindowEventInterface->TouchEvent( point, timestamp );
191   }
192 }
193
194 void InputManager::TouchMotion( Seat* seat, unsigned int timestamp, int touchId, float x, float y )
195 {
196   if( mWindowEventInterface )
197   {
198     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
199
200     TouchPoint point ( touchId, TouchPoint::Motion,  pointer.x,  pointer.y );
201     mWindowEventInterface->TouchEvent( point, timestamp );
202   }
203 }
204
205 void InputManager::TouchFrame( Seat* seat )
206 {
207   // un-used
208 }
209
210 void InputManager::TouchCancel( Seat* seat )
211 {
212   if( mWindowEventInterface )
213   {
214     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
215
216     // it looks like DALi just checks the first touch point for interruption
217     // so touchId can be zero
218     TouchPoint point ( 0, TouchPoint::Interrupted,  pointer.x,  pointer.y );
219     mWindowEventInterface->TouchEvent( point, 0 );
220   }
221 }
222
223 Seat* InputManager::GetSeat( const WlKeyboard* keyboard )
224 {
225   for( Dali::Vector< Seat *>::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
226   {
227     if( (*iter)->GetKeyboardInterface() == keyboard )
228     {
229       return (*iter);
230     }
231   }
232   return NULL;
233 }
234
235 Seat* InputManager::GetSeat( const WlPointer* pointer )
236 {
237   for( Dali::Vector< Seat *>::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
238   {
239     if( (*iter)->GetPointerInterface() == pointer )
240     {
241       return (*iter);
242     }
243   }
244   return NULL;
245 }
246
247 Seat* InputManager::GetSeat( const WlTouch* touch )
248 {
249   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
250   {
251     if( (*iter)->GetTouchInterface() == touch )
252     {
253       return (*iter);
254     }
255   }
256   return NULL;
257 }
258
259 Seat* InputManager::GetSeat( const WlSeat* seat)
260 {
261   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
262   {
263     if( (*iter)->GetSeatInterface() == seat )
264     {
265       return (*iter);
266     }
267   }
268   return NULL;
269 }
270
271 void InputManager::AddSeat( Seat* seat )
272 {
273   mSeats.PushBack( seat );
274 }
275
276
277 }
278 }
279 }