| 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 | #ifndef QEVDEVGAMEPADBACKEND_P_H |
| 38 | #define QEVDEVGAMEPADBACKEND_P_H |
| 39 | |
| 40 | #include <QtGamepad/QGamepadManager> |
| 41 | #include <QtGamepad/private/qgamepadbackend_p.h> |
| 42 | #include <QtCore/QHash> |
| 43 | #include <QtCore/QVector> |
| 44 | |
| 45 | struct input_event; |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | class QSocketNotifier; |
| 50 | class QDeviceDiscovery; |
| 51 | class QEvdevGamepadBackend; |
| 52 | |
| 53 | class QEvdevGamepadDevice : public QObject |
| 54 | { |
| 55 | Q_OBJECT |
| 56 | |
| 57 | public: |
| 58 | QEvdevGamepadDevice(const QByteArray &dev, QEvdevGamepadBackend *backend); |
| 59 | ~QEvdevGamepadDevice(); |
| 60 | QByteArray deviceName() const { return m_dev; } |
| 61 | int deviceId() const { return m_productId; } |
| 62 | void resetConfiguration(); |
| 63 | bool isConfigurationNeeded(); |
| 64 | bool configureButton(QGamepadManager::GamepadButton button); |
| 65 | bool configureAxis(QGamepadManager::GamepadAxis axis); |
| 66 | bool setCancelConfigureButton(QGamepadManager::GamepadButton button); |
| 67 | |
| 68 | private Q_SLOTS: |
| 69 | void readData(); |
| 70 | |
| 71 | private: |
| 72 | void saveData(); |
| 73 | void processInputEvent(input_event *e); |
| 74 | bool openDevice(const QByteArray &dev); |
| 75 | |
| 76 | QByteArray m_dev; |
| 77 | QEvdevGamepadBackend *m_backend; |
| 78 | int m_fd; |
| 79 | int m_productId; |
| 80 | bool m_needsConfigure; |
| 81 | QSocketNotifier *m_notifier; |
| 82 | struct EvdevAxisInfo : public QGamepadBackend::AxisInfo<int> |
| 83 | { |
| 84 | EvdevAxisInfo(); |
| 85 | EvdevAxisInfo(int fd, quint16 abs, int minValue = 0, int maxValue = 1, QGamepadManager::GamepadAxis gamepadAxis = QGamepadManager::AxisInvalid); |
| 86 | double normalized(int value) const; |
| 87 | void setAbsInfo(int fd, int abs); |
| 88 | void restoreSavedData(int fd, int abs, const QVariantMap &value); |
| 89 | QVariantMap dataToSave() const; |
| 90 | double flat; |
| 91 | QGamepadManager::GamepadButton gamepadMinButton; |
| 92 | QGamepadManager::GamepadButton gamepadMaxButton; |
| 93 | QGamepadManager::GamepadButton gamepadLastButton; |
| 94 | }; |
| 95 | typedef QHash<int, EvdevAxisInfo> AxisMap; |
| 96 | AxisMap m_axisMap; |
| 97 | |
| 98 | friend QDebug operator<<(QDebug dbg, const QEvdevGamepadDevice::EvdevAxisInfo &axisInfo); |
| 99 | |
| 100 | typedef QHash<int, QGamepadManager::GamepadButton> ButtonsMap; |
| 101 | ButtonsMap m_buttonsMap; |
| 102 | |
| 103 | QGamepadManager::GamepadButton m_configureButton; |
| 104 | QGamepadManager::GamepadAxis m_configureAxis; |
| 105 | QGamepadManager::GamepadButton m_configureCancelButton; |
| 106 | }; |
| 107 | |
| 108 | QDebug operator<<(QDebug dbg, const QEvdevGamepadDevice::EvdevAxisInfo &axisInfo); |
| 109 | |
| 110 | class QEvdevGamepadBackend : public QGamepadBackend |
| 111 | { |
| 112 | Q_OBJECT |
| 113 | |
| 114 | public: |
| 115 | QEvdevGamepadBackend(); |
| 116 | bool start() override; |
| 117 | void stop() override; |
| 118 | void resetConfiguration(int deviceId) override; |
| 119 | bool isConfigurationNeeded(int deviceId) override; |
| 120 | bool configureButton(int deviceId, QGamepadManager::GamepadButton button) override; |
| 121 | bool configureAxis(int deviceId, QGamepadManager::GamepadAxis axis) override; |
| 122 | bool setCancelConfigureButton(int deviceId, QGamepadManager::GamepadButton button) override; |
| 123 | |
| 124 | private slots: |
| 125 | void handleAddedDevice(const QString &device); |
| 126 | void handleRemovedDevice(const QString &device); |
| 127 | |
| 128 | private: |
| 129 | QEvdevGamepadDevice *newDevice(const QByteArray &device); |
| 130 | QEvdevGamepadDevice *device(int deviceId); |
| 131 | |
| 132 | QDeviceDiscovery *m_discovery; |
| 133 | QVector<QEvdevGamepadDevice *> m_devices; |
| 134 | }; |
| 135 | |
| 136 | QT_END_NAMESPACE |
| 137 | |
| 138 | #endif // QEVDEVGAMEPADBACKEND_P_H |
| 139 | |