File: WebXRInputSource.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (187 lines) | stat: -rw-r--r-- 7,455 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2020 Igalia S.L. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WebXRInputSource.h"

#if ENABLE(WEBXR)

#include "EventNames.h"
#if ENABLE(GAMEPAD)
#include "Gamepad.h"
#include "WebXRGamepad.h"
#endif
#include "WebXRFrame.h"
#include "WebXRSession.h"
#include "XRInputSourceEvent.h"
#include <wtf/TZoneMallocInlines.h>

namespace WebCore {

WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(WebXRInputSource);

Ref<WebXRInputSource> WebXRInputSource::create(Document& document, WebXRSession& session, double timestamp, const PlatformXR::FrameData::InputSource& source)
{
    return adoptRef(*new WebXRInputSource(document, session, timestamp, source));
}

WebXRInputSource::WebXRInputSource(Document& document, WebXRSession& session, double timestamp, const PlatformXR::FrameData::InputSource& source)
    : m_session(session)
    , m_targetRaySpace(WebXRInputSpace::create(document, session, source.pointerOrigin))
    , m_connectTime(timestamp)
#if ENABLE(GAMEPAD)
    , m_gamepad(Gamepad::create(&document, WebXRGamepad(timestamp, timestamp, source)))
#endif
{
    update(timestamp, source);
}

WebXRInputSource::~WebXRInputSource() = default;

WebXRSession* WebXRInputSource::session()
{
    return m_session.get();
}

void WebXRInputSource::update(double timestamp, const PlatformXR::FrameData::InputSource& source)
{
    RefPtr session = m_session.get();
    if (!session)
        return;

#if !ENABLE(GAMEPAD)
    UNUSED_PARAM(timestamp);
#endif
    m_source = source;
    m_targetRaySpace->setPose(source.pointerOrigin);
    if (auto gripOrigin = source.gripOrigin) {
        if (m_gripSpace)
            m_gripSpace->setPose(*gripOrigin);
        else if (RefPtr document = downcast<Document>(session->scriptExecutionContext()))
            m_gripSpace = WebXRInputSpace::create(*document, *session, *gripOrigin);
    } else
        m_gripSpace = nullptr;
#if ENABLE(GAMEPAD)
    m_gamepad->updateFromPlatformGamepad(WebXRGamepad(timestamp, m_connectTime, source));
#endif

#if ENABLE(WEBXR_HANDS)
    if (source.handJoints) {
        // https://www.w3.org/TR/webxr-hand-input-1/#xrinputsource-interface
        // If the XRInputSource belongs to an XRSession that has not been requested
        // with the "hand-tracking" feature descriptor, hand MUST be null.
        if (!m_hand && session->isHandTrackingEnabled())
            m_hand = WebXRHand::create(*this);

        if (m_hand)
            m_hand->updateFromInputSource(source);

        return;
    }
#endif
}

bool WebXRInputSource::requiresInputSourceChange(const InputSource& source)
{
    return m_source.handedness != source.handedness
        || m_source.targetRayMode != source.targetRayMode
        || m_source.profiles != source.profiles
        || static_cast<bool>(m_gripSpace) != source.gripOrigin.has_value();
}

void WebXRInputSource::disconnect()
{
    m_connected = false;
#if ENABLE(GAMEPAD)
    m_gamepad->setConnected(false);
#endif
}

void WebXRInputSource::pollEvents(Vector<Ref<XRInputSourceEvent>>& events)
{
    RefPtr session = m_session.get();
    if (!session)
        return;

    auto createEvent = [this, session](const AtomString& name) -> Ref<XRInputSourceEvent> {
        XRInputSourceEvent::Init init;
        init.frame = WebXRFrame::create(*session, WebXRFrame::IsAnimationFrame::No);
        init.inputSource = RefPtr { this };

        return XRInputSourceEvent::create(name, init);
    };

    if (!m_connected) {
        // A user agent MUST dispatch a selectend event on an XRSession when one of its XRInputSources ends 
        // when an XRInputSource that has begun a primary select action is disconnected.
        if (m_selectStarted) {
            if (targetRayMode() == PlatformXR::XRTargetRayMode::TransientPointer)
                events.append(createEvent(eventNames().selectEvent));
            events.append(createEvent(eventNames().selectendEvent));
        }

        // A user agent MUST dispatch a squeezeend event on an XRSession when one of its XRInputSources ends 
        // when an XRInputSource that has begun a primary squeeze action is disconnected.
        if (m_squeezeStarted)
            events.append(createEvent(eventNames().squeezeendEvent));

        return;
    }

    // https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping
    // Button indices are based on the xr-standard layout.
    bool selectStarted = !m_source.buttons.isEmpty() && m_source.buttons.first().pressed;
    bool squeezeStarted = m_source.buttons.size() > 1 && m_source.buttons[1].pressed;

    if (selectStarted != m_selectStarted) {
        if (selectStarted) {
            // A user agent MUST dispatch a selectstart event on an XRSession when one of its XRInputSources begins its primary action.
            events.append(createEvent(eventNames().selectstartEvent));
        } else {
            // A user agent MUST dispatch a selectend event on an XRSession when one of its XRInputSources ends its primary action.
            // A user agent MUST dispatch a select event on an XRSession when one of its XRInputSources has fully completed a primary action
            events.append(createEvent(eventNames().selectEvent));
            events.append(createEvent(eventNames().selectendEvent));
        }
        m_selectStarted = selectStarted;
    }

    if (squeezeStarted != m_squeezeStarted) {
        if (squeezeStarted) {
            // A user agent MUST dispatch a squeezestart event on an XRSession when one of its XRInputSources begins its primary squeeze action.
            events.append(createEvent(eventNames().squeezestartEvent));
        } else {
            // A user agent MUST dispatch a selectend event on an XRSession when one of its XRInputSources ends its primary squeeze action.
            // A user agent MUST dispatch a select event on an XRSession when one of its XRInputSources has fully completed a primary squeeze action.
            events.append(createEvent(eventNames().squeezeEvent));
            events.append(createEvent(eventNames().squeezeendEvent));
        }
        m_squeezeStarted = squeezeStarted;
    }
}

} // namespace WebCore

#endif // ENABLE(WEBXR)