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 ),
53 mInputInterface( inputInterface ),
54 mPointerPosition( 0, 0),
55 mDepressedKeyboardModifiers(0),
56 mKeyRepeatRate( DEFAULT_KEY_REPEAT_RATE ),
57 mKeyRepeatDelay( DEFAULT_KEY_REPEAT_DELAY )
63 DestroyPointerInterface();
64 DestroyTouchInterface();
65 DestroyKeyboardInterface();
68 void Seat::SetPointerInterface( InterfaceStatus status )
70 if( status == INTERFACE_AVAILABLE )
74 WlPointer* pointerInterface = wl_seat_get_pointer( mWaylandSeat );
76 // store the interface and add an event listener
77 wl_pointer_add_listener( pointerInterface, Wayland::GetPointerListener(), mInputInterface );
79 mPointer = pointerInterface;
84 DestroyPointerInterface();
88 void Seat::SetTouchInterface( InterfaceStatus status )
90 if( status == INTERFACE_AVAILABLE )
92 // check if it's configured already
95 WlTouch* touchInterface = wl_seat_get_touch( mWaylandSeat );
97 wl_touch_add_listener( touchInterface, Wayland::GetTouchListener(), mInputInterface );
99 // store the interface and add an event listener
100 mTouch = touchInterface;
105 DestroyTouchInterface();
110 void Seat::SetKeyboardInterface( InterfaceStatus status )
112 if( status == INTERFACE_AVAILABLE )
114 // check if it's configured already
117 WlKeyboard* keyboardInterface = wl_seat_get_keyboard( mWaylandSeat );
119 wl_keyboard_add_listener( keyboardInterface, Wayland::GetKeyboardListener(), mInputInterface );
121 // store the interface and add an event listener
122 mKeyboard = keyboardInterface;
127 DestroyKeyboardInterface();
131 WlPointer* Seat::GetPointerInterface()
136 WlTouch* Seat::GetTouchInterface()
141 WlKeyboard* Seat::GetKeyboardInterface()
146 WlSeat* Seat::GetSeatInterface()
151 void Seat::DestroyPointerInterface()
155 wl_pointer_destroy( mPointer );
160 void Seat::DestroyTouchInterface()
164 wl_touch_destroy( mTouch );
169 void Seat::DestroyKeyboardInterface()
173 wl_keyboard_destroy( mKeyboard );
178 void Seat::SetName( const char* name )
180 mName = std::string( mName );
182 const std::string& Seat::GetName() const
187 const Dali::Vector2& Seat::GetLastPointerPosition() const
189 return mPointerPosition;
192 void Seat::SetPointerPosition( Dali::Vector2 position)
194 mPointerPosition = position;
197 void Seat::KeyboardKeymap( unsigned int format, int fd, unsigned int size )
200 if(!mXkbData.mContext )
202 mXkbData.mContext = xkb_context_new( XKB_CONTEXT_NO_FLAGS );
205 // current formats defined in wayland-client-protocol.h
206 // WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP =0, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1=1
208 if( format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 )
210 DALI_LOG_ERROR("expected WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1");
215 // memory map the shared region between us and XKB
216 char* map = static_cast<char*> (mmap( NULL, size, PROT_READ, MAP_SHARED, fd, 0));
217 if( map == MAP_FAILED)
219 DALI_LOG_ERROR("mmap xkb failed");
224 mXkbData.mKeymap = xkb_map_new_from_string(mXkbData.mContext, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
229 if (! mXkbData.mKeymap )
231 DALI_LOG_ERROR(" xkb_map_new_from_string failed");
235 mXkbData.mState = xkb_state_new( mXkbData.mKeymap );
237 if( ! mXkbData.mState )
239 xkb_map_unref(mXkbData.mKeymap );
240 mXkbData.mKeymap = NULL;
244 // store the bit which each mod will set when calling xkb_state_serialize_mods
245 mXkbData.mControlMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_CTRL);
246 mXkbData.mAltMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_ALT);
247 mXkbData.mShiftMask = 1 << xkb_map_mod_get_index( mXkbData.mKeymap, XKB_MOD_NAME_SHIFT);
251 Dali::KeyEvent Seat::GetDALiKeyEvent( unsigned int serial, unsigned int timestamp, unsigned int keycode, unsigned int state )
253 unsigned int code( 0 );
254 unsigned int symbolCount( 0 );
255 const xkb_keysym_t* symbols( NULL );
256 xkb_keysym_t symbol = XKB_KEY_NoSymbol;
257 char key[256] = { 0 };
258 char keyName[256] = { 0 };
260 // X11 historically has a min keycode of 8 instead of 1, XKB follow this
263 //get the key symbols
264 symbolCount = xkb_key_get_syms( mXkbData.mState, code, &symbols);
266 if( symbolCount == 1)
273 xkb_keysym_get_name( symbol, key, sizeof(key));
276 memcpy(keyName, key, sizeof(keyName));
278 if (keyName[0] == '\0')
280 snprintf(keyName, sizeof(keyName), "Keycode-%u", code);
283 // todo support key repeat settings
285 Dali::KeyEvent keyEvent;
287 keyEvent.keyCode = code;
290 keyEvent.state = KeyEvent::Down;
294 keyEvent.state = KeyEvent::Up;
296 keyEvent.keyPressedName = keyName;
297 keyEvent.time = timestamp;
298 keyEvent.keyModifier = mDepressedKeyboardModifiers;
303 unsigned int Seat::GetDepressedKeyboardModifiers() const
305 return mDepressedKeyboardModifiers;
308 void Seat::SetDepressedKeyboardModifiers( unsigned int modifiers)
310 mDepressedKeyboardModifiers = modifiers;
313 void Seat::SetKeyRepeatInfo( unsigned int rate, unsigned int delay )
315 mKeyRepeatRate = rate;
316 mKeyRepeatDelay = delay;