From 017f2229ced81b268edd016d2d38cd1ae46c6b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Panzenb=C3=B6ck?= Date: Sun, 3 Apr 2016 00:20:59 +0200 Subject: [PATCH] fix scrambeled axis/buttons indices --- src/axis.h | 3 ++- src/button.h | 1 + src/error.h | 2 +- src/joypad.cpp | 16 ++++++++-------- src/joypad.h | 6 +++--- src/joypadw.cpp | 6 ++++-- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/axis.h b/src/axis.h index 2abcf8e..50c3e26 100644 --- a/src/axis.h +++ b/src/axis.h @@ -57,13 +57,14 @@ class Axis : public QObject { //recalculates the gradient curve. This should be run every time //maxSpeed, xZone, or dZone are changed. void adjustGradient(); + int axisIndex() const { return index; } protected: int tick; //This axis is logically depressed (positive or negative) //if the axis is gradient, this is true even if it is not //currently generating a keypress at the instant. bool isOn; - //which joystick this actually is + //the index of this axis on the joystick int index; //actually sends key events. Press is true iff the key //is to be depressed as opposed to released. diff --git a/src/button.h b/src/button.h index 4d74029..1f1f7e3 100644 --- a/src/button.h +++ b/src/button.h @@ -36,6 +36,7 @@ class Button : public QObject { void setKey(bool mouse, int value); //happens every MSEC (constant.h) milliseconds void timerTick( int tick ); + int buttonIndex() const { return index; } protected: //true iff this button is physically depressed. bool isButtonPressed; diff --git a/src/error.h b/src/error.h index fca29dc..1b05a67 100644 --- a/src/error.h +++ b/src/error.h @@ -23,6 +23,6 @@ inline void debug_mesg(const char *fmt, ...) { } #else inline void debug_mesg(...) {} -#define debug_mesg(...) +#define debug_mesg(...) {} #endif #endif diff --git a/src/joypad.cpp b/src/joypad.cpp index b7dacce..b9f72d5 100644 --- a/src/joypad.cpp +++ b/src/joypad.cpp @@ -86,11 +86,11 @@ void JoyPad::open(int dev) { //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 < axisCount; i++) { - if (axes[i] == 0) axes.insert(i, new Axis( i, this )); + for (int i = axes.size(); i < axisCount; i++) { + axes.append(new Axis( i, this )); } - for (int i = 0; i < buttonCount; i++) { - if (buttons[i] == 0) buttons.insert(i, new Button( i, this )); + for (int i = buttons.size(); i < buttonCount; i++) { + buttons.append(new Button( i, this )); } debug_mesg("Setting up joyDeviceListeners\n"); readNotifier = new QSocketNotifier(joydev, QSocketNotifier::Read, this); @@ -235,14 +235,14 @@ void JoyPad::jsevent(const js_event &msg) { if (type == JS_EVENT_AXIS) { debug_mesg("DEBUG: passing on an axis event\n"); debug_mesg("DEBUG: %d %d\n", msg.number, msg.value); - Axis *axis = axes[msg.number]; - if (axis) axis->jsevent(msg.value); + if (msg.number < axes.size()) axes[msg.number]->jsevent(msg.value); + else debug_mesg("DEBUG: axis index out of range: %d\n", msg.value); } 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); - Button *button = buttons[msg.number]; - if (button) button->jsevent(msg.value); + if (msg.number < buttons.size()) buttons[msg.number]->jsevent(msg.value); + else debug_mesg("DEBUG: button index out of range: %d\n", msg.value); } } diff --git a/src/joypad.h b/src/joypad.h index d0b8fe8..4a28aea 100644 --- a/src/joypad.h +++ b/src/joypad.h @@ -12,7 +12,7 @@ #include "error.h" #include -#include +#include #include class JoyPadWidget; @@ -63,8 +63,8 @@ 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; + QList axes; + QList buttons; //the index of this device (devicenum) int index; diff --git a/src/joypadw.cpp b/src/joypadw.cpp index 2a16884..d1d89c7 100644 --- a/src/joypadw.cpp +++ b/src/joypadw.cpp @@ -91,10 +91,12 @@ void JoyPadWidget::jsevent( const js_event& msg ) { //other than a flash :) unsigned int type = msg.type & ~JS_EVENT_INIT; if (type == JS_EVENT_AXIS) { - if (msg.number < axes.count()) axes[msg.number]->jsevent(msg.value); + if (msg.number < axes.size()) axes[msg.number]->jsevent(msg.value); + else debug_mesg("DEBUG: axis index out of range: %d\n", msg.value); } else if (type == JS_EVENT_BUTTON) { - if (msg.number < buttons.count()) buttons[msg.number]->jsevent(msg.value); + if (msg.number < buttons.size()) buttons[msg.number]->jsevent(msg.value); + else debug_mesg("DEBUG: button index out of range: %d\n", msg.value); } //if we're doing quickset, it needs to know when we do something. if (quickset != NULL) {