ui.js 47 KB

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