From 904a843b936c6c7ee793e146921603bc80f82151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Panzenb=C3=B6ck?= Date: Sun, 16 Feb 2014 00:37:54 +0100 Subject: [PATCH] foreach++, globals--, const& params++, new--, uninit mem usage--, mem leaks-- (still a lot to go) --- src/axis.cpp | 10 ++- src/axis.h | 4 +- src/button.cpp | 41 ++++------- src/button.h | 7 +- src/button_edit.cpp | 12 +--- src/constant.h | 4 +- src/error.h | 8 +-- src/joypad.cpp | 37 +++++----- src/joypad.h | 11 +-- src/joypadw.cpp | 58 +++++++-------- src/joypadw.h | 11 +-- src/layout.cpp | 168 ++++++++++++++++++++++++-------------------- src/layout.h | 19 +++-- src/layout_edit.cpp | 2 +- src/main.cpp | 23 +++--- src/quickset.cpp | 4 +- src/quickset.h | 2 +- 17 files changed, 213 insertions(+), 208 deletions(-) diff --git a/src/axis.cpp b/src/axis.cpp index c135309..5b1f911 100644 --- a/src/axis.cpp +++ b/src/axis.cpp @@ -15,13 +15,11 @@ Axis::Axis( int i, QObject *parent ) : QObject(parent) { gradient = false; toDefault(); tick = 0; - timer = new QTimer(this); } Axis::~Axis() { release(); - delete timer; } bool Axis::read( QTextStream &stream ) { @@ -210,8 +208,8 @@ void Axis::jsevent( int value ) { if (gradient) { duration = 0; release(); - timer->stop(); - disconnect(timer, SIGNAL(timeout()), 0, 0); + timer.stop(); + disconnect(&timer, SIGNAL(timeout()), 0, 0); tick = 0; } } @@ -220,8 +218,8 @@ void Axis::jsevent( int value ) { isOn = true; if (gradient) { duration = (abs(state) * FREQ) / JOYMAX; - connect(timer, SIGNAL(timeout()), this, SLOT(timerCalled())); - timer->start(MSEC); + connect(&timer, SIGNAL(timeout()), this, SLOT(timerCalled())); + timer.start(MSEC); } } //otherwise, state doesn't change! Don't touch it. diff --git a/src/axis.h b/src/axis.h index 043192c..5df77c8 100644 --- a/src/axis.h +++ b/src/axis.h @@ -41,7 +41,7 @@ class Axis : public QObject { void toDefault(); //True iff currently at defaults bool isDefault(); - QString getName() { return "Axis " + QString::number(index+1);}; + QString getName() { return QString("Axis %1").arg(index+1);} //true iff the given value is in the dead zone for this axis. bool inDeadZone( int val ); //a descriptive string used as a label for the button representing this axis @@ -95,7 +95,7 @@ class Axis : public QObject { //note, the key is still clicked at the same pace no matter what, //this just decides how long it stays down each cycle. int duration; - QTimer *timer; + QTimer timer; public slots: void timerCalled(); }; diff --git a/src/button.cpp b/src/button.cpp index 518a237..1b9d84b 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -6,14 +6,12 @@ Button::Button( int i, QObject *parent ) : QObject(parent) { isButtonPressed = false; isDown = false; rapidfire = false; - timer = new QTimer(this); toDefault(); tick = 0; } Button::~Button() { release(); - //delete timer; } bool Button::read( QTextStream &stream ) { @@ -37,7 +35,7 @@ bool Button::read( QTextStream &stream ) { val = (*it).toInt(&ok); if (ok && val >= 0 && val <= MAXKEY) { useMouse = true; - mousecode = val; + keycode = val; } else return false; } @@ -65,12 +63,7 @@ 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"; - } + stream << (useMouse ? "mouse " : "key ") << keycode << "\n"; } void Button::release() { @@ -94,12 +87,12 @@ void Button::jsevent( int value ) { isButtonPressed = newval; //change state if (isButtonPressed && rapidfire) { tick = 0; - connect(timer, SIGNAL(timeout()), this, SLOT(timerCalled())); - timer->start(MSEC); + connect(&timer, SIGNAL(timeout()), this, SLOT(timerCalled())); + timer.start(MSEC); } if (!isButtonPressed && rapidfire) { - timer->stop(); - disconnect(timer, SIGNAL(timeout()), 0, 0); + timer.stop(); + disconnect(&timer, SIGNAL(timeout()), 0, 0); if(isDown) { click(false); } @@ -120,36 +113,28 @@ void Button::toDefault() { sticky = false; useMouse = false; keycode = 0; - mousecode = 0; - timer->stop(); + timer.stop(); } bool Button::isDefault() { return (rapidfire == false) && (sticky == false) && (useMouse == false) && - (keycode == 0) && - (mousecode == 0); + (keycode == 0); } QString Button::status() { if (useMouse) { - return getName() + " : Mouse " + QString::number(mousecode); + return QString("%1 : Mouse %2").arg(getName(), keycode); } else { - return getName() + " : " + QString(ktos(keycode)); + return QString("%1 : %2").arg(getName(), ktos(keycode)); } } void Button::setKey( bool mouse, int value ) { - if (mouse) { - mousecode = value; - useMouse = true; - } - else { - keycode = value; - useMouse = false; - } + useMouse = mouse; + keycode = value; } void Button::timerTick( int tick ) { @@ -173,7 +158,7 @@ void Button::click( bool press ) { if (press) click.type = useMouse?BPRESS:KPRESS; else click.type = useMouse?BREL:KREL; //set up the event, - click.value1 = useMouse?mousecode:keycode; + click.value1 = keycode; click.value2 = 0; //and send it. sendevent( display, click ); diff --git a/src/button.h b/src/button.h index 92c0eb4..9f3a066 100644 --- a/src/button.h +++ b/src/button.h @@ -29,7 +29,7 @@ class Button : public QObject { //True iff is currently using default settings bool isDefault(); //returns a string representation of this button. - QString getName() { return "Button " + QString::number(index+1);}; + QString getName() { return QString("Button %1").arg(index+1); } //a descriptive string used as a label for the button representing this axis QString status(); //set the key code for this axis. Used by quickset. @@ -50,9 +50,8 @@ class Button : public QObject { bool rapidfire; bool sticky; bool useMouse; - int keycode; - int mousecode; //like keycode, only mousebutton ;) - QTimer *timer; + int keycode; + QTimer timer; public slots: void timerCalled(); }; diff --git a/src/button_edit.cpp b/src/button_edit.cpp index 43de502..dc3c8a9 100644 --- a/src/button_edit.cpp +++ b/src/button_edit.cpp @@ -15,7 +15,7 @@ ButtonEdit::ButtonEdit(Button* butt) v->setMargin(5); v->setSpacing(5); - BKKey = new KeyButton( button->getName(), button->useMouse?button->mousecode:button->keycode, this, true, button->useMouse); + BKKey = new KeyButton( button->getName(), button->keycode, this, true, button->useMouse); v->addWidget(BKKey); QHBoxLayout* h = new QHBoxLayout(); @@ -53,14 +53,8 @@ void ButtonEdit::accept() { button->rapidfire = CRapid->isChecked(); button->sticky = (CSticky->isChecked()); //if the user chose a mouse button... - if (BKKey->choseMouse()) { - button->useMouse = true; - button->mousecode = BKKey->getValue(); - } - else { - button->useMouse = false; - button->keycode = BKKey->getValue(); - } + button->useMouse = BKKey->choseMouse(); + button->keycode = BKKey->getValue(); QDialog::accept(); } diff --git a/src/constant.h b/src/constant.h index 0ada080..def9f79 100644 --- a/src/constant.h +++ b/src/constant.h @@ -1,5 +1,5 @@ -#ifndef JCONST_H -#define JCONST_H +#ifndef QJOYPAD_CONSTANT_H +#define QJOYPAD_CONSTANT_H //How many cycles there are per click. #define FREQ 10 diff --git a/src/error.h b/src/error.h index d55c753..7866de7 100644 --- a/src/error.h +++ b/src/error.h @@ -1,12 +1,12 @@ -#ifndef JOY_ERROR_H -#define JOY_ERROR_H +#ifndef QJOYPAD_ERROR_H +#define QJOYPAD_ERROR_H #include #include //a nice simple way of throwing up an error message if something goes wrong. -inline void error(QString type, QString message ) { - QMessageBox::warning(0,NAME" - " + type, +inline void error(const QString &type, const QString &message ) { + QMessageBox::warning(0, QString("%1 - %2").arg(NAME, type), message, QMessageBox::Ok, Qt::NoButton); } diff --git a/src/joypad.cpp b/src/joypad.cpp index bdc0d5a..6d0a9ad 100644 --- a/src/joypad.cpp +++ b/src/joypad.cpp @@ -13,13 +13,18 @@ JoyPad::JoyPad( int i, int dev ) { //load data from the joystick device, if available. joydevFileHandle = NULL; - if(dev > 0) { + if(dev >= 0) { debug_mesg("Valid file handle, setting up handlers and reading axis configs...\n"); resetToDev(dev); debug_mesg("done resetting and setting up device index %d\n", i); - char id[200]; - ioctl(joydev, JSIOCGNAME(199), id); - deviceId = id; + char id[256]; + memset(id, 0, sizeof(id)); + if (ioctl(joydev, JSIOCGNAME(sizeof(id)), id) < 0) { + deviceId = "Unknown"; + } + else { + deviceId = id; + } } else { debug_mesg("This joypad does not have a valid file handle, not setting up event listeners\n"); } @@ -96,25 +101,25 @@ bool JoyPad::readConfig( QTextStream &stream ) { toDefault(); QString word; - QChar dump; - int num; + QChar ch = 0; + int num = 0; stream >> word; - while (word != NULL && word != "}") { + while (!word.isNull() && word != "}") { word = word.toLower(); if (word == "button") { stream >> num; if (num > 0) { - stream >> dump; - if (dump != ':') { - error("Layout file error", "Expected ':', found '" + QString(dump) + "'."); + stream >> ch; + if (ch != ':') { + error("Layout file error", QString("Expected ':', found '%1'.").arg(ch)); return false; } if (Buttons[num-1] == 0) { Buttons.insert(num-1,new Button(num-1)); } if (!Buttons[num-1]->read( stream )) { - error("Layout file error", "Error reading Button " + QString::number(num)); + error("Layout file error", QString("Error reading Button %1").arg(num)); return false; } } @@ -125,22 +130,22 @@ bool JoyPad::readConfig( QTextStream &stream ) { else if (word == "axis") { stream >> num; if (num > 0) { - stream >> dump; - if (dump != ':') { - error("Layout file error", "Expected ':', found '" + QString(dump) + "'."); + stream >> ch; + if (ch != ':') { + error("Layout file error", QString("Expected ':', found '%1'.").arg(ch)); return false; } if (Axes[num-1] == 0) { Axes.insert(num-1,new Axis(num-1)); } if (!Axes[num-1]->read(stream)) { - error("Layout file error", "Error reading Axis " + QString::number(num)); + error("Layout file error", QString("Error reading Axis %1").arg(num)); return false; } } } else { - error( "Layout file error", "Error while reading layout. Unrecognized word: " + word ); + error( "Layout file error", QString("Error while reading layout. Unrecognized word: %1").arg(word) ); return false; } stream >> word; diff --git a/src/joypad.h b/src/joypad.h index d0dfd54..229eb84 100644 --- a/src/joypad.h +++ b/src/joypad.h @@ -23,11 +23,11 @@ class JoyPadWidget; //represents an actual joystick device -class JoyPad : public QObject{ +class JoyPad : public QObject { Q_OBJECT friend class JoyPadWidget; friend class QuickSet; - public: + public: void setupJoyDeviceListener(int dev); JoyPad( int i, int dev ); //read from a stream @@ -47,9 +47,11 @@ class JoyPad : public QObject{ //read the dimensions on the real joystick and use them void resetToDev( int dev ); //generates a name ("Joystick 1") - QString getName() { return QString("Joystick %1").arg(index+1);} + QString getName() const { return QString("Joystick %1").arg(index+1);} + int getIndex() const { return index; } private: + //it's just easier to have these publicly available. int joydev; //the actual file descriptor to the joystick device int axes; //the number of axes available on this device @@ -60,8 +62,7 @@ class JoyPad : public QObject{ //so that the joypad is always aware when it has a widget active. JoyPadWidget* widget(QWidget* parent, int i); //called when the joypad is no longer being edited. - void releaseWidget(); - QString getId(); + void releaseWidget(); protected: //lookup axes and buttons. These are dictionaries to support //layouts with different numbers of axes/buttons than the current diff --git a/src/joypadw.cpp b/src/joypadw.cpp index 824b59a..7000833 100644 --- a/src/joypadw.cpp +++ b/src/joypadw.cpp @@ -8,41 +8,41 @@ JoyPadWidget::JoyPadWidget( JoyPad* jp, int i, QWidget* parent ) /* This was in below, no idea what it does :( ... * (joypad->axes+1)/2 +(joypad->buttons+1)/2 + 2 */ - LMain = new QGridLayout(this); - LMain->setSpacing(5); - LMain->setMargin(5); + layoutMain = new QGridLayout(this); + layoutMain->setSpacing(5); + layoutMain->setMargin(5); flashcount = 0; int insertCounter = 0; quickset = NULL; - Axes = new AxisWidget*[joypad->axes]; - for (int i = 0; i < joypad->axes; i++) { - Axes[i] = new AxisWidget(joypad->Axes[i],this); - connect( Axes[i], SIGNAL( flashed( bool ) ), this, SLOT( flash( bool ))); - LMain->addWidget(Axes[i], insertCounter / 2, insertCounter %2); + foreach (Axis *axis, joypad->Axes) { + AxisWidget *aw = new AxisWidget(axis,this); + axes.append(aw); + connect( aw, SIGNAL( flashed( bool ) ), this, SLOT( flash( bool ))); + layoutMain->addWidget(aw, insertCounter / 2, insertCounter %2); insertCounter++; } - Buttons = new ButtonWidget*[joypad->buttons]; - for (int i = 0; i < joypad->buttons; i++) { - Buttons[i] = new ButtonWidget(joypad->Buttons[i],this); - connect( Buttons[i], SIGNAL( flashed( bool ) ), this, SLOT( flash( bool ))); - LMain->addWidget(Buttons[i], insertCounter/2, insertCounter%2); + foreach (Button *button, joypad->Buttons) { + ButtonWidget *bw = new ButtonWidget(button,this); + buttons.append(bw); + connect( bw, SIGNAL( flashed( bool ) ), this, SLOT( flash( bool ))); + layoutMain->addWidget(bw, insertCounter/2, insertCounter%2); insertCounter++; } - if(insertCounter % 2 == 1) { - insertCounter++; + if (insertCounter % 2 == 1) { + insertCounter ++; } - insertCounter+=2; - BClear = new QPushButton("Clear", this); - connect(BClear, SIGNAL(clicked()), this, SLOT(clear())); - LMain->addWidget(BClear, insertCounter / 2, insertCounter %2); + insertCounter += 2; + btnClear = new QPushButton("Clear", this); + connect(btnClear, SIGNAL(clicked()), this, SLOT(clear())); + layoutMain->addWidget(btnClear, insertCounter / 2, insertCounter %2); insertCounter++; - BAll = new QPushButton("Quick Set", this); - LMain->addWidget(BAll, insertCounter /2, insertCounter%2); - connect(BAll, SIGNAL(clicked()), this, SLOT(setAll())); + btnAll = new QPushButton("Quick Set", this); + layoutMain->addWidget(btnAll, insertCounter /2, insertCounter%2); + connect(btnAll, SIGNAL(clicked()), this, SLOT(setAll())); } JoyPadWidget::~JoyPadWidget() { @@ -63,11 +63,11 @@ void JoyPadWidget::flash( bool on ) { } void JoyPadWidget::update() { - for (int i = 0; i < joypad->axes; i++) { - Axes[i]->update(); + foreach (AxisWidget *axis, axes) { + axis->update(); } - for (int i = 0; i < joypad->buttons; i++) { - Buttons[i]->update(); + foreach (ButtonWidget *button, buttons) { + button->update(); } } @@ -79,7 +79,7 @@ void JoyPadWidget::clear() { void JoyPadWidget::setAll() { //quickset is NULL if there is no quickset dialog, and a pointer to the //dialog otherwise. This is so we can forward jsevents properly. - quickset = new QuickSet(joypad); + quickset = new QuickSet(joypad, this); quickset->exec(); update(); delete quickset; @@ -90,10 +90,10 @@ void JoyPadWidget::jsevent( js_event msg ) { //notify the component this event applies to. this cannot generate anything //other than a flash :) if (msg.type == JS_EVENT_AXIS) { - Axes[msg.number]->jsevent(msg.value); + if (msg.number < axes.count()) axes[msg.number]->jsevent(msg.value); } else { - Buttons[msg.number]->jsevent(msg.value); + if (msg.number < buttons.count()) buttons[msg.number]->jsevent(msg.value); } //if we're doing quickset, it needs to know when we do something. if (quickset != NULL) diff --git a/src/joypadw.h b/src/joypadw.h index f82ba63..b75240e 100644 --- a/src/joypadw.h +++ b/src/joypadw.h @@ -4,6 +4,7 @@ //parts for the widget //Added by qt3to4: +#include #include #include "axisw.h" //this all relates to a JoyPad @@ -40,7 +41,7 @@ class JoyPadWidget : public QWidget { //(either on or off) The int is the index of this widget so that this //signal can be directly connected to FlashRadioArray's flash(int) void flashed(int); - protected: + private: //the joypad this is linked to JoyPad* joypad; //the index of this widget IN THE WIDGET STACK! This is unique from @@ -53,10 +54,10 @@ class JoyPadWidget : public QWidget { int flashcount; //the parts of the dialog - QGridLayout *LMain; - AxisWidget **Axes; - ButtonWidget **Buttons; - QPushButton *BClear, *BAll; + QGridLayout *layoutMain; + QList axes; + QList buttons; + QPushButton *btnClear, *btnAll; //the quickset window, when we create it QuickSet* quickset; diff --git a/src/layout.cpp b/src/layout.cpp index 6e72a6f..6ac43fe 100644 --- a/src/layout.cpp +++ b/src/layout.cpp @@ -2,26 +2,22 @@ #include //initialize things and set up an icon :) -LayoutManager::LayoutManager( bool useTrayIcon ) { - //no LayoutEdit yet. - le = NULL; - +LayoutManager::LayoutManager( bool useTrayIcon, const QString &devdir, const QString &settingsDir ) : devdir(devdir), settingsDir(settingsDir), le(0) { //prepare the popup first. - Popup = new QMenu(); fillPopup(); - connect(Popup,SIGNAL(triggered(QAction*)),this, SLOT(trayMenu(QAction*))); + connect(&trayMenuPopup,SIGNAL(triggered(QAction*)),this, SLOT(trayMenu(QAction*))); //make a tray icon if (useTrayIcon) { - QSystemTrayIcon *Tray = new QSystemTrayIcon(this); - Tray->setContextMenu(Popup); - Tray->setIcon(QIcon(ICON24)); - Tray->show(); - connect(Tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayClick(QSystemTrayIcon::ActivationReason))); + QSystemTrayIcon *tray = new QSystemTrayIcon(this); + tray->setContextMenu(&trayMenuPopup); + tray->setIcon(QIcon(ICON24)); + tray->show(); + connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayClick(QSystemTrayIcon::ActivationReason))); } //or make a floating icon else { - FloatingIcon* Icon = new FloatingIcon(QPixmap(ICON64),Popup,0,"tray"); + FloatingIcon* Icon = new FloatingIcon(QPixmap(ICON64),&trayMenuPopup,0,"tray"); connect(Icon, SIGNAL( clicked()), this, SLOT( iconClick())); connect(Icon, SIGNAL( closed()), qApp, SLOT( quit())); Icon->show(); @@ -32,7 +28,7 @@ LayoutManager::LayoutManager( bool useTrayIcon ) { } QString LayoutManager::getFileName( QString layoutname ) { - return settingsDir + layoutname + ".lyt"; + return QString("%1%2.lyt").arg(settingsDir, layoutname); } bool LayoutManager::load(const QString& name) { @@ -65,42 +61,65 @@ bool LayoutManager::load(const QString& name) { //start reading joypads! QTextStream stream( &file ); - QString input = stream.readLine().toLower(); - QRegExp quoted("\"(.*)\""); - bool okay; - int num; + bool okay = false; + int num = 0; + QChar ch = 0; + QString word; + + while (!stream.atEnd()) { + stream >> word; + + if (word.isNull()) + break; - while (input != QString::null) { - QStringList words = input.split(" "); //if this line is specifying a joystick - if (words[0] == "joystick") { - num = words[1].toInt(&okay); + if (word.compare(QLatin1String("joystick"), Qt::CaseInsensitive) == 0) { + stream >> word; + num = word.toInt(&okay); //make sure the number of the joystick is valid - if (!okay || okay < 1) { - error( "Load error", "Error reading joystick definition. Expected: Joysyick 1 {"); - if (name != CurrentLayout) reload(); + if (!okay || num < 1) { + error( "Load error", QString("Error reading joystick definition. Unexpected token \"%1\". Expected a positive number.").arg(word)); + if (name != currentLayout) reload(); else clear(); return false; } + stream.skipWhiteSpace(); + stream >> ch; + if (ch != QChar('{')) { + error( "Load error", QString("Error reading joystick definition. Unexpected character \"%1\". Expected '{'.").arg(ch)); + if (name != currentLayout) reload(); + else clear(); + return false; + } + int index = num - 1; //if there was no joypad defined for this index before, make it now! - if (joypads[num-1] == 0) { - joypads.insert(num-1, new JoyPad(num-1, 0)); + if (joypads[index] == 0) { + joypads.insert(index, new JoyPad(index, -1)); } //try to read the joypad, report error on fail. - if (!joypads[num-1]->readConfig(stream)) { - error( "Load error", "Error reading definition for joystick " + QString::number(num-1)); + if (!joypads[index]->readConfig(stream)) { + error( "Load error", QString("Error reading definition for joystick %1.").arg(index)); //if this was attempting to change to a new layout and it failed, //revert back to the old layout. - if (name != CurrentLayout) reload(); + if (name != currentLayout) reload(); //to keep from going into an infinite loop, if there is no good //layout to fall back on, go to NL. else clear(); return false; } } - //read a new line. - input = stream.readLine().toLower(); + else if (word == QLatin1String("#")) { + // ignore comment + stream.readLine(); + } + else { + error("Load error", QString("Error reading joystick definition. Unexpected token \"%1\". Expected \"Joystick\".").arg(word)); + if (name != currentLayout) reload(); + else clear(); + return false; + } } + //if loading succeeded, this is our new layout. setLayoutName(name); return true; @@ -115,7 +134,7 @@ bool LayoutManager::load() { name = stream.readLine(); file.close(); //if there was no name, don't load. - if (name == "") { + if (name.isEmpty()) { return false; } //if there was a name, try to load it! Note, this will still return @@ -127,7 +146,7 @@ bool LayoutManager::load() { } bool LayoutManager::reload() { - return load(CurrentLayout); + return load(currentLayout); } void LayoutManager::clear() { @@ -140,13 +159,13 @@ void LayoutManager::clear() { } void LayoutManager::save() { - if (CurrentLayout == NL) { + if (currentLayout == NL) { saveAs(); return; } //get a filename - QString filename = getFileName( CurrentLayout ); + QString filename = getFileName( currentLayout ); QFile file(filename); //if it's good, start writing the file if (file.open(QIODevice::WriteOnly)) { @@ -194,15 +213,15 @@ void LayoutManager::saveDefault() { QFile file( settingsDir + "layout"); if (file.open(QIODevice::WriteOnly)) { QTextStream stream(&file); - stream << CurrentLayout; + stream << currentLayout; file.close(); } } void LayoutManager::remove() { - if (CurrentLayout == NL) return; + if (currentLayout == NL) return; if (QMessageBox::warning( 0, NAME" - Delete layout?","Remove layout permanently from your hard drive?", "Yes", "No", 0, 0, 1 ) == 1) return; - QString filename = getFileName( CurrentLayout ); + QString filename = getFileName( currentLayout ); if (!QFile(filename).remove()) { error("Remove error", "Could not remove file " + filename); } @@ -214,12 +233,12 @@ void LayoutManager::remove() { clear(); } -QStringList LayoutManager::getLayoutNames() { +QStringList LayoutManager::getLayoutNames() const { //goes through the list of .lyt files and removes the file extensions ;) QStringList result = QDir(settingsDir).entryList(QStringList("*.lyt")); - for ( QStringList::Iterator it = result.begin(); it != result.end(); ++it ) { - *it = (*it).left((*it).length() - 4); + for (int i = 0; i < result.size(); ++ i) { + result[i] = result[i].left(result[i].length() - 4); } //and, of course, there's always NL. result.prepend(NL); @@ -228,7 +247,7 @@ QStringList LayoutManager::getLayoutNames() { } void LayoutManager::setLayoutName(QString name) { - CurrentLayout = name; + currentLayout = name; fillPopup(); if (le != NULL) { @@ -238,7 +257,7 @@ void LayoutManager::setLayoutName(QString name) { void LayoutManager::iconClick() { //don't show the dialog if there aren't any joystick devices plugged in - if (available.count() == 0) { + if (available.isEmpty()) { error("No joystick devices available","No joystick devices are currently available to configure.\nPlease plug in a gaming device and select\n\"Update Joystick Devices\" from the popup menu."); return; } @@ -247,7 +266,7 @@ void LayoutManager::iconClick() { } //otherwise, make a new LayoutEdit dialog and show it. le = new LayoutEdit(this); - le->setLayout(CurrentLayout); + le->setLayout(currentLayout); } void LayoutManager::trayClick(QSystemTrayIcon::ActivationReason reason) { @@ -259,7 +278,7 @@ void LayoutManager::trayClick(QSystemTrayIcon::ActivationReason reason) { void LayoutManager::trayMenu(QAction *menuItemAction) { //if they clicked on a Layout name, load it! //note that the other options are handled with their own special functions - if (Popup->actions().indexOf(menuItemAction) > 1 && menuItemAction->text() != "Quit" && + if (trayMenuPopup.actions().indexOf(menuItemAction) > 1 && menuItemAction->text() != "Quit" && menuItemAction->text() != "Update lyaout list" && menuItemAction->text() != "Update joystick devices") { load(menuItemAction->text()); @@ -268,42 +287,40 @@ void LayoutManager::trayMenu(QAction *menuItemAction) { void LayoutManager::fillPopup() { //start with an empty slate - Popup->clear(); + trayMenuPopup.clear(); //make a list of joystick devices QString devs = "Joysticks: "; - QHashIterator it( available ); - while (it.hasNext()) - { - it.next(); - devs += QString::number(it.key() + 1) + " "; + foreach (JoyPad *joypad, available) { + devs += QString("%1 ").arg(joypad->getIndex() + 1); } - QAction *temp = Popup->addAction(devs); - Popup->addSeparator(/*temp*/); + + QAction *temp = trayMenuPopup.addAction(devs); + trayMenuPopup.addSeparator(/*temp*/); //add in the Update options QAction *tempAdd = new QAction("Update layout list", this); connect(tempAdd, SIGNAL(triggered(bool)), this, SLOT(fillPopup())); - Popup->addAction(tempAdd); + trayMenuPopup.addAction(tempAdd); tempAdd = new QAction("Update joystick devices", this); connect(tempAdd, SIGNAL(triggered(bool)), this, SLOT(updateJoyDevs())); - Popup->addAction(tempAdd); - Popup->addSeparator(/*temp*/); + trayMenuPopup.addAction(tempAdd); + trayMenuPopup.addSeparator(/*temp*/); //then add all the layout names QStringList names = getLayoutNames(); - for ( QStringList::Iterator it = names.begin(); it != names.end(); ++it ) { - temp = Popup->addAction(*it); + foreach (const QString &name, names) { + temp = trayMenuPopup.addAction(name); temp->setCheckable(true); //put a check by the current one ;) - if (CurrentLayout == (*it)) { + if (currentLayout == name) { temp->setChecked(true); } } - Popup->addSeparator(); + trayMenuPopup.addSeparator(); //and, at the end, quit! - Popup->addAction("Quit",qApp,SLOT(quit())); + trayMenuPopup.addAction("Quit",qApp,SLOT(quit())); } void LayoutManager::updateJoyDevs() { @@ -318,30 +335,31 @@ void LayoutManager::updateJoyDevs() { available.clear(); //set all joydevs anew (create new JoyPad's if necesary) - QDir DeviceDir(devdir); - QStringList devices = DeviceDir.entryList(QStringList("js*"), QDir::System ); + QDir deviceDir(devdir); + QStringList devices = deviceDir.entryList(QStringList("js*"), QDir::System); QRegExp devicename(".*\\js(\\d+)"); - int joydev; - int index; + int joydev = 0; + int index = 0; //for every joystick device in the directory listing... //(note, with devfs, only available devices are listed) - for (QStringList::Iterator it = devices.begin(); it != devices.end(); ++it) { - debug_mesg("found a device file, %s\n", qPrintable(devdir + "/" + (*it))); + foreach (const QString &device, devices) { + QString devpath = QString("%1/%2").arg(devdir, device); + debug_mesg("found a device file, %s\n", qPrintable(devpath)); //try opening the device. - joydev = open( qPrintable(devdir + "/" + (*it)), O_RDONLY | O_NONBLOCK); + joydev = open( qPrintable(devpath), O_RDONLY | O_NONBLOCK); //if it worked, then we have a live joystick! Make sure it's properly //setup. if (joydev > 0) { - devicename.indexIn(*it); - index = QString(devicename.cap(1)).toInt(); - JoyPad* joypad; + devicename.indexIn(device); + index = devicename.cap(1).toInt(); + JoyPad* joypad = joypads[index]; //if we've never seen this device before, make a new one! - if (joypads[index] == 0) { + if (joypad == 0) { struct pollfd read_struct; read_struct.fd = joydev; read_struct.events = POLLIN; char buf[10]; - while(poll(&read_struct, 1, 5)!=0) { + while (poll(&read_struct, 1, 5) != 0) { debug_mesg("reading junk data\n"); if (read(joydev, buf, 10) <= 0) break; } @@ -350,15 +368,13 @@ void LayoutManager::updateJoyDevs() { } else { debug_mesg("found previously open joypad with index %d, ignoring", index); - joypad = joypads[index]; joypad->resetToDev(joydev); } //make this joystick device available. available.insert(index,joypad); } else { - int errsv = errno; - printf("error reading joypad dev device: %s\n", strerror(errsv)); + perror(qPrintable(devpath)); } } //when it's all done, rebuild the popup menu so it displays the correct diff --git a/src/layout.h b/src/layout.h index aa61319..64ef938 100644 --- a/src/layout.h +++ b/src/layout.h @@ -31,19 +31,15 @@ //for recognizing when the special empty layout is in use #define NL "[NO LAYOUT]" -//where QJoyPad saves its settings! -const QString settingsDir(QDir::homePath() + "/.qjoypad3/"); - -extern QString devdir; - //handles loading, saving, and changing of layouts class LayoutManager : public QObject { friend class LayoutEdit; Q_OBJECT public: - LayoutManager( bool useTrayIcon); + LayoutManager(bool useTrayIcon, const QString &devdir, const QString &settingsDir); + //produces a list of the names of all the available layout. - QStringList getLayoutNames(); + QStringList getLayoutNames() const; void leWindowClosed(); public slots: //load a layout with a given name @@ -79,13 +75,16 @@ class LayoutManager : public QObject { void setLayoutName(QString name); //get the file name for a layout name QString getFileName( QString layoutname ); + //the directory in wich the joystick devices are (e.g. "/dev/input") + QString devdir; + QString settingsDir; //the layout that is currently in use - QString CurrentLayout; + QString currentLayout; //the popup menu from the tray/floating icon - QMenu* Popup; + QMenu trayMenuPopup; //if there is a LayoutEdit open, this points to it. Otherwise, NULL. - LayoutEdit* le; + LayoutEdit* le; }; #endif diff --git a/src/layout_edit.cpp b/src/layout_edit.cpp index af90047..cc18dca 100644 --- a/src/layout_edit.cpp +++ b/src/layout_edit.cpp @@ -98,7 +98,7 @@ void LayoutEdit::updateLayoutList() { CLayouts->clear(); QStringList layouts = lm->getLayoutNames(); CLayouts->insertItems(-1, layouts); - CLayouts->setCurrentIndex(layouts.indexOf(lm->CurrentLayout)); + CLayouts->setCurrentIndex(layouts.indexOf(lm->currentLayout)); } void LayoutEdit::updateJoypadWidgets() { diff --git a/src/main.cpp b/src/main.cpp index 2807253..2a69cc7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "error.h" #include #include +#include #include //for making universally available variables @@ -25,15 +26,14 @@ QHash available; //to device.h QHash joypads; //to device.h //variables needed in various functions in this file -LayoutManager* lm = 0; -QString devdir = DEVDIR; +QPointer layoutManagerPtr; //signal handler for SIGIO //SIGIO means that a new layout should be loaded. It is saved in // ~/.qjoypad/layout, where the last used layout is put. void catchSIGIO( int sig ) { - lm->load(); + if (layoutManagerPtr) layoutManagerPtr->load(); //remember to catch this signal again next time. signal( sig, catchSIGIO ); } @@ -44,7 +44,7 @@ void catchSIGIO( int sig ) //SIGUSR1 means that we should update the available joystick device list. void catchSIGUSR1( int sig ) { //buildJoyDevices(); - lm->updateJoyDevs(); + if (layoutManagerPtr) layoutManagerPtr->updateJoyDevs(); //remember to catch this signal again next time. signal( sig, catchSIGUSR1 ); } @@ -67,9 +67,15 @@ int main( int argc, char **argv ) a.setQuitOnLastWindowClosed(false); + //where QJoyPad saves its settings! + const QString settingsDir(QDir::homePath() + "/.qjoypad3/"); + //where to look for settings. If it does not exist, it will be created QDir dir(settingsDir); + //the directory in wich the joystick devices are (e.g. "/dev/input") + QString devdir = DEVDIR; + //if there is no new directory and we can't make it, complain if (!dir.exists() && !dir.mkdir(settingsDir)) { printf("Couldn't create the QJoyPad save directory (%s)!", settingsDir.toStdString().c_str()); @@ -236,14 +242,15 @@ int main( int argc, char **argv ) //create a new LayoutManager with a tray icon / floating icon, depending //on the user's request - lm = new LayoutManager(useTrayIcon); + LayoutManager layoutManager(useTrayIcon,devdir,settingsDir); + layoutManagerPtr = &layoutManager; //build the joystick device list for the first time, //buildJoyDevices(); - lm->updateJoyDevs(); + layoutManager.updateJoyDevs(); //load the last used layout (Or the one given as a command-line argument) - lm->load(); + layoutManager.load(); //prepare the signal handlers signal( SIGIO, catchSIGIO ); @@ -254,7 +261,7 @@ int main( int argc, char **argv ) int result = a.exec(); //when everything is done, save the current layout for next time... - lm->saveDefault(); + layoutManager.saveDefault(); //remove the lock file... pidFile.remove(); diff --git a/src/quickset.cpp b/src/quickset.cpp index 57aa636..d900bc9 100644 --- a/src/quickset.cpp +++ b/src/quickset.cpp @@ -2,8 +2,8 @@ #include "getkey.h" //build the dialog -QuickSet::QuickSet( JoyPad* jp) - : QDialog() { +QuickSet::QuickSet( JoyPad* jp, QWidget *parent) + : QDialog(parent) { setting = false; joypad = jp; setWindowTitle("Set " + jp->getName()); diff --git a/src/quickset.h b/src/quickset.h index 10f6e89..398cc65 100644 --- a/src/quickset.h +++ b/src/quickset.h @@ -19,7 +19,7 @@ class JoyPad; //a dialog to quickly set a key to a button class QuickSet : public QDialog { public: - QuickSet(JoyPad* jp); + QuickSet(JoyPad* jp, QWidget *parent = 0); //this needs to see js_events so it can capture them directly void jsevent( js_event msg ); private: