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