2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2016 Samuel Mannehed for Cendio AB
5 * Copyright (C) 2016 Pierre Ossman for Cendio AB
6 * Licensed under MPL 2.0 (see LICENSE.txt)
8 * See README.md for usage and integration instructions.
11 /* jslint white: false, browser: true */
12 /* global window, document.getElementById, Util, WebUtil, RFB, Display */
15 * import Util from "../core/util";
16 * import KeyTable from "../core/input/keysym";
17 * import RFB from "../core/rfb";
18 * import Display from "../core/display";
19 * import WebUtil from "./webutil";
27 /* [begin skip-as-module] */
28 // Load supporting scripts
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"]});
34 window
.onscriptsload = function () { UI
.load(); };
35 /* [end skip-as-module] */
43 hideKeyboardTimeout
: null,
44 idleControlbarTimeout
: null,
45 closeControlbarTimeout
: null,
47 controlbarGrabbed
: false,
48 controlbarDrag
: false,
49 controlbarMouseDownClientY
: 0,
50 controlbarMouseDownOffsetY
: 0,
51 keyboardVisible
: false,
55 rememberedClipSetting
: null,
56 lastKeyboardinput
: null,
57 defaultKeyboardinputLen
: 100,
59 // Setup rfb object, load settings from browser storage, then call
60 // UI.init to setup the UI/menus
61 load: function(callback
) {
62 WebUtil
.initSettings(UI
.start
, callback
);
65 // Render default UI and initialize settings menu
66 start: function(callback
) {
68 // Setup global variables first
69 UI
.isTouchDevice
= 'ontouchstart' in document
.documentElement
;
70 UI
.isSafari
= (navigator
.userAgent
.indexOf('Safari') !== -1 &&
71 navigator
.userAgent
.indexOf('Chrome') === -1);
75 // Adapt the interface for touch screen devices
76 if (UI
.isTouchDevice
) {
77 document
.documentElement
.classList
.add("noVNC_touch");
78 // Remove the address bar
79 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
80 UI
.forceSetting('clip', true);
82 UI
.initSetting('clip', false);
85 // Setup and initialize event handlers
86 UI
.setupWindowEvents();
88 UI
.addControlbarHandlers();
89 UI
.addTouchSpecificHandlers();
90 UI
.addExtraKeysHandlers();
92 UI
.addConnectionControlHandlers();
93 UI
.addClipboardHandlers();
94 UI
.addSettingsHandlers();
96 // Show the connect panel on first load unless autoconnecting
98 UI
.openConnectPanel();
103 UI
.updateVisualState();
105 document
.getElementById('noVNC_setting_host').focus();
107 var autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
108 if (autoconnect
=== 'true' || autoconnect
== '1') {
115 if (typeof callback
=== "function") {
120 initSettings: function() {
121 // Stylesheet selection dropdown
122 var sheet
= WebUtil
.selectStylesheet();
123 var sheets
= WebUtil
.getStylesheets();
125 for (i
= 0; i
< sheets
.length
; i
+= 1) {
126 UI
.addOption(document
.getElementById('noVNC_setting_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
129 // Logging selection dropdown
130 var llevels
= ['error', 'warn', 'info', 'debug'];
131 for (i
= 0; i
< llevels
.length
; i
+= 1) {
132 UI
.addOption(document
.getElementById('noVNC_setting_logging'),llevels
[i
], llevels
[i
]);
135 // Settings with immediate effects
136 UI
.initSetting('logging', 'warn');
137 WebUtil
.init_logging(UI
.getSetting('logging'));
139 UI
.initSetting('stylesheet', 'default');
140 WebUtil
.selectStylesheet(null);
141 // call twice to get around webkit bug
142 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
144 // if port == 80 (or 443) then it won't be present and should be
146 var port
= window
.location
.port
;
148 if (window
.location
.protocol
.substring(0,5) == 'https') {
151 else if (window
.location
.protocol
.substring(0,4) == 'http') {
156 /* Populate the controls if defaults are provided in the URL */
157 UI
.initSetting('host', window
.location
.hostname
);
158 UI
.initSetting('port', port
);
159 UI
.initSetting('password', '');
160 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
161 UI
.initSetting('true_color', true);
162 UI
.initSetting('cursor', !UI
.isTouchDevice
);
163 UI
.initSetting('resize', 'off');
164 UI
.initSetting('shared', true);
165 UI
.initSetting('view_only', false);
166 UI
.initSetting('path', 'websockify');
167 UI
.initSetting('repeaterID', '');
168 UI
.initSetting('token', '');
171 setupWindowEvents: function() {
172 window
.addEventListener( 'resize', function () {
173 UI
.applyResizeMode();
178 document
.getElementById("noVNC_status")
179 .addEventListener('click', UI
.hideStatus
);
182 setupFullscreen: function() {
183 // Only show the button if fullscreen is properly supported
184 // * Safari doesn't support alphanumerical input while in fullscreen
186 (document
.documentElement
.requestFullscreen
||
187 document
.documentElement
.mozRequestFullScreen
||
188 document
.documentElement
.webkitRequestFullscreen
||
189 document
.body
.msRequestFullscreen
)) {
190 document
.getElementById('noVNC_fullscreen_button')
191 .classList
.remove("noVNC_hidden");
192 UI
.addFullscreenHandlers();
196 addControlbarHandlers: function() {
197 document
.getElementById("noVNC_control_bar")
198 .addEventListener('mousemove', UI
.activateControlbar
);
199 document
.getElementById("noVNC_control_bar")
200 .addEventListener('mouseup', UI
.activateControlbar
);
201 document
.getElementById("noVNC_control_bar")
202 .addEventListener('mousedown', UI
.activateControlbar
);
203 document
.getElementById("noVNC_control_bar")
204 .addEventListener('keypress', UI
.activateControlbar
);
206 document
.getElementById("noVNC_control_bar")
207 .addEventListener('mousedown', UI
.keepControlbar
);
208 document
.getElementById("noVNC_control_bar")
209 .addEventListener('keypress', UI
.keepControlbar
);
211 document
.getElementById("noVNC_view_drag_button")
212 .addEventListener('click', UI
.toggleViewDrag
);
214 document
.getElementById("noVNC_control_bar_handle")
215 .addEventListener('mousedown', UI
.controlbarHandleMouseDown
);
216 document
.getElementById("noVNC_control_bar_handle")
217 .addEventListener('mouseup', UI
.controlbarHandleMouseUp
);
218 document
.getElementById("noVNC_control_bar_handle")
219 .addEventListener('mousemove', UI
.dragControlbarHandle
);
220 // resize events aren't available for elements
221 window
.addEventListener('resize', UI
.updateControlbarHandle
);
224 addTouchSpecificHandlers: function() {
225 document
.getElementById("noVNC_mouse_button0")
226 .addEventListener('click', function () { UI
.setMouseButton(1); });
227 document
.getElementById("noVNC_mouse_button1")
228 .addEventListener('click', function () { UI
.setMouseButton(2); });
229 document
.getElementById("noVNC_mouse_button2")
230 .addEventListener('click', function () { UI
.setMouseButton(4); });
231 document
.getElementById("noVNC_mouse_button4")
232 .addEventListener('click', function () { UI
.setMouseButton(0); });
233 document
.getElementById("noVNC_keyboard_button")
234 .addEventListener('click', UI
.toggleVirtualKeyboard
);
236 document
.getElementById("noVNC_keyboardinput")
237 .addEventListener('input', UI
.keyInput
);
238 document
.getElementById("noVNC_keyboardinput")
239 .addEventListener('blur', UI
.onblurVirtualKeyboard
);
240 document
.getElementById("noVNC_keyboardinput")
241 .addEventListener('submit', function () { return false; });
243 document
.getElementById("noVNC_control_bar")
244 .addEventListener('touchstart', UI
.activateControlbar
);
245 document
.getElementById("noVNC_control_bar")
246 .addEventListener('touchmove', UI
.activateControlbar
);
247 document
.getElementById("noVNC_control_bar")
248 .addEventListener('touchend', UI
.activateControlbar
);
249 document
.getElementById("noVNC_control_bar")
250 .addEventListener('input', UI
.activateControlbar
);
252 document
.getElementById("noVNC_control_bar")
253 .addEventListener('touchstart', UI
.keepControlbar
);
254 document
.getElementById("noVNC_control_bar")
255 .addEventListener('input', UI
.keepControlbar
);
257 document
.getElementById("noVNC_control_bar_handle")
258 .addEventListener('touchstart', UI
.controlbarHandleMouseDown
);
259 document
.getElementById("noVNC_control_bar_handle")
260 .addEventListener('touchend', UI
.controlbarHandleMouseUp
);
261 document
.getElementById("noVNC_control_bar_handle")
262 .addEventListener('touchmove', UI
.dragControlbarHandle
);
264 window
.addEventListener('load', UI
.keyboardinputReset
);
267 addExtraKeysHandlers: function() {
268 document
.getElementById("noVNC_toggle_extra_keys_button")
269 .addEventListener('click', UI
.toggleExtraKeys
);
270 document
.getElementById("noVNC_toggle_ctrl_button")
271 .addEventListener('click', UI
.toggleCtrl
);
272 document
.getElementById("noVNC_toggle_alt_button")
273 .addEventListener('click', UI
.toggleAlt
);
274 document
.getElementById("noVNC_send_tab_button")
275 .addEventListener('click', UI
.sendTab
);
276 document
.getElementById("noVNC_send_esc_button")
277 .addEventListener('click', UI
.sendEsc
);
278 document
.getElementById("noVNC_send_ctrl_alt_del_button")
279 .addEventListener('click', UI
.sendCtrlAltDel
);
282 addXvpHandlers: function() {
283 document
.getElementById("noVNC_xvp_shutdown_button")
284 .addEventListener('click', function() { UI
.rfb
.xvpShutdown(); });
285 document
.getElementById("noVNC_xvp_reboot_button")
286 .addEventListener('click', function() { UI
.rfb
.xvpReboot(); });
287 document
.getElementById("noVNC_xvp_reset_button")
288 .addEventListener('click', function() { UI
.rfb
.xvpReset(); });
289 document
.getElementById("noVNC_xvp_button")
290 .addEventListener('click', UI
.toggleXvpPanel
);
293 addConnectionControlHandlers: function() {
294 document
.getElementById("noVNC_connect_controls_button")
295 .addEventListener('click', UI
.toggleConnectPanel
);
296 document
.getElementById("noVNC_disconnect_button")
297 .addEventListener('click', UI
.disconnect
);
298 document
.getElementById("noVNC_connect_button")
299 .addEventListener('click', UI
.connect
);
301 document
.getElementById("noVNC_password_button")
302 .addEventListener('click', UI
.setPassword
);
305 addClipboardHandlers: function() {
306 document
.getElementById("noVNC_clipboard_button")
307 .addEventListener('click', UI
.toggleClipboardPanel
);
308 document
.getElementById("noVNC_clipboard_text")
309 .addEventListener('focus', UI
.displayBlur
);
310 document
.getElementById("noVNC_clipboard_text")
311 .addEventListener('blur', UI
.displayFocus
);
312 document
.getElementById("noVNC_clipboard_text")
313 .addEventListener('change', UI
.clipboardSend
);
314 document
.getElementById("noVNC_clipboard_clear_button")
315 .addEventListener('click', UI
.clipboardClear
);
318 addSettingsHandlers: function() {
319 document
.getElementById("noVNC_settings_button")
320 .addEventListener('click', UI
.toggleSettingsPanel
);
321 document
.getElementById("noVNC_settings_apply")
322 .addEventListener('click', UI
.settingsApply
);
324 document
.getElementById("noVNC_setting_resize")
325 .addEventListener('change', UI
.enableDisableViewClip
);
328 addFullscreenHandlers: function() {
329 document
.getElementById("noVNC_fullscreen_button")
330 .addEventListener('click', UI
.toggleFullscreen
);
332 window
.addEventListener('fullscreenchange', UI
.updateFullscreenButton
);
333 window
.addEventListener('mozfullscreenchange', UI
.updateFullscreenButton
);
334 window
.addEventListener('webkitfullscreenchange', UI
.updateFullscreenButton
);
335 window
.addEventListener('msfullscreenchange', UI
.updateFullscreenButton
);
338 initRFB: function() {
340 UI
.rfb
= new RFB({'target': document
.getElementById('noVNC_canvas'),
341 'onUpdateState': UI
.updateState
,
342 'onPasswordRequired': UI
.passwordRequired
,
343 'onXvpInit': UI
.updateXvpButton
,
344 'onClipboard': UI
.clipboardReceive
,
345 'onFBUComplete': UI
.initialResize
,
346 'onFBResize': UI
.updateViewDrag
,
347 'onDesktopName': UI
.updateDocumentTitle
});
350 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
361 updateState: function(rfb
, state
, oldstate
, msg
) {
362 UI
.rfb_state
= state
;
364 if (typeof(msg
) !== 'undefined') {
368 // zero means no timeout
369 UI
.showStatus(msg
, 'error', 0);
375 UI
.showStatus(msg
, 'normal');
378 UI
.showStatus(msg
, 'warn');
383 UI
.updateVisualState();
386 // Disable/enable controls depending on connection state
387 updateVisualState: function() {
388 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
390 //Util.Debug(">> updateVisualState");
391 document
.getElementById('noVNC_setting_encrypt').disabled
= connected
;
392 document
.getElementById('noVNC_setting_true_color').disabled
= connected
;
393 if (Util
.browserSupportsCursorURIs()) {
394 document
.getElementById('noVNC_setting_cursor').disabled
= connected
;
396 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
397 document
.getElementById('noVNC_setting_cursor').disabled
= true;
400 UI
.enableDisableViewClip();
401 document
.getElementById('noVNC_setting_resize').disabled
= connected
;
402 document
.getElementById('noVNC_setting_shared').disabled
= connected
;
403 document
.getElementById('noVNC_setting_view_only').disabled
= connected
;
404 document
.getElementById('noVNC_setting_path').disabled
= connected
;
405 document
.getElementById('noVNC_setting_repeaterID').disabled
= connected
;
408 document
.documentElement
.classList
.add("noVNC_connected");
410 UI
.setMouseButton(1);
412 // Hide the controlbar after 2 seconds
413 UI
.closeControlbarTimeout
= setTimeout(UI
.closeControlbar
, 2000);
415 document
.documentElement
.classList
.remove("noVNC_connected");
416 UI
.updateXvpButton(0);
419 // State change disables viewport dragging.
420 // It is enabled (toggled) by direct click on the button
421 UI
.setViewDrag(false);
423 // State change also closes the password dialog
424 document
.getElementById('noVNC_password_dlg')
425 .classList
.remove('noVNC_open');
427 switch (UI
.rfb_state
) {
431 UI
.openConnectPanel();
439 //Util.Debug("<< updateVisualState");
442 showStatus: function(text
, status_type
, time
) {
443 var statusElem
= document
.getElementById('noVNC_status');
445 clearTimeout(UI
.statusTimeout
);
447 if (typeof status_type
=== 'undefined') {
448 status_type
= 'normal';
451 statusElem
.classList
.remove("noVNC_status_normal",
453 "noVNC_status_error");
455 switch (status_type
) {
458 statusElem
.classList
.add("noVNC_status_warn");
461 statusElem
.classList
.add("noVNC_status_error");
466 statusElem
.classList
.add("noVNC_status_normal");
470 statusElem
.innerHTML
= text
;
471 statusElem
.classList
.add("noVNC_open");
473 // If no time was specified, show the status for 1.5 seconds
474 if (typeof time
=== 'undefined') {
478 // A specified time of zero means no timeout
480 UI
.statusTimeout
= window
.setTimeout(UI
.hideStatus
, time
);
484 hideStatus: function() {
485 clearTimeout(UI
.statusTimeout
);
486 document
.getElementById('noVNC_status').classList
.remove("noVNC_open");
489 activateControlbar: function(event
) {
490 clearTimeout(UI
.idleControlbarTimeout
);
491 // We manipulate the anchor instead of the actual control
492 // bar in order to avoid creating new a stacking group
493 document
.getElementById('noVNC_control_bar_anchor')
494 .classList
.remove("noVNC_idle");
495 UI
.idleControlbarTimeout
= window
.setTimeout(UI
.idleControlbar
, 2000);
498 idleControlbar: function() {
499 document
.getElementById('noVNC_control_bar_anchor')
500 .classList
.add("noVNC_idle");
503 keepControlbar: function() {
504 clearTimeout(UI
.closeControlbarTimeout
);
507 openControlbar: function() {
508 document
.getElementById('noVNC_control_bar')
509 .classList
.add("noVNC_open");
512 closeControlbar: function() {
514 document
.getElementById('noVNC_control_bar')
515 .classList
.remove("noVNC_open");
518 toggleControlbar: function() {
519 if (document
.getElementById('noVNC_control_bar')
520 .classList
.contains("noVNC_open")) {
521 UI
.closeControlbar();
527 dragControlbarHandle: function (e
) {
528 if (!UI
.controlbarGrabbed
) return;
530 var ptr
= Util
.getPointerEvent(e
);
532 if (!UI
.controlbarDrag
) {
533 // The goal is to trigger on a certain physical width, the
534 // devicePixelRatio brings us a bit closer but is not optimal.
535 var dragThreshold
= 10 * (window
.devicePixelRatio
|| 1);
536 var dragDistance
= Math
.abs(ptr
.clientY
- UI
.controlbarMouseDownClientY
);
538 if (dragDistance
< dragThreshold
) return;
540 UI
.controlbarDrag
= true;
543 var eventY
= ptr
.clientY
- UI
.controlbarMouseDownOffsetY
;
545 UI
.moveControlbarHandle(eventY
);
551 // Move the handle but don't allow any position outside the bounds
552 moveControlbarHandle: function (posY
) {
553 var handle
= document
.getElementById("noVNC_control_bar_handle");
554 var handleHeight
= Util
.getPosition(handle
).height
;
555 var controlbar
= document
.getElementById("noVNC_control_bar");
556 var controlbarBounds
= Util
.getPosition(controlbar
);
557 var controlbarTop
= controlbarBounds
.y
;
558 var controlbarBottom
= controlbarBounds
.y
+ controlbarBounds
.height
;
561 var viewportY
= posY
;
563 // Refuse coordinates outside the control bar
564 if (viewportY
< controlbarTop
+ margin
) {
565 viewportY
= controlbarTop
+ margin
;
566 } else if (viewportY
> controlbarBottom
- handleHeight
- margin
) {
567 viewportY
= controlbarBottom
- handleHeight
- margin
;
570 // Corner case: control bar too small for stable position
571 if (controlbarBounds
.height
< (handleHeight
+ margin
* 2)) {
572 viewportY
= controlbarTop
+ (controlbarBounds
.height
- handleHeight
) / 2;
575 var relativeY
= viewportY
- controlbarTop
;
576 handle
.style
.transform
= "translateY(" + relativeY
+ "px)";
579 updateControlbarHandle: function () {
580 var handle
= document
.getElementById("noVNC_control_bar_handle");
581 var pos
= Util
.getPosition(handle
);
582 UI
.moveControlbarHandle(pos
.y
);
585 controlbarHandleMouseUp: function(e
) {
586 if ((e
.type
== "mouseup") && (e
.button
!= 0)) return;
588 // mouseup and mousedown on the same place toggles the controlbar
589 if (UI
.controlbarGrabbed
&& !UI
.controlbarDrag
) {
590 UI
.toggleControlbar();
594 UI
.controlbarGrabbed
= false;
597 controlbarHandleMouseDown: function(e
) {
598 if ((e
.type
== "mousedown") && (e
.button
!= 0)) return;
600 var ptr
= Util
.getPointerEvent(e
);
602 var handle
= document
.getElementById("noVNC_control_bar_handle");
603 var bounds
= handle
.getBoundingClientRect();
605 WebUtil
.setCapture(handle
);
606 UI
.controlbarGrabbed
= true;
607 UI
.controlbarDrag
= false;
609 UI
.controlbarMouseDownClientY
= ptr
.clientY
;
610 UI
.controlbarMouseDownOffsetY
= ptr
.clientY
- bounds
.top
;
621 // Initial page load read/initialization of settings
622 initSetting: function(name
, defVal
) {
623 // Check Query string followed by cookie
624 var val
= WebUtil
.getConfigVar(name
);
626 val
= WebUtil
.readSetting(name
, defVal
);
628 UI
.updateSetting(name
, val
);
632 // Update cookie and form control setting. If value is not set, then
633 // updates from control to current cookie setting.
634 updateSetting: function(name
, value
) {
636 // Save the cookie for this session
637 if (typeof value
!== 'undefined') {
638 WebUtil
.writeSetting(name
, value
);
641 // Update the settings control
642 value
= UI
.getSetting(name
);
644 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
645 if (ctrl
.type
=== 'checkbox') {
646 ctrl
.checked
= value
;
648 } else if (typeof ctrl
.options
!== 'undefined') {
649 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
650 if (ctrl
.options
[i
].value
=== value
) {
651 ctrl
.selectedIndex
= i
;
656 /*Weird IE9 error leads to 'null' appearring
657 in textboxes instead of ''.*/
658 if (value
=== null) {
665 // Save control setting to cookie
666 saveSetting: function(name
) {
667 var val
, ctrl
= document
.getElementById('noVNC_setting_' + name
);
668 if (ctrl
.type
=== 'checkbox') {
670 } else if (typeof ctrl
.options
!== 'undefined') {
671 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
675 WebUtil
.writeSetting(name
, val
);
676 //Util.Debug("Setting saved '" + name + "=" + val + "'");
680 // Force a setting to be a certain value
681 forceSetting: function(name
, val
) {
682 UI
.updateSetting(name
, val
);
686 // Read form control compatible setting from cookie
687 getSetting: function(name
) {
688 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
689 var val
= WebUtil
.readSetting(name
);
690 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
691 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
700 // Save/apply settings when 'Apply' button is pressed
701 settingsApply: function() {
702 //Util.Debug(">> settingsApply");
703 UI
.saveSetting('encrypt');
704 UI
.saveSetting('true_color');
705 if (Util
.browserSupportsCursorURIs()) {
706 UI
.saveSetting('cursor');
709 UI
.saveSetting('resize');
711 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
712 UI
.forceSetting('clip', false);
715 UI
.saveSetting('clip');
716 UI
.saveSetting('shared');
717 UI
.saveSetting('view_only');
718 UI
.saveSetting('path');
719 UI
.saveSetting('repeaterID');
720 UI
.saveSetting('stylesheet');
721 UI
.saveSetting('logging');
723 // Settings with immediate (non-connected related) effect
724 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
725 WebUtil
.init_logging(UI
.getSetting('logging'));
728 //Util.Debug("<< settingsApply");
737 closeAllPanels: function() {
738 UI
.closeSettingsPanel();
740 UI
.closeClipboardPanel();
741 UI
.closeConnectPanel();
751 openSettingsPanel: function() {
755 UI
.updateSetting('encrypt');
756 UI
.updateSetting('true_color');
757 if (Util
.browserSupportsCursorURIs()) {
758 UI
.updateSetting('cursor');
760 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
761 document
.getElementById('noVNC_setting_cursor').disabled
= true;
763 UI
.updateSetting('clip');
764 UI
.updateSetting('resize');
765 UI
.updateSetting('shared');
766 UI
.updateSetting('view_only');
767 UI
.updateSetting('path');
768 UI
.updateSetting('repeaterID');
769 UI
.updateSetting('stylesheet');
770 UI
.updateSetting('logging');
772 document
.getElementById('noVNC_settings')
773 .classList
.add("noVNC_open");
774 document
.getElementById('noVNC_settings_button')
775 .classList
.add("noVNC_selected");
778 closeSettingsPanel: function() {
779 document
.getElementById('noVNC_settings')
780 .classList
.remove("noVNC_open");
781 document
.getElementById('noVNC_settings_button')
782 .classList
.remove("noVNC_selected");
785 // Toggle the settings menu:
786 // On open, settings are refreshed from saved cookies.
787 // On close, settings are applied
788 toggleSettingsPanel: function() {
789 if (document
.getElementById('noVNC_settings')
790 .classList
.contains("noVNC_open")) {
792 UI
.closeSettingsPanel();
794 UI
.openSettingsPanel();
804 openXvpPanel: function() {
808 document
.getElementById('noVNC_xvp')
809 .classList
.add("noVNC_open");
810 document
.getElementById('noVNC_xvp_button')
811 .classList
.add("noVNC_selected");
814 closeXvpPanel: function() {
815 document
.getElementById('noVNC_xvp')
816 .classList
.remove("noVNC_open");
817 document
.getElementById('noVNC_xvp_button')
818 .classList
.remove("noVNC_selected");
821 toggleXvpPanel: function() {
822 if (document
.getElementById('noVNC_xvp')
823 .classList
.contains("noVNC_open")) {
830 // Disable/enable XVP button
831 updateXvpButton: function(ver
) {
833 document
.getElementById('noVNC_xvp_button')
834 .classList
.remove("noVNC_hidden");
836 document
.getElementById('noVNC_xvp_button')
837 .classList
.add("noVNC_hidden");
838 // Close XVP panel if open
849 openClipboardPanel: function() {
853 document
.getElementById('noVNC_clipboard')
854 .classList
.add("noVNC_open");
855 document
.getElementById('noVNC_clipboard_button')
856 .classList
.add("noVNC_selected");
859 closeClipboardPanel: function() {
860 document
.getElementById('noVNC_clipboard')
861 .classList
.remove("noVNC_open");
862 document
.getElementById('noVNC_clipboard_button')
863 .classList
.remove("noVNC_selected");
866 toggleClipboardPanel: function() {
867 if (document
.getElementById('noVNC_clipboard')
868 .classList
.contains("noVNC_open")) {
869 UI
.closeClipboardPanel();
871 UI
.openClipboardPanel();
875 clipboardReceive: function(rfb
, text
) {
876 Util
.Debug(">> UI.clipboardReceive: " + text
.substr(0,40) + "...");
877 document
.getElementById('noVNC_clipboard_text').value
= text
;
878 Util
.Debug("<< UI.clipboardReceive");
881 clipboardClear: function() {
882 document
.getElementById('noVNC_clipboard_text').value
= "";
883 UI
.rfb
.clipboardPasteFrom("");
886 clipboardSend: function() {
887 var text
= document
.getElementById('noVNC_clipboard_text').value
;
888 Util
.Debug(">> UI.clipboardSend: " + text
.substr(0,40) + "...");
889 UI
.rfb
.clipboardPasteFrom(text
);
890 Util
.Debug("<< UI.clipboardSend");
899 openConnectPanel: function() {
903 document
.getElementById('noVNC_connect_controls')
904 .classList
.add("noVNC_open");
905 document
.getElementById('noVNC_connect_controls_button')
906 .classList
.add("noVNC_selected");
908 document
.getElementById('noVNC_setting_host').focus();
911 closeConnectPanel: function() {
912 document
.getElementById('noVNC_connect_controls')
913 .classList
.remove("noVNC_open");
914 document
.getElementById('noVNC_connect_controls_button')
915 .classList
.remove("noVNC_selected");
917 UI
.saveSetting('host');
918 UI
.saveSetting('port');
919 UI
.saveSetting('token');
920 //UI.saveSetting('password');
923 toggleConnectPanel: function() {
924 if (document
.getElementById('noVNC_connect_controls')
925 .classList
.contains("noVNC_open")) {
926 UI
.closeConnectPanel();
928 UI
.openConnectPanel();
932 connect: function() {
935 var host
= document
.getElementById('noVNC_setting_host').value
;
936 var port
= document
.getElementById('noVNC_setting_port').value
;
937 var password
= document
.getElementById('noVNC_setting_password').value
;
938 var token
= document
.getElementById('noVNC_setting_token').value
;
939 var path
= document
.getElementById('noVNC_setting_path').value
;
941 //if token is in path then ignore the new token variable
943 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
946 if ((!host
) || (!port
)) {
947 throw new Error("Must set host and port");
950 if (!UI
.initRFB()) return;
952 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
953 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
954 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
955 UI
.rfb
.set_shared(UI
.getSetting('shared'));
956 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
957 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
959 UI
.rfb
.connect(host
, port
, password
, path
);
962 disconnect: function() {
966 // Restore the callback used for initial resize
967 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
969 // Don't display the connection settings until we're actually disconnected
978 passwordRequired: function(rfb
, msg
) {
980 document
.getElementById('noVNC_password_dlg')
981 .classList
.add('noVNC_open');
983 setTimeout(function () {
984 document
.getElementById('noVNC_password_input').focus();
987 if (typeof msg
=== 'undefined') {
988 msg
= "Password is required";
990 UI
.updateState(null, "warning", null, msg
);
993 setPassword: function() {
994 UI
.rfb
.sendPassword(document
.getElementById('noVNC_password_input').value
);
995 document
.getElementById('noVNC_password_dlg')
996 .classList
.remove('noVNC_open');
1006 toggleFullscreen: function() {
1007 if (document
.fullscreenElement
|| // alternative standard method
1008 document
.mozFullScreenElement
|| // currently working methods
1009 document
.webkitFullscreenElement
||
1010 document
.msFullscreenElement
) {
1011 if (document
.exitFullscreen
) {
1012 document
.exitFullscreen();
1013 } else if (document
.mozCancelFullScreen
) {
1014 document
.mozCancelFullScreen();
1015 } else if (document
.webkitExitFullscreen
) {
1016 document
.webkitExitFullscreen();
1017 } else if (document
.msExitFullscreen
) {
1018 document
.msExitFullscreen();
1021 if (document
.documentElement
.requestFullscreen
) {
1022 document
.documentElement
.requestFullscreen();
1023 } else if (document
.documentElement
.mozRequestFullScreen
) {
1024 document
.documentElement
.mozRequestFullScreen();
1025 } else if (document
.documentElement
.webkitRequestFullscreen
) {
1026 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
1027 } else if (document
.body
.msRequestFullscreen
) {
1028 document
.body
.msRequestFullscreen();
1031 UI
.enableDisableViewClip();
1032 UI
.updateFullscreenButton();
1035 updateFullscreenButton: function() {
1036 if (document
.fullscreenElement
|| // alternative standard method
1037 document
.mozFullScreenElement
|| // currently working methods
1038 document
.webkitFullscreenElement
||
1039 document
.msFullscreenElement
) {
1040 document
.getElementById('noVNC_fullscreen_button')
1041 .classList
.add("noVNC_selected");
1043 document
.getElementById('noVNC_fullscreen_button')
1044 .classList
.remove("noVNC_selected");
1054 // Apply remote resizing or local scaling
1055 applyResizeMode: function() {
1056 if (!UI
.rfb
) return;
1058 var screen
= UI
.screenSize();
1060 if (screen
&& UI
.rfb_state
=== 'normal' && UI
.rfb
.get_display()) {
1062 var display
= UI
.rfb
.get_display();
1063 var resizeMode
= UI
.getSetting('resize');
1065 if (resizeMode
=== 'remote') {
1067 // Request changing the resolution of the remote display to
1068 // the size of the local browser viewport.
1070 // In order to not send multiple requests before the browser-resize
1071 // is finished we wait 0.5 seconds before sending the request.
1072 clearTimeout(UI
.resizeTimeout
);
1073 UI
.resizeTimeout
= setTimeout(function(){
1075 // Limit the viewport to the size of the browser window
1076 display
.set_maxWidth(screen
.w
);
1077 display
.set_maxHeight(screen
.h
);
1079 Util
.Debug('Attempting requestDesktopSize(' +
1080 screen
.w
+ ', ' + screen
.h
+ ')');
1082 // Request a remote size covering the viewport
1083 UI
.rfb
.requestDesktopSize(screen
.w
, screen
.h
);
1086 } else if (resizeMode
=== 'scale' || resizeMode
=== 'downscale') {
1087 var downscaleOnly
= resizeMode
=== 'downscale';
1088 var scaleRatio
= display
.autoscale(screen
.w
, screen
.h
, downscaleOnly
);
1089 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
1090 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
1095 // The screen is always the same size as the available viewport
1096 // in the browser window minus the height of the control bar
1097 screenSize: function() {
1098 var screen
= document
.getElementById('noVNC_screen');
1100 // Hide the scrollbars until the size is calculated
1101 screen
.style
.overflow
= "hidden";
1103 var pos
= Util
.getPosition(screen
);
1107 screen
.style
.overflow
= "visible";
1109 if (isNaN(w
) || isNaN(h
)) {
1112 return {w
: w
, h
: h
};
1116 // Normally we only apply the current resize mode after a window resize
1117 // event. This means that when a new connection is opened, there is no
1118 // resize mode active.
1119 // We have to wait until the first FBU because this is where the client
1120 // will find the supported encodings of the server. Some calls later in
1121 // the chain is dependant on knowing the server-capabilities.
1122 initialResize: function(rfb
, fbu
) {
1123 UI
.applyResizeMode();
1124 // After doing this once, we remove the callback.
1125 UI
.rfb
.set_onFBUComplete(function() { });
1134 // Set and configure viewport clipping
1135 setViewClip: function(clip
) {
1136 UI
.updateSetting('clip', clip
);
1137 UI
.updateViewClip();
1140 // Update parameters that depend on the clip setting
1141 updateViewClip: function() {
1143 if (!UI
.rfb
) return;
1145 var display
= UI
.rfb
.get_display();
1146 var cur_clip
= display
.get_viewport();
1147 var new_clip
= UI
.getSetting('clip');
1149 if (cur_clip
!== new_clip
) {
1150 display
.set_viewport(new_clip
);
1153 var size
= UI
.screenSize();
1155 if (new_clip
&& size
) {
1156 // When clipping is enabled, the screen is limited to
1157 // the size of the browser window.
1158 display
.set_maxWidth(size
.w
);
1159 display
.set_maxHeight(size
.h
);
1161 var screen
= document
.getElementById('noVNC_screen');
1162 var canvas
= document
.getElementById('noVNC_canvas');
1164 // Hide potential scrollbars that can skew the position
1165 screen
.style
.overflow
= "hidden";
1167 // The x position marks the left margin of the canvas,
1168 // remove the margin from both sides to keep it centered.
1169 var new_w
= size
.w
- (2 * Util
.getPosition(canvas
).x
);
1171 screen
.style
.overflow
= "visible";
1173 display
.viewportChangeSize(new_w
, size
.h
);
1175 // Disable max dimensions
1176 display
.set_maxWidth(0);
1177 display
.set_maxHeight(0);
1178 display
.viewportChangeSize();
1182 // Handle special cases where clipping is forced on/off or locked
1183 enableDisableViewClip: function() {
1184 var resizeSetting
= document
.getElementById('noVNC_setting_resize');
1185 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
1188 // Safari auto-hides the scrollbars which makes them
1189 // impossible to use in most cases
1190 UI
.setViewClip(true);
1191 document
.getElementById('noVNC_setting_clip').disabled
= true;
1192 } else if (resizeSetting
.value
=== 'downscale' || resizeSetting
.value
=== 'scale') {
1193 // Disable clipping if we are scaling
1194 UI
.setViewClip(false);
1195 document
.getElementById('noVNC_setting_clip').disabled
= true;
1196 } else if (document
.msFullscreenElement
) {
1197 // The browser is IE and we are in fullscreen mode.
1198 // - We need to force clipping while in fullscreen since
1199 // scrollbars doesn't work.
1200 UI
.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
1201 UI
.rememberedClipSetting
= UI
.getSetting('clip');
1202 UI
.setViewClip(true);
1203 document
.getElementById('noVNC_setting_clip').disabled
= true;
1204 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
1205 // Restore view clip to what it was before fullscreen on IE
1206 UI
.setViewClip(UI
.rememberedClipSetting
);
1207 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1209 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1210 if (UI
.isTouchDevice
) {
1211 UI
.setViewClip(true);
1222 toggleViewDrag: function() {
1223 if (!UI
.rfb
) return;
1225 var drag
= UI
.rfb
.get_viewportDrag();
1226 UI
.setViewDrag(!drag
);
1229 // Set the view drag mode which moves the viewport on mouse drags
1230 setViewDrag: function(drag
) {
1231 if (!UI
.rfb
) return;
1233 UI
.rfb
.set_viewportDrag(drag
);
1235 UI
.updateViewDrag();
1238 updateViewDrag: function() {
1239 var clipping
= false;
1241 if (UI
.rfb_state
!== 'normal') return;
1243 // Check if viewport drag is possible. It is only possible
1244 // if the remote display is clipping the client display.
1245 if (UI
.rfb
.get_display().get_viewport() &&
1246 UI
.rfb
.get_display().clippingDisplay()) {
1250 var viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1253 UI
.rfb
.get_viewportDrag()) {
1254 // The size of the remote display is the same or smaller
1255 // than the client display. Make sure viewport drag isn't
1256 // active when it can't be used.
1257 UI
.rfb
.set_viewportDrag(false);
1260 if (UI
.rfb
.get_viewportDrag()) {
1261 viewDragButton
.classList
.add("noVNC_selected");
1263 viewDragButton
.classList
.remove("noVNC_selected");
1266 // Different behaviour for touch vs non-touch
1267 // The button is disabled instead of hidden on touch devices
1268 if (UI
.isTouchDevice
) {
1269 viewDragButton
.classList
.remove("noVNC_hidden");
1272 viewDragButton
.disabled
= false;
1274 viewDragButton
.disabled
= true;
1277 viewDragButton
.disabled
= false;
1280 viewDragButton
.classList
.remove("noVNC_hidden");
1282 viewDragButton
.classList
.add("noVNC_hidden");
1293 showVirtualKeyboard: function() {
1294 if (!UI
.isTouchDevice
) return;
1296 var input
= document
.getElementById('noVNC_keyboardinput');
1298 if (document
.activeElement
== input
) return;
1300 UI
.keyboardVisible
= true;
1301 document
.getElementById('noVNC_keyboard_button')
1302 .classList
.add("noVNC_selected");
1306 var l
= input
.value
.length
;
1307 // Move the caret to the end
1308 input
.setSelectionRange(l
, l
);
1309 } catch (err
) {} // setSelectionRange is undefined in Google Chrome
1312 hideVirtualKeyboard: function() {
1313 if (!UI
.isTouchDevice
) return;
1315 var input
= document
.getElementById('noVNC_keyboardinput');
1317 if (document
.activeElement
!= input
) return;
1322 toggleVirtualKeyboard: function () {
1323 if (UI
.keyboardVisible
) {
1324 UI
.hideVirtualKeyboard();
1326 UI
.showVirtualKeyboard();
1330 onblurVirtualKeyboard: function() {
1331 //Weird bug in iOS if you change keyboardVisible
1332 //here it does not actually occur so next time
1333 //you click keyboard icon it doesnt work.
1334 UI
.hideKeyboardTimeout
= setTimeout(function() {
1335 UI
.keyboardVisible
= false;
1336 document
.getElementById('noVNC_keyboard_button')
1337 .classList
.remove("noVNC_selected");
1341 keepKeyboard: function() {
1342 clearTimeout(UI
.hideKeyboardTimeout
);
1343 if(UI
.keyboardVisible
=== true) {
1344 UI
.showVirtualKeyboard();
1345 } else if(UI
.keyboardVisible
=== false) {
1346 UI
.hideVirtualKeyboard();
1350 keyboardinputReset: function() {
1351 var kbi
= document
.getElementById('noVNC_keyboardinput');
1352 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1353 UI
.lastKeyboardinput
= kbi
.value
;
1356 // When normal keyboard events are left uncought, use the input events from
1357 // the keyboardinput element instead and generate the corresponding key events.
1358 // This code is required since some browsers on Android are inconsistent in
1359 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1360 keyInput: function(event
) {
1362 if (!UI
.rfb
) return;
1364 var newValue
= event
.target
.value
;
1366 if (!UI
.lastKeyboardinput
) {
1367 UI
.keyboardinputReset();
1369 var oldValue
= UI
.lastKeyboardinput
;
1373 // Try to check caret position since whitespace at the end
1374 // will not be considered by value.length in some browsers
1375 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1377 // selectionStart is undefined in Google Chrome
1378 newLen
= newValue
.length
;
1380 var oldLen
= oldValue
.length
;
1383 var inputs
= newLen
- oldLen
;
1385 backspaces
= -inputs
;
1390 // Compare the old string with the new to account for
1391 // text-corrections or other input that modify existing text
1393 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1394 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1395 inputs
= newLen
- i
;
1396 backspaces
= oldLen
- i
;
1401 // Send the key events
1402 for (i
= 0; i
< backspaces
; i
++) {
1403 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
);
1405 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1406 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1409 // Control the text content length in the keyboardinput element
1410 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1411 UI
.keyboardinputReset();
1412 } else if (newLen
< 1) {
1413 // There always have to be some text in the keyboardinput
1414 // element with which backspace can interact.
1415 UI
.keyboardinputReset();
1416 // This sometimes causes the keyboard to disappear for a second
1417 // but it is required for the android keyboard to recognize that
1418 // text has been added to the field
1419 event
.target
.blur();
1420 // This has to be ran outside of the input handler in order to work
1421 setTimeout(UI
.keepKeyboard
, 0);
1423 UI
.lastKeyboardinput
= newValue
;
1433 openExtraKeys: function() {
1434 UI
.closeAllPanels();
1435 UI
.openControlbar();
1437 document
.getElementById('noVNC_modifiers')
1438 .classList
.add("noVNC_open");
1439 document
.getElementById('noVNC_toggle_extra_keys_button')
1440 .classList
.add("noVNC_selected");
1443 closeExtraKeys: function() {
1444 document
.getElementById('noVNC_modifiers')
1445 .classList
.remove("noVNC_open");
1446 document
.getElementById('noVNC_toggle_extra_keys_button')
1447 .classList
.remove("noVNC_selected");
1450 toggleExtraKeys: function() {
1452 if(document
.getElementById('noVNC_modifiers')
1453 .classList
.contains("noVNC_open")) {
1454 UI
.closeExtraKeys();
1460 sendEsc: function() {
1462 UI
.rfb
.sendKey(KeyTable
.XK_Escape
);
1465 sendTab: function() {
1467 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1470 toggleCtrl: function() {
1472 var btn
= document
.getElementById('noVNC_toggle_ctrl_button');
1473 if (btn
.classList
.contains("noVNC_selected")) {
1474 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, false);
1475 btn
.classList
.remove("noVNC_selected");
1477 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, true);
1478 btn
.classList
.add("noVNC_selected");
1482 toggleAlt: function() {
1484 var btn
= document
.getElementById('noVNC_toggle_alt_button');
1485 if (btn
.classList
.contains("noVNC_selected")) {
1486 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, false);
1487 btn
.classList
.remove("noVNC_selected");
1489 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, true);
1490 btn
.classList
.add("noVNC_selected");
1494 sendCtrlAltDel: function() {
1496 UI
.rfb
.sendCtrlAltDel();
1505 setMouseButton: function(num
) {
1507 UI
.rfb
.get_mouse().set_touchButton(num
);
1510 var blist
= [0, 1,2,4];
1511 for (var b
= 0; b
< blist
.length
; b
++) {
1512 var button
= document
.getElementById('noVNC_mouse_button' + blist
[b
]);
1513 if (blist
[b
] === num
) {
1514 button
.classList
.remove("noVNC_hidden");
1516 button
.classList
.add("noVNC_hidden");
1521 displayBlur: function() {
1522 if (!UI
.rfb
) return;
1524 UI
.rfb
.get_keyboard().set_focused(false);
1525 UI
.rfb
.get_mouse().set_focused(false);
1528 displayFocus: function() {
1529 if (!UI
.rfb
) return;
1531 UI
.rfb
.get_keyboard().set_focused(true);
1532 UI
.rfb
.get_mouse().set_focused(true);
1535 // Display the desktop name in the document title
1536 updateDocumentTitle: function(rfb
, name
) {
1537 document
.title
= name
+ " - noVNC";
1540 //Helper to add options to dropdown.
1541 addOption: function(selectbox
, text
, value
) {
1542 var optn
= document
.createElement("OPTION");
1545 selectbox
.options
.add(optn
);
1554 /* [module] UI.load(); */
1557 /* [module] export default UI; */