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 "physicaldeviceproxy_p.h"
5
6#include <Qt3DInput/qabstractphysicaldevice.h>
7#include <QtCore/QCoreApplication>
8
9#include <Qt3DInput/private/inputmanagers_p.h>
10#include <Qt3DInput/private/qabstractphysicaldeviceproxy_p.h>
11#include <Qt3DInput/private/qabstractphysicaldeviceproxy_p_p.h>
12
13QT_BEGIN_NAMESPACE
14
15namespace Qt3DInput {
16
17namespace Input {
18
19PhysicalDeviceProxy::PhysicalDeviceProxy()
20 : BackendNode(QBackendNode::ReadWrite)
21 , m_manager(nullptr)
22{
23}
24
25void PhysicalDeviceProxy::cleanup()
26{
27 QBackendNode::setEnabled(false);
28 m_deviceName.clear();
29 m_manager = nullptr;
30 m_physicalDeviceId = Qt3DCore::QNodeId();
31}
32
33QString PhysicalDeviceProxy::deviceName() const
34{
35 return m_deviceName;
36}
37
38void PhysicalDeviceProxy::setManager(PhysicalDeviceProxyManager *manager)
39{
40 m_manager = manager;
41}
42
43PhysicalDeviceProxyManager *PhysicalDeviceProxy::manager() const
44{
45 return m_manager;
46}
47
48void PhysicalDeviceProxy::setDevice(QAbstractPhysicalDevice *device)
49{
50 m_physicalDeviceId = Qt3DCore::QNodeId();
51 // Move the device to the main thread
52 if (device != nullptr) {
53 m_physicalDeviceId = device->id();
54 device->moveToThread(thread: QCoreApplication::instance()->thread());
55 }
56}
57
58Qt3DCore::QNodeId PhysicalDeviceProxy::physicalDeviceId() const
59{
60 return m_physicalDeviceId;
61}
62
63void PhysicalDeviceProxy::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime)
64{
65 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
66
67 if (firstTime) {
68 const QAbstractPhysicalDeviceProxy *node = qobject_cast<const QAbstractPhysicalDeviceProxy *>(object: frontEnd);
69 if (!node)
70 return;
71
72 m_deviceName = node->deviceName();
73
74 // Request to load the actual device
75 m_manager->addPendingProxyToLoad(id: peerId());
76 }
77}
78
79PhysicalDeviceProxyNodeFunctor::PhysicalDeviceProxyNodeFunctor(PhysicalDeviceProxyManager *manager)
80 : m_manager(manager)
81{
82}
83
84Qt3DCore::QBackendNode *PhysicalDeviceProxyNodeFunctor::create(Qt3DCore::QNodeId id) const
85{
86 HPhysicalDeviceProxy handle = m_manager->getOrAcquireHandle(id);
87 PhysicalDeviceProxy *backend = m_manager->data(handle);
88 backend->setManager(m_manager);
89 return backend;
90}
91
92Qt3DCore::QBackendNode *PhysicalDeviceProxyNodeFunctor::get(Qt3DCore::QNodeId id) const
93{
94 return m_manager->lookupResource(id);
95}
96
97void PhysicalDeviceProxyNodeFunctor::destroy(Qt3DCore::QNodeId id) const
98{
99 m_manager->releaseResource(id);
100}
101
102} // namespace Input
103
104} // namespace Qt3DInput
105
106QT_END_NAMESPACE
107
108

source code of qt3d/src/input/backend/physicaldeviceproxy.cpp