Almost got rid of singleton (move a lot of stuff into free functions)

This commit is contained in:
Elias Daler 2018-03-31 02:36:11 +03:00 committed by Lukas Dürrenberger
parent e845e50142
commit 02b9dafc6f
3 changed files with 48 additions and 79 deletions

View file

@ -1606,9 +1606,6 @@ void WindowImplX11::initialize()
// Create the hidden cursor
createHiddenCursor();
// init X11 keycode <-> SFML scancode mapping
X11InputManager::getInstance().initialize();
// Flush the commands queue
XFlush(m_display);

View file

@ -41,6 +41,11 @@ namespace priv
{
namespace {
KeyCode scancodeToKeycode[sf::Keyboard::ScanCodeCount]; ///< Mapping of SFML scancode to X11 KeyCode
sf::Keyboard::Scancode keycodeToScancode[256]; ///< Mapping of X11 KeyCode to SFML scancode
bool is_init = false;
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode)
{
@ -219,36 +224,18 @@ sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode)
return sf::Keyboard::ScanUnknown;
}
} // anonymous namespace
////////////////////////////////////////////////////////////
X11InputManager::X11InputManager()
void init()
{
for (int i = 0; i < sf::Keyboard::ScanCodeCount; ++i)
{
m_scancodeToKeycode[i] = 0;
scancodeToKeycode[i] = 0;
}
for (int i = 0; i < 256; ++i)
{
m_keycodeToScancode[i] = sf::Keyboard::ScanUnknown;
keycodeToScancode[i] = sf::Keyboard::ScanUnknown;
}
}
////////////////////////////////////////////////////////////
X11InputManager& X11InputManager::getInstance()
{
static X11InputManager instance;
return instance;
}
////////////////////////////////////////////////////////////
void X11InputManager::initialize()
{
Display* display = OpenDisplay();
// Find the X11 key code -> SFML key code mapping
@ -316,8 +303,8 @@ void X11InputManager::initialize()
if ((keycode >= 0) && (keycode < 256))
{
m_scancodeToKeycode[sc] = keycode;
m_keycodeToScancode[keycode] = sc;
scancodeToKeycode[sc] = keycode;
keycodeToScancode[keycode] = sc;
}
}
@ -325,20 +312,54 @@ void X11InputManager::initialize()
XkbFreeKeyboard(desc, 0, True);
// Translate un-translated keycodes using traditional X11 KeySym lookups
for (int keycode = 0; keycode < 256; ++keycode)
for (int keycode = 0; keycode < 256; ++keycode)
{
if (m_keycodeToScancode[keycode] == sf::Keyboard::ScanUnknown)
if (keycodeToScancode[keycode] == sf::Keyboard::ScanUnknown)
{
sf::Keyboard::Scancode sc = translateKeyCode(display, keycode);
m_scancodeToKeycode[sc] = keycode;
m_keycodeToScancode[keycode] = sc;
scancodeToKeycode[sc] = keycode;
keycodeToScancode[keycode] = sc;
}
}
CloseDisplay(display);
is_init = true;
}
////////////////////////////////////////////////////////////
KeyCode SFScancodeToKeyCode(sf::Keyboard::Scancode code)
{
if (!is_init)
init();
return scancodeToKeycode[code];
}
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode keyCodeToSFScancode(KeyCode code)
{
if (!is_init)
init();
return keycodeToScancode[code];
}
} // anonymous namespace
////////////////////////////////////////////////////////////
X11InputManager::X11InputManager()
{
}
////////////////////////////////////////////////////////////
X11InputManager& X11InputManager::getInstance()
{
static X11InputManager instance;
return instance;
}
namespace {
////////////////////////////////////////////////////////////
@ -545,20 +566,6 @@ KeyCode X11InputManager::SFKeyToKeyCode(sf::Keyboard::Key key) const
return keycode;
}
////////////////////////////////////////////////////////////
KeyCode X11InputManager::SFScancodeToKeyCode(sf::Keyboard::Scancode code) const
{
return m_scancodeToKeycode[code];
}
////////////////////////////////////////////////////////////
sf::Keyboard::Scancode X11InputManager::keyCodeToSFScancode(KeyCode code) const
{
return m_keycodeToScancode[code];
}
////////////////////////////////////////////////////////////
KeySym X11InputManager::SFScancodeToKeySym(sf::Keyboard::Scancode code) const
{

View file

@ -67,15 +67,6 @@ private:
X11InputManager();
public:
////////////////////////////////////////////////////////////
/// \brief Initialize the keyboard
///
/// Builds a mapping between sf::Keyboard::Scancode and
/// X11 keycodes
///
////////////////////////////////////////////////////////////
void initialize();
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
@ -138,26 +129,6 @@ private:
////////////////////////////////////////////////////////////
KeyCode SFKeyToKeyCode(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 SFScancodeToKeyCode(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 keyCodeToSFScancode(KeyCode code) const;
////////////////////////////////////////////////////////////
/// \brief Convert sf::Keyboard::Scancode to KeySym
///
@ -167,12 +138,6 @@ private:
///
////////////////////////////////////////////////////////////
KeySym SFScancodeToKeySym(sf::Keyboard::Scancode 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
};
} // namespace priv