Input: move some pad handler logic to the parent class

This commit is contained in:
Megamouse 2019-09-20 16:28:55 +02:00
parent 8f47f9517a
commit 4594148409
19 changed files with 1574 additions and 1602 deletions

View file

@ -12,41 +12,38 @@ namespace XINPUT_INFO
};
} // namespace XINPUT_INFO
// ScpToolkit defined structure for pressure sensitive button query
struct SCP_EXTN
{
float SCP_UP;
float SCP_RIGHT;
float SCP_DOWN;
float SCP_LEFT;
float SCP_LX;
float SCP_LY;
float SCP_L1;
float SCP_L2;
float SCP_L3;
float SCP_RX;
float SCP_RY;
float SCP_R1;
float SCP_R2;
float SCP_R3;
float SCP_T;
float SCP_C;
float SCP_X;
float SCP_S;
float SCP_SELECT;
float SCP_START;
float SCP_PS;
};
xinput_pad_handler::xinput_pad_handler() : PadHandlerBase(pad_handler::xinput)
{
// Unique names for the config files and our pad settings dialog
button_list =
{
{ XInputKeyCodes::A, "A" },
{ XInputKeyCodes::B, "B" },
{ XInputKeyCodes::X, "X" },
{ XInputKeyCodes::Y, "Y" },
{ XInputKeyCodes::Left, "Left" },
{ XInputKeyCodes::Right, "Right" },
{ XInputKeyCodes::Up, "Up" },
{ XInputKeyCodes::Down, "Down" },
{ XInputKeyCodes::LB, "LB" },
{ XInputKeyCodes::RB, "RB" },
{ XInputKeyCodes::Back, "Back" },
{ XInputKeyCodes::Start, "Start" },
{ XInputKeyCodes::LS, "LS" },
{ XInputKeyCodes::RS, "RS" },
{ XInputKeyCodes::Guide, "Guide" },
{ XInputKeyCodes::LT, "LT" },
{ XInputKeyCodes::RT, "RT" },
{ XInputKeyCodes::LSXNeg, "LS X-" },
{ XInputKeyCodes::LSXPos, "LS X+" },
{ XInputKeyCodes::LSYPos, "LS Y+" },
{ XInputKeyCodes::LSYNeg, "LS Y-" },
{ XInputKeyCodes::RSXNeg, "RS X-" },
{ XInputKeyCodes::RSXPos, "RS X+" },
{ XInputKeyCodes::RSYPos, "RS Y+" },
{ XInputKeyCodes::RSYNeg, "RS Y-" }
};
init_configs();
// Define border values
@ -71,7 +68,15 @@ xinput_pad_handler::xinput_pad_handler() : PadHandlerBase(pad_handler::xinput)
xinput_pad_handler::~xinput_pad_handler()
{
Close();
if (library)
{
FreeLibrary(library);
library = nullptr;
xinputGetExtended = nullptr;
xinputGetState = nullptr;
xinputSetState = nullptr;
xinputGetBatteryInformation = nullptr;
}
}
void xinput_pad_handler::init_config(pad_config* cfg, const std::string& name)
@ -117,65 +122,6 @@ void xinput_pad_handler::init_config(pad_config* cfg, const std::string& name)
cfg->from_default();
}
void xinput_pad_handler::GetNextButtonPress(const std::string& padId, const std::function<void(u16, std::string, std::string, int[])>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist, const std::vector<std::string>& /*buttons*/)
{
if (get_blacklist)
blacklist.clear();
int device_number = GetDeviceNumber(padId);
if (device_number < 0)
return fail_callback(padId);
// Simply get the state of the controller from XInput.
const auto state = GetState(static_cast<u32>(device_number));
if (std::get<DWORD>(state) != ERROR_SUCCESS)
return fail_callback(padId);
// Check for each button in our list if its corresponding (maybe remapped) button or axis was pressed.
// Return the new value if the button was pressed (aka. its value was bigger than 0 or the defined threshold)
// Use a pair to get all the legally pressed buttons and use the one with highest value (prioritize first)
ASSERT(std::get<std::optional<PadButtonValues>>(state).has_value());
std::pair<u16, std::string> pressed_button = {0, ""};
const PadButtonValues& data = *std::get<std::optional<PadButtonValues>>(state);
for (const auto& button : button_list)
{
u32 keycode = button.first;
u16 value = data[keycode];
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), keycode) != blacklist.end())
continue;
if (((keycode < XInputKeyCodes::LT) && (value > 0))
|| ((keycode == XInputKeyCodes::LT) && (value > m_trigger_threshold))
|| ((keycode == XInputKeyCodes::RT) && (value > m_trigger_threshold))
|| ((keycode >= XInputKeyCodes::LSXNeg && keycode <= XInputKeyCodes::LSYPos) && (value > m_thumb_threshold))
|| ((keycode >= XInputKeyCodes::RSXNeg && keycode <= XInputKeyCodes::RSYPos) && (value > m_thumb_threshold)))
{
if (get_blacklist)
{
blacklist.emplace_back(keycode);
LOG_ERROR(HLE, "XInput Calibration: Added key [ %d = %s ] to blacklist. Value = %d", keycode, button.second, value);
}
else if (value > pressed_button.first)
pressed_button = { value, button.second };
}
}
if (get_blacklist)
{
if (blacklist.empty())
LOG_SUCCESS(HLE, "XInput Calibration: Blacklist is clear. No input spam detected");
return;
}
int preview_values[6] = { data[LT], data[RT], data[LSXPos] - data[LSXNeg], data[LSYPos] - data[LSYNeg], data[RSXPos] - data[RSXNeg], data[RSYPos] - data[RSYNeg] };
if (pressed_button.first > 0)
return callback(pressed_button.first, pressed_button.second, padId, preview_values);
else
return callback(0, "", padId, preview_values);
}
void xinput_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32/* r*/, s32/* g*/, s32/* b*/)
{
int device_number = GetDeviceNumber(padId);
@ -192,42 +138,6 @@ void xinput_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u3
(*xinputSetState)(static_cast<u32>(device_number), &vibrate);
}
void xinput_pad_handler::TranslateButtonPress(u64 keyCode, bool& pressed, u16& val, bool ignore_threshold)
{
// Update the pad button values based on their type and thresholds.
// With this you can use axis or triggers as buttons or vice versa
auto p_profile = m_dev->config;
switch (keyCode)
{
case XInputKeyCodes::LT:
pressed = val > p_profile->ltriggerthreshold;
val = pressed ? NormalizeTriggerInput(val, p_profile->ltriggerthreshold) : 0;
break;
case XInputKeyCodes::RT:
pressed = val > p_profile->rtriggerthreshold;
val = pressed ? NormalizeTriggerInput(val, p_profile->rtriggerthreshold) : 0;
break;
case XInputKeyCodes::LSXNeg:
case XInputKeyCodes::LSXPos:
case XInputKeyCodes::LSYPos:
case XInputKeyCodes::LSYNeg:
pressed = val > (ignore_threshold ? 0 : p_profile->lstickdeadzone);
val = pressed ? NormalizeStickInput(val, p_profile->lstickdeadzone, p_profile->lstickmultiplier, ignore_threshold) : 0;
break;
case XInputKeyCodes::RSXNeg:
case XInputKeyCodes::RSXPos:
case XInputKeyCodes::RSYPos:
case XInputKeyCodes::RSYNeg:
pressed = val > (ignore_threshold ? 0 : p_profile->rstickdeadzone);
val = pressed ? NormalizeStickInput(val, p_profile->rstickdeadzone, p_profile->rstickmultiplier, ignore_threshold) : 0;
break;
default: // normal button (should in theory also support sensitive buttons)
pressed = val > 0;
val = pressed ? val : 0;
break;
}
}
int xinput_pad_handler::GetDeviceNumber(const std::string& padId)
{
if (!Init())
@ -244,9 +154,25 @@ int xinput_pad_handler::GetDeviceNumber(const std::string& padId)
return device_number;
}
std::array<u16, xinput_pad_handler::XInputKeyCodes::KeyCodeCount> xinput_pad_handler::GetButtonValues_Base(const XINPUT_STATE& state)
std::unordered_map<u64, u16> xinput_pad_handler::get_button_values(const std::shared_ptr<PadDevice>& device)
{
std::array<u16, xinput_pad_handler::XInputKeyCodes::KeyCodeCount> values;
PadButtonValues values;
auto dev = std::static_pointer_cast<XInputDevice>(device);
if (!dev || dev->state != ERROR_SUCCESS) // the state has to be aquired with update_connection before calling this function
return values;
// Try SCP first, if it fails for that pad then try normal XInput
if (dev->is_scp_device)
{
return get_button_values_scp(dev->state_scp);
}
return get_button_values_base(dev->state_base);
}
xinput_pad_handler::PadButtonValues xinput_pad_handler::get_button_values_base(const XINPUT_STATE& state)
{
PadButtonValues values;
// Triggers
values[XInputKeyCodes::LT] = state.Gamepad.bLeftTrigger;
@ -303,9 +229,9 @@ std::array<u16, xinput_pad_handler::XInputKeyCodes::KeyCodeCount> xinput_pad_han
return values;
}
std::array<u16, xinput_pad_handler::XInputKeyCodes::KeyCodeCount> xinput_pad_handler::GetButtonValues_SCP(const SCP_EXTN& state)
xinput_pad_handler::PadButtonValues xinput_pad_handler::get_button_values_scp(const SCP_EXTN& state)
{
std::array<u16, xinput_pad_handler::XInputKeyCodes::KeyCodeCount> values;
PadButtonValues values;
// Triggers
values[xinput_pad_handler::XInputKeyCodes::LT] = static_cast<u16>(state.SCP_L2 * 255.0f);
@ -359,33 +285,9 @@ std::array<u16, xinput_pad_handler::XInputKeyCodes::KeyCodeCount> xinput_pad_han
return values;
}
auto xinput_pad_handler::GetState(u32 device_number) -> std::tuple<DWORD, std::optional<PadButtonValues>>
std::array<int, 6> xinput_pad_handler::get_preview_values(std::unordered_map<u64, u16> data)
{
std::tuple<DWORD, std::optional<PadButtonValues>> result;
std::get<DWORD>(result) = ERROR_NOT_CONNECTED;
// Try SCP first, if it fails for that pad then try normal XInput
if (xinputGetExtended)
{
SCP_EXTN stateExtn;
std::get<DWORD>(result) = xinputGetExtended(device_number, &stateExtn);
if (std::get<DWORD>(result) == ERROR_SUCCESS)
{
std::get<std::optional<PadButtonValues>>(result) = GetButtonValues_SCP(stateExtn);
}
}
if (std::get<DWORD>(result) != ERROR_SUCCESS)
{
XINPUT_STATE stateBase;
std::get<DWORD>(result) = xinputGetState(device_number, &stateBase);
if (std::get<DWORD>(result) == ERROR_SUCCESS)
{
std::get<std::optional<PadButtonValues>>(result) = GetButtonValues_Base(stateBase);
}
}
return result;
return { data[LT], data[RT], data[LSXPos] - data[LSXNeg], data[LSYPos] - data[LSYNeg], data[RSXPos] - data[RSXNeg], data[RSYPos] - data[RSYNeg] };
}
bool xinput_pad_handler::Init()
@ -427,154 +329,6 @@ bool xinput_pad_handler::Init()
return true;
}
void xinput_pad_handler::Close()
{
if (library)
{
FreeLibrary(library);
library = nullptr;
xinputGetExtended = nullptr;
xinputGetState = nullptr;
xinputSetState = nullptr;
xinputGetBatteryInformation = nullptr;
}
}
void xinput_pad_handler::ThreadProc()
{
for (int i = 0; i < static_cast<int>(bindings.size()); ++i)
{
auto& bind = bindings[i];
m_dev = bind.first;
auto padnum = m_dev->deviceNumber;
auto profile = m_dev->config;
auto pad = bind.second;
const auto state = GetState(padnum);
switch (std::get<DWORD>(state))
{
case ERROR_DEVICE_NOT_CONNECTED:
{
if (last_connection_status[i] == 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[i] = false;
connected--;
}
continue;
}
case ERROR_SUCCESS:
{
if (last_connection_status[i] == 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[i] = true;
connected++;
}
ASSERT(std::get<std::optional<PadButtonValues>>(state).has_value());
const PadButtonValues& button_values = *std::get<std::optional<PadButtonValues>>(state);
// 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);
}
for (const auto& btn : pad->m_buttons)
{
if (btn.m_pressed)
{
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
break;
}
}
// used to get the absolute value of an axis
s32 stick_val[4]{0};
// 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;
}
u16 lx, ly, rx, ry;
// Normalize our two stick's axis based on the thresholds
std::tie(lx, ly) = NormalizeStickDeadzone(stick_val[0], stick_val[1], profile->lstickdeadzone);
std::tie(rx, ry) = NormalizeStickDeadzone(stick_val[2], stick_val[3], profile->rstickdeadzone);
if (profile->padsquircling != 0)
{
std::tie(lx, ly) = ConvertToSquirclePoint(lx, ly, profile->padsquircling);
std::tie(rx, ry) = ConvertToSquirclePoint(rx, ry, profile->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;
// Receive Battery Info. If device is not on cable, get battery level, else assume full
XINPUT_BATTERY_INFORMATION battery_info;
(*xinputGetBatteryInformation)(padnum, BATTERY_DEVTYPE_GAMEPAD, &battery_info);
pad->m_cable_state = battery_info.BatteryType == BATTERY_TYPE_WIRED ? 1 : 0;
pad->m_battery_level = pad->m_cable_state ? BATTERY_LEVEL_FULL : battery_info.BatteryLevel;
// The left motor is the low-frequency rumble motor. The right motor is the high-frequency rumble motor.
// The two motors are not the same, and they create different vibration effects. Values range between 0 to 65535.
size_t idx_l = profile->switch_vibration_motors ? 1 : 0;
size_t idx_s = profile->switch_vibration_motors ? 0 : 1;
u16 speed_large = profile->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value : static_cast<u16>(vibration_min);
u16 speed_small = profile->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value : static_cast<u16>(vibration_min);
m_dev->newVibrateData |= m_dev->largeVibrate != speed_large || m_dev->smallVibrate != speed_small;
m_dev->largeVibrate = speed_large;
m_dev->smallVibrate = speed_small;
// XBox One Controller can't handle faster vibration updates than ~10ms. Elite is even worse. So I'll use 20ms to be on the safe side. No lag was noticable.
if (m_dev->newVibrateData && (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - m_dev->last_vibration) > 20ms))
{
XINPUT_VIBRATION vibrate;
vibrate.wLeftMotorSpeed = speed_large * 257;
vibrate.wRightMotorSpeed = speed_small * 257;
if ((*xinputSetState)(padnum, &vibrate) == ERROR_SUCCESS)
{
m_dev->newVibrateData = false;
m_dev->last_vibration = std::chrono::high_resolution_clock::now();
}
}
break;
}
default:
break;
}
}
}
std::vector<std::string> xinput_pad_handler::ListDevices()
{
std::vector<std::string> xinput_pads_list;
@ -584,74 +338,153 @@ std::vector<std::string> xinput_pad_handler::ListDevices()
for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
{
XINPUT_STATE state;
DWORD result = (*xinputGetState)(i, &state);
DWORD result = ERROR_NOT_CONNECTED;
// Try SCP first, if it fails for that pad then try normal XInput
if (xinputGetExtended)
{
SCP_EXTN state;
result = xinputGetExtended(i, &state);
}
if (result != ERROR_SUCCESS)
{
XINPUT_STATE state;
result = xinputGetState(i, &state);
}
if (result == ERROR_SUCCESS)
xinput_pads_list.push_back(m_name_string + std::to_string(i + 1)); // Controllers 1-n in GUI
}
return xinput_pads_list;
}
bool xinput_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device)
std::shared_ptr<PadDevice> xinput_pad_handler::get_device(const std::string& device)
{
//Convert device string to u32 representing xinput device number
// Convert device string to u32 representing xinput device number
int device_number = GetDeviceNumber(device);
if (device_number < 0)
return false;
return nullptr;
std::shared_ptr<XInputDevice> x_device = std::make_shared<XInputDevice>();
x_device->deviceNumber = static_cast<u32>(device_number);
int index = static_cast<int>(bindings.size());
m_pad_configs[index].load();
x_device->config = &m_pad_configs[index];
pad_config* p_profile = x_device->config;
if (p_profile == nullptr)
return x_device;
}
bool xinput_pad_handler::get_is_left_trigger(u64 keyCode)
{
return keyCode == XInputKeyCodes::LT;
}
bool xinput_pad_handler::get_is_right_trigger(u64 keyCode)
{
return keyCode == XInputKeyCodes::RT;
}
bool xinput_pad_handler::get_is_left_stick(u64 keyCode)
{
switch (keyCode)
{
case XInputKeyCodes::LSXNeg:
case XInputKeyCodes::LSXPos:
case XInputKeyCodes::LSYPos:
case XInputKeyCodes::LSYNeg:
return true;
default:
return false;
}
}
pad->Init
(
CELL_PAD_STATUS_DISCONNECTED,
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,
p_profile->device_class_type
);
bool xinput_pad_handler::get_is_right_stick(u64 keyCode)
{
switch (keyCode)
{
case XInputKeyCodes::RSXNeg:
case XInputKeyCodes::RSXPos:
case XInputKeyCodes::RSYPos:
case XInputKeyCodes::RSYNeg:
return true;
default:
return false;
}
}
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->up), CELL_PAD_CTRL_UP);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->down), CELL_PAD_CTRL_DOWN);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->left), CELL_PAD_CTRL_LEFT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->right), CELL_PAD_CTRL_RIGHT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->start), CELL_PAD_CTRL_START);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->select), CELL_PAD_CTRL_SELECT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->l3), CELL_PAD_CTRL_L3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, FindKeyCode(button_list, p_profile->r3), CELL_PAD_CTRL_R3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->l1), CELL_PAD_CTRL_L1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->r1), CELL_PAD_CTRL_R1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->ps), 0x100/*CELL_PAD_CTRL_PS*/);// TODO: PS button support
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->cross), CELL_PAD_CTRL_CROSS);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->circle), CELL_PAD_CTRL_CIRCLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->square), CELL_PAD_CTRL_SQUARE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->triangle), CELL_PAD_CTRL_TRIANGLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->l2), CELL_PAD_CTRL_L2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, FindKeyCode(button_list, p_profile->r2), CELL_PAD_CTRL_R2);
//pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x0); // Reserved (and currently not in use by rpcs3 at all)
PadHandlerBase::connection xinput_pad_handler::update_connection(const std::shared_ptr<PadDevice>& device)
{
auto dev = std::static_pointer_cast<XInputDevice>(device);
if (!dev)
return connection::disconnected;
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X, FindKeyCode(button_list, p_profile->ls_left), FindKeyCode(button_list, p_profile->ls_right));
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y, FindKeyCode(button_list, p_profile->ls_down), FindKeyCode(button_list, p_profile->ls_up));
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X, FindKeyCode(button_list, p_profile->rs_left), FindKeyCode(button_list, p_profile->rs_right));
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y, FindKeyCode(button_list, p_profile->rs_down), FindKeyCode(button_list, p_profile->rs_up));
dev->state = ERROR_NOT_CONNECTED;
dev->state_scp = {};
dev->state_base = {};
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);
// Try SCP first, if it fails for that pad then try normal XInput
if (xinputGetExtended)
dev->state = xinputGetExtended(dev->deviceNumber, &dev->state_scp);
pad->m_vibrateMotors.emplace_back(true, 0);
pad->m_vibrateMotors.emplace_back(false, 0);
dev->is_scp_device = dev->state == ERROR_SUCCESS;
bindings.emplace_back(x_device, pad);
if (!dev->is_scp_device)
dev->state = xinputGetState(dev->deviceNumber, &dev->state_base);
return true;
if (dev->state == ERROR_SUCCESS)
return connection::connected;
return connection::disconnected;
}
void xinput_pad_handler::get_extended_info(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad)
{
auto dev = std::static_pointer_cast<XInputDevice>(device);
if (!dev || !pad)
return;
auto padnum = dev->deviceNumber;
// Receive Battery Info. If device is not on cable, get battery level, else assume full
XINPUT_BATTERY_INFORMATION battery_info;
(*xinputGetBatteryInformation)(padnum, BATTERY_DEVTYPE_GAMEPAD, &battery_info);
pad->m_cable_state = battery_info.BatteryType == BATTERY_TYPE_WIRED ? 1 : 0;
pad->m_battery_level = pad->m_cable_state ? BATTERY_LEVEL_FULL : battery_info.BatteryLevel;
}
void xinput_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad)
{
auto dev = std::static_pointer_cast<XInputDevice>(device);
if (!dev || !pad)
return;
auto padnum = dev->deviceNumber;
auto profile = dev->config;
// The left motor is the low-frequency rumble motor. The right motor is the high-frequency rumble motor.
// The two motors are not the same, and they create different vibration effects. Values range between 0 to 65535.
size_t idx_l = profile->switch_vibration_motors ? 1 : 0;
size_t idx_s = profile->switch_vibration_motors ? 0 : 1;
u16 speed_large = profile->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value : static_cast<u16>(vibration_min);
u16 speed_small = profile->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value : static_cast<u16>(vibration_min);
dev->newVibrateData |= dev->largeVibrate != speed_large || dev->smallVibrate != speed_small;
dev->largeVibrate = speed_large;
dev->smallVibrate = speed_small;
// XBox One Controller can't handle faster vibration updates than ~10ms. Elite is even worse. So I'll use 20ms to be on the safe side. No lag was noticable.
if (dev->newVibrateData && (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - dev->last_vibration) > 20ms))
{
XINPUT_VIBRATION vibrate;
vibrate.wLeftMotorSpeed = speed_large * 257;
vibrate.wRightMotorSpeed = speed_small * 257;
if ((*xinputSetState)(padnum, &vibrate) == ERROR_SUCCESS)
{
dev->newVibrateData = false;
dev->last_vibration = std::chrono::high_resolution_clock::now();
}
}
}
#endif