Input: unify some more code

This commit is contained in:
Megamouse 2021-02-12 01:44:39 +01:00
parent aaae30cb84
commit f6d465667a
7 changed files with 51 additions and 40 deletions

View File

@ -248,28 +248,28 @@ ds3_pad_handler::DataStatus ds3_pad_handler::get_data(ds3_device* ds3dev)
if (!ds3dev)
return DataStatus::ReadError;
auto& dbuf = ds3dev->buf;
ds3dev->padData = {};
#ifdef _WIN32
dbuf[0] = ds3dev->report_id;
const int result = hid_get_feature_report(ds3dev->hidDevice, dbuf, sizeof(dbuf));
ds3dev->padData[0] = ds3dev->report_id;
const int result = hid_get_feature_report(ds3dev->hidDevice, ds3dev->padData.data(), 64);
#else
const int result = hid_read(ds3dev->hidDevice, dbuf, sizeof(dbuf));
const int result = hid_read(ds3dev->hidDevice, ds3dev->padData.data(), 64);
#endif
if (result > 0)
{
#ifdef _WIN32
if (dbuf[0] == ds3dev->report_id)
if (ds3dev->padData[0] == ds3dev->report_id)
#else
if (dbuf[0] == 0x01 && dbuf[1] != 0xFF)
if (ds3dev->padData[0] == 0x01 && ds3dev->padData[1] != 0xFF)
#endif
{
return DataStatus::NewData;
}
else
{
ds3_log.warning("Unknown packet received:0x%02x", dbuf[0]);
ds3_log.warning("Unknown packet received:0x%02x", ds3dev->padData[0]);
return DataStatus::NoNewData;
}
}
@ -289,7 +289,7 @@ std::unordered_map<u64, u16> ds3_pad_handler::get_button_values(const std::share
if (!dev)
return key_buf;
auto& dbuf = dev->buf;
auto& dbuf = dev->padData;
const u8 lsx = dbuf[6 + DS3_HID_OFFSET];
const u8 lsy = dbuf[7 + DS3_HID_OFFSET];
@ -358,14 +358,14 @@ void ds3_pad_handler::get_extended_info(const std::shared_ptr<PadDevice>& device
#ifdef _WIN32
// Official Sony Windows DS3 driver seems to do the same modification of this value as the ps3
pad->m_sensors[0].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->buf[41 + DS3_HID_OFFSET]);
pad->m_sensors[0].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->padData[41 + DS3_HID_OFFSET]);
#else
// When getting raw values from the device this adjustement is needed
pad->m_sensors[0].m_value = 512 - (*reinterpret_cast<le_t<u16, 1>*>(&ds3dev->buf[41]) - 512);
pad->m_sensors[0].m_value = 512 - (*reinterpret_cast<le_t<u16, 1>*>(&ds3dev->padData[41]) - 512);
#endif
pad->m_sensors[1].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->buf[45 + DS3_HID_OFFSET]);
pad->m_sensors[2].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->buf[43 + DS3_HID_OFFSET]);
pad->m_sensors[3].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->buf[47 + DS3_HID_OFFSET]);
pad->m_sensors[1].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->padData[45 + DS3_HID_OFFSET]);
pad->m_sensors[2].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->padData[43 + DS3_HID_OFFSET]);
pad->m_sensors[3].m_value = *reinterpret_cast<le_t<u16, 1>*>(&ds3dev->padData[47 + DS3_HID_OFFSET]);
// Those are formulas used to adjust sensor values in sys_hid code but I couldn't find all the vars.
//auto polish_value = [](s32 value, s32 dword_0x0, s32 dword_0x4, s32 dword_0x8, s32 dword_0xC, s32 dword_0x18, s32 dword_0x1C) -> u16
@ -464,13 +464,27 @@ PadHandlerBase::connection ds3_pad_handler::update_connection(const std::shared_
void ds3_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad)
{
ds3_device* dev = static_cast<ds3_device*>(device.get());
if (!dev || !dev->hidDevice || !pad)
if (!dev || !dev->hidDevice || !dev->config || !pad)
return;
if (dev->large_motor != pad->m_vibrateMotors[0].m_value || dev->small_motor != pad->m_vibrateMotors[1].m_value)
pad_config* config = dev->config;
const int idx_l = config->switch_vibration_motors ? 1 : 0;
const int idx_s = config->switch_vibration_motors ? 0 : 1;
const int speed_large = config->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value : vibration_min;
const int speed_small = config->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value : vibration_min;
dev->new_output_data |= dev->large_motor != speed_large || dev->small_motor != speed_small;
dev->large_motor = speed_large;
dev->small_motor = speed_small;
if (dev->new_output_data)
{
dev->large_motor = static_cast<u8>(pad->m_vibrateMotors[0].m_value);
dev->small_motor = static_cast<u8>(pad->m_vibrateMotors[1].m_value);
send_output_report(dev);
if (send_output_report(dev) >= 0)
{
dev->new_output_data = false;
}
}
}

View File

@ -7,7 +7,6 @@
class ds3_device : public HidDevice
{
public:
u8 buf[64]{0};
#ifdef _WIN32
u8 report_id = 0;
#endif

View File

@ -706,7 +706,6 @@ ds4_pad_handler::DataStatus ds4_pad_handler::get_data(DS4Device* device)
return DataStatus::NoNewData;
const int battery_offset = offset + DS4_INPUT_REPORT_BATTERY_OFFSET;
device->is_initialized = true;
device->cableState = (buf[battery_offset] >> 4) & 0x01;
device->batteryLevel = buf[battery_offset] & 0x0F;
@ -858,10 +857,10 @@ void ds4_pad_handler::get_extended_info(const std::shared_ptr<PadDevice>& device
void ds4_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad)
{
DS4Device* ds4_dev = static_cast<DS4Device*>(device.get());
if (!ds4_dev || !ds4_dev->hidDevice || !pad)
if (!ds4_dev || !ds4_dev->hidDevice || !ds4_dev->config || !pad)
return;
auto config = ds4_dev->config;
pad_config* config = ds4_dev->config;
// Attempt to send rumble no matter what
const int idx_l = config->switch_vibration_motors ? 1 : 0;
@ -874,8 +873,6 @@ void ds4_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, c
const bool lowBattery = ds4_dev->batteryLevel < 2;
const bool isBlinking = ds4_dev->led_delay_on > 0 || ds4_dev->led_delay_off > 0;
bool newBlinkData = false;
// Blink LED when battery is low
if (config->led_low_battery_blink)
{
@ -884,14 +881,14 @@ void ds4_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, c
{
ds4_dev->led_delay_on = 0;
ds4_dev->led_delay_off = 0;
newBlinkData = true;
ds4_dev->new_output_data = true;
}
// we are now wireless and low on battery -> blink
if (!isBlinking && wireless && lowBattery)
else if (!isBlinking && wireless && lowBattery)
{
ds4_dev->led_delay_on = 100;
ds4_dev->led_delay_off = 100;
newBlinkData = true;
ds4_dev->new_output_data = true;
}
}
@ -905,16 +902,20 @@ void ds4_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, c
config->colorR.set(combined_color >> 8);
config->colorG.set(combined_color & 0xff);
config->colorB.set(0);
ds4_dev->new_output_data = true;
}
}
ds4_dev->newVibrateData |= ds4_dev->large_motor != speed_large || ds4_dev->small_motor != speed_small || newBlinkData;
ds4_dev->new_output_data |= ds4_dev->large_motor != speed_large || ds4_dev->small_motor != speed_small;
ds4_dev->large_motor = speed_large;
ds4_dev->small_motor = speed_small;
if (ds4_dev->newVibrateData && send_output_report(ds4_dev) >= 0)
if (ds4_dev->new_output_data)
{
ds4_dev->newVibrateData = false;
if (send_output_report(ds4_dev) >= 0)
{
ds4_dev->new_output_data = false;
}
}
}

View File

@ -11,12 +11,9 @@ public:
bool btCon{false};
bool hasCalibData{false};
std::array<CalibData, CalibIndex::COUNT> calibData{};
bool newVibrateData{true};
std::array<u8, 64> padData{};
u8 batteryLevel{0};
u8 last_battery_level{0};
u8 cableState{0};
bool is_initialized{false};
};
class ds4_pad_handler final : public hid_pad_handler<DS4Device>

View File

@ -918,10 +918,10 @@ int dualsense_pad_handler::send_output_report(DualSenseDevice* device)
void dualsense_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad)
{
DualSenseDevice* dualsense_dev = static_cast<DualSenseDevice*>(device.get());
if (!dualsense_dev || !dualsense_dev->hidDevice || !pad)
if (!dualsense_dev || !dualsense_dev->hidDevice || !dualsense_dev->config || !pad)
return;
auto config = dualsense_dev->config;
pad_config* config = dualsense_dev->config;
// Attempt to send rumble no matter what
const int idx_l = config->switch_vibration_motors ? 1 : 0;
@ -930,16 +930,16 @@ void dualsense_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& dev
const int speed_large = config->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value : vibration_min;
const int speed_small = config->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value : vibration_min;
dualsense_dev->newVibrateData |= dualsense_dev->large_motor != speed_large || dualsense_dev->small_motor != speed_small;
dualsense_dev->new_output_data |= dualsense_dev->large_motor != speed_large || dualsense_dev->small_motor != speed_small;
dualsense_dev->large_motor = speed_large;
dualsense_dev->small_motor = speed_small;
if (dualsense_dev->newVibrateData)
if (dualsense_dev->new_output_data)
{
if (send_output_report(dualsense_dev) >= 0)
{
dualsense_dev->newVibrateData = false;
dualsense_dev->new_output_data = false;
}
}
}

View File

@ -19,8 +19,6 @@ public:
bool has_calib_data{false};
std::array<CalibData, CalibIndex::COUNT> calib_data{};
DualSenseDataMode dataMode{DualSenseDataMode::Simple};
std::array<u8, 64> padData{};
bool newVibrateData{true};
bool init_lightbar{true};
bool update_lightbar{true};
bool update_player_leds{true};

View File

@ -31,6 +31,8 @@ class HidDevice : public PadDevice
public:
hid_device* hidDevice{nullptr};
std::string path{""};
std::array<u8, 64> padData{};
bool new_output_data{true};
u8 large_motor{0};
u8 small_motor{0};
u8 led_delay_on{0};