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 'onNotification': UI
.notification
,
342 'onUpdateState': UI
.updateState
,
343 'onPasswordRequired': UI
.passwordRequired
,
344 'onXvpInit': UI
.updateXvpButton
,
345 'onClipboard': UI
.clipboardReceive
,
346 'onFBUComplete': UI
.initialResize
,
347 'onFBResize': UI
.updateViewDrag
,
348 'onDesktopName': UI
.updateDocumentTitle
});
351 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
362 updateState: function(rfb
, state
, oldstate
, msg
) {
363 UI
.rfb_state
= state
;
365 if (typeof(msg
) !== 'undefined') {
369 // zero means no timeout
370 UI
.showStatus(msg
, 'error', 0);
376 UI
.showStatus(msg
, 'normal');
379 UI
.showStatus(msg
, 'warn');
384 UI
.updateVisualState();
387 // Disable/enable controls depending on connection state
388 updateVisualState: function() {
389 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'connected';
391 //Util.Debug(">> updateVisualState");
392 document
.getElementById('noVNC_setting_encrypt').disabled
= connected
;
393 document
.getElementById('noVNC_setting_true_color').disabled
= connected
;
394 if (Util
.browserSupportsCursorURIs()) {
395 document
.getElementById('noVNC_setting_cursor').disabled
= connected
;
397 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
398 document
.getElementById('noVNC_setting_cursor').disabled
= true;
401 UI
.enableDisableViewClip();
402 document
.getElementById('noVNC_setting_resize').disabled
= connected
;
403 document
.getElementById('noVNC_setting_shared').disabled
= connected
;
404 document
.getElementById('noVNC_setting_view_only').disabled
= connected
;
405 document
.getElementById('noVNC_setting_path').disabled
= connected
;
406 document
.getElementById('noVNC_setting_repeaterID').disabled
= connected
;
409 document
.documentElement
.classList
.add("noVNC_connected");
411 UI
.setMouseButton(1);
413 // Hide the controlbar after 2 seconds
414 UI
.closeControlbarTimeout
= setTimeout(UI
.closeControlbar
, 2000);
416 document
.documentElement
.classList
.remove("noVNC_connected");
417 UI
.updateXvpButton(0);
420 // State change disables viewport dragging.
421 // It is enabled (toggled) by direct click on the button
422 UI
.setViewDrag(false);
424 // State change also closes the password dialog
425 document
.getElementById('noVNC_password_dlg')
426 .classList
.remove('noVNC_open');
428 switch (UI
.rfb_state
) {
432 UI
.openConnectPanel();
440 //Util.Debug("<< updateVisualState");
443 showStatus: function(text
, status_type
, time
) {
444 var statusElem
= document
.getElementById('noVNC_status');
446 clearTimeout(UI
.statusTimeout
);
448 if (typeof status_type
=== 'undefined') {
449 status_type
= 'normal';
452 statusElem
.classList
.remove("noVNC_status_normal",
454 "noVNC_status_error");
456 switch (status_type
) {
459 statusElem
.classList
.add("noVNC_status_warn");
462 statusElem
.classList
.add("noVNC_status_error");
467 statusElem
.classList
.add("noVNC_status_normal");
471 statusElem
.innerHTML
= text
;
472 statusElem
.classList
.add("noVNC_open");
474 // If no time was specified, show the status for 1.5 seconds
475 if (typeof time
=== 'undefined') {
479 // A specified time of zero means no timeout
481 UI
.statusTimeout
= window
.setTimeout(UI
.hideStatus
, time
);
485 hideStatus: function() {
486 clearTimeout(UI
.statusTimeout
);
487 document
.getElementById('noVNC_status').classList
.remove("noVNC_open");
490 notification: function (rfb
, msg
, level
, options
) {
491 UI
.showStatus(msg
, level
);
494 activateControlbar: function(event
) {
495 clearTimeout(UI
.idleControlbarTimeout
);
496 // We manipulate the anchor instead of the actual control
497 // bar in order to avoid creating new a stacking group
498 document
.getElementById('noVNC_control_bar_anchor')
499 .classList
.remove("noVNC_idle");
500 UI
.idleControlbarTimeout
= window
.setTimeout(UI
.idleControlbar
, 2000);
503 idleControlbar: function() {
504 document
.getElementById('noVNC_control_bar_anchor')
505 .classList
.add("noVNC_idle");
508 keepControlbar: function() {
509 clearTimeout(UI
.closeControlbarTimeout
);
512 openControlbar: function() {
513 document
.getElementById('noVNC_control_bar')
514 .classList
.add("noVNC_open");
517 closeControlbar: function() {
519 document
.getElementById('noVNC_control_bar')
520 .classList
.remove("noVNC_open");
523 toggleControlbar: function() {
524 if (document
.getElementById('noVNC_control_bar')
525 .classList
.contains("noVNC_open")) {
526 UI
.closeControlbar();
532 dragControlbarHandle: function (e
) {
533 if (!UI
.controlbarGrabbed
) return;
535 var ptr
= Util
.getPointerEvent(e
);
537 if (!UI
.controlbarDrag
) {
538 // The goal is to trigger on a certain physical width, the
539 // devicePixelRatio brings us a bit closer but is not optimal.
540 var dragThreshold
= 10 * (window
.devicePixelRatio
|| 1);
541 var dragDistance
= Math
.abs(ptr
.clientY
- UI
.controlbarMouseDownClientY
);
543 if (dragDistance
< dragThreshold
) return;
545 UI
.controlbarDrag
= true;
548 var eventY
= ptr
.clientY
- UI
.controlbarMouseDownOffsetY
;
550 UI
.moveControlbarHandle(eventY
);
556 // Move the handle but don't allow any position outside the bounds
557 moveControlbarHandle: function (posY
) {
558 var handle
= document
.getElementById("noVNC_control_bar_handle");
559 var handleHeight
= Util
.getPosition(handle
).height
;
560 var controlbar
= document
.getElementById("noVNC_control_bar");
561 var controlbarBounds
= Util
.getPosition(controlbar
);
562 var controlbarTop
= controlbarBounds
.y
;
563 var controlbarBottom
= controlbarBounds
.y
+ controlbarBounds
.height
;
566 var viewportY
= posY
;
568 // Refuse coordinates outside the control bar
569 if (viewportY
< controlbarTop
+ margin
) {
570 viewportY
= controlbarTop
+ margin
;
571 } else if (viewportY
> controlbarBottom
- handleHeight
- margin
) {
572 viewportY
= controlbarBottom
- handleHeight
- margin
;
575 // Corner case: control bar too small for stable position
576 if (controlbarBounds
.height
< (handleHeight
+ margin
* 2)) {
577 viewportY
= controlbarTop
+ (controlbarBounds
.height
- handleHeight
) / 2;
580 var relativeY
= viewportY
- controlbarTop
;
581 handle
.style
.transform
= "translateY(" + relativeY
+ "px)";
584 updateControlbarHandle: function () {
585 var handle
= document
.getElementById("noVNC_control_bar_handle");
586 var pos
= Util
.getPosition(handle
);
587 UI
.moveControlbarHandle(pos
.y
);
590 controlbarHandleMouseUp: function(e
) {
591 if ((e
.type
== "mouseup") && (e
.button
!= 0)) return;
593 // mouseup and mousedown on the same place toggles the controlbar
594 if (UI
.controlbarGrabbed
&& !UI
.controlbarDrag
) {
595 UI
.toggleControlbar();
599 UI
.controlbarGrabbed
= false;
602 controlbarHandleMouseDown: function(e
) {
603 if ((e
.type
== "mousedown") && (e
.button
!= 0)) return;
605 var ptr
= Util
.getPointerEvent(e
);
607 var handle
= document
.getElementById("noVNC_control_bar_handle");
608 var bounds
= handle
.getBoundingClientRect();
610 WebUtil
.setCapture(handle
);
611 UI
.controlbarGrabbed
= true;
612 UI
.controlbarDrag
= false;
614 UI
.controlbarMouseDownClientY
= ptr
.clientY
;
615 UI
.controlbarMouseDownOffsetY
= ptr
.clientY
- bounds
.top
;
626 // Initial page load read/initialization of settings
627 initSetting: function(name
, defVal
) {
628 // Check Query string followed by cookie
629 var val
= WebUtil
.getConfigVar(name
);
631 val
= WebUtil
.readSetting(name
, defVal
);
633 UI
.updateSetting(name
, val
);
637 // Update cookie and form control setting. If value is not set, then
638 // updates from control to current cookie setting.
639 updateSetting: function(name
, value
) {
641 // Save the cookie for this session
642 if (typeof value
!== 'undefined') {
643 WebUtil
.writeSetting(name
, value
);
646 // Update the settings control
647 value
= UI
.getSetting(name
);
649 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
650 if (ctrl
.type
=== 'checkbox') {
651 ctrl
.checked
= value
;
653 } else if (typeof ctrl
.options
!== 'undefined') {
654 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
655 if (ctrl
.options
[i
].value
=== value
) {
656 ctrl
.selectedIndex
= i
;
661 /*Weird IE9 error leads to 'null' appearring
662 in textboxes instead of ''.*/
663 if (value
=== null) {
670 // Save control setting to cookie
671 saveSetting: function(name
) {
672 var val
, ctrl
= document
.getElementById('noVNC_setting_' + name
);
673 if (ctrl
.type
=== 'checkbox') {
675 } else if (typeof ctrl
.options
!== 'undefined') {
676 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
680 WebUtil
.writeSetting(name
, val
);
681 //Util.Debug("Setting saved '" + name + "=" + val + "'");
685 // Force a setting to be a certain value
686 forceSetting: function(name
, val
) {
687 UI
.updateSetting(name
, val
);
691 // Read form control compatible setting from cookie
692 getSetting: function(name
) {
693 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
694 var val
= WebUtil
.readSetting(name
);
695 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
696 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
705 // Save/apply settings when 'Apply' button is pressed
706 settingsApply: function() {
707 //Util.Debug(">> settingsApply");
708 UI
.saveSetting('encrypt');
709 UI
.saveSetting('true_color');
710 if (Util
.browserSupportsCursorURIs()) {
711 UI
.saveSetting('cursor');
714 UI
.saveSetting('resize');
716 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
717 UI
.forceSetting('clip', false);
720 UI
.saveSetting('clip');
721 UI
.saveSetting('shared');
722 UI
.saveSetting('view_only');
723 UI
.saveSetting('path');
724 UI
.saveSetting('repeaterID');
725 UI
.saveSetting('stylesheet');
726 UI
.saveSetting('logging');
728 // Settings with immediate (non-connected related) effect
729 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
730 WebUtil
.init_logging(UI
.getSetting('logging'));
733 //Util.Debug("<< settingsApply");
742 closeAllPanels: function() {
743 UI
.closeSettingsPanel();
745 UI
.closeClipboardPanel();
746 UI
.closeConnectPanel();
756 openSettingsPanel: function() {
760 UI
.updateSetting('encrypt');
761 UI
.updateSetting('true_color');
762 if (Util
.browserSupportsCursorURIs()) {
763 UI
.updateSetting('cursor');
765 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
766 document
.getElementById('noVNC_setting_cursor').disabled
= true;
768 UI
.updateSetting('clip');
769 UI
.updateSetting('resize');
770 UI
.updateSetting('shared');
771 UI
.updateSetting('view_only');
772 UI
.updateSetting('path');
773 UI
.updateSetting('repeaterID');
774 UI
.updateSetting('stylesheet');
775 UI
.updateSetting('logging');
777 document
.getElementById('noVNC_settings')
778 .classList
.add("noVNC_open");
779 document
.getElementById('noVNC_settings_button')
780 .classList
.add("noVNC_selected");
783 closeSettingsPanel: function() {
784 document
.getElementById('noVNC_settings')
785 .classList
.remove("noVNC_open");
786 document
.getElementById('noVNC_settings_button')
787 .classList
.remove("noVNC_selected");
790 // Toggle the settings menu:
791 // On open, settings are refreshed from saved cookies.
792 // On close, settings are applied
793 toggleSettingsPanel: function() {
794 if (document
.getElementById('noVNC_settings')
795 .classList
.contains("noVNC_open")) {
797 UI
.closeSettingsPanel();
799 UI
.openSettingsPanel();
809 openXvpPanel: function() {
813 document
.getElementById('noVNC_xvp')
814 .classList
.add("noVNC_open");
815 document
.getElementById('noVNC_xvp_button')
816 .classList
.add("noVNC_selected");
819 closeXvpPanel: function() {
820 document
.getElementById('noVNC_xvp')
821 .classList
.remove("noVNC_open");
822 document
.getElementById('noVNC_xvp_button')
823 .classList
.remove("noVNC_selected");
826 toggleXvpPanel: function() {
827 if (document
.getElementById('noVNC_xvp')
828 .classList
.contains("noVNC_open")) {
835 // Disable/enable XVP button
836 updateXvpButton: function(ver
) {
838 document
.getElementById('noVNC_xvp_button')
839 .classList
.remove("noVNC_hidden");
841 document
.getElementById('noVNC_xvp_button')
842 .classList
.add("noVNC_hidden");
843 // Close XVP panel if open
854 openClipboardPanel: function() {
858 document
.getElementById('noVNC_clipboard')
859 .classList
.add("noVNC_open");
860 document
.getElementById('noVNC_clipboard_button')
861 .classList
.add("noVNC_selected");
864 closeClipboardPanel: function() {
865 document
.getElementById('noVNC_clipboard')
866 .classList
.remove("noVNC_open");
867 document
.getElementById('noVNC_clipboard_button')
868 .classList
.remove("noVNC_selected");
871 toggleClipboardPanel: function() {
872 if (document
.getElementById('noVNC_clipboard')
873 .classList
.contains("noVNC_open")) {
874 UI
.closeClipboardPanel();
876 UI
.openClipboardPanel();
880 clipboardReceive: function(rfb
, text
) {
881 Util
.Debug(">> UI.clipboardReceive: " + text
.substr(0,40) + "...");
882 document
.getElementById('noVNC_clipboard_text').value
= text
;
883 Util
.Debug("<< UI.clipboardReceive");
886 clipboardClear: function() {
887 document
.getElementById('noVNC_clipboard_text').value
= "";
888 UI
.rfb
.clipboardPasteFrom("");
891 clipboardSend: function() {
892 var text
= document
.getElementById('noVNC_clipboard_text').value
;
893 Util
.Debug(">> UI.clipboardSend: " + text
.substr(0,40) + "...");
894 UI
.rfb
.clipboardPasteFrom(text
);
895 Util
.Debug("<< UI.clipboardSend");
904 openConnectPanel: function() {
908 document
.getElementById('noVNC_connect_controls')
909 .classList
.add("noVNC_open");
910 document
.getElementById('noVNC_connect_controls_button')
911 .classList
.add("noVNC_selected");
913 document
.getElementById('noVNC_setting_host').focus();
916 closeConnectPanel: function() {
917 document
.getElementById('noVNC_connect_controls')
918 .classList
.remove("noVNC_open");
919 document
.getElementById('noVNC_connect_controls_button')
920 .classList
.remove("noVNC_selected");
922 UI
.saveSetting('host');
923 UI
.saveSetting('port');
924 UI
.saveSetting('token');
925 //UI.saveSetting('password');
928 toggleConnectPanel: function() {
929 if (document
.getElementById('noVNC_connect_controls')
930 .classList
.contains("noVNC_open")) {
931 UI
.closeConnectPanel();
933 UI
.openConnectPanel();
937 connect: function() {
940 var host
= document
.getElementById('noVNC_setting_host').value
;
941 var port
= document
.getElementById('noVNC_setting_port').value
;
942 var password
= document
.getElementById('noVNC_setting_password').value
;
943 var token
= document
.getElementById('noVNC_setting_token').value
;
944 var path
= document
.getElementById('noVNC_setting_path').value
;
946 //if token is in path then ignore the new token variable
948 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
951 if ((!host
) || (!port
)) {
952 throw new Error("Must set host and port");
955 if (!UI
.initRFB()) return;
957 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
958 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
959 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
960 UI
.rfb
.set_shared(UI
.getSetting('shared'));
961 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
962 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
964 UI
.rfb
.connect(host
, port
, password
, path
);
967 disconnect: function() {
971 // Restore the callback used for initial resize
972 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
974 // Don't display the connection settings until we're actually disconnected
983 passwordRequired: function(rfb
, msg
) {
985 document
.getElementById('noVNC_password_dlg')
986 .classList
.add('noVNC_open');
988 setTimeout(function () {
989 document
.getElementById('noVNC_password_input').focus();
992 if (typeof msg
=== 'undefined') {
993 msg
= "Password is required";
995 UI
.updateState(null, "warning", null, msg
);
998 setPassword: function() {
999 UI
.rfb
.sendPassword(document
.getElementById('noVNC_password_input').value
);
1000 document
.getElementById('noVNC_password_dlg')
1001 .classList
.remove('noVNC_open');
1011 toggleFullscreen: function() {
1012 if (document
.fullscreenElement
|| // alternative standard method
1013 document
.mozFullScreenElement
|| // currently working methods
1014 document
.webkitFullscreenElement
||
1015 document
.msFullscreenElement
) {
1016 if (document
.exitFullscreen
) {
1017 document
.exitFullscreen();
1018 } else if (document
.mozCancelFullScreen
) {
1019 document
.mozCancelFullScreen();
1020 } else if (document
.webkitExitFullscreen
) {
1021 document
.webkitExitFullscreen();
1022 } else if (document
.msExitFullscreen
) {
1023 document
.msExitFullscreen();
1026 if (document
.documentElement
.requestFullscreen
) {
1027 document
.documentElement
.requestFullscreen();
1028 } else if (document
.documentElement
.mozRequestFullScreen
) {
1029 document
.documentElement
.mozRequestFullScreen();
1030 } else if (document
.documentElement
.webkitRequestFullscreen
) {
1031 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
1032 } else if (document
.body
.msRequestFullscreen
) {
1033 document
.body
.msRequestFullscreen();
1036 UI
.enableDisableViewClip();
1037 UI
.updateFullscreenButton();
1040 updateFullscreenButton: function() {
1041 if (document
.fullscreenElement
|| // alternative standard method
1042 document
.mozFullScreenElement
|| // currently working methods
1043 document
.webkitFullscreenElement
||
1044 document
.msFullscreenElement
) {
1045 document
.getElementById('noVNC_fullscreen_button')
1046 .classList
.add("noVNC_selected");
1048 document
.getElementById('noVNC_fullscreen_button')
1049 .classList
.remove("noVNC_selected");
1059 // Apply remote resizing or local scaling
1060 applyResizeMode: function() {
1061 if (!UI
.rfb
) return;
1063 var screen
= UI
.screenSize();
1065 if (screen
&& UI
.rfb_state
=== 'connected' && UI
.rfb
.get_display()) {
1067 var display
= UI
.rfb
.get_display();
1068 var resizeMode
= UI
.getSetting('resize');
1070 if (resizeMode
=== 'remote') {
1072 // Request changing the resolution of the remote display to
1073 // the size of the local browser viewport.
1075 // In order to not send multiple requests before the browser-resize
1076 // is finished we wait 0.5 seconds before sending the request.
1077 clearTimeout(UI
.resizeTimeout
);
1078 UI
.resizeTimeout
= setTimeout(function(){
1080 // Limit the viewport to the size of the browser window
1081 display
.set_maxWidth(screen
.w
);
1082 display
.set_maxHeight(screen
.h
);
1084 Util
.Debug('Attempting requestDesktopSize(' +
1085 screen
.w
+ ', ' + screen
.h
+ ')');
1087 // Request a remote size covering the viewport
1088 UI
.rfb
.requestDesktopSize(screen
.w
, screen
.h
);
1091 } else if (resizeMode
=== 'scale' || resizeMode
=== 'downscale') {
1092 var downscaleOnly
= resizeMode
=== 'downscale';
1093 var scaleRatio
= display
.autoscale(screen
.w
, screen
.h
, downscaleOnly
);
1094 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
1095 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
1100 // The screen is always the same size as the available viewport
1101 // in the browser window minus the height of the control bar
1102 screenSize: function() {
1103 var screen
= document
.getElementById('noVNC_screen');
1105 // Hide the scrollbars until the size is calculated
1106 screen
.style
.overflow
= "hidden";
1108 var pos
= Util
.getPosition(screen
);
1112 screen
.style
.overflow
= "visible";
1114 if (isNaN(w
) || isNaN(h
)) {
1117 return {w
: w
, h
: h
};
1121 // Normally we only apply the current resize mode after a window resize
1122 // event. This means that when a new connection is opened, there is no
1123 // resize mode active.
1124 // We have to wait until the first FBU because this is where the client
1125 // will find the supported encodings of the server. Some calls later in
1126 // the chain is dependant on knowing the server-capabilities.
1127 initialResize: function(rfb
, fbu
) {
1128 UI
.applyResizeMode();
1129 // After doing this once, we remove the callback.
1130 UI
.rfb
.set_onFBUComplete(function() { });
1139 // Set and configure viewport clipping
1140 setViewClip: function(clip
) {
1141 UI
.updateSetting('clip', clip
);
1142 UI
.updateViewClip();
1145 // Update parameters that depend on the clip setting
1146 updateViewClip: function() {
1148 if (!UI
.rfb
) return;
1150 var display
= UI
.rfb
.get_display();
1151 var cur_clip
= display
.get_viewport();
1152 var new_clip
= UI
.getSetting('clip');
1154 if (cur_clip
!== new_clip
) {
1155 display
.set_viewport(new_clip
);
1158 var size
= UI
.screenSize();
1160 if (new_clip
&& size
) {
1161 // When clipping is enabled, the screen is limited to
1162 // the size of the browser window.
1163 display
.set_maxWidth(size
.w
);
1164 display
.set_maxHeight(size
.h
);
1166 var screen
= document
.getElementById('noVNC_screen');
1167 var canvas
= document
.getElementById('noVNC_canvas');
1169 // Hide potential scrollbars that can skew the position
1170 screen
.style
.overflow
= "hidden";
1172 // The x position marks the left margin of the canvas,
1173 // remove the margin from both sides to keep it centered.
1174 var new_w
= size
.w
- (2 * Util
.getPosition(canvas
).x
);
1176 screen
.style
.overflow
= "visible";
1178 display
.viewportChangeSize(new_w
, size
.h
);
1180 // Disable max dimensions
1181 display
.set_maxWidth(0);
1182 display
.set_maxHeight(0);
1183 display
.viewportChangeSize();
1187 // Handle special cases where clipping is forced on/off or locked
1188 enableDisableViewClip: function() {
1189 var resizeSetting
= document
.getElementById('noVNC_setting_resize');
1190 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'connected';
1193 // Safari auto-hides the scrollbars which makes them
1194 // impossible to use in most cases
1195 UI
.setViewClip(true);
1196 document
.getElementById('noVNC_setting_clip').disabled
= true;
1197 } else if (resizeSetting
.value
=== 'downscale' || resizeSetting
.value
=== 'scale') {
1198 // Disable clipping if we are scaling
1199 UI
.setViewClip(false);
1200 document
.getElementById('noVNC_setting_clip').disabled
= true;
1201 } else if (document
.msFullscreenElement
) {
1202 // The browser is IE and we are in fullscreen mode.
1203 // - We need to force clipping while in fullscreen since
1204 // scrollbars doesn't work.
1205 UI
.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
1206 UI
.rememberedClipSetting
= UI
.getSetting('clip');
1207 UI
.setViewClip(true);
1208 document
.getElementById('noVNC_setting_clip').disabled
= true;
1209 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
1210 // Restore view clip to what it was before fullscreen on IE
1211 UI
.setViewClip(UI
.rememberedClipSetting
);
1212 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1214 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1215 if (UI
.isTouchDevice
) {
1216 UI
.setViewClip(true);
1227 toggleViewDrag: function() {
1228 if (!UI
.rfb
) return;
1230 var drag
= UI
.rfb
.get_viewportDrag();
1231 UI
.setViewDrag(!drag
);
1234 // Set the view drag mode which moves the viewport on mouse drags
1235 setViewDrag: function(drag
) {
1236 if (!UI
.rfb
) return;
1238 UI
.rfb
.set_viewportDrag(drag
);
1240 UI
.updateViewDrag();
1243 updateViewDrag: function() {
1244 var clipping
= false;
1246 if (UI
.rfb_state
!== 'connected') return;
1248 // Check if viewport drag is possible. It is only possible
1249 // if the remote display is clipping the client display.
1250 if (UI
.rfb
.get_display().get_viewport() &&
1251 UI
.rfb
.get_display().clippingDisplay()) {
1255 var viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1258 UI
.rfb
.get_viewportDrag()) {
1259 // The size of the remote display is the same or smaller
1260 // than the client display. Make sure viewport drag isn't
1261 // active when it can't be used.
1262 UI
.rfb
.set_viewportDrag(false);
1265 if (UI
.rfb
.get_viewportDrag()) {
1266 viewDragButton
.classList
.add("noVNC_selected");
1268 viewDragButton
.classList
.remove("noVNC_selected");
1271 // Different behaviour for touch vs non-touch
1272 // The button is disabled instead of hidden on touch devices
1273 if (UI
.isTouchDevice
) {
1274 viewDragButton
.classList
.remove("noVNC_hidden");
1277 viewDragButton
.disabled
= false;
1279 viewDragButton
.disabled
= true;
1282 viewDragButton
.disabled
= false;
1285 viewDragButton
.classList
.remove("noVNC_hidden");
1287 viewDragButton
.classList
.add("noVNC_hidden");
1298 showVirtualKeyboard: function() {
1299 if (!UI
.isTouchDevice
) return;
1301 var input
= document
.getElementById('noVNC_keyboardinput');
1303 if (document
.activeElement
== input
) return;
1305 UI
.keyboardVisible
= true;
1306 document
.getElementById('noVNC_keyboard_button')
1307 .classList
.add("noVNC_selected");
1311 var l
= input
.value
.length
;
1312 // Move the caret to the end
1313 input
.setSelectionRange(l
, l
);
1314 } catch (err
) {} // setSelectionRange is undefined in Google Chrome
1317 hideVirtualKeyboard: function() {
1318 if (!UI
.isTouchDevice
) return;
1320 var input
= document
.getElementById('noVNC_keyboardinput');
1322 if (document
.activeElement
!= input
) return;
1327 toggleVirtualKeyboard: function () {
1328 if (UI
.keyboardVisible
) {
1329 UI
.hideVirtualKeyboard();
1331 UI
.showVirtualKeyboard();
1335 onblurVirtualKeyboard: function() {
1336 //Weird bug in iOS if you change keyboardVisible
1337 //here it does not actually occur so next time
1338 //you click keyboard icon it doesnt work.
1339 UI
.hideKeyboardTimeout
= setTimeout(function() {
1340 UI
.keyboardVisible
= false;
1341 document
.getElementById('noVNC_keyboard_button')
1342 .classList
.remove("noVNC_selected");
1346 keepKeyboard: function() {
1347 clearTimeout(UI
.hideKeyboardTimeout
);
1348 if(UI
.keyboardVisible
=== true) {
1349 UI
.showVirtualKeyboard();
1350 } else if(UI
.keyboardVisible
=== false) {
1351 UI
.hideVirtualKeyboard();
1355 keyboardinputReset: function() {
1356 var kbi
= document
.getElementById('noVNC_keyboardinput');
1357 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1358 UI
.lastKeyboardinput
= kbi
.value
;
1361 // When normal keyboard events are left uncought, use the input events from
1362 // the keyboardinput element instead and generate the corresponding key events.
1363 // This code is required since some browsers on Android are inconsistent in
1364 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1365 keyInput: function(event
) {
1367 if (!UI
.rfb
) return;
1369 var newValue
= event
.target
.value
;
1371 if (!UI
.lastKeyboardinput
) {
1372 UI
.keyboardinputReset();
1374 var oldValue
= UI
.lastKeyboardinput
;
1378 // Try to check caret position since whitespace at the end
1379 // will not be considered by value.length in some browsers
1380 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1382 // selectionStart is undefined in Google Chrome
1383 newLen
= newValue
.length
;
1385 var oldLen
= oldValue
.length
;
1388 var inputs
= newLen
- oldLen
;
1390 backspaces
= -inputs
;
1395 // Compare the old string with the new to account for
1396 // text-corrections or other input that modify existing text
1398 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1399 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1400 inputs
= newLen
- i
;
1401 backspaces
= oldLen
- i
;
1406 // Send the key events
1407 for (i
= 0; i
< backspaces
; i
++) {
1408 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
);
1410 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1411 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1414 // Control the text content length in the keyboardinput element
1415 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1416 UI
.keyboardinputReset();
1417 } else if (newLen
< 1) {
1418 // There always have to be some text in the keyboardinput
1419 // element with which backspace can interact.
1420 UI
.keyboardinputReset();
1421 // This sometimes causes the keyboard to disappear for a second
1422 // but it is required for the android keyboard to recognize that
1423 // text has been added to the field
1424 event
.target
.blur();
1425 // This has to be ran outside of the input handler in order to work
1426 setTimeout(UI
.keepKeyboard
, 0);
1428 UI
.lastKeyboardinput
= newValue
;
1438 openExtraKeys: function() {
1439 UI
.closeAllPanels();
1440 UI
.openControlbar();
1442 document
.getElementById('noVNC_modifiers')
1443 .classList
.add("noVNC_open");
1444 document
.getElementById('noVNC_toggle_extra_keys_button')
1445 .classList
.add("noVNC_selected");
1448 closeExtraKeys: function() {
1449 document
.getElementById('noVNC_modifiers')
1450 .classList
.remove("noVNC_open");
1451 document
.getElementById('noVNC_toggle_extra_keys_button')
1452 .classList
.remove("noVNC_selected");
1455 toggleExtraKeys: function() {
1457 if(document
.getElementById('noVNC_modifiers')
1458 .classList
.contains("noVNC_open")) {
1459 UI
.closeExtraKeys();
1465 sendEsc: function() {
1467 UI
.rfb
.sendKey(KeyTable
.XK_Escape
);
1470 sendTab: function() {
1472 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1475 toggleCtrl: function() {
1477 var btn
= document
.getElementById('noVNC_toggle_ctrl_button');
1478 if (btn
.classList
.contains("noVNC_selected")) {
1479 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, false);
1480 btn
.classList
.remove("noVNC_selected");
1482 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, true);
1483 btn
.classList
.add("noVNC_selected");
1487 toggleAlt: function() {
1489 var btn
= document
.getElementById('noVNC_toggle_alt_button');
1490 if (btn
.classList
.contains("noVNC_selected")) {
1491 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, false);
1492 btn
.classList
.remove("noVNC_selected");
1494 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, true);
1495 btn
.classList
.add("noVNC_selected");
1499 sendCtrlAltDel: function() {
1501 UI
.rfb
.sendCtrlAltDel();
1510 setMouseButton: function(num
) {
1512 UI
.rfb
.get_mouse().set_touchButton(num
);
1515 var blist
= [0, 1,2,4];
1516 for (var b
= 0; b
< blist
.length
; b
++) {
1517 var button
= document
.getElementById('noVNC_mouse_button' + blist
[b
]);
1518 if (blist
[b
] === num
) {
1519 button
.classList
.remove("noVNC_hidden");
1521 button
.classList
.add("noVNC_hidden");
1526 displayBlur: function() {
1527 if (!UI
.rfb
) return;
1529 UI
.rfb
.get_keyboard().set_focused(false);
1530 UI
.rfb
.get_mouse().set_focused(false);
1533 displayFocus: function() {
1534 if (!UI
.rfb
) return;
1536 UI
.rfb
.get_keyboard().set_focused(true);
1537 UI
.rfb
.get_mouse().set_focused(true);
1540 // Display the desktop name in the document title
1541 updateDocumentTitle: function(rfb
, name
) {
1542 document
.title
= name
+ " - noVNC";
1545 //Helper to add options to dropdown.
1546 addOption: function(selectbox
, text
, value
) {
1547 var optn
= document
.createElement("OPTION");
1550 selectbox
.options
.add(optn
);
1559 /* [module] UI.load(); */
1562 /* [module] export default UI; */