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