Skip to content

Instantly share code, notes, and snippets.

@computerquip
Last active August 29, 2015 14:05
Show Gist options
  • Save computerquip/efc53e7bd010f2e8e75f to your computer and use it in GitHub Desktop.
Save computerquip/efc53e7bd010f2e8e75f to your computer and use it in GitHub Desktop.
Xbox 360 Wired Controller driver using libusb-modified
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
#include <linux/uinput.h>
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION < 0x01000102)
#error This driver requires hotplug support. libusb headers indicate the version you have is not new enough.
#endif
struct xpad360_controller {
libusb_device *device;
};
int xpad360_connect(libusb_context *ctx, libusb_device *device)
{
struct xpad360_controller *controller = calloc(1, sizeof(struct xpad360_controller));
if (!controller) {
printf("Failed to allocate resources for controller!\n");
return 1;
}
controller->device = device;
printf("connected!\n");
libusb_set_device_user_data(device, controller);
return 0;
}
void xpad360_disconnect(libusb_context *ctx, libusb_device *device)
{
struct xpad360_controller *controller = libusb_get_device_user_data(device);
printf("disconnect!\n");
free(controller);
}
libusb_hotplug_driver driver = {
.connect = xpad360_connect,
.disconnect = xpad360_disconnect,
.vid = 0x045e, .pid = 0x028e,
.dev_class = LIBUSB_HOTPLUG_MATCH_ANY,
.flags = LIBUSB_HOTPLUG_ENUMERATE
};
int main()
{
int error = 0;
libusb_context *usb_ctx;
error = libusb_init(&usb_ctx);
if (error) {
printf("Failed to initialize libusb: %s\n", libusb_error_name(error));
return 1;
}
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
printf("libusb does not support hotplug for this system.\n");
goto hotplug_fail;
}
error = libusb_hotplug_register(usb_ctx, &driver);
if (error) goto hotplug_fail;
for (int i = 0; i < 6; ++i)
libusb_handle_events(usb_ctx);
goto success;
hotplug_fail:
printf("Failed to register hotplug driver: %s\n", libusb_error_name(error));
success:
libusb_hotplug_deregister(usb_ctx, &driver);
libusb_exit(usb_ctx);
return error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment