ui.js 46 KB

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