Skip to content

Commit

Permalink
Input: Fix connection count
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Dec 14, 2017
1 parent 892ebad commit 784744f
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 81 deletions.
3 changes: 3 additions & 0 deletions rpcs3/Emu/Io/PadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ class PadHandlerBase
protected:
static const u32 MAX_GAMEPADS = 7;

std::array<bool, MAX_GAMEPADS> last_connection_status{{ false, false, false, false, false, false, false }};

int m_trigger_threshold = 0;
int m_thumb_threshold = 0;

Expand Down Expand Up @@ -580,6 +582,7 @@ class PadHandlerBase
s32 trigger_max = 255;
s32 vibration_min = 0;
s32 vibration_max = 255;
u32 connected = 0;

virtual bool Init() { return true; };
virtual ~PadHandlerBase() = default;
Expand Down
18 changes: 15 additions & 3 deletions rpcs3/ds4_pad_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,17 +809,23 @@ bool ds4_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::strin

void ds4_pad_handler::ThreadProc()
{
for (auto &bind : bindings)
for (int i = 0; i < static_cast<int>(bindings.size()); i++)
{
std::shared_ptr<DS4Device> device = bind.first;
auto thepad = bind.second;
std::shared_ptr<DS4Device> device = bindings[i].first;
auto thepad = bindings[i].second;

if (device->hidDevice == nullptr)
{
// try to reconnect
hid_device* dev = hid_open_path(device->path.c_str());
if (dev)
{
if (last_connection_status[i] == false)
{
LOG_ERROR(HLE, "DS4 device %d reconnected", i);
last_connection_status[i] = true;
connected++;
}
hid_set_nonblocking(dev, 1);
device->hidDevice = dev;
thepad->m_port_status = CELL_PAD_STATUS_CONNECTED|CELL_PAD_STATUS_ASSIGN_CHANGES;
Expand All @@ -829,6 +835,12 @@ void ds4_pad_handler::ThreadProc()
else
{
// nope, not there
if (last_connection_status[i] == true)
{
LOG_ERROR(HLE, "DS4 device %d disconnected", i);
last_connection_status[i] = false;
connected--;
}
thepad->m_port_status = CELL_PAD_STATUS_DISCONNECTED|CELL_PAD_STATUS_ASSIGN_CHANGES;
continue;
}
Expand Down
2 changes: 0 additions & 2 deletions rpcs3/ds4_pad_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ class ds4_pad_handler final : public PadHandlerBase
private:
bool is_init;

// holds internal controller state change
std::array<bool, MAX_GAMEPADS> last_connection_status = {};
std::vector<u32> blacklist;
std::vector<std::pair<std::shared_ptr<DS4Device>, std::shared_ptr<Pad>>> bindings;

Expand Down
18 changes: 18 additions & 0 deletions rpcs3/evdev_joystick_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,32 @@ void evdev_joystick_handler::ThreadProc()
{
update_devs();

int padnum = 0;
for (auto& device : devices)
{
m_dev = device;
auto pad = device.pad;
auto axis_orientations = device.axis_orientations;
auto& dev = device.device;
if (dev == nullptr)
{
if (last_connection_status[padnum] == true)
{
LOG_ERROR(HLE, "evdev device %d disconnected", padnum);
last_connection_status[padnum] = false;
connected--;
}
padnum++;
continue;
}

if (last_connection_status[padnum] == false)
{
LOG_ERROR(HLE, "evdev device %d reconnected", padnum);
last_connection_status[padnum] = true;
connected++;
}
padnum++;

// Handle vibration
int idx_l = m_pad_config.switch_vibration_motors ? 1 : 0;
Expand Down
4 changes: 3 additions & 1 deletion rpcs3/keyboard_pad_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ u32 keyboard_pad_handler::GetKeyCode(const std::string& keyName)

bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device)
{
if (device != "Keyboard") return false;
if (device != "Keyboard")
return false;

m_pad_config.load();

Expand Down Expand Up @@ -373,6 +374,7 @@ bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::
pad->m_vibrateMotors.emplace_back(false, 0);

bindings.push_back(pad);
connected++;

return true;
}
Expand Down
108 changes: 52 additions & 56 deletions rpcs3/mm_joystick_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,87 +192,83 @@ bool mm_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::s
void mm_joystick_handler::ThreadProc()
{
MMRESULT status;
DWORD online = 0;

for (u32 i = 0; i != bindings.size(); ++i)
{
m_dev = bindings[i].first;
auto pad = bindings[i].second;
status = joyGetPosEx(m_dev->device_id, &m_dev->device_info);

switch (status)
if (status != JOYERR_NOERROR)
{
case JOYERR_UNPLUGGED:
if (last_connection_status[i] == true)
{
LOG_ERROR(HLE, "MMJOY Device %d disconnected.", m_dev->device_id);
pad->m_port_status &= ~CELL_PAD_STATUS_CONNECTED;
pad->m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
last_connection_status[i] = false;
connected--;
}
last_connection_status[i] = false;
pad->m_port_status &= ~CELL_PAD_STATUS_CONNECTED;
break;
continue;
}

case JOYERR_NOERROR:
++online;
if (last_connection_status[i] == false)
{
if (GetMMJOYDevice(m_dev->device_id, *m_dev) == false)
continue;
LOG_SUCCESS(HLE, "MMJOY Device %d reconnected.", m_dev->device_id);
pad->m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
}
last_connection_status[i] = true;
if (last_connection_status[i] == false)
{
if (GetMMJOYDevice(m_dev->device_id, *m_dev) == false)
continue;
LOG_SUCCESS(HLE, "MMJOY Device %d reconnected.", m_dev->device_id);
pad->m_port_status |= CELL_PAD_STATUS_CONNECTED;
pad->m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
last_connection_status[i] = true;
connected++;
}

auto button_values = GetButtonValues(m_dev->device_info, m_dev->device_caps);
auto button_values = GetButtonValues(m_dev->device_info, m_dev->device_caps);

// Translate any corresponding keycodes to our normal DS3 buttons and triggers
for (auto& btn : pad->m_buttons)
{
btn.m_value = button_values[btn.m_keyCode];
TranslateButtonPress(btn.m_keyCode, btn.m_pressed, btn.m_value);
}
// Translate any corresponding keycodes to our normal DS3 buttons and triggers
for (auto& btn : pad->m_buttons)
{
btn.m_value = button_values[btn.m_keyCode];
TranslateButtonPress(btn.m_keyCode, btn.m_pressed, btn.m_value);
}

float stick_val[4];
float stick_val[4];

// Translate any corresponding keycodes to our two sticks. (ignoring thresholds for now)
for (int i = 0; i < static_cast<int>(pad->m_sticks.size()); i++)
{
bool pressed;

// m_keyCodeMin is the mapped key for left or down
u32 key_min = pad->m_sticks[i].m_keyCodeMin;
u16 val_min = button_values[key_min];
TranslateButtonPress(key_min, pressed, val_min, true);

// m_keyCodeMax is the mapped key for right or up
u32 key_max = pad->m_sticks[i].m_keyCodeMax;
u16 val_max = button_values[key_max];
TranslateButtonPress(key_max, pressed, val_max, true);

// cancel out opposing values and get the resulting difference
stick_val[i] = val_max - val_min;
}
// Translate any corresponding keycodes to our two sticks. (ignoring thresholds for now)
for (int i = 0; i < static_cast<int>(pad->m_sticks.size()); i++)
{
bool pressed;

u16 lx, ly, rx, ry;
// m_keyCodeMin is the mapped key for left or down
u32 key_min = pad->m_sticks[i].m_keyCodeMin;
u16 val_min = button_values[key_min];
TranslateButtonPress(key_min, pressed, val_min, true);

// Normalize our two stick's axis based on the thresholds
std::tie(lx, ly) = NormalizeStickDeadzone(stick_val[0], stick_val[1], m_pad_config.lstickdeadzone);
std::tie(rx, ry) = NormalizeStickDeadzone(stick_val[2], stick_val[3], m_pad_config.rstickdeadzone);
// m_keyCodeMax is the mapped key for right or up
u32 key_max = pad->m_sticks[i].m_keyCodeMax;
u16 val_max = button_values[key_max];
TranslateButtonPress(key_max, pressed, val_max, true);

if (m_pad_config.padsquircling != 0)
{
std::tie(lx, ly) = ConvertToSquirclePoint(lx, ly, m_pad_config.padsquircling);
std::tie(rx, ry) = ConvertToSquirclePoint(rx, ry, m_pad_config.padsquircling);
}
// cancel out opposing values and get the resulting difference
stick_val[i] = val_max - val_min;
}

u16 lx, ly, rx, ry;

pad->m_sticks[0].m_value = lx;
pad->m_sticks[1].m_value = 255 - ly;
pad->m_sticks[2].m_value = rx;
pad->m_sticks[3].m_value = 255 - ry;
// Normalize our two stick's axis based on the thresholds
std::tie(lx, ly) = NormalizeStickDeadzone(stick_val[0], stick_val[1], m_pad_config.lstickdeadzone);
std::tie(rx, ry) = NormalizeStickDeadzone(stick_val[2], stick_val[3], m_pad_config.rstickdeadzone);

break;
if (m_pad_config.padsquircling != 0)
{
std::tie(lx, ly) = ConvertToSquirclePoint(lx, ly, m_pad_config.padsquircling);
std::tie(rx, ry) = ConvertToSquirclePoint(rx, ry, m_pad_config.padsquircling);
}

pad->m_sticks[0].m_value = lx;
pad->m_sticks[1].m_value = 255 - ly;
pad->m_sticks[2].m_value = rx;
pad->m_sticks[3].m_value = 255 - ry;
}
}

Expand Down
1 change: 0 additions & 1 deletion rpcs3/mm_joystick_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,5 @@ class mm_joystick_handler final : public PadHandlerBase
std::vector<u64> blacklist;
std::unordered_map<int, MMJOYDevice> m_devices;
std::vector<std::pair<std::shared_ptr<MMJOYDevice>, std::shared_ptr<Pad>>> bindings;
std::array<bool, 7> last_connection_status = {};
std::shared_ptr<MMJOYDevice> m_dev;
};
5 changes: 3 additions & 2 deletions rpcs3/pad_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ void pad_thread::Init(const u32 max_connect)
LOG_ERROR(GENERAL, "Failed to bind device %s to handler %s", input_cfg.player_device[i]->to_string(), handler_type.to_string());
nullpad->bindPadToDevice(m_pads.back(), input_cfg.player_device[i]->to_string());
}
else if (handler_type != pad_handler::null)
m_info.now_connect++;
}

thread = std::make_shared<std::thread>(&pad_thread::ThreadFunc, this);
Expand All @@ -117,10 +115,13 @@ void pad_thread::ThreadFunc()
active = true;
while (active)
{
u32 connected = 0;
for (auto& cur_pad_handler : handlers)
{
cur_pad_handler.second->ThreadProc();
connected += cur_pad_handler.second->connected;
}
m_info.now_connect = connected;
std::this_thread::sleep_for(1ms);
}
}
36 changes: 22 additions & 14 deletions rpcs3/xinput_pad_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ std::array<u16, xinput_pad_handler::XInputKeyCodes::KeyCodeCount> xinput_pad_han

bool xinput_pad_handler::Init()
{
if (is_init) return true;
if (is_init)
return true;

for (auto it : XINPUT_INFO::LIBRARY_FILENAMES)
{
Expand All @@ -270,9 +271,7 @@ bool xinput_pad_handler::Init()
xinputEnable = reinterpret_cast<PFN_XINPUTENABLE>(GetProcAddress(library, "XInputEnable"));
xinputGetState = reinterpret_cast<PFN_XINPUTGETSTATE>(GetProcAddress(library, reinterpret_cast<LPCSTR>(100)));
if (!xinputGetState)
{
xinputGetState = reinterpret_cast<PFN_XINPUTGETSTATE>(GetProcAddress(library, "XInputGetState"));
}

xinputSetState = reinterpret_cast<PFN_XINPUTSETSTATE>(GetProcAddress(library, "XInputSetState"));
xinputGetBatteryInformation = reinterpret_cast<PFN_XINPUTGETBATTERYINFORMATION>(GetProcAddress(library, "XInputGetBatteryInformation"));
Expand All @@ -291,10 +290,12 @@ bool xinput_pad_handler::Init()
}
}

if (!is_init) return false;
if (!is_init)
return false;

m_pad_config.load();
if (!m_pad_config.exist()) m_pad_config.save();
if (!m_pad_config.exist())
m_pad_config.save();

return true;
}
Expand All @@ -320,21 +321,29 @@ void xinput_pad_handler::ThreadProc()
auto pad = bind.second;

result = (*xinputGetState)(padnum, &state);

switch (result)
{
case ERROR_DEVICE_NOT_CONNECTED:
if (last_connection_status[padnum] == true)
{
LOG_ERROR(HLE, "XInput device %d disconnected", padnum);
pad->m_port_status &= ~CELL_PAD_STATUS_CONNECTED;
pad->m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
last_connection_status[padnum] = false;
pad->m_port_status &= ~CELL_PAD_STATUS_CONNECTED;
break;
last_connection_status[padnum] = false;
connected--;
}
return;

case ERROR_SUCCESS:
++online;
if (last_connection_status[padnum] == false)
{
LOG_SUCCESS(HLE, "XInput device %d reconnected", padnum);
pad->m_port_status |= CELL_PAD_STATUS_CONNECTED;
pad->m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
last_connection_status[padnum] = true;
pad->m_port_status |= CELL_PAD_STATUS_CONNECTED;
last_connection_status[padnum] = true;
connected++;
}

std::array<u16, XInputKeyCodes::KeyCodeCount> button_values = GetButtonValues(state);

Expand Down Expand Up @@ -435,16 +444,15 @@ std::vector<std::string> xinput_pad_handler::ListDevices()
{
std::vector<std::string> xinput_pads_list;

if (!Init()) return xinput_pads_list;
if (!Init())
return xinput_pads_list;

for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
{
XINPUT_STATE state;
DWORD result = (*xinputGetState)(i, &state);
if (result == ERROR_SUCCESS)
{
xinput_pads_list.push_back(fmt::format("Xinput Pad #%d", i));
}
}
return xinput_pads_list;
}
Expand Down
2 changes: 0 additions & 2 deletions rpcs3/xinput_pad_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ class xinput_pad_handler final : public PadHandlerBase

std::vector<u32> blacklist;
std::vector<std::pair<std::shared_ptr<XInputDevice>, std::shared_ptr<Pad>>> bindings;
std::array<bool, 7> last_connection_status = {};

// holds internal controller state change
XINPUT_STATE state;
DWORD result;
DWORD online = 0;
};

0 comments on commit 784744f

Please sign in to comment.