You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
9.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "qgamepadmanager.h"
  37. #include "qgamepadbackend_p.h"
  38. #include "qgamepadbackendfactory_p.h"
  39. #include <QtCore/QLoggingCategory>
  40. #include <private/qobject_p.h>
  41. QT_BEGIN_NAMESPACE
  42. Q_LOGGING_CATEGORY(gp, "qt.gamepad")
  43. class QGamepadManagerPrivate : public QObjectPrivate
  44. {
  45. Q_DECLARE_PUBLIC(QGamepadManager)
  46. public:
  47. QGamepadManagerPrivate()
  48. : gamepadBackend(nullptr)
  49. {
  50. loadBackend();
  51. }
  52. void loadBackend();
  53. QGamepadBackend *gamepadBackend;
  54. QMap<int, QString> connectedGamepads;
  55. //private slots
  56. void _q_forwardGamepadConnected(int deviceId);
  57. void _q_forwardGamepadNameChanged(int deviceId, const QString &name);
  58. void _q_forwardGamepadDisconnected(int deviceId);
  59. void _q_forwardGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value);
  60. void _q_forwardGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value);
  61. void _q_forwardGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button);
  62. };
  63. void QGamepadManagerPrivate::_q_forwardGamepadConnected(int deviceId)
  64. {
  65. Q_Q(QGamepadManager);
  66. connectedGamepads.insert(deviceId, QString());
  67. emit q->gamepadConnected(deviceId);
  68. emit q->connectedGamepadsChanged();
  69. }
  70. void QGamepadManagerPrivate::_q_forwardGamepadNameChanged(int deviceId, const QString &name)
  71. {
  72. Q_Q(QGamepadManager);
  73. connectedGamepads.insert(deviceId, name);
  74. emit q->gamepadNameChanged(deviceId, name);
  75. }
  76. void QGamepadManagerPrivate::_q_forwardGamepadDisconnected(int deviceId)
  77. {
  78. Q_Q(QGamepadManager);
  79. connectedGamepads.remove(deviceId);
  80. emit q->gamepadDisconnected(deviceId);
  81. emit q->connectedGamepadsChanged();
  82. }
  83. void QGamepadManagerPrivate::_q_forwardGamepadAxisEvent(int deviceId, QGamepadManager::GamepadAxis axis, double value)
  84. {
  85. Q_Q(QGamepadManager);
  86. emit q->gamepadAxisEvent(deviceId, axis, value);
  87. }
  88. void QGamepadManagerPrivate::_q_forwardGamepadButtonPressEvent(int deviceId, QGamepadManager::GamepadButton button, double value)
  89. {
  90. Q_Q(QGamepadManager);
  91. emit q->gamepadButtonPressEvent(deviceId, button, value);
  92. }
  93. void QGamepadManagerPrivate::_q_forwardGamepadButtonReleaseEvent(int deviceId, QGamepadManager::GamepadButton button)
  94. {
  95. Q_Q(QGamepadManager);
  96. emit q->gamepadButtonReleaseEvent(deviceId, button);
  97. }
  98. void QGamepadManagerPrivate::loadBackend()
  99. {
  100. QStringList keys = QGamepadBackendFactory::keys();
  101. qCDebug(gp) << "Available backends:" << keys;
  102. if (!keys.isEmpty()) {
  103. QString requestedKey = QString::fromUtf8(qgetenv("QT_GAMEPAD"));
  104. QString targetKey = keys.first();
  105. if (!requestedKey.isEmpty() && keys.contains(requestedKey))
  106. targetKey = requestedKey;
  107. if (!targetKey.isEmpty()) {
  108. qCDebug(gp) << "Loading backend" << targetKey;
  109. gamepadBackend = QGamepadBackendFactory::create(targetKey, QStringList());
  110. }
  111. }
  112. if (!gamepadBackend) {
  113. //Use dummy backend
  114. gamepadBackend = new QGamepadBackend();
  115. qCDebug(gp) << "Using dummy backend";
  116. }
  117. }
  118. /*!
  119. \class QGamepadManager
  120. \inmodule QtGamepad
  121. \brief Queries attached gamepads and related events.
  122. QGamepadManager provides a high-level interface for querying the attached
  123. gamepads and events related to all of the connected devices.
  124. */
  125. /*!
  126. * Constructor for QGamepadManager.
  127. */
  128. QGamepadManager::QGamepadManager() :
  129. QObject(*new QGamepadManagerPrivate(), nullptr)
  130. {
  131. Q_D(QGamepadManager);
  132. qRegisterMetaType<QGamepadManager::GamepadButton>("QGamepadManager::GamepadButton");
  133. qRegisterMetaType<QGamepadManager::GamepadAxis>("QGamepadManager::GamepadAxis");
  134. connect(d->gamepadBackend, SIGNAL(gamepadAdded(int)), this, SLOT(_q_forwardGamepadConnected(int)));
  135. connect(d->gamepadBackend, SIGNAL(gamepadNamed(int, QString)), this, SLOT(_q_forwardGamepadNameChanged(int, QString)));
  136. connect(d->gamepadBackend, SIGNAL(gamepadRemoved(int)), this, SLOT(_q_forwardGamepadDisconnected(int)));
  137. connect(d->gamepadBackend, SIGNAL(gamepadAxisMoved(int,QGamepadManager::GamepadAxis,double)), this, SLOT(_q_forwardGamepadAxisEvent(int,QGamepadManager::GamepadAxis,double)));
  138. connect(d->gamepadBackend, SIGNAL(gamepadButtonPressed(int,QGamepadManager::GamepadButton,double)), this, SLOT(_q_forwardGamepadButtonPressEvent(int,QGamepadManager::GamepadButton,double)));
  139. connect(d->gamepadBackend, SIGNAL(gamepadButtonReleased(int,QGamepadManager::GamepadButton)), this, SLOT(_q_forwardGamepadButtonReleaseEvent(int,QGamepadManager::GamepadButton)));
  140. connect(d->gamepadBackend, &QGamepadBackend::buttonConfigured, this, &QGamepadManager::buttonConfigured);
  141. connect(d->gamepadBackend, &QGamepadBackend::axisConfigured, this, &QGamepadManager::axisConfigured);
  142. connect(d->gamepadBackend, &QGamepadBackend::configurationCanceled, this, &QGamepadManager::configurationCanceled);
  143. if (!d->gamepadBackend->start())
  144. qCWarning(gp) << "Failed to start gamepad backend";
  145. }
  146. /*!
  147. * Destructor for QGamepadManager.
  148. */
  149. QGamepadManager::~QGamepadManager()
  150. {
  151. Q_D(QGamepadManager);
  152. d->gamepadBackend->stop();
  153. d->gamepadBackend->deleteLater();
  154. }
  155. /*!
  156. Returns the instance of the QGamepadManager.
  157. */
  158. QGamepadManager *QGamepadManager::instance()
  159. {
  160. static QGamepadManager instance;
  161. return &instance;
  162. }
  163. /*!
  164. Returns a boolean indicating whether the gamepad with
  165. the specified \a deviceId is connected or not.
  166. */
  167. bool QGamepadManager::isGamepadConnected(int deviceId) const
  168. {
  169. Q_D(const QGamepadManager);
  170. return d->connectedGamepads.contains(deviceId);
  171. }
  172. /*!
  173. Returns the name of the gamepad identified by \a deviceId.
  174. If \a deviceId does not identify a connected gamepad, returns an empty string.
  175. \since 5.11
  176. */
  177. QString QGamepadManager::gamepadName(int deviceId) const
  178. {
  179. Q_D(const QGamepadManager);
  180. return d->connectedGamepads.value(deviceId);
  181. }
  182. /*!
  183. Returns a QList containing the \l {QGamepad::}{deviceId}
  184. values of the connected gamepads.
  185. */
  186. const QList<int> QGamepadManager::connectedGamepads() const
  187. {
  188. Q_D(const QGamepadManager);
  189. return d->connectedGamepads.keys();
  190. }
  191. /*!
  192. Returns a boolean indicating whether configuration
  193. is needed for the specified \a deviceId.
  194. */
  195. bool QGamepadManager::isConfigurationNeeded(int deviceId) const
  196. {
  197. Q_D(const QGamepadManager);
  198. return d->gamepadBackend->isConfigurationNeeded(deviceId);
  199. }
  200. /*!
  201. Configures the specified \a button on the gamepad with
  202. this \a deviceId.
  203. Returns \c true in case of success.
  204. */
  205. bool QGamepadManager::configureButton(int deviceId, QGamepadManager::GamepadButton button)
  206. {
  207. Q_D(QGamepadManager);
  208. return d->gamepadBackend->configureButton(deviceId, button);
  209. }
  210. /*!
  211. Configures \a axis on the gamepad with the specified \a deviceId.
  212. Returns \c true in case of success.
  213. */
  214. bool QGamepadManager::configureAxis(int deviceId, QGamepadManager::GamepadAxis axis)
  215. {
  216. Q_D(QGamepadManager);
  217. return d->gamepadBackend->configureAxis(deviceId, axis);
  218. }
  219. /*!
  220. Configures \a button as the cancel button on the gamepad with
  221. id \a deviceId.
  222. Returns \c true in case of success.
  223. */
  224. bool QGamepadManager::setCancelConfigureButton(int deviceId, QGamepadManager::GamepadButton button)
  225. {
  226. Q_D(QGamepadManager);
  227. return d->gamepadBackend->setCancelConfigureButton(deviceId, button);
  228. }
  229. /*!
  230. Resets the configuration on the gamepad with the
  231. specified \a deviceId.
  232. */
  233. void QGamepadManager::resetConfiguration(int deviceId)
  234. {
  235. Q_D(QGamepadManager);
  236. d->gamepadBackend->resetConfiguration(deviceId);
  237. }
  238. /*!
  239. Sets the name of the \a file that stores the button and axis
  240. configuration data.
  241. */
  242. void QGamepadManager::setSettingsFile(const QString &file)
  243. {
  244. Q_D(QGamepadManager);
  245. d->gamepadBackend->setSettingsFile(file);
  246. }
  247. QT_END_NAMESPACE
  248. #include "moc_qgamepadmanager.cpp"