Wayland virtual keyboard 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 #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     TouchPoint point ( POINTER_DEVICE_ID, TouchPoint::Motion, x , y);
130     mWindowEventInterface->TouchEvent( point, timestamp );
131   }
132 }
133
134 void InputManager::PointerButton( Seat* seat, unsigned int serial, unsigned int timestamp, unsigned int button, unsigned int state )
135 {
136   // think about handling multiple pointer button states, if DALi starts to support them
137   if( mWindowEventInterface )
138   {
139     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
140
141     TouchPoint point ( POINTER_DEVICE_ID, TouchPoint::Up,  pointer.x,  pointer.y );
142     if( state == 1)
143     {
144       point.state = TouchPoint::Down;
145     }
146     mWindowEventInterface->TouchEvent( point, timestamp );
147   }
148 }
149
150 void InputManager::PointerAxis( Seat* seat, unsigned int timestamp, unsigned int axis, float value )
151 {
152
153   if( mWindowEventInterface )
154   {
155     WheelEvent wheelEvent( WheelEvent::MOUSE_WHEEL,
156                            axis,
157                            static_cast< int >( seat->GetDepressedKeyboardModifiers() ),
158                            seat->GetLastPointerPosition(),
159                            value,
160                            timestamp );
161
162     mWindowEventInterface->WheelEvent( wheelEvent );
163   }
164 }
165
166 void InputManager::KeyboardKeymap( Seat* seat, unsigned int format, int fd, unsigned int size )
167 {
168   seat->KeyboardKeymap( format, fd, size );
169 }
170
171 void InputManager::KeyFocusEnter( Seat* seat, unsigned int serial, WlSurface* surface, WlArray* keys )
172 {
173   // ignore for now
174 }
175
176 void InputManager::KeyFocusLeave( Seat* seat, unsigned int serial, WlSurface* surface )
177 {
178   // ignore for now
179 }
180
181 void InputManager::KeyEvent( Seat* seat, unsigned int serial, unsigned int timestamp, unsigned int keycode, unsigned int state )
182 {
183   Dali::KeyEvent keyEvent = seat->GetDALiKeyEvent( serial, timestamp, keycode, state );
184
185   mWindowEventInterface->KeyEvent( keyEvent);
186
187 }
188
189
190 void InputManager::KeyModifiers( Seat* seat,
191                           unsigned int serial,
192                           unsigned int depressed,
193                           unsigned int latched,
194                           unsigned int locked,
195                           unsigned int group )
196 {
197   seat->SetDepressedKeyboardModifiers( depressed );
198 }
199
200 void InputManager::KeyRepeatInfo( Seat* seat, int32_t rate, int32_t delay)
201 {
202   if(( rate >= 0 ) && ( delay >= 0))
203   {
204     seat->SetKeyRepeatInfo( static_cast< unsigned int >( rate) , static_cast< unsigned int >(delay ));
205   }
206 }
207
208 void InputManager::TouchDown( Seat* seat, unsigned int serial, unsigned int timestamp, WlSurface* surface, int touchId, float x, float y)
209 {
210   // think about handling multiple pointer button states, if DALi starts to support them
211   if( mWindowEventInterface )
212   {
213     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
214
215     TouchPoint point ( touchId, TouchPoint::Down,  pointer.x,  pointer.y );
216     mWindowEventInterface->TouchEvent( point, timestamp );
217   }
218 }
219
220
221 void InputManager::TouchUp( Seat* seat, unsigned int serial, unsigned int timestamp, int touchId )
222 {
223   if( mWindowEventInterface )
224   {
225     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
226
227     TouchPoint point ( touchId, TouchPoint::Up,  pointer.x,  pointer.y );
228     mWindowEventInterface->TouchEvent( point, timestamp );
229   }
230 }
231
232 void InputManager::TouchMotion( Seat* seat, unsigned int timestamp, int touchId, float x, float y )
233 {
234   if( mWindowEventInterface )
235   {
236     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
237
238     TouchPoint point ( touchId, TouchPoint::Motion,  pointer.x,  pointer.y );
239     mWindowEventInterface->TouchEvent( point, timestamp );
240   }
241 }
242
243 void InputManager::TouchFrame( Seat* seat )
244 {
245   // un-used
246 }
247
248 void InputManager::TouchCancel( Seat* seat )
249 {
250   if( mWindowEventInterface )
251   {
252     const Dali::Vector2& pointer( seat->GetLastPointerPosition() );
253
254     // it looks like DALi just checks the first touch point for interruption
255     // so touchId can be zero
256     TouchPoint point ( 0, TouchPoint::Interrupted,  pointer.x,  pointer.y );
257     mWindowEventInterface->TouchEvent( point, 0 );
258   }
259 }
260
261 Seat* InputManager::GetSeat( const WlKeyboard* keyboard )
262 {
263   for( Dali::Vector< Seat *>::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
264   {
265     if( (*iter)->GetKeyboardInterface() == keyboard )
266     {
267       return (*iter);
268     }
269   }
270   return NULL;
271 }
272
273 Seat* InputManager::GetSeat( const WlPointer* pointer )
274 {
275   for( Dali::Vector< Seat *>::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
276   {
277     if( (*iter)->GetPointerInterface() == pointer )
278     {
279       return (*iter);
280     }
281   }
282   return NULL;
283 }
284
285 Seat* InputManager::GetSeat( const WlTouch* touch )
286 {
287   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
288   {
289     if( (*iter)->GetTouchInterface() == touch )
290     {
291       return (*iter);
292     }
293   }
294   return NULL;
295 }
296
297 Seat* InputManager::GetSeat( const WlSeat* seat)
298 {
299   for( Dali::Vector< Seat* >::Iterator iter = mSeats.Begin(); iter != mSeats.End() ; ++iter )
300   {
301     if( (*iter)->GetSeatInterface() == seat )
302     {
303       return (*iter);
304     }
305   }
306   return NULL;
307 }
308
309 void InputManager::AddSeat( Seat* seat )
310 {
311   mSeats.PushBack( seat );
312 }
313
314
315 }
316 }
317 }