summaryrefslogtreecommitdiffstats
path: root/src/input/frontend/qinputaspect.cpp
blob: 8a348e3dae274da3c8e269aaa04a344a09b78838 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/****************************************************************************
**
** Copyright (C) 2014 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 "qinputaspect.h"
#include "qinputaspect_p.h"

#include <Qt3DInput/qabstractphysicaldevice.h>
#include <Qt3DInput/qaction.h>
#include <Qt3DInput/qactioninput.h>
#include <Qt3DInput/qanalogaxisinput.h>
#include <Qt3DInput/qaxis.h>
#include <Qt3DInput/qaxisaccumulator.h>
#include <Qt3DInput/qaxissetting.h>
#include <Qt3DInput/qbuttonaxisinput.h>
#include <Qt3DInput/qinputchord.h>
#include <Qt3DInput/qinputsequence.h>
#include <Qt3DInput/qinputsettings.h>
#include <Qt3DInput/qkeyboarddevice.h>
#include <Qt3DInput/qkeyboardhandler.h>
#include <Qt3DInput/qlogicaldevice.h>
#include <Qt3DInput/qmousedevice.h>
#include <Qt3DInput/qmousehandler.h>
#include <QtCore/QDir>

#include <QtCore/QLibraryInfo>
#include <QtCore/QPluginLoader>

#include <Qt3DInput/private/backendnode_p.h>
#include <Qt3DInput/private/action_p.h>
#include <Qt3DInput/private/actioninput_p.h>
#include <Qt3DInput/private/axis_p.h>
#include <Qt3DInput/private/axisaccumulator_p.h>
#include <Qt3DInput/private/axisaccumulatorjob_p.h>
#include <Qt3DInput/private/axissetting_p.h>
#include <Qt3DInput/private/buttonaxisinput_p.h>
#include <Qt3DInput/private/eventsourcesetterhelper_p.h>
#include <Qt3DInput/private/genericdevicebackendnode_p.h>
#include <Qt3DInput/private/inputbackendnodefunctor_p.h>
#include <Qt3DInput/private/inputchord_p.h>
#include <Qt3DInput/private/inputhandler_p.h>
#include <Qt3DInput/private/inputmanagers_p.h>
#include <Qt3DInput/private/inputsequence_p.h>
#include <Qt3DInput/private/inputsettings_p.h>
#include <Qt3DInput/private/keyboarddevice_p.h>
#include <Qt3DInput/private/keyboardhandler_p.h>
#include <Qt3DInput/private/keyboardmousegenericdeviceintegration_p.h>
#include <Qt3DInput/private/loadproxydevicejob_p.h>
#include <Qt3DInput/private/logicaldevice_p.h>
#include <Qt3DInput/private/mousedevice_p.h>
#include <Qt3DInput/private/mousehandler_p.h>
#include <Qt3DInput/private/qabstractphysicaldeviceproxy_p.h>
#include <Qt3DInput/private/qgenericinputdevice_p.h>
#include <Qt3DInput/private/qinputdeviceintegration_p.h>
#include <Qt3DInput/private/qinputdeviceintegrationfactory_p.h>
#include <Qt3DInput/private/updateaxisactionjob_p.h>
#include <Qt3DCore/private/qeventfilterservice_p.h>
#include <Qt3DCore/private/qservicelocator_p.h>

#ifdef HAVE_QGAMEPAD
# include <Qt3DInput/private/qgamepadinput_p.h>
#endif

QT_BEGIN_NAMESPACE

using namespace Qt3DCore;

namespace Qt3DInput {

QInputAspectPrivate::QInputAspectPrivate()
    : QAbstractAspectPrivate()
    , m_inputHandler(new Input::InputHandler())
    , m_keyboardMouseIntegration(new Input::KeyboardMouseGenericDeviceIntegration(m_inputHandler.data()))
    , m_time(0)
{
}

void QInputAspectPrivate::syncDirtyFrontEndNode(QNode *node, QBackendNode *backend, bool firstTime) const
{
    Input::BackendNode *renderBackend = static_cast<Input::BackendNode *>(backend);
    renderBackend->syncFromFrontEnd(node, firstTime);
}


/*!
    \class Qt3DInput::QInputAspect
    \inherits Qt3DCore::QAbstractAspect
    \inmodule Qt3DInput
    \brief Responsible for creating physical devices and handling associated jobs.
    \since 5.5
    \brief Handles mapping between front and backend nodes

    QInputAspect is responsible for creating physical devices.
    It is also the object responsible establishing the jobs to run at a particular time from the current input setup.
*/

/*!
 * Constructs a new QInputAspect with \a parent.
 */
QInputAspect::QInputAspect(QObject *parent)
    : QInputAspect(*new QInputAspectPrivate, parent)
{
}

/*! \internal */
QInputAspect::QInputAspect(QInputAspectPrivate &dd, QObject *parent)
    : QAbstractAspect(dd, parent)
{
    setObjectName(QStringLiteral("Input Aspect"));

    qRegisterMetaType<Qt3DInput::QAbstractPhysicalDevice*>();

    registerBackendType<QKeyboardDevice, true>(QBackendNodeMapperPtr(new Input::KeyboardDeviceFunctor(this, d_func()->m_inputHandler.data())));
    registerBackendType<QKeyboardHandler, true>(QBackendNodeMapperPtr(new Input::KeyboardHandlerFunctor(d_func()->m_inputHandler.data())));
    registerBackendType<QMouseDevice, true>(QBackendNodeMapperPtr(new Input::MouseDeviceFunctor(this, d_func()->m_inputHandler.data())));
    registerBackendType<QMouseHandler, true>(QBackendNodeMapperPtr(new Input::MouseHandlerFunctor(d_func()->m_inputHandler.data())));
    registerBackendType<QAxis, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::Axis, Input::AxisManager>(d_func()->m_inputHandler->axisManager())));
    registerBackendType<QAxisAccumulator, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::AxisAccumulator, Input::AxisAccumulatorManager>(d_func()->m_inputHandler->axisAccumulatorManager())));
    registerBackendType<QAnalogAxisInput, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::AnalogAxisInput, Input::AnalogAxisInputManager>(d_func()->m_inputHandler->analogAxisInputManager())));
    registerBackendType<QButtonAxisInput, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::ButtonAxisInput, Input::ButtonAxisInputManager>(d_func()->m_inputHandler->buttonAxisInputManager())));
    registerBackendType<QAxisSetting, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::AxisSetting, Input::AxisSettingManager>(d_func()->m_inputHandler->axisSettingManager())));
    registerBackendType<Qt3DInput::QAction, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::Action, Input::ActionManager>(d_func()->m_inputHandler->actionManager())));
    registerBackendType<QActionInput,  true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::ActionInput, Input::ActionInputManager>(d_func()->m_inputHandler->actionInputManager())));
    registerBackendType<QInputChord, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::InputChord, Input::InputChordManager>(d_func()->m_inputHandler->inputChordManager())));
    registerBackendType<QInputSequence, true>(QBackendNodeMapperPtr(new Input::InputNodeFunctor<Input::InputSequence, Input::InputSequenceManager>(d_func()->m_inputHandler->inputSequenceManager())));
    registerBackendType<QLogicalDevice, true>(QBackendNodeMapperPtr(new Input::LogicalDeviceNodeFunctor(d_func()->m_inputHandler->logicalDeviceManager())));
    registerBackendType<QGenericInputDevice, true>(QBackendNodeMapperPtr(new Input::GenericDeviceBackendFunctor(this, d_func()->m_inputHandler.data())));
    registerBackendType<QInputSettings, true>(QBackendNodeMapperPtr(new Input::InputSettingsFunctor(d_func()->m_inputHandler.data())));
    registerBackendType<QAbstractPhysicalDeviceProxy, true>(QBackendNodeMapperPtr(new Input::PhysicalDeviceProxyNodeFunctor(d_func()->m_inputHandler->physicalDeviceProxyManager())));

#ifdef HAVE_QGAMEPAD
    registerBackendType<QGamepadInput, true>(QBackendNodeMapperPtr(new Input::GenericDeviceBackendFunctor(this, d_func()->m_inputHandler.data())));
#endif

    Q_D(QInputAspect);
    // Plugins are QInputDeviceIntegration instances
    d->loadInputDevicePlugins();

    // KeyboardDevice and MouseDevice also provide their own QInputDeviceIntegration
    d->m_inputHandler->addInputDeviceIntegration(d->m_keyboardMouseIntegration.data());
}

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

/*
   Create each of the detected input device integrations through the Integration Factory
 */
void QInputAspectPrivate::loadInputDevicePlugins()
{
    const QStringList keys = QInputDeviceIntegrationFactory::keys();
    for (const QString &key : keys) {
        Qt3DInput::QInputDeviceIntegration *integration = QInputDeviceIntegrationFactory::create(key, QStringList());
        if (integration != nullptr) {
            m_inputHandler->addInputDeviceIntegration(integration);
            // Initialize will allow the InputDeviceIntegration to
            // register their frontend / backend types,
            // create their managers
            // launch a thread to listen to the actual physical device....
            integration->initialize(q_func());
        }
    }
}

/*!
    Create a physical device identified by \a name using the input device integrations present
    returns a \c nullptr if it is not found.

    \note Caller is responsible for ownership.
*/
QAbstractPhysicalDevice *QInputAspect::createPhysicalDevice(const QString &name)
{
    Q_D(QInputAspect);
    return d->m_inputHandler->createPhysicalDevice(name);
}

/*!
    Returns a list of all available physical devices.
 */
QStringList QInputAspect::availablePhysicalDevices() const
{
    Q_D(const QInputAspect);
    QStringList deviceNamesList;
    const auto deviceIntegrations = d->m_inputHandler->inputDeviceIntegrations();
    for (const QInputDeviceIntegration *integration : deviceIntegrations)
        deviceNamesList += integration->deviceNames();
    return deviceNamesList;
}

/*!
    \internal
 */
QVector<QAspectJobPtr> QInputAspect::jobsToExecute(qint64 time)
{
    Q_D(QInputAspect);
    const qint64 deltaTime = time - d->m_time;
    const float dt = static_cast<float>(deltaTime) / 1.0e9;
    d->m_time = time;

    QVector<QAspectJobPtr> jobs;

    d->m_inputHandler->updateEventSource();

    jobs.append(d->m_inputHandler->keyboardJobs());
    jobs.append(d->m_inputHandler->mouseJobs());

    const auto integrations = d->m_inputHandler->inputDeviceIntegrations();
    for (QInputDeviceIntegration *integration : integrations)
        jobs += integration->jobsToExecute(time);

    const QVector<Qt3DCore::QNodeId> proxiesToLoad = d->m_inputHandler->physicalDeviceProxyManager()->takePendingProxiesToLoad();
    if (!proxiesToLoad.isEmpty()) {
        // Since loading wrappers occurs quite rarely, no point in keeping the job in a
        // member variable
        auto loadWrappersJob = Input::LoadProxyDeviceJobPtr::create();
        loadWrappersJob->setProxiesToLoad(std::move(proxiesToLoad));
        loadWrappersJob->setInputHandler(d->m_inputHandler.data());
        jobs.push_back(loadWrappersJob);
    }

    // All the jobs added up until this point are independents
    // but the axis action jobs will be dependent on these
    const QVector<QAspectJobPtr> dependsOnJobs = jobs;

    // Jobs that update Axis/Action (store combined axis/action value)
    const auto devHandles = d->m_inputHandler->logicalDeviceManager()->activeDevices();
    QVector<QAspectJobPtr> axisActionJobs;
    axisActionJobs.reserve(devHandles.size());
    for (const Input::HLogicalDevice &devHandle : devHandles) {
        const auto device = d->m_inputHandler->logicalDeviceManager()->data(devHandle);
        if (!device->isEnabled())
            continue;

        QAspectJobPtr updateAxisActionJob(new Input::UpdateAxisActionJob(time, d->m_inputHandler.data(), devHandle));
        jobs += updateAxisActionJob;
        axisActionJobs.push_back(updateAxisActionJob);
        for (const QAspectJobPtr &job : dependsOnJobs)
            updateAxisActionJob->addDependency(job);
    }

    // Once all the axes have been updated we can step the integrations on
    // the AxisAccumulators
    auto accumulateJob = Input::AxisAccumulatorJobPtr::create(d->m_inputHandler->axisAccumulatorManager(),
                                                              d->m_inputHandler->axisManager());
    accumulateJob->setDeltaTime(dt);
    for (const QAspectJobPtr &job : qAsConst(axisActionJobs))
        accumulateJob->addDependency(job);
    jobs.push_back(accumulateJob);

    return jobs;
}

/*!
    \internal
 */
void QInputAspect::onRegistered()
{
    Q_D(QInputAspect);
    Qt3DCore::QEventFilterService *eventService = d->services()->eventFilterService();
    Q_ASSERT(eventService);

    // Set it on the input handler which will also handle its lifetime
    d->m_inputHandler->eventSourceHelper()->setEventFilterService(eventService);
}

/*!
    \internal
 */
void QInputAspect::onUnregistered()
{
    Q_D(QInputAspect);
    // At this point it is too late to call removeEventFilter as the eventSource (Window)
    // may already be destroyed
    d->m_inputHandler.reset(nullptr);
}

} // namespace Qt3DInput

QT_END_NAMESPACE

QT3D_REGISTER_NAMESPACED_ASPECT("input", QT_PREPEND_NAMESPACE(Qt3DInput), QInputAspect)