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 result = getName() + " : [";
QString label;
if (mode == Keyboard) {
if (throttle == 0) {
if (puseMouse != nuseMouse) {
result += tr("KEYBOARD/MOUSE");
label = tr("KEYBOARD/MOUSE");
}
else if (puseMouse) {
result += tr("MOUSE");
label = tr("MOUSE");
}
else {
result += tr("KEYBOARD");
label = tr("KEYBOARD");
}
}
else {
result += tr("THROTTLE");
label = tr("THROTTLE");
}
}
else {
label = tr("MOUSE");
}
else result += tr("MOUSE");
return result + "]";
return QString("%1 : [%2]").arg(getName(), label);
}
void Axis::setKey(bool positive, int value) {
setKey(false, positive, value);
}
void Axis::setKey(bool useMouse, bool positive, int value) {
if (positive) {
pkeycode = value;
puseMouse = false;
puseMouse = useMouse;
}
else {
nkeycode = value;
nuseMouse = false;
nuseMouse = useMouse;
}
}

View File

@ -43,13 +43,14 @@ class Axis : public QObject {
void toDefault();
//True iff currently at defaults
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.
bool inDeadZone( int val );
//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.
void setKey(bool positive, int value);
void setKey(bool useMouse, bool positive, int value);
//happens every MSEC milliseconds (constant.h)
//uses tick to decide if key events should be generated
void timerTick( int tick );

View File

@ -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 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
QString status();
//set the key code for this axis. Used by quickset.

View File

@ -2,10 +2,8 @@
ButtonWidget::ButtonWidget( Button* b, QWidget* parent )
: FlashButton(QString::null, QString::null, parent) {
button = b;
: FlashButton(QString::null, QString::null, parent), on(false), button(b) {
update();
on = false;
}
void ButtonWidget::jsevent( int val ) {
@ -17,7 +15,7 @@ void ButtonWidget::jsevent( int val ) {
}
void ButtonWidget::update() {
setText( button->status());
setText(button->status());
}
void ButtonWidget::mouseReleaseEvent( QMouseEvent* e ) {

View File

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

View File

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

View File

@ -15,19 +15,25 @@
class GetKey : public QDialog {
Q_OBJECT
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:
//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 );
//to avoid focus issues, there is only the dialog widget, all the
//rest is painted on. So, I need to know when to repaint.
void paintEvent ( QPaintEvent * );
void closeEvent (QCloseEvent *);
void paintEvent (QPaintEvent *);
private:
//the dialog's message
QString text;
//does this dialog accept mouse clicks?
bool mouse;
bool useMouse;
bool wasMouse;
int value;
};
#endif

View File

@ -90,23 +90,20 @@ KeyButton::KeyButton( QString name, int val, QWidget* parent, bool m, bool nowMo
void KeyButton::onClick() {
//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
// closed so we can ignore this
if(retValue == -1) {
if (retValue < 0) {
return;
} else {
value = retValue;
}
//if the return value was a mouse click...
if (value > MOUSE_OFFSET) {
mouseClicked = true;
value -= MOUSE_OFFSET;
if (mouseClicked) {
setText(tr("Mouse %1").arg(value));
}
//otherwise, it was a key press!
else {
mouseClicked = false;
setText(ktos(value));
}
}

View File

@ -127,7 +127,7 @@ void LayoutManager::udevUpdate() {
struct udev_device *dev = udev_monitor_receive_device(monitor);
if (dev) {
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);
if (devicename.indexIn(path) >= 0) {
@ -515,10 +515,18 @@ void LayoutManager::updateJoyDevs() {
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) {
QString devpath = udev_list_entry_get_name(dev_list_entry);
if (devicename.indexIn(devpath) >= 0) {
int index = devicename.cap(1).toInt();
addJoyPad(index, devpath);
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) {
int index = devicename.cap(1).toInt();
addJoyPad(index, devpath);
}
udev_device_unref(dev);
}
}

View File

@ -3,9 +3,7 @@
//build the dialog
QuickSet::QuickSet( JoyPad* jp, QWidget *parent)
: QDialog(parent) {
setting = false;
joypad = jp;
: QDialog(parent), joypad(jp), setting(false) {
setWindowTitle(tr("Set %1").arg(jp->getName()));
QVBoxLayout* LMain = new QVBoxLayout(this);
LMain->setMargin(5);
@ -29,19 +27,16 @@ void QuickSet::jsevent(const js_event &msg ) {
//capture that button.
Button* button = joypad->buttons[msg.number];
//go into setting mode and request a key/mousebutton
setting = true;
int code = GetKey(button->getName(), true).exec();
setting = false;
if (button) {
//go into setting mode and request a key/mousebutton
setting = true;
bool isMouse = false;
int code = GetKey::getKey(button->getName(), true, &isMouse, this);
setting = false;
//if a mouse button was used,
if (code > MOUSE_OFFSET) {
//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);
if (code >= 0) {
button->setKey(isMouse, code);
}
}
}
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
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;
//assign the key to the axis.
axis->setKey((msg.value > 0),code);
if (code >= 0) {
axis->setKey(isMouse, (msg.value > 0), code);
}
}
}