udev_gamepad_linux.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/gamepad/udev_gamepad_linux.h"
  5. #include "base/strings/string_number_conversions.h"
  6. #include "base/strings/string_util.h"
  7. #include "device/udev_linux/udev.h"
  8. namespace device {
  9. namespace {
  10. // Extract the device |index| from a device node |path|. The path must begin
  11. // with |prefix| and the remainder of the string must be parsable as an integer.
  12. // Returns true if parsing succeeded.
  13. bool DeviceIndexFromDevicePath(base::StringPiece path,
  14. base::StringPiece prefix,
  15. int* index) {
  16. DCHECK(index);
  17. if (!base::StartsWith(path, prefix))
  18. return false;
  19. base::StringPiece index_str = path;
  20. index_str.remove_prefix(prefix.length());
  21. return base::StringToInt(index_str, index);
  22. }
  23. // Small helper to avoid constructing a `StringPiece` from nullptr.
  24. base::StringPiece ToStringPiece(const char* str) {
  25. return str ? base::StringPiece(str) : base::StringPiece();
  26. }
  27. } // namespace
  28. const char UdevGamepadLinux::kInputSubsystem[] = "input";
  29. const char UdevGamepadLinux::kHidrawSubsystem[] = "hidraw";
  30. UdevGamepadLinux::UdevGamepadLinux(Type type,
  31. int index,
  32. base::StringPiece path,
  33. base::StringPiece syspath_prefix)
  34. : type(type), index(index), path(path), syspath_prefix(syspath_prefix) {}
  35. // static
  36. std::unique_ptr<UdevGamepadLinux> UdevGamepadLinux::Create(udev_device* dev) {
  37. using DeviceRootPair = std::pair<Type, const char*>;
  38. static const std::vector<DeviceRootPair> device_roots = {
  39. {Type::EVDEV, "/dev/input/event"},
  40. {Type::JOYDEV, "/dev/input/js"},
  41. {Type::HIDRAW, "/dev/hidraw"},
  42. };
  43. if (!dev)
  44. return nullptr;
  45. const auto node_path = ToStringPiece(device::udev_device_get_devnode(dev));
  46. if (node_path.empty())
  47. return nullptr;
  48. const auto node_syspath = ToStringPiece(device::udev_device_get_syspath(dev));
  49. if (node_syspath.empty())
  50. return nullptr;
  51. base::StringPiece parent_syspath;
  52. udev_device* parent_dev =
  53. device::udev_device_get_parent_with_subsystem_devtype(
  54. dev, kInputSubsystem, nullptr);
  55. if (parent_dev)
  56. parent_syspath = ToStringPiece(device::udev_device_get_syspath(parent_dev));
  57. for (const auto& entry : device_roots) {
  58. const Type node_type = entry.first;
  59. const base::StringPiece prefix = entry.second;
  60. int index_value;
  61. if (!DeviceIndexFromDevicePath(node_path, prefix, &index_value))
  62. continue;
  63. // The syspath can be used to associate device nodes that describe the same
  64. // gamepad through multiple interfaces. For input nodes (evdev and joydev),
  65. // we use the syspath of the parent node, which describes the underlying
  66. // physical device. For hidraw nodes, we use the syspath of the node itself.
  67. //
  68. // The parent syspaths for matching evdev and joydev nodes will be identical
  69. // because they share the same parent node. The syspath for hidraw nodes
  70. // will also be identical up to the subsystem. For instance, if the syspath
  71. // for the input subsystem is:
  72. // /sys/devices/[...]/0003:054C:09CC.0026/input/input91
  73. // And the corresponding hidraw syspath is:
  74. // /sys/devices/[...]/0003:054C:09CC.0026/hidraw/hidraw3
  75. // Then |syspath_prefix| is the common prefix before "input" or "hidraw":
  76. // /sys/devices/[...]/0003:054C:09CC.0026/
  77. base::StringPiece syspath;
  78. base::StringPiece subsystem;
  79. if (node_type == Type::EVDEV || node_type == Type::JOYDEV) {
  80. // If the device is in the input subsystem but does not have the
  81. // ID_INPUT_JOYSTICK property, ignore it.
  82. if (!device::udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
  83. return nullptr;
  84. syspath = parent_syspath;
  85. subsystem = kInputSubsystem;
  86. } else if (node_type == Type::HIDRAW) {
  87. syspath = node_syspath;
  88. subsystem = kHidrawSubsystem;
  89. }
  90. base::StringPiece syspath_prefix;
  91. if (!syspath.empty()) {
  92. size_t subsystem_start = syspath.find(subsystem);
  93. if (subsystem_start == std::string::npos)
  94. return nullptr;
  95. syspath_prefix = syspath.substr(0, subsystem_start);
  96. }
  97. return std::make_unique<UdevGamepadLinux>(node_type, index_value, node_path,
  98. syspath_prefix);
  99. }
  100. return nullptr;
  101. }
  102. } // namespace device