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