Cemu/src/input/api/Keyboard/KeyboardController.cpp
Exzap 028b3f7992
Make controller button code thread-safe (#405)
* Refactor spinlock to meet Lockable requirements
* Input: Refactor button code and make it thread-safe
2022-10-23 15:47:42 +02:00

60 lines
1.5 KiB
C++

#include <boost/container/small_vector.hpp>
#include "input/api/Keyboard/KeyboardController.h"
#include "gui/guiWrapper.h"
KeyboardController::KeyboardController()
: base_type("keyboard", "Keyboard")
{
}
std::string KeyboardController::get_button_name(uint64 button) const
{
#if BOOST_OS_WINDOWS
LONG scan_code = MapVirtualKeyA((UINT)button, MAPVK_VK_TO_VSC_EX);
if(HIBYTE(scan_code))
scan_code |= 0x100;
// because MapVirtualKey strips the extended bit for some keys
switch (button)
{
case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN: // arrow keys
case VK_PRIOR: case VK_NEXT: // page up and page down
case VK_END: case VK_HOME:
case VK_INSERT: case VK_DELETE:
case VK_DIVIDE: // numpad slash
case VK_NUMLOCK:
{
scan_code |= 0x100; // set extended bit
break;
}
}
scan_code <<= 16;
char key_name[128];
if (GetKeyNameTextA(scan_code, key_name, std::size(key_name)) != 0)
return key_name;
else
return fmt::format("key_{}", button);
#elif BOOST_OS_LINUX
return gui_gtkRawKeyCodeToString(button);
#else
return fmt::format("key_{}", button);
#endif
}
extern WindowInfo g_window_info;
ControllerState KeyboardController::raw_state()
{
ControllerState result{};
if (g_window_info.app_active)
{
boost::container::small_vector<uint32, 16> pressedKeys;
g_window_info.iter_keystates([&pressedKeys](const std::pair<const uint32, bool>& keyState) { if (keyState.second) pressedKeys.emplace_back(keyState.first); });
result.buttons.SetPressedButtons(pressedKeys);
}
return result;
}