ui.js 41 KB

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