USB: Change SubTypes() to use a span

This commit is contained in:
Connor McLaughlin 2022-12-19 20:45:02 +10:00 committed by refractionpcsx2
parent 9837dc88a0
commit c477ea1628
14 changed files with 81 additions and 52 deletions

View File

@ -875,7 +875,6 @@ QIcon USBDeviceWidget::getIcon() const
void USBDeviceWidget::populateDeviceTypes()
{
m_ui.deviceType->addItem(qApp->translate("USB", USB::GetDeviceName("None")), QStringLiteral("None"));
for (const auto& [name, display_name] : USB::GetDeviceTypes())
m_ui.deviceType->addItem(QString::fromStdString(display_name), QString::fromStdString(name));
}
@ -888,8 +887,8 @@ void USBDeviceWidget::populatePages()
{
QSignalBlocker sb(m_ui.deviceSubtype);
m_ui.deviceSubtype->clear();
for (const std::string& subtype : USB::GetDeviceSubtypes(m_device_type))
m_ui.deviceSubtype->addItem(qApp->translate("USB", subtype.c_str()));
for (const char* subtype : USB::GetDeviceSubtypes(m_device_type))
m_ui.deviceSubtype->addItem(qApp->translate("USB", subtype));
m_ui.deviceSubtype->setCurrentIndex(m_device_subtype);
m_ui.deviceSubtype->setVisible(m_ui.deviceSubtype->count() > 0);
}
@ -1085,7 +1084,7 @@ QIcon USBBindingWidget::getIcon() const
std::string USBBindingWidget::getBindingKey(const char* binding_name) const
{
return USB::GetConfigBindKey(getDeviceType(), binding_name);
return USB::GetConfigSubKey(getDeviceType(), binding_name);
}
void USBBindingWidget::createWidgets(gsl::span<const InputBindingInfo> bindings)

View File

@ -701,11 +701,11 @@ void InputManager::AddUSBBindings(SettingsInterface& si, u32 port)
if (device.empty() || device == "None")
return;
const std::string section(USBGetConfigSection(port));
const std::string section(USB::GetConfigSection(port));
const u32 subtype = USB::GetConfigSubType(si, port, device);
for (const InputBindingInfo& bi : USB::GetDeviceBindings(device, subtype))
{
const std::string bind_name(USB::GetConfigBindKey(device, bi.name));
const std::string bind_name(USB::GetConfigSubKey(device, bi.name));
switch (bi.bind_type)
{

View File

@ -1110,7 +1110,7 @@ void Pcsx2Config::USBOptions::LoadSave(SettingsWrapper& wrap)
{
for (u32 i = 0; i < static_cast<u32>(Ports.size()); i++)
{
const std::string section(USBGetConfigSection(i));
const std::string section(USB::GetConfigSection(i));
std::string device = USB::DeviceTypeIndexToName(Ports[i].DeviceType);
wrap.Entry(section.c_str(), "Type", device, device);
@ -1118,8 +1118,11 @@ void Pcsx2Config::USBOptions::LoadSave(SettingsWrapper& wrap)
if (wrap.IsLoading())
Ports[i].DeviceType = USB::DeviceTypeNameToIndex(device);
const std::string subtype_key(fmt::format("{}_subtype", USB::DeviceTypeIndexToName(Ports[i].DeviceType)));
wrap.Entry(section.c_str(), subtype_key.c_str(), Ports[i].DeviceSubtype);
if (Ports[i].DeviceType >= 0)
{
const std::string subtype_key(fmt::format("{}_subtype", USB::DeviceTypeIndexToName(Ports[i].DeviceType)));
wrap.Entry(section.c_str(), subtype_key.c_str(), Ports[i].DeviceSubtype);
}
}
}

View File

@ -58,7 +58,7 @@ int64_t g_usb_frame_time = 0;
int64_t g_usb_bit_time = 0;
int64_t g_usb_last_cycle = 0;
std::string USBGetConfigSection(int port)
std::string USB::GetConfigSection(int port)
{
return fmt::format("USB{}", port + 1);
}
@ -569,7 +569,8 @@ std::vector<std::pair<std::string, std::string>> USB::GetDeviceTypes()
{
RegisterDevice& rd = RegisterDevice::instance();
std::vector<std::pair<std::string, std::string>> ret;
ret.reserve(rd.Map().size());
ret.reserve(rd.Map().size() + 1);
ret.emplace_back("None", "Not Connected");
for (const auto& it : rd.Map())
ret.emplace_back(it.second->TypeName(), it.second->Name());
return ret;
@ -581,10 +582,23 @@ const char* USB::GetDeviceName(const std::string_view& device)
return dev ? dev->Name() : "Not Connected";
}
std::vector<std::string> USB::GetDeviceSubtypes(const std::string_view& device)
const char* USB::GetDeviceSubtypeName(const std::string_view& device, u32 subtype)
{
const DeviceProxy* dev = RegisterDevice::instance().Device(device);
return dev ? dev->SubTypes() : std::vector<std::string>();
if (!dev)
return "Unknown";
const gsl::span<const char*> subtypes(dev->SubTypes());
if (subtypes.empty() || subtype >= subtypes.size())
return "";
return subtypes[subtype];
}
gsl::span<const char*> USB::GetDeviceSubtypes(const std::string_view& device)
{
const DeviceProxy* dev = RegisterDevice::instance().Device(device);
return dev ? dev->SubTypes() : gsl::span<const char*>();
}
gsl::span<const InputBindingInfo> USB::GetDeviceBindings(const std::string_view& device, u32 subtype)
@ -646,15 +660,25 @@ void USB::InputDeviceDisconnected(const std::string_view& identifier)
std::string USB::GetConfigDevice(const SettingsInterface& si, u32 port)
{
return si.GetStringValue(USBGetConfigSection(port).c_str(), "Type", "None");
return si.GetStringValue(GetConfigSection(port).c_str(), "Type", "None");
}
void USB::SetConfigDevice(SettingsInterface& si, u32 port, const char* devname)
{
si.SetStringValue(GetConfigSection(port).c_str(), "Type", devname);
}
u32 USB::GetConfigSubType(const SettingsInterface& si, u32 port, const std::string_view& devname)
{
return si.GetUIntValue(USBGetConfigSection(port).c_str(), fmt::format("{}_subtype", devname).c_str(), 0u);
return si.GetUIntValue(GetConfigSection(port).c_str(), fmt::format("{}_subtype", devname).c_str(), 0u);
}
std::string USB::GetConfigBindKey(const std::string_view& device, const std::string_view& bind_name)
void USB::SetConfigSubType(SettingsInterface& si, u32 port, const std::string_view& devname, u32 subtype)
{
si.SetUIntValue(GetConfigSection(port).c_str(), fmt::format("{}_subtype", devname).c_str(), subtype);
}
std::string USB::GetConfigSubKey(const std::string_view& device, const std::string_view& bind_name)
{
return fmt::format("{}_{}", device, bind_name);
}
@ -662,26 +686,26 @@ std::string USB::GetConfigBindKey(const std::string_view& device, const std::str
bool USB::GetConfigBool(SettingsInterface& si, u32 port, const char* devname, const char* key, bool default_value)
{
const std::string real_key(fmt::format("{}_{}", devname, key));
return si.GetBoolValue(USBGetConfigSection(port).c_str(), real_key.c_str(), default_value);
return si.GetBoolValue(GetConfigSection(port).c_str(), real_key.c_str(), default_value);
}
s32 USB::GetConfigInt(SettingsInterface& si, u32 port, const char* devname, const char* key, s32 default_value)
{
const std::string real_key(fmt::format("{}_{}", devname, key));
return si.GetIntValue(USBGetConfigSection(port).c_str(), real_key.c_str(), default_value);
return si.GetIntValue(GetConfigSection(port).c_str(), real_key.c_str(), default_value);
}
float USB::GetConfigFloat(SettingsInterface& si, u32 port, const char* devname, const char* key, float default_value)
{
const std::string real_key(fmt::format("{}_{}", devname, key));
return si.GetFloatValue(USBGetConfigSection(port).c_str(), real_key.c_str(), default_value);
return si.GetFloatValue(GetConfigSection(port).c_str(), real_key.c_str(), default_value);
}
std::string USB::GetConfigString(SettingsInterface& si, u32 port, const char* devname, const char* key, const char* default_value /*= ""*/)
{
const std::string real_key(fmt::format("{}_{}", devname, key));
return si.GetStringValue(USBGetConfigSection(port).c_str(), real_key.c_str(), default_value);
return si.GetStringValue(GetConfigSection(port).c_str(), real_key.c_str(), default_value);
}
@ -700,7 +724,7 @@ static u32 TryMapGenericMapping(SettingsInterface& si, const std::string& sectio
}
}
const std::string key(USB::GetConfigBindKey(type, bind_name));
const std::string key(USB::GetConfigSubKey(type, bind_name));
if (found_mapping)
{
Console.WriteLn("(MapDevice) Map %s/%s to '%s'", section.c_str(), bind_name, found_mapping->c_str());
@ -716,7 +740,7 @@ static u32 TryMapGenericMapping(SettingsInterface& si, const std::string& sectio
bool USB::MapDevice(SettingsInterface& si, u32 port, const std::vector<std::pair<GenericInputBinding, std::string>>& mapping)
{
const std::string section(USBGetConfigSection(port));
const std::string section(GetConfigSection(port));
const std::string type(GetConfigDevice(si, port));
const u32 subtype = GetConfigSubType(si, port, type);
const DeviceProxy* dev = RegisterDevice::instance().Device(type);
@ -737,7 +761,7 @@ bool USB::MapDevice(SettingsInterface& si, u32 port, const std::vector<std::pair
void USB::ClearPortBindings(SettingsInterface& si, u32 port)
{
const std::string section(USBGetConfigSection(port));
const std::string section(GetConfigSection(port));
const std::string type(GetConfigDevice(si, port));
const u32 subtype = GetConfigSubType(si, port, type);
const DeviceProxy* dev = RegisterDevice::instance().Device(type);
@ -745,7 +769,7 @@ void USB::ClearPortBindings(SettingsInterface& si, u32 port)
return;
for (const InputBindingInfo& bi : dev->Bindings(subtype))
si.DeleteValue(section.c_str(), GetConfigBindKey(type, bi.name).c_str());
si.DeleteValue(section.c_str(), GetConfigSubKey(type, bi.name).c_str());
}
void USB::CheckForConfigChanges(const Pcsx2Config& old_config)

View File

@ -40,7 +40,8 @@ namespace USB
std::vector<std::pair<std::string, std::string>> GetDeviceTypes();
const char* GetDeviceName(const std::string_view& device);
std::vector<std::string> GetDeviceSubtypes(const std::string_view& device);
const char* GetDeviceSubtypeName(const std::string_view& device, u32 subtype);
gsl::span<const char*> GetDeviceSubtypes(const std::string_view& device);
gsl::span<const InputBindingInfo> GetDeviceBindings(const std::string_view& device, u32 subtype);
gsl::span<const SettingInfo> GetDeviceSettings(const std::string_view& device, u32 subtype);
@ -54,11 +55,14 @@ namespace USB
/// Called when an input device is disconnected.
void InputDeviceDisconnected(const std::string_view& identifier);
std::string GetConfigSection(int port);
std::string GetConfigDevice(const SettingsInterface& si, u32 port);
void SetConfigDevice(SettingsInterface& si, u32 port, const char* devname);
u32 GetConfigSubType(const SettingsInterface& si, u32 port, const std::string_view& devname);
void SetConfigSubType(SettingsInterface& si, u32 port, const std::string_view& devname, u32 subtype);
/// Returns the configuration key for the specified bind and device type.
std::string GetConfigBindKey(const std::string_view& device, const std::string_view& bind_name);
std::string GetConfigSubKey(const std::string_view& device, const std::string_view& bind_name);
/// Performs automatic controller mapping with the provided list of generic mappings.
bool MapDevice(SettingsInterface& si, u32 port, const std::vector<std::pair<GenericInputBinding, std::string>>& mapping);
@ -82,8 +86,6 @@ namespace USB
std::string GetConfigString(SettingsInterface& si, u32 port, const char* devname, const char* key, const char* default_value = "");
} // namespace USB
std::string USBGetConfigSection(int port);
struct WindowInfo;
// ---------------------------------------------------------------------

View File

@ -28,7 +28,7 @@ RegisterDevice* RegisterDevice::registerDevice = nullptr;
DeviceProxy::~DeviceProxy() = default;
std::vector<std::string> DeviceProxy::SubTypes() const
gsl::span<const char*> DeviceProxy::SubTypes() const
{
return {};
}

View File

@ -60,7 +60,7 @@ public:
virtual const char* Name() const = 0;
virtual const char* TypeName() const = 0;
virtual std::vector<std::string> SubTypes() const;
virtual gsl::span<const char*> SubTypes() const;
virtual gsl::span<const InputBindingInfo> Bindings(u32 subtype) const;
virtual gsl::span<const SettingInfo> Settings(u32 subtype) const;

View File

@ -513,9 +513,10 @@ namespace usb_eyetoy
// TODO: Update device name
}
std::vector<std::string> EyeToyWebCamDevice::SubTypes() const
gsl::span<const char*> EyeToyWebCamDevice::SubTypes() const
{
return {"Sony EyeToy", "Konami Capture Eye"};
static const char* subtypes[] = {"Sony EyeToy", "Konami Capture Eye"};
return subtypes;
}
gsl::span<const SettingInfo> EyeToyWebCamDevice::Settings(u32 subtype) const

View File

@ -475,7 +475,7 @@ namespace usb_eyetoy
const char* TypeName() const override;
bool Freeze(USBDevice* dev, StateWrapper& sw) const override;
void UpdateSettings(USBDevice* dev, SettingsInterface& si) const override;
std::vector<std::string> SubTypes() const override;
gsl::span<const char*> SubTypes() const override;
gsl::span<const SettingInfo> Settings(u32 subtype) const override;
};

View File

@ -870,9 +870,10 @@ namespace usb_pad
s->SetBindValue(bind_index, value);
}
std::vector<std::string> PadDevice::SubTypes() const
gsl::span<const char*> PadDevice::SubTypes() const
{
return {"Driving Force", "Driving Force Pro", "Driving Force Pro (rev11.02)", "GT Force"};
static const char* subtypes[] = {"Driving Force", "Driving Force Pro", "Driving Force Pro (rev11.02)", "GT Force"};
return subtypes;
}
gsl::span<const InputBindingInfo> PadDevice::Bindings(u32 subtype) const
@ -932,7 +933,7 @@ namespace usb_pad
return nullptr;
}
std::vector<std::string> RBDrumKitDevice::SubTypes() const
gsl::span<const char*> RBDrumKitDevice::SubTypes() const
{
return {};
}
@ -969,7 +970,7 @@ namespace usb_pad
return "BuzzDevice";
}
std::vector<std::string> BuzzDevice::SubTypes() const
gsl::span<const char*> BuzzDevice::SubTypes() const
{
return {};
}
@ -1043,7 +1044,7 @@ namespace usb_pad
return "Keyboardmania";
}
std::vector<std::string> KeyboardmaniaDevice::SubTypes() const
gsl::span<const char*> KeyboardmaniaDevice::SubTypes() const
{
return {};
}

View File

@ -84,7 +84,7 @@ namespace usb_pad
void SetBindingValue(USBDevice* dev, u32 bind_index, float value) const override;
void InputDeviceConnected(USBDevice* dev, const std::string_view& identifier) const override;
void InputDeviceDisconnected(USBDevice* dev, const std::string_view& identifier) const override;
std::vector<std::string> SubTypes() const override;
gsl::span<const char*> SubTypes() const override;
gsl::span<const InputBindingInfo> Bindings(u32 subtype) const override;
gsl::span<const SettingInfo> Settings(u32 subtype) const override;
};
@ -94,7 +94,7 @@ namespace usb_pad
public:
const char* Name() const override;
const char* TypeName() const override;
std::vector<std::string> SubTypes() const override;
gsl::span<const char*> SubTypes() const override;
gsl::span<const InputBindingInfo> Bindings(u32 subtype) const override;
gsl::span<const SettingInfo> Settings(u32 subtype) const override;
USBDevice* CreateDevice(SettingsInterface& si, u32 port, u32 subtype) const override;
@ -105,7 +105,7 @@ namespace usb_pad
public:
const char* Name() const;
const char* TypeName() const;
std::vector<std::string> SubTypes() const;
gsl::span<const char*> SubTypes() const;
gsl::span<const InputBindingInfo> Bindings(u32 subtype) const;
gsl::span<const SettingInfo> Settings(u32 subtype) const;
USBDevice* CreateDevice(SettingsInterface& si, u32 port, u32 subtype) const;
@ -116,7 +116,7 @@ namespace usb_pad
public:
const char* Name() const;
const char* TypeName() const;
std::vector<std::string> SubTypes() const;
gsl::span<const char*> SubTypes() const;
gsl::span<const InputBindingInfo> Bindings(u32 subtype) const;
gsl::span<const SettingInfo> Settings(u32 subtype) const;
USBDevice* CreateDevice(SettingsInterface& si, u32 port, u32 subtype) const;
@ -128,7 +128,7 @@ namespace usb_pad
public:
const char* Name() const;
const char* TypeName() const;
std::vector<std::string> SubTypes() const;
gsl::span<const char*> SubTypes() const;
gsl::span<const InputBindingInfo> Bindings(u32 subtype) const;
gsl::span<const SettingInfo> Settings(u32 subtype) const;
USBDevice* CreateDevice(SettingsInterface& si, u32 port, u32 subtype) const;

View File

@ -352,7 +352,7 @@ namespace usb_pad
return "seamic";
}
std::vector<std::string> SeamicDevice::SubTypes() const
gsl::span<const char*> SeamicDevice::SubTypes() const
{
return {};
}

View File

@ -361,14 +361,9 @@ namespace usb_printer
return true;
}
std::vector<std::string> PrinterDevice::SubTypes() const
gsl::span<const char*> PrinterDevice::SubTypes() const
{
std::vector<std::string> ret;
for (uint32_t i = 0; i < sizeof(sPrinters) / sizeof(sPrinters[0]); i++)
{
ret.push_back(sPrinters[i].commercial_name);
}
return ret;
return sPrinterNames;
}
} // namespace usb_printer

View File

@ -120,6 +120,10 @@ namespace usb_printer
},
};
static const char* sPrinterNames[] = {
"Sony DPP-MP1",
};
class PrinterDevice final : public DeviceProxy
{
public:
@ -128,7 +132,7 @@ namespace usb_printer
const char* TypeName() const override;
bool Freeze(USBDevice* dev, StateWrapper& sw) const override;
std::vector<std::string> SubTypes() const override;
gsl::span<const char*> SubTypes() const override;
};
#pragma pack(push, 1)