Add USB passthrough setting and USBUtils

This adds a USB passthrough setting to ConfigManager and everything
needed for the UI to show and manage the whitelist properly.
This commit is contained in:
Léo Lam 2016-11-11 01:33:52 +01:00
parent e4d20647d4
commit 69a13a12e2
7 changed files with 161 additions and 1 deletions

View File

@ -75,6 +75,7 @@ void SConfig::SaveSettings()
SaveAnalyticsSettings(ini);
SaveNetworkSettings(ini);
SaveBluetoothPassthroughSettings(ini);
SaveUSBPassthroughSettings(ini);
SaveSysconfSettings(ini);
ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX));
@ -341,6 +342,20 @@ void SConfig::SaveBluetoothPassthroughSettings(IniFile& ini)
section->Set("LinkKeys", m_bt_passthrough_link_keys);
}
void SConfig::SaveUSBPassthroughSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("USBPassthrough");
std::ostringstream oss;
for (const auto& device : m_usb_passthrough_devices)
oss << StringFromFormat("%04x:%04x", device.first, device.second) << ',';
std::string devices_string = oss.str();
if (!devices_string.empty())
devices_string.pop_back();
section->Set("Devices", devices_string);
}
void SConfig::SaveSysconfSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("Sysconf");
@ -398,6 +413,7 @@ void SConfig::LoadSettings()
LoadNetworkSettings(ini);
LoadAnalyticsSettings(ini);
LoadBluetoothPassthroughSettings(ini);
LoadUSBPassthroughSettings(ini);
LoadSysconfSettings(ini);
}
@ -674,6 +690,27 @@ void SConfig::LoadBluetoothPassthroughSettings(IniFile& ini)
section->Get("LinkKeys", &m_bt_passthrough_link_keys, "");
}
void SConfig::LoadUSBPassthroughSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("USBPassthrough");
m_usb_passthrough_devices.clear();
std::string devices_string;
std::vector<std::string> pairs;
section->Get("Devices", &devices_string, "");
SplitString(devices_string, ',', pairs);
for (const auto& pair : pairs)
{
const auto index = pair.find(':');
if (index == std::string::npos)
continue;
const u16 vid = static_cast<u16>(strtol(pair.substr(0, index).c_str(), nullptr, 16));
const u16 pid = static_cast<u16>(strtol(pair.substr(index + 1).c_str(), nullptr, 16));
if (vid && pid)
m_usb_passthrough_devices.emplace(vid, pid);
}
}
void SConfig::LoadSysconfSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("Sysconf");
@ -764,6 +801,11 @@ void SConfig::LoadDefaults()
m_revision = 0;
}
bool SConfig::IsUSBDeviceWhitelisted(const std::pair<u16, u16> vid_pid) const
{
return m_usb_passthrough_devices.find(vid_pid) != m_usb_passthrough_devices.end();
}
const char* SConfig::GetDirectoryForRegion(DiscIO::Region region)
{
switch (region)

View File

@ -5,7 +5,9 @@
#pragma once
#include <limits>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "Common/IniFile.h"
@ -151,6 +153,10 @@ struct SConfig : NonCopyable
int m_bt_passthrough_vid = -1;
std::string m_bt_passthrough_link_keys;
// USB passthrough settings
std::set<std::pair<u16, u16>> m_usb_passthrough_devices;
bool IsUSBDeviceWhitelisted(std::pair<u16, u16> vid_pid) const;
// SYSCONF settings
int m_sensor_bar_position = 0x01;
int m_sensor_bar_sensitivity = 0x03;
@ -350,6 +356,7 @@ private:
void SaveNetworkSettings(IniFile& ini);
void SaveAnalyticsSettings(IniFile& ini);
void SaveBluetoothPassthroughSettings(IniFile& ini);
void SaveUSBPassthroughSettings(IniFile& ini);
void SaveSysconfSettings(IniFile& ini);
void LoadGeneralSettings(IniFile& ini);
@ -364,6 +371,7 @@ private:
void LoadNetworkSettings(IniFile& ini);
void LoadAnalyticsSettings(IniFile& ini);
void LoadBluetoothPassthroughSettings(IniFile& ini);
void LoadUSBPassthroughSettings(IniFile& ini);
void LoadSysconfSettings(IniFile& ini);
bool SetRegion(DiscIO::Region region, std::string* directory_name);

View File

@ -1,6 +1,10 @@
set(SRCS Disassembler.cpp
UICommon.cpp)
UICommon.cpp
USBUtils.cpp)
set(LIBS common)
if(LIBUSB_FOUND)
set(LIBS ${LIBS} ${LIBUSB_LIBRARIES})
endif()
add_dolphin_library(uicommon "${SRCS}" "${LIBS}")

View File

@ -17,6 +17,7 @@
#include "InputCommon/GCAdapter.h"
#include "UICommon/UICommon.h"
#include "UICommon/USBUtils.h"
#include "VideoCommon/VideoBackendBase.h"
@ -29,6 +30,7 @@ void Init()
VideoBackendBase::PopulateList();
WiimoteReal::LoadSettings();
GCAdapter::Init();
USBUtils::Init();
VideoBackendBase::ActivateBackend(SConfig::GetInstance().m_strVideoBackend);
SetEnableAlert(SConfig::GetInstance().bUsePanicHandlers);
@ -40,6 +42,7 @@ void Shutdown()
WiimoteReal::Shutdown();
VideoBackendBase::ClearList();
SConfig::Shutdown();
USBUtils::Shutdown();
LogManager::Shutdown();
}

View File

@ -45,10 +45,14 @@
<ItemGroup>
<ClCompile Include="UICommon.cpp" />
<ClCompile Include="Disassembler.cpp" />
<ClCompile Include="USBUtils.cpp">
<DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="UICommon.h" />
<ClInclude Include="Disassembler.h" />
<ClInclude Include="USBUtils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -0,0 +1,79 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#ifdef __LIBUSB__
#include <libusb.h>
#endif
#include "Common/CommonTypes.h"
#include "Common/StringUtil.h"
#include "UICommon/USBUtils.h"
#ifdef __LIBUSB__
static libusb_context* s_libusb_context;
#endif
// Because opening and getting the device name from devices is slow, especially on Windows
// with usbdk, we cannot do that for every single device. We should however still show
// device names for known Wii peripherals.
static const std::map<std::pair<u16, u16>, std::string> s_wii_peripherals = {{
{{0x046d, 0x0a03}, "Logitech Microphone"},
{{0x057e, 0x0308}, "Wii Speak"},
{{0x057e, 0x0309}, "Nintendo USB Microphone"},
{{0x057e, 0x030a}, "Ubisoft Motion Tracking Camera"},
{{0x0e6f, 0x0129}, "Disney Infinity Reader (Portal Device)"},
{{0x1430, 0x0100}, "Tony Hawk Ride Skateboard"},
{{0x1430, 0x0150}, "Skylanders Portal"},
{{0x1bad, 0x0004}, "Harmonix Guitar Controller"},
{{0x1bad, 0x3110}, "Rock Band 3 Mustang Guitar Dongle"},
{{0x1bad, 0x3430}, "Rock Band Drum Set"},
{{0x21a4, 0xac40}, "EA Active NFL"},
}};
namespace USBUtils
{
void Init()
{
#ifdef __LIBUSB__
libusb_init(&s_libusb_context);
#endif
}
void Shutdown()
{
#ifdef __LIBUSB__
libusb_exit(s_libusb_context);
#endif
}
std::map<std::pair<u16, u16>, std::string> GetInsertedDevices()
{
std::map<std::pair<u16, u16>, std::string> devices;
#ifdef __LIBUSB__
if (!s_libusb_context)
return devices;
libusb_device** list;
const ssize_t cnt = libusb_get_device_list(s_libusb_context, &list);
for (ssize_t i = 0; i < cnt; ++i)
{
libusb_device_descriptor descr;
libusb_get_device_descriptor(list[i], &descr);
const std::pair<u16, u16> vid_pid{descr.idVendor, descr.idProduct};
devices[vid_pid] = GetDeviceName(vid_pid);
}
libusb_free_device_list(list, 1);
#endif
return devices;
}
std::string GetDeviceName(const std::pair<u16, u16> vid_pid)
{
const auto iter = s_wii_peripherals.find(vid_pid);
const std::string device_name = iter == s_wii_peripherals.cend() ? "Unknown" : iter->second;
return StringFromFormat("%04x:%04x - %s", vid_pid.first, vid_pid.second, device_name.c_str());
}
} // namespace USBUtils

View File

@ -0,0 +1,20 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <map>
#include <string>
#include <utility>
#include "Common/CommonTypes.h"
namespace USBUtils
{
void Init();
void Shutdown();
std::map<std::pair<u16, u16>, std::string> GetInsertedDevices();
std::string GetDeviceName(std::pair<u16, u16> vid_pid);
} // namespace USBUtils