Skip to content

Commit

Permalink
Updates Unix joystick implementation to use udev to look up product a…
Browse files Browse the repository at this point in the history
…nd manufacturer IDs.

Updates CMake to link udev.
  • Loading branch information
NoobsArePeople2 committed Jan 26, 2014
1 parent 24a444d commit 852b7b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/SFML/Window/CMakeLists.txt
Expand Up @@ -136,7 +136,7 @@ set(WINDOW_EXT_LIBS ${OPENGL_gl_LIBRARY})
if(SFML_OS_WINDOWS)
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} winmm gdi32)
elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD)
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} ${X11_X11_LIB} ${X11_Xrandr_LIB})
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} ${X11_X11_LIB} ${X11_Xrandr_LIB} udev)
if(SFML_OS_FREEBSD)
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} usbhid)
endif()
Expand Down
74 changes: 32 additions & 42 deletions src/SFML/Window/Unix/JoystickImpl.cpp
Expand Up @@ -34,6 +34,7 @@
#include <errno.h>
#include <cstdio>
#include <libudev.h>
#include <sstream>

namespace
{
Expand Down Expand Up @@ -160,57 +161,46 @@ bool JoystickImpl::open(unsigned int index)
// Retrieve the axes mapping
ioctl(m_file, JSIOCGAXMAP, m_mapping);

// Get the name
char joyname[128];
if (ioctl(m_file, JSIOCGNAME(128), joyname) < 0) {
m_name = "Unknown Joystick";
} else {
m_name = joyname;
}

// Use udev to look up the product and manufacturer IDs
struct udev *udev = udev_new();
if (!udev)
if (udev)
{
err() << "Cannot create udev" << std::endl;
return false;
}
char sysname[32];
std::snprintf(sysname, sizeof(sysname), "js%u", index);
struct udev_device *dev = udev_device_new_from_subsystem_sysname(udev, "input", sysname);

struct udev_device *dev = udev_device_new_from_syspath(udev, name);
dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");

if (!dev)
{
err() << "Unable to find parent USB device" << std::endl;
}

dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
std::stringstream ss;
ss << std::hex << udev_device_get_sysattr_value(dev, "idVendor");
ss >> m_manufacturerID;

if (!dev)
ss.clear();
ss.str("");
ss << std::hex << udev_device_get_sysattr_value(dev, "idProduct");
ss >> m_productID;

udev_device_unref(dev);
udev_unref(udev);
}
else
{
err() << "Unable to find parent USB device" << std::endl;
return false;
err() << "Cannot create udev" << std::endl;
}

m_name = udev_device_get_sysattr_value(dev, "product");
char manID[32] = udev_device_get_sysattr_value(dev, "idVendor");
char prodID[32] = udev_device_get_sysattr_value(dev, "idProduct");

printf("NAME: %s\n", m_name.c_str());
printf("MAN: %s\n", manID);
printf("PROD: %s\n", prodID);

udev_device_unref(dev);
udev_unref(udev);

// // Get the name
// char joyname[128];
// if (ioctl(m_file, JSIOCGNAME(128), joyname) < 0) {
// m_name = "Unknown Joystick";
// } else {
// m_name = joyname;
// }

// // Get manufacturer and product IDs
// struct input_id inputID;
// if (ioctl(m_file, EVIOCGID, &inputID) < 0) {
// if (errno == EBADF) printf("EBADF\n");
// if (errno == EFAULT) printf("EFAULT\n");
// if (errno == ENOTTY) printf("ENOTTY\n");
// if (errno == EINVAL) printf("EINVAL\n");
// m_manufacturerID = 0;
// m_productID = 0;
// } else {
// m_manufacturerID = inputID.vendor;
// m_productID = inputID.product;
// }

// Reset the joystick state
m_state = JoystickState();

Expand Down

0 comments on commit 852b7b4

Please sign in to comment.