-/*
- * Guacamole - Clientless Remote Desktop
- * Copyright (C) 2010 Michael Jumper
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
*
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * The Original Code is guacamole-common-js.
+ *
+ * The Initial Developer of the Original Code is
+ * Michael Jumper.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+// Guacamole namespace
+var Guacamole = Guacamole || {};
+
+/**
+ * Provides cross-browser mouse events for a given element. The events of
+ * the given element are automatically populated with handlers that translate
+ * mouse events into a non-browser-specific event provided by the
+ * Guacamole.Mouse instance.
+ *
+ * Touch events are translated into mouse events as if the touches occurred
+ * on a touchpad (drag to push the mouse pointer, tap to click).
+ *
+ * @constructor
+ * @param {Element} element The Element to use to provide mouse events.
*/
+Guacamole.Mouse = function(element) {
+
+ /**
+ * Reference to this Guacamole.Mouse.
+ * @private
+ */
+ var guac_mouse = this;
+
+ /**
+ * The distance a two-finger touch must move per scrollwheel event, in
+ * pixels.
+ */
+ this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
+
+ /**
+ * The maximum number of milliseconds to wait for a touch to end for the
+ * gesture to be considered a click.
+ */
+ this.clickTimingThreshold = 250;
+
+ /**
+ * The maximum number of pixels to allow a touch to move for the gesture to
+ * be considered a click.
+ */
+ this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
+
+ /**
+ * The current mouse state. The properties of this state are updated when
+ * mouse events fire. This state object is also passed in as a parameter to
+ * the handler of any mouse events.
+ *
+ * @type Guacamole.Mouse.State
+ */
+ this.currentState = new Guacamole.Mouse.State(
+ 0, 0,
+ false, false, false, false, false
+ );
+
+ /**
+ * Fired whenever the user presses a mouse button down over the element
+ * associated with this Guacamole.Mouse.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmousedown = null;
+
+ /**
+ * Fired whenever the user releases a mouse button down over the element
+ * associated with this Guacamole.Mouse.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmouseup = null;
+
+ /**
+ * Fired whenever the user moves the mouse over the element associated with
+ * this Guacamole.Mouse.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmousemove = null;
+
+ function cancelEvent(e) {
+ e.stopPropagation();
+ if (e.preventDefault) e.preventDefault();
+ e.returnValue = false;
+ }
+ function moveMouse(clientX, clientY) {
-function GuacamoleMouse(element) {
+ guac_mouse.currentState.x = clientX - element.offsetLeft;
+ guac_mouse.currentState.y = clientY - element.offsetTop;
- /*****************************************/
- /*** Mouse Handler ***/
- /*****************************************/
+ // This is all JUST so we can get the mouse position within the element
+ var parent = element.offsetParent;
+ while (parent) {
+ guac_mouse.currentState.x -= parent.offsetLeft - parent.scrollLeft;
+ guac_mouse.currentState.y -= parent.offsetTop - parent.scrollTop;
- var mouseIndex = 0;
+ parent = parent.offsetParent;
+ }
- var mouseLeftButton = 0;
- var mouseMiddleButton = 0;
- var mouseRightButton = 0;
+ if (guac_mouse.onmousemove)
+ guac_mouse.onmousemove(guac_mouse.currentState);
- var mouseX = 0;
- var mouseY = 0;
+ }
- var absoluteMouseX = 0;
- var absoluteMouseY = 0;
+ // Block context menu so right-click gets sent properly
+ element.addEventListener("contextmenu", function(e) {
+ cancelEvent(e);
+ }, false);
- function getMouseState(up, down) {
- var mouseState = new MouseEvent(mouseX, mouseY,
- mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
+ element.addEventListener("mousemove", function(e) {
- return mouseState;
- }
+ // Don't handle if we aren't supposed to
+ if (gesture_in_progress) return;
+ cancelEvent(e);
- function moveMouse(pageX, pageY) {
+ moveMouse(e.clientX, e.clientY);
- absoluteMouseX = pageX;
- absoluteMouseY = pageY;
+ }, false);
- mouseX = absoluteMouseX - element.offsetLeft;
- mouseY = absoluteMouseY - element.offsetTop;
+ var touch_count = 0;
+ var last_touch_x = 0;
+ var last_touch_y = 0;
+ var last_touch_time = 0;
+ var pixels_moved = 0;
- // This is all JUST so we can get the mouse position within the element
- var parent = element.offsetParent;
- while (parent) {
- if (parent.offsetLeft && parent.offsetTop) {
- mouseX -= parent.offsetLeft;
- mouseY -= parent.offsetTop;
- }
- parent = parent.offsetParent;
- }
+ var touch_buttons = {
+ 1: "left",
+ 2: "right",
+ 3: "middle"
+ };
- movementHandler(getMouseState(0, 0));
+ var gesture_in_progress = false;
+ var click_release_timeout = null;
- }
+ element.addEventListener("touchend", function(e) {
+
+ cancelEvent(e);
+
+ // If we're handling a gesture AND this is the last touch
+ if (gesture_in_progress && e.touches.length == 0) {
+
+ var time = new Date().getTime();
+ // Get corresponding mouse button
+ var button = touch_buttons[touch_count];
- // Block context menu so right-click gets sent properly
- element.oncontextmenu = function(e) {return false;};
+ // If mouse already down, release anad clear timeout
+ if (guac_mouse.currentState[button]) {
- element.onmousemove = function(e) {
+ // Fire button up event
+ guac_mouse.currentState[button] = false;
+ if (guac_mouse.onmouseup)
+ guac_mouse.onmouseup(guac_mouse.currentState);
- e.stopPropagation();
+ // Clear timeout, if set
+ if (click_release_timeout) {
+ window.clearTimeout(click_release_timeout);
+ click_release_timeout = null;
+ }
- moveMouse(e.pageX, e.pageY);
+ }
- };
+ // If single tap detected (based on time and distance)
+ if (time - last_touch_time <= guac_mouse.clickTimingThreshold
+ && pixels_moved < guac_mouse.clickMoveThreshold) {
+
+ // Fire button down event
+ guac_mouse.currentState[button] = true;
+ if (guac_mouse.onmousedown)
+ guac_mouse.onmousedown(guac_mouse.currentState);
+
+ // Delay mouse up - mouse up should be canceled if
+ // touchstart within timeout.
+ click_release_timeout = window.setTimeout(function() {
+
+ // Fire button up event
+ guac_mouse.currentState[button] = false;
+ if (guac_mouse.onmouseup)
+ guac_mouse.onmouseup(guac_mouse.currentState);
+
+ // Gesture now over
+ gesture_in_progress = false;
+
+ }, guac_mouse.clickTimingThreshold);
- element.ontouchend = function(e) {
-
- e.stopPropagation();
- e.preventDefault();
+ }
- // Release all buttons (FIXME: for now...)
- if (mouseLeftButton || mouseMiddleButton || mouseRightButton) {
- mouseLeftButton = 0;
- mouseMiddleButton = 0;
- mouseRightButton = 0;
+ // If we're not waiting to see if this is a click, stop gesture
+ if (!click_release_timeout)
+ gesture_in_progress = false;
- buttonReleasedHandler(getMouseState(0, 0));
}
- }
+ }, false);
- element.ontouchstart = function(e) {
+ element.addEventListener("touchstart", function(e) {
- e.stopPropagation();
- e.preventDefault();
+ cancelEvent(e);
- if (e.touches.length == 1)
- element.ontouchmove(e);
+ // Track number of touches, but no more than three
+ touch_count = Math.min(e.touches.length, 3);
- else {
-
- var button = e.touches[0];
- var pointer = e.touches[1];
+ // Clear timeout, if set
+ if (click_release_timeout) {
+ window.clearTimeout(click_release_timeout);
+ click_release_timeout = null;
+ }
+
+ // Record initial touch location and time for touch movement
+ // and tap gestures
+ if (!gesture_in_progress) {
+
+ // Stop mouse events while touching
+ gesture_in_progress = true;
- if (pointer.pageX < button.pageX)
- mouseLeftButton = 1;
- else
- mouseRightButton = 1;
+ // Record touch location and time
+ var starting_touch = e.touches[0];
+ last_touch_x = starting_touch.clientX;
+ last_touch_y = starting_touch.clientY;
+ last_touch_time = new Date().getTime();
+ pixels_moved = 0;
- buttonPressedHandler(getMouseState(0, 0));
}
- };
+ }, false);
- element.ontouchmove = function(e) {
+ element.addEventListener("touchmove", function(e) {
- e.stopPropagation();
- e.preventDefault();
+ cancelEvent(e);
+ // Get change in touch location
var touch = e.touches[0];
- moveMouse(touch.pageX, touch.pageY);
+ var delta_x = touch.clientX - last_touch_x;
+ var delta_y = touch.clientY - last_touch_y;
- };
+ // Track pixels moved
+ pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
+ // If only one touch involved, this is mouse move
+ if (touch_count == 1) {
- element.onmousedown = function(e) {
+ // Calculate average velocity in Manhatten pixels per millisecond
+ var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
- e.stopPropagation();
+ // Scale mouse movement relative to velocity
+ var scale = 1 + velocity;
+
+ // Update mouse location
+ guac_mouse.currentState.x += delta_x*scale;
+ guac_mouse.currentState.y += delta_y*scale;
+
+ // Prevent mouse from leaving screen
+
+ if (guac_mouse.currentState.x < 0)
+ guac_mouse.currentState.x = 0;
+ else if (guac_mouse.currentState.x >= element.offsetWidth)
+ guac_mouse.currentState.x = element.offsetWidth - 1;
+
+ if (guac_mouse.currentState.y < 0)
+ guac_mouse.currentState.y = 0;
+ else if (guac_mouse.currentState.y >= element.offsetHeight)
+ guac_mouse.currentState.y = element.offsetHeight - 1;
+
+ // Fire movement event, if defined
+ if (guac_mouse.onmousemove)
+ guac_mouse.onmousemove(guac_mouse.currentState);
+
+ // Update touch location
+ last_touch_x = touch.clientX;
+ last_touch_y = touch.clientY;
+
+ }
+
+ // Interpret two-finger swipe as scrollwheel
+ else if (touch_count == 2) {
+
+ // If change in location passes threshold for scroll
+ if (Math.abs(delta_y) >= guac_mouse.scrollThreshold) {
+
+ // Decide button based on Y movement direction
+ var button;
+ if (delta_y > 0) button = "down";
+ else button = "up";
+
+ // Fire button down event
+ guac_mouse.currentState[button] = true;
+ if (guac_mouse.onmousedown)
+ guac_mouse.onmousedown(guac_mouse.currentState);
+
+ // Fire button up event
+ guac_mouse.currentState[button] = false;
+ if (guac_mouse.onmouseup)
+ guac_mouse.onmouseup(guac_mouse.currentState);
+
+ // Only update touch location after a scroll has been
+ // detected
+ last_touch_x = touch.clientX;
+ last_touch_y = touch.clientY;
+
+ }
+
+ }
+
+ }, false);
+
+
+ element.addEventListener("mousedown", function(e) {
+
+ // Don't handle if we aren't supposed to
+ if (gesture_in_progress) return;
+
+ cancelEvent(e);
switch (e.button) {
case 0:
- mouseLeftButton = 1;
+ guac_mouse.currentState.left = true;
break;
case 1:
- mouseMiddleButton = 1;
+ guac_mouse.currentState.middle = true;
break;
case 2:
- mouseRightButton = 1;
+ guac_mouse.currentState.right = true;
break;
}
- buttonPressedHandler(getMouseState(0, 0));
- };
+ if (guac_mouse.onmousedown)
+ guac_mouse.onmousedown(guac_mouse.currentState);
+ }, false);
- element.onmouseup = function(e) {
- e.stopPropagation();
+ element.addEventListener("mouseup", function(e) {
+
+ // Don't handle if we aren't supposed to
+ if (gesture_in_progress) return;
+
+ cancelEvent(e);
switch (e.button) {
case 0:
- mouseLeftButton = 0;
+ guac_mouse.currentState.left = false;
break;
case 1:
- mouseMiddleButton = 0;
+ guac_mouse.currentState.middle = false;
break;
case 2:
- mouseRightButton = 0;
+ guac_mouse.currentState.right = false;
break;
}
- buttonReleasedHandler(getMouseState(0, 0));
- };
+ if (guac_mouse.onmouseup)
+ guac_mouse.onmouseup(guac_mouse.currentState);
- element.onmouseout = function(e) {
+ }, false);
- e.stopPropagation();
+ element.addEventListener("mouseout", function(e) {
+
+ // Don't handle if we aren't supposed to
+ if (gesture_in_progress) return;
+
+ // Get parent of the element the mouse pointer is leaving
+ if (!e) e = window.event;
+
+ // Check that mouseout is due to actually LEAVING the element
+ var target = e.relatedTarget || e.toElement;
+ while (target != null) {
+ if (target === element)
+ return;
+ target = target.parentNode;
+ }
+
+ cancelEvent(e);
// Release all buttons
- if (mouseLeftButton || mouseMiddleButton || mouseRightButton) {
- mouseLeftButton = 0;
- mouseMiddleButton = 0;
- mouseRightButton = 0;
+ if (guac_mouse.currentState.left
+ || guac_mouse.currentState.middle
+ || guac_mouse.currentState.right) {
- buttonReleasedHandler(getMouseState(0, 0));
+ guac_mouse.currentState.left = false;
+ guac_mouse.currentState.middle = false;
+ guac_mouse.currentState.right = false;
+
+ if (guac_mouse.onmouseup)
+ guac_mouse.onmouseup(guac_mouse.currentState);
}
- };
+ }, false);
// Override selection on mouse event element.
- element.onselectstart = function() {
- return false;
- };
+ element.addEventListener("selectstart", function(e) {
+ cancelEvent(e);
+ }, false);
// Scroll wheel support
- function handleScroll(e) {
+ function mousewheel_handler(e) {
+
+ // Don't handle if we aren't supposed to
+ if (gesture_in_progress) return;
var delta = 0;
if (e.detail)
// Up
if (delta < 0) {
- buttonPressedHandler(getMouseState(1, 0));
- buttonReleasedHandler(getMouseState(0, 0));
+ if (guac_mouse.onmousedown) {
+ guac_mouse.currentState.up = true;
+ guac_mouse.onmousedown(guac_mouse.currentState);
+ }
+
+ if (guac_mouse.onmouseup) {
+ guac_mouse.currentState.up = false;
+ guac_mouse.onmouseup(guac_mouse.currentState);
+ }
}
// Down
if (delta > 0) {
- buttonPressedHandler(getMouseState(0, 1));
- buttonReleasedHandler(getMouseState(0, 0));
- }
-
- if (e.preventDefault)
- e.preventDefault();
+ if (guac_mouse.onmousedown) {
+ guac_mouse.currentState.down = true;
+ guac_mouse.onmousedown(guac_mouse.currentState);
+ }
- e.returnValue = false;
- }
+ if (guac_mouse.onmouseup) {
+ guac_mouse.currentState.down = false;
+ guac_mouse.onmouseup(guac_mouse.currentState);
+ }
+ }
- element.addEventListener('DOMMouseScroll', handleScroll, false);
+ cancelEvent(e);
- element.onmousewheel = function(e) {
- handleScroll(e);
}
+ element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
+ element.addEventListener('mousewheel', mousewheel_handler, false);
+
+};
+
+/**
+ * Simple container for properties describing the state of a mouse.
+ *
+ * @constructor
+ * @param {Number} x The X position of the mouse pointer in pixels.
+ * @param {Number} y The Y position of the mouse pointer in pixels.
+ * @param {Boolean} left Whether the left mouse button is pressed.
+ * @param {Boolean} middle Whether the middle mouse button is pressed.
+ * @param {Boolean} right Whether the right mouse button is pressed.
+ * @param {Boolean} up Whether the up mouse button is pressed (the fourth
+ * button, usually part of a scroll wheel).
+ * @param {Boolean} down Whether the down mouse button is pressed (the fifth
+ * button, usually part of a scroll wheel).
+ */
+Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
+
+ /**
+ * The current X position of the mouse pointer.
+ * @type Number
+ */
+ this.x = x;
+
+ /**
+ * The current Y position of the mouse pointer.
+ * @type Number
+ */
+ this.y = y;
+
+ /**
+ * Whether the left mouse button is currently pressed.
+ * @type Boolean
+ */
+ this.left = left;
+
+ /**
+ * Whether the middle mouse button is currently pressed.
+ * @type Boolean
+ */
+ this.middle = middle
+
+ /**
+ * Whether the right mouse button is currently pressed.
+ * @type Boolean
+ */
+ this.right = right;
+
+ /**
+ * Whether the up mouse button is currently pressed. This is the fourth
+ * mouse button, associated with upward scrolling of the mouse scroll
+ * wheel.
+ * @type Boolean
+ */
+ this.up = up;
+
+ /**
+ * Whether the down mouse button is currently pressed. This is the fifth
+ * mouse button, associated with downward scrolling of the mouse scroll
+ * wheel.
+ * @type Boolean
+ */
+ this.down = down;
+
+};
- var buttonPressedHandler = null;
- var buttonReleasedHandler = null;
- var movementHandler = null;
-
- this.setButtonPressedHandler = function(mh) {buttonPressedHandler = mh;};
- this.setButtonReleasedHandler = function(mh) {buttonReleasedHandler = mh;};
- this.setMovementHandler = function(mh) {movementHandler = mh;};
-
-
- this.getX = function() {return mouseX;};
- this.getY = function() {return mouseY;};
- this.getLeftButton = function() {return mouseLeftButton;};
- this.getMiddleButton = function() {return mouseMiddleButton;};
- this.getRightButton = function() {return mouseRightButton;};
-
-}
-
-function MouseEvent(x, y, left, middle, right, up, down) {
-
- this.getX = function() {
- return x;
- };
-
- this.getY = function() {
- return y;
- };
-
- this.getLeft = function() {
- return left;
- };
-
- this.getMiddle = function() {
- return middle;
- };
-
- this.getRight = function() {
- return right;
- };
-
- this.getUp = function() {
- return up;
- };
-
- this.getDown = function() {
- return down;
- };
-
-}