Pad Refactoring

Adds a window to setup multiple input types as once
All controllers are now handled by a single thread
[hcorion] evdev refactor
This commit is contained in:
RipleyTom 2017-08-15 14:03:07 +02:00 committed by Ani
parent a6ba7ed21c
commit 0457f23b13
28 changed files with 1776 additions and 1416 deletions

View file

@ -11,7 +11,7 @@
namespace
{
const auto THREAD_SLEEP = 1ms; //ds4 has new data every ~4ms,
const auto THREAD_SLEEP = 1ms; //ds4 has new data every ~4ms,
const auto THREAD_SLEEP_INACTIVE = 100ms;
const u32 DS4_ACC_RES_PER_G = 8192;
@ -57,12 +57,12 @@ namespace
// compute angle and len of given point to be used for squircle radius
const f32 angle = std::atan2(y, x);
const f32 r = std::sqrt(std::pow(x, 2.f) + std::pow(y, 2.f));
// now find len/point on the given squircle from our current angle and radius in polar coords
// https://thatsmaths.com/2016/07/14/squircles/
const f32 newLen = (1 + std::pow(std::sin(2 * angle), 2.f) / 8.f) * r;
// we now have len and angle, convert to cartisian
// we now have len and angle, convert to cartisian
const int newX = Clamp0To255(((newLen * std::cos(angle)) + 1) * 127);
const int newY = Clamp0To255(((newLen * std::sin(angle)) + 1) * 127);
@ -125,134 +125,18 @@ namespace
return (u32)(((u32)buf[0] << 0) + ((u32)buf[1] << 8) + ((u32)buf[2] << 16) + ((u32)buf[3] << 24));
}
}
ds4_pad_handler::~ds4_pad_handler()
ds4_pad_handler::ds4_pad_handler() : is_init(false)
{
Close();
}
void ds4_pad_handler::Init(const u32 max_connect)
{
std::memset(&m_info, 0, sizeof m_info);
m_info.max_connect = max_connect;
for (u32 i = 0, max = std::min(max_connect, u32(MAX_GAMEPADS)); i != max; ++i)
{
m_pads.emplace_back(
CELL_PAD_STATUS_DISCONNECTED,
CELL_PAD_SETTING_PRESS_OFF | CELL_PAD_SETTING_SENSOR_OFF,
CELL_PAD_CAPABILITY_PS3_CONFORMITY | CELL_PAD_CAPABILITY_PRESS_MODE | CELL_PAD_CAPABILITY_HP_ANALOG_STICK | CELL_PAD_CAPABILITY_ACTUATOR | CELL_PAD_CAPABILITY_SENSOR_MODE,
CELL_PAD_DEV_TYPE_STANDARD
);
auto & pad = m_pads.back();
// 'keycode' here is just 0 as we have to manually calculate this
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_L2);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_R2);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_UP);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_DOWN);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_LEFT);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_RIGHT);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_SQUARE);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_CROSS);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_CIRCLE);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_TRIANGLE);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_L1);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_R1);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_SELECT);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_START);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_L3);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_R3);
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x100/*CELL_PAD_CTRL_PS*/);// TODO: PS button support
pad.m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x0); // Reserved
pad.m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_X, 512);
pad.m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_Y, 399);
pad.m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_Z, 512);
pad.m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_G, 512);
pad.m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X, 0, 0);
pad.m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y, 0, 0);
pad.m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X, 0, 0);
pad.m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y, 0, 0);
pad.m_vibrateMotors.emplace_back(true, 0);
pad.m_vibrateMotors.emplace_back(false, 0);
}
ds4Thread = std::make_shared<ds4_thread>();
ds4Thread->on_init(ds4Thread);
}
PadInfo& ds4_pad_handler::GetInfo()
{
if (ds4Thread)
{
auto info = ds4Thread->GetConnectedControllers();
m_info.now_connect = 0;
int i = 0;
for (auto & pad : m_pads)
{
if (info[i])
{
m_info.now_connect++;
if (last_connection_status[i] == false)
pad.m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
last_connection_status[i] = true;
pad.m_port_status |= CELL_PAD_STATUS_CONNECTED;
}
else
{
if (last_connection_status[i] == true)
pad.m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
last_connection_status[i] = false;
pad.m_port_status &= ~CELL_PAD_STATUS_CONNECTED;
}
++i;
}
}
return m_info;
}
std::vector<Pad>& ds4_pad_handler::GetPads()
{
if (ds4Thread)
ProcessData();
return m_pads;
}
void ds4_pad_handler::Close()
{
if (ds4Thread)
ds4Thread.reset();
m_pads.clear();
}
void ds4_pad_handler::ProcessData()
{
if (!ds4Thread)
return;
auto data = ds4Thread->GetControllerData();
int i = 0;
for (auto & pad : m_pads)
for (auto &bind : bindings)
{
auto buf = data[i];
std::shared_ptr<DS4Device> device = bind.first;
auto pad = bind.second;
auto buf = device->padData;
// these are added with previous value and divided to 'smooth' out the readings
// the ds4 seems to rapidly flicker sometimes between two values and this seems to stop that
@ -260,114 +144,114 @@ void ds4_pad_handler::ProcessData()
u16 lx, ly;
//std::tie(lx, ly) = ConvertToSquarePoint(buf[1], buf[2]);
std::tie(lx, ly) = ConvertToSquirclePoint(buf[1], buf[2]);
pad.m_sticks[0].m_value = (lx + pad.m_sticks[0].m_value) / 2; // LX
pad.m_sticks[1].m_value = (ly + pad.m_sticks[1].m_value) / 2; // LY
pad->m_sticks[0].m_value = (lx + pad->m_sticks[0].m_value) / 2; // LX
pad->m_sticks[1].m_value = (ly + pad->m_sticks[1].m_value) / 2; // LY
u16 rx, ry;
//std::tie(rx, ry) = ConvertToSquarePoint(buf[3], buf[4]);
std::tie(rx, ry) = ConvertToSquirclePoint(buf[3], buf[4]);
pad.m_sticks[2].m_value = (rx + pad.m_sticks[2].m_value) / 2; // RX
pad.m_sticks[3].m_value = (ry + pad.m_sticks[3].m_value) / 2; // RY
pad->m_sticks[2].m_value = (rx + pad->m_sticks[2].m_value) / 2; // RX
pad->m_sticks[3].m_value = (ry + pad->m_sticks[3].m_value) / 2; // RY
// l2 r2
pad.m_buttons[0].m_pressed = buf[8] > 0;
pad.m_buttons[0].m_value = buf[8];
pad.m_buttons[1].m_pressed = buf[9] > 0;
pad.m_buttons[1].m_value = buf[9];
// l2 r2
pad->m_buttons[0].m_pressed = buf[8] > 0;
pad->m_buttons[0].m_value = buf[8];
pad->m_buttons[1].m_pressed = buf[9] > 0;
pad->m_buttons[1].m_value = buf[9];
// bleh, dpad in buffer is stored in a different state
// bleh, dpad in buffer is stored in a different state
u8 dpadState = buf[5] & 0xf;
switch (dpadState)
{
case 0x08: // none pressed
pad.m_buttons[2].m_pressed = false;
pad.m_buttons[2].m_value = 0;
pad.m_buttons[3].m_pressed = false;
pad.m_buttons[3].m_value = 0;
pad.m_buttons[4].m_pressed = false;
pad.m_buttons[4].m_value = 0;
pad.m_buttons[5].m_pressed = false;
pad.m_buttons[5].m_value = 0;
pad->m_buttons[2].m_pressed = false;
pad->m_buttons[2].m_value = 0;
pad->m_buttons[3].m_pressed = false;
pad->m_buttons[3].m_value = 0;
pad->m_buttons[4].m_pressed = false;
pad->m_buttons[4].m_value = 0;
pad->m_buttons[5].m_pressed = false;
pad->m_buttons[5].m_value = 0;
break;
case 0x07: // NW...left and up
pad.m_buttons[2].m_pressed = true;
pad.m_buttons[2].m_value = 255;
pad.m_buttons[3].m_pressed = false;
pad.m_buttons[3].m_value = 0;
pad.m_buttons[4].m_pressed = true;
pad.m_buttons[4].m_value = 255;
pad.m_buttons[5].m_pressed = false;
pad.m_buttons[5].m_value = 0;
pad->m_buttons[2].m_pressed = true;
pad->m_buttons[2].m_value = 255;
pad->m_buttons[3].m_pressed = false;
pad->m_buttons[3].m_value = 0;
pad->m_buttons[4].m_pressed = true;
pad->m_buttons[4].m_value = 255;
pad->m_buttons[5].m_pressed = false;
pad->m_buttons[5].m_value = 0;
break;
case 0x06: // W..left
pad.m_buttons[2].m_pressed = false;
pad.m_buttons[2].m_value = 0;
pad.m_buttons[3].m_pressed = false;
pad.m_buttons[3].m_value = 0;
pad.m_buttons[4].m_pressed = true;
pad.m_buttons[4].m_value = 255;
pad.m_buttons[5].m_pressed = false;
pad.m_buttons[5].m_value = 0;
pad->m_buttons[2].m_pressed = false;
pad->m_buttons[2].m_value = 0;
pad->m_buttons[3].m_pressed = false;
pad->m_buttons[3].m_value = 0;
pad->m_buttons[4].m_pressed = true;
pad->m_buttons[4].m_value = 255;
pad->m_buttons[5].m_pressed = false;
pad->m_buttons[5].m_value = 0;
break;
case 0x05: // SW..left down
pad.m_buttons[2].m_pressed = false;
pad.m_buttons[2].m_value = 0;
pad.m_buttons[3].m_pressed = true;
pad.m_buttons[3].m_value = 255;
pad.m_buttons[4].m_pressed = true;
pad.m_buttons[4].m_value = 255;
pad.m_buttons[5].m_pressed = false;
pad.m_buttons[5].m_value = 0;
pad->m_buttons[2].m_pressed = false;
pad->m_buttons[2].m_value = 0;
pad->m_buttons[3].m_pressed = true;
pad->m_buttons[3].m_value = 255;
pad->m_buttons[4].m_pressed = true;
pad->m_buttons[4].m_value = 255;
pad->m_buttons[5].m_pressed = false;
pad->m_buttons[5].m_value = 0;
break;
case 0x04: // S..down
pad.m_buttons[2].m_pressed = false;
pad.m_buttons[2].m_value = 0;
pad.m_buttons[3].m_pressed = true;
pad.m_buttons[3].m_value = 255;
pad.m_buttons[4].m_pressed = false;
pad.m_buttons[4].m_value = 0;
pad.m_buttons[5].m_pressed = false;
pad.m_buttons[5].m_value = 0;
pad->m_buttons[2].m_pressed = false;
pad->m_buttons[2].m_value = 0;
pad->m_buttons[3].m_pressed = true;
pad->m_buttons[3].m_value = 255;
pad->m_buttons[4].m_pressed = false;
pad->m_buttons[4].m_value = 0;
pad->m_buttons[5].m_pressed = false;
pad->m_buttons[5].m_value = 0;
break;
case 0x03: // SE..down and right
pad.m_buttons[2].m_pressed = false;
pad.m_buttons[2].m_value = 0;
pad.m_buttons[3].m_pressed = true;
pad.m_buttons[3].m_value = 255;
pad.m_buttons[4].m_pressed = false;
pad.m_buttons[4].m_value = 0;
pad.m_buttons[5].m_pressed = true;
pad.m_buttons[5].m_value = 255;
pad->m_buttons[2].m_pressed = false;
pad->m_buttons[2].m_value = 0;
pad->m_buttons[3].m_pressed = true;
pad->m_buttons[3].m_value = 255;
pad->m_buttons[4].m_pressed = false;
pad->m_buttons[4].m_value = 0;
pad->m_buttons[5].m_pressed = true;
pad->m_buttons[5].m_value = 255;
break;
case 0x02: // E... right
pad.m_buttons[2].m_pressed = false;
pad.m_buttons[2].m_value = 0;
pad.m_buttons[3].m_pressed = false;
pad.m_buttons[3].m_value = 0;
pad.m_buttons[4].m_pressed = false;
pad.m_buttons[4].m_value = 0;
pad.m_buttons[5].m_pressed = true;
pad.m_buttons[5].m_value = 255;
pad->m_buttons[2].m_pressed = false;
pad->m_buttons[2].m_value = 0;
pad->m_buttons[3].m_pressed = false;
pad->m_buttons[3].m_value = 0;
pad->m_buttons[4].m_pressed = false;
pad->m_buttons[4].m_value = 0;
pad->m_buttons[5].m_pressed = true;
pad->m_buttons[5].m_value = 255;
break;
case 0x01: // NE.. up right
pad.m_buttons[2].m_pressed = true;
pad.m_buttons[2].m_value = 255;
pad.m_buttons[3].m_pressed = false;
pad.m_buttons[3].m_value = 0;
pad.m_buttons[4].m_pressed = false;
pad.m_buttons[4].m_value = 0;
pad.m_buttons[5].m_pressed = true;
pad.m_buttons[5].m_value = 255;
pad->m_buttons[2].m_pressed = true;
pad->m_buttons[2].m_value = 255;
pad->m_buttons[3].m_pressed = false;
pad->m_buttons[3].m_value = 0;
pad->m_buttons[4].m_pressed = false;
pad->m_buttons[4].m_value = 0;
pad->m_buttons[5].m_pressed = true;
pad->m_buttons[5].m_value = 255;
break;
case 0x00: // n.. up
pad.m_buttons[2].m_pressed = true;
pad.m_buttons[2].m_value = 255;
pad.m_buttons[3].m_pressed = false;
pad.m_buttons[3].m_value = 0;
pad.m_buttons[4].m_pressed = false;
pad.m_buttons[4].m_value = 0;
pad.m_buttons[5].m_pressed = false;
pad.m_buttons[5].m_value = 0;
pad->m_buttons[2].m_pressed = true;
pad->m_buttons[2].m_value = 255;
pad->m_buttons[3].m_pressed = false;
pad->m_buttons[3].m_value = 0;
pad->m_buttons[4].m_pressed = false;
pad->m_buttons[4].m_value = 0;
pad->m_buttons[5].m_pressed = false;
pad->m_buttons[5].m_value = 0;
break;
default:
fmt::throw_exception("ds4 dpad state encountered unexpected input");
@ -377,31 +261,31 @@ void ds4_pad_handler::ProcessData()
for (int i = 4; i < 8; ++i)
{
const bool pressed = ((buf[5] & (1 << i)) != 0);
pad.m_buttons[6 + i - 4].m_pressed = pressed;
pad.m_buttons[6 + i - 4].m_value = pressed ? 255 : 0;
pad->m_buttons[6 + i - 4].m_pressed = pressed;
pad->m_buttons[6 + i - 4].m_value = pressed ? 255 : 0;
}
// L1, R1
const bool l1press = ((buf[6] & (1 << 0)) != 0);
pad.m_buttons[10].m_pressed = l1press;
pad.m_buttons[10].m_value = l1press ? 255 : 0;
pad->m_buttons[10].m_pressed = l1press;
pad->m_buttons[10].m_value = l1press ? 255 : 0;
const bool l2press = ((buf[6] & (1 << 1)) != 0);
pad.m_buttons[11].m_pressed = l2press;
pad.m_buttons[11].m_value = l2press ? 255 : 0;
pad->m_buttons[11].m_pressed = l2press;
pad->m_buttons[11].m_value = l2press ? 255 : 0;
// select, start, l3, r3
for (int i = 4; i < 8; ++i)
{
const bool pressed = ((buf[6] & (1 << i)) != 0);
pad.m_buttons[12 + i - 4].m_pressed = pressed;
pad.m_buttons[12 + i - 4].m_value = pressed ? 255 : 0;
pad->m_buttons[12 + i - 4].m_pressed = pressed;
pad->m_buttons[12 + i - 4].m_value = pressed ? 255 : 0;
}
#ifdef _WIN32
for (int i = 6; i < 16; i++)
{
if (pad.m_buttons[i].m_pressed)
if (pad->m_buttons[i].m_pressed)
{
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
break;
@ -422,10 +306,10 @@ void ds4_pad_handler::ProcessData()
accelY = accelY * 113 + 512;
accelZ = accelZ * 113 + 512;
pad.m_sensors[0].m_value = Clamp0To1023(accelX);
pad.m_sensors[1].m_value = Clamp0To1023(accelY);
pad.m_sensors[2].m_value = Clamp0To1023(accelZ);
pad->m_sensors[0].m_value = Clamp0To1023(accelX);
pad->m_sensors[1].m_value = Clamp0To1023(accelY);
pad->m_sensors[2].m_value = Clamp0To1023(accelZ);
// gyroX is yaw, which is all that we need
f32 gyroX = (((s16)((u16)(buf[16] << 8) | buf[15])) / static_cast<f32>(DS4_GYRO_RES_PER_DEG_S)) * -1;
//const int gyroY = ((u16)(buf[14] << 8) | buf[13]) / 256;
@ -434,73 +318,25 @@ void ds4_pad_handler::ProcessData()
// convert to ds3
gyroX = gyroX * (123.f / 90.f) + 512;
pad.m_sensors[3].m_value = Clamp0To1023(gyroX);
i++;
pad->m_sensors[3].m_value = Clamp0To1023(gyroX);
}
}
void ds4_pad_handler::SetRumble(const u32 pad, u8 largeMotor, bool smallMotor)
void ds4_pad_handler::UpdateRumble()
{
if (pad > m_pads.size())
return;
m_pads[pad].m_vibrateMotors[0].m_value = largeMotor;
m_pads[pad].m_vibrateMotors[1].m_value = smallMotor ? 255 : 0;
if (!ds4Thread)
return;
ds4Thread->SetRumbleData(pad, largeMotor, smallMotor ? 255 : 0);
}
void ds4_thread::SetRumbleData(u32 port, u8 largeVibrate, u8 smallVibrate)
{
semaphore_lock lock(mutex);
// todo: give unique identifier to this instead of port
u32 i = 0;
for (auto & controller : controllers)
for (auto &bind : bindings)
{
if (i == port)
{
controller.second.newVibrateData = controller.second.largeVibrate != largeVibrate || controller.second.smallVibrate != smallVibrate;
controller.second.largeVibrate = largeVibrate;
controller.second.smallVibrate = smallVibrate;
break;
}
++i;
std::shared_ptr<DS4Device> device = bind.first;
auto thepad = bind.second;
device->newVibrateData = device->largeVibrate != thepad->m_vibrateMotors[0].m_value || device->smallVibrate != (thepad->m_vibrateMotors[1].m_value ? 255 : 0);
device->largeVibrate = thepad->m_vibrateMotors[0].m_value;
device->smallVibrate = (thepad->m_vibrateMotors[1].m_value ? 255 : 0);
}
}
std::array<bool, MAX_GAMEPADS> ds4_thread::GetConnectedControllers()
{
std::array<bool, MAX_GAMEPADS> rtnData{};
int i = 0;
semaphore_lock lock(mutex);
for (const auto & cont : controllers)
rtnData[i++] = cont.second.hidDevice != nullptr;
return rtnData;
}
std::array<std::array<u8, 64>, MAX_GAMEPADS> ds4_thread::GetControllerData()
{
std::array<std::array<u8, 64>, MAX_GAMEPADS> rtnData;
int i = 0;
semaphore_lock lock(mutex);
for (const auto & data : padData)
rtnData[i++] = data;
return rtnData;
}
bool ds4_thread::GetCalibrationData(DS4Device* ds4Dev)
bool ds4_pad_handler::GetCalibrationData(std::shared_ptr<DS4Device> ds4Dev)
{
std::array<u8, 64> buf;
if (ds4Dev->btCon)
@ -588,11 +424,11 @@ bool ds4_thread::GetCalibrationData(DS4Device* ds4Dev)
return true;
}
void ds4_thread::CheckAddDevice(hid_device* hidDevice, hid_device_info* hidDevInfo)
void ds4_pad_handler::CheckAddDevice(hid_device* hidDevice, hid_device_info* hidDevInfo)
{
std::string serial = "";
DS4Device ds4Dev;
ds4Dev.hidDevice = hidDevice;
std::shared_ptr<DS4Device> ds4Dev = std::make_shared<DS4Device>();
ds4Dev->hidDevice = hidDevice;
// There isnt a nice 'portable' way with hidapi to detect bt vs wired as the pid/vid's are the same
// Let's try getting 0x81 feature report, which should will return mac address on wired, and should error on bluetooth
std::array<u8, 64> buf{};
@ -603,80 +439,45 @@ void ds4_thread::CheckAddDevice(hid_device* hidDevice, hid_device_info* hidDevIn
}
else
{
ds4Dev.btCon = true;
ds4Dev->btCon = true;
std::wstring wSerial(hidDevInfo->serial_number);
serial = std::string(wSerial.begin(), wSerial.end());
}
if (!GetCalibrationData(&ds4Dev))
if (!GetCalibrationData(ds4Dev))
{
LOG_ERROR(HLE, "[DS4] Failed getting calibration data, ignoring controller!");
hid_close(hidDevice);
return;
}
ds4Dev.path = hidDevInfo->path;
ds4Dev->path = hidDevInfo->path;
hid_set_nonblocking(hidDevice, 1);
controllers.emplace(serial, ds4Dev);
}
void ds4_thread::on_init(const std::shared_ptr<void>& _this)
ds4_pad_handler::~ds4_pad_handler()
{
const int res = hid_init();
if (res != 0)
fmt::throw_exception("hidapi-init error.threadproc");
// get all the possible controllers at start
for (auto pid : ds4Pids)
for (auto& controller : controllers)
{
hid_device_info* devInfo = hid_enumerate(DS4_VID, pid);
hid_device_info* head = devInfo;
while (devInfo)
{
if (controllers.size() >= MAX_GAMEPADS)
break;
hid_device* dev = hid_open_path(devInfo->path);
if (dev)
CheckAddDevice(dev, devInfo);
devInfo = devInfo->next;
}
hid_free_enumeration(head);
}
if (controllers.size() == 0)
LOG_ERROR(HLE, "[DS4] No controllers found!");
else
LOG_SUCCESS(HLE, "[DS4] Controllers found: %d", controllers.size());
named_thread::on_init(_this);
}
ds4_thread::~ds4_thread()
{
for (auto & controller : controllers)
{
if (controller.second.hidDevice)
hid_close(controller.second.hidDevice);
if (controller.second->hidDevice)
hid_close(controller.second->hidDevice);
}
hid_exit();
}
void ds4_thread::SendVibrateData(const DS4Device& device)
void ds4_pad_handler::SendVibrateData(const std::shared_ptr<DS4Device> device)
{
std::array<u8, 78> outputBuf{0};
// write rumble state
if (device.btCon)
if (device->btCon)
{
outputBuf[0] = 0x11;
outputBuf[1] = 0xC4;
outputBuf[3] = 0x07;
outputBuf[6] = device.smallVibrate;
outputBuf[7] = device.largeVibrate;
outputBuf[6] = device->smallVibrate;
outputBuf[7] = device->largeVibrate;
outputBuf[8] = 0x00; // red
outputBuf[9] = 0x00; // green
outputBuf[10] = 0xff; // blue
@ -690,124 +491,228 @@ void ds4_thread::SendVibrateData(const DS4Device& device)
outputBuf[76] = (crcCalc >> 16) & 0xFF;
outputBuf[77] = (crcCalc >> 24) & 0xFF;
hid_write_control(device.hidDevice, outputBuf.data(), DS4_OUTPUT_REPORT_0x11_SIZE);
hid_write_control(device->hidDevice, outputBuf.data(), DS4_OUTPUT_REPORT_0x11_SIZE);
}
else
{
outputBuf[0] = 0x05;
outputBuf[1] = 0x07;
outputBuf[4] = device.smallVibrate;
outputBuf[5] = device.largeVibrate;
outputBuf[4] = device->smallVibrate;
outputBuf[5] = device->largeVibrate;
outputBuf[6] = 0x00; // red
outputBuf[7] = 0x00; // green
outputBuf[8] = 0xff; // blue
hid_write(device.hidDevice, outputBuf.data(), DS4_OUTPUT_REPORT_0x05_SIZE);
hid_write(device->hidDevice, outputBuf.data(), DS4_OUTPUT_REPORT_0x05_SIZE);
}
}
void ds4_thread::on_task()
bool ds4_pad_handler::Init()
{
while (!Emu.IsStopped())
if (is_init) return true;
const int res = hid_init();
if (res != 0)
fmt::throw_exception("hidapi-init error.threadproc");
// get all the possible controllers at start
for (auto pid : ds4Pids)
{
if (Emu.IsPaused())
hid_device_info* devInfo = hid_enumerate(DS4_VID, pid);
hid_device_info* head = devInfo;
while (devInfo)
{
std::this_thread::sleep_for(10ms);
if (controllers.size() >= MAX_GAMEPADS) break;
hid_device* dev = hid_open_path(devInfo->path);
if (dev) CheckAddDevice(dev, devInfo);
devInfo = devInfo->next;
}
hid_free_enumeration(head);
}
if (controllers.size() == 0)
LOG_ERROR(HLE, "[DS4] No controllers found!");
else
LOG_SUCCESS(HLE, "[DS4] Controllers found: %d", controllers.size());
is_init = true;
return true;
}
std::vector<std::string> ds4_pad_handler::ListDevices()
{
std::vector<std::string> ds4_pads_list;
if (!Init()) return ds4_pads_list;
for (auto& pad : controllers)
{
ds4_pads_list.emplace_back("Ds4 Pad #" + pad.first);
}
return ds4_pads_list;
}
bool ds4_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device)
{
size_t pos = device.find("Ds4 Pad #");
if (pos == std::string::npos) return false;
std::string pad_serial = device.substr(pos + 9);
std::shared_ptr<DS4Device> device_id = nullptr;
for (auto& cur_control : controllers)
{
if (pad_serial == cur_control.first)
{
device_id = cur_control.second;
break;
}
}
if (device_id == nullptr) return false;
pad->Init(
CELL_PAD_STATUS_CONNECTED,
CELL_PAD_SETTING_PRESS_OFF | CELL_PAD_SETTING_SENSOR_OFF,
CELL_PAD_CAPABILITY_PS3_CONFORMITY | CELL_PAD_CAPABILITY_PRESS_MODE | CELL_PAD_CAPABILITY_HP_ANALOG_STICK | CELL_PAD_CAPABILITY_ACTUATOR | CELL_PAD_CAPABILITY_SENSOR_MODE,
CELL_PAD_DEV_TYPE_STANDARD
);
// 'keycode' here is just 0 as we have to manually calculate this
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_L2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_R2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_UP);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_DOWN);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_LEFT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_RIGHT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_SQUARE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_CROSS);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_CIRCLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_TRIANGLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_L1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, CELL_PAD_CTRL_R1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_SELECT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_START);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_L3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, 0, CELL_PAD_CTRL_R3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x100/*CELL_PAD_CTRL_PS*/);// TODO: PS button support
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x0); // Reserved
pad->m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_X, 512);
pad->m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_Y, 399);
pad->m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_Z, 512);
pad->m_sensors.emplace_back(CELL_PAD_BTN_OFFSET_SENSOR_G, 512);
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X, 0, 0);
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y, 0, 0);
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X, 0, 0);
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y, 0, 0);
pad->m_vibrateMotors.emplace_back(true, 0);
pad->m_vibrateMotors.emplace_back(false, 0);
bindings.emplace_back(device_id, pad);
}
void ds4_pad_handler::ThreadProc()
{
UpdateRumble();
std::array<u8, 78> buf{};
for (auto &bind : bindings)
{
std::shared_ptr<DS4Device> device = bind.first;
auto thepad = bind.second;
if (device->hidDevice == nullptr)
{
// try to reconnect
hid_device* dev = hid_open_path(device->path.c_str());
if (dev)
{
hid_set_nonblocking(dev, 1);
device->hidDevice = dev;
thepad->m_port_status = CELL_PAD_STATUS_CONNECTED|CELL_PAD_STATUS_ASSIGN_CHANGES;
}
else
{
// nope, not there
thepad->m_port_status = CELL_PAD_STATUS_DISCONNECTED|CELL_PAD_STATUS_ASSIGN_CHANGES;
continue;
}
}
const int res = hid_read(device->hidDevice, buf.data(), device->btCon ? 78 : 64);
if (res == -1)
{
// looks like controller disconnected or read error, deal with it on next loop
hid_close(device->hidDevice);
device->hidDevice = nullptr;
continue;
}
u32 online = 0;
u32 i = 0;
std::array<u8, 78> buf{};
for (auto & controller : controllers)
if (device->newVibrateData)
{
semaphore_lock lock(mutex);
if (controller.second.hidDevice == nullptr)
{
// try to reconnect
hid_device* dev = hid_open_path(controller.second.path.c_str());
if (dev)
{
hid_set_nonblocking(dev, 1);
controller.second.hidDevice = dev;
}
else
{
// nope, not there
continue;
}
}
online++;
const int res = hid_read(controller.second.hidDevice, buf.data(), controller.second.btCon ? 78 : 64);
if (res == -1)
{
// looks like controller disconnected or read error, deal with it on next loop
hid_close(controller.second.hidDevice);
controller.second.hidDevice = nullptr;
continue;
}
// no data? keep going
if (res == 0)
continue;
// bt controller sends this until 0x02 feature report is sent back (happens on controller init/restart)
if (controller.second.btCon && buf[0] == 0x1)
{
// tells controller to send 0x11 reports
std::array<u8, 64> buf{};
buf[0] = 0x2;
hid_get_feature_report(controller.second.hidDevice, buf.data(), buf.size());
continue;
}
int offset = 0;
// check report and set offset
if (controller.second.btCon && buf[0] == 0x11 && res == 78)
{
offset = 2;
const u8 btHdr = 0xA1;
const u32 crcHdr = CRCPP::CRC::Calculate(&btHdr, 1, crcTable);
const u32 crcCalc = CRCPP::CRC::Calculate(buf.data(), (DS4_INPUT_REPORT_0x11_SIZE - 4), crcTable, crcHdr);
const u32 crcReported = GetU32LEData(&buf[DS4_INPUT_REPORT_0x11_SIZE - 4]);
if (crcCalc != crcReported) {
LOG_WARNING(HLE, "[DS4] Data packet CRC check failed, ignoring! Received 0x%x, Expected 0x%x", crcReported, crcCalc);
continue;
}
}
else if (!controller.second.btCon && buf[0] == 0x01 && res == 64)
offset = 0;
else
continue;
int calibOffset = offset + DS4_INPUT_REPORT_GYRO_X_OFFSET;
for (int i = 0; i < DS4CalibIndex::COUNT; ++i)
{
const s16 rawValue = GetS16LEData(&buf[calibOffset]);
const s16 calValue = ApplyCalibration(rawValue, controller.second.calibData[i]);
buf[calibOffset++] = ((u16)calValue >> 0) & 0xFF;
buf[calibOffset++] = ((u16)calValue >> 8) & 0xFF;
}
memcpy(padData[i].data(), &buf[offset], 64);
if (controller.second.newVibrateData)
{
SendVibrateData(controller.second);
controller.second.newVibrateData = false;
}
i++;
SendVibrateData(device);
device->newVibrateData = false;
}
std::this_thread::sleep_for((online > 0) ? THREAD_SLEEP : THREAD_SLEEP_INACTIVE);
}
}
// no data? keep going
if (res == 0)
continue;
// bt controller sends this until 0x02 feature report is sent back (happens on controller init/restart)
if (device->btCon && buf[0] == 0x1)
{
// tells controller to send 0x11 reports
std::array<u8, 64> buf_error{};
buf_error[0] = 0x2;
hid_get_feature_report(device->hidDevice, buf_error.data(), buf_error.size());
continue;
}
int offset = 0;
// check report and set offset
if (device->btCon && buf[0] == 0x11 && res == 78)
{
offset = 2;
const u8 btHdr = 0xA1;
const u32 crcHdr = CRCPP::CRC::Calculate(&btHdr, 1, crcTable);
const u32 crcCalc = CRCPP::CRC::Calculate(buf.data(), (DS4_INPUT_REPORT_0x11_SIZE - 4), crcTable, crcHdr);
const u32 crcReported = GetU32LEData(&buf[DS4_INPUT_REPORT_0x11_SIZE - 4]);
if (crcCalc != crcReported) {
LOG_WARNING(HLE, "[DS4] Data packet CRC check failed, ignoring! Received 0x%x, Expected 0x%x", crcReported, crcCalc);
continue;
}
}
else if (!device->btCon && buf[0] == 0x01 && res == 64)
offset = 0;
else
continue;
int calibOffset = offset + DS4_INPUT_REPORT_GYRO_X_OFFSET;
for (int i = 0; i < DS4CalibIndex::COUNT; ++i)
{
const s16 rawValue = GetS16LEData(&buf[calibOffset]);
const s16 calValue = ApplyCalibration(rawValue, device->calibData[i]);
buf[calibOffset++] = ((u16)calValue >> 0) & 0xFF;
buf[calibOffset++] = ((u16)calValue >> 8) & 0xFF;
}
memcpy(device->padData.data(), &buf[offset], 64);
}
ProcessData();
}