]>
Commit | Line | Data |
---|---|---|
15046f00 JM |
1 | /* |
2 | * noVNC: HTML5 VNC client | |
d58f8b51 | 3 | * Copyright (C) 2012 Joel Martin |
777cb7a0 | 4 | * Copyright (C) 2016 Samuel Mannehed for Cendio AB |
6cba147d | 5 | * Copyright (C) 2016 Pierre Ossman for Cendio AB |
1d728ace | 6 | * Licensed under MPL 2.0 (see LICENSE.txt) |
15046f00 JM |
7 | * |
8 | * See README.md for usage and integration instructions. | |
9 | */ | |
43cf7bd8 | 10 | |
bbbf42bb | 11 | /* jslint white: false, browser: true */ |
ae510306 SR |
12 | /* global window, document.getElementById, Util, WebUtil, RFB, Display */ |
13 | ||
14 | /* [module] | |
15 | * import Util from "../core/util"; | |
bd5340c7 | 16 | * import KeyTable from "../core/input/keysym"; |
ae510306 SR |
17 | * import RFB from "../core/rfb"; |
18 | * import Display from "../core/display"; | |
19 | * import WebUtil from "./webutil"; | |
20 | */ | |
bbbf42bb SR |
21 | |
22 | var UI; | |
23 | ||
24 | (function () { | |
25 | "use strict"; | |
26 | ||
ae510306 | 27 | /* [begin skip-as-module] */ |
bbbf42bb | 28 | // Load supporting scripts |
72bdd06e | 29 | WebUtil.load_scripts( |
bd5340c7 SR |
30 | {'core': ["base64.js", "websock.js", "des.js", "input/keysymdef.js", |
31 | "input/xtscancodes.js", "input/util.js", "input/devices.js", | |
32 | "display.js", "inflator.js", "rfb.js", "input/keysym.js"]}); | |
ae510306 | 33 | |
bbbf42bb | 34 | window.onscriptsload = function () { UI.load(); }; |
ae510306 | 35 | /* [end skip-as-module] */ |
bbbf42bb | 36 | |
bd6874e0 | 37 | UI = { |
bbbf42bb | 38 | |
045d9224 | 39 | rfb_state: 'loaded', |
529c64e1 | 40 | |
045d9224 | 41 | resizeTimeout: null, |
529c64e1 | 42 | popupStatusTimeout: null, |
43 | hideKeyboardTimeout: null, | |
44 | ||
045d9224 | 45 | settingsOpen: false, |
46 | connSettingsOpen: false, | |
bbbf42bb SR |
47 | clipboardOpen: false, |
48 | keyboardVisible: false, | |
9d16e512 | 49 | extraKeysVisible: false, |
529c64e1 | 50 | |
bbbf42bb | 51 | isTouchDevice: false, |
f620259b | 52 | isSafari: false, |
a6357e82 | 53 | rememberedClipSetting: null, |
529c64e1 | 54 | lastKeyboardinput: null, |
55 | defaultKeyboardinputLen: 100, | |
56 | ||
e961641f SM |
57 | ctrlOn: false, |
58 | altOn: false, | |
bbbf42bb SR |
59 | |
60 | // Setup rfb object, load settings from browser storage, then call | |
61 | // UI.init to setup the UI/menus | |
0bd2cbac | 62 | load: function(callback) { |
bbbf42bb SR |
63 | WebUtil.initSettings(UI.start, callback); |
64 | }, | |
65 | ||
66 | // Render default UI and initialize settings menu | |
67 | start: function(callback) { | |
0f6af1e3 | 68 | |
69 | // Setup global variables first | |
bbbf42bb | 70 | UI.isTouchDevice = 'ontouchstart' in document.documentElement; |
0f6af1e3 | 71 | UI.isSafari = (navigator.userAgent.indexOf('Safari') !== -1 && |
72 | navigator.userAgent.indexOf('Chrome') === -1); | |
73 | ||
74 | UI.initSettings(); | |
75 | ||
76 | // Show mouse selector buttons on touch screen devices | |
77 | if (UI.isTouchDevice) { | |
78 | // Show mobile buttons | |
e40978c7 PO |
79 | document.getElementById('noVNC_mobile_buttons') |
80 | .classList.remove("noVNC_hidden"); | |
f0d9ab96 | 81 | UI.hideMouseButton(); |
0f6af1e3 | 82 | // Remove the address bar |
83 | setTimeout(function() { window.scrollTo(0, 1); }, 100); | |
84 | UI.forceSetting('clip', true); | |
85 | } else { | |
86 | UI.initSetting('clip', false); | |
87 | } | |
88 | ||
89 | // Setup and initialize event handlers | |
90 | UI.setupWindowEvents(); | |
91 | UI.setupFullscreen(); | |
92 | UI.addControlbarHandlers(); | |
93 | UI.addTouchSpecificHandlers(); | |
94 | UI.addXvpHandlers(); | |
95 | UI.addConnectionControlHandlers(); | |
96 | UI.addClipboardHandlers(); | |
97 | UI.addSettingsHandlers(); | |
bbbf42bb | 98 | |
f9fff037 | 99 | // Show the connect panel on first load unless autoconnecting |
100 | if (!autoconnect && !UI.connSettingsOpen) | |
101 | UI.toggleConnectPanel(); | |
0f6af1e3 | 102 | |
f0d9ab96 | 103 | UI.updateViewClip(); |
0f6af1e3 | 104 | UI.setBarPosition(); |
105 | ||
106 | UI.updateVisualState(); | |
107 | ||
108 | document.getElementById('noVNC_setting_host').focus(); | |
109 | ||
110 | var autoconnect = WebUtil.getConfigVar('autoconnect', false); | |
111 | if (autoconnect === 'true' || autoconnect == '1') { | |
112 | autoconnect = true; | |
113 | UI.connect(); | |
114 | } else { | |
115 | autoconnect = false; | |
116 | } | |
117 | ||
118 | if (typeof callback === "function") { | |
119 | callback(UI.rfb); | |
120 | } | |
121 | }, | |
122 | ||
123 | initSettings: function() { | |
bbbf42bb SR |
124 | // Stylesheet selection dropdown |
125 | var sheet = WebUtil.selectStylesheet(); | |
126 | var sheets = WebUtil.getStylesheets(); | |
127 | var i; | |
128 | for (i = 0; i < sheets.length; i += 1) { | |
ae510306 | 129 | UI.addOption(document.getElementById('noVNC_setting_stylesheet'),sheets[i].title, sheets[i].title); |
bbbf42bb SR |
130 | } |
131 | ||
132 | // Logging selection dropdown | |
133 | var llevels = ['error', 'warn', 'info', 'debug']; | |
134 | for (i = 0; i < llevels.length; i += 1) { | |
ae510306 | 135 | UI.addOption(document.getElementById('noVNC_setting_logging'),llevels[i], llevels[i]); |
bbbf42bb SR |
136 | } |
137 | ||
138 | // Settings with immediate effects | |
139 | UI.initSetting('logging', 'warn'); | |
140 | WebUtil.init_logging(UI.getSetting('logging')); | |
141 | ||
142 | UI.initSetting('stylesheet', 'default'); | |
143 | WebUtil.selectStylesheet(null); | |
144 | // call twice to get around webkit bug | |
145 | WebUtil.selectStylesheet(UI.getSetting('stylesheet')); | |
146 | ||
147 | // if port == 80 (or 443) then it won't be present and should be | |
148 | // set manually | |
149 | var port = window.location.port; | |
150 | if (!port) { | |
151 | if (window.location.protocol.substring(0,5) == 'https') { | |
152 | port = 443; | |
153 | } | |
154 | else if (window.location.protocol.substring(0,4) == 'http') { | |
155 | port = 80; | |
156 | } | |
157 | } | |
158 | ||
159 | /* Populate the controls if defaults are provided in the URL */ | |
160 | UI.initSetting('host', window.location.hostname); | |
161 | UI.initSetting('port', port); | |
162 | UI.initSetting('password', ''); | |
163 | UI.initSetting('encrypt', (window.location.protocol === "https:")); | |
164 | UI.initSetting('true_color', true); | |
165 | UI.initSetting('cursor', !UI.isTouchDevice); | |
8b46c0de | 166 | UI.initSetting('resize', 'off'); |
bbbf42bb SR |
167 | UI.initSetting('shared', true); |
168 | UI.initSetting('view_only', false); | |
169 | UI.initSetting('path', 'websockify'); | |
170 | UI.initSetting('repeaterID', ''); | |
c55f05f6 | 171 | UI.initSetting('token', ''); |
0f6af1e3 | 172 | }, |
bbbf42bb | 173 | |
0f6af1e3 | 174 | setupWindowEvents: function() { |
175 | window.addEventListener( 'resize', function () { | |
777cb7a0 | 176 | UI.applyResizeMode(); |
f0d9ab96 | 177 | UI.updateViewClip(); |
31ddaa1c | 178 | UI.updateViewDrag(); |
fdedbafb | 179 | UI.setBarPosition(); |
f8b399d7 | 180 | } ); |
bbbf42bb SR |
181 | }, |
182 | ||
0f6af1e3 | 183 | setupFullscreen: function() { |
184 | // Only show the button if fullscreen is properly supported | |
185 | // * Safari doesn't support alphanumerical input while in fullscreen | |
186 | if (!UI.isSafari && | |
187 | (document.documentElement.requestFullscreen || | |
188 | document.documentElement.mozRequestFullScreen || | |
189 | document.documentElement.webkitRequestFullscreen || | |
190 | document.body.msRequestFullscreen)) { | |
e40978c7 PO |
191 | document.getElementById('noVNC_fullscreen_button') |
192 | .classList.remove("noVNC_hidden"); | |
0f6af1e3 | 193 | UI.addFullscreenHandlers(); |
d9fc1c7b | 194 | } |
e543525f SR |
195 | }, |
196 | ||
0f6af1e3 | 197 | addControlbarHandlers: function() { |
d7f79071 | 198 | document.getElementById("noVNC_view_drag_button") |
199 | .addEventListener('click', UI.toggleViewDrag); | |
a49d9298 | 200 | document.getElementById("noVNC_send_ctrl_alt_del_button") |
0f6af1e3 | 201 | .addEventListener('click', UI.sendCtrlAltDel); |
202 | document.getElementById("noVNC_status") | |
203 | .addEventListener('click', UI.popupStatus); | |
204 | document.getElementById("noVNC_popup_status") | |
205 | .addEventListener('click', UI.closePopup); | |
0f6af1e3 | 206 | }, |
207 | ||
208 | addTouchSpecificHandlers: function() { | |
d7f79071 | 209 | document.getElementById("noVNC_mouse_button0") |
210 | .addEventListener('click', function () { UI.setMouseButton(1); }); | |
211 | document.getElementById("noVNC_mouse_button1") | |
212 | .addEventListener('click', function () { UI.setMouseButton(2); }); | |
213 | document.getElementById("noVNC_mouse_button2") | |
214 | .addEventListener('click', function () { UI.setMouseButton(4); }); | |
215 | document.getElementById("noVNC_mouse_button4") | |
216 | .addEventListener('click', function () { UI.setMouseButton(0); }); | |
217 | document.getElementById("noVNC_keyboard_button") | |
218 | .addEventListener('click', UI.showKeyboard); | |
219 | ||
220 | document.getElementById("noVNC_keyboardinput") | |
221 | .addEventListener('input', UI.keyInput); | |
222 | document.getElementById("noVNC_keyboardinput") | |
223 | .addEventListener('blur', UI.hideKeyboard); | |
224 | document.getElementById("noVNC_keyboardinput") | |
225 | .addEventListener('submit', function () { return false; }); | |
226 | ||
0f6af1e3 | 227 | window.addEventListener('load', UI.keyboardinputReset); |
228 | ||
a49d9298 | 229 | document.getElementById("noVNC_toggle_extra_keys_button") |
d7f79071 | 230 | .addEventListener('click', UI.toggleExtraKeys); |
a49d9298 | 231 | document.getElementById("noVNC_toggle_ctrl_button") |
d7f79071 | 232 | .addEventListener('click', UI.toggleCtrl); |
a49d9298 | 233 | document.getElementById("noVNC_toggle_alt_button") |
d7f79071 | 234 | .addEventListener('click', UI.toggleAlt); |
a49d9298 | 235 | document.getElementById("noVNC_send_tab_button") |
d7f79071 | 236 | .addEventListener('click', UI.sendTab); |
a49d9298 | 237 | document.getElementById("noVNC_send_esc_button") |
d7f79071 | 238 | .addEventListener('click', UI.sendEsc); |
0f6af1e3 | 239 | }, |
d7f79071 | 240 | |
0f6af1e3 | 241 | addXvpHandlers: function() { |
a49d9298 | 242 | document.getElementById("noVNC_xvp_shutdown_button") |
d7f79071 | 243 | .addEventListener('click', function() { UI.rfb.xvpShutdown(); }); |
a49d9298 | 244 | document.getElementById("noVNC_xvp_reboot_button") |
d7f79071 | 245 | .addEventListener('click', function() { UI.rfb.xvpReboot(); }); |
a49d9298 | 246 | document.getElementById("noVNC_xvp_reset_button") |
d7f79071 | 247 | .addEventListener('click', function() { UI.rfb.xvpReset(); }); |
a49d9298 | 248 | document.getElementById("noVNC_xvp_button") |
d7f79071 | 249 | .addEventListener('click', UI.toggleXvpPanel); |
0f6af1e3 | 250 | }, |
251 | ||
252 | addConnectionControlHandlers: function() { | |
a49d9298 | 253 | document.getElementById("noVNC_connect_controls_button") |
d7f79071 | 254 | .addEventListener('click', UI.toggleConnectPanel); |
255 | document.getElementById("noVNC_disconnect_button") | |
256 | .addEventListener('click', UI.disconnect); | |
0f6af1e3 | 257 | document.getElementById("noVNC_connect_button") |
258 | .addEventListener('click', UI.connect); | |
259 | }, | |
d7f79071 | 260 | |
0f6af1e3 | 261 | addClipboardHandlers: function() { |
262 | document.getElementById("noVNC_clipboard_button") | |
263 | .addEventListener('click', UI.toggleClipboardPanel); | |
d7f79071 | 264 | document.getElementById("noVNC_clipboard_text") |
265 | .addEventListener('focus', UI.displayBlur); | |
266 | document.getElementById("noVNC_clipboard_text") | |
267 | .addEventListener('blur', UI.displayFocus); | |
268 | document.getElementById("noVNC_clipboard_text") | |
269 | .addEventListener('change', UI.clipboardSend); | |
270 | document.getElementById("noVNC_clipboard_clear_button") | |
271 | .addEventListener('click', UI.clipboardClear); | |
0f6af1e3 | 272 | }, |
d7f79071 | 273 | |
0f6af1e3 | 274 | addSettingsHandlers: function() { |
275 | document.getElementById("noVNC_settings_button") | |
276 | .addEventListener('click', UI.toggleSettingsPanel); | |
d7f79071 | 277 | document.getElementById("noVNC_settings_apply") |
278 | .addEventListener('click', UI.settingsApply); | |
279 | ||
d7f79071 | 280 | document.getElementById("noVNC_setting_resize") |
281 | .addEventListener('change', UI.enableDisableViewClip); | |
bbbf42bb SR |
282 | }, |
283 | ||
0f6af1e3 | 284 | addFullscreenHandlers: function() { |
285 | document.getElementById("noVNC_fullscreen_button") | |
286 | .addEventListener('click', UI.toggleFullscreen); | |
287 | ||
288 | window.addEventListener('fullscreenchange', UI.updateFullscreenButton); | |
289 | window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton); | |
290 | window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton); | |
291 | window.addEventListener('msfullscreenchange', UI.updateFullscreenButton); | |
292 | }, | |
293 | ||
294 | initRFB: function() { | |
295 | try { | |
296 | UI.rfb = new RFB({'target': document.getElementById('noVNC_canvas'), | |
297 | 'onUpdateState': UI.updateState, | |
298 | 'onXvpInit': UI.updateXvpButton, | |
299 | 'onClipboard': UI.clipboardReceive, | |
300 | 'onFBUComplete': UI.initialResize, | |
301 | 'onFBResize': UI.updateViewDrag, | |
302 | 'onDesktopName': UI.updateDocumentTitle}); | |
303 | return true; | |
304 | } catch (exc) { | |
305 | UI.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc); | |
306 | return false; | |
307 | } | |
308 | }, | |
309 | ||
95dd6001 | 310 | /* ------^------- |
311 | * /INIT | |
312 | * ============== | |
313 | * VISUAL | |
314 | * ------v------*/ | |
58ded70d | 315 | |
29475d77 | 316 | updateState: function(rfb, state, oldstate, msg) { |
317 | UI.rfb_state = state; | |
318 | var klass; | |
319 | switch (state) { | |
320 | case 'failed': | |
321 | case 'fatal': | |
322 | klass = "noVNC_status_error"; | |
323 | break; | |
324 | case 'normal': | |
325 | klass = "noVNC_status_normal"; | |
326 | break; | |
327 | case 'disconnected': | |
29475d77 | 328 | /* falls through */ |
329 | case 'loaded': | |
330 | klass = "noVNC_status_normal"; | |
331 | break; | |
332 | case 'password': | |
333 | UI.toggleConnectPanel(); | |
fdedbafb | 334 | |
ae510306 SR |
335 | document.getElementById('noVNC_connect_button').value = "Send Password"; |
336 | document.getElementById('noVNC_connect_button').onclick = UI.setPassword; | |
337 | document.getElementById('noVNC_setting_password').focus(); | |
fdedbafb | 338 | |
29475d77 | 339 | klass = "noVNC_status_warn"; |
340 | break; | |
341 | default: | |
342 | klass = "noVNC_status_warn"; | |
343 | break; | |
344 | } | |
fdedbafb | 345 | |
29475d77 | 346 | if (typeof(msg) !== 'undefined') { |
ae510306 SR |
347 | document.getElementById('noVNC_control_bar').setAttribute("class", klass); |
348 | document.getElementById('noVNC_status').innerHTML = msg; | |
fdedbafb | 349 | } |
29475d77 | 350 | |
351 | UI.updateVisualState(); | |
fdedbafb | 352 | }, |
353 | ||
29475d77 | 354 | // Disable/enable controls depending on connection state |
355 | updateVisualState: function() { | |
356 | var connected = UI.rfb && UI.rfb_state === 'normal'; | |
fdedbafb | 357 | |
29475d77 | 358 | //Util.Debug(">> updateVisualState"); |
ae510306 SR |
359 | document.getElementById('noVNC_setting_encrypt').disabled = connected; |
360 | document.getElementById('noVNC_setting_true_color').disabled = connected; | |
29475d77 | 361 | if (Util.browserSupportsCursorURIs()) { |
ae510306 | 362 | document.getElementById('noVNC_setting_cursor').disabled = connected; |
29475d77 | 363 | } else { |
364 | UI.updateSetting('cursor', !UI.isTouchDevice); | |
ae510306 | 365 | document.getElementById('noVNC_setting_cursor').disabled = true; |
29475d77 | 366 | } |
fdedbafb | 367 | |
29475d77 | 368 | UI.enableDisableViewClip(); |
ae510306 SR |
369 | document.getElementById('noVNC_setting_resize').disabled = connected; |
370 | document.getElementById('noVNC_setting_shared').disabled = connected; | |
371 | document.getElementById('noVNC_setting_view_only').disabled = connected; | |
372 | document.getElementById('noVNC_setting_path').disabled = connected; | |
373 | document.getElementById('noVNC_setting_repeaterID').disabled = connected; | |
fdedbafb | 374 | |
29475d77 | 375 | if (connected) { |
e40978c7 PO |
376 | document.getElementById('noVNC_logo') |
377 | .classList.add("noVNC_hidden"); | |
378 | document.getElementById('noVNC_screen') | |
379 | .classList.remove("noVNC_hidden"); | |
f0d9ab96 | 380 | UI.updateViewClip(); |
29475d77 | 381 | UI.setMouseButton(1); |
e40978c7 PO |
382 | document.getElementById('noVNC_clipboard_button') |
383 | .classList.remove("noVNC_hidden"); | |
384 | document.getElementById('noVNC_keyboard_button') | |
385 | .classList.remove("noVNC_hidden"); | |
386 | document.getElementById('noVNC_extra_keys') | |
387 | .classList.remove("noVNC_hidden"); | |
a49d9298 | 388 | document.getElementById('noVNC_send_ctrl_alt_del_button') |
e40978c7 | 389 | .classList.remove("noVNC_hidden"); |
29475d77 | 390 | } else { |
e40978c7 PO |
391 | document.getElementById('noVNC_logo') |
392 | .classList.remove("noVNC_hidden"); | |
393 | document.getElementById('noVNC_screen') | |
394 | .classList.add("noVNC_hidden"); | |
f0d9ab96 | 395 | UI.hideMouseButton(); |
e40978c7 PO |
396 | document.getElementById('noVNC_clipboard_button') |
397 | .classList.add("noVNC_hidden"); | |
398 | document.getElementById('noVNC_keyboard_button') | |
399 | .classList.add("noVNC_hidden"); | |
400 | document.getElementById('noVNC_extra_keys') | |
401 | .classList.add("noVNC_hidden"); | |
a49d9298 | 402 | document.getElementById('noVNC_send_ctrl_alt_del_button') |
e40978c7 | 403 | .classList.add("noVNC_hidden"); |
9e45354e | 404 | UI.updateXvpButton(0); |
29475d77 | 405 | } |
fdedbafb | 406 | |
29475d77 | 407 | // State change disables viewport dragging. |
408 | // It is enabled (toggled) by direct click on the button | |
f0d9ab96 | 409 | UI.setViewDrag(false); |
29475d77 | 410 | |
411 | switch (UI.rfb_state) { | |
412 | case 'fatal': | |
413 | case 'failed': | |
414 | case 'disconnected': | |
a49d9298 | 415 | document.getElementById('noVNC_connect_controls_button') |
e40978c7 PO |
416 | .classList.remove("noVNC_hidden"); |
417 | document.getElementById('noVNC_disconnect_button') | |
418 | .classList.add("noVNC_hidden"); | |
29475d77 | 419 | UI.connSettingsOpen = false; |
420 | UI.toggleConnectPanel(); | |
421 | break; | |
422 | case 'loaded': | |
a49d9298 | 423 | document.getElementById('noVNC_connect_controls_button') |
e40978c7 PO |
424 | .classList.remove("noVNC_hidden"); |
425 | document.getElementById('noVNC_disconnect_button') | |
426 | .classList.add("noVNC_hidden"); | |
29475d77 | 427 | break; |
428 | default: | |
a49d9298 | 429 | document.getElementById('noVNC_connect_controls_button') |
e40978c7 PO |
430 | .classList.add("noVNC_hidden"); |
431 | document.getElementById('noVNC_disconnect_button') | |
432 | .classList.remove("noVNC_hidden"); | |
29475d77 | 433 | break; |
434 | } | |
435 | ||
436 | //Util.Debug("<< updateVisualState"); | |
437 | }, | |
438 | ||
4e471b5b | 439 | popupStatus: function(text) { |
ae510306 | 440 | var psp = document.getElementById('noVNC_popup_status'); |
4e471b5b | 441 | |
442 | clearTimeout(UI.popupStatusTimeout); | |
443 | ||
444 | if (typeof text === 'string') { | |
445 | psp.innerHTML = text; | |
fdedbafb | 446 | } else { |
ae510306 | 447 | psp.innerHTML = document.getElementById('noVNC_status').innerHTML; |
fdedbafb | 448 | } |
e40978c7 | 449 | psp.classList.remove("noVNC_hidden"); |
4e471b5b | 450 | psp.style.left = window.innerWidth/2 - |
451 | parseInt(window.getComputedStyle(psp).width)/2 -30 + "px"; | |
452 | ||
453 | // Show the popup for a maximum of 1.5 seconds | |
67685d07 | 454 | UI.popupStatusTimeout = setTimeout(UI.closePopup, 1500); |
fdedbafb | 455 | }, |
456 | ||
4e471b5b | 457 | closePopup: function() { |
458 | clearTimeout(UI.popupStatusTimeout); | |
e40978c7 | 459 | document.getElementById('noVNC_popup_status').classList.add("noVNC_hidden"); |
4e471b5b | 460 | }, |
461 | ||
95dd6001 | 462 | /* ------^------- |
463 | * /VISUAL | |
464 | * ============== | |
465 | * SETTINGS | |
466 | * ------v------*/ | |
467 | ||
45c70c9e | 468 | // Initial page load read/initialization of settings |
469 | initSetting: function(name, defVal) { | |
470 | // Check Query string followed by cookie | |
471 | var val = WebUtil.getConfigVar(name); | |
472 | if (val === null) { | |
473 | val = WebUtil.readSetting(name, defVal); | |
bbbf42bb | 474 | } |
45c70c9e | 475 | UI.updateSetting(name, val); |
bbbf42bb SR |
476 | return val; |
477 | }, | |
478 | ||
479 | // Update cookie and form control setting. If value is not set, then | |
480 | // updates from control to current cookie setting. | |
481 | updateSetting: function(name, value) { | |
482 | ||
483 | // Save the cookie for this session | |
484 | if (typeof value !== 'undefined') { | |
485 | WebUtil.writeSetting(name, value); | |
486 | } | |
487 | ||
488 | // Update the settings control | |
489 | value = UI.getSetting(name); | |
490 | ||
ae510306 | 491 | var ctrl = document.getElementById('noVNC_setting_' + name); |
bbbf42bb SR |
492 | if (ctrl.type === 'checkbox') { |
493 | ctrl.checked = value; | |
494 | ||
495 | } else if (typeof ctrl.options !== 'undefined') { | |
496 | for (var i = 0; i < ctrl.options.length; i += 1) { | |
497 | if (ctrl.options[i].value === value) { | |
498 | ctrl.selectedIndex = i; | |
499 | break; | |
500 | } | |
501 | } | |
502 | } else { | |
503 | /*Weird IE9 error leads to 'null' appearring | |
504 | in textboxes instead of ''.*/ | |
505 | if (value === null) { | |
506 | value = ""; | |
507 | } | |
508 | ctrl.value = value; | |
509 | } | |
510 | }, | |
511 | ||
512 | // Save control setting to cookie | |
513 | saveSetting: function(name) { | |
ae510306 | 514 | var val, ctrl = document.getElementById('noVNC_setting_' + name); |
bbbf42bb SR |
515 | if (ctrl.type === 'checkbox') { |
516 | val = ctrl.checked; | |
517 | } else if (typeof ctrl.options !== 'undefined') { | |
518 | val = ctrl.options[ctrl.selectedIndex].value; | |
519 | } else { | |
520 | val = ctrl.value; | |
521 | } | |
522 | WebUtil.writeSetting(name, val); | |
523 | //Util.Debug("Setting saved '" + name + "=" + val + "'"); | |
524 | return val; | |
525 | }, | |
526 | ||
bbbf42bb SR |
527 | // Force a setting to be a certain value |
528 | forceSetting: function(name, val) { | |
529 | UI.updateSetting(name, val); | |
530 | return val; | |
531 | }, | |
532 | ||
45c70c9e | 533 | // Read form control compatible setting from cookie |
534 | getSetting: function(name) { | |
ae510306 | 535 | var ctrl = document.getElementById('noVNC_setting_' + name); |
45c70c9e | 536 | var val = WebUtil.readSetting(name); |
537 | if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') { | |
538 | if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) { | |
539 | val = false; | |
4f19e5c6 | 540 | } else { |
45c70c9e | 541 | val = true; |
4f19e5c6 | 542 | } |
bbbf42bb | 543 | } |
bbbf42bb | 544 | return val; |
bbbf42bb SR |
545 | }, |
546 | ||
95dd6001 | 547 | // Save/apply settings when 'Apply' button is pressed |
548 | settingsApply: function() { | |
549 | //Util.Debug(">> settingsApply"); | |
550 | UI.saveSetting('encrypt'); | |
551 | UI.saveSetting('true_color'); | |
552 | if (Util.browserSupportsCursorURIs()) { | |
553 | UI.saveSetting('cursor'); | |
bbbf42bb | 554 | } |
bbbf42bb | 555 | |
95dd6001 | 556 | UI.saveSetting('resize'); |
bbbf42bb | 557 | |
95dd6001 | 558 | if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') { |
559 | UI.forceSetting('clip', false); | |
7d1dc09a | 560 | } |
7d1dc09a | 561 | |
95dd6001 | 562 | UI.saveSetting('clip'); |
563 | UI.saveSetting('shared'); | |
564 | UI.saveSetting('view_only'); | |
565 | UI.saveSetting('path'); | |
566 | UI.saveSetting('repeaterID'); | |
567 | UI.saveSetting('stylesheet'); | |
568 | UI.saveSetting('logging'); | |
569 | ||
570 | // Settings with immediate (non-connected related) effect | |
571 | WebUtil.selectStylesheet(UI.getSetting('stylesheet')); | |
572 | WebUtil.init_logging(UI.getSetting('logging')); | |
f0d9ab96 | 573 | UI.updateViewClip(); |
95dd6001 | 574 | UI.updateViewDrag(); |
575 | //Util.Debug("<< settingsApply"); | |
7d1dc09a | 576 | }, |
577 | ||
45c70c9e | 578 | // Open menu |
579 | openSettingsMenu: function() { | |
bbbf42bb SR |
580 | // Close clipboard panel if open |
581 | if (UI.clipboardOpen === true) { | |
582 | UI.toggleClipboardPanel(); | |
583 | } | |
45c70c9e | 584 | // Close connection settings if open |
585 | if (UI.connSettingsOpen === true) { | |
586 | UI.toggleConnectPanel(); | |
587 | } | |
bbbf42bb SR |
588 | // Close XVP panel if open |
589 | if (UI.xvpOpen === true) { | |
590 | UI.toggleXvpPanel(); | |
591 | } | |
e40978c7 PO |
592 | document.getElementById('noVNC_settings') |
593 | .classList.add("noVNC_open"); | |
594 | document.getElementById('noVNC_settings_button') | |
a49d9298 | 595 | .className = "noVNC_button_selected"; |
45c70c9e | 596 | UI.settingsOpen = true; |
597 | }, | |
bbbf42bb | 598 | |
45c70c9e | 599 | // Close menu (without applying settings) |
600 | closeSettingsMenu: function() { | |
e40978c7 PO |
601 | document.getElementById('noVNC_settings') |
602 | .classList.remove("noVNC_open"); | |
603 | document.getElementById('noVNC_settings_button') | |
a49d9298 | 604 | .className = "noVNC_button"; |
45c70c9e | 605 | UI.settingsOpen = false; |
bbbf42bb SR |
606 | }, |
607 | ||
608 | // Toggle the settings menu: | |
609 | // On open, settings are refreshed from saved cookies. | |
610 | // On close, settings are applied | |
611 | toggleSettingsPanel: function() { | |
bbbf42bb SR |
612 | if (UI.settingsOpen) { |
613 | UI.settingsApply(); | |
614 | UI.closeSettingsMenu(); | |
615 | } else { | |
616 | UI.updateSetting('encrypt'); | |
617 | UI.updateSetting('true_color'); | |
58ded70d | 618 | if (Util.browserSupportsCursorURIs()) { |
bbbf42bb SR |
619 | UI.updateSetting('cursor'); |
620 | } else { | |
621 | UI.updateSetting('cursor', !UI.isTouchDevice); | |
ae510306 | 622 | document.getElementById('noVNC_setting_cursor').disabled = true; |
bbbf42bb SR |
623 | } |
624 | UI.updateSetting('clip'); | |
f8b399d7 | 625 | UI.updateSetting('resize'); |
bbbf42bb SR |
626 | UI.updateSetting('shared'); |
627 | UI.updateSetting('view_only'); | |
628 | UI.updateSetting('path'); | |
629 | UI.updateSetting('repeaterID'); | |
630 | UI.updateSetting('stylesheet'); | |
631 | UI.updateSetting('logging'); | |
632 | ||
633 | UI.openSettingsMenu(); | |
634 | } | |
635 | }, | |
636 | ||
95dd6001 | 637 | /* ------^------- |
638 | * /SETTINGS | |
639 | * ============== | |
640 | * XVP | |
641 | * ------v------*/ | |
642 | ||
bbbf42bb SR |
643 | // Show the XVP panel |
644 | toggleXvpPanel: function() { | |
bbbf42bb SR |
645 | // Close settings if open |
646 | if (UI.settingsOpen === true) { | |
647 | UI.settingsApply(); | |
648 | UI.closeSettingsMenu(); | |
bbbf42bb SR |
649 | } |
650 | // Close connection settings if open | |
651 | if (UI.connSettingsOpen === true) { | |
652 | UI.toggleConnectPanel(); | |
653 | } | |
bbbf42bb SR |
654 | // Close clipboard panel if open |
655 | if (UI.clipboardOpen === true) { | |
656 | UI.toggleClipboardPanel(); | |
bbbf42bb | 657 | } |
bbbf42bb SR |
658 | // Toggle XVP panel |
659 | if (UI.xvpOpen === true) { | |
e40978c7 PO |
660 | document.getElementById('noVNC_xvp') |
661 | .classList.remove("noVNC_open"); | |
a49d9298 | 662 | document.getElementById('noVNC_xvp_button') |
663 | .className = "noVNC_button"; | |
bbbf42bb SR |
664 | UI.xvpOpen = false; |
665 | } else { | |
e40978c7 PO |
666 | document.getElementById('noVNC_xvp') |
667 | .classList.add("noVNC_open"); | |
a49d9298 | 668 | document.getElementById('noVNC_xvp_button') |
669 | .className = "noVNC_button_selected"; | |
bbbf42bb | 670 | UI.xvpOpen = true; |
bbbf42bb | 671 | } |
bbbf42bb SR |
672 | }, |
673 | ||
674 | // Disable/enable XVP button | |
9e45354e | 675 | updateXvpButton: function(ver) { |
bbbf42bb | 676 | if (ver >= 1) { |
a49d9298 | 677 | document.getElementById('noVNC_xvp_button') |
e40978c7 | 678 | .classList.remove("noVNC_hidden"); |
bbbf42bb | 679 | } else { |
a49d9298 | 680 | document.getElementById('noVNC_xvp_button') |
e40978c7 | 681 | .classList.add("noVNC_hidden"); |
bbbf42bb SR |
682 | // Close XVP panel if open |
683 | if (UI.xvpOpen === true) { | |
684 | UI.toggleXvpPanel(); | |
685 | } | |
686 | } | |
687 | }, | |
688 | ||
95dd6001 | 689 | /* ------^------- |
690 | * /XVP | |
691 | * ============== | |
692 | * CLIPBOARD | |
693 | * ------v------*/ | |
f8b399d7 | 694 | |
bbbf42bb SR |
695 | // Show the clipboard panel |
696 | toggleClipboardPanel: function() { | |
bbbf42bb SR |
697 | // Close settings if open |
698 | if (UI.settingsOpen === true) { | |
699 | UI.settingsApply(); | |
700 | UI.closeSettingsMenu(); | |
701 | } | |
702 | // Close connection settings if open | |
703 | if (UI.connSettingsOpen === true) { | |
704 | UI.toggleConnectPanel(); | |
705 | } | |
bbbf42bb SR |
706 | // Close XVP panel if open |
707 | if (UI.xvpOpen === true) { | |
708 | UI.toggleXvpPanel(); | |
709 | } | |
710 | // Toggle Clipboard Panel | |
711 | if (UI.clipboardOpen === true) { | |
e40978c7 PO |
712 | document.getElementById('noVNC_clipboard') |
713 | .classList.remove("noVNC_open"); | |
714 | document.getElementById('noVNC_clipboard_button') | |
a49d9298 | 715 | .className = "noVNC_button"; |
bbbf42bb SR |
716 | UI.clipboardOpen = false; |
717 | } else { | |
e40978c7 PO |
718 | document.getElementById('noVNC_clipboard') |
719 | .classList.add("noVNC_open"); | |
720 | document.getElementById('noVNC_clipboard_button') | |
a49d9298 | 721 | .className = "noVNC_button_selected"; |
bbbf42bb SR |
722 | UI.clipboardOpen = true; |
723 | } | |
bbbf42bb SR |
724 | }, |
725 | ||
4d26f58e | 726 | clipboardReceive: function(rfb, text) { |
727 | Util.Debug(">> UI.clipboardReceive: " + text.substr(0,40) + "..."); | |
ae510306 | 728 | document.getElementById('noVNC_clipboard_text').value = text; |
4d26f58e | 729 | Util.Debug("<< UI.clipboardReceive"); |
95dd6001 | 730 | }, |
731 | ||
4d26f58e | 732 | clipboardClear: function() { |
ae510306 | 733 | document.getElementById('noVNC_clipboard_text').value = ""; |
95dd6001 | 734 | UI.rfb.clipboardPasteFrom(""); |
735 | }, | |
736 | ||
4d26f58e | 737 | clipboardSend: function() { |
ae510306 | 738 | var text = document.getElementById('noVNC_clipboard_text').value; |
4d26f58e | 739 | Util.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "..."); |
95dd6001 | 740 | UI.rfb.clipboardPasteFrom(text); |
4d26f58e | 741 | Util.Debug("<< UI.clipboardSend"); |
95dd6001 | 742 | }, |
743 | ||
744 | /* ------^------- | |
745 | * /CLIPBOARD | |
746 | * ============== | |
747 | * CONNECTION | |
748 | * ------v------*/ | |
749 | ||
ab81ddf5 | 750 | // Show the connection settings panel/menu |
751 | toggleConnectPanel: function() { | |
ab81ddf5 | 752 | // Close connection settings if open |
753 | if (UI.settingsOpen === true) { | |
754 | UI.settingsApply(); | |
755 | UI.closeSettingsMenu(); | |
a49d9298 | 756 | document.getElementById('noVNC_connect_controls_button') |
757 | .className = "noVNC_button"; | |
ab81ddf5 | 758 | } |
759 | // Close clipboard panel if open | |
760 | if (UI.clipboardOpen === true) { | |
761 | UI.toggleClipboardPanel(); | |
762 | } | |
763 | // Close XVP panel if open | |
764 | if (UI.xvpOpen === true) { | |
765 | UI.toggleXvpPanel(); | |
766 | } | |
767 | ||
768 | // Toggle Connection Panel | |
769 | if (UI.connSettingsOpen === true) { | |
a49d9298 | 770 | document.getElementById('noVNC_connect_controls') |
e40978c7 | 771 | .classList.remove("noVNC_open"); |
a49d9298 | 772 | document.getElementById('noVNC_connect_controls_button') |
773 | .className = "noVNC_button"; | |
ab81ddf5 | 774 | UI.connSettingsOpen = false; |
775 | UI.saveSetting('host'); | |
776 | UI.saveSetting('port'); | |
777 | UI.saveSetting('token'); | |
778 | //UI.saveSetting('password'); | |
779 | } else { | |
a49d9298 | 780 | document.getElementById('noVNC_connect_controls') |
e40978c7 | 781 | .classList.add("noVNC_open"); |
a49d9298 | 782 | document.getElementById('noVNC_connect_controls_button') |
783 | .className = "noVNC_button_selected"; | |
ab81ddf5 | 784 | UI.connSettingsOpen = true; |
ae510306 | 785 | document.getElementById('noVNC_setting_host').focus(); |
ab81ddf5 | 786 | } |
bbbf42bb SR |
787 | }, |
788 | ||
789 | connect: function() { | |
790 | UI.closeSettingsMenu(); | |
c506a481 | 791 | UI.toggleConnectPanel(); |
f00b1e37 | 792 | |
ae510306 SR |
793 | var host = document.getElementById('noVNC_setting_host').value; |
794 | var port = document.getElementById('noVNC_setting_port').value; | |
795 | var password = document.getElementById('noVNC_setting_password').value; | |
796 | var token = document.getElementById('noVNC_setting_token').value; | |
797 | var path = document.getElementById('noVNC_setting_path').value; | |
c55f05f6 MXPN |
798 | |
799 | //if token is in path then ignore the new token variable | |
800 | if (token) { | |
801 | path = WebUtil.injectParamIfMissing(path, "token", token); | |
802 | } | |
803 | ||
bbbf42bb SR |
804 | if ((!host) || (!port)) { |
805 | throw new Error("Must set host and port"); | |
806 | } | |
53fc7392 | 807 | |
d9fc1c7b | 808 | if (!UI.initRFB()) return; |
58ded70d | 809 | |
bbbf42bb SR |
810 | UI.rfb.set_encrypt(UI.getSetting('encrypt')); |
811 | UI.rfb.set_true_color(UI.getSetting('true_color')); | |
812 | UI.rfb.set_local_cursor(UI.getSetting('cursor')); | |
813 | UI.rfb.set_shared(UI.getSetting('shared')); | |
814 | UI.rfb.set_view_only(UI.getSetting('view_only')); | |
815 | UI.rfb.set_repeaterID(UI.getSetting('repeaterID')); | |
53fc7392 | 816 | |
bbbf42bb | 817 | UI.rfb.connect(host, port, password, path); |
8e0f0088 | 818 | |
bbbf42bb | 819 | //Close dialog. |
67685d07 | 820 | setTimeout(UI.setBarPosition, 100); |
bbbf42bb | 821 | }, |
5299db1a | 822 | |
bbbf42bb SR |
823 | disconnect: function() { |
824 | UI.closeSettingsMenu(); | |
825 | UI.rfb.disconnect(); | |
8e0f0088 | 826 | |
f8b399d7 | 827 | // Restore the callback used for initial resize |
ab81ddf5 | 828 | UI.rfb.set_onFBUComplete(UI.initialResize); |
f8b399d7 | 829 | |
e543525f | 830 | // Don't display the connection settings until we're actually disconnected |
bbbf42bb SR |
831 | }, |
832 | ||
95dd6001 | 833 | setPassword: function() { |
ae510306 | 834 | UI.rfb.sendPassword(document.getElementById('noVNC_setting_password').value); |
95dd6001 | 835 | //Reset connect button. |
ae510306 SR |
836 | document.getElementById('noVNC_connect_button').value = "Connect"; |
837 | document.getElementById('noVNC_connect_button').onclick = UI.connect; | |
95dd6001 | 838 | //Hide connection panel. |
839 | UI.toggleConnectPanel(); | |
840 | return false; | |
841 | }, | |
58ded70d | 842 | |
95dd6001 | 843 | /* ------^------- |
844 | * /CONNECTION | |
845 | * ============== | |
846 | * FULLSCREEN | |
847 | * ------v------*/ | |
848 | ||
7d1dc09a | 849 | toggleFullscreen: function() { |
850 | if (document.fullscreenElement || // alternative standard method | |
851 | document.mozFullScreenElement || // currently working methods | |
852 | document.webkitFullscreenElement || | |
a6357e82 | 853 | document.msFullscreenElement) { |
7d1dc09a | 854 | if (document.exitFullscreen) { |
855 | document.exitFullscreen(); | |
856 | } else if (document.mozCancelFullScreen) { | |
857 | document.mozCancelFullScreen(); | |
858 | } else if (document.webkitExitFullscreen) { | |
859 | document.webkitExitFullscreen(); | |
860 | } else if (document.msExitFullscreen) { | |
861 | document.msExitFullscreen(); | |
862 | } | |
863 | } else { | |
864 | if (document.documentElement.requestFullscreen) { | |
865 | document.documentElement.requestFullscreen(); | |
866 | } else if (document.documentElement.mozRequestFullScreen) { | |
867 | document.documentElement.mozRequestFullScreen(); | |
868 | } else if (document.documentElement.webkitRequestFullscreen) { | |
869 | document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); | |
870 | } else if (document.body.msRequestFullscreen) { | |
871 | document.body.msRequestFullscreen(); | |
872 | } | |
873 | } | |
a6357e82 | 874 | UI.enableDisableViewClip(); |
7d1dc09a | 875 | UI.updateFullscreenButton(); |
bbbf42bb SR |
876 | }, |
877 | ||
7d1dc09a | 878 | updateFullscreenButton: function() { |
879 | if (document.fullscreenElement || // alternative standard method | |
880 | document.mozFullScreenElement || // currently working methods | |
881 | document.webkitFullscreenElement || | |
882 | document.msFullscreenElement ) { | |
a49d9298 | 883 | document.getElementById('noVNC_fullscreen_button') |
884 | .className = "noVNC_button_selected"; | |
7d1dc09a | 885 | } else { |
a49d9298 | 886 | document.getElementById('noVNC_fullscreen_button') |
887 | .className = "noVNC_button"; | |
7d1dc09a | 888 | } |
889 | }, | |
890 | ||
95dd6001 | 891 | /* ------^------- |
892 | * /FULLSCREEN | |
893 | * ============== | |
894 | * RESIZE | |
895 | * ------v------*/ | |
777cb7a0 | 896 | |
897 | // Apply remote resizing or local scaling | |
0bd2cbac | 898 | applyResizeMode: function() { |
58ded70d SR |
899 | if (!UI.rfb) return; |
900 | ||
777cb7a0 | 901 | var screen = UI.screenSize(); |
902 | ||
903 | if (screen && UI.rfb_state === 'normal' && UI.rfb.get_display()) { | |
904 | ||
905 | var display = UI.rfb.get_display(); | |
906 | var resizeMode = UI.getSetting('resize'); | |
907 | ||
908 | if (resizeMode === 'remote') { | |
909 | ||
910 | // Request changing the resolution of the remote display to | |
911 | // the size of the local browser viewport. | |
912 | ||
913 | // In order to not send multiple requests before the browser-resize | |
914 | // is finished we wait 0.5 seconds before sending the request. | |
915 | clearTimeout(UI.resizeTimeout); | |
916 | UI.resizeTimeout = setTimeout(function(){ | |
917 | ||
918 | // Limit the viewport to the size of the browser window | |
919 | display.set_maxWidth(screen.w); | |
920 | display.set_maxHeight(screen.h); | |
921 | ||
5fd3f88e | 922 | Util.Debug('Attempting requestDesktopSize(' + |
777cb7a0 | 923 | screen.w + ', ' + screen.h + ')'); |
924 | ||
925 | // Request a remote size covering the viewport | |
5fd3f88e | 926 | UI.rfb.requestDesktopSize(screen.w, screen.h); |
777cb7a0 | 927 | }, 500); |
928 | ||
929 | } else if (resizeMode === 'scale' || resizeMode === 'downscale') { | |
930 | var downscaleOnly = resizeMode === 'downscale'; | |
931 | var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly); | |
932 | UI.rfb.get_mouse().set_scale(scaleRatio); | |
933 | Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale()); | |
934 | } | |
935 | } | |
bbbf42bb SR |
936 | }, |
937 | ||
777cb7a0 | 938 | // The screen is always the same size as the available viewport |
939 | // in the browser window minus the height of the control bar | |
0bd2cbac | 940 | screenSize: function() { |
ae510306 | 941 | var screen = document.getElementById('noVNC_screen'); |
777cb7a0 | 942 | |
943 | // Hide the scrollbars until the size is calculated | |
944 | screen.style.overflow = "hidden"; | |
945 | ||
946 | var pos = Util.getPosition(screen); | |
947 | var w = pos.width; | |
948 | var h = pos.height; | |
949 | ||
950 | screen.style.overflow = "visible"; | |
951 | ||
952 | if (isNaN(w) || isNaN(h)) { | |
953 | return false; | |
954 | } else { | |
955 | return {w: w, h: h}; | |
956 | } | |
bbbf42bb SR |
957 | }, |
958 | ||
777cb7a0 | 959 | // Normally we only apply the current resize mode after a window resize |
960 | // event. This means that when a new connection is opened, there is no | |
961 | // resize mode active. | |
962 | // We have to wait until the first FBU because this is where the client | |
963 | // will find the supported encodings of the server. Some calls later in | |
964 | // the chain is dependant on knowing the server-capabilities. | |
965 | initialResize: function(rfb, fbu) { | |
966 | UI.applyResizeMode(); | |
967 | // After doing this once, we remove the callback. | |
968 | UI.rfb.set_onFBUComplete(function() { }); | |
bbbf42bb SR |
969 | }, |
970 | ||
95dd6001 | 971 | /* ------^------- |
972 | * /RESIZE | |
973 | * ============== | |
974 | * CLIPPING | |
975 | * ------v------*/ | |
976 | ||
30bfff81 | 977 | // Set and configure viewport clipping |
bbbf42bb | 978 | setViewClip: function(clip) { |
f0d9ab96 | 979 | UI.updateSetting('clip', clip); |
980 | UI.updateViewClip(); | |
981 | }, | |
982 | ||
983 | // Update parameters that depend on the clip setting | |
984 | updateViewClip: function() { | |
bbbf42bb | 985 | var display; |
f0d9ab96 | 986 | if (!UI.rfb) { |
bbbf42bb SR |
987 | return; |
988 | } | |
8e0f0088 | 989 | |
f0d9ab96 | 990 | var display = UI.rfb.get_display(); |
bbbf42bb | 991 | var cur_clip = display.get_viewport(); |
f0d9ab96 | 992 | var new_clip = UI.getSetting('clip'); |
bbbf42bb | 993 | |
f0d9ab96 | 994 | if (cur_clip !== new_clip) { |
995 | display.set_viewport(new_clip); | |
bbbf42bb | 996 | } |
8e0f0088 | 997 | |
f0d9ab96 | 998 | var size = UI.screenSize(); |
fdedbafb | 999 | |
f0d9ab96 | 1000 | if (new_clip && size) { |
1001 | // When clipping is enabled, the screen is limited to | |
1002 | // the size of the browser window. | |
1003 | display.set_maxWidth(size.w); | |
1004 | display.set_maxHeight(size.h); | |
fdedbafb | 1005 | |
f0d9ab96 | 1006 | var screen = document.getElementById('noVNC_screen'); |
1007 | var canvas = document.getElementById('noVNC_canvas'); | |
fdedbafb | 1008 | |
f0d9ab96 | 1009 | // Hide potential scrollbars that can skew the position |
1010 | screen.style.overflow = "hidden"; | |
fdedbafb | 1011 | |
f0d9ab96 | 1012 | // The x position marks the left margin of the canvas, |
1013 | // remove the margin from both sides to keep it centered. | |
1014 | var new_w = size.w - (2 * Util.getPosition(canvas).x); | |
fdedbafb | 1015 | |
f0d9ab96 | 1016 | screen.style.overflow = "visible"; |
1017 | ||
1018 | display.viewportChangeSize(new_w, size.h); | |
1019 | } else { | |
1020 | // Disable max dimensions | |
1021 | display.set_maxWidth(0); | |
1022 | display.set_maxHeight(0); | |
1023 | display.viewportChangeSize(); | |
bbbf42bb SR |
1024 | } |
1025 | }, | |
1026 | ||
30bfff81 | 1027 | // Handle special cases where clipping is forced on/off or locked |
0bd2cbac | 1028 | enableDisableViewClip: function() { |
ae510306 | 1029 | var resizeSetting = document.getElementById('noVNC_setting_resize'); |
a6357e82 | 1030 | var connected = UI.rfb && UI.rfb_state === 'normal'; |
1031 | ||
f620259b | 1032 | if (UI.isSafari) { |
1033 | // Safari auto-hides the scrollbars which makes them | |
1034 | // impossible to use in most cases | |
1035 | UI.setViewClip(true); | |
ae510306 | 1036 | document.getElementById('noVNC_setting_clip').disabled = true; |
682fd02b | 1037 | } else if (resizeSetting.value === 'downscale' || resizeSetting.value === 'scale') { |
a6357e82 | 1038 | // Disable clipping if we are scaling |
1039 | UI.setViewClip(false); | |
ae510306 | 1040 | document.getElementById('noVNC_setting_clip').disabled = true; |
a6357e82 | 1041 | } else if (document.msFullscreenElement) { |
1042 | // The browser is IE and we are in fullscreen mode. | |
1043 | // - We need to force clipping while in fullscreen since | |
1044 | // scrollbars doesn't work. | |
4e471b5b | 1045 | UI.popupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen"); |
a6357e82 | 1046 | UI.rememberedClipSetting = UI.getSetting('clip'); |
1047 | UI.setViewClip(true); | |
ae510306 | 1048 | document.getElementById('noVNC_setting_clip').disabled = true; |
a6357e82 | 1049 | } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) { |
1050 | // Restore view clip to what it was before fullscreen on IE | |
1051 | UI.setViewClip(UI.rememberedClipSetting); | |
ae510306 | 1052 | document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice; |
30bfff81 | 1053 | } else { |
ae510306 | 1054 | document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice; |
30bfff81 | 1055 | if (UI.isTouchDevice) { |
a6357e82 | 1056 | UI.setViewClip(true); |
30bfff81 | 1057 | } |
1058 | } | |
1059 | }, | |
1060 | ||
95dd6001 | 1061 | /* ------^------- |
1062 | * /CLIPPING | |
1063 | * ============== | |
1064 | * VIEWDRAG | |
1065 | * ------v------*/ | |
1066 | ||
f0d9ab96 | 1067 | toggleViewDrag: function() { |
58ded70d | 1068 | if (!UI.rfb) return; |
bbbf42bb | 1069 | |
f0d9ab96 | 1070 | var drag = UI.rfb.get_viewportDrag(); |
1071 | UI.setViewDrag(!drag); | |
1072 | }, | |
1073 | ||
1074 | // Set the view drag mode which moves the viewport on mouse drags | |
1075 | setViewDrag: function(drag) { | |
1076 | if (!UI.rfb) return; | |
1077 | ||
1078 | UI.rfb.set_viewportDrag(drag); | |
1079 | ||
1080 | UI.updateViewDrag(); | |
1081 | }, | |
1082 | ||
1083 | updateViewDrag: function() { | |
1084 | var clipping = false; | |
bbbf42bb | 1085 | |
e00698fe | 1086 | // Check if viewport drag is possible. It is only possible |
1087 | // if the remote display is clipping the client display. | |
f8b399d7 | 1088 | if (UI.rfb_state === 'normal' && |
1089 | UI.rfb.get_display().get_viewport() && | |
fdedbafb | 1090 | UI.rfb.get_display().clippingDisplay()) { |
f0d9ab96 | 1091 | clipping = true; |
1092 | } | |
31ddaa1c | 1093 | |
f0d9ab96 | 1094 | var viewDragButton = document.getElementById('noVNC_view_drag_button'); |
29a0e6a8 | 1095 | |
f0d9ab96 | 1096 | if (UI.rfb_state !== 'normal') { |
1097 | // Always hide when not connected | |
e40978c7 | 1098 | viewDragButton.classList.add("noVNC_hidden"); |
31ddaa1c | 1099 | } else { |
f0d9ab96 | 1100 | if (!clipping && |
1101 | UI.rfb.get_viewportDrag()) { | |
1102 | // The size of the remote display is the same or smaller | |
1103 | // than the client display. Make sure viewport drag isn't | |
1104 | // active when it can't be used. | |
31ddaa1c | 1105 | UI.rfb.set_viewportDrag(false); |
1106 | } | |
1107 | ||
f0d9ab96 | 1108 | if (UI.rfb.get_viewportDrag()) { |
a49d9298 | 1109 | viewDragButton.className = "noVNC_button_selected"; |
31ddaa1c | 1110 | } else { |
a49d9298 | 1111 | viewDragButton.className = "noVNC_button"; |
31ddaa1c | 1112 | } |
29a0e6a8 | 1113 | |
f0d9ab96 | 1114 | // Different behaviour for touch vs non-touch |
1115 | // The button is disabled instead of hidden on touch devices | |
1116 | if (UI.isTouchDevice) { | |
e40978c7 | 1117 | viewDragButton.classList.remove("noVNC_hidden"); |
31ddaa1c | 1118 | |
f0d9ab96 | 1119 | if (clipping) { |
1120 | viewDragButton.disabled = false; | |
1121 | } else { | |
1122 | viewDragButton.disabled = true; | |
1123 | } | |
1124 | } else { | |
1125 | viewDragButton.disabled = false; | |
1126 | ||
1127 | if (clipping) { | |
e40978c7 | 1128 | viewDragButton.classList.remove("noVNC_hidden"); |
f0d9ab96 | 1129 | } else { |
e40978c7 | 1130 | viewDragButton.classList.add("noVNC_hidden"); |
f0d9ab96 | 1131 | } |
1132 | } | |
f8b399d7 | 1133 | } |
1134 | }, | |
1135 | ||
95dd6001 | 1136 | /* ------^------- |
1137 | * /VIEWDRAG | |
1138 | * ============== | |
1139 | * KEYBOARD | |
1140 | * ------v------*/ | |
fdf21468 | 1141 | |
bbbf42bb SR |
1142 | // On touch devices, show the OS keyboard |
1143 | showKeyboard: function() { | |
ae510306 SR |
1144 | var kbi = document.getElementById('noVNC_keyboardinput'); |
1145 | var skb = document.getElementById('noVNC_keyboard_button'); | |
bbbf42bb SR |
1146 | var l = kbi.value.length; |
1147 | if(UI.keyboardVisible === false) { | |
1148 | kbi.focus(); | |
1149 | try { kbi.setSelectionRange(l, l); } // Move the caret to the end | |
1150 | catch (err) {} // setSelectionRange is undefined in Google Chrome | |
1151 | UI.keyboardVisible = true; | |
a49d9298 | 1152 | skb.className = "noVNC_button_selected"; |
bbbf42bb SR |
1153 | } else if(UI.keyboardVisible === true) { |
1154 | kbi.blur(); | |
a49d9298 | 1155 | skb.className = "noVNC_button"; |
bbbf42bb SR |
1156 | UI.keyboardVisible = false; |
1157 | } | |
1158 | }, | |
1159 | ||
fdf21468 | 1160 | hideKeyboard: function() { |
a49d9298 | 1161 | document.getElementById('noVNC_keyboard_button') |
1162 | .className = "noVNC_button"; | |
fdf21468 | 1163 | //Weird bug in iOS if you change keyboardVisible |
1164 | //here it does not actually occur so next time | |
1165 | //you click keyboard icon it doesnt work. | |
1166 | UI.hideKeyboardTimeout = setTimeout(function() { | |
1167 | UI.keyboardVisible = false; | |
1168 | },100); | |
1169 | }, | |
1170 | ||
bbbf42bb SR |
1171 | keepKeyboard: function() { |
1172 | clearTimeout(UI.hideKeyboardTimeout); | |
1173 | if(UI.keyboardVisible === true) { | |
ae510306 | 1174 | document.getElementById('noVNC_keyboardinput').focus(); |
a49d9298 | 1175 | document.getElementById('noVNC_keyboard_button') |
1176 | .className = "noVNC_button_selected"; | |
bbbf42bb | 1177 | } else if(UI.keyboardVisible === false) { |
ae510306 | 1178 | document.getElementById('noVNC_keyboardinput').blur(); |
a49d9298 | 1179 | document.getElementById('noVNC_keyboard_button') |
1180 | .className = "noVNC_button"; | |
bbbf42bb SR |
1181 | } |
1182 | }, | |
1183 | ||
1184 | keyboardinputReset: function() { | |
ae510306 | 1185 | var kbi = document.getElementById('noVNC_keyboardinput'); |
bbbf42bb SR |
1186 | kbi.value = new Array(UI.defaultKeyboardinputLen).join("_"); |
1187 | UI.lastKeyboardinput = kbi.value; | |
1188 | }, | |
1189 | ||
1190 | // When normal keyboard events are left uncought, use the input events from | |
1191 | // the keyboardinput element instead and generate the corresponding key events. | |
1192 | // This code is required since some browsers on Android are inconsistent in | |
1193 | // sending keyCodes in the normal keyboard events when using on screen keyboards. | |
1194 | keyInput: function(event) { | |
3b8ec46f | 1195 | |
58ded70d | 1196 | if (!UI.rfb) return; |
3b8ec46f | 1197 | |
bbbf42bb | 1198 | var newValue = event.target.value; |
1138bdd4 | 1199 | |
1200 | if (!UI.lastKeyboardinput) { | |
1201 | UI.keyboardinputReset(); | |
1202 | } | |
cb3e4deb | 1203 | var oldValue = UI.lastKeyboardinput; |
bbbf42bb SR |
1204 | |
1205 | var newLen; | |
1206 | try { | |
1207 | // Try to check caret position since whitespace at the end | |
1208 | // will not be considered by value.length in some browsers | |
1209 | newLen = Math.max(event.target.selectionStart, newValue.length); | |
1210 | } catch (err) { | |
1211 | // selectionStart is undefined in Google Chrome | |
1212 | newLen = newValue.length; | |
1213 | } | |
1214 | var oldLen = oldValue.length; | |
1215 | ||
1216 | var backspaces; | |
1217 | var inputs = newLen - oldLen; | |
1218 | if (inputs < 0) { | |
1219 | backspaces = -inputs; | |
1220 | } else { | |
1221 | backspaces = 0; | |
1222 | } | |
8e0f0088 | 1223 | |
bbbf42bb SR |
1224 | // Compare the old string with the new to account for |
1225 | // text-corrections or other input that modify existing text | |
1226 | var i; | |
1227 | for (i = 0; i < Math.min(oldLen, newLen); i++) { | |
1228 | if (newValue.charAt(i) != oldValue.charAt(i)) { | |
1229 | inputs = newLen - i; | |
1230 | backspaces = oldLen - i; | |
1231 | break; | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | // Send the key events | |
1236 | for (i = 0; i < backspaces; i++) { | |
ae510306 | 1237 | UI.rfb.sendKey(KeyTable.XK_BackSpace); |
bbbf42bb SR |
1238 | } |
1239 | for (i = newLen - inputs; i < newLen; i++) { | |
1240 | UI.rfb.sendKey(newValue.charCodeAt(i)); | |
1241 | } | |
1242 | ||
1243 | // Control the text content length in the keyboardinput element | |
1244 | if (newLen > 2 * UI.defaultKeyboardinputLen) { | |
1245 | UI.keyboardinputReset(); | |
1246 | } else if (newLen < 1) { | |
1247 | // There always have to be some text in the keyboardinput | |
1248 | // element with which backspace can interact. | |
1249 | UI.keyboardinputReset(); | |
1250 | // This sometimes causes the keyboard to disappear for a second | |
1251 | // but it is required for the android keyboard to recognize that | |
1252 | // text has been added to the field | |
1253 | event.target.blur(); | |
1254 | // This has to be ran outside of the input handler in order to work | |
67685d07 | 1255 | setTimeout(UI.keepKeyboard, 0); |
bbbf42bb SR |
1256 | } else { |
1257 | UI.lastKeyboardinput = newValue; | |
1258 | } | |
1259 | }, | |
1260 | ||
cd611a53 | 1261 | toggleExtraKeys: function() { |
bbbf42bb SR |
1262 | UI.keepKeyboard(); |
1263 | if(UI.extraKeysVisible === false) { | |
e40978c7 PO |
1264 | document.getElementById('noVNC_modifiers') |
1265 | .classList.add("noVNC_open"); | |
a49d9298 | 1266 | document.getElementById('noVNC_toggle_extra_keys_button') |
1267 | .className = "noVNC_button_selected"; | |
bbbf42bb SR |
1268 | UI.extraKeysVisible = true; |
1269 | } else if(UI.extraKeysVisible === true) { | |
e40978c7 PO |
1270 | document.getElementById('noVNC_modifiers') |
1271 | .classList.remove("noVNC_open"); | |
a49d9298 | 1272 | document.getElementById('noVNC_toggle_extra_keys_button') |
1273 | .className = "noVNC_button"; | |
bbbf42bb SR |
1274 | UI.extraKeysVisible = false; |
1275 | } | |
1276 | }, | |
1277 | ||
fdf21468 | 1278 | sendEsc: function() { |
1279 | UI.keepKeyboard(); | |
ae510306 | 1280 | UI.rfb.sendKey(KeyTable.XK_Escape); |
fdf21468 | 1281 | }, |
1282 | ||
1283 | sendTab: function() { | |
1284 | UI.keepKeyboard(); | |
ae510306 | 1285 | UI.rfb.sendKey(KeyTable.XK_Tab); |
fdf21468 | 1286 | }, |
1287 | ||
bbbf42bb SR |
1288 | toggleCtrl: function() { |
1289 | UI.keepKeyboard(); | |
1290 | if(UI.ctrlOn === false) { | |
ae510306 | 1291 | UI.rfb.sendKey(KeyTable.XK_Control_L, true); |
a49d9298 | 1292 | document.getElementById('noVNC_toggle_ctrl_button') |
1293 | .className = "noVNC_button_selected"; | |
bbbf42bb SR |
1294 | UI.ctrlOn = true; |
1295 | } else if(UI.ctrlOn === true) { | |
ae510306 | 1296 | UI.rfb.sendKey(KeyTable.XK_Control_L, false); |
a49d9298 | 1297 | document.getElementById('noVNC_toggle_ctrl_button') |
1298 | .className = "noVNC_button"; | |
bbbf42bb SR |
1299 | UI.ctrlOn = false; |
1300 | } | |
1301 | }, | |
1302 | ||
1303 | toggleAlt: function() { | |
1304 | UI.keepKeyboard(); | |
1305 | if(UI.altOn === false) { | |
ae510306 | 1306 | UI.rfb.sendKey(KeyTable.XK_Alt_L, true); |
a49d9298 | 1307 | document.getElementById('noVNC_toggle_alt_button') |
1308 | .className = "noVNC_button_selected"; | |
bbbf42bb SR |
1309 | UI.altOn = true; |
1310 | } else if(UI.altOn === true) { | |
ae510306 | 1311 | UI.rfb.sendKey(KeyTable.XK_Alt_L, false); |
a49d9298 | 1312 | document.getElementById('noVNC_toggle_alt_button') |
1313 | .className = "noVNC_button"; | |
bbbf42bb SR |
1314 | UI.altOn = false; |
1315 | } | |
1316 | }, | |
1317 | ||
fdf21468 | 1318 | sendCtrlAltDel: function() { |
1319 | UI.rfb.sendCtrlAltDel(); | |
bbbf42bb SR |
1320 | }, |
1321 | ||
95dd6001 | 1322 | /* ------^------- |
1323 | * /KEYBOARD | |
1324 | * ============== | |
1325 | * MISC | |
1326 | * ------v------*/ | |
1327 | ||
f0d9ab96 | 1328 | hideMouseButton: function() { |
1329 | UI.setMouseButton(-1); | |
1330 | }, | |
1331 | ||
95dd6001 | 1332 | setMouseButton: function(num) { |
95dd6001 | 1333 | if (UI.rfb) { |
1334 | UI.rfb.get_mouse().set_touchButton(num); | |
1335 | } | |
1336 | ||
1337 | var blist = [0, 1,2,4]; | |
1338 | for (var b = 0; b < blist.length; b++) { | |
ae510306 | 1339 | var button = document.getElementById('noVNC_mouse_button' + blist[b]); |
95dd6001 | 1340 | if (blist[b] === num) { |
e40978c7 | 1341 | button.classList.remove("noVNC_hidden"); |
95dd6001 | 1342 | } else { |
e40978c7 | 1343 | button.classList.add("noVNC_hidden"); |
95dd6001 | 1344 | } |
1345 | } | |
1346 | }, | |
1347 | ||
1348 | displayBlur: function() { | |
1349 | if (!UI.rfb) return; | |
1350 | ||
1351 | UI.rfb.get_keyboard().set_focused(false); | |
1352 | UI.rfb.get_mouse().set_focused(false); | |
1353 | }, | |
1354 | ||
1355 | displayFocus: function() { | |
1356 | if (!UI.rfb) return; | |
1357 | ||
1358 | UI.rfb.get_keyboard().set_focused(true); | |
1359 | UI.rfb.get_mouse().set_focused(true); | |
bbbf42bb SR |
1360 | }, |
1361 | ||
95dd6001 | 1362 | // Display the desktop name in the document title |
1363 | updateDocumentTitle: function(rfb, name) { | |
1364 | document.title = name + " - noVNC"; | |
bbbf42bb SR |
1365 | }, |
1366 | ||
bbbf42bb SR |
1367 | //Helper to add options to dropdown. |
1368 | addOption: function(selectbox, text, value) { | |
1369 | var optn = document.createElement("OPTION"); | |
1370 | optn.text = text; | |
1371 | optn.value = value; | |
1372 | selectbox.options.add(optn); | |
1373 | }, | |
1374 | ||
1375 | setBarPosition: function() { | |
ae510306 SR |
1376 | document.getElementById('noVNC_control_bar').style.top = (window.pageYOffset) + 'px'; |
1377 | document.getElementById('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px'; | |
bbbf42bb | 1378 | |
ae510306 SR |
1379 | var vncwidth = document.getElementById('noVNC_container').style.offsetWidth; |
1380 | document.getElementById('noVNC_control_bar').style.width = vncwidth + 'px'; | |
bbbf42bb SR |
1381 | } |
1382 | ||
95dd6001 | 1383 | /* ------^------- |
1384 | * /MISC | |
1385 | * ============== | |
1386 | */ | |
bbbf42bb | 1387 | }; |
ae510306 SR |
1388 | |
1389 | /* [module] UI.load(); */ | |
bbbf42bb | 1390 | })(); |
ae510306 SR |
1391 | |
1392 | /* [module] export default UI; */ |