xboxdrv/xboxdrv.cpp

1480 lines
46 KiB
C++
Raw Normal View History

2008-04-11 05:43:15 -06:00
/*
** Xbox/Xbox360 USB Gamepad Userspace Driver
2008-04-11 05:43:15 -06:00
** Copyright (C) 2008 Ingo Ruhnke <grumbel@gmx.de>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2008-04-10 14:04:38 -06:00
#include <signal.h>
2008-04-10 11:33:56 -06:00
#include <boost/format.hpp>
#include <usb.h>
#include <unistd.h>
2008-04-10 10:38:50 -06:00
#include <iostream>
2008-04-10 15:43:47 -06:00
#include "uinput.hpp"
#include "xboxmsg.hpp"
2008-05-01 03:05:02 -06:00
#include "xbox_controller.hpp"
#include "xbox360_controller.hpp"
#include "xbox360_wireless_controller.hpp"
2008-05-02 15:07:06 -06:00
#include "helper.hpp"
2008-05-01 03:05:02 -06:00
#include "xbox_generic_controller.hpp"
2008-04-13 12:19:54 -06:00
#include "xboxdrv.hpp"
2008-04-10 10:38:50 -06:00
// Some ugly global variables, needed for sigint catching
bool global_exit_xboxdrv = false;
XboxGenericController* global_controller = 0;
XPadDevice xpad_devices[] = {
// Evil?! Anymore info we could use to identify the devices?
// { GAMEPAD_XBOX, 0x0000, 0x0000, "Generic X-Box pad" },
// { GAMEPAD_XBOX, 0xffff, 0xffff, "Chinese-made Xbox Controller" },
// These should work
{ GAMEPAD_XBOX, 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)" },
{ GAMEPAD_XBOX, 0x045e, 0x0285, "Microsoft X-Box pad (Japan)" },
{ GAMEPAD_XBOX, 0x045e, 0x0285, "Microsoft Xbox Controller S" },
{ GAMEPAD_XBOX, 0x045e, 0x0287, "Microsoft Xbox Controller S" },
2008-04-12 07:12:58 -06:00
{ GAMEPAD_XBOX, 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)" }, // duplicate
{ GAMEPAD_XBOX, 0x045e, 0x0289, "Microsoft Xbox Controller S" }, // duplicate
{ GAMEPAD_XBOX, 0x046d, 0xca84, "Logitech Xbox Cordless Controller" },
{ GAMEPAD_XBOX, 0x046d, 0xca88, "Logitech Compact Controller for Xbox" },
{ GAMEPAD_XBOX, 0x05fd, 0x1007, "Mad Catz Controller (unverified)" },
{ GAMEPAD_XBOX, 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)" },
{ GAMEPAD_XBOX, 0x0738, 0x4516, "Mad Catz Control Pad" },
{ GAMEPAD_XBOX, 0x0738, 0x4522, "Mad Catz LumiCON" },
{ GAMEPAD_XBOX, 0x0738, 0x4526, "Mad Catz Control Pad Pro" },
{ GAMEPAD_XBOX, 0x0738, 0x4536, "Mad Catz MicroCON" },
{ GAMEPAD_XBOX, 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller" },
{ GAMEPAD_XBOX, 0x0c12, 0x8802, "Zeroplus Xbox Controller" },
{ GAMEPAD_XBOX, 0x0c12, 0x8810, "Zeroplus Xbox Controller" },
{ GAMEPAD_XBOX, 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*" },
{ GAMEPAD_XBOX, 0x0e4c, 0x1097, "Radica Gamester Controller" },
{ GAMEPAD_XBOX, 0x0e4c, 0x2390, "Radica Games Jtech Controller" },
{ GAMEPAD_XBOX, 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller" },
{ GAMEPAD_XBOX, 0x0e6f, 0x0005, "Eclipse wireless Controller" },
{ GAMEPAD_XBOX, 0x0e6f, 0x0006, "Edge wireless Controller" },
{ GAMEPAD_XBOX, 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor" },
{ GAMEPAD_XBOX, 0x0f30, 0x0202, "Joytech Advanced Controller" },
{ GAMEPAD_XBOX, 0x0f30, 0x8888, "BigBen XBMiniPad Controller" },
{ GAMEPAD_XBOX, 0x102c, 0xff0c, "Joytech Wireless Advanced Controller" },
{ GAMEPAD_XBOX, 0x044f, 0x0f07, "Thrustmaster, Inc. Controller" },
{ GAMEPAD_XBOX360, 0x045e, 0x028e, "Microsoft Xbox 360 Controller" },
{ GAMEPAD_XBOX360, 0x0738, 0x4716, "Mad Catz Xbox 360 Controller" },
{ GAMEPAD_XBOX360, 0x162e, 0xbeef, "Joytech Neo-Se Take2" },
2008-04-17 12:31:50 -06:00
{ GAMEPAD_XBOX360_GUITAR, 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer" },
{ GAMEPAD_XBOX360_WIRELESS, 0x045e, 0x0291, "Microsoft Xbox 360 Wireless Controller" },
{ GAMEPAD_XBOX360_WIRELESS, 0x045e, 0x0719, "Microsoft Xbox 360 Wireless Controller (PC)" },
2008-04-11 19:49:33 -06:00
{ GAMEPAD_XBOX_MAT, 0x0738, 0x4540, "Mad Catz Beat Pad" },
{ GAMEPAD_XBOX_MAT, 0x0738, 0x6040, "Mad Catz Beat Pad Pro" },
{ GAMEPAD_XBOX_MAT, 0x0c12, 0x8809, "RedOctane Xbox Dance Pad" },
{ GAMEPAD_XBOX_MAT, 0x12ab, 0x8809, "Xbox DDR dancepad" },
{ GAMEPAD_XBOX_MAT, 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)" },
};
const int xpad_devices_count = sizeof(xpad_devices)/sizeof(XPadDevice);
2008-05-02 15:07:06 -06:00
XboxButton string2btn(const std::string& str_);
XboxAxis string2axis(const std::string& str_);
2008-05-02 15:07:06 -06:00
std::ostream& operator<<(std::ostream& out, const GamepadType& type)
{
switch (type)
{
case GAMEPAD_XBOX360:
2008-04-24 11:12:59 -06:00
return out << "Xbox360";
case GAMEPAD_XBOX360_WIRELESS:
2008-04-24 11:12:59 -06:00
return out << "Xbox360 (wireless)";
case GAMEPAD_XBOX:
2008-04-24 11:12:59 -06:00
return out << "Xbox Classic";
case GAMEPAD_XBOX_MAT:
2008-04-24 11:12:59 -06:00
return out << "Xbox Dancepad";
2008-04-17 15:02:28 -06:00
case GAMEPAD_XBOX360_GUITAR:
2008-04-24 11:12:59 -06:00
return out << "Xbox360 Guitar";
2008-04-17 15:02:28 -06:00
default:
return out << "unknown" << std::endl;
}
}
2008-05-02 15:07:06 -06:00
int get_button(XboxGenericMsg& msg, XboxButton button)
{
switch(msg.type)
{
case GAMEPAD_XBOX360_GUITAR:
case GAMEPAD_XBOX360:
case GAMEPAD_XBOX360_WIRELESS:
switch(button)
{
case XBOX_BTN_START:
return msg.xbox360.start;
case XBOX_BTN_GUIDE:
return msg.xbox360.guide;
case XBOX_BTN_BACK:
return msg.xbox360.back;
case XBOX_BTN_A:
return msg.xbox360.a;
case XBOX_BTN_B:
return msg.xbox360.b;
case XBOX_BTN_X:
return msg.xbox360.x;
case XBOX_BTN_Y:
return msg.xbox360.y;
case XBOX_BTN_LB:
case XBOX_BTN_WHITE:
return msg.xbox360.lb;
case XBOX_BTN_RB:
case XBOX_BTN_BLACK:
return msg.xbox360.rb;
case XBOX_BTN_LT:
return msg.xbox360.lt;
case XBOX_BTN_RT:
return msg.xbox360.rt;
case XBOX_BTN_THUMB_L:
return msg.xbox360.thumb_l;
case XBOX_BTN_THUMB_R:
return msg.xbox360.thumb_r;
case XBOX_DPAD_UP:
return msg.xbox360.dpad_up;
case XBOX_DPAD_DOWN:
return msg.xbox360.dpad_down;
case XBOX_DPAD_LEFT:
return msg.xbox360.dpad_left;
case XBOX_DPAD_RIGHT:
return msg.xbox360.dpad_right;
case XBOX_BTN_UNKNOWN:
return 0;
}
break;
case GAMEPAD_XBOX:
case GAMEPAD_XBOX_MAT:
switch(button)
{
case XBOX_BTN_START:
return msg.xbox.start;
case XBOX_BTN_GUIDE:
return 0;
case XBOX_BTN_BACK:
return msg.xbox.back;
case XBOX_BTN_A:
return msg.xbox.a;
case XBOX_BTN_B:
return msg.xbox.b;
case XBOX_BTN_X:
return msg.xbox.x;
case XBOX_BTN_Y:
return msg.xbox.y;
case XBOX_BTN_LB:
case XBOX_BTN_WHITE:
return msg.xbox.white;
case XBOX_BTN_RB:
case XBOX_BTN_BLACK:
return msg.xbox.black;
case XBOX_BTN_LT:
return msg.xbox.lt;
case XBOX_BTN_RT:
return msg.xbox.rt;
case XBOX_BTN_THUMB_L:
return msg.xbox.thumb_l;
case XBOX_BTN_THUMB_R:
return msg.xbox.thumb_r;
case XBOX_DPAD_UP:
return msg.xbox.dpad_up;
case XBOX_DPAD_DOWN:
return msg.xbox.dpad_down;
case XBOX_DPAD_LEFT:
return msg.xbox.dpad_left;
case XBOX_DPAD_RIGHT:
return msg.xbox.dpad_right;
case XBOX_BTN_UNKNOWN:
return 0;
}
break;
case GAMEPAD_UNKNOWN:
break;
}
return 0;
}
void set_button(XboxGenericMsg& msg, XboxButton button, int v)
{
switch(msg.type)
{
case GAMEPAD_XBOX360_GUITAR:
case GAMEPAD_XBOX360:
case GAMEPAD_XBOX360_WIRELESS:
switch(button)
{
case XBOX_BTN_START:
msg.xbox360.start = v; break;
case XBOX_BTN_GUIDE:
msg.xbox360.guide = v; break;
case XBOX_BTN_BACK:
msg.xbox360.back = v; break;
case XBOX_BTN_A:
msg.xbox360.a = v; break;
case XBOX_BTN_B:
msg.xbox360.b = v; break;
case XBOX_BTN_X:
msg.xbox360.x = v; break;
case XBOX_BTN_Y:
msg.xbox360.y = v; break;
case XBOX_BTN_LB:
case XBOX_BTN_WHITE:
msg.xbox360.lb = v; break;
case XBOX_BTN_RB:
case XBOX_BTN_BLACK:
msg.xbox360.rb = v; break;
case XBOX_BTN_LT:
msg.xbox360.lt = v; break;
case XBOX_BTN_RT:
msg.xbox360.rt = v; break;
case XBOX_BTN_THUMB_L:
msg.xbox360.thumb_l = v; break;
case XBOX_BTN_THUMB_R:
msg.xbox360.thumb_r = v; break;
case XBOX_DPAD_UP:
msg.xbox360.dpad_up = v; break;
case XBOX_DPAD_DOWN:
msg.xbox360.dpad_down = v; break;
case XBOX_DPAD_LEFT:
msg.xbox360.dpad_left = v; break;
case XBOX_DPAD_RIGHT:
msg.xbox360.dpad_right = v; break;
case XBOX_BTN_UNKNOWN:
break;
}
break;
case GAMEPAD_XBOX:
case GAMEPAD_XBOX_MAT:
switch(button)
{
case XBOX_BTN_START:
msg.xbox.start = v; break;
case XBOX_BTN_GUIDE:
break;
case XBOX_BTN_BACK:
msg.xbox.back = v; break;
case XBOX_BTN_A:
msg.xbox.a = v; break;
case XBOX_BTN_B:
msg.xbox.b = v; break;
case XBOX_BTN_X:
msg.xbox.x = v; break;
case XBOX_BTN_Y:
msg.xbox.y = v; break;
case XBOX_BTN_LB:
case XBOX_BTN_WHITE:
msg.xbox.white = v; break;
case XBOX_BTN_RB:
case XBOX_BTN_BLACK:
msg.xbox.black = v; break;
case XBOX_BTN_LT:
msg.xbox.lt = v; break;
case XBOX_BTN_RT:
msg.xbox.rt = v; break;
case XBOX_BTN_THUMB_L:
msg.xbox.thumb_l = v; break;
case XBOX_BTN_THUMB_R:
msg.xbox.thumb_r = v; break;
case XBOX_DPAD_UP:
msg.xbox.dpad_up = v; break;
case XBOX_DPAD_DOWN:
msg.xbox.dpad_down = v; break;
case XBOX_DPAD_LEFT:
msg.xbox.dpad_left = v; break;
case XBOX_DPAD_RIGHT:
msg.xbox.dpad_right = v; break;
case XBOX_BTN_UNKNOWN:
break;
}
break;
case GAMEPAD_UNKNOWN:
break;
}
}
int get_axis(XboxGenericMsg& msg, XboxAxis axis)
{
switch(msg.type)
{
case GAMEPAD_XBOX360_GUITAR:
case GAMEPAD_XBOX360:
case GAMEPAD_XBOX360_WIRELESS:
switch(axis)
{
case XBOX_AXIS_UNKNOWN:
return 0;
case XBOX_AXIS_X1:
return msg.xbox360.x1;
case XBOX_AXIS_Y1:
return msg.xbox360.y1;
case XBOX_AXIS_X2:
return msg.xbox360.x2;
case XBOX_AXIS_Y2:
return msg.xbox360.y2;
case XBOX_AXIS_LT:
return msg.xbox360.lt;
case XBOX_AXIS_RT:
return msg.xbox360.rt;
}
break;
case GAMEPAD_XBOX:
case GAMEPAD_XBOX_MAT:
switch(axis)
{
case XBOX_AXIS_UNKNOWN:
return 0;
case XBOX_AXIS_X1:
return msg.xbox.x1;
case XBOX_AXIS_Y1:
return msg.xbox.y1;
case XBOX_AXIS_X2:
return msg.xbox.x2;
case XBOX_AXIS_Y2:
return msg.xbox.y2;
case XBOX_AXIS_LT:
return msg.xbox.lt;
case XBOX_AXIS_RT:
return msg.xbox.rt;
}
break;
case GAMEPAD_UNKNOWN:
break;
}
return 0;
}
void set_axis(XboxGenericMsg& msg, XboxAxis axis, int v)
{
switch(msg.type)
{
case GAMEPAD_XBOX360_GUITAR:
case GAMEPAD_XBOX360:
case GAMEPAD_XBOX360_WIRELESS:
switch(axis)
{
case XBOX_AXIS_UNKNOWN:
break;
case XBOX_AXIS_X1:
msg.xbox360.x1 = v; break;
case XBOX_AXIS_Y1:
msg.xbox360.y1 = v; break;
case XBOX_AXIS_X2:
msg.xbox360.x2 = v; break;
case XBOX_AXIS_Y2:
msg.xbox360.y2 = v; break;
case XBOX_AXIS_LT:
msg.xbox360.lt = v; break;
case XBOX_AXIS_RT:
msg.xbox360.rt = v; break;
}
break;
case GAMEPAD_XBOX:
case GAMEPAD_XBOX_MAT:
switch(axis)
{
case XBOX_AXIS_UNKNOWN:
break;
case XBOX_AXIS_X1:
msg.xbox.x1 = v; break;
case XBOX_AXIS_Y1:
msg.xbox.y1 = v; break;
case XBOX_AXIS_X2:
msg.xbox.x2 = v; break;
case XBOX_AXIS_Y2:
msg.xbox.y2 = v; break;
case XBOX_AXIS_LT:
msg.xbox.lt = v; break;
case XBOX_AXIS_RT:
msg.xbox.rt = v; break;
}
break;
case GAMEPAD_UNKNOWN:
break;
}
}
void apply_button_map(XboxGenericMsg& msg, std::vector<ButtonMapping>& lst)
{
XboxGenericMsg newmsg = msg;
for(std::vector<ButtonMapping>::iterator i = lst.begin(); i != lst.end(); ++i)
set_button(newmsg, i->lhs, 0);
for(std::vector<ButtonMapping>::iterator i = lst.begin(); i != lst.end(); ++i)
set_button(newmsg, i->rhs, get_button(msg, i->lhs) || get_button(newmsg, i->rhs));
msg = newmsg;
}
void apply_axis_map(XboxGenericMsg& msg, std::vector<AxisMapping>& lst)
{
2008-05-02 15:39:26 -06:00
XboxGenericMsg newmsg = msg;
2008-05-02 15:07:06 -06:00
for(std::vector<AxisMapping>::iterator i = lst.begin(); i != lst.end(); ++i)
{
2008-05-02 15:39:26 -06:00
set_axis(newmsg, i->lhs, 0);
}
for(std::vector<AxisMapping>::iterator i = lst.begin(); i != lst.end(); ++i)
{
int lhs = get_axis(msg, i->lhs);
int nrhs = get_axis(newmsg, i->rhs);
2008-05-02 15:07:06 -06:00
if (i->invert)
2008-05-02 15:39:26 -06:00
{
if (i->lhs == XBOX_AXIS_LT ||
i->lhs == XBOX_AXIS_RT)
{
lhs = 255 - lhs;
}
else
{
lhs = -lhs;
}
}
set_axis(newmsg, i->rhs, std::max(std::min(nrhs + lhs, 32767), -32768));
2008-05-02 15:07:06 -06:00
}
msg = newmsg;
}
ButtonMapping string2buttonmapping(const std::string& str)
{
for(std::string::const_iterator i = str.begin(); i != str.end(); ++i)
{
if (*i == '=')
{
ButtonMapping mapping;
mapping.lhs = string2btn(std::string(str.begin(), i));
mapping.rhs = string2btn(std::string(i+1, str.end()));
if (mapping.lhs == XBOX_BTN_UNKNOWN ||
mapping.rhs == XBOX_BTN_UNKNOWN)
throw std::runtime_error("Couldn't convert string \"" + str + "\" to button mapping");
return mapping;
}
}
throw std::runtime_error("Couldn't convert string \"" + str + "\" to button mapping");
}
void string2buttonmap(const std::string& str, std::vector<ButtonMapping>& lst)
{
std::string::const_iterator start = str.begin();
for(std::string::const_iterator i = str.begin(); i != str.end(); ++i)
{
if (*i == ',')
{
if (i != start)
{
ButtonMapping mapping = string2buttonmapping(std::string(start, i));
lst.push_back(mapping);
}
start = i+1;
}
}
if (start != str.end())
{
ButtonMapping mapping = string2buttonmapping(std::string(start, str.end()));
lst.push_back(mapping);
}
}
2008-05-02 15:39:26 -06:00
AxisMapping string2axismapping(const std::string& str)
{
for(std::string::const_iterator i = str.begin(); i != str.end(); ++i)
{
if (*i == '=')
{
AxisMapping mapping;
std::string lhs(str.begin(), i);
std::string rhs(i+1, str.end());
if (lhs.empty() || rhs.empty())
throw std::runtime_error("Couldn't convert string \"" + str + "\" to axis mapping");
if (lhs[0] == '-')
{
mapping.invert = true;
mapping.lhs = string2axis(lhs.substr(1));
}
else
{
mapping.invert = false;
mapping.lhs = string2axis(lhs);
}
mapping.rhs = string2axis(rhs);
if (mapping.lhs == XBOX_AXIS_UNKNOWN ||
mapping.rhs == XBOX_AXIS_UNKNOWN)
throw std::runtime_error("Couldn't convert string \"" + str + "\" to axis mapping");
return mapping;
}
}
throw std::runtime_error("Couldn't convert string \"" + str + "\" to axis mapping");
}
2008-05-02 15:07:06 -06:00
void string2axismap(const std::string& str, std::vector<AxisMapping>& lst)
{
2008-05-02 15:39:26 -06:00
std::string::const_iterator start = str.begin();
for(std::string::const_iterator i = str.begin(); i != str.end(); ++i)
{
if (*i == ',')
{
if (i != start)
{
AxisMapping mapping = string2axismapping(std::string(start, i));
lst.push_back(mapping);
}
start = i+1;
}
}
if (start != str.end())
{
AxisMapping mapping = string2axismapping(std::string(start, str.end()));
lst.push_back(mapping);
}
2008-05-02 15:07:06 -06:00
}
XboxButton string2btn(const std::string& str_)
{
std::string str = to_lower(str_);
if (str == "start")
return XBOX_BTN_START;
else if (str == "guide")
return XBOX_BTN_GUIDE;
else if (str == "back")
return XBOX_BTN_BACK;
else if (str == "a")
return XBOX_BTN_A;
else if (str == "b")
return XBOX_BTN_B;
else if (str == "x")
return XBOX_BTN_X;
else if (str == "y")
return XBOX_BTN_Y;
else if (str == "black")
return XBOX_BTN_BLACK;
else if (str == "white")
return XBOX_BTN_WHITE;
else if (str == "lb")
return XBOX_BTN_LB;
else if (str == "rb")
return XBOX_BTN_RB;
else if (str == "lt")
return XBOX_BTN_LT;
else if (str == "rt")
return XBOX_BTN_RT;
else if (str == "tl")
return XBOX_BTN_THUMB_L;
else if (str == "tr")
return XBOX_BTN_THUMB_R;
else if (str == "du")
return XBOX_DPAD_UP;
else if (str == "dd")
return XBOX_DPAD_DOWN;
else if (str == "dl")
return XBOX_DPAD_LEFT;
else if (str == "dr")
return XBOX_DPAD_RIGHT;
else
return XBOX_BTN_UNKNOWN;
}
XboxAxis string2axis(const std::string& str_)
{
std::string str = to_lower(str_);
if (str == "x1")
return XBOX_AXIS_X1;
else if (str == "y1")
2008-05-02 15:39:26 -06:00
return XBOX_AXIS_Y1;
2008-05-02 15:07:06 -06:00
else if (str == "x2")
return XBOX_AXIS_X2;
else if (str == "y2")
return XBOX_AXIS_Y2;
else if (str == "lt")
return XBOX_AXIS_LT;
else if (str == "rt")
return XBOX_AXIS_RT;
else
return XBOX_AXIS_UNKNOWN;
}
void list_controller()
2008-04-10 13:39:08 -06:00
{
struct usb_bus* busses = usb_get_busses();
int id = 0;
2008-05-01 03:05:02 -06:00
std::cout << " id | wid | idVendor | idProduct | Name" << std::endl;
std::cout << "----+-----+----------+-----------+--------------------------------------" << std::endl;
for (struct usb_bus* bus = busses; bus; bus = bus->next)
{
for (struct usb_device* dev = bus->devices; dev; dev = dev->next)
{
for(int i = 0; i < xpad_devices_count; ++i)
{
if (dev->descriptor.idVendor == xpad_devices[i].idVendor &&
dev->descriptor.idProduct == xpad_devices[i].idProduct)
{
2008-05-01 03:05:02 -06:00
if (xpad_devices[i].type == GAMEPAD_XBOX360_WIRELESS)
{
2008-05-01 03:43:11 -06:00
for(int wid = 0; wid < 4; ++wid)
2008-05-01 03:05:02 -06:00
{
std::cout << boost::format(" %2d | %2d | 0x%04x | 0x%04x | %s (Port: %s)")
% id
% wid
% int(xpad_devices[i].idVendor)
% int(xpad_devices[i].idProduct)
% xpad_devices[i].name
% wid
<< std::endl;
}
}
else
{
std::cout << boost::format(" %2d | %2d | 0x%04x | 0x%04x | %s")
% id
% 0
% int(xpad_devices[i].idVendor)
% int(xpad_devices[i].idProduct)
% xpad_devices[i].name
<< std::endl;
}
id += 1;
break;
}
}
}
}
if (id == 0)
std::cout << "\nNo controller detected" << std::endl;
}
bool find_controller_by_path(const char* busid, const char* devid,struct usb_device** xbox_device)
2008-04-19 22:58:20 -06:00
{
struct usb_bus* busses = usb_get_busses();
for (struct usb_bus* bus = busses; bus; bus = bus->next)
{
if (strcmp(bus->dirname, busid) == 0)
{
for (struct usb_device* dev = bus->devices; dev; dev = dev->next)
{
if (strcmp(dev->filename, devid) == 0)
{
*xbox_device = dev;
return true;
}
}
}
}
return 0;
}
/** find the number of the next unused /dev/input/jsX device */
int find_jsdev_number()
{
for(int i = 0; ; ++i)
{
char filename1[32];
char filename2[32];
2008-04-19 22:58:20 -06:00
sprintf(filename1, "/dev/input/js%d", i);
sprintf(filename2, "/dev/js%d", i);
if (access(filename1, F_OK) != 0 && access(filename2, F_OK) != 0)
return i;
}
}
/** find the number of the next unused /dev/input/eventX device */
int find_evdev_number()
{
for(int i = 0; ; ++i)
{
char filename[32];
sprintf(filename, "/dev/input/event%d", i);
if (access(filename, F_OK) != 0)
return i;
}
}
bool find_xbox360_controller(int id, struct usb_device** xbox_device, XPadDevice* type)
{
struct usb_bus* busses = usb_get_busses();
int id_count = 0;
for (struct usb_bus* bus = busses; bus; bus = bus->next)
2008-04-10 13:39:08 -06:00
{
for (struct usb_device* dev = bus->devices; dev; dev = dev->next)
{
if (0)
std::cout << (boost::format("UsbDevice: idVendor: 0x%04x idProduct: 0x%04x")
% int(dev->descriptor.idProduct)
% int(dev->descriptor.idVendor))
2008-04-10 13:39:08 -06:00
<< std::endl;
2008-04-10 11:33:56 -06:00
for(int i = 0; i < xpad_devices_count; ++i)
{
if (dev->descriptor.idVendor == xpad_devices[i].idVendor &&
dev->descriptor.idProduct == xpad_devices[i].idProduct)
{
if (id_count == id)
{
*xbox_device = dev;
*type = xpad_devices[i];
return true;
}
else
{
id_count += 1;
2008-04-12 07:12:58 -06:00
break;
}
}
}
2008-04-10 13:39:08 -06:00
}
}
return 0;
}
2008-04-20 19:00:51 -06:00
struct CommandLineOptions
2008-04-10 10:38:50 -06:00
{
2008-05-02 05:30:19 -06:00
bool silent;
2008-04-20 19:00:51 -06:00
bool rumble;
int led;
2008-04-20 19:00:51 -06:00
int rumble_l;
int rumble_r;
int controller_id;
2008-05-01 03:05:02 -06:00
int wireless_id;
2008-04-20 19:00:51 -06:00
bool instant_exit;
bool no_uinput;
2008-04-20 19:19:02 -06:00
GamepadType gamepad_type;
2008-04-20 19:00:51 -06:00
char busid[4];
char devid[4];
2008-04-19 22:58:20 -06:00
uInputCfg uinput_config;
2008-04-20 19:19:02 -06:00
int deadzone;
2008-05-02 15:07:06 -06:00
std::vector<ButtonMapping> button_map;
std::vector<AxisMapping> axis_map;
2008-04-20 19:00:51 -06:00
CommandLineOptions() {
2008-05-02 05:30:19 -06:00
silent = false;
2008-04-20 19:19:02 -06:00
rumble = false;
led = -1;
2008-04-20 19:00:51 -06:00
rumble_l = 0;
rumble_r = 0;
controller_id = 0;
2008-05-01 03:05:02 -06:00
wireless_id = 0;
2008-04-20 19:00:51 -06:00
instant_exit = false;
no_uinput = false;
2008-04-20 19:19:02 -06:00
gamepad_type = GAMEPAD_UNKNOWN;
2008-04-20 19:00:51 -06:00
busid[0] = '\0';
devid[0] = '\0';
2008-04-20 19:19:02 -06:00
deadzone = 0;
2008-04-20 19:00:51 -06:00
}
};
void print_command_line_help(int argc, char** argv)
{
std::cout << "Usage: " << argv[0] << " [OPTION]..." << std::endl;
2008-04-24 11:12:59 -06:00
std::cout << "Xbox360 USB Gamepad Userspace Driver" << std::endl;
2008-04-20 19:00:51 -06:00
std::cout << std::endl;
std::cout << "General Options: " << std::endl;
std::cout << " -h, --help display this help and exit" << std::endl;
std::cout << " --help-led list possible values for the led" << std::endl;
std::cout << " --help-devices list supported devices" << std::endl;
2008-05-02 05:30:19 -06:00
std::cout << " -s, --silent do not display events on console" << std::endl;
2008-05-01 03:05:02 -06:00
std::cout << " -i, --id N use controller with id N (default: 0)" << std::endl;
std::cout << " -w, --wid N use wireless controller with wid N (default: 0)" << std::endl;
2008-04-20 19:00:51 -06:00
std::cout << " -L, --list-controller list available controllers" << std::endl;
std::cout << " -R, --test-rumble map rumbling to LT and RT (for testing only)" << std::endl;
std::cout << " --no-uinput do not try to start uinput event dispatching" << std::endl;
std::cout << std::endl;
std::cout << "Device Options: " << std::endl;
std::cout << " -d, --device BUS:DEV Use device BUS:DEV, do not do any scanning" << std::endl;
2008-04-20 19:00:51 -06:00
std::cout << std::endl;
std::cout << "Status Options: " << std::endl;
std::cout << " -l, --led NUM set LED status, see --list-led-values (default: 0)" << std::endl;
std::cout << " -r, --rumble L,R set the speed for both rumble motors [0-255] (default: 0,0)" << std::endl;
std::cout << " -q, --quit only set led and rumble status then quit" << std::endl;
std::cout << std::endl;
std::cout << "Configuration Options: " << std::endl;
2008-04-20 19:19:02 -06:00
std::cout << " --deadzone INT Threshold under which axis events are ignored (default: 0)" << std::endl;
2008-04-20 19:00:51 -06:00
std::cout << " --trigger-as-button LT and RT send button instead of axis events" << std::endl;
std::cout << " --trigger-as-zaxis Combine LT and RT to form a zaxis instead" << std::endl;
std::cout << " --dpad-as-button DPad sends button instead of axis events" << std::endl;
std::cout << " --type TYPE Ignore autodetection and enforce controller type\n"
<< " (xbox, xbox-mat, xbox360, xbox360-wireless, xbox360-guitar)" << std::endl;
2008-05-02 15:07:06 -06:00
std::cout << " -b, --buttonmap MAP Remap the buttons as specified by MAP" << std::endl;
std::cout << " -a, --axismap MAP Remap the axis as specified by MAP" << std::endl;
2008-04-20 19:00:51 -06:00
std::cout << std::endl;
std::cout << "Report bugs to Ingo Ruhnke <grumbel@gmx.de>" << std::endl;
}
void print_led_help()
{
2008-05-01 03:05:02 -06:00
std::cout <<
"Possible values for '--led VALUE' are:\n\n"
" 0: off\n"
" 1: all blinking\n"
" 2: 1/top-left blink, then on\n"
" 3: 2/top-right blink, then on\n"
" 4: 3/bottom-left blink, then on\n"
" 5: 4/bottom-right blink, then on\n"
" 6: 1/top-left on\n"
" 7: 2/top-right on\n"
" 8: 3/bottom-left on\n"
" 9: 4/bottom-right on\n"
" 10: rotate\n"
" 11: blink\n"
" 12: blink slower\n"
" 13: rotate with two lights\n"
" 14: blink\n"
" 15: blink once\n"
<< std::endl;
2008-04-20 19:00:51 -06:00
}
void parse_command_line(int argc, char** argv, CommandLineOptions& opts)
{
2008-04-11 05:43:15 -06:00
for(int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "--help") == 0)
{
2008-04-20 19:00:51 -06:00
print_command_line_help(argc, argv);
exit(EXIT_SUCCESS);
2008-04-11 05:43:15 -06:00
}
2008-05-02 05:30:19 -06:00
else if (strcmp(argv[i], "-s") == 0 ||
strcmp(argv[i], "--silent") == 0)
2008-04-11 05:43:15 -06:00
{
2008-05-02 05:30:19 -06:00
opts.silent = true;
2008-04-11 05:43:15 -06:00
}
2008-04-19 22:58:20 -06:00
else if (strcmp(argv[i], "--test-rumble") == 0 ||
strcmp(argv[i], "-R") == 0)
{
2008-04-20 19:00:51 -06:00
opts.rumble = true;
}
2008-04-11 05:43:15 -06:00
else if (strcmp(argv[i], "-r") == 0 ||
strcmp(argv[i], "--rumble") == 0)
{
++i;
if (i < argc)
{
2008-04-20 19:00:51 -06:00
if (sscanf(argv[i], "%d,%d", &opts.rumble_l, &opts.rumble_r) == 2)
2008-04-19 22:58:20 -06:00
{
2008-04-20 19:00:51 -06:00
opts.rumble_l = std::max(0, std::min(255, opts.rumble_l));
opts.rumble_r = std::max(0, std::min(255, opts.rumble_r));
2008-04-19 22:58:20 -06:00
}
else
{
2008-05-02 15:07:06 -06:00
std::cout << "Error: " << argv[i-1] << " expected an argument in form INT,INT" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-19 22:58:20 -06:00
}
}
else
{
2008-05-02 15:07:06 -06:00
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
}
}
else if (strcmp(argv[i], "-q") == 0 ||
strcmp(argv[i], "--quit") == 0)
{
2008-04-20 19:00:51 -06:00
opts.instant_exit = true;
2008-04-11 05:43:15 -06:00
}
2008-04-15 08:06:48 -06:00
else if (strcmp(argv[i], "--no-uinput") == 0)
{
2008-04-20 19:00:51 -06:00
opts.no_uinput = true;
2008-04-15 08:06:48 -06:00
}
2008-04-17 15:02:28 -06:00
else if (strcmp(argv[i], "-t") == 0 ||
strcmp(argv[i], "--type") == 0)
{
++i;
if (i < argc)
{
if (strcmp(argv[i], "xbox") == 0)
{
2008-04-20 19:00:51 -06:00
opts.gamepad_type = GAMEPAD_XBOX;
2008-04-17 15:02:28 -06:00
}
else if (strcmp(argv[i], "xbox-mat") == 0)
{
opts.gamepad_type = GAMEPAD_XBOX_MAT;
}
2008-04-17 15:02:28 -06:00
else if (strcmp(argv[i], "xbox360") == 0)
{
2008-04-20 19:00:51 -06:00
opts.gamepad_type = GAMEPAD_XBOX360;
2008-04-17 15:02:28 -06:00
}
else if (strcmp(argv[i], "xbox360-guitar") == 0)
{
2008-04-20 19:00:51 -06:00
opts.gamepad_type = GAMEPAD_XBOX360_GUITAR;
2008-04-17 15:02:28 -06:00
}
else if (strcmp(argv[i], "xbox360-wireless") == 0)
{
2008-04-20 19:00:51 -06:00
opts.gamepad_type = GAMEPAD_XBOX360_WIRELESS;
2008-04-17 15:02:28 -06:00
}
else
{
2008-04-19 22:58:20 -06:00
std::cout << "Error: unknown type: " << argv[i] << std::endl;
std::cout << "Possible types are:" << std::endl;
std::cout << " * xbox" << std::endl;
std::cout << " * xbox-emat" << std::endl;
2008-04-19 22:58:20 -06:00
std::cout << " * xbox360" << std::endl;
std::cout << " * xbox360-guitar" << std::endl;
std::cout << " * xbox360-wireless" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-17 15:02:28 -06:00
}
}
else
{
2008-05-02 15:07:06 -06:00
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-17 15:02:28 -06:00
}
}
2008-05-02 15:07:06 -06:00
else if (strcmp(argv[i], "-b") == 0 ||
strcmp(argv[i], "--buttonmap") == 0)
{
++i;
if (i < argc)
{
string2buttonmap(argv[i], opts.button_map);
}
else
{
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
exit(EXIT_FAILURE);
}
}
else if (strcmp(argv[i], "-a") == 0 ||
strcmp(argv[i], "--axismap") == 0)
{
++i;
if (i < argc)
{
string2axismap(argv[i], opts.axis_map);
}
else
{
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
exit(EXIT_FAILURE);
}
}
else if (strcmp(argv[i], "-i") == 0 ||
strcmp(argv[i], "--id") == 0)
{
++i;
if (i < argc)
{
2008-04-20 19:00:51 -06:00
opts.controller_id = atoi(argv[i]);
}
else
{
2008-05-02 15:07:06 -06:00
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
}
}
2008-05-01 03:05:02 -06:00
else if (strcmp(argv[i], "-w") == 0 ||
strcmp(argv[i], "--wid") == 0)
{
++i;
if (i < argc)
{
opts.wireless_id = atoi(argv[i]);
}
else
{
2008-05-02 15:07:06 -06:00
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
2008-05-01 03:05:02 -06:00
exit(EXIT_FAILURE);
}
}
2008-04-11 05:43:15 -06:00
else if (strcmp(argv[i], "-l") == 0 ||
strcmp(argv[i], "--led") == 0)
{
++i;
if (i < argc)
{
2008-04-20 19:00:51 -06:00
if (strcmp(argv[i], "help") == 0)
{
print_led_help();
exit(EXIT_SUCCESS);
}
else
{
opts.led = atoi(argv[i]);
}
2008-04-11 05:43:15 -06:00
}
else
{
2008-05-02 15:07:06 -06:00
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-11 05:43:15 -06:00
}
}
else if (strcmp("--dpad-as-button", argv[i]) == 0)
{
2008-04-20 19:00:51 -06:00
opts.uinput_config.dpad_as_button = true;
}
2008-04-20 19:19:02 -06:00
else if (strcmp("--deadzone", argv[i]) == 0)
{
++i;
if (i < argc)
{
opts.deadzone = atoi(argv[i]);
}
else
{
std::cout << "Error: " << argv[i-1] << " expected an INT argument" << std::endl;
exit(EXIT_FAILURE);
}
}
2008-04-13 08:56:31 -06:00
else if (strcmp("--trigger-as-button", argv[i]) == 0)
{
2008-04-20 19:00:51 -06:00
if (opts.uinput_config.trigger_as_zaxis)
2008-04-13 09:22:14 -06:00
{
std::cout << "Error: Can't combine --trigger-as-button and --trigger-as-zaxis" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-13 09:22:14 -06:00
}
else
{
2008-04-20 19:00:51 -06:00
opts.uinput_config.trigger_as_button = true;
2008-04-13 09:22:14 -06:00
}
}
else if (strcmp("--trigger-as-zaxis", argv[i]) == 0)
{
2008-04-20 19:00:51 -06:00
if (opts.uinput_config.trigger_as_button)
2008-04-13 09:22:14 -06:00
{
std::cout << "Error: Can't combine --trigger-as-button and --trigger-as-zaxis" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-13 09:22:14 -06:00
}
else
{
2008-04-20 19:00:51 -06:00
opts.uinput_config.trigger_as_zaxis = true;
2008-04-13 09:22:14 -06:00
}
2008-04-13 08:56:31 -06:00
}
else if (strcmp("--help-led", argv[i]) == 0)
{
2008-04-20 19:00:51 -06:00
print_led_help();
exit(EXIT_SUCCESS);
}
2008-04-19 22:58:20 -06:00
else if (strcmp(argv[i], "--device") == 0 ||
strcmp(argv[i], "-d") == 0)
{
++i;
if (i < argc)
{
2008-04-20 19:00:51 -06:00
if (sscanf(argv[i], "%3s:%3s", opts.busid, opts.devid) == 2)
2008-04-19 22:58:20 -06:00
{
std::cout << " ***************************************" << std::endl;
std::cout << " *** WARNING *** WARNING *** WARNING ***" << std::endl;
std::cout << " ***************************************" << std::endl;
std::cout << "The '--device DEV' option should not be needed for normal use" << std::endl;
std::cout << "and might potentially be harmful when used on devices that" << std::endl;
std::cout << "are not a gamepad, use at your own risk and ensure that you" << std::endl;
std::cout << "are accessing the right device.\n" << std::endl;
std::cout << "If you have multiple gamepads and want to select a differnt" << std::endl;
std::cout << "one use the '-id N' option instead.\n" << std::endl;
std::cout << "Press Ctrl-c to exit and Enter to continue." << std::endl;
getchar();
}
else
{
2008-05-02 15:07:06 -06:00
std::cout << "Error: " << argv[i-1] << " expected an argument in form BUS:DEV (i.e. 006:003)" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-19 22:58:20 -06:00
}
}
else
{
std::cout << "Error: " << argv[i-1] << " expected an argument" << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-19 22:58:20 -06:00
}
}
else if (strcmp(argv[i], "--list-controller") == 0 ||
2008-04-15 08:06:48 -06:00
strcmp(argv[i], "-L") == 0)
{
usb_init();
usb_find_busses();
usb_find_devices();
list_controller();
2008-04-20 19:00:51 -06:00
exit(EXIT_SUCCESS);
}
else if (strcmp(argv[i], "--help-devices") == 0)
{
std::cout << " idVendor | idProduct | Name" << std::endl;
std::cout << "----------+-----------+---------------------------------" << std::endl;
for(unsigned int i = 0; i < sizeof(xpad_devices)/sizeof(XPadDevice); ++i)
{
std::cout << boost::format(" 0x%04x | 0x%04x | %s")
% int(xpad_devices[i].idVendor)
% int(xpad_devices[i].idProduct)
% xpad_devices[i].name
<< std::endl;
}
2008-04-20 19:00:51 -06:00
exit(EXIT_SUCCESS);
}
2008-04-11 05:43:15 -06:00
else
{
std::cout << "Error: unknown command line option: " << argv[i] << std::endl;
2008-04-20 19:00:51 -06:00
exit(EXIT_FAILURE);
2008-04-11 05:43:15 -06:00
}
}
2008-04-20 19:00:51 -06:00
}
void print_info(struct usb_device* dev,
const XPadDevice& dev_type,
const CommandLineOptions& opts)
{
std::cout << "USB Device: " << dev->bus->dirname << ":" << dev->filename << std::endl;
std::cout << "Controller: " << boost::format("\"%s\" (idVendor: 0x%04x, idProduct: 0x%04x)")
% dev_type.name % uint16_t(dev->descriptor.idVendor) % uint16_t(dev->descriptor.idProduct) << std::endl;
if (dev_type.type == GAMEPAD_XBOX360_WIRELESS)
2008-05-01 03:05:02 -06:00
std::cout << "Wireless Port: " << opts.wireless_id << std::endl;
std::cout << "Controller Type: " << opts.gamepad_type << std::endl;
std::cout << "Deadzone: " << opts.deadzone << std::endl;
std::cout << "Rumble Debug: " << (opts.rumble ? "on" : "off") << std::endl;
std::cout << "Rumble Speed: " << "left: " << opts.rumble_l << " right: " << opts.rumble_r << std::endl;
if (opts.led == -1)
std::cout << "LED Status: " << "auto" << std::endl;
else
std::cout << "LED Status: " << opts.led << std::endl;
2008-05-02 15:07:06 -06:00
std::cout << "ButtonMap: ";
if (opts.button_map.empty())
{
std::cout << "none" << std::endl;
}
else
{
for(std::vector<ButtonMapping>::const_iterator i = opts.button_map.begin(); i != opts.button_map.end(); ++i)
{
std::cout << i->lhs << "->" << i->rhs << " ";
}
std::cout << std::endl;
}
std::cout << "AxisMap: ";
2008-05-02 15:07:06 -06:00
if (opts.axis_map.empty())
{
std::cout << "none" << std::endl;
}
else
{
for(std::vector<AxisMapping>::const_iterator i = opts.axis_map.begin(); i != opts.axis_map.end(); ++i)
{
std::cout << i->lhs << "->" << i->rhs << " ";
}
std::cout << std::endl;
}
}
void apply_deadzone(XboxGenericMsg& msg, int deadzone)
2008-05-01 03:05:02 -06:00
{
switch (msg.type)
2008-05-01 03:05:02 -06:00
{
case GAMEPAD_XBOX:
case GAMEPAD_XBOX_MAT:
if (abs(msg.xbox.x1) < deadzone)
msg.xbox.x1 = 0;
if (abs(msg.xbox.y1) < deadzone)
msg.xbox.y1 = 0;
if (abs(msg.xbox.x2) < deadzone)
msg.xbox.x2 = 0;
if (abs(msg.xbox.y2) < deadzone)
msg.xbox.y2 = 0;
break;
case GAMEPAD_XBOX360:
case GAMEPAD_XBOX360_WIRELESS:
if (abs(msg.xbox360.x1) < deadzone)
msg.xbox360.x1 = 0;
if (abs(msg.xbox360.y1) < deadzone)
msg.xbox360.y1 = 0;
if (abs(msg.xbox360.x2) < deadzone)
msg.xbox360.x2 = 0;
if (abs(msg.xbox360.y2) < deadzone)
msg.xbox360.y2 = 0;
break;
case GAMEPAD_XBOX360_GUITAR:
// FIXME: any use for deadzone here?
break;
2008-05-01 03:05:02 -06:00
default:
assert(!"apply_deadzone(): Unknown gamepad type");
}
}
void controller_loop(uInput* uinput, XboxGenericController* controller, CommandLineOptions& opts)
{
XboxGenericMsg oldmsg;
memset(&oldmsg, 0, sizeof(oldmsg));
while(!global_exit_xboxdrv)
2008-05-01 03:05:02 -06:00
{
XboxGenericMsg msg;
2008-05-01 04:03:40 -06:00
if (controller->read(msg))
2008-05-01 03:05:02 -06:00
{
apply_deadzone(msg, opts.deadzone);
2008-05-02 15:07:06 -06:00
if (!opts.button_map.empty())
apply_button_map(msg, opts.button_map);
if (!opts.axis_map.empty())
apply_axis_map(msg, opts.axis_map);
if (memcmp(&msg, &oldmsg, sizeof(XboxGenericMsg)))
{ // Only send a new event out if something has changed,
// this is useful since some controllers send events
// even if nothing has changed, deadzone can cause this
// too
oldmsg = msg;
if (!opts.silent)
std::cout << msg << std::endl;
if (uinput)
uinput->send(msg);
2008-05-01 04:03:40 -06:00
if (opts.rumble)
2008-05-01 04:03:40 -06:00
{
if (opts.gamepad_type == GAMEPAD_XBOX)
{
controller->set_rumble(msg.xbox.lt, msg.xbox.rt);
}
else if (opts.gamepad_type == GAMEPAD_XBOX360 ||
opts.gamepad_type == GAMEPAD_XBOX360_WIRELESS)
{
controller->set_rumble(msg.xbox360.lt, msg.xbox360.rt);
}
2008-05-01 04:03:40 -06:00
}
2008-05-01 03:05:02 -06:00
}
}
}
}
void find_controller(struct usb_device*& dev,
XPadDevice& dev_type,
const CommandLineOptions& opts)
2008-04-20 19:00:51 -06:00
{
if (opts.busid[0] != '\0' && opts.devid[0] != '\0')
2008-04-10 11:33:56 -06:00
{
2008-04-20 19:19:02 -06:00
if (opts.gamepad_type == GAMEPAD_UNKNOWN)
2008-04-19 22:58:20 -06:00
{
std::cout << "Error: --device BUS:DEV option must be used in combination with --type TYPE option" << std::endl;
exit(EXIT_FAILURE);
}
else
{
2008-04-20 19:00:51 -06:00
if (!find_controller_by_path(opts.busid, opts.devid, &dev))
2008-04-19 22:58:20 -06:00
{
2008-04-20 19:00:51 -06:00
std::cout << "Error: couldn't find device " << opts.busid << ":" << opts.devid << std::endl;
2008-04-19 22:58:20 -06:00
exit(EXIT_FAILURE);
}
}
}
else
{
2008-04-20 19:00:51 -06:00
if (!find_xbox360_controller(opts.controller_id, &dev, &dev_type))
2008-04-19 22:58:20 -06:00
{
2008-04-24 11:12:59 -06:00
std::cout << "No Xbox or Xbox360 controller found" << std::endl;
2008-04-19 22:58:20 -06:00
exit(EXIT_FAILURE);
}
}
}
int led_count = 0;
void on_sigint(int)
{
if (global_exit_xboxdrv)
{
std::cout << "Ctrl-c pressed twice, exting hard" << std::endl;
exit(EXIT_SUCCESS);
}
else
{
std::cout << "Shutdown initiated" << std::endl;
global_exit_xboxdrv = true;
if (global_controller)
global_controller->set_led(0);
}
}
int main(int argc, char** argv)
{
2008-05-02 15:07:06 -06:00
try
{
signal(SIGINT, on_sigint);
2008-05-02 15:07:06 -06:00
CommandLineOptions opts;
2008-05-02 15:07:06 -06:00
parse_command_line(argc, argv, opts);
2008-05-02 15:07:06 -06:00
usb_init();
usb_find_busses();
usb_find_devices();
2008-05-02 15:07:06 -06:00
struct usb_device* dev = 0;
XPadDevice dev_type;
2008-05-02 15:07:06 -06:00
find_controller(dev, dev_type, opts);
2008-04-19 22:58:20 -06:00
2008-05-02 15:07:06 -06:00
if (!dev)
2008-04-19 22:58:20 -06:00
{
2008-05-02 15:07:06 -06:00
std::cout << "No suitable USB device found, abort" << std::endl;
exit(EXIT_FAILURE);
2008-04-19 22:58:20 -06:00
}
2008-05-02 15:07:06 -06:00
else
{
if (opts.gamepad_type != GAMEPAD_UNKNOWN)
{ // Override the default gamepad type when given
dev_type.type = opts.gamepad_type;
}
else
{
opts.gamepad_type = dev_type.type;
}
2008-04-17 15:02:28 -06:00
2008-05-02 15:07:06 -06:00
print_info(dev, dev_type, opts);
2008-05-02 15:07:06 -06:00
XboxGenericController* controller = 0;
2008-05-01 03:05:02 -06:00
2008-05-02 15:07:06 -06:00
switch (dev_type.type)
{
case GAMEPAD_XBOX:
case GAMEPAD_XBOX_MAT:
controller = new XboxController(dev);
break;
2008-05-01 03:05:02 -06:00
2008-05-02 15:07:06 -06:00
case GAMEPAD_XBOX360_GUITAR:
controller = new Xbox360Controller(dev, true);
break;
2008-05-02 15:07:06 -06:00
case GAMEPAD_XBOX360:
controller = new Xbox360Controller(dev, false);
break;
2008-05-01 03:05:02 -06:00
2008-05-02 15:07:06 -06:00
case GAMEPAD_XBOX360_WIRELESS:
controller = new Xbox360WirelessController(dev, opts.wireless_id);
break;
2008-05-01 03:05:02 -06:00
2008-05-02 15:07:06 -06:00
default:
assert(!"Unknown gamepad type");
}
2008-04-15 10:09:16 -06:00
2008-05-02 15:07:06 -06:00
global_controller = controller;
2008-05-02 15:07:06 -06:00
int jsdev_number = find_jsdev_number();
int evdev_number = find_evdev_number();
2008-05-02 15:07:06 -06:00
// FIXME: insert /dev/input/jsX detection magic here
if (opts.led == -1)
controller->set_led(2 + jsdev_number % 4);
else
controller->set_led(opts.led);
2008-05-02 15:07:06 -06:00
controller->set_rumble(opts.rumble_l, opts.rumble_r);
2008-05-01 03:05:02 -06:00
2008-05-02 15:07:06 -06:00
if (opts.instant_exit)
{
2008-05-02 15:07:06 -06:00
usleep(1000);
}
else
2008-05-02 15:07:06 -06:00
{
uInput* uinput = 0;
if (!opts.no_uinput)
{
std::cout << "Starting with uinput" << std::endl;
uinput = new uInput(opts.gamepad_type, opts.uinput_config);
}
else
{
std::cout << "Starting without uinput" << std::endl;
}
std::cout << "\nYour Xbox/Xbox360 controller should now be available as:" << std::endl
<< " /dev/input/js" << jsdev_number << std::endl
<< " /dev/input/event" << evdev_number << std::endl;
2008-05-02 15:07:06 -06:00
std::cout << "\nPress Ctrl-c to quit\n" << std::endl;
2008-05-02 15:07:06 -06:00
global_exit_xboxdrv = false;
controller_loop(uinput, controller, opts);
2008-05-02 15:07:06 -06:00
delete controller;
delete uinput;
2008-05-02 15:07:06 -06:00
std::cout << "Shutdown complete" << std::endl;
}
2008-04-10 11:33:56 -06:00
}
}
2008-05-02 15:07:06 -06:00
catch(std::exception& err)
{
std::cout << "Exception: " << err.what() << std::endl;
}
return 0;
2008-04-10 10:38:50 -06:00
}
2008-04-10 13:39:08 -06:00
/* EOF */