clinuxgamepadevdev

Modern way to read gamepad input with C on Linux


I am interested in reading gamepad input, specifically a gamepad with the layout of a wired xbox 360 controller, with C on a linux machine. I understand that this can be done in a variety of ways, however I was wondering what is the modern method of accomplishing this. As I understand it, evdev is the 'future' while the Joystick API is legacy. Using libudev I am able to find attached gamepads and detect when they are connected and disconnected:

// skipping setup code ....
char const* val = udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
if (val != NULL && strcmp(val, "1") == 0) {
  char const* devfs_path = udev_device_get_devnode(udev_device);
}
// .....
struct udev_device* device = udev_monitor_recieve_device(udev_monitor); 
char const* action = udev_device_get_action(device);
if (strcmp(action, "add") == 0) {
  // .....
}
if (strcmp(action, "remove") == 0) {
  // .....
}

The crux of my question lies in the best way to read button presses and axis movement. Currently, I am unsure whether to use struct js_event or struct input_event. In truth, I was hoping to be able to do this with the same check for connected and removed gamepads with libudev. If this is not possible, which is the most forward-thinking approach? Thanks.


Solution

  • According to the kernel docs

    Newer clients are encouraged to switch to the generic event (evdev) interface.

    The specific mappings for a desired controller can be found in linux input kernel community docs

    Therefore, use struct input_event