Skip to content

Commit

Permalink
Review remarks + some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Jun 24, 2021
1 parent efd9108 commit d63058d
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 116 deletions.
14 changes: 7 additions & 7 deletions src/common/input/InputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,22 @@ std::string GetInputDeviceName(int dev_type)

std::string PortUserFormat(std::string_view port)
{
int port1, slot;
PortStr2Int(port, &port1, &slot);
++port1;
int port_num, slot;
PortStr2Int(port, &port_num, &slot);
++port_num;
if (slot != PORT_INVALID) {
++slot;
return std::to_string(port1) + "." + std::to_string(slot);
return std::to_string(port_num) + "." + std::to_string(slot);
}
else {
return std::to_string(port1);
return std::to_string(port_num);
}
}

void PortStr2Int(std::string_view port, int *port1, int *slot)
void PortStr2Int(std::string_view port, int *port_num, int *slot)
{
*slot = PORT_INVALID;
auto &ret = std::from_chars(port.data(), port.data() + port.size(), *port1);
auto &ret = std::from_chars(port.data(), port.data() + port.size(), *port_num);
assert(ret.ec != std::errc::invalid_argument);
if (ret.ptr != port.data() + port.size()) {
++ret.ptr;
Expand Down
4 changes: 1 addition & 3 deletions src/common/input/InputDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ XBOX_INPUT_DEVICE;
inline bool g_bIsTrackingMoLeave = false;
inline bool g_bIsTrackingMoMove = false;

// Lookup array used to translate a gui port to an xbox usb port and vice versa
extern int Gui2XboxPortArray[4];

// Retrieves the printable name of a xid type
std::string GetInputDeviceName(int dev_type);
// Converts the port number in the user format
std::string PortUserFormat(std::string_view);
// Extracts port and slot number from a port formatted as a string
void PortStr2Int(std::string_view port, int *port1, int *slot);
void PortStr2Int(std::string_view port, int *port_num, int *slot);

/* Abstract class which represents a host device usable for input/output */
class InputDevice
Expand Down
92 changes: 43 additions & 49 deletions src/common/input/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@
// hle input specific
#include "core\hle\XAPI\Xapi.h"

int Gui2XboxPortArray[4] = {
3,
4,
1,
2
};

int dev_num_buttons[to_underlying(XBOX_INPUT_DEVICE::DEVICE_MAX)] = {
XBOX_CTRL_NUM_BUTTONS, // MS_CONTROLLER_DUKE
Expand Down Expand Up @@ -95,13 +89,13 @@ void InputDeviceManager::Initialize(bool is_gui, HWND hwnd)
});

m_Cv.wait(lck, []() {
return (Sdl::SdlInitStatus != Sdl::SDL_NOT_INIT) &&
(XInput::XInputInitStatus != XInput::XINPUT_NOT_INIT) &&
(RawInput::RawInputInitStatus != RawInput::RAWINPUT_NOT_INIT);
return (Sdl::InitStatus != Sdl::NOT_INIT) &&
(XInput::InitStatus != XInput::NOT_INIT) &&
(RawInput::InitStatus != RawInput::NOT_INIT);
});
lck.unlock();

if (Sdl::SdlInitStatus < 0 || XInput::XInputInitStatus < 0 || RawInput::RawInputInitStatus < 0) {
if (Sdl::InitStatus < 0 || XInput::InitStatus < 0 || RawInput::InitStatus < 0) {
CxbxKrnlCleanup("Failed to initialize input subsystem! Consult debug log for more information");
}

Expand Down Expand Up @@ -225,31 +219,31 @@ void InputDeviceManager::RemoveDevice(std::function<bool(const InputDevice*)> Ca
void InputDeviceManager::UpdateDevices(std::string_view port, bool ack)
{
DeviceState *dev, *upstream;
int port1, slot, type;
PortStr2Int(port, &port1, &slot);
dev = &g_devs[port1];
int port_num, slot, type;
PortStr2Int(port, &port_num, &slot);
dev = &g_devs[port_num];

if (slot == PORT_INVALID) { // Port references a device attached to an xbox port
upstream = nullptr;
g_EmuShared->GetInputDevTypeSettings(&type, port1);
g_EmuShared->GetInputDevTypeSettings(&type, port_num);
}
else { // Port references a device attached to a slot port
upstream = dev;
dev = dev->slots[slot];
g_EmuShared->GetInputSlotTypeSettings(&type, port1, slot);
g_EmuShared->GetInputSlotTypeSettings(&type, port_num, slot);
}

// updating a slot
if (dev == nullptr) {
// connect slot
if (type != to_underlying(XBOX_INPUT_DEVICE::DEVICE_INVALID) &&
g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port1) + slot].type == XBOX_INPUT_DEVICE::DEVICE_INVALID) {
ConnectDevice(&g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port1) + slot], upstream, type, port);
g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port_num) + slot].type == XBOX_INPUT_DEVICE::DEVICE_INVALID) {
ConnectDevice(&g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port_num) + slot], upstream, type, port);
}
// disconnect slot
else if (type == to_underlying(XBOX_INPUT_DEVICE::DEVICE_INVALID) &&
g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port1) + slot].type != XBOX_INPUT_DEVICE::DEVICE_INVALID) {
DisconnectDevice(&g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port1) + slot], port, ack);
g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port_num) + slot].type != XBOX_INPUT_DEVICE::DEVICE_INVALID) {
DisconnectDevice(&g_devs[MU_OFFSET + (XBOX_CTRL_NUM_SLOTS * port_num) + slot], port, ack);
}
// update bindings slot
else {
Expand All @@ -270,9 +264,9 @@ void InputDeviceManager::UpdateDevices(std::string_view port, bool ack)
}
// update bindings
else {
auto dev1 = g_InputDeviceManager.FindDevice(port);
if (dev1 != nullptr) {
dev1->SetPort(port, false);
auto host_dev = g_InputDeviceManager.FindDevice(port);
if (host_dev != nullptr) {
host_dev->SetPort(port, false);
}
if (type != to_underlying(XBOX_INPUT_DEVICE::DEVICE_INVALID)) {
if (type != to_underlying(dev->type)) {
Expand Down Expand Up @@ -307,9 +301,9 @@ void InputDeviceManager::DisconnectDevice(DeviceState *dev, std::string_view por
else {
dev->bPendingRemoval = true;
}
auto dev1 = g_InputDeviceManager.FindDevice(port);
if (dev1 != nullptr) {
dev1->SetPort(port, false);
auto host_dev = g_InputDeviceManager.FindDevice(port);
if (host_dev != nullptr) {
host_dev->SetPort(port, false);
}
}

Expand All @@ -322,15 +316,15 @@ void InputDeviceManager::BindHostDevice(int type, std::string_view port)

char dev_name[50];
char dev_control_names[HIGHEST_NUM_BUTTONS][HOST_BUTTON_NAME_LENGTH];
int port1, slot;
PortStr2Int(port, &port1, &slot);
g_EmuShared->GetInputDevNameSettings(dev_name, port1);
g_EmuShared->GetInputBindingsSettings(dev_control_names, dev_num_buttons[type], port1);
int port_num, slot;
PortStr2Int(port, &port_num, &slot);
g_EmuShared->GetInputDevNameSettings(dev_name, port_num);
g_EmuShared->GetInputBindingsSettings(dev_control_names, dev_num_buttons[type], port_num);

auto dev = FindDevice(std::string(dev_name));
if (dev != nullptr) {
std::string port1(port);
dev->ClearBindings(port1);
std::string port_str(port);
dev->ClearBindings(port_str);
std::vector<InputDevice::IoControl *> controls = dev->GetIoControls();
for (int index = 0; index < dev_num_buttons[type]; index++) {
std::string dev_button(dev_control_names[index]);
Expand All @@ -340,7 +334,7 @@ void InputDeviceManager::BindHostDevice(int type, std::string_view port)
}
return false;
});
dev->SetBindings(index, (it != controls.end()) ? *it : nullptr, port1);
dev->SetBindings(index, (it != controls.end()) ? *it : nullptr, port_str);
}
dev->SetPort(port, true);
}
Expand All @@ -357,19 +351,19 @@ bool InputDeviceManager::UpdateXboxPortInput(int port, void* buffer, int directi
// If somebody else is currently holding the lock, we won't wait and instead report no input changes
if (!g_renderbase->IsImGuiFocus() && m_Mtx.try_lock()) {
for (auto &dev : m_Devices) {
std::string port1 = std::to_string(port);
if (dev->GetPort(port1)) {
std::string port_str = std::to_string(port);
if (dev->GetPort(port_str)) {
switch (type)
{
case to_underlying(XBOX_INPUT_DEVICE::MS_CONTROLLER_DUKE):
case to_underlying(XBOX_INPUT_DEVICE::MS_CONTROLLER_S):
case to_underlying(XBOX_INPUT_DEVICE::ARCADE_STICK):
has_changed = UpdateInputXpad(dev, buffer, direction, port1);
has_changed = UpdateInputXpad(dev, buffer, direction, port_str);
m_Mtx.unlock();
return has_changed;

case to_underlying(XBOX_INPUT_DEVICE::STEEL_BATTALION_CONTROLLER):
has_changed = UpdateInputSBC(dev, buffer, direction, port, port1);
has_changed = UpdateInputSBC(dev, buffer, direction, port, port_str);
m_Mtx.unlock();
return has_changed;

Expand All @@ -381,7 +375,7 @@ bool InputDeviceManager::UpdateXboxPortInput(int port, void* buffer, int directi
case to_underlying(XBOX_INPUT_DEVICE::STEERING_WHEEL):
case to_underlying(XBOX_INPUT_DEVICE::IR_DONGLE):
EmuLog(LOG_LEVEL::ERROR2, "An unsupported device is attached at port %d! The device was %s",
Gui2XboxPortArray[port], GetInputDeviceName(type).c_str());
PortUserFormat(port_str).c_str(), GetInputDeviceName(type).c_str());
break;

}
Expand All @@ -393,9 +387,9 @@ bool InputDeviceManager::UpdateXboxPortInput(int port, void* buffer, int directi
return has_changed;
}

bool InputDeviceManager::UpdateInputXpad(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, const std::string &Port1)
bool InputDeviceManager::UpdateInputXpad(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, const std::string &Port)
{
std::map<int, InputDevice::IoControl*> bindings = Device->GetBindings(Port1);
std::map<int, InputDevice::IoControl*> bindings = Device->GetBindings(Port);
assert(bindings.size() == static_cast<size_t>(dev_num_buttons[to_underlying(XBOX_INPUT_DEVICE::MS_CONTROLLER_DUKE)]));

if (Direction == DIRECTION_IN) {
Expand Down Expand Up @@ -457,9 +451,9 @@ bool InputDeviceManager::UpdateInputXpad(std::shared_ptr<InputDevice>& Device, v
return true;
}

bool InputDeviceManager::UpdateInputSBC(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, int Port, const std::string &Port1)
bool InputDeviceManager::UpdateInputSBC(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, int Port_num, const std::string &Port)
{
std::map<int, InputDevice::IoControl*> bindings = Device->GetBindings(Port1);
std::map<int, InputDevice::IoControl*> bindings = Device->GetBindings(Port);
assert(bindings.size() == static_cast<size_t>(dev_num_buttons[to_underlying(XBOX_INPUT_DEVICE::STEEL_BATTALION_CONTROLLER)]));

// NOTE: the output state is not supported
Expand Down Expand Up @@ -496,10 +490,10 @@ bool InputDeviceManager::UpdateInputSBC(std::shared_ptr<InputDevice>& Device, vo
for (int i = 4, j = 0; i < 6; i++, j++) {
ControlState state = (bindings[i] != nullptr) ? dynamic_cast<InputDevice::Input *>(bindings[i])->GetState() : 0.0;
uint16_t curr_in_state = static_cast<uint16_t>(!!state);
if ((~curr_in_state) & ((last_in_state[Port] >> j) & 1)) {
if ((~curr_in_state) & ((last_in_state[Port_num] >> j) & 1)) {
in_buf->wButtons[0] ^= (1 << i);
}
(last_in_state[Port] &= ~(1 << j)) |= (curr_in_state << j);
(last_in_state[Port_num] &= ~(1 << j)) |= (curr_in_state << j);
}

for (int i = 6; i < 34; i++) {
Expand All @@ -516,10 +510,10 @@ bool InputDeviceManager::UpdateInputSBC(std::shared_ptr<InputDevice>& Device, vo
for (int i = 34, j = 2; i < 39; i++, j++) {
ControlState state = (bindings[i] != nullptr) ? dynamic_cast<InputDevice::Input *>(bindings[i])->GetState() : 0.0;
uint16_t curr_in_state = static_cast<uint16_t>(!!state);
if ((~curr_in_state) & ((last_in_state[Port] >> j) & 1)) {
if ((~curr_in_state) & ((last_in_state[Port_num] >> j) & 1)) {
in_buf->wButtons[2] ^= (1 << (i % 16));
}
(last_in_state[Port] &= ~(1 << j)) |= (curr_in_state << j);
(last_in_state[Port_num] &= ~(1 << j)) |= (curr_in_state << j);
}

for (int i = 39; i < 49; i += 2) {
Expand Down Expand Up @@ -578,7 +572,7 @@ bool InputDeviceManager::UpdateInputSBC(std::shared_ptr<InputDevice>& Device, vo
for (int i = 52, j = 7; i < 56; i++, j++) {
ControlState state = (bindings[i] != nullptr) ? dynamic_cast<InputDevice::Input *>(bindings[i])->GetState() : 0.0;
uint16_t curr_in_state = static_cast<uint16_t>(!!state);
if ((~curr_in_state) & ((last_in_state[Port] >> j) & 1)) {
if ((~curr_in_state) & ((last_in_state[Port_num] >> j) & 1)) {
switch (i)
{
case 52:
Expand Down Expand Up @@ -606,7 +600,7 @@ bool InputDeviceManager::UpdateInputSBC(std::shared_ptr<InputDevice>& Device, vo
break;
}
}
(last_in_state[Port] &= ~(1 << j)) |= (curr_in_state << j);
(last_in_state[Port_num] &= ~(1 << j)) |= (curr_in_state << j);
}
}

Expand All @@ -616,15 +610,15 @@ bool InputDeviceManager::UpdateInputSBC(std::shared_ptr<InputDevice>& Device, vo
void InputDeviceManager::RefreshDevices()
{
std::unique_lock<std::mutex> lck(m_Mtx);
Sdl::SdlPopulateOK = false;
Sdl::PopulateOK = false;
m_Devices.clear();
lck.unlock();
XInput::PopulateDevices();
DInput::PopulateDevices();
Sdl::PopulateDevices();
lck.lock();
m_Cv.wait(lck, []() {
return Sdl::SdlPopulateOK;
return Sdl::PopulateOK;
});
for (auto &dev : m_Devices) {
if (StrStartsWith(dev->GetDeviceName(), "KeyboardMouse")) {
Expand Down
6 changes: 3 additions & 3 deletions src/common/input/InputManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ struct DeviceInfo {
};

struct DeviceState {
DeviceState *upstream;
std::string port;
int port_idx;
XBOX_INPUT_DEVICE type;
bool bPendingRemoval;
bool bSignaled;
DeviceInfo info;
DeviceState *slots[XBOX_CTRL_NUM_SLOTS];
DeviceState *upstream;
};

extern DeviceState g_devs[4 + 8];
Expand Down Expand Up @@ -165,9 +165,9 @@ class InputDeviceManager

private:
// update input for an xbox controller
bool UpdateInputXpad(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, const std::string &Port1);
bool UpdateInputXpad(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, const std::string &Port);
// update input for a Steel Battalion controller
bool UpdateInputSBC(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, int Port, const std::string &Port1);
bool UpdateInputSBC(std::shared_ptr<InputDevice>& Device, void* Buffer, int Direction, int Port_num, const std::string &Port);
// bind a host device to an emulated device
void BindHostDevice(int type, std::string_view port);
// connect a device to the emulated machine
Expand Down
10 changes: 5 additions & 5 deletions src/common/input/RawDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

namespace RawInput
{
int RawInputInitStatus = RAWINPUT_NOT_INIT;
int InitStatus = NOT_INIT;
bool IgnoreHotplug = true;

void Init(std::mutex &Mtx, bool is_gui, HWND hwnd)
Expand All @@ -44,7 +44,7 @@ namespace RawInput

if (is_gui) {
// We don't need to monitor xinput device changes from the gui, because there we have the refresh button to detect changes
RawInputInitStatus = RAWINPUT_INIT_SUCCESS;
InitStatus = INIT_SUCCESS;
return;
}

Expand All @@ -69,16 +69,16 @@ namespace RawInput
static_cast<UINT>(sizeof(decltype(devices)::value_type))))
{
EmuLog(LOG_LEVEL::ERROR2, "RegisterRawInputDevices failed: %i", GetLastError());
RawInputInitStatus = RAWINPUT_INIT_ERROR;
InitStatus = INIT_ERROR;
return;
}

RawInputInitStatus = RAWINPUT_INIT_SUCCESS;
InitStatus = INIT_SUCCESS;
}

void DeInit()
{
RawInputInitStatus = RAWINPUT_NOT_INIT;
InitStatus = NOT_INIT;
IgnoreHotplug = true;
}
}
12 changes: 6 additions & 6 deletions src/common/input/RawDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@

namespace RawInput
{
typedef enum _RAWINPUT_INIT_STATUS : int
typedef enum _INIT_STATUS : int
{
RAWINPUT_NOT_INIT = -2,
RAWINPUT_INIT_ERROR,
RAWINPUT_INIT_SUCCESS,
NOT_INIT = -2,
INIT_ERROR,
INIT_SUCCESS,
}
RAWINPUT_INIT_STATUS;
INIT_STATUS;

extern int RawInputInitStatus;
extern int InitStatus;
extern bool IgnoreHotplug;

// initialize RawInput
Expand Down

0 comments on commit d63058d

Please sign in to comment.