USBUtils.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "UICommon/USBUtils.h"
  4. #include <string_view>
  5. #include <fmt/format.h>
  6. #ifdef __LIBUSB__
  7. #include <libusb.h>
  8. #endif
  9. #include "Common/CommonTypes.h"
  10. #include "Common/Logging/Log.h"
  11. #include "Core/LibusbUtils.h"
  12. // Because opening and getting the device name from devices is slow, especially on Windows
  13. // with usbdk, we cannot do that for every single device. We should however still show
  14. // device names for known Wii peripherals.
  15. static const std::map<std::pair<u16, u16>, std::string_view> s_wii_peripherals{{
  16. {{0x046d, 0x0a03}, "Logitech Microphone"},
  17. {{0x057e, 0x0308}, "Wii Speak"},
  18. {{0x057e, 0x0309}, "Nintendo USB Microphone"},
  19. {{0x057e, 0x030a}, "Ubisoft Motion Tracking Camera"},
  20. {{0x0e6f, 0x0129}, "Disney Infinity Reader (Portal Device)"},
  21. {{0x1430, 0x0100}, "Tony Hawk Ride Skateboard"},
  22. {{0x1430, 0x0150}, "Skylanders Portal"},
  23. {{0x1bad, 0x0004}, "Harmonix Guitar Controller"},
  24. {{0x1bad, 0x3110}, "Rock Band Drum Set"},
  25. {{0x1bad, 0x3138}, "Harmonix Drum Controller for Nintendo Wii"},
  26. {{0x1bad, 0x3330}, "Harmonix RB3 Keyboard for Nintendo Wii"},
  27. {{0x1bad, 0x3338}, "Harmonix RB3 MIDI Keyboard Interface for Nintendo Wii"},
  28. {{0x1bad, 0x3430}, "Harmonix RB3 Mustang Guitar for Nintendo Wii"},
  29. {{0x1bad, 0x3538}, "Harmonix RB3 MIDI Guitar Interface for Nintendo Wii"},
  30. {{0x21a4, 0xac40}, "EA Active NFL"},
  31. }};
  32. namespace USBUtils
  33. {
  34. std::map<std::pair<u16, u16>, std::string> GetInsertedDevices()
  35. {
  36. std::map<std::pair<u16, u16>, std::string> devices;
  37. #ifdef __LIBUSB__
  38. LibusbUtils::Context context;
  39. if (!context.IsValid())
  40. return devices;
  41. const int ret = context.GetDeviceList([&](libusb_device* device) {
  42. libusb_device_descriptor descr;
  43. libusb_get_device_descriptor(device, &descr);
  44. const std::pair<u16, u16> vid_pid{descr.idVendor, descr.idProduct};
  45. devices[vid_pid] = GetDeviceName(vid_pid);
  46. return true;
  47. });
  48. if (ret != LIBUSB_SUCCESS)
  49. WARN_LOG_FMT(COMMON, "GetDeviceList failed: {}", LibusbUtils::ErrorWrap(ret));
  50. #endif
  51. return devices;
  52. }
  53. std::string GetDeviceName(const std::pair<u16, u16> vid_pid)
  54. {
  55. const auto iter = s_wii_peripherals.find(vid_pid);
  56. const std::string_view device_name = iter == s_wii_peripherals.cend() ? "Unknown" : iter->second;
  57. return fmt::format("{:04x}:{:04x} - {}", vid_pid.first, vid_pid.second, device_name);
  58. }
  59. } // namespace USBUtils