X11InputManager now only has static members

This commit is contained in:
Elias Daler 2018-03-31 02:49:44 +03:00 committed by Lukas Dürrenberger
parent 02b9dafc6f
commit 281d3d8528
4 changed files with 65 additions and 250 deletions

View file

@ -41,142 +41,38 @@ namespace priv
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Key key)
{
// Get the corresponding X11 keysym
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;
}
return X11InputManager::isKeyPressed(key);
}
// Sanity checks
if (key < 0 || key >= sf::Keyboard::KeyCount)
return false;
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Scancode code)
{
return X11InputManager::isKeyPressed(code);
}
// Open a connection with the X server
Display* display = OpenDisplay();
// Convert to keycode
KeyCode keycode = XKeysymToKeycode(display, keysym);
if (keycode != 0)
{
// Get the whole keyboard state
char keys[32];
XQueryKeymap(display, keys);
////////////////////////////////////////////////////////////
Keyboard::Key InputImpl::localize(Keyboard::Scancode code)
{
return X11InputManager::localize(code);
}
// Close the connection with the X server
CloseDisplay(display);
// Check our keycode
return (keys[keycode / 8] & (1 << (keycode % 8))) != 0;
}
else
{
// Close the connection with the X server
CloseDisplay(display);
////////////////////////////////////////////////////////////
Keyboard::Scancode InputImpl::unlocalize(Keyboard::Key key)
{
return X11InputManager::unlocalize(key);
}
return false;
}
////////////////////////////////////////////////////////////
String InputImpl::getDescription(Keyboard::Scancode code)
{
return X11InputManager::getDescription(code);
}

View file

@ -1826,27 +1826,16 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
// Key down event
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
// TODO: if modifiers are wrong, use XGetModifierMapping to retrieve the actual modifiers mapping
Event event;
event.type = Event::KeyPressed;
event.key.code = key;
event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.control = windowEvent.xkey.state & ControlMask;
event.key.shift = windowEvent.xkey.state & ShiftMask;
event.key.system = windowEvent.xkey.state & Mod4Mask;
event.type = Event::KeyPressed;
event.key.code = X11InputManager::getKeyFromEvent(windowEvent.xkey);
event.key.scancode = X11InputManager::getScancodeFromEvent(windowEvent.xkey);
event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.control = windowEvent.xkey.state & ControlMask;
event.key.shift = windowEvent.xkey.state & ShiftMask;
event.key.system = windowEvent.xkey.state & Mod4Mask;
pushEvent(event);
// Generate a TextEntered event
@ -1903,26 +1892,15 @@ bool WindowImplX11::processEvent(XEvent& windowEvent)
// Key up event
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
Event event;
event.type = Event::KeyReleased;
event.key.code = key;
event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.control = windowEvent.xkey.state & ControlMask;
event.key.shift = windowEvent.xkey.state & ShiftMask;
event.key.system = windowEvent.xkey.state & Mod4Mask;
event.type = Event::KeyReleased;
event.key.code = X11InputManager::getKeyFromEvent(windowEvent.xkey);
event.key.scancode = X11InputManager::getScancodeFromEvent(windowEvent.xkey);
event.key.alt = windowEvent.xkey.state & Mod1Mask;
event.key.control = windowEvent.xkey.state & ControlMask;
event.key.shift = windowEvent.xkey.state & ShiftMask;
event.key.system = windowEvent.xkey.state & Mod4Mask;
pushEvent(event);
break;

View file

@ -224,7 +224,7 @@ sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode)
return sf::Keyboard::ScanUnknown;
}
void init()
void initMapping()
{
for (int i = 0; i < sf::Keyboard::ScanCodeCount; ++i)
{
@ -331,7 +331,7 @@ void init()
KeyCode SFScancodeToKeyCode(sf::Keyboard::Scancode code)
{
if (!is_init)
init();
initMapping();
return scancodeToKeycode[code];
}
@ -340,28 +340,31 @@ KeyCode SFScancodeToKeyCode(sf::Keyboard::Scancode code)
sf::Keyboard::Scancode keyCodeToSFScancode(KeyCode code)
{
if (!is_init)
init();
initMapping();
return keycodeToScancode[code];
}
} // anonymous namespace
////////////////////////////////////////////////////////////
X11InputManager::X11InputManager()
KeyCode SFKeyToKeyCode(sf::Keyboard::Key key)
{
KeySym keysym = SFKeyToKeySym(key);
Display* display = OpenDisplay();
KeyCode keycode = XKeysymToKeycode(display, keysym);
CloseDisplay(display);
return keycode;
}
////////////////////////////////////////////////////////////
X11InputManager& X11InputManager::getInstance()
KeySym SFScancodeToKeySym(sf::Keyboard::Scancode code)
{
static X11InputManager instance;
return instance;
Display* display = OpenDisplay();
KeyCode keycode = SFScancodeToKeyCode(code);
KeySym keysym = XkbKeycodeToKeysym(display, keycode, 0, 0);
CloseDisplay(display);
return keysym;
}
namespace {
////////////////////////////////////////////////////////////
bool isKeyPressedImpl(Display* display, KeyCode keycode)
{
@ -380,7 +383,7 @@ bool isKeyPressedImpl(Display* display, KeyCode keycode)
} // anonymous namespace
////////////////////////////////////////////////////////////
bool X11InputManager::isKeyPressed(sf::Keyboard::Key key) const
bool X11InputManager::isKeyPressed(sf::Keyboard::Key key)
{
Display* display = OpenDisplay();
bool pressed = isKeyPressedImpl(display, SFKeyToKeyCode(key));
@ -390,7 +393,7 @@ bool X11InputManager::isKeyPressed(sf::Keyboard::Key key) const
////////////////////////////////////////////////////////////
bool X11InputManager::isKeyPressed(sf::Keyboard::Scancode code) const
bool X11InputManager::isKeyPressed(sf::Keyboard::Scancode code)
{
Display* display = OpenDisplay();
bool pressed = isKeyPressedImpl(display, SFScancodeToKeyCode(code));
@ -400,7 +403,7 @@ bool X11InputManager::isKeyPressed(sf::Keyboard::Scancode code) const
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode X11InputManager::unlocalize(sf::Keyboard::Key key) const
sf::Keyboard::Scancode X11InputManager::unlocalize(sf::Keyboard::Key key)
{
KeyCode keycode = SFKeyToKeyCode(key);
return keyCodeToSFScancode(keycode);
@ -408,14 +411,14 @@ sf::Keyboard::Scancode X11InputManager::unlocalize(sf::Keyboard::Key key) const
////////////////////////////////////////////////////////////
sf::Keyboard::Key X11InputManager::localize(sf::Keyboard::Scancode code) const
sf::Keyboard::Key X11InputManager::localize(sf::Keyboard::Scancode code)
{
KeySym keysym = SFScancodeToKeySym(code);
return keySymToSFKey(keysym);
}
////////////////////////////////////////////////////////////
sf::String X11InputManager::getDescription(Keyboard::Scancode code) const
sf::String X11InputManager::getDescription(Keyboard::Scancode code)
{
bool checkInput = true;
@ -531,7 +534,7 @@ sf::String X11InputManager::getDescription(Keyboard::Scancode code) const
////////////////////////////////////////////////////////////
sf::Keyboard::Key X11InputManager::getKeyFromEvent(XKeyEvent& event) const
sf::Keyboard::Key X11InputManager::getKeyFromEvent(XKeyEvent& event)
{
sf::Keyboard::Key key = Keyboard::Unknown;
@ -550,31 +553,10 @@ sf::Keyboard::Key X11InputManager::getKeyFromEvent(XKeyEvent& event) const
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode X11InputManager::getScancodeFromEvent(XKeyEvent& event) const
sf::Keyboard::Scancode X11InputManager::getScancodeFromEvent(XKeyEvent& event)
{
return keyCodeToSFScancode(event.keycode);
}
////////////////////////////////////////////////////////////
KeyCode X11InputManager::SFKeyToKeyCode(sf::Keyboard::Key key) const
{
KeySym keysym = SFKeyToKeySym(key);
Display* display = OpenDisplay();
KeyCode keycode = XKeysymToKeycode(display, keysym);
CloseDisplay(display);
return keycode;
}
////////////////////////////////////////////////////////////
KeySym X11InputManager::SFScancodeToKeySym(sf::Keyboard::Scancode code) const
{
Display* display = OpenDisplay();
KeyCode keycode = SFScancodeToKeyCode(code);
KeySym keysym = XkbKeycodeToKeysym(display, keycode, 0, 0);
CloseDisplay(display);
return keysym;
}
} // namespace priv
} // namespace sf

View file

@ -48,54 +48,35 @@ class X11InputManager
{
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:
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
bool isKeyPressed(sf::Keyboard::Key key) const;
static bool isKeyPressed(sf::Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
bool isKeyPressed(sf::Keyboard::Scancode code) const;
static bool isKeyPressed(sf::Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode unlocalize(sf::Keyboard::Key key) const;
static sf::Keyboard::Scancode unlocalize(sf::Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::unlocalize
///
////////////////////////////////////////////////////////////
sf::Keyboard::Key localize(sf::Keyboard::Scancode code) const;
static sf::Keyboard::Key localize(sf::Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
sf::String getDescription(Keyboard::Scancode code) const;
static sf::String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \brief Get the sf::Keyboard::Key from XKeyEvent
@ -105,7 +86,7 @@ public:
/// \return A key being pressed or released
///
////////////////////////////////////////////////////////////
sf::Keyboard::Key getKeyFromEvent(XKeyEvent& event) const;
static sf::Keyboard::Key getKeyFromEvent(XKeyEvent& event);
////////////////////////////////////////////////////////////
/// \brief Get the sf::Keyboard::Scancode from XKeyEvent
@ -115,29 +96,7 @@ public:
/// \return A scancode of a key being pressed or released
///
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode getScancodeFromEvent(XKeyEvent& event) const;
private:
////////////////////////////////////////////////////////////
/// \brief Converts sf::Keyboard::Key to X11 keycode
///
/// \param key A key to be converted
///
/// \return A corresponding X11 Keycode
///
////////////////////////////////////////////////////////////
KeyCode SFKeyToKeyCode(sf::Keyboard::Key key) const;
////////////////////////////////////////////////////////////
/// \brief Convert sf::Keyboard::Scancode to KeySym
///
/// \param code sf::Keyboard::ScanCode
///
/// \return The corresponding X11 KeySym
///
////////////////////////////////////////////////////////////
KeySym SFScancodeToKeySym(sf::Keyboard::Scancode code) const;
static sf::Keyboard::Scancode getScancodeFromEvent(XKeyEvent& event);
};
} // namespace priv