ui.js 46 KB

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