ui.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Copyright (C) 2015 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. /* jslint white: false, browser: true */
  10. /* global window, $D, Util, WebUtil, RFB, Display */
  11. var UI;
  12. (function () {
  13. "use strict";
  14. var resizeTimeout;
  15. // Load supporting scripts
  16. window.onscriptsload = function () { UI.load(); };
  17. Util.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
  18. "keysymdef.js", "keyboard.js", "input.js", "display.js",
  19. "jsunzip.js", "rfb.js", "keysym.js"]);
  20. UI = {
  21. rfb_state : 'loaded',
  22. settingsOpen : false,
  23. connSettingsOpen : false,
  24. popupStatusTimeout: null,
  25. clipboardOpen: false,
  26. keyboardVisible: false,
  27. hideKeyboardTimeout: null,
  28. lastKeyboardinput: null,
  29. defaultKeyboardinputLen: 100,
  30. extraKeysVisible: false,
  31. ctrlOn: false,
  32. altOn: false,
  33. isTouchDevice: false,
  34. // Setup rfb object, load settings from browser storage, then call
  35. // UI.init to setup the UI/menus
  36. load: function (callback) {
  37. WebUtil.initSettings(UI.start, callback);
  38. },
  39. // Render default UI and initialize settings menu
  40. start: function(callback) {
  41. UI.isTouchDevice = 'ontouchstart' in document.documentElement;
  42. // Stylesheet selection dropdown
  43. var sheet = WebUtil.selectStylesheet();
  44. var sheets = WebUtil.getStylesheets();
  45. var i;
  46. for (i = 0; i < sheets.length; i += 1) {
  47. UI.addOption($D('noVNC_stylesheet'),sheets[i].title, sheets[i].title);
  48. }
  49. // Logging selection dropdown
  50. var llevels = ['error', 'warn', 'info', 'debug'];
  51. for (i = 0; i < llevels.length; i += 1) {
  52. UI.addOption($D('noVNC_logging'),llevels[i], llevels[i]);
  53. }
  54. // Settings with immediate effects
  55. UI.initSetting('logging', 'warn');
  56. WebUtil.init_logging(UI.getSetting('logging'));
  57. UI.initSetting('stylesheet', 'default');
  58. WebUtil.selectStylesheet(null);
  59. // call twice to get around webkit bug
  60. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  61. // if port == 80 (or 443) then it won't be present and should be
  62. // set manually
  63. var port = window.location.port;
  64. if (!port) {
  65. if (window.location.protocol.substring(0,5) == 'https') {
  66. port = 443;
  67. }
  68. else if (window.location.protocol.substring(0,4) == 'http') {
  69. port = 80;
  70. }
  71. }
  72. /* Populate the controls if defaults are provided in the URL */
  73. UI.initSetting('host', window.location.hostname);
  74. UI.initSetting('port', port);
  75. UI.initSetting('password', '');
  76. UI.initSetting('encrypt', (window.location.protocol === "https:"));
  77. UI.initSetting('true_color', true);
  78. UI.initSetting('cursor', !UI.isTouchDevice);
  79. UI.initSetting('resize', 'off');
  80. UI.initSetting('shared', true);
  81. UI.initSetting('view_only', false);
  82. UI.initSetting('path', 'websockify');
  83. UI.initSetting('repeaterID', '');
  84. var autoconnect = WebUtil.getQueryVar('autoconnect', false);
  85. if (autoconnect === 'true' || autoconnect == '1') {
  86. autoconnect = true;
  87. UI.connect();
  88. } else {
  89. autoconnect = false;
  90. }
  91. UI.updateVisualState();
  92. $D('noVNC_host').focus();
  93. // Show mouse selector buttons on touch screen devices
  94. if (UI.isTouchDevice) {
  95. // Show mobile buttons
  96. $D('noVNC_mobile_buttons').style.display = "inline";
  97. UI.setMouseButton();
  98. // Remove the address bar
  99. setTimeout(function() { window.scrollTo(0, 1); }, 100);
  100. UI.forceSetting('clip', true);
  101. } else {
  102. UI.initSetting('clip', false);
  103. }
  104. UI.setViewClip();
  105. UI.setBarPosition();
  106. Util.addEvent(window, 'resize', function () {
  107. UI.onresize();
  108. UI.setViewClip();
  109. UI.updateViewDrag();
  110. UI.setBarPosition();
  111. } );
  112. Util.addEvent(window, 'load', UI.keyboardinputReset);
  113. Util.addEvent(window, 'beforeunload', function () {
  114. if (UI.rfb && UI.rfb_state === 'normal') {
  115. return "You are currently connected.";
  116. }
  117. } );
  118. // Show description by default when hosted at for kanaka.github.com
  119. if (location.host === "kanaka.github.io") {
  120. // Open the description dialog
  121. $D('noVNC_description').style.display = "block";
  122. } else {
  123. // Show the connect panel on first load unless autoconnecting
  124. if (autoconnect === UI.connSettingsOpen) {
  125. UI.toggleConnectPanel();
  126. }
  127. }
  128. // Add mouse event click/focus/blur event handlers to the UI
  129. UI.addMouseHandlers();
  130. if (typeof callback === "function") {
  131. callback(UI.rfb);
  132. }
  133. },
  134. initRFB: function () {
  135. try {
  136. UI.rfb = new RFB({'target': $D('noVNC_canvas'),
  137. 'onUpdateState': UI.updateState,
  138. 'onXvpInit': UI.updateXvpVisualState,
  139. 'onClipboard': UI.clipReceive,
  140. 'onFBUComplete': UI.FBUComplete,
  141. 'onFBResize': UI.updateViewDrag,
  142. 'onDesktopName': UI.updateDocumentTitle});
  143. return true;
  144. } catch (exc) {
  145. UI.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
  146. return false;
  147. }
  148. },
  149. addMouseHandlers: function() {
  150. // Setup interface handlers that can't be inline
  151. $D("noVNC_view_drag_button").onclick = UI.toggleViewDrag;
  152. $D("noVNC_mouse_button0").onclick = function () { UI.setMouseButton(1); };
  153. $D("noVNC_mouse_button1").onclick = function () { UI.setMouseButton(2); };
  154. $D("noVNC_mouse_button2").onclick = function () { UI.setMouseButton(4); };
  155. $D("noVNC_mouse_button4").onclick = function () { UI.setMouseButton(0); };
  156. $D("showKeyboard").onclick = UI.showKeyboard;
  157. $D("keyboardinput").oninput = UI.keyInput;
  158. $D("keyboardinput").onblur = UI.keyInputBlur;
  159. $D("keyboardinput").onsubmit = function () { return false; };
  160. $D("showExtraKeysButton").onclick = UI.showExtraKeys;
  161. $D("toggleCtrlButton").onclick = UI.toggleCtrl;
  162. $D("toggleAltButton").onclick = UI.toggleAlt;
  163. $D("sendTabButton").onclick = UI.sendTab;
  164. $D("sendEscButton").onclick = UI.sendEsc;
  165. $D("sendCtrlAltDelButton").onclick = UI.sendCtrlAltDel;
  166. $D("xvpShutdownButton").onclick = UI.xvpShutdown;
  167. $D("xvpRebootButton").onclick = UI.xvpReboot;
  168. $D("xvpResetButton").onclick = UI.xvpReset;
  169. $D("noVNC_status").onclick = UI.togglePopupStatus;
  170. $D("noVNC_popup_status").onclick = UI.togglePopupStatus;
  171. $D("xvpButton").onclick = UI.toggleXvpPanel;
  172. $D("clipboardButton").onclick = UI.toggleClipboardPanel;
  173. $D("settingsButton").onclick = UI.toggleSettingsPanel;
  174. $D("connectButton").onclick = UI.toggleConnectPanel;
  175. $D("disconnectButton").onclick = UI.disconnect;
  176. $D("descriptionButton").onclick = UI.toggleConnectPanel;
  177. $D("noVNC_clipboard_text").onfocus = UI.displayBlur;
  178. $D("noVNC_clipboard_text").onblur = UI.displayFocus;
  179. $D("noVNC_clipboard_text").onchange = UI.clipSend;
  180. $D("noVNC_clipboard_clear_button").onclick = UI.clipClear;
  181. $D("noVNC_settings_menu").onmouseover = UI.displayBlur;
  182. $D("noVNC_settings_menu").onmouseover = UI.displayFocus;
  183. $D("noVNC_apply").onclick = UI.settingsApply;
  184. $D("noVNC_connect_button").onclick = UI.connect;
  185. $D("noVNC_resize").onchange = function () {
  186. var connected = UI.rfb && UI.rfb_state === 'normal';
  187. UI.enableDisableViewClip(connected);
  188. };
  189. },
  190. onresize: function (callback) {
  191. if (!UI.rfb) return;
  192. var size = UI.getCanvasLimit();
  193. if (size && UI.rfb_state === 'normal' && UI.rfb.get_display()) {
  194. var display = UI.rfb.get_display();
  195. var scaleType = UI.getSetting('resize');
  196. if (scaleType === 'remote') {
  197. // use remote resizing
  198. // When the local window has been resized, wait until the size remains
  199. // the same for 0.5 seconds before sending the request for changing
  200. // the resolution of the session
  201. clearTimeout(resizeTimeout);
  202. resizeTimeout = setTimeout(function(){
  203. display.set_maxWidth(size.w);
  204. display.set_maxHeight(size.h);
  205. Util.Debug('Attempting setDesktopSize(' +
  206. size.w + ', ' + size.h + ')');
  207. UI.rfb.setDesktopSize(size.w, size.h);
  208. }, 500);
  209. } else if (scaleType === 'scale' || scaleType === 'downscale') {
  210. // use local scaling
  211. var downscaleOnly = scaleType === 'downscale';
  212. var scaleRatio = display.autoscale(size.w, size.h, downscaleOnly);
  213. UI.rfb.get_mouse().set_scale(scaleRatio);
  214. Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
  215. }
  216. }
  217. },
  218. getCanvasLimit: function () {
  219. var container = $D('noVNC_container');
  220. // Hide the scrollbars until the size is calculated
  221. container.style.overflow = "hidden";
  222. var pos = Util.getPosition(container);
  223. var w = pos.width;
  224. var h = pos.height;
  225. container.style.overflow = "visible";
  226. if (isNaN(w) || isNaN(h)) {
  227. return false;
  228. } else {
  229. return {w: w, h: h};
  230. }
  231. },
  232. // Read form control compatible setting from cookie
  233. getSetting: function(name) {
  234. var ctrl = $D('noVNC_' + name);
  235. var val = WebUtil.readSetting(name);
  236. if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
  237. if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
  238. val = false;
  239. } else {
  240. val = true;
  241. }
  242. }
  243. return val;
  244. },
  245. // Update cookie and form control setting. If value is not set, then
  246. // updates from control to current cookie setting.
  247. updateSetting: function(name, value) {
  248. // Save the cookie for this session
  249. if (typeof value !== 'undefined') {
  250. WebUtil.writeSetting(name, value);
  251. }
  252. // Update the settings control
  253. value = UI.getSetting(name);
  254. var ctrl = $D('noVNC_' + name);
  255. if (ctrl.type === 'checkbox') {
  256. ctrl.checked = value;
  257. } else if (typeof ctrl.options !== 'undefined') {
  258. for (var i = 0; i < ctrl.options.length; i += 1) {
  259. if (ctrl.options[i].value === value) {
  260. ctrl.selectedIndex = i;
  261. break;
  262. }
  263. }
  264. } else {
  265. /*Weird IE9 error leads to 'null' appearring
  266. in textboxes instead of ''.*/
  267. if (value === null) {
  268. value = "";
  269. }
  270. ctrl.value = value;
  271. }
  272. },
  273. // Save control setting to cookie
  274. saveSetting: function(name) {
  275. var val, ctrl = $D('noVNC_' + name);
  276. if (ctrl.type === 'checkbox') {
  277. val = ctrl.checked;
  278. } else if (typeof ctrl.options !== 'undefined') {
  279. val = ctrl.options[ctrl.selectedIndex].value;
  280. } else {
  281. val = ctrl.value;
  282. }
  283. WebUtil.writeSetting(name, val);
  284. //Util.Debug("Setting saved '" + name + "=" + val + "'");
  285. return val;
  286. },
  287. // Initial page load read/initialization of settings
  288. initSetting: function(name, defVal) {
  289. // Check Query string followed by cookie
  290. var val = WebUtil.getQueryVar(name);
  291. if (val === null) {
  292. val = WebUtil.readSetting(name, defVal);
  293. }
  294. UI.updateSetting(name, val);
  295. return val;
  296. },
  297. // Force a setting to be a certain value
  298. forceSetting: function(name, val) {
  299. UI.updateSetting(name, val);
  300. return val;
  301. },
  302. // Show the popup status
  303. togglePopupStatus: function(text) {
  304. var psp = $D('noVNC_popup_status');
  305. var closePopup = function() { psp.style.display = "none"; };
  306. if (window.getComputedStyle(psp).display === 'none') {
  307. if (typeof text === 'string') {
  308. psp.innerHTML = text;
  309. } else {
  310. psp.innerHTML = $D('noVNC_status').innerHTML;
  311. }
  312. psp.style.display = "block";
  313. psp.style.left = window.innerWidth/2 -
  314. parseInt(window.getComputedStyle(psp).width)/2 -30 + "px";
  315. // Show the popup for a maximum of 1.5 seconds
  316. UI.popupStatusTimeout = setTimeout(function() { closePopup(); }, 1500);
  317. } else {
  318. clearTimeout(UI.popupStatusTimeout);
  319. closePopup();
  320. }
  321. },
  322. // Show the XVP panel
  323. toggleXvpPanel: function() {
  324. // Close the description panel
  325. $D('noVNC_description').style.display = "none";
  326. // Close settings if open
  327. if (UI.settingsOpen === true) {
  328. UI.settingsApply();
  329. UI.closeSettingsMenu();
  330. }
  331. // Close connection settings if open
  332. if (UI.connSettingsOpen === true) {
  333. UI.toggleConnectPanel();
  334. }
  335. // Close clipboard panel if open
  336. if (UI.clipboardOpen === true) {
  337. UI.toggleClipboardPanel();
  338. }
  339. // Toggle XVP panel
  340. if (UI.xvpOpen === true) {
  341. $D('noVNC_xvp').style.display = "none";
  342. $D('xvpButton').className = "noVNC_status_button";
  343. UI.xvpOpen = false;
  344. } else {
  345. $D('noVNC_xvp').style.display = "block";
  346. $D('xvpButton').className = "noVNC_status_button_selected";
  347. UI.xvpOpen = true;
  348. }
  349. },
  350. // Show the clipboard panel
  351. toggleClipboardPanel: function() {
  352. // Close the description panel
  353. $D('noVNC_description').style.display = "none";
  354. // Close settings if open
  355. if (UI.settingsOpen === true) {
  356. UI.settingsApply();
  357. UI.closeSettingsMenu();
  358. }
  359. // Close connection settings if open
  360. if (UI.connSettingsOpen === true) {
  361. UI.toggleConnectPanel();
  362. }
  363. // Close XVP panel if open
  364. if (UI.xvpOpen === true) {
  365. UI.toggleXvpPanel();
  366. }
  367. // Toggle Clipboard Panel
  368. if (UI.clipboardOpen === true) {
  369. $D('noVNC_clipboard').style.display = "none";
  370. $D('clipboardButton').className = "noVNC_status_button";
  371. UI.clipboardOpen = false;
  372. } else {
  373. $D('noVNC_clipboard').style.display = "block";
  374. $D('clipboardButton').className = "noVNC_status_button_selected";
  375. UI.clipboardOpen = true;
  376. }
  377. },
  378. // Show the connection settings panel/menu
  379. toggleConnectPanel: function() {
  380. // Close the description panel
  381. $D('noVNC_description').style.display = "none";
  382. // Close connection settings if open
  383. if (UI.settingsOpen === true) {
  384. UI.settingsApply();
  385. UI.closeSettingsMenu();
  386. $D('connectButton').className = "noVNC_status_button";
  387. }
  388. // Close clipboard panel if open
  389. if (UI.clipboardOpen === true) {
  390. UI.toggleClipboardPanel();
  391. }
  392. // Close XVP panel if open
  393. if (UI.xvpOpen === true) {
  394. UI.toggleXvpPanel();
  395. }
  396. // Toggle Connection Panel
  397. if (UI.connSettingsOpen === true) {
  398. $D('noVNC_controls').style.display = "none";
  399. $D('connectButton').className = "noVNC_status_button";
  400. UI.connSettingsOpen = false;
  401. UI.saveSetting('host');
  402. UI.saveSetting('port');
  403. //UI.saveSetting('password');
  404. } else {
  405. $D('noVNC_controls').style.display = "block";
  406. $D('connectButton').className = "noVNC_status_button_selected";
  407. UI.connSettingsOpen = true;
  408. $D('noVNC_host').focus();
  409. }
  410. },
  411. // Toggle the settings menu:
  412. // On open, settings are refreshed from saved cookies.
  413. // On close, settings are applied
  414. toggleSettingsPanel: function() {
  415. // Close the description panel
  416. $D('noVNC_description').style.display = "none";
  417. if (UI.settingsOpen) {
  418. UI.settingsApply();
  419. UI.closeSettingsMenu();
  420. } else {
  421. UI.updateSetting('encrypt');
  422. UI.updateSetting('true_color');
  423. if (Util.browserSupportsCursorURIs()) {
  424. UI.updateSetting('cursor');
  425. } else {
  426. UI.updateSetting('cursor', !UI.isTouchDevice);
  427. $D('noVNC_cursor').disabled = true;
  428. }
  429. UI.updateSetting('clip');
  430. UI.updateSetting('resize');
  431. UI.updateSetting('shared');
  432. UI.updateSetting('view_only');
  433. UI.updateSetting('path');
  434. UI.updateSetting('repeaterID');
  435. UI.updateSetting('stylesheet');
  436. UI.updateSetting('logging');
  437. UI.openSettingsMenu();
  438. }
  439. },
  440. // Open menu
  441. openSettingsMenu: function() {
  442. // Close the description panel
  443. $D('noVNC_description').style.display = "none";
  444. // Close clipboard panel if open
  445. if (UI.clipboardOpen === true) {
  446. UI.toggleClipboardPanel();
  447. }
  448. // Close connection settings if open
  449. if (UI.connSettingsOpen === true) {
  450. UI.toggleConnectPanel();
  451. }
  452. // Close XVP panel if open
  453. if (UI.xvpOpen === true) {
  454. UI.toggleXvpPanel();
  455. }
  456. $D('noVNC_settings').style.display = "block";
  457. $D('settingsButton').className = "noVNC_status_button_selected";
  458. UI.settingsOpen = true;
  459. },
  460. // Close menu (without applying settings)
  461. closeSettingsMenu: function() {
  462. $D('noVNC_settings').style.display = "none";
  463. $D('settingsButton').className = "noVNC_status_button";
  464. UI.settingsOpen = false;
  465. },
  466. // Save/apply settings when 'Apply' button is pressed
  467. settingsApply: function() {
  468. //Util.Debug(">> settingsApply");
  469. UI.saveSetting('encrypt');
  470. UI.saveSetting('true_color');
  471. if (Util.browserSupportsCursorURIs()) {
  472. UI.saveSetting('cursor');
  473. }
  474. UI.saveSetting('resize');
  475. if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
  476. UI.forceSetting('clip', false);
  477. }
  478. UI.saveSetting('clip');
  479. UI.saveSetting('shared');
  480. UI.saveSetting('view_only');
  481. UI.saveSetting('path');
  482. UI.saveSetting('repeaterID');
  483. UI.saveSetting('stylesheet');
  484. UI.saveSetting('logging');
  485. // Settings with immediate (non-connected related) effect
  486. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  487. WebUtil.init_logging(UI.getSetting('logging'));
  488. UI.setViewClip();
  489. UI.updateViewDrag();
  490. //Util.Debug("<< settingsApply");
  491. },
  492. setPassword: function() {
  493. UI.rfb.sendPassword($D('noVNC_password').value);
  494. //Reset connect button.
  495. $D('noVNC_connect_button').value = "Connect";
  496. $D('noVNC_connect_button').onclick = UI.Connect;
  497. //Hide connection panel.
  498. UI.toggleConnectPanel();
  499. return false;
  500. },
  501. sendCtrlAltDel: function() {
  502. UI.rfb.sendCtrlAltDel();
  503. },
  504. xvpShutdown: function() {
  505. UI.rfb.xvpShutdown();
  506. },
  507. xvpReboot: function() {
  508. UI.rfb.xvpReboot();
  509. },
  510. xvpReset: function() {
  511. UI.rfb.xvpReset();
  512. },
  513. setMouseButton: function(num) {
  514. if (typeof num === 'undefined') {
  515. // Disable mouse buttons
  516. num = -1;
  517. }
  518. if (UI.rfb) {
  519. UI.rfb.get_mouse().set_touchButton(num);
  520. }
  521. var blist = [0, 1,2,4];
  522. for (var b = 0; b < blist.length; b++) {
  523. var button = $D('noVNC_mouse_button' + blist[b]);
  524. if (blist[b] === num) {
  525. button.style.display = "";
  526. } else {
  527. button.style.display = "none";
  528. }
  529. }
  530. },
  531. updateState: function(rfb, state, oldstate, msg) {
  532. UI.rfb_state = state;
  533. var klass;
  534. switch (state) {
  535. case 'failed':
  536. case 'fatal':
  537. klass = "noVNC_status_error";
  538. break;
  539. case 'normal':
  540. klass = "noVNC_status_normal";
  541. break;
  542. case 'disconnected':
  543. $D('noVNC_logo').style.display = "block";
  544. $D('noVNC_container').style.display = "none";
  545. /* falls through */
  546. case 'loaded':
  547. klass = "noVNC_status_normal";
  548. break;
  549. case 'password':
  550. UI.toggleConnectPanel();
  551. $D('noVNC_connect_button').value = "Send Password";
  552. $D('noVNC_connect_button').onclick = UI.setPassword;
  553. $D('noVNC_password').focus();
  554. klass = "noVNC_status_warn";
  555. break;
  556. default:
  557. klass = "noVNC_status_warn";
  558. break;
  559. }
  560. if (typeof(msg) !== 'undefined') {
  561. $D('noVNC-control-bar').setAttribute("class", klass);
  562. $D('noVNC_status').innerHTML = msg;
  563. }
  564. UI.updateVisualState();
  565. },
  566. // Disable/enable controls depending on connection state
  567. updateVisualState: function() {
  568. var connected = UI.rfb && UI.rfb_state === 'normal';
  569. //Util.Debug(">> updateVisualState");
  570. $D('noVNC_encrypt').disabled = connected;
  571. $D('noVNC_true_color').disabled = connected;
  572. if (Util.browserSupportsCursorURIs()) {
  573. $D('noVNC_cursor').disabled = connected;
  574. } else {
  575. UI.updateSetting('cursor', !UI.isTouchDevice);
  576. $D('noVNC_cursor').disabled = true;
  577. }
  578. UI.enableDisableViewClip(connected);
  579. $D('noVNC_resize').disabled = connected;
  580. $D('noVNC_shared').disabled = connected;
  581. $D('noVNC_view_only').disabled = connected;
  582. $D('noVNC_path').disabled = connected;
  583. $D('noVNC_repeaterID').disabled = connected;
  584. if (connected) {
  585. UI.setViewClip();
  586. UI.setMouseButton(1);
  587. $D('clipboardButton').style.display = "inline";
  588. $D('showKeyboard').style.display = "inline";
  589. $D('noVNC_extra_keys').style.display = "";
  590. $D('sendCtrlAltDelButton').style.display = "inline";
  591. } else {
  592. UI.setMouseButton();
  593. $D('clipboardButton').style.display = "none";
  594. $D('showKeyboard').style.display = "none";
  595. $D('noVNC_extra_keys').style.display = "none";
  596. $D('sendCtrlAltDelButton').style.display = "none";
  597. UI.updateXvpVisualState(0);
  598. }
  599. // State change disables viewport dragging.
  600. // It is enabled (toggled) by direct click on the button
  601. UI.updateViewDrag(false);
  602. switch (UI.rfb_state) {
  603. case 'fatal':
  604. case 'failed':
  605. case 'disconnected':
  606. $D('connectButton').style.display = "";
  607. $D('disconnectButton').style.display = "none";
  608. UI.connSettingsOpen = false;
  609. UI.toggleConnectPanel();
  610. break;
  611. case 'loaded':
  612. $D('connectButton').style.display = "";
  613. $D('disconnectButton').style.display = "none";
  614. break;
  615. default:
  616. $D('connectButton').style.display = "none";
  617. $D('disconnectButton').style.display = "";
  618. break;
  619. }
  620. //Util.Debug("<< updateVisualState");
  621. },
  622. // Disable/enable XVP button
  623. updateXvpVisualState: function(ver) {
  624. if (ver >= 1) {
  625. $D('xvpButton').style.display = 'inline';
  626. } else {
  627. $D('xvpButton').style.display = 'none';
  628. // Close XVP panel if open
  629. if (UI.xvpOpen === true) {
  630. UI.toggleXvpPanel();
  631. }
  632. }
  633. },
  634. // This resize can not be done until we know from the first Frame Buffer Update
  635. // if it is supported or not.
  636. // The resize is needed to make sure the server desktop size is updated to the
  637. // corresponding size of the current local window when reconnecting to an
  638. // existing session.
  639. FBUComplete: function(rfb, fbu) {
  640. UI.onresize();
  641. UI.rfb.set_onFBUComplete(function() { });
  642. },
  643. // Display the desktop name in the document title
  644. updateDocumentTitle: function(rfb, name) {
  645. document.title = name + " - noVNC";
  646. },
  647. clipReceive: function(rfb, text) {
  648. Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
  649. $D('noVNC_clipboard_text').value = text;
  650. Util.Debug("<< UI.clipReceive");
  651. },
  652. connect: function() {
  653. UI.closeSettingsMenu();
  654. UI.toggleConnectPanel();
  655. var host = $D('noVNC_host').value;
  656. var port = $D('noVNC_port').value;
  657. var password = $D('noVNC_password').value;
  658. var path = $D('noVNC_path').value;
  659. if ((!host) || (!port)) {
  660. throw new Error("Must set host and port");
  661. }
  662. if (!UI.initRFB()) return;
  663. UI.rfb.set_encrypt(UI.getSetting('encrypt'));
  664. UI.rfb.set_true_color(UI.getSetting('true_color'));
  665. UI.rfb.set_local_cursor(UI.getSetting('cursor'));
  666. UI.rfb.set_shared(UI.getSetting('shared'));
  667. UI.rfb.set_view_only(UI.getSetting('view_only'));
  668. UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
  669. UI.rfb.connect(host, port, password, path);
  670. //Close dialog.
  671. setTimeout(UI.setBarPosition, 100);
  672. $D('noVNC_logo').style.display = "none";
  673. $D('noVNC_container').style.display = "inline";
  674. },
  675. disconnect: function() {
  676. UI.closeSettingsMenu();
  677. UI.rfb.disconnect();
  678. // Restore the callback used for initial resize
  679. UI.rfb.set_onFBUComplete(UI.FBUComplete);
  680. $D('noVNC_logo').style.display = "block";
  681. $D('noVNC_container').style.display = "none";
  682. // Don't display the connection settings until we're actually disconnected
  683. },
  684. displayBlur: function() {
  685. if (!UI.rfb) return;
  686. UI.rfb.get_keyboard().set_focused(false);
  687. UI.rfb.get_mouse().set_focused(false);
  688. },
  689. displayFocus: function() {
  690. if (!UI.rfb) return;
  691. UI.rfb.get_keyboard().set_focused(true);
  692. UI.rfb.get_mouse().set_focused(true);
  693. },
  694. clipClear: function() {
  695. $D('noVNC_clipboard_text').value = "";
  696. UI.rfb.clipboardPasteFrom("");
  697. },
  698. clipSend: function() {
  699. var text = $D('noVNC_clipboard_text').value;
  700. Util.Debug(">> UI.clipSend: " + text.substr(0,40) + "...");
  701. UI.rfb.clipboardPasteFrom(text);
  702. Util.Debug("<< UI.clipSend");
  703. },
  704. // Set and configure viewport clipping
  705. setViewClip: function(clip) {
  706. var display;
  707. if (UI.rfb) {
  708. display = UI.rfb.get_display();
  709. } else {
  710. return;
  711. }
  712. var cur_clip = display.get_viewport();
  713. if (typeof(clip) !== 'boolean') {
  714. // Use current setting
  715. clip = UI.getSetting('clip');
  716. }
  717. if (clip && !cur_clip) {
  718. // Turn clipping on
  719. UI.updateSetting('clip', true);
  720. } else if (!clip && cur_clip) {
  721. // Turn clipping off
  722. UI.updateSetting('clip', false);
  723. display.set_viewport(false);
  724. // Disable max dimensions
  725. display.set_maxWidth(0);
  726. display.set_maxHeight(0);
  727. display.viewportChangeSize();
  728. }
  729. if (UI.getSetting('clip')) {
  730. // If clipping, update clipping settings
  731. display.set_viewport(true);
  732. var size = UI.getCanvasLimit();
  733. if (size) {
  734. display.set_maxWidth(size.w);
  735. display.set_maxHeight(size.h);
  736. // Hide potential scrollbars that can skew the position
  737. $D('noVNC_container').style.overflow = "hidden";
  738. // The x position marks the left margin of the canvas,
  739. // remove the margin from both sides to keep it centered
  740. var new_w = size.w - (2 * Util.getPosition($D('noVNC_canvas')).x);
  741. $D('noVNC_container').style.overflow = "visible";
  742. display.viewportChangeSize(new_w, size.h);
  743. }
  744. }
  745. },
  746. // Handle special cases where clipping is forced on/off or locked
  747. enableDisableViewClip: function (connected) {
  748. var resizeElem = $D('noVNC_resize');
  749. if (resizeElem.value === 'downscale' || resizeElem.value === 'scale') {
  750. UI.forceSetting('clip', false);
  751. $D('noVNC_clip').disabled = true;
  752. } else {
  753. $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
  754. if (UI.isTouchDevice) {
  755. UI.forceSetting('clip', true);
  756. }
  757. }
  758. },
  759. // Update the viewport drag/move button
  760. updateViewDrag: function(drag) {
  761. if (!UI.rfb) return;
  762. var vmb = $D('noVNC_view_drag_button');
  763. // Check if viewport drag is possible
  764. if (UI.rfb_state === 'normal' &&
  765. UI.rfb.get_display().get_viewport() &&
  766. UI.rfb.get_display().clippingDisplay()) {
  767. // Show and enable the drag button
  768. vmb.style.display = "inline";
  769. vmb.disabled = false;
  770. } else {
  771. // The VNC content is the same size as
  772. // or smaller than the display
  773. if (UI.rfb.get_viewportDrag) {
  774. // Turn off viewport drag when it's
  775. // active since it can't be used here
  776. vmb.className = "noVNC_status_button";
  777. UI.rfb.set_viewportDrag(false);
  778. }
  779. // Disable or hide the drag button
  780. if (UI.rfb_state === 'normal' && UI.isTouchDevice) {
  781. vmb.style.display = "inline";
  782. vmb.disabled = true;
  783. } else {
  784. vmb.style.display = "none";
  785. }
  786. return;
  787. }
  788. if (typeof(drag) !== "undefined" &&
  789. typeof(drag) !== "object") {
  790. if (drag) {
  791. vmb.className = "noVNC_status_button_selected";
  792. UI.rfb.set_viewportDrag(true);
  793. } else {
  794. vmb.className = "noVNC_status_button";
  795. UI.rfb.set_viewportDrag(false);
  796. }
  797. }
  798. },
  799. toggleViewDrag: function() {
  800. if (!UI.rfb) return;
  801. var vmb = $D('noVNC_view_drag_button');
  802. if (UI.rfb.get_viewportDrag()) {
  803. vmb.className = "noVNC_status_button";
  804. UI.rfb.set_viewportDrag(false);
  805. } else {
  806. vmb.className = "noVNC_status_button_selected";
  807. UI.rfb.set_viewportDrag(true);
  808. }
  809. },
  810. // On touch devices, show the OS keyboard
  811. showKeyboard: function() {
  812. var kbi = $D('keyboardinput');
  813. var skb = $D('showKeyboard');
  814. var l = kbi.value.length;
  815. if(UI.keyboardVisible === false) {
  816. kbi.focus();
  817. try { kbi.setSelectionRange(l, l); } // Move the caret to the end
  818. catch (err) {} // setSelectionRange is undefined in Google Chrome
  819. UI.keyboardVisible = true;
  820. skb.className = "noVNC_status_button_selected";
  821. } else if(UI.keyboardVisible === true) {
  822. kbi.blur();
  823. skb.className = "noVNC_status_button";
  824. UI.keyboardVisible = false;
  825. }
  826. },
  827. keepKeyboard: function() {
  828. clearTimeout(UI.hideKeyboardTimeout);
  829. if(UI.keyboardVisible === true) {
  830. $D('keyboardinput').focus();
  831. $D('showKeyboard').className = "noVNC_status_button_selected";
  832. } else if(UI.keyboardVisible === false) {
  833. $D('keyboardinput').blur();
  834. $D('showKeyboard').className = "noVNC_status_button";
  835. }
  836. },
  837. keyboardinputReset: function() {
  838. var kbi = $D('keyboardinput');
  839. kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
  840. UI.lastKeyboardinput = kbi.value;
  841. },
  842. // When normal keyboard events are left uncought, use the input events from
  843. // the keyboardinput element instead and generate the corresponding key events.
  844. // This code is required since some browsers on Android are inconsistent in
  845. // sending keyCodes in the normal keyboard events when using on screen keyboards.
  846. keyInput: function(event) {
  847. if (!UI.rfb) return;
  848. var newValue = event.target.value;
  849. if (!UI.lastKeyboardinput) {
  850. UI.keyboardinputReset();
  851. }
  852. var oldValue = UI.lastKeyboardinput;
  853. var newLen;
  854. try {
  855. // Try to check caret position since whitespace at the end
  856. // will not be considered by value.length in some browsers
  857. newLen = Math.max(event.target.selectionStart, newValue.length);
  858. } catch (err) {
  859. // selectionStart is undefined in Google Chrome
  860. newLen = newValue.length;
  861. }
  862. var oldLen = oldValue.length;
  863. var backspaces;
  864. var inputs = newLen - oldLen;
  865. if (inputs < 0) {
  866. backspaces = -inputs;
  867. } else {
  868. backspaces = 0;
  869. }
  870. // Compare the old string with the new to account for
  871. // text-corrections or other input that modify existing text
  872. var i;
  873. for (i = 0; i < Math.min(oldLen, newLen); i++) {
  874. if (newValue.charAt(i) != oldValue.charAt(i)) {
  875. inputs = newLen - i;
  876. backspaces = oldLen - i;
  877. break;
  878. }
  879. }
  880. // Send the key events
  881. for (i = 0; i < backspaces; i++) {
  882. UI.rfb.sendKey(XK_BackSpace);
  883. }
  884. for (i = newLen - inputs; i < newLen; i++) {
  885. UI.rfb.sendKey(newValue.charCodeAt(i));
  886. }
  887. // Control the text content length in the keyboardinput element
  888. if (newLen > 2 * UI.defaultKeyboardinputLen) {
  889. UI.keyboardinputReset();
  890. } else if (newLen < 1) {
  891. // There always have to be some text in the keyboardinput
  892. // element with which backspace can interact.
  893. UI.keyboardinputReset();
  894. // This sometimes causes the keyboard to disappear for a second
  895. // but it is required for the android keyboard to recognize that
  896. // text has been added to the field
  897. event.target.blur();
  898. // This has to be ran outside of the input handler in order to work
  899. setTimeout(function() { UI.keepKeyboard(); }, 0);
  900. } else {
  901. UI.lastKeyboardinput = newValue;
  902. }
  903. },
  904. keyInputBlur: function() {
  905. $D('showKeyboard').className = "noVNC_status_button";
  906. //Weird bug in iOS if you change keyboardVisible
  907. //here it does not actually occur so next time
  908. //you click keyboard icon it doesnt work.
  909. UI.hideKeyboardTimeout = setTimeout(function() { UI.setKeyboard(); },100);
  910. },
  911. showExtraKeys: function() {
  912. UI.keepKeyboard();
  913. if(UI.extraKeysVisible === false) {
  914. $D('toggleCtrlButton').style.display = "inline";
  915. $D('toggleAltButton').style.display = "inline";
  916. $D('sendTabButton').style.display = "inline";
  917. $D('sendEscButton').style.display = "inline";
  918. $D('showExtraKeysButton').className = "noVNC_status_button_selected";
  919. UI.extraKeysVisible = true;
  920. } else if(UI.extraKeysVisible === true) {
  921. $D('toggleCtrlButton').style.display = "";
  922. $D('toggleAltButton').style.display = "";
  923. $D('sendTabButton').style.display = "";
  924. $D('sendEscButton').style.display = "";
  925. $D('showExtraKeysButton').className = "noVNC_status_button";
  926. UI.extraKeysVisible = false;
  927. }
  928. },
  929. toggleCtrl: function() {
  930. UI.keepKeyboard();
  931. if(UI.ctrlOn === false) {
  932. UI.rfb.sendKey(XK_Control_L, true);
  933. $D('toggleCtrlButton').className = "noVNC_status_button_selected";
  934. UI.ctrlOn = true;
  935. } else if(UI.ctrlOn === true) {
  936. UI.rfb.sendKey(XK_Control_L, false);
  937. $D('toggleCtrlButton').className = "noVNC_status_button";
  938. UI.ctrlOn = false;
  939. }
  940. },
  941. toggleAlt: function() {
  942. UI.keepKeyboard();
  943. if(UI.altOn === false) {
  944. UI.rfb.sendKey(XK_Alt_L, true);
  945. $D('toggleAltButton').className = "noVNC_status_button_selected";
  946. UI.altOn = true;
  947. } else if(UI.altOn === true) {
  948. UI.rfb.sendKey(XK_Alt_L, false);
  949. $D('toggleAltButton').className = "noVNC_status_button";
  950. UI.altOn = false;
  951. }
  952. },
  953. sendTab: function() {
  954. UI.keepKeyboard();
  955. UI.rfb.sendKey(XK_Tab);
  956. },
  957. sendEsc: function() {
  958. UI.keepKeyboard();
  959. UI.rfb.sendKey(XK_Escape);
  960. },
  961. setKeyboard: function() {
  962. UI.keyboardVisible = false;
  963. },
  964. //Helper to add options to dropdown.
  965. addOption: function(selectbox, text, value) {
  966. var optn = document.createElement("OPTION");
  967. optn.text = text;
  968. optn.value = value;
  969. selectbox.options.add(optn);
  970. },
  971. setBarPosition: function() {
  972. $D('noVNC-control-bar').style.top = (window.pageYOffset) + 'px';
  973. $D('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
  974. var vncwidth = $D('noVNC_screen').style.offsetWidth;
  975. $D('noVNC-control-bar').style.width = vncwidth + 'px';
  976. }
  977. };
  978. })();