MCPcopy Create free account
hub / github.com/SFML/SFML / getJoystickName

Function getJoystickName

src/SFML/Window/Unix/JoystickImpl.cpp:380–409  ·  view source on GitHub ↗

Get the joystick name

Source from the content-addressed store, hash-verified

378
379// Get the joystick name
380std::string getJoystickName(unsigned int index)
381{
382 const std::string devnode = joystickList[index].deviceNode;
383
384 // First try using ioctl with JSIOCGNAME
385 const int fd = ::open(devnode.c_str(), O_RDONLY | O_NONBLOCK);
386
387 if (fd >= 0)
388 {
389 // Get the name
390 std::array<char, 128> name{};
391 const int result = ioctl(fd, JSIOCGNAME(name.size()), name.data());
392
393 ::close(fd);
394
395 if (result >= 0)
396 return name.data();
397 }
398
399 // Fall back to manual USB chain walk via udev
400 if (udevContext)
401 if (const auto udevDevice = UdevPtr<udev_device>(
402 udev_device_new_from_syspath(udevContext.get(), joystickList[index].systemPath.c_str())))
403 if (const char* product = getUsbAttribute(udevDevice.get(), "product"))
404 return {product};
405
406 sf::err() << "Unable to get name for joystick " << devnode << std::endl;
407
408 return "Unknown Joystick";
409}
410} // namespace
411
412

Callers 1

openMethod · 0.85

Calls 3

closeFunction · 0.85
getUsbAttributeFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected