USBUtils.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_known_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. {{0x12ba, 0x0200}, "Harmonix Guitar for PlayStation 3"},
  22. {{0x12ba, 0x0210}, "Harmonix Drum Kit for PlayStation 3"},
  23. {{0x12ba, 0x0218}, "Harmonix Drum Kit for PlayStation 3"},
  24. {{0x12ba, 0x2330}, "Harmonix RB3 Keyboard for PlayStation 3"},
  25. {{0x12ba, 0x2338}, "Harmonix RB3 MIDI Keyboard Interface for PlayStation 3"},
  26. {{0x12ba, 0x2430}, "Harmonix RB3 Mustang Guitar for PlayStation 3"},
  27. {{0x12ba, 0x2438}, "Harmonix RB3 MIDI Guitar Interface for PlayStation 3"},
  28. {{0x12ba, 0x2530}, "Harmonix RB3 Squier Guitar for PlayStation 3"},
  29. {{0x12ba, 0x2538}, "Harmonix RB3 MIDI Guitar Interface for PlayStation 3"},
  30. {{0x1430, 0x0100}, "Tony Hawk Ride Skateboard"},
  31. {{0x1430, 0x0150}, "Skylanders Portal"},
  32. {{0x1bad, 0x0004}, "Harmonix Guitar Controller for Nintendo Wii"},
  33. {{0x1bad, 0x0005}, "Harmonix Drum Controller for Nintendo Wii"},
  34. {{0x1bad, 0x3010}, "Harmonix Guitar Controller for Nintendo Wii"},
  35. {{0x1bad, 0x3110}, "Harmonix Drum Controller for Nintendo Wii"},
  36. {{0x1bad, 0x3138}, "Harmonix Drum Controller for Nintendo Wii"},
  37. {{0x1bad, 0x3330}, "Harmonix RB3 Keyboard for Nintendo Wii"},
  38. {{0x1bad, 0x3338}, "Harmonix RB3 MIDI Keyboard Interface for Nintendo Wii"},
  39. {{0x1bad, 0x3430}, "Harmonix RB3 Mustang Guitar for Nintendo Wii"},
  40. {{0x1bad, 0x3438}, "Harmonix RB3 MIDI Guitar Interface for Nintendo Wii"},
  41. {{0x1bad, 0x3530}, "Harmonix RB3 Squier Guitar for Nintendo Wii"},
  42. {{0x1bad, 0x3538}, "Harmonix RB3 MIDI Guitar Interface for Nintendo Wii"},
  43. {{0x21a4, 0xac40}, "EA Active NFL"},
  44. }};
  45. namespace USBUtils
  46. {
  47. std::map<std::pair<u16, u16>, std::string> GetInsertedDevices()
  48. {
  49. std::map<std::pair<u16, u16>, std::string> devices;
  50. #ifdef __LIBUSB__
  51. LibusbUtils::Context context;
  52. if (!context.IsValid())
  53. return devices;
  54. const int ret = context.GetDeviceList([&](libusb_device* device) {
  55. libusb_device_descriptor descr;
  56. libusb_get_device_descriptor(device, &descr);
  57. const std::pair<u16, u16> vid_pid{descr.idVendor, descr.idProduct};
  58. devices[vid_pid] = GetDeviceName(vid_pid);
  59. return true;
  60. });
  61. if (ret != LIBUSB_SUCCESS)
  62. WARN_LOG_FMT(COMMON, "GetDeviceList failed: {}", LibusbUtils::ErrorWrap(ret));
  63. #endif
  64. return devices;
  65. }
  66. std::string GetDeviceName(const std::pair<u16, u16> vid_pid)
  67. {
  68. const auto iter = s_known_peripherals.find(vid_pid);
  69. const std::string_view device_name =
  70. iter == s_known_peripherals.cend() ? "Unknown" : iter->second;
  71. return fmt::format("{:04x}:{:04x} - {}", vid_pid.first, vid_pid.second, device_name);
  72. }
  73. } // namespace USBUtils