From c477ea1628e4d2dfe4f9f1675ce2839caf216660 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Mon, 19 Dec 2022 20:45:02 +1000 Subject: [PATCH] USB: Change SubTypes() to use a span --- .../Settings/ControllerBindingWidgets.cpp | 7 ++- pcsx2/Frontend/InputManager.cpp | 4 +- pcsx2/Pcsx2Config.cpp | 9 ++-- pcsx2/USB/USB.cpp | 54 +++++++++++++------ pcsx2/USB/USB.h | 10 ++-- pcsx2/USB/deviceproxy.cpp | 2 +- pcsx2/USB/deviceproxy.h | 2 +- pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.cpp | 5 +- pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.h | 2 +- pcsx2/USB/usb-pad/usb-pad.cpp | 11 ++-- pcsx2/USB/usb-pad/usb-pad.h | 10 ++-- pcsx2/USB/usb-pad/usb-seamic.cpp | 2 +- pcsx2/USB/usb-printer/usb-printer.cpp | 9 +--- pcsx2/USB/usb-printer/usb-printer.h | 6 ++- 14 files changed, 81 insertions(+), 52 deletions(-) diff --git a/pcsx2-qt/Settings/ControllerBindingWidgets.cpp b/pcsx2-qt/Settings/ControllerBindingWidgets.cpp index f3a24ce31d..270b246848 100644 --- a/pcsx2-qt/Settings/ControllerBindingWidgets.cpp +++ b/pcsx2-qt/Settings/ControllerBindingWidgets.cpp @@ -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 bindings) diff --git a/pcsx2/Frontend/InputManager.cpp b/pcsx2/Frontend/InputManager.cpp index 0875209d4c..29dc64d161 100644 --- a/pcsx2/Frontend/InputManager.cpp +++ b/pcsx2/Frontend/InputManager.cpp @@ -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) { diff --git a/pcsx2/Pcsx2Config.cpp b/pcsx2/Pcsx2Config.cpp index bc3dad4783..2033cff599 100644 --- a/pcsx2/Pcsx2Config.cpp +++ b/pcsx2/Pcsx2Config.cpp @@ -1110,7 +1110,7 @@ void Pcsx2Config::USBOptions::LoadSave(SettingsWrapper& wrap) { for (u32 i = 0; i < static_cast(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); + } } } diff --git a/pcsx2/USB/USB.cpp b/pcsx2/USB/USB.cpp index e33ecf3fde..f4b0f26334 100644 --- a/pcsx2/USB/USB.cpp +++ b/pcsx2/USB/USB.cpp @@ -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> USB::GetDeviceTypes() { RegisterDevice& rd = RegisterDevice::instance(); std::vector> 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 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(); + if (!dev) + return "Unknown"; + + const gsl::span subtypes(dev->SubTypes()); + if (subtypes.empty() || subtype >= subtypes.size()) + return ""; + + return subtypes[subtype]; +} + +gsl::span USB::GetDeviceSubtypes(const std::string_view& device) +{ + const DeviceProxy* dev = RegisterDevice::instance().Device(device); + return dev ? dev->SubTypes() : gsl::span(); } gsl::span 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>& 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::vectorBindings(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) diff --git a/pcsx2/USB/USB.h b/pcsx2/USB/USB.h index 5be5db0f1b..05a143529f 100644 --- a/pcsx2/USB/USB.h +++ b/pcsx2/USB/USB.h @@ -40,7 +40,8 @@ namespace USB std::vector> GetDeviceTypes(); const char* GetDeviceName(const std::string_view& device); - std::vector GetDeviceSubtypes(const std::string_view& device); + const char* GetDeviceSubtypeName(const std::string_view& device, u32 subtype); + gsl::span GetDeviceSubtypes(const std::string_view& device); gsl::span GetDeviceBindings(const std::string_view& device, u32 subtype); gsl::span 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>& 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; // --------------------------------------------------------------------- diff --git a/pcsx2/USB/deviceproxy.cpp b/pcsx2/USB/deviceproxy.cpp index 6c59733aec..79a66025c6 100644 --- a/pcsx2/USB/deviceproxy.cpp +++ b/pcsx2/USB/deviceproxy.cpp @@ -28,7 +28,7 @@ RegisterDevice* RegisterDevice::registerDevice = nullptr; DeviceProxy::~DeviceProxy() = default; -std::vector DeviceProxy::SubTypes() const +gsl::span DeviceProxy::SubTypes() const { return {}; } diff --git a/pcsx2/USB/deviceproxy.h b/pcsx2/USB/deviceproxy.h index f89a76650c..3be7ce096c 100644 --- a/pcsx2/USB/deviceproxy.h +++ b/pcsx2/USB/deviceproxy.h @@ -60,7 +60,7 @@ public: virtual const char* Name() const = 0; virtual const char* TypeName() const = 0; - virtual std::vector SubTypes() const; + virtual gsl::span SubTypes() const; virtual gsl::span Bindings(u32 subtype) const; virtual gsl::span Settings(u32 subtype) const; diff --git a/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.cpp b/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.cpp index 9e1bee0fd5..1435877f38 100644 --- a/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.cpp +++ b/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.cpp @@ -513,9 +513,10 @@ namespace usb_eyetoy // TODO: Update device name } - std::vector EyeToyWebCamDevice::SubTypes() const + gsl::span EyeToyWebCamDevice::SubTypes() const { - return {"Sony EyeToy", "Konami Capture Eye"}; + static const char* subtypes[] = {"Sony EyeToy", "Konami Capture Eye"}; + return subtypes; } gsl::span EyeToyWebCamDevice::Settings(u32 subtype) const diff --git a/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.h b/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.h index 51001372e0..57b7270955 100644 --- a/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.h +++ b/pcsx2/USB/usb-eyetoy/usb-eyetoy-webcam.h @@ -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 SubTypes() const override; + gsl::span SubTypes() const override; gsl::span Settings(u32 subtype) const override; }; diff --git a/pcsx2/USB/usb-pad/usb-pad.cpp b/pcsx2/USB/usb-pad/usb-pad.cpp index f25701f2c5..3751a4da9a 100644 --- a/pcsx2/USB/usb-pad/usb-pad.cpp +++ b/pcsx2/USB/usb-pad/usb-pad.cpp @@ -870,9 +870,10 @@ namespace usb_pad s->SetBindValue(bind_index, value); } - std::vector PadDevice::SubTypes() const + gsl::span 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 PadDevice::Bindings(u32 subtype) const @@ -932,7 +933,7 @@ namespace usb_pad return nullptr; } - std::vector RBDrumKitDevice::SubTypes() const + gsl::span RBDrumKitDevice::SubTypes() const { return {}; } @@ -969,7 +970,7 @@ namespace usb_pad return "BuzzDevice"; } - std::vector BuzzDevice::SubTypes() const + gsl::span BuzzDevice::SubTypes() const { return {}; } @@ -1043,7 +1044,7 @@ namespace usb_pad return "Keyboardmania"; } - std::vector KeyboardmaniaDevice::SubTypes() const + gsl::span KeyboardmaniaDevice::SubTypes() const { return {}; } diff --git a/pcsx2/USB/usb-pad/usb-pad.h b/pcsx2/USB/usb-pad/usb-pad.h index cfa2bafe98..f65b71bc90 100644 --- a/pcsx2/USB/usb-pad/usb-pad.h +++ b/pcsx2/USB/usb-pad/usb-pad.h @@ -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 SubTypes() const override; + gsl::span SubTypes() const override; gsl::span Bindings(u32 subtype) const override; gsl::span 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 SubTypes() const override; + gsl::span SubTypes() const override; gsl::span Bindings(u32 subtype) const override; gsl::span 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 SubTypes() const; + gsl::span SubTypes() const; gsl::span Bindings(u32 subtype) const; gsl::span 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 SubTypes() const; + gsl::span SubTypes() const; gsl::span Bindings(u32 subtype) const; gsl::span 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 SubTypes() const; + gsl::span SubTypes() const; gsl::span Bindings(u32 subtype) const; gsl::span Settings(u32 subtype) const; USBDevice* CreateDevice(SettingsInterface& si, u32 port, u32 subtype) const; diff --git a/pcsx2/USB/usb-pad/usb-seamic.cpp b/pcsx2/USB/usb-pad/usb-seamic.cpp index 9178c07182..6c21bb5aa8 100644 --- a/pcsx2/USB/usb-pad/usb-seamic.cpp +++ b/pcsx2/USB/usb-pad/usb-seamic.cpp @@ -352,7 +352,7 @@ namespace usb_pad return "seamic"; } - std::vector SeamicDevice::SubTypes() const + gsl::span SeamicDevice::SubTypes() const { return {}; } diff --git a/pcsx2/USB/usb-printer/usb-printer.cpp b/pcsx2/USB/usb-printer/usb-printer.cpp index 3067f69c66..1b37ef6a2b 100644 --- a/pcsx2/USB/usb-printer/usb-printer.cpp +++ b/pcsx2/USB/usb-printer/usb-printer.cpp @@ -361,14 +361,9 @@ namespace usb_printer return true; } - std::vector PrinterDevice::SubTypes() const + gsl::span PrinterDevice::SubTypes() const { - std::vector 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 diff --git a/pcsx2/USB/usb-printer/usb-printer.h b/pcsx2/USB/usb-printer/usb-printer.h index e0b1b5c9ca..9cb5d839be 100644 --- a/pcsx2/USB/usb-printer/usb-printer.h +++ b/pcsx2/USB/usb-printer/usb-printer.h @@ -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 SubTypes() const override; + gsl::span SubTypes() const override; }; #pragma pack(push, 1)