ui.js 39 KB

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