ui.js 47 KB

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