2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/integration-api/debug.h>
30 #include "input-listeners.h"
44 const unsigned int DEFAULT_KEY_REPEAT_RATE = 25;
45 const unsigned int DEFAULT_KEY_REPEAT_DELAY = 400;
48 Seat::Seat( InputInterface* inputInterface, WlSeat* seatInterface )
52 mWaylandSeat( seatInterface ),
55 mInputInterface( inputInterface ),
56 mPointerPosition( 0, 0),
57 mDepressedKeyboardModifiers(0),
58 mKeyRepeatRate( DEFAULT_KEY_REPEAT_RATE ),
59 mKeyRepeatDelay( DEFAULT_KEY_REPEAT_DELAY )
65 DestroyPointerInterface();
66 DestroyTouchInterface();
67 DestroyKeyboardInterface();
70 void Seat::SetTextInputInterface( WlTextInput* textInput )
72 mTextInput = textInput;
75 void Seat::SetSurfaceInterface( WlSurface* surface )
80 void Seat::SetPointerInterface( InterfaceStatus status )
82 if( status == INTERFACE_AVAILABLE )
86 WlPointer* pointerInterface = wl_seat_get_pointer( mWaylandSeat );
88 // store the interface and add an event listener
89 wl_pointer_add_listener( pointerInterface, Wayland::GetPointerListener(), mInputInterface );
91 mPointer = pointerInterface;
96 DestroyPointerInterface();
100 void Seat::SetTouchInterface( InterfaceStatus status )
102 if( status == INTERFACE_AVAILABLE )
104 // check if it's configured already
107 WlTouch* touchInterface = wl_seat_get_touch( mWaylandSeat );
109 wl_touch_add_listener( touchInterface, Wayland::GetTouchListener(), mInputInterface );
111 // store the interface and add an event listener
112 mTouch = touchInterface;
117 DestroyTouchInterface();
122 void Seat::SetKeyboardInterface( InterfaceStatus status )
124 if( status == INTERFACE_AVAILABLE )
126 // check if it's configured already
129 WlKeyboard* keyboardInterface = wl_seat_get_keyboard( mWaylandSeat );
131 wl_keyboard_add_listener( keyboardInterface, Wayland::GetKeyboardListener(), mInputInterface );
133 // store the interface and add an event listener
134 mKeyboard = keyboardInterface;
139 DestroyKeyboardInterface();
143 WlPointer* Seat::GetPointerInterface()
148 WlTouch* Seat::GetTouchInterface()
153 WlKeyboard* Seat::GetKeyboardInterface()
158 WlSeat* Seat::GetSeatInterface()
163 WlTextInput* Seat::GetTextInputInterface()
168 WlSurface* Seat::GetSurface()
173 void Seat::DestroyPointerInterface()
177 wl_pointer_destroy( mPointer );
182 void Seat::DestroyTouchInterface()
186 wl_touch_destroy( mTouch );
191 void Seat::DestroyKeyboardInterface()
195 wl_keyboard_destroy( mKeyboard );
200 void Seat::SetName( const char* name )
202 mName = std::string( mName );
204 const std::string& Seat::GetName() const
209 const Dali::Vector2& Seat::GetLastPointerPosition() const
211 return mPointerPosition;
214 void Seat::SetPointerPosition( Dali::Vector2 position)
216 mPointerPosition = position;
219 void Seat::KeyboardKeymap( unsigned int format, int fd, unsigned int size )
222 if( !mXkbData.mContext )
224 mXkbData.mContext = xkb_context_new( XKB_CONTEXT_NO_FLAGS );
227 // current formats defined in wayland-client-protocol.h
228 // WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP =0, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1=1
230 if( format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 )
232 DALI_LOG_ERROR("expected WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1");
237 // memory map the shared region between us and XKB
238 char* map = static_cast<char*> (mmap( NULL, size, PROT_READ, MAP_SHARED, fd, 0));
239 if( map == MAP_FAILED)
241 DALI_LOG_ERROR("mmap xkb failed");
246 if( !mXkbData.mContext )
248 DALI_LOG_ERROR("xkb_context_new failed");
252 mXkbData.mKeymap = xkb_map_new_from_string(mXkbData.mContext, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
257 if (! mXkbData.mKeymap )
259 DALI_LOG_ERROR(" xkb_map_new_from_string failed");
263 mXkbData.mState = xkb_state_new( mXkbData.mKeymap );
265 if( ! mXkbData.mState )
267 xkb_map_unref(mXkbData.mKeymap );
268 mXkbData.mKeymap = NULL;
272 // store the bit which each mod will set when calling xkb_state_serialize_mods
273 mXkbData.mControlMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_CTRL);
274 mXkbData.mAltMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_ALT);
275 mXkbData.mShiftMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_SHIFT);
279 Dali::KeyEvent Seat::GetDALiKeyEventFromSymbol( unsigned int serial,
280 unsigned int timestamp,
283 unsigned int modifiers )
285 char key[256] = { 0 };
286 char keyName[256] = { 0 };
289 xkb_keysym_get_name( symbol, key, sizeof(key));
292 memcpy(keyName, key, sizeof(keyName));
294 if (keyName[0] == '\0')
296 snprintf(keyName, sizeof(keyName), "Keycode-%u", symbol);
299 Dali::KeyEvent keyEvent;
301 keyEvent.keyCode = symbol; // we don't get the keycode so just the symbol
304 keyEvent.state = KeyEvent::Down;
308 keyEvent.state = KeyEvent::Up;
311 keyEvent.keyPressed = keyName;
312 keyEvent.keyPressedName = keyName;
313 keyEvent.time = timestamp;
314 keyEvent.keyModifier = modifiers;
321 Dali::KeyEvent Seat::GetDALiKeyEvent( unsigned int serial, unsigned int timestamp, unsigned int keycode, unsigned int state )
323 unsigned int code( 0 );
324 unsigned int symbolCount( 0 );
325 const xkb_keysym_t* symbols( NULL );
326 xkb_keysym_t symbol = XKB_KEY_NoSymbol;
327 char key[256] = { 0 };
328 char keyName[256] = { 0 };
330 // X11 historically has a min keycode of 8 instead of 1, XKB follow this
333 //get the key symbols
334 symbolCount = xkb_key_get_syms( mXkbData.mState, code, &symbols);
336 if( symbolCount == 1)
342 xkb_keysym_get_name( symbol, key, sizeof(key));
345 memcpy(keyName, key, sizeof(keyName));
347 if (keyName[0] == '\0')
349 snprintf(keyName, sizeof(keyName), "Keycode-%u", code);
352 // todo support key repeat settings
354 Dali::KeyEvent keyEvent;
356 keyEvent.keyCode = code;
359 keyEvent.state = KeyEvent::Down;
363 keyEvent.state = KeyEvent::Up;
365 keyEvent.keyPressed = keyName;
366 keyEvent.keyPressedName = keyName;
367 keyEvent.time = timestamp;
368 keyEvent.keyModifier = mDepressedKeyboardModifiers;
373 unsigned int Seat::GetDepressedKeyboardModifiers() const
375 return mDepressedKeyboardModifiers;
378 void Seat::SetDepressedKeyboardModifiers( unsigned int modifiers)
380 mDepressedKeyboardModifiers = modifiers;
383 void Seat::SetKeyRepeatInfo( unsigned int rate, unsigned int delay )
385 mKeyRepeatRate = rate;
386 mKeyRepeatDelay = delay;