| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Gamepad module |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include "qgamepadmanager.h" |
| 38 | |
| 39 | #include "qgamepadbackend_p.h" |
| 40 | #include "qgamepadbackendfactory_p.h" |
| 41 | |
| 42 | #include <QtCore/QLoggingCategory> |
| 43 | |
| 44 | #include <private/qobject_p.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | Q_LOGGING_CATEGORY(gp, "qt.gamepad" ) |
| 49 | |
| 50 | class QGamepadManagerPrivate : public QObjectPrivate |
| 51 | { |
| 52 | Q_DECLARE_PUBLIC(QGamepadManager) |
| 53 | public: |
| 54 | QGamepadManagerPrivate() |
| 55 | : gamepadBackend(nullptr) |
| 56 | { |
| 57 | loadBackend(); |
| 58 | } |
| 59 | |
| 60 | void loadBackend(); |
| 61 | |
| 62 | QGamepadBackend *gamepadBackend; |
| 63 | QMap<int, QString> connectedGamepads; |
| 64 | |
| 65 | //private slots |
| 66 | void _q_forwardGamepadConnected(int deviceId); |
| 67 | void _q_forwardGamepadNameChanged(int deviceId, const QString &name); |
| 68 | void _q_forwardGamepadDisconnected(int deviceId); |
| 69 | void _q_forwardGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value); |
| 70 | void _q_forwardGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value); |
| 71 | void _q_forwardGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button); |
| 72 | }; |
| 73 | |
| 74 | void QGamepadManagerPrivate::_q_forwardGamepadConnected(int deviceId) |
| 75 | { |
| 76 | Q_Q(QGamepadManager); |
| 77 | connectedGamepads.insert(key: deviceId, value: QString()); |
| 78 | emit q->gamepadConnected(deviceId); |
| 79 | emit q->connectedGamepadsChanged(); |
| 80 | } |
| 81 | |
| 82 | void QGamepadManagerPrivate::_q_forwardGamepadNameChanged(int deviceId, const QString &name) |
| 83 | { |
| 84 | Q_Q(QGamepadManager); |
| 85 | connectedGamepads.insert(key: deviceId, value: name); |
| 86 | emit q->gamepadNameChanged(deviceId, name); |
| 87 | } |
| 88 | |
| 89 | void QGamepadManagerPrivate::_q_forwardGamepadDisconnected(int deviceId) |
| 90 | { |
| 91 | Q_Q(QGamepadManager); |
| 92 | connectedGamepads.remove(key: deviceId); |
| 93 | emit q->gamepadDisconnected(deviceId); |
| 94 | emit q->connectedGamepadsChanged(); |
| 95 | } |
| 96 | |
| 97 | void QGamepadManagerPrivate::_q_forwardGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value) |
| 98 | { |
| 99 | Q_Q(QGamepadManager); |
| 100 | emit q->gamepadAxisEvent(deviceId, axis, value); |
| 101 | } |
| 102 | |
| 103 | void QGamepadManagerPrivate::_q_forwardGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value) |
| 104 | { |
| 105 | Q_Q(QGamepadManager); |
| 106 | emit q->gamepadButtonPressEvent(deviceId, button, value); |
| 107 | } |
| 108 | |
| 109 | void QGamepadManagerPrivate::_q_forwardGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button) |
| 110 | { |
| 111 | Q_Q(QGamepadManager); |
| 112 | emit q->gamepadButtonReleaseEvent(deviceId, button); |
| 113 | } |
| 114 | |
| 115 | void QGamepadManagerPrivate::loadBackend() |
| 116 | { |
| 117 | QStringList keys = QGamepadBackendFactory::keys(); |
| 118 | qCDebug(gp) << "Available backends:" << keys; |
| 119 | if (!keys.isEmpty()) { |
| 120 | QString requestedKey = QString::fromUtf8(str: qgetenv(varName: "QT_GAMEPAD" )); |
| 121 | QString targetKey = keys.first(); |
| 122 | if (!requestedKey.isEmpty() && keys.contains(str: requestedKey)) |
| 123 | targetKey = requestedKey; |
| 124 | if (!targetKey.isEmpty()) { |
| 125 | qCDebug(gp) << "Loading backend" << targetKey; |
| 126 | gamepadBackend = QGamepadBackendFactory::create(name: targetKey, args: QStringList()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (!gamepadBackend) { |
| 131 | //Use dummy backend |
| 132 | gamepadBackend = new QGamepadBackend(); |
| 133 | qCDebug(gp) << "Using dummy backend" ; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /*! |
| 138 | \class QGamepadManager |
| 139 | \inmodule QtGamepad |
| 140 | \brief Queries attached gamepads and related events. |
| 141 | |
| 142 | QGamepadManager provides a high-level interface for querying the attached |
| 143 | gamepads and events related to all of the connected devices. |
| 144 | */ |
| 145 | |
| 146 | /*! |
| 147 | * \qmltype GamepadManager |
| 148 | * \inqmlmodule QtGamepad |
| 149 | * \instantiates QGamepadManager |
| 150 | * \brief Queries attached gamepads and related events. |
| 151 | * |
| 152 | * GamepadManager QML type provides a high-level interface for |
| 153 | * querying the attached gamepads and events related to all of the |
| 154 | * connected devices. |
| 155 | */ |
| 156 | |
| 157 | /*! |
| 158 | * Constructor for QGamepadManager. |
| 159 | */ |
| 160 | |
| 161 | QGamepadManager::QGamepadManager() : |
| 162 | QObject(*new QGamepadManagerPrivate(), nullptr) |
| 163 | { |
| 164 | Q_D(QGamepadManager); |
| 165 | |
| 166 | qRegisterMetaType<QGamepadManager::GamepadButton>(typeName: "QGamepadManager::GamepadButton" ); |
| 167 | qRegisterMetaType<QGamepadManager::GamepadAxis>(typeName: "QGamepadManager::GamepadAxis" ); |
| 168 | |
| 169 | connect(sender: d->gamepadBackend, SIGNAL(gamepadAdded(int)), receiver: this, SLOT(_q_forwardGamepadConnected(int))); |
| 170 | connect(sender: d->gamepadBackend, SIGNAL(gamepadNamed(int, QString)), receiver: this, SLOT(_q_forwardGamepadNameChanged(int, QString))); |
| 171 | connect(sender: d->gamepadBackend, SIGNAL(gamepadRemoved(int)), receiver: this, SLOT(_q_forwardGamepadDisconnected(int))); |
| 172 | connect(sender: d->gamepadBackend, SIGNAL(gamepadAxisMoved(int,QGamepadManager::GamepadAxis,double)), receiver: this, SLOT(_q_forwardGamepadAxisEvent(int,QGamepadManager::GamepadAxis,double))); |
| 173 | connect(sender: d->gamepadBackend, SIGNAL(gamepadButtonPressed(int,QGamepadManager::GamepadButton,double)), receiver: this, SLOT(_q_forwardGamepadButtonPressEvent(int,QGamepadManager::GamepadButton,double))); |
| 174 | connect(sender: d->gamepadBackend, SIGNAL(gamepadButtonReleased(int,QGamepadManager::GamepadButton)), receiver: this, SLOT(_q_forwardGamepadButtonReleaseEvent(int,QGamepadManager::GamepadButton))); |
| 175 | |
| 176 | connect(sender: d->gamepadBackend, signal: &QGamepadBackend::buttonConfigured, receiver: this, slot: &QGamepadManager::buttonConfigured); |
| 177 | connect(sender: d->gamepadBackend, signal: &QGamepadBackend::axisConfigured, receiver: this, slot: &QGamepadManager::axisConfigured); |
| 178 | connect(sender: d->gamepadBackend, signal: &QGamepadBackend::configurationCanceled, receiver: this, slot: &QGamepadManager::configurationCanceled); |
| 179 | |
| 180 | if (!d->gamepadBackend->start()) |
| 181 | qCWarning(gp) << "Failed to start gamepad backend" ; |
| 182 | } |
| 183 | |
| 184 | /*! |
| 185 | * Destructor for QGamepadManager. |
| 186 | */ |
| 187 | |
| 188 | QGamepadManager::~QGamepadManager() |
| 189 | { |
| 190 | Q_D(QGamepadManager); |
| 191 | d->gamepadBackend->stop(); |
| 192 | d->gamepadBackend->deleteLater(); |
| 193 | } |
| 194 | |
| 195 | /*! |
| 196 | Returns the instance of the QGamepadManager. |
| 197 | */ |
| 198 | QGamepadManager *QGamepadManager::instance() |
| 199 | { |
| 200 | static QGamepadManager instance; |
| 201 | return &instance; |
| 202 | } |
| 203 | |
| 204 | /*! |
| 205 | Returns a boolean indicating whether the gamepad with |
| 206 | the specified \a deviceId is connected or not. |
| 207 | */ |
| 208 | bool QGamepadManager::isGamepadConnected(int deviceId) const |
| 209 | { |
| 210 | Q_D(const QGamepadManager); |
| 211 | return d->connectedGamepads.contains(key: deviceId); |
| 212 | } |
| 213 | |
| 214 | /*! |
| 215 | Returns the name of the gamepad identified by \a deviceId. |
| 216 | If \a deviceId does not identify a connected gamepad, returns an empty string. |
| 217 | |
| 218 | \since 5.11 |
| 219 | */ |
| 220 | QString QGamepadManager::gamepadName(int deviceId) const |
| 221 | { |
| 222 | Q_D(const QGamepadManager); |
| 223 | return d->connectedGamepads.value(akey: deviceId); |
| 224 | } |
| 225 | |
| 226 | /*! |
| 227 | \qmlproperty var GamepadManager::connectedGamepads |
| 228 | \readonly |
| 229 | |
| 230 | Returns a list of integers containing the \l {QGamepad::}{deviceId} |
| 231 | values of the connected gamepads. |
| 232 | */ |
| 233 | |
| 234 | /*! |
| 235 | Returns a list of integers containing the \l {QGamepad::}{deviceId} |
| 236 | values of the connected gamepads. |
| 237 | */ |
| 238 | const QList<int> QGamepadManager::connectedGamepads() const |
| 239 | { |
| 240 | Q_D(const QGamepadManager); |
| 241 | return d->connectedGamepads.keys(); |
| 242 | } |
| 243 | |
| 244 | /*! |
| 245 | Returns a boolean indicating whether configuration |
| 246 | is needed for the specified \a deviceId. |
| 247 | */ |
| 248 | bool QGamepadManager::isConfigurationNeeded(int deviceId) const |
| 249 | { |
| 250 | Q_D(const QGamepadManager); |
| 251 | return d->gamepadBackend->isConfigurationNeeded(deviceId); |
| 252 | } |
| 253 | |
| 254 | /*! |
| 255 | Configures the specified \a button on the gamepad with |
| 256 | this \a deviceId. |
| 257 | Returns \c true in case of success. |
| 258 | */ |
| 259 | bool QGamepadManager::configureButton(int deviceId, QGamepadManager::GamepadButton button) |
| 260 | { |
| 261 | Q_D(QGamepadManager); |
| 262 | return d->gamepadBackend->configureButton(deviceId, button); |
| 263 | } |
| 264 | |
| 265 | /*! |
| 266 | Configures \a axis on the gamepad with the specified \a deviceId. |
| 267 | Returns \c true in case of success. |
| 268 | */ |
| 269 | bool QGamepadManager::configureAxis(int deviceId, QGamepadManager::GamepadAxis axis) |
| 270 | { |
| 271 | Q_D(QGamepadManager); |
| 272 | return d->gamepadBackend->configureAxis(deviceId, axis); |
| 273 | } |
| 274 | |
| 275 | /*! |
| 276 | Configures \a button as the cancel button on the gamepad with |
| 277 | id \a deviceId. |
| 278 | Returns \c true in case of success. |
| 279 | */ |
| 280 | bool QGamepadManager::setCancelConfigureButton(int deviceId, QGamepadManager::GamepadButton button) |
| 281 | { |
| 282 | Q_D(QGamepadManager); |
| 283 | return d->gamepadBackend->setCancelConfigureButton(deviceId, button); |
| 284 | } |
| 285 | |
| 286 | /*! |
| 287 | Resets the configuration on the gamepad with the |
| 288 | specified \a deviceId. |
| 289 | */ |
| 290 | void QGamepadManager::resetConfiguration(int deviceId) |
| 291 | { |
| 292 | Q_D(QGamepadManager); |
| 293 | d->gamepadBackend->resetConfiguration(deviceId); |
| 294 | } |
| 295 | |
| 296 | /*! |
| 297 | Sets the name of the \a file that stores the button and axis |
| 298 | configuration data. |
| 299 | */ |
| 300 | void QGamepadManager::setSettingsFile(const QString &file) |
| 301 | { |
| 302 | Q_D(QGamepadManager); |
| 303 | d->gamepadBackend->setSettingsFile(file); |
| 304 | } |
| 305 | |
| 306 | QT_END_NAMESPACE |
| 307 | |
| 308 | #include "moc_qgamepadmanager.cpp" |
| 309 | |