Merge "Set proper locale to harfbuzz" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / input / input-listeners.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 // CLASS HEADER
18 #include "input-listeners.h"
19
20 // EXTERNAL INCLUDES
21 #include <cctype>
22 #include <stdio.h>
23
24 // INTERNAL INCLUDES
25 #include "input-interface.h"
26 #include "seat.h"
27
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace
39 {
40 /************
41  *
42  * Pointer events callbacks. See wl_pointer_listener in wayland-client-protocol.h for a description
43  *
44  ************/
45 void PointerEnter( void* data,
46                    WlPointer* pointer,
47                    unsigned int serial,
48                    WlSurface* surface,
49                    wl_fixed_t surfaceX,
50                    wl_fixed_t surfaceY)
51 {
52   InputInterface* input = static_cast< InputInterface* >( data );
53   Seat* seat = input->GetSeat( pointer );
54
55   float x = static_cast<float>( wl_fixed_to_double(surfaceX) );
56   float y = static_cast<float>( wl_fixed_to_double(surfaceY) );
57
58   seat->SetPointerPosition( Vector2(x, y) ); // record last pointer position
59
60   input->PointerEnter( seat, serial, surface, x, y );
61 }
62
63 void PointerLeave( void* data,
64                    WlPointer* pointer,
65                    unsigned int serial,
66                    WlSurface* surface )
67 {
68   InputInterface* input = static_cast< InputInterface* >( data );
69   Seat* seat = input->GetSeat( pointer );
70
71   input->PointerLeave( seat, serial, surface );
72 }
73
74 void PointerMotion( void* data,
75                     WlPointer* pointer,
76                     unsigned int timestamp,
77                     wl_fixed_t surfaceX,
78                     wl_fixed_t surfaceY )
79 {
80   InputInterface* input = static_cast< InputInterface* >( data );
81   Seat* seat = input->GetSeat( pointer );
82
83   float x = static_cast<float>( wl_fixed_to_double( surfaceX) );
84   float y = static_cast<float>( wl_fixed_to_double( surfaceY) );
85
86   seat->SetPointerPosition( Vector2(x, y) ); // record last pointer position
87
88   input->PointerMotion( seat, timestamp, x, y );
89 }
90
91 void PointerButton( void* data,
92                     WlPointer* pointer,
93                     unsigned int serial,
94                     unsigned int timestamp,
95                     unsigned int button,
96                     unsigned int state )
97 {
98   InputInterface* input = static_cast< InputInterface* >( data );
99   Seat* seat = input->GetSeat( pointer );
100
101   input->PointerButton( seat, serial, timestamp, button, state );
102
103 }
104
105 void PointerAxis( void* data,
106                   WlPointer* pointer,
107                   unsigned int timestamp,
108                   unsigned int axis,
109                   wl_fixed_t value )
110 {
111   InputInterface* input = static_cast< InputInterface* >( data );
112   Seat* seat = input->GetSeat( pointer );
113
114   float length = static_cast<float>( wl_fixed_to_double( value ) );
115
116   input->PointerAxis( seat, timestamp, axis, length);
117 }
118
119 /************
120  *
121  * Key event callbacks. See wl_keyboard_listener wayland-client-protocol.h for a description
122  *
123  ************/
124 void KeyboardKeymap( void* data,
125                      WlKeyboard* keyboard,
126                      unsigned int format,
127                      int fd,
128                      unsigned int size )
129 {
130   InputInterface* input = static_cast< InputInterface* >( data );
131   Seat* seat = input->GetSeat( keyboard );
132
133   input->KeyboardKeymap( seat, format, fd, size );
134
135 }
136
137 void KeyFocusEnter( void* data,
138                     WlKeyboard* keyboard,
139                     unsigned int serial,
140                     WlSurface* surface,
141                     WlArray* keys )
142 {
143   InputInterface* input = static_cast< InputInterface* >( data );
144   Seat* seat = input->GetSeat( keyboard );
145
146   input->KeyFocusEnter( seat, serial, surface, keys );
147 }
148
149 void KeyFocusLeave( void* data,
150                     WlKeyboard* keyboard,
151                     unsigned int serial,
152                     WlSurface* surface )
153 {
154   InputInterface* input = static_cast< InputInterface* >( data );
155   Seat* seat = input->GetSeat( keyboard );
156
157   input->KeyFocusLeave( seat, serial, surface );
158 }
159
160 void KeyEvent( void* data,
161                WlKeyboard* keyboard,
162                unsigned int serial,
163                unsigned int timestamp,
164                unsigned int keycode,
165                unsigned int state)
166 {
167   InputInterface* input = static_cast< InputInterface* >( data );
168   Seat* seat = input->GetSeat( keyboard );
169
170   input->KeyEvent( seat, serial, timestamp, keycode, state );
171 }
172
173 void KeyModifiers( void* data, WlKeyboard* keyboard,
174                    unsigned int serial,
175                    unsigned int depressed,
176                    unsigned int latched,
177                    unsigned int locked,
178                    unsigned int group)
179 {
180   InputInterface* input = static_cast< InputInterface* >( data );
181   Seat* seat = input->GetSeat( keyboard );
182
183   input->KeyModifiers( seat, serial, depressed, latched, locked, group );
184
185
186 }
187
188 void KeyRepeatInfo( void* data,
189                     WlKeyboard* keyboard,
190                     int32_t rate,
191                     int32_t delay)
192 {
193   InputInterface* input = static_cast< InputInterface* >( data );
194   Seat* seat = input->GetSeat( keyboard );
195
196   input->KeyRepeatInfo( seat, rate, delay );
197
198 }
199
200 /************
201  *
202  * Touch event callbacks See wl_touch_listener wayland-client-protocol.h for a description
203  *
204  ************/
205 void TouchDown( void* data,
206                 WlTouch* touch,
207                 unsigned int serial,
208                 unsigned int timestamp,
209                 WlSurface* surface,
210                 int touchId,
211                 wl_fixed_t surfaceX,
212                 wl_fixed_t surfaceY)
213 {
214   InputInterface* input = static_cast< InputInterface* >( data );
215   Seat* seat = input->GetSeat( touch );
216
217   float x = static_cast<float>( wl_fixed_to_double( surfaceX) );
218   float y = static_cast<float>( wl_fixed_to_double( surfaceY) );
219
220
221   input->TouchDown( seat, serial, timestamp, surface, touchId, x, y );
222 }
223
224 void TouchUp( void* data,
225               WlTouch* touch,
226               unsigned int serial,
227               unsigned int timestamp,
228               int touchId )
229 {
230   InputInterface* input = static_cast< InputInterface* >( data );
231   Seat* seat = input->GetSeat( touch );
232
233   input->TouchUp( seat, serial, timestamp, touchId );
234 }
235
236 void TouchMotion( void* data,
237                   WlTouch* touch,
238                   unsigned int timestamp,
239                   int touchId,
240                   wl_fixed_t surfaceX,
241                   wl_fixed_t surfaceY)
242 {
243   InputInterface* input = static_cast< InputInterface* >( data );
244   Seat* seat = input->GetSeat( touch );
245
246   float x = static_cast<float>( wl_fixed_to_double( surfaceX) );
247   float y = static_cast<float>( wl_fixed_to_double( surfaceY) );
248
249   input->TouchMotion( seat, timestamp, touchId, x, y );
250 }
251
252 void TouchFrame( void* data, WlTouch* touch )
253 {
254   InputInterface* input = static_cast< InputInterface* >( data );
255   Seat* seat = input->GetSeat( touch );
256
257   input->TouchFrame( seat );
258 }
259
260 void TouchCancel( void* data, WlTouch* touch )
261 {
262   InputInterface* input = static_cast< InputInterface* >( data );
263   Seat* seat = input->GetSeat( touch );
264
265   input->TouchCancel( seat );
266 }
267
268 /**
269  * pointer listener interface ( wl_pointer_listener )
270  */
271 const WlPointerListener PointerListener =
272 {
273    PointerEnter,
274    PointerLeave,
275    PointerMotion,
276    PointerButton,
277    PointerAxis,
278 };
279
280 /**
281  * Keyboard listener interface ( wl_keyboard_listener )
282  */
283 const WlKeyboardListener KeyboardListener =
284 {
285    KeyboardKeymap,
286    KeyFocusEnter,
287    KeyFocusLeave,
288    KeyEvent,
289    KeyModifiers,
290    KeyRepeatInfo
291 };
292
293 /**
294  * Touch listener interface ( wl_touch_listener )
295  */
296 const WlTouchListener TouchListener =
297 {
298    TouchDown,
299    TouchUp,
300    TouchMotion,
301    TouchFrame,
302    TouchCancel
303 };
304
305 /**
306  * @brief emitted whenever a seat gains or loses the pointer, keyboard or touch capabilities.
307  * @param[in] data user data
308  * @param[in] seatInterface seat interface
309  * @param[in] caps enum containing the complete set of capabilities this seat has.
310  */
311 void SeatHandleCapabilities( void* data, WlSeat* seatInterface, unsigned int caps)
312 {
313
314   InputInterface* input = static_cast< InputInterface* >( data );
315   Seat* seat = input->GetSeat( seatInterface );
316
317
318   // go through either adding or removing pointer/keyboard/touch interfaces
319   // most devices are hot plug so this function may be called many times
320   // a single keyboard/ pointer / touch interface handles multiple devices
321   // e.g. if you plug in 2 mouses, you will still only get a single pointer_interface
322
323   if( caps & WL_SEAT_CAPABILITY_POINTER )
324   {
325     // at least one pointer available
326     seat->SetPointerInterface( Seat::INTERFACE_AVAILABLE );
327   }
328   else
329   {
330     // all pointer devices removed, or never connected
331     seat->SetPointerInterface( Seat::INTERFACE_NOT_AVAILABLE );
332   }
333
334   if( caps & WL_SEAT_CAPABILITY_KEYBOARD )
335   {
336     // at least one keyboard available
337     seat->SetKeyboardInterface( Seat::INTERFACE_AVAILABLE );
338   }
339   else
340   {
341     // all keyboard devices removed, or never connected
342     seat->SetKeyboardInterface( Seat::INTERFACE_NOT_AVAILABLE );
343   }
344
345   if( caps & WL_SEAT_CAPABILITY_TOUCH )
346   {
347     // new touch device found
348     seat->SetTouchInterface( Seat::INTERFACE_AVAILABLE );
349   }
350   else
351   {
352     // all touch devices removed, or never connected
353     seat->SetTouchInterface( Seat::INTERFACE_NOT_AVAILABLE );
354   }
355
356 }
357
358 void SeatName(void* data, WlSeat* seatInterface, const char* name)
359 {
360   InputInterface* input = static_cast< InputInterface* >( data );
361   Seat* seat = input->GetSeat( seatInterface );
362   seat->SetName( name );
363 }
364
365 const WlSeatListener SeatListener =
366 {
367   SeatHandleCapabilities, //emitted whenever a seat gains or loses the pointer, keyboard or touch capabilities.
368   SeatName, // used to help identify seat in multi-seat configurations
369 };
370
371 } // unnamed namespace
372
373
374 namespace Wayland
375 {
376
377 const WlSeatListener* GetSeatListener()
378 {
379   return &SeatListener;
380 }
381
382 const WlPointerListener* GetPointerListener()
383 {
384   return &PointerListener;
385 }
386
387 const  WlTouchListener* GetTouchListener()
388 {
389   return &TouchListener;
390 }
391
392 const WlKeyboardListener* GetKeyboardListener()
393 {
394   return &KeyboardListener;
395 }
396
397 }
398 }
399 }
400 }