ui.js 44 KB

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