2018-08-14 12:11:49 +00:00
|
|
|
/*
|
|
|
|
* noVNC: HTML5 VNC client
|
2019-06-24 11:26:25 +00:00
|
|
|
* Copyright (C) 2018 The noVNC Authors
|
2018-08-14 12:11:49 +00:00
|
|
|
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
|
|
|
|
*/
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
import * as Log from '../util/logging.js';
|
|
|
|
import { isTouchDevice } from '../util/browser.js';
|
|
|
|
import { setCapture, stopEvent, getPointerEvent } from '../util/events.js';
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
const WHEEL_STEP = 10; // Delta threshold for a mouse wheel step
|
|
|
|
const WHEEL_STEP_TIMEOUT = 50; // ms
|
|
|
|
const WHEEL_LINE_HEIGHT = 19;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export default class Mouse {
|
|
|
|
constructor(target) {
|
|
|
|
this._target = target || document;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
this._doubleClickTimer = null;
|
|
|
|
this._lastTouchPos = null;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
this._pos = null;
|
|
|
|
this._wheelStepXTimer = null;
|
|
|
|
this._wheelStepYTimer = null;
|
|
|
|
this._accumulatedWheelDeltaX = 0;
|
|
|
|
this._accumulatedWheelDeltaY = 0;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
this._eventHandlers = {
|
|
|
|
'mousedown': this._handleMouseDown.bind(this),
|
|
|
|
'mouseup': this._handleMouseUp.bind(this),
|
|
|
|
'mousemove': this._handleMouseMove.bind(this),
|
|
|
|
'mousewheel': this._handleMouseWheel.bind(this),
|
|
|
|
'mousedisable': this._handleMouseDisable.bind(this)
|
|
|
|
};
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
// ===== PROPERTIES =====
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
this.touchButton = 1; // Button mask (1, 2, 4) for touch devices (0 means ignore clicks)
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
// ===== EVENT HANDLERS =====
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
this.onmousebutton = () => {}; // Handler for mouse button click/release
|
|
|
|
this.onmousemove = () => {}; // Handler for mouse movement
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// ===== PRIVATE METHODS =====
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_resetDoubleClickTimer() {
|
2018-08-14 12:11:49 +00:00
|
|
|
this._doubleClickTimer = null;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_handleMouseButton(e, down) {
|
2018-08-14 12:11:49 +00:00
|
|
|
this._updateMousePosition(e);
|
2019-06-24 11:26:25 +00:00
|
|
|
let pos = this._pos;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
let bmask;
|
2018-08-14 12:11:49 +00:00
|
|
|
if (e.touches || e.changedTouches) {
|
|
|
|
// Touch device
|
|
|
|
|
|
|
|
// When two touches occur within 500 ms of each other and are
|
|
|
|
// close enough together a double click is triggered.
|
|
|
|
if (down == 1) {
|
|
|
|
if (this._doubleClickTimer === null) {
|
|
|
|
this._lastTouchPos = pos;
|
|
|
|
} else {
|
|
|
|
clearTimeout(this._doubleClickTimer);
|
|
|
|
|
|
|
|
// When the distance between the two touches is small enough
|
|
|
|
// force the position of the latter touch to the position of
|
|
|
|
// the first.
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
const xs = this._lastTouchPos.x - pos.x;
|
|
|
|
const ys = this._lastTouchPos.y - pos.y;
|
|
|
|
const d = Math.sqrt((xs * xs) + (ys * ys));
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// The goal is to trigger on a certain physical width, the
|
|
|
|
// devicePixelRatio brings us a bit closer but is not optimal.
|
2019-06-24 11:26:25 +00:00
|
|
|
const threshold = 20 * (window.devicePixelRatio || 1);
|
2018-08-14 12:11:49 +00:00
|
|
|
if (d < threshold) {
|
|
|
|
pos = this._lastTouchPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._doubleClickTimer = setTimeout(this._resetDoubleClickTimer.bind(this), 500);
|
|
|
|
}
|
|
|
|
bmask = this.touchButton;
|
|
|
|
// If bmask is set
|
|
|
|
} else if (e.which) {
|
|
|
|
/* everything except IE */
|
|
|
|
bmask = 1 << e.button;
|
|
|
|
} else {
|
|
|
|
/* IE including 9 */
|
2018-08-27 12:30:50 +00:00
|
|
|
bmask = (e.button & 0x1) + // Left
|
|
|
|
(e.button & 0x2) * 2 + // Right
|
|
|
|
(e.button & 0x4) / 2; // Middle
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
Log.Debug("onmousebutton " + (down ? "down" : "up") +
|
|
|
|
", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask);
|
2018-08-14 12:11:49 +00:00
|
|
|
this.onmousebutton(pos.x, pos.y, down, bmask);
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
stopEvent(e);
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_handleMouseDown(e) {
|
2018-08-14 12:11:49 +00:00
|
|
|
// Touch events have implicit capture
|
|
|
|
if (e.type === "mousedown") {
|
2018-08-27 12:30:50 +00:00
|
|
|
setCapture(this._target);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._handleMouseButton(e, 1);
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_handleMouseUp(e) {
|
2018-08-14 12:11:49 +00:00
|
|
|
this._handleMouseButton(e, 0);
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Mouse wheel events are sent in steps over VNC. This means that the VNC
|
|
|
|
// protocol can't handle a wheel event with specific distance or speed.
|
|
|
|
// Therefor, if we get a lot of small mouse wheel events we combine them.
|
2019-06-24 11:26:25 +00:00
|
|
|
_generateWheelStepX() {
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
if (this._accumulatedWheelDeltaX < 0) {
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 5);
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 5);
|
|
|
|
} else if (this._accumulatedWheelDeltaX > 0) {
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 6);
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._accumulatedWheelDeltaX = 0;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_generateWheelStepY() {
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
if (this._accumulatedWheelDeltaY < 0) {
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 3);
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 3);
|
|
|
|
} else if (this._accumulatedWheelDeltaY > 0) {
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 4);
|
|
|
|
this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._accumulatedWheelDeltaY = 0;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_resetWheelStepTimers() {
|
2018-08-14 12:11:49 +00:00
|
|
|
window.clearTimeout(this._wheelStepXTimer);
|
|
|
|
window.clearTimeout(this._wheelStepYTimer);
|
|
|
|
this._wheelStepXTimer = null;
|
|
|
|
this._wheelStepYTimer = null;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_handleMouseWheel(e) {
|
2018-08-14 12:11:49 +00:00
|
|
|
this._resetWheelStepTimers();
|
|
|
|
|
|
|
|
this._updateMousePosition(e);
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
let dX = e.deltaX;
|
|
|
|
let dY = e.deltaY;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Pixel units unless it's non-zero.
|
|
|
|
// Note that if deltamode is line or page won't matter since we aren't
|
|
|
|
// sending the mouse wheel delta to the server anyway.
|
|
|
|
// The difference between pixel and line can be important however since
|
|
|
|
// we have a threshold that can be smaller than the line height.
|
|
|
|
if (e.deltaMode !== 0) {
|
|
|
|
dX *= WHEEL_LINE_HEIGHT;
|
|
|
|
dY *= WHEEL_LINE_HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._accumulatedWheelDeltaX += dX;
|
|
|
|
this._accumulatedWheelDeltaY += dY;
|
|
|
|
|
|
|
|
// Generate a mouse wheel step event when the accumulated delta
|
|
|
|
// for one of the axes is large enough.
|
|
|
|
// Small delta events that do not pass the threshold get sent
|
|
|
|
// after a timeout.
|
|
|
|
if (Math.abs(this._accumulatedWheelDeltaX) > WHEEL_STEP) {
|
|
|
|
this._generateWheelStepX();
|
|
|
|
} else {
|
2018-08-27 12:30:50 +00:00
|
|
|
this._wheelStepXTimer =
|
|
|
|
window.setTimeout(this._generateWheelStepX.bind(this),
|
|
|
|
WHEEL_STEP_TIMEOUT);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
if (Math.abs(this._accumulatedWheelDeltaY) > WHEEL_STEP) {
|
|
|
|
this._generateWheelStepY();
|
|
|
|
} else {
|
2018-08-27 12:30:50 +00:00
|
|
|
this._wheelStepYTimer =
|
|
|
|
window.setTimeout(this._generateWheelStepY.bind(this),
|
|
|
|
WHEEL_STEP_TIMEOUT);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
stopEvent(e);
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_handleMouseMove(e) {
|
2018-08-14 12:11:49 +00:00
|
|
|
this._updateMousePosition(e);
|
|
|
|
this.onmousemove(this._pos.x, this._pos.y);
|
2018-08-27 12:30:50 +00:00
|
|
|
stopEvent(e);
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
_handleMouseDisable(e) {
|
2018-08-14 12:11:49 +00:00
|
|
|
/*
|
|
|
|
* Stop propagation if inside canvas area
|
|
|
|
* Note: This is only needed for the 'click' event as it fails
|
|
|
|
* to fire properly for the target element so we have
|
|
|
|
* to listen on the document element instead.
|
|
|
|
*/
|
|
|
|
if (e.target == this._target) {
|
2018-08-27 12:30:50 +00:00
|
|
|
stopEvent(e);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Update coordinates relative to target
|
2019-06-24 11:26:25 +00:00
|
|
|
_updateMousePosition(e) {
|
2018-08-27 12:30:50 +00:00
|
|
|
e = getPointerEvent(e);
|
2019-06-24 11:26:25 +00:00
|
|
|
const bounds = this._target.getBoundingClientRect();
|
|
|
|
let x;
|
|
|
|
let y;
|
2018-08-14 12:11:49 +00:00
|
|
|
// Clip to target bounds
|
|
|
|
if (e.clientX < bounds.left) {
|
|
|
|
x = 0;
|
|
|
|
} else if (e.clientX >= bounds.right) {
|
|
|
|
x = bounds.width - 1;
|
|
|
|
} else {
|
|
|
|
x = e.clientX - bounds.left;
|
|
|
|
}
|
|
|
|
if (e.clientY < bounds.top) {
|
|
|
|
y = 0;
|
|
|
|
} else if (e.clientY >= bounds.bottom) {
|
|
|
|
y = bounds.height - 1;
|
|
|
|
} else {
|
|
|
|
y = e.clientY - bounds.top;
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
this._pos = {x: x, y: y};
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// ===== PUBLIC METHODS =====
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
grab() {
|
2018-08-27 12:30:50 +00:00
|
|
|
if (isTouchDevice) {
|
2019-06-24 11:26:25 +00:00
|
|
|
this._target.addEventListener('touchstart', this._eventHandlers.mousedown);
|
|
|
|
this._target.addEventListener('touchend', this._eventHandlers.mouseup);
|
|
|
|
this._target.addEventListener('touchmove', this._eventHandlers.mousemove);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
this._target.addEventListener('mousedown', this._eventHandlers.mousedown);
|
|
|
|
this._target.addEventListener('mouseup', this._eventHandlers.mouseup);
|
|
|
|
this._target.addEventListener('mousemove', this._eventHandlers.mousemove);
|
|
|
|
this._target.addEventListener('wheel', this._eventHandlers.mousewheel);
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
/* Prevent middle-click pasting (see above for why we bind to document) */
|
|
|
|
document.addEventListener('click', this._eventHandlers.mousedisable);
|
|
|
|
|
|
|
|
/* preventDefault() on mousedown doesn't stop this event for some
|
|
|
|
reason so we have to explicitly block it */
|
2019-06-24 11:26:25 +00:00
|
|
|
this._target.addEventListener('contextmenu', this._eventHandlers.mousedisable);
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
ungrab() {
|
2018-08-14 12:11:49 +00:00
|
|
|
this._resetWheelStepTimers();
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
if (isTouchDevice) {
|
2019-06-24 11:26:25 +00:00
|
|
|
this._target.removeEventListener('touchstart', this._eventHandlers.mousedown);
|
|
|
|
this._target.removeEventListener('touchend', this._eventHandlers.mouseup);
|
|
|
|
this._target.removeEventListener('touchmove', this._eventHandlers.mousemove);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
this._target.removeEventListener('mousedown', this._eventHandlers.mousedown);
|
|
|
|
this._target.removeEventListener('mouseup', this._eventHandlers.mouseup);
|
|
|
|
this._target.removeEventListener('mousemove', this._eventHandlers.mousemove);
|
|
|
|
this._target.removeEventListener('wheel', this._eventHandlers.mousewheel);
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
document.removeEventListener('click', this._eventHandlers.mousedisable);
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
this._target.removeEventListener('contextmenu', this._eventHandlers.mousedisable);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|