more foreach, less pointers
This commit is contained in:
36
src/axis.cpp
36
src/axis.cpp
@ -24,11 +24,11 @@ Axis::~Axis() {
|
||||
delete timer;
|
||||
}
|
||||
|
||||
bool Axis::read( QTextStream* stream ) {
|
||||
bool Axis::read( QTextStream &stream ) {
|
||||
// At this point, toDefault has just been called.
|
||||
|
||||
//read in a line from the stream, and split it up into individual words
|
||||
QString input = stream->readLine().toLower();
|
||||
QString input = stream.readLine().toLower();
|
||||
QRegExp regex("[\\s,]+");
|
||||
QStringList words = input.split(regex);
|
||||
|
||||
@ -156,33 +156,33 @@ void Axis::timerCalled() {
|
||||
timerTick(++tick);
|
||||
}
|
||||
|
||||
void Axis::write( QTextStream* stream ) {
|
||||
*stream << "\t" << getName() << ": ";
|
||||
if (gradient) *stream << "gradient, ";
|
||||
if (throttle > 0) *stream << "throttle+, ";
|
||||
else if (throttle < 0) *stream << "throttle-, ";
|
||||
if (dZone != DZONE) *stream << "dZone " << dZone << ", ";
|
||||
if (xZone != XZONE) *stream << "xZone " << xZone << ", ";
|
||||
void Axis::write( QTextStream &stream ) {
|
||||
stream << "\t" << getName() << ": ";
|
||||
if (gradient) stream << "gradient, ";
|
||||
if (throttle > 0) stream << "throttle+, ";
|
||||
else if (throttle < 0) stream << "throttle-, ";
|
||||
if (dZone != DZONE) stream << "dZone " << dZone << ", ";
|
||||
if (xZone != XZONE) stream << "xZone " << xZone << ", ";
|
||||
if (mode == keybd) {
|
||||
*stream
|
||||
stream
|
||||
<< (puseMouse ? "+mouse " : "+key ") << pkeycode << ", "
|
||||
<< (nuseMouse ? "-mouse " : "-key ") << nkeycode << "\n";
|
||||
}
|
||||
else {
|
||||
if (gradient) *stream << "maxSpeed " << maxSpeed << ", ";
|
||||
if (gradient) stream << "maxSpeed " << maxSpeed << ", ";
|
||||
if (transferCurve != quadratic)
|
||||
*stream << "tCurve " << transferCurve << ", ";
|
||||
stream << "tCurve " << transferCurve << ", ";
|
||||
if (sensitivity != 1.0F)
|
||||
*stream << "sens " << sensitivity << ", ";
|
||||
*stream << "mouse";
|
||||
stream << "sens " << sensitivity << ", ";
|
||||
stream << "mouse";
|
||||
if (mode == mousepv)
|
||||
*stream << "+v\n";
|
||||
stream << "+v\n";
|
||||
else if (mode == mousenv)
|
||||
*stream << "-v\n";
|
||||
stream << "-v\n";
|
||||
else if (mode == mouseph)
|
||||
*stream << "+h\n";
|
||||
stream << "+h\n";
|
||||
else if (mode == mousenh)
|
||||
*stream << "-h\n";
|
||||
stream << "-h\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,9 +30,9 @@ class Axis : public QObject {
|
||||
Axis( int i, QObject *parent = 0 );
|
||||
~Axis();
|
||||
//read axis settings from a stream
|
||||
bool read( QTextStream* stream );
|
||||
bool read( QTextStream &stream );
|
||||
//write axis settings to a stream
|
||||
void write( QTextStream* stream );
|
||||
void write( QTextStream &stream );
|
||||
//releases any pushed buttons and returns to a neutral state
|
||||
void release();
|
||||
//pass a message from the joystick device to this axis object
|
||||
|
@ -16,11 +16,11 @@ Button::~Button() {
|
||||
//delete timer;
|
||||
}
|
||||
|
||||
bool Button::read( QTextStream* stream ) {
|
||||
bool Button::read( QTextStream &stream ) {
|
||||
// at this point, toDefault() has just been called.
|
||||
|
||||
//read in a line of text and break it into words
|
||||
QString input = stream->readLine().toLower();
|
||||
QString input = stream.readLine().toLower();
|
||||
QRegExp regex("[\\s,]+");
|
||||
QStringList words = input.split(regex);
|
||||
|
||||
@ -61,11 +61,16 @@ bool Button::read( QTextStream* stream ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Button::write( QTextStream* stream ) {
|
||||
*stream << "\t" << getName() << ": ";
|
||||
if (rapidfire) *stream << "rapidfire, ";
|
||||
if (sticky) *stream << "sticky, ";
|
||||
*stream << (useMouse?"mouse ":"key ") << (useMouse?mousecode:keycode) << "\n";
|
||||
void Button::write( QTextStream &stream ) {
|
||||
stream << "\t" << getName() << ": ";
|
||||
if (rapidfire) stream << "rapidfire, ";
|
||||
if (sticky) stream << "sticky, ";
|
||||
if (useMouse) {
|
||||
stream << "mouse " << mousecode << "\n";
|
||||
}
|
||||
else {
|
||||
stream << "key " << keycode << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void Button::release() {
|
||||
|
@ -17,9 +17,9 @@ class Button : public QObject {
|
||||
Button( int i, QObject *parent = 0 );
|
||||
~Button();
|
||||
//read from stream
|
||||
bool read( QTextStream* stream );
|
||||
bool read( QTextStream &stream );
|
||||
//write to stream
|
||||
void write( QTextStream* stream );
|
||||
void write( QTextStream &stream );
|
||||
//releases any pushed buttons and returns to a neutral state
|
||||
void release();
|
||||
//process an event from the actual joystick device
|
||||
|
115
src/joypad.cpp
115
src/joypad.cpp
@ -5,6 +5,7 @@
|
||||
#include<stdint.h>
|
||||
#include <poll.h>
|
||||
#include <QApplication>
|
||||
|
||||
JoyPad::JoyPad( int i, int dev ) {
|
||||
debug_mesg("Constructing the joypad device with index %d and fd %d\n", i, dev);
|
||||
//remember the index,
|
||||
@ -72,61 +73,39 @@ void JoyPad::setupJoyDeviceListener(int dev) {
|
||||
|
||||
void JoyPad::toDefault() {
|
||||
//to reset the whole, reset all the parts.
|
||||
do
|
||||
{
|
||||
QHashIterator<int, Axis*> it( Axes );
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
it.value()->toDefault();
|
||||
}
|
||||
} while (0);
|
||||
do
|
||||
{
|
||||
QHashIterator<int, Button*> it( Buttons );
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
it.value()->toDefault();
|
||||
}
|
||||
} while (0);
|
||||
foreach (Axis *axis, Axes) {
|
||||
axis->toDefault();
|
||||
}
|
||||
foreach (Button *button, Buttons) {
|
||||
button->toDefault();
|
||||
}
|
||||
}
|
||||
|
||||
bool JoyPad::isDefault() {
|
||||
//if any of the parts are not at default, then the whole isn't either.
|
||||
do {
|
||||
QHashIterator<int, Axis*> it( Axes );
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
if (!it.value()->isDefault()) return false;
|
||||
}
|
||||
} while (0);
|
||||
do {
|
||||
QHashIterator<int, Button*> it( Buttons );
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
if (!it.value()->isDefault()) return false;
|
||||
}
|
||||
} while (0);
|
||||
foreach (Axis *axis, Axes) {
|
||||
if (!axis->isDefault()) return false;
|
||||
}
|
||||
foreach (Button *button, Buttons) {
|
||||
if (!button->isDefault()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JoyPad::readConfig( QTextStream* stream ) {
|
||||
bool JoyPad::readConfig( QTextStream &stream ) {
|
||||
toDefault();
|
||||
|
||||
QString word;
|
||||
QChar dump;
|
||||
int num;
|
||||
|
||||
*stream >> word;
|
||||
stream >> word;
|
||||
while (word != NULL && word != "}") {
|
||||
word = word.toLower();
|
||||
if (word == "button") {
|
||||
*stream >> num;
|
||||
stream >> num;
|
||||
if (num > 0) {
|
||||
*stream >> dump;
|
||||
stream >> dump;
|
||||
if (dump != ':') {
|
||||
error("Layout file error", "Expected ':', found '" + QString(dump) + "'.");
|
||||
return false;
|
||||
@ -140,13 +119,13 @@ bool JoyPad::readConfig( QTextStream* stream ) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
stream->readLine();
|
||||
stream.readLine();
|
||||
}
|
||||
}
|
||||
else if (word == "axis") {
|
||||
*stream >> num;
|
||||
stream >> num;
|
||||
if (num > 0) {
|
||||
*stream >> dump;
|
||||
stream >> dump;
|
||||
if (dump != ':') {
|
||||
error("Layout file error", "Expected ':', found '" + QString(dump) + "'.");
|
||||
return false;
|
||||
@ -164,56 +143,32 @@ bool JoyPad::readConfig( QTextStream* stream ) {
|
||||
error( "Layout file error", "Error while reading layout. Unrecognized word: " + word );
|
||||
return false;
|
||||
}
|
||||
*stream >> word;
|
||||
stream >> word;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//only actually writes something if this JoyPad is NON DEFAULT.
|
||||
void JoyPad::write( QTextStream* stream ) {
|
||||
int i = 0; //use to test if this is default or not.
|
||||
QString result;
|
||||
QTextStream* s = new QTextStream(&result, QIODevice::WriteOnly);
|
||||
*s << getName() << " {\n";
|
||||
do {
|
||||
QHashIterator<int, Axis*> it( Axes );
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (!it.value()->isDefault()) {
|
||||
it.value()->write( s );
|
||||
++i;
|
||||
}
|
||||
void JoyPad::write( QTextStream &stream ) {
|
||||
if (!Axes.empty() || !Buttons.empty()) {
|
||||
stream << getName() << " {\n";
|
||||
foreach (Axis *axis, Axes) {
|
||||
axis->write(stream);
|
||||
}
|
||||
} while (0);
|
||||
QHashIterator<int, Button*> it( Buttons );
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (!it.value()->isDefault()) {
|
||||
it.value()->write( s );
|
||||
++i;
|
||||
foreach (Button *button, Buttons) {
|
||||
button->write(stream);
|
||||
}
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
*stream << result << "}\n\n";
|
||||
stream << "}\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
void JoyPad::release() {
|
||||
do {
|
||||
QHashIterator<int, Axis*> it( Axes );
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
it.value()->release();
|
||||
}
|
||||
} while (0);
|
||||
do {
|
||||
QHashIterator<int, Button*> it( Buttons );
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
it.value()->release();
|
||||
}
|
||||
} while (0);
|
||||
foreach (Axis *axis, Axes) {
|
||||
axis->release();
|
||||
}
|
||||
foreach (Button *button, Buttons) {
|
||||
button->release();
|
||||
}
|
||||
}
|
||||
|
||||
void JoyPad::jsevent( js_event msg ) {
|
||||
|
@ -31,9 +31,9 @@ class JoyPad : public QObject{
|
||||
void setupJoyDeviceListener(int dev);
|
||||
JoyPad( int i, int dev );
|
||||
//read from a stream
|
||||
bool readConfig( QTextStream* stream );
|
||||
bool readConfig( QTextStream &stream );
|
||||
//write to a stream
|
||||
void write( QTextStream* stream );
|
||||
void write( QTextStream &stream );
|
||||
//release any pushed buttons and return to a neutral state
|
||||
void release();
|
||||
//handle an event from the joystick device this is associated with
|
||||
|
@ -59,11 +59,8 @@ bool LayoutManager::load(const QString& name) {
|
||||
//note that we don't use available here, but joypads instead. This is so
|
||||
//if one layout has more joypads than this one does, this won't have the
|
||||
//extra settings left over after things are supposed to be "cleared"
|
||||
QHashIterator<int, JoyPad*> it( joypads );
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
it.value()->toDefault();
|
||||
foreach (JoyPad *joypad, joypads) {
|
||||
joypad->toDefault();
|
||||
}
|
||||
|
||||
//start reading joypads!
|
||||
@ -90,7 +87,7 @@ bool LayoutManager::load(const QString& name) {
|
||||
joypads.insert(num-1, new JoyPad(num-1, 0));
|
||||
}
|
||||
//try to read the joypad, report error on fail.
|
||||
if (!joypads[num-1]->readConfig(&stream)) {
|
||||
if (!joypads[num-1]->readConfig(stream)) {
|
||||
error( "Load error", "Error reading definition for joystick " + QString::number(num-1));
|
||||
//if this was attempting to change to a new layout and it failed,
|
||||
//revert back to the old layout.
|
||||
@ -135,11 +132,8 @@ bool LayoutManager::reload() {
|
||||
|
||||
void LayoutManager::clear() {
|
||||
//reset all the joypads...
|
||||
QHashIterator<int, JoyPad*> it (joypads);
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
it.value()->toDefault();
|
||||
foreach (JoyPad *joypad, joypads) {
|
||||
joypad->toDefault();
|
||||
}
|
||||
//and call our layout NL
|
||||
setLayoutName(NL);
|
||||
@ -158,11 +152,8 @@ void LayoutManager::save() {
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
QTextStream stream( &file );
|
||||
stream << "# " NAME " Layout File\n\n";
|
||||
QHashIterator<int, JoyPad*> it (joypads);
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
it.value()->write( &stream );
|
||||
foreach (JoyPad *joypad, joypads) {
|
||||
joypad->write( stream );
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
@ -320,13 +311,9 @@ void LayoutManager::updateJoyDevs() {
|
||||
QString devdir = DEVDIR;
|
||||
|
||||
//reset all joydevs to sentinal value (-1)
|
||||
do {
|
||||
QHashIterator<int, JoyPad*> it( joypads );
|
||||
while (it.hasNext() ) {
|
||||
it.next();
|
||||
it.value()->unsetDev();
|
||||
}
|
||||
} while (0);
|
||||
foreach (JoyPad *joypad, joypads) {
|
||||
joypad->unsetDev();
|
||||
}
|
||||
|
||||
//clear out the list of previously available joysticks
|
||||
available.clear();
|
||||
|
Reference in New Issue
Block a user