aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBogDan Vatra <bogdan@kde.org>2015-10-14 17:13:51 +0300
committerBogDan Vatra <bogdan@kdab.com>2015-10-15 08:49:40 +0000
commit5b96c19f6eb01dc438e1e5bc8dc28504769d2f1d (patch)
tree37032729621d8fe8523f7bb910aef234296efcf8
parentfe6040df4bed9851c2ac2d8cca3b129b3e507828 (diff)
Rename index -> deviceId
index is not the smartest way to uniquely identify a device, let's use something that doesn't change. Change-Id: Idf925b13efc8dd604f185ea8de44659509d11054 Reviewed-by: Andy Nichols <andy.nichols@theqtcompany.com>
-rw-r--r--src/gamepad/qgamepad.cpp48
-rw-r--r--src/gamepad/qgamepad.h22
-rw-r--r--src/gamepad/qgamepadbackend_p.h10
-rw-r--r--src/gamepad/qgamepadkeynavigation.cpp4
-rw-r--r--src/gamepad/qgamepadmanager.cpp35
-rw-r--r--src/gamepad/qgamepadmanager.h29
-rw-r--r--src/imports/gamepad/gamepad.pro7
-rw-r--r--src/plugins/gamepads/evdev/qevdevgamepadbackend.cpp4
-rw-r--r--src/plugins/gamepads/evdev/qevdevgamepadbackend_p.h2
9 files changed, 86 insertions, 75 deletions
diff --git a/src/gamepad/qgamepad.cpp b/src/gamepad/qgamepad.cpp
index 9c3b73e..40ad0cb 100644
--- a/src/gamepad/qgamepad.cpp
+++ b/src/gamepad/qgamepad.cpp
@@ -48,13 +48,13 @@ QT_BEGIN_NAMESPACE
*/
/*!
- * \brief Constructs a QGamepad for device at \a index.
- * \param index is the number of the gamepad you wish to see the state of
+ * \brief Constructs a QGamepad for \a deviceId.
+ * \param deviceId is the id of the gamepad you wish to see the state of
* \param parent
*/
-QGamepad::QGamepad(int index, QObject *parent)
+QGamepad::QGamepad(int deviceId, QObject *parent)
: QObject(parent)
- , m_index(index)
+ , m_deviceId(deviceId)
, m_connected(false)
, m_axisLeftX(0.0)
, m_axisLeftY(0.0)
@@ -85,7 +85,7 @@ QGamepad::QGamepad(int index, QObject *parent)
connect(m_gamepadManager, SIGNAL(gamepadButtonPressEvent(int,QGamepadManager::GamepadButton,double)), this, SLOT(handleGamepadButtonPressEvent(int,QGamepadManager::GamepadButton,double)));
connect(m_gamepadManager, SIGNAL(gamepadButtonReleaseEvent(int,QGamepadManager::GamepadButton)), this, SLOT(handleGamepadButtonReleaseEvent(int,QGamepadManager::GamepadButton)));
- setConnected(m_gamepadManager->isGamepadConnected(m_index));
+ setConnected(m_gamepadManager->isGamepadConnected(m_deviceId));
}
/*!
@@ -96,16 +96,16 @@ QGamepad::~QGamepad()
}
/*!
- * \property QGamepad::index
+ * \property QGamepad::deviceId
*
- * This property holds the index of the gamepad device. It is possible for there to be
+ * This property holds the deviceId of the gamepad device. It is possible for there to be
* multiple gamepad devices connected at any given time, so setting this property defines
* which gamepad to use.
*
*/
-int QGamepad::index() const
+int QGamepad::deviceId() const
{
- return m_index;
+ return m_deviceId;
}
/*!
@@ -365,12 +365,12 @@ bool QGamepad::buttonGuide() const
return m_buttonGuide;
}
-void QGamepad::setIndex(int number)
+void QGamepad::setDeviceId(int number)
{
- if (m_index != number) {
- m_index = number;
- emit indexChanged(number);
- setConnected(m_gamepadManager->isGamepadConnected(m_index));
+ if (m_deviceId != number) {
+ m_deviceId = number;
+ emit deviceIdChanged(number);
+ setConnected(m_gamepadManager->isGamepadConnected(m_deviceId));
}
}
@@ -385,9 +385,9 @@ void QGamepad::setConnected(bool isConnected)
/*!
* \internal
*/\
-void QGamepad::handleGamepadConnected(int index)
+void QGamepad::handleGamepadConnected(int deviceId)
{
- if (index == m_index) {
+ if (deviceId == m_deviceId) {
setConnected(true);
}
}
@@ -395,9 +395,9 @@ void QGamepad::handleGamepadConnected(int index)
/*!
* \internal
*/\
-void QGamepad::handleGamepadDisconnected(int index)
+void QGamepad::handleGamepadDisconnected(int deviceId)
{
- if (index == m_index) {
+ if (deviceId == m_deviceId) {
setConnected(false);
}
}
@@ -405,9 +405,9 @@ void QGamepad::handleGamepadDisconnected(int index)
/*!
* \internal
*/\
-void QGamepad::handleGamepadAxisEvent(int index, QGamepadManager::GamepadAxis axis, double value)
+void QGamepad::handleGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value)
{
- if (index != m_index)
+ if (deviceId != m_deviceId)
return;
switch (axis) {
@@ -435,9 +435,9 @@ void QGamepad::handleGamepadAxisEvent(int index, QGamepadManager::GamepadAxis ax
/*!
* \internal
*/\
-void QGamepad::handleGamepadButtonPressEvent(int index, QGamepadManager::GamepadButton button, double value)
+void QGamepad::handleGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value)
{
- if (index != m_index)
+ if (deviceId != m_deviceId)
return;
switch (button) {
@@ -518,9 +518,9 @@ void QGamepad::handleGamepadButtonPressEvent(int index, QGamepadManager::Gamepad
/*!
* \internal
*/\
-void QGamepad::handleGamepadButtonReleaseEvent(int index, QGamepadManager::GamepadButton button)
+void QGamepad::handleGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button)
{
- if (index != m_index)
+ if (deviceId != m_deviceId)
return;
switch (button) {
diff --git a/src/gamepad/qgamepad.h b/src/gamepad/qgamepad.h
index 5cce27f..26c883f 100644
--- a/src/gamepad/qgamepad.h
+++ b/src/gamepad/qgamepad.h
@@ -46,7 +46,7 @@ QT_BEGIN_NAMESPACE
class Q_GAMEPAD_EXPORT QGamepad : public QObject
{
Q_OBJECT
- Q_PROPERTY(int index READ index WRITE setIndex NOTIFY indexChanged)
+ Q_PROPERTY(int deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged)
Q_PROPERTY(bool connected READ isConnected NOTIFY connectedChanged)
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Q_PROPERTY(double axisLeftX READ axisLeftX NOTIFY axisLeftXChanged)
@@ -71,10 +71,10 @@ class Q_GAMEPAD_EXPORT QGamepad : public QObject
Q_PROPERTY(bool buttonRight READ buttonRight NOTIFY buttonRightChanged)
Q_PROPERTY(bool buttonGuide READ buttonGuide NOTIFY buttonGuideChanged)
public:
- explicit QGamepad(int index = 0, QObject *parent = 0);
+ explicit QGamepad(int deviceId = 0, QObject *parent = 0);
~QGamepad();
- int index() const;
+ int deviceId() const;
bool isConnected() const;
@@ -104,7 +104,7 @@ public:
Q_SIGNALS:
- void indexChanged(int value);
+ void deviceIdChanged(int value);
void connectedChanged(bool value);
void nameChanged(QString value);
void axisLeftXChanged(double value);
@@ -131,22 +131,22 @@ Q_SIGNALS:
public Q_SLOTS:
- void setIndex(int number);
+ void setDeviceId(int number);
private Q_SLOTS:
void setConnected(bool isConnected);
- void handleGamepadConnected(int index);
- void handleGamepadDisconnected(int index);
- void handleGamepadAxisEvent(int index, QGamepadManager::GamepadAxis axis, double value);
- void handleGamepadButtonPressEvent(int index, QGamepadManager::GamepadButton button, double value);
- void handleGamepadButtonReleaseEvent(int index, QGamepadManager::GamepadButton button);
+ void handleGamepadConnected(int deviceId);
+ void handleGamepadDisconnected(int deviceId);
+ void handleGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value);
+ void handleGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value);
+ void handleGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button);
private:
QGamepadManager *m_gamepadManager;
- int m_index;
+ int m_deviceId;
bool m_connected;
QString m_name;
double m_axisLeftX;
diff --git a/src/gamepad/qgamepadbackend_p.h b/src/gamepad/qgamepadbackend_p.h
index cc6453d..60f01dc 100644
--- a/src/gamepad/qgamepadbackend_p.h
+++ b/src/gamepad/qgamepadbackend_p.h
@@ -55,11 +55,11 @@ public slots:
virtual void stop();
signals:
- void gamepadAdded(int index);
- void gamepadRemoved(int index);
- void gamepadAxisMoved(int index, QGamepadManager::GamepadAxis axis, double value);
- void gamepadButtonPressed(int index, QGamepadManager::GamepadButton button, double value);
- void gamepadButtonReleased(int index, QGamepadManager::GamepadButton button);
+ void gamepadAdded(int deviceId);
+ void gamepadRemoved(int deviceId);
+ void gamepadAxisMoved(int deviceId, QGamepadManager::GamepadAxis axis, double value);
+ void gamepadButtonPressed(int deviceId, QGamepadManager::GamepadButton button, double value);
+ void gamepadButtonReleased(int deviceId, QGamepadManager::GamepadButton button);
};
QT_END_NAMESPACE
diff --git a/src/gamepad/qgamepadkeynavigation.cpp b/src/gamepad/qgamepadkeynavigation.cpp
index f6f1201..ba30dc5 100644
--- a/src/gamepad/qgamepadkeynavigation.cpp
+++ b/src/gamepad/qgamepadkeynavigation.cpp
@@ -326,7 +326,7 @@ void QGamepadKeyNavigation::processGamepadButtonPressEvent(int index, QGamepadMa
Q_UNUSED(value)
//If a gamepad has been set then, only use the events of that gamepad
- if (m_gamepad && m_gamepad->index() != index)
+ if (m_gamepad && m_gamepad->deviceId() != index)
return;
//Trigger buttons are a special case as they get multiple press events as the value changes
@@ -346,7 +346,7 @@ void QGamepadKeyNavigation::processGamepadButtonPressEvent(int index, QGamepadMa
void QGamepadKeyNavigation::procressGamepadButtonReleaseEvent(int index, QGamepadManager::GamepadButton button)
{
//If a gamepad has been set then, only use the events of that gamepad
- if (m_gamepad && m_gamepad->index() != index)
+ if (m_gamepad && m_gamepad->deviceId() != index)
return;
//Free the trigger buttons if necessary
diff --git a/src/gamepad/qgamepadmanager.cpp b/src/gamepad/qgamepadmanager.cpp
index e58298b..91ca70f 100644
--- a/src/gamepad/qgamepadmanager.cpp
+++ b/src/gamepad/qgamepadmanager.cpp
@@ -97,41 +97,48 @@ QGamepadManager *QGamepadManager::instance()
return &instance;
}
-bool QGamepadManager::isGamepadConnected(int index)
+bool QGamepadManager::isGamepadConnected(int deviceId)
{
- return m_connectedGamepads.contains(index);
+ return m_connectedGamepads.contains(deviceId);
}
-void QGamepadManager::forwardGamepadConnected(int index)
+const QList<int> QGamepadManager::connectedGamepads() const
+{
+ return m_connectedGamepads.toList();
+}
+
+void QGamepadManager::forwardGamepadConnected(int deviceId)
{
//qDebug() << "gamepad connected: " << index;
- m_connectedGamepads.append(index);
- emit gamepadConnected(index);
+ m_connectedGamepads.insert(deviceId);
+ emit gamepadConnected(deviceId);
+ emit connectedGamepadsChanged();
}
-void QGamepadManager::forwardGamepadDisconnected(int index)
+void QGamepadManager::forwardGamepadDisconnected(int deviceId)
{
//qDebug() << "gamepad disconnected: " << index;
- m_connectedGamepads.removeAll(index);
- emit gamepadDisconnected(index);
+ m_connectedGamepads.remove(deviceId);
+ emit gamepadDisconnected(deviceId);
+ emit connectedGamepadsChanged();
}
-void QGamepadManager::forwardGamepadAxisEvent(int index, QGamepadManager::GamepadAxis axis, double value)
+void QGamepadManager::forwardGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value)
{
//qDebug() << "gamepad axis event: " << index << axis << value;
- emit gamepadAxisEvent(index, axis, value);
+ emit gamepadAxisEvent(deviceId, axis, value);
}
-void QGamepadManager::forwardGamepadButtonPressEvent(int index, QGamepadManager::GamepadButton button, double value)
+void QGamepadManager::forwardGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value)
{
//qDebug() << "gamepad button press event: " << index << button << value;
- emit gamepadButtonPressEvent(index, button, value);
+ emit gamepadButtonPressEvent(deviceId, button, value);
}
-void QGamepadManager::forwardGamepadButtonReleaseEvent(int index, QGamepadManager::GamepadButton button)
+void QGamepadManager::forwardGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button)
{
//qDebug() << "gamepad button release event: " << index << button;
- emit gamepadButtonReleaseEvent(index, button);
+ emit gamepadButtonReleaseEvent(deviceId, button);
}
QT_END_NAMESPACE
diff --git a/src/gamepad/qgamepadmanager.h b/src/gamepad/qgamepadmanager.h
index 685d0e5..6502ff6 100644
--- a/src/gamepad/qgamepadmanager.h
+++ b/src/gamepad/qgamepadmanager.h
@@ -38,7 +38,7 @@
#define JOYSTICKMANAGER_H
#include <QtCore/QObject>
-#include <QtCore/QList>
+#include <QtCore/QSet>
#include <QtGamepad/qtgamepadglobal.h>
QT_BEGIN_NAMESPACE
@@ -51,6 +51,7 @@ class Q_GAMEPAD_EXPORT QGamepadManager : public QObject
Q_OBJECT
Q_FLAGS(GamepadButton GamepadButtons)
Q_FLAGS(GamepadAxis GamepadAxes)
+ Q_PROPERTY(QList<int> connectedGamepads READ connectedGamepads NOTIFY connectedGamepadsChanged)
public:
enum GamepadButton {
@@ -86,21 +87,23 @@ public:
static QGamepadManager* instance();
- bool isGamepadConnected(int index);
+ bool isGamepadConnected(int deviceId);
+ const QList<int> connectedGamepads() const;
Q_SIGNALS:
- void gamepadConnected(int index);
- void gamepadDisconnected(int index);
- void gamepadAxisEvent(int index, QGamepadManager::GamepadAxis axis, double value);
- void gamepadButtonPressEvent(int index, QGamepadManager::GamepadButton button, double value);
- void gamepadButtonReleaseEvent(int index, QGamepadManager::GamepadButton button);
+ void connectedGamepadsChanged();
+ void gamepadConnected(int deviceId);
+ void gamepadDisconnected(int deviceId);
+ void gamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value);
+ void gamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value);
+ void gamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button);
private Q_SLOTS:
- void forwardGamepadConnected(int index);
- void forwardGamepadDisconnected(int index);
- void forwardGamepadAxisEvent(int index, QGamepadManager::GamepadAxis axis, double value);
- void forwardGamepadButtonPressEvent(int index, QGamepadManager::GamepadButton button, double value);
- void forwardGamepadButtonReleaseEvent(int index, QGamepadManager::GamepadButton button);
+ void forwardGamepadConnected(int deviceId);
+ void forwardGamepadDisconnected(int deviceId);
+ void forwardGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value);
+ void forwardGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value);
+ void forwardGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button);
private:
QGamepadManager();
@@ -111,7 +114,7 @@ private:
void loadBackend();
QGamepadBackend *m_gamepadBackend;
- QList<int> m_connectedGamepads;
+ QSet<int> m_connectedGamepads;
};
diff --git a/src/imports/gamepad/gamepad.pro b/src/imports/gamepad/gamepad.pro
index c6ee21c..4b00665 100644
--- a/src/imports/gamepad/gamepad.pro
+++ b/src/imports/gamepad/gamepad.pro
@@ -4,13 +4,14 @@ TARGETPATH = QtGamepad
IMPORT_VERSION = 1.0
QT += qml quick gamepad
-SOURCES += \
- $$PWD/qtgamepad.cpp \
- qgamepadmouseitem.cpp
load(qml_plugin)
OTHER_FILES += qmldir
+SOURCES += \
+ qtgamepad.cpp \
+ qgamepadmouseitem.cpp
+
HEADERS += \
qgamepadmouseitem.h
diff --git a/src/plugins/gamepads/evdev/qevdevgamepadbackend.cpp b/src/plugins/gamepads/evdev/qevdevgamepadbackend.cpp
index bc7d2d9..7915896 100644
--- a/src/plugins/gamepads/evdev/qevdevgamepadbackend.cpp
+++ b/src/plugins/gamepads/evdev/qevdevgamepadbackend.cpp
@@ -84,7 +84,7 @@ QEvdevGamepadDevice *QEvdevGamepadBackend::newDevice(const QByteArray &device)
}
// To be called only when it is sure that there is a controller on-line.
-int QEvdevGamepadBackend::indexForDevice(const QByteArray &device)
+int QEvdevGamepadBackend::idForDevice(const QByteArray &device)
{
int index;
if (m_devIndex.contains(device)) {
@@ -240,7 +240,7 @@ double QEvdevGamepadDevice::AxisInfo::normalized(int value) const
void QEvdevGamepadDevice::processInputEvent(input_event *e)
{
if (m_index < 0) {
- m_index = m_backend->indexForDevice(m_dev);
+ m_index = m_backend->idForDevice(m_dev);
qCDebug(lcEGB) << "Adding gamepad" << m_dev << "with index" << m_index;
emit m_backend->gamepadAdded(m_index);
}
diff --git a/src/plugins/gamepads/evdev/qevdevgamepadbackend_p.h b/src/plugins/gamepads/evdev/qevdevgamepadbackend_p.h
index 0371989..e23af7c 100644
--- a/src/plugins/gamepads/evdev/qevdevgamepadbackend_p.h
+++ b/src/plugins/gamepads/evdev/qevdevgamepadbackend_p.h
@@ -95,7 +95,7 @@ public:
QEvdevGamepadBackend();
bool start() Q_DECL_OVERRIDE;
void stop() Q_DECL_OVERRIDE;
- int indexForDevice(const QByteArray &device);
+ int idForDevice(const QByteArray &device);
private slots:
void handleAddedDevice(const QString &device);