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
.showKeyboard
);
236 document
.getElementById("noVNC_keyboardinput")
237 .addEventListener('input', UI
.keyInput
);
238 document
.getElementById("noVNC_keyboardinput")
239 .addEventListener('blur', UI
.hideKeyboard
);
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 'onXvpInit': UI
.updateXvpButton
,
343 'onClipboard': UI
.clipboardReceive
,
344 'onFBUComplete': UI
.initialResize
,
345 'onFBResize': UI
.updateViewDrag
,
346 'onDesktopName': UI
.updateDocumentTitle
});
349 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
360 updateState: function(rfb
, state
, oldstate
, msg
) {
361 UI
.rfb_state
= state
;
363 if (typeof(msg
) !== 'undefined') {
367 // zero means no timeout
368 UI
.showStatus(msg
, 'error', 0);
374 UI
.showStatus(msg
, 'normal');
377 document
.getElementById('noVNC_password_dlg')
378 .classList
.add('noVNC_open');
379 setTimeout(function () {
380 document
.getElementById(('noVNC_password_input').focus());
383 UI
.showStatus(msg
, 'warn');
386 UI
.showStatus(msg
, 'warn');
391 UI
.updateVisualState();
394 // Disable/enable controls depending on connection state
395 updateVisualState: function() {
396 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
398 //Util.Debug(">> updateVisualState");
399 document
.getElementById('noVNC_setting_encrypt').disabled
= connected
;
400 document
.getElementById('noVNC_setting_true_color').disabled
= connected
;
401 if (Util
.browserSupportsCursorURIs()) {
402 document
.getElementById('noVNC_setting_cursor').disabled
= connected
;
404 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
405 document
.getElementById('noVNC_setting_cursor').disabled
= true;
408 UI
.enableDisableViewClip();
409 document
.getElementById('noVNC_setting_resize').disabled
= connected
;
410 document
.getElementById('noVNC_setting_shared').disabled
= connected
;
411 document
.getElementById('noVNC_setting_view_only').disabled
= connected
;
412 document
.getElementById('noVNC_setting_path').disabled
= connected
;
413 document
.getElementById('noVNC_setting_repeaterID').disabled
= connected
;
416 document
.documentElement
.classList
.add("noVNC_connected");
418 UI
.setMouseButton(1);
420 // Hide the controlbar after 2 seconds
421 UI
.closeControlbarTimeout
= setTimeout(UI
.closeControlbar
, 2000);
423 document
.documentElement
.classList
.remove("noVNC_connected");
424 UI
.updateXvpButton(0);
427 // State change disables viewport dragging.
428 // It is enabled (toggled) by direct click on the button
429 UI
.setViewDrag(false);
431 // State change also closes the password dialog
432 document
.getElementById('noVNC_password_dlg')
433 .classList
.remove('noVNC_open');
435 switch (UI
.rfb_state
) {
439 UI
.openConnectPanel();
447 //Util.Debug("<< updateVisualState");
450 showStatus: function(text
, status_type
, time
) {
451 var statusElem
= document
.getElementById('noVNC_status');
453 clearTimeout(UI
.statusTimeout
);
455 if (typeof status_type
=== 'undefined') {
456 status_type
= 'normal';
459 statusElem
.classList
.remove("noVNC_status_normal",
461 "noVNC_status_error");
463 switch (status_type
) {
466 statusElem
.classList
.add("noVNC_status_warn");
469 statusElem
.classList
.add("noVNC_status_error");
474 statusElem
.classList
.add("noVNC_status_normal");
478 statusElem
.innerHTML
= text
;
479 statusElem
.classList
.add("noVNC_open");
481 // If no time was specified, show the status for 1.5 seconds
482 if (typeof time
=== 'undefined') {
486 // A specified time of zero means no timeout
488 UI
.statusTimeout
= window
.setTimeout(UI
.hideStatus
, time
);
492 hideStatus: function() {
493 clearTimeout(UI
.statusTimeout
);
494 document
.getElementById('noVNC_status').classList
.remove("noVNC_open");
497 activateControlbar: function(event
) {
498 clearTimeout(UI
.idleControlbarTimeout
);
499 // We manipulate the anchor instead of the actual control
500 // bar in order to avoid creating new a stacking group
501 document
.getElementById('noVNC_control_bar_anchor')
502 .classList
.remove("noVNC_idle");
503 UI
.idleControlbarTimeout
= window
.setTimeout(UI
.idleControlbar
, 2000);
506 idleControlbar: function() {
507 document
.getElementById('noVNC_control_bar_anchor')
508 .classList
.add("noVNC_idle");
511 keepControlbar: function() {
512 clearTimeout(UI
.closeControlbarTimeout
);
515 openControlbar: function() {
516 document
.getElementById('noVNC_control_bar')
517 .classList
.add("noVNC_open");
520 closeControlbar: function() {
522 document
.getElementById('noVNC_control_bar')
523 .classList
.remove("noVNC_open");
526 toggleControlbar: function() {
527 if (document
.getElementById('noVNC_control_bar')
528 .classList
.contains("noVNC_open")) {
529 UI
.closeControlbar();
535 dragControlbarHandle: function (e
) {
536 if (!UI
.controlbarGrabbed
) return;
538 var ptr
= Util
.getPointerEvent(e
);
540 if (!UI
.controlbarDrag
) {
541 // The goal is to trigger on a certain physical width, the
542 // devicePixelRatio brings us a bit closer but is not optimal.
543 var dragThreshold
= 10 * (window
.devicePixelRatio
|| 1);
544 var dragDistance
= Math
.abs(ptr
.clientY
- UI
.controlbarMouseDownClientY
);
546 if (dragDistance
< dragThreshold
) return;
548 UI
.controlbarDrag
= true;
551 var eventY
= ptr
.clientY
- UI
.controlbarMouseDownOffsetY
;
553 UI
.moveControlbarHandle(eventY
);
559 // Move the handle but don't allow any position outside the bounds
560 moveControlbarHandle: function (posY
) {
561 var handle
= document
.getElementById("noVNC_control_bar_handle");
562 var handleHeight
= Util
.getPosition(handle
).height
;
563 var controlbar
= document
.getElementById("noVNC_control_bar");
564 var controlbarBounds
= Util
.getPosition(controlbar
);
565 var controlbarTop
= controlbarBounds
.y
;
566 var controlbarBottom
= controlbarBounds
.y
+ controlbarBounds
.height
;
569 var viewportY
= posY
;
571 // Refuse coordinates outside the control bar
572 if (viewportY
< controlbarTop
+ margin
) {
573 viewportY
= controlbarTop
+ margin
;
574 } else if (viewportY
> controlbarBottom
- handleHeight
- margin
) {
575 viewportY
= controlbarBottom
- handleHeight
- margin
;
578 // Corner case: control bar too small for stable position
579 if (controlbarBounds
.height
< (handleHeight
+ margin
* 2)) {
580 viewportY
= controlbarTop
+ (controlbarBounds
.height
- handleHeight
) / 2;
583 var relativeY
= viewportY
- controlbarTop
;
584 handle
.style
.transform
= "translateY(" + relativeY
+ "px)";
587 updateControlbarHandle: function () {
588 var handle
= document
.getElementById("noVNC_control_bar_handle");
589 var pos
= Util
.getPosition(handle
);
590 UI
.moveControlbarHandle(pos
.y
);
593 controlbarHandleMouseUp: function(e
) {
594 if ((e
.type
== "mouseup") && (e
.button
!= 0))
597 // mouseup and mousedown on the same place toggles the controlbar
598 if (UI
.controlbarGrabbed
&& !UI
.controlbarDrag
) {
599 UI
.toggleControlbar();
603 UI
.controlbarGrabbed
= false;
606 controlbarHandleMouseDown: function(e
) {
607 if ((e
.type
== "mousedown") && (e
.button
!= 0))
610 var ptr
= Util
.getPointerEvent(e
);
612 var handle
= document
.getElementById("noVNC_control_bar_handle");
613 var bounds
= handle
.getBoundingClientRect();
615 WebUtil
.setCapture(handle
);
616 UI
.controlbarGrabbed
= true;
617 UI
.controlbarDrag
= false;
619 UI
.controlbarMouseDownClientY
= ptr
.clientY
;
620 UI
.controlbarMouseDownOffsetY
= ptr
.clientY
- bounds
.top
;
631 // Initial page load read/initialization of settings
632 initSetting: function(name
, defVal
) {
633 // Check Query string followed by cookie
634 var val
= WebUtil
.getConfigVar(name
);
636 val
= WebUtil
.readSetting(name
, defVal
);
638 UI
.updateSetting(name
, val
);
642 // Update cookie and form control setting. If value is not set, then
643 // updates from control to current cookie setting.
644 updateSetting: function(name
, value
) {
646 // Save the cookie for this session
647 if (typeof value
!== 'undefined') {
648 WebUtil
.writeSetting(name
, value
);
651 // Update the settings control
652 value
= UI
.getSetting(name
);
654 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
655 if (ctrl
.type
=== 'checkbox') {
656 ctrl
.checked
= value
;
658 } else if (typeof ctrl
.options
!== 'undefined') {
659 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
660 if (ctrl
.options
[i
].value
=== value
) {
661 ctrl
.selectedIndex
= i
;
666 /*Weird IE9 error leads to 'null' appearring
667 in textboxes instead of ''.*/
668 if (value
=== null) {
675 // Save control setting to cookie
676 saveSetting: function(name
) {
677 var val
, ctrl
= document
.getElementById('noVNC_setting_' + name
);
678 if (ctrl
.type
=== 'checkbox') {
680 } else if (typeof ctrl
.options
!== 'undefined') {
681 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
685 WebUtil
.writeSetting(name
, val
);
686 //Util.Debug("Setting saved '" + name + "=" + val + "'");
690 // Force a setting to be a certain value
691 forceSetting: function(name
, val
) {
692 UI
.updateSetting(name
, val
);
696 // Read form control compatible setting from cookie
697 getSetting: function(name
) {
698 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
699 var val
= WebUtil
.readSetting(name
);
700 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
701 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
710 // Save/apply settings when 'Apply' button is pressed
711 settingsApply: function() {
712 //Util.Debug(">> settingsApply");
713 UI
.saveSetting('encrypt');
714 UI
.saveSetting('true_color');
715 if (Util
.browserSupportsCursorURIs()) {
716 UI
.saveSetting('cursor');
719 UI
.saveSetting('resize');
721 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
722 UI
.forceSetting('clip', false);
725 UI
.saveSetting('clip');
726 UI
.saveSetting('shared');
727 UI
.saveSetting('view_only');
728 UI
.saveSetting('path');
729 UI
.saveSetting('repeaterID');
730 UI
.saveSetting('stylesheet');
731 UI
.saveSetting('logging');
733 // Settings with immediate (non-connected related) effect
734 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
735 WebUtil
.init_logging(UI
.getSetting('logging'));
738 //Util.Debug("<< settingsApply");
747 closeAllPanels: function() {
748 UI
.closeSettingsPanel();
750 UI
.closeClipboardPanel();
751 UI
.closeConnectPanel();
761 openSettingsPanel: function() {
765 UI
.updateSetting('encrypt');
766 UI
.updateSetting('true_color');
767 if (Util
.browserSupportsCursorURIs()) {
768 UI
.updateSetting('cursor');
770 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
771 document
.getElementById('noVNC_setting_cursor').disabled
= true;
773 UI
.updateSetting('clip');
774 UI
.updateSetting('resize');
775 UI
.updateSetting('shared');
776 UI
.updateSetting('view_only');
777 UI
.updateSetting('path');
778 UI
.updateSetting('repeaterID');
779 UI
.updateSetting('stylesheet');
780 UI
.updateSetting('logging');
782 document
.getElementById('noVNC_settings')
783 .classList
.add("noVNC_open");
784 document
.getElementById('noVNC_settings_button')
785 .classList
.add("noVNC_selected");
788 closeSettingsPanel: function() {
789 document
.getElementById('noVNC_settings')
790 .classList
.remove("noVNC_open");
791 document
.getElementById('noVNC_settings_button')
792 .classList
.remove("noVNC_selected");
795 // Toggle the settings menu:
796 // On open, settings are refreshed from saved cookies.
797 // On close, settings are applied
798 toggleSettingsPanel: function() {
799 if (document
.getElementById('noVNC_settings')
800 .classList
.contains("noVNC_open")) {
802 UI
.closeSettingsPanel();
804 UI
.openSettingsPanel();
814 openXvpPanel: function() {
818 document
.getElementById('noVNC_xvp')
819 .classList
.add("noVNC_open");
820 document
.getElementById('noVNC_xvp_button')
821 .classList
.add("noVNC_selected");
824 closeXvpPanel: function() {
825 document
.getElementById('noVNC_xvp')
826 .classList
.remove("noVNC_open");
827 document
.getElementById('noVNC_xvp_button')
828 .classList
.remove("noVNC_selected");
831 toggleXvpPanel: function() {
832 if (document
.getElementById('noVNC_xvp')
833 .classList
.contains("noVNC_open")) {
840 // Disable/enable XVP button
841 updateXvpButton: function(ver
) {
843 document
.getElementById('noVNC_xvp_button')
844 .classList
.remove("noVNC_hidden");
846 document
.getElementById('noVNC_xvp_button')
847 .classList
.add("noVNC_hidden");
848 // Close XVP panel if open
859 openClipboardPanel: function() {
863 document
.getElementById('noVNC_clipboard')
864 .classList
.add("noVNC_open");
865 document
.getElementById('noVNC_clipboard_button')
866 .classList
.add("noVNC_selected");
869 closeClipboardPanel: function() {
870 document
.getElementById('noVNC_clipboard')
871 .classList
.remove("noVNC_open");
872 document
.getElementById('noVNC_clipboard_button')
873 .classList
.remove("noVNC_selected");
876 toggleClipboardPanel: function() {
877 if (document
.getElementById('noVNC_clipboard')
878 .classList
.contains("noVNC_open")) {
879 UI
.closeClipboardPanel();
881 UI
.openClipboardPanel();
885 clipboardReceive: function(rfb
, text
) {
886 Util
.Debug(">> UI.clipboardReceive: " + text
.substr(0,40) + "...");
887 document
.getElementById('noVNC_clipboard_text').value
= text
;
888 Util
.Debug("<< UI.clipboardReceive");
891 clipboardClear: function() {
892 document
.getElementById('noVNC_clipboard_text').value
= "";
893 UI
.rfb
.clipboardPasteFrom("");
896 clipboardSend: function() {
897 var text
= document
.getElementById('noVNC_clipboard_text').value
;
898 Util
.Debug(">> UI.clipboardSend: " + text
.substr(0,40) + "...");
899 UI
.rfb
.clipboardPasteFrom(text
);
900 Util
.Debug("<< UI.clipboardSend");
909 openConnectPanel: function() {
913 document
.getElementById('noVNC_connect_controls')
914 .classList
.add("noVNC_open");
915 document
.getElementById('noVNC_connect_controls_button')
916 .classList
.add("noVNC_selected");
918 document
.getElementById('noVNC_setting_host').focus();
921 closeConnectPanel: function() {
922 document
.getElementById('noVNC_connect_controls')
923 .classList
.remove("noVNC_open");
924 document
.getElementById('noVNC_connect_controls_button')
925 .classList
.remove("noVNC_selected");
927 UI
.saveSetting('host');
928 UI
.saveSetting('port');
929 UI
.saveSetting('token');
930 //UI.saveSetting('password');
933 toggleConnectPanel: function() {
934 if (document
.getElementById('noVNC_connect_controls')
935 .classList
.contains("noVNC_open")) {
936 UI
.closeConnectPanel();
938 UI
.openConnectPanel();
942 connect: function() {
945 var host
= document
.getElementById('noVNC_setting_host').value
;
946 var port
= document
.getElementById('noVNC_setting_port').value
;
947 var password
= document
.getElementById('noVNC_setting_password').value
;
948 var token
= document
.getElementById('noVNC_setting_token').value
;
949 var path
= document
.getElementById('noVNC_setting_path').value
;
951 //if token is in path then ignore the new token variable
953 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
956 if ((!host
) || (!port
)) {
957 throw new Error("Must set host and port");
960 if (!UI
.initRFB()) return;
962 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
963 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
964 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
965 UI
.rfb
.set_shared(UI
.getSetting('shared'));
966 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
967 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
969 UI
.rfb
.connect(host
, port
, password
, path
);
972 disconnect: function() {
976 // Restore the callback used for initial resize
977 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
979 // Don't display the connection settings until we're actually disconnected
982 setPassword: function() {
983 UI
.rfb
.sendPassword(document
.getElementById('noVNC_password_input').value
);
984 document
.getElementById('noVNC_password_dlg')
985 .classList
.remove('noVNC_open');
995 toggleFullscreen: function() {
996 if (document
.fullscreenElement
|| // alternative standard method
997 document
.mozFullScreenElement
|| // currently working methods
998 document
.webkitFullscreenElement
||
999 document
.msFullscreenElement
) {
1000 if (document
.exitFullscreen
) {
1001 document
.exitFullscreen();
1002 } else if (document
.mozCancelFullScreen
) {
1003 document
.mozCancelFullScreen();
1004 } else if (document
.webkitExitFullscreen
) {
1005 document
.webkitExitFullscreen();
1006 } else if (document
.msExitFullscreen
) {
1007 document
.msExitFullscreen();
1010 if (document
.documentElement
.requestFullscreen
) {
1011 document
.documentElement
.requestFullscreen();
1012 } else if (document
.documentElement
.mozRequestFullScreen
) {
1013 document
.documentElement
.mozRequestFullScreen();
1014 } else if (document
.documentElement
.webkitRequestFullscreen
) {
1015 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
1016 } else if (document
.body
.msRequestFullscreen
) {
1017 document
.body
.msRequestFullscreen();
1020 UI
.enableDisableViewClip();
1021 UI
.updateFullscreenButton();
1024 updateFullscreenButton: function() {
1025 if (document
.fullscreenElement
|| // alternative standard method
1026 document
.mozFullScreenElement
|| // currently working methods
1027 document
.webkitFullscreenElement
||
1028 document
.msFullscreenElement
) {
1029 document
.getElementById('noVNC_fullscreen_button')
1030 .classList
.add("noVNC_selected");
1032 document
.getElementById('noVNC_fullscreen_button')
1033 .classList
.remove("noVNC_selected");
1043 // Apply remote resizing or local scaling
1044 applyResizeMode: function() {
1045 if (!UI
.rfb
) return;
1047 var screen
= UI
.screenSize();
1049 if (screen
&& UI
.rfb_state
=== 'normal' && UI
.rfb
.get_display()) {
1051 var display
= UI
.rfb
.get_display();
1052 var resizeMode
= UI
.getSetting('resize');
1054 if (resizeMode
=== 'remote') {
1056 // Request changing the resolution of the remote display to
1057 // the size of the local browser viewport.
1059 // In order to not send multiple requests before the browser-resize
1060 // is finished we wait 0.5 seconds before sending the request.
1061 clearTimeout(UI
.resizeTimeout
);
1062 UI
.resizeTimeout
= setTimeout(function(){
1064 // Limit the viewport to the size of the browser window
1065 display
.set_maxWidth(screen
.w
);
1066 display
.set_maxHeight(screen
.h
);
1068 Util
.Debug('Attempting requestDesktopSize(' +
1069 screen
.w
+ ', ' + screen
.h
+ ')');
1071 // Request a remote size covering the viewport
1072 UI
.rfb
.requestDesktopSize(screen
.w
, screen
.h
);
1075 } else if (resizeMode
=== 'scale' || resizeMode
=== 'downscale') {
1076 var downscaleOnly
= resizeMode
=== 'downscale';
1077 var scaleRatio
= display
.autoscale(screen
.w
, screen
.h
, downscaleOnly
);
1078 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
1079 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
1084 // The screen is always the same size as the available viewport
1085 // in the browser window minus the height of the control bar
1086 screenSize: function() {
1087 var screen
= document
.getElementById('noVNC_screen');
1089 // Hide the scrollbars until the size is calculated
1090 screen
.style
.overflow
= "hidden";
1092 var pos
= Util
.getPosition(screen
);
1096 screen
.style
.overflow
= "visible";
1098 if (isNaN(w
) || isNaN(h
)) {
1101 return {w
: w
, h
: h
};
1105 // Normally we only apply the current resize mode after a window resize
1106 // event. This means that when a new connection is opened, there is no
1107 // resize mode active.
1108 // We have to wait until the first FBU because this is where the client
1109 // will find the supported encodings of the server. Some calls later in
1110 // the chain is dependant on knowing the server-capabilities.
1111 initialResize: function(rfb
, fbu
) {
1112 UI
.applyResizeMode();
1113 // After doing this once, we remove the callback.
1114 UI
.rfb
.set_onFBUComplete(function() { });
1123 // Set and configure viewport clipping
1124 setViewClip: function(clip
) {
1125 UI
.updateSetting('clip', clip
);
1126 UI
.updateViewClip();
1129 // Update parameters that depend on the clip setting
1130 updateViewClip: function() {
1136 var display
= UI
.rfb
.get_display();
1137 var cur_clip
= display
.get_viewport();
1138 var new_clip
= UI
.getSetting('clip');
1140 if (cur_clip
!== new_clip
) {
1141 display
.set_viewport(new_clip
);
1144 var size
= UI
.screenSize();
1146 if (new_clip
&& size
) {
1147 // When clipping is enabled, the screen is limited to
1148 // the size of the browser window.
1149 display
.set_maxWidth(size
.w
);
1150 display
.set_maxHeight(size
.h
);
1152 var screen
= document
.getElementById('noVNC_screen');
1153 var canvas
= document
.getElementById('noVNC_canvas');
1155 // Hide potential scrollbars that can skew the position
1156 screen
.style
.overflow
= "hidden";
1158 // The x position marks the left margin of the canvas,
1159 // remove the margin from both sides to keep it centered.
1160 var new_w
= size
.w
- (2 * Util
.getPosition(canvas
).x
);
1162 screen
.style
.overflow
= "visible";
1164 display
.viewportChangeSize(new_w
, size
.h
);
1166 // Disable max dimensions
1167 display
.set_maxWidth(0);
1168 display
.set_maxHeight(0);
1169 display
.viewportChangeSize();
1173 // Handle special cases where clipping is forced on/off or locked
1174 enableDisableViewClip: function() {
1175 var resizeSetting
= document
.getElementById('noVNC_setting_resize');
1176 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
1179 // Safari auto-hides the scrollbars which makes them
1180 // impossible to use in most cases
1181 UI
.setViewClip(true);
1182 document
.getElementById('noVNC_setting_clip').disabled
= true;
1183 } else if (resizeSetting
.value
=== 'downscale' || resizeSetting
.value
=== 'scale') {
1184 // Disable clipping if we are scaling
1185 UI
.setViewClip(false);
1186 document
.getElementById('noVNC_setting_clip').disabled
= true;
1187 } else if (document
.msFullscreenElement
) {
1188 // The browser is IE and we are in fullscreen mode.
1189 // - We need to force clipping while in fullscreen since
1190 // scrollbars doesn't work.
1191 UI
.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
1192 UI
.rememberedClipSetting
= UI
.getSetting('clip');
1193 UI
.setViewClip(true);
1194 document
.getElementById('noVNC_setting_clip').disabled
= true;
1195 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
1196 // Restore view clip to what it was before fullscreen on IE
1197 UI
.setViewClip(UI
.rememberedClipSetting
);
1198 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1200 document
.getElementById('noVNC_setting_clip').disabled
= connected
|| UI
.isTouchDevice
;
1201 if (UI
.isTouchDevice
) {
1202 UI
.setViewClip(true);
1213 toggleViewDrag: function() {
1214 if (!UI
.rfb
) return;
1216 var drag
= UI
.rfb
.get_viewportDrag();
1217 UI
.setViewDrag(!drag
);
1220 // Set the view drag mode which moves the viewport on mouse drags
1221 setViewDrag: function(drag
) {
1222 if (!UI
.rfb
) return;
1224 UI
.rfb
.set_viewportDrag(drag
);
1226 UI
.updateViewDrag();
1229 updateViewDrag: function() {
1230 var clipping
= false;
1232 if (UI
.rfb_state
!== 'normal') return;
1234 // Check if viewport drag is possible. It is only possible
1235 // if the remote display is clipping the client display.
1236 if (UI
.rfb
.get_display().get_viewport() &&
1237 UI
.rfb
.get_display().clippingDisplay()) {
1241 var viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1244 UI
.rfb
.get_viewportDrag()) {
1245 // The size of the remote display is the same or smaller
1246 // than the client display. Make sure viewport drag isn't
1247 // active when it can't be used.
1248 UI
.rfb
.set_viewportDrag(false);
1251 if (UI
.rfb
.get_viewportDrag()) {
1252 viewDragButton
.classList
.add("noVNC_selected");
1254 viewDragButton
.classList
.remove("noVNC_selected");
1257 // Different behaviour for touch vs non-touch
1258 // The button is disabled instead of hidden on touch devices
1259 if (UI
.isTouchDevice
) {
1260 viewDragButton
.classList
.remove("noVNC_hidden");
1263 viewDragButton
.disabled
= false;
1265 viewDragButton
.disabled
= true;
1268 viewDragButton
.disabled
= false;
1271 viewDragButton
.classList
.remove("noVNC_hidden");
1273 viewDragButton
.classList
.add("noVNC_hidden");
1284 // On touch devices, show the OS keyboard
1285 showKeyboard: function() {
1286 var kbi
= document
.getElementById('noVNC_keyboardinput');
1287 var skb
= document
.getElementById('noVNC_keyboard_button');
1288 var l
= kbi
.value
.length
;
1289 if(UI
.keyboardVisible
=== false) {
1291 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
1292 catch (err
) {} // setSelectionRange is undefined in Google Chrome
1293 UI
.keyboardVisible
= true;
1294 skb
.classList
.add("noVNC_selected");
1295 } else if(UI
.keyboardVisible
=== true) {
1297 skb
.classList
.remove("noVNC_selected");
1298 UI
.keyboardVisible
= false;
1302 hideKeyboard: function() {
1303 document
.getElementById('noVNC_keyboard_button')
1304 .classList
.remove("noVNC_selected");
1305 //Weird bug in iOS if you change keyboardVisible
1306 //here it does not actually occur so next time
1307 //you click keyboard icon it doesnt work.
1308 UI
.hideKeyboardTimeout
= setTimeout(function() {
1309 UI
.keyboardVisible
= false;
1313 keepKeyboard: function() {
1314 clearTimeout(UI
.hideKeyboardTimeout
);
1315 if(UI
.keyboardVisible
=== true) {
1316 document
.getElementById('noVNC_keyboardinput').focus();
1317 document
.getElementById('noVNC_keyboard_button')
1318 .classList
.add("noVNC_selected");
1319 } else if(UI
.keyboardVisible
=== false) {
1320 document
.getElementById('noVNC_keyboardinput').blur();
1321 document
.getElementById('noVNC_keyboard_button')
1322 .classList
.remove("noVNC_selected");
1326 keyboardinputReset: function() {
1327 var kbi
= document
.getElementById('noVNC_keyboardinput');
1328 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1329 UI
.lastKeyboardinput
= kbi
.value
;
1332 // When normal keyboard events are left uncought, use the input events from
1333 // the keyboardinput element instead and generate the corresponding key events.
1334 // This code is required since some browsers on Android are inconsistent in
1335 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1336 keyInput: function(event
) {
1338 if (!UI
.rfb
) return;
1340 var newValue
= event
.target
.value
;
1342 if (!UI
.lastKeyboardinput
) {
1343 UI
.keyboardinputReset();
1345 var oldValue
= UI
.lastKeyboardinput
;
1349 // Try to check caret position since whitespace at the end
1350 // will not be considered by value.length in some browsers
1351 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1353 // selectionStart is undefined in Google Chrome
1354 newLen
= newValue
.length
;
1356 var oldLen
= oldValue
.length
;
1359 var inputs
= newLen
- oldLen
;
1361 backspaces
= -inputs
;
1366 // Compare the old string with the new to account for
1367 // text-corrections or other input that modify existing text
1369 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1370 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1371 inputs
= newLen
- i
;
1372 backspaces
= oldLen
- i
;
1377 // Send the key events
1378 for (i
= 0; i
< backspaces
; i
++) {
1379 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
);
1381 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1382 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1385 // Control the text content length in the keyboardinput element
1386 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1387 UI
.keyboardinputReset();
1388 } else if (newLen
< 1) {
1389 // There always have to be some text in the keyboardinput
1390 // element with which backspace can interact.
1391 UI
.keyboardinputReset();
1392 // This sometimes causes the keyboard to disappear for a second
1393 // but it is required for the android keyboard to recognize that
1394 // text has been added to the field
1395 event
.target
.blur();
1396 // This has to be ran outside of the input handler in order to work
1397 setTimeout(UI
.keepKeyboard
, 0);
1399 UI
.lastKeyboardinput
= newValue
;
1403 openExtraKeys: function() {
1404 UI
.closeAllPanels();
1405 UI
.openControlbar();
1407 document
.getElementById('noVNC_modifiers')
1408 .classList
.add("noVNC_open");
1409 document
.getElementById('noVNC_toggle_extra_keys_button')
1410 .classList
.add("noVNC_selected");
1413 closeExtraKeys: function() {
1414 document
.getElementById('noVNC_modifiers')
1415 .classList
.remove("noVNC_open");
1416 document
.getElementById('noVNC_toggle_extra_keys_button')
1417 .classList
.remove("noVNC_selected");
1420 toggleExtraKeys: function() {
1422 if(document
.getElementById('noVNC_modifiers')
1423 .classList
.contains("noVNC_open")) {
1424 UI
.closeExtraKeys();
1430 sendEsc: function() {
1432 UI
.rfb
.sendKey(KeyTable
.XK_Escape
);
1435 sendTab: function() {
1437 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1440 toggleCtrl: function() {
1442 var btn
= document
.getElementById('noVNC_toggle_ctrl_button');
1443 if (btn
.classList
.contains("noVNC_selected")) {
1444 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, false);
1445 btn
.classList
.remove("noVNC_selected");
1447 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, true);
1448 btn
.classList
.add("noVNC_selected");
1452 toggleAlt: function() {
1454 var btn
= document
.getElementById('noVNC_toggle_alt_button');
1455 if (btn
.classList
.contains("noVNC_selected")) {
1456 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, false);
1457 btn
.classList
.remove("noVNC_selected");
1459 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, true);
1460 btn
.classList
.add("noVNC_selected");
1464 sendCtrlAltDel: function() {
1466 UI
.rfb
.sendCtrlAltDel();
1475 setMouseButton: function(num
) {
1477 UI
.rfb
.get_mouse().set_touchButton(num
);
1480 var blist
= [0, 1,2,4];
1481 for (var b
= 0; b
< blist
.length
; b
++) {
1482 var button
= document
.getElementById('noVNC_mouse_button' + blist
[b
]);
1483 if (blist
[b
] === num
) {
1484 button
.classList
.remove("noVNC_hidden");
1486 button
.classList
.add("noVNC_hidden");
1491 displayBlur: function() {
1492 if (!UI
.rfb
) return;
1494 UI
.rfb
.get_keyboard().set_focused(false);
1495 UI
.rfb
.get_mouse().set_focused(false);
1498 displayFocus: function() {
1499 if (!UI
.rfb
) return;
1501 UI
.rfb
.get_keyboard().set_focused(true);
1502 UI
.rfb
.get_mouse().set_focused(true);
1505 // Display the desktop name in the document title
1506 updateDocumentTitle: function(rfb
, name
) {
1507 document
.title
= name
+ " - noVNC";
1510 //Helper to add options to dropdown.
1511 addOption: function(selectbox
, text
, value
) {
1512 var optn
= document
.createElement("OPTION");
1515 selectbox
.options
.add(optn
);
1524 /* [module] UI.load(); */
1527 /* [module] export default UI; */