ui.js 41 KB

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