ui.js 46 KB

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