2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2016 Samuel Mannehed for Cendio AB
5 * Licensed under MPL 2.0 (see LICENSE.txt)
7 * See README.md for usage and integration instructions.
9 * TIGHT decoder portion:
10 * (c) 2012 Michael Tinglof, Joe Balaz, Les Piech (Mercuri.ca)
13 import * as Log
from './util/logging.js';
14 import _
from './util/localization.js';
15 import { decodeUTF8
} from './util/strings.js';
16 import { browserSupportsCursorURIs
} from './util/browsers.js';
17 import Display
from "./display.js";
18 import Keyboard
from "./input/keyboard.js";
19 import Mouse
from "./input/mouse.js";
20 import Websock
from "./websock.js";
21 import Base64
from "./base64.js";
22 import DES
from "./des.js";
23 import KeyTable
from "./input/keysym.js";
24 import XtScancode
from "./input/xtscancodes.js";
25 import Inflator
from "./inflator.js";
26 import { encodings
, encodingName
} from "./encodings.js";
28 /*jslint white: false, browser: true */
29 /*global window, Util, Display, Keyboard, Mouse, Websock, Websock_native, Base64, DES, KeyTable, Inflator, XtScancode */
31 export default function RFB(target
) {
32 this._target
= target
;
36 this._rfb_credentials
= {};
39 this._rfb_connection_state
= '';
40 this._rfb_init_state
= '';
41 this._rfb_auth_scheme
= '';
42 this._rfb_disconnect_reason
= "";
44 // Server capabilities
45 this._rfb_version
= 0;
46 this._rfb_max_version
= 3.8;
47 this._rfb_tightvnc
= false;
48 this._rfb_xvp_ver
= 0;
55 this._capabilities
= { power
: false, resize
: false };
57 this._supportsFence
= false;
59 this._supportsContinuousUpdates
= false;
60 this._enabledContinuousUpdates
= false;
62 this._supportsSetDesktopSize
= false;
64 this._screen_flags
= 0;
66 this._qemuExtKeyEventSupported
= false;
69 this._sock
= null; // Websock object
70 this._display
= null; // Display object
71 this._flushing
= false; // Display flushing state
72 this._keyboard
= null; // Keyboard input handler object
73 this._mouse
= null; // Mouse input handler object
76 this._disconnTimer
= null; // disconnection timer
78 // Decoder states and stats
79 this._encHandlers
= {};
84 subrects
: 0, // RRE and HEXTILE
95 zlibs
: [] // TIGHT zlib streams
97 for (var i
= 0; i
< 4; i
++) {
98 this._FBU
.zlibs
[i
] = new Inflator();
101 this._destBuff
= null;
102 this._paletteBuff
= new Uint8Array(1024); // 256 * 4 (max palette size * max bytes-per-pixel)
104 this._rre_chunk_sz
= 100;
120 this._mouse_buttonMask
= 0;
121 this._mouse_arr
= [];
122 this._viewportDragging
= false;
123 this._viewportDragPos
= {};
124 this._viewportHasMoved
= false;
127 Log
.Debug(">> RFB.constructor");
129 // Target canvas must be able to have focus
130 if (!this._target
.hasAttribute('tabindex')) {
131 this._target
.tabIndex
= -1;
134 // populate encHandlers with bound versions
135 this._encHandlers
[encodings
.encodingRaw
] = RFB
.encodingHandlers
.RAW
.bind(this);
136 this._encHandlers
[encodings
.encodingCopyRect
] = RFB
.encodingHandlers
.COPYRECT
.bind(this);
137 this._encHandlers
[encodings
.encodingRRE
] = RFB
.encodingHandlers
.RRE
.bind(this);
138 this._encHandlers
[encodings
.encodingHextile
] = RFB
.encodingHandlers
.HEXTILE
.bind(this);
139 this._encHandlers
[encodings
.encodingTight
] = RFB
.encodingHandlers
.TIGHT
.bind(this);
141 this._encHandlers
[encodings
.pseudoEncodingDesktopSize
] = RFB
.encodingHandlers
.DesktopSize
.bind(this);
142 this._encHandlers
[encodings
.pseudoEncodingLastRect
] = RFB
.encodingHandlers
.last_rect
.bind(this);
143 this._encHandlers
[encodings
.pseudoEncodingCursor
] = RFB
.encodingHandlers
.Cursor
.bind(this);
144 this._encHandlers
[encodings
.pseudoEncodingQEMUExtendedKeyEvent
] = RFB
.encodingHandlers
.QEMUExtendedKeyEvent
.bind(this);
145 this._encHandlers
[encodings
.pseudoEncodingExtendedDesktopSize
] = RFB
.encodingHandlers
.ExtendedDesktopSize
.bind(this);
147 // NB: nothing that needs explicit teardown should be done
148 // before this point, since this can throw an exception
150 this._display
= new Display(this._target
);
152 Log
.Error("Display exception: " + exc
);
155 this._display
.onflush
= this._onFlush
.bind(this);
156 this._display
.clear();
158 this._keyboard
= new Keyboard(this._target
);
159 this._keyboard
.onkeyevent
= this._handleKeyEvent
.bind(this);
161 this._mouse
= new Mouse(this._target
);
162 this._mouse
.onmousebutton
= this._handleMouseButton
.bind(this);
163 this._mouse
.onmousemove
= this._handleMouseMove
.bind(this);
165 this._sock
= new Websock();
166 this._sock
.on('message', this._handle_message
.bind(this));
167 this._sock
.on('open', function () {
168 if ((this._rfb_connection_state
=== 'connecting') &&
169 (this._rfb_init_state
=== '')) {
170 this._rfb_init_state
= 'ProtocolVersion';
171 Log
.Debug("Starting VNC handshake");
173 this._fail("Unexpected server connection");
176 this._sock
.on('close', function (e
) {
177 Log
.Warn("WebSocket on-close event");
180 msg
= " (code: " + e
.code
;
182 msg
+= ", reason: " + e
.reason
;
186 switch (this._rfb_connection_state
) {
187 case 'disconnecting':
188 this._updateConnectionState('disconnected');
191 this._fail('Failed to connect to server', msg
);
194 // Handle disconnects that were initiated server-side
195 this._updateConnectionState('disconnecting');
196 this._updateConnectionState('disconnected');
199 this._fail("Unexpected server disconnect",
200 "Already disconnected: " + msg
);
203 this._fail("Unexpected server disconnect",
204 "Not in any state yet: " + msg
);
207 this._sock
.off('close');
209 this._sock
.on('error', function (e
) {
210 Log
.Warn("WebSocket on-error event");
213 Log
.Debug("<< RFB.constructor");
217 // ===== PROPERTIES =====
219 disconnectTimeout
: 3,
223 get localCursor() { return this._localCursor
; },
224 set localCursor(cursor
) {
225 if (!cursor
|| (cursor
in {'0': 1, 'no': 1, 'false': 1})) {
226 this._localCursor
= false;
227 this._display
.disableLocalCursor(); //Only show server-side cursor
229 if (browserSupportsCursorURIs()) {
230 this._localCursor
= true;
232 Log
.Warn("Browser does not support local cursor");
233 this._display
.disableLocalCursor();
237 // Need to send an updated list of encodings if we are connected
238 if (this._rfb_connection_state
=== "connected") {
239 this._sendEncodings();
244 get viewOnly() { return this._viewOnly
; },
245 set viewOnly(viewOnly
) {
246 this._viewOnly
= viewOnly
;
248 if (this._rfb_connection_state
=== "connecting" ||
249 this._rfb_connection_state
=== "connected") {
251 this._keyboard
.ungrab();
252 this._mouse
.ungrab();
254 this._keyboard
.grab();
260 get capabilities() { return this._capabilities
; },
262 get touchButton() { return this._mouse
.touchButton
; },
263 set touchButton(button
) { this._mouse
.touchButton
= button
; },
265 get scale() { return this._display
.scale
; },
266 set scale(scale
) { this._display
.scale
= scale
; },
268 get viewport() { return this._display
.viewport
; },
269 set viewport(viewport
) { this._display
.viewport
= viewport
; },
271 // ===== EVENT HANDLERS =====
273 onupdatestate: function () {}, // onupdatestate(rfb, state, oldstate): connection state change
274 onnotification: function () {}, // onnotification(rfb, msg, level, options): notification for the UI
275 ondisconnected: function () {}, // ondisconnected(rfb, reason): disconnection finished
276 oncredentialsrequired: function () {}, // oncredentialsrequired(rfb, types): VNC credentials are required
277 onclipboard: function () {}, // onclipboard(rfb, text): RFB clipboard contents received
278 onbell: function () {}, // onbell(rfb): RFB Bell message received
279 onfbresize: function () {}, // onfbresize(rfb, width, height): frame buffer resized
280 ondesktopname: function () {}, // ondesktopname(rfb, name): desktop name received
281 oncapabilities: function () {}, // oncapabilities(rfb, caps): the supported capabilities has changed
283 // ===== PUBLIC METHODS =====
285 connect: function (url
, options
) {
287 this._fail(_("Must specify URL"));
293 options
= options
|| {}
295 this._rfb_credentials
= options
.credentials
|| {};
296 this._shared
= 'shared' in options
? !!options
.shared
: true;
297 this._repeaterID
= options
.repeaterID
|| '';
299 this._rfb_init_state
= '';
300 this._updateConnectionState('connecting');
303 disconnect: function () {
304 this._updateConnectionState('disconnecting');
305 this._sock
.off('error');
306 this._sock
.off('message');
307 this._sock
.off('open');
310 sendCredentials: function (creds
) {
311 this._rfb_credentials
= creds
;
312 setTimeout(this._init_msg
.bind(this), 0);
315 sendCtrlAltDel: function () {
316 if (this._rfb_connection_state
!== 'connected' || this._viewOnly
) { return; }
317 Log
.Info("Sending Ctrl-Alt-Del");
319 this.sendKey(KeyTable
.XK_Control_L
, "ControlLeft", true);
320 this.sendKey(KeyTable
.XK_Alt_L
, "AltLeft", true);
321 this.sendKey(KeyTable
.XK_Delete
, "Delete", true);
322 this.sendKey(KeyTable
.XK_Delete
, "Delete", false);
323 this.sendKey(KeyTable
.XK_Alt_L
, "AltLeft", false);
324 this.sendKey(KeyTable
.XK_Control_L
, "ControlLeft", false);
327 machineShutdown: function () {
331 machineReboot: function () {
335 machineReset: function () {
339 // Send a key press. If 'down' is not specified then send a down key
340 // followed by an up key.
341 sendKey: function (keysym
, code
, down
) {
342 if (this._rfb_connection_state
!== 'connected' || this._viewOnly
) { return; }
344 if (down
=== undefined) {
345 this.sendKey(keysym
, code
, true);
346 this.sendKey(keysym
, code
, false);
350 var scancode
= XtScancode
[code
];
352 if (this._qemuExtKeyEventSupported
&& scancode
) {
354 keysym
= keysym
|| 0;
356 Log
.Info("Sending key (" + (down
? "down" : "up") + "): keysym " + keysym
+ ", scancode " + scancode
);
358 RFB
.messages
.QEMUExtendedKeyEvent(this._sock
, keysym
, down
, scancode
);
363 Log
.Info("Sending keysym (" + (down
? "down" : "up") + "): " + keysym
);
364 RFB
.messages
.keyEvent(this._sock
, keysym
, down
? 1 : 0);
368 clipboardPasteFrom: function (text
) {
369 if (this._rfb_connection_state
!== 'connected' || this._viewOnly
) { return; }
370 RFB
.messages
.clientCutText(this._sock
, text
);
373 autoscale: function (width
, height
, downscaleOnly
) {
374 if (this._rfb_connection_state
!== 'connected') { return; }
375 this._display
.autoscale(width
, height
, downscaleOnly
);
378 viewportChangeSize: function(width
, height
) {
379 if (this._rfb_connection_state
!== 'connected') { return; }
380 this._display
.viewportChangeSize(width
, height
);
383 clippingDisplay: function () {
384 if (this._rfb_connection_state
!== 'connected') { return false; }
385 return this._display
.clippingDisplay();
388 // Requests a change of remote desktop size. This message is an extension
389 // and may only be sent if we have received an ExtendedDesktopSize message
390 requestDesktopSize: function (width
, height
) {
391 if (this._rfb_connection_state
!== 'connected' ||
396 if (!this._supportsSetDesktopSize
) {
400 RFB
.messages
.setDesktopSize(this._sock
, width
, height
,
401 this._screen_id
, this._screen_flags
);
405 // ===== PRIVATE METHODS =====
407 _connect: function () {
408 Log
.Debug(">> RFB.connect");
410 Log
.Info("connecting to " + this._url
);
413 // WebSocket.onopen transitions to the RFB init states
414 this._sock
.open(this._url
, ['binary']);
416 if (e
.name
=== 'SyntaxError') {
417 this._fail("Invalid host or port value given", e
);
419 this._fail("Error while connecting", e
);
423 // Always grab focus on some kind of click event
424 this._target
.addEventListener("mousedown", this._focusCanvas
);
425 this._target
.addEventListener("touchstart", this._focusCanvas
);
427 Log
.Debug("<< RFB.connect");
430 _disconnect: function () {
431 Log
.Debug(">> RFB.disconnect");
432 this._target
.removeEventListener("mousedown", this._focusCanvas
);
433 this._target
.removeEventListener("touchstart", this._focusCanvas
);
437 Log
.Debug("<< RFB.disconnect");
440 _print_stats: function () {
441 var stats
= this._encStats
;
443 Log
.Info("Encoding stats for this connection:");
444 Object
.keys(stats
).forEach(function (key
) {
446 if (s
[0] + s
[1] > 0) {
447 Log
.Info(" " + encodingName(key
) + ": " + s
[0] + " rects");
451 Log
.Info("Encoding stats since page load:");
452 Object
.keys(stats
).forEach(function (key
) {
454 Log
.Info(" " + encodingName(key
) + ": " + s
[1] + " rects");
458 _cleanup: function () {
459 if (!this._viewOnly
) { this._keyboard
.ungrab(); }
460 if (!this._viewOnly
) { this._mouse
.ungrab(); }
461 this._display
.defaultCursor();
462 if (Log
.get_logging() !== 'debug') {
463 // Show noVNC logo when disconnected, unless in
465 this._display
.clear();
469 // Event handler for canvas so this points to the canvas element
470 _focusCanvas: function(event
) {
471 // Respect earlier handlers' request to not do side-effects
472 if (!event
.defaultPrevented
)
481 * disconnected - permanent state
483 _updateConnectionState: function (state
) {
484 var oldstate
= this._rfb_connection_state
;
486 if (state
=== oldstate
) {
487 Log
.Debug("Already in state '" + state
+ "', ignoring");
491 // The 'disconnected' state is permanent for each RFB object
492 if (oldstate
=== 'disconnected') {
493 Log
.Error("Tried changing state of a disconnected RFB object");
497 // Ensure proper transitions before doing anything
500 if (oldstate
!== 'connecting') {
501 Log
.Error("Bad transition to connected state, " +
502 "previous connection state: " + oldstate
);
508 if (oldstate
!== 'disconnecting') {
509 Log
.Error("Bad transition to disconnected state, " +
510 "previous connection state: " + oldstate
);
516 if (oldstate
!== '') {
517 Log
.Error("Bad transition to connecting state, " +
518 "previous connection state: " + oldstate
);
523 case 'disconnecting':
524 if (oldstate
!== 'connected' && oldstate
!== 'connecting') {
525 Log
.Error("Bad transition to disconnecting state, " +
526 "previous connection state: " + oldstate
);
532 Log
.Error("Unknown connection state: " + state
);
536 // State change actions
538 this._rfb_connection_state
= state
;
539 this.onupdatestate(this, state
, oldstate
);
541 var smsg
= "New state '" + state
+ "', was '" + oldstate
+ "'.";
544 if (this._disconnTimer
&& state
!== 'disconnecting') {
545 Log
.Debug("Clearing disconnect timer");
546 clearTimeout(this._disconnTimer
);
547 this._disconnTimer
= null;
549 // make sure we don't get a double event
550 this._sock
.off('close');
555 // Call ondisconnected callback after onupdatestate since
556 // we don't know if the UI only displays the latest message
557 if (this._rfb_disconnect_reason
!== "") {
558 this.ondisconnected(this, this._rfb_disconnect_reason
);
560 // No reason means clean disconnect
561 this.ondisconnected(this);
569 case 'disconnecting':
572 this._disconnTimer
= setTimeout(function () {
573 this._rfb_disconnect_reason
= _("Disconnect timeout");
574 this._updateConnectionState('disconnected');
575 }.bind(this), this._disconnectTimeout
* 1000);
580 /* Print errors and disconnect
582 * The optional parameter 'details' is used for information that
583 * should be logged but not sent to the user interface.
585 _fail: function (msg
, details
) {
587 if (typeof details
!== 'undefined') {
588 fullmsg
= msg
+ " (" + details
+ ")";
590 switch (this._rfb_connection_state
) {
591 case 'disconnecting':
592 Log
.Error("Failed when disconnecting: " + fullmsg
);
595 Log
.Error("Failed while connected: " + fullmsg
);
598 Log
.Error("Failed when connecting: " + fullmsg
);
601 Log
.Error("RFB failure: " + fullmsg
);
604 this._rfb_disconnect_reason
= msg
; //This is sent to the UI
606 // Transition to disconnected without waiting for socket to close
607 this._updateConnectionState('disconnecting');
608 this._updateConnectionState('disconnected');
614 * Send a notification to the UI. Valid levels are:
615 * 'normal'|'warn'|'error'
617 * NOTE: Options could be added in the future.
618 * NOTE: If this function is called multiple times, remember that the
619 * interface could be only showing the latest notification.
621 _notification: function(msg
, level
, options
) {
626 Log
.Debug("Notification[" + level
+ "]:" + msg
);
629 Log
.Error("Invalid notification level: " + level
);
634 this.onnotification(this, msg
, level
, options
);
636 this.onnotification(this, msg
, level
);
640 _setCapability: function (cap
, val
) {
641 this._capabilities
[cap
] = val
;
642 this.oncapabilities(this, this._capabilities
);
645 _handle_message: function () {
646 if (this._sock
.rQlen() === 0) {
647 Log
.Warn("handle_message called on an empty receive queue");
651 switch (this._rfb_connection_state
) {
653 Log
.Error("Got data while disconnected");
657 if (this._flushing
) {
660 if (!this._normal_msg()) {
663 if (this._sock
.rQlen() === 0) {
674 _handleKeyEvent: function (keysym
, code
, down
) {
675 this.sendKey(keysym
, code
, down
);
678 _handleMouseButton: function (x
, y
, down
, bmask
) {
680 this._mouse_buttonMask
|= bmask
;
682 this._mouse_buttonMask
&= ~bmask
;
685 if (this._viewportDrag
) {
686 if (down
&& !this._viewportDragging
) {
687 this._viewportDragging
= true;
688 this._viewportDragPos
= {'x': x
, 'y': y
};
690 // Skip sending mouse events
693 this._viewportDragging
= false;
695 // If the viewport didn't actually move, then treat as a mouse click event
696 // Send the button down event here, as the button up event is sent at the end of this function
697 if (!this._viewportHasMoved
&& !this._viewOnly
) {
698 RFB
.messages
.pointerEvent(this._sock
, this._display
.absX(x
), this._display
.absY(y
), bmask
);
700 this._viewportHasMoved
= false;
704 if (this._viewOnly
) { return; } // View only, skip mouse events
706 if (this._rfb_connection_state
!== 'connected') { return; }
707 RFB
.messages
.pointerEvent(this._sock
, this._display
.absX(x
), this._display
.absY(y
), this._mouse_buttonMask
);
710 _handleMouseMove: function (x
, y
) {
711 if (this._viewportDragging
) {
712 var deltaX
= this._viewportDragPos
.x
- x
;
713 var deltaY
= this._viewportDragPos
.y
- y
;
715 // The goal is to trigger on a certain physical width, the
716 // devicePixelRatio brings us a bit closer but is not optimal.
717 var dragThreshold
= 10 * (window
.devicePixelRatio
|| 1);
719 if (this._viewportHasMoved
|| (Math
.abs(deltaX
) > dragThreshold
||
720 Math
.abs(deltaY
) > dragThreshold
)) {
721 this._viewportHasMoved
= true;
723 this._viewportDragPos
= {'x': x
, 'y': y
};
724 this._display
.viewportChangePos(deltaX
, deltaY
);
727 // Skip sending mouse events
731 if (this._viewOnly
) { return; } // View only, skip mouse events
733 if (this._rfb_connection_state
!== 'connected') { return; }
734 RFB
.messages
.pointerEvent(this._sock
, this._display
.absX(x
), this._display
.absY(y
), this._mouse_buttonMask
);
739 _negotiate_protocol_version: function () {
740 if (this._sock
.rQlen() < 12) {
741 return this._fail("Error while negotiating with server",
742 "Incomplete protocol version");
745 var sversion
= this._sock
.rQshiftStr(12).substr(4, 7);
746 Log
.Info("Server ProtocolVersion: " + sversion
);
749 case "000.000": // UltraVNC repeater
753 case "003.006": // UltraVNC
754 case "003.889": // Apple Remote Desktop
755 this._rfb_version
= 3.3;
758 this._rfb_version
= 3.7;
761 case "004.000": // Intel AMT KVM
762 case "004.001": // RealVNC 4.6
763 case "005.000": // RealVNC 5.3
764 this._rfb_version
= 3.8;
767 return this._fail("Unsupported server",
768 "Invalid server version: " + sversion
);
772 var repeaterID
= "ID:" + this._repeaterID
;
773 while (repeaterID
.length
< 250) {
776 this._sock
.send_string(repeaterID
);
780 if (this._rfb_version
> this._rfb_max_version
) {
781 this._rfb_version
= this._rfb_max_version
;
784 var cversion
= "00" + parseInt(this._rfb_version
, 10) +
785 ".00" + ((this._rfb_version
* 10) % 10);
786 this._sock
.send_string("RFB " + cversion
+ "\n");
787 Log
.Debug('Sent ProtocolVersion: ' + cversion
);
789 this._rfb_init_state
= 'Security';
792 _negotiate_security: function () {
793 // Polyfill since IE and PhantomJS doesn't have
794 // TypedArray.includes()
795 function includes(item
, array
) {
796 for (var i
= 0; i
< array
.length
; i
++) {
797 if (array
[i
] === item
) {
804 if (this._rfb_version
>= 3.7) {
805 // Server sends supported list, client decides
806 var num_types
= this._sock
.rQshift8();
807 if (this._sock
.rQwait("security type", num_types
, 1)) { return false; }
809 if (num_types
=== 0) {
810 var strlen
= this._sock
.rQshift32();
811 var reason
= this._sock
.rQshiftStr(strlen
);
812 return this._fail("Error while negotiating with server",
813 "Security failure: " + reason
);
816 var types
= this._sock
.rQshiftBytes(num_types
);
817 Log
.Debug("Server security types: " + types
);
819 // Look for each auth in preferred order
820 this._rfb_auth_scheme
= 0;
821 if (includes(1, types
)) {
822 this._rfb_auth_scheme
= 1; // None
823 } else if (includes(22, types
)) {
824 this._rfb_auth_scheme
= 22; // XVP
825 } else if (includes(16, types
)) {
826 this._rfb_auth_scheme
= 16; // Tight
827 } else if (includes(2, types
)) {
828 this._rfb_auth_scheme
= 2; // VNC Auth
830 return this._fail("Unsupported server",
831 "Unsupported security types: " + types
);
834 this._sock
.send([this._rfb_auth_scheme
]);
837 if (this._sock
.rQwait("security scheme", 4)) { return false; }
838 this._rfb_auth_scheme
= this._sock
.rQshift32();
841 this._rfb_init_state
= 'Authentication';
842 Log
.Debug('Authenticating using scheme: ' + this._rfb_auth_scheme
);
844 return this._init_msg(); // jump to authentication
848 _negotiate_xvp_auth: function () {
849 if (!this._rfb_credentials
.username
||
850 !this._rfb_credentials
.password
||
851 !this._rfb_credentials
.target
) {
852 this.oncredentialsrequired(this, ["username", "password", "target"]);
856 var xvp_auth_str
= String
.fromCharCode(this._rfb_credentials
.username
.length
) +
857 String
.fromCharCode(this._rfb_credentials
.target
.length
) +
858 this._rfb_credentials
.username
+
859 this._rfb_credentials
.target
;
860 this._sock
.send_string(xvp_auth_str
);
861 this._rfb_auth_scheme
= 2;
862 return this._negotiate_authentication();
865 _negotiate_std_vnc_auth: function () {
866 if (this._sock
.rQwait("auth challenge", 16)) { return false; }
868 if (!this._rfb_credentials
.password
) {
869 this.oncredentialsrequired(this, ["password"]);
873 // TODO(directxman12): make genDES not require an Array
874 var challenge
= Array
.prototype.slice
.call(this._sock
.rQshiftBytes(16));
875 var response
= RFB
.genDES(this._rfb_credentials
.password
, challenge
);
876 this._sock
.send(response
);
877 this._rfb_init_state
= "SecurityResult";
881 _negotiate_tight_tunnels: function (numTunnels
) {
882 var clientSupportedTunnelTypes
= {
883 0: { vendor
: 'TGHT', signature
: 'NOTUNNEL' }
885 var serverSupportedTunnelTypes
= {};
886 // receive tunnel capabilities
887 for (var i
= 0; i
< numTunnels
; i
++) {
888 var cap_code
= this._sock
.rQshift32();
889 var cap_vendor
= this._sock
.rQshiftStr(4);
890 var cap_signature
= this._sock
.rQshiftStr(8);
891 serverSupportedTunnelTypes
[cap_code
] = { vendor
: cap_vendor
, signature
: cap_signature
};
894 // choose the notunnel type
895 if (serverSupportedTunnelTypes
[0]) {
896 if (serverSupportedTunnelTypes
[0].vendor
!= clientSupportedTunnelTypes
[0].vendor
||
897 serverSupportedTunnelTypes
[0].signature
!= clientSupportedTunnelTypes
[0].signature
) {
898 return this._fail("Unsupported server",
899 "Client's tunnel type had the incorrect " +
900 "vendor or signature");
902 this._sock
.send([0, 0, 0, 0]); // use NOTUNNEL
903 return false; // wait until we receive the sub auth count to continue
905 return this._fail("Unsupported server",
906 "Server wanted tunnels, but doesn't support " +
907 "the notunnel type");
911 _negotiate_tight_auth: function () {
912 if (!this._rfb_tightvnc
) { // first pass, do the tunnel negotiation
913 if (this._sock
.rQwait("num tunnels", 4)) { return false; }
914 var numTunnels
= this._sock
.rQshift32();
915 if (numTunnels
> 0 && this._sock
.rQwait("tunnel capabilities", 16 * numTunnels
, 4)) { return false; }
917 this._rfb_tightvnc
= true;
919 if (numTunnels
> 0) {
920 this._negotiate_tight_tunnels(numTunnels
);
921 return false; // wait until we receive the sub auth to continue
925 // second pass, do the sub-auth negotiation
926 if (this._sock
.rQwait("sub auth count", 4)) { return false; }
927 var subAuthCount
= this._sock
.rQshift32();
928 if (subAuthCount
=== 0) { // empty sub-auth list received means 'no auth' subtype selected
929 this._rfb_init_state
= 'SecurityResult';
933 if (this._sock
.rQwait("sub auth capabilities", 16 * subAuthCount
, 4)) { return false; }
935 var clientSupportedTypes
= {
940 var serverSupportedTypes
= [];
942 for (var i
= 0; i
< subAuthCount
; i
++) {
943 var capNum
= this._sock
.rQshift32();
944 var capabilities
= this._sock
.rQshiftStr(12);
945 serverSupportedTypes
.push(capabilities
);
948 for (var authType
in clientSupportedTypes
) {
949 if (serverSupportedTypes
.indexOf(authType
) != -1) {
950 this._sock
.send([0, 0, 0, clientSupportedTypes
[authType
]]);
953 case 'STDVNOAUTH__': // no auth
954 this._rfb_init_state
= 'SecurityResult';
956 case 'STDVVNCAUTH_': // VNC auth
957 this._rfb_auth_scheme
= 2;
958 return this._init_msg();
960 return this._fail("Unsupported server",
961 "Unsupported tiny auth scheme: " +
967 return this._fail("Unsupported server",
968 "No supported sub-auth types!");
971 _negotiate_authentication: function () {
972 switch (this._rfb_auth_scheme
) {
973 case 0: // connection failed
974 if (this._sock
.rQwait("auth reason", 4)) { return false; }
975 var strlen
= this._sock
.rQshift32();
976 var reason
= this._sock
.rQshiftStr(strlen
);
977 return this._fail("Authentication failure", reason
);
980 if (this._rfb_version
>= 3.8) {
981 this._rfb_init_state
= 'SecurityResult';
984 this._rfb_init_state
= 'ClientInitialisation';
985 return this._init_msg();
988 return this._negotiate_xvp_auth();
990 case 2: // VNC authentication
991 return this._negotiate_std_vnc_auth();
993 case 16: // TightVNC Security Type
994 return this._negotiate_tight_auth();
997 return this._fail("Unsupported server",
998 "Unsupported auth scheme: " +
999 this._rfb_auth_scheme
);
1003 _handle_security_result: function () {
1004 if (this._sock
.rQwait('VNC auth response ', 4)) { return false; }
1005 switch (this._sock
.rQshift32()) {
1007 this._rfb_init_state
= 'ClientInitialisation';
1008 Log
.Debug('Authentication OK');
1009 return this._init_msg();
1011 if (this._rfb_version
>= 3.8) {
1012 var length
= this._sock
.rQshift32();
1013 if (this._sock
.rQwait("SecurityResult reason", length
, 8)) { return false; }
1014 var reason
= this._sock
.rQshiftStr(length
);
1015 return this._fail("Authentication failure", reason
);
1017 return this._fail("Authentication failure");
1020 return this._fail("Too many authentication attempts");
1022 return this._fail("Unsupported server",
1023 "Unknown SecurityResult");
1027 _negotiate_server_init: function () {
1028 if (this._sock
.rQwait("server initialization", 24)) { return false; }
1031 var width
= this._sock
.rQshift16();
1032 var height
= this._sock
.rQshift16();
1035 var bpp
= this._sock
.rQshift8();
1036 var depth
= this._sock
.rQshift8();
1037 var big_endian
= this._sock
.rQshift8();
1038 var true_color
= this._sock
.rQshift8();
1040 var red_max
= this._sock
.rQshift16();
1041 var green_max
= this._sock
.rQshift16();
1042 var blue_max
= this._sock
.rQshift16();
1043 var red_shift
= this._sock
.rQshift8();
1044 var green_shift
= this._sock
.rQshift8();
1045 var blue_shift
= this._sock
.rQshift8();
1046 this._sock
.rQskipBytes(3); // padding
1048 // NB(directxman12): we don't want to call any callbacks or print messages until
1049 // *after* we're past the point where we could backtrack
1051 /* Connection name/title */
1052 var name_length = this._sock.rQshift32();
1053 if (this._sock.rQwait('server init name', name_length, 24)) { return false; }
1054 this._fb_name = decodeUTF8(this._sock.rQshiftStr(name_length));
1056 if (this._rfb_tightvnc) {
1057 if (this._sock.rQwait('TightVNC extended server init header', 8, 24 + name_length)) { return false; }
1058 // In TightVNC mode, ServerInit message is extended
1059 var numServerMessages = this._sock.rQshift16();
1060 var numClientMessages = this._sock.rQshift16();
1061 var numEncodings = this._sock.rQshift16();
1062 this._sock.rQskipBytes(2); // padding
1064 var totalMessagesLength = (numServerMessages + numClientMessages + numEncodings) * 16;
1065 if (this._sock.rQwait('TightVNC extended server init header', totalMessagesLength, 32 + name_length)) { return false; }
1067 // we don't actually do anything with the capability information that TIGHT sends,
1068 // so we just skip the all of this.
1070 // TIGHT server message capabilities
1071 this._sock.rQskipBytes(16 * numServerMessages);
1073 // TIGHT client message capabilities
1074 this._sock.rQskipBytes(16 * numClientMessages);
1076 // TIGHT encoding capabilities
1077 this._sock.rQskipBytes(16 * numEncodings);
1080 // NB(directxman12): these are down here so that we don't run them multiple times
1082 Log.Info("Screen: " + width + "x" + height +
1083 ", bpp: " + bpp + ", depth: " + depth +
1084 ", big_endian: " + big_endian +
1085 ", true_color: " + true_color +
1086 ", red_max: " + red_max +
1087 ", green_max: " + green_max +
1088 ", blue_max: " + blue_max +
1089 ", red_shift: " + red_shift +
1090 ", green_shift: " + green_shift +
1091 ", blue_shift: " + blue_shift);
1093 if (big_endian !== 0) {
1094 Log.Warn("Server native endian is not little endian");
1097 if (red_shift !== 16) {
1098 Log.Warn("Server native red-shift is not 16");
1101 if (blue_shift !== 0) {
1102 Log.Warn("Server native blue-shift is not 0");
1105 // we're past the point where we could backtrack, so it's safe to call this
1106 this.ondesktopname(this, this._fb_name);
1108 this._resize(width, height);
1110 if (!this._viewOnly) { this._keyboard.grab(); }
1111 if (!this._viewOnly) { this._mouse.grab(); }
1113 this._fb_depth = 24;
1115 if (this._fb_name === "Intel(r) AMT KVM") {
1116 Log.Warn("Intel AMT KVM only supports 8/16 bit depths. Using low color mode.");
1120 RFB.messages.pixelFormat(this._sock, this._fb_depth, true);
1121 this._sendEncodings();
1122 RFB.messages.fbUpdateRequest(this._sock, false, 0, 0, this._fb_width, this._fb_height);
1124 this._timing.fbu_rt_start = (new Date()).getTime();
1125 this._timing.pixels = 0;
1127 // Cursor will be server side until the server decides to honor
1128 // our request and send over the cursor image
1129 this._display.disableLocalCursor();
1131 this._updateConnectionState('connected');
1135 _sendEncodings: function () {
1138 // In preference order
1139 encs.push(encodings.encodingCopyRect);
1140 // Only supported with full depth support
1141 if (this._fb_depth == 24) {
1142 encs.push(encodings.encodingTight);
1143 encs.push(encodings.encodingHextile);
1144 encs.push(encodings.encodingRRE);
1146 encs.push(encodings.encodingRaw);
1148 // Psuedo-encoding settings
1149 encs.push(encodings.pseudoEncodingTightPNG);
1150 encs.push(encodings.pseudoEncodingQualityLevel0 + 6);
1151 encs.push(encodings.pseudoEncodingCompressLevel0 + 2);
1153 encs.push(encodings.pseudoEncodingDesktopSize);
1154 encs.push(encodings.pseudoEncodingLastRect);
1155 encs.push(encodings.pseudoEncodingQEMUExtendedKeyEvent);
1156 encs.push(encodings.pseudoEncodingExtendedDesktopSize);
1157 encs.push(encodings.pseudoEncodingXvp);
1158 encs.push(encodings.pseudoEncodingFence);
1159 encs.push(encodings.pseudoEncodingContinuousUpdates);
1161 if (this._localCursor && this._fb_depth == 24) {
1162 encs.push(encodings.pseudoEncodingCursor);
1165 RFB.messages.clientEncodings(this._sock, encs);
1168 /* RFB protocol initialization states:
1173 * ClientInitialization - not triggered by server message
1174 * ServerInitialization
1176 _init_msg: function () {
1177 switch (this._rfb_init_state
) {
1178 case 'ProtocolVersion':
1179 return this._negotiate_protocol_version();
1182 return this._negotiate_security();
1184 case 'Authentication':
1185 return this._negotiate_authentication();
1187 case 'SecurityResult':
1188 return this._handle_security_result();
1190 case 'ClientInitialisation':
1191 this._sock
.send([this._shared
? 1 : 0]); // ClientInitialisation
1192 this._rfb_init_state
= 'ServerInitialisation';
1195 case 'ServerInitialisation':
1196 return this._negotiate_server_init();
1199 return this._fail("Internal error", "Unknown init state: " +
1200 this._rfb_init_state
);
1204 _handle_set_colour_map_msg: function () {
1205 Log
.Debug("SetColorMapEntries");
1207 return this._fail("Protocol error", "Unexpected SetColorMapEntries message");
1210 _handle_server_cut_text: function () {
1211 Log
.Debug("ServerCutText");
1213 if (this._sock
.rQwait("ServerCutText header", 7, 1)) { return false; }
1214 this._sock
.rQskipBytes(3); // Padding
1215 var length
= this._sock
.rQshift32();
1216 if (this._sock
.rQwait("ServerCutText", length
, 8)) { return false; }
1218 var text
= this._sock
.rQshiftStr(length
);
1220 if (this._viewOnly
) { return true; }
1222 this.onclipboard(this, text
);
1227 _handle_server_fence_msg: function() {
1228 if (this._sock
.rQwait("ServerFence header", 8, 1)) { return false; }
1229 this._sock
.rQskipBytes(3); // Padding
1230 var flags
= this._sock
.rQshift32();
1231 var length
= this._sock
.rQshift8();
1233 if (this._sock
.rQwait("ServerFence payload", length
, 9)) { return false; }
1236 Log
.Warn("Bad payload length (" + length
+ ") in fence response");
1240 var payload
= this._sock
.rQshiftStr(length
);
1242 this._supportsFence
= true;
1247 * (1<<0) - BlockBefore
1248 * (1<<1) - BlockAfter
1253 if (!(flags
& (1<<31))) {
1254 return this._fail("Internal error",
1255 "Unexpected fence response");
1258 // Filter out unsupported flags
1259 // FIXME: support syncNext
1260 flags
&= (1<<0) | (1<<1);
1262 // BlockBefore and BlockAfter are automatically handled by
1263 // the fact that we process each incoming message
1265 RFB
.messages
.clientFence(this._sock
, flags
, payload
);
1270 _handle_xvp_msg: function () {
1271 if (this._sock
.rQwait("XVP version and message", 3, 1)) { return false; }
1272 this._sock
.rQskip8(); // Padding
1273 var xvp_ver
= this._sock
.rQshift8();
1274 var xvp_msg
= this._sock
.rQshift8();
1278 Log
.Error("Operation Failed");
1279 this._notification("XVP Operation Failed", 'error');
1282 this._rfb_xvp_ver
= xvp_ver
;
1283 Log
.Info("XVP extensions enabled (version " + this._rfb_xvp_ver
+ ")");
1284 this._setCapability("power", true);
1287 this._fail("Unexpected server message",
1288 "Illegal server XVP message " + xvp_msg
);
1295 _normal_msg: function () {
1298 if (this._FBU
.rects
> 0) {
1301 msg_type
= this._sock
.rQshift8();
1305 case 0: // FramebufferUpdate
1306 var ret
= this._framebufferUpdate();
1307 if (ret
&& !this._enabledContinuousUpdates
) {
1308 RFB
.messages
.fbUpdateRequest(this._sock
, true, 0, 0,
1309 this._fb_width
, this._fb_height
);
1313 case 1: // SetColorMapEntries
1314 return this._handle_set_colour_map_msg();
1321 case 3: // ServerCutText
1322 return this._handle_server_cut_text();
1324 case 150: // EndOfContinuousUpdates
1325 var first
= !(this._supportsContinuousUpdates
);
1326 this._supportsContinuousUpdates
= true;
1327 this._enabledContinuousUpdates
= false;
1329 this._enabledContinuousUpdates
= true;
1330 this._updateContinuousUpdates();
1331 Log
.Info("Enabling continuous updates.");
1333 // FIXME: We need to send a framebufferupdaterequest here
1334 // if we add support for turning off continuous updates
1338 case 248: // ServerFence
1339 return this._handle_server_fence_msg();
1342 return this._handle_xvp_msg();
1345 this._fail("Unexpected server message", "Type:" + msg_type
);
1346 Log
.Debug("sock.rQslice(0, 30): " + this._sock
.rQslice(0, 30));
1351 _onFlush: function() {
1352 this._flushing
= false;
1353 // Resume processing
1354 if (this._sock
.rQlen() > 0) {
1355 this._handle_message();
1359 _framebufferUpdate: function () {
1363 if (this._FBU
.rects
=== 0) {
1364 if (this._sock
.rQwait("FBU header", 3, 1)) { return false; }
1365 this._sock
.rQskip8(); // Padding
1366 this._FBU
.rects
= this._sock
.rQshift16();
1367 this._FBU
.bytes
= 0;
1368 this._timing
.cur_fbu
= 0;
1369 if (this._timing
.fbu_rt_start
> 0) {
1370 now
= (new Date()).getTime();
1371 Log
.Info("First FBU latency: " + (now
- this._timing
.fbu_rt_start
));
1374 // Make sure the previous frame is fully rendered first
1375 // to avoid building up an excessive queue
1376 if (this._display
.pending()) {
1377 this._flushing
= true;
1378 this._display
.flush();
1383 while (this._FBU
.rects
> 0) {
1384 if (this._rfb_connection_state
!== 'connected') { return false; }
1386 if (this._sock
.rQwait("FBU", this._FBU
.bytes
)) { return false; }
1387 if (this._FBU
.bytes
=== 0) {
1388 if (this._sock
.rQwait("rect header", 12)) { return false; }
1389 /* New FramebufferUpdate */
1391 var hdr
= this._sock
.rQshiftBytes(12);
1392 this._FBU
.x
= (hdr
[0] << 8) + hdr
[1];
1393 this._FBU
.y
= (hdr
[2] << 8) + hdr
[3];
1394 this._FBU
.width
= (hdr
[4] << 8) + hdr
[5];
1395 this._FBU
.height
= (hdr
[6] << 8) + hdr
[7];
1396 this._FBU
.encoding
= parseInt((hdr
[8] << 24) + (hdr
[9] << 16) +
1397 (hdr
[10] << 8) + hdr
[11], 10);
1399 if (!this._encHandlers
[this._FBU
.encoding
]) {
1400 this._fail("Unexpected server message",
1401 "Unsupported encoding " +
1402 this._FBU
.encoding
);
1407 this._timing
.last_fbu
= (new Date()).getTime();
1409 ret
= this._encHandlers
[this._FBU
.encoding
]();
1411 now
= (new Date()).getTime();
1412 this._timing
.cur_fbu
+= (now
- this._timing
.last_fbu
);
1415 if (!(this._FBU
.encoding
in this._encStats
)) {
1416 this._encStats
[this._FBU
.encoding
] = [0, 0];
1418 this._encStats
[this._FBU
.encoding
][0]++;
1419 this._encStats
[this._FBU
.encoding
][1]++;
1420 this._timing
.pixels
+= this._FBU
.width
* this._FBU
.height
;
1423 if (this._timing
.pixels
>= (this._fb_width
* this._fb_height
)) {
1424 if ((this._FBU
.width
=== this._fb_width
&& this._FBU
.height
=== this._fb_height
) ||
1425 this._timing
.fbu_rt_start
> 0) {
1426 this._timing
.full_fbu_total
+= this._timing
.cur_fbu
;
1427 this._timing
.full_fbu_cnt
++;
1428 Log
.Info("Timing of full FBU, curr: " +
1429 this._timing
.cur_fbu
+ ", total: " +
1430 this._timing
.full_fbu_total
+ ", cnt: " +
1431 this._timing
.full_fbu_cnt
+ ", avg: " +
1432 (this._timing
.full_fbu_total
/ this._timing
.full_fbu_cnt
));
1435 if (this._timing
.fbu_rt_start
> 0) {
1436 var fbu_rt_diff
= now
- this._timing
.fbu_rt_start
;
1437 this._timing
.fbu_rt_total
+= fbu_rt_diff
;
1438 this._timing
.fbu_rt_cnt
++;
1439 Log
.Info("full FBU round-trip, cur: " +
1440 fbu_rt_diff
+ ", total: " +
1441 this._timing
.fbu_rt_total
+ ", cnt: " +
1442 this._timing
.fbu_rt_cnt
+ ", avg: " +
1443 (this._timing
.fbu_rt_total
/ this._timing
.fbu_rt_cnt
));
1444 this._timing
.fbu_rt_start
= 0;
1448 if (!ret
) { return ret
; } // need more data
1451 this._display
.flip();
1453 return true; // We finished this FBU
1456 _updateContinuousUpdates: function() {
1457 if (!this._enabledContinuousUpdates
) { return; }
1459 RFB
.messages
.enableContinuousUpdates(this._sock
, true, 0, 0,
1460 this._fb_width
, this._fb_height
);
1463 _resize: function(width
, height
) {
1464 this._fb_width
= width
;
1465 this._fb_height
= height
;
1467 this._destBuff
= new Uint8Array(this._fb_width
* this._fb_height
* 4);
1469 this._display
.resize(this._fb_width
, this._fb_height
);
1470 this.onfbresize(this, this._fb_width
, this._fb_height
);
1472 this._timing
.fbu_rt_start
= (new Date()).getTime();
1473 this._updateContinuousUpdates();
1476 _xvpOp: function (ver
, op
) {
1477 if (this._rfb_xvp_ver
< ver
) { return; }
1478 Log
.Info("Sending XVP operation " + op
+ " (version " + ver
+ ")");
1479 RFB
.messages
.xvpOp(this._sock
, ver
, op
);
1485 keyEvent: function (sock
, keysym
, down
) {
1486 var buff
= sock
._sQ
;
1487 var offset
= sock
._sQlen
;
1489 buff
[offset
] = 4; // msg-type
1490 buff
[offset
+ 1] = down
;
1492 buff
[offset
+ 2] = 0;
1493 buff
[offset
+ 3] = 0;
1495 buff
[offset
+ 4] = (keysym
>> 24);
1496 buff
[offset
+ 5] = (keysym
>> 16);
1497 buff
[offset
+ 6] = (keysym
>> 8);
1498 buff
[offset
+ 7] = keysym
;
1504 QEMUExtendedKeyEvent: function (sock
, keysym
, down
, keycode
) {
1505 function getRFBkeycode(xt_scancode
) {
1506 var upperByte
= (keycode
>> 8);
1507 var lowerByte
= (keycode
& 0x00ff);
1508 if (upperByte
=== 0xe0 && lowerByte
< 0x7f) {
1509 lowerByte
= lowerByte
| 0x80;
1515 var buff
= sock
._sQ
;
1516 var offset
= sock
._sQlen
;
1518 buff
[offset
] = 255; // msg-type
1519 buff
[offset
+ 1] = 0; // sub msg-type
1521 buff
[offset
+ 2] = (down
>> 8);
1522 buff
[offset
+ 3] = down
;
1524 buff
[offset
+ 4] = (keysym
>> 24);
1525 buff
[offset
+ 5] = (keysym
>> 16);
1526 buff
[offset
+ 6] = (keysym
>> 8);
1527 buff
[offset
+ 7] = keysym
;
1529 var RFBkeycode
= getRFBkeycode(keycode
);
1531 buff
[offset
+ 8] = (RFBkeycode
>> 24);
1532 buff
[offset
+ 9] = (RFBkeycode
>> 16);
1533 buff
[offset
+ 10] = (RFBkeycode
>> 8);
1534 buff
[offset
+ 11] = RFBkeycode
;
1540 pointerEvent: function (sock
, x
, y
, mask
) {
1541 var buff
= sock
._sQ
;
1542 var offset
= sock
._sQlen
;
1544 buff
[offset
] = 5; // msg-type
1546 buff
[offset
+ 1] = mask
;
1548 buff
[offset
+ 2] = x
>> 8;
1549 buff
[offset
+ 3] = x
;
1551 buff
[offset
+ 4] = y
>> 8;
1552 buff
[offset
+ 5] = y
;
1558 // TODO(directxman12): make this unicode compatible?
1559 clientCutText: function (sock
, text
) {
1560 var buff
= sock
._sQ
;
1561 var offset
= sock
._sQlen
;
1563 buff
[offset
] = 6; // msg-type
1565 buff
[offset
+ 1] = 0; // padding
1566 buff
[offset
+ 2] = 0; // padding
1567 buff
[offset
+ 3] = 0; // padding
1569 var n
= text
.length
;
1571 buff
[offset
+ 4] = n
>> 24;
1572 buff
[offset
+ 5] = n
>> 16;
1573 buff
[offset
+ 6] = n
>> 8;
1574 buff
[offset
+ 7] = n
;
1576 for (var i
= 0; i
< n
; i
++) {
1577 buff
[offset
+ 8 + i
] = text
.charCodeAt(i
);
1580 sock
._sQlen
+= 8 + n
;
1584 setDesktopSize: function (sock
, width
, height
, id
, flags
) {
1585 var buff
= sock
._sQ
;
1586 var offset
= sock
._sQlen
;
1588 buff
[offset
] = 251; // msg-type
1589 buff
[offset
+ 1] = 0; // padding
1590 buff
[offset
+ 2] = width
>> 8; // width
1591 buff
[offset
+ 3] = width
;
1592 buff
[offset
+ 4] = height
>> 8; // height
1593 buff
[offset
+ 5] = height
;
1595 buff
[offset
+ 6] = 1; // number-of-screens
1596 buff
[offset
+ 7] = 0; // padding
1599 buff
[offset
+ 8] = id
>> 24; // id
1600 buff
[offset
+ 9] = id
>> 16;
1601 buff
[offset
+ 10] = id
>> 8;
1602 buff
[offset
+ 11] = id
;
1603 buff
[offset
+ 12] = 0; // x-position
1604 buff
[offset
+ 13] = 0;
1605 buff
[offset
+ 14] = 0; // y-position
1606 buff
[offset
+ 15] = 0;
1607 buff
[offset
+ 16] = width
>> 8; // width
1608 buff
[offset
+ 17] = width
;
1609 buff
[offset
+ 18] = height
>> 8; // height
1610 buff
[offset
+ 19] = height
;
1611 buff
[offset
+ 20] = flags
>> 24; // flags
1612 buff
[offset
+ 21] = flags
>> 16;
1613 buff
[offset
+ 22] = flags
>> 8;
1614 buff
[offset
+ 23] = flags
;
1620 clientFence: function (sock
, flags
, payload
) {
1621 var buff
= sock
._sQ
;
1622 var offset
= sock
._sQlen
;
1624 buff
[offset
] = 248; // msg-type
1626 buff
[offset
+ 1] = 0; // padding
1627 buff
[offset
+ 2] = 0; // padding
1628 buff
[offset
+ 3] = 0; // padding
1630 buff
[offset
+ 4] = flags
>> 24; // flags
1631 buff
[offset
+ 5] = flags
>> 16;
1632 buff
[offset
+ 6] = flags
>> 8;
1633 buff
[offset
+ 7] = flags
;
1635 var n
= payload
.length
;
1637 buff
[offset
+ 8] = n
; // length
1639 for (var i
= 0; i
< n
; i
++) {
1640 buff
[offset
+ 9 + i
] = payload
.charCodeAt(i
);
1643 sock
._sQlen
+= 9 + n
;
1647 enableContinuousUpdates: function (sock
, enable
, x
, y
, width
, height
) {
1648 var buff
= sock
._sQ
;
1649 var offset
= sock
._sQlen
;
1651 buff
[offset
] = 150; // msg-type
1652 buff
[offset
+ 1] = enable
; // enable-flag
1654 buff
[offset
+ 2] = x
>> 8; // x
1655 buff
[offset
+ 3] = x
;
1656 buff
[offset
+ 4] = y
>> 8; // y
1657 buff
[offset
+ 5] = y
;
1658 buff
[offset
+ 6] = width
>> 8; // width
1659 buff
[offset
+ 7] = width
;
1660 buff
[offset
+ 8] = height
>> 8; // height
1661 buff
[offset
+ 9] = height
;
1667 pixelFormat: function (sock
, depth
, true_color
) {
1668 var buff
= sock
._sQ
;
1669 var offset
= sock
._sQlen
;
1675 } else if (depth
> 8) {
1681 bits
= Math
.floor(depth
/3);
1683 buff
[offset
] = 0; // msg-type
1685 buff
[offset
+ 1] = 0; // padding
1686 buff
[offset
+ 2] = 0; // padding
1687 buff
[offset
+ 3] = 0; // padding
1689 buff
[offset
+ 4] = bpp
; // bits-per-pixel
1690 buff
[offset
+ 5] = depth
; // depth
1691 buff
[offset
+ 6] = 0; // little-endian
1692 buff
[offset
+ 7] = true_color
? 1 : 0; // true-color
1694 buff
[offset
+ 8] = 0; // red-max
1695 buff
[offset
+ 9] = (1 << bits
) - 1; // red-max
1697 buff
[offset
+ 10] = 0; // green-max
1698 buff
[offset
+ 11] = (1 << bits
) - 1; // green-max
1700 buff
[offset
+ 12] = 0; // blue-max
1701 buff
[offset
+ 13] = (1 << bits
) - 1; // blue-max
1703 buff
[offset
+ 14] = bits
* 2; // red-shift
1704 buff
[offset
+ 15] = bits
* 1; // green-shift
1705 buff
[offset
+ 16] = bits
* 0; // blue-shift
1707 buff
[offset
+ 17] = 0; // padding
1708 buff
[offset
+ 18] = 0; // padding
1709 buff
[offset
+ 19] = 0; // padding
1715 clientEncodings: function (sock
, encodings
) {
1716 var buff
= sock
._sQ
;
1717 var offset
= sock
._sQlen
;
1719 buff
[offset
] = 2; // msg-type
1720 buff
[offset
+ 1] = 0; // padding
1722 buff
[offset
+ 2] = encodings
.length
>> 8;
1723 buff
[offset
+ 3] = encodings
.length
;
1725 var i
, j
= offset
+ 4;
1726 for (i
= 0; i
< encodings
.length
; i
++) {
1727 var enc
= encodings
[i
];
1728 buff
[j
] = enc
>> 24;
1729 buff
[j
+ 1] = enc
>> 16;
1730 buff
[j
+ 2] = enc
>> 8;
1736 sock
._sQlen
+= j
- offset
;
1740 fbUpdateRequest: function (sock
, incremental
, x
, y
, w
, h
) {
1741 var buff
= sock
._sQ
;
1742 var offset
= sock
._sQlen
;
1744 if (typeof(x
) === "undefined") { x
= 0; }
1745 if (typeof(y
) === "undefined") { y
= 0; }
1747 buff
[offset
] = 3; // msg-type
1748 buff
[offset
+ 1] = incremental
? 1 : 0;
1750 buff
[offset
+ 2] = (x
>> 8) & 0xFF;
1751 buff
[offset
+ 3] = x
& 0xFF;
1753 buff
[offset
+ 4] = (y
>> 8) & 0xFF;
1754 buff
[offset
+ 5] = y
& 0xFF;
1756 buff
[offset
+ 6] = (w
>> 8) & 0xFF;
1757 buff
[offset
+ 7] = w
& 0xFF;
1759 buff
[offset
+ 8] = (h
>> 8) & 0xFF;
1760 buff
[offset
+ 9] = h
& 0xFF;
1766 xvpOp: function (sock
, ver
, op
) {
1767 var buff
= sock
._sQ
;
1768 var offset
= sock
._sQlen
;
1770 buff
[offset
] = 250; // msg-type
1771 buff
[offset
+ 1] = 0; // padding
1773 buff
[offset
+ 2] = ver
;
1774 buff
[offset
+ 3] = op
;
1781 RFB
.genDES = function (password
, challenge
) {
1783 for (var i
= 0; i
< password
.length
; i
++) {
1784 passwd
.push(password
.charCodeAt(i
));
1786 return (new DES(passwd
)).encrypt(challenge
);
1789 RFB
.encodingHandlers
= {
1791 if (this._FBU
.lines
=== 0) {
1792 this._FBU
.lines
= this._FBU
.height
;
1795 var pixelSize
= this._fb_depth
== 8 ? 1 : 4;
1796 this._FBU
.bytes
= this._FBU
.width
* pixelSize
; // at least a line
1797 if (this._sock
.rQwait("RAW", this._FBU
.bytes
)) { return false; }
1798 var cur_y
= this._FBU
.y
+ (this._FBU
.height
- this._FBU
.lines
);
1799 var curr_height
= Math
.min(this._FBU
.lines
,
1800 Math
.floor(this._sock
.rQlen() / (this._FBU
.width
* pixelSize
)));
1801 var data
= this._sock
.get_rQ();
1802 var index
= this._sock
.get_rQi();
1803 if (this._fb_depth
== 8) {
1804 var pixels
= this._FBU
.width
* curr_height
1805 var newdata
= new Uint8Array(pixels
* 4);
1807 for (i
= 0;i
< pixels
;i
++) {
1808 newdata
[i
* 4 + 0] = ((data
[index
+ i
] >> 0) & 0x3) * 255 / 3;
1809 newdata
[i
* 4 + 1] = ((data
[index
+ i
] >> 2) & 0x3) * 255 / 3;
1810 newdata
[i
* 4 + 2] = ((data
[index
+ i
] >> 4) & 0x3) * 255 / 3;
1811 newdata
[i
* 4 + 4] = 0;
1816 this._display
.blitImage(this._FBU
.x
, cur_y
, this._FBU
.width
,
1817 curr_height
, data
, index
);
1818 this._sock
.rQskipBytes(this._FBU
.width
* curr_height
* pixelSize
);
1819 this._FBU
.lines
-= curr_height
;
1821 if (this._FBU
.lines
> 0) {
1822 this._FBU
.bytes
= this._FBU
.width
* pixelSize
; // At least another line
1825 this._FBU
.bytes
= 0;
1831 COPYRECT: function () {
1832 this._FBU
.bytes
= 4;
1833 if (this._sock
.rQwait("COPYRECT", 4)) { return false; }
1834 this._display
.copyImage(this._sock
.rQshift16(), this._sock
.rQshift16(),
1835 this._FBU
.x
, this._FBU
.y
, this._FBU
.width
,
1839 this._FBU
.bytes
= 0;
1845 if (this._FBU
.subrects
=== 0) {
1846 this._FBU
.bytes
= 4 + 4;
1847 if (this._sock
.rQwait("RRE", 4 + 4)) { return false; }
1848 this._FBU
.subrects
= this._sock
.rQshift32();
1849 color
= this._sock
.rQshiftBytes(4); // Background
1850 this._display
.fillRect(this._FBU
.x
, this._FBU
.y
, this._FBU
.width
, this._FBU
.height
, color
);
1853 while (this._FBU
.subrects
> 0 && this._sock
.rQlen() >= (4 + 8)) {
1854 color
= this._sock
.rQshiftBytes(4);
1855 var x
= this._sock
.rQshift16();
1856 var y
= this._sock
.rQshift16();
1857 var width
= this._sock
.rQshift16();
1858 var height
= this._sock
.rQshift16();
1859 this._display
.fillRect(this._FBU
.x
+ x
, this._FBU
.y
+ y
, width
, height
, color
);
1860 this._FBU
.subrects
--;
1863 if (this._FBU
.subrects
> 0) {
1864 var chunk
= Math
.min(this._rre_chunk_sz
, this._FBU
.subrects
);
1865 this._FBU
.bytes
= (4 + 8) * chunk
;
1868 this._FBU
.bytes
= 0;
1874 HEXTILE: function () {
1875 var rQ
= this._sock
.get_rQ();
1876 var rQi
= this._sock
.get_rQi();
1878 if (this._FBU
.tiles
=== 0) {
1879 this._FBU
.tiles_x
= Math
.ceil(this._FBU
.width
/ 16);
1880 this._FBU
.tiles_y
= Math
.ceil(this._FBU
.height
/ 16);
1881 this._FBU
.total_tiles
= this._FBU
.tiles_x
* this._FBU
.tiles_y
;
1882 this._FBU
.tiles
= this._FBU
.total_tiles
;
1885 while (this._FBU
.tiles
> 0) {
1886 this._FBU
.bytes
= 1;
1887 if (this._sock
.rQwait("HEXTILE subencoding", this._FBU
.bytes
)) { return false; }
1888 var subencoding
= rQ
[rQi
]; // Peek
1889 if (subencoding
> 30) { // Raw
1890 this._fail("Unexpected server message",
1891 "Illegal hextile subencoding: " + subencoding
);
1896 var curr_tile
= this._FBU
.total_tiles
- this._FBU
.tiles
;
1897 var tile_x
= curr_tile
% this._FBU
.tiles_x
;
1898 var tile_y
= Math
.floor(curr_tile
/ this._FBU
.tiles_x
);
1899 var x
= this._FBU
.x
+ tile_x
* 16;
1900 var y
= this._FBU
.y
+ tile_y
* 16;
1901 var w
= Math
.min(16, (this._FBU
.x
+ this._FBU
.width
) - x
);
1902 var h
= Math
.min(16, (this._FBU
.y
+ this._FBU
.height
) - y
);
1904 // Figure out how much we are expecting
1905 if (subencoding
& 0x01) { // Raw
1906 this._FBU
.bytes
+= w
* h
* 4;
1908 if (subencoding
& 0x02) { // Background
1909 this._FBU
.bytes
+= 4;
1911 if (subencoding
& 0x04) { // Foreground
1912 this._FBU
.bytes
+= 4;
1914 if (subencoding
& 0x08) { // AnySubrects
1915 this._FBU
.bytes
++; // Since we aren't shifting it off
1916 if (this._sock
.rQwait("hextile subrects header", this._FBU
.bytes
)) { return false; }
1917 subrects
= rQ
[rQi
+ this._FBU
.bytes
- 1]; // Peek
1918 if (subencoding
& 0x10) { // SubrectsColoured
1919 this._FBU
.bytes
+= subrects
* (4 + 2);
1921 this._FBU
.bytes
+= subrects
* 2;
1926 if (this._sock
.rQwait("hextile", this._FBU
.bytes
)) { return false; }
1928 // We know the encoding and have a whole tile
1929 this._FBU
.subencoding
= rQ
[rQi
];
1931 if (this._FBU
.subencoding
=== 0) {
1932 if (this._FBU
.lastsubencoding
& 0x01) {
1933 // Weird: ignore blanks are RAW
1934 Log
.Debug(" Ignoring blank after RAW");
1936 this._display
.fillRect(x
, y
, w
, h
, this._FBU
.background
);
1938 } else if (this._FBU
.subencoding
& 0x01) { // Raw
1939 this._display
.blitImage(x
, y
, w
, h
, rQ
, rQi
);
1940 rQi
+= this._FBU
.bytes
- 1;
1942 if (this._FBU
.subencoding
& 0x02) { // Background
1943 this._FBU
.background
= [rQ
[rQi
], rQ
[rQi
+ 1], rQ
[rQi
+ 2], rQ
[rQi
+ 3]];
1946 if (this._FBU
.subencoding
& 0x04) { // Foreground
1947 this._FBU
.foreground
= [rQ
[rQi
], rQ
[rQi
+ 1], rQ
[rQi
+ 2], rQ
[rQi
+ 3]];
1951 this._display
.startTile(x
, y
, w
, h
, this._FBU
.background
);
1952 if (this._FBU
.subencoding
& 0x08) { // AnySubrects
1956 for (var s
= 0; s
< subrects
; s
++) {
1958 if (this._FBU
.subencoding
& 0x10) { // SubrectsColoured
1959 color
= [rQ
[rQi
], rQ
[rQi
+ 1], rQ
[rQi
+ 2], rQ
[rQi
+ 3]];
1962 color
= this._FBU
.foreground
;
1967 var sy
= (xy
& 0x0f);
1971 var sw
= (wh
>> 4) + 1;
1972 var sh
= (wh
& 0x0f) + 1;
1974 this._display
.subTile(sx
, sy
, sw
, sh
, color
);
1977 this._display
.finishTile();
1979 this._sock
.set_rQi(rQi
);
1980 this._FBU
.lastsubencoding
= this._FBU
.subencoding
;
1981 this._FBU
.bytes
= 0;
1985 if (this._FBU
.tiles
=== 0) {
1992 TIGHT: function () {
1993 this._FBU
.bytes
= 1; // compression-control byte
1994 if (this._sock
.rQwait("TIGHT compression-control", this._FBU
.bytes
)) { return false; }
1996 var checksum = function (data
) {
1998 for (var i
= 0; i
< data
.length
; i
++) {
2000 if (sum
> 65536) sum
-= 65536;
2005 var resetStreams
= 0;
2007 var decompress = function (data
, expected
) {
2008 for (var i
= 0; i
< 4; i
++) {
2009 if ((resetStreams
>> i
) & 1) {
2010 this._FBU
.zlibs
[i
].reset();
2011 Log
.Info("Reset zlib stream " + i
);
2015 //var uncompressed = this._FBU.zlibs[streamId].uncompress(data, 0);
2016 var uncompressed
= this._FBU
.zlibs
[streamId
].inflate(data
, true, expected
);
2017 /*if (uncompressed.status !== 0) {
2018 Log.Error("Invalid data in zlib stream");
2021 //return uncompressed.data;
2022 return uncompressed
;
2025 var indexedToRGBX2Color = function (data
, palette
, width
, height
) {
2026 // Convert indexed (palette based) image data to RGB
2027 // TODO: reduce number of calculations inside loop
2028 var dest
= this._destBuff
;
2029 var w
= Math
.floor((width
+ 7) / 8);
2030 var w1
= Math
.floor(width
/ 8);
2032 /*for (var y = 0; y < height; y++) {
2034 var yoffset = y * width;
2035 var ybitoffset = y * w;
2036 var xoffset, targetbyte;
2037 for (x = 0; x < w1; x++) {
2038 xoffset = yoffset + x * 8;
2039 targetbyte = data[ybitoffset + x];
2040 for (b = 7; b >= 0; b--) {
2041 dp = (xoffset + 7 - b) * 3;
2042 sp = (targetbyte >> b & 1) * 3;
2043 dest[dp] = palette[sp];
2044 dest[dp + 1] = palette[sp + 1];
2045 dest[dp + 2] = palette[sp + 2];
2049 xoffset = yoffset + x * 8;
2050 targetbyte = data[ybitoffset + x];
2051 for (b = 7; b >= 8 - width % 8; b--) {
2052 dp = (xoffset + 7 - b) * 3;
2053 sp = (targetbyte >> b & 1) * 3;
2054 dest[dp] = palette[sp];
2055 dest[dp + 1] = palette[sp + 1];
2056 dest[dp + 2] = palette[sp + 2];
2060 for (var y
= 0; y
< height
; y
++) {
2062 for (x
= 0; x
< w1
; x
++) {
2063 for (b
= 7; b
>= 0; b
--) {
2064 dp
= (y
* width
+ x
* 8 + 7 - b
) * 4;
2065 sp
= (data
[y
* w
+ x
] >> b
& 1) * 3;
2066 dest
[dp
] = palette
[sp
];
2067 dest
[dp
+ 1] = palette
[sp
+ 1];
2068 dest
[dp
+ 2] = palette
[sp
+ 2];
2073 for (b
= 7; b
>= 8 - width
% 8; b
--) {
2074 dp
= (y
* width
+ x
* 8 + 7 - b
) * 4;
2075 sp
= (data
[y
* w
+ x
] >> b
& 1) * 3;
2076 dest
[dp
] = palette
[sp
];
2077 dest
[dp
+ 1] = palette
[sp
+ 1];
2078 dest
[dp
+ 2] = palette
[sp
+ 2];
2086 var indexedToRGBX = function (data
, palette
, width
, height
) {
2087 // Convert indexed (palette based) image data to RGB
2088 var dest
= this._destBuff
;
2089 var total
= width
* height
* 4;
2090 for (var i
= 0, j
= 0; i
< total
; i
+= 4, j
++) {
2091 var sp
= data
[j
] * 3;
2092 dest
[i
] = palette
[sp
];
2093 dest
[i
+ 1] = palette
[sp
+ 1];
2094 dest
[i
+ 2] = palette
[sp
+ 2];
2101 var rQi
= this._sock
.get_rQi();
2102 var rQ
= this._sock
.rQwhole();
2104 var cl_header
, cl_data
;
2106 var handlePalette = function () {
2107 var numColors
= rQ
[rQi
+ 2] + 1;
2108 var paletteSize
= numColors
* 3;
2109 this._FBU
.bytes
+= paletteSize
;
2110 if (this._sock
.rQwait("TIGHT palette " + cmode
, this._FBU
.bytes
)) { return false; }
2112 var bpp
= (numColors
<= 2) ? 1 : 8;
2113 var rowSize
= Math
.floor((this._FBU
.width
* bpp
+ 7) / 8);
2115 if (rowSize
* this._FBU
.height
< 12) {
2118 cl_data
= rowSize
* this._FBU
.height
;
2119 //clength = [0, rowSize * this._FBU.height];
2121 // begin inline getTightCLength (returning two-item arrays is bad for performance with GC)
2122 var cl_offset
= rQi
+ 3 + paletteSize
;
2125 cl_data
+= rQ
[cl_offset
] & 0x7f;
2126 if (rQ
[cl_offset
] & 0x80) {
2128 cl_data
+= (rQ
[cl_offset
+ 1] & 0x7f) << 7;
2129 if (rQ
[cl_offset
+ 1] & 0x80) {
2131 cl_data
+= rQ
[cl_offset
+ 2] << 14;
2134 // end inline getTightCLength
2137 this._FBU
.bytes
+= cl_header
+ cl_data
;
2138 if (this._sock
.rQwait("TIGHT " + cmode
, this._FBU
.bytes
)) { return false; }
2140 // Shift ctl, filter id, num colors, palette entries, and clength off
2141 this._sock
.rQskipBytes(3);
2142 //var palette = this._sock.rQshiftBytes(paletteSize);
2143 this._sock
.rQshiftTo(this._paletteBuff
, paletteSize
);
2144 this._sock
.rQskipBytes(cl_header
);
2147 data
= this._sock
.rQshiftBytes(cl_data
);
2149 data
= decompress(this._sock
.rQshiftBytes(cl_data
), rowSize
* this._FBU
.height
);
2152 // Convert indexed (palette based) image data to RGB
2154 if (numColors
== 2) {
2155 rgbx
= indexedToRGBX2Color(data
, this._paletteBuff
, this._FBU
.width
, this._FBU
.height
);
2156 this._display
.blitRgbxImage(this._FBU
.x
, this._FBU
.y
, this._FBU
.width
, this._FBU
.height
, rgbx
, 0, false);
2158 rgbx
= indexedToRGBX(data
, this._paletteBuff
, this._FBU
.width
, this._FBU
.height
);
2159 this._display
.blitRgbxImage(this._FBU
.x
, this._FBU
.y
, this._FBU
.width
, this._FBU
.height
, rgbx
, 0, false);
2166 var handleCopy = function () {
2168 var uncompressedSize
= this._FBU
.width
* this._FBU
.height
* 3;
2169 if (uncompressedSize
< 12) {
2172 cl_data
= uncompressedSize
;
2174 // begin inline getTightCLength (returning two-item arrays is for peformance with GC)
2175 var cl_offset
= rQi
+ 1;
2178 cl_data
+= rQ
[cl_offset
] & 0x7f;
2179 if (rQ
[cl_offset
] & 0x80) {
2181 cl_data
+= (rQ
[cl_offset
+ 1] & 0x7f) << 7;
2182 if (rQ
[cl_offset
+ 1] & 0x80) {
2184 cl_data
+= rQ
[cl_offset
+ 2] << 14;
2187 // end inline getTightCLength
2189 this._FBU
.bytes
= 1 + cl_header
+ cl_data
;
2190 if (this._sock
.rQwait("TIGHT " + cmode
, this._FBU
.bytes
)) { return false; }
2192 // Shift ctl, clength off
2193 this._sock
.rQshiftBytes(1 + cl_header
);
2196 data
= this._sock
.rQshiftBytes(cl_data
);
2198 data
= decompress(this._sock
.rQshiftBytes(cl_data
), uncompressedSize
);
2201 this._display
.blitRgbImage(this._FBU
.x
, this._FBU
.y
, this._FBU
.width
, this._FBU
.height
, data
, 0, false);
2206 var ctl
= this._sock
.rQpeek8();
2208 // Keep tight reset bits
2209 resetStreams
= ctl
& 0xF;
2211 // Figure out filter
2213 streamId
= ctl
& 0x3;
2215 if (ctl
=== 0x08) cmode
= "fill";
2216 else if (ctl
=== 0x09) cmode
= "jpeg";
2217 else if (ctl
=== 0x0A) cmode
= "png";
2218 else if (ctl
& 0x04) cmode
= "filter";
2219 else if (ctl
< 0x04) cmode
= "copy";
2220 else return this._fail("Unexpected server message",
2221 "Illegal tight compression received, " +
2225 // fill use depth because TPIXELs drop the padding byte
2226 case "fill": // TPIXEL
2227 this._FBU
.bytes
+= 3;
2229 case "jpeg": // max clength
2230 this._FBU
.bytes
+= 3;
2232 case "png": // max clength
2233 this._FBU
.bytes
+= 3;
2235 case "filter": // filter id + num colors if palette
2236 this._FBU
.bytes
+= 2;
2242 if (this._sock
.rQwait("TIGHT " + cmode
, this._FBU
.bytes
)) { return false; }
2244 // Determine FBU.bytes
2248 this._display
.fillRect(this._FBU
.x
, this._FBU
.y
, this._FBU
.width
, this._FBU
.height
, [rQ
[rQi
+ 3], rQ
[rQi
+ 2], rQ
[rQi
+ 1]], false);
2249 this._sock
.rQskipBytes(4);
2253 // begin inline getTightCLength (returning two-item arrays is for peformance with GC)
2254 var cl_offset
= rQi
+ 1;
2257 cl_data
+= rQ
[cl_offset
] & 0x7f;
2258 if (rQ
[cl_offset
] & 0x80) {
2260 cl_data
+= (rQ
[cl_offset
+ 1] & 0x7f) << 7;
2261 if (rQ
[cl_offset
+ 1] & 0x80) {
2263 cl_data
+= rQ
[cl_offset
+ 2] << 14;
2266 // end inline getTightCLength
2267 this._FBU
.bytes
= 1 + cl_header
+ cl_data
; // ctl + clength size + jpeg-data
2268 if (this._sock
.rQwait("TIGHT " + cmode
, this._FBU
.bytes
)) { return false; }
2270 // We have everything, render it
2271 this._sock
.rQskipBytes(1 + cl_header
); // shift off clt + compact length
2272 data
= this._sock
.rQshiftBytes(cl_data
);
2273 this._display
.imageRect(this._FBU
.x
, this._FBU
.y
, "image/" + cmode
, data
);
2276 var filterId
= rQ
[rQi
+ 1];
2277 if (filterId
=== 1) {
2278 if (!handlePalette()) { return false; }
2280 // Filter 0, Copy could be valid here, but servers don't send it as an explicit filter
2281 // Filter 2, Gradient is valid but not use if jpeg is enabled
2282 this._fail("Unexpected server message",
2283 "Unsupported tight subencoding received, " +
2284 "filter: " + filterId
);
2288 if (!handleCopy()) { return false; }
2293 this._FBU
.bytes
= 0;
2299 last_rect: function () {
2300 this._FBU
.rects
= 0;
2304 ExtendedDesktopSize: function () {
2305 this._FBU
.bytes
= 1;
2306 if (this._sock
.rQwait("ExtendedDesktopSize", this._FBU
.bytes
)) { return false; }
2308 this._supportsSetDesktopSize
= true;
2309 this._setCapability("resize", true);
2311 var number_of_screens
= this._sock
.rQpeek8();
2313 this._FBU
.bytes
= 4 + (number_of_screens
* 16);
2314 if (this._sock
.rQwait("ExtendedDesktopSize", this._FBU
.bytes
)) { return false; }
2316 this._sock
.rQskipBytes(1); // number-of-screens
2317 this._sock
.rQskipBytes(3); // padding
2319 for (var i
= 0; i
< number_of_screens
; i
+= 1) {
2320 // Save the id and flags of the first screen
2322 this._screen_id
= this._sock
.rQshiftBytes(4); // id
2323 this._sock
.rQskipBytes(2); // x-position
2324 this._sock
.rQskipBytes(2); // y-position
2325 this._sock
.rQskipBytes(2); // width
2326 this._sock
.rQskipBytes(2); // height
2327 this._screen_flags
= this._sock
.rQshiftBytes(4); // flags
2329 this._sock
.rQskipBytes(16);
2334 * The x-position indicates the reason for the change:
2336 * 0 - server resized on its own
2337 * 1 - this client requested the resize
2338 * 2 - another client requested the resize
2341 // We need to handle errors when we requested the resize.
2342 if (this._FBU
.x
=== 1 && this._FBU
.y
!== 0) {
2344 // The y-position indicates the status code from the server
2345 switch (this._FBU
.y
) {
2347 msg
= "Resize is administratively prohibited";
2350 msg
= "Out of resources";
2353 msg
= "Invalid screen layout";
2356 msg
= "Unknown reason";
2359 this._notification("Server did not accept the resize request: "
2362 this._resize(this._FBU
.width
, this._FBU
.height
);
2365 this._FBU
.bytes
= 0;
2366 this._FBU
.rects
-= 1;
2370 DesktopSize: function () {
2371 this._resize(this._FBU
.width
, this._FBU
.height
);
2372 this._FBU
.bytes
= 0;
2373 this._FBU
.rects
-= 1;
2377 Cursor: function () {
2378 Log
.Debug(">> set_cursor");
2379 var x
= this._FBU
.x
; // hotspot-x
2380 var y
= this._FBU
.y
; // hotspot-y
2381 var w
= this._FBU
.width
;
2382 var h
= this._FBU
.height
;
2384 var pixelslength
= w
* h
* 4;
2385 var masklength
= Math
.floor((w
+ 7) / 8) * h
;
2387 this._FBU
.bytes
= pixelslength
+ masklength
;
2388 if (this._sock
.rQwait("cursor encoding", this._FBU
.bytes
)) { return false; }
2390 this._display
.changeCursor(this._sock
.rQshiftBytes(pixelslength
),
2391 this._sock
.rQshiftBytes(masklength
),
2394 this._FBU
.bytes
= 0;
2397 Log
.Debug("<< set_cursor");
2401 QEMUExtendedKeyEvent: function () {
2404 // Old Safari doesn't support creating keyboard events
2406 var keyboardEvent
= document
.createEvent("keyboardEvent");
2407 if (keyboardEvent
.code
!== undefined) {
2408 this._qemuExtKeyEventSupported
= true;