Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect XR usage #1379

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/DOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,13 @@ class HTMLIFrameElement extends HTMLSrcableElement {
onrequest(req) {
parentPort.postMessage(req);
},
onxrmode(event) {
this.dispatchEvent(new CustomEvent('xrmode', {
detail: {
xr: event.xr,
},
}));
},
onhapticpulse(event) {
parentPort.postMessage({
method: 'emit',
Expand Down
31 changes: 24 additions & 7 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ function initDocument (document, window) {
}

process.nextTick(async () => {
const _tryDispatchEvent = (target, event) => {
try {
target.dispatchEvent(event);
} catch(err) {
console.warn(err);
}
};

if (body) {
const bodyChildNodes = body.childNodes;
body.childNodes = new window.NodeList();
Expand All @@ -173,7 +181,11 @@ function initDocument (document, window) {
}

body.childNodes = bodyChildNodes;
body._emit('children', Array.from(bodyChildNodes), [], null, null);
try {
body._emit('children', Array.from(bodyChildNodes), [], null, null);
} catch(err) {
console.warn(err);
}

try {
await GlobalContext._runHtml(document.body, window);
Expand All @@ -182,9 +194,9 @@ function initDocument (document, window) {
}

document.readyState = 'interactive';
document.dispatchEvent(new Event('readystatechange', {target: document}));
_tryDispatchEvent(document, new Event('readystatechange', {target: document}));

document.dispatchEvent(new Event('DOMContentLoaded', {
_tryDispatchEvent(document, new Event('DOMContentLoaded', {
target: document,
bubbles: true,
}));
Expand All @@ -196,19 +208,24 @@ function initDocument (document, window) {
}

document.readyState = 'interactive';
document.dispatchEvent(new Event('readystatechange', {target: document}));
_tryDispatchEvent(document, new Event('readystatechange', {target: document}));

document.dispatchEvent(new Event('DOMContentLoaded', {
_tryDispatchEvent(document, new Event('DOMContentLoaded', {
target: document,
bubbles: true,
}));
}

document.readyState = 'complete';
document.dispatchEvent(new Event('readystatechange', {target: document}));
_tryDispatchEvent(document, new Event('readystatechange', {target: document}));

document.dispatchEvent(new Event('load', {target: document}));
window.dispatchEvent(new Event('load', {target: window}));
_tryDispatchEvent(window, new Event('load', {target: window}));

parentPort.postMessage({
method: 'xrMode',
xr: GlobalContext.requestedXr,
});
});

return document;
Expand Down
10 changes: 9 additions & 1 deletion src/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ GlobalContext.id = id;
GlobalContext.args = args;
GlobalContext.version = version;
GlobalContext.baseUrl = options.baseUrl;
GlobalContext.requestedXr = false;

const {_parseDocument, _parseDocumentAst, getBoundDocumentElements, DocumentType, DOMImplementation, initDocument} = require('./Document');
const {
Expand Down Expand Up @@ -824,7 +825,12 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => {
return _parseDocumentAst(htmlAst, window, false);
}
};
window.addEventListener = EventTarget.prototype.addEventListener.bind(window);
window.addEventListener = function(e, fn, opts) {
if (e === 'vrdisplayactivate') {
GlobalContext.requestedXr = true;
}
return EventTarget.prototype.addEventListener.apply(window, arguments);
};
window.removeEventListener = EventTarget.prototype.removeEventListener.bind(window);
window.dispatchEvent = EventTarget.prototype.dispatchEvent.bind(window);
window.Image = HTMLImageElement;
Expand Down Expand Up @@ -1193,6 +1199,8 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => {

const _makeMrDisplays = () => {
const _onrequestpresent = async () => {
GlobalContext.requestedXr = true;

// if (!GlobalContext.xrState.isPresenting[0]) {
await new Promise((accept, reject) => {
vrPresentState.responseAccepts.push(accept);
Expand Down
3 changes: 3 additions & 0 deletions src/WindowVm.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ const _makeWindow = (options = {}, handlers = {}) => {
window.on('framebuffer', e => {
window.framebuffer = e;
});
window.on('xrMode', e => {
options.onxrmode && options.onxrmode(e);
});
window.on('hapticPulse', e => {
options.onhapticpulse && options.onhapticpulse(e);
});
Expand Down