ui.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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.enableDisableViewClip(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.enableDisableViewClip(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. // 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. // Set 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. // Disable max dimensions
  746. display.set_maxWidth(0);
  747. display.set_maxHeight(0);
  748. display.viewportChangeSize();
  749. }
  750. if (UI.getSetting('clip')) {
  751. // If clipping, update clipping settings
  752. display.set_viewport(true);
  753. var size = UI.getCanvasLimit();
  754. if (size) {
  755. display.set_maxWidth(size.w);
  756. display.set_maxHeight(size.h);
  757. // Hide potential scrollbars that can skew the position
  758. $D('noVNC_container').style.overflow = "hidden";
  759. // The x position marks the left margin of the canvas,
  760. // remove the margin from both sides to keep it centered
  761. var new_w = size.w - (2 * Util.getPosition($D('noVNC_canvas')).x);
  762. $D('noVNC_container').style.overflow = "visible";
  763. display.viewportChangeSize(new_w, size.h);
  764. }
  765. }
  766. },
  767. // Handle special cases where clipping is forced on/off or locked
  768. enableDisableViewClip: function (connected) {
  769. var resizeElem = $D('noVNC_resize');
  770. if (resizeElem.value === 'downscale' || resizeElem.value === 'scale') {
  771. UI.forceSetting('clip', false);
  772. $D('noVNC_clip').disabled = true;
  773. } else {
  774. $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
  775. if (UI.isTouchDevice) {
  776. UI.forceSetting('clip', true);
  777. }
  778. }
  779. },
  780. // Update the viewport drag/move button
  781. updateViewDrag: function(drag) {
  782. if (!UI.rfb) return;
  783. var vmb = $D('noVNC_view_drag_button');
  784. // Check if viewport drag is possible
  785. if (UI.rfb_state === 'normal' &&
  786. UI.rfb.get_display().get_viewport() &&
  787. UI.rfb.get_display().clippingDisplay()) {
  788. // Show and enable the drag button
  789. vmb.style.display = "inline";
  790. vmb.disabled = false;
  791. } else {
  792. // The VNC content is the same size as
  793. // or smaller than the display
  794. if (UI.rfb.get_viewportDrag) {
  795. // Turn off viewport drag when it's
  796. // active since it can't be used here
  797. vmb.className = "noVNC_status_button";
  798. UI.rfb.set_viewportDrag(false);
  799. }
  800. // Disable or hide the drag button
  801. if (UI.rfb_state === 'normal' && UI.isTouchDevice) {
  802. vmb.style.display = "inline";
  803. vmb.disabled = true;
  804. } else {
  805. vmb.style.display = "none";
  806. }
  807. return;
  808. }
  809. if (typeof(drag) !== "undefined" &&
  810. typeof(drag) !== "object") {
  811. if (drag) {
  812. vmb.className = "noVNC_status_button_selected";
  813. UI.rfb.set_viewportDrag(true);
  814. } else {
  815. vmb.className = "noVNC_status_button";
  816. UI.rfb.set_viewportDrag(false);
  817. }
  818. }
  819. },
  820. toggleViewDrag: function() {
  821. if (!UI.rfb) return;
  822. var vmb = $D('noVNC_view_drag_button');
  823. if (UI.rfb.get_viewportDrag()) {
  824. vmb.className = "noVNC_status_button";
  825. UI.rfb.set_viewportDrag(false);
  826. } else {
  827. vmb.className = "noVNC_status_button_selected";
  828. UI.rfb.set_viewportDrag(true);
  829. }
  830. },
  831. // On touch devices, show the OS keyboard
  832. showKeyboard: function() {
  833. var kbi = $D('keyboardinput');
  834. var skb = $D('showKeyboard');
  835. var l = kbi.value.length;
  836. if(UI.keyboardVisible === false) {
  837. kbi.focus();
  838. try { kbi.setSelectionRange(l, l); } // Move the caret to the end
  839. catch (err) {} // setSelectionRange is undefined in Google Chrome
  840. UI.keyboardVisible = true;
  841. skb.className = "noVNC_status_button_selected";
  842. } else if(UI.keyboardVisible === true) {
  843. kbi.blur();
  844. skb.className = "noVNC_status_button";
  845. UI.keyboardVisible = false;
  846. }
  847. },
  848. keepKeyboard: function() {
  849. clearTimeout(UI.hideKeyboardTimeout);
  850. if(UI.keyboardVisible === true) {
  851. $D('keyboardinput').focus();
  852. $D('showKeyboard').className = "noVNC_status_button_selected";
  853. } else if(UI.keyboardVisible === false) {
  854. $D('keyboardinput').blur();
  855. $D('showKeyboard').className = "noVNC_status_button";
  856. }
  857. },
  858. keyboardinputReset: function() {
  859. var kbi = $D('keyboardinput');
  860. kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
  861. UI.lastKeyboardinput = kbi.value;
  862. },
  863. // When normal keyboard events are left uncought, use the input events from
  864. // the keyboardinput element instead and generate the corresponding key events.
  865. // This code is required since some browsers on Android are inconsistent in
  866. // sending keyCodes in the normal keyboard events when using on screen keyboards.
  867. keyInput: function(event) {
  868. if (!UI.rfb) return;
  869. var newValue = event.target.value;
  870. if (!UI.lastKeyboardinput) {
  871. UI.keyboardinputReset();
  872. }
  873. var oldValue = UI.lastKeyboardinput;
  874. var newLen;
  875. try {
  876. // Try to check caret position since whitespace at the end
  877. // will not be considered by value.length in some browsers
  878. newLen = Math.max(event.target.selectionStart, newValue.length);
  879. } catch (err) {
  880. // selectionStart is undefined in Google Chrome
  881. newLen = newValue.length;
  882. }
  883. var oldLen = oldValue.length;
  884. var backspaces;
  885. var inputs = newLen - oldLen;
  886. if (inputs < 0) {
  887. backspaces = -inputs;
  888. } else {
  889. backspaces = 0;
  890. }
  891. // Compare the old string with the new to account for
  892. // text-corrections or other input that modify existing text
  893. var i;
  894. for (i = 0; i < Math.min(oldLen, newLen); i++) {
  895. if (newValue.charAt(i) != oldValue.charAt(i)) {
  896. inputs = newLen - i;
  897. backspaces = oldLen - i;
  898. break;
  899. }
  900. }
  901. // Send the key events
  902. for (i = 0; i < backspaces; i++) {
  903. UI.rfb.sendKey(XK_BackSpace);
  904. }
  905. for (i = newLen - inputs; i < newLen; i++) {
  906. UI.rfb.sendKey(newValue.charCodeAt(i));
  907. }
  908. // Control the text content length in the keyboardinput element
  909. if (newLen > 2 * UI.defaultKeyboardinputLen) {
  910. UI.keyboardinputReset();
  911. } else if (newLen < 1) {
  912. // There always have to be some text in the keyboardinput
  913. // element with which backspace can interact.
  914. UI.keyboardinputReset();
  915. // This sometimes causes the keyboard to disappear for a second
  916. // but it is required for the android keyboard to recognize that
  917. // text has been added to the field
  918. event.target.blur();
  919. // This has to be ran outside of the input handler in order to work
  920. setTimeout(function() { UI.keepKeyboard(); }, 0);
  921. } else {
  922. UI.lastKeyboardinput = newValue;
  923. }
  924. },
  925. keyInputBlur: function() {
  926. $D('showKeyboard').className = "noVNC_status_button";
  927. //Weird bug in iOS if you change keyboardVisible
  928. //here it does not actually occur so next time
  929. //you click keyboard icon it doesnt work.
  930. UI.hideKeyboardTimeout = setTimeout(function() { UI.setKeyboard(); },100);
  931. },
  932. showExtraKeys: function() {
  933. UI.keepKeyboard();
  934. if(UI.extraKeysVisible === false) {
  935. $D('toggleCtrlButton').style.display = "inline";
  936. $D('toggleAltButton').style.display = "inline";
  937. $D('sendTabButton').style.display = "inline";
  938. $D('sendEscButton').style.display = "inline";
  939. $D('showExtraKeysButton').className = "noVNC_status_button_selected";
  940. UI.extraKeysVisible = true;
  941. } else if(UI.extraKeysVisible === true) {
  942. $D('toggleCtrlButton').style.display = "";
  943. $D('toggleAltButton').style.display = "";
  944. $D('sendTabButton').style.display = "";
  945. $D('sendEscButton').style.display = "";
  946. $D('showExtraKeysButton').className = "noVNC_status_button";
  947. UI.extraKeysVisible = false;
  948. }
  949. },
  950. toggleCtrl: function() {
  951. UI.keepKeyboard();
  952. if(UI.ctrlOn === false) {
  953. UI.rfb.sendKey(XK_Control_L, true);
  954. $D('toggleCtrlButton').className = "noVNC_status_button_selected";
  955. UI.ctrlOn = true;
  956. } else if(UI.ctrlOn === true) {
  957. UI.rfb.sendKey(XK_Control_L, false);
  958. $D('toggleCtrlButton').className = "noVNC_status_button";
  959. UI.ctrlOn = false;
  960. }
  961. },
  962. toggleAlt: function() {
  963. UI.keepKeyboard();
  964. if(UI.altOn === false) {
  965. UI.rfb.sendKey(XK_Alt_L, true);
  966. $D('toggleAltButton').className = "noVNC_status_button_selected";
  967. UI.altOn = true;
  968. } else if(UI.altOn === true) {
  969. UI.rfb.sendKey(XK_Alt_L, false);
  970. $D('toggleAltButton').className = "noVNC_status_button";
  971. UI.altOn = false;
  972. }
  973. },
  974. sendTab: function() {
  975. UI.keepKeyboard();
  976. UI.rfb.sendKey(XK_Tab);
  977. },
  978. sendEsc: function() {
  979. UI.keepKeyboard();
  980. UI.rfb.sendKey(XK_Escape);
  981. },
  982. setKeyboard: function() {
  983. UI.keyboardVisible = false;
  984. },
  985. //Helper to add options to dropdown.
  986. addOption: function(selectbox, text, value) {
  987. var optn = document.createElement("OPTION");
  988. optn.text = text;
  989. optn.value = value;
  990. selectbox.options.add(optn);
  991. },
  992. setBarPosition: function() {
  993. $D('noVNC-control-bar').style.top = (window.pageYOffset) + 'px';
  994. $D('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
  995. var vncwidth = $D('noVNC_screen').style.offsetWidth;
  996. $D('noVNC-control-bar').style.width = vncwidth + 'px';
  997. }
  998. };
  999. })();