ui.js 46 KB

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