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