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] */
44 hideKeyboardTimeout
: null,
45 idleControlbarTimeout
: null,
46 closeControlbarTimeout
: null,
48 controlbarGrabbed
: false,
49 controlbarDrag
: false,
50 controlbarMouseDownClientY
: 0,
51 controlbarMouseDownOffsetY
: 0,
52 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
.isSafari
= (navigator
.userAgent
.indexOf('Safari') !== -1 &&
70 navigator
.userAgent
.indexOf('Chrome') === -1);
74 // Adapt the interface for touch screen devices
75 if (Util
.isTouchDevice
) {
76 document
.documentElement
.classList
.add("noVNC_touch");
77 // Remove the address bar
78 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
79 UI
.forceSetting('clip', true);
81 UI
.initSetting('clip', false);
84 // Setup and initialize event handlers
85 UI
.setupWindowEvents();
87 UI
.addControlbarHandlers();
88 UI
.addTouchSpecificHandlers();
89 UI
.addExtraKeysHandlers();
91 UI
.addConnectionControlHandlers();
92 UI
.addClipboardHandlers();
93 UI
.addSettingsHandlers();
95 // Show the connect panel on first load unless autoconnecting
97 UI
.openConnectPanel();
102 UI
.updateVisualState();
104 document
.getElementById('noVNC_setting_host').focus();
106 var autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
107 if (autoconnect
=== 'true' || autoconnect
== '1') {
114 if (typeof callback
=== "function") {
119 initSettings: function() {
120 // Stylesheet selection dropdown
121 var sheet
= WebUtil
.selectStylesheet();
122 var sheets
= WebUtil
.getStylesheets();
124 for (i
= 0; i
< sheets
.length
; i
+= 1) {
125 UI
.addOption(document
.getElementById('noVNC_setting_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
128 // Logging selection dropdown
129 var llevels
= ['error', 'warn', 'info', 'debug'];
130 for (i
= 0; i
< llevels
.length
; i
+= 1) {
131 UI
.addOption(document
.getElementById('noVNC_setting_logging'),llevels
[i
], llevels
[i
]);
134 // Settings with immediate effects
135 UI
.initSetting('logging', 'warn');
136 WebUtil
.init_logging(UI
.getSetting('logging'));
138 UI
.initSetting('stylesheet', 'default');
139 WebUtil
.selectStylesheet(null);
140 // call twice to get around webkit bug
141 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
143 // if port == 80 (or 443) then it won't be present and should be
145 var port
= window
.location
.port
;
147 if (window
.location
.protocol
.substring(0,5) == 'https') {
150 else if (window
.location
.protocol
.substring(0,4) == 'http') {
155 /* Populate the controls if defaults are provided in the URL */
156 UI
.initSetting('host', window
.location
.hostname
);
157 UI
.initSetting('port', port
);
158 UI
.initSetting('password', '');
159 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
160 UI
.initSetting('true_color', true);
161 UI
.initSetting('cursor', !Util
.isTouchDevice
);
162 UI
.initSetting('resize', 'off');
163 UI
.initSetting('shared', true);
164 UI
.initSetting('view_only', false);
165 UI
.initSetting('path', 'websockify');
166 UI
.initSetting('repeaterID', '');
167 UI
.initSetting('token', '');
170 setupWindowEvents: function() {
171 window
.addEventListener( 'resize', function () {
172 UI
.applyResizeMode();
177 document
.getElementById("noVNC_status")
178 .addEventListener('click', UI
.hideStatus
);
181 setupFullscreen: function() {
182 // Only show the button if fullscreen is properly supported
183 // * Safari doesn't support alphanumerical input while in fullscreen
185 (document
.documentElement
.requestFullscreen
||
186 document
.documentElement
.mozRequestFullScreen
||
187 document
.documentElement
.webkitRequestFullscreen
||
188 document
.body
.msRequestFullscreen
)) {
189 document
.getElementById('noVNC_fullscreen_button')
190 .classList
.remove("noVNC_hidden");
191 UI
.addFullscreenHandlers();
195 addControlbarHandlers: function() {
196 document
.getElementById("noVNC_control_bar")
197 .addEventListener('mousemove', UI
.activateControlbar
);
198 document
.getElementById("noVNC_control_bar")
199 .addEventListener('mouseup', UI
.activateControlbar
);
200 document
.getElementById("noVNC_control_bar")
201 .addEventListener('mousedown', UI
.activateControlbar
);
202 document
.getElementById("noVNC_control_bar")
203 .addEventListener('keypress', UI
.activateControlbar
);
205 document
.getElementById("noVNC_control_bar")
206 .addEventListener('mousedown', UI
.keepControlbar
);
207 document
.getElementById("noVNC_control_bar")
208 .addEventListener('keypress', UI
.keepControlbar
);
210 document
.getElementById("noVNC_view_drag_button")
211 .addEventListener('click', UI
.toggleViewDrag
);
213 document
.getElementById("noVNC_control_bar_handle")
214 .addEventListener('mousedown', UI
.controlbarHandleMouseDown
);
215 document
.getElementById("noVNC_control_bar_handle")
216 .addEventListener('mouseup', UI
.controlbarHandleMouseUp
);
217 document
.getElementById("noVNC_control_bar_handle")
218 .addEventListener('mousemove', UI
.dragControlbarHandle
);
219 // resize events aren't available for elements
220 window
.addEventListener('resize', UI
.updateControlbarHandle
);
223 addTouchSpecificHandlers: function() {
224 document
.getElementById("noVNC_mouse_button0")
225 .addEventListener('click', function () { UI
.setMouseButton(1); });
226 document
.getElementById("noVNC_mouse_button1")
227 .addEventListener('click', function () { UI
.setMouseButton(2); });
228 document
.getElementById("noVNC_mouse_button2")
229 .addEventListener('click', function () { UI
.setMouseButton(4); });
230 document
.getElementById("noVNC_mouse_button4")
231 .addEventListener('click', function () { UI
.setMouseButton(0); });
232 document
.getElementById("noVNC_keyboard_button")
233 .addEventListener('click', UI
.toggleVirtualKeyboard
);
235 document
.getElementById("noVNC_keyboardinput")
236 .addEventListener('input', UI
.keyInput
);
237 document
.getElementById("noVNC_keyboardinput")
238 .addEventListener('blur', UI
.onblurVirtualKeyboard
);
239 document
.getElementById("noVNC_keyboardinput")
240 .addEventListener('submit', function () { return false; });
242 document
.getElementById("noVNC_control_bar")
243 .addEventListener('touchstart', UI
.activateControlbar
);
244 document
.getElementById("noVNC_control_bar")
245 .addEventListener('touchmove', UI
.activateControlbar
);
246 document
.getElementById("noVNC_control_bar")
247 .addEventListener('touchend', UI
.activateControlbar
);
248 document
.getElementById("noVNC_control_bar")
249 .addEventListener('input', UI
.activateControlbar
);
251 document
.getElementById("noVNC_control_bar")
252 .addEventListener('touchstart', UI
.keepControlbar
);
253 document
.getElementById("noVNC_control_bar")
254 .addEventListener('input', UI
.keepControlbar
);
256 document
.getElementById("noVNC_control_bar_handle")
257 .addEventListener('touchstart', UI
.controlbarHandleMouseDown
);
258 document
.getElementById("noVNC_control_bar_handle")
259 .addEventListener('touchend', UI
.controlbarHandleMouseUp
);
260 document
.getElementById("noVNC_control_bar_handle")
261 .addEventListener('touchmove', UI
.dragControlbarHandle
);
263 window
.addEventListener('load', UI
.keyboardinputReset
);
266 addExtraKeysHandlers: function() {
267 document
.getElementById("noVNC_toggle_extra_keys_button")
268 .addEventListener('click', UI
.toggleExtraKeys
);
269 document
.getElementById("noVNC_toggle_ctrl_button")
270 .addEventListener('click', UI
.toggleCtrl
);
271 document
.getElementById("noVNC_toggle_alt_button")
272 .addEventListener('click', UI
.toggleAlt
);
273 document
.getElementById("noVNC_send_tab_button")
274 .addEventListener('click', UI
.sendTab
);
275 document
.getElementById("noVNC_send_esc_button")
276 .addEventListener('click', UI
.sendEsc
);
277 document
.getElementById("noVNC_send_ctrl_alt_del_button")
278 .addEventListener('click', UI
.sendCtrlAltDel
);
281 addXvpHandlers: function() {
282 document
.getElementById("noVNC_xvp_shutdown_button")
283 .addEventListener('click', function() { UI
.rfb
.xvpShutdown(); });
284 document
.getElementById("noVNC_xvp_reboot_button")
285 .addEventListener('click', function() { UI
.rfb
.xvpReboot(); });
286 document
.getElementById("noVNC_xvp_reset_button")
287 .addEventListener('click', function() { UI
.rfb
.xvpReset(); });
288 document
.getElementById("noVNC_xvp_button")
289 .addEventListener('click', UI
.toggleXvpPanel
);
292 addConnectionControlHandlers: function() {
293 document
.getElementById("noVNC_connect_controls_button")
294 .addEventListener('click', UI
.toggleConnectPanel
);
295 document
.getElementById("noVNC_disconnect_button")
296 .addEventListener('click', UI
.disconnect
);
297 document
.getElementById("noVNC_connect_button")
298 .addEventListener('click', UI
.connect
);
300 document
.getElementById("noVNC_password_button")
301 .addEventListener('click', UI
.setPassword
);
304 addClipboardHandlers: function() {
305 document
.getElementById("noVNC_clipboard_button")
306 .addEventListener('click', UI
.toggleClipboardPanel
);
307 document
.getElementById("noVNC_clipboard_text")
308 .addEventListener('focus', UI
.displayBlur
);
309 document
.getElementById("noVNC_clipboard_text")
310 .addEventListener('blur', UI
.displayFocus
);
311 document
.getElementById("noVNC_clipboard_text")
312 .addEventListener('change', UI
.clipboardSend
);
313 document
.getElementById("noVNC_clipboard_clear_button")
314 .addEventListener('click', UI
.clipboardClear
);
317 addSettingsHandlers: function() {
318 document
.getElementById("noVNC_settings_button")
319 .addEventListener('click', UI
.toggleSettingsPanel
);
320 document
.getElementById("noVNC_settings_apply")
321 .addEventListener('click', UI
.settingsApply
);
323 document
.getElementById("noVNC_setting_resize")
324 .addEventListener('change', UI
.enableDisableViewClip
);
327 addFullscreenHandlers: function() {
328 document
.getElementById("noVNC_fullscreen_button")
329 .addEventListener('click', UI
.toggleFullscreen
);
331 window
.addEventListener('fullscreenchange', UI
.updateFullscreenButton
);
332 window
.addEventListener('mozfullscreenchange', UI
.updateFullscreenButton
);
333 window
.addEventListener('webkitfullscreenchange', UI
.updateFullscreenButton
);
334 window
.addEventListener('msfullscreenchange', UI
.updateFullscreenButton
);
337 initRFB: function() {
339 UI
.rfb
= new RFB({'target': document
.getElementById('noVNC_canvas'),
340 'onNotification': UI
.notification
,
341 'onUpdateState': UI
.updateState
,
342 'onDisconnected': UI
.disconnectFinished
,
343 'onPasswordRequired': UI
.passwordRequired
,
344 'onXvpInit': UI
.updateXvpButton
,
345 'onClipboard': UI
.clipboardReceive
,
347 'onFBUComplete': UI
.initialResize
,
348 'onFBResize': UI
.updateViewDrag
,
349 'onDesktopName': UI
.updateDesktopName
});
352 UI
.showStatus('Unable to create RFB client -- ' + exc
, 'error');
363 updateState: function(rfb
, state
, oldstate
) {
366 UI
.showStatus("Connecting");
370 if (rfb
&& rfb
.get_encrypt()) {
371 UI
.showStatus("Connected (encrypted) to " +
374 UI
.showStatus("Connected (unencrypted) to " +
378 case 'disconnecting':
379 UI
.showStatus("Disconnecting");
382 UI
.connected
= false;
383 UI
.showStatus("Disconnected");
386 UI
.showStatus("Invalid state", 'error');
390 UI
.updateVisualState();
393 // Disable/enable controls depending on connection state
394 updateVisualState: function() {
395 //Util.Debug(">> updateVisualState");
396 document
.getElementById('noVNC_setting_encrypt').disabled
= UI
.connected
;
397 document
.getElementById('noVNC_setting_true_color').disabled
= UI
.connected
;
398 if (Util
.browserSupportsCursorURIs()) {
399 document
.getElementById('noVNC_setting_cursor').disabled
= UI
.connected
;
401 UI
.updateSetting('cursor', !Util
.isTouchDevice
);
402 document
.getElementById('noVNC_setting_cursor').disabled
= true;
405 UI
.enableDisableViewClip();
406 document
.getElementById('noVNC_setting_resize').disabled
= UI
.connected
;
407 document
.getElementById('noVNC_setting_shared').disabled
= UI
.connected
;
408 document
.getElementById('noVNC_setting_view_only').disabled
= UI
.connected
;
409 document
.getElementById('noVNC_setting_path').disabled
= UI
.connected
;
410 document
.getElementById('noVNC_setting_repeaterID').disabled
= UI
.connected
;
413 document
.documentElement
.classList
.add("noVNC_connected");
415 UI
.setMouseButton(1);
417 // Hide the controlbar after 2 seconds
418 UI
.closeControlbarTimeout
= setTimeout(UI
.closeControlbar
, 2000);
420 document
.documentElement
.classList
.remove("noVNC_connected");
421 UI
.updateXvpButton(0);
425 // State change disables viewport dragging.
426 // It is enabled (toggled) by direct click on the button
427 UI
.setViewDrag(false);
429 // State change also closes the password dialog
430 document
.getElementById('noVNC_password_dlg')
431 .classList
.remove('noVNC_open');
433 //Util.Debug("<< updateVisualState");
436 showStatus: function(text
, status_type
, time
) {
437 var statusElem
= document
.getElementById('noVNC_status');
439 clearTimeout(UI
.statusTimeout
);
441 if (typeof status_type
=== 'undefined') {
442 status_type
= 'normal';
445 statusElem
.classList
.remove("noVNC_status_normal",
447 "noVNC_status_error");
449 switch (status_type
) {
452 statusElem
.classList
.add("noVNC_status_warn");
455 statusElem
.classList
.add("noVNC_status_error");
460 statusElem
.classList
.add("noVNC_status_normal");
464 statusElem
.innerHTML
= text
;
465 statusElem
.classList
.add("noVNC_open");
467 // If no time was specified, show the status for 1.5 seconds
468 if (typeof time
=== 'undefined') {
472 // Error messages do not timeout
473 if (status_type
!== 'error') {
474 UI
.statusTimeout
= window
.setTimeout(UI
.hideStatus
, time
);
478 hideStatus: function() {
479 clearTimeout(UI
.statusTimeout
);
480 document
.getElementById('noVNC_status').classList
.remove("noVNC_open");
483 notification: function (rfb
, msg
, level
, options
) {
484 UI
.showStatus(msg
, level
);
487 activateControlbar: function(event
) {
488 clearTimeout(UI
.idleControlbarTimeout
);
489 // We manipulate the anchor instead of the actual control
490 // bar in order to avoid creating new a stacking group
491 document
.getElementById('noVNC_control_bar_anchor')
492 .classList
.remove("noVNC_idle");
493 UI
.idleControlbarTimeout
= window
.setTimeout(UI
.idleControlbar
, 2000);
496 idleControlbar: function() {
497 document
.getElementById('noVNC_control_bar_anchor')
498 .classList
.add("noVNC_idle");
501 keepControlbar: function() {
502 clearTimeout(UI
.closeControlbarTimeout
);
505 openControlbar: function() {
506 document
.getElementById('noVNC_control_bar')
507 .classList
.add("noVNC_open");
510 closeControlbar: function() {
512 document
.getElementById('noVNC_control_bar')
513 .classList
.remove("noVNC_open");
516 toggleControlbar: function() {
517 if (document
.getElementById('noVNC_control_bar')
518 .classList
.contains("noVNC_open")) {
519 UI
.closeControlbar();
525 dragControlbarHandle: function (e
) {
526 if (!UI
.controlbarGrabbed
) return;
528 var ptr
= Util
.getPointerEvent(e
);
530 if (!UI
.controlbarDrag
) {
531 // The goal is to trigger on a certain physical width, the
532 // devicePixelRatio brings us a bit closer but is not optimal.
533 var dragThreshold
= 10 * (window
.devicePixelRatio
|| 1);
534 var dragDistance
= Math
.abs(ptr
.clientY
- UI
.controlbarMouseDownClientY
);
536 if (dragDistance
< dragThreshold
) return;
538 UI
.controlbarDrag
= true;
541 var eventY
= ptr
.clientY
- UI
.controlbarMouseDownOffsetY
;
543 UI
.moveControlbarHandle(eventY
);
549 // Move the handle but don't allow any position outside the bounds
550 moveControlbarHandle: function (posY
) {
551 var handle
= document
.getElementById("noVNC_control_bar_handle");
552 var handleHeight
= Util
.getPosition(handle
).height
;
553 var controlbar
= document
.getElementById("noVNC_control_bar");
554 var controlbarBounds
= Util
.getPosition(controlbar
);
555 var controlbarTop
= controlbarBounds
.y
;
556 var controlbarBottom
= controlbarBounds
.y
+ controlbarBounds
.height
;
559 var viewportY
= posY
;
561 // Refuse coordinates outside the control bar
562 if (viewportY
< controlbarTop
+ margin
) {
563 viewportY
= controlbarTop
+ margin
;
564 } else if (viewportY
> controlbarBottom
- handleHeight
- margin
) {
565 viewportY
= controlbarBottom
- handleHeight
- margin
;
568 // Corner case: control bar too small for stable position
569 if (controlbarBounds
.height
< (handleHeight
+ margin
* 2)) {
570 viewportY
= controlbarTop
+ (controlbarBounds
.height
- handleHeight
) / 2;
573 var relativeY
= viewportY
- controlbarTop
;
574 handle
.style
.transform
= "translateY(" + relativeY
+ "px)";
577 updateControlbarHandle: function () {
578 var handle
= document
.getElementById("noVNC_control_bar_handle");
579 var pos
= Util
.getPosition(handle
);
580 UI
.moveControlbarHandle(pos
.y
);
583 controlbarHandleMouseUp: function(e
) {
584 if ((e
.type
== "mouseup") && (e
.button
!= 0)) return;
586 // mouseup and mousedown on the same place toggles the controlbar
587 if (UI
.controlbarGrabbed
&& !UI
.controlbarDrag
) {
588 UI
.toggleControlbar();
592 UI
.controlbarGrabbed
= false;
595 controlbarHandleMouseDown: function(e
) {
596 if ((e
.type
== "mousedown") && (e
.button
!= 0)) return;
598 var ptr
= Util
.getPointerEvent(e
);
600 var handle
= document
.getElementById("noVNC_control_bar_handle");
601 var bounds
= handle
.getBoundingClientRect();
603 WebUtil
.setCapture(handle
);
604 UI
.controlbarGrabbed
= true;
605 UI
.controlbarDrag
= false;
607 UI
.controlbarMouseDownClientY
= ptr
.clientY
;
608 UI
.controlbarMouseDownOffsetY
= ptr
.clientY
- bounds
.top
;
619 // Initial page load read/initialization of settings
620 initSetting: function(name
, defVal
) {
621 // Check Query string followed by cookie
622 var val
= WebUtil
.getConfigVar(name
);
624 val
= WebUtil
.readSetting(name
, defVal
);
626 UI
.updateSetting(name
, val
);
630 // Update cookie and form control setting. If value is not set, then
631 // updates from control to current cookie setting.
632 updateSetting: function(name
, value
) {
634 // Save the cookie for this session
635 if (typeof value
!== 'undefined') {
636 WebUtil
.writeSetting(name
, value
);
639 // Update the settings control
640 value
= UI
.getSetting(name
);
642 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
643 if (ctrl
.type
=== 'checkbox') {
644 ctrl
.checked
= value
;
646 } else if (typeof ctrl
.options
!== 'undefined') {
647 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
648 if (ctrl
.options
[i
].value
=== value
) {
649 ctrl
.selectedIndex
= i
;
654 /*Weird IE9 error leads to 'null' appearring
655 in textboxes instead of ''.*/
656 if (value
=== null) {
663 // Save control setting to cookie
664 saveSetting: function(name
) {
665 var val
, ctrl
= document
.getElementById('noVNC_setting_' + name
);
666 if (ctrl
.type
=== 'checkbox') {
668 } else if (typeof ctrl
.options
!== 'undefined') {
669 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
673 WebUtil
.writeSetting(name
, val
);
674 //Util.Debug("Setting saved '" + name + "=" + val + "'");
678 // Force a setting to be a certain value
679 forceSetting: function(name
, val
) {
680 UI
.updateSetting(name
, val
);
684 // Read form control compatible setting from cookie
685 getSetting: function(name
) {
686 var ctrl
= document
.getElementById('noVNC_setting_' + name
);
687 var val
= WebUtil
.readSetting(name
);
688 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
689 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
698 // Save/apply settings when 'Apply' button is pressed
699 settingsApply: function() {
700 //Util.Debug(">> settingsApply");
701 UI
.saveSetting('encrypt');
702 UI
.saveSetting('true_color');
703 if (Util
.browserSupportsCursorURIs()) {
704 UI
.saveSetting('cursor');
707 UI
.saveSetting('resize');
709 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
710 UI
.forceSetting('clip', false);
713 UI
.saveSetting('clip');
714 UI
.saveSetting('shared');
715 UI
.saveSetting('view_only');
716 UI
.saveSetting('path');
717 UI
.saveSetting('repeaterID');
718 UI
.saveSetting('stylesheet');
719 UI
.saveSetting('logging');
721 // Settings with immediate (non-connected related) effect
722 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
723 WebUtil
.init_logging(UI
.getSetting('logging'));
726 //Util.Debug("<< settingsApply");
735 closeAllPanels: function() {
736 UI
.closeSettingsPanel();
738 UI
.closeClipboardPanel();
739 UI
.closeConnectPanel();
749 openSettingsPanel: function() {
753 UI
.updateSetting('encrypt');
754 UI
.updateSetting('true_color');
755 if (Util
.browserSupportsCursorURIs()) {
756 UI
.updateSetting('cursor');
758 UI
.updateSetting('cursor', !Util
.isTouchDevice
);
759 document
.getElementById('noVNC_setting_cursor').disabled
= true;
761 UI
.updateSetting('clip');
762 UI
.updateSetting('resize');
763 UI
.updateSetting('shared');
764 UI
.updateSetting('view_only');
765 UI
.updateSetting('path');
766 UI
.updateSetting('repeaterID');
767 UI
.updateSetting('stylesheet');
768 UI
.updateSetting('logging');
770 document
.getElementById('noVNC_settings')
771 .classList
.add("noVNC_open");
772 document
.getElementById('noVNC_settings_button')
773 .classList
.add("noVNC_selected");
776 closeSettingsPanel: function() {
777 document
.getElementById('noVNC_settings')
778 .classList
.remove("noVNC_open");
779 document
.getElementById('noVNC_settings_button')
780 .classList
.remove("noVNC_selected");
783 // Toggle the settings menu:
784 // On open, settings are refreshed from saved cookies.
785 // On close, settings are applied
786 toggleSettingsPanel: function() {
787 if (document
.getElementById('noVNC_settings')
788 .classList
.contains("noVNC_open")) {
790 UI
.closeSettingsPanel();
792 UI
.openSettingsPanel();
802 openXvpPanel: function() {
806 document
.getElementById('noVNC_xvp')
807 .classList
.add("noVNC_open");
808 document
.getElementById('noVNC_xvp_button')
809 .classList
.add("noVNC_selected");
812 closeXvpPanel: function() {
813 document
.getElementById('noVNC_xvp')
814 .classList
.remove("noVNC_open");
815 document
.getElementById('noVNC_xvp_button')
816 .classList
.remove("noVNC_selected");
819 toggleXvpPanel: function() {
820 if (document
.getElementById('noVNC_xvp')
821 .classList
.contains("noVNC_open")) {
828 // Disable/enable XVP button
829 updateXvpButton: function(ver
) {
831 document
.getElementById('noVNC_xvp_button')
832 .classList
.remove("noVNC_hidden");
834 document
.getElementById('noVNC_xvp_button')
835 .classList
.add("noVNC_hidden");
836 // Close XVP panel if open
847 openClipboardPanel: function() {
851 document
.getElementById('noVNC_clipboard')
852 .classList
.add("noVNC_open");
853 document
.getElementById('noVNC_clipboard_button')
854 .classList
.add("noVNC_selected");
857 closeClipboardPanel: function() {
858 document
.getElementById('noVNC_clipboard')
859 .classList
.remove("noVNC_open");
860 document
.getElementById('noVNC_clipboard_button')
861 .classList
.remove("noVNC_selected");
864 toggleClipboardPanel: function() {
865 if (document
.getElementById('noVNC_clipboard')
866 .classList
.contains("noVNC_open")) {
867 UI
.closeClipboardPanel();
869 UI
.openClipboardPanel();
873 clipboardReceive: function(rfb
, text
) {
874 Util
.Debug(">> UI.clipboardReceive: " + text
.substr(0,40) + "...");
875 document
.getElementById('noVNC_clipboard_text').value
= text
;
876 Util
.Debug("<< UI.clipboardReceive");
879 clipboardClear: function() {
880 document
.getElementById('noVNC_clipboard_text').value
= "";
881 UI
.rfb
.clipboardPasteFrom("");
884 clipboardSend: function() {
885 var text
= document
.getElementById('noVNC_clipboard_text').value
;
886 Util
.Debug(">> UI.clipboardSend: " + text
.substr(0,40) + "...");
887 UI
.rfb
.clipboardPasteFrom(text
);
888 Util
.Debug("<< UI.clipboardSend");
897 openConnectPanel: function() {
901 document
.getElementById('noVNC_connect_controls')
902 .classList
.add("noVNC_open");
903 document
.getElementById('noVNC_connect_controls_button')
904 .classList
.add("noVNC_selected");
906 document
.getElementById('noVNC_setting_host').focus();
909 closeConnectPanel: function() {
910 document
.getElementById('noVNC_connect_controls')
911 .classList
.remove("noVNC_open");
912 document
.getElementById('noVNC_connect_controls_button')
913 .classList
.remove("noVNC_selected");
915 UI
.saveSetting('host');
916 UI
.saveSetting('port');
917 UI
.saveSetting('token');
918 //UI.saveSetting('password');
921 toggleConnectPanel: function() {
922 if (document
.getElementById('noVNC_connect_controls')
923 .classList
.contains("noVNC_open")) {
924 UI
.closeConnectPanel();
926 UI
.openConnectPanel();
930 connect: function() {
931 var host
= document
.getElementById('noVNC_setting_host').value
;
932 var port
= document
.getElementById('noVNC_setting_port').value
;
933 var password
= document
.getElementById('noVNC_setting_password').value
;
934 var token
= document
.getElementById('noVNC_setting_token').value
;
935 var path
= document
.getElementById('noVNC_setting_path').value
;
937 //if token is in path then ignore the new token variable
939 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
942 if ((!host
) || (!port
)) {
943 UI
.showStatus("Must set host and port", 'error');
947 if (!UI
.initRFB()) return;
951 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
952 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
953 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
954 UI
.rfb
.set_shared(UI
.getSetting('shared'));
955 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
956 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
958 UI
.rfb
.connect(host
, port
, password
, path
);
961 disconnect: function() {
965 // Restore the callback used for initial resize
966 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
968 // Don't display the connection settings until we're actually disconnected
971 disconnectFinished: function (rfb
, reason
) {
972 if (typeof reason
!== 'undefined') {
973 UI
.showStatus(reason
, 'error');
975 UI
.openConnectPanel();
984 passwordRequired: function(rfb
, msg
) {
986 document
.getElementById('noVNC_password_dlg')
987 .classList
.add('noVNC_open');
989 setTimeout(function () {
990 document
.getElementById('noVNC_password_input').focus();
993 if (typeof msg
=== 'undefined') {
994 msg
= "Password is required";
996 UI
.showStatus(msg
, "warning");
999 setPassword: function() {
1000 UI
.rfb
.sendPassword(document
.getElementById('noVNC_password_input').value
);
1001 document
.getElementById('noVNC_password_dlg')
1002 .classList
.remove('noVNC_open');
1012 toggleFullscreen: function() {
1013 if (document
.fullscreenElement
|| // alternative standard method
1014 document
.mozFullScreenElement
|| // currently working methods
1015 document
.webkitFullscreenElement
||
1016 document
.msFullscreenElement
) {
1017 if (document
.exitFullscreen
) {
1018 document
.exitFullscreen();
1019 } else if (document
.mozCancelFullScreen
) {
1020 document
.mozCancelFullScreen();
1021 } else if (document
.webkitExitFullscreen
) {
1022 document
.webkitExitFullscreen();
1023 } else if (document
.msExitFullscreen
) {
1024 document
.msExitFullscreen();
1027 if (document
.documentElement
.requestFullscreen
) {
1028 document
.documentElement
.requestFullscreen();
1029 } else if (document
.documentElement
.mozRequestFullScreen
) {
1030 document
.documentElement
.mozRequestFullScreen();
1031 } else if (document
.documentElement
.webkitRequestFullscreen
) {
1032 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
1033 } else if (document
.body
.msRequestFullscreen
) {
1034 document
.body
.msRequestFullscreen();
1037 UI
.enableDisableViewClip();
1038 UI
.updateFullscreenButton();
1041 updateFullscreenButton: function() {
1042 if (document
.fullscreenElement
|| // alternative standard method
1043 document
.mozFullScreenElement
|| // currently working methods
1044 document
.webkitFullscreenElement
||
1045 document
.msFullscreenElement
) {
1046 document
.getElementById('noVNC_fullscreen_button')
1047 .classList
.add("noVNC_selected");
1049 document
.getElementById('noVNC_fullscreen_button')
1050 .classList
.remove("noVNC_selected");
1060 // Apply remote resizing or local scaling
1061 applyResizeMode: function() {
1062 if (!UI
.rfb
) return;
1064 var screen
= UI
.screenSize();
1066 if (screen
&& UI
.connected
&& UI
.rfb
.get_display()) {
1068 var display
= UI
.rfb
.get_display();
1069 var resizeMode
= UI
.getSetting('resize');
1071 if (resizeMode
=== 'remote') {
1073 // Request changing the resolution of the remote display to
1074 // the size of the local browser viewport.
1076 // In order to not send multiple requests before the browser-resize
1077 // is finished we wait 0.5 seconds before sending the request.
1078 clearTimeout(UI
.resizeTimeout
);
1079 UI
.resizeTimeout
= setTimeout(function(){
1081 // Limit the viewport to the size of the browser window
1082 display
.set_maxWidth(screen
.w
);
1083 display
.set_maxHeight(screen
.h
);
1085 Util
.Debug('Attempting requestDesktopSize(' +
1086 screen
.w
+ ', ' + screen
.h
+ ')');
1088 // Request a remote size covering the viewport
1089 UI
.rfb
.requestDesktopSize(screen
.w
, screen
.h
);
1092 } else if (resizeMode
=== 'scale' || resizeMode
=== 'downscale') {
1093 var downscaleOnly
= resizeMode
=== 'downscale';
1094 var scaleRatio
= display
.autoscale(screen
.w
, screen
.h
, downscaleOnly
);
1095 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
1096 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
1101 // The screen is always the same size as the available viewport
1102 // in the browser window minus the height of the control bar
1103 screenSize: function() {
1104 var screen
= document
.getElementById('noVNC_screen');
1106 // Hide the scrollbars until the size is calculated
1107 screen
.style
.overflow
= "hidden";
1109 var pos
= Util
.getPosition(screen
);
1113 screen
.style
.overflow
= "visible";
1115 if (isNaN(w
) || isNaN(h
)) {
1118 return {w
: w
, h
: h
};
1122 // Normally we only apply the current resize mode after a window resize
1123 // event. This means that when a new connection is opened, there is no
1124 // resize mode active.
1125 // We have to wait until the first FBU because this is where the client
1126 // will find the supported encodings of the server. Some calls later in
1127 // the chain is dependant on knowing the server-capabilities.
1128 initialResize: function(rfb
, fbu
) {
1129 UI
.applyResizeMode();
1130 // After doing this once, we remove the callback.
1131 UI
.rfb
.set_onFBUComplete(function() { });
1140 // Set and configure viewport clipping
1141 setViewClip: function(clip
) {
1142 UI
.updateSetting('clip', clip
);
1143 UI
.updateViewClip();
1146 // Update parameters that depend on the clip setting
1147 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');
1192 // Safari auto-hides the scrollbars which makes them
1193 // impossible to use in most cases
1194 UI
.setViewClip(true);
1195 document
.getElementById('noVNC_setting_clip').disabled
= true;
1196 } else if (resizeSetting
.value
=== 'downscale' || resizeSetting
.value
=== 'scale') {
1197 // Disable clipping if we are scaling
1198 UI
.setViewClip(false);
1199 document
.getElementById('noVNC_setting_clip').disabled
= true;
1200 } else if (document
.msFullscreenElement
) {
1201 // The browser is IE and we are in fullscreen mode.
1202 // - We need to force clipping while in fullscreen since
1203 // scrollbars doesn't work.
1204 UI
.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
1205 UI
.rememberedClipSetting
= UI
.getSetting('clip');
1206 UI
.setViewClip(true);
1207 document
.getElementById('noVNC_setting_clip').disabled
= true;
1208 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
1209 // Restore view clip to what it was before fullscreen on IE
1210 UI
.setViewClip(UI
.rememberedClipSetting
);
1211 document
.getElementById('noVNC_setting_clip').disabled
=
1212 UI
.connected
|| Util
.isTouchDevice
;
1214 document
.getElementById('noVNC_setting_clip').disabled
=
1215 UI
.connected
|| Util
.isTouchDevice
;
1216 if (Util
.isTouchDevice
) {
1217 UI
.setViewClip(true);
1228 toggleViewDrag: function() {
1229 if (!UI
.rfb
) return;
1231 var drag
= UI
.rfb
.get_viewportDrag();
1232 UI
.setViewDrag(!drag
);
1235 // Set the view drag mode which moves the viewport on mouse drags
1236 setViewDrag: function(drag
) {
1237 if (!UI
.rfb
) return;
1239 UI
.rfb
.set_viewportDrag(drag
);
1241 UI
.updateViewDrag();
1244 updateViewDrag: function() {
1245 var clipping
= false;
1247 if (!UI
.connected
) return;
1249 // Check if viewport drag is possible. It is only possible
1250 // if the remote display is clipping the client display.
1251 if (UI
.rfb
.get_display().get_viewport() &&
1252 UI
.rfb
.get_display().clippingDisplay()) {
1256 var viewDragButton
= document
.getElementById('noVNC_view_drag_button');
1259 UI
.rfb
.get_viewportDrag()) {
1260 // The size of the remote display is the same or smaller
1261 // than the client display. Make sure viewport drag isn't
1262 // active when it can't be used.
1263 UI
.rfb
.set_viewportDrag(false);
1266 if (UI
.rfb
.get_viewportDrag()) {
1267 viewDragButton
.classList
.add("noVNC_selected");
1269 viewDragButton
.classList
.remove("noVNC_selected");
1272 // Different behaviour for touch vs non-touch
1273 // The button is disabled instead of hidden on touch devices
1274 if (Util
.isTouchDevice
) {
1275 viewDragButton
.classList
.remove("noVNC_hidden");
1278 viewDragButton
.disabled
= false;
1280 viewDragButton
.disabled
= true;
1283 viewDragButton
.disabled
= false;
1286 viewDragButton
.classList
.remove("noVNC_hidden");
1288 viewDragButton
.classList
.add("noVNC_hidden");
1299 showVirtualKeyboard: function() {
1300 if (!Util
.isTouchDevice
) return;
1302 var input
= document
.getElementById('noVNC_keyboardinput');
1304 if (document
.activeElement
== input
) return;
1306 UI
.keyboardVisible
= true;
1307 document
.getElementById('noVNC_keyboard_button')
1308 .classList
.add("noVNC_selected");
1312 var l
= input
.value
.length
;
1313 // Move the caret to the end
1314 input
.setSelectionRange(l
, l
);
1315 } catch (err
) {} // setSelectionRange is undefined in Google Chrome
1318 hideVirtualKeyboard: function() {
1319 if (!Util
.isTouchDevice
) return;
1321 var input
= document
.getElementById('noVNC_keyboardinput');
1323 if (document
.activeElement
!= input
) return;
1328 toggleVirtualKeyboard: function () {
1329 if (UI
.keyboardVisible
) {
1330 UI
.hideVirtualKeyboard();
1332 UI
.showVirtualKeyboard();
1336 onblurVirtualKeyboard: function() {
1337 //Weird bug in iOS if you change keyboardVisible
1338 //here it does not actually occur so next time
1339 //you click keyboard icon it doesnt work.
1340 UI
.hideKeyboardTimeout
= setTimeout(function() {
1341 UI
.keyboardVisible
= false;
1342 document
.getElementById('noVNC_keyboard_button')
1343 .classList
.remove("noVNC_selected");
1347 keepKeyboard: function() {
1348 clearTimeout(UI
.hideKeyboardTimeout
);
1349 if(UI
.keyboardVisible
=== true) {
1350 UI
.showVirtualKeyboard();
1351 } else if(UI
.keyboardVisible
=== false) {
1352 UI
.hideVirtualKeyboard();
1356 keyboardinputReset: function() {
1357 var kbi
= document
.getElementById('noVNC_keyboardinput');
1358 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1359 UI
.lastKeyboardinput
= kbi
.value
;
1362 // When normal keyboard events are left uncought, use the input events from
1363 // the keyboardinput element instead and generate the corresponding key events.
1364 // This code is required since some browsers on Android are inconsistent in
1365 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1366 keyInput: function(event
) {
1368 if (!UI
.rfb
) return;
1370 var newValue
= event
.target
.value
;
1372 if (!UI
.lastKeyboardinput
) {
1373 UI
.keyboardinputReset();
1375 var oldValue
= UI
.lastKeyboardinput
;
1379 // Try to check caret position since whitespace at the end
1380 // will not be considered by value.length in some browsers
1381 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1383 // selectionStart is undefined in Google Chrome
1384 newLen
= newValue
.length
;
1386 var oldLen
= oldValue
.length
;
1389 var inputs
= newLen
- oldLen
;
1391 backspaces
= -inputs
;
1396 // Compare the old string with the new to account for
1397 // text-corrections or other input that modify existing text
1399 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1400 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1401 inputs
= newLen
- i
;
1402 backspaces
= oldLen
- i
;
1407 // Send the key events
1408 for (i
= 0; i
< backspaces
; i
++) {
1409 UI
.rfb
.sendKey(KeyTable
.XK_BackSpace
);
1411 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1412 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1415 // Control the text content length in the keyboardinput element
1416 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1417 UI
.keyboardinputReset();
1418 } else if (newLen
< 1) {
1419 // There always have to be some text in the keyboardinput
1420 // element with which backspace can interact.
1421 UI
.keyboardinputReset();
1422 // This sometimes causes the keyboard to disappear for a second
1423 // but it is required for the android keyboard to recognize that
1424 // text has been added to the field
1425 event
.target
.blur();
1426 // This has to be ran outside of the input handler in order to work
1427 setTimeout(UI
.keepKeyboard
, 0);
1429 UI
.lastKeyboardinput
= newValue
;
1439 openExtraKeys: function() {
1440 UI
.closeAllPanels();
1441 UI
.openControlbar();
1443 document
.getElementById('noVNC_modifiers')
1444 .classList
.add("noVNC_open");
1445 document
.getElementById('noVNC_toggle_extra_keys_button')
1446 .classList
.add("noVNC_selected");
1449 closeExtraKeys: function() {
1450 document
.getElementById('noVNC_modifiers')
1451 .classList
.remove("noVNC_open");
1452 document
.getElementById('noVNC_toggle_extra_keys_button')
1453 .classList
.remove("noVNC_selected");
1456 toggleExtraKeys: function() {
1458 if(document
.getElementById('noVNC_modifiers')
1459 .classList
.contains("noVNC_open")) {
1460 UI
.closeExtraKeys();
1466 sendEsc: function() {
1468 UI
.rfb
.sendKey(KeyTable
.XK_Escape
);
1471 sendTab: function() {
1473 UI
.rfb
.sendKey(KeyTable
.XK_Tab
);
1476 toggleCtrl: function() {
1478 var btn
= document
.getElementById('noVNC_toggle_ctrl_button');
1479 if (btn
.classList
.contains("noVNC_selected")) {
1480 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, false);
1481 btn
.classList
.remove("noVNC_selected");
1483 UI
.rfb
.sendKey(KeyTable
.XK_Control_L
, true);
1484 btn
.classList
.add("noVNC_selected");
1488 toggleAlt: function() {
1490 var btn
= document
.getElementById('noVNC_toggle_alt_button');
1491 if (btn
.classList
.contains("noVNC_selected")) {
1492 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, false);
1493 btn
.classList
.remove("noVNC_selected");
1495 UI
.rfb
.sendKey(KeyTable
.XK_Alt_L
, true);
1496 btn
.classList
.add("noVNC_selected");
1500 sendCtrlAltDel: function() {
1502 UI
.rfb
.sendCtrlAltDel();
1511 setMouseButton: function(num
) {
1513 UI
.rfb
.get_mouse().set_touchButton(num
);
1516 var blist
= [0, 1,2,4];
1517 for (var b
= 0; b
< blist
.length
; b
++) {
1518 var button
= document
.getElementById('noVNC_mouse_button' + blist
[b
]);
1519 if (blist
[b
] === num
) {
1520 button
.classList
.remove("noVNC_hidden");
1522 button
.classList
.add("noVNC_hidden");
1527 displayBlur: function() {
1528 if (!UI
.rfb
) return;
1530 UI
.rfb
.get_keyboard().set_focused(false);
1531 UI
.rfb
.get_mouse().set_focused(false);
1534 displayFocus: function() {
1535 if (!UI
.rfb
) return;
1537 UI
.rfb
.get_keyboard().set_focused(true);
1538 UI
.rfb
.get_mouse().set_focused(true);
1541 updateDesktopName: function(rfb
, name
) {
1542 UI
.desktopName
= name
;
1543 // Display the desktop name in the document title
1544 document
.title
= name
+ " - noVNC";
1547 bell: function(rfb
) {
1548 if (WebUtil
.getConfigVar('bell', 'on') === 'on') {
1549 document
.getElementById('noVNC_bell').play();
1553 //Helper to add options to dropdown.
1554 addOption: function(selectbox
, text
, value
) {
1555 var optn
= document
.createElement("OPTION");
1558 selectbox
.options
.add(optn
);
1567 /* [module] UI.load(); */
1570 /* [module] export default UI; */