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