ui.js 36 KB

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