ui.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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.hideKeyboard;
  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 = function() { UI.rfb.xvpShutdown(); },
  186. $D("xvpRebootButton").onclick = function() { UI.rfb.xvpReboot(); },
  187. $D("xvpResetButton").onclick = function() { UI.rfb.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. // Display the desktop name in the document title
  656. updateDocumentTitle: function(rfb, name) {
  657. document.title = name + " - noVNC";
  658. },
  659. clipReceive: function(rfb, text) {
  660. Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
  661. $D('noVNC_clipboard_text').value = text;
  662. Util.Debug("<< UI.clipReceive");
  663. },
  664. displayBlur: function() {
  665. if (!UI.rfb) return;
  666. UI.rfb.get_keyboard().set_focused(false);
  667. UI.rfb.get_mouse().set_focused(false);
  668. },
  669. displayFocus: function() {
  670. if (!UI.rfb) return;
  671. UI.rfb.get_keyboard().set_focused(true);
  672. UI.rfb.get_mouse().set_focused(true);
  673. },
  674. clipClear: function() {
  675. $D('noVNC_clipboard_text').value = "";
  676. UI.rfb.clipboardPasteFrom("");
  677. },
  678. clipSend: function() {
  679. var text = $D('noVNC_clipboard_text').value;
  680. Util.Debug(">> UI.clipSend: " + text.substr(0,40) + "...");
  681. UI.rfb.clipboardPasteFrom(text);
  682. Util.Debug("<< UI.clipSend");
  683. },
  684. // Apply remote resizing or local scaling
  685. applyResizeMode: function() {
  686. if (!UI.rfb) return;
  687. var screen = UI.screenSize();
  688. if (screen && UI.rfb_state === 'normal' && UI.rfb.get_display()) {
  689. var display = UI.rfb.get_display();
  690. var resizeMode = UI.getSetting('resize');
  691. if (resizeMode === 'remote') {
  692. // Request changing the resolution of the remote display to
  693. // the size of the local browser viewport.
  694. // In order to not send multiple requests before the browser-resize
  695. // is finished we wait 0.5 seconds before sending the request.
  696. clearTimeout(UI.resizeTimeout);
  697. UI.resizeTimeout = setTimeout(function(){
  698. // Limit the viewport to the size of the browser window
  699. display.set_maxWidth(screen.w);
  700. display.set_maxHeight(screen.h);
  701. Util.Debug('Attempting setDesktopSize(' +
  702. screen.w + ', ' + screen.h + ')');
  703. // Request a remote size covering the viewport
  704. UI.rfb.setDesktopSize(screen.w, screen.h);
  705. }, 500);
  706. } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
  707. var downscaleOnly = resizeMode === 'downscale';
  708. var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
  709. UI.rfb.get_mouse().set_scale(scaleRatio);
  710. Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
  711. }
  712. }
  713. },
  714. // The screen is always the same size as the available viewport
  715. // in the browser window minus the height of the control bar
  716. screenSize: function() {
  717. var screen = $D('noVNC_screen');
  718. // Hide the scrollbars until the size is calculated
  719. screen.style.overflow = "hidden";
  720. var pos = Util.getPosition(screen);
  721. var w = pos.width;
  722. var h = pos.height;
  723. screen.style.overflow = "visible";
  724. if (isNaN(w) || isNaN(h)) {
  725. return false;
  726. } else {
  727. return {w: w, h: h};
  728. }
  729. },
  730. // Normally we only apply the current resize mode after a window resize
  731. // event. This means that when a new connection is opened, there is no
  732. // resize mode active.
  733. // We have to wait until the first FBU because this is where the client
  734. // will find the supported encodings of the server. Some calls later in
  735. // the chain is dependant on knowing the server-capabilities.
  736. initialResize: function(rfb, fbu) {
  737. UI.applyResizeMode();
  738. // After doing this once, we remove the callback.
  739. UI.rfb.set_onFBUComplete(function() { });
  740. },
  741. // Set and configure viewport clipping
  742. setViewClip: function(clip) {
  743. var display;
  744. if (UI.rfb) {
  745. display = UI.rfb.get_display();
  746. } else {
  747. UI.forceSetting('clip', clip);
  748. return;
  749. }
  750. var cur_clip = display.get_viewport();
  751. if (typeof(clip) !== 'boolean') {
  752. // Use current setting
  753. clip = UI.getSetting('clip');
  754. }
  755. if (clip && !cur_clip) {
  756. // Turn clipping on
  757. UI.updateSetting('clip', true);
  758. } else if (!clip && cur_clip) {
  759. // Turn clipping off
  760. UI.updateSetting('clip', false);
  761. display.set_viewport(false);
  762. // Disable max dimensions
  763. display.set_maxWidth(0);
  764. display.set_maxHeight(0);
  765. display.viewportChangeSize();
  766. }
  767. if (UI.getSetting('clip')) {
  768. // If clipping, update clipping settings
  769. display.set_viewport(true);
  770. var size = UI.screenSize();
  771. if (size) {
  772. display.set_maxWidth(size.w);
  773. display.set_maxHeight(size.h);
  774. // Hide potential scrollbars that can skew the position
  775. $D('noVNC_screen').style.overflow = "hidden";
  776. // The x position marks the left margin of the canvas,
  777. // remove the margin from both sides to keep it centered
  778. var new_w = size.w - (2 * Util.getPosition($D('noVNC_canvas')).x);
  779. $D('noVNC_screen').style.overflow = "visible";
  780. display.viewportChangeSize(new_w, size.h);
  781. }
  782. }
  783. },
  784. // Handle special cases where clipping is forced on/off or locked
  785. enableDisableViewClip: function() {
  786. var resizeElem = $D('noVNC_resize');
  787. var connected = UI.rfb && UI.rfb_state === 'normal';
  788. if (UI.isSafari) {
  789. // Safari auto-hides the scrollbars which makes them
  790. // impossible to use in most cases
  791. UI.setViewClip(true);
  792. $D('noVNC_clip').disabled = true;
  793. } else if (resizeElem.value === 'downscale' || resizeElem.value === 'scale') {
  794. // Disable clipping if we are scaling
  795. UI.setViewClip(false);
  796. $D('noVNC_clip').disabled = true;
  797. } else if (document.msFullscreenElement) {
  798. // The browser is IE and we are in fullscreen mode.
  799. // - We need to force clipping while in fullscreen since
  800. // scrollbars doesn't work.
  801. UI.popupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
  802. UI.rememberedClipSetting = UI.getSetting('clip');
  803. UI.setViewClip(true);
  804. $D('noVNC_clip').disabled = true;
  805. } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) {
  806. // Restore view clip to what it was before fullscreen on IE
  807. UI.setViewClip(UI.rememberedClipSetting);
  808. $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
  809. } else {
  810. $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
  811. if (UI.isTouchDevice) {
  812. UI.setViewClip(true);
  813. }
  814. }
  815. },
  816. // Update the viewport drag state
  817. updateViewDrag: function(drag) {
  818. if (!UI.rfb) return;
  819. var viewDragButton = $D('noVNC_view_drag_button');
  820. // Check if viewport drag is possible. It is only possible
  821. // if the remote display is clipping the client display.
  822. if (UI.rfb_state === 'normal' &&
  823. UI.rfb.get_display().get_viewport() &&
  824. UI.rfb.get_display().clippingDisplay()) {
  825. viewDragButton.style.display = "inline";
  826. viewDragButton.disabled = false;
  827. } else {
  828. // The size of the remote display is the same or smaller
  829. // than the client display. Make sure viewport drag isn't
  830. // active when it can't be used.
  831. if (UI.rfb.get_viewportDrag) {
  832. viewDragButton.className = "noVNC_status_button";
  833. UI.rfb.set_viewportDrag(false);
  834. }
  835. // The button is disabled instead of hidden on touch devices
  836. if (UI.rfb_state === 'normal' && UI.isTouchDevice) {
  837. viewDragButton.style.display = "inline";
  838. viewDragButton.disabled = true;
  839. } else {
  840. viewDragButton.style.display = "none";
  841. }
  842. return;
  843. }
  844. if (typeof(drag) !== "undefined" &&
  845. typeof(drag) !== "object") {
  846. if (drag) {
  847. viewDragButton.className = "noVNC_status_button_selected";
  848. UI.rfb.set_viewportDrag(true);
  849. } else {
  850. viewDragButton.className = "noVNC_status_button";
  851. UI.rfb.set_viewportDrag(false);
  852. }
  853. }
  854. },
  855. toggleViewDrag: function() {
  856. if (!UI.rfb) return;
  857. var viewDragButton = $D('noVNC_view_drag_button');
  858. if (UI.rfb.get_viewportDrag()) {
  859. viewDragButton.className = "noVNC_status_button";
  860. UI.rfb.set_viewportDrag(false);
  861. } else {
  862. viewDragButton.className = "noVNC_status_button_selected";
  863. UI.rfb.set_viewportDrag(true);
  864. }
  865. },
  866. setMouseButton: function(num) {
  867. if (typeof num === 'undefined') {
  868. // Disable mouse buttons
  869. num = -1;
  870. }
  871. if (UI.rfb) {
  872. UI.rfb.get_mouse().set_touchButton(num);
  873. }
  874. var blist = [0, 1,2,4];
  875. for (var b = 0; b < blist.length; b++) {
  876. var button = $D('noVNC_mouse_button' + blist[b]);
  877. if (blist[b] === num) {
  878. button.style.display = "";
  879. } else {
  880. button.style.display = "none";
  881. }
  882. }
  883. },
  884. // On touch devices, show the OS keyboard
  885. showKeyboard: function() {
  886. var kbi = $D('keyboardinput');
  887. var skb = $D('showKeyboard');
  888. var l = kbi.value.length;
  889. if(UI.keyboardVisible === false) {
  890. kbi.focus();
  891. try { kbi.setSelectionRange(l, l); } // Move the caret to the end
  892. catch (err) {} // setSelectionRange is undefined in Google Chrome
  893. UI.keyboardVisible = true;
  894. skb.className = "noVNC_status_button_selected";
  895. } else if(UI.keyboardVisible === true) {
  896. kbi.blur();
  897. skb.className = "noVNC_status_button";
  898. UI.keyboardVisible = false;
  899. }
  900. },
  901. hideKeyboard: function() {
  902. $D('showKeyboard').className = "noVNC_status_button";
  903. //Weird bug in iOS if you change keyboardVisible
  904. //here it does not actually occur so next time
  905. //you click keyboard icon it doesnt work.
  906. UI.hideKeyboardTimeout = setTimeout(function() {
  907. UI.keyboardVisible = false;
  908. },100);
  909. },
  910. keepKeyboard: function() {
  911. clearTimeout(UI.hideKeyboardTimeout);
  912. if(UI.keyboardVisible === true) {
  913. $D('keyboardinput').focus();
  914. $D('showKeyboard').className = "noVNC_status_button_selected";
  915. } else if(UI.keyboardVisible === false) {
  916. $D('keyboardinput').blur();
  917. $D('showKeyboard').className = "noVNC_status_button";
  918. }
  919. },
  920. keyboardinputReset: function() {
  921. var kbi = $D('keyboardinput');
  922. kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
  923. UI.lastKeyboardinput = kbi.value;
  924. },
  925. // When normal keyboard events are left uncought, use the input events from
  926. // the keyboardinput element instead and generate the corresponding key events.
  927. // This code is required since some browsers on Android are inconsistent in
  928. // sending keyCodes in the normal keyboard events when using on screen keyboards.
  929. keyInput: function(event) {
  930. if (!UI.rfb) return;
  931. var newValue = event.target.value;
  932. if (!UI.lastKeyboardinput) {
  933. UI.keyboardinputReset();
  934. }
  935. var oldValue = UI.lastKeyboardinput;
  936. var newLen;
  937. try {
  938. // Try to check caret position since whitespace at the end
  939. // will not be considered by value.length in some browsers
  940. newLen = Math.max(event.target.selectionStart, newValue.length);
  941. } catch (err) {
  942. // selectionStart is undefined in Google Chrome
  943. newLen = newValue.length;
  944. }
  945. var oldLen = oldValue.length;
  946. var backspaces;
  947. var inputs = newLen - oldLen;
  948. if (inputs < 0) {
  949. backspaces = -inputs;
  950. } else {
  951. backspaces = 0;
  952. }
  953. // Compare the old string with the new to account for
  954. // text-corrections or other input that modify existing text
  955. var i;
  956. for (i = 0; i < Math.min(oldLen, newLen); i++) {
  957. if (newValue.charAt(i) != oldValue.charAt(i)) {
  958. inputs = newLen - i;
  959. backspaces = oldLen - i;
  960. break;
  961. }
  962. }
  963. // Send the key events
  964. for (i = 0; i < backspaces; i++) {
  965. UI.rfb.sendKey(XK_BackSpace);
  966. }
  967. for (i = newLen - inputs; i < newLen; i++) {
  968. UI.rfb.sendKey(newValue.charCodeAt(i));
  969. }
  970. // Control the text content length in the keyboardinput element
  971. if (newLen > 2 * UI.defaultKeyboardinputLen) {
  972. UI.keyboardinputReset();
  973. } else if (newLen < 1) {
  974. // There always have to be some text in the keyboardinput
  975. // element with which backspace can interact.
  976. UI.keyboardinputReset();
  977. // This sometimes causes the keyboard to disappear for a second
  978. // but it is required for the android keyboard to recognize that
  979. // text has been added to the field
  980. event.target.blur();
  981. // This has to be ran outside of the input handler in order to work
  982. setTimeout(function() { UI.keepKeyboard(); }, 0);
  983. } else {
  984. UI.lastKeyboardinput = newValue;
  985. }
  986. },
  987. toggleExtraKeys: 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('toggleExtraKeysButton').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('toggleExtraKeysButton').className = "noVNC_status_button";
  1002. UI.extraKeysVisible = false;
  1003. }
  1004. },
  1005. sendEsc: function() {
  1006. UI.keepKeyboard();
  1007. UI.rfb.sendKey(XK_Escape);
  1008. },
  1009. sendTab: function() {
  1010. UI.keepKeyboard();
  1011. UI.rfb.sendKey(XK_Tab);
  1012. },
  1013. toggleCtrl: function() {
  1014. UI.keepKeyboard();
  1015. if(UI.ctrlOn === false) {
  1016. UI.rfb.sendKey(XK_Control_L, true);
  1017. $D('toggleCtrlButton').className = "noVNC_status_button_selected";
  1018. UI.ctrlOn = true;
  1019. } else if(UI.ctrlOn === true) {
  1020. UI.rfb.sendKey(XK_Control_L, false);
  1021. $D('toggleCtrlButton').className = "noVNC_status_button";
  1022. UI.ctrlOn = false;
  1023. }
  1024. },
  1025. toggleAlt: function() {
  1026. UI.keepKeyboard();
  1027. if(UI.altOn === false) {
  1028. UI.rfb.sendKey(XK_Alt_L, true);
  1029. $D('toggleAltButton').className = "noVNC_status_button_selected";
  1030. UI.altOn = true;
  1031. } else if(UI.altOn === true) {
  1032. UI.rfb.sendKey(XK_Alt_L, false);
  1033. $D('toggleAltButton').className = "noVNC_status_button";
  1034. UI.altOn = false;
  1035. }
  1036. },
  1037. sendCtrlAltDel: function() {
  1038. UI.rfb.sendCtrlAltDel();
  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_container').style.offsetWidth;
  1051. $D('noVNC-control-bar').style.width = vncwidth + 'px';
  1052. }
  1053. };
  1054. })();