input: fix player id if handler has 2 or more pads

This commit is contained in:
Megamouse 2021-08-09 23:41:49 +02:00
parent 6e1c7a2c16
commit 5816505e61
18 changed files with 41 additions and 40 deletions

View file

@ -62,7 +62,7 @@ public:
return nulllist;
}
bool bindPadToDevice(std::shared_ptr<Pad> /*pad*/, const std::string& /*device*/) override
bool bindPadToDevice(std::shared_ptr<Pad> /*pad*/, const std::string& /*device*/, u8 /*player_id*/) override
{
return true;
}

View file

@ -451,7 +451,7 @@ void PadHandlerBase::TranslateButtonPress(const std::shared_ptr<PadDevice>& devi
}
}
bool PadHandlerBase::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device)
bool PadHandlerBase::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device, u8 player_id)
{
std::shared_ptr<PadDevice> pad_device = get_device(device);
if (!pad_device)
@ -463,6 +463,7 @@ bool PadHandlerBase::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string
const int index = static_cast<int>(bindings.size());
m_pad_configs[index].load();
pad_device->config = &m_pad_configs[index];
pad_device->player_id = player_id;
pad_config* profile = pad_device->config;
if (profile == nullptr)
{

View file

@ -16,6 +16,7 @@ class PadDevice
{
public:
pad_config* config{ nullptr };
u8 player_id{0};
};
using pad_preview_values = std::array<int, 6>;
@ -71,7 +72,6 @@ protected:
std::array<bool, MAX_GAMEPADS> last_connection_status{{ false, false, false, false, false, false, false }};
std::string m_name_string;
u32 m_player_id = 0;
usz m_max_devices = 0;
int m_trigger_threshold = 0;
int m_thumb_threshold = 0;
@ -153,8 +153,6 @@ public:
bool has_battery() const;
bool has_pressure_intensity_button() const;
void set_player(u32 player_id) { m_player_id = player_id; }
static std::string get_config_dir(pad_handler type, const std::string& title_id = "");
static std::string get_config_filename(int i, const std::string& title_id = "");
@ -165,14 +163,14 @@ public:
PadHandlerBase(pad_handler type = pad_handler::null);
virtual ~PadHandlerBase() = default;
// Sets window to config the controller(optional)
virtual void SetPadData(const std::string& /*padId*/, u32 /*largeMotor*/, u32 /*smallMotor*/, s32 /*r*/, s32 /*g*/, s32 /*b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/) {}
virtual void SetPadData(const std::string& /*padId*/, u8 /*player_id*/, u32 /*largeMotor*/, u32 /*smallMotor*/, s32 /*r*/, s32 /*g*/, s32 /*b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/) {}
virtual u32 get_battery_level(const std::string& /*padId*/) { return 0; }
// Return list of devices for that handler
virtual std::vector<std::string> ListDevices() = 0;
// Callback called during pad_thread::ThreadFunc
virtual void ThreadProc();
// Binds a Pad to a device
virtual bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device);
virtual bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device, u8 player_id);
virtual void init_config(pad_config* /*cfg*/, const std::string& /*name*/) = 0;
virtual void get_next_button_press(const std::string& padId, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons = {});

View file

@ -119,7 +119,7 @@ u32 ds3_pad_handler::get_battery_level(const std::string& padId)
return std::clamp<u32>(device->battery_level, 0, 100);
}
void ds3_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32/* r*/, s32/* g*/, s32 /* b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/)
void ds3_pad_handler::SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32/* r*/, s32/* g*/, s32 /* b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/)
{
std::shared_ptr<ds3_device> device = get_hid_device(padId);
if (device == nullptr || device->hidDevice == nullptr)
@ -128,6 +128,7 @@ void ds3_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 s
// Set the device's motor speeds to our requested values 0-255
device->large_motor = largeMotor;
device->small_motor = smallMotor;
device->player_id = player_id;
int index = 0;
for (uint i = 0; i < MAX_GAMEPADS; i++)
@ -172,7 +173,7 @@ int ds3_pad_handler::send_output_report(ds3_device* ds3dev)
}
else
{
switch (m_player_id)
switch (ds3dev->player_id)
{
case 0: output_report.led_enabled = 0b00000010; break;
case 1: output_report.led_enabled = 0b00000100; break;
@ -182,7 +183,7 @@ int ds3_pad_handler::send_output_report(ds3_device* ds3dev)
case 5: output_report.led_enabled = 0b00010100; break;
case 6: output_report.led_enabled = 0b00011000; break;
default:
fmt::throw_exception("DS3 is using forbidden player id %d", m_player_id);
fmt::throw_exception("DS3 is using forbidden player id %d", ds3dev->player_id);
}
}

View file

@ -80,7 +80,7 @@ public:
ds3_pad_handler();
~ds3_pad_handler();
void SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
void SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
u32 get_battery_level(const std::string& padId) override;
void init_config(pad_config* cfg, const std::string& name) override;

View file

@ -197,7 +197,7 @@ u32 ds4_pad_handler::get_battery_level(const std::string& padId)
return std::min<u32>(device->battery_level * 10, 100);
}
void ds4_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness)
void ds4_pad_handler::SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness)
{
std::shared_ptr<DS4Device> device = get_hid_device(padId);
if (!device || !device->hidDevice || !device->config)
@ -206,6 +206,7 @@ void ds4_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 s
// Set the device's motor speeds to our requested values 0-255
device->large_motor = largeMotor;
device->small_motor = smallMotor;
device->player_id = player_id;
int index = 0;
for (uint i = 0; i < MAX_GAMEPADS; i++)

View file

@ -56,7 +56,7 @@ public:
ds4_pad_handler();
~ds4_pad_handler();
void SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
void SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
u32 get_battery_level(const std::string& padId) override;
void init_config(pad_config* cfg, const std::string& name) override;

View file

@ -955,7 +955,7 @@ int dualsense_pad_handler::send_output_report(DualSenseDevice* device)
// Use OR with 0x1, 0x2, 0x4, 0x8 and 0x10 to enable the LEDs (from leftmost to rightmost).
common.valid_flag_1 |= VALID_FLAG_1_PLAYER_INDICATOR_CONTROL_ENABLE;
switch (m_player_id)
switch (device->player_id)
{
case 0: common.player_leds = 0b00100; break;
case 1: common.player_leds = 0b01010; break;
@ -965,7 +965,7 @@ int dualsense_pad_handler::send_output_report(DualSenseDevice* device)
case 5: common.player_leds = 0b10111; break;
case 6: common.player_leds = 0b11101; break;
default:
fmt::throw_exception("Dualsense is using forbidden player id %d", m_player_id);
fmt::throw_exception("Dualsense is using forbidden player id %d", device->player_id);
}
}
}
@ -1086,7 +1086,7 @@ void dualsense_pad_handler::apply_pad_data(const std::shared_ptr<PadDevice>& dev
}
}
void dualsense_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness)
void dualsense_pad_handler::SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness)
{
std::shared_ptr<DualSenseDevice> device = get_hid_device(padId);
if (device == nullptr || device->hidDevice == nullptr)
@ -1095,6 +1095,7 @@ void dualsense_pad_handler::SetPadData(const std::string& padId, u32 largeMotor,
// Set the device's motor speeds to our requested values 0-255
device->large_motor = largeMotor;
device->small_motor = smallMotor;
device->player_id = player_id;
int index = 0;
for (uint i = 0; i < MAX_GAMEPADS; i++)

View file

@ -69,7 +69,7 @@ public:
dualsense_pad_handler();
~dualsense_pad_handler();
void SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
void SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
u32 get_battery_level(const std::string& padId) override;
void init_config(pad_config* cfg, const std::string& name) override;

View file

@ -509,7 +509,7 @@ void evdev_joystick_handler::SetRumble(EvdevDevice* device, u16 large, u16 small
device->force_small = small;
}
void evdev_joystick_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 /* r*/, s32 /* g*/, s32 /* b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/)
void evdev_joystick_handler::SetPadData(const std::string& padId, u8 /*player_id*/, u32 largeMotor, u32 smallMotor, s32 /* r*/, s32 /* g*/, s32 /* b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/)
{
// Get our evdev device
auto dev = get_evdev_device(padId);
@ -893,7 +893,7 @@ int evdev_joystick_handler::FindAxisDirection(const std::unordered_map<int, bool
return -1;
}
bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device)
bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device, u8 player_id)
{
if (!pad)
return false;
@ -904,7 +904,8 @@ bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std
const int index = static_cast<int>(bindings.size());
m_pad_configs[index].load();
m_dev->config = &m_pad_configs[index];
m_dev->config = &m_pad_configs[index];
m_dev->player_id = player_id;
pad_config* p_profile = m_dev->config;
if (p_profile == nullptr)
return false;

View file

@ -362,10 +362,10 @@ public:
void init_config(pad_config* cfg, const std::string& name) override;
bool Init() override;
std::vector<std::string> ListDevices() override;
bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device) override;
bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device, u8 player_id) override;
void Close();
void get_next_button_press(const std::string& padId, const pad_callback& callback, const pad_fail_callback& fail_callback, bool get_blacklist = false, const std::vector<std::string>& buttons = {}) override;
void SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
void SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
private:
std::shared_ptr<EvdevDevice> get_evdev_device(const std::string& device);

View file

@ -690,7 +690,7 @@ std::string keyboard_pad_handler::native_scan_code_to_string(int native_scan_cod
}
}
bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device)
bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device, u8 /*player_id*/)
{
if (device != pad::keyboard_device_name)
return false;

View file

@ -85,7 +85,7 @@ public:
void init_config(pad_config* cfg, const std::string& name) override;
std::vector<std::string> ListDevices() override;
void get_next_button_press(const std::string& /*padId*/, const pad_callback& /*callback*/, const pad_fail_callback& /*fail_callback*/, bool /*get_blacklist*/ = false, const std::vector<std::string>& /*buttons*/ = {}) override {}
bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device) override;
bool bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device, u8 player_id) override;
void ThreadProc() override;
std::string GetMouseName(const QMouseEvent* event) const;

View file

@ -145,7 +145,6 @@ void pad_thread::Init()
}
handlers.emplace(handler_type, cur_pad_handler);
}
cur_pad_handler->set_player(i);
cur_pad_handler->Init();
m_pads[i] = std::make_shared<Pad>(CELL_PAD_STATUS_DISCONNECTED, pad_settings[i].device_capability, pad_settings[i].device_type);
@ -154,11 +153,11 @@ void pad_thread::Init()
{
InitLddPad(pad_settings[i].ldd_handle);
}
else if (cur_pad_handler->bindPadToDevice(m_pads[i], g_cfg_input.player[i]->device.to_string()) == false)
else if (!cur_pad_handler->bindPadToDevice(m_pads[i], g_cfg_input.player[i]->device.to_string(), i))
{
// Failed to bind the device to cur_pad_handler so binds to NullPadHandler
input_log.error("Failed to bind device %s to handler %s", g_cfg_input.player[i]->device.to_string(), handler_type);
nullpad->bindPadToDevice(m_pads[i], g_cfg_input.player[i]->device.to_string());
nullpad->bindPadToDevice(m_pads[i], g_cfg_input.player[i]->device.to_string(), i);
}
m_pads_interface[i] = std::make_shared<Pad>(CELL_PAD_STATUS_DISCONNECTED, pad_settings[i].device_capability, pad_settings[i].device_type);

View file

@ -131,7 +131,7 @@ void xinput_pad_handler::init_config(pad_config* cfg, const std::string& name)
cfg->from_default();
}
void xinput_pad_handler::SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32/* r*/, s32/* g*/, s32/* b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/)
void xinput_pad_handler::SetPadData(const std::string& padId, u8 /*player_id*/, u32 largeMotor, u32 smallMotor, s32/* r*/, s32/* g*/, s32/* b*/, bool /*battery_led*/, u32 /*battery_led_brightness*/)
{
const int device_number = GetDeviceNumber(padId);
if (device_number < 0)

View file

@ -110,7 +110,7 @@ public:
bool Init() override;
std::vector<std::string> ListDevices() override;
void SetPadData(const std::string& padId, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
void SetPadData(const std::string& padId, u8 player_id, u32 largeMotor, u32 smallMotor, s32 r, s32 g, s32 b, bool battery_led, u32 battery_led_brightness) override;
u32 get_battery_level(const std::string& padId) override;
void init_config(pad_config* cfg, const std::string& name) override;

View file

@ -104,9 +104,8 @@ pad_settings_dialog::pad_settings_dialog(std::shared_ptr<gui_settings> gui_setti
connect(ui->chooseDevice, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index)
{
if (index < 0)
{
return;
}
const pad_device_info info = ui->chooseDevice->itemData(index).value<pad_device_info>();
m_device_name = info.name;
if (!g_cfg_input.player[ui->tabWidget->currentIndex()]->device.from_string(m_device_name))
@ -379,11 +378,11 @@ void pad_settings_dialog::InitButtons()
{
// Allow LED battery indication while the dialog is open
ensure(m_handler);
m_handler->SetPadData(m_device_name, 0, 0, m_handler_cfg.colorR, m_handler_cfg.colorG, m_handler_cfg.colorB, m_handler_cfg.led_battery_indicator.get(), m_handler_cfg.led_battery_indicator_brightness);
SetPadData(0, 0, m_handler_cfg.led_battery_indicator.get());
pad_led_settings_dialog dialog(this, m_handler_cfg.colorR, m_handler_cfg.colorG, m_handler_cfg.colorB, m_handler->has_rgb(), m_handler->has_battery(), m_handler_cfg.led_low_battery_blink.get(), m_handler_cfg.led_battery_indicator.get(), m_handler_cfg.led_battery_indicator_brightness);
connect(&dialog, &pad_led_settings_dialog::pass_led_settings, this, &pad_settings_dialog::apply_led_settings);
dialog.exec();
m_handler->SetPadData(m_device_name, 0, 0, m_handler_cfg.colorR, m_handler_cfg.colorG, m_handler_cfg.colorB, false, m_handler_cfg.led_battery_indicator_brightness);
SetPadData(0, 0);
});
// Enable Button Remapping
@ -495,24 +494,25 @@ void pad_settings_dialog::InitButtons()
});
}
void pad_settings_dialog::SetPadData(u32 large_motor, u32 small_motor)
void pad_settings_dialog::SetPadData(u32 large_motor, u32 small_motor, bool led_battery_indicator)
{
ensure(m_handler);
const QColor led_color(m_handler_cfg.colorR, m_handler_cfg.colorG, m_handler_cfg.colorB);
m_handler->SetPadData(m_device_name, large_motor, small_motor, led_color.red(), led_color.green(), led_color.blue(), static_cast<bool>(m_handler_cfg.led_battery_indicator), m_handler_cfg.led_battery_indicator_brightness);
const int player_id = ui->tabWidget->currentIndex();
m_handler->SetPadData(m_device_name, player_id, large_motor, small_motor, m_handler_cfg.colorR, m_handler_cfg.colorG, m_handler_cfg.colorB, led_battery_indicator, m_handler_cfg.led_battery_indicator_brightness);
}
// Slot to handle the data from a signal in the led settings dialog
void pad_settings_dialog::apply_led_settings(int colorR, int colorG, int colorB, bool led_low_battery_blink, bool led_battery_indicator, int led_battery_indicator_brightness)
{
ensure(m_handler);
const int player_id = ui->tabWidget->currentIndex();
m_handler_cfg.colorR.set(colorR);
m_handler_cfg.colorG.set(colorG);
m_handler_cfg.colorB.set(colorB);
m_handler_cfg.led_battery_indicator.set(led_battery_indicator);
m_handler_cfg.led_battery_indicator_brightness.set(led_battery_indicator_brightness);
m_handler_cfg.led_low_battery_blink.set(led_low_battery_blink);
m_handler->SetPadData(m_device_name, 0, 0, colorR, colorG, colorB, led_battery_indicator, led_battery_indicator_brightness);
SetPadData(0, 0, led_battery_indicator);
}
void pad_settings_dialog::SwitchPadInfo(const std::string& pad_name, bool is_connected)
@ -1033,7 +1033,7 @@ void pad_settings_dialog::UpdateLabels(bool is_reset)
// Apply stored/default LED settings to the device
m_enable_led = m_handler->has_led();
m_handler->SetPadData(m_device_name, 0, 0, m_handler_cfg.colorR, m_handler_cfg.colorG, m_handler_cfg.colorB, false, m_handler_cfg.led_battery_indicator_brightness);
SetPadData(0, 0);
// Enable battery and LED group box
m_enable_battery = m_handler->has_battery();
@ -1206,7 +1206,6 @@ void pad_settings_dialog::ChangeInputType()
// Get this player's current handler and it's currently available devices
m_handler = GetHandler(g_cfg_input.player[player]->handler);
ensure(m_handler);
m_handler->set_player(player);
const auto device_list = m_handler->ListDevices();
// Localized tooltips

View file

@ -161,7 +161,7 @@ private:
void CancelExit();
// Set vibrate data while keeping the current color
void SetPadData(u32 large_motor, u32 small_motor);
void SetPadData(u32 large_motor, u32 small_motor, bool led_battery_indicator = false);
/** Update all the Button Labels with current button mapping */
void UpdateLabels(bool is_reset = false);