ui.js 40 KB

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