fix udev code and some refactorings for cleaner code

This commit is contained in:
Mathias Panzenböck
2014-02-23 19:37:01 +01:00
parent 6771b75516
commit a1d2c947c7
10 changed files with 96 additions and 66 deletions

View File

@ -278,35 +278,41 @@ bool Axis::inDeadZone( int val ) {
} }
QString Axis::status() { QString Axis::status() {
QString result = getName() + " : ["; QString label;
if (mode == Keyboard) { if (mode == Keyboard) {
if (throttle == 0) { if (throttle == 0) {
if (puseMouse != nuseMouse) { if (puseMouse != nuseMouse) {
result += tr("KEYBOARD/MOUSE"); label = tr("KEYBOARD/MOUSE");
} }
else if (puseMouse) { else if (puseMouse) {
result += tr("MOUSE"); label = tr("MOUSE");
} }
else { else {
result += tr("KEYBOARD"); label = tr("KEYBOARD");
} }
} }
else { else {
result += tr("THROTTLE"); label = tr("THROTTLE");
} }
} }
else result += tr("MOUSE"); else {
return result + "]"; label = tr("MOUSE");
}
return QString("%1 : [%2]").arg(getName(), label);
} }
void Axis::setKey(bool positive, int value) { void Axis::setKey(bool positive, int value) {
setKey(false, positive, value);
}
void Axis::setKey(bool useMouse, bool positive, int value) {
if (positive) { if (positive) {
pkeycode = value; pkeycode = value;
puseMouse = false; puseMouse = useMouse;
} }
else { else {
nkeycode = value; nkeycode = value;
nuseMouse = false; nuseMouse = useMouse;
} }
} }

View File

@ -43,13 +43,14 @@ class Axis : public QObject {
void toDefault(); void toDefault();
//True iff currently at defaults //True iff currently at defaults
bool isDefault(); bool isDefault();
QString getName() { return QString("Axis %1").arg(index+1);} QString getName() { return tr("Axis %1").arg(index+1);}
//true iff the given value is in the dead zone for this axis. //true iff the given value is in the dead zone for this axis.
bool inDeadZone( int val ); bool inDeadZone( int val );
//a descriptive string used as a label for the button representing this axis //a descriptive string used as a label for the button representing this axis
QString status(); QString status();
//set the key code for this axis. Used by quickset. //set the key code for this axis. Used by quickset.
void setKey(bool positive, int value); void setKey(bool positive, int value);
void setKey(bool useMouse, bool positive, int value);
//happens every MSEC milliseconds (constant.h) //happens every MSEC milliseconds (constant.h)
//uses tick to decide if key events should be generated //uses tick to decide if key events should be generated
void timerTick( int tick ); void timerTick( int tick );

View File

@ -29,7 +29,7 @@ class Button : public QObject {
//True iff is currently using default settings //True iff is currently using default settings
bool isDefault(); bool isDefault();
//returns a string representation of this button. //returns a string representation of this button.
QString getName() { return QString("Button %1").arg(index+1); } QString getName() { return tr("Button %1").arg(index+1); }
//a descriptive string used as a label for the button representing this axis //a descriptive string used as a label for the button representing this axis
QString status(); QString status();
//set the key code for this axis. Used by quickset. //set the key code for this axis. Used by quickset.

View File

@ -2,10 +2,8 @@
ButtonWidget::ButtonWidget( Button* b, QWidget* parent ) ButtonWidget::ButtonWidget( Button* b, QWidget* parent )
: FlashButton(QString::null, QString::null, parent) { : FlashButton(QString::null, QString::null, parent), on(false), button(b) {
button = b;
update(); update();
on = false;
} }
void ButtonWidget::jsevent( int val ) { void ButtonWidget::jsevent( int val ) {

View File

@ -25,6 +25,4 @@
#define SENSITIVITY_MIN 1e-8F #define SENSITIVITY_MIN 1e-8F
#define SENSITIVITY_MAX 1e+8F #define SENSITIVITY_MAX 1e+8F
#define MOUSE_OFFSET 400
#endif #endif

View File

@ -2,18 +2,29 @@
#include "config.h" #include "config.h"
#include "getkey.h" #include "getkey.h"
GetKey::GetKey( QString button, bool m ) int GetKey::getKey(const QString& button, bool useMouse, bool *isMouse, QWidget *parent) {
:QDialog( 0 ) GetKey getkey(button, useMouse, parent);
if (getkey.exec() == QDialog::Accepted) {
if (isMouse) {
*isMouse = getkey.choseMouse();
}
return getkey.getValue();
}
return -1;
}
GetKey::GetKey(const QString &button, bool useMouse, QWidget *parent)
: QDialog( parent ), useMouse(useMouse), wasMouse(false), value(-1)
{ {
//prepare the dialog //prepare the dialog
mouse = m;
setWindowTitle( tr("Choose a key") ); setWindowTitle( tr("Choose a key") );
setWindowIcon(QIcon(QJOYPAD_ICON24)); setWindowIcon(QIcon(QJOYPAD_ICON24));
//I'd use a QLabel, but that steals x11Events! //I'd use a QLabel, but that steals x11Events!
//So, I'll draw the text directly. That means //So, I'll draw the text directly. That means
//I need to resolve the size of the dialog by hand: //I need to resolve the size of the dialog by hand:
text = (mouse ? tr("Choose a new key or mouse button for %1") : tr("Choose a new key for %1")).arg(button); text = (useMouse ? tr("Choose a new key or mouse button for %1") : tr("Choose a new key for %1")).arg(button);
QRect rect = fontMetrics().boundingRect( text ); QRect rect = fontMetrics().boundingRect( text );
//I calculate the size based on the first line of text, which is longer. //I calculate the size based on the first line of text, which is longer.
//The fontMetrics function is dumb and would treat the string with a //The fontMetrics function is dumb and would treat the string with a
@ -22,7 +33,7 @@ GetKey::GetKey( QString button, bool m )
text += tr("\n(Ctrl-X for no key)"); text += tr("\n(Ctrl-X for no key)");
//now I add 20 pixels of padding and double the height to make room for //now I add 20 pixels of padding and double the height to make room for
//two lines. //two lines.
setFixedSize( QSize( rect.width() + 20, rect.height()*2 + 20 ) ); setFixedSize(rect.width() + 20, rect.height()*2 + 20);
} }
bool GetKey::x11Event( XEvent* e ) bool GetKey::x11Event( XEvent* e )
@ -32,16 +43,28 @@ bool GetKey::x11Event( XEvent* e )
//On a key press, return the key and quit //On a key press, return the key and quit
//Ctrl+X == [No Key] //Ctrl+X == [No Key]
if (e->type == KeyRelease) { if (e->type == KeyRelease) {
wasMouse = false;
if (XKeycodeToKeysym(QX11Info::display(),e->xkey.keycode,0) == XK_x) { if (XKeycodeToKeysym(QX11Info::display(),e->xkey.keycode,0) == XK_x) {
if (e->xkey.state & ControlMask) done( 0 ); if (e->xkey.state & ControlMask) {
else done( e->xkey.keycode ); value = -1;
reject();
}
else {
value = e->xkey.keycode;
accept();
}
}
else {
value = e->xkey.keycode;
accept();
} }
else done( e->xkey.keycode );
return true; return true;
} }
//if we're accepting mouse clicks and a mouse button was clicked... //if we're accepting mouse clicks and a mouse button was clicked...
if (mouse && e->type == ButtonRelease) { if (useMouse && e->type == ButtonRelease) {
done ( e->xbutton.button + MOUSE_OFFSET); wasMouse = true;
value = e->xbutton.button;
accept();
return true; return true;
} }
@ -50,11 +73,6 @@ bool GetKey::x11Event( XEvent* e )
return false; return false;
} }
void GetKey::closeEvent(QCloseEvent *e) {
e->ignore();
done(-1);
}
void GetKey::paintEvent ( QPaintEvent * ) { void GetKey::paintEvent ( QPaintEvent * ) {
//whenever we need to repaint, draw in our text. //whenever we need to repaint, draw in our text.
QPainter paint( this ); QPainter paint( this );

View File

@ -15,19 +15,25 @@
class GetKey : public QDialog { class GetKey : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
GetKey( QString button, bool m = false ); GetKey(const QString& button, bool useMouse = false, QWidget *parent = 0);
static int getKey(const QString& button, bool useMouse, bool *choseMouse, QWidget *parent = 0);
int getValue() const { return value; }
bool choseMouse() const { return wasMouse; }
protected: protected:
//to filter through every event this thing sees, before QT does. //to filter through every event this thing sees, before Qt does.
bool x11Event( XEvent* e ); bool x11Event( XEvent* e );
//to avoid focus issues, there is only the dialog widget, all the //to avoid focus issues, there is only the dialog widget, all the
//rest is painted on. So, I need to know when to repaint. //rest is painted on. So, I need to know when to repaint.
void paintEvent (QPaintEvent *); void paintEvent (QPaintEvent *);
void closeEvent (QCloseEvent *);
private: private:
//the dialog's message //the dialog's message
QString text; QString text;
//does this dialog accept mouse clicks? //does this dialog accept mouse clicks?
bool mouse; bool useMouse;
bool wasMouse;
int value;
}; };
#endif #endif

View File

@ -90,23 +90,20 @@ KeyButton::KeyButton( QString name, int val, QWidget* parent, bool m, bool nowMo
void KeyButton::onClick() { void KeyButton::onClick() {
//when clicked, ask for a key! //when clicked, ask for a key!
int retValue = GetKey( buttonname, mouse ).exec(); int retValue = GetKey::getKey(buttonname, mouse, &mouseClicked, this->window());
// -1 is a special value meaning that the window was simply // -1 is a special value meaning that the window was simply
// closed so we can ignore this // closed so we can ignore this
if(retValue == -1) { if (retValue < 0) {
return; return;
} else { } else {
value = retValue; value = retValue;
} }
//if the return value was a mouse click... //if the return value was a mouse click...
if (value > MOUSE_OFFSET) { if (mouseClicked) {
mouseClicked = true;
value -= MOUSE_OFFSET;
setText(tr("Mouse %1").arg(value)); setText(tr("Mouse %1").arg(value));
} }
//otherwise, it was a key press! //otherwise, it was a key press!
else { else {
mouseClicked = false;
setText(ktos(value)); setText(ktos(value));
} }
} }

View File

@ -127,7 +127,7 @@ void LayoutManager::udevUpdate() {
struct udev_device *dev = udev_monitor_receive_device(monitor); struct udev_device *dev = udev_monitor_receive_device(monitor);
if (dev) { if (dev) {
QRegExp devicename("/js(\\d+)$"); QRegExp devicename("/js(\\d+)$");
QString path = QString("/sys%1").arg(udev_device_get_devpath(dev)); QString path = udev_device_get_devnode(dev);
const char *action = udev_device_get_action(dev); const char *action = udev_device_get_action(dev);
if (devicename.indexIn(path) >= 0) { if (devicename.indexIn(path) >= 0) {
@ -515,11 +515,19 @@ void LayoutManager::updateJoyDevs() {
devices = udev_enumerate_get_list_entry(enumerate); devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) { udev_list_entry_foreach(dev_list_entry, devices) {
QString devpath = udev_list_entry_get_name(dev_list_entry); const char *path = udev_list_entry_get_name(dev_list_entry);
struct udev_device *dev = udev_device_new_from_syspath(udev, path);
if (dev) {
QString devpath = udev_device_get_devnode(dev);
if (devicename.indexIn(devpath) >= 0) { if (devicename.indexIn(devpath) >= 0) {
int index = devicename.cap(1).toInt(); int index = devicename.cap(1).toInt();
addJoyPad(index, devpath); addJoyPad(index, devpath);
} }
udev_device_unref(dev);
}
} }
udev_ok = true; udev_ok = true;

View File

@ -3,9 +3,7 @@
//build the dialog //build the dialog
QuickSet::QuickSet( JoyPad* jp, QWidget *parent) QuickSet::QuickSet( JoyPad* jp, QWidget *parent)
: QDialog(parent) { : QDialog(parent), joypad(jp), setting(false) {
setting = false;
joypad = jp;
setWindowTitle(tr("Set %1").arg(jp->getName())); setWindowTitle(tr("Set %1").arg(jp->getName()));
QVBoxLayout* LMain = new QVBoxLayout(this); QVBoxLayout* LMain = new QVBoxLayout(this);
LMain->setMargin(5); LMain->setMargin(5);
@ -29,19 +27,16 @@ void QuickSet::jsevent(const js_event &msg ) {
//capture that button. //capture that button.
Button* button = joypad->buttons[msg.number]; Button* button = joypad->buttons[msg.number];
if (button) {
//go into setting mode and request a key/mousebutton //go into setting mode and request a key/mousebutton
setting = true; setting = true;
int code = GetKey(button->getName(), true).exec(); bool isMouse = false;
int code = GetKey::getKey(button->getName(), true, &isMouse, this);
setting = false; setting = false;
//if a mouse button was used, if (code >= 0) {
if (code > MOUSE_OFFSET) { button->setKey(isMouse, code);
//then tell it to the Button a mouse button
button->setKey(true, code - MOUSE_OFFSET);
} }
else {
//otherwise, tell it to use a keycode.
button->setKey(false, code);
} }
} }
else if (type == JS_EVENT_AXIS) { else if (type == JS_EVENT_AXIS) {
@ -53,10 +48,13 @@ void QuickSet::jsevent(const js_event &msg ) {
//grab a keycode for that axis and that direction //grab a keycode for that axis and that direction
setting = true; setting = true;
int code = GetKey((msg.value >= 0 ? tr("%1, positive") : tr("%1, negative")).arg(axis->getName()), false).exec(); bool isMouse = false;
int code = GetKey::getKey((msg.value >= 0 ? tr("%1, positive") : tr("%1, negative")).arg(axis->getName()), true, &isMouse, this);
setting = false; setting = false;
//assign the key to the axis. //assign the key to the axis.
axis->setKey((msg.value > 0),code); if (code >= 0) {
axis->setKey(isMouse, (msg.value > 0), code);
}
} }
} }