[dali_1.2.34] Merge branch 'devel/master'
[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 #include <input/text/text-input-listeners.h>
24 #include <virtual-keyboard.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace Adaptor
33 {
34
35 namespace
36 {
37
38 const unsigned int POINTER_DEVICE_ID = 2;
39 const unsigned int TOUCH_DEVICE_ID = 3;
40
41 } // unnamed namespace
42
43 InputManager::InputManager()
44 :mDisplay( NULL ),
45  mWindowEventInterface( NULL )
46 {
47
48 }
49 InputManager::~InputManager()
50 {
51   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
52   {
53     delete (*iter);
54   }
55   mSeats.Clear();
56 }
57
58 void InputManager::AssignWindowEventInterface( WindowEventInterface* eventInterface)
59 {
60   mWindowEventInterface = eventInterface;
61   mTextInputManger.AssignWindowEventInterface( mWindowEventInterface );
62 }
63
64 void InputManager::AssignDisplay( WlDisplay* display )
65 {
66   mDisplay = display;
67   mTextInputManger.AssignDisplay( mDisplay );
68 }
69
70 void InputManager::AssignSurface( WlSurface* surface)
71 {
72   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
73   {
74     Seat* seat = (*iter);
75     seat->SetSurfaceInterface( surface );
76   }
77 }
78
79
80 void InputManager::AddSeatListener( Dali::WlSeat* seatInterface )
81 {
82   Seat* seat = new Seat( this, seatInterface );
83
84   AddSeat( seat );
85   mTextInputManger.AddSeat( seat );
86
87   // listen to seat events
88   wl_seat_add_listener( seatInterface, Wayland::GetSeatListener(), this );
89
90 }
91
92 void InputManager::AddTextInputManager( Dali::WlTextInputManager* textInputManager )
93 {
94
95   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
96   {
97     Seat* seat = (*iter);
98
99     // Create a text input object for each seat
100     WlTextInput* textInput = wl_text_input_manager_create_text_input( textInputManager );
101     seat->SetTextInputInterface( textInput );
102
103     wl_text_input_add_listener( textInput, Wayland::GetTextInputListener(), &mTextInputManger );
104   }
105
106 }
107
108 void InputManager::PointerEnter( Seat* seat, unsigned int serial, WlSurface* surface, float x, float y )
109 {
110   if( mWindowEventInterface )
111   {
112     mWindowEventInterface->WindowFocusIn();
113   }
114 }
115
116 void InputManager::PointerLeave( Seat* seat, unsigned int serial, WlSurface* surface )
117 {
118   if( mWindowEventInterface )
119   {
120     mWindowEventInterface->WindowFocusOut();
121   }
122
123 }
124
125 void InputManager::PointerMotion( Seat* seat, unsigned int timestamp, float x, float y )
126 {
127   if( mWindowEventInterface )
128   {
129     Integration::Point point;
130     point.SetDeviceId( POINTER_DEVICE_ID );
131     point.SetState( PointState::MOTION );
132     point.SetScreenPosition( Vector2( x , y ) );
133     mWindowEventInterface->TouchEvent( point, timestamp );
134   }
135 }
136
137 void InputManager::PointerButton( Seat* seat, unsigned int serial, unsigned int timestamp, unsigned int button, unsigned int state )
138 {
139   // think about handling multiple pointer button states, if DALi starts to support them
140   if( mWindowEventInterface )
141   {
142     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
143
144     Integration::Point point;
145     point.SetDeviceId( POINTER_DEVICE_ID );
146     point.SetScreenPosition( pointer );
147
148     if( state == 1 )
149     {
150       point.SetState( PointState::DOWN );
151     }
152     else
153     {
154       point.SetState( PointState::UP );
155     }
156
157     mWindowEventInterface->TouchEvent( point, timestamp );
158   }
159 }
160
161 void InputManager::PointerAxis( Seat* seat, unsigned int timestamp, unsigned int axis, float value )
162 {
163
164   if( mWindowEventInterface )
165   {
166     WheelEvent wheelEvent( WheelEvent::MOUSE_WHEEL,
167                            axis,
168                            static_cast< int >( seat->GetDepressedKeyboardModifiers() ),
169                            seat->GetLastPointerPosition(),
170                            value,
171                            timestamp );
172
173     mWindowEventInterface->WheelEvent( wheelEvent );
174   }
175 }
176
177 void InputManager::KeyboardKeymap( Seat* seat, unsigned int format, int fd, unsigned int size )
178 {
179   seat->KeyboardKeymap( format, fd, size );
180 }
181
182 void InputManager::KeyFocusEnter( Seat* seat, unsigned int serial, WlSurface* surface, WlArray* keys )
183 {
184   // ignore for now
185 }
186
187 void InputManager::KeyFocusLeave( Seat* seat, unsigned int serial, WlSurface* surface )
188 {
189   // ignore for now
190 }
191
192 void InputManager::KeyEvent( Seat* seat, unsigned int serial, unsigned int timestamp, unsigned int keycode, unsigned int state )
193 {
194   Dali::KeyEvent keyEvent = seat->GetDALiKeyEvent( serial, timestamp, keycode, state );
195
196   mWindowEventInterface->KeyEvent( keyEvent);
197
198 }
199
200
201 void InputManager::KeyModifiers( Seat* seat,
202                           unsigned int serial,
203                           unsigned int depressed,
204                           unsigned int latched,
205                           unsigned int locked,
206                           unsigned int group )
207 {
208   seat->SetDepressedKeyboardModifiers( depressed );
209 }
210
211 void InputManager::KeyRepeatInfo( Seat* seat, int32_t rate, int32_t delay)
212 {
213   if(( rate >= 0 ) && ( delay >= 0))
214   {
215     seat->SetKeyRepeatInfo( static_cast< unsigned int >( rate) , static_cast< unsigned int >(delay ));
216   }
217 }
218
219 void InputManager::TouchDown( Seat* seat, unsigned int serial, unsigned int timestamp, WlSurface* surface, int touchId, float x, float y)
220 {
221   // think about handling multiple pointer button states, if DALi starts to support them
222   if( mWindowEventInterface )
223   {
224     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
225
226     Integration::Point point;
227     point.SetDeviceId( touchId );
228     point.SetState( PointState::DOWN );
229     point.SetScreenPosition( pointer );
230     mWindowEventInterface->TouchEvent( point, timestamp );
231   }
232 }
233
234
235 void InputManager::TouchUp( Seat* seat, unsigned int serial, unsigned int timestamp, int touchId )
236 {
237   if( mWindowEventInterface )
238   {
239     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
240
241     Integration::Point point;
242     point.SetDeviceId( touchId );
243     point.SetState( PointState::UP );
244     point.SetScreenPosition( pointer );
245     mWindowEventInterface->TouchEvent( point, timestamp );
246   }
247 }
248
249 void InputManager::TouchMotion( Seat* seat, unsigned int timestamp, int touchId, float x, float y )
250 {
251   if( mWindowEventInterface )
252   {
253     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
254
255     Integration::Point point;
256     point.SetDeviceId( touchId );
257     point.SetState( PointState::MOTION );
258     point.SetScreenPosition( pointer );
259     mWindowEventInterface->TouchEvent( point, timestamp );
260   }
261 }
262
263 void InputManager::TouchFrame( Seat* seat )
264 {
265   // un-used
266 }
267
268 void InputManager::TouchCancel( Seat* seat )
269 {
270   if( mWindowEventInterface )
271   {
272     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
273
274     // it looks like DALi just checks the first touch point for interruption
275     // so touchId can be zero
276     Integration::Point point;
277     point.SetState( PointState::INTERRUPTED );
278     point.SetScreenPosition( pointer );
279     mWindowEventInterface->TouchEvent( point, 0 );
280   }
281 }
282
283 Seat* InputManager::GetSeat( const WlKeyboard* keyboard )
284 {
285   for( Dali::Vector< Seat *>::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
286   {
287     if( (*iter)->GetKeyboardInterface() == keyboard )
288     {
289       return (*iter);
290     }
291   }
292   return NULL;
293 }
294
295 Seat* InputManager::GetSeat( const WlPointer* pointer )
296 {
297   for( Dali::Vector< Seat *>::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
298   {
299     if( (*iter)->GetPointerInterface() == pointer )
300     {
301       return (*iter);
302     }
303   }
304   return NULL;
305 }
306
307 Seat* InputManager::GetSeat( const WlTouch* touch )
308 {
309   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
310   {
311     if( (*iter)->GetTouchInterface() == touch )
312     {
313       return (*iter);
314     }
315   }
316   return NULL;
317 }
318
319 Seat* InputManager::GetSeat( const WlSeat* seat)
320 {
321   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
322   {
323     if( (*iter)->GetSeatInterface() == seat )
324     {
325       return (*iter);
326     }
327   }
328   return NULL;
329 }
330
331 void InputManager::AddSeat( Seat* seat )
332 {
333   mSeats.PushBack( seat );
334 }
335
336
337 }
338 }
339 }