From cfa17f894715458be2c1b47ba8ad55d7ae2976d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Panzenb=C3=B6ck?= Date: Sun, 16 Feb 2014 04:37:11 +0100 Subject: [PATCH] globals--, fmt string fix, foreeach++, better names++, close fds, tray icon activates/closes --- src/axis.cpp | 3 +- src/axisw.cpp | 2 +- src/button.cpp | 5 +- src/buttonw.cpp | 2 +- src/error.h | 4 +- src/event.cpp | 2 +- src/event.h | 4 +- src/flash.cpp | 58 ++++++++-------- src/flash.h | 14 ++-- src/getkey.cpp | 3 +- src/getkey.h | 2 - src/joypad.cpp | 166 +++++++++++++++++++++++--------------------- src/joypad.h | 28 ++++---- src/joypadw.cpp | 23 +++--- src/joypadw.h | 2 +- src/keycode.cpp | 3 +- src/keycode.h | 3 - src/layout.cpp | 46 +++++++----- src/layout.h | 5 +- src/layout_edit.cpp | 9 +-- src/layout_edit.h | 5 +- src/main.cpp | 10 +-- src/quickset.cpp | 13 ++-- src/quickset.h | 2 +- 24 files changed, 208 insertions(+), 206 deletions(-) diff --git a/src/axis.cpp b/src/axis.cpp index 5b1f911..43baf5b 100644 --- a/src/axis.cpp +++ b/src/axis.cpp @@ -1,3 +1,4 @@ +#include #include "axis.h" #include "event.h" #include "time.h" @@ -423,5 +424,5 @@ void Axis::move( bool press ) { } } //actually create the event - sendevent(display, e); + sendevent(QX11Info::display(), e); } diff --git a/src/axisw.cpp b/src/axisw.cpp index e85bd0c..d8e6e80 100644 --- a/src/axisw.cpp +++ b/src/axisw.cpp @@ -2,7 +2,7 @@ AxisWidget::AxisWidget( Axis* a, QWidget* parent ) - : FlashButton( "",parent) { + : FlashButton(QString::null, QString::null, parent) { axis = a; ae = NULL; update(); diff --git a/src/button.cpp b/src/button.cpp index 1b9d84b..fdc97be 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -1,3 +1,4 @@ +#include #include "button.h" #include "event.h" @@ -125,7 +126,7 @@ bool Button::isDefault() { QString Button::status() { if (useMouse) { - return QString("%1 : Mouse %2").arg(getName(), keycode); + return QString("%1 : Mouse %2").arg(getName()).arg(keycode); } else { return QString("%1 : %2").arg(getName(), ktos(keycode)); @@ -161,7 +162,7 @@ void Button::click( bool press ) { click.value1 = keycode; click.value2 = 0; //and send it. - sendevent( display, click ); + sendevent( QX11Info::display(), click ); } void Button::timerCalled() { diff --git a/src/buttonw.cpp b/src/buttonw.cpp index 0211b8c..67af257 100644 --- a/src/buttonw.cpp +++ b/src/buttonw.cpp @@ -2,7 +2,7 @@ ButtonWidget::ButtonWidget( Button* b, QWidget* parent ) - : FlashButton( "", parent) { + : FlashButton(QString::null, QString::null, parent) { button = b; update(); on = false; diff --git a/src/error.h b/src/error.h index 7866de7..0ca9c23 100644 --- a/src/error.h +++ b/src/error.h @@ -18,8 +18,6 @@ inline void debug_mesg(const char *fmt, ...) { va_end(ap); } #else -inline void debug_mesg(...) { - return; -} +inline void debug_mesg(...) {} #endif #endif diff --git a/src/event.cpp b/src/event.cpp index ae64e6b..8e8810d 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -4,7 +4,7 @@ Display *display = 0; //actually creates an XWindows event :) -void sendevent( Display* display, xevent e ) { +void sendevent(Display* display, const xevent &e ) { if (e.value1 == 0 && e.value2 == 0) return; if (e.type == WARP) { XTestFakeRelativeMotionEvent(display, e.value1, e.value2, 0); diff --git a/src/event.h b/src/event.h index d1917bf..8356403 100644 --- a/src/event.h +++ b/src/event.h @@ -16,8 +16,6 @@ struct xevent { int value2; //y }; -extern Display* display; - -void sendevent( Display* display, xevent e ); +void sendevent( Display* display, const xevent& e ); #endif diff --git a/src/flash.cpp b/src/flash.cpp index 02c4078..6cf791e 100644 --- a/src/flash.cpp +++ b/src/flash.cpp @@ -4,7 +4,7 @@ //Modified here (and in .h) to not have default arguments for 2 and 3. //This caused an error with a development version of g++ on a Mandrake system //in Sweden. -FlashButton::FlashButton( QString text, QWidget* parent, QString name ) +FlashButton::FlashButton( QString text, QString name, QWidget* parent ) :QPushButton( text, parent ) { this->setObjectName(name); @@ -47,41 +47,38 @@ FlashRadioArray::FlashRadioArray( const QStringList &names, bool horizontal, QWi :QWidget( parent ) { if (horizontal) { - LMain = new QHBoxLayout( this); - LMain->setMargin(5); - LMain->setSpacing(5); + mainLayout = new QHBoxLayout( this); + mainLayout->setMargin(5); + mainLayout->setSpacing(5); } else { - LMain = new QVBoxLayout( this); - LMain->setMargin(5); - LMain->setSpacing(5); + mainLayout = new QVBoxLayout( this); + mainLayout->setMargin(5); + mainLayout->setSpacing(5); } -// TODO: fix memleak - int count = names.size(); - int i = 0; - Buttons = new FlashButton*[count]; foreach (const QString &name, names) { - Buttons[i] = new FlashButton( name, this, "" ); + FlashButton *button = new FlashButton( name, QString::null, this ); + buttons.append(button); //when any of the buttons is clicked, it calls the same function on this. - connect( Buttons[i], SIGNAL( clicked() ), this, SLOT( clicked() )); - LMain->addWidget(Buttons[i]); - ++i; + connect( button, SIGNAL( clicked() ), this, SLOT( clicked() )); + mainLayout->addWidget(button); } - Count = count; - State = 0; - if (count > 0) { - Buttons[0]->setDown( true ); + state = 0; + if (!buttons.isEmpty()) { + buttons[0]->setDown( true ); } } int FlashRadioArray::getState() { - return State; + return state; } void FlashRadioArray::flash( int index ) { - Buttons[index]->flash(); + if (index < buttons.size()) { + buttons[index]->flash(); + } } void FlashRadioArray::clicked() @@ -89,15 +86,16 @@ void FlashRadioArray::clicked() //go through each button. If it wasn't the button that was just clicked, //then make sure that it is up. If it WAS the button that was clicked, //remember that index as the new state. - for (int i = 0; i < Count; i++) - { - if ( Buttons[i] != sender() ) - Buttons[i]->setDown( false ); - else - { - State = i; - Buttons[i]->setDown( true ); + int i = 0; + foreach (FlashButton *button, buttons) { + if ( button != sender() ) { + button->setDown( false ); } + else { + state = i; + button->setDown( true ); + } + ++ i; } - emit changed( State ); + emit changed( state ); } diff --git a/src/flash.h b/src/flash.h index 65c0e4a..30cc518 100644 --- a/src/flash.h +++ b/src/flash.h @@ -30,7 +30,7 @@ class FlashButton : public QPushButton { Q_OBJECT public: - FlashButton( QString text, QWidget* parent, QString name = "" ); + FlashButton(QString text, QString name = "" , QWidget* parent = 0); public slots: //make the button turn blue if it was gray, or visa versa. void flash(); @@ -51,7 +51,7 @@ class FlashRadioArray : public QWidget { Q_OBJECT public: - FlashRadioArray( const QStringList &names, bool horizontal, QWidget* parent); + FlashRadioArray( const QStringList &names, bool horizontal, QWidget* parent); //returns an integer returning the currently selected button. //First button is 0. int getState(); @@ -64,15 +64,13 @@ class FlashRadioArray : public QWidget signals: //happens when the state changes. The int is the new state. void changed( int ); - private: - //how many buttons - int Count; + private: //which is currently down - int State; + int state; //the array of buttons - FlashButton** Buttons; + QList buttons; //the main layout. - QBoxLayout* LMain; + QBoxLayout* mainLayout; }; #endif diff --git a/src/getkey.cpp b/src/getkey.cpp index 3a561f6..8c93c47 100644 --- a/src/getkey.cpp +++ b/src/getkey.cpp @@ -1,3 +1,4 @@ +#include #include "getkey.h" GetKey::GetKey( QString button, bool m ) @@ -33,7 +34,7 @@ bool GetKey::x11Event( XEvent* e ) //On a key press, return the key and quit //Ctrl+X == [No Key] if (e->type == KeyRelease) { - if (XKeycodeToKeysym(display,e->xkey.keycode,0) == XK_x ) { + if (XKeycodeToKeysym(QX11Info::display(),e->xkey.keycode,0) == XK_x ) { if (e->xkey.state & ControlMask) done( 0 ); else done( e->xkey.keycode ); } diff --git a/src/getkey.h b/src/getkey.h index eac03e0..35e0b74 100644 --- a/src/getkey.h +++ b/src/getkey.h @@ -10,8 +10,6 @@ //The KeySym for "x" #define XK_x 0x078 -extern Display *display; - //a keycode dialog box class GetKey : public QDialog { Q_OBJECT diff --git a/src/joypad.cpp b/src/joypad.cpp index 6a5b5c4..c84166d 100644 --- a/src/joypad.cpp +++ b/src/joypad.cpp @@ -1,21 +1,24 @@ -#include "unistd.h" -#include "joypad.h" +#include #include +#include +#include #include -#include +#include #include #include -JoyPad::JoyPad( int i, int dev, QObject *parent ) : QObject(parent) { +#include "joypad.h" + +JoyPad::JoyPad( int i, int dev, QObject *parent ) + : QObject(parent), joydev(-1), axisCount(0), buttonCount(0), jpw(0), readNotifier(0), errorNotifier(0) { debug_mesg("Constructing the joypad device with index %d and fd %d\n", i, dev); //remember the index, index = i; //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); + open(dev); debug_mesg("done resetting and setting up device index %d\n", i); char id[256]; memset(id, 0, sizeof(id)); @@ -28,70 +31,96 @@ JoyPad::JoyPad( int i, int dev, QObject *parent ) : QObject(parent) { } else { debug_mesg("This joypad does not have a valid file handle, not setting up event listeners\n"); } - //there is no JoyPadWidget yet. - jpw = NULL; debug_mesg("Done constructing the joypad device %d\n", i); } -void JoyPad::resetToDev(int dev ) { +JoyPad::~JoyPad() { + close(); +} + +void JoyPad::close() { + if (readNotifier) { + disconnect(readNotifier, 0, 0, 0); + + readNotifier->blockSignals(true); + readNotifier->setEnabled(false); + + delete readNotifier; + readNotifier = 0; + } + if (errorNotifier) { + disconnect(errorNotifier, 0, 0, 0); + + errorNotifier->blockSignals(true); + errorNotifier->setEnabled(false); + + delete errorNotifier; + errorNotifier = 0; + } + if (joydev >= 0) { + if (::close(joydev) != 0) { + debug_mesg("close(js%d %d): %s\n", index, joydev, strerror(errno)); + } + joydev = -1; + } +} + +void JoyPad::open(int dev) { debug_mesg("resetting to dev\n"); //remember the device file descriptor + close(); joydev = dev; //read in the number of axes / buttons - axes = 0; - ioctl (joydev, JSIOCGAXES, &axes); - buttons = 0; - ioctl (joydev, JSIOCGBUTTONS, &buttons); + axisCount = 0; + ioctl (joydev, JSIOCGAXES, &axisCount); + buttonCount = 0; + ioctl (joydev, JSIOCGBUTTONS, &buttonCount); //make sure that we have the axes we need. //if one that we need doesn't yet exist, add it in. //Note: if the current layout has a key assigned to an axis that did not //have a real joystick axis mapped to it, and this function suddenly brings //that axis into use, the key assignment will not be lost because the axis //will already exist and no new axis will be created. - for (int i = 0; i < axes; i++) { - if (Axes[i] == 0) Axes.insert(i, new Axis( i, this )); + for (int i = 0; i < axisCount; i++) { + if (axes[i] == 0) axes.insert(i, new Axis( i, this )); } - for (int i = 0; i < buttons; i++) { - if (Buttons[i] == 0) Buttons.insert(i, new Button( i, this )); + for (int i = 0; i < buttonCount; i++) { + if (buttons[i] == 0) buttons.insert(i, new Button( i, this )); } 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; } - setupJoyDeviceListener(dev); - debug_mesg("done resetting to dev\n"); -} - -void JoyPad::setupJoyDeviceListener(int dev) { debug_mesg("Setting up joyDeviceListeners\n"); - joydevFileHandle = new QSocketNotifier(dev, QSocketNotifier::Read, this); - connect(joydevFileHandle, SIGNAL(activated(int)), this, SLOT(handleJoyEvents(int))); - joydevFileException = new QSocketNotifier(dev, QSocketNotifier::Exception, this); - connect(joydevFileException, SIGNAL(activated(int)), this, SLOT(errorRead(int))); + readNotifier = new QSocketNotifier(joydev, QSocketNotifier::Read, this); + connect(readNotifier, SIGNAL(activated(int)), this, SLOT(handleJoyEvents())); + errorNotifier = new QSocketNotifier(joydev, QSocketNotifier::Exception, this); + connect(errorNotifier, SIGNAL(activated(int)), this, SLOT(handleJoyEvents())); debug_mesg("Done setting up joyDeviceListeners\n"); + debug_mesg("done resetting to dev\n"); } void JoyPad::toDefault() { //to reset the whole, reset all the parts. - foreach (Axis *axis, Axes) { + foreach (Axis *axis, axes) { axis->toDefault(); } - foreach (Button *button, Buttons) { + foreach (Button *button, buttons) { button->toDefault(); } } bool JoyPad::isDefault() { //if any of the parts are not at default, then the whole isn't either. - foreach (Axis *axis, Axes) { + foreach (Axis *axis, axes) { if (!axis->isDefault()) return false; } - foreach (Button *button, Buttons) { + foreach (Button *button, buttons) { if (!button->isDefault()) return false; } return true; @@ -115,10 +144,10 @@ bool JoyPad::readConfig( QTextStream &stream ) { 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] == 0) { + buttons.insert(num-1,new Button(num-1)); } - if (!Buttons[num-1]->read( stream )) { + if (!buttons[num-1]->read( stream )) { error("Layout file error", QString("Error reading Button %1").arg(num)); return false; } @@ -135,10 +164,10 @@ bool JoyPad::readConfig( QTextStream &stream ) { 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] == 0) { + axes.insert(num-1,new Axis(num-1)); } - if (!Axes[num-1]->read(stream)) { + if (!axes[num-1]->read(stream)) { error("Layout file error", QString("Error reading Axis %1").arg(num)); return false; } @@ -155,12 +184,12 @@ bool JoyPad::readConfig( QTextStream &stream ) { //only actually writes something if this JoyPad is NON DEFAULT. void JoyPad::write( QTextStream &stream ) { - if (!Axes.empty() || !Buttons.empty()) { + if (!axes.empty() || !buttons.empty()) { stream << getName() << " {\n"; - foreach (Axis *axis, Axes) { + foreach (Axis *axis, axes) { axis->write(stream); } - foreach (Button *button, Buttons) { + foreach (Button *button, buttons) { button->write(stream); } stream << "}\n\n"; @@ -168,15 +197,15 @@ void JoyPad::write( QTextStream &stream ) { } void JoyPad::release() { - foreach (Axis *axis, Axes) { + foreach (Axis *axis, axes) { axis->release(); } - foreach (Button *button, Buttons) { + foreach (Button *button, buttons) { button->release(); } } -void JoyPad::jsevent( js_event msg ) { +void JoyPad::jsevent(const js_event &msg) { //if there is a JoyPadWidget around, ie, if the joypad is being edited if (jpw != NULL && hasFocus) { //tell the dialog there was an event. It will use this to flash @@ -190,15 +219,18 @@ void JoyPad::jsevent( js_event msg ) { //otherwise, lets create us a fake event! Pass on the event to whichever //Button or Axis was pressed and let them decide what to do with it. - if (msg.type & JS_EVENT_AXIS) { + qulonglong type = msg.type & ~JS_EVENT_INIT; + if (type == JS_EVENT_AXIS) { debug_mesg("DEBUG: passing on an axis event\n"); debug_mesg("DEBUG: %d %d\n", msg.number, msg.value); - Axes[msg.number]->jsevent(msg.value); + Axis *axis = axes[msg.number]; + if (axis) axis->jsevent(msg.value); } - else { + else if (type == JS_EVENT_BUTTON) { debug_mesg("DEBUG: passing on a button event\n"); debug_mesg("DEBUG: %d %d\n", msg.number, msg.value); - Buttons[msg.number]->jsevent(msg.value); + Button *button = buttons[msg.number]; + if (button) button->jsevent(msg.value); } } @@ -208,15 +240,11 @@ JoyPadWidget* JoyPad::widget( QWidget* parent, int i) { return jpw; } -void JoyPad::handleJoyEvents(int fd) { - Q_UNUSED(fd); - +void JoyPad::handleJoyEvents() { js_event msg; - int len; - - len = read( joydev, &msg, sizeof(js_event)); + ssize_t len = read(joydev, &msg, sizeof(js_event)); //if there was a real event waiting, - if (len == (int) sizeof(js_event)) { + if (len == sizeof(js_event)) { //pass that event on to the joypad! jsevent(msg); } @@ -227,30 +255,10 @@ void JoyPad::releaseWidget() { jpw = NULL; } -void JoyPad::unsetDev() { - close(joydev); - joydev = -1; - if(joydevFileHandle != NULL) { - delete joydevFileHandle; - } -} - -void JoyPad::errorRead(int fd) { - debug_mesg("There was an error reading off of the device with fd %d, disabling\n", fd); - joydevFileHandle->blockSignals(true); - joydevFileHandle->setEnabled(false); - close(joydev); - if(disconnect(joydevFileHandle , 0, 0, 0)) { - joydevFileHandle->deleteLater(); - joydevFileHandle = NULL; - } - if(disconnect(joydevFileException, 0, 0, 0)) { - joydevFileException->setEnabled(false); - joydevFileException->deleteLater(); - joydevFileException = NULL; - } - joydev = -1; - debug_mesg("Done disabling device with fd %d\n", fd); +void JoyPad::errorRead() { + debug_mesg("There was an error reading off of the device with fd %d, disabling\n", joydev); + close(); + debug_mesg("Done disabling device with fd %d\n", joydev); } void JoyPad::focusChange(bool focusState) { diff --git a/src/joypad.h b/src/joypad.h index cb8f8f5..b6e70e1 100644 --- a/src/joypad.h +++ b/src/joypad.h @@ -28,8 +28,10 @@ class JoyPad : public QObject { friend class JoyPadWidget; friend class QuickSet; public: - void setupJoyDeviceListener(int dev); JoyPad( int i, int dev, QObject* parent ); + ~JoyPad(); + // close file descriptor and socket notifier + void close(); //read from a stream bool readConfig( QTextStream &stream ); //write to a stream @@ -37,15 +39,13 @@ class JoyPad : public QObject { //release any pushed buttons and return to a neutral state void release(); //handle an event from the joystick device this is associated with - void jsevent( js_event msg ); + void jsevent( const js_event& msg ); //reset to default settings void toDefault(); //true iff this is currently at default settings - bool isDefault(); - //release the connection to the real joystick - void unsetDev(); + bool isDefault(); //read the dimensions on the real joystick and use them - void resetToDev( int dev ); + void open( int dev ); //generates a name ("Joystick 1") QString getName() const { return QString("Joystick %1").arg(index+1);} int getIndex() const { return index; } @@ -54,8 +54,8 @@ class JoyPad : public QObject { //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 - int buttons; //the number of buttons + char axisCount; //the number of axes available on this device + char buttonCount; //the number of buttons public: //request the joypad to make a JoyPadWidget. We create them this way @@ -68,21 +68,21 @@ class JoyPad : public QObject { //layouts with different numbers of axes/buttons than the current //devices. Note that with the current layout settings, the defined //buttons that don't actually exist on the device may not be contiguous. - QHash Axes; - QHash Buttons; + QHash axes; + QHash buttons; //the index of this device (devicenum) int index; //the widget that edits this. Mainly I keep track of this to know if //the joypad is currently being edited. JoyPadWidget* jpw; - QSocketNotifier *joydevFileHandle; - QSocketNotifier *joydevFileException; + QSocketNotifier *readNotifier; + QSocketNotifier *errorNotifier; QString deviceId; bool hasFocus; public slots: - void handleJoyEvents(int fd); - void errorRead(int fd); + void handleJoyEvents(); + void errorRead(); void focusChange(bool windowHasFocus); }; diff --git a/src/joypadw.cpp b/src/joypadw.cpp index 7000833..25db63b 100644 --- a/src/joypadw.cpp +++ b/src/joypadw.cpp @@ -15,33 +15,32 @@ JoyPadWidget::JoyPadWidget( JoyPad* jp, int i, QWidget* parent ) int insertCounter = 0; quickset = NULL; - foreach (Axis *axis, joypad->Axes) { + 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); + layoutMain->addWidget(aw, insertCounter / 2, insertCounter % 2); insertCounter++; } - foreach (Button *button, joypad->Buttons) { + 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); + layoutMain->addWidget(bw, insertCounter / 2, insertCounter % 2); insertCounter++; } - if (insertCounter % 2 == 1) { insertCounter ++; } insertCounter += 2; btnClear = new QPushButton("Clear", this); connect(btnClear, SIGNAL(clicked()), this, SLOT(clear())); - layoutMain->addWidget(btnClear, insertCounter / 2, insertCounter %2); + layoutMain->addWidget(btnClear, insertCounter / 2, insertCounter % 2); insertCounter++; btnAll = new QPushButton("Quick Set", this); - layoutMain->addWidget(btnAll, insertCounter /2, insertCounter%2); + layoutMain->addWidget(btnAll, insertCounter / 2, insertCounter % 2); connect(btnAll, SIGNAL(clicked()), this, SLOT(setAll())); } @@ -86,16 +85,18 @@ void JoyPadWidget::setAll() { quickset = NULL; } -void JoyPadWidget::jsevent( js_event msg ) { +void JoyPadWidget::jsevent( const js_event& msg ) { //notify the component this event applies to. this cannot generate anything //other than a flash :) - if (msg.type == JS_EVENT_AXIS) { + qulonglong type = msg.type & ~JS_EVENT_INIT; + if (type == JS_EVENT_AXIS) { if (msg.number < axes.count()) axes[msg.number]->jsevent(msg.value); } - else { + else if (type == JS_EVENT_BUTTON) { 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) + if (quickset != NULL) { quickset->jsevent(msg); + } } diff --git a/src/joypadw.h b/src/joypadw.h index a758309..4581693 100644 --- a/src/joypadw.h +++ b/src/joypadw.h @@ -25,7 +25,7 @@ class JoyPadWidget : public QWidget { JoyPadWidget( JoyPad* jp, int i, QWidget* parent); ~JoyPadWidget(); //takes in an event and decides whether or not to flash anything - void jsevent( js_event msg ); + void jsevent(const js_event &msg ); public slots: //called whenever one of the subwidgets flashes... used to determine //when to emit the flashed() signal. diff --git a/src/keycode.cpp b/src/keycode.cpp index 0607c90..b454b52 100644 --- a/src/keycode.cpp +++ b/src/keycode.cpp @@ -1,3 +1,4 @@ +#include #include "keycode.h" #include "getkey.h" @@ -7,7 +8,7 @@ const QString ktos( int keycode ) if (keycode == 0) return "[NO KEY]"; - QString xname = XKeysymToString(XKeycodeToKeysym(display, keycode,0)); + QString xname = XKeysymToString(XKeycodeToKeysym(QX11Info::display(), keycode,0)); //this section of code converts standard X11 keynames into much nicer names //which are prettier, fit the dialogs better, and are more readily understandable. diff --git a/src/keycode.h b/src/keycode.h index ab7f253..4ed006c 100644 --- a/src/keycode.h +++ b/src/keycode.h @@ -13,9 +13,6 @@ //Produce a string for any keycode const QString ktos( int keycode ); -//The X11 display, taken from main.cpp -extern Display* display; - //a button that requests a keycode from the user when clicked. class KeyButton : public QPushButton { diff --git a/src/layout.cpp b/src/layout.cpp index e848f25..299fc35 100644 --- a/src/layout.cpp +++ b/src/layout.cpp @@ -17,16 +17,23 @@ LayoutManager::LayoutManager( bool useTrayIcon, const QString &devdir, const QSt } //or make a floating icon else { - 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(); + 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(); } //no layout loaded at start. setLayoutName(NL); } +LayoutManager::~LayoutManager() { + if (le) { + le->close(); + le = 0; + } +} + QString LayoutManager::getFileName( QString layoutname ) { return QString("%1%2.lyt").arg(settingsDir, layoutname); } @@ -204,7 +211,7 @@ void LayoutManager::saveAs() { //add the new name to our lists fillPopup(); - if (le != NULL) { + if (le) { le->updateLayoutList(); } } @@ -227,7 +234,7 @@ void LayoutManager::remove() { } fillPopup(); - if (le != NULL) { + if (le) { le->updateLayoutList(); } clear(); @@ -238,7 +245,8 @@ QStringList LayoutManager::getLayoutNames() const { QStringList result = QDir(settingsDir).entryList(QStringList("*.lyt")); for (int i = 0; i < result.size(); ++ i) { - result[i] = result[i].left(result[i].length() - 4); + QString& name = result[i]; + name.truncate(name.length() - 4); } //and, of course, there's always NL. result.prepend(NL); @@ -250,7 +258,7 @@ void LayoutManager::setLayoutName(QString name) { currentLayout = name; fillPopup(); - if (le != NULL) { + if (le) { le->setLayout(name); } } @@ -261,7 +269,13 @@ void LayoutManager::iconClick() { 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; } - if(le) { + if (le) { + if (le->hasFocus()) { + le->close(); + } + else { + le->raise(); + } return; } //otherwise, make a new LayoutEdit dialog and show it. @@ -328,7 +342,7 @@ void LayoutManager::updateJoyDevs() { //reset all joydevs to sentinal value (-1) foreach (JoyPad *joypad, joypads) { - joypad->unsetDev(); + joypad->close(); } //clear out the list of previously available joysticks @@ -338,8 +352,8 @@ void LayoutManager::updateJoyDevs() { QDir deviceDir(devdir); QStringList devices = deviceDir.entryList(QStringList("js*"), QDir::System); QRegExp devicename(".*\\js(\\d+)"); - int joydev = 0; - int index = 0; + int joydev = -1; + int index = -1; //for every joystick device in the directory listing... //(note, with devfs, only available devices are listed) foreach (const QString &device, devices) { @@ -349,7 +363,7 @@ void LayoutManager::updateJoyDevs() { 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) { + if (joydev >= 0) { devicename.indexIn(device); index = devicename.cap(1).toInt(); JoyPad* joypad = joypads[index]; @@ -368,7 +382,7 @@ void LayoutManager::updateJoyDevs() { } else { debug_mesg("found previously open joypad with index %d, ignoring", index); - joypad->resetToDev(joydev); + joypad->open(joydev); } //make this joystick device available. available.insert(index,joypad); @@ -385,7 +399,3 @@ void LayoutManager::updateJoyDevs() { } debug_mesg("done updating joydevs\n"); } - -void LayoutManager::leWindowClosed() { - le=NULL; -} diff --git a/src/layout.h b/src/layout.h index 1d59d6f..3b5e714 100644 --- a/src/layout.h +++ b/src/layout.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -35,10 +36,10 @@ class LayoutManager : public QObject { Q_OBJECT public: LayoutManager(bool useTrayIcon, const QString &devdir, const QString &settingsDir); + ~LayoutManager(); //produces a list of the names of all the available layout. QStringList getLayoutNames() const; - void leWindowClosed(); public slots: //load a layout with a given name bool load(const QString& name); @@ -82,7 +83,7 @@ class LayoutManager : public QObject { QMenu trayMenuPopup; //if there is a LayoutEdit open, this points to it. Otherwise, NULL. - LayoutEdit* le; + QPointer le; QHash available; QHash joypads; diff --git a/src/layout_edit.cpp b/src/layout_edit.cpp index 08c048a..77d47e1 100644 --- a/src/layout_edit.cpp +++ b/src/layout_edit.cpp @@ -130,15 +130,10 @@ void LayoutEdit::updateJoypadWidgets() { } } -void LayoutEdit::closeEvent(QCloseEvent *event) { - lm->leWindowClosed(); - event->accept(); -} - void LayoutEdit::appFocusChanged(QWidget *old, QWidget *now) { - if(now!=NULL && old==NULL) { + if (now != NULL && old == NULL) { emit focusStateChanged(false); - } else if(old!=NULL && now==NULL) { + } else if(old != NULL && now == NULL) { emit focusStateChanged(true); foreach (JoyPad *joypad, lm->available) { debug_mesg("iterating and releasing\n"); diff --git a/src/layout_edit.h b/src/layout_edit.h index 43b9a6d..eb0664a 100644 --- a/src/layout_edit.h +++ b/src/layout_edit.h @@ -16,7 +16,7 @@ class LayoutManager; class LayoutEdit : public QWidget { Q_OBJECT public: - LayoutEdit( LayoutManager* l ); + LayoutEdit( LayoutManager* l ); //swap to a new layout void setLayout(QString layout); //update the list of available layouts @@ -28,8 +28,7 @@ class LayoutEdit : public QWidget { void appFocusChanged(QWidget *old, QWidget *now); protected: //the layout manager this represents - LayoutManager* lm; - virtual void closeEvent(QCloseEvent *event); + LayoutManager* lm; //parts of the dialog: QVBoxLayout *mainLayout; QStackedWidget *padStack; diff --git a/src/main.cpp b/src/main.cpp index d68cbcc..ebcd27f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,6 @@ #include "event.h" //to produce errors! #include "error.h" -#include #include #include #include @@ -57,8 +56,8 @@ int main( int argc, char **argv ) { //create a new event loop. This will be captured by the QApplication //when it gets created - QApplication a( argc, argv ); - a.setQuitOnLastWindowClosed(false); + QApplication app( argc, argv ); + app.setQuitOnLastWindowClosed(false); //where QJoyPad saves its settings! @@ -231,9 +230,6 @@ int main( int argc, char **argv ) } } } - //capture the current display for event.h - display = QX11Info::display(); - //create a new LayoutManager with a tray icon / floating icon, depending //on the user's request LayoutManager layoutManager(useTrayIcon,devdir,settingsDir); @@ -252,7 +248,7 @@ int main( int argc, char **argv ) // signal( SIGUSR2, catchSIGUSR2 ); //and run the program! - int result = a.exec(); + int result = app.exec(); //when everything is done, save the current layout for next time... layoutManager.saveDefault(); diff --git a/src/quickset.cpp b/src/quickset.cpp index d900bc9..104634f 100644 --- a/src/quickset.cpp +++ b/src/quickset.cpp @@ -19,14 +19,15 @@ QuickSet::QuickSet( JoyPad* jp, QWidget *parent) connect( button, SIGNAL(clicked()), this, SLOT(accept())); } -void QuickSet::jsevent( js_event msg ) { +void QuickSet::jsevent(const js_event &msg ) { //ignore any joystick events if we're waiting for a keypress if (setting) return; //if a button was pressed on the joystick - if (msg.type == JS_EVENT_BUTTON) { + qulonglong type = msg.type & ~JS_EVENT_INIT; + if (type == JS_EVENT_BUTTON) { //capture that button. - Button* button = joypad->Buttons[msg.number]; + Button* button = joypad->buttons[msg.number]; //go into setting mode and request a key/mousebutton setting = true; @@ -41,16 +42,16 @@ void QuickSet::jsevent( js_event msg ) { //otherwise, tell it to use a keycode. button->setKey(false, code); } - else { + else if (type == JS_EVENT_AXIS) { //require a signal strength of at least 5000 to consider an axis moved. if (abs(msg.value) < 5000) return; //capture the axis that moved - Axis* axis = joypad->Axes[msg.number]; + Axis* axis = joypad->axes[msg.number]; //grab a keycode for that axis and that direction setting = true; - int code = GetKey(axis->getName() + ", " + QString((msg.value > 0)?"positive":"negative"), false).exec(); + int code = GetKey(QString("%1, %2").arg(axis->getName(), msg.value > 0 ? "positive" : "negative"), false).exec(); setting = false; //assign the key to the axis. diff --git a/src/quickset.h b/src/quickset.h index 1d53342..99ff7a2 100644 --- a/src/quickset.h +++ b/src/quickset.h @@ -23,7 +23,7 @@ class QuickSet : public QDialog { public: QuickSet(JoyPad* jp, QWidget *parent = 0); //this needs to see js_events so it can capture them directly - void jsevent( js_event msg ); + void jsevent( const js_event& msg ); private: //the joypad that is being configured JoyPad* joypad;