ui.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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.updateXvpVisualState,
  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.updateXvpVisualState(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. // Show the clipboard panel
  472. toggleClipboardPanel: function() {
  473. // Close the description panel
  474. $D('noVNC_description').style.display = "none";
  475. // Close settings if open
  476. if (UI.settingsOpen === true) {
  477. UI.settingsApply();
  478. UI.closeSettingsMenu();
  479. }
  480. // Close connection settings if open
  481. if (UI.connSettingsOpen === true) {
  482. UI.toggleConnectPanel();
  483. }
  484. // Close XVP panel if open
  485. if (UI.xvpOpen === true) {
  486. UI.toggleXvpPanel();
  487. }
  488. // Toggle Clipboard Panel
  489. if (UI.clipboardOpen === true) {
  490. $D('noVNC_clipboard').style.display = "none";
  491. $D('clipboardButton').className = "noVNC_status_button";
  492. UI.clipboardOpen = false;
  493. } else {
  494. $D('noVNC_clipboard').style.display = "block";
  495. $D('clipboardButton').className = "noVNC_status_button_selected";
  496. UI.clipboardOpen = true;
  497. }
  498. },
  499. // Toggle fullscreen mode
  500. toggleFullscreen: function() {
  501. if (document.fullscreenElement || // alternative standard method
  502. document.mozFullScreenElement || // currently working methods
  503. document.webkitFullscreenElement ||
  504. document.msFullscreenElement) {
  505. if (document.exitFullscreen) {
  506. document.exitFullscreen();
  507. } else if (document.mozCancelFullScreen) {
  508. document.mozCancelFullScreen();
  509. } else if (document.webkitExitFullscreen) {
  510. document.webkitExitFullscreen();
  511. } else if (document.msExitFullscreen) {
  512. document.msExitFullscreen();
  513. }
  514. } else {
  515. if (document.documentElement.requestFullscreen) {
  516. document.documentElement.requestFullscreen();
  517. } else if (document.documentElement.mozRequestFullScreen) {
  518. document.documentElement.mozRequestFullScreen();
  519. } else if (document.documentElement.webkitRequestFullscreen) {
  520. document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  521. } else if (document.body.msRequestFullscreen) {
  522. document.body.msRequestFullscreen();
  523. }
  524. }
  525. UI.enableDisableViewClip();
  526. UI.updateFullscreenButton();
  527. },
  528. updateFullscreenButton: function() {
  529. if (document.fullscreenElement || // alternative standard method
  530. document.mozFullScreenElement || // currently working methods
  531. document.webkitFullscreenElement ||
  532. document.msFullscreenElement ) {
  533. $D('fullscreenButton').className = "noVNC_status_button_selected";
  534. } else {
  535. $D('fullscreenButton').className = "noVNC_status_button";
  536. }
  537. },
  538. // Show the connection settings panel/menu
  539. toggleConnectPanel: function() {
  540. // Close the description panel
  541. $D('noVNC_description').style.display = "none";
  542. // Close connection settings if open
  543. if (UI.settingsOpen === true) {
  544. UI.settingsApply();
  545. UI.closeSettingsMenu();
  546. $D('connectButton').className = "noVNC_status_button";
  547. }
  548. // Close clipboard panel if open
  549. if (UI.clipboardOpen === true) {
  550. UI.toggleClipboardPanel();
  551. }
  552. // Close XVP panel if open
  553. if (UI.xvpOpen === true) {
  554. UI.toggleXvpPanel();
  555. }
  556. // Toggle Connection Panel
  557. if (UI.connSettingsOpen === true) {
  558. $D('noVNC_controls').style.display = "none";
  559. $D('connectButton').className = "noVNC_status_button";
  560. UI.connSettingsOpen = false;
  561. UI.saveSetting('host');
  562. UI.saveSetting('port');
  563. UI.saveSetting('token');
  564. //UI.saveSetting('password');
  565. } else {
  566. $D('noVNC_controls').style.display = "block";
  567. $D('connectButton').className = "noVNC_status_button_selected";
  568. UI.connSettingsOpen = true;
  569. $D('noVNC_host').focus();
  570. }
  571. },
  572. // Save/apply settings when 'Apply' button is pressed
  573. settingsApply: function() {
  574. //Util.Debug(">> settingsApply");
  575. UI.saveSetting('encrypt');
  576. UI.saveSetting('true_color');
  577. if (Util.browserSupportsCursorURIs()) {
  578. UI.saveSetting('cursor');
  579. }
  580. UI.saveSetting('resize');
  581. if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
  582. UI.forceSetting('clip', false);
  583. }
  584. UI.saveSetting('clip');
  585. UI.saveSetting('shared');
  586. UI.saveSetting('view_only');
  587. UI.saveSetting('path');
  588. UI.saveSetting('repeaterID');
  589. UI.saveSetting('stylesheet');
  590. UI.saveSetting('logging');
  591. // Settings with immediate (non-connected related) effect
  592. WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
  593. WebUtil.init_logging(UI.getSetting('logging'));
  594. UI.setViewClip();
  595. UI.updateViewDrag();
  596. //Util.Debug("<< settingsApply");
  597. },
  598. setPassword: function() {
  599. UI.rfb.sendPassword($D('noVNC_password').value);
  600. //Reset connect button.
  601. $D('noVNC_connect_button').value = "Connect";
  602. $D('noVNC_connect_button').onclick = UI.connect;
  603. //Hide connection panel.
  604. UI.toggleConnectPanel();
  605. return false;
  606. },
  607. sendCtrlAltDel: function() {
  608. UI.rfb.sendCtrlAltDel();
  609. },
  610. xvpShutdown: function() {
  611. UI.rfb.xvpShutdown();
  612. },
  613. xvpReboot: function() {
  614. UI.rfb.xvpReboot();
  615. },
  616. xvpReset: function() {
  617. UI.rfb.xvpReset();
  618. },
  619. setMouseButton: function(num) {
  620. if (typeof num === 'undefined') {
  621. // Disable mouse buttons
  622. num = -1;
  623. }
  624. if (UI.rfb) {
  625. UI.rfb.get_mouse().set_touchButton(num);
  626. }
  627. var blist = [0, 1,2,4];
  628. for (var b = 0; b < blist.length; b++) {
  629. var button = $D('noVNC_mouse_button' + blist[b]);
  630. if (blist[b] === num) {
  631. button.style.display = "";
  632. } else {
  633. button.style.display = "none";
  634. }
  635. }
  636. },
  637. // Disable/enable XVP button
  638. updateXvpVisualState: function(ver) {
  639. if (ver >= 1) {
  640. $D('xvpButton').style.display = 'inline';
  641. } else {
  642. $D('xvpButton').style.display = 'none';
  643. // Close XVP panel if open
  644. if (UI.xvpOpen === true) {
  645. UI.toggleXvpPanel();
  646. }
  647. }
  648. },
  649. // Display the desktop name in the document title
  650. updateDocumentTitle: function(rfb, name) {
  651. document.title = name + " - noVNC";
  652. },
  653. clipReceive: function(rfb, text) {
  654. Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
  655. $D('noVNC_clipboard_text').value = text;
  656. Util.Debug("<< UI.clipReceive");
  657. },
  658. connect: function() {
  659. UI.closeSettingsMenu();
  660. UI.toggleConnectPanel();
  661. var host = $D('noVNC_host').value;
  662. var port = $D('noVNC_port').value;
  663. var password = $D('noVNC_password').value;
  664. var token = $D('noVNC_token').value;
  665. var path = $D('noVNC_path').value;
  666. //if token is in path then ignore the new token variable
  667. if (token) {
  668. path = WebUtil.injectParamIfMissing(path, "token", token);
  669. }
  670. if ((!host) || (!port)) {
  671. throw new Error("Must set host and port");
  672. }
  673. if (!UI.initRFB()) return;
  674. UI.rfb.set_encrypt(UI.getSetting('encrypt'));
  675. UI.rfb.set_true_color(UI.getSetting('true_color'));
  676. UI.rfb.set_local_cursor(UI.getSetting('cursor'));
  677. UI.rfb.set_shared(UI.getSetting('shared'));
  678. UI.rfb.set_view_only(UI.getSetting('view_only'));
  679. UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
  680. UI.rfb.connect(host, port, password, path);
  681. //Close dialog.
  682. setTimeout(UI.setBarPosition, 100);
  683. $D('noVNC_logo').style.display = "none";
  684. $D('noVNC_screen').style.display = "inline";
  685. },
  686. disconnect: function() {
  687. UI.closeSettingsMenu();
  688. UI.rfb.disconnect();
  689. // Restore the callback used for initial resize
  690. UI.rfb.set_onFBUComplete(UI.initialResize);
  691. $D('noVNC_logo').style.display = "block";
  692. $D('noVNC_screen').style.display = "none";
  693. // Don't display the connection settings until we're actually disconnected
  694. },
  695. displayBlur: function() {
  696. if (!UI.rfb) return;
  697. UI.rfb.get_keyboard().set_focused(false);
  698. UI.rfb.get_mouse().set_focused(false);
  699. },
  700. displayFocus: function() {
  701. if (!UI.rfb) return;
  702. UI.rfb.get_keyboard().set_focused(true);
  703. UI.rfb.get_mouse().set_focused(true);
  704. },
  705. clipClear: function() {
  706. $D('noVNC_clipboard_text').value = "";
  707. UI.rfb.clipboardPasteFrom("");
  708. },
  709. clipSend: function() {
  710. var text = $D('noVNC_clipboard_text').value;
  711. Util.Debug(">> UI.clipSend: " + text.substr(0,40) + "...");
  712. UI.rfb.clipboardPasteFrom(text);
  713. Util.Debug("<< UI.clipSend");
  714. },
  715. // Apply remote resizing or local scaling
  716. applyResizeMode: function() {
  717. if (!UI.rfb) return;
  718. var screen = UI.screenSize();
  719. if (screen && UI.rfb_state === 'normal' && UI.rfb.get_display()) {
  720. var display = UI.rfb.get_display();
  721. var resizeMode = UI.getSetting('resize');
  722. if (resizeMode === 'remote') {
  723. // Request changing the resolution of the remote display to
  724. // the size of the local browser viewport.
  725. // In order to not send multiple requests before the browser-resize
  726. // is finished we wait 0.5 seconds before sending the request.
  727. clearTimeout(UI.resizeTimeout);
  728. UI.resizeTimeout = setTimeout(function(){
  729. // Limit the viewport to the size of the browser window
  730. display.set_maxWidth(screen.w);
  731. display.set_maxHeight(screen.h);
  732. Util.Debug('Attempting setDesktopSize(' +
  733. screen.w + ', ' + screen.h + ')');
  734. // Request a remote size covering the viewport
  735. UI.rfb.setDesktopSize(screen.w, screen.h);
  736. }, 500);
  737. } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
  738. var downscaleOnly = resizeMode === 'downscale';
  739. var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
  740. UI.rfb.get_mouse().set_scale(scaleRatio);
  741. Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
  742. }
  743. }
  744. },
  745. // The screen is always the same size as the available viewport
  746. // in the browser window minus the height of the control bar
  747. screenSize: function() {
  748. var screen = $D('noVNC_screen');
  749. // Hide the scrollbars until the size is calculated
  750. screen.style.overflow = "hidden";
  751. var pos = Util.getPosition(screen);
  752. var w = pos.width;
  753. var h = pos.height;
  754. screen.style.overflow = "visible";
  755. if (isNaN(w) || isNaN(h)) {
  756. return false;
  757. } else {
  758. return {w: w, h: h};
  759. }
  760. },
  761. // Normally we only apply the current resize mode after a window resize
  762. // event. This means that when a new connection is opened, there is no
  763. // resize mode active.
  764. // We have to wait until the first FBU because this is where the client
  765. // will find the supported encodings of the server. Some calls later in
  766. // the chain is dependant on knowing the server-capabilities.
  767. initialResize: function(rfb, fbu) {
  768. UI.applyResizeMode();
  769. // After doing this once, we remove the callback.
  770. UI.rfb.set_onFBUComplete(function() { });
  771. },
  772. // Set and configure viewport clipping
  773. setViewClip: function(clip) {
  774. var display;
  775. if (UI.rfb) {
  776. display = UI.rfb.get_display();
  777. } else {
  778. UI.forceSetting('clip', clip);
  779. return;
  780. }
  781. var cur_clip = display.get_viewport();
  782. if (typeof(clip) !== 'boolean') {
  783. // Use current setting
  784. clip = UI.getSetting('clip');
  785. }
  786. if (clip && !cur_clip) {
  787. // Turn clipping on
  788. UI.updateSetting('clip', true);
  789. } else if (!clip && cur_clip) {
  790. // Turn clipping off
  791. UI.updateSetting('clip', false);
  792. display.set_viewport(false);
  793. // Disable max dimensions
  794. display.set_maxWidth(0);
  795. display.set_maxHeight(0);
  796. display.viewportChangeSize();
  797. }
  798. if (UI.getSetting('clip')) {
  799. // If clipping, update clipping settings
  800. display.set_viewport(true);
  801. var size = UI.screenSize();
  802. if (size) {
  803. display.set_maxWidth(size.w);
  804. display.set_maxHeight(size.h);
  805. // Hide potential scrollbars that can skew the position
  806. $D('noVNC_screen').style.overflow = "hidden";
  807. // The x position marks the left margin of the canvas,
  808. // remove the margin from both sides to keep it centered
  809. var new_w = size.w - (2 * Util.getPosition($D('noVNC_canvas')).x);
  810. $D('noVNC_screen').style.overflow = "visible";
  811. display.viewportChangeSize(new_w, size.h);
  812. }
  813. }
  814. },
  815. // Handle special cases where clipping is forced on/off or locked
  816. enableDisableViewClip: function() {
  817. var resizeElem = $D('noVNC_resize');
  818. var connected = UI.rfb && UI.rfb_state === 'normal';
  819. if (UI.isSafari) {
  820. // Safari auto-hides the scrollbars which makes them
  821. // impossible to use in most cases
  822. UI.setViewClip(true);
  823. $D('noVNC_clip').disabled = true;
  824. } else if (resizeElem.value === 'downscale' || resizeElem.value === 'scale') {
  825. // Disable clipping if we are scaling
  826. UI.setViewClip(false);
  827. $D('noVNC_clip').disabled = true;
  828. } else if (document.msFullscreenElement) {
  829. // The browser is IE and we are in fullscreen mode.
  830. // - We need to force clipping while in fullscreen since
  831. // scrollbars doesn't work.
  832. UI.popupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
  833. UI.rememberedClipSetting = UI.getSetting('clip');
  834. UI.setViewClip(true);
  835. $D('noVNC_clip').disabled = true;
  836. } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) {
  837. // Restore view clip to what it was before fullscreen on IE
  838. UI.setViewClip(UI.rememberedClipSetting);
  839. $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
  840. } else {
  841. $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
  842. if (UI.isTouchDevice) {
  843. UI.setViewClip(true);
  844. }
  845. }
  846. },
  847. // Update the viewport drag state
  848. updateViewDrag: function(drag) {
  849. if (!UI.rfb) return;
  850. var viewDragButton = $D('noVNC_view_drag_button');
  851. // Check if viewport drag is possible. It is only possible
  852. // if the remote display is clipping the client display.
  853. if (UI.rfb_state === 'normal' &&
  854. UI.rfb.get_display().get_viewport() &&
  855. UI.rfb.get_display().clippingDisplay()) {
  856. viewDragButton.style.display = "inline";
  857. viewDragButton.disabled = false;
  858. } else {
  859. // The size of the remote display is the same or smaller
  860. // than the client display. Make sure viewport drag isn't
  861. // active when it can't be used.
  862. if (UI.rfb.get_viewportDrag) {
  863. viewDragButton.className = "noVNC_status_button";
  864. UI.rfb.set_viewportDrag(false);
  865. }
  866. // The button is disabled instead of hidden on touch devices
  867. if (UI.rfb_state === 'normal' && UI.isTouchDevice) {
  868. viewDragButton.style.display = "inline";
  869. viewDragButton.disabled = true;
  870. } else {
  871. viewDragButton.style.display = "none";
  872. }
  873. return;
  874. }
  875. if (typeof(drag) !== "undefined" &&
  876. typeof(drag) !== "object") {
  877. if (drag) {
  878. viewDragButton.className = "noVNC_status_button_selected";
  879. UI.rfb.set_viewportDrag(true);
  880. } else {
  881. viewDragButton.className = "noVNC_status_button";
  882. UI.rfb.set_viewportDrag(false);
  883. }
  884. }
  885. },
  886. toggleViewDrag: function() {
  887. if (!UI.rfb) return;
  888. var viewDragButton = $D('noVNC_view_drag_button');
  889. if (UI.rfb.get_viewportDrag()) {
  890. viewDragButton.className = "noVNC_status_button";
  891. UI.rfb.set_viewportDrag(false);
  892. } else {
  893. viewDragButton.className = "noVNC_status_button_selected";
  894. UI.rfb.set_viewportDrag(true);
  895. }
  896. },
  897. // On touch devices, show the OS keyboard
  898. showKeyboard: function() {
  899. var kbi = $D('keyboardinput');
  900. var skb = $D('showKeyboard');
  901. var l = kbi.value.length;
  902. if(UI.keyboardVisible === false) {
  903. kbi.focus();
  904. try { kbi.setSelectionRange(l, l); } // Move the caret to the end
  905. catch (err) {} // setSelectionRange is undefined in Google Chrome
  906. UI.keyboardVisible = true;
  907. skb.className = "noVNC_status_button_selected";
  908. } else if(UI.keyboardVisible === true) {
  909. kbi.blur();
  910. skb.className = "noVNC_status_button";
  911. UI.keyboardVisible = false;
  912. }
  913. },
  914. keepKeyboard: function() {
  915. clearTimeout(UI.hideKeyboardTimeout);
  916. if(UI.keyboardVisible === true) {
  917. $D('keyboardinput').focus();
  918. $D('showKeyboard').className = "noVNC_status_button_selected";
  919. } else if(UI.keyboardVisible === false) {
  920. $D('keyboardinput').blur();
  921. $D('showKeyboard').className = "noVNC_status_button";
  922. }
  923. },
  924. keyboardinputReset: function() {
  925. var kbi = $D('keyboardinput');
  926. kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
  927. UI.lastKeyboardinput = kbi.value;
  928. },
  929. // When normal keyboard events are left uncought, use the input events from
  930. // the keyboardinput element instead and generate the corresponding key events.
  931. // This code is required since some browsers on Android are inconsistent in
  932. // sending keyCodes in the normal keyboard events when using on screen keyboards.
  933. keyInput: function(event) {
  934. if (!UI.rfb) return;
  935. var newValue = event.target.value;
  936. if (!UI.lastKeyboardinput) {
  937. UI.keyboardinputReset();
  938. }
  939. var oldValue = UI.lastKeyboardinput;
  940. var newLen;
  941. try {
  942. // Try to check caret position since whitespace at the end
  943. // will not be considered by value.length in some browsers
  944. newLen = Math.max(event.target.selectionStart, newValue.length);
  945. } catch (err) {
  946. // selectionStart is undefined in Google Chrome
  947. newLen = newValue.length;
  948. }
  949. var oldLen = oldValue.length;
  950. var backspaces;
  951. var inputs = newLen - oldLen;
  952. if (inputs < 0) {
  953. backspaces = -inputs;
  954. } else {
  955. backspaces = 0;
  956. }
  957. // Compare the old string with the new to account for
  958. // text-corrections or other input that modify existing text
  959. var i;
  960. for (i = 0; i < Math.min(oldLen, newLen); i++) {
  961. if (newValue.charAt(i) != oldValue.charAt(i)) {
  962. inputs = newLen - i;
  963. backspaces = oldLen - i;
  964. break;
  965. }
  966. }
  967. // Send the key events
  968. for (i = 0; i < backspaces; i++) {
  969. UI.rfb.sendKey(XK_BackSpace);
  970. }
  971. for (i = newLen - inputs; i < newLen; i++) {
  972. UI.rfb.sendKey(newValue.charCodeAt(i));
  973. }
  974. // Control the text content length in the keyboardinput element
  975. if (newLen > 2 * UI.defaultKeyboardinputLen) {
  976. UI.keyboardinputReset();
  977. } else if (newLen < 1) {
  978. // There always have to be some text in the keyboardinput
  979. // element with which backspace can interact.
  980. UI.keyboardinputReset();
  981. // This sometimes causes the keyboard to disappear for a second
  982. // but it is required for the android keyboard to recognize that
  983. // text has been added to the field
  984. event.target.blur();
  985. // This has to be ran outside of the input handler in order to work
  986. setTimeout(function() { UI.keepKeyboard(); }, 0);
  987. } else {
  988. UI.lastKeyboardinput = newValue;
  989. }
  990. },
  991. keyInputBlur: function() {
  992. $D('showKeyboard').className = "noVNC_status_button";
  993. //Weird bug in iOS if you change keyboardVisible
  994. //here it does not actually occur so next time
  995. //you click keyboard icon it doesnt work.
  996. UI.hideKeyboardTimeout = setTimeout(function() { UI.setKeyboard(); },100);
  997. },
  998. toggleExtraKeys: function() {
  999. UI.keepKeyboard();
  1000. if(UI.extraKeysVisible === false) {
  1001. $D('toggleCtrlButton').style.display = "inline";
  1002. $D('toggleAltButton').style.display = "inline";
  1003. $D('sendTabButton').style.display = "inline";
  1004. $D('sendEscButton').style.display = "inline";
  1005. $D('toggleExtraKeysButton').className = "noVNC_status_button_selected";
  1006. UI.extraKeysVisible = true;
  1007. } else if(UI.extraKeysVisible === true) {
  1008. $D('toggleCtrlButton').style.display = "";
  1009. $D('toggleAltButton').style.display = "";
  1010. $D('sendTabButton').style.display = "";
  1011. $D('sendEscButton').style.display = "";
  1012. $D('toggleExtraKeysButton').className = "noVNC_status_button";
  1013. UI.extraKeysVisible = false;
  1014. }
  1015. },
  1016. toggleCtrl: function() {
  1017. UI.keepKeyboard();
  1018. if(UI.ctrlOn === false) {
  1019. UI.rfb.sendKey(XK_Control_L, true);
  1020. $D('toggleCtrlButton').className = "noVNC_status_button_selected";
  1021. UI.ctrlOn = true;
  1022. } else if(UI.ctrlOn === true) {
  1023. UI.rfb.sendKey(XK_Control_L, false);
  1024. $D('toggleCtrlButton').className = "noVNC_status_button";
  1025. UI.ctrlOn = false;
  1026. }
  1027. },
  1028. toggleAlt: function() {
  1029. UI.keepKeyboard();
  1030. if(UI.altOn === false) {
  1031. UI.rfb.sendKey(XK_Alt_L, true);
  1032. $D('toggleAltButton').className = "noVNC_status_button_selected";
  1033. UI.altOn = true;
  1034. } else if(UI.altOn === true) {
  1035. UI.rfb.sendKey(XK_Alt_L, false);
  1036. $D('toggleAltButton').className = "noVNC_status_button";
  1037. UI.altOn = false;
  1038. }
  1039. },
  1040. sendTab: function() {
  1041. UI.keepKeyboard();
  1042. UI.rfb.sendKey(XK_Tab);
  1043. },
  1044. sendEsc: function() {
  1045. UI.keepKeyboard();
  1046. UI.rfb.sendKey(XK_Escape);
  1047. },
  1048. setKeyboard: function() {
  1049. UI.keyboardVisible = false;
  1050. },
  1051. //Helper to add options to dropdown.
  1052. addOption: function(selectbox, text, value) {
  1053. var optn = document.createElement("OPTION");
  1054. optn.text = text;
  1055. optn.value = value;
  1056. selectbox.options.add(optn);
  1057. },
  1058. setBarPosition: function() {
  1059. $D('noVNC-control-bar').style.top = (window.pageYOffset) + 'px';
  1060. $D('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
  1061. var vncwidth = $D('noVNC_container').style.offsetWidth;
  1062. $D('noVNC-control-bar').style.width = vncwidth + 'px';
  1063. }
  1064. };
  1065. })();