ui.js 41 KB

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