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