vnc.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2010 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.LGPL-3)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. "use strict";
  9. /*jslint white: false, nomen: false, browser: true, bitwise: false */
  10. /*global window, WebSocket, Util, Canvas, VNC_uri_prefix, Base64, DES */
  11. // Globals defined here
  12. var VNC_native_ws, RFB;
  13. /*
  14. * Load supporting scripts
  15. */
  16. function get_VNC_uri_prefix() {
  17. return (typeof VNC_uri_prefix !== "undefined") ? VNC_uri_prefix : "include/";
  18. }
  19. (function () {
  20. var extra = "", start, end;
  21. start = "<script src='" + get_VNC_uri_prefix();
  22. end = "'><\/script>";
  23. // Uncomment to activate firebug lite
  24. //extra += "<script src='http://getfirebug.com/releases/lite/1.2/" +
  25. // "firebug-lite-compressed.js'><\/script>";
  26. extra += start + "util.js" + end;
  27. extra += start + "base64.js" + end;
  28. extra += start + "des.js" + end;
  29. extra += start + "canvas.js" + end;
  30. /* If no builtin websockets then load web_socket.js */
  31. if (window.WebSocket) {
  32. VNC_native_ws = true;
  33. } else {
  34. VNC_native_ws = false;
  35. WebSocket__swfLocation = get_VNC_uri_prefix() +
  36. "web-socket-js/WebSocketMain.swf";
  37. extra += start + "web-socket-js/swfobject.js" + end;
  38. extra += start + "web-socket-js/FABridge.js" + end;
  39. extra += start + "web-socket-js/web_socket.js" + end;
  40. }
  41. document.write(extra);
  42. }());
  43. /*
  44. * RFB namespace
  45. */
  46. RFB = {
  47. /*
  48. * External interface variables and methods
  49. */
  50. host : '',
  51. port : 5900,
  52. password : '',
  53. encrypt : true,
  54. true_color : false,
  55. b64encode : true, // false means UTF-8 on the wire
  56. //b64encode : false, // false means UTF-8 on the wire
  57. connectTimeout : 2000, // time to wait for connection
  58. // In preference order
  59. encodings : [
  60. ['COPYRECT', 0x01, 'display_copy_rect'],
  61. ['TIGHT_PNG', -260, 'display_tight_png'],
  62. ['HEXTILE', 0x05, 'display_hextile'],
  63. ['RRE', 0x02, 'display_rre'],
  64. ['RAW', 0x00, 'display_raw'],
  65. ['DesktopSize', -223, 'set_desktopsize'],
  66. // Psuedo-encoding settings
  67. ['JPEG_quality_lo', -32, 'set_jpeg_quality'],
  68. // ['JPEG_quality_hi', -23, 'set_jpeg_quality'],
  69. ['compress_lo', -255, 'set_compress_level']
  70. // ['compress_hi', -247, 'set_compress_level']
  71. ],
  72. setUpdateState: function(externalUpdateState) {
  73. RFB.externalUpdateState = externalUpdateState;
  74. },
  75. setClipboardReceive: function(clipReceive) {
  76. RFB.clipboardCopyTo = clipReceive;
  77. },
  78. setCanvasID: function(canvasID) {
  79. RFB.canvasID = canvasID;
  80. },
  81. sendPassword: function(passwd) {
  82. RFB.password = passwd;
  83. RFB.state = "Authentication";
  84. setTimeout(RFB.init_msg, 1);
  85. },
  86. sendCtrlAltDel: function() {
  87. if (RFB.state !== "normal") { return false; }
  88. Util.Info("Sending Ctrl-Alt-Del");
  89. var arr = [];
  90. arr = arr.concat(RFB.keyEvent(0xFFE3, 1)); // Control
  91. arr = arr.concat(RFB.keyEvent(0xFFE9, 1)); // Alt
  92. arr = arr.concat(RFB.keyEvent(0xFFFF, 1)); // Delete
  93. arr = arr.concat(RFB.keyEvent(0xFFFF, 0)); // Delete
  94. arr = arr.concat(RFB.keyEvent(0xFFE9, 0)); // Alt
  95. arr = arr.concat(RFB.keyEvent(0xFFE3, 0)); // Control
  96. arr = arr.concat(RFB.fbUpdateRequest(1));
  97. RFB.send_array(arr);
  98. },
  99. load: function () {
  100. var i;
  101. //Util.Debug(">> load");
  102. /* Load web-socket-js if no builtin WebSocket support */
  103. if (VNC_native_ws) {
  104. Util.Info("Using native WebSockets");
  105. RFB.updateState('disconnected', 'Disconnected');
  106. } else {
  107. Util.Warn("Using web-socket-js flash bridge");
  108. if ((! Util.Flash) ||
  109. (Util.Flash.version < 9)) {
  110. RFB.updateState('failed', "WebSockets or Adobe Flash is required");
  111. } else if (document.location.href.substr(0, 7) === "file://") {
  112. RFB.updateState('failed',
  113. "'file://' URL is incompatible with Adobe Flash");
  114. } else {
  115. RFB.updateState('disconnected', 'Disconnected');
  116. }
  117. }
  118. // Initialize canvas/fxcanvas
  119. Canvas.init(RFB.canvasID);
  120. // Populate encoding lookup tables
  121. RFB.encHandlers = {};
  122. RFB.encNames = {};
  123. for (i=0; i < RFB.encodings.length; i+=1) {
  124. RFB.encHandlers[RFB.encodings[i][1]] = RFB[RFB.encodings[i][2]];
  125. RFB.encNames[RFB.encodings[i][1]] = RFB.encodings[i][0];
  126. }
  127. //Util.Debug("<< load");
  128. },
  129. connect: function (host, port, password, encrypt, true_color) {
  130. //Util.Debug(">> connect");
  131. RFB.host = host;
  132. RFB.port = port;
  133. RFB.password = (password !== undefined) ? password : "";
  134. RFB.encrypt = (encrypt !== undefined) ? encrypt : true;
  135. if ((RFB.encrypt === "0") ||
  136. (RFB.encrypt === "no") ||
  137. (RFB.encrypt === "false")) {
  138. RFB.encrypt = false;
  139. }
  140. RFB.true_color = (true_color !== undefined) ? true_color: true;
  141. if ((RFB.true_color === "0") ||
  142. (RFB.true_color === "no") ||
  143. (RFB.true_color === "false")) {
  144. RFB.true_color = false;
  145. }
  146. if ((!RFB.host) || (!RFB.port)) {
  147. RFB.updateState('disconnected', "Must set host and port");
  148. return;
  149. }
  150. RFB.init_vars();
  151. if ((RFB.ws) && (RFB.ws.readyState === WebSocket.OPEN)) {
  152. RFB.ws.close();
  153. }
  154. RFB.init_ws();
  155. RFB.updateState('ProtocolVersion');
  156. //Util.Debug("<< connect");
  157. },
  158. disconnect: function () {
  159. //Util.Debug(">> disconnect");
  160. if ((RFB.ws) && (RFB.ws.readyState === WebSocket.OPEN)) {
  161. RFB.ws.close();
  162. RFB.updateState('closed');
  163. RFB.ws.onmessage = function (e) { return; };
  164. }
  165. if (Canvas.ctx) {
  166. Canvas.stop();
  167. if (! /__debug__$/i.test(document.location.href)) {
  168. Canvas.clear();
  169. }
  170. }
  171. RFB.updateState('disconnected', 'Disconnected');
  172. //Util.Debug("<< disconnect");
  173. },
  174. clipboardPasteFrom: function (text) {
  175. if (RFB.state !== "normal") { return; }
  176. //Util.Debug(">> clipboardPasteFrom: " + text.substr(0,40) + "...");
  177. RFB.send_array(RFB.clientCutText(text));
  178. //Util.Debug("<< clipboardPasteFrom");
  179. },
  180. /*
  181. * Private variables and methods
  182. */
  183. ws : null, // Web Socket object
  184. sendID : null,
  185. scanID : null, // TIGHT_PNG render image scanner
  186. // Receive and send queues
  187. RQ : [], // Receive Queue
  188. SQ : "", // Send Queue
  189. encHandlers : {},
  190. encNames : {},
  191. // Frame buffer update state
  192. FBU : {
  193. rects : 0,
  194. subrects : 0, // RRE and HEXTILE
  195. lines : 0, // RAW
  196. tiles : 0, // HEXTILE
  197. bytes : 0,
  198. x : 0,
  199. y : 0,
  200. width : 0,
  201. height : 0,
  202. encoding : 0,
  203. subencoding : -1,
  204. background : null,
  205. imgs : [] // TIGHT_PNG image queue
  206. },
  207. fb_Bpp : 4,
  208. fb_depth : 3,
  209. max_version : 3.8,
  210. version : 0,
  211. auth_scheme : '',
  212. state : 'disconnected',
  213. cuttext : 'none', // ServerCutText wait state
  214. ct_length : 0,
  215. shared : 1,
  216. check_rate : 217,
  217. scan_imgs_rate : 100,
  218. req_rate : 1413,
  219. last_req : 0,
  220. canvasID : 'VNC_canvas',
  221. fb_width : 0,
  222. fb_height : 0,
  223. fb_name : "",
  224. rre_chunk : 100,
  225. timing : {
  226. last_fbu : 0,
  227. fbu_total : 0,
  228. fbu_total_cnt : 0,
  229. full_fbu_total : 0,
  230. full_fbu_cnt : 0,
  231. fbu_rt_start : 0,
  232. fbu_rt_total : 0,
  233. fbu_rt_cnt : 0,
  234. history : [],
  235. history_start : 0,
  236. h_time : 0,
  237. h_rects : 0,
  238. h_fbus : 0,
  239. h_bytes : 0,
  240. h_pixels : 0
  241. },
  242. /* Mouse state */
  243. mouse_buttonmask : 0,
  244. mouse_arr : [],
  245. /*
  246. * Server message handlers
  247. */
  248. /* RFB/VNC initialisation */
  249. init_msg: function () {
  250. //Util.Debug(">> init_msg [RFB.state '" + RFB.state + "']");
  251. var RQ = RFB.RQ, strlen, reason, reason_len, sversion, cversion,
  252. i, types, num_types, challenge, response, bpp, depth,
  253. big_endian, true_color, name_length;
  254. //Util.Debug("RQ (" + RQ.length + ") " + RQ);
  255. switch (RFB.state) {
  256. case 'ProtocolVersion' :
  257. if (RQ.length < 12) {
  258. RFB.updateState('failed',
  259. "Disconnected: incomplete protocol version");
  260. return;
  261. }
  262. sversion = RQ.shiftStr(12).substr(4,7);
  263. Util.Info("Server ProtocolVersion: " + sversion);
  264. switch (sversion) {
  265. case "003.003": RFB.version = 3.3; break;
  266. case "003.007": RFB.version = 3.7; break;
  267. case "003.008": RFB.version = 3.8; break;
  268. default:
  269. RFB.updateState('failed',
  270. "Invalid server version " + sversion);
  271. return;
  272. }
  273. if (RFB.version > RFB.max_version) {
  274. RFB.version = RFB.max_version;
  275. }
  276. cversion = "00" + parseInt(RFB.version,10) +
  277. ".00" + ((RFB.version * 10) % 10);
  278. RFB.send_string("RFB " + cversion + "\n");
  279. RFB.updateState('Security', "Sent ProtocolVersion: " + sversion);
  280. break;
  281. case 'Security' :
  282. if (RFB.version >= 3.7) {
  283. num_types = RQ.shift8();
  284. if (num_types === 0) {
  285. strlen = RQ.shift32();
  286. reason = RQ.shiftStr(strlen);
  287. RFB.updateState('failed',
  288. "Disconnected: security failure: " + reason);
  289. return;
  290. }
  291. RFB.auth_scheme = 0;
  292. types = RQ.shiftBytes(num_types);
  293. for (i=0; i < types.length; i+=1) {
  294. if ((types[i] > RFB.auth_scheme) && (types[i] < 3)) {
  295. RFB.auth_scheme = types[i];
  296. }
  297. }
  298. if (RFB.auth_scheme === 0) {
  299. RFB.updateState('failed',
  300. "Disconnected: unsupported security types: " + types);
  301. return;
  302. }
  303. RFB.send_array([RFB.auth_scheme]);
  304. } else {
  305. if (RQ.length < 4) {
  306. RFB.updateState('failed', "Invalid security frame");
  307. return;
  308. }
  309. RFB.auth_scheme = RQ.shift32();
  310. }
  311. RFB.updateState('Authentication',
  312. "Authenticating using scheme: " + RFB.auth_scheme);
  313. // Fall through
  314. case 'Authentication' :
  315. //Util.Debug("Security auth scheme: " + RFB.auth_scheme);
  316. switch (RFB.auth_scheme) {
  317. case 0: // connection failed
  318. if (RQ.length < 4) {
  319. //Util.Debug(" waiting for auth reason bytes");
  320. return;
  321. }
  322. strlen = RQ.shift32();
  323. reason = RQ.shiftStr(strlen);
  324. RFB.updateState('failed',
  325. "Disconnected: auth failure: " + reason);
  326. return;
  327. case 1: // no authentication
  328. // RFB.send_array([RFB.shared]); // ClientInitialisation
  329. RFB.updateState('SecurityResult');
  330. break;
  331. case 2: // VNC authentication
  332. if (RFB.password.length === 0) {
  333. RFB.updateState('password', "Password Required");
  334. return;
  335. }
  336. if (RQ.length < 16) {
  337. //Util.Debug(" waiting for auth challenge bytes");
  338. return;
  339. }
  340. challenge = RQ.shiftBytes(16);
  341. //Util.Debug("Password: " + RFB.password);
  342. //Util.Debug("Challenge: " + challenge +
  343. // " (" + challenge.length + ")");
  344. response = RFB.DES(RFB.password, challenge);
  345. //Util.Debug("Response: " + response +
  346. // " (" + response.length + ")");
  347. //Util.Debug("Sending DES encrypted auth response");
  348. RFB.send_array(response);
  349. RFB.updateState('SecurityResult');
  350. break;
  351. default:
  352. RFB.updateState('failed',
  353. "Disconnected: unsupported auth scheme: " +
  354. RFB.auth_scheme);
  355. return;
  356. }
  357. break;
  358. case 'SecurityResult' :
  359. if (RQ.length < 4) {
  360. RFB.updateState('failed', "Invalid VNC auth response");
  361. return;
  362. }
  363. switch (RQ.shift32()) {
  364. case 0: // OK
  365. RFB.updateState('ServerInitialisation', "Authentication OK");
  366. break;
  367. case 1: // failed
  368. if (RFB.version >= 3.8) {
  369. reason_len = RQ.shift32();
  370. reason = RQ.shiftStr(reason_len);
  371. RFB.updateState('failed', reason);
  372. } else {
  373. RFB.updateState('failed', "Authentication failed");
  374. }
  375. return;
  376. case 2: // too-many
  377. RFB.updateState('failed',
  378. "Disconnected: too many auth attempts");
  379. return;
  380. }
  381. RFB.send_array([RFB.shared]); // ClientInitialisation
  382. break;
  383. case 'ServerInitialisation' :
  384. if (RQ.length < 24) {
  385. RFB.updateState('failed', "Invalid server initialisation");
  386. return;
  387. }
  388. /* Screen size */
  389. RFB.fb_width = RQ.shift16();
  390. RFB.fb_height = RQ.shift16();
  391. /* PIXEL_FORMAT */
  392. bpp = RQ.shift8();
  393. depth = RQ.shift8();
  394. big_endian = RQ.shift8();
  395. true_color = RQ.shift8();
  396. Util.Info("Screen: " + RFB.fb_width + "x" + RFB.fb_height +
  397. ", bpp: " + bpp + ", depth: " + depth +
  398. ", big_endian: " + big_endian +
  399. ", true_color: " + true_color);
  400. /* Connection name/title */
  401. RQ.shiftStr(12);
  402. name_length = RQ.shift32();
  403. RFB.fb_name = RQ.shiftStr(name_length);
  404. Canvas.resize(RFB.fb_width, RFB.fb_height, RFB.true_color);
  405. Canvas.start(RFB.keyPress, RFB.mouseButton, RFB.mouseMove);
  406. if (RFB.true_color) {
  407. RFB.fb_Bpp = 4;
  408. RFB.fb_depth = 3;
  409. } else {
  410. RFB.fb_Bpp = 1;
  411. RFB.fb_depth = 1;
  412. }
  413. response = RFB.pixelFormat();
  414. response = response.concat(RFB.clientEncodings());
  415. response = response.concat(RFB.fbUpdateRequest(0));
  416. RFB.timing.fbu_rt_start = (new Date()).getTime();
  417. RFB.send_array(response);
  418. /* Start pushing/polling */
  419. setTimeout(RFB.checkEvents, RFB.check_rate);
  420. setTimeout(RFB.scan_tight_imgs, RFB.scan_imgs_rate);
  421. RFB.timing.history_start = (new Date()).getTime();
  422. setTimeout(RFB.update_timings, 1000);
  423. RFB.updateState('normal', "Connected to: " + RFB.fb_name);
  424. break;
  425. }
  426. //Util.Debug("<< init_msg");
  427. },
  428. /* Normal RFB/VNC server messages */
  429. normal_msg: function () {
  430. //Util.Debug(">> normal_msg");
  431. var RQ = RFB.RQ, ret = true, msg_type,
  432. c, first_colour, num_colours, red, green, blue;
  433. //Util.Debug(">> msg RQ.slice(0,10): " + RQ.slice(0,20));
  434. //Util.Debug(">> msg RQ.slice(-10,-1): " + RQ.slice(RQ.length-10,RQ.length));
  435. if (RFB.FBU.rects > 0) {
  436. msg_type = 0;
  437. } else if (RFB.cuttext !== 'none') {
  438. msg_type = 3;
  439. } else {
  440. msg_type = RQ.shift8();
  441. }
  442. switch (msg_type) {
  443. case 0: // FramebufferUpdate
  444. ret = RFB.framebufferUpdate();
  445. break;
  446. case 1: // SetColourMapEntries
  447. Util.Debug("SetColourMapEntries");
  448. RQ.shift8(); // Padding
  449. first_colour = RQ.shift16(); // First colour
  450. num_colours = RQ.shift16();
  451. for (c=0; c < num_colours; c+=1) {
  452. red = RQ.shift16();
  453. //Util.Debug("red before: " + red);
  454. red = parseInt(red / 256, 10);
  455. //Util.Debug("red after: " + red);
  456. green = parseInt(RQ.shift16() / 256, 10);
  457. blue = parseInt(RQ.shift16() / 256, 10);
  458. Canvas.colourMap[first_colour + c] = [red, green, blue];
  459. }
  460. Util.Info("Registered " + num_colours + " colourMap entries");
  461. //Util.Debug("colourMap: " + Canvas.colourMap);
  462. break;
  463. case 2: // Bell
  464. Util.Warn("Bell (unsupported)");
  465. break;
  466. case 3: // ServerCutText
  467. Util.Debug("ServerCutText");
  468. Util.Debug("RQ:" + RQ.slice(0,20));
  469. if (RFB.cuttext === 'none') {
  470. RFB.cuttext = 'header';
  471. }
  472. if (RFB.cuttext === 'header') {
  473. if (RQ.length < 7) {
  474. //Util.Debug("waiting for ServerCutText header");
  475. return false;
  476. }
  477. RQ.shiftBytes(3); // Padding
  478. RFB.ct_length = RQ.shift32();
  479. }
  480. RFB.cuttext = 'bytes';
  481. if (RQ.length < RFB.ct_length) {
  482. //Util.Debug("waiting for ServerCutText bytes");
  483. return false;
  484. }
  485. RFB.clipboardCopyTo(RQ.shiftStr(RFB.ct_length));
  486. RFB.cuttext = 'none';
  487. break;
  488. default:
  489. RFB.updateState('failed',
  490. "Disconnected: illegal server message type " + msg_type);
  491. Util.Debug("RQ.slice(0,30):" + RQ.slice(0,30));
  492. break;
  493. }
  494. //Util.Debug("<< normal_msg");
  495. return ret;
  496. },
  497. framebufferUpdate: function() {
  498. var RQ = RFB.RQ, FBU = RFB.FBU, timing = RFB.timing,
  499. now, fbu_rt_diff, last_bytes, last_rects,
  500. ret = true, msg;
  501. if (FBU.rects === 0) {
  502. //Util.Debug("New FBU: RQ.slice(0,20): " + RQ.slice(0,20));
  503. if (RQ.length < 3) {
  504. RQ.unshift(0); // FBU msg_type
  505. Util.Debug(" waiting for FBU header bytes");
  506. return false;
  507. }
  508. RQ.shift8();
  509. FBU.rects = RQ.shift16();
  510. //Util.Debug("FramebufferUpdate, rects:" + FBU.rects);
  511. FBU.bytes = 0;
  512. timing.cur_fbu = 0;
  513. timing.h_fbus += 1;
  514. if (timing.fbu_rt_start > 0) {
  515. now = (new Date()).getTime();
  516. Util.Info("First FBU latency: " + (now - timing.fbu_rt_start));
  517. }
  518. }
  519. while ((FBU.rects > 0) && (RQ.length >= FBU.bytes)) {
  520. if (FBU.bytes === 0) {
  521. if (RQ.length < 12) {
  522. //Util.Debug(" waiting for rect header bytes");
  523. return false;
  524. }
  525. /* New FramebufferUpdate */
  526. FBU.x = RQ.shift16();
  527. FBU.y = RQ.shift16();
  528. FBU.width = RQ.shift16();
  529. FBU.height = RQ.shift16();
  530. FBU.encoding = parseInt(RQ.shift32(), 10);
  531. timing.h_bytes += 12;
  532. if (RFB.encNames[FBU.encoding]) {
  533. // Debug:
  534. /*
  535. msg = "FramebufferUpdate rects:" + FBU.rects;
  536. msg += " x: " + FBU.x + " y: " + FBU.y
  537. msg += " width: " + FBU.width + " height: " + FBU.height;
  538. msg += " encoding:" + FBU.encoding;
  539. msg += "(" + RFB.encNames[FBU.encoding] + ")";
  540. msg += ", RQ.length: " + RQ.length;
  541. Util.Debug(msg);
  542. */
  543. } else {
  544. RFB.updateState('failed',
  545. "Disconnected: unsupported encoding " +
  546. FBU.encoding);
  547. return false;
  548. }
  549. }
  550. timing.last_fbu = (new Date()).getTime();
  551. last_bytes = RQ.length;
  552. last_rects = FBU.rects;
  553. ret = RFB.encHandlers[FBU.encoding]();
  554. now = (new Date()).getTime();
  555. timing.cur_fbu += (now - timing.last_fbu);
  556. timing.h_bytes += last_bytes-RQ.length;
  557. if (FBU.rects < last_rects) {
  558. // Some work was done
  559. timing.h_rects += last_rects-FBU.rects;
  560. timing.h_pixels += FBU.width*FBU.height;
  561. }
  562. if (FBU.rects === 0) {
  563. if (((FBU.width === RFB.fb_width) &&
  564. (FBU.height === RFB.fb_height)) ||
  565. (timing.fbu_rt_start > 0)) {
  566. timing.full_fbu_total += timing.cur_fbu;
  567. timing.full_fbu_cnt += 1;
  568. Util.Info("Timing of full FBU, cur: " +
  569. timing.cur_fbu + ", total: " +
  570. timing.full_fbu_total + ", cnt: " +
  571. timing.full_fbu_cnt + ", avg: " +
  572. (timing.full_fbu_total /
  573. timing.full_fbu_cnt));
  574. }
  575. if (timing.fbu_rt_start > 0) {
  576. fbu_rt_diff = now - timing.fbu_rt_start;
  577. timing.fbu_rt_total += fbu_rt_diff;
  578. timing.fbu_rt_cnt += 1;
  579. Util.Info("full FBU round-trip, cur: " +
  580. fbu_rt_diff + ", total: " +
  581. timing.fbu_rt_total + ", cnt: " +
  582. timing.fbu_rt_cnt + ", avg: " +
  583. (timing.fbu_rt_total /
  584. timing.fbu_rt_cnt));
  585. timing.fbu_rt_start = 0;
  586. }
  587. }
  588. if (RFB.state !== "normal") { return true; }
  589. }
  590. return ret;
  591. },
  592. /*
  593. * FramebufferUpdate encodings
  594. */
  595. display_raw: function () {
  596. //Util.Debug(">> display_raw");
  597. var RQ = RFB.RQ, FBU = RFB.FBU, cur_y, cur_height;
  598. if (FBU.lines === 0) {
  599. FBU.lines = FBU.height;
  600. }
  601. FBU.bytes = FBU.width * RFB.fb_Bpp; // At least a line
  602. if (RQ.length < FBU.bytes) {
  603. //Util.Debug(" waiting for " +
  604. // (FBU.bytes - RQ.length) + " RAW bytes");
  605. return;
  606. }
  607. cur_y = FBU.y + (FBU.height - FBU.lines);
  608. cur_height = Math.min(FBU.lines,
  609. Math.floor(RQ.length/(FBU.width * RFB.fb_Bpp)));
  610. Canvas.blitImage(FBU.x, cur_y, FBU.width, cur_height, RQ, 0);
  611. RQ.shiftBytes(FBU.width * cur_height * RFB.fb_Bpp);
  612. FBU.lines -= cur_height;
  613. if (FBU.lines > 0) {
  614. FBU.bytes = FBU.width * RFB.fb_Bpp; // At least another line
  615. } else {
  616. FBU.rects -= 1;
  617. FBU.bytes = 0;
  618. }
  619. },
  620. display_copy_rect: function () {
  621. //Util.Debug(">> display_copy_rect");
  622. var RQ = RFB.RQ, FBU = RFB.FBU, old_x, old_y;
  623. if (RQ.length < 4) {
  624. //Util.Debug(" waiting for " +
  625. // (FBU.bytes - RQ.length) + " COPYRECT bytes");
  626. return;
  627. }
  628. old_x = RQ.shift16();
  629. old_y = RQ.shift16();
  630. Canvas.copyImage(old_x, old_y, FBU.x, FBU.y, FBU.width, FBU.height);
  631. FBU.rects -= 1;
  632. FBU.bytes = 0;
  633. },
  634. display_rre: function () {
  635. //Util.Debug(">> display_rre (" + RFB.RQ.length + " bytes)");
  636. var RQ = RFB.RQ, FBU = RFB.FBU, color, x, y, width, height, chunk;
  637. if (FBU.subrects === 0) {
  638. if (RQ.length < 4 + RFB.fb_Bpp) {
  639. //Util.Debug(" waiting for " +
  640. // (4 + RFB.fb_Bpp - RQ.length) + " RRE bytes");
  641. return;
  642. }
  643. FBU.subrects = RQ.shift32();
  644. color = RQ.shiftBytes(RFB.fb_Bpp); // Background
  645. Canvas.fillRect(FBU.x, FBU.y, FBU.width, FBU.height, color);
  646. }
  647. while ((FBU.subrects > 0) && (RQ.length >= (RFB.fb_Bpp + 8))) {
  648. color = RQ.shiftBytes(RFB.fb_Bpp);
  649. x = RQ.shift16();
  650. y = RQ.shift16();
  651. width = RQ.shift16();
  652. height = RQ.shift16();
  653. Canvas.fillRect(FBU.x + x, FBU.y + y, width, height, color);
  654. FBU.subrects -= 1;
  655. }
  656. //Util.Debug(" display_rre: rects: " + FBU.rects +
  657. // ", FBU.subrects: " + FBU.subrects);
  658. if (FBU.subrects > 0) {
  659. chunk = Math.min(RFB.rre_chunk, FBU.subrects);
  660. FBU.bytes = (RFB.fb_Bpp + 8) * chunk;
  661. } else {
  662. FBU.rects -= 1;
  663. FBU.bytes = 0;
  664. }
  665. //Util.Debug("<< display_rre, FBU.bytes: " + FBU.bytes);
  666. },
  667. display_hextile: function() {
  668. //Util.Debug(">> display_hextile");
  669. var RQ = RFB.RQ, FBU = RFB.FBU,
  670. subencoding, subrects, idx, tile, color, cur_tile,
  671. tile_x, x, w, tile_y, y, h, xy, s, sx, sy, wh, sw, sh;
  672. if (FBU.tiles === 0) {
  673. FBU.tiles_x = Math.ceil(FBU.width/16);
  674. FBU.tiles_y = Math.ceil(FBU.height/16);
  675. FBU.total_tiles = FBU.tiles_x * FBU.tiles_y;
  676. FBU.tiles = FBU.total_tiles;
  677. }
  678. /* FBU.bytes comes in as 1, RQ.length at least 1 */
  679. while (FBU.tiles > 0) {
  680. FBU.bytes = 1;
  681. if (RQ.length < FBU.bytes) {
  682. //Util.Debug(" waiting for HEXTILE subencoding byte");
  683. return;
  684. }
  685. subencoding = RQ[0]; // Peek
  686. if (subencoding > 30) { // Raw
  687. RFB.updateState('failed',
  688. "Disconnected: illegal hextile subencoding " + subencoding);
  689. Util.Debug("RQ.slice(0,30):" + RQ.slice(0,30));
  690. return;
  691. }
  692. subrects = 0;
  693. cur_tile = FBU.total_tiles - FBU.tiles;
  694. tile_x = cur_tile % FBU.tiles_x;
  695. tile_y = Math.floor(cur_tile / FBU.tiles_x);
  696. x = FBU.x + tile_x * 16;
  697. y = FBU.y + tile_y * 16;
  698. w = Math.min(16, (FBU.x + FBU.width) - x);
  699. h = Math.min(16, (FBU.y + FBU.height) - y);
  700. /* Figure out how much we are expecting */
  701. if (subencoding & 0x01) { // Raw
  702. //Util.Debug(" Raw subencoding");
  703. FBU.bytes += w * h * RFB.fb_Bpp;
  704. } else {
  705. if (subencoding & 0x02) { // Background
  706. FBU.bytes += RFB.fb_Bpp;
  707. }
  708. if (subencoding & 0x04) { // Foreground
  709. FBU.bytes += RFB.fb_Bpp;
  710. }
  711. if (subencoding & 0x08) { // AnySubrects
  712. FBU.bytes += 1; // Since we aren't shifting it off
  713. if (RQ.length < FBU.bytes) {
  714. /* Wait for subrects byte */
  715. //Util.Debug(" waiting for hextile subrects header byte");
  716. return;
  717. }
  718. subrects = RQ[FBU.bytes-1]; // Peek
  719. if (subencoding & 0x10) { // SubrectsColoured
  720. FBU.bytes += subrects * (RFB.fb_Bpp + 2);
  721. } else {
  722. FBU.bytes += subrects * 2;
  723. }
  724. }
  725. }
  726. //Util.Debug(" tile:" + cur_tile + "/" + (FBU.total_tiles - 1) +
  727. // ", subencoding:" + subencoding +
  728. // "(last: " + FBU.lastsubencoding + "), subrects:" +
  729. // subrects + ", tile:" + tile_x + "," + tile_y +
  730. // " [" + x + "," + y + "]@" + w + "x" + h +
  731. // ", d.length:" + RQ.length + ", bytes:" + FBU.bytes +
  732. // " last:" + RQ.slice(FBU.bytes-10, FBU.bytes) +
  733. // " next:" + RQ.slice(FBU.bytes-1, FBU.bytes+10));
  734. if (RQ.length < FBU.bytes) {
  735. //Util.Debug(" waiting for " +
  736. // (FBU.bytes - RQ.length) + " hextile bytes");
  737. return;
  738. }
  739. /* We know the encoding and have a whole tile */
  740. FBU.subencoding = RQ[0];
  741. idx = 1;
  742. if (FBU.subencoding === 0) {
  743. if (FBU.lastsubencoding & 0x01) {
  744. /* Weird: ignore blanks after RAW */
  745. Util.Debug(" Ignoring blank after RAW");
  746. } else {
  747. Canvas.fillRect(x, y, w, h, FBU.background);
  748. }
  749. } else if (FBU.subencoding & 0x01) { // Raw
  750. Canvas.blitImage(x, y, w, h, RQ, idx);
  751. } else {
  752. if (FBU.subencoding & 0x02) { // Background
  753. FBU.background = RQ.slice(idx, idx + RFB.fb_Bpp);
  754. idx += RFB.fb_Bpp;
  755. }
  756. if (FBU.subencoding & 0x04) { // Foreground
  757. FBU.foreground = RQ.slice(idx, idx + RFB.fb_Bpp);
  758. idx += RFB.fb_Bpp;
  759. }
  760. tile = Canvas.getTile(x, y, w, h, FBU.background);
  761. if (FBU.subencoding & 0x08) { // AnySubrects
  762. subrects = RQ[idx];
  763. idx += 1;
  764. for (s = 0; s < subrects; s += 1) {
  765. if (FBU.subencoding & 0x10) { // SubrectsColoured
  766. color = RQ.slice(idx, idx + RFB.fb_Bpp);
  767. idx += RFB.fb_Bpp;
  768. } else {
  769. color = FBU.foreground;
  770. }
  771. xy = RQ[idx];
  772. idx += 1;
  773. sx = (xy >> 4);
  774. sy = (xy & 0x0f);
  775. wh = RQ[idx];
  776. idx += 1;
  777. sw = (wh >> 4) + 1;
  778. sh = (wh & 0x0f) + 1;
  779. Canvas.setSubTile(tile, sx, sy, sw, sh, color);
  780. }
  781. }
  782. Canvas.putTile(tile);
  783. }
  784. RQ.shiftBytes(FBU.bytes);
  785. FBU.lastsubencoding = FBU.subencoding;
  786. FBU.bytes = 0;
  787. FBU.tiles -= 1;
  788. }
  789. if (FBU.tiles === 0) {
  790. FBU.rects -= 1;
  791. }
  792. //Util.Debug("<< display_hextile");
  793. },
  794. display_tight_png: function() {
  795. //Util.Debug(">> display_tight_png");
  796. var RQ = RFB.RQ, FBU = RFB.FBU,
  797. ctl, cmode, clength, getCLength, color, img;
  798. //Util.Debug(" FBU.rects: " + FBU.rects);
  799. //Util.Debug(" RQ.length: " + RQ.length);
  800. //Util.Debug(" RQ.slice(0,20): " + RQ.slice(0,20));
  801. FBU.bytes = 1; // compression-control byte
  802. if (RQ.length < FBU.bytes) {
  803. Util.Debug(" waiting for TIGHT compression-control byte");
  804. return;
  805. }
  806. // Get 'compact length' header and data size
  807. getCLength = function (arr, offset) {
  808. var header = 1, data = 0;
  809. data += arr[offset + 0] & 0x7f;
  810. if (arr[offset + 0] & 0x80) {
  811. header += 1;
  812. data += (arr[offset + 1] & 0x7f) << 7;
  813. if (arr[offset + 1] & 0x80) {
  814. header += 1;
  815. data += arr[offset + 2] << 14;
  816. }
  817. }
  818. return [header, data];
  819. };
  820. ctl = RQ[0];
  821. switch (ctl >> 4) {
  822. case 0x08: cmode = "fill"; break;
  823. case 0x09: cmode = "jpeg"; break;
  824. case 0x0A: cmode = "png"; break;
  825. default: throw("Illegal basic compression received, ctl: " + ctl);
  826. }
  827. switch (cmode) {
  828. // fill uses fb_depth because TPIXELs drop the padding byte
  829. case "fill": FBU.bytes += RFB.fb_depth; break; // TPIXEL
  830. case "jpeg": FBU.bytes += 3; break; // max clength
  831. case "png": FBU.bytes += 3; break; // max clength
  832. }
  833. if (RQ.length < FBU.bytes) {
  834. Util.Debug(" waiting for TIGHT " + cmode + " bytes");
  835. return;
  836. }
  837. //Util.Debug(" RQ.slice(0,20): " + RFB.RQ.slice(0,20) + " (" + RFB.RQ.length + ")");
  838. //Util.Debug(" cmode: " + cmode);
  839. // Determine FBU.bytes
  840. switch (cmode) {
  841. case "fill":
  842. RQ.shift8(); // shift off ctl
  843. color = RQ.shiftBytes(RFB.fb_depth);
  844. Canvas.fillRect(FBU.x, FBU.y, FBU.width, FBU.height, color);
  845. break;
  846. case "jpeg":
  847. case "png":
  848. clength = getCLength(RQ, 1);
  849. FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + jpeg-data
  850. if (RQ.length < FBU.bytes) {
  851. Util.Debug(" waiting for TIGHT " + cmode + " bytes");
  852. return;
  853. }
  854. // We have everything, render it
  855. //Util.Debug(" png, RQ.length: " + RQ.length + ", clength[0]: " + clength[0] + ", clength[1]: " + clength[1]);
  856. RQ.shiftBytes(1 + clength[0]); // shift off ctl + compact length
  857. img = new Image();
  858. img.onload = RFB.scan_tight_imgs;
  859. FBU.imgs.push([img, FBU.x, FBU.y]);
  860. img.src = "data:image/" + cmode +
  861. RFB.extract_data_uri(RQ.shiftBytes(clength[1]));
  862. img = null;
  863. break;
  864. }
  865. FBU.bytes = 0;
  866. FBU.rects -= 1;
  867. //Util.Debug(" ending RQ.length: " + RQ.length);
  868. //Util.Debug(" ending RQ.slice(0,20): " + RQ.slice(0,20));
  869. //Util.Debug("<< display_tight_png");
  870. },
  871. extract_data_uri : function (arr) {
  872. //var i, stra = [];
  873. //for (i=0; i< arr.length; i += 1) {
  874. // stra.push(String.fromCharCode(arr[i]));
  875. //}
  876. //return "," + escape(stra.join(''));
  877. return ";base64," + Base64.encode(arr);
  878. },
  879. scan_tight_imgs : function () {
  880. var img, imgs;
  881. if (RFB.state === 'normal') {
  882. imgs = RFB.FBU.imgs;
  883. while ((imgs.length > 0) && (imgs[0][0].complete)) {
  884. img = imgs.shift();
  885. Canvas.ctx.drawImage(img[0], img[1], img[2]);
  886. }
  887. setTimeout(RFB.scan_tight_imgs, RFB.scan_imgs_rate);
  888. }
  889. },
  890. set_desktopsize : function () {
  891. Util.Debug(">> set_desktopsize");
  892. RFB.fb_width = RFB.FBU.width;
  893. RFB.fb_height = RFB.FBU.height;
  894. Canvas.clear();
  895. Canvas.resize(RFB.fb_width, RFB.fb_height);
  896. RFB.timing.fbu_rt_start = (new Date()).getTime();
  897. // Send a new non-incremental request
  898. RFB.send_array(RFB.fbUpdateRequest(0));
  899. Util.Debug("<< set_desktopsize");
  900. RFB.FBU.bytes = 0;
  901. RFB.FBU.rects -= 1;
  902. },
  903. set_jpeg_quality : function () {
  904. Util.Debug(">> set_jpeg_quality");
  905. },
  906. set_compress_level: function () {
  907. Util.Debug(">> set_compress_level");
  908. },
  909. /*
  910. * Client message routines
  911. */
  912. pixelFormat: function () {
  913. //Util.Debug(">> pixelFormat");
  914. var arr;
  915. arr = [0]; // msg-type
  916. arr.push8(0); // padding
  917. arr.push8(0); // padding
  918. arr.push8(0); // padding
  919. arr.push8(RFB.fb_Bpp * 8); // bits-per-pixel
  920. arr.push8(RFB.fb_depth * 8); // depth
  921. arr.push8(0); // little-endian
  922. arr.push8(RFB.true_color); // true-color
  923. arr.push16(255); // red-max
  924. arr.push16(255); // green-max
  925. arr.push16(255); // blue-max
  926. arr.push8(0); // red-shift
  927. arr.push8(8); // green-shift
  928. arr.push8(16); // blue-shift
  929. arr.push8(0); // padding
  930. arr.push8(0); // padding
  931. arr.push8(0); // padding
  932. //Util.Debug("<< pixelFormat");
  933. return arr;
  934. },
  935. fixColourMapEntries: function () {
  936. },
  937. clientEncodings: function () {
  938. //Util.Debug(">> clientEncodings");
  939. var arr, i;
  940. arr = [2]; // msg-type
  941. arr.push8(0); // padding
  942. arr.push16(RFB.encodings.length); // encoding count
  943. for (i=0; i<RFB.encodings.length; i += 1) {
  944. arr.push32(RFB.encodings[i][1]);
  945. }
  946. //Util.Debug("<< clientEncodings: " + arr);
  947. return arr;
  948. },
  949. fbUpdateRequest: function (incremental, x, y, xw, yw) {
  950. //Util.Debug(">> fbUpdateRequest");
  951. if (!x) { x = 0; }
  952. if (!y) { y = 0; }
  953. if (!xw) { xw = RFB.fb_width; }
  954. if (!yw) { yw = RFB.fb_height; }
  955. var arr;
  956. arr = [3]; // msg-type
  957. arr.push8(incremental);
  958. arr.push16(x);
  959. arr.push16(y);
  960. arr.push16(xw);
  961. arr.push16(yw);
  962. //Util.Debug("<< fbUpdateRequest");
  963. return arr;
  964. },
  965. keyEvent: function (keysym, down) {
  966. //Util.Debug(">> keyEvent, keysym: " + keysym + ", down: " + down);
  967. var arr;
  968. arr = [4]; // msg-type
  969. arr.push8(down);
  970. arr.push16(0);
  971. arr.push32(keysym);
  972. //Util.Debug("<< keyEvent");
  973. return arr;
  974. },
  975. pointerEvent: function (x, y) {
  976. //Util.Debug(">> pointerEvent, x,y: " + x + "," + y +
  977. // " , mask: " + RFB.mouse_buttonMask);
  978. var arr;
  979. arr = [5]; // msg-type
  980. arr.push8(RFB.mouse_buttonMask);
  981. arr.push16(x);
  982. arr.push16(y);
  983. //Util.Debug("<< pointerEvent");
  984. return arr;
  985. },
  986. clientCutText: function (text) {
  987. //Util.Debug(">> clientCutText");
  988. var arr;
  989. arr = [6]; // msg-type
  990. arr.push8(0); // padding
  991. arr.push8(0); // padding
  992. arr.push8(0); // padding
  993. arr.push32(text.length);
  994. arr.pushStr(text);
  995. //Util.Debug("<< clientCutText:" + arr);
  996. return arr;
  997. },
  998. /*
  999. * Utility routines
  1000. */
  1001. encode_message: function(arr) {
  1002. if (RFB.b64encode) {
  1003. RFB.SQ = RFB.SQ + Base64.encode(arr);
  1004. } else {
  1005. RFB.SQ = RFB.SQ + arr.map(function (num) {
  1006. return String.fromCharCode(num); } ).join('');
  1007. }
  1008. },
  1009. decode_message: function(data) {
  1010. var raw, i, length, RQ = RFB.RQ;
  1011. //Util.Debug(">> decode_message: " + data);
  1012. if (RFB.b64encode) {
  1013. RFB.RQ = RFB.RQ.concat(Base64.decode(data, 0));
  1014. } else {
  1015. // A bit faster in firefox
  1016. length = data.length;
  1017. for (i=0; i < length; i += 1) {
  1018. RQ.push(data.charCodeAt(i) % 256);
  1019. }
  1020. }
  1021. //Util.Debug(">> decode_message, RQ: " + RFB.RQ);
  1022. },
  1023. recv_message: function(e) {
  1024. //Util.Debug(">> recv_message");
  1025. try {
  1026. RFB.decode_message(e.data);
  1027. RFB.handle_message();
  1028. } catch (exc) {
  1029. if (typeof exc.stack !== 'undefined') {
  1030. Util.Warn("recv_message, caught exception: " + exc.stack);
  1031. } else if (typeof exc.description !== 'undefined') {
  1032. Util.Warn("recv_message, caught exception: " + exc.description);
  1033. } else {
  1034. Util.Warn("recv_message, caught exception:" + exc);
  1035. }
  1036. if (typeof exc.name !== 'undefined') {
  1037. RFB.updateState('failed', exc.name + ": " + exc.message);
  1038. } else {
  1039. RFB.updateState('failed', exc);
  1040. }
  1041. }
  1042. //Util.Debug("<< recv_message");
  1043. },
  1044. handle_message: function () {
  1045. //Util.Debug("RQ.slice(0,20): " + RFB.RQ.slice(0,20) + " (" + RFB.RQ.length + ")");
  1046. switch (RFB.state) {
  1047. case 'disconnected':
  1048. Util.Error("Got data while disconnected");
  1049. break;
  1050. case 'failed':
  1051. Util.Warn("Giving up!");
  1052. RFB.disconnect();
  1053. break;
  1054. case 'normal':
  1055. RFB.normal_msg();
  1056. /*
  1057. while (RFB.RQ.length > 0) {
  1058. if (RFB.normal_msg() && RFB.state === 'normal') {
  1059. Util.Debug("More to process");
  1060. } else {
  1061. break;
  1062. }
  1063. }
  1064. */
  1065. break;
  1066. default:
  1067. RFB.init_msg();
  1068. break;
  1069. }
  1070. },
  1071. send_string: function (str) {
  1072. //Util.Debug(">> send_string: " + str);
  1073. RFB.send_array(str.split('').map(
  1074. function (chr) { return chr.charCodeAt(0); } ) );
  1075. },
  1076. send_array: function (arr) {
  1077. //Util.Debug(">> send_array: " + arr);
  1078. RFB.encode_message(arr);
  1079. if (RFB.ws.bufferedAmount === 0) {
  1080. //Util.Debug("arr: " + arr);
  1081. //Util.Debug("RFB.SQ: " + RFB.SQ);
  1082. RFB.ws.send(RFB.SQ);
  1083. RFB.SQ = "";
  1084. } else {
  1085. Util.Debug("Delaying send");
  1086. }
  1087. },
  1088. DES: function (password, challenge) {
  1089. var i, passwd, response;
  1090. passwd = [];
  1091. response = challenge.slice();
  1092. for (i=0; i < password.length; i += 1) {
  1093. passwd.push(password.charCodeAt(i));
  1094. }
  1095. DES.setKeys(passwd);
  1096. DES.encrypt(response, 0, response, 0);
  1097. DES.encrypt(response, 8, response, 8);
  1098. return response;
  1099. },
  1100. flushClient: function () {
  1101. if (RFB.mouse_arr.length > 0) {
  1102. //RFB.send_array(RFB.mouse_arr.concat(RFB.fbUpdateRequest(1)));
  1103. RFB.send_array(RFB.mouse_arr);
  1104. setTimeout(function() {
  1105. RFB.send_array(RFB.fbUpdateRequest(1));
  1106. }, 50);
  1107. RFB.mouse_arr = [];
  1108. return true;
  1109. } else {
  1110. return false;
  1111. }
  1112. },
  1113. checkEvents: function () {
  1114. var now;
  1115. if (RFB.state === 'normal') {
  1116. if (! RFB.flushClient()) {
  1117. now = new Date().getTime();
  1118. if (now > RFB.last_req + RFB.req_rate) {
  1119. RFB.last_req = now;
  1120. RFB.send_array(RFB.fbUpdateRequest(1));
  1121. }
  1122. }
  1123. }
  1124. setTimeout(RFB.checkEvents, RFB.check_rate);
  1125. },
  1126. keyPress: function (keysym, down) {
  1127. var arr;
  1128. arr = RFB.keyEvent(keysym, down);
  1129. arr = arr.concat(RFB.fbUpdateRequest(1));
  1130. RFB.send_array(arr);
  1131. },
  1132. mouseButton: function(x, y, down, bmask) {
  1133. if (down) {
  1134. RFB.mouse_buttonMask |= bmask;
  1135. } else {
  1136. RFB.mouse_buttonMask ^= bmask;
  1137. }
  1138. RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) );
  1139. RFB.flushClient();
  1140. },
  1141. mouseMove: function(x, y) {
  1142. //Util.Debug('>> mouseMove ' + x + "," + y);
  1143. RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) );
  1144. },
  1145. clipboardCopyTo: function (text) {
  1146. Util.Debug(">> clipboardCopyTo stub");
  1147. // Stub
  1148. },
  1149. externalUpdateState: function(state, msg) {
  1150. Util.Debug(">> externalUpdateState stub");
  1151. // Stub
  1152. },
  1153. updateState: function(state, statusMsg) {
  1154. var func, cmsg;
  1155. if (state === 'failed') {
  1156. func = Util.Error;
  1157. } else {
  1158. func = Util.Warn;
  1159. }
  1160. cmsg = typeof(statusMsg) !== 'undefined' ? (" Msg: " + statusMsg) : "";
  1161. func("New state '" + state + "'." + cmsg);
  1162. if ((state === 'disconnected') && (RFB.state !== 'disconnected')) {
  1163. RFB.show_timings();
  1164. }
  1165. if ((RFB.state === 'failed') &&
  1166. ((state === 'disconnected') || (state === 'closed'))) {
  1167. // Leave the failed message
  1168. RFB.externalUpdateState(state);
  1169. } else {
  1170. RFB.state = state;
  1171. RFB.externalUpdateState(state, statusMsg);
  1172. }
  1173. },
  1174. update_timings: function() {
  1175. var now, timing = RFB.timing, offset;
  1176. now = (new Date()).getTime();
  1177. timing.history.push([now,
  1178. timing.h_fbus,
  1179. timing.h_rects,
  1180. timing.h_bytes,
  1181. timing.h_pixels]);
  1182. timing.h_fbus = 0;
  1183. timing.h_rects = 0;
  1184. timing.h_bytes = 0;
  1185. timing.h_pixels = 0;
  1186. if ((RFB.state !== 'disconnected') && (RFB.state !== 'failed')) {
  1187. // Try for every second
  1188. offset = (now - timing.history_start) % 1000;
  1189. if (offset < 500) {
  1190. setTimeout(RFB.update_timings, 1000 - offset);
  1191. } else {
  1192. setTimeout(RFB.update_timings, 2000 - offset);
  1193. }
  1194. }
  1195. },
  1196. show_timings: function() {
  1197. var i, timing = RFB.timing, history, msg,
  1198. delta, tot_time = 0, tot_fbus = 0, tot_rects = 0,
  1199. tot_bytes = 0, tot_pixels = 0;
  1200. if (timing.history_start === 0) { return; }
  1201. //Util.Debug(">> show_timings");
  1202. RFB.update_timings(); // Final accumulate
  1203. msg = "\nTimings\n";
  1204. msg += " time: fbus,rects,bytes,pixels\n";
  1205. for (i=0; i < timing.history.length; i += 1) {
  1206. history = timing.history[i];
  1207. delta = ((history[0]-timing.history_start)/1000);
  1208. tot_time = delta;
  1209. tot_fbus += history[1];
  1210. tot_rects += history[2];
  1211. tot_bytes += history[3];
  1212. tot_pixels += history[4];
  1213. msg += " " + delta.toFixed(3);
  1214. msg += ": " + history.slice(1) + "\n";
  1215. }
  1216. msg += "\nTotals:\n";
  1217. msg += " time: fbus,rects,bytes,pixels\n";
  1218. msg += " " + tot_time.toFixed(3);
  1219. msg += ": " + tot_fbus + "," + tot_rects;
  1220. msg += "," + tot_bytes + "," + tot_pixels;
  1221. Util.Info(msg);
  1222. //Util.Debug("<< show_timings");
  1223. },
  1224. /*
  1225. * Setup routines
  1226. */
  1227. init_ws: function () {
  1228. //Util.Debug(">> init_ws");
  1229. var uri = "", vars = [];
  1230. if (RFB.encrypt) {
  1231. uri = "wss://";
  1232. } else {
  1233. uri = "ws://";
  1234. }
  1235. uri += RFB.host + ":" + RFB.port + "/";
  1236. if (RFB.b64encode) {
  1237. vars.push("b64encode");
  1238. }
  1239. if (vars.length > 0) {
  1240. uri += "?" + vars.join("&");
  1241. }
  1242. Util.Info("connecting to " + uri);
  1243. RFB.ws = new WebSocket(uri);
  1244. RFB.ws.onmessage = RFB.recv_message;
  1245. RFB.ws.onopen = function(e) {
  1246. Util.Debug(">> WebSocket.onopen");
  1247. RFB.updateState('ProtocolVersion', "Starting VNC handshake");
  1248. RFB.sendID = setInterval(function() {
  1249. /*
  1250. * Send updates either at a rate of one update every 50ms,
  1251. * or whatever slower rate the network can handle
  1252. */
  1253. if (RFB.ws.bufferedAmount === 0) {
  1254. if (RFB.SQ) {
  1255. RFB.ws.send(RFB.SQ);
  1256. RFB.SQ = "";
  1257. }
  1258. } else {
  1259. Util.Debug("Delaying send");
  1260. }
  1261. }, 50);
  1262. Util.Debug("<< WebSocket.onopen");
  1263. };
  1264. RFB.ws.onclose = function(e) {
  1265. Util.Debug(">> WebSocket.onclose");
  1266. clearInterval(RFB.sendID);
  1267. RFB.updateState('disconnected', 'VNC disconnected');
  1268. Util.Debug("<< WebSocket.onclose");
  1269. };
  1270. RFB.ws.onerror = function(e) {
  1271. Util.Debug(">> WebSocket.onerror");
  1272. RFB.updateState('failed', "WebSocket error");
  1273. Util.Debug("<< WebSocket.onerror");
  1274. };
  1275. setTimeout(function () {
  1276. if (RFB.ws.readyState === WebSocket.CONNECTING) {
  1277. RFB.updateState('failed', "Connect timeout");
  1278. RFB.ws.close();
  1279. }
  1280. }, RFB.connectTimeout);
  1281. //Util.Debug("<< init_ws");
  1282. },
  1283. init_vars: function () {
  1284. /* Reset state */
  1285. RFB.cuttext = 'none';
  1286. RFB.ct_length = 0;
  1287. RFB.RQ = [];
  1288. RFB.SQ = "";
  1289. RFB.FBU.rects = 0;
  1290. RFB.FBU.subrects = 0; // RRE and HEXTILE
  1291. RFB.FBU.lines = 0; // RAW
  1292. RFB.FBU.tiles = 0; // HEXTILE
  1293. RFB.FBU.imgs = []; // TIGHT_PNG image queue
  1294. RFB.mouse_buttonmask = 0;
  1295. RFB.mouse_arr = [];
  1296. RFB.timing.history_start = 0;
  1297. RFB.timing.history = [];
  1298. RFB.timing.h_fbus = 0;
  1299. RFB.timing.h_rects = 0;
  1300. RFB.timing.h_bytes = 0;
  1301. RFB.timing.h_pixels = 0;
  1302. }
  1303. }; /* End of RFB */