default_controls.js 13 KB

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