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