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