image/w/core/rfb.js.orig

changeset 11
f816cf5b6bcb
parent 0
b74b0e4902c3
equal deleted inserted replaced
10:29e2e2e0b9ef 11:f816cf5b6bcb
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 } else if (this._fb_name === "MBSE BrewBoard") {
1235 Log.Warn("MBSE BrewBoard only supports 8 bit depths. Using low color mode.");
1236 this._fb_depth = 8;
1237 }
1238
1239 RFB.messages.pixelFormat(this._sock, this._fb_depth, true);
1240 this._sendEncodings();
1241 RFB.messages.fbUpdateRequest(this._sock, false, 0, 0, this._fb_width, this._fb_height);
1242
1243 this._timing.fbu_rt_start = (new Date()).getTime();
1244 this._timing.pixels = 0;
1245
1246 // Cursor will be server side until the server decides to honor
1247 // our request and send over the cursor image
1248 this._display.disableLocalCursor();
1249
1250 this._updateConnectionState('connected');
1251 return true;
1252 },
1253
1254 _sendEncodings: function () {
1255 var encs = [];
1256
1257 // In preference order
1258 encs.push(encodings.encodingCopyRect);
1259 // Only supported with full depth support
1260 if (this._fb_depth == 24) {
1261 encs.push(encodings.encodingTight);
1262 encs.push(encodings.encodingHextile);
1263 encs.push(encodings.encodingRRE);
1264 }
1265 encs.push(encodings.encodingRaw);
1266
1267 // Psuedo-encoding settings
1268 encs.push(encodings.pseudoEncodingTightPNG);
1269 encs.push(encodings.pseudoEncodingQualityLevel0 + 6);
1270 encs.push(encodings.pseudoEncodingCompressLevel0 + 2);
1271
1272 encs.push(encodings.pseudoEncodingDesktopSize);
1273 encs.push(encodings.pseudoEncodingLastRect);
1274 encs.push(encodings.pseudoEncodingQEMUExtendedKeyEvent);
1275 encs.push(encodings.pseudoEncodingExtendedDesktopSize);
1276 encs.push(encodings.pseudoEncodingXvp);
1277 encs.push(encodings.pseudoEncodingFence);
1278 encs.push(encodings.pseudoEncodingContinuousUpdates);
1279
1280 if (supportsCursorURIs() &&
1281 !isTouchDevice && this._fb_depth == 24) {
1282 encs.push(encodings.pseudoEncodingCursor);
1283 }
1284
1285 RFB.messages.clientEncodings(this._sock, encs);
1286 },
1287
1288 /* RFB protocol initialization states:
1289 * ProtocolVersion
1290 * Security
1291 * Authentication
1292 * SecurityResult
1293 * ClientInitialization - not triggered by server message
1294 * ServerInitialization
1295 */
1296 _init_msg: function () {
1297 switch (this._rfb_init_state) {
1298 case 'ProtocolVersion':
1299 return this._negotiate_protocol_version();
1300
1301 case 'Security':
1302 return this._negotiate_security();
1303
1304 case 'Authentication':
1305 return this._negotiate_authentication();
1306
1307 case 'SecurityResult':
1308 return this._handle_security_result();
1309
1310 case 'ClientInitialisation':
1311 this._sock.send([this._shared ? 1 : 0]); // ClientInitialisation
1312 this._rfb_init_state = 'ServerInitialisation';
1313 return true;
1314
1315 case 'ServerInitialisation':
1316 return this._negotiate_server_init();
1317
1318 default:
1319 return this._fail("Unknown init state (state: " +
1320 this._rfb_init_state + ")");
1321 }
1322 },
1323
1324 _handle_set_colour_map_msg: function () {
1325 Log.Debug("SetColorMapEntries");
1326
1327 return this._fail("Unexpected SetColorMapEntries message");
1328 },
1329
1330 _handle_server_cut_text: function () {
1331 Log.Debug("ServerCutText");
1332
1333 if (this._sock.rQwait("ServerCutText header", 7, 1)) { return false; }
1334 this._sock.rQskipBytes(3); // Padding
1335 var length = this._sock.rQshift32();
1336 if (this._sock.rQwait("ServerCutText", length, 8)) { return false; }
1337
1338 var text = this._sock.rQshiftStr(length);
1339
1340 if (this._viewOnly) { return true; }
1341
1342 var event = new CustomEvent("clipboard",
1343 { detail: { text: text } });
1344 this.dispatchEvent(event);
1345
1346 return true;
1347 },
1348
1349 _handle_server_fence_msg: function() {
1350 if (this._sock.rQwait("ServerFence header", 8, 1)) { return false; }
1351 this._sock.rQskipBytes(3); // Padding
1352 var flags = this._sock.rQshift32();
1353 var length = this._sock.rQshift8();
1354
1355 if (this._sock.rQwait("ServerFence payload", length, 9)) { return false; }
1356
1357 if (length > 64) {
1358 Log.Warn("Bad payload length (" + length + ") in fence response");
1359 length = 64;
1360 }
1361
1362 var payload = this._sock.rQshiftStr(length);
1363
1364 this._supportsFence = true;
1365
1366 /*
1367 * Fence flags
1368 *
1369 * (1<<0) - BlockBefore
1370 * (1<<1) - BlockAfter
1371 * (1<<2) - SyncNext
1372 * (1<<31) - Request
1373 */
1374
1375 if (!(flags & (1<<31))) {
1376 return this._fail("Unexpected fence response");
1377 }
1378
1379 // Filter out unsupported flags
1380 // FIXME: support syncNext
1381 flags &= (1<<0) | (1<<1);
1382
1383 // BlockBefore and BlockAfter are automatically handled by
1384 // the fact that we process each incoming message
1385 // synchronuosly.
1386 RFB.messages.clientFence(this._sock, flags, payload);
1387
1388 return true;
1389 },
1390
1391 _handle_xvp_msg: function () {
1392 if (this._sock.rQwait("XVP version and message", 3, 1)) { return false; }
1393 this._sock.rQskip8(); // Padding
1394 var xvp_ver = this._sock.rQshift8();
1395 var xvp_msg = this._sock.rQshift8();
1396
1397 switch (xvp_msg) {
1398 case 0: // XVP_FAIL
1399 Log.Error("XVP Operation Failed");
1400 break;
1401 case 1: // XVP_INIT
1402 this._rfb_xvp_ver = xvp_ver;
1403 Log.Info("XVP extensions enabled (version " + this._rfb_xvp_ver + ")");
1404 this._setCapability("power", true);
1405 break;
1406 default:
1407 this._fail("Illegal server XVP message (msg: " + xvp_msg + ")");
1408 break;
1409 }
1410
1411 return true;
1412 },
1413
1414 _normal_msg: function () {
1415 var msg_type;
1416
1417 if (this._FBU.rects > 0) {
1418 msg_type = 0;
1419 } else {
1420 msg_type = this._sock.rQshift8();
1421 }
1422
1423 switch (msg_type) {
1424 case 0: // FramebufferUpdate
1425 var ret = this._framebufferUpdate();
1426 if (ret && !this._enabledContinuousUpdates) {
1427 RFB.messages.fbUpdateRequest(this._sock, true, 0, 0,
1428 this._fb_width, this._fb_height);
1429 }
1430 return ret;
1431
1432 case 1: // SetColorMapEntries
1433 return this._handle_set_colour_map_msg();
1434
1435 case 2: // Bell
1436 Log.Debug("Bell");
1437 var event = new CustomEvent("bell", { detail: {} });
1438 this.dispatchEvent(event);
1439 return true;
1440
1441 case 3: // ServerCutText
1442 return this._handle_server_cut_text();
1443
1444 case 150: // EndOfContinuousUpdates
1445 var first = !(this._supportsContinuousUpdates);
1446 this._supportsContinuousUpdates = true;
1447 this._enabledContinuousUpdates = false;
1448 if (first) {
1449 this._enabledContinuousUpdates = true;
1450 this._updateContinuousUpdates();
1451 Log.Info("Enabling continuous updates.");
1452 } else {
1453 // FIXME: We need to send a framebufferupdaterequest here
1454 // if we add support for turning off continuous updates
1455 }
1456 return true;
1457
1458 case 248: // ServerFence
1459 return this._handle_server_fence_msg();
1460
1461 case 250: // XVP
1462 return this._handle_xvp_msg();
1463
1464 default:
1465 this._fail("Unexpected server message (type " + msg_type + ")");
1466 Log.Debug("sock.rQslice(0, 30): " + this._sock.rQslice(0, 30));
1467 return true;
1468 }
1469 },
1470
1471 _onFlush: function() {
1472 this._flushing = false;
1473 // Resume processing
1474 if (this._sock.rQlen() > 0) {
1475 this._handle_message();
1476 }
1477 },
1478
1479 _framebufferUpdate: function () {
1480 var ret = true;
1481 var now;
1482
1483 if (this._FBU.rects === 0) {
1484 if (this._sock.rQwait("FBU header", 3, 1)) { return false; }
1485 this._sock.rQskip8(); // Padding
1486 this._FBU.rects = this._sock.rQshift16();
1487 this._FBU.bytes = 0;
1488 this._timing.cur_fbu = 0;
1489 if (this._timing.fbu_rt_start > 0) {
1490 now = (new Date()).getTime();
1491 Log.Info("First FBU latency: " + (now - this._timing.fbu_rt_start));
1492 }
1493
1494 // Make sure the previous frame is fully rendered first
1495 // to avoid building up an excessive queue
1496 if (this._display.pending()) {
1497 this._flushing = true;
1498 this._display.flush();
1499 return false;
1500 }
1501 }
1502
1503 while (this._FBU.rects > 0) {
1504 if (this._rfb_connection_state !== 'connected') { return false; }
1505
1506 if (this._sock.rQwait("FBU", this._FBU.bytes)) { return false; }
1507 if (this._FBU.bytes === 0) {
1508 if (this._sock.rQwait("rect header", 12)) { return false; }
1509 /* New FramebufferUpdate */
1510
1511 var hdr = this._sock.rQshiftBytes(12);
1512 this._FBU.x = (hdr[0] << 8) + hdr[1];
1513 this._FBU.y = (hdr[2] << 8) + hdr[3];
1514 this._FBU.width = (hdr[4] << 8) + hdr[5];
1515 this._FBU.height = (hdr[6] << 8) + hdr[7];
1516 this._FBU.encoding = parseInt((hdr[8] << 24) + (hdr[9] << 16) +
1517 (hdr[10] << 8) + hdr[11], 10);
1518
1519 if (!this._encHandlers[this._FBU.encoding]) {
1520 this._fail("Unsupported encoding (encoding: " +
1521 this._FBU.encoding + ")");
1522 return false;
1523 }
1524 }
1525
1526 this._timing.last_fbu = (new Date()).getTime();
1527
1528 ret = this._encHandlers[this._FBU.encoding]();
1529
1530 now = (new Date()).getTime();
1531 this._timing.cur_fbu += (now - this._timing.last_fbu);
1532
1533 if (ret) {
1534 if (!(this._FBU.encoding in this._encStats)) {
1535 this._encStats[this._FBU.encoding] = [0, 0];
1536 }
1537 this._encStats[this._FBU.encoding][0]++;
1538 this._encStats[this._FBU.encoding][1]++;
1539 this._timing.pixels += this._FBU.width * this._FBU.height;
1540 }
1541
1542 if (this._timing.pixels >= (this._fb_width * this._fb_height)) {
1543 if ((this._FBU.width === this._fb_width && this._FBU.height === this._fb_height) ||
1544 this._timing.fbu_rt_start > 0) {
1545 this._timing.full_fbu_total += this._timing.cur_fbu;
1546 this._timing.full_fbu_cnt++;
1547 Log.Info("Timing of full FBU, curr: " +
1548 this._timing.cur_fbu + ", total: " +
1549 this._timing.full_fbu_total + ", cnt: " +
1550 this._timing.full_fbu_cnt + ", avg: " +
1551 (this._timing.full_fbu_total / this._timing.full_fbu_cnt));
1552 }
1553
1554 if (this._timing.fbu_rt_start > 0) {
1555 var fbu_rt_diff = now - this._timing.fbu_rt_start;
1556 this._timing.fbu_rt_total += fbu_rt_diff;
1557 this._timing.fbu_rt_cnt++;
1558 Log.Info("full FBU round-trip, cur: " +
1559 fbu_rt_diff + ", total: " +
1560 this._timing.fbu_rt_total + ", cnt: " +
1561 this._timing.fbu_rt_cnt + ", avg: " +
1562 (this._timing.fbu_rt_total / this._timing.fbu_rt_cnt));
1563 this._timing.fbu_rt_start = 0;
1564 }
1565 }
1566
1567 if (!ret) { return ret; } // need more data
1568 }
1569
1570 this._display.flip();
1571
1572 return true; // We finished this FBU
1573 },
1574
1575 _updateContinuousUpdates: function() {
1576 if (!this._enabledContinuousUpdates) { return; }
1577
1578 RFB.messages.enableContinuousUpdates(this._sock, true, 0, 0,
1579 this._fb_width, this._fb_height);
1580 },
1581
1582 _resize: function(width, height) {
1583 this._fb_width = width;
1584 this._fb_height = height;
1585
1586 this._destBuff = new Uint8Array(this._fb_width * this._fb_height * 4);
1587
1588 this._display.resize(this._fb_width, this._fb_height);
1589
1590 // Adjust the visible viewport based on the new dimensions
1591 this._updateClip();
1592 this._updateScale();
1593
1594 this._timing.fbu_rt_start = (new Date()).getTime();
1595 this._updateContinuousUpdates();
1596 },
1597
1598 _xvpOp: function (ver, op) {
1599 if (this._rfb_xvp_ver < ver) { return; }
1600 Log.Info("Sending XVP operation " + op + " (version " + ver + ")");
1601 RFB.messages.xvpOp(this._sock, ver, op);
1602 },
1603 };
1604
1605 Object.assign(RFB.prototype, EventTargetMixin);
1606
1607 // Class Methods
1608 RFB.messages = {
1609 keyEvent: function (sock, keysym, down) {
1610 var buff = sock._sQ;
1611 var offset = sock._sQlen;
1612
1613 buff[offset] = 4; // msg-type
1614 buff[offset + 1] = down;
1615
1616 buff[offset + 2] = 0;
1617 buff[offset + 3] = 0;
1618
1619 buff[offset + 4] = (keysym >> 24);
1620 buff[offset + 5] = (keysym >> 16);
1621 buff[offset + 6] = (keysym >> 8);
1622 buff[offset + 7] = keysym;
1623
1624 sock._sQlen += 8;
1625 sock.flush();
1626 },
1627
1628 QEMUExtendedKeyEvent: function (sock, keysym, down, keycode) {
1629 function getRFBkeycode(xt_scancode) {
1630 var upperByte = (keycode >> 8);
1631 var lowerByte = (keycode & 0x00ff);
1632 if (upperByte === 0xe0 && lowerByte < 0x7f) {
1633 lowerByte = lowerByte | 0x80;
1634 return lowerByte;
1635 }
1636 return xt_scancode;
1637 }
1638
1639 var buff = sock._sQ;
1640 var offset = sock._sQlen;
1641
1642 buff[offset] = 255; // msg-type
1643 buff[offset + 1] = 0; // sub msg-type
1644
1645 buff[offset + 2] = (down >> 8);
1646 buff[offset + 3] = down;
1647
1648 buff[offset + 4] = (keysym >> 24);
1649 buff[offset + 5] = (keysym >> 16);
1650 buff[offset + 6] = (keysym >> 8);
1651 buff[offset + 7] = keysym;
1652
1653 var RFBkeycode = getRFBkeycode(keycode);
1654
1655 buff[offset + 8] = (RFBkeycode >> 24);
1656 buff[offset + 9] = (RFBkeycode >> 16);
1657 buff[offset + 10] = (RFBkeycode >> 8);
1658 buff[offset + 11] = RFBkeycode;
1659
1660 sock._sQlen += 12;
1661 sock.flush();
1662 },
1663
1664 pointerEvent: function (sock, x, y, mask) {
1665 var buff = sock._sQ;
1666 var offset = sock._sQlen;
1667
1668 buff[offset] = 5; // msg-type
1669
1670 buff[offset + 1] = mask;
1671
1672 buff[offset + 2] = x >> 8;
1673 buff[offset + 3] = x;
1674
1675 buff[offset + 4] = y >> 8;
1676 buff[offset + 5] = y;
1677
1678 sock._sQlen += 6;
1679 sock.flush();
1680 },
1681
1682 // TODO(directxman12): make this unicode compatible?
1683 clientCutText: function (sock, text) {
1684 var buff = sock._sQ;
1685 var offset = sock._sQlen;
1686
1687 buff[offset] = 6; // msg-type
1688
1689 buff[offset + 1] = 0; // padding
1690 buff[offset + 2] = 0; // padding
1691 buff[offset + 3] = 0; // padding
1692
1693 var n = text.length;
1694
1695 buff[offset + 4] = n >> 24;
1696 buff[offset + 5] = n >> 16;
1697 buff[offset + 6] = n >> 8;
1698 buff[offset + 7] = n;
1699
1700 for (var i = 0; i < n; i++) {
1701 buff[offset + 8 + i] = text.charCodeAt(i);
1702 }
1703
1704 sock._sQlen += 8 + n;
1705 sock.flush();
1706 },
1707
1708 setDesktopSize: function (sock, width, height, id, flags) {
1709 var buff = sock._sQ;
1710 var offset = sock._sQlen;
1711
1712 buff[offset] = 251; // msg-type
1713 buff[offset + 1] = 0; // padding
1714 buff[offset + 2] = width >> 8; // width
1715 buff[offset + 3] = width;
1716 buff[offset + 4] = height >> 8; // height
1717 buff[offset + 5] = height;
1718
1719 buff[offset + 6] = 1; // number-of-screens
1720 buff[offset + 7] = 0; // padding
1721
1722 // screen array
1723 buff[offset + 8] = id >> 24; // id
1724 buff[offset + 9] = id >> 16;
1725 buff[offset + 10] = id >> 8;
1726 buff[offset + 11] = id;
1727 buff[offset + 12] = 0; // x-position
1728 buff[offset + 13] = 0;
1729 buff[offset + 14] = 0; // y-position
1730 buff[offset + 15] = 0;
1731 buff[offset + 16] = width >> 8; // width
1732 buff[offset + 17] = width;
1733 buff[offset + 18] = height >> 8; // height
1734 buff[offset + 19] = height;
1735 buff[offset + 20] = flags >> 24; // flags
1736 buff[offset + 21] = flags >> 16;
1737 buff[offset + 22] = flags >> 8;
1738 buff[offset + 23] = flags;
1739
1740 sock._sQlen += 24;
1741 sock.flush();
1742 },
1743
1744 clientFence: function (sock, flags, payload) {
1745 var buff = sock._sQ;
1746 var offset = sock._sQlen;
1747
1748 buff[offset] = 248; // msg-type
1749
1750 buff[offset + 1] = 0; // padding
1751 buff[offset + 2] = 0; // padding
1752 buff[offset + 3] = 0; // padding
1753
1754 buff[offset + 4] = flags >> 24; // flags
1755 buff[offset + 5] = flags >> 16;
1756 buff[offset + 6] = flags >> 8;
1757 buff[offset + 7] = flags;
1758
1759 var n = payload.length;
1760
1761 buff[offset + 8] = n; // length
1762
1763 for (var i = 0; i < n; i++) {
1764 buff[offset + 9 + i] = payload.charCodeAt(i);
1765 }
1766
1767 sock._sQlen += 9 + n;
1768 sock.flush();
1769 },
1770
1771 enableContinuousUpdates: function (sock, enable, x, y, width, height) {
1772 var buff = sock._sQ;
1773 var offset = sock._sQlen;
1774
1775 buff[offset] = 150; // msg-type
1776 buff[offset + 1] = enable; // enable-flag
1777
1778 buff[offset + 2] = x >> 8; // x
1779 buff[offset + 3] = x;
1780 buff[offset + 4] = y >> 8; // y
1781 buff[offset + 5] = y;
1782 buff[offset + 6] = width >> 8; // width
1783 buff[offset + 7] = width;
1784 buff[offset + 8] = height >> 8; // height
1785 buff[offset + 9] = height;
1786
1787 sock._sQlen += 10;
1788 sock.flush();
1789 },
1790
1791 pixelFormat: function (sock, depth, true_color) {
1792 var buff = sock._sQ;
1793 var offset = sock._sQlen;
1794
1795 var bpp, bits;
1796
1797 if (depth > 16) {
1798 bpp = 32;
1799 } else if (depth > 8) {
1800 bpp = 16;
1801 } else {
1802 bpp = 8;
1803 }
1804
1805 bits = Math.floor(depth/3);
1806
1807 buff[offset] = 0; // msg-type
1808
1809 buff[offset + 1] = 0; // padding
1810 buff[offset + 2] = 0; // padding
1811 buff[offset + 3] = 0; // padding
1812
1813 buff[offset + 4] = bpp; // bits-per-pixel
1814 buff[offset + 5] = depth; // depth
1815 buff[offset + 6] = 0; // little-endian
1816 buff[offset + 7] = true_color ? 1 : 0; // true-color
1817
1818 buff[offset + 8] = 0; // red-max
1819 buff[offset + 10] = 0; // green-max
1820 buff[offset + 12] = 0; // blue-max
1821
1822 if (depth == 8) {
1823 buff[offset + 9] = 7; // red-max
1824 buff[offset + 11] = 7; // green-max
1825 buff[offset + 13] = 3; // blue-max
1826 buff[offset + 14] = 5; // red-shift
1827 buff[offset + 15] = 2; // green-shift
1828 buff[offset + 16] = 0; // blue-shift
1829 } else {
1830 buff[offset + 9] = (1 << bits) - 1; // red-max
1831 buff[offset + 11] = (1 << bits) - 1; // green-max
1832 buff[offset + 13] = (1 << bits) - 1; // blue-max
1833 buff[offset + 14] = bits * 2; // red-shift
1834 buff[offset + 15] = bits * 1; // green-shift
1835 buff[offset + 16] = bits * 0; // blue-shift
1836 }
1837
1838 buff[offset + 17] = 0; // padding
1839 buff[offset + 18] = 0; // padding
1840 buff[offset + 19] = 0; // padding
1841
1842 sock._sQlen += 20;
1843 sock.flush();
1844 },
1845
1846 clientEncodings: function (sock, encodings) {
1847 var buff = sock._sQ;
1848 var offset = sock._sQlen;
1849
1850 buff[offset] = 2; // msg-type
1851 buff[offset + 1] = 0; // padding
1852
1853 buff[offset + 2] = encodings.length >> 8;
1854 buff[offset + 3] = encodings.length;
1855
1856 var i, j = offset + 4;
1857 for (i = 0; i < encodings.length; i++) {
1858 var enc = encodings[i];
1859 buff[j] = enc >> 24;
1860 buff[j + 1] = enc >> 16;
1861 buff[j + 2] = enc >> 8;
1862 buff[j + 3] = enc;
1863
1864 j += 4;
1865 }
1866
1867 sock._sQlen += j - offset;
1868 sock.flush();
1869 },
1870
1871 fbUpdateRequest: function (sock, incremental, x, y, w, h) {
1872 var buff = sock._sQ;
1873 var offset = sock._sQlen;
1874
1875 if (typeof(x) === "undefined") { x = 0; }
1876 if (typeof(y) === "undefined") { y = 0; }
1877
1878 buff[offset] = 3; // msg-type
1879 buff[offset + 1] = incremental ? 1 : 0;
1880
1881 buff[offset + 2] = (x >> 8) & 0xFF;
1882 buff[offset + 3] = x & 0xFF;
1883
1884 buff[offset + 4] = (y >> 8) & 0xFF;
1885 buff[offset + 5] = y & 0xFF;
1886
1887 buff[offset + 6] = (w >> 8) & 0xFF;
1888 buff[offset + 7] = w & 0xFF;
1889
1890 buff[offset + 8] = (h >> 8) & 0xFF;
1891 buff[offset + 9] = h & 0xFF;
1892
1893 sock._sQlen += 10;
1894 sock.flush();
1895 },
1896
1897 xvpOp: function (sock, ver, op) {
1898 var buff = sock._sQ;
1899 var offset = sock._sQlen;
1900
1901 buff[offset] = 250; // msg-type
1902 buff[offset + 1] = 0; // padding
1903
1904 buff[offset + 2] = ver;
1905 buff[offset + 3] = op;
1906
1907 sock._sQlen += 4;
1908 sock.flush();
1909 },
1910 };
1911
1912 RFB.genDES = function (password, challenge) {
1913 var passwd = [];
1914 for (var i = 0; i < password.length; i++) {
1915 passwd.push(password.charCodeAt(i));
1916 }
1917 return (new DES(passwd)).encrypt(challenge);
1918 };
1919
1920 RFB.encodingHandlers = {
1921 RAW: function () {
1922 if (this._FBU.lines === 0) {
1923 this._FBU.lines = this._FBU.height;
1924 }
1925
1926 var pixelSize = this._fb_depth == 8 ? 1 : 4;
1927 this._FBU.bytes = this._FBU.width * pixelSize; // at least a line
1928 if (this._sock.rQwait("RAW", this._FBU.bytes)) { return false; }
1929 var cur_y = this._FBU.y + (this._FBU.height - this._FBU.lines);
1930 var curr_height = Math.min(this._FBU.lines,
1931 Math.floor(this._sock.rQlen() / (this._FBU.width * pixelSize)));
1932 var data = this._sock.get_rQ();
1933 var index = this._sock.get_rQi();
1934 if (this._fb_depth == 8) {
1935 var pixels = this._FBU.width * curr_height
1936 var newdata = new Uint8Array(pixels * 4);
1937 var i;
1938 for (i = 0;i < pixels;i++) {
1939 //newdata[i * 4 + 0] = ((data[index + i] >> 0) & 0x3) * 255 / 3;
1940 //newdata[i * 4 + 1] = ((data[index + i] >> 2) & 0x3) * 255 / 3;
1941 //newdata[i * 4 + 2] = ((data[index + i] >> 4) & 0x3) * 255 / 3;
1942 // Convert 8 bit RGB332 color to internal RGB888.
1943 newdata[i * 4 + 0] = ((data[index + i] >> 0) & 0x3) * 255 / 3; // blue
1944 newdata[i * 4 + 1] = ((data[index + i] >> 2) & 0x7) * 255 / 7; // green
1945 newdata[i * 4 + 2] = ((data[index + i] >> 5) & 0x7) * 255 / 7; // red
1946 newdata[i * 4 + 4] = 0;
1947 }
1948 data = newdata;
1949 index = 0;
1950 }
1951 this._display.blitImage(this._FBU.x, cur_y, this._FBU.width,
1952 curr_height, data, index);
1953 this._sock.rQskipBytes(this._FBU.width * curr_height * pixelSize);
1954 this._FBU.lines -= curr_height;
1955
1956 if (this._FBU.lines > 0) {
1957 this._FBU.bytes = this._FBU.width * pixelSize; // At least another line
1958 } else {
1959 this._FBU.rects--;
1960 this._FBU.bytes = 0;
1961 }
1962
1963 return true;
1964 },
1965
1966 COPYRECT: function () {
1967 this._FBU.bytes = 4;
1968 if (this._sock.rQwait("COPYRECT", 4)) { return false; }
1969 this._display.copyImage(this._sock.rQshift16(), this._sock.rQshift16(),
1970 this._FBU.x, this._FBU.y, this._FBU.width,
1971 this._FBU.height);
1972
1973 this._FBU.rects--;
1974 this._FBU.bytes = 0;
1975 return true;
1976 },
1977
1978 RRE: function () {
1979 var color;
1980 if (this._FBU.subrects === 0) {
1981 this._FBU.bytes = 4 + 4;
1982 if (this._sock.rQwait("RRE", 4 + 4)) { return false; }
1983 this._FBU.subrects = this._sock.rQshift32();
1984 color = this._sock.rQshiftBytes(4); // Background
1985 this._display.fillRect(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, color);
1986 }
1987
1988 while (this._FBU.subrects > 0 && this._sock.rQlen() >= (4 + 8)) {
1989 color = this._sock.rQshiftBytes(4);
1990 var x = this._sock.rQshift16();
1991 var y = this._sock.rQshift16();
1992 var width = this._sock.rQshift16();
1993 var height = this._sock.rQshift16();
1994 this._display.fillRect(this._FBU.x + x, this._FBU.y + y, width, height, color);
1995 this._FBU.subrects--;
1996 }
1997
1998 if (this._FBU.subrects > 0) {
1999 var chunk = Math.min(this._rre_chunk_sz, this._FBU.subrects);
2000 this._FBU.bytes = (4 + 8) * chunk;
2001 } else {
2002 this._FBU.rects--;
2003 this._FBU.bytes = 0;
2004 }
2005
2006 return true;
2007 },
2008
2009 HEXTILE: function () {
2010 var rQ = this._sock.get_rQ();
2011 var rQi = this._sock.get_rQi();
2012
2013 if (this._FBU.tiles === 0) {
2014 this._FBU.tiles_x = Math.ceil(this._FBU.width / 16);
2015 this._FBU.tiles_y = Math.ceil(this._FBU.height / 16);
2016 this._FBU.total_tiles = this._FBU.tiles_x * this._FBU.tiles_y;
2017 this._FBU.tiles = this._FBU.total_tiles;
2018 }
2019
2020 while (this._FBU.tiles > 0) {
2021 this._FBU.bytes = 1;
2022 if (this._sock.rQwait("HEXTILE subencoding", this._FBU.bytes)) { return false; }
2023 var subencoding = rQ[rQi]; // Peek
2024 if (subencoding > 30) { // Raw
2025 this._fail("Illegal hextile subencoding (subencoding: " +
2026 subencoding + ")");
2027 return false;
2028 }
2029
2030 var subrects = 0;
2031 var curr_tile = this._FBU.total_tiles - this._FBU.tiles;
2032 var tile_x = curr_tile % this._FBU.tiles_x;
2033 var tile_y = Math.floor(curr_tile / this._FBU.tiles_x);
2034 var x = this._FBU.x + tile_x * 16;
2035 var y = this._FBU.y + tile_y * 16;
2036 var w = Math.min(16, (this._FBU.x + this._FBU.width) - x);
2037 var h = Math.min(16, (this._FBU.y + this._FBU.height) - y);
2038
2039 // Figure out how much we are expecting
2040 if (subencoding & 0x01) { // Raw
2041 this._FBU.bytes += w * h * 4;
2042 } else {
2043 if (subencoding & 0x02) { // Background
2044 this._FBU.bytes += 4;
2045 }
2046 if (subencoding & 0x04) { // Foreground
2047 this._FBU.bytes += 4;
2048 }
2049 if (subencoding & 0x08) { // AnySubrects
2050 this._FBU.bytes++; // Since we aren't shifting it off
2051 if (this._sock.rQwait("hextile subrects header", this._FBU.bytes)) { return false; }
2052 subrects = rQ[rQi + this._FBU.bytes - 1]; // Peek
2053 if (subencoding & 0x10) { // SubrectsColoured
2054 this._FBU.bytes += subrects * (4 + 2);
2055 } else {
2056 this._FBU.bytes += subrects * 2;
2057 }
2058 }
2059 }
2060
2061 if (this._sock.rQwait("hextile", this._FBU.bytes)) { return false; }
2062
2063 // We know the encoding and have a whole tile
2064 this._FBU.subencoding = rQ[rQi];
2065 rQi++;
2066 if (this._FBU.subencoding === 0) {
2067 if (this._FBU.lastsubencoding & 0x01) {
2068 // Weird: ignore blanks are RAW
2069 Log.Debug(" Ignoring blank after RAW");
2070 } else {
2071 this._display.fillRect(x, y, w, h, this._FBU.background);
2072 }
2073 } else if (this._FBU.subencoding & 0x01) { // Raw
2074 this._display.blitImage(x, y, w, h, rQ, rQi);
2075 rQi += this._FBU.bytes - 1;
2076 } else {
2077 if (this._FBU.subencoding & 0x02) { // Background
2078 this._FBU.background = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
2079 rQi += 4;
2080 }
2081 if (this._FBU.subencoding & 0x04) { // Foreground
2082 this._FBU.foreground = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
2083 rQi += 4;
2084 }
2085
2086 this._display.startTile(x, y, w, h, this._FBU.background);
2087 if (this._FBU.subencoding & 0x08) { // AnySubrects
2088 subrects = rQ[rQi];
2089 rQi++;
2090
2091 for (var s = 0; s < subrects; s++) {
2092 var color;
2093 if (this._FBU.subencoding & 0x10) { // SubrectsColoured
2094 color = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
2095 rQi += 4;
2096 } else {
2097 color = this._FBU.foreground;
2098 }
2099 var xy = rQ[rQi];
2100 rQi++;
2101 var sx = (xy >> 4);
2102 var sy = (xy & 0x0f);
2103
2104 var wh = rQ[rQi];
2105 rQi++;
2106 var sw = (wh >> 4) + 1;
2107 var sh = (wh & 0x0f) + 1;
2108
2109 this._display.subTile(sx, sy, sw, sh, color);
2110 }
2111 }
2112 this._display.finishTile();
2113 }
2114 this._sock.set_rQi(rQi);
2115 this._FBU.lastsubencoding = this._FBU.subencoding;
2116 this._FBU.bytes = 0;
2117 this._FBU.tiles--;
2118 }
2119
2120 if (this._FBU.tiles === 0) {
2121 this._FBU.rects--;
2122 }
2123
2124 return true;
2125 },
2126
2127 TIGHT: function () {
2128 this._FBU.bytes = 1; // compression-control byte
2129 if (this._sock.rQwait("TIGHT compression-control", this._FBU.bytes)) { return false; }
2130
2131 var checksum = function (data) {
2132 var sum = 0;
2133 for (var i = 0; i < data.length; i++) {
2134 sum += data[i];
2135 if (sum > 65536) sum -= 65536;
2136 }
2137 return sum;
2138 };
2139
2140 var resetStreams = 0;
2141 var streamId = -1;
2142 var decompress = function (data, expected) {
2143 for (var i = 0; i < 4; i++) {
2144 if ((resetStreams >> i) & 1) {
2145 this._FBU.zlibs[i].reset();
2146 Log.Info("Reset zlib stream " + i);
2147 }
2148 }
2149
2150 //var uncompressed = this._FBU.zlibs[streamId].uncompress(data, 0);
2151 var uncompressed = this._FBU.zlibs[streamId].inflate(data, true, expected);
2152 /*if (uncompressed.status !== 0) {
2153 Log.Error("Invalid data in zlib stream");
2154 }*/
2155
2156 //return uncompressed.data;
2157 return uncompressed;
2158 }.bind(this);
2159
2160 var indexedToRGBX2Color = function (data, palette, width, height) {
2161 // Convert indexed (palette based) image data to RGB
2162 // TODO: reduce number of calculations inside loop
2163 var dest = this._destBuff;
2164 var w = Math.floor((width + 7) / 8);
2165 var w1 = Math.floor(width / 8);
2166
2167 /*for (var y = 0; y < height; y++) {
2168 var b, x, dp, sp;
2169 var yoffset = y * width;
2170 var ybitoffset = y * w;
2171 var xoffset, targetbyte;
2172 for (x = 0; x < w1; x++) {
2173 xoffset = yoffset + x * 8;
2174 targetbyte = data[ybitoffset + x];
2175 for (b = 7; b >= 0; b--) {
2176 dp = (xoffset + 7 - b) * 3;
2177 sp = (targetbyte >> b & 1) * 3;
2178 dest[dp] = palette[sp];
2179 dest[dp + 1] = palette[sp + 1];
2180 dest[dp + 2] = palette[sp + 2];
2181 }
2182 }
2183
2184 xoffset = yoffset + x * 8;
2185 targetbyte = data[ybitoffset + x];
2186 for (b = 7; b >= 8 - width % 8; b--) {
2187 dp = (xoffset + 7 - b) * 3;
2188 sp = (targetbyte >> b & 1) * 3;
2189 dest[dp] = palette[sp];
2190 dest[dp + 1] = palette[sp + 1];
2191 dest[dp + 2] = palette[sp + 2];
2192 }
2193 }*/
2194
2195 for (var y = 0; y < height; y++) {
2196 var b, x, dp, sp;
2197 for (x = 0; x < w1; x++) {
2198 for (b = 7; b >= 0; b--) {
2199 dp = (y * width + x * 8 + 7 - b) * 4;
2200 sp = (data[y * w + x] >> b & 1) * 3;
2201 dest[dp] = palette[sp];
2202 dest[dp + 1] = palette[sp + 1];
2203 dest[dp + 2] = palette[sp + 2];
2204 dest[dp + 3] = 255;
2205 }
2206 }
2207
2208 for (b = 7; b >= 8 - width % 8; b--) {
2209 dp = (y * width + x * 8 + 7 - b) * 4;
2210 sp = (data[y * w + x] >> b & 1) * 3;
2211 dest[dp] = palette[sp];
2212 dest[dp + 1] = palette[sp + 1];
2213 dest[dp + 2] = palette[sp + 2];
2214 dest[dp + 3] = 255;
2215 }
2216 }
2217
2218 return dest;
2219 }.bind(this);
2220
2221 var indexedToRGBX = function (data, palette, width, height) {
2222 // Convert indexed (palette based) image data to RGB
2223 var dest = this._destBuff;
2224 var total = width * height * 4;
2225 for (var i = 0, j = 0; i < total; i += 4, j++) {
2226 var sp = data[j] * 3;
2227 dest[i] = palette[sp];
2228 dest[i + 1] = palette[sp + 1];
2229 dest[i + 2] = palette[sp + 2];
2230 dest[i + 3] = 255;
2231 }
2232
2233 return dest;
2234 }.bind(this);
2235
2236 var rQi = this._sock.get_rQi();
2237 var rQ = this._sock.rQwhole();
2238 var cmode, data;
2239 var cl_header, cl_data;
2240
2241 var handlePalette = function () {
2242 var numColors = rQ[rQi + 2] + 1;
2243 var paletteSize = numColors * 3;
2244 this._FBU.bytes += paletteSize;
2245 if (this._sock.rQwait("TIGHT palette " + cmode, this._FBU.bytes)) { return false; }
2246
2247 var bpp = (numColors <= 2) ? 1 : 8;
2248 var rowSize = Math.floor((this._FBU.width * bpp + 7) / 8);
2249 var raw = false;
2250 if (rowSize * this._FBU.height < 12) {
2251 raw = true;
2252 cl_header = 0;
2253 cl_data = rowSize * this._FBU.height;
2254 //clength = [0, rowSize * this._FBU.height];
2255 } else {
2256 // begin inline getTightCLength (returning two-item arrays is bad for performance with GC)
2257 var cl_offset = rQi + 3 + paletteSize;
2258 cl_header = 1;
2259 cl_data = 0;
2260 cl_data += rQ[cl_offset] & 0x7f;
2261 if (rQ[cl_offset] & 0x80) {
2262 cl_header++;
2263 cl_data += (rQ[cl_offset + 1] & 0x7f) << 7;
2264 if (rQ[cl_offset + 1] & 0x80) {
2265 cl_header++;
2266 cl_data += rQ[cl_offset + 2] << 14;
2267 }
2268 }
2269 // end inline getTightCLength
2270 }
2271
2272 this._FBU.bytes += cl_header + cl_data;
2273 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2274
2275 // Shift ctl, filter id, num colors, palette entries, and clength off
2276 this._sock.rQskipBytes(3);
2277 //var palette = this._sock.rQshiftBytes(paletteSize);
2278 this._sock.rQshiftTo(this._paletteBuff, paletteSize);
2279 this._sock.rQskipBytes(cl_header);
2280
2281 if (raw) {
2282 data = this._sock.rQshiftBytes(cl_data);
2283 } else {
2284 data = decompress(this._sock.rQshiftBytes(cl_data), rowSize * this._FBU.height);
2285 }
2286
2287 // Convert indexed (palette based) image data to RGB
2288 var rgbx;
2289 if (numColors == 2) {
2290 rgbx = indexedToRGBX2Color(data, this._paletteBuff, this._FBU.width, this._FBU.height);
2291 this._display.blitRgbxImage(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, rgbx, 0, false);
2292 } else {
2293 rgbx = indexedToRGBX(data, this._paletteBuff, this._FBU.width, this._FBU.height);
2294 this._display.blitRgbxImage(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, rgbx, 0, false);
2295 }
2296
2297
2298 return true;
2299 }.bind(this);
2300
2301 var handleCopy = function () {
2302 var raw = false;
2303 var uncompressedSize = this._FBU.width * this._FBU.height * 3;
2304 if (uncompressedSize < 12) {
2305 raw = true;
2306 cl_header = 0;
2307 cl_data = uncompressedSize;
2308 } else {
2309 // begin inline getTightCLength (returning two-item arrays is for peformance with GC)
2310 var cl_offset = rQi + 1;
2311 cl_header = 1;
2312 cl_data = 0;
2313 cl_data += rQ[cl_offset] & 0x7f;
2314 if (rQ[cl_offset] & 0x80) {
2315 cl_header++;
2316 cl_data += (rQ[cl_offset + 1] & 0x7f) << 7;
2317 if (rQ[cl_offset + 1] & 0x80) {
2318 cl_header++;
2319 cl_data += rQ[cl_offset + 2] << 14;
2320 }
2321 }
2322 // end inline getTightCLength
2323 }
2324 this._FBU.bytes = 1 + cl_header + cl_data;
2325 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2326
2327 // Shift ctl, clength off
2328 this._sock.rQshiftBytes(1 + cl_header);
2329
2330 if (raw) {
2331 data = this._sock.rQshiftBytes(cl_data);
2332 } else {
2333 data = decompress(this._sock.rQshiftBytes(cl_data), uncompressedSize);
2334 }
2335
2336 this._display.blitRgbImage(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, data, 0, false);
2337
2338 return true;
2339 }.bind(this);
2340
2341 var ctl = this._sock.rQpeek8();
2342
2343 // Keep tight reset bits
2344 resetStreams = ctl & 0xF;
2345
2346 // Figure out filter
2347 ctl = ctl >> 4;
2348 streamId = ctl & 0x3;
2349
2350 if (ctl === 0x08) cmode = "fill";
2351 else if (ctl === 0x09) cmode = "jpeg";
2352 else if (ctl === 0x0A) cmode = "png";
2353 else if (ctl & 0x04) cmode = "filter";
2354 else if (ctl < 0x04) cmode = "copy";
2355 else return this._fail("Illegal tight compression received (ctl: " +
2356 ctl + ")");
2357
2358 switch (cmode) {
2359 // fill use depth because TPIXELs drop the padding byte
2360 case "fill": // TPIXEL
2361 this._FBU.bytes += 3;
2362 break;
2363 case "jpeg": // max clength
2364 this._FBU.bytes += 3;
2365 break;
2366 case "png": // max clength
2367 this._FBU.bytes += 3;
2368 break;
2369 case "filter": // filter id + num colors if palette
2370 this._FBU.bytes += 2;
2371 break;
2372 case "copy":
2373 break;
2374 }
2375
2376 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2377
2378 // Determine FBU.bytes
2379 switch (cmode) {
2380 case "fill":
2381 // skip ctl byte
2382 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);
2383 this._sock.rQskipBytes(4);
2384 break;
2385 case "png":
2386 case "jpeg":
2387 // begin inline getTightCLength (returning two-item arrays is for peformance with GC)
2388 var cl_offset = rQi + 1;
2389 cl_header = 1;
2390 cl_data = 0;
2391 cl_data += rQ[cl_offset] & 0x7f;
2392 if (rQ[cl_offset] & 0x80) {
2393 cl_header++;
2394 cl_data += (rQ[cl_offset + 1] & 0x7f) << 7;
2395 if (rQ[cl_offset + 1] & 0x80) {
2396 cl_header++;
2397 cl_data += rQ[cl_offset + 2] << 14;
2398 }
2399 }
2400 // end inline getTightCLength
2401 this._FBU.bytes = 1 + cl_header + cl_data; // ctl + clength size + jpeg-data
2402 if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
2403
2404 // We have everything, render it
2405 this._sock.rQskipBytes(1 + cl_header); // shift off clt + compact length
2406 data = this._sock.rQshiftBytes(cl_data);
2407 this._display.imageRect(this._FBU.x, this._FBU.y, "image/" + cmode, data);
2408 break;
2409 case "filter":
2410 var filterId = rQ[rQi + 1];
2411 if (filterId === 1) {
2412 if (!handlePalette()) { return false; }
2413 } else {
2414 // Filter 0, Copy could be valid here, but servers don't send it as an explicit filter
2415 // Filter 2, Gradient is valid but not use if jpeg is enabled
2416 this._fail("Unsupported tight subencoding received " +
2417 "(filter: " + filterId + ")");
2418 }
2419 break;
2420 case "copy":
2421 if (!handleCopy()) { return false; }
2422 break;
2423 }
2424
2425
2426 this._FBU.bytes = 0;
2427 this._FBU.rects--;
2428
2429 return true;
2430 },
2431
2432 last_rect: function () {
2433 this._FBU.rects = 0;
2434 return true;
2435 },
2436
2437 ExtendedDesktopSize: function () {
2438 this._FBU.bytes = 1;
2439 if (this._sock.rQwait("ExtendedDesktopSize", this._FBU.bytes)) { return false; }
2440
2441 var firstUpdate = !this._supportsSetDesktopSize;
2442 this._supportsSetDesktopSize = true;
2443
2444 // Normally we only apply the current resize mode after a
2445 // window resize event. However there is no such trigger on the
2446 // initial connect. And we don't know if the server supports
2447 // resizing until we've gotten here.
2448 if (firstUpdate) {
2449 this._requestRemoteResize();
2450 }
2451
2452 var number_of_screens = this._sock.rQpeek8();
2453
2454 this._FBU.bytes = 4 + (number_of_screens * 16);
2455 if (this._sock.rQwait("ExtendedDesktopSize", this._FBU.bytes)) { return false; }
2456
2457 this._sock.rQskipBytes(1); // number-of-screens
2458 this._sock.rQskipBytes(3); // padding
2459
2460 for (var i = 0; i < number_of_screens; i += 1) {
2461 // Save the id and flags of the first screen
2462 if (i === 0) {
2463 this._screen_id = this._sock.rQshiftBytes(4); // id
2464 this._sock.rQskipBytes(2); // x-position
2465 this._sock.rQskipBytes(2); // y-position
2466 this._sock.rQskipBytes(2); // width
2467 this._sock.rQskipBytes(2); // height
2468 this._screen_flags = this._sock.rQshiftBytes(4); // flags
2469 } else {
2470 this._sock.rQskipBytes(16);
2471 }
2472 }
2473
2474 /*
2475 * The x-position indicates the reason for the change:
2476 *
2477 * 0 - server resized on its own
2478 * 1 - this client requested the resize
2479 * 2 - another client requested the resize
2480 */
2481
2482 // We need to handle errors when we requested the resize.
2483 if (this._FBU.x === 1 && this._FBU.y !== 0) {
2484 var msg = "";
2485 // The y-position indicates the status code from the server
2486 switch (this._FBU.y) {
2487 case 1:
2488 msg = "Resize is administratively prohibited";
2489 break;
2490 case 2:
2491 msg = "Out of resources";
2492 break;
2493 case 3:
2494 msg = "Invalid screen layout";
2495 break;
2496 default:
2497 msg = "Unknown reason";
2498 break;
2499 }
2500 Log.Warn("Server did not accept the resize request: "
2501 + msg);
2502 } else {
2503 this._resize(this._FBU.width, this._FBU.height);
2504 }
2505
2506 this._FBU.bytes = 0;
2507 this._FBU.rects -= 1;
2508 return true;
2509 },
2510
2511 DesktopSize: function () {
2512 this._resize(this._FBU.width, this._FBU.height);
2513 this._FBU.bytes = 0;
2514 this._FBU.rects -= 1;
2515 return true;
2516 },
2517
2518 Cursor: function () {
2519 Log.Debug(">> set_cursor");
2520 var x = this._FBU.x; // hotspot-x
2521 var y = this._FBU.y; // hotspot-y
2522 var w = this._FBU.width;
2523 var h = this._FBU.height;
2524
2525 var pixelslength = w * h * 4;
2526 var masklength = Math.floor((w + 7) / 8) * h;
2527
2528 this._FBU.bytes = pixelslength + masklength;
2529 if (this._sock.rQwait("cursor encoding", this._FBU.bytes)) { return false; }
2530
2531 this._display.changeCursor(this._sock.rQshiftBytes(pixelslength),
2532 this._sock.rQshiftBytes(masklength),
2533 x, y, w, h);
2534
2535 this._FBU.bytes = 0;
2536 this._FBU.rects--;
2537
2538 Log.Debug("<< set_cursor");
2539 return true;
2540 },
2541
2542 QEMUExtendedKeyEvent: function () {
2543 this._FBU.rects--;
2544
2545 // Old Safari doesn't support creating keyboard events
2546 try {
2547 var keyboardEvent = document.createEvent("keyboardEvent");
2548 if (keyboardEvent.code !== undefined) {
2549 this._qemuExtKeyEventSupported = true;
2550 }
2551 } catch (err) {
2552 }
2553 },
2554 };

mercurial