Small refactoring, no semantic changes

This commit is contained in:
Ingo Ruhnke 2008-12-28 18:20:40 +01:00
parent aff7c79df3
commit e725680386
7 changed files with 38 additions and 209 deletions

View file

@ -5,6 +5,7 @@ env.Program("xboxdrv", ["src/xboxdrv.cpp",
"src/xboxmsg.cpp",
"src/uinput.cpp",
"src/helper.cpp",
"src/modifier.cpp",
"src/command_line_options.cpp",
"src/xbox_controller.cpp",
"src/xbox360_controller.cpp",

View file

@ -1,26 +1,19 @@
/* $Id$
** __ __ __ ___ __ __ __ __
** / \ / \__| ____ __| _/_______/ |_|__| | | | ____
** \ \/\/ / |/ \ / __ |/ ___/\ __\ | | | | _/ __ \
** \ /| | | \/ /_/ |\___ \ | | | | |_| |_\ ___/
** \__/\ / |__|___| /\____ /____ > |__| |__|____/____/\___ >
** \/ \/ \/ \/ \/
** Copyright (C) 2007 Ingo Ruhnke <grumbel@gmx.de>
/*
** Xbox360 USB Gamepad Userspace Driver
** 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 2
** of the License, or (at your option) any later version.
** 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, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
** 02111-1307, USA.
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "command_line_options.hpp"

View file

@ -27,6 +27,7 @@
#define HEADER_COMMAND_LINE_OPTIONS_HPP
#include <vector>
#include "modifier.hpp"
#include "xboxmsg.hpp"
#include "uinput.hpp"

View file

@ -97,94 +97,6 @@ XPadDevice xpad_devices[] = {
const int xpad_devices_count = sizeof(xpad_devices)/sizeof(XPadDevice);
std::ostream& operator<<(std::ostream& out, const GamepadType& type)
{
switch (type)
{
case GAMEPAD_XBOX360:
return out << "Xbox360";
case GAMEPAD_XBOX360_WIRELESS:
return out << "Xbox360 (wireless)";
case GAMEPAD_XBOX:
return out << "Xbox Classic";
case GAMEPAD_XBOX_MAT:
return out << "Xbox Dancepad";
case GAMEPAD_XBOX360_GUITAR:
return out << "Xbox360 Guitar";
default:
return out << "unknown" << std::endl;
}
}
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)
{
XboxGenericMsg newmsg = msg;
for(std::vector<AxisMapping>::iterator i = lst.begin(); i != lst.end(); ++i)
{
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);
if (i->invert)
{
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));
}
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");
}
template<class C, class Func>
void arg2vector(const std::string& str, typename std::vector<C>& lst, Func func)
@ -205,84 +117,6 @@ void arg2vector(const std::string& str, typename std::vector<C>& lst, Func func)
lst.push_back(func(std::string(start, str.end())));
}
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");
}
RelativeAxisMapping
RelativeAxisMapping::from_string(const std::string& str)
{
/* Format of str: A={SPEED} */
std::string::size_type i = str.find('=');
if (i == std::string::npos)
{
throw std::runtime_error("Couldn't convert string \"" + str + "\" to RelativeAxisMapping");
}
else
{
RelativeAxisMapping mapping;
mapping.axis = string2axis(str.substr(0, i));
mapping.speed = atoi(str.substr(i+1, str.size()-i).c_str());
// FIXME: insert some error checking here
return mapping;
}
}
AutoFireMapping
AutoFireMapping::from_string(const std::string& str)
{
/* Format of str: A={ON-DELAY}[:{OFF-DELAY}]
Examples: A=10 or A=10:50
if OFF-DELAY == nil then ON-DELAY = OFF-DELAY
*/
std::string::size_type i = str.find_first_of('=');
if (i == std::string::npos)
{
throw std::runtime_error("Couldn't convert string \"" + str + "\" to AutoFireMapping");
}
else
{
AutoFireMapping mapping;
mapping.button = string2btn(str.substr(0, i));
mapping.frequency = atoi(str.substr(i+1, str.size()-i).c_str())/1000.0f;
return mapping;
}
}
void list_controller()
{
struct usb_bus* busses = usb_get_busses();
@ -615,7 +449,7 @@ void parse_command_line(int argc, char** argv, CommandLineOptions& opts)
++i;
if (i < argc)
{
arg2vector(argv[i], opts.button_map, string2buttonmapping);
arg2vector(argv[i], opts.button_map, &ButtonMapping::from_string);
}
else
{
@ -629,7 +463,7 @@ void parse_command_line(int argc, char** argv, CommandLineOptions& opts)
++i;
if (i < argc)
{
arg2vector(argv[i], opts.axis_map, string2axismapping);
arg2vector(argv[i], opts.axis_map, &AxisMapping::from_string);
}
else
{

View file

@ -27,31 +27,6 @@ struct XPadDevice {
uint16_t idProduct;
const char* name;
};
struct ButtonMapping {
XboxButton lhs;
XboxButton rhs;
};
struct AxisMapping {
XboxAxis lhs;
XboxAxis rhs;
bool invert;
};
struct AutoFireMapping {
static AutoFireMapping from_string(const std::string&);
XboxButton button;
float frequency;
};
struct RelativeAxisMapping {
static RelativeAxisMapping from_string(const std::string&);
XboxAxis axis;
int speed;
};
#endif

View file

@ -22,6 +22,30 @@
#include "command_line_options.hpp"
#include "xboxmsg.hpp"
std::ostream& operator<<(std::ostream& out, const GamepadType& type)
{
switch (type)
{
case GAMEPAD_XBOX360:
return out << "Xbox360";
case GAMEPAD_XBOX360_WIRELESS:
return out << "Xbox360 (wireless)";
case GAMEPAD_XBOX:
return out << "Xbox Classic";
case GAMEPAD_XBOX_MAT:
return out << "Xbox Dancepad";
case GAMEPAD_XBOX360_GUITAR:
return out << "Xbox360 Guitar";
default:
return out << "unknown" << std::endl;
}
}
std::ostream& operator<<(std::ostream& out, const XboxGenericMsg& msg)
{
if (msg.type == GAMEPAD_XBOX)

View file

@ -171,6 +171,7 @@ struct XboxGenericMsg
};
};
std::ostream& operator<<(std::ostream& out, const GamepadType& type);
std::ostream& operator<<(std::ostream& out, const Xbox360GuitarMsg& msg);
std::ostream& operator<<(std::ostream& out, const Xbox360Msg& msg);
std::ostream& operator<<(std::ostream& out, const XboxMsg& msg);