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