Commit c4164bda authored by Joel Martin's avatar Joel Martin

JSLint and "use strict".

parent 753bde8f
Canvas = { /*
* noVNC: HTML5 VNC client
*
* Licensed under AGPL-3 (see LICENSE.AGPL-3)
*
* See README.md for usage and integration instructions.
*/
"use strict";
/*global window, $, Browser */
// Everything namespaced inside Canvas
var Canvas = {
c_x : 0, c_x : 0,
c_y : 0, c_y : 0,
...@@ -40,26 +52,29 @@ keyUp : function (e) { ...@@ -40,26 +52,29 @@ keyUp : function (e) {
}, },
ctxDisable: function (e) { ctxDisable: function (e) {
evt = e.event || window.event; var evt = e.event || window.event;
/* Stop propagation if inside canvas area */ /* Stop propagation if inside canvas area */
if ((evt.clientX >= Canvas.c_x) && (evt.clientX < (Canvas.c_x + Canvas.c_wx)) && if ((evt.clientX >= Canvas.c_x) &&
(evt.clientY >= Canvas.c_y) && (evt.clientY < (Canvas.c_y + Canvas.c_wy))) { (evt.clientY >= Canvas.c_y) &&
(evt.clientX < (Canvas.c_x + Canvas.c_wx)) &&
(evt.clientY < (Canvas.c_y + Canvas.c_wy))) {
e.stop(); e.stop();
return false; return false;
}; }
}, },
init: function (id, width, height, keyDown, keyUp, mouseDown, mouseUp, mouseMove) { init: function (id, width, height, keyDown, keyUp,
mouseDown, mouseUp, mouseMove) {
console.log(">> Canvas.init"); console.log(">> Canvas.init");
Canvas.id = id; Canvas.id = id;
if (! keyDown) keyDown = Canvas.keyDown; if (! keyDown) { keyDown = Canvas.keyDown; }
if (! keyUp) keyUp = Canvas.keyUp; if (! keyUp) { keyUp = Canvas.keyUp; }
if (! mouseDown) mouseDown = Canvas.mouseDown; if (! mouseDown) { mouseDown = Canvas.mouseDown; }
if (! mouseUp) mouseUp = Canvas.mouseUp; if (! mouseUp) { mouseUp = Canvas.mouseUp; }
if (! mouseMove) mouseMove = Canvas.mouseMove; if (! mouseMove) { mouseMove = Canvas.mouseMove; }
var c = $(Canvas.id); var c = $(Canvas.id);
document.addEvent('keydown', keyDown); document.addEvent('keydown', keyDown);
...@@ -79,7 +94,7 @@ init: function (id, width, height, keyDown, keyUp, mouseDown, mouseUp, mouseMove ...@@ -79,7 +94,7 @@ init: function (id, width, height, keyDown, keyUp, mouseDown, mouseUp, mouseMove
Canvas.c_wx = c.getSize().x; Canvas.c_wx = c.getSize().x;
Canvas.c_wy = c.getSize().y; Canvas.c_wy = c.getSize().y;
if (! c.getContext) return; if (! c.getContext) { return; }
Canvas.ctx = c.getContext('2d'); Canvas.ctx = c.getContext('2d');
Canvas.prevStyle = ""; Canvas.prevStyle = "";
...@@ -108,6 +123,7 @@ stop: function () { ...@@ -108,6 +123,7 @@ stop: function () {
}, },
draw: function () { draw: function () {
var img, x, y;
/* Border */ /* Border */
Canvas.ctx.stroke(); Canvas.ctx.stroke();
Canvas.ctx.rect(0, 0, Canvas.c_wx, Canvas.c_wy); Canvas.ctx.rect(0, 0, Canvas.c_wx, Canvas.c_wy);
...@@ -121,13 +137,13 @@ draw: function () { ...@@ -121,13 +137,13 @@ draw: function () {
*/ */
/* Test array image data */ /* Test array image data */
//var img = Canvas.ctx.createImageData(50, 50); //img = Canvas.ctx.createImageData(50, 50);
var img = Canvas.ctx.getImageData(0, 0, 50, 50); img = Canvas.ctx.getImageData(0, 0, 50, 50);
for (y=0; y< 50; y++) { for (y=0; y< 50; y++) {
for (x=0; x< 50; x++) { for (x=0; x< 50; x++) {
img.data[(y*50 + x)*4 + 0] = 255 - parseInt((255 / 50) * y); img.data[(y*50 + x)*4 + 0] = 255 - parseInt((255 / 50) * y, 10);
img.data[(y*50 + x)*4 + 1] = parseInt((255 / 50) * y); img.data[(y*50 + x)*4 + 1] = parseInt((255 / 50) * y, 10);
img.data[(y*50 + x)*4 + 2] = parseInt((255 / 50) * x); img.data[(y*50 + x)*4 + 2] = parseInt((255 / 50) * x, 10);
img.data[(y*50 + x)*4 + 3] = 255; img.data[(y*50 + x)*4 + 3] = 255;
} }
} }
...@@ -143,13 +159,15 @@ draw: function () { ...@@ -143,13 +159,15 @@ draw: function () {
* gecko, Javascript array handling is much slower. * gecko, Javascript array handling is much slower.
*/ */
getTile: function(x, y, width, height, color) { getTile: function(x, y, width, height, color) {
var img = {'x': x, 'y': y, 'width': width, 'height': height, var img, p, red, green, blue, j, i;
img = {'x': x, 'y': y, 'width': width, 'height': height,
'data': []}; 'data': []};
if (Browser.Engine.webkit) { if (Browser.Engine.webkit) {
var p, red = color[0], green = color[1], blue = color[2]; red = color[0];
var width = img.width, height = img.height; green = color[1];
for (var j = 0; j < height; j++) { blue = color[2];
for (var i = 0; i < width; i++) { for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
p = (i + (j * width) ) * 4; p = (i + (j * width) ) * 4;
img.data[p + 0] = red; img.data[p + 0] = red;
img.data[p + 1] = green; img.data[p + 1] = green;
...@@ -164,11 +182,14 @@ getTile: function(x, y, width, height, color) { ...@@ -164,11 +182,14 @@ getTile: function(x, y, width, height, color) {
}, },
setTile: function(img, x, y, w, h, color) { setTile: function(img, x, y, w, h, color) {
var p, red, green, blue, width, j, i;
if (Browser.Engine.webkit) { if (Browser.Engine.webkit) {
var p, red = color[0], green = color[1], blue = color[2]; width = img.width;
var width = img.width; red = color[0];
for (var j = 0; j < h; j++) { green = color[1];
for (var i = 0; i < w; i++) { blue = color[2];
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
p = (x + i + ((y + j) * width) ) * 4; p = (x + i + ((y + j) * width) ) * 4;
img.data[p + 0] = red; img.data[p + 0] = red;
img.data[p + 1] = green; img.data[p + 1] = green;
...@@ -192,9 +213,10 @@ putTile: function(img) { ...@@ -192,9 +213,10 @@ putTile: function(img) {
rgbxImage: function(x, y, width, height, arr) { rgbxImage: function(x, y, width, height, arr) {
var img, i;
/* Old firefox and Opera don't support createImageData */ /* Old firefox and Opera don't support createImageData */
var img = Canvas.ctx.getImageData(0, 0, width, height); img = Canvas.ctx.getImageData(0, 0, width, height);
for (var i=0; i < (width * height); i++) { for (i=0; i < (width * height); i++) {
img.data[i*4 + 0] = arr[i*4 + 0]; img.data[i*4 + 0] = arr[i*4 + 0];
img.data[i*4 + 1] = arr[i*4 + 1]; img.data[i*4 + 1] = arr[i*4 + 1];
img.data[i*4 + 2] = arr[i*4 + 2]; img.data[i*4 + 2] = arr[i*4 + 2];
...@@ -206,9 +228,9 @@ rgbxImage: function(x, y, width, height, arr) { ...@@ -206,9 +228,9 @@ rgbxImage: function(x, y, width, height, arr) {
fillRect: function(x, y, width, height, color) { fillRect: function(x, y, width, height, color) {
var newStyle = "rgb(" + color[0] + "," + color[1] + "," + color[2] + ")"; var newStyle = "rgb(" + color[0] + "," + color[1] + "," + color[2] + ")";
if (newStyle != Canvas.prevStyle) { if (newStyle !== Canvas.prevStyle) {
Canvas.ctx.fillStyle = newStyle; Canvas.ctx.fillStyle = newStyle;
prevStyle = newStyle; Canvas.prevStyle = newStyle;
} }
Canvas.ctx.fillRect(x, y, width, height); Canvas.ctx.fillRect(x, y, width, height);
}, },
...@@ -220,8 +242,8 @@ copyImage: function(old_x, old_y, new_x, new_y, width, height) { ...@@ -220,8 +242,8 @@ copyImage: function(old_x, old_y, new_x, new_y, width, height) {
/* Translate DOM key event to keysym value */ /* Translate DOM key event to keysym value */
getKeysym: function(e) { getKeysym: function(e) {
var evt, keysym;
evt = e.event || window.event; evt = e.event || window.event;
var keysym;
/* Remap modifier and special keys */ /* Remap modifier and special keys */
switch ( evt.keyCode ) { switch ( evt.keyCode ) {
...@@ -263,8 +285,9 @@ getKeysym: function(e) { ...@@ -263,8 +285,9 @@ getKeysym: function(e) {
case 187 : keysym = 61; break; // = (IE) case 187 : keysym = 61; break; // = (IE)
case 188 : keysym = 44; break; // , (Mozilla, IE) case 188 : keysym = 44; break; // , (Mozilla, IE)
case 109 : // - (Mozilla) case 109 : // - (Mozilla)
if (Browser.Engine.gecko) if (Browser.Engine.gecko) {
keysym = 45; break; keysym = 45; }
break;
case 189 : keysym = 45; break; // - (IE) case 189 : keysym = 45; break; // - (IE)
case 190 : keysym = 46; break; // . (Mozilla, IE) case 190 : keysym = 46; break; // . (Mozilla, IE)
case 191 : keysym = 47; break; // / (Mozilla, IE) case 191 : keysym = 47; break; // / (Mozilla, IE)
......
...@@ -4,38 +4,45 @@ ...@@ -4,38 +4,45 @@
* Licensed under AGPL-3 (see LICENSE.AGPL-3) * Licensed under AGPL-3 (see LICENSE.AGPL-3)
* *
* See README.md for usage and integration instructions. * See README.md for usage and integration instructions.
*
* This script defines the following globals:
* VNC_scripts, VNC_native_ws, FBU, RFB,
* RQ, RQ_reorder, RQ_seq_num, SQ
*/ */
"use strict";
/*global window, WebSocket, $, Browser, Canvas, Base64, DES */
// Globals defined here
var VNC_native_ws, FBU, RFB, RQ, RQ_reorder, RQ_seq_num, SQ;
/* /*
* Load supporting scripts * Load supporting scripts
*/ */
(function () {
VNC_scripts = ""; var extra, start, end;
extra = "";
// Uncomment to activate firebug lite start = "<script src='";
//VNC_scripts += "<script src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'><\/script>"; end = "'><\/script>";
VNC_scripts += "<script src='include/mootools.js'><\/script>"; // Uncomment to activate firebug lite
VNC_scripts += "<script src='include/base64.js'><\/script>"; //extra += start + "http://getfirebug.com/releases/lite/1.2/" +
VNC_scripts += "<script src='include/des.js'><\/script>"; // firebug-lite-compressed.js" + end;
VNC_scripts += "<script src='include/util.js'><\/script>";
VNC_scripts += "<script src='canvas.js'><\/script>"; extra += start + "include/mootools.js" + end;
extra += start + "include/base64.js" + end;
/* If no builtin websockets then load web_socket.js */ extra += start + "include/des.js" + end;
if (window.WebSocket) { extra += start + "include/util.js" + end;
extra += start + "canvas.js" + end;
/* If no builtin websockets then load web_socket.js */
if (window.WebSocket) {
VNC_native_ws = true; VNC_native_ws = true;
} else { } else {
VNC_native_ws = false; VNC_native_ws = false;
VNC_scripts += "<script src='include/web-socket-js/swfobject.js'><\/script>"; extra += start + "include/web-socket-js/swfobject.js" + end;
VNC_scripts += "<script src='include/web-socket-js/FABridge.js'><\/script>"; extra += start + "include/web-socket-js/FABridge.js" + end;
VNC_scripts += "<script src='include/web-socket-js/web_socket.js'><\/script>"; extra += start + "include/web-socket-js/web_socket.js" + end;
} }
document.write(VNC_scripts); document.write(extra);
}());
/* /*
* Make arrays quack * Make arrays quack
...@@ -43,19 +50,19 @@ document.write(VNC_scripts); ...@@ -43,19 +50,19 @@ document.write(VNC_scripts);
Array.prototype.shift8 = function () { Array.prototype.shift8 = function () {
return this.shift(); return this.shift();
} };
Array.prototype.push8 = function (num) { Array.prototype.push8 = function (num) {
this.push(num & 0xFF); this.push(num & 0xFF);
} };
Array.prototype.shift16 = function () { Array.prototype.shift16 = function () {
return (this.shift() << 8) + return (this.shift() << 8) +
(this.shift() ); (this.shift() );
} };
Array.prototype.push16 = function (num) { Array.prototype.push16 = function (num) {
this.push((num >> 8) & 0xFF, this.push((num >> 8) & 0xFF,
(num ) & 0xFF ); (num ) & 0xFF );
} };
Array.prototype.shift32 = function () { Array.prototype.shift32 = function () {
...@@ -63,35 +70,35 @@ Array.prototype.shift32 = function () { ...@@ -63,35 +70,35 @@ Array.prototype.shift32 = function () {
(this.shift() << 16) + (this.shift() << 16) +
(this.shift() << 8) + (this.shift() << 8) +
(this.shift() ); (this.shift() );
} };
Array.prototype.get32 = function (off) { Array.prototype.get32 = function (off) {
return (this[off ] << 24) + return (this[off ] << 24) +
(this[off + 1] << 16) + (this[off + 1] << 16) +
(this[off + 2] << 8) + (this[off + 2] << 8) +
(this[off + 3] ); (this[off + 3] );
} };
Array.prototype.push32 = function (num) { Array.prototype.push32 = function (num) {
this.push((num >> 24) & 0xFF, this.push((num >> 24) & 0xFF,
(num >> 16) & 0xFF, (num >> 16) & 0xFF,
(num >> 8) & 0xFF, (num >> 8) & 0xFF,
(num ) & 0xFF ); (num ) & 0xFF );
} };
Array.prototype.shiftStr = function (len) { Array.prototype.shiftStr = function (len) {
var arr = this.splice(0, len); var arr = this.splice(0, len);
return arr.map(function (num) { return arr.map(function (num) {
return String.fromCharCode(num); } ).join(''); return String.fromCharCode(num); } ).join('');
} };
Array.prototype.pushStr = function (str) { Array.prototype.pushStr = function (str) {
var n = str.length; var i, n = str.length;
for (var i=0; i < n; i++) { for (i=0; i < n; i++) {
this.push(str.charCodeAt(i)); this.push(str.charCodeAt(i));
} }
} };
Array.prototype.shiftBytes = function (len) { Array.prototype.shiftBytes = function (len) {
return this.splice(0, len); return this.splice(0, len);
} };
/* /*
* Frame buffer update state * Frame buffer update state
...@@ -173,64 +180,72 @@ mouse_arr : [], ...@@ -173,64 +180,72 @@ mouse_arr : [],
/* RFB/VNC initialisation */ /* RFB/VNC initialisation */
init_msg: function () { init_msg: function () {
console.log(">> init_msg [RFB.state '" + RFB.state + "']"); console.log(">> init_msg [RFB.state '" + RFB.state + "']");
//console.log("RQ (" + RQ.length + ") " + RQ); //console.log("RQ (" + RQ.length + ") " + RQ);
var verstr, strlen, reason, reason_len, server_version,
types, num_types, challenge, response,
bpp, depth, big_endian, true_color, name_length;
switch (RFB.state) { switch (RFB.state) {
case 'ProtocolVersion' : case 'ProtocolVersion' :
if (RQ.length < 12) { if (RQ.length < 12) {
RFB.updateState('failed', "Disconnected: invalid RFB protocol version received"); RFB.updateState('failed',
"Disconnected: incomplete protocol version");
return; return;
} }
var server_version = RQ.shiftStr(12).substr(0,11); server_version = RQ.shiftStr(12).substr(0,11);
console.log("Server ProtocolVersion: " + server_version); console.log("Server ProtocolVersion: " + server_version);
if ((server_version == "RFB 003.003") || (RFB.max_version == 3.3)) { if ((server_version === "RFB 003.003") || (RFB.max_version === 3.3)) {
RFB.version = 3.3; RFB.version = 3.3;
var verstr = "RFB 003.003"; verstr = "RFB 003.003";
RFB.send_string(verstr + "\n"); RFB.send_string(verstr + "\n");
RFB.updateState('Security', "Sent ProtocolVersion: " + verstr); RFB.updateState('Security', "Sent ProtocolVersion: " + verstr);
} else if (server_version == "RFB 003.008") { } else if (server_version === "RFB 003.008") {
RFB.version = 3.8; RFB.version = 3.8;
var verstr = "RFB 003.008"; verstr = "RFB 003.008";
RFB.send_string(verstr + "\n"); RFB.send_string(verstr + "\n");
RFB.updateState('Security', "Sent ProtocolVersion: " + verstr); RFB.updateState('Security', "Sent ProtocolVersion: " + verstr);
} else { } else {
RFB.updateState('failed', "Invalid server version " + server_version); RFB.updateState('failed',
"Invalid server version " + server_version);
return; return;
} }
break; break;
case 'Security' : case 'Security' :
if (RFB.version == 3.8) { if (RFB.version === 3.8) {
var num_types = RQ.shift8(); num_types = RQ.shift8();
if (num_types == 0) { if (num_types === 0) {
var strlen = RQ.shift32(); strlen = RQ.shift32();
var reason = RQ.shiftStr(strlen); reason = RQ.shiftStr(strlen);
RFB.updateState('failed', "Disconnected: security failure: " + reason); RFB.updateState('failed',
"Disconnected: security failure: " + reason);
return; return;
} }
var types = RQ.shiftBytes(num_types); types = RQ.shiftBytes(num_types);
if ((types[0] != 1) && (types[0] != 2)) { if ((types[0] !== 1) && (types[0] !== 2)) {
RFB.updateState('failed', "Disconnected: invalid security types list: " + types); RFB.updateState('failed',
"Disconnected: invalid security types list: " + types);
return; return;
} }
if (RFB.password.length == 0) { if (RFB.password.length === 0) {
RFB.auth_scheme = 1; RFB.auth_scheme = 1;
} else { } else {
RFB.auth_scheme = types[0]; RFB.auth_scheme = types[0];
} }
RFB.send_array([RFB.auth_scheme]); RFB.send_array([RFB.auth_scheme]);
} else if (RFB.version == 3.3) { } else if (RFB.version === 3.3) {
if (RQ.length < 4) { if (RQ.length < 4) {
RFB.updateState('failed', "Invalid security frame"); RFB.updateState('failed', "Invalid security frame");
return; return;
} }
RFB.auth_scheme = RQ.shift32(); RFB.auth_scheme = RQ.shift32();
} }
RFB.updateState('Authentication', "Authenticating using scheme: " + RFB.auth_scheme); RFB.updateState('Authentication',
"Authenticating using scheme: " + RFB.auth_scheme);
// Fall through // Fall through
case 'Authentication' : case 'Authentication' :
...@@ -241,9 +256,10 @@ init_msg: function () { ...@@ -241,9 +256,10 @@ init_msg: function () {
console.log(" waiting for auth reason bytes"); console.log(" waiting for auth reason bytes");
return; return;
} }
var strlen = RQ.shift32(); strlen = RQ.shift32();
var reason = RQ.shiftStr(strlen); reason = RQ.shiftStr(strlen);
RFB.updateState('failed', "Disconnected: auth failure: " + reason); RFB.updateState('failed',
"Disconnected: auth failure: " + reason);
return; return;
case 1: // no authentication case 1: // no authentication
// RFB.send_array([RFB.shared]); // ClientInitialisation // RFB.send_array([RFB.shared]); // ClientInitialisation
...@@ -254,18 +270,22 @@ init_msg: function () { ...@@ -254,18 +270,22 @@ init_msg: function () {
console.log(" waiting for auth challenge bytes"); console.log(" waiting for auth challenge bytes");
return; return;
} }
var challenge = RQ.shiftBytes(16); challenge = RQ.shiftBytes(16);
//console.log("Password: " + RFB.password); //console.log("Password: " + RFB.password);
//console.log("Challenge: " + challenge + " (" + challenge.length + ")"); //console.log("Challenge: " + challenge +
var response = RFB.DES(RFB.password, challenge); // " (" + challenge.length + ")");
//console.log("Response: " + response + " (" + response.length + ")"); response = RFB.DES(RFB.password, challenge);
//console.log("Response: " + response +
// " (" + response.length + ")");
console.log("Sending DES encrypted auth response"); console.log("Sending DES encrypted auth response");
RFB.send_array(response); RFB.send_array(response);
RFB.updateState('SecurityResult'); RFB.updateState('SecurityResult');
break; break;
default: default:
RFB.updateState('failed', "Disconnected: unsupported auth scheme: " + RFB.auth_scheme); RFB.updateState('failed',
"Disconnected: unsupported auth scheme: " +
RFB.auth_scheme);
return; return;
} }
break; break;
...@@ -275,22 +295,22 @@ init_msg: function () { ...@@ -275,22 +295,22 @@ init_msg: function () {
RFB.updateState('failed', "Invalid VNC auth response"); RFB.updateState('failed', "Invalid VNC auth response");
return; return;
} }
var resp = RQ.shift32(); switch (RQ.shift32()) {
switch (resp) {
case 0: // OK case 0: // OK
RFB.updateState('ServerInitialisation', "Authentication OK"); RFB.updateState('ServerInitialisation', "Authentication OK");
break; break;
case 1: // failed case 1: // failed
if (RFB.version == 3.8) { if (RFB.version === 3.8) {
var reason_len = RQ.shift32(); reason_len = RQ.shift32();
var reason = RQ.shiftStr(reason_len); reason = RQ.shiftStr(reason_len);
RFB.updateState('failed', reason); RFB.updateState('failed', reason);
} else if (RFB.version == 3.3) { } else if (RFB.version === 3.3) {
RFB.updateState('failed', "Authentication failed"); RFB.updateState('failed', "Authentication failed");
} }
return; return;
case 2: // too-many case 2: // too-many
RFB.updateState('failed', "Disconnected: too many auth attempts"); RFB.updateState('failed',
"Disconnected: too many auth attempts");
return; return;
} }
RFB.send_array([RFB.shared]); // ClientInitialisation RFB.send_array([RFB.shared]); // ClientInitialisation
...@@ -307,10 +327,10 @@ init_msg: function () { ...@@ -307,10 +327,10 @@ init_msg: function () {
RFB.fb_height = RQ.shift16(); RFB.fb_height = RQ.shift16();
/* PIXEL_FORMAT */ /* PIXEL_FORMAT */
var bpp = RQ.shift8(); bpp = RQ.shift8();
var depth = RQ.shift8(); depth = RQ.shift8();
var big_endian = RQ.shift8(); big_endian = RQ.shift8();
var true_color = RQ.shift8(); true_color = RQ.shift8();
console.log("Screen: " + RFB.fb_width + "x" + RFB.fb_height + console.log("Screen: " + RFB.fb_width + "x" + RFB.fb_height +
", bpp: " + bpp + ", depth: " + depth + ", bpp: " + bpp + ", depth: " + depth +
...@@ -319,18 +339,17 @@ init_msg: function () { ...@@ -319,18 +339,17 @@ init_msg: function () {
/* Connection name/title */ /* Connection name/title */
RQ.shiftStr(12); RQ.shiftStr(12);
var name_length = RQ.shift32(); name_length = RQ.shift32();
RFB.fb_name = RQ.shiftStr(name_length); RFB.fb_name = RQ.shiftStr(name_length);
Canvas.init('VNC_canvas', RFB.fb_width, RFB.fb_height, Canvas.init('VNC_canvas', RFB.fb_width, RFB.fb_height,
RFB.keyDown, RFB.keyUp, RFB.keyDown, RFB.keyUp,
RFB.mouseDown, RFB.mouseUp, RFB.mouseMove); RFB.mouseDown, RFB.mouseUp, RFB.mouseMove);
var init = []; response = RFB.pixelFormat();
init = init.concat(RFB.pixelFormat()); response = response.concat(RFB.encodings());
init = init.concat(RFB.encodings()); response = response.concat(RFB.fbUpdateRequest(0));
init = init.concat(RFB.fbUpdateRequest(0)); RFB.send_array(response);
RFB.send_array(init);
/* Start pushing/polling */ /* Start pushing/polling */
RFB.checkEvents.delay(RFB.check_rate); RFB.checkEvents.delay(RFB.check_rate);
...@@ -345,17 +364,18 @@ init_msg: function () { ...@@ -345,17 +364,18 @@ init_msg: function () {
/* Normal RFB/VNC server messages */ /* Normal RFB/VNC server messages */
normal_msg: function () { normal_msg: function () {
//console.log(">> normal_msg"); //console.log(">> normal_msg");
var ret = true;
var ret = true, msg_type, num_colours, msg;
if (FBU.rects > 0) { if (FBU.rects > 0) {
var msg_type = 0; msg_type = 0;
} else if (RFB.cuttext != 'none') { } else if (RFB.cuttext !== 'none') {
var msg_type = 3; msg_type = 3;
} else { } else {
var msg_type = RQ.shift8(); msg_type = RQ.shift8();
} }
switch (msg_type) { switch (msg_type) {
case 0: // FramebufferUpdate case 0: // FramebufferUpdate
if (FBU.rects == 0) { if (FBU.rects === 0) {
if (RQ.length < 3) { if (RQ.length < 3) {
RQ.unshift(msg_type); RQ.unshift(msg_type);
console.log(" waiting for FBU header bytes"); console.log(" waiting for FBU header bytes");
...@@ -369,7 +389,7 @@ normal_msg: function () { ...@@ -369,7 +389,7 @@ normal_msg: function () {
} }
while ((FBU.rects > 0) && (RQ.length >= FBU.bytes)) { while ((FBU.rects > 0) && (RQ.length >= FBU.bytes)) {
if (FBU.bytes == 0) { if (FBU.bytes === 0) {
if (RQ.length < 12) { if (RQ.length < 12) {
console.log(" waiting for rect header bytes"); console.log(" waiting for rect header bytes");
return false; return false;
...@@ -383,14 +403,17 @@ normal_msg: function () { ...@@ -383,14 +403,17 @@ normal_msg: function () {
// Debug: // Debug:
/* /*
var msg = "FramebufferUpdate rects:" + FBU.rects + " encoding:" + FBU.encoding msg = "FramebufferUpdate rects:" + FBU.rects +
" encoding:" + FBU.encoding
switch (FBU.encoding) { switch (FBU.encoding) {
case 0: msg += "(RAW)"; break; case 0: msg += "(RAW)"; break;
case 1: msg += "(COPY-RECT)"; break; case 1: msg += "(COPY-RECT)"; break;
case 2: msg += "(RRE)"; break; case 2: msg += "(RRE)"; break;
case 5: msg += "(HEXTILE " + FBU.tiles + " tiles)"; break; case 5: msg += "(HEXTILE " + FBU.tiles + " tiles)"; break;
default: default:
RFB.updateState('failed', "Disconnected: unsupported encoding " + FBU.encoding); RFB.updateState('failed',
"Disconnected: unsupported encoding " +
FBU.encoding);
return false; return false;
} }
msg += ", RQ.length: " + RQ.length msg += ", RQ.length: " + RQ.length
...@@ -398,22 +421,29 @@ normal_msg: function () { ...@@ -398,22 +421,29 @@ normal_msg: function () {
*/ */
} }
RFB.timing.last_fbu = (new Date).getTime(); RFB.timing.last_fbu = (new Date()).getTime();
switch (FBU.encoding) { switch (FBU.encoding) {
case 0: ret = RFB.display_raw(); break; // Raw case 0: ret = RFB.display_raw(); break; // Raw
case 1: ret = RFB.display_copy_rect(); break; // Copy-Rect case 1: ret = RFB.display_copy_rect(); break; // Copy-Rect
case 2: ret = RFB.display_rre(); break; // RRE case 2: ret = RFB.display_rre(); break; // RRE
case 5: ret = RFB.display_hextile(); break; // hextile case 5: ret = RFB.display_hextile(); break; // hextile
} }
RFB.timing.cur_fbu += ((new Date).getTime() - RFB.timing.last_fbu); RFB.timing.cur_fbu += ((new Date()).getTime() -
if (FBU.rects == 0) { RFB.timing.last_fbu);
if ((FBU.width === RFB.fb_width) && (FBU.height === RFB.fb_height)) { if (FBU.rects === 0) {
if ((FBU.width === RFB.fb_width) &&
(FBU.height === RFB.fb_height)) {
RFB.timing.full_fbu_total += RFB.timing.cur_fbu; RFB.timing.full_fbu_total += RFB.timing.cur_fbu;
RFB.timing.full_fbu_cnt += 1; RFB.timing.full_fbu_cnt += 1;
console.log("Timing of full FBU, cur: " + RFB.timing.cur_fbu + ", total: " + RFB.timing.full_fbu_total + ", cnt: " + RFB.timing.full_fbu_cnt + ", avg: " + (RFB.timing.full_fbu_total / RFB.timing.full_fbu_cnt)); console.log("Timing of full FBU, cur: " +
RFB.timing.cur_fbu + ", total: " +
RFB.timing.full_fbu_total + ", cnt: " +
RFB.timing.full_fbu_cnt + ", avg: " +
(RFB.timing.full_fbu_total /
RFB.timing.full_fbu_cnt));
} }
} }
if (RFB.state != "normal") return true; if (RFB.state !== "normal") { return true; }
} }
break; break;
...@@ -421,7 +451,7 @@ normal_msg: function () { ...@@ -421,7 +451,7 @@ normal_msg: function () {
console.log("SetColourMapEntries (unsupported)"); console.log("SetColourMapEntries (unsupported)");
RQ.shift8(); // Padding RQ.shift8(); // Padding
RQ.shift16(); // First colour RQ.shift16(); // First colour
var num_colours = RQ.shift16(); num_colours = RQ.shift16();
RQ.shiftBytes(num_colours * 6); RQ.shiftBytes(num_colours * 6);
break; break;
case 2: // Bell case 2: // Bell
...@@ -430,10 +460,10 @@ normal_msg: function () { ...@@ -430,10 +460,10 @@ normal_msg: function () {
case 3: // ServerCutText case 3: // ServerCutText
console.log("ServerCutText"); console.log("ServerCutText");
console.log("RQ:" + RQ.slice(0,20)); console.log("RQ:" + RQ.slice(0,20));
if (RFB.cuttext == 'none') { if (RFB.cuttext === 'none') {
RFB.cuttext = 'header'; RFB.cuttext = 'header';
} }
if (RFB.cuttext == 'header') { if (RFB.cuttext === 'header') {
if (RQ.length < 7) { if (RQ.length < 7) {
console.log("waiting for ServerCutText header"); console.log("waiting for ServerCutText header");
return false; return false;
...@@ -450,7 +480,8 @@ normal_msg: function () { ...@@ -450,7 +480,8 @@ normal_msg: function () {
RFB.cuttext = 'none'; RFB.cuttext = 'none';
break; break;
default: default:
RFB.updateState('failed', "Disconnected: illegal server message type " + msg_type); RFB.updateState('failed',
"Disconnected: illegal server message type " + msg_type);
console.log("RQ.slice(0,30):" + RQ.slice(0,30)); console.log("RQ.slice(0,30):" + RQ.slice(0,30));
break; break;
} }
...@@ -465,16 +496,21 @@ normal_msg: function () { ...@@ -465,16 +496,21 @@ normal_msg: function () {
display_raw: function () { display_raw: function () {
//console.log(">> display_raw"); //console.log(">> display_raw");
if (FBU.lines == 0) {
var cur_y, cur_height;
if (FBU.lines === 0) {
FBU.lines = FBU.height; FBU.lines = FBU.height;
} }
FBU.bytes = FBU.width * RFB.fb_Bpp; // At least a line FBU.bytes = FBU.width * RFB.fb_Bpp; // At least a line
if (RQ.length < FBU.bytes) { if (RQ.length < FBU.bytes) {
//console.log(" waiting for " + (FBU.bytes - RQ.length) + " RAW bytes"); //console.log(" waiting for " +
// (FBU.bytes - RQ.length) + " RAW bytes");
return; return;
} }
var cur_y = FBU.y + (FBU.height - FBU.lines); cur_y = FBU.y + (FBU.height - FBU.lines);
var cur_height = Math.min(FBU.lines, Math.floor(RQ.length/(FBU.width * RFB.fb_Bpp))); cur_height = Math.min(FBU.lines,
Math.floor(RQ.length/(FBU.width * RFB.fb_Bpp)));
Canvas.rgbxImage(FBU.x, cur_y, FBU.width, cur_height, RQ); Canvas.rgbxImage(FBU.x, cur_y, FBU.width, cur_height, RQ);
RQ.shiftBytes(FBU.width * cur_height * RFB.fb_Bpp); RQ.shiftBytes(FBU.width * cur_height * RFB.fb_Bpp);
FBU.lines -= cur_height; FBU.lines -= cur_height;
...@@ -489,12 +525,16 @@ display_raw: function () { ...@@ -489,12 +525,16 @@ display_raw: function () {
display_copy_rect: function () { display_copy_rect: function () {
//console.log(">> display_copy_rect"); //console.log(">> display_copy_rect");
var old_x, old_y;
if (RQ.length < 4) { if (RQ.length < 4) {
//console.log(" waiting for " + (FBU.bytes - RQ.length) + " COPY-RECT bytes"); //console.log(" waiting for " +
// (FBU.bytes - RQ.length) + " COPY-RECT bytes");
return; return;
} }
var old_x = RQ.shift16(); old_x = RQ.shift16();
var old_y = RQ.shift16(); old_y = RQ.shift16();
Canvas.copyImage(old_x, old_y, FBU.x, FBU.y, FBU.width, FBU.height); Canvas.copyImage(old_x, old_y, FBU.x, FBU.y, FBU.width, FBU.height);
FBU.rects --; FBU.rects --;
FBU.bytes = 0; FBU.bytes = 0;
...@@ -502,29 +542,31 @@ display_copy_rect: function () { ...@@ -502,29 +542,31 @@ display_copy_rect: function () {
display_rre: function () { display_rre: function () {
//console.log(">> display_rre (" + RQ.length + " bytes)"); //console.log(">> display_rre (" + RQ.length + " bytes)");
if (FBU.subrects == 0) { var color, x, y, width, height, chunk;
; if (FBU.subrects === 0) {
if (RQ.length < 4 + RFB.fb_Bpp) { if (RQ.length < 4 + RFB.fb_Bpp) {
//console.log(" waiting for " + (4 + RFB.fb_Bpp - RQ.length) + " RRE bytes"); //console.log(" waiting for " +
// (4 + RFB.fb_Bpp - RQ.length) + " RRE bytes");
return; return;
} }
FBU.subrects = RQ.shift32(); FBU.subrects = RQ.shift32();
var color = RQ.shiftBytes(RFB.fb_Bpp); // Background color = RQ.shiftBytes(RFB.fb_Bpp); // Background
Canvas.fillRect(FBU.x, FBU.y, FBU.width, FBU.height, color); Canvas.fillRect(FBU.x, FBU.y, FBU.width, FBU.height, color);
} }
while ((FBU.subrects > 0) && (RQ.length >= (RFB.fb_Bpp + 8))) { while ((FBU.subrects > 0) && (RQ.length >= (RFB.fb_Bpp + 8))) {
var color = RQ.shiftBytes(RFB.fb_Bpp); color = RQ.shiftBytes(RFB.fb_Bpp);
var x = RQ.shift16(); x = RQ.shift16();
var y = RQ.shift16(); y = RQ.shift16();
var width = RQ.shift16(); width = RQ.shift16();
var height = RQ.shift16(); height = RQ.shift16();
Canvas.fillRect(FBU.x + x, FBU.y + y, width, height, color); Canvas.fillRect(FBU.x + x, FBU.y + y, width, height, color);
FBU.subrects --; FBU.subrects --;
} }
//console.log(" display_rre: rects: " + FBU.rects + ", FBU.subrects: " + FBU.subrects); //console.log(" display_rre: rects: " + FBU.rects +
// ", FBU.subrects: " + FBU.subrects);
if (FBU.subrects > 0) { if (FBU.subrects > 0) {
var chunk = Math.min(RFB.rre_chunk, FBU.subrects); chunk = Math.min(RFB.rre_chunk, FBU.subrects);
FBU.bytes = (RFB.fb_Bpp + 8) * chunk; FBU.bytes = (RFB.fb_Bpp + 8) * chunk;
} else { } else {
FBU.rects --; FBU.rects --;
...@@ -535,9 +577,10 @@ display_rre: function () { ...@@ -535,9 +577,10 @@ display_rre: function () {
display_hextile: function() { display_hextile: function() {
//console.log(">> display_hextile"); //console.log(">> display_hextile");
var subencoding, subrects, cur_tile, tile_x, x, w, tile_y, y, h; var subencoding, subrects, cur_tile, tile_x, x, w, tile_y, y, h,
idx, tile, xy, s, sx, sy, wh, sw, sh, color;
if (FBU.tiles == 0) { if (FBU.tiles === 0) {
FBU.tiles_x = Math.ceil(FBU.width/16); FBU.tiles_x = Math.ceil(FBU.width/16);
FBU.tiles_y = Math.ceil(FBU.height/16); FBU.tiles_y = Math.ceil(FBU.height/16);
FBU.total_tiles = FBU.tiles_x * FBU.tiles_y; FBU.total_tiles = FBU.tiles_x * FBU.tiles_y;
...@@ -553,7 +596,8 @@ display_hextile: function() { ...@@ -553,7 +596,8 @@ display_hextile: function() {
} }
subencoding = RQ[0]; // Peek subencoding = RQ[0]; // Peek
if (subencoding > 30) { // Raw if (subencoding > 30) { // Raw
RFB.updateState('failed', "Disconnected: illegal hextile subencoding " + subencoding); RFB.updateState('failed',
"Disconnected: illegal hextile subencoding " + subencoding);
console.log("RQ.slice(0,30):" + RQ.slice(0,30)); console.log("RQ.slice(0,30):" + RQ.slice(0,30));
return; return;
} }
...@@ -563,8 +607,8 @@ display_hextile: function() { ...@@ -563,8 +607,8 @@ display_hextile: function() {
tile_y = Math.floor(cur_tile / FBU.tiles_x); tile_y = Math.floor(cur_tile / FBU.tiles_x);
x = FBU.x + tile_x * 16; x = FBU.x + tile_x * 16;
y = FBU.y + tile_y * 16; y = FBU.y + tile_y * 16;
w = Math.min(16, (FBU.x + FBU.width) - x) w = Math.min(16, (FBU.x + FBU.width) - x);
h = Math.min(16, (FBU.y + FBU.height) - y) h = Math.min(16, (FBU.y + FBU.height) - y);
/* Figure out how much we are expecting */ /* Figure out how much we are expecting */
if (subencoding & 0x01) { // Raw if (subencoding & 0x01) { // Raw
...@@ -593,16 +637,24 @@ display_hextile: function() { ...@@ -593,16 +637,24 @@ display_hextile: function() {
} }
} }
//console.log(" tile:" + cur_tile + "/" + (FBU.total_tiles - 1) + ", subencoding:" + subencoding + "(last: " + FBU.lastsubencoding + "), subrects:" + subrects + ", tile:" + tile_x + "," + tile_y + " [" + x + "," + y + "]@" + w + "x" + h + ", d.length:" + RQ.length + ", bytes:" + FBU.bytes + " last:" + RQ.slice(FBU.bytes-10, FBU.bytes) + " next:" + RQ.slice(FBU.bytes-1, FBU.bytes+10)); //console.log(" tile:" + cur_tile + "/" + (FBU.total_tiles - 1) +
// ", subencoding:" + subencoding +
// "(last: " + FBU.lastsubencoding + "), subrects:" +
// subrects + ", tile:" + tile_x + "," + tile_y +
// " [" + x + "," + y + "]@" + w + "x" + h +
// ", d.length:" + RQ.length + ", bytes:" + FBU.bytes +
// " last:" + RQ.slice(FBU.bytes-10, FBU.bytes) +
// " next:" + RQ.slice(FBU.bytes-1, FBU.bytes+10));
if (RQ.length < FBU.bytes) { if (RQ.length < FBU.bytes) {
//console.log(" waiting for " + (FBU.bytes - RQ.length) + " hextile bytes"); //console.log(" waiting for " +
// (FBU.bytes - RQ.length) + " hextile bytes");
return; return;
} }
/* We know the encoding and have a whole tile */ /* We know the encoding and have a whole tile */
FBU.subencoding = RQ.shift8(); FBU.subencoding = RQ.shift8();
FBU.bytes--; FBU.bytes--;
if (FBU.subencoding == 0) { if (FBU.subencoding === 0) {
if (FBU.lastsubencoding & 0x01) { if (FBU.lastsubencoding & 0x01) {
/* Weird: ignore blanks after RAW */ /* Weird: ignore blanks after RAW */
console.log(" Ignoring blank after RAW"); console.log(" Ignoring blank after RAW");
...@@ -612,7 +664,7 @@ display_hextile: function() { ...@@ -612,7 +664,7 @@ display_hextile: function() {
} else if (FBU.subencoding & 0x01) { // Raw } else if (FBU.subencoding & 0x01) { // Raw
Canvas.rgbxImage(x, y, w, h, RQ); Canvas.rgbxImage(x, y, w, h, RQ);
} else { } else {
var idx = 0; idx = 0;
if (FBU.subencoding & 0x02) { // Background if (FBU.subencoding & 0x02) { // Background
FBU.background = RQ.slice(idx, idx + RFB.fb_Bpp); FBU.background = RQ.slice(idx, idx + RFB.fb_Bpp);
idx += RFB.fb_Bpp; idx += RFB.fb_Bpp;
...@@ -622,12 +674,11 @@ display_hextile: function() { ...@@ -622,12 +674,11 @@ display_hextile: function() {
idx += RFB.fb_Bpp; idx += RFB.fb_Bpp;
} }
var tile = Canvas.getTile(x, y, w, h, FBU.background); tile = Canvas.getTile(x, y, w, h, FBU.background);
if (FBU.subencoding & 0x08) { // AnySubrects if (FBU.subencoding & 0x08) { // AnySubrects
subrects = RQ[idx]; subrects = RQ[idx];
idx++; idx++;
var xy, sx, sy, wh, sw, sh; for (s = 0; s < subrects; s ++) {
for (var s = 0; s < subrects; s ++) {
if (FBU.subencoding & 0x10) { // SubrectsColoured if (FBU.subencoding & 0x10) { // SubrectsColoured
color = RQ.slice(idx, idx + RFB.fb_Bpp); color = RQ.slice(idx, idx + RFB.fb_Bpp);
idx += RFB.fb_Bpp; idx += RFB.fb_Bpp;
...@@ -655,7 +706,7 @@ display_hextile: function() { ...@@ -655,7 +706,7 @@ display_hextile: function() {
FBU.tiles --; FBU.tiles --;
} }
if (FBU.tiles == 0) { if (FBU.tiles === 0) {
FBU.rects --; FBU.rects --;
} }
...@@ -717,10 +768,10 @@ encodings: function () { ...@@ -717,10 +768,10 @@ encodings: function () {
fbUpdateRequest: function (incremental, x, y, xw, yw) { fbUpdateRequest: function (incremental, x, y, xw, yw) {
//console.log(">> fbUpdateRequest"); //console.log(">> fbUpdateRequest");
if (!x) x = 0; if (!x) { x = 0; }
if (!y) y = 0; if (!y) { y = 0; }
if (!xw) xw = RFB.fb_width; if (!xw) { xw = RFB.fb_width; }
if (!yw) yw = RFB.fb_height; if (!yw) { yw = RFB.fb_height; }
var arr; var arr;
arr = [3]; // msg-type arr = [3]; // msg-type
arr.push8(incremental); arr.push8(incremental);
...@@ -744,7 +795,8 @@ keyEvent: function (keysym, down) { ...@@ -744,7 +795,8 @@ keyEvent: function (keysym, down) {
}, },
pointerEvent: function (x, y) { pointerEvent: function (x, y) {
//console.log(">> pointerEvent, x,y: " + x + "," + y + " , mask: " + RFB.mouse_buttonMask); //console.log(">> pointerEvent, x,y: " + x + "," + y +
// " , mask: " + RFB.mouse_buttonMask);
var arr; var arr;
arr = [5]; // msg-type arr = [5]; // msg-type
arr.push8(RFB.mouse_buttonMask); arr.push8(RFB.mouse_buttonMask);
...@@ -783,12 +835,12 @@ recv_message: function(e) { ...@@ -783,12 +835,12 @@ recv_message: function(e) {
RFB.handle_message(); RFB.handle_message();
} }
} catch (e) { } catch (exc) {
console.log("recv_message, caught exception: " + e); console.log("recv_message, caught exception: " + exc);
if (typeof e.name !== 'undefined') { if (typeof exc.name !== 'undefined') {
RFB.updateState('failed', e.name + ": " + e.message); RFB.updateState('failed', exc.name + ": " + exc.message);
} else { } else {
RFB.updateState('failed', e); RFB.updateState('failed', exc);
} }
} }
//console.log("<< recv_message"); //console.log("<< recv_message");
...@@ -797,27 +849,32 @@ recv_message: function(e) { ...@@ -797,27 +849,32 @@ recv_message: function(e) {
recv_message_reorder: function(e) { recv_message_reorder: function(e) {
//console.log(">> recv_message_reorder"); //console.log(">> recv_message_reorder");
var offset = e.data.indexOf(":") + 1; var offset, seq_num, i;
var seq_num = parseInt(e.data.substr(0, offset-1));
if (RQ_seq_num == seq_num) { offset = e.data.indexOf(":") + 1;
seq_num = parseInt(e.data.substr(0, offset-1), 10);
if (RQ_seq_num === seq_num) {
RQ = RQ.concat(Base64.decode(e.data, offset)); RQ = RQ.concat(Base64.decode(e.data, offset));
RQ_seq_num++; RQ_seq_num++;
} else { } else {
console.warn("sequence number mismatch: expected " + RQ_seq_num + ", got " + seq_num); console.warn("sequence number mismatch: expected " +
RQ_seq_num + ", got " + seq_num);
if (RQ_reorder.length > 20) { if (RQ_reorder.length > 20) {
RFB.updateState('failed', "Re-order queue too long"); RFB.updateState('failed', "Re-order queue too long");
} else { } else {
RQ_reorder = RQ_reorder.concat(e.data.substr(0)); RQ_reorder = RQ_reorder.concat(e.data.substr(0));
var i = 0; i = 0;
while (i < RQ_reorder.length) { while (i < RQ_reorder.length) {
var offset = RQ_reorder[i].indexOf(":") + 1; offset = RQ_reorder[i].indexOf(":") + 1;
var seq_num = parseInt(RQ_reorder[i].substr(0, offset-1)); seq_num = parseInt(RQ_reorder[i].substr(0, offset-1), 10);
//console.log("Searching reorder list item " + i + ", seq_num " + seq_num); //console.log("Searching reorder list item " +
if (seq_num == RQ_seq_num) { // i + ", seq_num " + seq_num);
if (seq_num === RQ_seq_num) {
/* Remove it from reorder queue, decode it and /* Remove it from reorder queue, decode it and
* add it to the receive queue */ * add it to the receive queue */
console.log("Found re-ordered packet seq_num " + seq_num); console.log("Found re-ordered packet seq_num " + seq_num);
RQ = RQ.concat(Base64.decode(RQ_reorder.splice(i, 1)[0], offset)); RQ = RQ.concat(Base64.decode(RQ_reorder.splice(i, 1)[0],
offset));
RQ_seq_num++; RQ_seq_num++;
i = 0; // Start search again for next one i = 0; // Start search again for next one
} else { } else {
...@@ -845,7 +902,7 @@ handle_message: function () { ...@@ -845,7 +902,7 @@ handle_message: function () {
RFB.normal_msg(); RFB.normal_msg();
/* /*
while (RQ.length > 0) { while (RQ.length > 0) {
if (RFB.normal_msg() && RFB.state == 'normal') { if (RFB.normal_msg() && RFB.state === 'normal') {
console.log("More to process"); console.log("More to process");
} else { } else {
break; break;
...@@ -862,25 +919,26 @@ handle_message: function () { ...@@ -862,25 +919,26 @@ handle_message: function () {
send_string: function (str) { send_string: function (str) {
//console.log(">> send_string: " + str); //console.log(">> send_string: " + str);
RFB.send_array(str.split('').map( RFB.send_array(str.split('').map(
function (chr) { return chr.charCodeAt(0) } ) ); function (chr) { return chr.charCodeAt(0); } ) );
}, },
send_array: function (arr) { send_array: function (arr) {
//console.log(">> send_array: " + arr); //console.log(">> send_array: " + arr);
//console.log(">> send_array: " + Base64.encode(arr)); //console.log(">> send_array: " + Base64.encode(arr));
SQ = SQ + Base64.encode(arr); SQ = SQ + Base64.encode(arr);
if (RFB.ws.bufferedAmount == 0) { if (RFB.ws.bufferedAmount === 0) {
RFB.ws.send(SQ); RFB.ws.send(SQ);
SQ = "" SQ = "";
} else { } else {
console.log("Delaying send"); console.log("Delaying send");
} }
}, },
DES: function (password, challenge) { DES: function (password, challenge) {
var passwd = []; var i, passwd, response;
var response = challenge.slice(); passwd = [];
for (var i=0; i < password.length; i++) { response = challenge.slice();
for (i=0; i < password.length; i++) {
passwd.push(password.charCodeAt(i)); passwd.push(password.charCodeAt(i));
} }
...@@ -891,10 +949,9 @@ DES: function (password, challenge) { ...@@ -891,10 +949,9 @@ DES: function (password, challenge) {
}, },
flushClient: function () { flushClient: function () {
var arr = [];
if (RFB.mouse_arr.length > 0) { if (RFB.mouse_arr.length > 0) {
//RFB.send_array(RFB.mouse_arr.concat(RFB.fbUpdateRequest(1))); //RFB.send_array(RFB.mouse_arr.concat(RFB.fbUpdateRequest(1)));
RFB.send_array(RFB.mouse_arr) RFB.send_array(RFB.mouse_arr);
setTimeout(function() { setTimeout(function() {
RFB.send_array(RFB.fbUpdateRequest(1)); RFB.send_array(RFB.fbUpdateRequest(1));
}, 50); }, 50);
...@@ -907,9 +964,10 @@ flushClient: function () { ...@@ -907,9 +964,10 @@ flushClient: function () {
}, },
checkEvents: function () { checkEvents: function () {
if (RFB.state == 'normal') { var now;
if (RFB.state === 'normal') {
if (! RFB.flushClient()) { if (! RFB.flushClient()) {
var now = new Date().getTime(); now = new Date().getTime();
if (now > RFB.last_req + RFB.req_rate) { if (now > RFB.last_req + RFB.req_rate) {
RFB.last_req = now; RFB.last_req = now;
RFB.send_array(RFB.fbUpdateRequest(1)); RFB.send_array(RFB.fbUpdateRequest(1));
...@@ -919,32 +977,34 @@ checkEvents: function () { ...@@ -919,32 +977,34 @@ checkEvents: function () {
RFB.checkEvents.delay(RFB.check_rate); RFB.checkEvents.delay(RFB.check_rate);
}, },
_keyX: function (e, down) { keyX: function (e, down) {
var arr;
if (RFB.clipboardFocus) { if (RFB.clipboardFocus) {
return true; return true;
} }
e.stop(); e.stop();
var arr = RFB.keyEvent(Canvas.getKeysym(e), down); arr = RFB.keyEvent(Canvas.getKeysym(e), down);
arr = arr.concat(RFB.fbUpdateRequest(1)); arr = arr.concat(RFB.fbUpdateRequest(1));
RFB.send_array(arr); RFB.send_array(arr);
}, },
keyDown: function (e) { keyDown: function (e) {
//console.log(">> keyDown: " + Canvas.getKeysym(e)); //console.log(">> keyDown: " + Canvas.getKeysym(e));
RFB._keyX(e, 1); RFB.keyX(e, 1);
}, },
keyUp: function (e) { keyUp: function (e) {
//console.log(">> keyUp: " + Canvas.getKeysym(e)); //console.log(">> keyUp: " + Canvas.getKeysym(e));
RFB._keyX(e, 0); RFB.keyX(e, 0);
}, },
mouseDown: function(e) { mouseDown: function(e) {
var evt = e.event || window.event; var evt, x, y;
var x, y; evt = e.event || window.event;
x = (evt.clientX - Canvas.c_x); x = (evt.clientX - Canvas.c_x);
y = (evt.clientY - Canvas.c_y); y = (evt.clientY - Canvas.c_y);
//console.log('>> mouseDown ' + evt.which + '/' + evt.button + " " + x + "," + y); //console.log(">> mouseDown " + evt.which + "/" + evt.button +
// " " + x + "," + y);
RFB.mouse_buttonMask |= 1 << evt.button; RFB.mouse_buttonMask |= 1 << evt.button;
RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) ); RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) );
...@@ -952,11 +1012,12 @@ mouseDown: function(e) { ...@@ -952,11 +1012,12 @@ mouseDown: function(e) {
}, },
mouseUp: function(e) { mouseUp: function(e) {
var evt = e.event || window.event; var evt, x, y;
var x, y; evt = e.event || window.event;
x = (evt.clientX - Canvas.c_x); x = (evt.clientX - Canvas.c_x);
y = (evt.clientY - Canvas.c_y); y = (evt.clientY - Canvas.c_y);
//console.log('>> mouseUp ' + evt.which + '/' + evt.button + " " + x + "," + y); //console.log(">> mouseUp " + evt.which + "/" + evt.button +
// " " + x + "," + y);
RFB.mouse_buttonMask ^= 1 << evt.button; RFB.mouse_buttonMask ^= 1 << evt.button;
RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) ); RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) );
...@@ -964,8 +1025,8 @@ mouseUp: function(e) { ...@@ -964,8 +1025,8 @@ mouseUp: function(e) {
}, },
mouseMove: function(e) { mouseMove: function(e) {
var evt = e.event || window.event; var evt, x, y;
var x, y; evt = e.event || window.event;
x = (evt.clientX - Canvas.c_x); x = (evt.clientX - Canvas.c_x);
y = (evt.clientY - Canvas.c_y); y = (evt.clientY - Canvas.c_y);
//console.log('>> mouseMove ' + x + "," + y); //console.log('>> mouseMove ' + x + "," + y);
...@@ -979,8 +1040,9 @@ clipboardCopyTo: function (text) { ...@@ -979,8 +1040,9 @@ clipboardCopyTo: function (text) {
}, },
clipboardPasteFrom: function () { clipboardPasteFrom: function () {
if (RFB.state != "normal") return; var text;
var text = RFB.clipboard.value; if (RFB.state !== "normal") { return; }
text = RFB.clipboard.value;
console.log(">> clipboardPasteFrom: " + text.substr(0,40) + "..."); console.log(">> clipboardPasteFrom: " + text.substr(0,40) + "...");
RFB.send_array(RFB.clientCutText(text)); RFB.send_array(RFB.clientCutText(text));
console.log("<< clipboardPasteFrom"); console.log("<< clipboardPasteFrom");
...@@ -992,13 +1054,13 @@ clipboardClear: function () { ...@@ -992,13 +1054,13 @@ clipboardClear: function () {
}, },
updateState: function(state, statusMsg) { updateState: function(state, statusMsg) {
var s = RFB.statusLine; var s, c, func, color, cmsg;
var c = RFB.connectBtn; s = RFB.statusLine;
var func = function(msg) { console.log(msg) }; c = RFB.connectBtn;
var color; func = function(msg) { console.log(msg); };
switch (state) { switch (state) {
case 'failed': case 'failed':
func = function(msg) { console.error(msg) }; func = function(msg) { console.error(msg); };
c.disabled = true; c.disabled = true;
color = "#880000"; color = "#880000";
break; break;
...@@ -1010,13 +1072,13 @@ updateState: function(state, statusMsg) { ...@@ -1010,13 +1072,13 @@ updateState: function(state, statusMsg) {
break; break;
case 'disconnected': case 'disconnected':
c.value = "Connect"; c.value = "Connect";
c.onclick = function () { RFB.connect(); }, c.onclick = function () { RFB.connect(); };
c.disabled = false; c.disabled = false;
color = "#000000"; color = "#000000";
break; break;
default: default:
func = function(msg) { console.warn(msg) }; func = function(msg) { console.warn(msg); };
c.disabled = true; c.disabled = true;
color = "#444400"; color = "#444400";
break; break;
...@@ -1030,9 +1092,9 @@ updateState: function(state, statusMsg) { ...@@ -1030,9 +1092,9 @@ updateState: function(state, statusMsg) {
RFB.state = state; RFB.state = state;
s.style.color = color; s.style.color = color;
var cmsg = typeof(statusMsg) != 'undefined' ? (" Msg: " + statusMsg) : ""; cmsg = typeof(statusMsg) !== 'undefined' ? (" Msg: " + statusMsg) : "";
func("New state '" + state + "'." + cmsg); func("New state '" + state + "'." + cmsg);
if (typeof(statusMsg) != 'undefined') { if (typeof(statusMsg) !== 'undefined') {
s.innerHTML = statusMsg; s.innerHTML = statusMsg;
} }
}, },
...@@ -1042,8 +1104,8 @@ updateState: function(state, statusMsg) { ...@@ -1042,8 +1104,8 @@ updateState: function(state, statusMsg) {
*/ */
init_ws: function () { init_ws: function () {
console.log(">> init_ws"); console.log(">> init_ws");
var uri = ""; var uri = "";
if (RFB.encrypt) { if (RFB.encrypt) {
uri = "wss://"; uri = "wss://";
...@@ -1066,7 +1128,7 @@ init_ws: function () { ...@@ -1066,7 +1128,7 @@ init_ws: function () {
* Send updates either at a rate of one update every 50ms, * Send updates either at a rate of one update every 50ms,
* or whatever slower rate the network can handle * or whatever slower rate the network can handle
*/ */
if (RFB.ws.bufferedAmount == 0) { if (RFB.ws.bufferedAmount === 0) {
if (SQ) { if (SQ) {
RFB.ws.send(SQ); RFB.ws.send(SQ);
SQ = ""; SQ = "";
...@@ -1102,8 +1164,8 @@ init_vars: function () { ...@@ -1102,8 +1164,8 @@ init_vars: function () {
SQ = ""; SQ = "";
FBU.rects = 0; FBU.rects = 0;
FBU.subrects = 0; // RRE and HEXTILE FBU.subrects = 0; // RRE and HEXTILE
FBU.lines = 0, // RAW FBU.lines = 0; // RAW
FBU.tiles = 0, // HEXTILE FBU.tiles = 0; // HEXTILE
RFB.mouse_buttonmask = 0; RFB.mouse_buttonmask = 0;
RFB.mouse_arr = []; RFB.mouse_arr = [];
}, },
...@@ -1112,10 +1174,14 @@ init_vars: function () { ...@@ -1112,10 +1174,14 @@ init_vars: function () {
connect: function (host, port, password, encrypt) { connect: function (host, port, password, encrypt) {
console.log(">> connect"); console.log(">> connect");
RFB.host = (host !== undefined) ? host : $('VNC_host').value; RFB.host = (host !== undefined) ? host :
RFB.port = (port !== undefined) ? port : $('VNC_port').value; $('VNC_host').value;
RFB.password = (password !== undefined) ? password : $('VNC_password').value; RFB.port = (port !== undefined) ? port :
RFB.encrypt = (encrypt !== undefined) ? encrypt : $('VNC_encrypt').checked; $('VNC_port').value;
RFB.password = (password !== undefined) ? password :
$('VNC_password').value;
RFB.encrypt = (encrypt !== undefined) ? encrypt :
$('VNC_encrypt').checked;
if ((!RFB.host) || (!RFB.port)) { if ((!RFB.host) || (!RFB.port)) {
alert("Must set host and port"); alert("Must set host and port");
return; return;
...@@ -1123,7 +1189,7 @@ connect: function (host, port, password, encrypt) { ...@@ -1123,7 +1189,7 @@ connect: function (host, port, password, encrypt) {
RFB.init_vars(); RFB.init_vars();
if ((RFB.ws) && (RFB.ws.readyState == WebSocket.OPEN)) { if ((RFB.ws) && (RFB.ws.readyState === WebSocket.OPEN)) {
RFB.ws.close(); RFB.ws.close();
} }
RFB.init_ws(); RFB.init_ws();
...@@ -1135,9 +1201,9 @@ connect: function (host, port, password, encrypt) { ...@@ -1135,9 +1201,9 @@ connect: function (host, port, password, encrypt) {
disconnect: function () { disconnect: function () {
console.log(">> disconnect"); console.log(">> disconnect");
if ((RFB.ws) && (RFB.ws.readyState == WebSocket.OPEN)) { if ((RFB.ws) && (RFB.ws.readyState === WebSocket.OPEN)) {
RFB.updateState('closed'); RFB.updateState('closed');
RFB.ws.onmessage = function (e) { return; } RFB.ws.onmessage = function (e) { return; };
RFB.ws.close(); RFB.ws.close();
} }
if (Canvas.ctx) { if (Canvas.ctx) {
...@@ -1156,18 +1222,21 @@ disconnect: function () { ...@@ -1156,18 +1222,21 @@ disconnect: function () {
*/ */
load: function (target) { load: function (target) {
//console.log(">> load"); //console.log(">> load");
var html, url;
if (!target) { target = 'vnc' }; if (!target) { target = 'vnc'; }
/* Populate the 'vnc' div */ /* Populate the 'vnc' div */
var html = ""; html = "";
if ($('VNC_controls') === null) { if ($('VNC_controls') === null) {
html += '<div id="VNC_controls">'; html += '<div id="VNC_controls">';
html += ' <ul>'; html += ' <ul>';
html += ' <li>Host: <input id="VNC_host"></li>'; html += ' <li>Host: <input id="VNC_host"></li>';
html += ' <li>Port: <input id="VNC_port"></li>'; html += ' <li>Port: <input id="VNC_port"></li>';
html += ' <li>Password: <input id="VNC_password" type="password"></li>'; html += ' <li>Password: <input id="VNC_password"';
html += ' <li>Encrypt: <input id="VNC_encrypt" type="checkbox"></li>'; html += ' type="password"></li>';
html += ' <li>Encrypt: <input id="VNC_encrypt"';
html += ' type="checkbox"></li>';
html += ' <li><input id="VNC_connect_button" type="button"'; html += ' <li><input id="VNC_connect_button" type="button"';
html += ' value="Loading" disabled></li>'; html += ' value="Loading" disabled></li>';
html += ' </ul>'; html += ' </ul>';
...@@ -1181,7 +1250,8 @@ load: function (target) { ...@@ -1181,7 +1250,8 @@ load: function (target) {
html += '<br><br>'; html += '<br><br>';
html += '<div id="VNC_clipboard">'; html += '<div id="VNC_clipboard">';
html += ' VNC Clipboard:'; html += ' VNC Clipboard:';
html += ' <input id="VNC_clipboard_clear_button" type="button" value="Clear">'; html += ' <input id="VNC_clipboard_clear_button"';
html += ' type="button" value="Clear">';
html += ' <br>'; html += ' <br>';
html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5>'; html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5>';
html += ' </textarea>'; html += ' </textarea>';
...@@ -1220,8 +1290,9 @@ load: function (target) { ...@@ -1220,8 +1290,9 @@ load: function (target) {
if ((! Browser.Plugins.Flash) || if ((! Browser.Plugins.Flash) ||
(Browser.Plugins.Flash.version < 9)) { (Browser.Plugins.Flash.version < 9)) {
RFB.updateState('failed', "WebSockets or Adobe Flash is required"); RFB.updateState('failed', "WebSockets or Adobe Flash is required");
} else if (location.href.substr(0, 7) == "file://") { } else if (location.href.substr(0, 7) === "file://") {
RFB.updateState('failed', "'file://' URL is incompatible with Adobe Flash"); RFB.updateState('failed',
"'file://' URL is incompatible with Adobe Flash");
} else { } else {
WebSocket.__swfLocation = "include/web-socket-js/WebSocketMain.swf"; WebSocket.__swfLocation = "include/web-socket-js/WebSocketMain.swf";
RFB.use_seq = true; RFB.use_seq = true;
...@@ -1230,15 +1301,19 @@ load: function (target) { ...@@ -1230,15 +1301,19 @@ load: function (target) {
} }
/* Populate the controls if defaults are provided in the URL */ /* Populate the controls if defaults are provided in the URL */
if (RFB.state == 'disconnected') { if (RFB.state === 'disconnected') {
var url = document.location.href; url = document.location.href;
$('VNC_host').value = (url.match(/host=([^&#]*)/) || ['',''])[1]; $('VNC_host').value = (url.match(/host=([A-Za-z0-9.\-]*)/) ||
$('VNC_port').value = (url.match(/port=([^&#]*)/) || ['',''])[1]; ['',''])[1];
$('VNC_password').value = (url.match(/password=([^&#]*)/) || ['',''])[1]; $('VNC_port').value = (url.match(/port=([0-9]*)/) ||
$('VNC_encrypt').checked = (url.match(/encrypt=([^&#]*)/) || ['',''])[1]; ['',''])[1];
$('VNC_password').value = (url.match(/password=([^&#]*)/) ||
['',''])[1];
$('VNC_encrypt').checked = (url.match(/encrypt=([A-Za-z0-9]*)/) ||
['',''])[1];
} }
//console.log("<< load"); //console.log("<< load");
}, }
}; /* End of RFB */ }; /* End of RFB */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment