Commit 1098b5bf authored by Joel Martin's avatar Joel Martin

Refactor FBU.bytes handling to simplify.

- raw encoding displays horizontal lines as they arrive for better
  feedback and less time hanging waiting for something.
parent 6321aff0
...@@ -42,8 +42,9 @@ Array.prototype.shiftBytes = function (len) { ...@@ -42,8 +42,9 @@ Array.prototype.shiftBytes = function (len) {
*/ */
FBU = { FBU = {
rects : 0, rects : 0,
subrects : 0, subrects : 0, // RRE and HEXTILE
tiles : 0, lines : 0, // RAW
tiles : 0, // HEXTILE
bytes : 0, bytes : 0,
x : 0, x : 0,
y : 0, y : 0,
...@@ -230,7 +231,7 @@ init_msg: function () { ...@@ -230,7 +231,7 @@ 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");
if ((FBU.rects > 0) || (FBU.bytes > 0)) { if (FBU.rects > 0) {
var msg_type = 0; var msg_type = 0;
} else { } else {
var msg_type = RFB.d.shift8(); var msg_type = RFB.d.shift8();
...@@ -248,7 +249,7 @@ normal_msg: function () { ...@@ -248,7 +249,7 @@ normal_msg: function () {
FBU.bytes = 0; FBU.bytes = 0;
} }
while ((FBU.rects > 0) && (RFB.d.length > 0)) { while ((FBU.rects > 0) && (RFB.d.length >= FBU.bytes)) {
if (FBU.bytes == 0) { if (FBU.bytes == 0) {
if (RFB.d.length < 12) { if (RFB.d.length < 12) {
console.log(" waiting for rect header bytes"); console.log(" waiting for rect header bytes");
...@@ -260,51 +261,33 @@ normal_msg: function () { ...@@ -260,51 +261,33 @@ normal_msg: function () {
FBU.width = RFB.d.shift16(); FBU.width = RFB.d.shift16();
FBU.height = RFB.d.shift16(); FBU.height = RFB.d.shift16();
FBU.encoding = parseInt(RFB.d.shift32(), 10); FBU.encoding = parseInt(RFB.d.shift32(), 10);
//var msg = "FramebufferUpdate rects:" + FBU.rects + " encoding:" + FBU.encoding
// Debug:
/*
var msg = "FramebufferUpdate rects:" + FBU.rects + " encoding:" + FBU.encoding
switch (FBU.encoding) { switch (FBU.encoding) {
case 0: // Raw case 0: msg += "(RAW)"; break;
FBU.bytes = FBU.width * FBU.height * RFB.fb_Bpp; case 1: msg += "(COPY-RECT)"; break;
//msg += "(RAW)" case 2: msg += "(RRE)"; break;
break; case 5: msg += "(HEXTILE " + FBU.tiles + " tiles)"; break;
case 1: // Copy-Rect
FBU.bytes = 4;
//msg += "(COPY-RECT)"
break;
case 2: // RRE
FBU.bytes = 4 + RFB.fb_Bpp;
//msg += "(RRE)"
break;
case 5: // hextile
FBU.bytes = 2; // No header; get it started
FBU.tiles_x = Math.ceil(FBU.width/16);
FBU.tiles_y = Math.ceil(FBU.height/16);
FBU.total_tiles = FBU.tiles_x * FBU.tiles_y;
FBU.tiles = FBU.total_tiles;
//msg += "(HEXTILE " + FBU.tiles + " tiles)"
break;
default: default:
console.log("Unsupported encoding " + FBU.encoding); console.log("Unsupported encoding " + FBU.encoding);
RFB.state = "failed"; RFB.state = "failed";
return; return;
} }
//msg += ", RFB.d.length: " + RFB.d.length + ", FBU.bytes: " + FBU.bytes msg += ", RFB.d.length: " + RFB.d.length
//console.log(msg); console.log(msg);
*/
} }
if (RFB.d.length >= FBU.bytes) { //console.log("> RFB.d.length: " + RFB.d.length + ", arr[0..30]: " + RFB.d.slice(0,30));
FBU.bytes = 0; switch (FBU.encoding) {
case 0: RFB.display_raw(); break; // Raw
switch (FBU.encoding) { case 1: RFB.display_copy_rect(); break; // Copy-Rect
case 0: RFB.display_raw(); break; // Raw case 2: RFB.display_rre(); break; // RRE
case 1: RFB.display_copy_rect(); break; // Copy-Rect case 5: RFB.display_hextile(); break; // hextile
case 2: RFB.display_rre(); break; // RRE
case 5: RFB.display_hextile(); break; // hextile
}
} else {
/* We don't have enough yet */
FBU.bytes = FBU.bytes - RFB.d.length;
break;
} }
//console.log("< RFB.d.length: " + RFB.d.length + ", FBU.bytes: " + FBU.bytes);
if (RFB.state != "normal") return; if (RFB.state != "normal") return;
} }
...@@ -340,36 +323,65 @@ normal_msg: function () { ...@@ -340,36 +323,65 @@ normal_msg: function () {
*/ */
display_raw: function () { display_raw: function () {
console.log(">> display_raw"); //console.log(">> display_raw");
Canvas.rgbxImage(FBU.x, FBU.y, FBU.width, FBU.height, RFB.d); if (FBU.lines == 0) {
RFB.d.shiftBytes(FBU.width * FBU.height * RFB.fb_Bpp); FBU.lines = FBU.height;
FBU.rects --; }
FBU.bytes = FBU.width * RFB.fb_Bpp; // At least a line
if (RFB.d.length < FBU.bytes) {
//console.log(" waiting for " + (FBU.bytes - RFB.d.length) + " RAW bytes");
return;
}
var cur_y = FBU.y + (FBU.height - FBU.lines);
var cur_height = Math.min(FBU.lines, Math.floor(RFB.d.length/(FBU.width * RFB.fb_Bpp)));
//console.log("cur_y:" + cur_y + ", cur_height:" + cur_height);
Canvas.rgbxImage(FBU.x, cur_y, FBU.width, cur_height, RFB.d);
RFB.d.shiftBytes(FBU.width * cur_height * RFB.fb_Bpp);
FBU.lines -= cur_height;
if (FBU.lines > 0) {
FBU.bytes = FBU.width * RFB.fb_Bpp; // At least another line
} else {
FBU.rects --;
FBU.bytes = 0;
}
}, },
display_copy_rect: function () { display_copy_rect: function () {
console.log(">> display_copy_rect"); //console.log(">> display_copy_rect");
FBU.bytes = 4;
if (RFB.d.length < FBU.bytes) {
//console.log(" waiting for " + (FBU.bytes - RFB.d.length) + " COPY-RECT bytes");
return;
}
var old_x = RFB.d.shift16(); var old_x = RFB.d.shift16();
var old_y = RFB.d.shift16(); var old_y = RFB.d.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;
}, },
display_rre: function () { display_rre: function () {
//console.log(">> display_rre (" + RFB.d.length + " bytes)"); //console.log(">> display_rre (" + RFB.d.length + " bytes)");
if (FBU.subrects == 0) { if (FBU.subrects == 0) {
FBU.bytes = 4 + RFB.fb_Bpp;
if (RFB.d.length < FBU.bytes) {
//console.log(" waiting for " + (FBU.bytes - RFB.d.length) + " RRE bytes");
return;
}
FBU.subrects = RFB.d.shift32(); FBU.subrects = RFB.d.shift32();
console.log(">> display_rre " + "(" + FBU.subrects + " subrects)"); //console.log(">> display_rre " + "(" + FBU.subrects + " subrects)");
var color = RFB.d.shiftBytes(RFB.fb_Bpp); // Background var color = RFB.d.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) && (RFB.d.length >= (RFB.fb_Bpp + 8))) { while ((FBU.subrects > 0) && (RFB.d.length >= (RFB.fb_Bpp + 8))) {
FBU.subrects --;
var color = RFB.d.shiftBytes(RFB.fb_Bpp); var color = RFB.d.shiftBytes(RFB.fb_Bpp);
var x = RFB.d.shift16(); var x = RFB.d.shift16();
var y = RFB.d.shift16(); var y = RFB.d.shift16();
var width = RFB.d.shift16(); var width = RFB.d.shift16();
var height = RFB.d.shift16(); var height = RFB.d.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 --;
} }
//console.log(" display_rre: rects: " + FBU.rects + ", FBU.subrects: " + FBU.subrects); //console.log(" display_rre: rects: " + FBU.rects + ", FBU.subrects: " + FBU.subrects);
...@@ -378,6 +390,7 @@ display_rre: function () { ...@@ -378,6 +390,7 @@ display_rre: function () {
FBU.bytes = (RFB.fb_Bpp + 8) * chunk; FBU.bytes = (RFB.fb_Bpp + 8) * chunk;
} else { } else {
FBU.rects --; FBU.rects --;
FBU.bytes = 0;
} }
//console.log("<< display_rre, FBU.bytes: " + FBU.bytes); //console.log("<< display_rre, FBU.bytes: " + FBU.bytes);
}, },
...@@ -386,8 +399,27 @@ display_hextile: function() { ...@@ -386,8 +399,27 @@ display_hextile: function() {
//console.log(">> display_hextile, tiles: " + FBU.tiles + ", arr.length: " + RFB.d.length + ", bytes: " + FBU.bytes); //console.log(">> display_hextile, tiles: " + FBU.tiles + ", arr.length: " + RFB.d.length + ", bytes: " + FBU.bytes);
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;
/* FBU.bytes comes in as 0, RFB.d.length at least 1 */ if (FBU.tiles == 0) {
while ((FBU.tiles > 0) && (RFB.d.length >= Math.max(1, FBU.bytes))) { FBU.tiles_x = Math.ceil(FBU.width/16);
FBU.tiles_y = Math.ceil(FBU.height/16);
FBU.total_tiles = FBU.tiles_x * FBU.tiles_y;
FBU.tiles = FBU.total_tiles;
}
/* FBU.bytes comes in as 1, RFB.d.length at least 1 */
while (FBU.tiles > 0) {
FBU.bytes = 1;
if (RFB.d.length < FBU.bytes) {
console.log(" waiting for HEXTILE subencoding byte");
return;
}
subencoding = RFB.d[0]; // Peek
if (subencoding > 30) { // Raw
console.log("Illegal subencoding " + subencoding);
RFB.state = "failed";
return;
}
subrects = 0;
cur_tile = FBU.total_tiles - FBU.tiles; cur_tile = FBU.total_tiles - FBU.tiles;
tile_x = cur_tile % FBU.tiles_x; tile_x = cur_tile % FBU.tiles_x;
tile_y = Math.floor(cur_tile / FBU.tiles_x); tile_y = Math.floor(cur_tile / FBU.tiles_x);
...@@ -395,43 +427,30 @@ display_hextile: function() { ...@@ -395,43 +427,30 @@ display_hextile: function() {
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)
subrects = 0;
if (FBU.subencoding == -1) {
/* We enter with at least 2 bytes */
subencoding = RFB.d[0]; // Peek
//console.log(" display_hextile, subencoding: " + subencoding);
FBU.bytes++; // Since we aren't shifting it off
//console.log(" subencoding: " + subencoding);
if (subencoding > 30) { // Raw
console.log("Illegal subencoding " + subencoding);
RFB.state = "failed";
return;
}
/* Figure out how much we are expecting */ /* Figure out how much we are expecting */
if (subencoding & 0x01) { // Raw if (subencoding & 0x01) { // Raw
//console.log(" Raw subencoding"); //console.log(" Raw subencoding");
FBU.bytes += w * h * RFB.fb_Bpp; FBU.bytes += w * h * RFB.fb_Bpp;
} else { } else {
if (subencoding & 0x02) { // Background if (subencoding & 0x02) { // Background
FBU.bytes += RFB.fb_Bpp; FBU.bytes += RFB.fb_Bpp;
} }
if (subencoding & 0x04) { // Foreground if (subencoding & 0x04) { // Foreground
FBU.bytes += RFB.fb_Bpp; FBU.bytes += RFB.fb_Bpp;
}
if (subencoding & 0x08) { // AnySubrects
FBU.bytes++; // Since we aren't shifting it off
if (RFB.d.length < FBU.bytes) {
/* Wait for subrects byte */
console.log(" waiting for hextile subrects header byte");
return;
} }
if (subencoding & 0x08) { // AnySubrects subrects = RFB.d[FBU.bytes-1]; // Peek
FBU.bytes++; // Since we aren't shifting it off if (subencoding & 0x10) { // SubrectsColoured
if (RFB.d.length < FBU.bytes) { FBU.bytes += subrects * (RFB.fb_Bpp + 2);
/* Wait for subrects byte */ } else {
console.log(" waiting for hextile subrects header bytes"); FBU.bytes += subrects * 2;
return;
}
subrects = RFB.d[FBU.bytes-1]; // Peek
if (subencoding & 0x10) { // SubrectsColoured
FBU.bytes += subrects * (RFB.fb_Bpp + 2);
} else {
FBU.bytes += subrects * 2;
}
} }
} }
} }
...@@ -443,72 +462,63 @@ display_hextile: function() { ...@@ -443,72 +462,63 @@ display_hextile: function() {
return; return;
} }
if (subencoding > -1) { /* We know the encoding and have a whole tile */
/* We know the encoding and have a whole tile */ FBU.subencoding = RFB.d.shift8();
FBU.subencoding = RFB.d.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"); continue;
FBU.subencoding = -1; }
//FBU.lastsubencoding = 0; Canvas.fillRect(x, y, w, h, FBU.background);
continue; } else if (FBU.subencoding & 0x01) { // Raw
} else { Canvas.rgbxImage(x, y, w, h, RFB.d);
Canvas.fillRect(x, y, w, h, FBU.background); } else {
} var idx = 0;
} else if (FBU.subencoding & 0x01) { // Raw if (FBU.subencoding & 0x02) { // Background
Canvas.rgbxImage(x, y, w, h, RFB.d); FBU.background = RFB.d.slice(idx, idx + RFB.fb_Bpp);
} else { idx += RFB.fb_Bpp;
var idx = 0; //console.log(" background: " + FBU.background);
if (FBU.subencoding & 0x02) { // Background }
FBU.background = RFB.d.slice(idx, idx + RFB.fb_Bpp); if (FBU.subencoding & 0x04) { // Foreground
idx += RFB.fb_Bpp; FBU.foreground = RFB.d.slice(idx, idx + RFB.fb_Bpp);
//console.log(" background: " + FBU.background); idx += RFB.fb_Bpp;
} //console.log(" foreground: " + FBU.foreground);
if (FBU.subencoding & 0x04) { // Foreground }
FBU.foreground = RFB.d.slice(idx, idx + RFB.fb_Bpp); Canvas.fillRect(x, y, w, h, FBU.background);
idx += RFB.fb_Bpp; if (FBU.subencoding & 0x08) { // AnySubrects
//console.log(" foreground: " + FBU.foreground); subrects = RFB.d[idx];
} idx++;
Canvas.fillRect(x, y, w, h, FBU.background); var color, xy, sx, sy, wh, sw, sh;
if (FBU.subencoding & 0x08) { // AnySubrects for (var i = 0; i < subrects; i ++) {
subrects = RFB.d[idx]; if (FBU.subencoding & 0x10) { // SubrectsColoured
idx++; color = RFB.d.slice(idx, idx + RFB.fb_Bpp);
var color, xy, sx, sy, wh, sw, sh; idx += RFB.fb_Bpp;
for (var i = 0; i < subrects; i ++) { } else {
if (FBU.subencoding & 0x10) { // SubrectsColoured color = FBU.foreground;
color = RFB.d.slice(idx, idx + RFB.fb_Bpp);
idx += RFB.fb_Bpp;
} else {
color = FBU.foreground;
}
xy = RFB.d[idx];
idx++;
sx = x + (xy >> 4);
sy = y + (xy & 0x0f);
wh = RFB.d[idx];
idx++;
sw = (wh >> 4) + 1;
sh = (wh & 0x0f) + 1;
Canvas.fillRect(sx, sy, sw, sh, color);
} }
xy = RFB.d[idx];
idx++;
sx = x + (xy >> 4);
sy = y + (xy & 0x0f);
wh = RFB.d[idx];
idx++;
sw = (wh >> 4) + 1;
sh = (wh & 0x0f) + 1;
Canvas.fillRect(sx, sy, sw, sh, color);
} }
} }
if (FBU.bytes > 0) RFB.d.shiftBytes(FBU.bytes);
FBU.lastsubencoding = FBU.subencoding;
FBU.subencoding = -1;
FBU.tiles --;
FBU.bytes = 0;
} }
RFB.d.shiftBytes(FBU.bytes);
FBU.lastsubencoding = FBU.subencoding;
FBU.bytes = 0;
FBU.tiles --;
} }
if (FBU.tiles > 0) { if (FBU.tiles == 0) {
FBU.bytes = 2;
} else {
FBU.background = [255, 255, 0, 0]; // Yellow: invalid
FBU.rects --; FBU.rects --;
} }
...@@ -623,7 +633,7 @@ send_string: function (str) { ...@@ -623,7 +633,7 @@ send_string: function (str) {
}, },
send_array: function (arr) { send_array: function (arr) {
console.log(">> send_array: " + arr); //console.log(">> send_array: " + arr);
//console.log(">> send_array: " + Base64.encode_array(arr)); //console.log(">> send_array: " + Base64.encode_array(arr));
RFB.ws.send(Base64.encode_array(arr)); RFB.ws.send(Base64.encode_array(arr));
}, },
...@@ -671,7 +681,7 @@ checkEvents: function () { ...@@ -671,7 +681,7 @@ checkEvents: function () {
}, },
keyDown: function (e) { keyDown: function (e) {
console.log(">> keyDown: " + Canvas.getKeysym(e)); //console.log(">> keyDown: " + Canvas.getKeysym(e));
e.stop(); e.stop();
var arr = RFB.keyEvent(Canvas.getKeysym(e), 1); var arr = RFB.keyEvent(Canvas.getKeysym(e), 1);
arr = arr.concat(RFB.fbUpdateRequest(1)); arr = arr.concat(RFB.fbUpdateRequest(1));
...@@ -679,7 +689,7 @@ keyDown: function (e) { ...@@ -679,7 +689,7 @@ keyDown: function (e) {
}, },
keyUp: function (e) { keyUp: function (e) {
console.log(">> keyUp: " + Canvas.getKeysym(e)); //console.log(">> keyUp: " + Canvas.getKeysym(e));
e.stop(); e.stop();
var arr = RFB.keyEvent(Canvas.getKeysym(e), 0); var arr = RFB.keyEvent(Canvas.getKeysym(e), 0);
arr = arr.concat(RFB.fbUpdateRequest(1)); arr = arr.concat(RFB.fbUpdateRequest(1));
...@@ -691,7 +701,7 @@ mouseDown: function(e) { ...@@ -691,7 +701,7 @@ mouseDown: function(e) {
var x, y; var x, y;
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);
Mouse.buttonMask |= 1 << evt.button; Mouse.buttonMask |= 1 << evt.button;
Mouse.arr = Mouse.arr.concat( RFB.pointerEvent(x, y) ); Mouse.arr = Mouse.arr.concat( RFB.pointerEvent(x, y) );
...@@ -703,7 +713,7 @@ mouseUp: function(e) { ...@@ -703,7 +713,7 @@ mouseUp: function(e) {
var x, y; var x, y;
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);
Mouse.buttonMask ^= 1 << evt.button; Mouse.buttonMask ^= 1 << evt.button;
Mouse.arr = Mouse.arr.concat( RFB.pointerEvent(x, y) ); Mouse.arr = Mouse.arr.concat( RFB.pointerEvent(x, y) );
...@@ -715,7 +725,7 @@ mouseMove: function(e) { ...@@ -715,7 +725,7 @@ mouseMove: function(e) {
var x, y; var x, y;
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);
Mouse.arr = Mouse.arr.concat( RFB.pointerEvent(x, y) ); Mouse.arr = Mouse.arr.concat( RFB.pointerEvent(x, y) );
}, },
......
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