Safety and handling of edge cases

This commit is contained in:
Elias Daler 2018-03-31 14:20:52 +03:00 committed by Lukas Dürrenberger
parent 6cef8046dd
commit c4f72fe8c2
2 changed files with 26 additions and 12 deletions

View file

@ -167,7 +167,7 @@ inline sf::Keyboard::Key keySymToSFKey(KeySym symbol)
inline KeySym SFKeyToKeySym(sf::Keyboard::Key key) inline KeySym SFKeyToKeySym(sf::Keyboard::Key key)
{ {
// Get the corresponding X11 keysym // Get the corresponding X11 keysym
KeySym keysym = 0; KeySym keysym = NoSymbol;
switch (key) switch (key)
{ {
case sf::Keyboard::LShift: keysym = XK_Shift_L; break; case sf::Keyboard::LShift: keysym = XK_Shift_L; break;

View file

@ -42,15 +42,21 @@ namespace priv
namespace { namespace {
KeyCode scancodeToKeycode[sf::Keyboard::ScanCodeCount] = { 0 }; ///< Mapping of SFML scancode to X11 KeyCode KeyCode scancodeToKeycode[sf::Keyboard::ScanCodeCount] = { 0 }; ///< Mapping of SFML scancode to X11 KeyCode
sf::Keyboard::Scancode keycodeToScancode[256] = { sf::Keyboard::ScanUnknown}; ///< Mapping of X11 KeyCode to SFML scancode sf::Keyboard::Scancode keycodeToScancode[256] = { sf::Keyboard::ScanUnknown}; ///< Mapping of X11 KeyCode to SFML scancode
bool isMappingInitialized = false; bool isMappingInitialized = false;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode) bool isValidKeycode(KeyCode keycode)
{ {
// Valid key code range is [8,255], according to the Xlib manual // Valid key code range is [8,255], according to the Xlib manual
if (keycode < 8 || keycode > 255) return keycode >= 8 || keycode <= 255;
}
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode)
{
if (!isValidKeycode(keycode))
return sf::Keyboard::ScanUnknown; return sf::Keyboard::ScanUnknown;
// Try secondary keysym, for numeric keypad keys // Try secondary keysym, for numeric keypad keys
@ -289,7 +295,7 @@ void initMapping()
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))
{ {
scancodeToKeycode[sc] = keycode; scancodeToKeycode[sc] = keycode;
keycodeToScancode[keycode] = sc; keycodeToScancode[keycode] = sc;
@ -300,7 +306,8 @@ void initMapping()
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 (keycodeToScancode[keycode] == sf::Keyboard::ScanUnknown) if (keycodeToScancode[keycode] == sf::Keyboard::ScanUnknown)
{ {
@ -330,25 +337,32 @@ sf::Keyboard::Scancode keyCodeToSFScancode(KeyCode code)
if (!isMappingInitialized) if (!isMappingInitialized)
initMapping(); initMapping();
return keycodeToScancode[code]; if (isValidKeycode(code))
return keycodeToScancode[code];
return sf::Keyboard::ScanUnknown;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
KeyCode SFKeyToKeyCode(sf::Keyboard::Key key) KeyCode SFKeyToKeyCode(sf::Keyboard::Key key)
{ {
KeySym keysym = SFKeyToKeySym(key); KeySym keysym = SFKeyToKeySym(key);
Display* display = OpenDisplay(); if (keysym != NoSymbol) {
KeyCode keycode = XKeysymToKeycode(display, keysym); Display* display = OpenDisplay();
CloseDisplay(display); KeyCode keycode = XKeysymToKeycode(display, keysym);
return keycode; CloseDisplay(display);
return keycode;
}
return 0;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
KeySym SFScancodeToKeySym(sf::Keyboard::Scancode code) KeySym SFScancodeToKeySym(sf::Keyboard::Scancode code)
{ {
Display* display = OpenDisplay(); Display* display = OpenDisplay();
KeySym keysym = NoSymbol;
KeyCode keycode = SFScancodeToKeyCode(code); KeyCode keycode = SFScancodeToKeyCode(code);
KeySym keysym = XkbKeycodeToKeysym(display, keycode, 0, 0); if (keycode != 0) // ensure that this Scancode is mapped to keycode
keysym = XkbKeycodeToKeysym(display, keycode, 0, 0);
CloseDisplay(display); CloseDisplay(display);
return keysym; return keysym;
} }