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