ui.js 45 KB

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