ui.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. popupStatusOpen : false,
  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.updateViewDragButton();
  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.updateViewDragButton,
  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.setViewDrag;
  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.togglePopupStatusPanel;
  170. $D("noVNC_popup_status_panel").onclick = UI.togglePopupStatusPanel;
  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.enableDisableClip(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 panel
  303. togglePopupStatusPanel: function() {
  304. var psp = $D('noVNC_popup_status_panel');
  305. if (UI.popupStatusOpen === true) {
  306. psp.style.display = "none";
  307. UI.popupStatusOpen = false;
  308. } else {
  309. psp.innerHTML = $D('noVNC_status').innerHTML;
  310. psp.style.display = "block";
  311. psp.style.left = window.innerWidth/2 -
  312. parseInt(window.getComputedStyle(psp, false).width)/2 -30 + "px";
  313. UI.popupStatusOpen = true;
  314. }
  315. },
  316. // Show the XVP panel
  317. toggleXvpPanel: function() {
  318. // Close the description panel
  319. $D('noVNC_description').style.display = "none";
  320. // Close settings if open
  321. if (UI.settingsOpen === true) {
  322. UI.settingsApply();
  323. UI.closeSettingsMenu();
  324. }
  325. // Close connection settings if open
  326. if (UI.connSettingsOpen === true) {
  327. UI.toggleConnectPanel();
  328. }
  329. // Close popup status panel if open
  330. if (UI.popupStatusOpen === true) {
  331. UI.togglePopupStatusPanel();
  332. }
  333. // Close clipboard panel if open
  334. if (UI.clipboardOpen === true) {
  335. UI.toggleClipboardPanel();
  336. }
  337. // Toggle XVP panel
  338. if (UI.xvpOpen === true) {
  339. $D('noVNC_xvp').style.display = "none";
  340. $D('xvpButton').className = "noVNC_status_button";
  341. UI.xvpOpen = false;
  342. } else {
  343. $D('noVNC_xvp').style.display = "block";
  344. $D('xvpButton').className = "noVNC_status_button_selected";
  345. UI.xvpOpen = true;
  346. }
  347. },
  348. // Show the clipboard panel
  349. toggleClipboardPanel: function() {
  350. // Close the description panel
  351. $D('noVNC_description').style.display = "none";
  352. // Close settings if open
  353. if (UI.settingsOpen === true) {
  354. UI.settingsApply();
  355. UI.closeSettingsMenu();
  356. }
  357. // Close connection settings if open
  358. if (UI.connSettingsOpen === true) {
  359. UI.toggleConnectPanel();
  360. }
  361. // Close popup status panel if open
  362. if (UI.popupStatusOpen === true) {
  363. UI.togglePopupStatusPanel();
  364. }
  365. // Close XVP panel if open
  366. if (UI.xvpOpen === true) {
  367. UI.toggleXvpPanel();
  368. }
  369. // Toggle Clipboard Panel
  370. if (UI.clipboardOpen === true) {
  371. $D('noVNC_clipboard').style.display = "none";
  372. $D('clipboardButton').className = "noVNC_status_button";
  373. UI.clipboardOpen = false;
  374. } else {
  375. $D('noVNC_clipboard').style.display = "block";
  376. $D('clipboardButton').className = "noVNC_status_button_selected";
  377. UI.clipboardOpen = true;
  378. }
  379. },
  380. // Show the connection settings panel/menu
  381. toggleConnectPanel: function() {
  382. // Close the description panel
  383. $D('noVNC_description').style.display = "none";
  384. // Close connection settings if open
  385. if (UI.settingsOpen === true) {
  386. UI.settingsApply();
  387. UI.closeSettingsMenu();
  388. $D('connectButton').className = "noVNC_status_button";
  389. }
  390. // Close clipboard panel if open
  391. if (UI.clipboardOpen === true) {
  392. UI.toggleClipboardPanel();
  393. }
  394. // Close popup status panel if open
  395. if (UI.popupStatusOpen === true) {
  396. UI.togglePopupStatusPanel();
  397. }
  398. // Close XVP panel if open
  399. if (UI.xvpOpen === true) {
  400. UI.toggleXvpPanel();
  401. }
  402. // Toggle Connection Panel
  403. if (UI.connSettingsOpen === true) {
  404. $D('noVNC_controls').style.display = "none";
  405. $D('connectButton').className = "noVNC_status_button";
  406. UI.connSettingsOpen = false;
  407. UI.saveSetting('host');
  408. UI.saveSetting('port');
  409. //UI.saveSetting('password');
  410. } else {
  411. $D('noVNC_controls').style.display = "block";
  412. $D('connectButton').className = "noVNC_status_button_selected";
  413. UI.connSettingsOpen = true;
  414. $D('noVNC_host').focus();
  415. }
  416. },
  417. // Toggle the settings menu:
  418. // On open, settings are refreshed from saved cookies.
  419. // On close, settings are applied
  420. toggleSettingsPanel: function() {
  421. // Close the description panel
  422. $D('noVNC_description').style.display = "none";
  423. if (UI.settingsOpen) {
  424. UI.settingsApply();
  425. UI.closeSettingsMenu();
  426. } else {
  427. UI.updateSetting('encrypt');
  428. UI.updateSetting('true_color');
  429. if (Util.browserSupportsCursorURIs()) {
  430. UI.updateSetting('cursor');
  431. } else {
  432. UI.updateSetting('cursor', !UI.isTouchDevice);
  433. $D('noVNC_cursor').disabled = true;
  434. }
  435. UI.updateSetting('clip');
  436. UI.updateSetting('resize');
  437. UI.updateSetting('shared');
  438. UI.updateSetting('view_only');
  439. UI.updateSetting('path');
  440. UI.updateSetting('repeaterID');
  441. UI.updateSetting('stylesheet');
  442. UI.updateSetting('logging');
  443. UI.openSettingsMenu();
  444. }
  445. },
  446. // Open menu
  447. openSettingsMenu: function() {
  448. // Close the description panel
  449. $D('noVNC_description').style.display = "none";
  450. // Close clipboard panel if open
  451. if (UI.clipboardOpen === true) {
  452. UI.toggleClipboardPanel();
  453. }
  454. // Close connection settings if open
  455. if (UI.connSettingsOpen === true) {
  456. UI.toggleConnectPanel();
  457. }
  458. // Close popup status panel if open
  459. if (UI.popupStatusOpen === true) {
  460. UI.togglePopupStatusPanel();
  461. }
  462. // Close XVP panel if open
  463. if (UI.xvpOpen === true) {
  464. UI.toggleXvpPanel();
  465. }
  466. $D('noVNC_settings').style.display = "block";
  467. $D('settingsButton').className = "noVNC_status_button_selected";
  468. UI.settingsOpen = true;
  469. },
  470. // Close menu (without applying settings)
  471. closeSettingsMenu: function() {
  472. $D('noVNC_settings').style.display = "none";
  473. $D('settingsButton').className = "noVNC_status_button";
  474. UI.settingsOpen = false;
  475. },
  476. // Save/apply settings when 'Apply' button is pressed
  477. settingsApply: function() {
  478. //Util.Debug(">> settingsApply");
  479. UI.saveSetting('encrypt');
  480. UI.saveSetting('true_color');
  481. if (Util.browserSupportsCursorURIs()) {
  482. UI.saveSetting('cursor');
  483. }
  484. UI.saveSetting('resize');
  485. if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
  486. UI.forceSetting('clip', false);
  487. }
  488. UI.saveSetting('clip');
  489. UI.saveSetting('shared');
  490. UI.saveSetting('view_only');
  491. UI.saveSetting('path');
  492. UI.saveSetting('repeaterID');
  493. UI.saveSetting('stylesheet');
  494. UI.saveSetting('logging');
  495. // Settings with immediate (non-connected related) effect
  496. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  497. WebUtil.init_logging(UI.getSetting('logging'));
  498. UI.setViewClip();
  499. UI.setViewDrag(UI.rfb && UI.rfb.get_viewportDrag());
  500. //Util.Debug("<< settingsApply");
  501. },
  502. setPassword: function() {
  503. UI.rfb.sendPassword($D('noVNC_password').value);
  504. //Reset connect button.
  505. $D('noVNC_connect_button').value = "Connect";
  506. $D('noVNC_connect_button').onclick = UI.Connect;
  507. //Hide connection panel.
  508. UI.toggleConnectPanel();
  509. return false;
  510. },
  511. sendCtrlAltDel: function() {
  512. UI.rfb.sendCtrlAltDel();
  513. },
  514. xvpShutdown: function() {
  515. UI.rfb.xvpShutdown();
  516. },
  517. xvpReboot: function() {
  518. UI.rfb.xvpReboot();
  519. },
  520. xvpReset: function() {
  521. UI.rfb.xvpReset();
  522. },
  523. setMouseButton: function(num) {
  524. if (typeof num === 'undefined') {
  525. // Disable mouse buttons
  526. num = -1;
  527. }
  528. if (UI.rfb) {
  529. UI.rfb.get_mouse().set_touchButton(num);
  530. }
  531. var blist = [0, 1,2,4];
  532. for (var b = 0; b < blist.length; b++) {
  533. var button = $D('noVNC_mouse_button' + blist[b]);
  534. if (blist[b] === num) {
  535. button.style.display = "";
  536. } else {
  537. button.style.display = "none";
  538. }
  539. }
  540. },
  541. updateState: function(rfb, state, oldstate, msg) {
  542. UI.rfb_state = state;
  543. var klass;
  544. switch (state) {
  545. case 'failed':
  546. case 'fatal':
  547. klass = "noVNC_status_error";
  548. break;
  549. case 'normal':
  550. klass = "noVNC_status_normal";
  551. break;
  552. case 'disconnected':
  553. $D('noVNC_logo').style.display = "block";
  554. $D('noVNC_container').style.display = "none";
  555. /* falls through */
  556. case 'loaded':
  557. klass = "noVNC_status_normal";
  558. break;
  559. case 'password':
  560. UI.toggleConnectPanel();
  561. $D('noVNC_connect_button').value = "Send Password";
  562. $D('noVNC_connect_button').onclick = UI.setPassword;
  563. $D('noVNC_password').focus();
  564. klass = "noVNC_status_warn";
  565. break;
  566. default:
  567. klass = "noVNC_status_warn";
  568. break;
  569. }
  570. if (typeof(msg) !== 'undefined') {
  571. $D('noVNC-control-bar').setAttribute("class", klass);
  572. $D('noVNC_status').innerHTML = msg;
  573. }
  574. UI.updateVisualState();
  575. },
  576. // Disable/enable controls depending on connection state
  577. updateVisualState: function() {
  578. var connected = UI.rfb && UI.rfb_state === 'normal';
  579. //Util.Debug(">> updateVisualState");
  580. $D('noVNC_encrypt').disabled = connected;
  581. $D('noVNC_true_color').disabled = connected;
  582. if (Util.browserSupportsCursorURIs()) {
  583. $D('noVNC_cursor').disabled = connected;
  584. } else {
  585. UI.updateSetting('cursor', !UI.isTouchDevice);
  586. $D('noVNC_cursor').disabled = true;
  587. }
  588. UI.enableDisableClip(connected);
  589. $D('noVNC_resize').disabled = connected;
  590. $D('noVNC_shared').disabled = connected;
  591. $D('noVNC_view_only').disabled = connected;
  592. $D('noVNC_path').disabled = connected;
  593. $D('noVNC_repeaterID').disabled = connected;
  594. if (connected) {
  595. UI.setViewClip();
  596. UI.setMouseButton(1);
  597. $D('clipboardButton').style.display = "inline";
  598. $D('showKeyboard').style.display = "inline";
  599. $D('noVNC_extra_keys').style.display = "";
  600. $D('sendCtrlAltDelButton').style.display = "inline";
  601. } else {
  602. UI.setMouseButton();
  603. $D('clipboardButton').style.display = "none";
  604. $D('showKeyboard').style.display = "none";
  605. $D('noVNC_extra_keys').style.display = "none";
  606. $D('sendCtrlAltDelButton').style.display = "none";
  607. UI.updateXvpVisualState(0);
  608. }
  609. // State change disables viewport dragging.
  610. // It is enabled (toggled) by direct click on the button
  611. UI.setViewDrag(false);
  612. UI.updateViewDragButton();
  613. switch (UI.rfb_state) {
  614. case 'fatal':
  615. case 'failed':
  616. case 'disconnected':
  617. $D('connectButton').style.display = "";
  618. $D('disconnectButton').style.display = "none";
  619. UI.connSettingsOpen = false;
  620. UI.toggleConnectPanel();
  621. break;
  622. case 'loaded':
  623. $D('connectButton').style.display = "";
  624. $D('disconnectButton').style.display = "none";
  625. break;
  626. default:
  627. $D('connectButton').style.display = "none";
  628. $D('disconnectButton').style.display = "";
  629. break;
  630. }
  631. //Util.Debug("<< updateVisualState");
  632. },
  633. // Disable/enable XVP button
  634. updateXvpVisualState: function(ver) {
  635. if (ver >= 1) {
  636. $D('xvpButton').style.display = 'inline';
  637. } else {
  638. $D('xvpButton').style.display = 'none';
  639. // Close XVP panel if open
  640. if (UI.xvpOpen === true) {
  641. UI.toggleXvpPanel();
  642. }
  643. }
  644. },
  645. enableDisableClip: function (connected) {
  646. var resizeElem = $D('noVNC_resize');
  647. if (resizeElem.value === 'downscale' || resizeElem.value === 'scale') {
  648. UI.forceSetting('clip', false);
  649. $D('noVNC_clip').disabled = true;
  650. } else {
  651. $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
  652. if (UI.isTouchDevice) {
  653. UI.forceSetting('clip', true);
  654. }
  655. }
  656. },
  657. // This resize can not be done until we know from the first Frame Buffer Update
  658. // if it is supported or not.
  659. // The resize is needed to make sure the server desktop size is updated to the
  660. // corresponding size of the current local window when reconnecting to an
  661. // existing session.
  662. FBUComplete: function(rfb, fbu) {
  663. UI.onresize();
  664. UI.rfb.set_onFBUComplete(function() { });
  665. },
  666. // Display the desktop name in the document title
  667. updateDocumentTitle: function(rfb, name) {
  668. document.title = name + " - noVNC";
  669. },
  670. clipReceive: function(rfb, text) {
  671. Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
  672. $D('noVNC_clipboard_text').value = text;
  673. Util.Debug("<< UI.clipReceive");
  674. },
  675. connect: function() {
  676. UI.closeSettingsMenu();
  677. UI.toggleConnectPanel();
  678. var host = $D('noVNC_host').value;
  679. var port = $D('noVNC_port').value;
  680. var password = $D('noVNC_password').value;
  681. var path = $D('noVNC_path').value;
  682. if ((!host) || (!port)) {
  683. throw new Error("Must set host and port");
  684. }
  685. if (!UI.initRFB()) return;
  686. UI.rfb.set_encrypt(UI.getSetting('encrypt'));
  687. UI.rfb.set_true_color(UI.getSetting('true_color'));
  688. UI.rfb.set_local_cursor(UI.getSetting('cursor'));
  689. UI.rfb.set_shared(UI.getSetting('shared'));
  690. UI.rfb.set_view_only(UI.getSetting('view_only'));
  691. UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
  692. UI.rfb.connect(host, port, password, path);
  693. //Close dialog.
  694. setTimeout(UI.setBarPosition, 100);
  695. $D('noVNC_logo').style.display = "none";
  696. $D('noVNC_container').style.display = "inline";
  697. },
  698. disconnect: function() {
  699. UI.closeSettingsMenu();
  700. UI.rfb.disconnect();
  701. // Restore the callback used for initial resize
  702. UI.rfb.set_onFBUComplete(UI.FBUComplete);
  703. $D('noVNC_logo').style.display = "block";
  704. $D('noVNC_container').style.display = "none";
  705. // Don't display the connection settings until we're actually disconnected
  706. },
  707. displayBlur: function() {
  708. if (!UI.rfb) return;
  709. UI.rfb.get_keyboard().set_focused(false);
  710. UI.rfb.get_mouse().set_focused(false);
  711. },
  712. displayFocus: function() {
  713. if (!UI.rfb) return;
  714. UI.rfb.get_keyboard().set_focused(true);
  715. UI.rfb.get_mouse().set_focused(true);
  716. },
  717. clipClear: function() {
  718. $D('noVNC_clipboard_text').value = "";
  719. UI.rfb.clipboardPasteFrom("");
  720. },
  721. clipSend: function() {
  722. var text = $D('noVNC_clipboard_text').value;
  723. Util.Debug(">> UI.clipSend: " + text.substr(0,40) + "...");
  724. UI.rfb.clipboardPasteFrom(text);
  725. Util.Debug("<< UI.clipSend");
  726. },
  727. // Enable/disable and configure viewport clipping
  728. setViewClip: function(clip) {
  729. var display;
  730. if (UI.rfb) {
  731. display = UI.rfb.get_display();
  732. } else {
  733. return;
  734. }
  735. var cur_clip = display.get_viewport();
  736. if (typeof(clip) !== 'boolean') {
  737. // Use current setting
  738. clip = UI.getSetting('clip');
  739. }
  740. if (clip && !cur_clip) {
  741. // Turn clipping on
  742. UI.updateSetting('clip', true);
  743. } else if (!clip && cur_clip) {
  744. // Turn clipping off
  745. UI.updateSetting('clip', false);
  746. display.set_viewport(false);
  747. display.set_maxWidth(0);
  748. display.set_maxHeight(0);
  749. display.viewportChangeSize();
  750. }
  751. if (UI.getSetting('clip')) {
  752. // If clipping, update clipping settings
  753. display.set_viewport(true);
  754. var size = UI.getCanvasLimit();
  755. if (size) {
  756. display.set_maxWidth(size.w);
  757. display.set_maxHeight(size.h);
  758. // Hide potential scrollbars that can skew the position
  759. $D('noVNC_container').style.overflow = "hidden";
  760. // The x position marks the left margin of the canvas,
  761. // remove the margin from both sides to keep it centered
  762. var new_w = size.w - (2 * Util.getPosition($D('noVNC_canvas')).x);
  763. $D('noVNC_container').style.overflow = "visible";
  764. display.viewportChangeSize(new_w, size.h);
  765. }
  766. }
  767. },
  768. // Toggle/set/unset the viewport drag/move button
  769. setViewDrag: function(drag) {
  770. if (!UI.rfb) return;
  771. if (typeof(drag) === "undefined" ||
  772. typeof(drag) === "object") {
  773. // If not specified, then toggle
  774. drag = !UI.rfb.get_viewportDrag();
  775. }
  776. var vmb = $D('noVNC_view_drag_button');
  777. if (drag) {
  778. vmb.className = "noVNC_status_button_selected";
  779. UI.rfb.set_viewportDrag(true);
  780. } else {
  781. vmb.className = "noVNC_status_button";
  782. UI.rfb.set_viewportDrag(false);
  783. }
  784. },
  785. updateViewDragButton: function() {
  786. var vmb = $D('noVNC_view_drag_button');
  787. if (UI.rfb_state === 'normal' &&
  788. UI.rfb.get_display().get_viewport() &&
  789. UI.rfb.get_display().clippingDisplay()) {
  790. // Enable the viewport drag button
  791. vmb.style.display = "inline";
  792. vmb.disabled = false;
  793. } else if (UI.rfb_state === 'normal' &&
  794. UI.isTouchDevice) {
  795. // Disable the viewport drag button
  796. vmb.style.display = "inline";
  797. vmb.disabled = true;
  798. } else {
  799. // Hide the viewport drag button
  800. vmb.style.display = "none";
  801. }
  802. },
  803. // On touch devices, show the OS keyboard
  804. showKeyboard: function() {
  805. var kbi = $D('keyboardinput');
  806. var skb = $D('showKeyboard');
  807. var l = kbi.value.length;
  808. if(UI.keyboardVisible === false) {
  809. kbi.focus();
  810. try { kbi.setSelectionRange(l, l); } // Move the caret to the end
  811. catch (err) {} // setSelectionRange is undefined in Google Chrome
  812. UI.keyboardVisible = true;
  813. skb.className = "noVNC_status_button_selected";
  814. } else if(UI.keyboardVisible === true) {
  815. kbi.blur();
  816. skb.className = "noVNC_status_button";
  817. UI.keyboardVisible = false;
  818. }
  819. },
  820. keepKeyboard: function() {
  821. clearTimeout(UI.hideKeyboardTimeout);
  822. if(UI.keyboardVisible === true) {
  823. $D('keyboardinput').focus();
  824. $D('showKeyboard').className = "noVNC_status_button_selected";
  825. } else if(UI.keyboardVisible === false) {
  826. $D('keyboardinput').blur();
  827. $D('showKeyboard').className = "noVNC_status_button";
  828. }
  829. },
  830. keyboardinputReset: function() {
  831. var kbi = $D('keyboardinput');
  832. kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
  833. UI.lastKeyboardinput = kbi.value;
  834. },
  835. // When normal keyboard events are left uncought, use the input events from
  836. // the keyboardinput element instead and generate the corresponding key events.
  837. // This code is required since some browsers on Android are inconsistent in
  838. // sending keyCodes in the normal keyboard events when using on screen keyboards.
  839. keyInput: function(event) {
  840. if (!UI.rfb) return;
  841. var newValue = event.target.value;
  842. if (!UI.lastKeyboardinput) {
  843. UI.keyboardinputReset();
  844. }
  845. var oldValue = UI.lastKeyboardinput;
  846. var newLen;
  847. try {
  848. // Try to check caret position since whitespace at the end
  849. // will not be considered by value.length in some browsers
  850. newLen = Math.max(event.target.selectionStart, newValue.length);
  851. } catch (err) {
  852. // selectionStart is undefined in Google Chrome
  853. newLen = newValue.length;
  854. }
  855. var oldLen = oldValue.length;
  856. var backspaces;
  857. var inputs = newLen - oldLen;
  858. if (inputs < 0) {
  859. backspaces = -inputs;
  860. } else {
  861. backspaces = 0;
  862. }
  863. // Compare the old string with the new to account for
  864. // text-corrections or other input that modify existing text
  865. var i;
  866. for (i = 0; i < Math.min(oldLen, newLen); i++) {
  867. if (newValue.charAt(i) != oldValue.charAt(i)) {
  868. inputs = newLen - i;
  869. backspaces = oldLen - i;
  870. break;
  871. }
  872. }
  873. // Send the key events
  874. for (i = 0; i < backspaces; i++) {
  875. UI.rfb.sendKey(XK_BackSpace);
  876. }
  877. for (i = newLen - inputs; i < newLen; i++) {
  878. UI.rfb.sendKey(newValue.charCodeAt(i));
  879. }
  880. // Control the text content length in the keyboardinput element
  881. if (newLen > 2 * UI.defaultKeyboardinputLen) {
  882. UI.keyboardinputReset();
  883. } else if (newLen < 1) {
  884. // There always have to be some text in the keyboardinput
  885. // element with which backspace can interact.
  886. UI.keyboardinputReset();
  887. // This sometimes causes the keyboard to disappear for a second
  888. // but it is required for the android keyboard to recognize that
  889. // text has been added to the field
  890. event.target.blur();
  891. // This has to be ran outside of the input handler in order to work
  892. setTimeout(function() { UI.keepKeyboard(); }, 0);
  893. } else {
  894. UI.lastKeyboardinput = newValue;
  895. }
  896. },
  897. keyInputBlur: function() {
  898. $D('showKeyboard').className = "noVNC_status_button";
  899. //Weird bug in iOS if you change keyboardVisible
  900. //here it does not actually occur so next time
  901. //you click keyboard icon it doesnt work.
  902. UI.hideKeyboardTimeout = setTimeout(function() { UI.setKeyboard(); },100);
  903. },
  904. showExtraKeys: function() {
  905. UI.keepKeyboard();
  906. if(UI.extraKeysVisible === false) {
  907. $D('toggleCtrlButton').style.display = "inline";
  908. $D('toggleAltButton').style.display = "inline";
  909. $D('sendTabButton').style.display = "inline";
  910. $D('sendEscButton').style.display = "inline";
  911. $D('showExtraKeysButton').className = "noVNC_status_button_selected";
  912. UI.extraKeysVisible = true;
  913. } else if(UI.extraKeysVisible === true) {
  914. $D('toggleCtrlButton').style.display = "";
  915. $D('toggleAltButton').style.display = "";
  916. $D('sendTabButton').style.display = "";
  917. $D('sendEscButton').style.display = "";
  918. $D('showExtraKeysButton').className = "noVNC_status_button";
  919. UI.extraKeysVisible = false;
  920. }
  921. },
  922. toggleCtrl: function() {
  923. UI.keepKeyboard();
  924. if(UI.ctrlOn === false) {
  925. UI.rfb.sendKey(XK_Control_L, true);
  926. $D('toggleCtrlButton').className = "noVNC_status_button_selected";
  927. UI.ctrlOn = true;
  928. } else if(UI.ctrlOn === true) {
  929. UI.rfb.sendKey(XK_Control_L, false);
  930. $D('toggleCtrlButton').className = "noVNC_status_button";
  931. UI.ctrlOn = false;
  932. }
  933. },
  934. toggleAlt: function() {
  935. UI.keepKeyboard();
  936. if(UI.altOn === false) {
  937. UI.rfb.sendKey(XK_Alt_L, true);
  938. $D('toggleAltButton').className = "noVNC_status_button_selected";
  939. UI.altOn = true;
  940. } else if(UI.altOn === true) {
  941. UI.rfb.sendKey(XK_Alt_L, false);
  942. $D('toggleAltButton').className = "noVNC_status_button";
  943. UI.altOn = false;
  944. }
  945. },
  946. sendTab: function() {
  947. UI.keepKeyboard();
  948. UI.rfb.sendKey(XK_Tab);
  949. },
  950. sendEsc: function() {
  951. UI.keepKeyboard();
  952. UI.rfb.sendKey(XK_Escape);
  953. },
  954. setKeyboard: function() {
  955. UI.keyboardVisible = false;
  956. },
  957. //Helper to add options to dropdown.
  958. addOption: function(selectbox, text, value) {
  959. var optn = document.createElement("OPTION");
  960. optn.text = text;
  961. optn.value = value;
  962. selectbox.options.add(optn);
  963. },
  964. setBarPosition: function() {
  965. $D('noVNC-control-bar').style.top = (window.pageYOffset) + 'px';
  966. $D('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
  967. var vncwidth = $D('noVNC_screen').style.offsetWidth;
  968. $D('noVNC-control-bar').style.width = vncwidth + 'px';
  969. }
  970. };
  971. })();