Files

134 lines
3.2 KiB
C++
Raw Permalink Normal View History

// SPDX-FileCopyrightText: 2002-2026 PCSX2 Dev Team
2024-07-30 13:42:36 +02:00
// SPDX-License-Identifier: GPL-3.0+
2020-11-02 23:41:01 +01:00
#pragma once
#include <memory>
2023-07-19 23:53:42 +10:00
#include <span>
#include <string>
#include <string_view>
#include <map>
#include <list>
#include <algorithm>
#include <iterator>
#include <memory>
#include "qemu-usb/USBinternal.h"
#include "Config.h"
2021-07-06 08:08:18 +02:00
#include "SaveState.h"
class StateWrapper;
// also map key/array index
enum DeviceType : s32
{
DEVTYPE_NONE = -1,
DEVTYPE_PAD = 0,
DEVTYPE_MSD,
DEVTYPE_MICROPHONE,
DEVTYPE_LOGITECH_HEADSET,
DEVTYPE_HIDKEYBOARD,
DEVTYPE_HIDMOUSE,
DEVTYPE_RBKIT,
DEVTYPE_BUZZ,
DEVTYPE_EYETOY,
2024-07-23 10:15:04 +03:00
DEVTYPE_TRANCE_VIBRATOR,
2020-11-03 16:45:39 +01:00
DEVTYPE_SEGA_SEAMIC,
2021-07-12 22:47:54 +03:00
DEVTYPE_PRINTER,
2020-10-07 23:00:57 +02:00
DEVTYPE_KEYBOARDMANIA,
2023-07-01 21:46:41 +12:00
DEVTYPE_GUNCON2,
2024-07-23 16:40:51 +03:00
DEVTYPE_DJ,
DEVTYPE_GAMETRAK,
DEVTYPE_REALPLAY,
2024-08-04 20:48:01 -07:00
DEVTYPE_TRAIN,
};
class DeviceProxy
{
2020-11-02 23:42:03 +01:00
public:
virtual ~DeviceProxy();
virtual const char* Name() const = 0;
virtual const char* TypeName() const = 0;
virtual const char* IconName() const = 0;
2023-07-19 23:53:42 +10:00
virtual std::span<const char*> SubTypes() const;
virtual std::span<const InputBindingInfo> Bindings(u32 subtype) const;
virtual std::span<const SettingInfo> Settings(u32 subtype) const;
virtual USBDevice* CreateDevice(SettingsInterface& si, u32 port, u32 subtype) const = 0;
virtual float GetBindingValue(const USBDevice* dev, u32 bind) const;
virtual void SetBindingValue(USBDevice* dev, u32 bind, float value) const;
virtual bool Freeze(USBDevice* dev, StateWrapper& sw) const;
virtual void UpdateSettings(USBDevice* dev, SettingsInterface& si) const;
virtual void InputDeviceConnected(USBDevice* dev, const std::string_view identifier) const;
virtual void InputDeviceDisconnected(USBDevice* dev, const std::string_view identifier) const;
};
class RegisterDevice
{
RegisterDevice(const RegisterDevice&) = delete;
RegisterDevice() {}
2020-11-02 23:42:03 +01:00
static RegisterDevice* registerDevice;
2020-11-02 23:42:03 +01:00
public:
typedef std::map<DeviceType, std::unique_ptr<DeviceProxy>> RegisterDeviceMap;
2020-11-02 23:42:03 +01:00
static RegisterDevice& instance()
{
if (!registerDevice)
registerDevice = new RegisterDevice();
return *registerDevice;
}
2020-10-07 23:00:57 +02:00
~RegisterDevice() {}
static void Register();
void Unregister();
void Add(DeviceType key, DeviceProxy* creator)
{
registerDeviceMap[key] = std::unique_ptr<DeviceProxy>(creator);
}
DeviceProxy* Device(const std::string_view name)
{
auto proxy = std::find_if(registerDeviceMap.begin(),
2020-11-02 23:42:03 +01:00
registerDeviceMap.end(),
[&name](const RegisterDeviceMap::value_type& val) -> bool {
return val.second->TypeName() == name;
});
if (proxy != registerDeviceMap.end())
return proxy->second.get();
return nullptr;
}
2022-12-27 19:14:59 +10:00
DeviceProxy* Device(s32 index)
{
2022-12-27 19:14:59 +10:00
const auto it = registerDeviceMap.find(static_cast<DeviceType>(index));
return (it != registerDeviceMap.end()) ? it->second.get() : nullptr;
}
DeviceType Index(const std::string_view name)
{
auto proxy = std::find_if(registerDeviceMap.begin(),
2020-11-02 23:42:03 +01:00
registerDeviceMap.end(),
[&name](RegisterDeviceMap::value_type& val) -> bool {
return val.second->TypeName() == name;
});
if (proxy != registerDeviceMap.end())
return proxy->first;
return DEVTYPE_NONE;
}
const RegisterDeviceMap& Map() const
{
return registerDeviceMap;
}
2020-11-02 23:42:03 +01:00
private:
RegisterDeviceMap registerDeviceMap;
};