rfb.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Copyright (C) 2013 Samuel Mannehed for Cendio AB
  5. * Licensed under MPL 2.0 (see LICENSE.txt)
  6. *
  7. * See README.md for usage and integration instructions.
  8. *
  9. * TIGHT decoder portion:
  10. * (c) 2012 Michael Tinglof, Joe Balaz, Les Piech (Mercuri.ca)
  11. */
  12. /*jslint white: false, browser: true */
  13. /*global window, Util, Display, Keyboard, Mouse, Websock, Websock_native, Base64, DES */
  14. var RFB;
  15. (function () {
  16. "use strict";
  17. RFB = function (defaults) {
  18. if (!defaults) {
  19. defaults = {};
  20. }
  21. this._rfb_host = '';
  22. this._rfb_port = 5900;
  23. this._rfb_password = '';
  24. this._rfb_path = '';
  25. this._rfb_state = 'disconnected';
  26. this._rfb_version = 0;
  27. this._rfb_max_version = 3.8;
  28. this._rfb_auth_scheme = '';
  29. this._rfb_tightvnc = false;
  30. this._rfb_xvp_ver = 0;
  31. // In preference order
  32. this._encodings = [
  33. ['COPYRECT', 0x01 ],
  34. ['TIGHT', 0x07 ],
  35. ['TIGHT_PNG', -260 ],
  36. ['HEXTILE', 0x05 ],
  37. ['RRE', 0x02 ],
  38. ['RAW', 0x00 ],
  39. ['DesktopSize', -223 ],
  40. ['Cursor', -239 ],
  41. // Psuedo-encoding settings
  42. //['JPEG_quality_lo', -32 ],
  43. ['JPEG_quality_med', -26 ],
  44. //['JPEG_quality_hi', -23 ],
  45. //['compress_lo', -255 ],
  46. ['compress_hi', -247 ],
  47. ['last_rect', -224 ],
  48. ['xvp', -309 ]
  49. ];
  50. this._encHandlers = {};
  51. this._encNames = {};
  52. this._encStats = {};
  53. this._sock = null; // Websock object
  54. this._display = null; // Display object
  55. this._keyboard = null; // Keyboard input handler object
  56. this._mouse = null; // Mouse input handler object
  57. this._sendTimer = null; // Send Queue check timer
  58. this._disconnTimer = null; // disconnection timer
  59. this._msgTimer = null; // queued handle_msg timer
  60. // Frame buffer update state
  61. this._FBU = {
  62. rects: 0,
  63. subrects: 0, // RRE
  64. lines: 0, // RAW
  65. tiles: 0, // HEXTILE
  66. bytes: 0,
  67. x: 0,
  68. y: 0,
  69. width: 0,
  70. height: 0,
  71. encoding: 0,
  72. subencoding: -1,
  73. background: null,
  74. zlib: [] // TIGHT zlib streams
  75. };
  76. this._fb_Bpp = 4;
  77. this._fb_depth = 3;
  78. this._fb_width = 0;
  79. this._fb_height = 0;
  80. this._fb_name = "";
  81. this._rre_chunk_sz = 100;
  82. this._timing = {
  83. last_fbu: 0,
  84. fbu_total: 0,
  85. fbu_total_cnt: 0,
  86. full_fbu_total: 0,
  87. full_fbu_cnt: 0,
  88. fbu_rt_start: 0,
  89. fbu_rt_total: 0,
  90. fbu_rt_cnt: 0,
  91. pixels: 0
  92. };
  93. // Mouse state
  94. this._mouse_buttonMask = 0;
  95. this._mouse_arr = [];
  96. this._viewportDragging = false;
  97. this._viewportDragPos = {};
  98. // set the default value on user-facing properties
  99. Util.set_defaults(this, defaults, {
  100. 'target': 'null', // VNC display rendering Canvas object
  101. 'focusContainer': document, // DOM element that captures keyboard input
  102. 'encrypt': false, // Use TLS/SSL/wss encryption
  103. 'true_color': true, // Request true color pixel data
  104. 'local_cursor': false, // Request locally rendered cursor
  105. 'shared': true, // Request shared mode
  106. 'view_only': false, // Disable client mouse/keyboard
  107. 'xvp_password_sep': '@', // Separator for XVP password fields
  108. 'disconnectTimeout': 3, // Time (s) to wait for disconnection
  109. 'wsProtocols': ['binary', 'base64'], // Protocols to use in the WebSocket connection
  110. 'repeaterID': '', // [UltraVNC] RepeaterID to connect to
  111. 'viewportDrag': false, // Move the viewport on mouse drags
  112. // Callback functions
  113. 'onUpdateState': function () { }, // onUpdateState(rfb, state, oldstate, statusMsg): state update/change
  114. 'onPasswordRequired': function () { }, // onPasswordRequired(rfb): VNC password is required
  115. 'onClipboard': function () { }, // onClipboard(rfb, text): RFB clipboard contents received
  116. 'onBell': function () { }, // onBell(rfb): RFB Bell message received
  117. 'onFBUReceive': function () { }, // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
  118. 'onFBUComplete': function () { }, // onFBUComplete(rfb, fbu): RFB FBU received and processed
  119. 'onFBResize': function () { }, // onFBResize(rfb, width, height): frame buffer resized
  120. 'onDesktopName': function () { }, // onDesktopName(rfb, name): desktop name received
  121. 'onXvpInit': function () { }, // onXvpInit(version): XVP extensions active for this connection
  122. });
  123. // main setup
  124. Util.Debug(">> RFB.constructor");
  125. // populate encHandlers with bound versions
  126. Object.keys(RFB.encodingHandlers).forEach(function (encName) {
  127. this._encHandlers[encName] = RFB.encodingHandlers[encName].bind(this);
  128. }.bind(this));
  129. // Create lookup tables based on encoding number
  130. for (var i = 0; i < this._encodings.length; i++) {
  131. this._encHandlers[this._encodings[i][1]] = this._encHandlers[this._encodings[i][0]];
  132. this._encNames[this._encodings[i][1]] = this._encodings[i][0];
  133. this._encStats[this._encodings[i][1]] = [0, 0];
  134. }
  135. try {
  136. this._display = new Display({target: this._target});
  137. } catch (exc) {
  138. Util.Error("Display exception: " + exc);
  139. this._updateState('fatal', "No working Display");
  140. }
  141. this._keyboard = new Keyboard({target: this._focusContainer,
  142. onKeyPress: this._handleKeyPress.bind(this)});
  143. this._mouse = new Mouse({target: this._target,
  144. onMouseButton: this._handleMouseButton.bind(this),
  145. onMouseMove: this._handleMouseMove.bind(this),
  146. notify: this._keyboard.sync.bind(this._keyboard)});
  147. this._sock = new Websock();
  148. this._sock.on('message', this._handle_message.bind(this));
  149. this._sock.on('open', function () {
  150. if (this._rfb_state === 'connect') {
  151. this._updateState('ProtocolVersion', "Starting VNC handshake");
  152. } else {
  153. this._fail("Got unexpected WebSocket connection");
  154. }
  155. }.bind(this));
  156. this._sock.on('close', function (e) {
  157. Util.Warn("WebSocket on-close event");
  158. var msg = "";
  159. if (e.code) {
  160. msg = " (code: " + e.code;
  161. if (e.reason) {
  162. msg += ", reason: " + e.reason;
  163. }
  164. msg += ")";
  165. }
  166. if (this._rfb_state === 'disconnect') {
  167. this._updateState('disconnected', 'VNC disconnected' + msg);
  168. } else if (this._rfb_state === 'ProtocolVersion') {
  169. this._fail('Failed to connect to server' + msg);
  170. } else if (this._rfb_state in {'failed': 1, 'disconnected': 1}) {
  171. Util.Error("Received onclose while disconnected" + msg);
  172. } else {
  173. this._fail("Server disconnected" + msg);
  174. }
  175. }.bind(this));
  176. this._sock.on('error', function (e) {
  177. Util.Warn("WebSocket on-error event");
  178. });
  179. this._init_vars();
  180. var rmode = this._display.get_render_mode();
  181. if (Websock_native) {
  182. Util.Info("Using native WebSockets");
  183. this._updateState('loaded', 'noVNC ready: native WebSockets, ' + rmode);
  184. } else {
  185. Util.Warn("Using web-socket-js bridge. Flash version: " + Util.Flash.version);
  186. if (!Util.Flash || Util.Flash.version < 9) {
  187. this._updateState('fatal', "WebSockets or <a href='http://get.adobe.com/flashplayer'>Adobe Flash</a> is required");
  188. } else if (document.location.href.substr(0, 7) === 'file://') {
  189. this._updateState('fatal', "'file://' URL is incompatible with Adobe Flash");
  190. } else {
  191. this._updateState('loaded', 'noVNC ready: WebSockets emulation, ' + rmode);
  192. }
  193. }
  194. Util.Debug("<< RFB.constructor");
  195. };
  196. RFB.prototype = {
  197. // Public methods
  198. connect: function (host, port, password, path) {
  199. this._rfb_host = host;
  200. this._rfb_port = port;
  201. this._rfb_password = (password !== undefined) ? password : "";
  202. this._rfb_path = (path !== undefined) ? path : "";
  203. if (!this._rfb_host || !this._rfb_port) {
  204. return this._fail("Must set host and port");
  205. }
  206. this._updateState('connect');
  207. },
  208. disconnect: function () {
  209. this._updateState('disconnect', 'Disconnecting');
  210. },
  211. sendPassword: function (passwd) {
  212. this._rfb_password = passwd;
  213. this._rfb_state = 'Authentication';
  214. setTimeout(this._init_msg.bind(this), 1);
  215. },
  216. sendCtrlAltDel: function () {
  217. if (this._rfb_state !== 'normal' || this._view_only) { return false; }
  218. Util.Info("Sending Ctrl-Alt-Del");
  219. var arr = [];
  220. arr = arr.concat(RFB.messages.keyEvent(XK_Control_L, 1));
  221. arr = arr.concat(RFB.messages.keyEvent(XK_Alt_L, 1));
  222. arr = arr.concat(RFB.messages.keyEvent(XK_Delete, 1));
  223. arr = arr.concat(RFB.messages.keyEvent(XK_Delete, 0));
  224. arr = arr.concat(RFB.messages.keyEvent(XK_Alt_L, 0));
  225. arr = arr.concat(RFB.messages.keyEvent(XK_Control_L, 0));
  226. this._sock.send(arr);
  227. },
  228. xvpOp: function (ver, op) {
  229. if (this._rfb_xvp_ver < ver) { return false; }
  230. Util.Info("Sending XVP operation " + op + " (version " + ver + ")");
  231. this._sock.send_string("\xFA\x00" + String.fromCharCode(ver) + String.fromCharCode(op));
  232. return true;
  233. },
  234. xvpShutdown: function () {
  235. return this.xvpOp(1, 2);
  236. },
  237. xvpReboot: function () {
  238. return this.xvpOp(1, 3);
  239. },
  240. xvpReset: function () {
  241. return this.xvpOp(1, 4);
  242. },
  243. // Send a key press. If 'down' is not specified then send a down key
  244. // followed by an up key.
  245. sendKey: function (code, down) {
  246. if (this._rfb_state !== "normal" || this._view_only) { return false; }
  247. var arr = [];
  248. if (typeof down !== 'undefined') {
  249. Util.Info("Sending key code (" + (down ? "down" : "up") + "): " + code);
  250. arr = arr.concat(RFB.messages.keyEvent(code, down ? 1 : 0));
  251. } else {
  252. Util.Info("Sending key code (down + up): " + code);
  253. arr = arr.concat(RFB.messages.keyEvent(code, 1));
  254. arr = arr.concat(RFB.messages.keyEvent(code, 0));
  255. }
  256. this._sock.send(arr);
  257. },
  258. clipboardPasteFrom: function (text) {
  259. if (this._rfb_state !== 'normal') { return; }
  260. this._sock.send(RFB.messages.clientCutText(text));
  261. },
  262. // Private methods
  263. _connect: function () {
  264. Util.Debug(">> RFB.connect");
  265. var uri;
  266. if (typeof UsingSocketIO !== 'undefined') {
  267. uri = 'http';
  268. } else {
  269. uri = this._encrypt ? 'wss' : 'ws';
  270. }
  271. uri += '://' + this._rfb_host + ':' + this._rfb_port + '/' + this._rfb_path;
  272. Util.Info("connecting to " + uri);
  273. this._sock.open(uri, this._wsProtocols);
  274. Util.Debug("<< RFB.connect");
  275. },
  276. _init_vars: function () {
  277. // reset state
  278. this._sock.init();
  279. this._FBU.rects = 0;
  280. this._FBU.subrects = 0; // RRE and HEXTILE
  281. this._FBU.lines = 0; // RAW
  282. this._FBU.tiles = 0; // HEXTILE
  283. this._FBU.zlibs = []; // TIGHT zlib encoders
  284. this._mouse_buttonMask = 0;
  285. this._mouse_arr = [];
  286. this._rfb_tightvnc = false;
  287. // Clear the per connection encoding stats
  288. var i;
  289. for (i = 0; i < this._encodings.length; i++) {
  290. this._encStats[this._encodings[i][1]][0] = 0;
  291. }
  292. for (i = 0; i < 4; i++) {
  293. this._FBU.zlibs[i] = new TINF();
  294. this._FBU.zlibs[i].init();
  295. }
  296. },
  297. _print_stats: function () {
  298. Util.Info("Encoding stats for this connection:");
  299. var i, s;
  300. for (i = 0; i < this._encodings.length; i++) {
  301. s = this._encStats[this._encodings[i][1]];
  302. if (s[0] + s[1] > 0) {
  303. Util.Info(" " + this._encodings[i][0] + ": " + s[0] + " rects");
  304. }
  305. }
  306. Util.Info("Encoding stats since page load:");
  307. for (i = 0; i < this._encodings.length; i++) {
  308. s = this._encStats[this._encodings[i][1]];
  309. Util.Info(" " + this._encodings[i][0] + ": " + s[1] + " rects");
  310. }
  311. },
  312. /*
  313. * Page states:
  314. * loaded - page load, equivalent to disconnected
  315. * disconnected - idle state
  316. * connect - starting to connect (to ProtocolVersion)
  317. * normal - connected
  318. * disconnect - starting to disconnect
  319. * failed - abnormal disconnect
  320. * fatal - failed to load page, or fatal error
  321. *
  322. * RFB protocol initialization states:
  323. * ProtocolVersion
  324. * Security
  325. * Authentication
  326. * password - waiting for password, not part of RFB
  327. * SecurityResult
  328. * ClientInitialization - not triggered by server message
  329. * ServerInitialization (to normal)
  330. */
  331. _updateState: function (state, statusMsg) {
  332. var oldstate = this._rfb_state;
  333. if (state === oldstate) {
  334. // Already here, ignore
  335. Util.Debug("Already in state '" + state + "', ignoring");
  336. }
  337. /*
  338. * These are disconnected states. A previous connect may
  339. * asynchronously cause a connection so make sure we are closed.
  340. */
  341. if (state in {'disconnected': 1, 'loaded': 1, 'connect': 1,
  342. 'disconnect': 1, 'failed': 1, 'fatal': 1}) {
  343. if (this._sendTimer) {
  344. clearInterval(this._sendTimer);
  345. this._sendTimer = null;
  346. }
  347. if (this._msgTimer) {
  348. clearInterval(this._msgTimer);
  349. this._msgTimer = null;
  350. }
  351. if (this._display && this._display.get_context()) {
  352. this._keyboard.ungrab();
  353. this._mouse.ungrab();
  354. this._display.defaultCursor();
  355. if (Util.get_logging() !== 'debug' || state === 'loaded') {
  356. // Show noVNC logo on load and when disconnected, unless in
  357. // debug mode
  358. this._display.clear();
  359. }
  360. }
  361. this._sock.close();
  362. }
  363. if (oldstate === 'fatal') {
  364. Util.Error('Fatal error, cannot continue');
  365. }
  366. var cmsg = typeof(statusMsg) !== 'undefined' ? (" Msg: " + statusMsg) : "";
  367. var fullmsg = "New state '" + state + "', was '" + oldstate + "'." + cmsg;
  368. if (state === 'failed' || state === 'fatal') {
  369. Util.Error(cmsg);
  370. } else {
  371. Util.Warn(cmsg);
  372. }
  373. if (oldstate === 'failed' && state === 'disconnected') {
  374. // do disconnect action, but stay in failed state
  375. this._rfb_state = 'failed';
  376. } else {
  377. this._rfb_state = state;
  378. }
  379. if (this._disconnTimer && this._rfb_state !== 'disconnect') {
  380. Util.Debug("Clearing disconnect timer");
  381. clearTimeout(this._disconnTimer);
  382. this._disconnTimer = null;
  383. }
  384. switch (state) {
  385. case 'normal':
  386. if (oldstate === 'disconnected' || oldstate === 'failed') {
  387. Util.Error("Invalid transition from 'disconnected' or 'failed' to 'normal'");
  388. }
  389. break;
  390. case 'connect':
  391. this._init_vars();
  392. this._connect();
  393. // WebSocket.onopen transitions to 'ProtocolVersion'
  394. break;
  395. case 'disconnect':
  396. this._disconnTimer = setTimeout(function () {
  397. this._fail("Disconnect timeout");
  398. }.bind(this), this._disconnectTimeout * 1000);
  399. this._print_stats();
  400. // WebSocket.onclose transitions to 'disconnected'
  401. break;
  402. case 'failed':
  403. if (oldstate === 'disconnected') {
  404. Util.Error("Invalid transition from 'disconnected' to 'failed'");
  405. } else if (oldstate === 'normal') {
  406. Util.Error("Error while connected.");
  407. } else if (oldstate === 'init') {
  408. Util.Error("Error while initializing.");
  409. }
  410. // Make sure we transition to disconnected
  411. setTimeout(function () {
  412. this._updateState('disconnected');
  413. }.bind(this), 50);
  414. break;
  415. default:
  416. // No state change action to take
  417. }
  418. if (oldstate === 'failed' && state === 'disconnected') {
  419. this._onUpdateState(this, state, oldstate);
  420. } else {
  421. this._onUpdateState(this, state, oldstate, statusMsg);
  422. }
  423. },
  424. _fail: function (msg) {
  425. this._updateState('failed', msg);
  426. return false;
  427. },
  428. _handle_message: function () {
  429. if (this._sock.rQlen() === 0) {
  430. Util.Warn("handle_message called on an empty receive queue");
  431. return;
  432. }
  433. switch (this._rfb_state) {
  434. case 'disconnected':
  435. case 'failed':
  436. Util.Error("Got data while disconnected");
  437. break;
  438. case 'normal':
  439. if (this._normal_msg() && this._sock.rQlen() > 0) {
  440. // true means we can continue processing
  441. // Give other events a chance to run
  442. if (this._msgTimer === null) {
  443. Util.Debug("More data to process, creating timer");
  444. this._msgTimer = setTimeout(function () {
  445. this._msgTimer = null;
  446. this._handle_message();
  447. }.bind(this), 10);
  448. } else {
  449. Util.Debug("More data to process, existing timer");
  450. }
  451. }
  452. break;
  453. default:
  454. this._init_msg();
  455. break;
  456. }
  457. },
  458. _checkEvents: function () {
  459. if (this._rfb_state === 'normal' && !this._viewportDragging && this._mouse_arr.length > 0) {
  460. this._sock.send(this._mouse_arr);
  461. this._mouse_arr = [];
  462. }
  463. },
  464. _handleKeyPress: function (keysym, down) {
  465. if (this._view_only) { return; } // View only, skip keyboard, events
  466. this._sock.send(RFB.messages.keyEvent(keysym, down));
  467. },
  468. _handleMouseButton: function (x, y, down, bmask) {
  469. if (down) {
  470. this._mouse_buttonMask |= bmask;
  471. } else {
  472. this._mouse_buttonMask ^= bmask;
  473. }
  474. if (this._viewportDrag) {
  475. if (down && !this._viewportDragging) {
  476. this._viewportDragging = true;
  477. this._viewportDragPos = {'x': x, 'y': y};
  478. // Skip sending mouse events
  479. return;
  480. } else {
  481. this._viewportDragging = false;
  482. }
  483. }
  484. if (this._view_only) { return; } // View only, skip mouse events
  485. this._mouse_arr = this._mouse_arr.concat(
  486. RFB.messages.pointerEvent(this._display.absX(x), this._display.absY(y), this._mouse_buttonMask));
  487. this._sock.send(this._mouse_arr);
  488. this._mouse_arr = [];
  489. },
  490. _handleMouseMove: function (x, y) {
  491. if (this._viewportDragging) {
  492. var deltaX = this._viewportDragPos.x - x;
  493. var deltaY = this._viewportDragPos.y - y;
  494. this._viewportDragPos = {'x': x, 'y': y};
  495. this._display.viewportChange(deltaX, deltaY);
  496. // Skip sending mouse events
  497. return;
  498. }
  499. if (this._view_only) { return; } // View only, skip mouse events
  500. this._mouse_arr = this._mouse_arr.concat(
  501. RFB.messages.pointerEvent(this._display.absX(x), this._display.absY(y), this._mouse_buttonMask));
  502. this._checkEvents();
  503. },
  504. // Message Handlers
  505. _negotiate_protocol_version: function () {
  506. if (this._sock.rQlen() < 12) {
  507. return this._fail("Incomplete protocol version");
  508. }
  509. var sversion = this._sock.rQshiftStr(12).substr(4, 7);
  510. Util.Info("Server ProtocolVersion: " + sversion);
  511. var is_repeater = 0;
  512. switch (sversion) {
  513. case "000.000": // UltraVNC repeater
  514. is_repeater = 1;
  515. break;
  516. case "003.003":
  517. case "003.006": // UltraVNC
  518. case "003.889": // Apple Remote Desktop
  519. this._rfb_version = 3.3;
  520. break;
  521. case "003.007":
  522. this._rfb_version = 3.7;
  523. break;
  524. case "003.008":
  525. case "004.000": // Intel AMT KVM
  526. case "004.001": // RealVNC 4.6
  527. this._rfb_version = 3.8;
  528. break;
  529. default:
  530. return this._fail("Invalid server version " + sversion);
  531. }
  532. if (is_repeater) {
  533. var repeaterID = this._repeaterID;
  534. while (repeaterID.length < 250) {
  535. repeaterID += "\0";
  536. }
  537. this._sock.send_string(repeaterID);
  538. return true;
  539. }
  540. if (this._rfb_version > this._rfb_max_version) {
  541. this._rfb_version = this._rfb_max_version;
  542. }
  543. // Send updates either at a rate of 1 update per 50ms, or
  544. // whatever slower rate the network can handle
  545. this._sendTimer = setInterval(this._sock.flush.bind(this._sock), 50);
  546. var cversion = "00" + parseInt(this._rfb_version, 10) +
  547. ".00" + ((this._rfb_version * 10) % 10);
  548. this._sock.send_string("RFB " + cversion + "\n");
  549. this._updateState('Security', 'Sent ProtocolVersion: ' + cversion);
  550. },
  551. _negotiate_security: function () {
  552. if (this._rfb_version >= 3.7) {
  553. // Server sends supported list, client decides
  554. var num_types = this._sock.rQshift8();
  555. if (this._sock.rQwait("security type", num_types, 1)) { return false; }
  556. if (num_types === 0) {
  557. var strlen = this._sock.rQshift32();
  558. var reason = this._sock.rQshiftStr(strlen);
  559. return this._fail("Security failure: " + reason);
  560. }
  561. this._rfb_auth_scheme = 0;
  562. var types = this._sock.rQshiftBytes(num_types);
  563. Util.Debug("Server security types: " + types);
  564. for (var i = 0; i < types.length; i++) {
  565. if (types[i] > this._rfb_auth_scheme && (types[i] <= 16 || types[i] == 22)) {
  566. this._rfb_auth_scheme = types[i];
  567. }
  568. }
  569. if (this._rfb_auth_scheme === 0) {
  570. return this._fail("Unsupported security types: " + types);
  571. }
  572. this._sock.send([this._rfb_auth_scheme]);
  573. } else {
  574. // Server decides
  575. if (this._sock.rQwait("security scheme", 4)) { return false; }
  576. this._rfb_auth_scheme = this._sock.rQshift32();
  577. }
  578. this._updateState('Authentication', 'Authenticating using scheme: ' + this._rfb_auth_scheme);
  579. return this._init_msg(); // jump to authentication
  580. },
  581. // authentication
  582. _negotiate_xvp_auth: function () {
  583. var xvp_sep = this._xvp_password_sep;
  584. var xvp_auth = this._rfb_password.split(xvp_sep);
  585. if (xvp_auth.length < 3) {
  586. this._updateState('password', 'XVP credentials required (user' + xvp_sep +
  587. 'target' + xvp_sep + 'password) -- got only ' + this._rfb_password);
  588. this._onPasswordRequired(this);
  589. return false;
  590. }
  591. var xvp_auth_str = String.fromCharCode(xvp_auth[0].length) +
  592. String.fromCharCode(xvp_auth[1].length) +
  593. xvp_auth[0] +
  594. xvp_auth[1];
  595. this._sock.send_string(xvp_auth_str);
  596. this._rfb_password = xvp_auth.slice(2).join(xvp_sep);
  597. this._rfb_auth_scheme = 2;
  598. return this._negotiate_authentication();
  599. },
  600. _negotiate_std_vnc_auth: function () {
  601. if (this._rfb_password.length === 0) {
  602. // Notify via both callbacks since it's kind of
  603. // an RFB state change and a UI interface issue
  604. this._updateState('password', "Password Required");
  605. this._onPasswordRequired(this);
  606. }
  607. if (this._sock.rQwait("auth challenge", 16)) { return false; }
  608. var challenge = this._sock.rQshiftBytes(16);
  609. var response = RFB.genDES(this._rfb_password, challenge);
  610. this._sock.send(response);
  611. this._updateState("SecurityResult");
  612. return true;
  613. },
  614. _negotiate_tight_tunnels: function (numTunnels) {
  615. var clientSupportedTunnelTypes = {
  616. 0: { vendor: 'TGHT', signature: 'NOTUNNEL' }
  617. };
  618. var serverSupportedTunnelTypes = {};
  619. // receive tunnel capabilities
  620. for (var i = 0; i < numTunnels; i++) {
  621. var cap_code = this._sock.rQshift32();
  622. var cap_vendor = this._sock.rQshiftStr(4);
  623. var cap_signature = this._sock.rQshiftStr(8);
  624. serverSupportedTunnelTypes[cap_code] = { vendor: cap_vendor, signature: cap_signature };
  625. }
  626. // choose the notunnel type
  627. if (serverSupportedTunnelTypes[0]) {
  628. if (serverSupportedTunnelTypes[0].vendor != clientSupportedTunnelTypes[0].vendor ||
  629. serverSupportedTunnelTypes[0].signature != clientSupportedTunnelTypes[0].signature) {
  630. return this._fail("Client's tunnel type had the incorrect vendor or signature");
  631. }
  632. this._sock.send([0, 0, 0, 0]); // use NOTUNNEL
  633. return false; // wait until we receive the sub auth count to continue
  634. } else {
  635. return this._fail("Server wanted tunnels, but doesn't support the notunnel type");
  636. }
  637. },
  638. _negotiate_tight_auth: function () {
  639. if (!this._rfb_tightvnc) { // first pass, do the tunnel negotiation
  640. if (this._sock.rQwait("num tunnels", 4)) { return false; }
  641. var numTunnels = this._sock.rQshift32();
  642. if (numTunnels > 0 && this._sock.rQwait("tunnel capabilities", 16 * numTunnels, 4)) { return false; }
  643. this._rfb_tightvnc = true;
  644. if (numTunnels > 0) {
  645. this._negotiate_tight_tunnels(numTunnels);
  646. return false; // wait until we receive the sub auth to continue
  647. }
  648. }
  649. // second pass, do the sub-auth negotiation
  650. if (this._sock.rQwait("sub auth count", 4)) { return false; }
  651. var subAuthCount = this._sock.rQshift32();
  652. if (this._sock.rQwait("sub auth capabilities", 16 * subAuthCount, 4)) { return false; }
  653. var clientSupportedTypes = {
  654. 'STDVNOAUTH__': 1,
  655. 'STDVVNCAUTH_': 2
  656. };
  657. var serverSupportedTypes = [];
  658. for (var i = 0; i < subAuthCount; i++) {
  659. var capNum = this._sock.rQshift32();
  660. var capabilities = this._sock.rQshiftStr(12);
  661. serverSupportedTypes.push(capabilities);
  662. }
  663. for (var authType in clientSupportedTypes) {
  664. if (serverSupportedTypes.indexOf(authType) != -1) {
  665. this._sock.send([0, 0, 0, clientSupportedTypes[authType]]);
  666. switch (authType) {
  667. case 'STDVNOAUTH__': // no auth
  668. this._updateState('SecurityResult');
  669. return true;
  670. case 'STDVVNCAUTH_': // VNC auth
  671. this._rfb_auth_scheme = 2;
  672. return this._init_msg();
  673. default:
  674. return this._fail("Unsupported tiny auth scheme: " + authType);
  675. }
  676. }
  677. }
  678. this._fail("No supported sub-auth types!");
  679. },
  680. _negotiate_authentication: function () {
  681. switch (this._rfb_auth_scheme) {
  682. case 0: // connection failed
  683. if (this._sock.rQwait("auth reason", 4)) { return false; }
  684. var strlen = this._sock.rQshift32();
  685. var reason = this._sock.rQshiftStr(strlen);
  686. return this._fail("Auth failure: " + reason);
  687. case 1: // no auth
  688. if (this._rfb_version >= 3.8) {
  689. this._updateState('SecurityResult');
  690. return true;
  691. }
  692. this._updateState('ClientInitialisation', "No auth required");
  693. return this._init_msg();
  694. case 22: // XVP auth
  695. return this._negotiate_xvp_auth();
  696. case 2: // VNC authentication
  697. return this._negotiate_std_vnc_auth();
  698. case 16: // TightVNC Security Type
  699. return this._negotiate_tight_auth();
  700. default:
  701. return this._fail("Unsupported auth scheme: " + this._rfb_auth_scheme);
  702. }
  703. },
  704. _handle_security_result: function () {
  705. if (this._sock.rQwait('VNC auth response ', 4)) { return false; }
  706. switch (this._sock.rQshift32()) {
  707. case 0: // OK
  708. this._updateState('ClientInitialisation', 'Authentication OK');
  709. return this._init_msg();
  710. case 1: // failed
  711. if (this._rfb_version >= 3.8) {
  712. var length = this._sock.rQshift32();
  713. if (this._sock.rQwait("SecurityResult reason", length, 8)) { return false; }
  714. var reason = this._sock.rQshiftStr(length);
  715. return this._fail(reason);
  716. } else {
  717. return this._fail("Authentication failure");
  718. }
  719. return false;
  720. case 2:
  721. return this._fail("Too many auth attempts");
  722. }
  723. },
  724. _negotiate_server_init: function () {
  725. if (this._sock.rQwait("server initialization", 24)) { return false; }
  726. /* Screen size */
  727. this._fb_width = this._sock.rQshift16();
  728. this._fb_height = this._sock.rQshift16();
  729. /* PIXEL_FORMAT */
  730. var bpp = this._sock.rQshift8();
  731. var depth = this._sock.rQshift8();
  732. var big_endian = this._sock.rQshift8();
  733. var true_color = this._sock.rQshift8();
  734. var red_max = this._sock.rQshift16();
  735. var green_max = this._sock.rQshift16();
  736. var blue_max = this._sock.rQshift16();
  737. var red_shift = this._sock.rQshift8();
  738. var green_shift = this._sock.rQshift8();
  739. var blue_shift = this._sock.rQshift8();
  740. this._sock.rQskipBytes(3); // padding
  741. // NB(directxman12): we don't want to call any callbacks or print messages until
  742. // *after* we're past the point where we could backtrack
  743. /* Connection name/title */
  744. var name_length = this._sock.rQshift32();
  745. if (this._sock.rQwait('server init name', name_length, 24)) { return false; }
  746. this._fb_name = Util.decodeUTF8(this._sock.rQshiftStr(name_length));
  747. if (this._rfb_tightvnc) {
  748. if (this._sock.rQwait('TightVNC extended server init header', 8, 24 + name_length)) { return false; }
  749. // In TightVNC mode, ServerInit message is extended
  750. var numServerMessages = this._sock.rQshift16();
  751. var numClientMessages = this._sock.rQshift16();
  752. var numEncodings = this._sock.rQshift16();
  753. this._sock.rQskipBytes(2); // padding
  754. var totalMessagesLength = (numServerMessages + numClientMessages + numEncodings) * 16;
  755. if (this._sock.rQwait('TightVNC extended server init header', totalMessagesLength, 32 + name_length)) { return false; }
  756. var i;
  757. for (i = 0; i < numServerMessages; i++) {
  758. var srvMsg = this._sock.rQshiftStr(16);
  759. }
  760. for (i = 0; i < numClientMessages; i++) {
  761. var clientMsg = this._sock.rQshiftStr(16);
  762. }
  763. for (i = 0; i < numEncodings; i++) {
  764. var encoding = this._sock.rQshiftStr(16);
  765. }
  766. }
  767. // NB(directxman12): these are down here so that we don't run them multiple times
  768. // if we backtrack
  769. Util.Info("Screen: " + this._fb_width + "x" + this._fb_height +
  770. ", bpp: " + bpp + ", depth: " + depth +
  771. ", big_endian: " + big_endian +
  772. ", true_color: " + true_color +
  773. ", red_max: " + red_max +
  774. ", green_max: " + green_max +
  775. ", blue_max: " + blue_max +
  776. ", red_shift: " + red_shift +
  777. ", green_shift: " + green_shift +
  778. ", blue_shift: " + blue_shift);
  779. if (big_endian !== 0) {
  780. Util.Warn("Server native endian is not little endian");
  781. }
  782. if (red_shift !== 16) {
  783. Util.Warn("Server native red-shift is not 16");
  784. }
  785. if (blue_shift !== 0) {
  786. Util.Warn("Server native blue-shift is not 0");
  787. }
  788. // we're past the point where we could backtrack, so it's safe to call this
  789. this._onDesktopName(this, this._fb_name);
  790. if (this._true_color && this._fb_name === "Intel(r) AMT KVM") {
  791. Util.Warn("Intel AMT KVM only supports 8/16 bit depths. Disabling true color");
  792. this._true_color = false;
  793. }
  794. this._display.set_true_color(this._true_color);
  795. this._onFBResize(this, this._fb_width, this._fb_height);
  796. this._display.resize(this._fb_width, this._fb_height);
  797. this._keyboard.grab();
  798. this._mouse.grab();
  799. if (this._true_color) {
  800. this._fb_Bpp = 4;
  801. this._fb_depth = 3;
  802. } else {
  803. this._fb_Bpp = 1;
  804. this._fb_depth = 1;
  805. }
  806. var response = RFB.messages.pixelFormat(this._fb_Bpp, this._fb_depth, this._true_color);
  807. response = response.concat(
  808. RFB.messages.clientEncodings(this._encodings, this._local_cursor, this._true_color));
  809. response = response.concat(
  810. RFB.messages.fbUpdateRequests(this._display.getCleanDirtyReset(),
  811. this._fb_width, this._fb_height));
  812. this._timing.fbu_rt_start = (new Date()).getTime();
  813. this._timing.pixels = 0;
  814. this._sock.send(response);
  815. this._checkEvents();
  816. if (this._encrypt) {
  817. this._updateState('normal', 'Connected (encrypted) to: ' + this._fb_name);
  818. } else {
  819. this._updateState('normal', 'Connected (unencrypted) to: ' + this._fb_name);
  820. }
  821. },
  822. _init_msg: function () {
  823. switch (this._rfb_state) {
  824. case 'ProtocolVersion':
  825. return this._negotiate_protocol_version();
  826. case 'Security':
  827. return this._negotiate_security();
  828. case 'Authentication':
  829. return this._negotiate_authentication();
  830. case 'SecurityResult':
  831. return this._handle_security_result();
  832. case 'ClientInitialisation':
  833. this._sock.send([this._shared ? 1 : 0]); // ClientInitialisation
  834. this._updateState('ServerInitialisation', "Authentication OK");
  835. return true;
  836. case 'ServerInitialisation':
  837. return this._negotiate_server_init();
  838. }
  839. },
  840. _handle_set_colour_map_msg: function () {
  841. Util.Debug("SetColorMapEntries");
  842. this._sock.rQskip8(); // Padding
  843. var first_colour = this._sock.rQshift16();
  844. var num_colours = this._sock.rQshift16();
  845. if (this._sock.rQwait('SetColorMapEntries', num_colours * 6, 6)) { return false; }
  846. for (var c = 0; c < num_colours; c++) {
  847. var red = parseInt(this._sock.rQshift16() / 256, 10);
  848. var green = parseInt(this._sock.rQshift16() / 256, 10);
  849. var blue = parseInt(this._sock.rQshift16() / 256, 10);
  850. this._display.set_colourMap([blue, green, red], first_colour + c);
  851. }
  852. Util.Debug("colourMap: " + this._display.get_colourMap());
  853. Util.Info("Registered " + num_colours + " colourMap entries");
  854. return true;
  855. },
  856. _handle_server_cut_text: function () {
  857. Util.Debug("ServerCutText");
  858. if (this._sock.rQwait("ServerCutText header", 7, 1)) { return false; }
  859. this._sock.rQskipBytes(3); // Padding
  860. var length = this._sock.rQshift32();
  861. if (this._sock.rQwait("ServerCutText", length, 8)) { return false; }
  862. var text = this._sock.rQshiftStr(length);
  863. this._onClipboard(this, text);
  864. return true;
  865. },
  866. _handle_xvp_msg: function () {
  867. if (this._sock.rQwait("XVP version and message", 3, 1)) { return false; }
  868. this._sock.rQskip8(); // Padding
  869. var xvp_ver = this._sock.rQshift8();
  870. var xvp_msg = this._sock.rQshift8();
  871. switch (xvp_msg) {
  872. case 0: // XVP_FAIL
  873. this._updateState(this._rfb_state, "Operation Failed");
  874. break;
  875. case 1: // XVP_INIT
  876. this._rfb_xvp_ver = xvp_ver;
  877. Util.Info("XVP extensions enabled (version " + this._rfb_xvp_ver + ")");
  878. this._onXvpInit(this._rfb_xvp_ver);
  879. break;
  880. default:
  881. this._fail("Disconnected: illegal server XVP message " + xvp_msg);
  882. break;
  883. }
  884. return true;
  885. },
  886. _normal_msg: function () {
  887. var msg_type;
  888. if (this._FBU.rects > 0) {
  889. msg_type = 0;
  890. } else {
  891. msg_type = this._sock.rQshift8();
  892. }
  893. switch (msg_type) {
  894. case 0: // FramebufferUpdate
  895. var ret = this._framebufferUpdate();
  896. if (ret) {
  897. this._sock.send(RFB.messages.fbUpdateRequests(this._display.getCleanDirtyReset(),
  898. this._fb_width, this._fb_height));
  899. }
  900. return ret;
  901. case 1: // SetColorMapEntries
  902. return this._handle_set_colour_map_msg();
  903. case 2: // Bell
  904. Util.Debug("Bell");
  905. this._onBell(this);
  906. return true;
  907. case 3: // ServerCutText
  908. return this._handle_server_cut_text();
  909. case 250: // XVP
  910. return this._handle_xvp_msg();
  911. default:
  912. this._fail("Disconnected: illegal server message type " + msg_type);
  913. Util.Debug("sock.rQslice(0, 30): " + this._sock.rQslice(0, 30));
  914. return true;
  915. }
  916. },
  917. _framebufferUpdate: function () {
  918. var ret = true;
  919. var now;
  920. if (this._FBU.rects === 0) {
  921. if (this._sock.rQwait("FBU header", 3, 1)) { return false; }
  922. this._sock.rQskip8(); // Padding
  923. this._FBU.rects = this._sock.rQshift16();
  924. this._FBU.bytes = 0;
  925. this._timing.cur_fbu = 0;
  926. if (this._timing.fbu_rt_start > 0) {
  927. now = (new Date()).getTime();
  928. Util.Info("First FBU latency: " + (now - this._timing.fbu_rt_start));
  929. }
  930. }
  931. while (this._FBU.rects > 0) {
  932. if (this._rfb_state !== "normal") { return false; }
  933. if (this._sock.rQwait("FBU", this._FBU.bytes)) { return false; }
  934. if (this._FBU.bytes === 0) {
  935. if (this._sock.rQwait("rect header", 12)) { return false; }
  936. /* New FramebufferUpdate */
  937. var hdr = this._sock.rQshiftBytes(12);
  938. this._FBU.x = (hdr[0] << 8) + hdr[1];
  939. this._FBU.y = (hdr[2] << 8) + hdr[3];
  940. this._FBU.width = (hdr[4] << 8) + hdr[5];
  941. this._FBU.height = (hdr[6] << 8) + hdr[7];
  942. this._FBU.encoding = parseInt((hdr[8] << 24) + (hdr[9] << 16) +
  943. (hdr[10] << 8) + hdr[11], 10);
  944. this._onFBUReceive(this,
  945. {'x': this._FBU.x, 'y': this._FBU.y,
  946. 'width': this._FBU.width, 'height': this._FBU.height,
  947. 'encoding': this._FBU.encoding,
  948. 'encodingName': this._encNames[this._FBU.encoding]});
  949. if (!this._encNames[this._FBU.encoding]) {
  950. this._fail("Disconnected: unsupported encoding " +
  951. this._FBU.encoding);
  952. return false;
  953. }
  954. }
  955. this._timing.last_fbu = (new Date()).getTime();
  956. ret = this._encHandlers[this._FBU.encoding]();
  957. now = (new Date()).getTime();
  958. this._timing.cur_fbu += (now - this._timing.last_fbu);
  959. if (ret) {
  960. this._encStats[this._FBU.encoding][0]++;
  961. this._encStats[this._FBU.encoding][1]++;
  962. this._timing.pixels += this._FBU.width * this._FBU.height;
  963. }
  964. if (this._timing.pixels >= (this._fb_width * this._fb_height)) {
  965. if ((this._FBU.width === this._fb_width && this._FBU.height === this._fb_height) ||
  966. this._timing.fbu_rt_start > 0) {
  967. this._timing.full_fbu_total += this._timing.cur_fbu;
  968. this._timing.full_fbu_cnt++;
  969. Util.Info("Timing of full FBU, curr: " +
  970. this._timing.cur_fbu + ", total: " +
  971. this._timing.full_fbu_total + ", cnt: " +
  972. this._timing.full_fbu_cnt + ", avg: " +
  973. (this._timing.full_fbu_total / this._timing.full_fbu_cnt));
  974. }
  975. if (this._timing.fbu_rt_start > 0) {
  976. var fbu_rt_diff = now - this._timing.fbu_rt_start;
  977. this._timing.fbu_rt_total += fbu_rt_diff;
  978. this._timing.fbu_rt_cnt++;
  979. Util.Info("full FBU round-trip, cur: " +
  980. fbu_rt_diff + ", total: " +
  981. this._timing.fbu_rt_total + ", cnt: " +
  982. this._timing.fbu_rt_cnt + ", avg: " +
  983. (this._timing.fbu_rt_total / this._timing.fbu_rt_cnt));
  984. this._timing.fbu_rt_start = 0;
  985. }
  986. }
  987. if (!ret) { return ret; } // need more data
  988. }
  989. this._onFBUComplete(this,
  990. {'x': this._FBU.x, 'y': this._FBU.y,
  991. 'width': this._FBU.width, 'height': this._FBU.height,
  992. 'encoding': this._FBU.encoding,
  993. 'encodingName': this._encNames[this._FBU.encoding]});
  994. return true; // We finished this FBU
  995. },
  996. };
  997. Util.make_properties(RFB, [
  998. ['target', 'wo', 'dom'], // VNC display rendering Canvas object
  999. ['focusContainer', 'wo', 'dom'], // DOM element that captures keyboard input
  1000. ['encrypt', 'rw', 'bool'], // Use TLS/SSL/wss encryption
  1001. ['true_color', 'rw', 'bool'], // Request true color pixel data
  1002. ['local_cursor', 'rw', 'bool'], // Request locally rendered cursor
  1003. ['shared', 'rw', 'bool'], // Request shared mode
  1004. ['view_only', 'rw', 'bool'], // Disable client mouse/keyboard
  1005. ['xvp_password_sep', 'rw', 'str'], // Separator for XVP password fields
  1006. ['disconnectTimeout', 'rw', 'int'], // Time (s) to wait for disconnection
  1007. ['wsProtocols', 'rw', 'arr'], // Protocols to use in the WebSocket connection
  1008. ['repeaterID', 'rw', 'str'], // [UltraVNC] RepeaterID to connect to
  1009. ['viewportDrag', 'rw', 'bool'], // Move the viewport on mouse drags
  1010. // Callback functions
  1011. ['onUpdateState', 'rw', 'func'], // onUpdateState(rfb, state, oldstate, statusMsg): RFB state update/change
  1012. ['onPasswordRequired', 'rw', 'func'], // onPasswordRequired(rfb): VNC password is required
  1013. ['onClipboard', 'rw', 'func'], // onClipboard(rfb, text): RFB clipboard contents received
  1014. ['onBell', 'rw', 'func'], // onBell(rfb): RFB Bell message received
  1015. ['onFBUReceive', 'rw', 'func'], // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
  1016. ['onFBUComplete', 'rw', 'func'], // onFBUComplete(rfb, fbu): RFB FBU received and processed
  1017. ['onFBResize', 'rw', 'func'], // onFBResize(rfb, width, height): frame buffer resized
  1018. ['onDesktopName', 'rw', 'func'], // onDesktopName(rfb, name): desktop name received
  1019. ['onXvpInit', 'rw', 'func'], // onXvpInit(version): XVP extensions active for this connection
  1020. ]);
  1021. RFB.prototype.set_local_cursor = function (cursor) {
  1022. if (!cursor || (cursor in {'0': 1, 'no': 1, 'false': 1})) {
  1023. this._local_cursor = false;
  1024. } else {
  1025. if (this._display.get_cursor_uri()) {
  1026. this._local_cursor = true;
  1027. } else {
  1028. Util.Warn("Browser does not support local cursor");
  1029. }
  1030. }
  1031. };
  1032. RFB.prototype.get_display = function () { return this._display; };
  1033. RFB.prototype.get_keyboard = function () { return this._keyboard; };
  1034. RFB.prototype.get_mouse = function () { return this._mouse; };
  1035. // Class Methods
  1036. RFB.messages = {
  1037. keyEvent: function (keysym, down) {
  1038. var arr = [4];
  1039. arr.push8(down);
  1040. arr.push16(0);
  1041. arr.push32(keysym);
  1042. return arr;
  1043. },
  1044. pointerEvent: function (x, y, mask) {
  1045. var arr = [5]; // msg-type
  1046. arr.push8(mask);
  1047. arr.push16(x);
  1048. arr.push16(y);
  1049. return arr;
  1050. },
  1051. // TODO(directxman12): make this unicode compatible?
  1052. clientCutText: function (text) {
  1053. var arr = [6]; // msg-type
  1054. arr.push8(0); // padding
  1055. arr.push8(0); // padding
  1056. arr.push8(0); // padding
  1057. arr.push32(text.length);
  1058. var n = text.length;
  1059. for (var i = 0; i < n; i++) {
  1060. arr.push(text.charCodeAt(i));
  1061. }
  1062. return arr;
  1063. },
  1064. pixelFormat: function (bpp, depth, true_color) {
  1065. var arr = [0]; // msg-type
  1066. arr.push8(0); // padding
  1067. arr.push8(0); // padding
  1068. arr.push8(0); // padding
  1069. arr.push8(bpp * 8); // bits-per-pixel
  1070. arr.push8(depth * 8); // depth
  1071. arr.push8(0); // little-endian
  1072. arr.push8(true_color ? 1 : 0); // true-color
  1073. arr.push16(255); // red-max
  1074. arr.push16(255); // green-max
  1075. arr.push16(255); // blue-max
  1076. arr.push8(16); // red-shift
  1077. arr.push8(8); // green-shift
  1078. arr.push8(0); // blue-shift
  1079. arr.push8(0); // padding
  1080. arr.push8(0); // padding
  1081. arr.push8(0); // padding
  1082. return arr;
  1083. },
  1084. clientEncodings: function (encodings, local_cursor, true_color) {
  1085. var i, encList = [];
  1086. for (i = 0; i < encodings.length; i++) {
  1087. if (encodings[i][0] === "Cursor" && !local_cursor) {
  1088. Util.Debug("Skipping Cursor pseudo-encoding");
  1089. } else if (encodings[i][0] === "TIGHT" && !true_color) {
  1090. // TODO: remove this when we have tight+non-true-color
  1091. Util.Warn("Skipping tight as it is only supported with true color");
  1092. } else {
  1093. encList.push(encodings[i][1]);
  1094. }
  1095. }
  1096. var arr = [2]; // msg-type
  1097. arr.push8(0); // padding
  1098. arr.push16(encList.length); // encoding count
  1099. for (i = 0; i < encList.length; i++) {
  1100. arr.push32(encList[i]);
  1101. }
  1102. return arr;
  1103. },
  1104. fbUpdateRequests: function (cleanDirty, fb_width, fb_height) {
  1105. var arr = [];
  1106. var cb = cleanDirty.cleanBox;
  1107. var w, h;
  1108. if (cb.w > 0 && cb.h > 0) {
  1109. w = typeof cb.w === "undefined" ? fb_width : cb.w;
  1110. h = typeof cb.h === "undefined" ? fb_height : cb.h;
  1111. // Request incremental for clean box
  1112. arr = arr.concat(RFB.messages.fbUpdateRequest(1, cb.x, cb.y, w, h));
  1113. }
  1114. for (var i = 0; i < cleanDirty.dirtyBoxes.length; i++) {
  1115. var db = cleanDirty.dirtyBoxes[i];
  1116. // Force all (non-incremental) for dirty box
  1117. w = typeof db.w === "undefined" ? fb_width : db.w;
  1118. h = typeof db.h === "undefined" ? fb_height : db.h;
  1119. arr = arr.concat(RFB.messages.fbUpdateRequest(0, db.x, db.y, w, h));
  1120. }
  1121. return arr;
  1122. },
  1123. fbUpdateRequest: function (incremental, x, y, w, h) {
  1124. if (typeof(x) === "undefined") { x = 0; }
  1125. if (typeof(y) === "undefined") { y = 0; }
  1126. var arr = [3]; // msg-type
  1127. arr.push8(incremental);
  1128. arr.push16(x);
  1129. arr.push16(y);
  1130. arr.push16(w);
  1131. arr.push16(h);
  1132. return arr;
  1133. }
  1134. };
  1135. RFB.genDES = function (password, challenge) {
  1136. var passwd = [];
  1137. for (var i = 0; i < password.length; i++) {
  1138. passwd.push(password.charCodeAt(i));
  1139. }
  1140. return (new DES(passwd)).encrypt(challenge);
  1141. };
  1142. RFB.extract_data_uri = function (arr) {
  1143. return ";base64," + Base64.encode(arr);
  1144. };
  1145. RFB.encodingHandlers = {
  1146. RAW: function () {
  1147. if (this._FBU.lines === 0) {
  1148. this._FBU.lines = this._FBU.height;
  1149. }
  1150. this._FBU.bytes = this._FBU.width * this._fb_Bpp; // at least a line
  1151. if (this._sock.rQwait("RAW", this._FBU.bytes)) { return false; }
  1152. var cur_y = this._FBU.y + (this._FBU.height - this._FBU.lines);
  1153. var curr_height = Math.min(this._FBU.lines,
  1154. Math.floor(this._sock.rQlen() / (this._FBU.width * this._fb_Bpp)));
  1155. this._display.blitImage(this._FBU.x, cur_y, this._FBU.width,
  1156. curr_height, this._sock.get_rQ(),
  1157. this._sock.get_rQi());
  1158. this._sock.rQskipBytes(this._FBU.width * curr_height * this._fb_Bpp);
  1159. this._FBU.lines -= curr_height;
  1160. if (this._FBU.lines > 0) {
  1161. this._FBU.bytes = this._FBU.width * this._fb_Bpp; // At least another line
  1162. } else {
  1163. this._FBU.rects--;
  1164. this._FBU.bytes = 0;
  1165. }
  1166. return true;
  1167. },
  1168. COPYRECT: function () {
  1169. this._FBU.bytes = 4;
  1170. if (this._sock.rQwait("COPYRECT", 4)) { return false; }
  1171. this._display.renderQ_push({
  1172. 'type': 'copy',
  1173. 'old_x': this._sock.rQshift16(),
  1174. 'old_y': this._sock.rQshift16(),
  1175. 'x': this._FBU.x,
  1176. 'y': this._FBU.y,
  1177. 'width': this._FBU.width,
  1178. 'height': this._FBU.height
  1179. });
  1180. this._FBU.rects--;
  1181. this._FBU.bytes = 0;
  1182. return true;
  1183. },
  1184. RRE: function () {
  1185. var color;
  1186. if (this._FBU.subrects === 0) {
  1187. this._FBU.bytes = 4 + this._fb_Bpp;
  1188. if (this._sock.rQwait("RRE", 4 + this._fb_Bpp)) { return false; }
  1189. this._FBU.subrects = this._sock.rQshift32();
  1190. color = this._sock.rQshiftBytes(this._fb_Bpp); // Background
  1191. this._display.fillRect(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, color);
  1192. }
  1193. while (this._FBU.subrects > 0 && this._sock.rQlen() >= (this._fb_Bpp + 8)) {
  1194. color = this._sock.rQshiftBytes(this._fb_Bpp);
  1195. var x = this._sock.rQshift16();
  1196. var y = this._sock.rQshift16();
  1197. var width = this._sock.rQshift16();
  1198. var height = this._sock.rQshift16();
  1199. this._display.fillRect(this._FBU.x + x, this._FBU.y + y, width, height, color);
  1200. this._FBU.subrects--;
  1201. }
  1202. if (this._FBU.subrects > 0) {
  1203. var chunk = Math.min(this._rre_chunk_sz, this._FBU.subrects);
  1204. this._FBU.bytes = (this._fb_Bpp + 8) * chunk;
  1205. } else {
  1206. this._FBU.rects--;
  1207. this._FBU.bytes = 0;
  1208. }
  1209. return true;
  1210. },
  1211. HEXTILE: function () {
  1212. var rQ = this._sock.get_rQ();
  1213. var rQi = this._sock.get_rQi();
  1214. if (this._FBU.tiles === 0) {
  1215. this._FBU.tiles_x = Math.ceil(this._FBU.width / 16);
  1216. this._FBU.tiles_y = Math.ceil(this._FBU.height / 16);
  1217. this._FBU.total_tiles = this._FBU.tiles_x * this._FBU.tiles_y;
  1218. this._FBU.tiles = this._FBU.total_tiles;
  1219. }
  1220. while (this._FBU.tiles > 0) {
  1221. this._FBU.bytes = 1;
  1222. if (this._sock.rQwait("HEXTILE subencoding", this._FBU.bytes)) { return false; }
  1223. var subencoding = rQ[rQi]; // Peek
  1224. if (subencoding > 30) { // Raw
  1225. this._fail("Disconnected: illegal hextile subencoding " + subencoding);
  1226. return false;
  1227. }
  1228. var subrects = 0;
  1229. var curr_tile = this._FBU.total_tiles - this._FBU.tiles;
  1230. var tile_x = curr_tile % this._FBU.tiles_x;
  1231. var tile_y = Math.floor(curr_tile / this._FBU.tiles_x);
  1232. var x = this._FBU.x + tile_x * 16;
  1233. var y = this._FBU.y + tile_y * 16;
  1234. var w = Math.min(16, (this._FBU.x + this._FBU.width) - x);
  1235. var h = Math.min(16, (this._FBU.y + this._FBU.height) - y);
  1236. // Figure out how much we are expecting
  1237. if (subencoding & 0x01) { // Raw
  1238. this._FBU.bytes += w * h * this._fb_Bpp;
  1239. } else {
  1240. if (subencoding & 0x02) { // Background
  1241. this._FBU.bytes += this._fb_Bpp;
  1242. }
  1243. if (subencoding & 0x04) { // Foreground
  1244. this._FBU.bytes += this._fb_Bpp;
  1245. }
  1246. if (subencoding & 0x08) { // AnySubrects
  1247. this._FBU.bytes++; // Since we aren't shifting it off
  1248. if (this._sock.rQwait("hextile subrects header", this._FBU.bytes)) { return false; }
  1249. subrects = rQ[rQi + this._FBU.bytes - 1]; // Peek
  1250. if (subencoding & 0x10) { // SubrectsColoured
  1251. this._FBU.bytes += subrects * (this._fb_Bpp + 2);
  1252. } else {
  1253. this._FBU.bytes += subrects * 2;
  1254. }
  1255. }
  1256. }
  1257. if (this._sock.rQwait("hextile", this._FBU.bytes)) { return false; }
  1258. // We know the encoding and have a whole tile
  1259. this._FBU.subencoding = rQ[rQi];
  1260. rQi++;
  1261. if (this._FBU.subencoding === 0) {
  1262. if (this._FBU.lastsubencoding & 0x01) {
  1263. // Weird: ignore blanks are RAW
  1264. Util.Debug(" Ignoring blank after RAW");
  1265. } else {
  1266. this._display.fillRect(x, y, w, h, this._FBU.background);
  1267. }
  1268. } else if (this._FBU.subencoding & 0x01) { // Raw
  1269. this._display.blitImage(x, y, w, h, rQ, rQi);
  1270. rQi += this._FBU.bytes - 1;
  1271. } else {
  1272. if (this._FBU.subencoding & 0x02) { // Background
  1273. this._FBU.background = rQ.slice(rQi, rQi + this._fb_Bpp);
  1274. rQi += this._fb_Bpp;
  1275. }
  1276. if (this._FBU.subencoding & 0x04) { // Foreground
  1277. this._FBU.foreground = rQ.slice(rQi, rQi + this._fb_Bpp);
  1278. rQi += this._fb_Bpp;
  1279. }
  1280. this._display.startTile(x, y, w, h, this._FBU.background);
  1281. if (this._FBU.subencoding & 0x08) { // AnySubrects
  1282. subrects = rQ[rQi];
  1283. rQi++;
  1284. for (var s = 0; s < subrects; s++) {
  1285. var color;
  1286. if (this._FBU.subencoding & 0x10) { // SubrectsColoured
  1287. color = rQ.slice(rQi, rQi + this._fb_Bpp);
  1288. rQi += this._fb_Bpp;
  1289. } else {
  1290. color = this._FBU.foreground;
  1291. }
  1292. var xy = rQ[rQi];
  1293. rQi++;
  1294. var sx = (xy >> 4);
  1295. var sy = (xy & 0x0f);
  1296. var wh = rQ[rQi];
  1297. rQi++;
  1298. var sw = (wh >> 4) + 1;
  1299. var sh = (wh & 0x0f) + 1;
  1300. this._display.subTile(sx, sy, sw, sh, color);
  1301. }
  1302. }
  1303. this._display.finishTile();
  1304. }
  1305. this._sock.set_rQi(rQi);
  1306. this._FBU.lastsubencoding = this._FBU.subencoding;
  1307. this._FBU.bytes = 0;
  1308. this._FBU.tiles--;
  1309. }
  1310. if (this._FBU.tiles === 0) {
  1311. this._FBU.rects--;
  1312. }
  1313. return true;
  1314. },
  1315. getTightCLength: function (arr) {
  1316. var header = 1, data = 0;
  1317. data += arr[0] & 0x7f;
  1318. if (arr[0] & 0x80) {
  1319. header++;
  1320. data += (arr[1] & 0x7f) << 7;
  1321. if (arr[1] & 0x80) {
  1322. header++;
  1323. data += arr[2] << 14;
  1324. }
  1325. }
  1326. return [header, data];
  1327. },
  1328. display_tight: function (isTightPNG) {
  1329. if (this._fb_depth === 1) {
  1330. this._fail("Tight protocol handler only implements true color mode");
  1331. }
  1332. this._FBU.bytes = 1; // compression-control byte
  1333. if (this._sock.rQwait("TIGHT compression-control", this._FBU.bytes)) { return false; }
  1334. var checksum = function (data) {
  1335. var sum = 0;
  1336. for (var i = 0; i < data.length; i++) {
  1337. sum += data[i];
  1338. if (sum > 65536) sum -= 65536;
  1339. }
  1340. return sum;
  1341. };
  1342. var resetStreams = 0;
  1343. var streamId = -1;
  1344. var decompress = function (data) {
  1345. for (var i = 0; i < 4; i++) {
  1346. if ((resetStreams >> i) & 1) {
  1347. this._FBU.zlibs[i].reset();
  1348. Util.Info("Reset zlib stream " + i);
  1349. }
  1350. }
  1351. var uncompressed = this._FBU.zlibs[streamId].uncompress(data, 0);
  1352. if (uncompressed.status !== 0) {
  1353. Util.Error("Invalid data in zlib stream");
  1354. }
  1355. return uncompressed.data;
  1356. }.bind(this);
  1357. var indexedToRGB = function (data, numColors, palette, width, height) {
  1358. // Convert indexed (palette based) image data to RGB
  1359. // TODO: reduce number of calculations inside loop
  1360. var dest = [];
  1361. var x, y, dp, sp;
  1362. if (numColors === 2) {
  1363. var w = Math.floor((width + 7) / 8);
  1364. var w1 = Math.floor(width / 8);
  1365. for (y = 0; y < height; y++) {
  1366. var b;
  1367. for (x = 0; x < w1; x++) {
  1368. for (b = 7; b >= 0; b--) {
  1369. dp = (y * width + x * 8 + 7 - b) * 3;
  1370. sp = (data[y * w + x] >> b & 1) * 3;
  1371. dest[dp] = palette[sp];
  1372. dest[dp + 1] = palette[sp + 1];
  1373. dest[dp + 2] = palette[sp + 2];
  1374. }
  1375. }
  1376. for (b = 7; b >= 8 - width % 8; b--) {
  1377. dp = (y * width + x * 8 + 7 - b) * 3;
  1378. sp = (data[y * w + x] >> b & 1) * 3;
  1379. dest[dp] = palette[sp];
  1380. dest[dp + 1] = palette[sp + 1];
  1381. dest[dp + 2] = palette[sp + 2];
  1382. }
  1383. }
  1384. } else {
  1385. for (y = 0; y < height; y++) {
  1386. for (x = 0; x < width; x++) {
  1387. dp = (y * width + x) * 3;
  1388. sp = data[y * width + x] * 3;
  1389. dest[dp] = palette[sp];
  1390. dest[dp + 1] = palette[sp + 1];
  1391. dest[dp + 2] = palette[sp + 2];
  1392. }
  1393. }
  1394. }
  1395. return dest;
  1396. }.bind(this);
  1397. var rQ = this._sock.get_rQ();
  1398. var rQi = this._sock.get_rQi();
  1399. var cmode, clength, data;
  1400. var handlePalette = function () {
  1401. var numColors = rQ[rQi + 2] + 1;
  1402. var paletteSize = numColors * this._fb_depth;
  1403. this._FBU.bytes += paletteSize;
  1404. if (this._sock.rQwait("TIGHT palette " + cmode, this._FBU.bytes)) { return false; }
  1405. var bpp = (numColors <= 2) ? 1 : 8;
  1406. var rowSize = Math.floor((this._FBU.width * bpp + 7) / 8);
  1407. var raw = false;
  1408. if (rowSize * this._FBU.height < 12) {
  1409. raw = true;
  1410. clength = [0, rowSize * this._FBU.height];
  1411. } else {
  1412. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(3 + paletteSize,
  1413. 3 + paletteSize + 3));
  1414. }
  1415. this._FBU.bytes += clength[0] + clength[1];
  1416. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1417. // Shift ctl, filter id, num colors, palette entries, and clength off
  1418. this._sock.rQskipBytes(3);
  1419. var palette = this._sock.rQshiftBytes(paletteSize);
  1420. this._sock.rQskipBytes(clength[0]);
  1421. if (raw) {
  1422. data = this._sock.rQshiftBytes(clength[1]);
  1423. } else {
  1424. data = decompress(this._sock.rQshiftBytes(clength[1]));
  1425. }
  1426. // Convert indexed (palette based) image data to RGB
  1427. var rgb = indexedToRGB(data, numColors, palette, this._FBU.width, this._FBU.height);
  1428. this._display.renderQ_push({
  1429. 'type': 'blitRgb',
  1430. 'data': rgb,
  1431. 'x': this._FBU.x,
  1432. 'y': this._FBU.y,
  1433. 'width': this._FBU.width,
  1434. 'height': this._FBU.height
  1435. });
  1436. return true;
  1437. }.bind(this);
  1438. var handleCopy = function () {
  1439. var raw = false;
  1440. var uncompressedSize = this._FBU.width * this._FBU.height * this._fb_depth;
  1441. if (uncompressedSize < 12) {
  1442. raw = true;
  1443. clength = [0, uncompressedSize];
  1444. } else {
  1445. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(1, 4));
  1446. }
  1447. this._FBU.bytes = 1 + clength[0] + clength[1];
  1448. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1449. // Shift ctl, clength off
  1450. this._sock.rQshiftBytes(1 + clength[0]);
  1451. if (raw) {
  1452. data = this._sock.rQshiftBytes(clength[1]);
  1453. } else {
  1454. data = decompress(this._sock.rQshiftBytes(clength[1]));
  1455. }
  1456. this._display.renderQ_push({
  1457. 'type': 'blitRgb',
  1458. 'data': data,
  1459. 'x': this._FBU.x,
  1460. 'y': this._FBU.y,
  1461. 'width': this._FBU.width,
  1462. 'height': this._FBU.height
  1463. });
  1464. return true;
  1465. }.bind(this);
  1466. var ctl = this._sock.rQpeek8();
  1467. // Keep tight reset bits
  1468. resetStreams = ctl & 0xF;
  1469. // Figure out filter
  1470. ctl = ctl >> 4;
  1471. streamId = ctl & 0x3;
  1472. if (ctl === 0x08) cmode = "fill";
  1473. else if (ctl === 0x09) cmode = "jpeg";
  1474. else if (ctl === 0x0A) cmode = "png";
  1475. else if (ctl & 0x04) cmode = "filter";
  1476. else if (ctl < 0x04) cmode = "copy";
  1477. else return this._fail("Illegal tight compression received, ctl: " + ctl);
  1478. if (isTightPNG && (cmode === "filter" || cmode === "copy")) {
  1479. return this._fail("filter/copy received in tightPNG mode");
  1480. }
  1481. switch (cmode) {
  1482. // fill use fb_depth because TPIXELs drop the padding byte
  1483. case "fill": // TPIXEL
  1484. this._FBU.bytes += this._fb_depth;
  1485. break;
  1486. case "jpeg": // max clength
  1487. this._FBU.bytes += 3;
  1488. break;
  1489. case "png": // max clength
  1490. this._FBU.bytes += 3;
  1491. break;
  1492. case "filter": // filter id + num colors if palette
  1493. this._FBU.bytes += 2;
  1494. break;
  1495. case "copy":
  1496. break;
  1497. }
  1498. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1499. // Determine FBU.bytes
  1500. switch (cmode) {
  1501. case "fill":
  1502. this._sock.rQskip8(); // shift off ctl
  1503. var color = this._sock.rQshiftBytes(this._fb_depth);
  1504. this._display.renderQ_push({
  1505. 'type': 'fill',
  1506. 'x': this._FBU.x,
  1507. 'y': this._FBU.y,
  1508. 'width': this._FBU.width,
  1509. 'height': this._FBU.height,
  1510. 'color': [color[2], color[1], color[0]]
  1511. });
  1512. break;
  1513. case "png":
  1514. case "jpeg":
  1515. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(1, 4));
  1516. this._FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + jpeg-data
  1517. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1518. // We have everything, render it
  1519. this._sock.rQskipBytes(1 + clength[0]); // shift off clt + compact length
  1520. var img = new Image();
  1521. img.src = "data: image/" + cmode +
  1522. RFB.extract_data_uri(this._sock.rQshiftBytes(clength[1]));
  1523. this._display.renderQ_push({
  1524. 'type': 'img',
  1525. 'img': img,
  1526. 'x': this._FBU.x,
  1527. 'y': this._FBU.y
  1528. });
  1529. img = null;
  1530. break;
  1531. case "filter":
  1532. var filterId = rQ[rQi + 1];
  1533. if (filterId === 1) {
  1534. if (!handlePalette()) { return false; }
  1535. } else {
  1536. // Filter 0, Copy could be valid here, but servers don't send it as an explicit filter
  1537. // Filter 2, Gradient is valid but not use if jpeg is enabled
  1538. // TODO(directxman12): why aren't we just calling '_fail' here
  1539. throw new Error("Unsupported tight subencoding received, filter: " + filterId);
  1540. }
  1541. break;
  1542. case "copy":
  1543. if (!handleCopy()) { return false; }
  1544. break;
  1545. }
  1546. this._FBU.bytes = 0;
  1547. this._FBU.rects--;
  1548. return true;
  1549. },
  1550. TIGHT: function () { return this._encHandlers.display_tight(false); },
  1551. TIGHT_PNG: function () { return this._encHandlers.display_tight(true); },
  1552. last_rect: function () {
  1553. this._FBU.rects = 0;
  1554. return true;
  1555. },
  1556. DesktopSize: function () {
  1557. Util.Debug(">> set_desktopsize");
  1558. this._fb_width = this._FBU.width;
  1559. this._fb_height = this._FBU.height;
  1560. this._onFBResize(this, this._fb_width, this._fb_height);
  1561. this._display.resize(this._fb_width, this._fb_height);
  1562. this._timing.fbu_rt_start = (new Date()).getTime();
  1563. this._FBU.bytes = 0;
  1564. this._FBU.rects--;
  1565. Util.Debug("<< set_desktopsize");
  1566. return true;
  1567. },
  1568. Cursor: function () {
  1569. Util.Debug(">> set_cursor");
  1570. var x = this._FBU.x; // hotspot-x
  1571. var y = this._FBU.y; // hotspot-y
  1572. var w = this._FBU.width;
  1573. var h = this._FBU.height;
  1574. var pixelslength = w * h * this._fb_Bpp;
  1575. var masklength = Math.floor((w + 7) / 8) * h;
  1576. this._FBU.bytes = pixelslength + masklength;
  1577. if (this._sock.rQwait("cursor encoding", this._FBU.bytes)) { return false; }
  1578. this._display.changeCursor(this._sock.rQshiftBytes(pixelslength),
  1579. this._sock.rQshiftBytes(masklength),
  1580. x, y, w, h);
  1581. this._FBU.bytes = 0;
  1582. this._FBU.rects--;
  1583. Util.Debug("<< set_cursor");
  1584. return true;
  1585. },
  1586. JPEG_quality_lo: function () {
  1587. Util.Error("Server sent jpeg_quality pseudo-encoding");
  1588. },
  1589. compress_lo: function () {
  1590. Util.Error("Server sent compress level pseudo-encoding");
  1591. }
  1592. };
  1593. })();