ui.js 42 KB

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