ui.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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/move button
  848. updateViewDrag: function(drag) {
  849. if (!UI.rfb) return;
  850. var vmb = $D('noVNC_view_drag_button');
  851. // Check if viewport drag is possible
  852. if (UI.rfb_state === 'normal' &&
  853. UI.rfb.get_display().get_viewport() &&
  854. UI.rfb.get_display().clippingDisplay()) {
  855. // Show and enable the drag button
  856. vmb.style.display = "inline";
  857. vmb.disabled = false;
  858. } else {
  859. // The VNC content is the same size as
  860. // or smaller than the display
  861. if (UI.rfb.get_viewportDrag) {
  862. // Turn off viewport drag when it's
  863. // active since it can't be used here
  864. vmb.className = "noVNC_status_button";
  865. UI.rfb.set_viewportDrag(false);
  866. }
  867. // Disable or hide the drag button
  868. if (UI.rfb_state === 'normal' && UI.isTouchDevice) {
  869. vmb.style.display = "inline";
  870. vmb.disabled = true;
  871. } else {
  872. vmb.style.display = "none";
  873. }
  874. return;
  875. }
  876. if (typeof(drag) !== "undefined" &&
  877. typeof(drag) !== "object") {
  878. if (drag) {
  879. vmb.className = "noVNC_status_button_selected";
  880. UI.rfb.set_viewportDrag(true);
  881. } else {
  882. vmb.className = "noVNC_status_button";
  883. UI.rfb.set_viewportDrag(false);
  884. }
  885. }
  886. },
  887. toggleViewDrag: function() {
  888. if (!UI.rfb) return;
  889. var vmb = $D('noVNC_view_drag_button');
  890. if (UI.rfb.get_viewportDrag()) {
  891. vmb.className = "noVNC_status_button";
  892. UI.rfb.set_viewportDrag(false);
  893. } else {
  894. vmb.className = "noVNC_status_button_selected";
  895. UI.rfb.set_viewportDrag(true);
  896. }
  897. },
  898. // On touch devices, show the OS keyboard
  899. showKeyboard: function() {
  900. var kbi = $D('keyboardinput');
  901. var skb = $D('showKeyboard');
  902. var l = kbi.value.length;
  903. if(UI.keyboardVisible === false) {
  904. kbi.focus();
  905. try { kbi.setSelectionRange(l, l); } // Move the caret to the end
  906. catch (err) {} // setSelectionRange is undefined in Google Chrome
  907. UI.keyboardVisible = true;
  908. skb.className = "noVNC_status_button_selected";
  909. } else if(UI.keyboardVisible === true) {
  910. kbi.blur();
  911. skb.className = "noVNC_status_button";
  912. UI.keyboardVisible = false;
  913. }
  914. },
  915. keepKeyboard: function() {
  916. clearTimeout(UI.hideKeyboardTimeout);
  917. if(UI.keyboardVisible === true) {
  918. $D('keyboardinput').focus();
  919. $D('showKeyboard').className = "noVNC_status_button_selected";
  920. } else if(UI.keyboardVisible === false) {
  921. $D('keyboardinput').blur();
  922. $D('showKeyboard').className = "noVNC_status_button";
  923. }
  924. },
  925. keyboardinputReset: function() {
  926. var kbi = $D('keyboardinput');
  927. kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
  928. UI.lastKeyboardinput = kbi.value;
  929. },
  930. // When normal keyboard events are left uncought, use the input events from
  931. // the keyboardinput element instead and generate the corresponding key events.
  932. // This code is required since some browsers on Android are inconsistent in
  933. // sending keyCodes in the normal keyboard events when using on screen keyboards.
  934. keyInput: function(event) {
  935. if (!UI.rfb) return;
  936. var newValue = event.target.value;
  937. if (!UI.lastKeyboardinput) {
  938. UI.keyboardinputReset();
  939. }
  940. var oldValue = UI.lastKeyboardinput;
  941. var newLen;
  942. try {
  943. // Try to check caret position since whitespace at the end
  944. // will not be considered by value.length in some browsers
  945. newLen = Math.max(event.target.selectionStart, newValue.length);
  946. } catch (err) {
  947. // selectionStart is undefined in Google Chrome
  948. newLen = newValue.length;
  949. }
  950. var oldLen = oldValue.length;
  951. var backspaces;
  952. var inputs = newLen - oldLen;
  953. if (inputs < 0) {
  954. backspaces = -inputs;
  955. } else {
  956. backspaces = 0;
  957. }
  958. // Compare the old string with the new to account for
  959. // text-corrections or other input that modify existing text
  960. var i;
  961. for (i = 0; i < Math.min(oldLen, newLen); i++) {
  962. if (newValue.charAt(i) != oldValue.charAt(i)) {
  963. inputs = newLen - i;
  964. backspaces = oldLen - i;
  965. break;
  966. }
  967. }
  968. // Send the key events
  969. for (i = 0; i < backspaces; i++) {
  970. UI.rfb.sendKey(XK_BackSpace);
  971. }
  972. for (i = newLen - inputs; i < newLen; i++) {
  973. UI.rfb.sendKey(newValue.charCodeAt(i));
  974. }
  975. // Control the text content length in the keyboardinput element
  976. if (newLen > 2 * UI.defaultKeyboardinputLen) {
  977. UI.keyboardinputReset();
  978. } else if (newLen < 1) {
  979. // There always have to be some text in the keyboardinput
  980. // element with which backspace can interact.
  981. UI.keyboardinputReset();
  982. // This sometimes causes the keyboard to disappear for a second
  983. // but it is required for the android keyboard to recognize that
  984. // text has been added to the field
  985. event.target.blur();
  986. // This has to be ran outside of the input handler in order to work
  987. setTimeout(function() { UI.keepKeyboard(); }, 0);
  988. } else {
  989. UI.lastKeyboardinput = newValue;
  990. }
  991. },
  992. keyInputBlur: function() {
  993. $D('showKeyboard').className = "noVNC_status_button";
  994. //Weird bug in iOS if you change keyboardVisible
  995. //here it does not actually occur so next time
  996. //you click keyboard icon it doesnt work.
  997. UI.hideKeyboardTimeout = setTimeout(function() { UI.setKeyboard(); },100);
  998. },
  999. toggleExtraKeys: function() {
  1000. UI.keepKeyboard();
  1001. if(UI.extraKeysVisible === false) {
  1002. $D('toggleCtrlButton').style.display = "inline";
  1003. $D('toggleAltButton').style.display = "inline";
  1004. $D('sendTabButton').style.display = "inline";
  1005. $D('sendEscButton').style.display = "inline";
  1006. $D('toggleExtraKeysButton').className = "noVNC_status_button_selected";
  1007. UI.extraKeysVisible = true;
  1008. } else if(UI.extraKeysVisible === true) {
  1009. $D('toggleCtrlButton').style.display = "";
  1010. $D('toggleAltButton').style.display = "";
  1011. $D('sendTabButton').style.display = "";
  1012. $D('sendEscButton').style.display = "";
  1013. $D('toggleExtraKeysButton').className = "noVNC_status_button";
  1014. UI.extraKeysVisible = false;
  1015. }
  1016. },
  1017. toggleCtrl: function() {
  1018. UI.keepKeyboard();
  1019. if(UI.ctrlOn === false) {
  1020. UI.rfb.sendKey(XK_Control_L, true);
  1021. $D('toggleCtrlButton').className = "noVNC_status_button_selected";
  1022. UI.ctrlOn = true;
  1023. } else if(UI.ctrlOn === true) {
  1024. UI.rfb.sendKey(XK_Control_L, false);
  1025. $D('toggleCtrlButton').className = "noVNC_status_button";
  1026. UI.ctrlOn = false;
  1027. }
  1028. },
  1029. toggleAlt: function() {
  1030. UI.keepKeyboard();
  1031. if(UI.altOn === false) {
  1032. UI.rfb.sendKey(XK_Alt_L, true);
  1033. $D('toggleAltButton').className = "noVNC_status_button_selected";
  1034. UI.altOn = true;
  1035. } else if(UI.altOn === true) {
  1036. UI.rfb.sendKey(XK_Alt_L, false);
  1037. $D('toggleAltButton').className = "noVNC_status_button";
  1038. UI.altOn = false;
  1039. }
  1040. },
  1041. sendTab: function() {
  1042. UI.keepKeyboard();
  1043. UI.rfb.sendKey(XK_Tab);
  1044. },
  1045. sendEsc: function() {
  1046. UI.keepKeyboard();
  1047. UI.rfb.sendKey(XK_Escape);
  1048. },
  1049. setKeyboard: function() {
  1050. UI.keyboardVisible = false;
  1051. },
  1052. //Helper to add options to dropdown.
  1053. addOption: function(selectbox, text, value) {
  1054. var optn = document.createElement("OPTION");
  1055. optn.text = text;
  1056. optn.value = value;
  1057. selectbox.options.add(optn);
  1058. },
  1059. setBarPosition: function() {
  1060. $D('noVNC-control-bar').style.top = (window.pageYOffset) + 'px';
  1061. $D('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
  1062. var vncwidth = $D('noVNC_container').style.offsetWidth;
  1063. $D('noVNC-control-bar').style.width = vncwidth + 'px';
  1064. }
  1065. };
  1066. })();