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