]> git.proxmox.com Git - mirror_novnc.git/blob - core/rfb.js
Fix no auth "this._updateState" bug
[mirror_novnc.git] / core / rfb.js
1 /*
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)
6 *
7 * See README.md for usage and integration instructions.
8 *
9 * TIGHT decoder portion:
10 * (c) 2012 Michael Tinglof, Joe Balaz, Les Piech (Mercuri.ca)
11 */
12
13 /* [module]
14 * import Util from "./util";
15 * import Display from "./display";
16 * import { Keyboard, Mouse } from "./input/devices"
17 * import Websock from "./websock"
18 * import Base64 from "./base64";
19 * import DES from "./des";
20 * import KeyTable from "./input/keysym";
21 * import XtScancode from "./input/xtscancodes";
22 * import Inflator from "./inflator.mod";
23 */
24 /*jslint white: false, browser: true */
25 /*global window, Util, Display, Keyboard, Mouse, Websock, Websock_native, Base64, DES, KeyTable, Inflator, XtScancode */
26
27 /* [module] export default */ function RFB(defaults) {
28 "use strict";
29 if (!defaults) {
30 defaults = {};
31 }
32
33 this._rfb_host = '';
34 this._rfb_port = 5900;
35 this._rfb_password = '';
36 this._rfb_path = '';
37
38 this._rfb_connection_state = '';
39 this._rfb_init_state = '';
40 this._rfb_version = 0;
41 this._rfb_max_version = 3.8;
42 this._rfb_auth_scheme = '';
43 this._rfb_disconnect_reason = "";
44
45 this._rfb_tightvnc = false;
46 this._rfb_xvp_ver = 0;
47
48 // In preference order
49 this._encodings = [
50 ['COPYRECT', 0x01 ],
51 ['TIGHT', 0x07 ],
52 ['TIGHT_PNG', -260 ],
53 ['HEXTILE', 0x05 ],
54 ['RRE', 0x02 ],
55 ['RAW', 0x00 ],
56
57 // Psuedo-encoding settings
58
59 //['JPEG_quality_lo', -32 ],
60 ['JPEG_quality_med', -26 ],
61 //['JPEG_quality_hi', -23 ],
62 //['compress_lo', -255 ],
63 ['compress_hi', -247 ],
64
65 ['DesktopSize', -223 ],
66 ['last_rect', -224 ],
67 ['Cursor', -239 ],
68 ['QEMUExtendedKeyEvent', -258 ],
69 ['ExtendedDesktopSize', -308 ],
70 ['xvp', -309 ],
71 ['Fence', -312 ],
72 ['ContinuousUpdates', -313 ]
73 ];
74
75 this._encHandlers = {};
76 this._encNames = {};
77 this._encStats = {};
78
79 this._sock = null; // Websock object
80 this._display = null; // Display object
81 this._flushing = false; // Display flushing state
82 this._keyboard = null; // Keyboard input handler object
83 this._mouse = null; // Mouse input handler object
84 this._disconnTimer = null; // disconnection timer
85
86 this._supportsFence = false;
87
88 this._supportsContinuousUpdates = false;
89 this._enabledContinuousUpdates = false;
90
91 // Frame buffer update state
92 this._FBU = {
93 rects: 0,
94 subrects: 0, // RRE
95 lines: 0, // RAW
96 tiles: 0, // HEXTILE
97 bytes: 0,
98 x: 0,
99 y: 0,
100 width: 0,
101 height: 0,
102 encoding: 0,
103 subencoding: -1,
104 background: null,
105 zlib: [] // TIGHT zlib streams
106 };
107
108 this._fb_Bpp = 4;
109 this._fb_depth = 3;
110 this._fb_width = 0;
111 this._fb_height = 0;
112 this._fb_name = "";
113
114 this._destBuff = null;
115 this._paletteBuff = new Uint8Array(1024); // 256 * 4 (max palette size * max bytes-per-pixel)
116
117 this._rre_chunk_sz = 100;
118
119 this._timing = {
120 last_fbu: 0,
121 fbu_total: 0,
122 fbu_total_cnt: 0,
123 full_fbu_total: 0,
124 full_fbu_cnt: 0,
125
126 fbu_rt_start: 0,
127 fbu_rt_total: 0,
128 fbu_rt_cnt: 0,
129 pixels: 0
130 };
131
132 this._supportsSetDesktopSize = false;
133 this._screen_id = 0;
134 this._screen_flags = 0;
135
136 // Mouse state
137 this._mouse_buttonMask = 0;
138 this._mouse_arr = [];
139 this._viewportDragging = false;
140 this._viewportDragPos = {};
141 this._viewportHasMoved = false;
142
143 // QEMU Extended Key Event support - default to false
144 this._qemuExtKeyEventSupported = false;
145
146 // set the default value on user-facing properties
147 Util.set_defaults(this, defaults, {
148 'target': 'null', // VNC display rendering Canvas object
149 'focusContainer': document, // DOM element that captures keyboard input
150 'encrypt': false, // Use TLS/SSL/wss encryption
151 'true_color': true, // Request true color pixel data
152 'local_cursor': false, // Request locally rendered cursor
153 'shared': true, // Request shared mode
154 'view_only': false, // Disable client mouse/keyboard
155 'xvp_password_sep': '@', // Separator for XVP password fields
156 'disconnectTimeout': 3, // Time (s) to wait for disconnection
157 'wsProtocols': ['binary'], // Protocols to use in the WebSocket connection
158 'repeaterID': '', // [UltraVNC] RepeaterID to connect to
159 'viewportDrag': false, // Move the viewport on mouse drags
160
161 // Callback functions
162 'onUpdateState': function () { }, // onUpdateState(rfb, state, oldstate): connection state change
163 'onNotification': function () { }, // onNotification(rfb, msg, level, options): notification for UI
164 'onDisconnected': function () { }, // onDisconnected(rfb, reason): disconnection finished
165 'onPasswordRequired': function () { }, // onPasswordRequired(rfb, msg): VNC password is required
166 'onClipboard': function () { }, // onClipboard(rfb, text): RFB clipboard contents received
167 'onBell': function () { }, // onBell(rfb): RFB Bell message received
168 'onFBUReceive': function () { }, // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
169 'onFBUComplete': function () { }, // onFBUComplete(rfb, fbu): RFB FBU received and processed
170 'onFBResize': function () { }, // onFBResize(rfb, width, height): frame buffer resized
171 'onDesktopName': function () { }, // onDesktopName(rfb, name): desktop name received
172 'onXvpInit': function () { } // onXvpInit(version): XVP extensions active for this connection
173 });
174
175 // main setup
176 Util.Debug(">> RFB.constructor");
177
178 // populate encHandlers with bound versions
179 Object.keys(RFB.encodingHandlers).forEach(function (encName) {
180 this._encHandlers[encName] = RFB.encodingHandlers[encName].bind(this);
181 }.bind(this));
182
183 // Create lookup tables based on encoding number
184 for (var i = 0; i < this._encodings.length; i++) {
185 this._encHandlers[this._encodings[i][1]] = this._encHandlers[this._encodings[i][0]];
186 this._encNames[this._encodings[i][1]] = this._encodings[i][0];
187 this._encStats[this._encodings[i][1]] = [0, 0];
188 }
189
190 // NB: nothing that needs explicit teardown should be done
191 // before this point, since this can throw an exception
192 try {
193 this._display = new Display({target: this._target,
194 onFlush: this._onFlush.bind(this)});
195 } catch (exc) {
196 Util.Error("Display exception: " + exc);
197 throw exc;
198 }
199
200 this._keyboard = new Keyboard({target: this._focusContainer,
201 onKeyPress: this._handleKeyPress.bind(this)});
202
203 this._mouse = new Mouse({target: this._target,
204 onMouseButton: this._handleMouseButton.bind(this),
205 onMouseMove: this._handleMouseMove.bind(this),
206 notify: this._keyboard.sync.bind(this._keyboard)});
207
208 this._sock = new Websock();
209 this._sock.on('message', this._handle_message.bind(this));
210 this._sock.on('open', function () {
211 if ((this._rfb_connection_state === 'connecting') &&
212 (this._rfb_init_state === '')) {
213 this._rfb_init_state = 'ProtocolVersion';
214 Util.Debug("Starting VNC handshake");
215 } else {
216 this._fail("Unexpected server connection");
217 }
218 }.bind(this));
219 this._sock.on('close', function (e) {
220 Util.Warn("WebSocket on-close event");
221 var msg = "";
222 if (e.code) {
223 msg = " (code: " + e.code;
224 if (e.reason) {
225 msg += ", reason: " + e.reason;
226 }
227 msg += ")";
228 }
229 switch (this._rfb_connection_state) {
230 case 'disconnecting':
231 this._updateConnectionState('disconnected');
232 break;
233 case 'connecting':
234 this._fail('Failed to connect to server', msg);
235 break;
236 case 'connected':
237 // Handle disconnects that were initiated server-side
238 this._updateConnectionState('disconnecting');
239 this._updateConnectionState('disconnected');
240 break;
241 case 'disconnected':
242 this._fail("Unexpected server disconnect",
243 "Already disconnected: " + msg);
244 break;
245 default:
246 this._fail("Unexpected server disconnect",
247 "Not in any state yet: " + msg);
248 break;
249 }
250 this._sock.off('close');
251 }.bind(this));
252 this._sock.on('error', function (e) {
253 Util.Warn("WebSocket on-error event");
254 });
255
256 this._init_vars();
257 this._cleanup();
258
259 var rmode = this._display.get_render_mode();
260 Util.Info("Using native WebSockets, render mode: " + rmode);
261
262 Util.Debug("<< RFB.constructor");
263 };
264
265 (function() {
266 var _ = Util.Localisation.get;
267
268 RFB.prototype = {
269 // Public methods
270 connect: function (host, port, password, path) {
271 this._rfb_host = host;
272 this._rfb_port = port;
273 this._rfb_password = (password !== undefined) ? password : "";
274 this._rfb_path = (path !== undefined) ? path : "";
275
276 if (!this._rfb_host || !this._rfb_port) {
277 return this._fail(
278 _("Must set host and port"));
279 }
280
281 this._rfb_init_state = '';
282 this._updateConnectionState('connecting');
283 return true;
284 },
285
286 disconnect: function () {
287 this._updateConnectionState('disconnecting');
288 this._sock.off('error');
289 this._sock.off('message');
290 this._sock.off('open');
291 },
292
293 sendPassword: function (passwd) {
294 this._rfb_password = passwd;
295 setTimeout(this._init_msg.bind(this), 0);
296 },
297
298 sendCtrlAltDel: function () {
299 if (this._rfb_connection_state !== 'connected' || this._view_only) { return false; }
300 Util.Info("Sending Ctrl-Alt-Del");
301
302 RFB.messages.keyEvent(this._sock, KeyTable.XK_Control_L, 1);
303 RFB.messages.keyEvent(this._sock, KeyTable.XK_Alt_L, 1);
304 RFB.messages.keyEvent(this._sock, KeyTable.XK_Delete, 1);
305 RFB.messages.keyEvent(this._sock, KeyTable.XK_Delete, 0);
306 RFB.messages.keyEvent(this._sock, KeyTable.XK_Alt_L, 0);
307 RFB.messages.keyEvent(this._sock, KeyTable.XK_Control_L, 0);
308 return true;
309 },
310
311 xvpOp: function (ver, op) {
312 if (this._rfb_xvp_ver < ver) { return false; }
313 Util.Info("Sending XVP operation " + op + " (version " + ver + ")");
314 this._sock.send_string("\xFA\x00" + String.fromCharCode(ver) + String.fromCharCode(op));
315 return true;
316 },
317
318 xvpShutdown: function () {
319 return this.xvpOp(1, 2);
320 },
321
322 xvpReboot: function () {
323 return this.xvpOp(1, 3);
324 },
325
326 xvpReset: function () {
327 return this.xvpOp(1, 4);
328 },
329
330 // Send a key press. If 'down' is not specified then send a down key
331 // followed by an up key.
332 sendKey: function (keysym, down) {
333 if (this._rfb_connection_state !== 'connected' || this._view_only) { return false; }
334 if (typeof down !== 'undefined') {
335 Util.Info("Sending keysym (" + (down ? "down" : "up") + "): " + keysym);
336 RFB.messages.keyEvent(this._sock, keysym, down ? 1 : 0);
337 } else {
338 Util.Info("Sending keysym (down + up): " + keysym);
339 RFB.messages.keyEvent(this._sock, keysym, 1);
340 RFB.messages.keyEvent(this._sock, keysym, 0);
341 }
342 return true;
343 },
344
345 clipboardPasteFrom: function (text) {
346 if (this._rfb_connection_state !== 'connected') { return; }
347 RFB.messages.clientCutText(this._sock, text);
348 },
349
350 // Requests a change of remote desktop size. This message is an extension
351 // and may only be sent if we have received an ExtendedDesktopSize message
352 requestDesktopSize: function (width, height) {
353 if (this._rfb_connection_state !== 'connected' ||
354 this._view_only) {
355 return false;
356 }
357
358 if (this._supportsSetDesktopSize) {
359 RFB.messages.setDesktopSize(this._sock, width, height,
360 this._screen_id, this._screen_flags);
361 this._sock.flush();
362 return true;
363 } else {
364 return false;
365 }
366 },
367
368
369 // Private methods
370
371 _connect: function () {
372 Util.Debug(">> RFB.connect");
373 this._init_vars();
374
375 var uri;
376 if (typeof UsingSocketIO !== 'undefined') {
377 uri = 'http';
378 } else {
379 uri = this._encrypt ? 'wss' : 'ws';
380 }
381
382 uri += '://' + this._rfb_host + ':' + this._rfb_port + '/' + this._rfb_path;
383 Util.Info("connecting to " + uri);
384
385 try {
386 // WebSocket.onopen transitions to the RFB init states
387 this._sock.open(uri, this._wsProtocols);
388 } catch (e) {
389 if (e.name === 'SyntaxError') {
390 this._fail("Invalid host or port value given", e);
391 } else {
392 this._fail("Error while connecting", e);
393 }
394 }
395
396 Util.Debug("<< RFB.connect");
397 },
398
399 _disconnect: function () {
400 Util.Debug(">> RFB.disconnect");
401 this._cleanup();
402 this._sock.close();
403 this._print_stats();
404 Util.Debug("<< RFB.disconnect");
405 },
406
407 _init_vars: function () {
408 // reset state
409 this._FBU.rects = 0;
410 this._FBU.subrects = 0; // RRE and HEXTILE
411 this._FBU.lines = 0; // RAW
412 this._FBU.tiles = 0; // HEXTILE
413 this._FBU.zlibs = []; // TIGHT zlib encoders
414 this._mouse_buttonMask = 0;
415 this._mouse_arr = [];
416 this._rfb_tightvnc = false;
417
418 // Clear the per connection encoding stats
419 var i;
420 for (i = 0; i < this._encodings.length; i++) {
421 this._encStats[this._encodings[i][1]][0] = 0;
422 }
423
424 for (i = 0; i < 4; i++) {
425 this._FBU.zlibs[i] = new Inflator.Inflate();
426 }
427 },
428
429 _print_stats: function () {
430 Util.Info("Encoding stats for this connection:");
431 var i, s;
432 for (i = 0; i < this._encodings.length; i++) {
433 s = this._encStats[this._encodings[i][1]];
434 if (s[0] + s[1] > 0) {
435 Util.Info(" " + this._encodings[i][0] + ": " + s[0] + " rects");
436 }
437 }
438
439 Util.Info("Encoding stats since page load:");
440 for (i = 0; i < this._encodings.length; i++) {
441 s = this._encStats[this._encodings[i][1]];
442 Util.Info(" " + this._encodings[i][0] + ": " + s[1] + " rects");
443 }
444 },
445
446 _cleanup: function () {
447 if (this._display && this._display.get_context()) {
448 if (!this._view_only) { this._keyboard.ungrab(); }
449 if (!this._view_only) { this._mouse.ungrab(); }
450 this._display.defaultCursor();
451 if (Util.get_logging() !== 'debug') {
452 // Show noVNC logo on load and when disconnected, unless in
453 // debug mode
454 this._display.clear();
455 }
456 }
457 },
458
459 /*
460 * Connection states:
461 * connecting
462 * connected
463 * disconnecting
464 * disconnected - permanent state
465 */
466 _updateConnectionState: function (state) {
467 var oldstate = this._rfb_connection_state;
468
469 if (state === oldstate) {
470 Util.Debug("Already in state '" + state + "', ignoring");
471 return;
472 }
473
474 // The 'disconnected' state is permanent for each RFB object
475 if (oldstate === 'disconnected') {
476 Util.Error("Tried changing state of a disconnected RFB object");
477 return;
478 }
479
480 // Ensure proper transitions before doing anything
481 switch (state) {
482 case 'connected':
483 if (oldstate !== 'connecting') {
484 Util.Error("Bad transition to connected state, " +
485 "previous connection state: " + oldstate);
486 return;
487 }
488 break;
489
490 case 'disconnected':
491 if (oldstate !== 'disconnecting') {
492 Util.Error("Bad transition to disconnected state, " +
493 "previous connection state: " + oldstate);
494 return;
495 }
496 break;
497
498 case 'connecting':
499 if (oldstate !== '') {
500 Util.Error("Bad transition to connecting state, " +
501 "previous connection state: " + oldstate);
502 return;
503 }
504 break;
505
506 case 'disconnecting':
507 if (oldstate !== 'connected' && oldstate !== 'connecting') {
508 Util.Error("Bad transition to disconnecting state, " +
509 "previous connection state: " + oldstate);
510 return;
511 }
512 break;
513
514 default:
515 Util.Error("Unknown connection state: " + state);
516 return;
517 }
518
519 // State change actions
520
521 this._rfb_connection_state = state;
522 this._onUpdateState(this, state, oldstate);
523
524 var smsg = "New state '" + state + "', was '" + oldstate + "'.";
525 Util.Debug(smsg);
526
527 if (this._disconnTimer && state !== 'disconnecting') {
528 Util.Debug("Clearing disconnect timer");
529 clearTimeout(this._disconnTimer);
530 this._disconnTimer = null;
531
532 // make sure we don't get a double event
533 this._sock.off('close');
534 }
535
536 switch (state) {
537 case 'disconnected':
538 // Call onDisconnected callback after onUpdateState since
539 // we don't know if the UI only displays the latest message
540 if (this._rfb_disconnect_reason !== "") {
541 this._onDisconnected(this, this._rfb_disconnect_reason);
542 } else {
543 // No reason means clean disconnect
544 this._onDisconnected(this);
545 }
546 break;
547
548 case 'connecting':
549 this._connect();
550 break;
551
552 case 'disconnecting':
553 this._disconnect();
554
555 this._disconnTimer = setTimeout(function () {
556 this._rfb_disconnect_reason = _("Disconnect timeout");
557 this._updateConnectionState('disconnected');
558 }.bind(this), this._disconnectTimeout * 1000);
559 break;
560 }
561 },
562
563 /* Print errors and disconnect
564 *
565 * The optional parameter 'details' is used for information that
566 * should be logged but not sent to the user interface.
567 */
568 _fail: function (msg, details) {
569 var fullmsg = msg;
570 if (typeof details !== 'undefined') {
571 fullmsg = msg + " (" + details + ")";
572 }
573 switch (this._rfb_connection_state) {
574 case 'disconnecting':
575 Util.Error("Failed when disconnecting: " + fullmsg);
576 break;
577 case 'connected':
578 Util.Error("Failed while connected: " + fullmsg);
579 break;
580 case 'connecting':
581 Util.Error("Failed when connecting: " + fullmsg);
582 break;
583 default:
584 Util.Error("RFB failure: " + fullmsg);
585 break;
586 }
587 this._rfb_disconnect_reason = msg; //This is sent to the UI
588
589 // Transition to disconnected without waiting for socket to close
590 this._updateConnectionState('disconnecting');
591 this._updateConnectionState('disconnected');
592
593 return false;
594 },
595
596 /*
597 * Send a notification to the UI. Valid levels are:
598 * 'normal'|'warn'|'error'
599 *
600 * NOTE: Options could be added in the future.
601 * NOTE: If this function is called multiple times, remember that the
602 * interface could be only showing the latest notification.
603 */
604 _notification: function(msg, level, options) {
605 switch (level) {
606 case 'normal':
607 case 'warn':
608 case 'error':
609 Util.Debug("Notification[" + level + "]:" + msg);
610 break;
611 default:
612 Util.Error("Invalid notification level: " + level);
613 return;
614 }
615
616 if (options) {
617 this._onNotification(this, msg, level, options);
618 } else {
619 this._onNotification(this, msg, level);
620 }
621 },
622
623 _handle_message: function () {
624 if (this._sock.rQlen() === 0) {
625 Util.Warn("handle_message called on an empty receive queue");
626 return;
627 }
628
629 switch (this._rfb_connection_state) {
630 case 'disconnected':
631 Util.Error("Got data while disconnected");
632 break;
633 case 'connected':
634 while (true) {
635 if (this._flushing) {
636 break;
637 }
638 if (!this._normal_msg()) {
639 break;
640 }
641 if (this._sock.rQlen() === 0) {
642 break;
643 }
644 }
645 break;
646 default:
647 this._init_msg();
648 break;
649 }
650 },
651
652 _handleKeyPress: function (keyevent) {
653 if (this._view_only) { return; } // View only, skip keyboard, events
654
655 var down = (keyevent.type == 'keydown');
656 if (this._qemuExtKeyEventSupported) {
657 var scancode = XtScancode[keyevent.code];
658 if (scancode) {
659 var keysym = keyevent.keysym;
660 RFB.messages.QEMUExtendedKeyEvent(this._sock, keysym, down, scancode);
661 } else {
662 Util.Error('Unable to find a xt scancode for code = ' + keyevent.code);
663 }
664 } else {
665 keysym = keyevent.keysym.keysym;
666 RFB.messages.keyEvent(this._sock, keysym, down);
667 }
668 },
669
670 _handleMouseButton: function (x, y, down, bmask) {
671 if (down) {
672 this._mouse_buttonMask |= bmask;
673 } else {
674 this._mouse_buttonMask ^= bmask;
675 }
676
677 if (this._viewportDrag) {
678 if (down && !this._viewportDragging) {
679 this._viewportDragging = true;
680 this._viewportDragPos = {'x': x, 'y': y};
681
682 // Skip sending mouse events
683 return;
684 } else {
685 this._viewportDragging = false;
686
687 // If the viewport didn't actually move, then treat as a mouse click event
688 // Send the button down event here, as the button up event is sent at the end of this function
689 if (!this._viewportHasMoved && !this._view_only) {
690 RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), bmask);
691 }
692 this._viewportHasMoved = false;
693 }
694 }
695
696 if (this._view_only) { return; } // View only, skip mouse events
697
698 if (this._rfb_connection_state !== 'connected') { return; }
699 RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask);
700 },
701
702 _handleMouseMove: function (x, y) {
703 if (this._viewportDragging) {
704 var deltaX = this._viewportDragPos.x - x;
705 var deltaY = this._viewportDragPos.y - y;
706
707 // The goal is to trigger on a certain physical width, the
708 // devicePixelRatio brings us a bit closer but is not optimal.
709 var dragThreshold = 10 * (window.devicePixelRatio || 1);
710
711 if (this._viewportHasMoved || (Math.abs(deltaX) > dragThreshold ||
712 Math.abs(deltaY) > dragThreshold)) {
713 this._viewportHasMoved = true;
714
715 this._viewportDragPos = {'x': x, 'y': y};
716 this._display.viewportChangePos(deltaX, deltaY);
717 }
718
719 // Skip sending mouse events
720 return;
721 }
722
723 if (this._view_only) { return; } // View only, skip mouse events
724
725 if (this._rfb_connection_state !== 'connected') { return; }
726 RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask);
727 },
728
729 // Message Handlers
730
731 _negotiate_protocol_version: function () {
732 if (this._sock.rQlen() < 12) {
733 return this._fail("Error while negotiating with server",
734 "Incomplete protocol version");
735 }
736
737 var sversion = this._sock.rQshiftStr(12).substr(4, 7);
738 Util.Info("Server ProtocolVersion: " + sversion);
739 var is_repeater = 0;
740 switch (sversion) {
741 case "000.000": // UltraVNC repeater
742 is_repeater = 1;
743 break;
744 case "003.003":
745 case "003.006": // UltraVNC
746 case "003.889": // Apple Remote Desktop
747 this._rfb_version = 3.3;
748 break;
749 case "003.007":
750 this._rfb_version = 3.7;
751 break;
752 case "003.008":
753 case "004.000": // Intel AMT KVM
754 case "004.001": // RealVNC 4.6
755 case "005.000": // RealVNC 5.3
756 this._rfb_version = 3.8;
757 break;
758 default:
759 return this._fail("Unsupported server",
760 "Invalid server version: " + sversion);
761 }
762
763 if (is_repeater) {
764 var repeaterID = this._repeaterID;
765 while (repeaterID.length < 250) {
766 repeaterID += "\0";
767 }
768 this._sock.send_string(repeaterID);
769 return true;
770 }
771
772 if (this._rfb_version > this._rfb_max_version) {
773 this._rfb_version = this._rfb_max_version;
774 }
775
776 var cversion = "00" + parseInt(this._rfb_version, 10) +
777 ".00" + ((this._rfb_version * 10) % 10);
778 this._sock.send_string("RFB " + cversion + "\n");
779 Util.Debug('Sent ProtocolVersion: ' + cversion);
780
781 this._rfb_init_state = 'Security';
782 },
783
784 _negotiate_security: function () {
785 if (this._rfb_version >= 3.7) {
786 // Server sends supported list, client decides
787 var num_types = this._sock.rQshift8();
788 if (this._sock.rQwait("security type", num_types, 1)) { return false; }
789
790 if (num_types === 0) {
791 var strlen = this._sock.rQshift32();
792 var reason = this._sock.rQshiftStr(strlen);
793 return this._fail("Error while negotiating with server",
794 "Security failure: " + reason);
795 }
796
797 this._rfb_auth_scheme = 0;
798 var types = this._sock.rQshiftBytes(num_types);
799 Util.Debug("Server security types: " + types);
800 for (var i = 0; i < types.length; i++) {
801 switch (types[i]) {
802 case 1: // None
803 case 2: // VNC Authentication
804 case 16: // Tight
805 case 22: // XVP
806 if (types[i] > this._rfb_auth_scheme) {
807 this._rfb_auth_scheme = types[i];
808 }
809 break;
810 default:
811 break;
812 }
813 }
814
815 if (this._rfb_auth_scheme === 0) {
816 return this._fail("Unsupported server",
817 "Unsupported security types: " + types);
818 }
819
820 this._sock.send([this._rfb_auth_scheme]);
821 } else {
822 // Server decides
823 if (this._sock.rQwait("security scheme", 4)) { return false; }
824 this._rfb_auth_scheme = this._sock.rQshift32();
825 }
826
827 this._rfb_init_state = 'Authentication';
828 Util.Debug('Authenticating using scheme: ' + this._rfb_auth_scheme);
829
830 return this._init_msg(); // jump to authentication
831 },
832
833 // authentication
834 _negotiate_xvp_auth: function () {
835 var xvp_sep = this._xvp_password_sep;
836 var xvp_auth = this._rfb_password.split(xvp_sep);
837 if (xvp_auth.length < 3) {
838 var msg = 'XVP credentials required (user' + xvp_sep +
839 'target' + xvp_sep + 'password) -- got only ' + this._rfb_password;
840 this._onPasswordRequired(this, msg);
841 return false;
842 }
843
844 var xvp_auth_str = String.fromCharCode(xvp_auth[0].length) +
845 String.fromCharCode(xvp_auth[1].length) +
846 xvp_auth[0] +
847 xvp_auth[1];
848 this._sock.send_string(xvp_auth_str);
849 this._rfb_password = xvp_auth.slice(2).join(xvp_sep);
850 this._rfb_auth_scheme = 2;
851 return this._negotiate_authentication();
852 },
853
854 _negotiate_std_vnc_auth: function () {
855 if (this._rfb_password.length === 0) {
856 this._onPasswordRequired(this);
857 return false;
858 }
859
860 if (this._sock.rQwait("auth challenge", 16)) { return false; }
861
862 // TODO(directxman12): make genDES not require an Array
863 var challenge = Array.prototype.slice.call(this._sock.rQshiftBytes(16));
864 var response = RFB.genDES(this._rfb_password, challenge);
865 this._sock.send(response);
866 this._rfb_init_state = "SecurityResult";
867 return true;
868 },
869
870 _negotiate_tight_tunnels: function (numTunnels) {
871 var clientSupportedTunnelTypes = {
872 0: { vendor: 'TGHT', signature: 'NOTUNNEL' }
873 };
874 var serverSupportedTunnelTypes = {};
875 // receive tunnel capabilities
876 for (var i = 0; i < numTunnels; i++) {
877 var cap_code = this._sock.rQshift32();
878 var cap_vendor = this._sock.rQshiftStr(4);
879 var cap_signature = this._sock.rQshiftStr(8);
880 serverSupportedTunnelTypes[cap_code] = { vendor: cap_vendor, signature: cap_signature };
881 }
882
883 // choose the notunnel type
884 if (serverSupportedTunnelTypes[0]) {
885 if (serverSupportedTunnelTypes[0].vendor != clientSupportedTunnelTypes[0].vendor ||
886 serverSupportedTunnelTypes[0].signature != clientSupportedTunnelTypes[0].signature) {
887 return this._fail("Unsupported server",
888 "Client's tunnel type had the incorrect " +
889 "vendor or signature");
890 }
891 this._sock.send([0, 0, 0, 0]); // use NOTUNNEL
892 return false; // wait until we receive the sub auth count to continue
893 } else {
894 return this._fail("Unsupported server",
895 "Server wanted tunnels, but doesn't support " +
896 "the notunnel type");
897 }
898 },
899
900 _negotiate_tight_auth: function () {
901 if (!this._rfb_tightvnc) { // first pass, do the tunnel negotiation
902 if (this._sock.rQwait("num tunnels", 4)) { return false; }
903 var numTunnels = this._sock.rQshift32();
904 if (numTunnels > 0 && this._sock.rQwait("tunnel capabilities", 16 * numTunnels, 4)) { return false; }
905
906 this._rfb_tightvnc = true;
907
908 if (numTunnels > 0) {
909 this._negotiate_tight_tunnels(numTunnels);
910 return false; // wait until we receive the sub auth to continue
911 }
912 }
913
914 // second pass, do the sub-auth negotiation
915 if (this._sock.rQwait("sub auth count", 4)) { return false; }
916 var subAuthCount = this._sock.rQshift32();
917 if (subAuthCount === 0) { // empty sub-auth list received means 'no auth' subtype selected
918 this._rfb_init_state = 'SecurityResult';
919 return true;
920 }
921
922 if (this._sock.rQwait("sub auth capabilities", 16 * subAuthCount, 4)) { return false; }
923
924 var clientSupportedTypes = {
925 'STDVNOAUTH__': 1,
926 'STDVVNCAUTH_': 2
927 };
928
929 var serverSupportedTypes = [];
930
931 for (var i = 0; i < subAuthCount; i++) {
932 var capNum = this._sock.rQshift32();
933 var capabilities = this._sock.rQshiftStr(12);
934 serverSupportedTypes.push(capabilities);
935 }
936
937 for (var authType in clientSupportedTypes) {
938 if (serverSupportedTypes.indexOf(authType) != -1) {
939 this._sock.send([0, 0, 0, clientSupportedTypes[authType]]);
940
941 switch (authType) {
942 case 'STDVNOAUTH__': // no auth
943 this._rfb_init_state = 'SecurityResult';
944 return true;
945 case 'STDVVNCAUTH_': // VNC auth
946 this._rfb_auth_scheme = 2;
947 return this._init_msg();
948 default:
949 return this._fail("Unsupported server",
950 "Unsupported tiny auth scheme: " +
951 authType);
952 }
953 }
954 }
955
956 return this._fail("Unsupported server",
957 "No supported sub-auth types!");
958 },
959
960 _negotiate_authentication: function () {
961 switch (this._rfb_auth_scheme) {
962 case 0: // connection failed
963 if (this._sock.rQwait("auth reason", 4)) { return false; }
964 var strlen = this._sock.rQshift32();
965 var reason = this._sock.rQshiftStr(strlen);
966 return this._fail("Authentication failure", reason);
967
968 case 1: // no auth
969 if (this._rfb_version >= 3.8) {
970 this._rfb_init_state = 'SecurityResult';
971 return true;
972 }
973 this._rfb_init_state = 'ClientInitialisation';
974 return this._init_msg();
975
976 case 22: // XVP auth
977 return this._negotiate_xvp_auth();
978
979 case 2: // VNC authentication
980 return this._negotiate_std_vnc_auth();
981
982 case 16: // TightVNC Security Type
983 return this._negotiate_tight_auth();
984
985 default:
986 return this._fail("Unsupported server",
987 "Unsupported auth scheme: " +
988 this._rfb_auth_scheme);
989 }
990 },
991
992 _handle_security_result: function () {
993 if (this._sock.rQwait('VNC auth response ', 4)) { return false; }
994 switch (this._sock.rQshift32()) {
995 case 0: // OK
996 this._rfb_init_state = 'ClientInitialisation';
997 Util.Debug('Authentication OK');
998 return this._init_msg();
999 case 1: // failed
1000 if (this._rfb_version >= 3.8) {
1001 var length = this._sock.rQshift32();
1002 if (this._sock.rQwait("SecurityResult reason", length, 8)) { return false; }
1003 var reason = this._sock.rQshiftStr(length);
1004 return this._fail("Authentication failure", reason);
1005 } else {
1006 return this._fail("Authentication failure");
1007 }
1008 return false;
1009 case 2:
1010 return this._fail("Too many authentication attempts");
1011 default:
1012 return this._fail("Unsupported server",
1013 "Unknown SecurityResult");
1014 }
1015 },
1016
1017 _negotiate_server_init: function () {
1018 if (this._sock.rQwait("server initialization", 24)) { return false; }
1019
1020 /* Screen size */
1021 this._fb_width = this._sock.rQshift16();
1022 this._fb_height = this._sock.rQshift16();
1023 this._destBuff = new Uint8Array(this._fb_width * this._fb_height * 4);
1024
1025 /* PIXEL_FORMAT */
1026 var bpp = this._sock.rQshift8();
1027 var depth = this._sock.rQshift8();
1028 var big_endian = this._sock.rQshift8();
1029 var true_color = this._sock.rQshift8();
1030
1031 var red_max = this._sock.rQshift16();
1032 var green_max = this._sock.rQshift16();
1033 var blue_max = this._sock.rQshift16();
1034 var red_shift = this._sock.rQshift8();
1035 var green_shift = this._sock.rQshift8();
1036 var blue_shift = this._sock.rQshift8();
1037 this._sock.rQskipBytes(3); // padding
1038
1039 // NB(directxman12): we don't want to call any callbacks or print messages until
1040 // *after* we're past the point where we could backtrack
1041
1042 /* Connection name/title */
1043 var name_length = this._sock.rQshift32();
1044 if (this._sock.rQwait('server init name', name_length, 24)) { return false; }
1045 this._fb_name = Util.decodeUTF8(this._sock.rQshiftStr(name_length));
1046
1047 if (this._rfb_tightvnc) {
1048 if (this._sock.rQwait('TightVNC extended server init header', 8, 24 + name_length)) { return false; }
1049 // In TightVNC mode, ServerInit message is extended
1050 var numServerMessages = this._sock.rQshift16();
1051 var numClientMessages = this._sock.rQshift16();
1052 var numEncodings = this._sock.rQshift16();
1053 this._sock.rQskipBytes(2); // padding
1054
1055 var totalMessagesLength = (numServerMessages + numClientMessages + numEncodings) * 16;
1056 if (this._sock.rQwait('TightVNC extended server init header', totalMessagesLength, 32 + name_length)) { return false; }
1057
1058 // we don't actually do anything with the capability information that TIGHT sends,
1059 // so we just skip the all of this.
1060
1061 // TIGHT server message capabilities
1062 this._sock.rQskipBytes(16 * numServerMessages);
1063
1064 // TIGHT client message capabilities
1065 this._sock.rQskipBytes(16 * numClientMessages);
1066
1067 // TIGHT encoding capabilities
1068 this._sock.rQskipBytes(16 * numEncodings);
1069 }
1070
1071 // NB(directxman12): these are down here so that we don't run them multiple times
1072 // if we backtrack
1073 Util.Info("Screen: " + this._fb_width + "x" + this._fb_height +
1074 ", bpp: " + bpp + ", depth: " + depth +
1075 ", big_endian: " + big_endian +
1076 ", true_color: " + true_color +
1077 ", red_max: " + red_max +
1078 ", green_max: " + green_max +
1079 ", blue_max: " + blue_max +
1080 ", red_shift: " + red_shift +
1081 ", green_shift: " + green_shift +
1082 ", blue_shift: " + blue_shift);
1083
1084 if (big_endian !== 0) {
1085 Util.Warn("Server native endian is not little endian");
1086 }
1087
1088 if (red_shift !== 16) {
1089 Util.Warn("Server native red-shift is not 16");
1090 }
1091
1092 if (blue_shift !== 0) {
1093 Util.Warn("Server native blue-shift is not 0");
1094 }
1095
1096 // we're past the point where we could backtrack, so it's safe to call this
1097 this._onDesktopName(this, this._fb_name);
1098
1099 if (this._true_color && this._fb_name === "Intel(r) AMT KVM") {
1100 Util.Warn("Intel AMT KVM only supports 8/16 bit depths. Disabling true color");
1101 this._true_color = false;
1102 }
1103
1104 this._display.set_true_color(this._true_color);
1105 this._display.resize(this._fb_width, this._fb_height);
1106 this._onFBResize(this, this._fb_width, this._fb_height);
1107
1108 if (!this._view_only) { this._keyboard.grab(); }
1109 if (!this._view_only) { this._mouse.grab(); }
1110
1111 if (this._true_color) {
1112 this._fb_Bpp = 4;
1113 this._fb_depth = 3;
1114 } else {
1115 this._fb_Bpp = 1;
1116 this._fb_depth = 1;
1117 }
1118
1119 RFB.messages.pixelFormat(this._sock, this._fb_Bpp, this._fb_depth, this._true_color);
1120 RFB.messages.clientEncodings(this._sock, this._encodings, this._local_cursor, this._true_color);
1121 RFB.messages.fbUpdateRequest(this._sock, false, 0, 0, this._fb_width, this._fb_height);
1122
1123 this._timing.fbu_rt_start = (new Date()).getTime();
1124 this._timing.pixels = 0;
1125
1126 this._updateConnectionState('connected');
1127 return true;
1128 },
1129
1130 /* RFB protocol initialization states:
1131 * ProtocolVersion
1132 * Security
1133 * Authentication
1134 * SecurityResult
1135 * ClientInitialization - not triggered by server message
1136 * ServerInitialization
1137 */
1138 _init_msg: function () {
1139 switch (this._rfb_init_state) {
1140 case 'ProtocolVersion':
1141 return this._negotiate_protocol_version();
1142
1143 case 'Security':
1144 return this._negotiate_security();
1145
1146 case 'Authentication':
1147 return this._negotiate_authentication();
1148
1149 case 'SecurityResult':
1150 return this._handle_security_result();
1151
1152 case 'ClientInitialisation':
1153 this._sock.send([this._shared ? 1 : 0]); // ClientInitialisation
1154 this._rfb_init_state = 'ServerInitialisation';
1155 return true;
1156
1157 case 'ServerInitialisation':
1158 return this._negotiate_server_init();
1159
1160 default:
1161 return this._fail("Internal error", "Unknown init state: " +
1162 this._rfb_init_state);
1163 }
1164 },
1165
1166 _handle_set_colour_map_msg: function () {
1167 Util.Debug("SetColorMapEntries");
1168 this._sock.rQskip8(); // Padding
1169
1170 var first_colour = this._sock.rQshift16();
1171 var num_colours = this._sock.rQshift16();
1172 if (this._sock.rQwait('SetColorMapEntries', num_colours * 6, 6)) { return false; }
1173
1174 for (var c = 0; c < num_colours; c++) {
1175 var red = parseInt(this._sock.rQshift16() / 256, 10);
1176 var green = parseInt(this._sock.rQshift16() / 256, 10);
1177 var blue = parseInt(this._sock.rQshift16() / 256, 10);
1178 this._display.set_colourMap([blue, green, red], first_colour + c);
1179 }
1180 Util.Debug("colourMap: " + this._display.get_colourMap());
1181 Util.Info("Registered " + num_colours + " colourMap entries");
1182
1183 return true;
1184 },
1185
1186 _handle_server_cut_text: function () {
1187 Util.Debug("ServerCutText");
1188 if (this._sock.rQwait("ServerCutText header", 7, 1)) { return false; }
1189 this._sock.rQskipBytes(3); // Padding
1190 var length = this._sock.rQshift32();
1191 if (this._sock.rQwait("ServerCutText", length, 8)) { return false; }
1192
1193 var text = this._sock.rQshiftStr(length);
1194 this._onClipboard(this, text);
1195
1196 return true;
1197 },
1198
1199 _handle_server_fence_msg: function() {
1200 if (this._sock.rQwait("ServerFence header", 8, 1)) { return false; }
1201 this._sock.rQskipBytes(3); // Padding
1202 var flags = this._sock.rQshift32();
1203 var length = this._sock.rQshift8();
1204
1205 if (this._sock.rQwait("ServerFence payload", length, 9)) { return false; }
1206
1207 if (length > 64) {
1208 Util.Warn("Bad payload length (" + length + ") in fence response");
1209 length = 64;
1210 }
1211
1212 var payload = this._sock.rQshiftStr(length);
1213
1214 this._supportsFence = true;
1215
1216 /*
1217 * Fence flags
1218 *
1219 * (1<<0) - BlockBefore
1220 * (1<<1) - BlockAfter
1221 * (1<<2) - SyncNext
1222 * (1<<31) - Request
1223 */
1224
1225 if (!(flags & (1<<31))) {
1226 return this._fail("Internal error",
1227 "Unexpected fence response");
1228 }
1229
1230 // Filter out unsupported flags
1231 // FIXME: support syncNext
1232 flags &= (1<<0) | (1<<1);
1233
1234 // BlockBefore and BlockAfter are automatically handled by
1235 // the fact that we process each incoming message
1236 // synchronuosly.
1237 RFB.messages.clientFence(this._sock, flags, payload);
1238
1239 return true;
1240 },
1241
1242 _handle_xvp_msg: function () {
1243 if (this._sock.rQwait("XVP version and message", 3, 1)) { return false; }
1244 this._sock.rQskip8(); // Padding
1245 var xvp_ver = this._sock.rQshift8();
1246 var xvp_msg = this._sock.rQshift8();
1247
1248 switch (xvp_msg) {
1249 case 0: // XVP_FAIL
1250 Util.Error("Operation Failed");
1251 this._notification("XVP Operation Failed", 'error');
1252 break;
1253 case 1: // XVP_INIT
1254 this._rfb_xvp_ver = xvp_ver;
1255 Util.Info("XVP extensions enabled (version " + this._rfb_xvp_ver + ")");
1256 this._onXvpInit(this._rfb_xvp_ver);
1257 break;
1258 default:
1259 this._fail("Unexpected server message",
1260 "Illegal server XVP message " + xvp_msg);
1261 break;
1262 }
1263
1264 return true;
1265 },
1266
1267 _normal_msg: function () {
1268 var msg_type;
1269
1270 if (this._FBU.rects > 0) {
1271 msg_type = 0;
1272 } else {
1273 msg_type = this._sock.rQshift8();
1274 }
1275
1276 switch (msg_type) {
1277 case 0: // FramebufferUpdate
1278 var ret = this._framebufferUpdate();
1279 if (ret && !this._enabledContinuousUpdates) {
1280 RFB.messages.fbUpdateRequest(this._sock, true, 0, 0,
1281 this._fb_width, this._fb_height);
1282 }
1283 return ret;
1284
1285 case 1: // SetColorMapEntries
1286 return this._handle_set_colour_map_msg();
1287
1288 case 2: // Bell
1289 Util.Debug("Bell");
1290 this._onBell(this);
1291 return true;
1292
1293 case 3: // ServerCutText
1294 return this._handle_server_cut_text();
1295
1296 case 150: // EndOfContinuousUpdates
1297 var first = !(this._supportsContinuousUpdates);
1298 this._supportsContinuousUpdates = true;
1299 this._enabledContinuousUpdates = false;
1300 if (first) {
1301 this._enabledContinuousUpdates = true;
1302 this._updateContinuousUpdates();
1303 Util.Info("Enabling continuous updates.");
1304 } else {
1305 // FIXME: We need to send a framebufferupdaterequest here
1306 // if we add support for turning off continuous updates
1307 }
1308 return true;
1309
1310 case 248: // ServerFence
1311 return this._handle_server_fence_msg();
1312
1313 case 250: // XVP
1314 return this._handle_xvp_msg();
1315
1316 default:
1317 this._fail("Unexpected server message", "Type:" + msg_type);
1318 Util.Debug("sock.rQslice(0, 30): " + this._sock.rQslice(0, 30));
1319 return true;
1320 }
1321 },
1322
1323 _onFlush: function() {
1324 this._flushing = false;
1325 // Resume processing
1326 if (this._sock.rQlen() > 0) {
1327 this._handle_message();
1328 }
1329 },
1330
1331 _framebufferUpdate: function () {
1332 var ret = true;
1333 var now;
1334
1335 if (this._FBU.rects === 0) {
1336 if (this._sock.rQwait("FBU header", 3, 1)) { return false; }
1337 this._sock.rQskip8(); // Padding
1338 this._FBU.rects = this._sock.rQshift16();
1339 this._FBU.bytes = 0;
1340 this._timing.cur_fbu = 0;
1341 if (this._timing.fbu_rt_start > 0) {
1342 now = (new Date()).getTime();
1343 Util.Info("First FBU latency: " + (now - this._timing.fbu_rt_start));
1344 }
1345
1346 // Make sure the previous frame is fully rendered first
1347 // to avoid building up an excessive queue
1348 if (this._display.pending()) {
1349 this._flushing = true;
1350 this._display.flush();
1351 return false;
1352 }
1353 }
1354
1355 while (this._FBU.rects > 0) {
1356 if (this._rfb_connection_state !== 'connected') { return false; }
1357
1358 if (this._sock.rQwait("FBU", this._FBU.bytes)) { return false; }
1359 if (this._FBU.bytes === 0) {
1360 if (this._sock.rQwait("rect header", 12)) { return false; }
1361 /* New FramebufferUpdate */
1362
1363 var hdr = this._sock.rQshiftBytes(12);
1364 this._FBU.x = (hdr[0] << 8) + hdr[1];
1365 this._FBU.y = (hdr[2] << 8) + hdr[3];
1366 this._FBU.width = (hdr[4] << 8) + hdr[5];
1367 this._FBU.height = (hdr[6] << 8) + hdr[7];
1368 this._FBU.encoding = parseInt((hdr[8] << 24) + (hdr[9] << 16) +
1369 (hdr[10] << 8) + hdr[11], 10);
1370
1371 this._onFBUReceive(this,
1372 {'x': this._FBU.x, 'y': this._FBU.y,
1373 'width': this._FBU.width, 'height': this._FBU.height,
1374 'encoding': this._FBU.encoding,
1375 'encodingName': this._encNames[this._FBU.encoding]});
1376
1377 if (!this._encNames[this._FBU.encoding]) {
1378 this._fail("Unexpected server message",
1379 "Unsupported encoding " +
1380 this._FBU.encoding);
1381 return false;
1382 }
1383 }
1384
1385 this._timing.last_fbu = (new Date()).getTime();
1386
1387 ret = this._encHandlers[this._FBU.encoding]();
1388
1389 now = (new Date()).getTime();
1390 this._timing.cur_fbu += (now - this._timing.last_fbu);
1391
1392 if (ret) {
1393 this._encStats[this._FBU.encoding][0]++;
1394 this._encStats[this._FBU.encoding][1]++;
1395 this._timing.pixels += this._FBU.width * this._FBU.height;
1396 }
1397
1398 if (this._timing.pixels >= (this._fb_width * this._fb_height)) {
1399 if ((this._FBU.width === this._fb_width && this._FBU.height === this._fb_height) ||
1400 this._timing.fbu_rt_start > 0) {
1401 this._timing.full_fbu_total += this._timing.cur_fbu;
1402 this._timing.full_fbu_cnt++;
1403 Util.Info("Timing of full FBU, curr: " +
1404 this._timing.cur_fbu + ", total: " +
1405 this._timing.full_fbu_total + ", cnt: " +
1406 this._timing.full_fbu_cnt + ", avg: " +
1407 (this._timing.full_fbu_total / this._timing.full_fbu_cnt));
1408 }
1409
1410 if (this._timing.fbu_rt_start > 0) {
1411 var fbu_rt_diff = now - this._timing.fbu_rt_start;
1412 this._timing.fbu_rt_total += fbu_rt_diff;
1413 this._timing.fbu_rt_cnt++;
1414 Util.Info("full FBU round-trip, cur: " +
1415 fbu_rt_diff + ", total: " +
1416 this._timing.fbu_rt_total + ", cnt: " +
1417 this._timing.fbu_rt_cnt + ", avg: " +
1418 (this._timing.fbu_rt_total / this._timing.fbu_rt_cnt));
1419 this._timing.fbu_rt_start = 0;
1420 }
1421 }
1422
1423 if (!ret) { return ret; } // need more data
1424 }
1425
1426 this._display.flip();
1427
1428 this._onFBUComplete(this,
1429 {'x': this._FBU.x, 'y': this._FBU.y,
1430 'width': this._FBU.width, 'height': this._FBU.height,
1431 'encoding': this._FBU.encoding,
1432 'encodingName': this._encNames[this._FBU.encoding]});
1433
1434 return true; // We finished this FBU
1435 },
1436
1437 _updateContinuousUpdates: function() {
1438 if (!this._enabledContinuousUpdates) { return; }
1439
1440 RFB.messages.enableContinuousUpdates(this._sock, true, 0, 0,
1441 this._fb_width, this._fb_height);
1442 }
1443 };
1444
1445 Util.make_properties(RFB, [
1446 ['target', 'wo', 'dom'], // VNC display rendering Canvas object
1447 ['focusContainer', 'wo', 'dom'], // DOM element that captures keyboard input
1448 ['encrypt', 'rw', 'bool'], // Use TLS/SSL/wss encryption
1449 ['true_color', 'rw', 'bool'], // Request true color pixel data
1450 ['local_cursor', 'rw', 'bool'], // Request locally rendered cursor
1451 ['shared', 'rw', 'bool'], // Request shared mode
1452 ['view_only', 'rw', 'bool'], // Disable client mouse/keyboard
1453 ['xvp_password_sep', 'rw', 'str'], // Separator for XVP password fields
1454 ['disconnectTimeout', 'rw', 'int'], // Time (s) to wait for disconnection
1455 ['wsProtocols', 'rw', 'arr'], // Protocols to use in the WebSocket connection
1456 ['repeaterID', 'rw', 'str'], // [UltraVNC] RepeaterID to connect to
1457 ['viewportDrag', 'rw', 'bool'], // Move the viewport on mouse drags
1458
1459 // Callback functions
1460 ['onUpdateState', 'rw', 'func'], // onUpdateState(rfb, state, oldstate): connection state change
1461 ['onNotification', 'rw', 'func'], // onNotification(rfb, msg, level, options): notification for the UI
1462 ['onDisconnected', 'rw', 'func'], // onDisconnected(rfb, reason): disconnection finished
1463 ['onPasswordRequired', 'rw', 'func'], // onPasswordRequired(rfb, msg): VNC password is required
1464 ['onClipboard', 'rw', 'func'], // onClipboard(rfb, text): RFB clipboard contents received
1465 ['onBell', 'rw', 'func'], // onBell(rfb): RFB Bell message received
1466 ['onFBUReceive', 'rw', 'func'], // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
1467 ['onFBUComplete', 'rw', 'func'], // onFBUComplete(rfb, fbu): RFB FBU received and processed
1468 ['onFBResize', 'rw', 'func'], // onFBResize(rfb, width, height): frame buffer resized
1469 ['onDesktopName', 'rw', 'func'], // onDesktopName(rfb, name): desktop name received
1470 ['onXvpInit', 'rw', 'func'] // onXvpInit(version): XVP extensions active for this connection
1471 ]);
1472
1473 RFB.prototype.set_local_cursor = function (cursor) {
1474 if (!cursor || (cursor in {'0': 1, 'no': 1, 'false': 1})) {
1475 this._local_cursor = false;
1476 this._display.disableLocalCursor(); //Only show server-side cursor
1477 } else {
1478 if (this._display.get_cursor_uri()) {
1479 this._local_cursor = true;
1480 } else {
1481 Util.Warn("Browser does not support local cursor");
1482 this._display.disableLocalCursor();
1483 }
1484 }
1485 };
1486
1487 RFB.prototype.get_display = function () { return this._display; };
1488 RFB.prototype.get_keyboard = function () { return this._keyboard; };
1489 RFB.prototype.get_mouse = function () { return this._mouse; };
1490
1491 // Class Methods
1492 RFB.messages = {
1493 keyEvent: function (sock, keysym, down) {
1494 var buff = sock._sQ;
1495 var offset = sock._sQlen;
1496
1497 buff[offset] = 4; // msg-type
1498 buff[offset + 1] = down;
1499
1500 buff[offset + 2] = 0;
1501 buff[offset + 3] = 0;
1502
1503 buff[offset + 4] = (keysym >> 24);
1504 buff[offset + 5] = (keysym >> 16);
1505 buff[offset + 6] = (keysym >> 8);
1506 buff[offset + 7] = keysym;
1507
1508 sock._sQlen += 8;
1509 sock.flush();
1510 },
1511
1512 QEMUExtendedKeyEvent: function (sock, keysym, down, keycode) {
1513 function getRFBkeycode(xt_scancode) {
1514 var upperByte = (keycode >> 8);
1515 var lowerByte = (keycode & 0x00ff);
1516 if (upperByte === 0xe0 && lowerByte < 0x7f) {
1517 lowerByte = lowerByte | 0x80;
1518 return lowerByte;
1519 }
1520 return xt_scancode;
1521 }
1522
1523 var buff = sock._sQ;
1524 var offset = sock._sQlen;
1525
1526 buff[offset] = 255; // msg-type
1527 buff[offset + 1] = 0; // sub msg-type
1528
1529 buff[offset + 2] = (down >> 8);
1530 buff[offset + 3] = down;
1531
1532 buff[offset + 4] = (keysym >> 24);
1533 buff[offset + 5] = (keysym >> 16);
1534 buff[offset + 6] = (keysym >> 8);
1535 buff[offset + 7] = keysym;
1536
1537 var RFBkeycode = getRFBkeycode(keycode);
1538
1539 buff[offset + 8] = (RFBkeycode >> 24);
1540 buff[offset + 9] = (RFBkeycode >> 16);
1541 buff[offset + 10] = (RFBkeycode >> 8);
1542 buff[offset + 11] = RFBkeycode;
1543
1544 sock._sQlen += 12;
1545 sock.flush();
1546 },
1547
1548 pointerEvent: function (sock, x, y, mask) {
1549 var buff = sock._sQ;
1550 var offset = sock._sQlen;
1551
1552 buff[offset] = 5; // msg-type
1553
1554 buff[offset + 1] = mask;
1555
1556 buff[offset + 2] = x >> 8;
1557 buff[offset + 3] = x;
1558
1559 buff[offset + 4] = y >> 8;
1560 buff[offset + 5] = y;
1561
1562 sock._sQlen += 6;
1563 sock.flush();
1564 },
1565
1566 // TODO(directxman12): make this unicode compatible?
1567 clientCutText: function (sock, text) {
1568 var buff = sock._sQ;
1569 var offset = sock._sQlen;
1570
1571 buff[offset] = 6; // msg-type
1572
1573 buff[offset + 1] = 0; // padding
1574 buff[offset + 2] = 0; // padding
1575 buff[offset + 3] = 0; // padding
1576
1577 var n = text.length;
1578
1579 buff[offset + 4] = n >> 24;
1580 buff[offset + 5] = n >> 16;
1581 buff[offset + 6] = n >> 8;
1582 buff[offset + 7] = n;
1583
1584 for (var i = 0; i < n; i++) {
1585 buff[offset + 8 + i] = text.charCodeAt(i);
1586 }
1587
1588 sock._sQlen += 8 + n;
1589 sock.flush();
1590 },
1591
1592 setDesktopSize: function (sock, width, height, id, flags) {
1593 var buff = sock._sQ;
1594 var offset = sock._sQlen;
1595
1596 buff[offset] = 251; // msg-type
1597 buff[offset + 1] = 0; // padding
1598 buff[offset + 2] = width >> 8; // width
1599 buff[offset + 3] = width;
1600 buff[offset + 4] = height >> 8; // height
1601 buff[offset + 5] = height;
1602
1603 buff[offset + 6] = 1; // number-of-screens
1604 buff[offset + 7] = 0; // padding
1605
1606 // screen array
1607 buff[offset + 8] = id >> 24; // id
1608 buff[offset + 9] = id >> 16;
1609 buff[offset + 10] = id >> 8;
1610 buff[offset + 11] = id;
1611 buff[offset + 12] = 0; // x-position
1612 buff[offset + 13] = 0;
1613 buff[offset + 14] = 0; // y-position
1614 buff[offset + 15] = 0;
1615 buff[offset + 16] = width >> 8; // width
1616 buff[offset + 17] = width;
1617 buff[offset + 18] = height >> 8; // height
1618 buff[offset + 19] = height;
1619 buff[offset + 20] = flags >> 24; // flags
1620 buff[offset + 21] = flags >> 16;
1621 buff[offset + 22] = flags >> 8;
1622 buff[offset + 23] = flags;
1623
1624 sock._sQlen += 24;
1625 sock.flush();
1626 },
1627
1628 clientFence: function (sock, flags, payload) {
1629 var buff = sock._sQ;
1630 var offset = sock._sQlen;
1631
1632 buff[offset] = 248; // msg-type
1633
1634 buff[offset + 1] = 0; // padding
1635 buff[offset + 2] = 0; // padding
1636 buff[offset + 3] = 0; // padding
1637
1638 buff[offset + 4] = flags >> 24; // flags
1639 buff[offset + 5] = flags >> 16;
1640 buff[offset + 6] = flags >> 8;
1641 buff[offset + 7] = flags;
1642
1643 var n = payload.length;
1644
1645 buff[offset + 8] = n; // length
1646
1647 for (var i = 0; i < n; i++) {
1648 buff[offset + 9 + i] = payload.charCodeAt(i);
1649 }
1650
1651 sock._sQlen += 9 + n;
1652 sock.flush();
1653 },
1654
1655 enableContinuousUpdates: function (sock, enable, x, y, width, height) {
1656 var buff = sock._sQ;
1657 var offset = sock._sQlen;
1658
1659 buff[offset] = 150; // msg-type
1660 buff[offset + 1] = enable; // enable-flag
1661
1662 buff[offset + 2] = x >> 8; // x
1663 buff[offset + 3] = x;
1664 buff[offset + 4] = y >> 8; // y
1665 buff[offset + 5] = y;
1666 buff[offset + 6] = width >> 8; // width
1667 buff[offset + 7] = width;
1668 buff[offset + 8] = height >> 8; // height
1669 buff[offset + 9] = height;
1670
1671 sock._sQlen += 10;
1672 sock.flush();
1673 },
1674
1675 pixelFormat: function (sock, bpp, depth, true_color) {
1676 var buff = sock._sQ;
1677 var offset = sock._sQlen;
1678
1679 buff[offset] = 0; // msg-type
1680
1681 buff[offset + 1] = 0; // padding
1682 buff[offset + 2] = 0; // padding
1683 buff[offset + 3] = 0; // padding
1684
1685 buff[offset + 4] = bpp * 8; // bits-per-pixel
1686 buff[offset + 5] = depth * 8; // depth
1687 buff[offset + 6] = 0; // little-endian
1688 buff[offset + 7] = true_color ? 1 : 0; // true-color
1689
1690 buff[offset + 8] = 0; // red-max
1691 buff[offset + 9] = 255; // red-max
1692
1693 buff[offset + 10] = 0; // green-max
1694 buff[offset + 11] = 255; // green-max
1695
1696 buff[offset + 12] = 0; // blue-max
1697 buff[offset + 13] = 255; // blue-max
1698
1699 buff[offset + 14] = 16; // red-shift
1700 buff[offset + 15] = 8; // green-shift
1701 buff[offset + 16] = 0; // blue-shift
1702
1703 buff[offset + 17] = 0; // padding
1704 buff[offset + 18] = 0; // padding
1705 buff[offset + 19] = 0; // padding
1706
1707 sock._sQlen += 20;
1708 sock.flush();
1709 },
1710
1711 clientEncodings: function (sock, encodings, local_cursor, true_color) {
1712 var buff = sock._sQ;
1713 var offset = sock._sQlen;
1714
1715 buff[offset] = 2; // msg-type
1716 buff[offset + 1] = 0; // padding
1717
1718 // offset + 2 and offset + 3 are encoding count
1719
1720 var i, j = offset + 4, cnt = 0;
1721 for (i = 0; i < encodings.length; i++) {
1722 if (encodings[i][0] === "Cursor" && !local_cursor) {
1723 Util.Debug("Skipping Cursor pseudo-encoding");
1724 } else if (encodings[i][0] === "TIGHT" && !true_color) {
1725 // TODO: remove this when we have tight+non-true-color
1726 Util.Warn("Skipping tight as it is only supported with true color");
1727 } else {
1728 var enc = encodings[i][1];
1729 buff[j] = enc >> 24;
1730 buff[j + 1] = enc >> 16;
1731 buff[j + 2] = enc >> 8;
1732 buff[j + 3] = enc;
1733
1734 j += 4;
1735 cnt++;
1736 }
1737 }
1738
1739 buff[offset + 2] = cnt >> 8;
1740 buff[offset + 3] = cnt;
1741
1742 sock._sQlen += j - offset;
1743 sock.flush();
1744 },
1745
1746 fbUpdateRequest: function (sock, incremental, x, y, w, h) {
1747 var buff = sock._sQ;
1748 var offset = sock._sQlen;
1749
1750 if (typeof(x) === "undefined") { x = 0; }
1751 if (typeof(y) === "undefined") { y = 0; }
1752
1753 buff[offset] = 3; // msg-type
1754 buff[offset + 1] = incremental ? 1 : 0;
1755
1756 buff[offset + 2] = (x >> 8) & 0xFF;
1757 buff[offset + 3] = x & 0xFF;
1758
1759 buff[offset + 4] = (y >> 8) & 0xFF;
1760 buff[offset + 5] = y & 0xFF;
1761
1762 buff[offset + 6] = (w >> 8) & 0xFF;
1763 buff[offset + 7] = w & 0xFF;
1764
1765 buff[offset + 8] = (h >> 8) & 0xFF;
1766 buff[offset + 9] = h & 0xFF;
1767
1768 sock._sQlen += 10;
1769 sock.flush();
1770 }
1771 };
1772
1773 RFB.genDES = function (password, challenge) {
1774 var passwd = [];
1775 for (var i = 0; i < password.length; i++) {
1776 passwd.push(password.charCodeAt(i));
1777 }
1778 return (new DES(passwd)).encrypt(challenge);
1779 };
1780
1781 RFB.encodingHandlers = {
1782 RAW: function () {
1783 if (this._FBU.lines === 0) {
1784 this._FBU.lines = this._FBU.height;
1785 }
1786
1787 this._FBU.bytes = this._FBU.width * this._fb_Bpp; // at least a line
1788 if (this._sock.rQwait("RAW", this._FBU.bytes)) { return false; }
1789 var cur_y = this._FBU.y + (this._FBU.height - this._FBU.lines);
1790 var curr_height = Math.min(this._FBU.lines,
1791 Math.floor(this._sock.rQlen() / (this._FBU.width * this._fb_Bpp)));
1792 this._display.blitImage(this._FBU.x, cur_y, this._FBU.width,
1793 curr_height, this._sock.get_rQ(),
1794 this._sock.get_rQi());
1795 this._sock.rQskipBytes(this._FBU.width * curr_height * this._fb_Bpp);
1796 this._FBU.lines -= curr_height;
1797
1798 if (this._FBU.lines > 0) {
1799 this._FBU.bytes = this._FBU.width * this._fb_Bpp; // At least another line
1800 } else {
1801 this._FBU.rects--;
1802 this._FBU.bytes = 0;
1803 }
1804
1805 return true;
1806 },
1807
1808 COPYRECT: function () {
1809 this._FBU.bytes = 4;
1810 if (this._sock.rQwait("COPYRECT", 4)) { return false; }
1811 this._display.copyImage(this._sock.rQshift16(), this._sock.rQshift16(),
1812 this._FBU.x, this._FBU.y, this._FBU.width,
1813 this._FBU.height);
1814
1815 this._FBU.rects--;
1816 this._FBU.bytes = 0;
1817 return true;
1818 },
1819
1820 RRE: function () {
1821 var color;
1822 if (this._FBU.subrects === 0) {
1823 this._FBU.bytes = 4 + this._fb_Bpp;
1824 if (this._sock.rQwait("RRE", 4 + this._fb_Bpp)) { return false; }
1825 this._FBU.subrects = this._sock.rQshift32();
1826 color = this._sock.rQshiftBytes(this._fb_Bpp); // Background
1827 this._display.fillRect(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, color);
1828 }
1829
1830 while (this._FBU.subrects > 0 && this._sock.rQlen() >= (this._fb_Bpp + 8)) {
1831 color = this._sock.rQshiftBytes(this._fb_Bpp);
1832 var x = this._sock.rQshift16();
1833 var y = this._sock.rQshift16();
1834 var width = this._sock.rQshift16();
1835 var height = this._sock.rQshift16();
1836 this._display.fillRect(this._FBU.x + x, this._FBU.y + y, width, height, color);
1837 this._FBU.subrects--;
1838 }
1839
1840 if (this._FBU.subrects > 0) {
1841 var chunk = Math.min(this._rre_chunk_sz, this._FBU.subrects);
1842 this._FBU.bytes = (this._fb_Bpp + 8) * chunk;
1843 } else {
1844 this._FBU.rects--;
1845 this._FBU.bytes = 0;
1846 }
1847
1848 return true;
1849 },
1850
1851 HEXTILE: function () {
1852 var rQ = this._sock.get_rQ();
1853 var rQi = this._sock.get_rQi();
1854
1855 if (this._FBU.tiles === 0) {
1856 this._FBU.tiles_x = Math.ceil(this._FBU.width / 16);
1857 this._FBU.tiles_y = Math.ceil(this._FBU.height / 16);
1858 this._FBU.total_tiles = this._FBU.tiles_x * this._FBU.tiles_y;
1859 this._FBU.tiles = this._FBU.total_tiles;
1860 }
1861
1862 while (this._FBU.tiles > 0) {
1863 this._FBU.bytes = 1;
1864 if (this._sock.rQwait("HEXTILE subencoding", this._FBU.bytes)) { return false; }
1865 var subencoding = rQ[rQi]; // Peek
1866 if (subencoding > 30) { // Raw
1867 this._fail("Unexpected server message",
1868 "Illegal hextile subencoding: " + subencoding);
1869 return false;
1870 }
1871
1872 var subrects = 0;
1873 var curr_tile = this._FBU.total_tiles - this._FBU.tiles;
1874 var tile_x = curr_tile % this._FBU.tiles_x;
1875 var tile_y = Math.floor(curr_tile / this._FBU.tiles_x);
1876 var x = this._FBU.x + tile_x * 16;
1877 var y = this._FBU.y + tile_y * 16;
1878 var w = Math.min(16, (this._FBU.x + this._FBU.width) - x);
1879 var h = Math.min(16, (this._FBU.y + this._FBU.height) - y);
1880
1881 // Figure out how much we are expecting
1882 if (subencoding & 0x01) { // Raw
1883 this._FBU.bytes += w * h * this._fb_Bpp;
1884 } else {
1885 if (subencoding & 0x02) { // Background
1886 this._FBU.bytes += this._fb_Bpp;
1887 }
1888 if (subencoding & 0x04) { // Foreground
1889 this._FBU.bytes += this._fb_Bpp;
1890 }
1891 if (subencoding & 0x08) { // AnySubrects
1892 this._FBU.bytes++; // Since we aren't shifting it off
1893 if (this._sock.rQwait("hextile subrects header", this._FBU.bytes)) { return false; }
1894 subrects = rQ[rQi + this._FBU.bytes - 1]; // Peek
1895 if (subencoding & 0x10) { // SubrectsColoured
1896 this._FBU.bytes += subrects * (this._fb_Bpp + 2);
1897 } else {
1898 this._FBU.bytes += subrects * 2;
1899 }
1900 }
1901 }
1902
1903 if (this._sock.rQwait("hextile", this._FBU.bytes)) { return false; }
1904
1905 // We know the encoding and have a whole tile
1906 this._FBU.subencoding = rQ[rQi];
1907 rQi++;
1908 if (this._FBU.subencoding === 0) {
1909 if (this._FBU.lastsubencoding & 0x01) {
1910 // Weird: ignore blanks are RAW
1911 Util.Debug(" Ignoring blank after RAW");
1912 } else {
1913 this._display.fillRect(x, y, w, h, this._FBU.background);
1914 }
1915 } else if (this._FBU.subencoding & 0x01) { // Raw
1916 this._display.blitImage(x, y, w, h, rQ, rQi);
1917 rQi += this._FBU.bytes - 1;
1918 } else {
1919 if (this._FBU.subencoding & 0x02) { // Background
1920 if (this._fb_Bpp == 1) {
1921 this._FBU.background = rQ[rQi];
1922 } else {
1923 // fb_Bpp is 4
1924 this._FBU.background = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
1925 }
1926 rQi += this._fb_Bpp;
1927 }
1928 if (this._FBU.subencoding & 0x04) { // Foreground
1929 if (this._fb_Bpp == 1) {
1930 this._FBU.foreground = rQ[rQi];
1931 } else {
1932 // this._fb_Bpp is 4
1933 this._FBU.foreground = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
1934 }
1935 rQi += this._fb_Bpp;
1936 }
1937
1938 this._display.startTile(x, y, w, h, this._FBU.background);
1939 if (this._FBU.subencoding & 0x08) { // AnySubrects
1940 subrects = rQ[rQi];
1941 rQi++;
1942
1943 for (var s = 0; s < subrects; s++) {
1944 var color;
1945 if (this._FBU.subencoding & 0x10) { // SubrectsColoured
1946 if (this._fb_Bpp === 1) {
1947 color = rQ[rQi];
1948 } else {
1949 // _fb_Bpp is 4
1950 color = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
1951 }
1952 rQi += this._fb_Bpp;
1953 } else {
1954 color = this._FBU.foreground;
1955 }
1956 var xy = rQ[rQi];
1957 rQi++;
1958 var sx = (xy >> 4);
1959 var sy = (xy & 0x0f);
1960
1961 var wh = rQ[rQi];
1962 rQi++;
1963 var sw = (wh >> 4) + 1;
1964 var sh = (wh & 0x0f) + 1;
1965
1966 this._display.subTile(sx, sy, sw, sh, color);
1967 }
1968 }
1969 this._display.finishTile();
1970 }
1971 this._sock.set_rQi(rQi);
1972 this._FBU.lastsubencoding = this._FBU.subencoding;
1973 this._FBU.bytes = 0;
1974 this._FBU.tiles--;
1975 }
1976
1977 if (this._FBU.tiles === 0) {
1978 this._FBU.rects--;
1979 }
1980
1981 return true;
1982 },
1983
1984 getTightCLength: function (arr) {
1985 var header = 1, data = 0;
1986 data += arr[0] & 0x7f;
1987 if (arr[0] & 0x80) {
1988 header++;
1989 data += (arr[1] & 0x7f) << 7;
1990 if (arr[1] & 0x80) {
1991 header++;
1992 data += arr[2] << 14;
1993 }
1994 }
1995 return [header, data];
1996 },
1997
1998 display_tight: function (isTightPNG) {
1999 if (this._fb_depth === 1) {
2000 this._fail("Internal error",
2001 "Tight protocol handler only implements " +
2002 "true color mode");
2003 }
2004
2005 this._FBU.bytes = 1; // compression-control byte
2006 if (this._sock.rQwait("TIGHT compression-control", this._FBU.bytes)) { return false; }
2007
2008 var checksum = function (data) {
2009 var sum = 0;
2010 for (var i = 0; i < data.length; i++) {
2011 sum += data[i];
2012 if (sum > 65536) sum -= 65536;
2013 }
2014 return sum;
2015 };
2016
2017 var resetStreams = 0;
2018 var streamId = -1;
2019 var decompress = function (data, expected) {
2020 for (var i = 0; i < 4; i++) {
2021 if ((resetStreams >> i) & 1) {
2022 this._FBU.zlibs[i].reset();
2023 Util.Info("Reset zlib stream " + i);
2024 }
2025 }
2026
2027 //var uncompressed = this._FBU.zlibs[streamId].uncompress(data, 0);
2028 var uncompressed = this._FBU.zlibs[streamId].inflate(data, true, expected);
2029 /*if (uncompressed.status !== 0) {
2030 Util.Error("Invalid data in zlib stream");
2031 }*/
2032
2033 //return uncompressed.data;
2034 return uncompressed;
2035 }.bind(this);
2036
2037 var indexedToRGBX2Color = function (data, palette, width, height) {
2038 // Convert indexed (palette based) image data to RGB
2039 // TODO: reduce number of calculations inside loop
2040 var dest = this._destBuff;
2041 var w = Math.floor((width + 7) / 8);
2042 var w1 = Math.floor(width / 8);
2043
2044 /*for (var y = 0; y < height; y++) {
2045 var b, x, dp, sp;
2046 var yoffset = y * width;
2047 var ybitoffset = y * w;
2048 var xoffset, targetbyte;
2049 for (x = 0; x < w1; x++) {
2050 xoffset = yoffset + x * 8;
2051 targetbyte = data[ybitoffset + x];
2052 for (b = 7; b >= 0; b--) {
2053 dp = (xoffset + 7 - b) * 3;
2054 sp = (targetbyte >> b & 1) * 3;
2055 dest[dp] = palette[sp];
2056 dest[dp + 1] = palette[sp + 1];
2057 dest[dp + 2] = palette[sp + 2];
2058 }
2059 }
2060
2061 xoffset = yoffset + x * 8;
2062 targetbyte = data[ybitoffset + x];
2063 for (b = 7; b >= 8 - width % 8; b--) {
2064 dp = (xoffset + 7 - b) * 3;
2065 sp = (targetbyte >> b & 1) * 3;
2066 dest[dp] = palette[sp];
2067 dest[dp + 1] = palette[sp + 1];
2068 dest[dp + 2] = palette[sp + 2];
2069 }
2070 }*/
2071
2072 for (var y = 0; y < height; y++) {
2073 var b, x, dp, sp;
2074 for (x = 0; x < w1; x++) {
2075 for (b = 7; b >= 0; b--) {
2076 dp = (y * width + x * 8 + 7 - b) * 4;
2077 sp = (data[y * w + x] >> b & 1) * 3;
2078 dest[dp] = palette[sp];
2079 dest[dp + 1] = palette[sp + 1];
2080 dest[dp + 2] = palette[sp + 2];
2081 dest[dp + 3] = 255;
2082 }
2083 }
2084
2085 for (b = 7; b >= 8 - width % 8; b--) {
2086 dp = (y * width + x * 8 + 7 - b) * 4;
2087 sp = (data[y * w + x] >> b & 1) * 3;
2088 dest[dp] = palette[sp];
2089 dest[dp + 1] = palette[sp + 1];
2090 dest[dp + 2] = palette[sp + 2];
2091 dest[dp + 3] = 255;
2092 }
2093 }
2094
2095 return dest;
2096 }.bind(this);
2097
2098 var indexedToRGBX = function (data, palette, width, height) {
2099 // Convert indexed (palette based) image data to RGB
2100 var dest = this._destBuff;
2101 var total = width * height * 4;
2102 for (var i = 0, j = 0; i < total; i += 4, j++) {
2103 var sp = data[j] * 3;
2104 dest[i] = palette[sp];
2105 dest[i + 1] = palette[sp + 1];
2106 dest[i + 2] = palette[sp + 2];
2107 dest[i + 3] = 255;
2108 }
2109
2110 return dest;
2111 }.bind(this);
2112
2113 var rQi = this._sock.get_rQi();
2114 var rQ = this._sock.rQwhole();
2115 var cmode, data;
2116 var cl_header, cl_data;
2117
2118 var handlePalette = function () {
2119 var numColors = rQ[rQi + 2] + 1;
2120 var paletteSize = numColors * this._fb_depth;
2121 this._FBU.bytes += paletteSize;
2122 if (this._sock.rQwait("TIGHT palette " + cmode, this._FBU.bytes)) { return false; }
2123
2124 var bpp = (numColors <= 2) ? 1 : 8;
2125 var rowSize = Math.floor((this._FBU.width * bpp + 7) / 8);
2126 var raw = false;
2127 if (rowSize * this._FBU.height < 12) {
2128 raw = true;
2129 cl_header = 0;
2130 cl_data = rowSize * this._FBU.height;
2131 //clength = [0, rowSize * this._FBU.height];
2132 } else {
2133 // begin inline getTightCLength (returning two-item arrays is bad for performance with GC)
2134 var cl_offset = rQi + 3 + paletteSize;
2135 cl_header = 1;
2136 cl_data = 0;
2137 cl_data += rQ[cl_offset] & 0x7f;
2138 if (rQ[cl_offset] & 0x80) {
2139 cl_header++;
2140 cl_data += (rQ[cl_offset + 1] & 0x7f) << 7;
2141 if (rQ[cl_offset + 1] & 0x80) {
2142 cl_header++;
2143 cl_data += rQ[cl_offset + 2] << 14;
2144 }
2145 }
2146 // end inline getTightCLength
2147 }
2148
2149 this._FBU.bytes += cl_header + cl_data;
2150 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2151
2152 // Shift ctl, filter id, num colors, palette entries, and clength off
2153 this._sock.rQskipBytes(3);
2154 //var palette = this._sock.rQshiftBytes(paletteSize);
2155 this._sock.rQshiftTo(this._paletteBuff, paletteSize);
2156 this._sock.rQskipBytes(cl_header);
2157
2158 if (raw) {
2159 data = this._sock.rQshiftBytes(cl_data);
2160 } else {
2161 data = decompress(this._sock.rQshiftBytes(cl_data), rowSize * this._FBU.height);
2162 }
2163
2164 // Convert indexed (palette based) image data to RGB
2165 var rgbx;
2166 if (numColors == 2) {
2167 rgbx = indexedToRGBX2Color(data, this._paletteBuff, this._FBU.width, this._FBU.height);
2168 this._display.blitRgbxImage(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, rgbx, 0, false);
2169 } else {
2170 rgbx = indexedToRGBX(data, this._paletteBuff, this._FBU.width, this._FBU.height);
2171 this._display.blitRgbxImage(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, rgbx, 0, false);
2172 }
2173
2174
2175 return true;
2176 }.bind(this);
2177
2178 var handleCopy = function () {
2179 var raw = false;
2180 var uncompressedSize = this._FBU.width * this._FBU.height * this._fb_depth;
2181 if (uncompressedSize < 12) {
2182 raw = true;
2183 cl_header = 0;
2184 cl_data = uncompressedSize;
2185 } else {
2186 // begin inline getTightCLength (returning two-item arrays is for peformance with GC)
2187 var cl_offset = rQi + 1;
2188 cl_header = 1;
2189 cl_data = 0;
2190 cl_data += rQ[cl_offset] & 0x7f;
2191 if (rQ[cl_offset] & 0x80) {
2192 cl_header++;
2193 cl_data += (rQ[cl_offset + 1] & 0x7f) << 7;
2194 if (rQ[cl_offset + 1] & 0x80) {
2195 cl_header++;
2196 cl_data += rQ[cl_offset + 2] << 14;
2197 }
2198 }
2199 // end inline getTightCLength
2200 }
2201 this._FBU.bytes = 1 + cl_header + cl_data;
2202 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2203
2204 // Shift ctl, clength off
2205 this._sock.rQshiftBytes(1 + cl_header);
2206
2207 if (raw) {
2208 data = this._sock.rQshiftBytes(cl_data);
2209 } else {
2210 data = decompress(this._sock.rQshiftBytes(cl_data), uncompressedSize);
2211 }
2212
2213 this._display.blitRgbImage(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, data, 0, false);
2214
2215 return true;
2216 }.bind(this);
2217
2218 var ctl = this._sock.rQpeek8();
2219
2220 // Keep tight reset bits
2221 resetStreams = ctl & 0xF;
2222
2223 // Figure out filter
2224 ctl = ctl >> 4;
2225 streamId = ctl & 0x3;
2226
2227 if (ctl === 0x08) cmode = "fill";
2228 else if (ctl === 0x09) cmode = "jpeg";
2229 else if (ctl === 0x0A) cmode = "png";
2230 else if (ctl & 0x04) cmode = "filter";
2231 else if (ctl < 0x04) cmode = "copy";
2232 else return this._fail("Unexpected server message",
2233 "Illegal tight compression received, " +
2234 "ctl: " + ctl);
2235
2236 if (isTightPNG && (cmode === "filter" || cmode === "copy")) {
2237 return this._fail("Unexpected server message",
2238 "filter/copy received in tightPNG mode");
2239 }
2240
2241 switch (cmode) {
2242 // fill use fb_depth because TPIXELs drop the padding byte
2243 case "fill": // TPIXEL
2244 this._FBU.bytes += this._fb_depth;
2245 break;
2246 case "jpeg": // max clength
2247 this._FBU.bytes += 3;
2248 break;
2249 case "png": // max clength
2250 this._FBU.bytes += 3;
2251 break;
2252 case "filter": // filter id + num colors if palette
2253 this._FBU.bytes += 2;
2254 break;
2255 case "copy":
2256 break;
2257 }
2258
2259 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2260
2261 // Determine FBU.bytes
2262 switch (cmode) {
2263 case "fill":
2264 // skip ctl byte
2265 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);
2266 this._sock.rQskipBytes(4);
2267 break;
2268 case "png":
2269 case "jpeg":
2270 // begin inline getTightCLength (returning two-item arrays is for peformance with GC)
2271 var cl_offset = rQi + 1;
2272 cl_header = 1;
2273 cl_data = 0;
2274 cl_data += rQ[cl_offset] & 0x7f;
2275 if (rQ[cl_offset] & 0x80) {
2276 cl_header++;
2277 cl_data += (rQ[cl_offset + 1] & 0x7f) << 7;
2278 if (rQ[cl_offset + 1] & 0x80) {
2279 cl_header++;
2280 cl_data += rQ[cl_offset + 2] << 14;
2281 }
2282 }
2283 // end inline getTightCLength
2284 this._FBU.bytes = 1 + cl_header + cl_data; // ctl + clength size + jpeg-data
2285 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2286
2287 // We have everything, render it
2288 this._sock.rQskipBytes(1 + cl_header); // shift off clt + compact length
2289 data = this._sock.rQshiftBytes(cl_data);
2290 this._display.imageRect(this._FBU.x, this._FBU.y, "image/" + cmode, data);
2291 break;
2292 case "filter":
2293 var filterId = rQ[rQi + 1];
2294 if (filterId === 1) {
2295 if (!handlePalette()) { return false; }
2296 } else {
2297 // Filter 0, Copy could be valid here, but servers don't send it as an explicit filter
2298 // Filter 2, Gradient is valid but not use if jpeg is enabled
2299 this._fail("Unexpected server message",
2300 "Unsupported tight subencoding received, " +
2301 "filter: " + filterId);
2302 }
2303 break;
2304 case "copy":
2305 if (!handleCopy()) { return false; }
2306 break;
2307 }
2308
2309
2310 this._FBU.bytes = 0;
2311 this._FBU.rects--;
2312
2313 return true;
2314 },
2315
2316 TIGHT: function () { return this._encHandlers.display_tight(false); },
2317 TIGHT_PNG: function () { return this._encHandlers.display_tight(true); },
2318
2319 last_rect: function () {
2320 this._FBU.rects = 0;
2321 return true;
2322 },
2323
2324 handle_FB_resize: function () {
2325 this._fb_width = this._FBU.width;
2326 this._fb_height = this._FBU.height;
2327 this._destBuff = new Uint8Array(this._fb_width * this._fb_height * 4);
2328 this._display.resize(this._fb_width, this._fb_height);
2329 this._onFBResize(this, this._fb_width, this._fb_height);
2330 this._timing.fbu_rt_start = (new Date()).getTime();
2331 this._updateContinuousUpdates();
2332
2333 this._FBU.bytes = 0;
2334 this._FBU.rects -= 1;
2335 return true;
2336 },
2337
2338 ExtendedDesktopSize: function () {
2339 this._FBU.bytes = 1;
2340 if (this._sock.rQwait("ExtendedDesktopSize", this._FBU.bytes)) { return false; }
2341
2342 this._supportsSetDesktopSize = true;
2343 var number_of_screens = this._sock.rQpeek8();
2344
2345 this._FBU.bytes = 4 + (number_of_screens * 16);
2346 if (this._sock.rQwait("ExtendedDesktopSize", this._FBU.bytes)) { return false; }
2347
2348 this._sock.rQskipBytes(1); // number-of-screens
2349 this._sock.rQskipBytes(3); // padding
2350
2351 for (var i = 0; i < number_of_screens; i += 1) {
2352 // Save the id and flags of the first screen
2353 if (i === 0) {
2354 this._screen_id = this._sock.rQshiftBytes(4); // id
2355 this._sock.rQskipBytes(2); // x-position
2356 this._sock.rQskipBytes(2); // y-position
2357 this._sock.rQskipBytes(2); // width
2358 this._sock.rQskipBytes(2); // height
2359 this._screen_flags = this._sock.rQshiftBytes(4); // flags
2360 } else {
2361 this._sock.rQskipBytes(16);
2362 }
2363 }
2364
2365 /*
2366 * The x-position indicates the reason for the change:
2367 *
2368 * 0 - server resized on its own
2369 * 1 - this client requested the resize
2370 * 2 - another client requested the resize
2371 */
2372
2373 // We need to handle errors when we requested the resize.
2374 if (this._FBU.x === 1 && this._FBU.y !== 0) {
2375 var msg = "";
2376 // The y-position indicates the status code from the server
2377 switch (this._FBU.y) {
2378 case 1:
2379 msg = "Resize is administratively prohibited";
2380 break;
2381 case 2:
2382 msg = "Out of resources";
2383 break;
2384 case 3:
2385 msg = "Invalid screen layout";
2386 break;
2387 default:
2388 msg = "Unknown reason";
2389 break;
2390 }
2391 this._notification("Server did not accept the resize request: "
2392 + msg, 'normal');
2393 return true;
2394 }
2395
2396 this._encHandlers.handle_FB_resize();
2397 return true;
2398 },
2399
2400 DesktopSize: function () {
2401 this._encHandlers.handle_FB_resize();
2402 return true;
2403 },
2404
2405 Cursor: function () {
2406 Util.Debug(">> set_cursor");
2407 var x = this._FBU.x; // hotspot-x
2408 var y = this._FBU.y; // hotspot-y
2409 var w = this._FBU.width;
2410 var h = this._FBU.height;
2411
2412 var pixelslength = w * h * this._fb_Bpp;
2413 var masklength = Math.floor((w + 7) / 8) * h;
2414
2415 this._FBU.bytes = pixelslength + masklength;
2416 if (this._sock.rQwait("cursor encoding", this._FBU.bytes)) { return false; }
2417
2418 this._display.changeCursor(this._sock.rQshiftBytes(pixelslength),
2419 this._sock.rQshiftBytes(masklength),
2420 x, y, w, h);
2421
2422 this._FBU.bytes = 0;
2423 this._FBU.rects--;
2424
2425 Util.Debug("<< set_cursor");
2426 return true;
2427 },
2428
2429 QEMUExtendedKeyEvent: function () {
2430 this._FBU.rects--;
2431
2432 var keyboardEvent = document.createEvent("keyboardEvent");
2433 if (keyboardEvent.code !== undefined) {
2434 this._qemuExtKeyEventSupported = true;
2435 this._keyboard.setQEMUVNCKeyboardHandler();
2436 }
2437 },
2438
2439 JPEG_quality_lo: function () {
2440 Util.Error("Server sent jpeg_quality pseudo-encoding");
2441 },
2442
2443 compress_lo: function () {
2444 Util.Error("Server sent compress level pseudo-encoding");
2445 }
2446 };
2447 })();