ui.js 38 KB

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