Skip to content

Commit

Permalink
Input: Fix connection count
Browse files Browse the repository at this point in the history
and some minor commenting
  • Loading branch information
Megamouse committed Dec 19, 2017
1 parent 892ebad commit 8837578
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 92 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
39 changes: 28 additions & 11 deletions rpcs3/evdev_joystick_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,14 @@ void evdev_joystick_handler::GetNextButtonPress(const std::string& padId, const
return;

auto data = GetButtonValues(*device);

std::pair<u16, std::string> pressed_button = { 0, "" };

for (const auto& button : button_list)
{
int code = button.first;
std::string name = button.second;

// Handle annoying useless buttons
if (padId.find("Xbox 360") != std::string::npos && code >= BTN_TRIGGER_HAPPY)
continue;
if (padId.find("Sony") != std::string::npos && (code == BTN_TL2 || code == BTN_TR2))
Expand Down Expand Up @@ -355,12 +356,12 @@ void evdev_joystick_handler::GetNextButtonPress(const std::string& padId, const

int preview_values[6] =
{
find_value(buttons[0]),
find_value(buttons[1]),
find_value(buttons[3]) - find_value(buttons[2]),
find_value(buttons[5]) - find_value(buttons[4]),
find_value(buttons[7]) - find_value(buttons[6]),
find_value(buttons[9]) - find_value(buttons[8]),
find_value(buttons[0]), // Left Trigger
find_value(buttons[1]), // Right Trigger
find_value(buttons[3]) - find_value(buttons[2]), // Left Stick X
find_value(buttons[5]) - find_value(buttons[4]), // Left Stick Y
find_value(buttons[7]) - find_value(buttons[6]), // Right Stick X
find_value(buttons[9]) - find_value(buttons[8]), // Right Stick Y
};

if (pressed_button.first > 0)
Expand Down Expand Up @@ -623,15 +624,13 @@ int evdev_joystick_handler::add_device(const std::string& device, bool in_settin
libevdev_has_event_code(dev, EV_ABS, ABS_Y) &&
name == device)
{
// It's a joystick.

// Now let's make sure we don't already have this one.
// It's a joystick. Now let's make sure we don't already have this one.
auto it = std::find_if(devices.begin(), devices.end(), [&path](const EvdevDevice &device) { return path == device.path; });
if (it != devices.end())
{
libevdev_free(dev);
close(fd);
return std::distance(devices.begin(), it);
continue;
}

// Alright, now that we've confirmed we haven't added this joystick yet, les do dis.
Expand All @@ -652,14 +651,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);
}
}
Loading

0 comments on commit 8837578

Please sign in to comment.