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

Support grabbing the pointer with the Pointer Lock API #1520

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
78 changes: 78 additions & 0 deletions app/images/pointer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ const UI = {
document.getElementById("noVNC_view_drag_button")
.addEventListener('click', UI.toggleViewDrag);

document
.getElementById("noVNC_pointer_lock_button")
.addEventListener("click", UI.requestPointerLock);

document.getElementById("noVNC_control_bar_handle")
.addEventListener('mousedown', UI.controlbarHandleMouseDown);
document.getElementById("noVNC_control_bar_handle")
Expand Down Expand Up @@ -441,6 +445,7 @@ const UI = {
UI.updatePowerButton();
UI.keepControlbar();
}
UI.updatePointerLockButton();

// State change closes dialogs as they may not be relevant
// anymore
Expand Down Expand Up @@ -1036,6 +1041,7 @@ const UI = {
UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
UI.rfb.addEventListener("bell", UI.bell);
UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
UI.rfb.addEventListener("inputlock", UI.inputLockChanged);
UI.rfb.clipViewport = UI.getSetting('view_clip');
UI.rfb.scaleViewport = UI.getSetting('resize') === 'scale';
UI.rfb.resizeSession = UI.getSetting('resize') === 'remote';
Expand Down Expand Up @@ -1245,6 +1251,7 @@ const UI = {
document.getElementById('noVNC_fullscreen_button')
.classList.remove("noVNC_selected");
}
UI.updatePointerLockButton();
},

/* ------^-------
Expand Down Expand Up @@ -1297,6 +1304,38 @@ const UI = {
/* ------^-------
* /VIEW CLIPPING
* ==============
* POINTER LOCK
* ------v------*/

updatePointerLockButton() {
// Only show the button if the pointer lock API is properly supported
// AND in fullscreen.
if (
UI.connected &&
(document.fullscreenElement || // alternative standard method
document.mozFullScreenElement || // currently working methods
document.webkitFullscreenElement ||
document.msFullscreenElement) &&
(document.pointerLockElement !== undefined ||
document.mozPointerLockElement !== undefined)
) {
document
.getElementById("noVNC_pointer_lock_button")
.classList.remove("noVNC_hidden");
} else {
document
.getElementById("noVNC_pointer_lock_button")
.classList.add("noVNC_hidden");
}
},

requestPointerLock() {
UI.rfb.requestInputLock({ pointer: true });
},

/* ------^-------
* /POINTER LOCK
* ==============
* VIEWDRAG
* ------v------*/

Expand Down Expand Up @@ -1662,6 +1701,18 @@ const UI = {
document.title = e.detail.name + " - " + PAGE_TITLE;
},

inputLockChanged(e) {
if (e.detail.pointer) {
document
.getElementById("noVNC_pointer_lock_button")
.classList.add("noVNC_selected");
} else {
document
.getElementById("noVNC_pointer_lock_button")
.classList.remove("noVNC_selected");
}
},

bell(e) {
if (WebUtil.getConfigVar('bell', 'on') === 'on') {
const promise = document.getElementById('noVNC_bell').play();
Expand Down
1 change: 1 addition & 0 deletions core/encodings.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const encodings = {
pseudoEncodingCompressLevel9: -247,
pseudoEncodingCompressLevel0: -256,
pseudoEncodingVMwareCursor: 0x574d5664,
pseudoEncodingVMwareCursorPosition: 0x574d5666,
pseudoEncodingExtendedClipboard: 0xc0a1e5ce
};

Expand Down
97 changes: 96 additions & 1 deletion core/rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export default class RFB extends EventTargetMixin {
this._mousePos = {};
this._mouseButtonMask = 0;
this._mouseLastMoveTime = 0;
this._pointerLock = false;
this._viewportDragging = false;
this._viewportDragPos = {};
this._viewportHasMoved = false;
Expand All @@ -168,6 +169,8 @@ export default class RFB extends EventTargetMixin {
focusCanvas: this._focusCanvas.bind(this),
windowResize: this._windowResize.bind(this),
handleMouse: this._handleMouse.bind(this),
handlePointerLockChange: this._handlePointerLockChange.bind(this),
handlePointerLockError: this._handlePointerLockError.bind(this),
handleWheel: this._handleWheel.bind(this),
handleGesture: this._handleGesture.bind(this),
};
Expand Down Expand Up @@ -477,6 +480,24 @@ export default class RFB extends EventTargetMixin {
this._canvas.blur();
}

requestInputLock(locks) {
if (locks.pointer) {
if (this._canvas.requestPointerLock) {
this._canvas.requestPointerLock();
return;
}
if (this._canvas.mozRequestPointerLock) {
this._canvas.mozRequestPointerLock();
return;
}
}
// If we were not able to request any lock, still let the user know
// about the result.
this.dispatchEvent(new CustomEvent(
"inputlock",
{ detail: { pointer: this._pointerLock }, }));
}

clipboardPasteFrom(text) {
if (this._rfbConnectionState !== 'connected' || this._viewOnly) { return; }

Expand Down Expand Up @@ -539,6 +560,14 @@ export default class RFB extends EventTargetMixin {
// preventDefault() on mousedown doesn't stop this event for some
// reason so we have to explicitly block it
this._canvas.addEventListener('contextmenu', this._eventHandlers.handleMouse);
// Pointer Lock listeners need to be installed in document instead of the canvas.
if (document.onpointerlockchange !== undefined) {
document.addEventListener('pointerlockchange', this._eventHandlers.handlePointerLockChange, false);
document.addEventListener('pointerlockerror', this._eventHandlers.handlePointerLockError, false);
} else if (document.onmozpointerlockchange !== undefined) {
document.addEventListener('mozpointerlockchange', this._eventHandlers.handlePointerLockChange, false);
document.addEventListener('mozpointerlockerror', this._eventHandlers.handlePointerLockError, false);
}

// Wheel events
this._canvas.addEventListener("wheel", this._eventHandlers.handleWheel);
Expand All @@ -563,6 +592,13 @@ export default class RFB extends EventTargetMixin {
this._canvas.removeEventListener('mousemove', this._eventHandlers.handleMouse);
this._canvas.removeEventListener('click', this._eventHandlers.handleMouse);
this._canvas.removeEventListener('contextmenu', this._eventHandlers.handleMouse);
if (document.onpointerlockchange !== undefined) {
document.removeEventListener('pointerlockchange', this._eventHandlers.handlePointerLockChange);
document.removeEventListener('pointerlockerror', this._eventHandlers.handlePointerLockError);
} else if (document.onmozpointerlockchange !== undefined) {
document.removeEventListener('mozpointerlockchange', this._eventHandlers.handlePointerLockChange);
document.removeEventListener('mozpointerlockerror', this._eventHandlers.handlePointerLockError);
}
this._canvas.removeEventListener("mousedown", this._eventHandlers.focusCanvas);
this._canvas.removeEventListener("touchstart", this._eventHandlers.focusCanvas);
window.removeEventListener('resize', this._eventHandlers.windowResize);
Expand Down Expand Up @@ -885,8 +921,27 @@ export default class RFB extends EventTargetMixin {
return;
}

let pos = clientToElement(ev.clientX, ev.clientY,
let pos;
if (this._pointerLock) {
pos = {
x: this._mousePos.x + ev.movementX,
y: this._mousePos.y + ev.movementY,
};
if (pos.x < 0) {
pos.x = 0;
} else if (pos.x > this._fbWidth) {
pos.x = this._fbWidth;
}
if (pos.y < 0) {
pos.y = 0;
} else if (pos.y > this._fbHeight) {
pos.y = this._fbHeight;
}
this._cursor.move(pos.x, pos.y);
} else {
pos = clientToElement(ev.clientX, ev.clientY,
this._canvas);
}

switch (ev.type) {
case 'mousedown':
Expand Down Expand Up @@ -987,6 +1042,28 @@ export default class RFB extends EventTargetMixin {
this._mouseLastMoveTime = Date.now();
}

_handlePointerLockChange() {
if (
document.pointerLockElement === this._canvas ||
document.mozPointerLockElement === this._canvas
) {
this._pointerLock = true;
this._cursor.setEmulateCursor(true);
} else {
this._pointerLock = false;
this._cursor.setEmulateCursor(false);
}
this.dispatchEvent(new CustomEvent(
"inputlock",
{ detail: { pointer: this._pointerLock }, }));
}

_handlePointerLockError() {
this.dispatchEvent(new CustomEvent(
"inputlock",
{ detail: { pointer: this._pointerLock }, }));
}

_sendMouse(x, y, mask) {
if (this._rfbConnectionState !== 'connected') { return; }
if (this._viewOnly) { return; } // View only, skip mouse events
Expand Down Expand Up @@ -1767,6 +1844,8 @@ export default class RFB extends EventTargetMixin {
encs.push(encodings.pseudoEncodingCursor);
}

encs.push(encodings.pseudoEncodingVMwareCursorPosition);

RFB.messages.clientEncodings(this._sock, encs);
}

Expand Down Expand Up @@ -2165,6 +2244,9 @@ export default class RFB extends EventTargetMixin {
case encodings.pseudoEncodingVMwareCursor:
return this._handleVMwareCursor();

case encodings.pseudoEncodingVMwareCursorPosition:
return this._handleVMwareCursorPosition();

case encodings.pseudoEncodingCursor:
return this._handleCursor();

Expand Down Expand Up @@ -2303,6 +2385,19 @@ export default class RFB extends EventTargetMixin {
return true;
}

_handleVMwareCursorPosition() {
const x = this._FBU.x;
const y = this._FBU.y;

if (this._pointerLock) {
// Only attempt to match the server's pointer position if we are in
// pointer lock mode.
this._mousePos = { x: x, y: y };
}

return true;
}

_handleCursor() {
const hotx = this._FBU.x; // hotspot-x
const hoty = this._FBU.y; // hotspot-y
Expand Down
Loading