Autodetect endpoints

This commit is contained in:
Ingo Ruhnke 2009-01-20 08:42:07 +01:00
parent 96f8bb21c7
commit ba7c6dab55
2 changed files with 70 additions and 2 deletions

View file

@ -27,11 +27,32 @@
#include "xbox360_controller.hpp"
Xbox360Controller::Xbox360Controller(struct usb_device* dev, bool is_guitar)
: is_guitar(is_guitar),
: dev(dev),
is_guitar(is_guitar),
endpoint_in(1),
endpoint_out(2)
{
find_endpoints();
if (0)
{
std::cout << "EP(IN): " << endpoint_in << std::endl;
std::cout << "EP(OUT): " << endpoint_out << std::endl;
}
handle = usb_open(dev);
if (0)
{
int err;
if ((err = usb_set_configuration(handle, 0)) < 0)
{
std::ostringstream out;
out << "Error set USB configuration: " << strerror(-err) << std::endl
<< "Try to run 'rmmod xpad' and start xboxdrv again.";
throw std::runtime_error(out.str());
}
}
if (!handle)
{
throw std::runtime_error("Error opening Xbox360 controller");
@ -78,6 +99,52 @@ Xbox360Controller::~Xbox360Controller()
usb_close(handle);
}
void
Xbox360Controller::find_endpoints()
{
bool print_debug = false;
if (print_debug)
std::cout << "find_endpoints: numcfg: " << (int)dev->descriptor.bNumConfigurations << std::endl;
for(int i = 0; i < dev->descriptor.bNumConfigurations; ++i)
{
if (print_debug)
std::cout << "Configuration: " << i << std::endl;
for(int j = 0; j < dev->config[i].bNumInterfaces; ++j)
{
if (print_debug)
std::cout << " Interface " << j << ":" << std::endl;
for(int k = 0; k < dev->config[i].interface[j].num_altsetting; ++k)
{
for(int l = 0; l < dev->config[i].interface[j].altsetting[k].bNumEndpoints; ++l)
{
if (dev->config[i].interface[j].altsetting[k].bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
dev->config[i].interface[j].altsetting[k].bInterfaceSubClass == 93 &&
dev->config[i].interface[j].altsetting[k].bInterfaceProtocol == 1)
{
if (dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK)
{
endpoint_in = int(dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK);
}
else
{
endpoint_out = int(dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK);
}
}
if (print_debug)
std::cout << " Endpoint: "
<< int(dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK)
<< ((dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? " (IN)" : " (OUT)")
<< std::endl;
}
}
}
}
}
void
Xbox360Controller::set_rumble(uint8_t left, uint8_t right)
{

View file

@ -28,14 +28,15 @@ struct XPadDevice;
class Xbox360Controller : public XboxGenericController
{
private:
bool is_guitar;
struct usb_device* dev;
bool is_guitar;
XPadDevice* dev_type;
struct usb_dev_handle* handle;
int endpoint_in;
int endpoint_out;
void find_endpoints();
public:
Xbox360Controller(struct usb_device* dev, bool is_guitar);
~Xbox360Controller();