1// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qabstractphysicaldeviceproxy_p.h"
5#include "qabstractphysicaldeviceproxy_p_p.h"
6
7
8QT_BEGIN_NAMESPACE
9
10namespace Qt3DInput {
11
12/*!
13 \internal
14 */
15QAbstractPhysicalDeviceProxyPrivate::QAbstractPhysicalDeviceProxyPrivate(const QString &deviceName)
16 : QAbstractPhysicalDevicePrivate()
17 , m_deviceName(deviceName)
18 , m_status(QAbstractPhysicalDeviceProxy::NotFound)
19 , m_device(nullptr)
20{
21}
22
23/*!
24 \internal
25 */
26QAbstractPhysicalDeviceProxyPrivate::~QAbstractPhysicalDeviceProxyPrivate()
27{
28}
29
30/*!
31 \internal
32 */
33void QAbstractPhysicalDeviceProxyPrivate::setStatus(QAbstractPhysicalDeviceProxy::DeviceStatus status)
34{
35 if (status != m_status) {
36 m_status = status;
37 emit q_func()->statusChanged(status);
38 }
39}
40
41/*!
42 \class Qt3DInput::QAbstractPhysicalDeviceProxy
43 \inmodule Qt3DInput
44
45 \brief Qt3DInput::QAbstractPhysicalDeviceProxy acts as a proxy
46 for an actual Qt3DInput::QQAbstractPhysicalDevice device.
47
48 Qt3DInput::QAbstractPhysicalDeviceProxy can be used to facilitate
49 exposing a physical device to users. It alleviates the need to introspect
50 the axis and buttons based on their names.
51
52 It is typcally used through subclassing allowing to set the device name and
53 defining enums for the various axis and buttons of your targeted device.
54
55 At runtime, the status property will be updated to reflect whether an
56 actual device matching the device name could be created.
57
58 \since 5.8
59 */
60
61QString QAbstractPhysicalDeviceProxy::deviceName() const
62{
63 Q_D(const QAbstractPhysicalDeviceProxy);
64 return d->m_deviceName;
65}
66
67QAbstractPhysicalDeviceProxy::DeviceStatus QAbstractPhysicalDeviceProxy::status() const
68{
69 Q_D(const QAbstractPhysicalDeviceProxy);
70 return d->m_status;
71}
72
73int QAbstractPhysicalDeviceProxy::axisCount() const
74{
75 Q_D(const QAbstractPhysicalDeviceProxy);
76 if (d->m_device != nullptr)
77 return d->m_device->axisCount();
78 return 0;
79}
80
81int QAbstractPhysicalDeviceProxy::buttonCount() const
82{
83 Q_D(const QAbstractPhysicalDeviceProxy);
84 if (d->m_device != nullptr)
85 return d->m_device->buttonCount();
86 return 0;
87}
88
89QStringList QAbstractPhysicalDeviceProxy::axisNames() const
90{
91 Q_D(const QAbstractPhysicalDeviceProxy);
92 if (d->m_device != nullptr)
93 return d->m_device->axisNames();
94 return QStringList();
95}
96
97QStringList QAbstractPhysicalDeviceProxy::buttonNames() const
98{
99 Q_D(const QAbstractPhysicalDeviceProxy);
100 if (d->m_device != nullptr)
101 return d->m_device->buttonNames();
102 return QStringList();
103}
104
105int QAbstractPhysicalDeviceProxy::axisIdentifier(const QString &name) const
106{
107 Q_D(const QAbstractPhysicalDeviceProxy);
108 if (d->m_device != nullptr)
109 return d->m_device->axisIdentifier(name);
110 return -1;
111}
112
113int QAbstractPhysicalDeviceProxy::buttonIdentifier(const QString &name) const
114{
115 Q_D(const QAbstractPhysicalDeviceProxy);
116 if (d->m_device != nullptr)
117 return d->m_device->buttonIdentifier(name);
118 return -1;
119}
120
121/*!
122 \internal
123 */
124QAbstractPhysicalDeviceProxy::QAbstractPhysicalDeviceProxy(QAbstractPhysicalDeviceProxyPrivate &dd, Qt3DCore::QNode *parent)
125 : QAbstractPhysicalDevice(dd, parent)
126{
127}
128
129/*!
130 \internal
131 */
132void QAbstractPhysicalDeviceProxyPrivate::setDevice(QAbstractPhysicalDevice *device)
133{
134 Q_Q(QAbstractPhysicalDeviceProxy);
135
136 // Note: technically book keeping could be optional since we are the parent
137 // of the device. But who knows if someone plays with the object tree...
138
139 // Unset bookkeeper
140 if (m_device != nullptr) {
141 // Note: we cannot delete the device here as we don't how if we are
142 // called by the bookkeeper (in which case we would do a double free)
143 // or by the sceneChangeEvent
144 unregisterDestructionHelper(node: m_device);
145 setStatus(QAbstractPhysicalDeviceProxy::NotFound);
146 }
147
148 // Set parent so that node is created in the backend
149 if (device != nullptr && device->parent() == nullptr)
150 device->setParent(q);
151
152 m_device = device;
153
154 // Set bookkeeper
155 if (device != nullptr) {
156 setStatus(QAbstractPhysicalDeviceProxy::Ready);
157 registerPrivateDestructionHelper(node: m_device, func: &QAbstractPhysicalDeviceProxyPrivate::resetDevice);
158 }
159}
160
161void QAbstractPhysicalDeviceProxyPrivate::resetDevice(QAbstractPhysicalDevice *device)
162{
163 if (m_device == device) {
164 // Note: we cannot delete the device here as we don't how if we are
165 // called by the bookkeeper (in which case we would do a double free)
166 // or by the sceneChangeEvent
167 unregisterDestructionHelper(node: m_device);
168 setStatus(QAbstractPhysicalDeviceProxy::NotFound);
169
170 m_device = nullptr;
171 }
172}
173
174} // Qt3DInput
175
176QT_END_NAMESPACE
177
178#include "moc_qabstractphysicaldeviceproxy_p.cpp"
179

source code of qt3d/src/input/frontend/qabstractphysicaldeviceproxy.cpp