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 if( !mXkbData.mContext )
229 DALI_LOG_ERROR("xkb_context_new failed\n");
234 // current formats defined in wayland-client-protocol.h
235 // WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP =0, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1=1
237 if( format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 )
239 DALI_LOG_ERROR("expected WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1\n");
244 // memory map the shared region between us and XKB
245 char* map = static_cast<char*> (mmap( NULL, size, PROT_READ, MAP_SHARED, fd, 0));
246 if( map == MAP_FAILED)
248 DALI_LOG_ERROR("mmap xkb failed\n");
253 mXkbData.mKeymap = xkb_map_new_from_string(mXkbData.mContext, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
258 if (! mXkbData.mKeymap )
260 DALI_LOG_ERROR(" xkb_map_new_from_string failed\n");
264 mXkbData.mState = xkb_state_new( mXkbData.mKeymap );
266 if( ! mXkbData.mState )
268 xkb_map_unref(mXkbData.mKeymap );
269 mXkbData.mKeymap = NULL;
273 // store the bit which each mod will set when calling xkb_state_serialize_mods
274 mXkbData.mControlMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_CTRL);
275 mXkbData.mAltMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_ALT);
276 mXkbData.mShiftMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_SHIFT);
280 Dali::KeyEvent Seat::GetDALiKeyEventFromSymbol( unsigned int serial,
281 unsigned int timestamp,
284 unsigned int modifiers )
286 char key[256] = { 0 };
287 char keyName[256] = { 0 };
290 xkb_keysym_get_name( symbol, key, sizeof(key));
293 memcpy(keyName, key, sizeof(keyName));
295 if (keyName[0] == '\0')
297 snprintf(keyName, sizeof(keyName), "Keycode-%u", symbol);
300 Dali::KeyEvent keyEvent;
302 keyEvent.keyCode = symbol; // we don't get the keycode so just the symbol
305 keyEvent.state = KeyEvent::Down;
309 keyEvent.state = KeyEvent::Up;
312 keyEvent.keyPressed = keyName;
313 keyEvent.keyPressedName = keyName;
314 keyEvent.time = timestamp;
315 keyEvent.keyModifier = modifiers;
322 Dali::KeyEvent Seat::GetDALiKeyEvent( unsigned int serial, unsigned int timestamp, unsigned int keycode, unsigned int state )
324 unsigned int code( 0 );
325 unsigned int symbolCount( 0 );
326 const xkb_keysym_t* symbols( NULL );
327 xkb_keysym_t symbol = XKB_KEY_NoSymbol;
328 char key[256] = { 0 };
329 char keyName[256] = { 0 };
331 // X11 historically has a min keycode of 8 instead of 1, XKB follow this
334 //get the key symbols
335 symbolCount = xkb_key_get_syms( mXkbData.mState, code, &symbols);
337 if( symbolCount == 1)
343 xkb_keysym_get_name( symbol, key, sizeof(key));
346 memcpy(keyName, key, sizeof(keyName));
348 if (keyName[0] == '\0')
350 snprintf(keyName, sizeof(keyName), "Keycode-%u", code);
353 // todo support key repeat settings
355 Dali::KeyEvent keyEvent;
357 keyEvent.keyCode = code;
360 keyEvent.state = KeyEvent::Down;
364 keyEvent.state = KeyEvent::Up;
366 keyEvent.keyPressed = keyName;
367 keyEvent.keyPressedName = keyName;
368 keyEvent.time = timestamp;
369 keyEvent.keyModifier = mDepressedKeyboardModifiers;
374 unsigned int Seat::GetDepressedKeyboardModifiers() const
376 return mDepressedKeyboardModifiers;
379 void Seat::SetDepressedKeyboardModifiers( unsigned int modifiers)
381 mDepressedKeyboardModifiers = modifiers;
384 void Seat::SetKeyRepeatInfo( unsigned int rate, unsigned int delay )
386 mKeyRepeatRate = rate;
387 mKeyRepeatDelay = delay;