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