default_controls.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2010 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. "use strict";
  9. /*jslint white: false */
  10. /*global $, Util, RFB, Canvas, VNC_uri_prefix, Element, Fx */
  11. var DefaultControls = {
  12. settingsOpen : false,
  13. // Render default controls and initialize settings menu
  14. load: function(target) {
  15. var html, i, DC = DefaultControls, sheet, sheets, llevels;
  16. /* Populate the 'target' DOM element with default controls */
  17. if (!target) { target = 'vnc'; }
  18. html = "";
  19. html += '<div id="VNC_controls">';
  20. html += ' <ul>';
  21. html += ' <li>Host: <input id="VNC_host"></li>';
  22. html += ' <li>Port: <input id="VNC_port"></li>';
  23. html += ' <li>Password: <input id="VNC_password"';
  24. html += ' type="password"></li>';
  25. html += ' <li><input id="VNC_connect_button" type="button"';
  26. html += ' value="Loading" disabled></li>';
  27. html += ' </ul>';
  28. html += '</div>';
  29. html += '<div id="VNC_screen">';
  30. html += ' <div id="VNC_status_bar" class="VNC_status_bar" style="margin-top: 0px;">';
  31. html += ' <table border=0 width=100%><tr>';
  32. html += ' <td><div id="VNC_status">Loading</div></td>';
  33. html += ' <td width=1%><div class="VNC_buttons_right">';
  34. html += ' <input type=button class="VNC_status_button" value="Settings"';
  35. html += ' id="menuButton"';
  36. html += ' onclick="DefaultControls.clickSettingsMenu();">';
  37. html += ' <span id="VNC_settings_menu"';
  38. html += ' onmouseover="DefaultControls.canvasBlur();"';
  39. html += ' onmouseout="DefaultControls.canvasFocus();">';
  40. html += ' <ul>';
  41. html += ' <li><input id="VNC_encrypt"';
  42. html += ' type="checkbox"> Encrypt</li>';
  43. html += ' <li><input id="VNC_true_color"';
  44. html += ' type="checkbox" checked> True Color</li>';
  45. html += ' <li><input id="VNC_cursor"';
  46. html += ' type="checkbox" checked> Local Cursor</li>';
  47. html += ' <hr>';
  48. // Stylesheet selection dropdown
  49. html += ' <li><select id="VNC_stylesheet" name="vncStyle">';
  50. html += ' <option value="default">default</option>';
  51. sheet = Util.selectStylesheet();
  52. sheets = Util.getStylesheets();
  53. for (i = 0; i < sheets.length; i += 1) {
  54. html += '<option value="' + sheets[i].title + '">' + sheets[i].title + '</option>';
  55. }
  56. html += ' </select> Style</li>';
  57. // Logging selection dropdown
  58. html += ' <li><select id="VNC_logging" name="vncLogging">';
  59. llevels = ['error', 'warn', 'info', 'debug'];
  60. for (i = 0; i < llevels.length; i += 1) {
  61. html += '<option value="' + llevels[i] + '">' + llevels[i] + '</option>';
  62. }
  63. html += ' </select> Logging</li>';
  64. html += ' <hr>';
  65. html += ' <li><input type="button" id="VNC_apply" value="Apply"';
  66. html += ' onclick="DefaultControls.settingsApply()"></li>';
  67. html += ' </ul>';
  68. html += ' </span></div></td>';
  69. html += ' <td width=1%><div class="VNC_buttons_right">';
  70. html += ' <input type=button class="VNC_status_button" value="Send CtrlAltDel"';
  71. html += ' id="sendCtrlAltDelButton"';
  72. html += ' onclick="DefaultControls.sendCtrlAltDel();"></div></td>';
  73. html += ' </tr></table>';
  74. html += ' </div>';
  75. html += ' <canvas id="VNC_canvas" width="640px" height="20px">';
  76. html += ' Canvas not supported.';
  77. html += ' </canvas>';
  78. html += '</div>';
  79. html += '<br><br>';
  80. html += '<div id="VNC_clipboard">';
  81. html += ' VNC Clipboard:';
  82. html += ' <input id="VNC_clipboard_clear_button"';
  83. html += ' type="button" value="Clear"';
  84. html += ' onclick="DefaultControls.clipClear();">';
  85. html += ' <br>';
  86. html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5';
  87. html += ' onfocus="DefaultControls.canvasBlur();"';
  88. html += ' onblur="DefaultControls.canvasFocus();"';
  89. html += ' onchange="DefaultControls.clipSend();"></textarea>';
  90. html += '</div>';
  91. $(target).innerHTML = html;
  92. // Settings with immediate effects
  93. DC.initSetting('logging', 'warn');
  94. Util.init_logging(DC.getSetting('logging'));
  95. DC.initSetting('stylesheet', 'default');
  96. Util.selectStylesheet(null); // call twice to get around webkit bug
  97. Util.selectStylesheet(DC.getSetting('stylesheet'));
  98. /* Populate the controls if defaults are provided in the URL */
  99. DC.initSetting('host', '');
  100. DC.initSetting('port', '');
  101. DC.initSetting('password', '');
  102. DC.initSetting('encrypt', false);
  103. DC.initSetting('true_color', true);
  104. DC.initSetting('cursor', true);
  105. DC.rfb = RFB({'target': 'VNC_canvas',
  106. 'updateState': DC.updateState,
  107. 'clipboardReceive': DC.clipReceive});
  108. // Unfocus clipboard when over the VNC area
  109. $('VNC_screen').onmousemove = function () {
  110. var canvas = DC.rfb.get_canvas();
  111. if ((! canvas) || (! canvas.get_focused())) {
  112. $('VNC_clipboard_text').blur();
  113. }
  114. };
  115. },
  116. // Read form control compatible setting from cookie
  117. getSetting: function(name) {
  118. var val, ctrl = $('VNC_' + name);
  119. val = Util.readCookie(name);
  120. if (ctrl.type === 'checkbox') {
  121. if (val.toLowerCase() in {'0':1, 'no':1, 'false':1}) {
  122. val = false;
  123. } else {
  124. val = true;
  125. }
  126. }
  127. return val;
  128. },
  129. // Update cookie and form control setting. If value is not set, then
  130. // updates from control to current cookie setting.
  131. updateSetting: function(name, value) {
  132. var i, ctrl = $('VNC_' + name);
  133. // Save the cookie for this session
  134. if (typeof value !== 'undefined') {
  135. Util.createCookie(name, value);
  136. }
  137. // Update the settings control
  138. value = DefaultControls.getSetting(name);
  139. if (ctrl.type === 'checkbox') {
  140. ctrl.checked = value;
  141. } else if (typeof ctrl.options !== 'undefined') {
  142. for (i = 0; i < ctrl.options.length; i += 1) {
  143. if (ctrl.options[i].value === value) {
  144. ctrl.selectedIndex = i;
  145. break;
  146. }
  147. }
  148. } else {
  149. ctrl.value = value;
  150. }
  151. },
  152. // Save control setting to cookie
  153. saveSetting: function(name) {
  154. var val, ctrl = $('VNC_' + name);
  155. if (ctrl.type === 'checkbox') {
  156. val = ctrl.checked;
  157. } else if (typeof ctrl.options !== 'undefined') {
  158. val = ctrl.options[ctrl.selectedIndex].value;
  159. } else {
  160. val = ctrl.value;
  161. }
  162. Util.createCookie(name, val);
  163. //Util.Debug("Setting saved '" + name + "=" + val + "'");
  164. return val;
  165. },
  166. // Initial page load read/initialization of settings
  167. initSetting: function(name, defVal) {
  168. var val;
  169. // Check Query string followed by cookie
  170. val = Util.getQueryVar(name);
  171. if (val === null) {
  172. val = Util.readCookie(name, defVal);
  173. }
  174. DefaultControls.updateSetting(name, val);
  175. //Util.Debug("Setting '" + name + "' initialized to '" + val + "'");
  176. return val;
  177. },
  178. // Toggle the settings menu:
  179. // On open, settings are refreshed from saved cookies.
  180. // On close, settings are applied
  181. clickSettingsMenu: function() {
  182. var DC = DefaultControls;
  183. if (DC.settingsOpen) {
  184. DC.settingsApply();
  185. DC.closeSettingsMenu();
  186. } else {
  187. DC.updateSetting('encrypt');
  188. DC.updateSetting('true_color');
  189. if (DC.rfb.get_canvas().get_cursor_uri()) {
  190. DC.updateSetting('cursor');
  191. } else {
  192. DC.updateSetting('cursor', false);
  193. $('VNC_cursor').disabled = true;
  194. }
  195. DC.updateSetting('stylesheet');
  196. DC.updateSetting('logging');
  197. DC.openSettingsMenu();
  198. }
  199. },
  200. // Open menu
  201. openSettingsMenu: function() {
  202. $('VNC_settings_menu').style.display = "block";
  203. DefaultControls.settingsOpen = true;
  204. },
  205. // Close menu (without applying settings)
  206. closeSettingsMenu: function() {
  207. $('VNC_settings_menu').style.display = "none";
  208. DefaultControls.settingsOpen = false;
  209. },
  210. // Disable/enable controls depending on connection state
  211. settingsDisabled: function(disabled, rfb) {
  212. //Util.Debug(">> settingsDisabled");
  213. $('VNC_encrypt').disabled = disabled;
  214. $('VNC_true_color').disabled = disabled;
  215. if (rfb && rfb.get_canvas().get_cursor_uri()) {
  216. $('VNC_cursor').disabled = disabled;
  217. } else {
  218. DefaultControls.updateSetting('cursor', false);
  219. $('VNC_cursor').disabled = true;
  220. }
  221. //Util.Debug("<< settingsDisabled");
  222. },
  223. // Save/apply settings when 'Apply' button is pressed
  224. settingsApply: function() {
  225. //Util.Debug(">> settingsApply");
  226. var DC = DefaultControls;
  227. DC.saveSetting('encrypt');
  228. DC.saveSetting('true_color');
  229. if (DC.rfb.get_canvas().get_cursor_uri()) {
  230. DC.saveSetting('cursor');
  231. }
  232. DC.saveSetting('stylesheet');
  233. DC.saveSetting('logging');
  234. // Settings with immediate (non-connected related) effect
  235. Util.selectStylesheet(DC.getSetting('stylesheet'));
  236. Util.init_logging(DC.getSetting('logging'));
  237. //Util.Debug("<< settingsApply");
  238. },
  239. setPassword: function() {
  240. DefaultControls.rfb.sendPassword($('VNC_password').value);
  241. return false;
  242. },
  243. sendCtrlAltDel: function() {
  244. DefaultControls.rfb.sendCtrlAltDel();
  245. },
  246. updateState: function(rfb, state, oldstate, msg) {
  247. var s, sb, c, cad, klass;
  248. s = $('VNC_status');
  249. sb = $('VNC_status_bar');
  250. c = $('VNC_connect_button');
  251. cad = $('sendCtrlAltDelButton');
  252. switch (state) {
  253. case 'failed':
  254. case 'fatal':
  255. c.disabled = true;
  256. cad.disabled = true;
  257. DefaultControls.settingsDisabled(true, rfb);
  258. klass = "VNC_status_error";
  259. break;
  260. case 'normal':
  261. c.value = "Disconnect";
  262. c.onclick = DefaultControls.disconnect;
  263. c.disabled = false;
  264. cad.disabled = false;
  265. DefaultControls.settingsDisabled(true, rfb);
  266. klass = "VNC_status_normal";
  267. break;
  268. case 'disconnected':
  269. case 'loaded':
  270. c.value = "Connect";
  271. c.onclick = DefaultControls.connect;
  272. c.disabled = false;
  273. cad.disabled = true;
  274. DefaultControls.settingsDisabled(false, rfb);
  275. klass = "VNC_status_normal";
  276. break;
  277. case 'password':
  278. c.value = "Send Password";
  279. c.onclick = DefaultControls.setPassword;
  280. c.disabled = false;
  281. cad.disabled = true;
  282. DefaultControls.settingsDisabled(true, rfb);
  283. klass = "VNC_status_warn";
  284. break;
  285. default:
  286. c.disabled = true;
  287. cad.disabled = true;
  288. DefaultControls.settingsDisabled(true, rfb);
  289. klass = "VNC_status_warn";
  290. break;
  291. }
  292. if (typeof(msg) !== 'undefined') {
  293. s.setAttribute("class", klass);
  294. sb.setAttribute("class", klass);
  295. s.innerHTML = msg;
  296. }
  297. },
  298. clipReceive: function(rfb, text) {
  299. Util.Debug(">> DefaultControls.clipReceive: " + text.substr(0,40) + "...");
  300. $('VNC_clipboard_text').value = text;
  301. Util.Debug("<< DefaultControls.clipReceive");
  302. },
  303. connect: function() {
  304. var host, port, password, DC = DefaultControls;
  305. DC.closeSettingsMenu();
  306. host = $('VNC_host').value;
  307. port = $('VNC_port').value;
  308. password = $('VNC_password').value;
  309. if ((!host) || (!port)) {
  310. throw("Must set host and port");
  311. }
  312. DC.rfb.set_encrypt(DC.getSetting('encrypt'));
  313. DC.rfb.set_true_color(DC.getSetting('true_color'));
  314. DC.rfb.set_local_cursor(DC.getSetting('cursor'));
  315. DC.rfb.connect(host, port, password);
  316. },
  317. disconnect: function() {
  318. DefaultControls.closeSettingsMenu();
  319. DefaultControls.rfb.disconnect();
  320. },
  321. canvasBlur: function() {
  322. DefaultControls.rfb.get_canvas().set_focused(false);
  323. },
  324. canvasFocus: function() {
  325. DefaultControls.rfb.get_canvas().set_focused(true);
  326. },
  327. clipClear: function() {
  328. $('VNC_clipboard_text').value = "";
  329. DefaultControls.rfb.clipboardPasteFrom("");
  330. },
  331. clipSend: function() {
  332. var text = $('VNC_clipboard_text').value;
  333. Util.Debug(">> DefaultControls.clipSend: " + text.substr(0,40) + "...");
  334. DefaultControls.rfb.clipboardPasteFrom(text);
  335. Util.Debug("<< DefaultControls.clipSend");
  336. }
  337. };