From: Pierre Ossman Date: Sat, 14 Oct 2017 13:39:56 +0000 (+0200) Subject: Use standard JavaScript properties X-Git-Tag: v1.0.0~43^2~13 X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=747b462337ae4db11208e42fb951c59e53c44be3;p=mirror_novnc.git Use standard JavaScript properties Use normal properties with JavaScript setters and getters instead of our homegrown stuff. This also changes the properties to follow normal naming conventions. --- diff --git a/app/ui.js b/app/ui.js index 9b4fac1..626b5f4 100644 --- a/app/ui.js +++ b/app/ui.js @@ -202,16 +202,16 @@ var UI = { initRFB: function() { try { - UI.rfb = new RFB(document.getElementById('noVNC_canvas'), - {'onNotification': UI.notification, - 'onUpdateState': UI.updateState, - 'onDisconnected': UI.disconnectFinished, - 'onCredentialsRequired': UI.credentials, - 'onCapabilities': function () { UI.updatePowerButton(); UI.initialResize(); }, - 'onClipboard': UI.clipboardReceive, - 'onBell': UI.bell, - 'onFBResize': UI.updateSessionSize, - 'onDesktopName': UI.updateDesktopName}); + UI.rfb = new RFB(document.getElementById('noVNC_canvas')); + UI.rfb.onnotification = UI.notification; + UI.rfb.onupdatestate = UI.updateState; + UI.rfb.ondisconnected = UI.disconnectFinished; + UI.rfb.oncredentialsrequired = UI.credentials; + UI.rfb.oncapabilities = function () { UI.updatePowerButton(); UI.initialResize(); }; + UI.rfb.onclipboard = UI.clipboardReceive; + UI.rfb.onbell = UI.bell; + UI.rfb.onfbresize = UI.updateSessionSize; + UI.rfb.ondesktopname = UI.updateDesktopName; return true; } catch (exc) { var msg = "Unable to create RFB client -- " + exc; @@ -277,8 +277,8 @@ var UI = { document.getElementById("noVNC_keyboard_button") .addEventListener('click', UI.toggleVirtualKeyboard); - UI.touchKeyboard = new Keyboard(document.getElementById('noVNC_keyboardinput'), - {onKeyEvent: UI.keyEvent}); + UI.touchKeyboard = new Keyboard(document.getElementById('noVNC_keyboardinput')); + UI.touchKeyboard.onkeyevent = UI.keyEvent; UI.touchKeyboard.grab(); document.getElementById("noVNC_keyboardinput") .addEventListener('input', UI.keyInput); @@ -494,7 +494,7 @@ var UI = { } // Hide input related buttons in view only mode - if (UI.rfb && UI.rfb.get_view_only()) { + if (UI.rfb && UI.rfb.viewOnly) { document.getElementById('noVNC_keyboard_button') .classList.add('noVNC_hidden'); document.getElementById('noVNC_toggle_extra_keys_button') @@ -958,8 +958,8 @@ var UI = { // Disable/enable power button updatePowerButton: function() { if (UI.connected && - UI.rfb.get_capabilities().power && - !UI.rfb.get_view_only()) { + UI.rfb.capabilities.power && + !UI.rfb.viewOnly) { document.getElementById('noVNC_power_button') .classList.remove("noVNC_hidden"); } else { @@ -1224,7 +1224,7 @@ var UI = { if (screen && UI.connected) { var resizeMode = UI.getSetting('resize'); - UI.rfb.set_scale(1); + UI.rfb.scale = 1.0; // Make sure the viewport is adjusted first UI.updateViewClip(); @@ -1306,7 +1306,7 @@ var UI = { updateViewClip: function() { if (!UI.rfb) return; - var cur_clip = UI.rfb.get_viewport(); + var cur_clip = UI.rfb.viewport; var new_clip = UI.getSetting('view_clip'); var resizeSetting = UI.getSetting('resize'); @@ -1319,7 +1319,7 @@ var UI = { } if (cur_clip !== new_clip) { - UI.rfb.set_viewport(new_clip); + UI.rfb.viewport = new_clip; } var size = UI.screenSize(); @@ -1357,7 +1357,7 @@ var UI = { toggleViewDrag: function() { if (!UI.rfb) return; - var drag = UI.rfb.get_viewportDrag(); + var drag = UI.rfb.viewportDrag; UI.setViewDrag(!drag); }, @@ -1365,7 +1365,7 @@ var UI = { setViewDrag: function(drag) { if (!UI.rfb) return; - UI.rfb.set_viewportDrag(drag); + UI.rfb.viewportDrag = drag; UI.updateViewDrag(); }, @@ -1377,7 +1377,7 @@ var UI = { // Check if viewport drag is possible. It is only possible // if the remote display is clipping the client display. - if (UI.rfb.get_viewport() && + if (UI.rfb.viewport && UI.rfb.clippingDisplay()) { clipping = true; } @@ -1385,14 +1385,14 @@ var UI = { var viewDragButton = document.getElementById('noVNC_view_drag_button'); if (!clipping && - UI.rfb.get_viewportDrag()) { + UI.rfb.viewportDrag) { // The size of the remote display is the same or smaller // than the client display. Make sure viewport drag isn't // active when it can't be used. - UI.rfb.set_viewportDrag(false); + UI.rfb.viewportDrag = false; } - if (UI.rfb.get_viewportDrag()) { + if (UI.rfb.viewportDrag) { viewDragButton.classList.add("noVNC_selected"); } else { viewDragButton.classList.remove("noVNC_selected"); @@ -1661,9 +1661,9 @@ var UI = { * ------v------*/ setMouseButton: function(num) { - var view_only = UI.rfb.get_view_only(); + var view_only = UI.rfb.viewOnly; if (UI.rfb && !view_only) { - UI.rfb.set_touchButton(num); + UI.rfb.touchButton = num; } var blist = [0, 1,2,4]; @@ -1680,12 +1680,12 @@ var UI = { updateLocalCursor: function() { if (!UI.rfb) return; - UI.rfb.set_local_cursor(UI.getSetting('cursor')); + UI.rfb.localCursor = UI.getSetting('cursor'); }, updateViewOnly: function() { if (!UI.rfb) return; - UI.rfb.set_view_only(UI.getSetting('view_only')); + UI.rfb.viewOnly = UI.getSetting('view_only'); }, updateLogging: function() { diff --git a/core/display.js b/core/display.js index 07997cb..07d4ef5 100644 --- a/core/display.js +++ b/core/display.js @@ -10,11 +10,10 @@ /*jslint browser: true, white: false */ /*global Util, Base64, changeCursor */ -import { set_defaults, make_properties } from './util/properties.js'; import * as Log from './util/logging.js'; import Base64 from "./base64.js"; -export default function Display(target, defaults) { +export default function Display(target) { this._drawCtx = null; this._c_forceCanvas = false; @@ -31,12 +30,6 @@ export default function Display(target, defaults) { this._tile_x = 0; this._tile_y = 0; - set_defaults(this, defaults, { - 'scale': 1.0, - 'viewport': false, - "onFlush": function () {}, - }); - Log.Debug(">> Display.constructor"); // The visible canvas @@ -88,7 +81,39 @@ try { } Display.prototype = { - // Public methods + // ===== PROPERTIES ===== + + _scale: 1.0, + get scale() { return this._scale; }, + set scale(scale) { + this._rescale(scale); + }, + + _viewport: false, + get viewport() { return this._viewport; }, + set viewport(viewport) { + this._viewport = viewport; + // May need to readjust the viewport dimensions + var vp = this._viewportLoc; + this.viewportChangeSize(vp.w, vp.h); + this.viewportChangePos(0, 0); + }, + + get width() { + return this._fb_width; + }, + get height() { + return this._fb_height; + }, + + logo: null, + + // ===== EVENT HANDLERS ===== + + onflush: function () {}, // A flush request has finished + + // ===== PUBLIC METHODS ===== + viewportChangePos: function (deltaX, deltaY) { var vp = this._viewportLoc; deltaX = Math.floor(deltaX); @@ -294,7 +319,7 @@ Display.prototype = { flush: function() { if (this._renderQ.length === 0) { - this._onFlush(); + this.onflush(); } else { this._flushing = true; } @@ -492,26 +517,6 @@ Display.prototype = { return this._fb_width > vp.w || this._fb_height > vp.h; }, - // Overridden getters/setters - set_scale: function (scale) { - this._rescale(scale); - }, - - set_viewport: function (viewport) { - this._viewport = viewport; - // May need to readjust the viewport dimensions - var vp = this._viewportLoc; - this.viewportChangeSize(vp.w, vp.h); - this.viewportChangePos(0, 0); - }, - - get_width: function () { - return this._fb_width; - }, - get_height: function () { - return this._fb_height; - }, - autoscale: function (containerWidth, containerHeight, downscaleOnly) { var vp = this._viewportLoc; var targetAspectRatio = containerWidth / containerHeight; @@ -531,7 +536,8 @@ Display.prototype = { this._rescale(scaleRatio); }, - // Private Methods + // ===== PRIVATE METHODS ===== + _rescale: function (factor) { this._scale = factor; var vp = this._viewportLoc; @@ -656,22 +662,11 @@ Display.prototype = { if (this._renderQ.length === 0 && this._flushing) { this._flushing = false; - this._onFlush(); + this.onflush(); } }, }; -make_properties(Display, [ - ['context', 'ro', 'raw'], // Canvas 2D context for rendering (read-only) - ['logo', 'rw', 'raw'], // Logo to display when cleared: {"width": w, "height": h, "type": mime-type, "data": data} - ['scale', 'rw', 'float'], // Display area scale factor 0.0 - 1.0 - ['viewport', 'rw', 'bool'], // Use viewport clipping - ['width', 'ro', 'int'], // Display area width - ['height', 'ro', 'int'], // Display area height - - ['onFlush', 'rw', 'func'], // onFlush(): A flush request has finished -]); - // Class Methods Display.changeCursor = function (target, pixels, mask, hotx, hoty, w, h) { if ((w === 0) || (h === 0)) { diff --git a/core/input/keyboard.js b/core/input/keyboard.js index 6dee460..464d1ce 100644 --- a/core/input/keyboard.js +++ b/core/input/keyboard.js @@ -10,7 +10,6 @@ import * as Log from '../util/logging.js'; import { stopEvent } from '../util/events.js'; -import { set_defaults, make_properties } from '../util/properties.js'; import * as KeyboardUtil from "./util.js"; import KeyTable from "./keysym.js"; @@ -18,15 +17,13 @@ import KeyTable from "./keysym.js"; // Keyboard event handler // -export default function Keyboard(target, defaults) { +export default function Keyboard(target) { this._target = target || null; this._keyDownList = {}; // List of depressed keys // (even if they are happy) this._pendingKey = null; // Key waiting for keypress - set_defaults(this, defaults, {}); - // keep these here so we can refer to them later this._eventHandlers = { 'keyup': this._handleKeyUp.bind(this), @@ -56,14 +53,14 @@ function isEdge() { } Keyboard.prototype = { - // private methods + // ===== EVENT HANDLERS ===== - _sendKeyEvent: function (keysym, code, down) { - if (!this._onKeyEvent) { - return; - } + onkeyevent: function () {}, // Handler for key press/release - Log.Debug("onKeyEvent " + (down ? "down" : "up") + + // ===== PRIVATE METHODS ===== + + _sendKeyEvent: function (keysym, code, down) { + Log.Debug("onkeyevent " + (down ? "down" : "up") + ", keysym: " + keysym, ", code: " + code); // Windows sends CtrlLeft+AltRight when you press @@ -77,19 +74,19 @@ Keyboard.prototype = { ('ControlLeft' in this._keyDownList) && ('AltRight' in this._keyDownList)) { fakeAltGraph = true; - this._onKeyEvent(this._keyDownList['AltRight'], + this.onkeyevent(this._keyDownList['AltRight'], 'AltRight', false); - this._onKeyEvent(this._keyDownList['ControlLeft'], + this.onkeyevent(this._keyDownList['ControlLeft'], 'ControlLeft', false); } } - this._onKeyEvent(keysym, code, down); + this.onkeyevent(keysym, code, down); if (fakeAltGraph) { - this._onKeyEvent(this._keyDownList['ControlLeft'], + this.onkeyevent(this._keyDownList['ControlLeft'], 'ControlLeft', true); - this._onKeyEvent(this._keyDownList['AltRight'], + this.onkeyevent(this._keyDownList['AltRight'], 'AltRight', true); } }, @@ -305,7 +302,7 @@ Keyboard.prototype = { Log.Debug("<< Keyboard.allKeysUp"); }, - // Public methods + // ===== PUBLIC METHODS ===== grab: function () { //Log.Debug(">> Keyboard.grab"); @@ -336,7 +333,3 @@ Keyboard.prototype = { //Log.Debug(">> Keyboard.ungrab"); }, }; - -make_properties(Keyboard, [ - ['onKeyEvent', 'rw', 'func'] // Handler for key press/release -]); diff --git a/core/input/mouse.js b/core/input/mouse.js index ae3ca10..eaf908a 100644 --- a/core/input/mouse.js +++ b/core/input/mouse.js @@ -11,13 +11,12 @@ import * as Log from '../util/logging.js'; import { isTouchDevice } from '../util/browsers.js'; import { setCapture, stopEvent, getPointerEvent } from '../util/events.js'; -import { set_defaults, make_properties } from '../util/properties.js'; var WHEEL_STEP = 10; // Delta threshold for a mouse wheel step var WHEEL_STEP_TIMEOUT = 50; // ms var WHEEL_LINE_HEIGHT = 19; -export default function Mouse(target, defaults) { +export default function Mouse(target) { this._target = target || document; this._doubleClickTimer = null; @@ -29,11 +28,6 @@ export default function Mouse(target, defaults) { this._accumulatedWheelDeltaX = 0; this._accumulatedWheelDeltaY = 0; - // Configuration attributes - set_defaults(this, defaults, { - 'touchButton': 1 - }); - this._eventHandlers = { 'mousedown': this._handleMouseDown.bind(this), 'mouseup': this._handleMouseUp.bind(this), @@ -44,7 +38,16 @@ export default function Mouse(target, defaults) { }; Mouse.prototype = { - // private methods + // ===== PROPERTIES ===== + + touchButton: 1, // Button mask (1, 2, 4) for touch devices (0 means ignore clicks) + + // ===== EVENT HANDLERS ===== + + onmousebutton: function () {}, // Handler for mouse button click/release + onmousemove: function () {}, // Handler for mouse movement + + // ===== PRIVATE METHODS ===== _resetDoubleClickTimer: function () { this._doubleClickTimer = null; @@ -83,7 +86,7 @@ Mouse.prototype = { } this._doubleClickTimer = setTimeout(this._resetDoubleClickTimer.bind(this), 500); } - bmask = this._touchButton; + bmask = this.touchButton; // If bmask is set } else if (e.which) { /* everything except IE */ @@ -95,11 +98,10 @@ Mouse.prototype = { (e.button & 0x4) / 2; // Middle } - if (this._onMouseButton) { - Log.Debug("onMouseButton " + (down ? "down" : "up") + - ", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask); - this._onMouseButton(pos.x, pos.y, down, bmask); - } + Log.Debug("onmousebutton " + (down ? "down" : "up") + + ", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask); + this.onmousebutton(pos.x, pos.y, down, bmask); + stopEvent(e); }, @@ -122,11 +124,11 @@ Mouse.prototype = { _generateWheelStepX: function () { 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); + 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.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 6); + this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 6); } this._accumulatedWheelDeltaX = 0; @@ -135,11 +137,11 @@ Mouse.prototype = { _generateWheelStepY: function () { 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); + 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.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 4); + this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 4); } this._accumulatedWheelDeltaY = 0; @@ -153,8 +155,6 @@ Mouse.prototype = { }, _handleMouseWheel: function (e) { - if (!this._onMouseButton) { return; } - this._resetWheelStepTimers(); this._updateMousePosition(e); @@ -199,9 +199,7 @@ Mouse.prototype = { _handleMouseMove: function (e) { this._updateMousePosition(e); - if (this._onMouseMove) { - this._onMouseMove(this._pos.x, this._pos.y); - } + this.onmousemove(this._pos.x, this._pos.y); stopEvent(e); }, @@ -240,7 +238,8 @@ Mouse.prototype = { this._pos = {x:x, y:y}; }, - // Public methods + // ===== PUBLIC METHODS ===== + grab: function () { var c = this._target; @@ -282,9 +281,3 @@ Mouse.prototype = { c.removeEventListener('contextmenu', this._eventHandlers.mousedisable); } }; - -make_properties(Mouse, [ - ['onMouseButton', 'rw', 'func'], // Handler for mouse button click/release - ['onMouseMove', 'rw', 'func'], // Handler for mouse movement - ['touchButton', 'rw', 'int'] // Button mask (1, 2, 4) for touch devices (0 means ignore clicks) -]); diff --git a/core/rfb.js b/core/rfb.js index 55a9650..2b2dc84 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -13,7 +13,6 @@ import * as Log from './util/logging.js'; import _ from './util/localization.js'; import { decodeUTF8 } from './util/strings.js'; -import { set_defaults, make_properties } from './util/properties.js'; import { browserSupportsCursorURIs } from './util/browsers.js'; import Display from "./display.js"; import Keyboard from "./input/keyboard.js"; @@ -29,11 +28,7 @@ import { encodings, encodingName } from "./encodings.js"; /*jslint white: false, browser: true */ /*global window, Util, Display, Keyboard, Mouse, Websock, Websock_native, Base64, DES, KeyTable, Inflator, XtScancode */ -export default function RFB(target, defaults) { - "use strict"; - if (!defaults) { - defaults = {}; - } +export default function RFB(target) { this._target = target; // Connection details @@ -128,25 +123,6 @@ export default function RFB(target, defaults) { this._viewportDragPos = {}; this._viewportHasMoved = false; - // set the default value on user-facing properties - set_defaults(this, defaults, { - 'local_cursor': false, // Request locally rendered cursor - 'view_only': false, // Disable client mouse/keyboard - 'disconnectTimeout': 3, // Time (s) to wait for disconnection - 'viewportDrag': false, // Move the viewport on mouse drags - - // Callback functions - 'onUpdateState': function () { }, // onUpdateState(rfb, state, oldstate): connection state change - 'onNotification': function () { }, // onNotification(rfb, msg, level, options): notification for UI - 'onDisconnected': function () { }, // onDisconnected(rfb, reason): disconnection finished - 'onCredentialsRequired': function () { }, // onCredentialsRequired(rfb, types): VNC credentials are required - 'onClipboard': function () { }, // onClipboard(rfb, text): RFB clipboard contents received - 'onBell': function () { }, // onBell(rfb): RFB Bell message received - 'onFBResize': function () { }, // onFBResize(rfb, width, height): frame buffer resized - 'onDesktopName': function () { }, // onDesktopName(rfb, name): desktop name received - 'onCapabilities': function () { } // onCapabilities(rfb, caps): the supported capabilities has changed - }); - // main setup Log.Debug(">> RFB.constructor"); @@ -171,20 +147,20 @@ export default function RFB(target, defaults) { // NB: nothing that needs explicit teardown should be done // before this point, since this can throw an exception try { - this._display = new Display(this._target, - {onFlush: this._onFlush.bind(this)}); + this._display = new Display(this._target); } catch (exc) { Log.Error("Display exception: " + exc); throw exc; } + this._display.onflush = this._onFlush.bind(this); this._display.clear(); - this._keyboard = new Keyboard(this._target, - {onKeyEvent: this._handleKeyEvent.bind(this)}); + this._keyboard = new Keyboard(this._target); + this._keyboard.onkeyevent = this._handleKeyEvent.bind(this); - this._mouse = new Mouse(this._target, - {onMouseButton: this._handleMouseButton.bind(this), - onMouseMove: this._handleMouseMove.bind(this)}); + this._mouse = new Mouse(this._target); + this._mouse.onmousebutton = this._handleMouseButton.bind(this); + this._mouse.onmousemove = this._handleMouseMove.bind(this); this._sock = new Websock(); this._sock.on('message', this._handle_message.bind(this)); @@ -238,7 +214,74 @@ export default function RFB(target, defaults) { }; RFB.prototype = { - // Public methods + // ===== PROPERTIES ===== + + disconnectTimeout: 3, + viewportDrag: false, + + _localCursor: false, + get localCursor() { return this._localCursor; }, + set localCursor(cursor) { + if (!cursor || (cursor in {'0': 1, 'no': 1, 'false': 1})) { + this._localCursor = false; + this._display.disableLocalCursor(); //Only show server-side cursor + } else { + if (browserSupportsCursorURIs()) { + this._localCursor = true; + } else { + Log.Warn("Browser does not support local cursor"); + this._display.disableLocalCursor(); + } + } + + // Need to send an updated list of encodings if we are connected + if (this._rfb_connection_state === "connected") { + this._sendEncodings(); + } + }, + + _viewOnly: false, + get viewOnly() { return this._viewOnly; }, + set viewOnly(viewOnly) { + this._viewOnly = viewOnly; + + if (this._rfb_connection_state === "connecting" || + this._rfb_connection_state === "connected") { + if (viewOnly) { + this._keyboard.ungrab(); + this._mouse.ungrab(); + } else { + this._keyboard.grab(); + this._mouse.grab(); + } + } + }, + + get capabilities() { return this._capabilities; }, + + get touchButton() { return this._mouse.touchButton; }, + set touchButton(button) { this._mouse.touchButton = button; }, + + get scale() { return this._display.scale; }, + set scale(scale) { this._display.scale = scale; }, + + get viewport() { return this._display.viewport; }, + set viewport(viewport) { this._display.viewport = viewport; }, + + // ===== EVENT HANDLERS ===== + + onupdatestate: function () {}, // onupdatestate(rfb, state, oldstate): connection state change + onnotification: function () {}, // onnotification(rfb, msg, level, options): notification for the UI + ondisconnected: function () {}, // ondisconnected(rfb, reason): disconnection finished + oncredentialsrequired: function () {}, // oncredentialsrequired(rfb, types): VNC credentials are required + onclipboard: function () {}, // onclipboard(rfb, text): RFB clipboard contents received + onbell: function () {}, // onbell(rfb): RFB Bell message received + onfbresize: function () {}, // onfbresize(rfb, width, height): frame buffer resized + ondesktopname: function () {}, // ondesktopname(rfb, name): desktop name received + oncapabilities: function () {}, // oncapabilities(rfb, caps): the supported capabilities has changed + + // ===== PUBLIC METHODS ===== + connect: function (url, options) { if (!url) { this._fail(_("Must specify URL")); @@ -271,7 +314,7 @@ RFB.prototype = { }, sendCtrlAltDel: function () { - if (this._rfb_connection_state !== 'connected' || this._view_only) { return false; } + if (this._rfb_connection_state !== 'connected' || this._viewOnly) { return false; } Log.Info("Sending Ctrl-Alt-Del"); this.sendKey(KeyTable.XK_Control_L, "ControlLeft", true); @@ -299,7 +342,7 @@ RFB.prototype = { // Send a key press. If 'down' is not specified then send a down key // followed by an up key. sendKey: function (keysym, code, down) { - if (this._rfb_connection_state !== 'connected' || this._view_only) { return false; } + if (this._rfb_connection_state !== 'connected' || this._viewOnly) { return false; } if (down === undefined) { this.sendKey(keysym, code, true); @@ -328,7 +371,7 @@ RFB.prototype = { }, clipboardPasteFrom: function (text) { - if (this._rfb_connection_state !== 'connected' || this._view_only) { return; } + if (this._rfb_connection_state !== 'connected' || this._viewOnly) { return; } RFB.messages.clientCutText(this._sock, text); }, @@ -351,7 +394,7 @@ RFB.prototype = { // and may only be sent if we have received an ExtendedDesktopSize message requestDesktopSize: function (width, height) { if (this._rfb_connection_state !== 'connected' || - this._view_only) { + this._viewOnly) { return false; } @@ -366,7 +409,7 @@ RFB.prototype = { }, - // Private methods + // ===== PRIVATE METHODS ===== _connect: function () { Log.Debug(">> RFB.connect"); @@ -420,8 +463,8 @@ RFB.prototype = { }, _cleanup: function () { - if (!this._view_only) { this._keyboard.ungrab(); } - if (!this._view_only) { this._mouse.ungrab(); } + if (!this._viewOnly) { this._keyboard.ungrab(); } + if (!this._viewOnly) { this._mouse.ungrab(); } this._display.defaultCursor(); if (Log.get_logging() !== 'debug') { // Show noVNC logo when disconnected, unless in @@ -500,7 +543,7 @@ RFB.prototype = { // State change actions this._rfb_connection_state = state; - this._onUpdateState(this, state, oldstate); + this.onupdatestate(this, state, oldstate); var smsg = "New state '" + state + "', was '" + oldstate + "'."; Log.Debug(smsg); @@ -516,13 +559,13 @@ RFB.prototype = { switch (state) { case 'disconnected': - // Call onDisconnected callback after onUpdateState since + // Call ondisconnected callback after onupdatestate since // we don't know if the UI only displays the latest message if (this._rfb_disconnect_reason !== "") { - this._onDisconnected(this, this._rfb_disconnect_reason); + this.ondisconnected(this, this._rfb_disconnect_reason); } else { // No reason means clean disconnect - this._onDisconnected(this); + this.ondisconnected(this); } break; @@ -595,15 +638,15 @@ RFB.prototype = { } if (options) { - this._onNotification(this, msg, level, options); + this.onnotification(this, msg, level, options); } else { - this._onNotification(this, msg, level); + this.onnotification(this, msg, level); } }, _setCapability: function (cap, val) { this._capabilities[cap] = val; - this._onCapabilities(this, this._capabilities); + this.oncapabilities(this, this._capabilities); }, _handle_message: function () { @@ -658,14 +701,14 @@ RFB.prototype = { // If the viewport didn't actually move, then treat as a mouse click event // Send the button down event here, as the button up event is sent at the end of this function - if (!this._viewportHasMoved && !this._view_only) { + if (!this._viewportHasMoved && !this._viewOnly) { RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), bmask); } this._viewportHasMoved = false; } } - if (this._view_only) { return; } // View only, skip mouse events + if (this._viewOnly) { return; } // View only, skip mouse events if (this._rfb_connection_state !== 'connected') { return; } RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask); @@ -692,7 +735,7 @@ RFB.prototype = { return; } - if (this._view_only) { return; } // View only, skip mouse events + if (this._viewOnly) { return; } // View only, skip mouse events if (this._rfb_connection_state !== 'connected') { return; } RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask); @@ -813,7 +856,7 @@ RFB.prototype = { if (!this._rfb_credentials.username || !this._rfb_credentials.password || !this._rfb_credentials.target) { - this._onCredentialsRequired(this, ["username", "password", "target"]); + this.oncredentialsrequired(this, ["username", "password", "target"]); return false; } @@ -830,7 +873,7 @@ RFB.prototype = { if (this._sock.rQwait("auth challenge", 16)) { return false; } if (!this._rfb_credentials.password) { - this._onCredentialsRequired(this, ["password"]); + this.oncredentialsrequired(this, ["password"]); return false; } @@ -1067,12 +1110,12 @@ RFB.prototype = { } // we're past the point where we could backtrack, so it's safe to call this - this._onDesktopName(this, this._fb_name); + this.ondesktopname(this, this._fb_name); this._resize(width, height); - if (!this._view_only) { this._keyboard.grab(); } - if (!this._view_only) { this._mouse.grab(); } + if (!this._viewOnly) { this._keyboard.grab(); } + if (!this._viewOnly) { this._mouse.grab(); } this._fb_depth = 24; @@ -1122,7 +1165,7 @@ RFB.prototype = { encs.push(encodings.pseudoEncodingFence); encs.push(encodings.pseudoEncodingContinuousUpdates); - if (this._local_cursor && this._fb_depth == 24) { + if (this._localCursor && this._fb_depth == 24) { encs.push(encodings.pseudoEncodingCursor); } @@ -1181,9 +1224,9 @@ RFB.prototype = { var text = this._sock.rQshiftStr(length); - if (this._view_only) { return true; } + if (this._viewOnly) { return true; } - this._onClipboard(this, text); + this.onclipboard(this, text); return true; }, @@ -1279,7 +1322,7 @@ RFB.prototype = { case 2: // Bell Log.Debug("Bell"); - this._onBell(this); + this.onbell(this); return true; case 3: // ServerCutText @@ -1431,7 +1474,7 @@ RFB.prototype = { this._destBuff = new Uint8Array(this._fb_width * this._fb_height * 4); this._display.resize(this._fb_width, this._fb_height); - this._onFBResize(this, this._fb_width, this._fb_height); + this.onfbresize(this, this._fb_width, this._fb_height); this._timing.fbu_rt_start = (new Date()).getTime(); this._updateContinuousUpdates(); @@ -1444,86 +1487,6 @@ RFB.prototype = { }, }; -make_properties(RFB, [ - ['local_cursor', 'rw', 'bool'], // Request locally rendered cursor - ['view_only', 'rw', 'bool'], // Disable client mouse/keyboard - ['touchButton', 'rw', 'int'], // Button mask (1, 2, 4) for touch devices (0 means ignore clicks) - ['scale', 'rw', 'float'], // Display area scale factor - ['viewport', 'rw', 'bool'], // Use viewport clipping - ['disconnectTimeout', 'rw', 'int'], // Time (s) to wait for disconnection - ['viewportDrag', 'rw', 'bool'], // Move the viewport on mouse drags - ['capabilities', 'ro', 'arr'], // Supported capabilities - - // Callback functions - ['onUpdateState', 'rw', 'func'], // onUpdateState(rfb, state, oldstate): connection state change - ['onNotification', 'rw', 'func'], // onNotification(rfb, msg, level, options): notification for the UI - ['onDisconnected', 'rw', 'func'], // onDisconnected(rfb, reason): disconnection finished - ['onCredentialsRequired', 'rw', 'func'], // onCredentialsRequired(rfb, types): VNC credentials are required - ['onClipboard', 'rw', 'func'], // onClipboard(rfb, text): RFB clipboard contents received - ['onBell', 'rw', 'func'], // onBell(rfb): RFB Bell message received - ['onFBResize', 'rw', 'func'], // onFBResize(rfb, width, height): frame buffer resized - ['onDesktopName', 'rw', 'func'], // onDesktopName(rfb, name): desktop name received - ['onCapabilities', 'rw', 'func'] // onCapabilities(rfb, caps): the supported capabilities has changed -]); - -RFB.prototype.set_local_cursor = function (cursor) { - if (!cursor || (cursor in {'0': 1, 'no': 1, 'false': 1})) { - this._local_cursor = false; - this._display.disableLocalCursor(); //Only show server-side cursor - } else { - if (browserSupportsCursorURIs()) { - this._local_cursor = true; - } else { - Log.Warn("Browser does not support local cursor"); - this._display.disableLocalCursor(); - } - } - - // Need to send an updated list of encodings if we are connected - if (this._rfb_connection_state === "connected") { - this._sendEncodings(); - } -}; - -RFB.prototype.set_view_only = function (view_only) { - this._view_only = view_only; - - if (this._rfb_connection_state === "connecting" || - this._rfb_connection_state === "connected") { - if (view_only) { - this._keyboard.ungrab(); - this._mouse.ungrab(); - } else { - this._keyboard.grab(); - this._mouse.grab(); - } - } -}; - -RFB.prototype.set_touchButton = function (button) { - this._mouse.set_touchButton(button); -}; - -RFB.prototype.get_touchButton = function () { - return this._mouse.get_touchButton(); -}; - -RFB.prototype.set_scale = function (scale) { - this._display.set_scale(scale); -}; - -RFB.prototype.get_scale = function () { - return this._display.get_scale(); -}; - -RFB.prototype.set_viewport = function (viewport) { - this._display.set_viewport(viewport); -}; - -RFB.prototype.get_viewport = function () { - return this._display.get_viewport(); -}; - // Class Methods RFB.messages = { keyEvent: function (sock, keysym, down) { diff --git a/core/util/properties.js b/core/util/properties.js deleted file mode 100644 index 7b08a64..0000000 --- a/core/util/properties.js +++ /dev/null @@ -1,139 +0,0 @@ -/* - * noVNC: HTML5 VNC client - * Copyright (C) 2012 Joel Martin - * Licensed under MPL 2.0 (see LICENSE.txt) - * - * See README.md for usage and integration instructions. - */ - -/* - * Getter/Setter Creation Utilities - */ - -import * as Log from './logging.js'; - -function make_property (proto, name, mode, type) { - "use strict"; - - var getter; - if (type === 'arr') { - getter = function (idx) { - if (typeof idx !== 'undefined') { - return this['_' + name][idx]; - } else { - return this['_' + name]; - } - }; - } else { - getter = function () { - return this['_' + name]; - }; - } - - var make_setter = function (process_val) { - if (process_val) { - return function (val, idx) { - if (typeof idx !== 'undefined') { - this['_' + name][idx] = process_val(val); - } else { - this['_' + name] = process_val(val); - } - }; - } else { - return function (val, idx) { - if (typeof idx !== 'undefined') { - this['_' + name][idx] = val; - } else { - this['_' + name] = val; - } - }; - } - }; - - var setter; - if (type === 'bool') { - setter = make_setter(function (val) { - if (!val || (val in {'0': 1, 'no': 1, 'false': 1})) { - return false; - } else { - return true; - } - }); - } else if (type === 'int') { - setter = make_setter(function (val) { return parseInt(val, 10); }); - } else if (type === 'float') { - setter = make_setter(parseFloat); - } else if (type === 'str') { - setter = make_setter(String); - } else if (type === 'func') { - setter = make_setter(function (val) { - if (!val) { - return function () {}; - } else { - return val; - } - }); - } else if (type === 'arr' || type === 'dom' || type == 'raw') { - setter = make_setter(); - } else { - throw new Error('Unknown property type ' + type); // some sanity checking - } - - // set the getter - if (typeof proto['get_' + name] === 'undefined') { - proto['get_' + name] = getter; - } - - // set the setter if needed - if (typeof proto['set_' + name] === 'undefined') { - if (mode === 'rw') { - proto['set_' + name] = setter; - } else if (mode === 'wo') { - proto['set_' + name] = function (val, idx) { - if (typeof this['_' + name] !== 'undefined') { - throw new Error(name + " can only be set once"); - } - setter.call(this, val, idx); - }; - } - } - - // make a special setter that we can use in set defaults - proto['_raw_set_' + name] = function (val, idx) { - setter.call(this, val, idx); - //delete this['_init_set_' + name]; // remove it after use - }; -}; - -export function make_properties (constructor, arr) { - "use strict"; - for (var i = 0; i < arr.length; i++) { - make_property(constructor.prototype, arr[i][0], arr[i][1], arr[i][2]); - } -}; - -export function set_defaults (obj, conf, defaults) { - var defaults_keys = Object.keys(defaults); - conf = conf || {}; - var conf_keys = Object.keys(conf); - var keys_obj = {}; - var i; - for (i = 0; i < defaults_keys.length; i++) { keys_obj[defaults_keys[i]] = 1; } - for (i = 0; i < conf_keys.length; i++) { keys_obj[conf_keys[i]] = 1; } - var keys = Object.keys(keys_obj); - - for (i = 0; i < keys.length; i++) { - var setter = obj['_raw_set_' + keys[i]]; - if (!setter) { - Log.Warn('Invalid property ' + keys[i]); - continue; - } - - if (keys[i] in conf) { - setter.call(obj, conf[keys[i]]); - } else { - setter.call(obj, defaults[keys[i]]); - } - } -}; - diff --git a/docs/API-internal.md b/docs/API-internal.md index b9f3be8..6a45d38 100644 --- a/docs/API-internal.md +++ b/docs/API-internal.md @@ -26,14 +26,7 @@ with transparent binary data support. [Websock API](https://github.com/novnc/websockify/wiki/websock.js) wiki page. -## 1.2 Configuration Attributes - -The Mouse, Keyboard and Display objects have a similar API for -configuration options as the RFB object. See the official API -documentation for details. - - -## 1.3 Callbacks +## 1.2 Callbacks For the Mouse, Keyboard and Display objects the callback functions are assigned to configuration attributes, just as for the RFB object. The @@ -61,8 +54,8 @@ callback event name, and the callback function. | name | parameters | description | ------------- | ------------------- | ------------ -| onMouseButton | (x, y, down, bmask) | Handler for mouse button click/release -| onMouseMove | (x, y) | Handler for mouse movement +| onmousebutton | (x, y, down, bmask) | Handler for mouse button click/release +| onmousemove | (x, y) | Handler for mouse movement ## 2.2 Keyboard Module @@ -82,7 +75,7 @@ None | name | parameters | description | ---------- | -------------------- | ------------ -| onKeyPress | (keysym, code, down) | Handler for key press/release +| onkeypress | (keysym, code, down) | Handler for key press/release ## 2.3 Display Module @@ -91,7 +84,6 @@ None | name | type | mode | default | description | ----------- | ----- | ---- | ------- | ------------ -| context | raw | RO | | Canvas 2D context for rendering | logo | raw | RW | | Logo to display when cleared: {"width": width, "height": height, "type": mime-type, "data": data} | scale | float | RW | 1.0 | Display area scale factor 0.0 - 1.0 | viewport | bool | RW | false | Use viewport clipping @@ -131,4 +123,4 @@ None | name | parameters | description | ------- | ---------- | ------------ -| onFlush | () | A display flush has been requested and we are now ready to resume FBU processing +| onflush | () | A display flush has been requested and we are now ready to resume FBU processing diff --git a/docs/API.md b/docs/API.md index 63293c0..6462769 100644 --- a/docs/API.md +++ b/docs/API.md @@ -6,31 +6,10 @@ is instantiated once per connection. ## 1 Configuration Attributes -Each configuration option has a default value, which can be overridden -by a a configuration object passed to the constructor. Configuration -options can then be read and modified after initialization with "get_*" -and "set_*" methods respectively. For example, the following -initializes an RFB object with the 'view_only' configuration option -enabled, then confirms it was set, then disables it: - - var rfb = new RFB(target, {'view_only': true}); - if (rfb.get_view_only()) { - alert("View Only is set"); - } - rfb.set_view_only(false); - -Some attributes are read-only and cannot be changed. An exception will -be thrown if an attempt is made to set one of these attributs. The -attribute mode is one of the following: - - RO - read only - RW - read write - WO - write once - | name | type | mode | default | description | ----------------- | ----- | ---- | ---------- | ------------ -| local_cursor | bool | RW | false | Request locally rendered cursor -| view_only | bool | RW | false | Disable client mouse/keyboard +| localCursor | bool | RW | false | Request locally rendered cursor +| viewOnly | bool | RW | false | Disable client mouse/keyboard | touchButton | int | RW | 1 | Button mask (1, 2, 4) for which click to send on touch devices. 0 means ignore clicks. | scale | float | RW | 1.0 | Display area scale factor | viewport | bool | RW | false | Use viewport clipping @@ -41,10 +20,6 @@ attribute mode is one of the following: ## 2 Methods -In addition to the getter and setter methods to modify configuration -attributes, the RFB object has other methods that are available in the -object instance. - | name | parameters | description | ------------------ | ------------------------------- | ------------ | connect | (url, options) | Connect to the given URL @@ -79,15 +54,15 @@ functions. | name | parameters | description | --------------------- | -------------------------- | ------------ -| onUpdateState | (rfb, state, oldstate) | Connection state change (see details below) -| onNotification | (rfb, msg, level, options) | Notification for the UI (optional options) -| onDisconnected | (rfb, reason) | Disconnection finished with an optional reason. No reason specified means normal disconnect. -| onCredentialsRequired | (rfb, types) | VNC credentials are required (use sendCredentials) -| onClipboard | (rfb, text) | RFB clipboard contents received -| onBell | (rfb) | RFB Bell message received -| onFBResize | (rfb, width, height) | Frame buffer (remote desktop) size changed -| onDesktopName | (rfb, name) | VNC desktop name recieved -| onCapabilities | (rfb, capabilities) | The supported capabilities has changed +| onupdatestate | (rfb, state, oldstate) | Connection state change (see details below) +| onnotification | (rfb, msg, level, options) | Notification for the UI (optional options) +| ondisconnected | (rfb, reason) | Disconnection finished with an optional reason. No reason specified means normal disconnect. +| oncredentialsrequired | (rfb, types) | VNC credentials are required (use sendCredentials) +| onclipboard | (rfb, text) | RFB clipboard contents received +| onbell | (rfb) | RFB Bell message received +| onfbresize | (rfb, width, height) | Frame buffer (remote desktop) size changed +| ondesktopname | (rfb, name) | VNC desktop name recieved +| oncapabilities | (rfb, capabilities) | The supported capabilities has changed __RFB onUpdateState callback details__ diff --git a/tests/playback.js b/tests/playback.js index c2567a9..03748c5 100644 --- a/tests/playback.js +++ b/tests/playback.js @@ -77,10 +77,10 @@ export default function RecordingPlayer (frames, encoding, disconnected, notific RecordingPlayer.prototype = { run: function (realtime, trafficManagement) { // initialize a new RFB - this._rfb = new RFB(document.getElementById('VNC_canvas'), - {'view_only': true, - 'onDisconnected': this._handleDisconnect.bind(this), - 'onNotification': this._notification}); + this._rfb = new RFB(document.getElementById('VNC_canvas')); + this._rfb.viewOnly = true; + this._rfb.ondisconnected = this._handleDisconnect.bind(this); + this._rfb.onnotification = this._notification; this._enablePlaybackMode(); // reset the frame index and timer @@ -152,12 +152,12 @@ RecordingPlayer.prototype = { // Avoid having excessive queue buildup in non-realtime mode if (this._trafficManagement && this._rfb._flushing) { let player = this; - let orig = this._rfb._display.get_onFlush(); - this._rfb._display.set_onFlush(function () { - player._rfb._display.set_onFlush(orig); + let orig = this._rfb._display.onflush; + this._rfb._display.onflush = function () { + player._rfb._display.onflush = orig; player._rfb._onFlush(); player._doPacket(); - }); + }; return; } @@ -182,12 +182,12 @@ RecordingPlayer.prototype = { _finish() { if (this._rfb._display.pending()) { var player = this; - this._rfb._display.set_onFlush(function () { + this._rfb._display.onflush = function () { if (player._rfb._flushing) { player._rfb._onFlush(); } player._finish(); - }); + }; this._rfb._display.flush(); } else { this._running = false; diff --git a/tests/test.display.js b/tests/test.display.js index 32d054e..675b43d 100644 --- a/tests/test.display.js +++ b/tests/test.display.js @@ -39,7 +39,8 @@ describe('Display/Canvas Helper', function () { describe('viewport handling', function () { var display; beforeEach(function () { - display = new Display(document.createElement('canvas'), { viewport: true }); + display = new Display(document.createElement('canvas')); + display.viewport = true; display.resize(5, 5); display.viewportChangeSize(3, 3); display.viewportChangePos(1, 1); @@ -102,7 +103,7 @@ describe('Display/Canvas Helper', function () { }); it('should show the entire framebuffer when disabling the viewport', function() { - display.set_viewport(false); + display.viewport = false; expect(display.absX(0)).to.equal(0); expect(display.absY(0)).to.equal(0); expect(display._target.width).to.equal(5); @@ -110,7 +111,7 @@ describe('Display/Canvas Helper', function () { }); it('should ignore viewport changes when the viewport is disabled', function() { - display.set_viewport(false); + display.viewport = false; display.viewportChangeSize(2, 2); display.viewportChangePos(1, 1); expect(display.absX(0)).to.equal(0); @@ -120,8 +121,8 @@ describe('Display/Canvas Helper', function () { }); it('should show the entire framebuffer just after enabling the viewport', function() { - display.set_viewport(false); - display.set_viewport(true); + display.viewport = false; + display.viewport = true; expect(display.absX(0)).to.equal(0); expect(display.absY(0)).to.equal(0); expect(display._target.width).to.equal(5); @@ -132,7 +133,8 @@ describe('Display/Canvas Helper', function () { describe('resizing', function () { var display; beforeEach(function () { - display = new Display(document.createElement('canvas'), { viewport: false }); + display = new Display(document.createElement('canvas')); + display.viewport = false; display.resize(4, 4); }); @@ -157,7 +159,7 @@ describe('Display/Canvas Helper', function () { describe('viewport', function () { beforeEach(function () { - display.set_viewport(true); + display.viewport = true; display.viewportChangeSize(3, 3); display.viewportChangePos(1, 1); }); @@ -194,7 +196,8 @@ describe('Display/Canvas Helper', function () { beforeEach(function () { canvas = document.createElement('canvas'); - display = new Display(canvas, { viewport: true }); + display = new Display(canvas); + display.viewport = true; display.resize(4, 4); display.viewportChangeSize(3, 3); display.viewportChangePos(1, 1); @@ -206,21 +209,21 @@ describe('Display/Canvas Helper', function () { }); it('should not change the bitmap size of the canvas', function () { - display.set_scale(2.0); + display.scale = 2.0; expect(canvas.width).to.equal(3); expect(canvas.height).to.equal(3); }); it('should change the effective rendered size of the canvas', function () { - display.set_scale(2.0); + display.scale = 2.0; expect(canvas.clientWidth).to.equal(6); expect(canvas.clientHeight).to.equal(6); }); it('should not change when resizing', function () { - display.set_scale(2.0); + display.scale = 2.0; display.resize(5, 5); - expect(display.get_scale()).to.equal(2.0); + expect(display.scale).to.equal(2.0); expect(canvas.width).to.equal(3); expect(canvas.height).to.equal(3); expect(canvas.clientWidth).to.equal(6); @@ -234,7 +237,8 @@ describe('Display/Canvas Helper', function () { beforeEach(function () { canvas = document.createElement('canvas'); - display = new Display(canvas, { viewport: true }); + display = new Display(canvas); + display.viewport = true; display.resize(4, 3); document.body.appendChild(canvas); }); @@ -309,12 +313,12 @@ describe('Display/Canvas Helper', function () { it('should draw the logo on #clear with a logo set', function (done) { display._logo = { width: 4, height: 4, type: "image/png", data: make_image_png(checked_data) }; display.clear(); - display.set_onFlush(function () { + display.onflush = function () { expect(display).to.have.displayed(checked_data); expect(display._fb_width).to.equal(4); expect(display._fb_height).to.equal(4); done(); - }); + }; display.flush(); }); @@ -350,10 +354,10 @@ describe('Display/Canvas Helper', function () { it('should support drawing images via #imageRect', function (done) { display.imageRect(0, 0, "image/png", make_image_png(checked_data)); display.flip(); - display.set_onFlush(function () { + display.onflush = function () { expect(display).to.have.displayed(checked_data); done(); - }); + }; display.flush(); }); @@ -442,11 +446,11 @@ describe('Display/Canvas Helper', function () { }); it('should call callback when queue is flushed', function () { - display.set_onFlush(sinon.spy()); + display.onflush = sinon.spy(); display.fillRect(0, 0, 4, 4, [0, 0xff, 0]); - expect(display.get_onFlush()).to.not.have.been.called; + expect(display.onflush).to.not.have.been.called; display.flush(); - expect(display.get_onFlush()).to.have.been.calledOnce; + expect(display.onflush).to.have.been.calledOnce; }); it('should draw a blit image on type "blit"', function () { diff --git a/tests/test.keyboard.js b/tests/test.keyboard.js index 52f21f0..513d797 100644 --- a/tests/test.keyboard.js +++ b/tests/test.keyboard.js @@ -31,105 +31,105 @@ describe('Key Event Handling', function() { describe('Decode Keyboard Events', function() { it('should decode keydown events', function(done) { if (isIE() || isEdge()) this.skip(); - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('KeyA'); expect(down).to.be.equal(true); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'})); }); it('should decode keyup events', function(done) { if (isIE() || isEdge()) this.skip(); var calls = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('KeyA'); if (calls++ === 1) { expect(down).to.be.equal(false); done(); } - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'})); kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'})); }); describe('Legacy keypress Events', function() { it('should wait for keypress when needed', function() { - var callback = sinon.spy(); - var kbd = new Keyboard(document, {onKeyEvent: callback}); + var kbd = new Keyboard(document); + kbd.onkeyevent = sinon.spy(); kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41})); - expect(callback).to.not.have.been.called; + expect(kbd.onkeyevent).to.not.have.been.called; }); it('should decode keypress events', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('KeyA'); expect(down).to.be.equal(true); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41})); kbd._handleKeyPress(keyevent('keypress', {code: 'KeyA', charCode: 0x61})); }); it('should ignore keypress with different code', function() { - var callback = sinon.spy(); - var kbd = new Keyboard(document, {onKeyEvent: callback}); + var kbd = new Keyboard(document); + kbd.onkeyevent = sinon.spy(); kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41})); kbd._handleKeyPress(keyevent('keypress', {code: 'KeyB', charCode: 0x61})); - expect(callback).to.not.have.been.called; + expect(kbd.onkeyevent).to.not.have.been.called; }); it('should handle keypress with missing code', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('KeyA'); expect(down).to.be.equal(true); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41})); kbd._handleKeyPress(keyevent('keypress', {charCode: 0x61})); }); it('should guess key if no keypress and numeric key', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x32); expect(code).to.be.equal('Digit2'); expect(down).to.be.equal(true); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'Digit2', keyCode: 0x32})); }); it('should guess key if no keypress and alpha key', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('KeyA'); expect(down).to.be.equal(true); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41, shiftKey: false})); }); it('should guess key if no keypress and alpha key (with shift)', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x41); expect(code).to.be.equal('KeyA'); expect(down).to.be.equal(true); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41, shiftKey: true})); }); it('should not guess key if no keypress and unknown key', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0); expect(code).to.be.equal('KeyA'); expect(down).to.be.equal(true); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x09})); }); }); @@ -168,8 +168,8 @@ describe('Key Event Handling', function() { it('should fake keyup events for virtual keyboards', function(done) { if (isIE() || isEdge()) this.skip(); var count = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { switch (count++) { case 0: expect(keysym).to.be.equal(0x61); @@ -182,7 +182,7 @@ describe('Key Event Handling', function() { expect(down).to.be.equal(false); done(); } - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'Unidentified', key: 'a'})); }); @@ -215,8 +215,8 @@ describe('Key Event Handling', function() { it('should fake keyup events on iOS', function(done) { if (isIE() || isEdge()) this.skip(); var count = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { switch (count++) { case 0: expect(keysym).to.be.equal(0x61); @@ -229,7 +229,7 @@ describe('Key Event Handling', function() { expect(down).to.be.equal(false); done(); } - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'})); }); }); @@ -240,67 +240,67 @@ describe('Key Event Handling', function() { if (isIE() || isEdge()) this.skip(); }); it('should send release using the same keysym as the press', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('KeyA'); if (!down) { done(); } - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'})); kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'b'})); }); it('should send the same keysym for multiple presses', function() { var count = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('KeyA'); expect(down).to.be.equal(true); count++; - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'})); kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'b'})); expect(count).to.be.equal(2); }); it('should do nothing on keyup events if no keys are down', function() { - var callback = sinon.spy(); - var kbd = new Keyboard(document, {onKeyEvent: callback}); + var kbd = new Keyboard(document); + kbd.onkeyevent = sinon.spy(); kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'})); - expect(callback).to.not.have.been.called; + expect(kbd.onkeyevent).to.not.have.been.called; }); describe('Legacy Events', function() { it('should track keys using keyCode if no code', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('Platform65'); if (!down) { done(); } - }}); + }; kbd._handleKeyDown(keyevent('keydown', {keyCode: 65, key: 'a'})); kbd._handleKeyUp(keyevent('keyup', {keyCode: 65, key: 'b'})); }); it('should ignore compositing code', function() { - var kbd = new Keyboard({ - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('Unidentified'); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {keyCode: 229, key: 'a'})); }); it('should track keys using keyIdentifier if no code', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0x61); expect(code).to.be.equal('Platform65'); if (!down) { done(); } - }}); + }; kbd._handleKeyDown(keyevent('keydown', {keyIdentifier: 'U+0041', key: 'a'})); kbd._handleKeyUp(keyevent('keyup', {keyIdentifier: 'U+0041', key: 'b'})); }); @@ -335,8 +335,8 @@ describe('Key Event Handling', function() { it('should change Alt to AltGraph', function() { var count = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { switch (count++) { case 0: expect(keysym).to.be.equal(0xFF7E); @@ -347,27 +347,27 @@ describe('Key Event Handling', function() { expect(code).to.be.equal('AltRight'); break; } - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'AltLeft', key: 'Alt', location: 1})); kbd._handleKeyDown(keyevent('keydown', {code: 'AltRight', key: 'Alt', location: 2})); expect(count).to.be.equal(2); }); it('should change left Super to Alt', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0xFFE9); expect(code).to.be.equal('MetaLeft'); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'MetaLeft', key: 'Meta', location: 1})); }); it('should change right Super to left Super', function(done) { - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { expect(keysym).to.be.equal(0xFFEB); expect(code).to.be.equal('MetaRight'); done(); - }}); + }; kbd._handleKeyDown(keyevent('keydown', {code: 'MetaRight', key: 'Meta', location: 2})); }); }); @@ -400,8 +400,8 @@ describe('Key Event Handling', function() { it('should generate fake undo/redo events on press when AltGraph is down', function() { var times_called = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { switch(times_called++) { case 0: expect(keysym).to.be.equal(0xFFE3); @@ -439,7 +439,7 @@ describe('Key Event Handling', function() { expect(down).to.be.equal(true); break; } - }}); + }; // First the modifier combo kbd._handleKeyDown(keyevent('keydown', {code: 'ControlLeft', key: 'Control', location: 1})); kbd._handleKeyDown(keyevent('keydown', {code: 'AltRight', key: 'Alt', location: 2})); @@ -449,8 +449,8 @@ describe('Key Event Handling', function() { }); it('should no do anything on key release', function() { var times_called = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { switch(times_called++) { case 7: expect(keysym).to.be.equal(0x61); @@ -458,7 +458,7 @@ describe('Key Event Handling', function() { expect(down).to.be.equal(false); break; } - }}); + }; // First the modifier combo kbd._handleKeyDown(keyevent('keydown', {code: 'ControlLeft', key: 'Control', location: 1})); kbd._handleKeyDown(keyevent('keydown', {code: 'AltRight', key: 'Alt', location: 2})); @@ -469,8 +469,8 @@ describe('Key Event Handling', function() { }); it('should not consider a char modifier to be down on the modifier key itself', function() { var times_called = 0; - var kbd = new Keyboard(document, { - onKeyEvent: function(keysym, code, down) { + var kbd = new Keyboard(document); + kbd.onkeyevent = function(keysym, code, down) { switch(times_called++) { case 0: expect(keysym).to.be.equal(0xFFE3); @@ -488,7 +488,7 @@ describe('Key Event Handling', function() { expect(down).to.be.equal(true); break; } - }}); + }; // First the modifier combo kbd._handleKeyDown(keyevent('keydown', {code: 'ControlLeft', key: 'Control', location: 1})); kbd._handleKeyDown(keyevent('keydown', {code: 'AltLeft', key: 'Alt', location: 1})); diff --git a/tests/test.mouse.js b/tests/test.mouse.js index 905b524..248a933 100644 --- a/tests/test.mouse.js +++ b/tests/test.mouse.js @@ -33,55 +33,51 @@ describe('Mouse Event Handling', function() { describe('Decode Mouse Events', function() { it('should decode mousedown events', function(done) { - var mouse = new Mouse(target, { - onMouseButton: function(x, y, down, bmask) { - expect(bmask).to.be.equal(0x01); - expect(down).to.be.equal(1); - done(); - } - }); + var mouse = new Mouse(target); + mouse.onmousebutton = function(x, y, down, bmask) { + expect(bmask).to.be.equal(0x01); + expect(down).to.be.equal(1); + done(); + }; mouse._handleMouseDown(mouseevent('mousedown', { button: '0x01' })); }); it('should decode mouseup events', function(done) { var calls = 0; - var mouse = new Mouse(target, { - onMouseButton: function(x, y, down, bmask) { - expect(bmask).to.be.equal(0x01); - if (calls++ === 1) { - expect(down).to.not.be.equal(1); - done(); - } + var mouse = new Mouse(target); + mouse.onmousebutton = function(x, y, down, bmask) { + expect(bmask).to.be.equal(0x01); + if (calls++ === 1) { + expect(down).to.not.be.equal(1); + done(); } - }); + }; mouse._handleMouseDown(mouseevent('mousedown', { button: '0x01' })); mouse._handleMouseUp(mouseevent('mouseup', { button: '0x01' })); }); it('should decode mousemove events', function(done) { - var mouse = new Mouse(target, { - onMouseMove: function(x, y) { - // Note that target relative coordinates are sent - expect(x).to.be.equal(40); - expect(y).to.be.equal(10); - done(); - } - }); + var mouse = new Mouse(target); + mouse.onmousemove = function(x, y) { + // Note that target relative coordinates are sent + expect(x).to.be.equal(40); + expect(y).to.be.equal(10); + done(); + }; mouse._handleMouseMove(mouseevent('mousemove', { clientX: 50, clientY: 20 })); }); it('should decode mousewheel events', function(done) { var calls = 0; - var mouse = new Mouse(target, { - onMouseButton: function(x, y, down, bmask) { - calls++; - expect(bmask).to.be.equal(1<<6); - if (calls === 1) { - expect(down).to.be.equal(1); - } else if (calls === 2) { - expect(down).to.not.be.equal(1); - done(); - } + var mouse = new Mouse(target); + mouse.onmousebutton = function(x, y, down, bmask) { + calls++; + expect(bmask).to.be.equal(1<<6); + if (calls === 1) { + expect(down).to.be.equal(1); + } else if (calls === 2) { + expect(down).to.not.be.equal(1); + done(); } - }); + }; mouse._handleMouseWheel(mouseevent('mousewheel', { deltaX: 50, deltaY: 0, deltaMode: 0})); @@ -95,21 +91,20 @@ describe('Mouse Event Handling', function() { it('should use same pos for 2nd tap if close enough', function(done) { var calls = 0; - var mouse = new Mouse(target, { - onMouseButton: function(x, y, down, bmask) { - calls++; - if (calls === 1) { - expect(down).to.be.equal(1); - expect(x).to.be.equal(68); - expect(y).to.be.equal(36); - } else if (calls === 3) { - expect(down).to.be.equal(1); - expect(x).to.be.equal(68); - expect(y).to.be.equal(36); - done(); - } + var mouse = new Mouse(target); + mouse.onmousebutton = function(x, y, down, bmask) { + calls++; + if (calls === 1) { + expect(down).to.be.equal(1); + expect(x).to.be.equal(68); + expect(y).to.be.equal(36); + } else if (calls === 3) { + expect(down).to.be.equal(1); + expect(x).to.be.equal(68); + expect(y).to.be.equal(36); + done(); } - }); + }; // touch events are sent in an array of events // with one item for each touch point mouse._handleMouseDown(touchevent( @@ -127,21 +122,20 @@ describe('Mouse Event Handling', function() { it('should not modify 2nd tap pos if far apart', function(done) { var calls = 0; - var mouse = new Mouse(target, { - onMouseButton: function(x, y, down, bmask) { - calls++; - if (calls === 1) { - expect(down).to.be.equal(1); - expect(x).to.be.equal(68); - expect(y).to.be.equal(36); - } else if (calls === 3) { - expect(down).to.be.equal(1); - expect(x).to.not.be.equal(68); - expect(y).to.not.be.equal(36); - done(); - } + var mouse = new Mouse(target); + mouse.onmousebutton = function(x, y, down, bmask) { + calls++; + if (calls === 1) { + expect(down).to.be.equal(1); + expect(x).to.be.equal(68); + expect(y).to.be.equal(36); + } else if (calls === 3) { + expect(down).to.be.equal(1); + expect(x).to.not.be.equal(68); + expect(y).to.not.be.equal(36); + done(); } - }); + }; mouse._handleMouseDown(touchevent( 'touchstart', { touches: [{ clientX: 78, clientY: 46 }]})); this.clock.tick(10); @@ -157,21 +151,20 @@ describe('Mouse Event Handling', function() { it('should not modify 2nd tap pos if not soon enough', function(done) { var calls = 0; - var mouse = new Mouse(target, { - onMouseButton: function(x, y, down, bmask) { - calls++; - if (calls === 1) { - expect(down).to.be.equal(1); - expect(x).to.be.equal(68); - expect(y).to.be.equal(36); - } else if (calls === 3) { - expect(down).to.be.equal(1); - expect(x).to.not.be.equal(68); - expect(y).to.not.be.equal(36); - done(); - } + var mouse = new Mouse(target); + mouse.onmousebutton = function(x, y, down, bmask) { + calls++; + if (calls === 1) { + expect(down).to.be.equal(1); + expect(x).to.be.equal(68); + expect(y).to.be.equal(36); + } else if (calls === 3) { + expect(down).to.be.equal(1); + expect(x).to.not.be.equal(68); + expect(y).to.not.be.equal(36); + done(); } - }); + }; mouse._handleMouseDown(touchevent( 'touchstart', { touches: [{ clientX: 78, clientY: 46 }]})); this.clock.tick(10); @@ -187,21 +180,20 @@ describe('Mouse Event Handling', function() { it('should not modify 2nd tap pos if not touch', function(done) { var calls = 0; - var mouse = new Mouse(target, { - onMouseButton: function(x, y, down, bmask) { - calls++; - if (calls === 1) { - expect(down).to.be.equal(1); - expect(x).to.be.equal(68); - expect(y).to.be.equal(36); - } else if (calls === 3) { - expect(down).to.be.equal(1); - expect(x).to.not.be.equal(68); - expect(y).to.not.be.equal(36); - done(); - } + var mouse = new Mouse(target); + mouse.onmousebutton = function(x, y, down, bmask) { + calls++; + if (calls === 1) { + expect(down).to.be.equal(1); + expect(x).to.be.equal(68); + expect(y).to.be.equal(36); + } else if (calls === 3) { + expect(down).to.be.equal(1); + expect(x).to.not.be.equal(68); + expect(y).to.not.be.equal(36); + done(); } - }); + }; mouse._handleMouseDown(mouseevent( 'mousedown', { button: '0x01', clientX: 78, clientY: 46 })); this.clock.tick(10); @@ -223,8 +215,8 @@ describe('Mouse Event Handling', function() { afterEach(function () { this.clock.restore(); }); it('should accumulate wheel events if small enough', function () { - var callback = sinon.spy(); - var mouse = new Mouse(target, { onMouseButton: callback }); + var mouse = new Mouse(target); + mouse.onmousebutton = sinon.spy(); mouse._handleMouseWheel(mouseevent( 'mousewheel', { clientX: 18, clientY: 40, @@ -242,7 +234,7 @@ describe('Mouse Event Handling', function() { 'mousewheel', { clientX: 18, clientY: 40, deltaX: 4, deltaY: 0, deltaMode: 0 })); - expect(callback).to.have.callCount(2); // mouse down and up + expect(mouse.onmousebutton).to.have.callCount(2); // mouse down and up this.clock.tick(10); mouse._handleMouseWheel(mouseevent( @@ -252,12 +244,12 @@ describe('Mouse Event Handling', function() { expect(mouse._accumulatedWheelDeltaX).to.be.equal(4); expect(mouse._accumulatedWheelDeltaY).to.be.equal(9); - expect(callback).to.have.callCount(2); // still + expect(mouse.onmousebutton).to.have.callCount(2); // still }); it('should not accumulate large wheel events', function () { - var callback = sinon.spy(); - var mouse = new Mouse(target, { onMouseButton: callback }); + var mouse = new Mouse(target); + mouse.onmousebutton = sinon.spy(); mouse._handleMouseWheel(mouseevent( 'mousewheel', { clientX: 18, clientY: 40, @@ -271,24 +263,24 @@ describe('Mouse Event Handling', function() { 'mousewheel', { clientX: 18, clientY: 40, deltaX: 400, deltaY: 400, deltaMode: 0 })); - expect(callback).to.have.callCount(8); // mouse down and up + expect(mouse.onmousebutton).to.have.callCount(8); // mouse down and up }); it('should send even small wheel events after a timeout', function () { - var callback = sinon.spy(); - var mouse = new Mouse(target, { onMouseButton: callback }); + var mouse = new Mouse(target); + mouse.onmousebutton = sinon.spy(); mouse._handleMouseWheel(mouseevent( 'mousewheel', { clientX: 18, clientY: 40, deltaX: 1, deltaY: 0, deltaMode: 0 })); this.clock.tick(51); // timeout on 50 ms - expect(callback).to.have.callCount(2); // mouse down and up + expect(mouse.onmousebutton).to.have.callCount(2); // mouse down and up }); it('should account for non-zero deltaMode', function () { - var callback = sinon.spy(); - var mouse = new Mouse(target, { onMouseButton: callback }); + var mouse = new Mouse(target); + mouse.onmousebutton = sinon.spy(); mouse._handleMouseWheel(mouseevent( 'mousewheel', { clientX: 18, clientY: 40, @@ -300,7 +292,7 @@ describe('Mouse Event Handling', function() { 'mousewheel', { clientX: 18, clientY: 40, deltaX: 1, deltaY: 0, deltaMode: 2 })); - expect(callback).to.have.callCount(4); // mouse down and up + expect(mouse.onmousebutton).to.have.callCount(4); // mouse down and up }); }); diff --git a/tests/test.rfb.js b/tests/test.rfb.js index a1d2f9e..918d5dc 100644 --- a/tests/test.rfb.js +++ b/tests/test.rfb.js @@ -9,9 +9,8 @@ import { encodings } from '../core/encodings.js'; import FakeWebSocket from './fake.websocket.js'; import sinon from '../vendor/sinon.js'; -function make_rfb (extra_opts) { - extra_opts = extra_opts || {}; - return new RFB(document.createElement('canvas'), extra_opts); +function make_rfb () { + return new RFB(document.createElement('canvas')); } var push8 = function (arr, num) { @@ -136,7 +135,7 @@ describe('Remote Frame Buffer Protocol Client', function() { client._sock._websocket._open(); sinon.spy(client._sock, 'flush'); client._rfb_connection_state = 'connected'; - client._view_only = false; + client._viewOnly = false; }); it('should sent ctrl[down]-alt[down]-del[down] then del[up]-alt[up]-ctrl[up]', function () { @@ -159,7 +158,7 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should not send the keys if we are set as view_only', function () { - client._view_only = true; + client._viewOnly = true; client.sendCtrlAltDel(); expect(client._sock.flush).to.not.have.been.called; }); @@ -172,7 +171,7 @@ describe('Remote Frame Buffer Protocol Client', function() { client._sock._websocket._open(); sinon.spy(client._sock, 'flush'); client._rfb_connection_state = 'connected'; - client._view_only = false; + client._viewOnly = false; }); it('should send a single key with the given code and state (down = true)', function () { @@ -197,7 +196,7 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should not send the key if we are set as view_only', function () { - client._view_only = true; + client._viewOnly = true; client.sendKey(123, 'Key123'); expect(client._sock.flush).to.not.have.been.called; }); @@ -226,7 +225,7 @@ describe('Remote Frame Buffer Protocol Client', function() { client._sock._websocket._open(); sinon.spy(client._sock, 'flush'); client._rfb_connection_state = 'connected'; - client._view_only = false; + client._viewOnly = false; }); it('should send the given text in a paste event', function () { @@ -250,7 +249,7 @@ describe('Remote Frame Buffer Protocol Client', function() { client._sock._websocket._open(); sinon.spy(client._sock, 'flush'); client._rfb_connection_state = 'connected'; - client._view_only = false; + client._viewOnly = false; client._supportsSetDesktopSize = true; }); @@ -292,7 +291,7 @@ describe('Remote Frame Buffer Protocol Client', function() { client._sock._websocket._open(); sinon.spy(client._sock, 'flush'); client._rfb_connection_state = 'connected'; - client._view_only = false; + client._viewOnly = false; client._rfb_xvp_ver = 1; }); @@ -340,9 +339,9 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should call the updateState callback', function () { - client.set_onUpdateState(sinon.spy()); + client.onupdatestate = sinon.spy(); client._updateConnectionState('connecting'); - var spy = client.get_onUpdateState(); + var spy = client.onupdatestate; expect(spy.args[0][1]).to.equal('connecting'); }); @@ -359,19 +358,19 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should ignore state changes to the same state', function () { - client.set_onUpdateState(sinon.spy()); + client.onupdatestate = sinon.spy(); client._rfb_connection_state = 'connecting'; client._updateConnectionState('connecting'); - var spy = client.get_onUpdateState(); + var spy = client.onupdatestate; expect(spy).to.not.have.been.called; }); it('should ignore illegal state changes', function () { - client.set_onUpdateState(sinon.spy()); + client.onupdatestate = sinon.spy(); client._rfb_connection_state = 'connected'; client._updateConnectionState('disconnected'); expect(client._rfb_connection_state).to.not.equal('disconnected'); - var spy = client.get_onUpdateState(); + var spy = client.onupdatestate; expect(spy).to.not.have.been.called; }); }); @@ -416,9 +415,9 @@ describe('Remote Frame Buffer Protocol Client', function() { it('should result in disconnect callback with message when reason given', function () { client._rfb_connection_state = 'connected'; - client.set_onDisconnected(sinon.spy()); + client.ondisconnected = sinon.spy(); client._fail('a reason'); - var spy = client.get_onDisconnected(); + var spy = client.ondisconnected; this.clock.tick(2000); expect(spy).to.have.been.calledOnce; expect(spy.args[0].length).to.equal(2); @@ -432,18 +431,18 @@ describe('Remote Frame Buffer Protocol Client', function() { beforeEach(function () { client = make_rfb(); }); it('should call the notification callback', function () { - client.set_onNotification(sinon.spy()); + client.onnotification = sinon.spy(); client._notification('notify!', 'warn'); - var spy = client.get_onNotification(); + var spy = client.onnotification; expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.equal('notify!'); expect(spy.args[0][2]).to.equal('warn'); }); it('should not call the notification callback when level is invalid', function () { - client.set_onNotification(sinon.spy()); + client.onnotification = sinon.spy(); client._notification('notify!', 'invalid'); - var spy = client.get_onNotification(); + var spy = client.onnotification; expect(spy).to.not.have.been.called; }); }); @@ -484,7 +483,7 @@ describe('Remote Frame Buffer Protocol Client', function() { sinon.spy(client, '_updateConnectionState'); client._sock._websocket.close = function () {}; // explicitly don't call onclose client._updateConnectionState('disconnecting'); - this.clock.tick(client.get_disconnectTimeout() * 1000); + this.clock.tick(client.disconnectTimeout * 1000); expect(client._updateConnectionState).to.have.been.calledTwice; expect(client._rfb_disconnect_reason).to.not.equal(""); expect(client._rfb_connection_state).to.equal("disconnected"); @@ -492,9 +491,9 @@ describe('Remote Frame Buffer Protocol Client', function() { it('should not fail if Websock.onclose gets called within the disconnection timeout', function () { client._updateConnectionState('disconnecting'); - this.clock.tick(client.get_disconnectTimeout() * 500); + this.clock.tick(client.disconnectTimeout * 500); client._sock._websocket.close(); - this.clock.tick(client.get_disconnectTimeout() * 500 + 1); + this.clock.tick(client.disconnectTimeout * 500 + 1); expect(client._rfb_connection_state).to.equal('disconnected'); }); @@ -510,39 +509,39 @@ describe('Remote Frame Buffer Protocol Client', function() { beforeEach(function () { client = make_rfb(); }); it('should call the disconnect callback if the state is "disconnected"', function () { - client.set_onDisconnected(sinon.spy()); + client.ondisconnected = sinon.spy(); client._rfb_connection_state = 'disconnecting'; client._rfb_disconnect_reason = "error"; client._updateConnectionState('disconnected'); - var spy = client.get_onDisconnected(); + var spy = client.ondisconnected; expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.equal("error"); }); it('should not call the disconnect callback if the state is not "disconnected"', function () { - client.set_onDisconnected(sinon.spy()); + client.ondisconnected = sinon.spy(); client._updateConnectionState('disconnecting'); - var spy = client.get_onDisconnected(); + var spy = client.ondisconnected; expect(spy).to.not.have.been.called; }); it('should call the disconnect callback without msg when no reason given', function () { - client.set_onDisconnected(sinon.spy()); + client.ondisconnected = sinon.spy(); client._rfb_connection_state = 'disconnecting'; client._rfb_disconnect_reason = ""; client._updateConnectionState('disconnected'); - var spy = client.get_onDisconnected(); + var spy = client.ondisconnected; expect(spy).to.have.been.calledOnce; expect(spy.args[0].length).to.equal(1); }); it('should call the updateState callback before the disconnect callback', function () { - client.set_onDisconnected(sinon.spy()); - client.set_onUpdateState(sinon.spy()); + client.ondisconnected = sinon.spy(); + client.onupdatestate = sinon.spy(); client._rfb_connection_state = 'disconnecting'; client._updateConnectionState('disconnected'); - var updateStateSpy = client.get_onUpdateState(); - var disconnectSpy = client.get_onDisconnected(); + var updateStateSpy = client.onupdatestate; + var disconnectSpy = client.ondisconnected; expect(updateStateSpy.calledBefore(disconnectSpy)).to.be.true; }); }); @@ -790,14 +789,14 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should call the onCredentialsRequired callback if missing a password', function () { - client.set_onCredentialsRequired(sinon.spy()); + client.oncredentialsrequired = sinon.spy(); send_security(2, client); var challenge = []; for (var i = 0; i < 16; i++) { challenge[i] = i; } client._sock._websocket._receive_data(new Uint8Array(challenge)); - var spy = client.get_onCredentialsRequired(); + var spy = client.oncredentialsrequired; expect(client._rfb_credentials).to.be.empty; expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.have.members(["password"]); @@ -849,23 +848,23 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should call the onCredentialsRequired callback if all credentials are missing', function() { - client.set_onCredentialsRequired(sinon.spy()); + client.oncredentialsrequired = sinon.spy(); client._rfb_credentials = {}; send_security(22, client); - var spy = client.get_onCredentialsRequired(); + var spy = client.oncredentialsrequired; expect(client._rfb_credentials).to.be.empty; expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.have.members(["username", "password", "target"]); }); it('should call the onCredentialsRequired callback if some credentials are missing', function() { - client.set_onCredentialsRequired(sinon.spy()); + client.oncredentialsrequired = sinon.spy(); client._rfb_credentials = { username: 'user', target: 'target' }; send_security(22, client); - var spy = client.get_onCredentialsRequired(); + var spy = client.oncredentialsrequired; expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.have.members(["username", "password", "target"]); }); @@ -1098,10 +1097,10 @@ describe('Remote Frame Buffer Protocol Client', function() { // NB(sross): we just warn, not fail, for endian-ness and shifts, so we don't test them it('should set the framebuffer name and call the callback', function () { - client.set_onDesktopName(sinon.spy()); + client.ondesktopname = sinon.spy(); send_server_init({ name: 'some name' }, client); - var spy = client.get_onDesktopName(); + var spy = client.ondesktopname; expect(client._fb_name).to.equal('some name'); expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.equal('some name'); @@ -1127,11 +1126,11 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should call the resize callback and resize the display', function () { - client.set_onFBResize(sinon.spy()); + client.onfbresize = sinon.spy(); sinon.spy(client._display, 'resize'); send_server_init({ width: 27, height: 32 }, client); - var spy = client.get_onFBResize(); + var spy = client.onfbresize; expect(client._display.resize).to.have.been.calledOnce; expect(client._display.resize).to.have.been.calledWith(27, 32); expect(spy).to.have.been.calledOnce; @@ -1559,11 +1558,11 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should handle the DesktopSize pseduo-encoding', function () { - client.set_onFBResize(sinon.spy()); + client.onfbresize = sinon.spy(); sinon.spy(client._display, 'resize'); send_fbu_msg([{ x: 0, y: 0, width: 20, height: 50, encoding: -223 }], [[]], client); - var spy = client.get_onFBResize(); + var spy = client.onfbresize; expect(spy).to.have.been.calledOnce; expect(spy).to.have.been.calledWith(sinon.match.any, 20, 50); @@ -1582,7 +1581,7 @@ describe('Remote Frame Buffer Protocol Client', function() { client._fb_height = 4; client._display.resize(4, 4); sinon.spy(client._display, 'resize'); - client.set_onFBResize(sinon.spy()); + client.onfbresize = sinon.spy(); }); function make_screen_data (nr_of_screens) { @@ -1602,10 +1601,10 @@ describe('Remote Frame Buffer Protocol Client', function() { } it('should call callback when resize is supported', function () { - client.set_onCapabilities(sinon.spy()); + client.oncapabilities = sinon.spy(); expect(client._supportsSetDesktopSize).to.be.false; - expect(client.get_capabilities().resize).to.be.false; + expect(client.capabilities.resize).to.be.false; var reason_for_change = 0; // server initiated var status_code = 0; // No error @@ -1615,9 +1614,9 @@ describe('Remote Frame Buffer Protocol Client', function() { make_screen_data(1), client); expect(client._supportsSetDesktopSize).to.be.true; - expect(client.get_onCapabilities()).to.have.been.calledOnce; - expect(client.get_onCapabilities().args[0][1].resize).to.be.true; - expect(client.get_capabilities().resize).to.be.true; + expect(client.oncapabilities).to.have.been.calledOnce; + expect(client.oncapabilities.args[0][1].resize).to.be.true; + expect(client.capabilities.resize).to.be.true; }), it('should handle a resize requested by this client', function () { @@ -1634,7 +1633,7 @@ describe('Remote Frame Buffer Protocol Client', function() { expect(client._display.resize).to.have.been.calledOnce; expect(client._display.resize).to.have.been.calledWith(20, 50); - var spy = client.get_onFBResize(); + var spy = client.onfbresize; expect(spy).to.have.been.calledOnce; expect(spy).to.have.been.calledWith(sinon.match.any, 20, 50); }); @@ -1653,7 +1652,7 @@ describe('Remote Frame Buffer Protocol Client', function() { expect(client._display.resize).to.have.been.calledOnce; expect(client._display.resize).to.have.been.calledWith(20, 50); - var spy = client.get_onFBResize(); + var spy = client.onfbresize; expect(spy).to.have.been.calledOnce; expect(spy).to.have.been.calledWith(sinon.match.any, 20, 50); }); @@ -1672,7 +1671,7 @@ describe('Remote Frame Buffer Protocol Client', function() { expect(client._display.resize).to.have.been.calledOnce; expect(client._display.resize).to.have.been.calledWith(60, 50); - var spy = client.get_onFBResize(); + var spy = client.onfbresize; expect(spy).to.have.been.calledOnce; expect(spy).to.have.been.calledWith(sinon.match.any, 60, 50); }); @@ -1690,7 +1689,7 @@ describe('Remote Frame Buffer Protocol Client', function() { expect(client._display.resize).to.not.have.been.called; - var spy = client.get_onFBResize(); + var spy = client.onfbresize; expect(spy).to.not.have.been.called; }); }); @@ -1708,20 +1707,20 @@ describe('Remote Frame Buffer Protocol Client', function() { describe('XVP Message Handling', function () { it('should send a notification on XVP_FAIL', function () { - client.set_onNotification(sinon.spy()); + client.onnotification = sinon.spy(); client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 0])); - var spy = client.get_onNotification(); + var spy = client.onnotification; expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.equal('XVP Operation Failed'); }); it('should set the XVP version and fire the callback with the version on XVP_INIT', function () { - client.set_onCapabilities(sinon.spy()); + client.oncapabilities = sinon.spy(); client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 1])); expect(client._rfb_xvp_ver).to.equal(10); - expect(client.get_onCapabilities()).to.have.been.calledOnce; - expect(client.get_onCapabilities().args[0][1].power).to.be.true; - expect(client.get_capabilities().power).to.be.true; + expect(client.oncapabilities).to.have.been.calledOnce; + expect(client.oncapabilities.args[0][1].power).to.be.true; + expect(client.capabilities.power).to.be.true; }); it('should fail on unknown XVP message types', function () { @@ -1736,18 +1735,18 @@ describe('Remote Frame Buffer Protocol Client', function() { var data = [3, 0, 0, 0]; push32(data, expected_str.length); for (var i = 0; i < expected_str.length; i++) { data.push(expected_str.charCodeAt(i)); } - client.set_onClipboard(sinon.spy()); + client.onclipboard = sinon.spy(); client._sock._websocket._receive_data(new Uint8Array(data)); - var spy = client.get_onClipboard(); + var spy = client.onclipboard; expect(spy).to.have.been.calledOnce; expect(spy.args[0][1]).to.equal(expected_str); }); it('should fire the bell callback on Bell', function () { - client.set_onBell(sinon.spy()); + client.onbell = sinon.spy(); client._sock._websocket._receive_data(new Uint8Array([2])); - expect(client.get_onBell()).to.have.been.calledOnce; + expect(client.onbell).to.have.been.calledOnce; }); it('should respond correctly to ServerFence', function () { @@ -1832,26 +1831,26 @@ describe('Remote Frame Buffer Protocol Client', function() { }); it('should not send button messages in view-only mode', function () { - client._view_only = true; - client._mouse._onMouseButton(0, 0, 1, 0x001); + client._viewOnly = true; + client._handleMouseButton(0, 0, 1, 0x001); expect(client._sock.flush).to.not.have.been.called; }); it('should not send movement messages in view-only mode', function () { - client._view_only = true; - client._mouse._onMouseMove(0, 0); + client._viewOnly = true; + client._handleMouseMove(0, 0); expect(client._sock.flush).to.not.have.been.called; }); it('should send a pointer event on mouse button presses', function () { - client._mouse._onMouseButton(10, 12, 1, 0x001); + client._handleMouseButton(10, 12, 1, 0x001); var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: function () {}}; RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x001); expect(client._sock).to.have.sent(pointer_msg._sQ); }); it('should send a mask of 1 on mousedown', function () { - client._mouse._onMouseButton(10, 12, 1, 0x001); + client._handleMouseButton(10, 12, 1, 0x001); var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: function () {}}; RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x001); expect(client._sock).to.have.sent(pointer_msg._sQ); @@ -1859,22 +1858,22 @@ describe('Remote Frame Buffer Protocol Client', function() { it('should send a mask of 0 on mouseup', function () { client._mouse_buttonMask = 0x001; - client._mouse._onMouseButton(10, 12, 0, 0x001); + client._handleMouseButton(10, 12, 0, 0x001); var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: function () {}}; RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000); expect(client._sock).to.have.sent(pointer_msg._sQ); }); it('should send a pointer event on mouse movement', function () { - client._mouse._onMouseMove(10, 12); + client._handleMouseMove(10, 12); var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: function () {}}; RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000); expect(client._sock).to.have.sent(pointer_msg._sQ); }); it('should set the button mask so that future mouse movements use it', function () { - client._mouse._onMouseButton(10, 12, 1, 0x010); - client._mouse._onMouseMove(13, 9); + client._handleMouseButton(10, 12, 1, 0x010); + client._handleMouseMove(13, 9); var pointer_msg = {_sQ: new Uint8Array(12), _sQlen: 0, flush: function () {}}; RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x010); RFB.messages.pointerEvent(pointer_msg, 13, 9, 0x010); @@ -1888,19 +1887,19 @@ describe('Remote Frame Buffer Protocol Client', function() { it('should not send movement messages when viewport dragging', function () { client._viewportDragging = true; client._display.viewportChangePos = sinon.spy(); - client._mouse._onMouseMove(13, 9); + client._handleMouseMove(13, 9); expect(client._sock.flush).to.not.have.been.called; }); it('should not send button messages when initiating viewport dragging', function () { client._viewportDrag = true; - client._mouse._onMouseButton(13, 9, 0x001); + client._handleMouseButton(13, 9, 0x001); expect(client._sock.flush).to.not.have.been.called; }); it('should be initiate viewport dragging on a button down event, if enabled', function () { client._viewportDrag = true; - client._mouse._onMouseButton(13, 9, 0x001); + client._handleMouseButton(13, 9, 0x001); expect(client._viewportDragging).to.be.true; expect(client._viewportDragPos).to.deep.equal({ x: 13, y: 9 }); }); @@ -1908,7 +1907,7 @@ describe('Remote Frame Buffer Protocol Client', function() { it('should terminate viewport dragging on a button up event, if enabled', function () { client._viewportDrag = true; client._viewportDragging = true; - client._mouse._onMouseButton(13, 9, 0x000); + client._handleMouseButton(13, 9, 0x000); expect(client._viewportDragging).to.be.false; }); @@ -1924,7 +1923,7 @@ describe('Remote Frame Buffer Protocol Client', function() { client._viewportDragPos = { x: oldX, y: oldY }; client._display.viewportChangePos = sinon.spy(); - client._mouse._onMouseMove(newX, newY); + client._handleMouseMove(newX, newY); expect(client._viewportDragging).to.be.true; expect(client._viewportHasMoved).to.be.true; @@ -1943,20 +1942,20 @@ describe('Remote Frame Buffer Protocol Client', function() { client._sock._websocket._open(); sinon.spy(client._sock, 'flush'); client._rfb_connection_state = 'connected'; - client._view_only = false; + client._viewOnly = false; }); it('should send a key message on a key press', function () { var keyevent = {}; - client._keyboard._onKeyEvent(0x41, 'KeyA', true); + client._handleKeyEvent(0x41, 'KeyA', true); var key_msg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: function () {}}; RFB.messages.keyEvent(key_msg, 0x41, 1); expect(client._sock).to.have.sent(key_msg._sQ); }); it('should not send messages in view-only mode', function () { - client._view_only = true; - client._keyboard._onKeyEvent('a', 'KeyA', true); + client._viewOnly = true; + client._handleKeyEvent('a', 'KeyA', true); expect(client._sock.flush).to.not.have.been.called; }); }); @@ -1996,9 +1995,9 @@ describe('Remote Frame Buffer Protocol Client', function() { it('should process all normal messages directly', function () { client._sock._websocket._open(); client._rfb_connection_state = 'connected'; - client.set_onBell(sinon.spy()); + client.onbell = sinon.spy(); client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02])); - expect(client.get_onBell()).to.have.been.calledTwice; + expect(client.onbell).to.have.been.calledTwice; }); // open events diff --git a/vnc_lite.html b/vnc_lite.html index 95cf286..f0c8ace 100644 --- a/vnc_lite.html +++ b/vnc_lite.html @@ -206,7 +206,7 @@ function updatePowerButtons() { var powerbuttons; powerbuttons = document.getElementById('noVNC_power_buttons'); - if (rfb.get_capabilities().power) { + if (rfb.capabilities.power) { powerbuttons.style.display = 'inline'; } else { powerbuttons.style.display = 'none'; @@ -256,15 +256,15 @@ } try { - rfb = new RFB(document.getElementById('noVNC_canvas'), - {'local_cursor': WebUtil.getConfigVar('cursor', true), - 'view_only': WebUtil.getConfigVar('view_only', false), - 'onNotification': notification, - 'onUpdateState': updateState, - 'onDisconnected': disconnected, - 'onCapabilities': function () { updatePowerButtons(); initialResize(); }, - 'onCredentialsRequired': credentials, - 'onDesktopName': updateDesktopName}); + rfb = new RFB(document.getElementById('noVNC_canvas')); + rfb.localCursor = WebUtil.getConfigVar('cursor', true); + rfb.viewOnly = WebUtil.getConfigVar('view_only', false); + rfb.onnotification = notification; + rfb.onupdatestate = updateState; + rfb.ondisconnected = disconnected; + rfb.oncapabilities = function () { updatePowerButtons(); initialResize(); }; + rfb.oncredentialsrequired = credentials; + rfb.ondesktopname = updateDesktopName; } catch (exc) { status('Unable to create RFB client -- ' + exc, 'error'); return; // don't continue trying to connect