summaryrefslogtreecommitdiffstats
path: root/src/input/frontend/qabstractphysicaldeviceproxy.cpp
blob: d449bced0fc127ef11482a6d4ee0b08d07519511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/****************************************************************************
**
** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qabstractphysicaldeviceproxy_p.h"
#include "qabstractphysicaldeviceproxy_p_p.h"

#include <Qt3DInput/qphysicaldevicecreatedchange.h>


QT_BEGIN_NAMESPACE

namespace Qt3DInput {

/*!
    \internal
 */
QAbstractPhysicalDeviceProxyPrivate::QAbstractPhysicalDeviceProxyPrivate(const QString &deviceName)
    : QAbstractPhysicalDevicePrivate()
    , m_deviceName(deviceName)
    , m_status(QAbstractPhysicalDeviceProxy::NotFound)
    , m_device(nullptr)
{
}

/*!
    \internal
 */
QAbstractPhysicalDeviceProxyPrivate::~QAbstractPhysicalDeviceProxyPrivate()
{
}

/*!
    \internal
 */
void QAbstractPhysicalDeviceProxyPrivate::setStatus(QAbstractPhysicalDeviceProxy::DeviceStatus status)
{
    if (status != m_status) {
        m_status = status;
        emit q_func()->statusChanged(status);
    }
}

/*!
    \class Qt3DInput::QAbstractPhysicalDeviceProxy
    \inmodule Qt3DInput

    \brief Qt3DInput::QAbstractPhysicalDeviceProxy acts as a proxy
    for an actual Qt3DInput::QQAbstractPhysicalDevice device.

    Qt3DInput::QAbstractPhysicalDeviceProxy can be used to facilitate
    exposing a physical device to users. It alleviates the need to introspect
    the axis and buttons based on their names.

    It is typcally used through subclassing allowing to set the device name and
    defining enums for the various axis and buttons of your targeted device.

    At runtime, the status property will be updated to reflect whether an
    actual device matching the device name could be created.

    \since 5.8
 */

QString QAbstractPhysicalDeviceProxy::deviceName() const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    return d->m_deviceName;
}

QAbstractPhysicalDeviceProxy::DeviceStatus QAbstractPhysicalDeviceProxy::status() const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    return d->m_status;
}

int QAbstractPhysicalDeviceProxy::axisCount() const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    if (d->m_device != nullptr)
        return d->m_device->axisCount();
    return 0;
}

int QAbstractPhysicalDeviceProxy::buttonCount() const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    if (d->m_device != nullptr)
        return d->m_device->buttonCount();
    return 0;
}

QStringList QAbstractPhysicalDeviceProxy::axisNames() const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    if (d->m_device != nullptr)
        return d->m_device->axisNames();
    return QStringList();
}

QStringList QAbstractPhysicalDeviceProxy::buttonNames() const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    if (d->m_device != nullptr)
        return d->m_device->buttonNames();
    return QStringList();
}

int QAbstractPhysicalDeviceProxy::axisIdentifier(const QString &name) const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    if (d->m_device != nullptr)
        return d->m_device->axisIdentifier(name);
    return -1;
}

int QAbstractPhysicalDeviceProxy::buttonIdentifier(const QString &name) const
{
    Q_D(const QAbstractPhysicalDeviceProxy);
    if (d->m_device != nullptr)
        return d->m_device->buttonIdentifier(name);
    return -1;
}

/*!
    \internal
 */
QAbstractPhysicalDeviceProxy::QAbstractPhysicalDeviceProxy(QAbstractPhysicalDeviceProxyPrivate &dd, Qt3DCore::QNode *parent)
    : QAbstractPhysicalDevice(dd, parent)
{
}

/*!
    \internal
 */
Qt3DCore::QNodeCreatedChangeBasePtr QAbstractPhysicalDeviceProxy::createNodeCreationChange() const
{
    auto creationChange = QPhysicalDeviceCreatedChangePtr<QAbstractPhysicalDeviceProxyData>::create(this);
    QAbstractPhysicalDeviceProxyData &data = creationChange->data;

    Q_D(const QAbstractPhysicalDeviceProxy);
    data.deviceName = d->m_deviceName;

    return creationChange;
}

/*!
    \internal
 */
void QAbstractPhysicalDeviceProxyPrivate::setDevice(QAbstractPhysicalDevice *device)
{
    Q_Q(QAbstractPhysicalDeviceProxy);

    // Note: technically book keeping could be optional since we are the parent
    // of the device. But who knows if someone plays with the object tree...

    // Unset bookkeeper
    if (m_device != nullptr) {
        // Note: we cannot delete the device here as we don't how if we are
        // called by the bookkeeper (in which case we would do a double free)
        // or by the sceneChangeEvent
        unregisterDestructionHelper(m_device);
        setStatus(QAbstractPhysicalDeviceProxy::NotFound);
    }

    // Set parent so that node is created in the backend
    if (device != nullptr && device->parent() == nullptr)
        device->setParent(q);

    m_device = device;

    // Set bookkeeper
    if (device != nullptr) {
       setStatus(QAbstractPhysicalDeviceProxy::Ready);
       registerPrivateDestructionHelper(m_device, &QAbstractPhysicalDeviceProxyPrivate::resetDevice);
    }
}

void QAbstractPhysicalDeviceProxyPrivate::resetDevice(QAbstractPhysicalDevice *device)
{
    if (m_device == device) {
        // Note: we cannot delete the device here as we don't how if we are
        // called by the bookkeeper (in which case we would do a double free)
        // or by the sceneChangeEvent
        unregisterDestructionHelper(m_device);
        setStatus(QAbstractPhysicalDeviceProxy::NotFound);

        m_device = nullptr;
    }
}

} // Qt3DInput

QT_END_NAMESPACE