| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qabstractphysicaldeviceproxy_p.h" |
| 41 | #include "qabstractphysicaldeviceproxy_p_p.h" |
| 42 | |
| 43 | #include <Qt3DInput/qphysicaldevicecreatedchange.h> |
| 44 | |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | namespace Qt3DInput { |
| 49 | |
| 50 | /*! |
| 51 | \internal |
| 52 | */ |
| 53 | QAbstractPhysicalDeviceProxyPrivate::QAbstractPhysicalDeviceProxyPrivate(const QString &deviceName) |
| 54 | : QAbstractPhysicalDevicePrivate() |
| 55 | , m_deviceName(deviceName) |
| 56 | , m_status(QAbstractPhysicalDeviceProxy::NotFound) |
| 57 | , m_device(nullptr) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | /*! |
| 62 | \internal |
| 63 | */ |
| 64 | QAbstractPhysicalDeviceProxyPrivate::~QAbstractPhysicalDeviceProxyPrivate() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | /*! |
| 69 | \internal |
| 70 | */ |
| 71 | void QAbstractPhysicalDeviceProxyPrivate::setStatus(QAbstractPhysicalDeviceProxy::DeviceStatus status) |
| 72 | { |
| 73 | if (status != m_status) { |
| 74 | m_status = status; |
| 75 | emit q_func()->statusChanged(status); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | \class Qt3DInput::QAbstractPhysicalDeviceProxy |
| 81 | \inmodule Qt3DInput |
| 82 | |
| 83 | \brief Qt3DInput::QAbstractPhysicalDeviceProxy acts as a proxy |
| 84 | for an actual Qt3DInput::QQAbstractPhysicalDevice device. |
| 85 | |
| 86 | Qt3DInput::QAbstractPhysicalDeviceProxy can be used to facilitate |
| 87 | exposing a physical device to users. It alleviates the need to introspect |
| 88 | the axis and buttons based on their names. |
| 89 | |
| 90 | It is typcally used through subclassing allowing to set the device name and |
| 91 | defining enums for the various axis and buttons of your targeted device. |
| 92 | |
| 93 | At runtime, the status property will be updated to reflect whether an |
| 94 | actual device matching the device name could be created. |
| 95 | |
| 96 | \since 5.8 |
| 97 | */ |
| 98 | |
| 99 | QString QAbstractPhysicalDeviceProxy::deviceName() const |
| 100 | { |
| 101 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 102 | return d->m_deviceName; |
| 103 | } |
| 104 | |
| 105 | QAbstractPhysicalDeviceProxy::DeviceStatus QAbstractPhysicalDeviceProxy::status() const |
| 106 | { |
| 107 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 108 | return d->m_status; |
| 109 | } |
| 110 | |
| 111 | int QAbstractPhysicalDeviceProxy::axisCount() const |
| 112 | { |
| 113 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 114 | if (d->m_device != nullptr) |
| 115 | return d->m_device->axisCount(); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int QAbstractPhysicalDeviceProxy::buttonCount() const |
| 120 | { |
| 121 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 122 | if (d->m_device != nullptr) |
| 123 | return d->m_device->buttonCount(); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | QStringList QAbstractPhysicalDeviceProxy::axisNames() const |
| 128 | { |
| 129 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 130 | if (d->m_device != nullptr) |
| 131 | return d->m_device->axisNames(); |
| 132 | return QStringList(); |
| 133 | } |
| 134 | |
| 135 | QStringList QAbstractPhysicalDeviceProxy::buttonNames() const |
| 136 | { |
| 137 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 138 | if (d->m_device != nullptr) |
| 139 | return d->m_device->buttonNames(); |
| 140 | return QStringList(); |
| 141 | } |
| 142 | |
| 143 | int QAbstractPhysicalDeviceProxy::axisIdentifier(const QString &name) const |
| 144 | { |
| 145 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 146 | if (d->m_device != nullptr) |
| 147 | return d->m_device->axisIdentifier(name); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | int QAbstractPhysicalDeviceProxy::buttonIdentifier(const QString &name) const |
| 152 | { |
| 153 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 154 | if (d->m_device != nullptr) |
| 155 | return d->m_device->buttonIdentifier(name); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | /*! |
| 160 | \internal |
| 161 | */ |
| 162 | QAbstractPhysicalDeviceProxy::QAbstractPhysicalDeviceProxy(QAbstractPhysicalDeviceProxyPrivate &dd, Qt3DCore::QNode *parent) |
| 163 | : QAbstractPhysicalDevice(dd, parent) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | /*! |
| 168 | \internal |
| 169 | */ |
| 170 | Qt3DCore::QNodeCreatedChangeBasePtr QAbstractPhysicalDeviceProxy::createNodeCreationChange() const |
| 171 | { |
| 172 | auto creationChange = QPhysicalDeviceCreatedChangePtr<QAbstractPhysicalDeviceProxyData>::create(arguments: this); |
| 173 | QAbstractPhysicalDeviceProxyData &data = creationChange->data; |
| 174 | |
| 175 | Q_D(const QAbstractPhysicalDeviceProxy); |
| 176 | data.deviceName = d->m_deviceName; |
| 177 | |
| 178 | return creationChange; |
| 179 | } |
| 180 | |
| 181 | /*! |
| 182 | \internal |
| 183 | */ |
| 184 | void QAbstractPhysicalDeviceProxyPrivate::setDevice(QAbstractPhysicalDevice *device) |
| 185 | { |
| 186 | Q_Q(QAbstractPhysicalDeviceProxy); |
| 187 | |
| 188 | // Note: technically book keeping could be optional since we are the parent |
| 189 | // of the device. But who knows if someone plays with the object tree... |
| 190 | |
| 191 | // Unset bookkeeper |
| 192 | if (m_device != nullptr) { |
| 193 | // Note: we cannot delete the device here as we don't how if we are |
| 194 | // called by the bookkeeper (in which case we would do a double free) |
| 195 | // or by the sceneChangeEvent |
| 196 | unregisterDestructionHelper(node: m_device); |
| 197 | setStatus(QAbstractPhysicalDeviceProxy::NotFound); |
| 198 | } |
| 199 | |
| 200 | // Set parent so that node is created in the backend |
| 201 | if (device != nullptr && device->parent() == nullptr) |
| 202 | device->setParent(q); |
| 203 | |
| 204 | m_device = device; |
| 205 | |
| 206 | // Set bookkeeper |
| 207 | if (device != nullptr) { |
| 208 | setStatus(QAbstractPhysicalDeviceProxy::Ready); |
| 209 | registerPrivateDestructionHelper(node: m_device, func: &QAbstractPhysicalDeviceProxyPrivate::resetDevice); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void QAbstractPhysicalDeviceProxyPrivate::resetDevice(QAbstractPhysicalDevice *device) |
| 214 | { |
| 215 | if (m_device == device) { |
| 216 | // Note: we cannot delete the device here as we don't how if we are |
| 217 | // called by the bookkeeper (in which case we would do a double free) |
| 218 | // or by the sceneChangeEvent |
| 219 | unregisterDestructionHelper(node: m_device); |
| 220 | setStatus(QAbstractPhysicalDeviceProxy::NotFound); |
| 221 | |
| 222 | m_device = nullptr; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | } // Qt3DInput |
| 227 | |
| 228 | QT_END_NAMESPACE |
| 229 | |