Generate KeySym -> Unicode mapping, so we don't need to make fake events in getDescription now

This commit is contained in:
Elias Daler 2018-03-31 01:47:06 +03:00 committed by Lukas Dürrenberger
parent 7a862c917a
commit ba513484fc
4 changed files with 1427 additions and 45 deletions

File diff suppressed because it is too large Load diff

View file

@ -1607,7 +1607,7 @@ void WindowImplX11::initialize()
createHiddenCursor(); createHiddenCursor();
// init X11 keycode <-> SFML scancode mapping // init X11 keycode <-> SFML scancode mapping
X11InputManager::getInstance().initialize(m_display, m_inputContext); X11InputManager::getInstance().initialize(m_display);
// Flush the commands queue // Flush the commands queue
XFlush(m_display); XFlush(m_display);

View file

@ -25,9 +25,10 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/System/Utf.hpp>
#include <SFML/Window/Unix/X11InputManager.hpp> #include <SFML/Window/Unix/X11InputManager.hpp>
#include <SFML/System/Utf.hpp>
#include <SFML/Window/Unix/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <SFML/Window/Unix/KeySymToUnicodeMapping.hpp>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
@ -39,6 +40,7 @@ namespace priv
{ {
namespace { namespace {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Keyboard::Key keysymToSF(KeySym symbol) sf::Keyboard::Key keysymToSF(KeySym symbol)
{ {
@ -450,8 +452,7 @@ sf::Keyboard::Scancode translateKeyCode(Display* display, KeyCode keycode)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
X11InputManager::X11InputManager() : X11InputManager::X11InputManager() :
m_display(NULL), m_display(NULL)
m_inputContext(NULL)
{ {
for (int i = 0; i < sf::Keyboard::ScanCodeCount; ++i) for (int i = 0; i < sf::Keyboard::ScanCodeCount; ++i)
{ {
@ -474,10 +475,9 @@ X11InputManager& X11InputManager::getInstance()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void X11InputManager::initialize(Display* display, XIC inputContext) void X11InputManager::initialize(Display* display)
{ {
m_display = display; m_display = display;
m_inputContext = inputContext;
// 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
@ -634,43 +634,11 @@ sf::String X11InputManager::getDescription(Keyboard::Scancode code) const
if (checkInput) if (checkInput)
{ {
// fake keypress event KeyCode keycode = SFtoKeyCode(code);
XKeyPressedEvent ev; KeySym keysym = XkbKeycodeToKeysym(m_display, keycode, 0, 0);
ev.keycode = SFtoKeyCode(code); sf::Uint32 unicode = keysymToUnicode(keysym);
ev.display = m_display; if (unicode != 0)
ev.type = KeyPress; return sf::String(unicode);
#ifdef X_HAVE_UTF8_STRING
if (m_inputContext)
{
Status status;
Uint8 keyBuffer[16];
int length = Xutf8LookupString(
m_inputContext,
&ev,
reinterpret_cast<char*>(keyBuffer),
sizeof(keyBuffer),
NULL,
&status
);
if (length > 0)
{
Uint32 unicode = 0;
Utf8::decode(keyBuffer, keyBuffer + length, unicode, 0);
if (unicode != 0)
return sf::String(unicode);
}
}
else
#endif
{
static XComposeStatus status;
char keyBuffer[16];
if (XLookupString(&ev, keyBuffer, sizeof(keyBuffer), NULL, &status))
return sf::String(static_cast<Uint32>(keyBuffer[0]));
}
} }
// Fallback to our best guess for the keys that are known to be independent of the layout. // Fallback to our best guess for the keys that are known to be independent of the layout.

View file

@ -74,7 +74,7 @@ public:
/// X11 keycodes /// X11 keycodes
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void initialize(Display* display, XIC inputContext); void initialize(Display* display);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key) /// \copydoc sf::Keyboard::isKeyPressed(Key)
@ -164,10 +164,10 @@ private:
KeyCode m_scancodeToKeycode[sf::Keyboard::ScanCodeCount]; ///< Mapping of SFML scancode to X11 KeyCode 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 sf::Keyboard::Scancode m_keycodeToScancode[256]; ///< Mapping of X11 KeyCode to SFML scancode
Display* m_display; ///< Pointer to the display Display* m_display; ///< Pointer to the display
XIC m_inputContext; ///< Input context used to get unicode input in our window (used for getDescription)
}; };
} // namespace priv } // namespace priv
} // namespace sf } // namespace sf
#endif // SFML_X11INPUTMANAGER_HPP #endif // SFML_X11INPUTMANAGER_HPP