Refactor X11InputManager and align mappings

- getDescription implementation for X11
- Generate KeySym -> Unicode mapping, so we don't need
  to make fake events in getDescription now
- KeySym to sf::Keyboard::Key mapping
- Better names for keycode, sf::Scancode and sf::Key conversion functions
- X11InputManager doesn't need m_display now
- Move a lot of stuff into free functions
- X11InputManager now only has static members
- X11InputManager -> KeyboardImpl
- Removed redundant "sf::" where possible
- Moved some functions from sf::priv::anonymous to anonymous namespace.
- Added NullKeyCode constant for readability
This commit is contained in:
Elias Daler 2018-03-29 23:33:14 +03:00 committed by Lukas Dürrenberger
parent b62c0ed13f
commit 95f19ff478
7 changed files with 1975 additions and 571 deletions

View File

@ -118,6 +118,10 @@ elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD OR SFML_OS_OPENBSD OR SFML_OS_NETBSD)
${SRCROOT}/Unix/ClipboardImpl.cpp ${SRCROOT}/Unix/ClipboardImpl.cpp
${SRCROOT}/Unix/InputImpl.cpp ${SRCROOT}/Unix/InputImpl.cpp
${SRCROOT}/Unix/InputImpl.hpp ${SRCROOT}/Unix/InputImpl.hpp
${SRCROOT}/Unix/KeyboardImpl.hpp
${SRCROOT}/Unix/KeyboardImpl.cpp
${SRCROOT}/Unix/KeySymToSFKeyMapping.hpp
${SRCROOT}/Unix/KeySymToUnicodeMapping.hpp
${SRCROOT}/Unix/SensorImpl.cpp ${SRCROOT}/Unix/SensorImpl.cpp
${SRCROOT}/Unix/SensorImpl.hpp ${SRCROOT}/Unix/SensorImpl.hpp
${SRCROOT}/Unix/Display.cpp ${SRCROOT}/Unix/Display.cpp

View File

@ -26,8 +26,9 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/Window.hpp> // important to be included first (conflict with None) #include <SFML/Window/Window.hpp> // important to be included first (conflict with None)
#include <SFML/Window/Unix/InputImpl.hpp>
#include <SFML/Window/Unix/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <SFML/Window/Unix/InputImpl.hpp>
#include <SFML/Window/Unix/KeyboardImpl.hpp>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>
@ -39,142 +40,38 @@ namespace priv
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Key key) bool InputImpl::isKeyPressed(Keyboard::Key key)
{ {
// Get the corresponding X11 keysym return KeyboardImpl::isKeyPressed(key);
KeySym keysym = 0; }
switch (key)
{
case Keyboard::LShift: keysym = XK_Shift_L; break;
case Keyboard::RShift: keysym = XK_Shift_R; break;
case Keyboard::LControl: keysym = XK_Control_L; break;
case Keyboard::RControl: keysym = XK_Control_R; break;
case Keyboard::LAlt: keysym = XK_Alt_L; break;
case Keyboard::RAlt: keysym = XK_Alt_R; break;
case Keyboard::LSystem: keysym = XK_Super_L; break;
case Keyboard::RSystem: keysym = XK_Super_R; break;
case Keyboard::Menu: keysym = XK_Menu; break;
case Keyboard::Escape: keysym = XK_Escape; break;
case Keyboard::Semicolon: keysym = XK_semicolon; break;
case Keyboard::Slash: keysym = XK_slash; break;
case Keyboard::Equal: keysym = XK_equal; break;
case Keyboard::Hyphen: keysym = XK_minus; break;
case Keyboard::LBracket: keysym = XK_bracketleft; break;
case Keyboard::RBracket: keysym = XK_bracketright; break;
case Keyboard::Comma: keysym = XK_comma; break;
case Keyboard::Period: keysym = XK_period; break;
case Keyboard::Quote: keysym = XK_apostrophe; break;
case Keyboard::Backslash: keysym = XK_backslash; break;
case Keyboard::Tilde: keysym = XK_grave; break;
case Keyboard::Space: keysym = XK_space; break;
case Keyboard::Enter: keysym = XK_Return; break;
case Keyboard::Backspace: keysym = XK_BackSpace; break;
case Keyboard::Tab: keysym = XK_Tab; break;
case Keyboard::PageUp: keysym = XK_Prior; break;
case Keyboard::PageDown: keysym = XK_Next; break;
case Keyboard::End: keysym = XK_End; break;
case Keyboard::Home: keysym = XK_Home; break;
case Keyboard::Insert: keysym = XK_Insert; break;
case Keyboard::Delete: keysym = XK_Delete; break;
case Keyboard::Add: keysym = XK_KP_Add; break;
case Keyboard::Subtract: keysym = XK_KP_Subtract; break;
case Keyboard::Multiply: keysym = XK_KP_Multiply; break;
case Keyboard::Divide: keysym = XK_KP_Divide; break;
case Keyboard::Pause: keysym = XK_Pause; break;
case Keyboard::F1: keysym = XK_F1; break;
case Keyboard::F2: keysym = XK_F2; break;
case Keyboard::F3: keysym = XK_F3; break;
case Keyboard::F4: keysym = XK_F4; break;
case Keyboard::F5: keysym = XK_F5; break;
case Keyboard::F6: keysym = XK_F6; break;
case Keyboard::F7: keysym = XK_F7; break;
case Keyboard::F8: keysym = XK_F8; break;
case Keyboard::F9: keysym = XK_F9; break;
case Keyboard::F10: keysym = XK_F10; break;
case Keyboard::F11: keysym = XK_F11; break;
case Keyboard::F12: keysym = XK_F12; break;
case Keyboard::F13: keysym = XK_F13; break;
case Keyboard::F14: keysym = XK_F14; break;
case Keyboard::F15: keysym = XK_F15; break;
case Keyboard::Left: keysym = XK_Left; break;
case Keyboard::Right: keysym = XK_Right; break;
case Keyboard::Up: keysym = XK_Up; break;
case Keyboard::Down: keysym = XK_Down; break;
case Keyboard::Numpad0: keysym = XK_KP_Insert; break;
case Keyboard::Numpad1: keysym = XK_KP_End; break;
case Keyboard::Numpad2: keysym = XK_KP_Down; break;
case Keyboard::Numpad3: keysym = XK_KP_Page_Down; break;
case Keyboard::Numpad4: keysym = XK_KP_Left; break;
case Keyboard::Numpad5: keysym = XK_KP_Begin; break;
case Keyboard::Numpad6: keysym = XK_KP_Right; break;
case Keyboard::Numpad7: keysym = XK_KP_Home; break;
case Keyboard::Numpad8: keysym = XK_KP_Up; break;
case Keyboard::Numpad9: keysym = XK_KP_Page_Up; break;
case Keyboard::A: keysym = XK_a; break;
case Keyboard::B: keysym = XK_b; break;
case Keyboard::C: keysym = XK_c; break;
case Keyboard::D: keysym = XK_d; break;
case Keyboard::E: keysym = XK_e; break;
case Keyboard::F: keysym = XK_f; break;
case Keyboard::G: keysym = XK_g; break;
case Keyboard::H: keysym = XK_h; break;
case Keyboard::I: keysym = XK_i; break;
case Keyboard::J: keysym = XK_j; break;
case Keyboard::K: keysym = XK_k; break;
case Keyboard::L: keysym = XK_l; break;
case Keyboard::M: keysym = XK_m; break;
case Keyboard::N: keysym = XK_n; break;
case Keyboard::O: keysym = XK_o; break;
case Keyboard::P: keysym = XK_p; break;
case Keyboard::Q: keysym = XK_q; break;
case Keyboard::R: keysym = XK_r; break;
case Keyboard::S: keysym = XK_s; break;
case Keyboard::T: keysym = XK_t; break;
case Keyboard::U: keysym = XK_u; break;
case Keyboard::V: keysym = XK_v; break;
case Keyboard::W: keysym = XK_w; break;
case Keyboard::X: keysym = XK_x; break;
case Keyboard::Y: keysym = XK_y; break;
case Keyboard::Z: keysym = XK_z; break;
case Keyboard::Num0: keysym = XK_0; break;
case Keyboard::Num1: keysym = XK_1; break;
case Keyboard::Num2: keysym = XK_2; break;
case Keyboard::Num3: keysym = XK_3; break;
case Keyboard::Num4: keysym = XK_4; break;
case Keyboard::Num5: keysym = XK_5; break;
case Keyboard::Num6: keysym = XK_6; break;
case Keyboard::Num7: keysym = XK_7; break;
case Keyboard::Num8: keysym = XK_8; break;
case Keyboard::Num9: keysym = XK_9; break;
default: keysym = 0; break;
}
// Sanity checks ////////////////////////////////////////////////////////////
if (key < 0 || key >= sf::Keyboard::KeyCount) bool InputImpl::isKeyPressed(Keyboard::Scancode code)
return false; {
return KeyboardImpl::isKeyPressed(code);
}
// Open a connection with the X server // Open a connection with the X server
Display* display = OpenDisplay(); Display* display = OpenDisplay();
// Convert to keycode ////////////////////////////////////////////////////////////
KeyCode keycode = XKeysymToKeycode(display, keysym); Keyboard::Key InputImpl::localize(Keyboard::Scancode code)
if (keycode != 0) {
{ return KeyboardImpl::localize(code);
// Get the whole keyboard state }
char keys[32];
XQueryKeymap(display, keys);
// Close the connection with the X server // Close the connection with the X server
CloseDisplay(display); CloseDisplay(display);
// Check our keycode ////////////////////////////////////////////////////////////
return (keys[keycode / 8] & (1 << (keycode % 8))) != 0; Keyboard::Scancode InputImpl::unlocalize(Keyboard::Key key)
} {
else return KeyboardImpl::unlocalize(key);
{ }
// Close the connection with the X server
CloseDisplay(display);
return false;
} ////////////////////////////////////////////////////////////
String InputImpl::getDescription(Keyboard::Scancode code)
{
return KeyboardImpl::getDescription(code);
} }

View File

@ -0,0 +1,282 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_KEYSYMTOSFKEYMAPPING_HPP
#define SFML_KEYSYMTOSFKEYMAPPING_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Keyboard.hpp>
#include <X11/Xlib.h>
#include <X11/keysym.h>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief Convert X11 KeySym to sf::Keyboard::Key
///
/// \param symbol X11 KeySym
///
/// \return The corresponding sf::Keyboard::Key
///
////////////////////////////////////////////////////////////
inline Keyboard::Key keySymToSFKey(KeySym symbol)
{
switch (symbol)
{
case XK_Shift_L: return Keyboard::LShift;
case XK_Shift_R: return Keyboard::RShift;
case XK_Control_L: return Keyboard::LControl;
case XK_Control_R: return Keyboard::RControl;
case XK_Alt_L: return Keyboard::LAlt;
case XK_Alt_R: return Keyboard::RAlt;
case XK_Super_L: return Keyboard::LSystem;
case XK_Super_R: return Keyboard::RSystem;
case XK_Menu: return Keyboard::Menu;
case XK_Escape: return Keyboard::Escape;
case XK_semicolon: return Keyboard::SemiColon;
case XK_slash: return Keyboard::Slash;
case XK_equal: return Keyboard::Equal;
case XK_minus: return Keyboard::Dash;
case XK_bracketleft: return Keyboard::LBracket;
case XK_bracketright: return Keyboard::RBracket;
case XK_comma: return Keyboard::Comma;
case XK_period: return Keyboard::Period;
case XK_apostrophe: return Keyboard::Quote;
case XK_backslash: return Keyboard::BackSlash;
case XK_grave: return Keyboard::Tilde;
case XK_space: return Keyboard::Space;
case XK_Return: return Keyboard::Return;
case XK_KP_Enter: return Keyboard::Return;
case XK_BackSpace: return Keyboard::BackSpace;
case XK_Tab: return Keyboard::Tab;
case XK_Prior: return Keyboard::PageUp;
case XK_Next: return Keyboard::PageDown;
case XK_End: return Keyboard::End;
case XK_Home: return Keyboard::Home;
case XK_Insert: return Keyboard::Insert;
case XK_Delete: return Keyboard::Delete;
case XK_KP_Add: return Keyboard::Add;
case XK_KP_Subtract: return Keyboard::Subtract;
case XK_KP_Multiply: return Keyboard::Multiply;
case XK_KP_Divide: return Keyboard::Divide;
case XK_Pause: return Keyboard::Pause;
case XK_F1: return Keyboard::F1;
case XK_F2: return Keyboard::F2;
case XK_F3: return Keyboard::F3;
case XK_F4: return Keyboard::F4;
case XK_F5: return Keyboard::F5;
case XK_F6: return Keyboard::F6;
case XK_F7: return Keyboard::F7;
case XK_F8: return Keyboard::F8;
case XK_F9: return Keyboard::F9;
case XK_F10: return Keyboard::F10;
case XK_F11: return Keyboard::F11;
case XK_F12: return Keyboard::F12;
case XK_F13: return Keyboard::F13;
case XK_F14: return Keyboard::F14;
case XK_F15: return Keyboard::F15;
case XK_Left: return Keyboard::Left;
case XK_Right: return Keyboard::Right;
case XK_Up: return Keyboard::Up;
case XK_Down: return Keyboard::Down;
case XK_KP_Insert: return Keyboard::Numpad0;
case XK_KP_End: return Keyboard::Numpad1;
case XK_KP_Down: return Keyboard::Numpad2;
case XK_KP_Page_Down: return Keyboard::Numpad3;
case XK_KP_Left: return Keyboard::Numpad4;
case XK_KP_Begin: return Keyboard::Numpad5;
case XK_KP_Right: return Keyboard::Numpad6;
case XK_KP_Home: return Keyboard::Numpad7;
case XK_KP_Up: return Keyboard::Numpad8;
case XK_KP_Page_Up: return Keyboard::Numpad9;
case XK_a: return Keyboard::A;
case XK_b: return Keyboard::B;
case XK_c: return Keyboard::C;
case XK_d: return Keyboard::D;
case XK_e: return Keyboard::E;
case XK_f: return Keyboard::F;
case XK_g: return Keyboard::G;
case XK_h: return Keyboard::H;
case XK_i: return Keyboard::I;
case XK_j: return Keyboard::J;
case XK_k: return Keyboard::K;
case XK_l: return Keyboard::L;
case XK_m: return Keyboard::M;
case XK_n: return Keyboard::N;
case XK_o: return Keyboard::O;
case XK_p: return Keyboard::P;
case XK_q: return Keyboard::Q;
case XK_r: return Keyboard::R;
case XK_s: return Keyboard::S;
case XK_t: return Keyboard::T;
case XK_u: return Keyboard::U;
case XK_v: return Keyboard::V;
case XK_w: return Keyboard::W;
case XK_x: return Keyboard::X;
case XK_y: return Keyboard::Y;
case XK_z: return Keyboard::Z;
case XK_0: return Keyboard::Num0;
case XK_1: return Keyboard::Num1;
case XK_2: return Keyboard::Num2;
case XK_3: return Keyboard::Num3;
case XK_4: return Keyboard::Num4;
case XK_5: return Keyboard::Num5;
case XK_6: return Keyboard::Num6;
case XK_7: return Keyboard::Num7;
case XK_8: return Keyboard::Num8;
case XK_9: return Keyboard::Num9;
}
return Keyboard::Unknown;
}
////////////////////////////////////////////////////////////
/// \brief Convert sf::Keyboard::Key to X11 KeySym
///
/// \param key X11 sf::Keyboard::Key
///
/// \return The corresponding X11 KeySym
///
////////////////////////////////////////////////////////////
inline KeySym SFKeyToKeySym(Keyboard::Key key)
{
// Get the corresponding X11 keysym
KeySym keysym = NoSymbol;
switch (key)
{
case Keyboard::LShift: keysym = XK_Shift_L; break;
case Keyboard::RShift: keysym = XK_Shift_R; break;
case Keyboard::LControl: keysym = XK_Control_L; break;
case Keyboard::RControl: keysym = XK_Control_R; break;
case Keyboard::LAlt: keysym = XK_Alt_L; break;
case Keyboard::RAlt: keysym = XK_Alt_R; break;
case Keyboard::LSystem: keysym = XK_Super_L; break;
case Keyboard::RSystem: keysym = XK_Super_R; break;
case Keyboard::Menu: keysym = XK_Menu; break;
case Keyboard::Escape: keysym = XK_Escape; break;
case Keyboard::SemiColon: keysym = XK_semicolon; break;
case Keyboard::Slash: keysym = XK_slash; break;
case Keyboard::Equal: keysym = XK_equal; break;
case Keyboard::Dash: keysym = XK_minus; break;
case Keyboard::LBracket: keysym = XK_bracketleft; break;
case Keyboard::RBracket: keysym = XK_bracketright; break;
case Keyboard::Comma: keysym = XK_comma; break;
case Keyboard::Period: keysym = XK_period; break;
case Keyboard::Quote: keysym = XK_apostrophe; break;
case Keyboard::BackSlash: keysym = XK_backslash; break;
case Keyboard::Tilde: keysym = XK_grave; break;
case Keyboard::Space: keysym = XK_space; break;
case Keyboard::Return: keysym = XK_Return; break;
case Keyboard::BackSpace: keysym = XK_BackSpace; break;
case Keyboard::Tab: keysym = XK_Tab; break;
case Keyboard::PageUp: keysym = XK_Prior; break;
case Keyboard::PageDown: keysym = XK_Next; break;
case Keyboard::End: keysym = XK_End; break;
case Keyboard::Home: keysym = XK_Home; break;
case Keyboard::Insert: keysym = XK_Insert; break;
case Keyboard::Delete: keysym = XK_Delete; break;
case Keyboard::Add: keysym = XK_KP_Add; break;
case Keyboard::Subtract: keysym = XK_KP_Subtract; break;
case Keyboard::Multiply: keysym = XK_KP_Multiply; break;
case Keyboard::Divide: keysym = XK_KP_Divide; break;
case Keyboard::Pause: keysym = XK_Pause; break;
case Keyboard::F1: keysym = XK_F1; break;
case Keyboard::F2: keysym = XK_F2; break;
case Keyboard::F3: keysym = XK_F3; break;
case Keyboard::F4: keysym = XK_F4; break;
case Keyboard::F5: keysym = XK_F5; break;
case Keyboard::F6: keysym = XK_F6; break;
case Keyboard::F7: keysym = XK_F7; break;
case Keyboard::F8: keysym = XK_F8; break;
case Keyboard::F9: keysym = XK_F9; break;
case Keyboard::F10: keysym = XK_F10; break;
case Keyboard::F11: keysym = XK_F11; break;
case Keyboard::F12: keysym = XK_F12; break;
case Keyboard::F13: keysym = XK_F13; break;
case Keyboard::F14: keysym = XK_F14; break;
case Keyboard::F15: keysym = XK_F15; break;
case Keyboard::Left: keysym = XK_Left; break;
case Keyboard::Right: keysym = XK_Right; break;
case Keyboard::Up: keysym = XK_Up; break;
case Keyboard::Down: keysym = XK_Down; break;
case Keyboard::Numpad0: keysym = XK_KP_Insert; break;
case Keyboard::Numpad1: keysym = XK_KP_End; break;
case Keyboard::Numpad2: keysym = XK_KP_Down; break;
case Keyboard::Numpad3: keysym = XK_KP_Page_Down; break;
case Keyboard::Numpad4: keysym = XK_KP_Left; break;
case Keyboard::Numpad5: keysym = XK_KP_Begin; break;
case Keyboard::Numpad6: keysym = XK_KP_Right; break;
case Keyboard::Numpad7: keysym = XK_KP_Home; break;
case Keyboard::Numpad8: keysym = XK_KP_Up; break;
case Keyboard::Numpad9: keysym = XK_KP_Page_Up; break;
case Keyboard::A: keysym = XK_a; break;
case Keyboard::B: keysym = XK_b; break;
case Keyboard::C: keysym = XK_c; break;
case Keyboard::D: keysym = XK_d; break;
case Keyboard::E: keysym = XK_e; break;
case Keyboard::F: keysym = XK_f; break;
case Keyboard::G: keysym = XK_g; break;
case Keyboard::H: keysym = XK_h; break;
case Keyboard::I: keysym = XK_i; break;
case Keyboard::J: keysym = XK_j; break;
case Keyboard::K: keysym = XK_k; break;
case Keyboard::L: keysym = XK_l; break;
case Keyboard::M: keysym = XK_m; break;
case Keyboard::N: keysym = XK_n; break;
case Keyboard::O: keysym = XK_o; break;
case Keyboard::P: keysym = XK_p; break;
case Keyboard::Q: keysym = XK_q; break;
case Keyboard::R: keysym = XK_r; break;
case Keyboard::S: keysym = XK_s; break;
case Keyboard::T: keysym = XK_t; break;
case Keyboard::U: keysym = XK_u; break;
case Keyboard::V: keysym = XK_v; break;
case Keyboard::W: keysym = XK_w; break;
case Keyboard::X: keysym = XK_x; break;
case Keyboard::Y: keysym = XK_y; break;
case Keyboard::Z: keysym = XK_z; break;
case Keyboard::Num0: keysym = XK_0; break;
case Keyboard::Num1: keysym = XK_1; break;
case Keyboard::Num2: keysym = XK_2; break;
case Keyboard::Num3: keysym = XK_3; break;
case Keyboard::Num4: keysym = XK_4; break;
case Keyboard::Num5: keysym = XK_5; break;
case Keyboard::Num6: keysym = XK_6; break;
case Keyboard::Num7: keysym = XK_7; break;
case Keyboard::Num8: keysym = XK_8; break;
case Keyboard::Num9: keysym = XK_9; break;
}
return keysym;
}
} // namespace priv
} // namespace sf
#endif // SFML_KEYSYMTOSFKEYMAPPING_HPP

File diff suppressed because it is too large Load Diff

View File

@ -25,259 +25,41 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/Unix/X11InputManager.hpp> #include <SFML/Window/Unix/KeyboardImpl.hpp>
#include <SFML/System/Utf.hpp>
#include <SFML/Window/Unix/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <X11/Xlib.h> #include <SFML/Window/Unix/KeySymToSFKeyMapping.hpp>
#include <SFML/Window/Unix/KeySymToUnicodeMapping.hpp>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstring> #include <cstring>
namespace sf
{
namespace priv
{
namespace { namespace {
////////////////////////////////////////////////////////////
sf::Keyboard::Key keysymToSF(KeySym symbol)
{
switch (symbol)
{
case XK_Shift_L: return sf::Keyboard::LShift;
case XK_Shift_R: return sf::Keyboard::RShift;
case XK_Control_L: return sf::Keyboard::LControl;
case XK_Control_R: return sf::Keyboard::RControl;
case XK_Alt_L: return sf::Keyboard::LAlt;
case XK_Alt_R: return sf::Keyboard::RAlt;
case XK_Super_L: return sf::Keyboard::LSystem;
case XK_Super_R: return sf::Keyboard::RSystem;
case XK_Menu: return sf::Keyboard::Menu;
case XK_Escape: return sf::Keyboard::Escape;
case XK_semicolon: return sf::Keyboard::SemiColon;
case XK_slash: return sf::Keyboard::Slash;
case XK_equal: return sf::Keyboard::Equal;
case XK_minus: return sf::Keyboard::Dash;
case XK_bracketleft: return sf::Keyboard::LBracket;
case XK_bracketright: return sf::Keyboard::RBracket;
case XK_comma: return sf::Keyboard::Comma;
case XK_period: return sf::Keyboard::Period;
case XK_apostrophe: return sf::Keyboard::Quote;
case XK_backslash: return sf::Keyboard::BackSlash;
case XK_grave: return sf::Keyboard::Tilde;
case XK_space: return sf::Keyboard::Space;
case XK_Return: return sf::Keyboard::Return;
case XK_KP_Enter: return sf::Keyboard::Return;
case XK_BackSpace: return sf::Keyboard::BackSpace;
case XK_Tab: return sf::Keyboard::Tab;
case XK_Prior: return sf::Keyboard::PageUp;
case XK_Next: return sf::Keyboard::PageDown;
case XK_End: return sf::Keyboard::End;
case XK_Home: return sf::Keyboard::Home;
case XK_Insert: return sf::Keyboard::Insert;
case XK_Delete: return sf::Keyboard::Delete;
case XK_KP_Add: return sf::Keyboard::Add;
case XK_KP_Subtract: return sf::Keyboard::Subtract;
case XK_KP_Multiply: return sf::Keyboard::Multiply;
case XK_KP_Divide: return sf::Keyboard::Divide;
case XK_Pause: return sf::Keyboard::Pause;
case XK_F1: return sf::Keyboard::F1;
case XK_F2: return sf::Keyboard::F2;
case XK_F3: return sf::Keyboard::F3;
case XK_F4: return sf::Keyboard::F4;
case XK_F5: return sf::Keyboard::F5;
case XK_F6: return sf::Keyboard::F6;
case XK_F7: return sf::Keyboard::F7;
case XK_F8: return sf::Keyboard::F8;
case XK_F9: return sf::Keyboard::F9;
case XK_F10: return sf::Keyboard::F10;
case XK_F11: return sf::Keyboard::F11;
case XK_F12: return sf::Keyboard::F12;
case XK_F13: return sf::Keyboard::F13;
case XK_F14: return sf::Keyboard::F14;
case XK_F15: return sf::Keyboard::F15;
case XK_Left: return sf::Keyboard::Left;
case XK_Right: return sf::Keyboard::Right;
case XK_Up: return sf::Keyboard::Up;
case XK_Down: return sf::Keyboard::Down;
case XK_KP_Insert: return sf::Keyboard::Numpad0;
case XK_KP_End: return sf::Keyboard::Numpad1;
case XK_KP_Down: return sf::Keyboard::Numpad2;
case XK_KP_Page_Down: return sf::Keyboard::Numpad3;
case XK_KP_Left: return sf::Keyboard::Numpad4;
case XK_KP_Begin: return sf::Keyboard::Numpad5;
case XK_KP_Right: return sf::Keyboard::Numpad6;
case XK_KP_Home: return sf::Keyboard::Numpad7;
case XK_KP_Up: return sf::Keyboard::Numpad8;
case XK_KP_Page_Up: return sf::Keyboard::Numpad9;
case XK_a: return sf::Keyboard::A;
case XK_b: return sf::Keyboard::B;
case XK_c: return sf::Keyboard::C;
case XK_d: return sf::Keyboard::D;
case XK_e: return sf::Keyboard::E;
case XK_f: return sf::Keyboard::F;
case XK_g: return sf::Keyboard::G;
case XK_h: return sf::Keyboard::H;
case XK_i: return sf::Keyboard::I;
case XK_j: return sf::Keyboard::J;
case XK_k: return sf::Keyboard::K;
case XK_l: return sf::Keyboard::L;
case XK_m: return sf::Keyboard::M;
case XK_n: return sf::Keyboard::N;
case XK_o: return sf::Keyboard::O;
case XK_p: return sf::Keyboard::P;
case XK_q: return sf::Keyboard::Q;
case XK_r: return sf::Keyboard::R;
case XK_s: return sf::Keyboard::S;
case XK_t: return sf::Keyboard::T;
case XK_u: return sf::Keyboard::U;
case XK_v: return sf::Keyboard::V;
case XK_w: return sf::Keyboard::W;
case XK_x: return sf::Keyboard::X;
case XK_y: return sf::Keyboard::Y;
case XK_z: return sf::Keyboard::Z;
case XK_0: return sf::Keyboard::Num0;
case XK_1: return sf::Keyboard::Num1;
case XK_2: return sf::Keyboard::Num2;
case XK_3: return sf::Keyboard::Num3;
case XK_4: return sf::Keyboard::Num4;
case XK_5: return sf::Keyboard::Num5;
case XK_6: return sf::Keyboard::Num6;
case XK_7: return sf::Keyboard::Num7;
case XK_8: return sf::Keyboard::Num8;
case XK_9: return sf::Keyboard::Num9;
}
return sf::Keyboard::Unknown;
}
const KeyCode NullKeyCode = 0;
KeyCode scancodeToKeycode[sf::Keyboard::ScanCodeCount] = { NullKeyCode }; ///< Mapping of SFML scancode to X11 KeyCode
sf::Keyboard::Scancode keycodeToScancode[256] = { sf::Keyboard::ScanUnknown}; ///< Mapping of X11 KeyCode to SFML scancode
bool isMappingInitialized = false;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
KeySym SFtoKeysym(sf::Keyboard::Key key) bool isValidKeycode(KeyCode keycode)
{ {
// Get the corresponding X11 keysym // Valid key code range is [8,255], according to the Xlib manual
KeySym keysym = 0; return keycode >= 8 || keycode <= 255;
switch (key)
{
case sf::Keyboard::LShift: keysym = XK_Shift_L; break;
case sf::Keyboard::RShift: keysym = XK_Shift_R; break;
case sf::Keyboard::LControl: keysym = XK_Control_L; break;
case sf::Keyboard::RControl: keysym = XK_Control_R; break;
case sf::Keyboard::LAlt: keysym = XK_Alt_L; break;
case sf::Keyboard::RAlt: keysym = XK_Alt_R; break;
case sf::Keyboard::LSystem: keysym = XK_Super_L; break;
case sf::Keyboard::RSystem: keysym = XK_Super_R; break;
case sf::Keyboard::Menu: keysym = XK_Menu; break;
case sf::Keyboard::Escape: keysym = XK_Escape; break;
case sf::Keyboard::SemiColon: keysym = XK_semicolon; break;
case sf::Keyboard::Slash: keysym = XK_slash; break;
case sf::Keyboard::Equal: keysym = XK_equal; break;
case sf::Keyboard::Dash: keysym = XK_minus; break;
case sf::Keyboard::LBracket: keysym = XK_bracketleft; break;
case sf::Keyboard::RBracket: keysym = XK_bracketright; break;
case sf::Keyboard::Comma: keysym = XK_comma; break;
case sf::Keyboard::Period: keysym = XK_period; break;
case sf::Keyboard::Quote: keysym = XK_apostrophe; break;
case sf::Keyboard::BackSlash: keysym = XK_backslash; break;
case sf::Keyboard::Tilde: keysym = XK_grave; break;
case sf::Keyboard::Space: keysym = XK_space; break;
case sf::Keyboard::Return: keysym = XK_Return; break;
case sf::Keyboard::BackSpace: keysym = XK_BackSpace; break;
case sf::Keyboard::Tab: keysym = XK_Tab; break;
case sf::Keyboard::PageUp: keysym = XK_Prior; break;
case sf::Keyboard::PageDown: keysym = XK_Next; break;
case sf::Keyboard::End: keysym = XK_End; break;
case sf::Keyboard::Home: keysym = XK_Home; break;
case sf::Keyboard::Insert: keysym = XK_Insert; break;
case sf::Keyboard::Delete: keysym = XK_Delete; break;
case sf::Keyboard::Add: keysym = XK_KP_Add; break;
case sf::Keyboard::Subtract: keysym = XK_KP_Subtract; break;
case sf::Keyboard::Multiply: keysym = XK_KP_Multiply; break;
case sf::Keyboard::Divide: keysym = XK_KP_Divide; break;
case sf::Keyboard::Pause: keysym = XK_Pause; break;
case sf::Keyboard::F1: keysym = XK_F1; break;
case sf::Keyboard::F2: keysym = XK_F2; break;
case sf::Keyboard::F3: keysym = XK_F3; break;
case sf::Keyboard::F4: keysym = XK_F4; break;
case sf::Keyboard::F5: keysym = XK_F5; break;
case sf::Keyboard::F6: keysym = XK_F6; break;
case sf::Keyboard::F7: keysym = XK_F7; break;
case sf::Keyboard::F8: keysym = XK_F8; break;
case sf::Keyboard::F9: keysym = XK_F9; break;
case sf::Keyboard::F10: keysym = XK_F10; break;
case sf::Keyboard::F11: keysym = XK_F11; break;
case sf::Keyboard::F12: keysym = XK_F12; break;
case sf::Keyboard::F13: keysym = XK_F13; break;
case sf::Keyboard::F14: keysym = XK_F14; break;
case sf::Keyboard::F15: keysym = XK_F15; break;
case sf::Keyboard::Left: keysym = XK_Left; break;
case sf::Keyboard::Right: keysym = XK_Right; break;
case sf::Keyboard::Up: keysym = XK_Up; break;
case sf::Keyboard::Down: keysym = XK_Down; break;
case sf::Keyboard::Numpad0: keysym = XK_KP_Insert; break;
case sf::Keyboard::Numpad1: keysym = XK_KP_End; break;
case sf::Keyboard::Numpad2: keysym = XK_KP_Down; break;
case sf::Keyboard::Numpad3: keysym = XK_KP_Page_Down; break;
case sf::Keyboard::Numpad4: keysym = XK_KP_Left; break;
case sf::Keyboard::Numpad5: keysym = XK_KP_Begin; break;
case sf::Keyboard::Numpad6: keysym = XK_KP_Right; break;
case sf::Keyboard::Numpad7: keysym = XK_KP_Home; break;
case sf::Keyboard::Numpad8: keysym = XK_KP_Up; break;
case sf::Keyboard::Numpad9: keysym = XK_KP_Page_Up; break;
case sf::Keyboard::A: keysym = XK_a; break;
case sf::Keyboard::B: keysym = XK_b; break;
case sf::Keyboard::C: keysym = XK_c; break;
case sf::Keyboard::D: keysym = XK_d; break;
case sf::Keyboard::E: keysym = XK_e; break;
case sf::Keyboard::F: keysym = XK_f; break;
case sf::Keyboard::G: keysym = XK_g; break;
case sf::Keyboard::H: keysym = XK_h; break;
case sf::Keyboard::I: keysym = XK_i; break;
case sf::Keyboard::J: keysym = XK_j; break;
case sf::Keyboard::K: keysym = XK_k; break;
case sf::Keyboard::L: keysym = XK_l; break;
case sf::Keyboard::M: keysym = XK_m; break;
case sf::Keyboard::N: keysym = XK_n; break;
case sf::Keyboard::O: keysym = XK_o; break;
case sf::Keyboard::P: keysym = XK_p; break;
case sf::Keyboard::Q: keysym = XK_q; break;
case sf::Keyboard::R: keysym = XK_r; break;
case sf::Keyboard::S: keysym = XK_s; break;
case sf::Keyboard::T: keysym = XK_t; break;
case sf::Keyboard::U: keysym = XK_u; break;
case sf::Keyboard::V: keysym = XK_v; break;
case sf::Keyboard::W: keysym = XK_w; break;
case sf::Keyboard::X: keysym = XK_x; break;
case sf::Keyboard::Y: keysym = XK_y; break;
case sf::Keyboard::Z: keysym = XK_z; break;
case sf::Keyboard::Num0: keysym = XK_0; break;
case sf::Keyboard::Num1: keysym = XK_1; break;
case sf::Keyboard::Num2: keysym = XK_2; break;
case sf::Keyboard::Num3: keysym = XK_3; break;
case sf::Keyboard::Num4: keysym = XK_4; break;
case sf::Keyboard::Num5: keysym = XK_5; break;
case sf::Keyboard::Num6: keysym = XK_6; break;
case sf::Keyboard::Num7: keysym = XK_7; break;
case sf::Keyboard::Num8: keysym = XK_8; break;
case sf::Keyboard::Num9: keysym = XK_9; break;
default: keysym = 0; break;
}
return keysym;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode) sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode)
{ {
KeySym keySym; if (!isValidKeycode(keycode))
// Valid key code range is [8,255], according to the Xlib manual
if (keycode < 8 || keycode > 255)
return sf::Keyboard::ScanUnknown; return sf::Keyboard::ScanUnknown;
// Try secondary keysym, for numeric keypad keys // Try secondary keysym, for numeric keypad keys
// Note: This way we always force "NumLock = ON", which is intentional // Note: This way we always force "NumLock = ON", which is intentional
// since the returned key code should correspond to a physical // since the returned key code should correspond to a physical
// location. // location.
keySym = XkbKeycodeToKeysym(display, keycode, 0, 1); KeySym keySym = XkbKeycodeToKeysym(display, keycode, 0, 1);
switch (keySym) switch (keySym)
{ {
case XK_KP_0: return sf::Keyboard::ScanNumpad0; case XK_KP_0: return sf::Keyboard::ScanNumpad0;
@ -442,45 +224,16 @@ sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode)
return sf::Keyboard::ScanUnknown; return sf::Keyboard::ScanUnknown;
} }
void initMapping()
} // anonymous namespace
////////////////////////////////////////////////////////////
X11InputManager::X11InputManager() :
m_display(NULL)
{ {
for (int i = 0; i < sf::Keyboard::ScanCodeCount; ++i) Display* display = sf::priv::OpenDisplay();
{
m_scancodeToKeycode[i] = 0;
}
for (int i = 0; i < 256; ++i)
{
m_keycodeToScancode[i] = sf::Keyboard::ScanUnknown;
}
}
////////////////////////////////////////////////////////////
X11InputManager& X11InputManager::getInstance()
{
static X11InputManager instance;
return instance;
}
////////////////////////////////////////////////////////////
void X11InputManager::initialize(Display* display)
{
m_display = display;
// Find the X11 key code -> SFML key code mapping // Find the X11 key code -> SFML key code mapping
// This code was inspired by GLFW implementation // This code was inspired by GLFW implementation
char name[XkbKeyNameLength + 1]; char name[XkbKeyNameLength + 1];
XkbDescPtr desc = XkbGetMap(m_display, 0, XkbUseCoreKbd); XkbDescPtr desc = XkbGetMap(display, 0, XkbUseCoreKbd);
XkbGetNames(m_display, XkbKeyNamesMask, desc); XkbGetNames(display, XkbKeyNamesMask, desc);
sf::Keyboard::Scancode sc; sf::Keyboard::Scancode sc;
for (int keycode = desc->min_key_code; keycode <= desc->max_key_code; ++keycode) for (int keycode = desc->min_key_code; keycode <= desc->max_key_code; ++keycode)
@ -538,10 +291,10 @@ void X11InputManager::initialize(Display* display)
else if (strcmp(name, "AB10") == 0) sc = sf::Keyboard::ScanForwardSlash; else if (strcmp(name, "AB10") == 0) sc = sf::Keyboard::ScanForwardSlash;
else sc = sf::Keyboard::ScanUnknown; else sc = sf::Keyboard::ScanUnknown;
if ((keycode >= 0) && (keycode < 256)) if (isValidKeycode(keycode))
{ {
m_scancodeToKeycode[sc] = keycode; scancodeToKeycode[sc] = keycode;
m_keycodeToScancode[keycode] = sc; keycodeToScancode[keycode] = sc;
} }
} }
@ -549,29 +302,81 @@ void X11InputManager::initialize(Display* display)
XkbFreeKeyboard(desc, 0, True); XkbFreeKeyboard(desc, 0, True);
// Translate un-translated keycodes using traditional X11 KeySym lookups // Translate un-translated keycodes using traditional X11 KeySym lookups
for (int keycode = 0; keycode < 256; ++keycode) // Valid keycodes are [8;255], so we only initialize them
for (int keycode = 8; keycode < 256; ++keycode)
{ {
if (m_keycodeToScancode[keycode] == sf::Keyboard::ScanUnknown) if (keycodeToScancode[keycode] == sf::Keyboard::ScanUnknown)
{ {
sf::Keyboard::Scancode sc = translateKeyCode(m_display, keycode); sf::Keyboard::Scancode sc = translateKeyCode(display, keycode);
m_scancodeToKeycode[sc] = keycode; scancodeToKeycode[sc] = keycode;
m_keycodeToScancode[keycode] = sc; keycodeToScancode[keycode] = sc;
} }
} }
sf::priv::CloseDisplay(display);
isMappingInitialized = true;
} }
namespace {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool isKeyPressedImpl(Display* display, KeyCode keycode) KeyCode SFScancodeToKeyCode(sf::Keyboard::Scancode code)
{ {
if (keycode != 0) if (!isMappingInitialized)
initMapping();
return scancodeToKeycode[code];
}
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode keyCodeToSFScancode(KeyCode code)
{
if (!isMappingInitialized)
initMapping();
if (isValidKeycode(code))
return keycodeToScancode[code];
return sf::Keyboard::ScanUnknown;
}
////////////////////////////////////////////////////////////
KeyCode SFKeyToKeyCode(sf::Keyboard::Key key)
{
KeySym keysym = sf::priv::SFKeyToKeySym(key);
if (keysym != NoSymbol)
{ {
Display* display = sf::priv::OpenDisplay();
KeyCode keycode = XKeysymToKeycode(display, keysym);
sf::priv::CloseDisplay(display);
return keycode;
}
return NullKeyCode;
}
////////////////////////////////////////////////////////////
KeySym SFScancodeToKeySym(sf::Keyboard::Scancode code)
{
Display* display = sf::priv::OpenDisplay();
KeySym keysym = NoSymbol;
KeyCode keycode = SFScancodeToKeyCode(code);
if (keycode != NullKeyCode) // ensure that this Scancode is mapped to keycode
keysym = XkbKeycodeToKeysym(display, keycode, 0, 0);
sf::priv::CloseDisplay(display);
return keysym;
}
////////////////////////////////////////////////////////////
bool isKeyPressedImpl(KeyCode keycode)
{
if (keycode != NullKeyCode)
{
Display* display = sf::priv::OpenDisplay();
// Get the whole keyboard state // Get the whole keyboard state
char keys[32]; char keys[32];
XQueryKeymap(display, keys); XQueryKeymap(display, keys);
sf::priv::CloseDisplay(display);
// Check our keycode // Check our keycode
return (keys[keycode / 8] & (1 << (keycode % 8))) != 0; return (keys[keycode / 8] & (1 << (keycode % 8))) != 0;
} }
@ -580,57 +385,171 @@ bool isKeyPressedImpl(Display* display, KeyCode keycode)
} // anonymous namespace } // anonymous namespace
//////////////////////////////////////////////////////////// namespace sf
bool X11InputManager::isKeyPressed(sf::Keyboard::Key key) const
{ {
KeyCode keycode = SFtoKeyCode(key); namespace priv
return isKeyPressedImpl(m_display, keycode); {
////////////////////////////////////////////////////////////
bool KeyboardImpl::isKeyPressed(Keyboard::Key key)
{
KeyCode keycode = SFKeyToKeyCode(key);
return isKeyPressedImpl(keycode);
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool X11InputManager::isKeyPressed(sf::Keyboard::Scancode code) const bool KeyboardImpl::isKeyPressed(Keyboard::Scancode code)
{ {
KeyCode keycode = SFtoKeyCode(code); KeyCode keycode = SFScancodeToKeyCode(code);
return isKeyPressedImpl(m_display, keycode); return isKeyPressedImpl(keycode);
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Scancode X11InputManager::unlocalize(sf::Keyboard::Key key) const Keyboard::Scancode KeyboardImpl::unlocalize(Keyboard::Key key)
{ {
KeyCode keycode = SFtoKeyCode(key); KeyCode keycode = SFKeyToKeyCode(key);
return keyCodeToSF(keycode); return keyCodeToSFScancode(keycode);
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Key X11InputManager::localize(sf::Keyboard::Scancode code) const Keyboard::Key KeyboardImpl::localize(Keyboard::Scancode code)
{ {
KeyCode keycode = SFtoKeyCode(code); KeySym keysym = SFScancodeToKeySym(code);
KeySym keysym = XkbKeycodeToKeysym(m_display, keycode, 0, 0); return keySymToSFKey(keysym);
return keysymToSF(keysym); }
////////////////////////////////////////////////////////////
String KeyboardImpl::getDescription(Keyboard::Scancode code)
{
bool checkInput = true;
// these scancodes actually correspond to keys with input
// but we want to return their description, not their behaviour
if (code == Keyboard::ScanEnter ||
code == Keyboard::ScanReturn ||
code == Keyboard::ScanTab ||
code == Keyboard::ScanDelete ||
code == Keyboard::ScanBackspace ||
code == Keyboard::ScanSpace)
{
checkInput = false;
}
if (checkInput)
{
KeySym keysym = SFScancodeToKeySym(code);
Uint32 unicode = keysymToUnicode(keysym);
if (unicode != 0)
return String(unicode);
}
// Fallback to our best guess for the keys that are known to be independent of the layout.
switch (code)
{
case Keyboard::ScanEnter: return "Enter";
case Keyboard::ScanEscape: return "Escape";
case Keyboard::ScanBackspace: return "Backspace";
case Keyboard::ScanTab: return "Tab";
case Keyboard::ScanSpace: return "Space";
case Keyboard::ScanF1: return "F1";
case Keyboard::ScanF2: return "F2";
case Keyboard::ScanF3: return "F3";
case Keyboard::ScanF4: return "F4";
case Keyboard::ScanF5: return "F5";
case Keyboard::ScanF6: return "F6";
case Keyboard::ScanF7: return "F7";
case Keyboard::ScanF8: return "F8";
case Keyboard::ScanF9: return "F9";
case Keyboard::ScanF10: return "F10";
case Keyboard::ScanF11: return "F11";
case Keyboard::ScanF12: return "F12";
case Keyboard::ScanF13: return "F13";
case Keyboard::ScanF14: return "F14";
case Keyboard::ScanF15: return "F15";
case Keyboard::ScanCapsLock: return "CapsLock";
case Keyboard::ScanPrintScreen: return "PrintScreen";
case Keyboard::ScanScrollLock: return "ScrollLock";
case Keyboard::ScanPause: return "Pause";
case Keyboard::ScanInsert: return "Insert";
case Keyboard::ScanHome: return "Home";
case Keyboard::ScanPageUp: return "PageUp";
case Keyboard::ScanDelete: return "Delete";
case Keyboard::ScanEnd: return "End";
case Keyboard::ScanPageDown: return "PageDown";
case Keyboard::ScanLeft: return "Left Arrow";
case Keyboard::ScanRight: return "Right Arrow";
case Keyboard::ScanDown: return "Down Arrow";
case Keyboard::ScanUp: return "Up Arrow";
case Keyboard::ScanNumLock: return "NumLock";
case Keyboard::ScanDivide: return "Divide (Numpad)";
case Keyboard::ScanMultiply: return "Multiply (Numpad)";
case Keyboard::ScanMinus: return "Minux (Numpad)";
case Keyboard::ScanPlus: return "Plus (Numpad)";
case Keyboard::ScanPadEquals: return "Equals (Numpad)";
case Keyboard::ScanReturn: return "Return (Numpad)";
case Keyboard::ScanDecimal: return "Decimal (Numpad)";
case Keyboard::ScanNumpad0: return "0 (Numpad)";
case Keyboard::ScanNumpad1: return "1 (Numpad)";
case Keyboard::ScanNumpad2: return "2 (Numpad)";
case Keyboard::ScanNumpad3: return "3 (Numpad)";
case Keyboard::ScanNumpad4: return "4 (Numpad)";
case Keyboard::ScanNumpad5: return "5 (Numpad)";
case Keyboard::ScanNumpad6: return "6 (Numpad)";
case Keyboard::ScanNumpad7: return "7 (Numpad)";
case Keyboard::ScanNumpad8: return "8 (Numpad)";
case Keyboard::ScanNumpad9: return "9 (Numpad)";
case Keyboard::ScanApplication: return "Application";
case Keyboard::ScanExecute: return "Execute";
case Keyboard::ScanHelp: return "Help";
case Keyboard::ScanMenu: return "Menu";
case Keyboard::ScanSelect: return "Select";
case Keyboard::ScanStop: return "Stop";
case Keyboard::ScanAgain: return "Again";
case Keyboard::ScanUndo: return "Undo";
case Keyboard::ScanCut: return "Cut";
case Keyboard::ScanCopy: return "Copy";
case Keyboard::ScanPaste: return "Paste";
case Keyboard::ScanFind: return "Find";
case Keyboard::ScanMute: return "Mute";
case Keyboard::ScanVolumeUp: return "Volume Up";
case Keyboard::ScanVolumeDown: return "Volume Down";
case Keyboard::ScanLControl: return "Control (Left)";
case Keyboard::ScanLShift: return "Shift (Left)";
case Keyboard::ScanLAlt: return "Alt (Left)";
case Keyboard::ScanLSystem: return "Meta (Left)";
case Keyboard::ScanRControl: return "Control (Right)";
case Keyboard::ScanRShift: return "Shift (Right)";
case Keyboard::ScanRAlt: return "Alt (Right)";
case Keyboard::ScanRSystem: return "Meta (Right)";
}
return "Unknown Scancode"; // no guess good enough possible.
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::String X11InputManager::getDescription(Keyboard::Scancode code) const Keyboard::Key KeyboardImpl::getKeyFromEvent(XKeyEvent& event)
{ {
return ""; // TODO Keyboard::Key key = Keyboard::Unknown;
}
////////////////////////////////////////////////////////////
sf::Keyboard::Key X11InputManager::getKeyFromEvent(XKeyEvent& event) const
{
sf::Keyboard::Key key = Keyboard::Unknown;
// Try each KeySym index (modifier group) until we get a match // Try each KeySym index (modifier group) until we get a match
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
{ {
// Get the SFML keyboard code from the keysym of the key that has been pressed // Get the SFML keyboard code from the keysym of the key that has been pressed
KeySym keysym = XLookupKeysym(&event, i); KeySym keysym = XLookupKeysym(&event, i);
key = keysymToSF(keysym); key = keySymToSFKey(keysym);
if (key != Keyboard::Unknown) if (key != Keyboard::Unknown)
break; break;
@ -640,31 +559,9 @@ sf::Keyboard::Key X11InputManager::getKeyFromEvent(XKeyEvent& event) const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Scancode X11InputManager::getScancodeFromEvent(XKeyEvent& event) const Keyboard::Scancode KeyboardImpl::getScancodeFromEvent(XKeyEvent& event)
{ {
return keyCodeToSF(event.keycode); return keyCodeToSFScancode(event.keycode);
}
////////////////////////////////////////////////////////////
KeyCode X11InputManager::SFtoKeyCode(sf::Keyboard::Key key) const
{
KeySym keysym = SFtoKeysym(key);
return XKeysymToKeycode(m_display, keysym);
}
////////////////////////////////////////////////////////////
KeyCode X11InputManager::SFtoKeyCode(sf::Keyboard::Scancode code) const
{
return m_scancodeToKeycode[code];
}
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode X11InputManager::keyCodeToSF(KeyCode code) const
{
return m_keycodeToScancode[code];
} }
} // namespace priv } // namespace priv

View File

@ -22,8 +22,8 @@
// //
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#ifndef SFML_X11INPUTMANAGER_HPP #ifndef SFML_KEYBOARD_IMPL_HPP
#define SFML_X11INPUTMANAGER_HPP #define SFML_KEYBOARD_IMPL_HPP
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Headers // Headers
@ -44,67 +44,39 @@ namespace priv {
/// Its purpose is to help sf::priv::InputImpl class. /// Its purpose is to help sf::priv::InputImpl class.
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
class X11InputManager class KeyboardImpl
{ {
public: public:
////////////////////////////////////////////////////////////
/// \brief Get the unique instance of the class
///
/// \note Private use only
///
/// \return Reference to the X11InputManager instance
///
////////////////////////////////////////////////////////////
static X11InputManager& getInstance();
private:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
X11InputManager();
public:
////////////////////////////////////////////////////////////
/// \brief Initialize the keyboard
///
/// Builds a mapping between sf::Keyboard::Scancode and
/// X11 keycodes
///
////////////////////////////////////////////////////////////
void initialize(Display* display);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key) /// \copydoc sf::Keyboard::isKeyPressed(Key)
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool isKeyPressed(sf::Keyboard::Key key) const; static bool isKeyPressed(Keyboard::Key key);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode) /// \copydoc sf::Keyboard::isKeyPressed(Scancode)
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool isKeyPressed(sf::Keyboard::Scancode code) const; static bool isKeyPressed(Keyboard::Scancode code);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize /// \copydoc sf::Keyboard::localize
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Scancode unlocalize(sf::Keyboard::Key key) const; static Keyboard::Scancode unlocalize(Keyboard::Key key);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::unlocalize /// \copydoc sf::Keyboard::unlocalize
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Key localize(sf::Keyboard::Scancode code) const; static Keyboard::Key localize(Keyboard::Scancode code);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription /// \copydoc sf::Keyboard::getDescription
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::String getDescription(Keyboard::Scancode code) const; static String getDescription(Keyboard::Scancode code);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the sf::Keyboard::Key from XKeyEvent /// \brief Get the sf::Keyboard::Key from XKeyEvent
@ -114,7 +86,7 @@ public:
/// \return A key being pressed or released /// \return A key being pressed or released
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Key getKeyFromEvent(XKeyEvent& event) const; static Keyboard::Key getKeyFromEvent(XKeyEvent& event);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the sf::Keyboard::Scancode from XKeyEvent /// \brief Get the sf::Keyboard::Scancode from XKeyEvent
@ -124,49 +96,11 @@ public:
/// \return A scancode of a key being pressed or released /// \return A scancode of a key being pressed or released
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Scancode getScancodeFromEvent(XKeyEvent& event) const; static Keyboard::Scancode getScancodeFromEvent(XKeyEvent& event);
private:
////////////////////////////////////////////////////////////
/// \brief Converts sf::Keyboard::Key to X11 keycode
///
/// \param key A key to be converted
///
/// \return A corresponding X11 Keycode
///
////////////////////////////////////////////////////////////
KeyCode SFtoKeyCode(sf::Keyboard::Key key) const;
////////////////////////////////////////////////////////////
/// \brief Converts sf::Keyboard::Scancode to X11 keycode
///
/// \param code A scancode to be converted
///
/// \return A corresponding X11 Keycode
///
////////////////////////////////////////////////////////////
KeyCode SFtoKeyCode(sf::Keyboard::Scancode code) const;
////////////////////////////////////////////////////////////
/// \brief Convert X11 Keycode to sf::Keyboard::Scancode
///
/// \param code X11 keycode
///
/// \return The corresponding sf::Keyboard::Scancode
///
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode keyCodeToSF(KeyCode code) const;
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
KeyCode m_scancodeToKeycode[sf::Keyboard::ScanCodeCount]; ///< Mapping of SFML scancode to X11 KeyCode
sf::Keyboard::Scancode m_keycodeToScancode[256]; ///< Mapping of X11 KeyCode to SFML scancode
Display* m_display; ///< Pointer to the display
}; };
} // namespace priv } // namespace priv
} // namespace sf } // namespace sf
#endif // SFML_X11INPUTMANAGER_HPP #endif // SFML_KEYBOARD_IMPL_HPP

View File

@ -29,6 +29,7 @@
#include <SFML/Window/Unix/ClipboardImpl.hpp> #include <SFML/Window/Unix/ClipboardImpl.hpp>
#include <SFML/Window/Unix/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <SFML/Window/Unix/InputImpl.hpp> #include <SFML/Window/Unix/InputImpl.hpp>
#include <SFML/Window/Unix/KeyboardImpl.hpp>
#include <SFML/System/Utf.hpp> #include <SFML/System/Utf.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <SFML/System/Mutex.hpp> #include <SFML/System/Mutex.hpp>
@ -1688,9 +1689,6 @@ void WindowImplX11::initialize()
// Create the hidden cursor // Create the hidden cursor
createHiddenCursor(); createHiddenCursor();
// init X11 keycode <-> SFML scancode mapping
X11InputManager::getInstance().initialize(m_display);
// Flush the commands queue // Flush the commands queue
XFlush(m_display); XFlush(m_display);
@ -1888,27 +1886,16 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
// Key down event // Key down event
case KeyPress: case KeyPress:
{ {
Keyboard::Key key = Keyboard::Unknown;
// Try each KeySym index (modifier group) until we get a match
for (int i = 0; i < 4; ++i)
{
// Get the SFML keyboard code from the keysym of the key that has been pressed
key = keysymToSF(XLookupKeysym(&windowEvent.xkey, i));
if (key != Keyboard::Unknown)
break;
}
// Fill the event parameters // Fill the event parameters
// TODO: if modifiers are wrong, use XGetModifierMapping to retrieve the actual modifiers mapping // TODO: if modifiers are wrong, use XGetModifierMapping to retrieve the actual modifiers mapping
Event event; Event event;
event.type = Event::KeyPressed; event.type = Event::KeyPressed;
event.key.code = key; event.key.code = KeyboardImpl::getKeyFromEvent(windowEvent.xkey);
event.key.alt = windowEvent.xkey.state & Mod1Mask; event.key.scancode = KeyboardImpl::getScancodeFromEvent(windowEvent.xkey);
event.key.control = windowEvent.xkey.state & ControlMask; event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.shift = windowEvent.xkey.state & ShiftMask; event.key.control = windowEvent.xkey.state & ControlMask;
event.key.system = windowEvent.xkey.state & Mod4Mask; event.key.shift = windowEvent.xkey.state & ShiftMask;
event.key.system = windowEvent.xkey.state & Mod4Mask;
const bool filtered = XFilterEvent(&windowEvent, None); const bool filtered = XFilterEvent(&windowEvent, None);
@ -1995,26 +1982,15 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
// Key up event // Key up event
case KeyRelease: case KeyRelease:
{ {
Keyboard::Key key = Keyboard::Unknown;
// Try each KeySym index (modifier group) until we get a match
for (int i = 0; i < 4; ++i)
{
// Get the SFML keyboard code from the keysym of the key that has been released
key = keysymToSF(XLookupKeysym(&windowEvent.xkey, i));
if (key != Keyboard::Unknown)
break;
}
// Fill the event parameters // Fill the event parameters
Event event; Event event;
event.type = Event::KeyReleased; event.type = Event::KeyReleased;
event.key.code = key; event.key.code = KeyboardImpl::getKeyFromEvent(windowEvent.xkey);
event.key.alt = windowEvent.xkey.state & Mod1Mask; event.key.scancode = KeyboardImpl::getScancodeFromEvent(windowEvent.xkey);
event.key.control = windowEvent.xkey.state & ControlMask; event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.shift = windowEvent.xkey.state & ShiftMask; event.key.control = windowEvent.xkey.state & ControlMask;
event.key.system = windowEvent.xkey.state & Mod4Mask; event.key.shift = windowEvent.xkey.state & ShiftMask;
event.key.system = windowEvent.xkey.state & Mod4Mask;
pushEvent(event); pushEvent(event);
break; break;