refactor: sendevent to hide X11 deps, rename and move enums

This commit is contained in:
Mathias Panzenböck
2014-02-19 00:26:24 +01:00
parent cd97b1633e
commit c1647166a8
6 changed files with 115 additions and 97 deletions

View File

@ -1,7 +1,7 @@
#include <QX11Info>
#include "axis.h"
#include "event.h"
#include "time.h"
#define sqr(a) ((a)*(a))
#define cub(a) ((a)*(a)*(a))
#define clamp(a, a_low, a_high) \
@ -69,7 +69,7 @@ bool Axis::read( QTextStream &stream ) {
++it;
if (it == words.end()) return false;
val = (*it).toInt(&ok);
if (ok && val >= 0 && val <= power_function) transferCurve = val;
if (ok && val >= 0 && val <= PowerFunction) transferCurve = val;
else return false;
}
else if (*it == "sens") {
@ -127,16 +127,16 @@ bool Axis::read( QTextStream &stream ) {
throttle = -1;
}
else if (*it == "mouse+v") {
mode = mousepv;
mode = MousePosVert;
}
else if (*it == "mouse-v") {
mode = mousenv;
mode = MouseNegVert;
}
else if (*it == "mouse+h") {
mode = mouseph;
mode = MousePosHor;
}
else if (*it == "mouse-h") {
mode = mousenh;
mode = MouseNegHor;
}
//we ignore unrecognized words to be friendly and allow for additions to
//the format in later versions. Note, this means that typos will not get
@ -162,25 +162,25 @@ void Axis::write( QTextStream &stream ) {
else if (throttle < 0) stream << "throttle-, ";
if (dZone != DZONE) stream << "dZone " << dZone << ", ";
if (xZone != XZONE) stream << "xZone " << xZone << ", ";
if (mode == keybd) {
if (mode == Keyboard) {
stream
<< (puseMouse ? "+mouse " : "+key ") << pkeycode << ", "
<< (nuseMouse ? "-mouse " : "-key ") << nkeycode << "\n";
}
else {
if (gradient) stream << "maxSpeed " << maxSpeed << ", ";
if (transferCurve != quadratic)
if (transferCurve != Quadratic)
stream << "tCurve " << transferCurve << ", ";
if (sensitivity != 1.0F)
stream << "sens " << sensitivity << ", ";
stream << "mouse";
if (mode == mousepv)
if (mode == MousePosVert)
stream << "+v\n";
else if (mode == mousenv)
else if (mode == MouseNegVert)
stream << "-v\n";
else if (mode == mouseph)
else if (mode == MousePosHor)
stream << "+h\n";
else if (mode == mousenh)
else if (mode == MouseNegHor)
stream << "-h\n";
}
@ -238,12 +238,12 @@ void Axis::toDefault() {
gradient = false;
throttle = 0;
maxSpeed = 100;
transferCurve = quadratic;
transferCurve = Quadratic;
sensitivity = 1.0F;
dZone = DZONE;
tick = 0;
xZone = XZONE;
mode = keybd;
mode = Keyboard;
pkeycode = 0;
nkeycode = 0;
puseMouse = false;
@ -259,7 +259,7 @@ bool Axis::isDefault() {
(maxSpeed == 100) &&
(dZone == DZONE) &&
(xZone == XZONE) &&
(mode == keybd) &&
(mode == Keyboard) &&
(pkeycode == 0) &&
(nkeycode == 0) &&
(puseMouse == false) &&
@ -279,7 +279,7 @@ bool Axis::inDeadZone( int val ) {
QString Axis::status() {
QString result = getName() + " : [";
if (mode == keybd) {
if (mode == Keyboard) {
if (throttle == 0) {
if (puseMouse != nuseMouse) {
result += "KEYBOARD/MOUSE";
@ -312,7 +312,7 @@ void Axis::setKey(bool positive, int value) {
void Axis::timerTick( int tick ) {
if (isOn) {
if (mode == keybd) {
if (mode == Keyboard) {
if (tick % FREQ == 0)
{
if (duration == FREQ)
@ -342,8 +342,8 @@ void Axis::adjustGradient() {
}
void Axis::move( bool press ) {
xevent e;
if (mode == keybd) {
FakeEvent e;
if (mode == Keyboard) {
//prevent KeyPress-KeyPress and KeyRelease-KeyRelease pairs.
//this would only happen in odd circumstances involving the setup
//dialog being open and blocking events from happening.
@ -351,14 +351,13 @@ void Axis::move( bool press ) {
isDown = press;
bool useMouse = (state > 0)?puseMouse:nuseMouse;
if (press) {
e.type = useMouse?BPRESS:KPRESS;
e.type = useMouse ? FakeEvent::MouseDown : FakeEvent::KeyDown;
downkey = (state > 0)?pkeycode:nkeycode;
}
else {
e.type = useMouse?BREL:KREL;
e.type = useMouse ? FakeEvent::MouseUp : FakeEvent::KeyUp;
}
e.value1 = downkey;
e.value2 = 0;
e.keycode = downkey;
}
//if using the mouse
else if (press) {
@ -374,19 +373,19 @@ void Axis::move( bool press ) {
const float u = inverseRange * (absState - dZone);
switch(transferCurve) {
case quadratic:
case Quadratic:
fdist = sqr(u);
break;
case cubic:
case Cubic:
fdist = cub(u);
break;
case quadratic_extreme:
case QuadraticExtreme:
fdist = sqr(u);
if(u >= 0.95F) {
fdist *= 1.5F;
}
break;
case power_function:
case PowerFunction:
fdist = clamp(powf(u, 1.0F / clamp(
sensitivity, 1e-8F, 1e+3F)), 0.0F, 1.0F);
break;
@ -405,24 +404,24 @@ void Axis::move( bool press ) {
//if not gradient, always go full speed.
else dist = maxSpeed;
e.type = WARP;
if (mode == mousepv) {
e.value1 = 0;
e.value2 = dist;
}
else if (mode == mousenv) {
e.value1 = 0;
e.value2 = -dist;
}
else if (mode == mouseph) {
e.value1 = dist;
e.value2 = 0;
}
else if (mode == mousenh) {
e.value1 = -dist;
e.value2 = 0;
}
e.type = FakeEvent::MouseMove;
if (mode == MousePosVert) {
e.move.x = 0;
e.move.y = dist;
}
else if (mode == MouseNegVert) {
e.move.x = 0;
e.move.y = -dist;
}
else if (mode == MousePosHor) {
e.move.x = dist;
e.move.y = 0;
}
else if (mode == MouseNegHor) {
e.move.x = -dist;
e.move.y = 0;
}
}
//actually create the event
sendevent(QX11Info::display(), e);
sendevent(e);
}

View File

@ -16,14 +16,16 @@
#define DZONE 3000
#define XZONE 30000
//each axis can create a key press or move the mouse in one of four directions.
enum AxisMode {keybd, mousepv, mousenv, mouseph, mousenh};
enum TransferCurve {linear, quadratic, cubic, quadratic_extreme,
power_function};
//represents one joystick axis
class Axis : public QObject {
Q_OBJECT
Q_OBJECT
//each axis can create a key press or move the mouse in one of four directions.
enum Mode {Keyboard, MousePosVert, MouseNegVert, MousePosHor, MouseNegHor};
enum TransferCurve {Linear, Quadratic, Cubic, QuadraticExtreme,
PowerFunction};
//so AxisEdit can manipulate fields directly.
friend class AxisEdit;
public:
@ -80,7 +82,7 @@ class Axis : public QObject {
int dZone;//-32767 .. 32767
int xZone;//-32767 .. 32767
double sumDist;
AxisMode mode;
Mode mode;
//positive keycode
int pkeycode;
//negative keycode

View File

@ -29,20 +29,20 @@ AxisEdit::AxisEdit( Axis* ax )
v2->addWidget(CGradient);
CMode = new QComboBox(this);
CMode->insertItem((int)keybd, QString("Keyboard/Mouse Button"), Qt::DisplayRole);
CMode->insertItem((int) mousepv,QString("Mouse (Vert.)"),Qt::DisplayRole);
CMode->insertItem((int) mousenv, QString("Mouse (Vert. Rev.)"), Qt::DisplayRole);
CMode->insertItem((int) mouseph, "Mouse (Hor.)", Qt::DisplayRole);
CMode->insertItem((int)mousenh, QString("Mouse (Hor. Rev.)"), Qt::DisplayRole);
CMode->insertItem((int) Axis::Keyboard, QString("Keyboard/Mouse Button"), Qt::DisplayRole);
CMode->insertItem((int) Axis::MousePosVert,QString("Mouse (Vert.)"),Qt::DisplayRole);
CMode->insertItem((int) Axis::MouseNegVert, QString("Mouse (Vert. Rev.)"), Qt::DisplayRole);
CMode->insertItem((int) Axis::MousePosHor, "Mouse (Hor.)", Qt::DisplayRole);
CMode->insertItem((int) Axis::MouseNegHor, QString("Mouse (Hor. Rev.)"), Qt::DisplayRole);
CMode->setCurrentIndex( axis->mode );
connect(CMode, SIGNAL(activated(int)), this, SLOT( CModeChanged( int )));
v2->addWidget(CMode);
CTransferCurve = new QComboBox(this);
CTransferCurve->insertItem(linear, QString("Linear"), Qt::DisplayRole);
CTransferCurve->insertItem(quadratic, QString("Quadratic"),Qt::DisplayRole );
CTransferCurve->insertItem(cubic, QString("Cubic"),Qt::DisplayRole );
CTransferCurve->insertItem(quadratic_extreme, QString("Quadratic Extreme"), Qt::DisplayRole);
CTransferCurve->insertItem(power_function, QString("Power Function"), Qt::DisplayRole);
CTransferCurve->insertItem(Axis::Linear, QString("Linear"), Qt::DisplayRole);
CTransferCurve->insertItem(Axis::Quadratic, QString("Quadratic"),Qt::DisplayRole );
CTransferCurve->insertItem(Axis::Cubic, QString("Cubic"),Qt::DisplayRole );
CTransferCurve->insertItem(Axis::QuadraticExtreme, QString("Quadratic Extreme"), Qt::DisplayRole);
CTransferCurve->insertItem(Axis::PowerFunction, QString("Power Function"), Qt::DisplayRole);
CTransferCurve->setCurrentIndex( axis->transferCurve );
CTransferCurve->setEnabled(axis->gradient);
connect(CTransferCurve, SIGNAL(activated(int)), this, SLOT( CTransferCurveChanged( int )));
@ -132,7 +132,7 @@ void AxisEdit::CGradientChanged( bool on ) {
}
void AxisEdit::CModeChanged( int index ) {
if (index == keybd) {
if (index == Axis::Keyboard) {
MouseBox->setEnabled(false);
KeyBox->setEnabled(true);
}
@ -147,7 +147,7 @@ void AxisEdit::CModeChanged( int index ) {
}
void AxisEdit::CTransferCurveChanged( int index ) {
if (index == power_function) {
if (index == Axis::PowerFunction) {
LSensitivity->setEnabled(true);
SSensitivity->setEnabled(true);
}
@ -178,12 +178,12 @@ void AxisEdit::CThrottleChanged( int index ) {
void AxisEdit::accept() {
axis->gradient = CGradient->isChecked();
axis->maxSpeed = SSpeed->value();
axis->transferCurve = (TransferCurve)CTransferCurve->currentIndex();
axis->transferCurve = (Axis::TransferCurve)CTransferCurve->currentIndex();
axis->sensitivity = SSensitivity->value();
axis->throttle = CThrottle->currentIndex() - 1;
axis->dZone = Slider->deadZone();
axis->xZone = Slider->xZone();
axis->mode = (AxisMode) CMode->currentIndex();
axis->mode = (Axis::Mode) CMode->currentIndex();
axis->pkeycode = BPos->getValue();
axis->nkeycode = BNeg->getValue();
axis->puseMouse = BPos->choseMouse();

View File

@ -1,4 +1,3 @@
#include <QX11Info>
#include "button.h"
#include "event.h"
@ -154,15 +153,14 @@ void Button::timerTick( int tick ) {
void Button::click( bool press ) {
if (isDown == press) return;
isDown = press;
xevent click;
FakeEvent click;
//determine which of the four possible events we're sending.
if (press) click.type = useMouse?BPRESS:KPRESS;
else click.type = useMouse?BREL:KREL;
if (press) click.type = useMouse ? FakeEvent::MouseDown : FakeEvent::KeyDown;
else click.type = useMouse ? FakeEvent::MouseUp : FakeEvent::KeyUp;
//set up the event,
click.value1 = keycode;
click.value2 = 0;
click.keycode = keycode;
//and send it.
sendevent( QX11Info::display(), click );
sendevent(click);
}
void Button::timerCalled() {

View File

@ -1,21 +1,35 @@
#include <QX11Info>
#include "event.h"
//this should be initialized by main.cpp as soon as the program starts.
Display *display = 0;
//actually creates an XWindows event :)
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);
}
else {
if (e.type == KREL || e.type == KPRESS) {
XTestFakeKeyEvent(display, e.value1, (e.type == KPRESS), 0);
}
else if (e.type == BREL || e.type == BPRESS) {
XTestFakeButtonEvent(display, e.value1, (e.type == BPRESS), 0);
}
void sendevent(const FakeEvent &e) {
Display* display = QX11Info::display();
switch (e.type) {
case FakeEvent::MouseMove:
if (e.move.x == 0 && e.move.y == 0) return;
XTestFakeRelativeMotionEvent(display, e.move.x, e.move.y, 0);
break;
case FakeEvent::KeyUp:
if (e.keycode == 0) return;
XTestFakeKeyEvent(display, e.keycode, false, 0);
break;
case FakeEvent::KeyDown:
if (e.keycode == 0) return;
XTestFakeKeyEvent(display, e.keycode, true, 0);
break;
case FakeEvent::MouseUp:
if (e.keycode == 0) return;
XTestFakeButtonEvent(display, e.keycode, false, 0);
break;
case FakeEvent::MouseDown:
if (e.keycode == 0) return;
XTestFakeButtonEvent(display, e.keycode, true, 0);
break;
}
XFlush(display);
}

View File

@ -4,18 +4,23 @@
//for the functions we need to generate keypresses / mouse actions
#include <X11/extensions/XTest.h>
//types of events QJoyPad can create.
//KeyRelease, KeyPress, ButtonRelease, ButtonPress, and MouseMove
enum eventType {KREL, KPRESS, BREL, BPRESS, WARP};
//a simplified event structure that can handle buttons and mouse movements
struct xevent {
eventType type;
int value1; //button, keycode, or x
int value2; //y
struct FakeEvent {
//types of events QJoyPad can create.
//KeyRelease, KeyPress, ButtonRelease, ButtonPress, and MouseMove
enum EventType {KeyUp, KeyDown, MouseUp, MouseDown, MouseMove};
EventType type;
union {
int keycode;
struct {
int x;
int y;
} move;
};
};
void sendevent( Display* display, const xevent& e );
void sendevent(const FakeEvent& e);
#endif