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