]>
git.proxmox.com Git - mirror_novnc.git/blob - include/ui.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2016 Samuel Mannehed for Cendio AB
5 * Licensed under MPL 2.0 (see LICENSE.txt)
7 * See README.md for usage and integration instructions.
10 /* jslint white: false, browser: true */
11 /* global window, $D, Util, WebUtil, RFB, Display */
18 // Load supporting scripts
19 window
.onscriptsload = function () { UI
.load(); };
20 Util
.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
21 "keysymdef.js", "keyboard.js", "input.js", "display.js",
22 "rfb.js", "keysym.js", "inflator.js"]);
29 popupStatusTimeout
: null,
30 hideKeyboardTimeout
: null,
33 connSettingsOpen
: false,
35 keyboardVisible
: false,
39 rememberedClipSetting
: null,
40 lastKeyboardinput
: null,
41 defaultKeyboardinputLen
: 100,
48 // Setup rfb object, load settings from browser storage, then call
49 // UI.init to setup the UI/menus
50 load: function(callback
) {
51 WebUtil
.initSettings(UI
.start
, callback
);
54 // Render default UI and initialize settings menu
55 start: function(callback
) {
56 UI
.isTouchDevice
= 'ontouchstart' in document
.documentElement
;
58 // Stylesheet selection dropdown
59 var sheet
= WebUtil
.selectStylesheet();
60 var sheets
= WebUtil
.getStylesheets();
62 for (i
= 0; i
< sheets
.length
; i
+= 1) {
63 UI
.addOption($D('noVNC_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
66 // Logging selection dropdown
67 var llevels
= ['error', 'warn', 'info', 'debug'];
68 for (i
= 0; i
< llevels
.length
; i
+= 1) {
69 UI
.addOption($D('noVNC_logging'),llevels
[i
], llevels
[i
]);
72 // Settings with immediate effects
73 UI
.initSetting('logging', 'warn');
74 WebUtil
.init_logging(UI
.getSetting('logging'));
76 UI
.initSetting('stylesheet', 'default');
77 WebUtil
.selectStylesheet(null);
78 // call twice to get around webkit bug
79 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
81 // if port == 80 (or 443) then it won't be present and should be
83 var port
= window
.location
.port
;
85 if (window
.location
.protocol
.substring(0,5) == 'https') {
88 else if (window
.location
.protocol
.substring(0,4) == 'http') {
93 /* Populate the controls if defaults are provided in the URL */
94 UI
.initSetting('host', window
.location
.hostname
);
95 UI
.initSetting('port', port
);
96 UI
.initSetting('password', '');
97 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
98 UI
.initSetting('true_color', true);
99 UI
.initSetting('cursor', !UI
.isTouchDevice
);
100 UI
.initSetting('resize', 'off');
101 UI
.initSetting('shared', true);
102 UI
.initSetting('view_only', false);
103 UI
.initSetting('path', 'websockify');
104 UI
.initSetting('repeaterID', '');
105 UI
.initSetting('token', '');
107 var autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
108 if (autoconnect
=== 'true' || autoconnect
== '1') {
115 UI
.updateVisualState();
117 $D('noVNC_host').focus();
119 // Show mouse selector buttons on touch screen devices
120 if (UI
.isTouchDevice
) {
121 // Show mobile buttons
122 $D('noVNC_mobile_buttons').style
.display
= "inline";
124 // Remove the address bar
125 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
126 UI
.forceSetting('clip', true);
128 UI
.initSetting('clip', false);
134 Util
.addEvent(window
, 'resize', function () {
135 UI
.applyResizeMode();
141 UI
.isSafari
= (navigator
.userAgent
.indexOf('Safari') != -1 &&
142 navigator
.userAgent
.indexOf('Chrome') == -1);
144 // Only show the button if fullscreen is properly supported
145 // * Safari doesn't support alphanumerical input while in fullscreen
147 (document
.documentElement
.requestFullscreen
||
148 document
.documentElement
.mozRequestFullScreen
||
149 document
.documentElement
.webkitRequestFullscreen
||
150 document
.body
.msRequestFullscreen
)) {
151 $D('fullscreenButton').style
.display
= "inline";
152 Util
.addEvent(window
, 'fullscreenchange', UI
.updateFullscreenButton
);
153 Util
.addEvent(window
, 'mozfullscreenchange', UI
.updateFullscreenButton
);
154 Util
.addEvent(window
, 'webkitfullscreenchange', UI
.updateFullscreenButton
);
155 Util
.addEvent(window
, 'msfullscreenchange', UI
.updateFullscreenButton
);
158 Util
.addEvent(window
, 'load', UI
.keyboardinputReset
);
160 Util
.addEvent(window
, 'beforeunload', function () {
161 if (UI
.rfb
&& UI
.rfb_state
=== 'normal') {
162 return "You are currently connected.";
166 // Show description by default when hosted at for kanaka.github.com
167 if (location
.host
=== "kanaka.github.io") {
168 // Open the description dialog
169 $D('noVNC_description').style
.display
= "block";
171 // Show the connect panel on first load unless autoconnecting
172 if (autoconnect
=== UI
.connSettingsOpen
) {
173 UI
.toggleConnectPanel();
177 // Add mouse event click/focus/blur event handlers to the UI
178 UI
.addMouseHandlers();
180 if (typeof callback
=== "function") {
185 initRFB: function() {
187 UI
.rfb
= new RFB({'target': $D('noVNC_canvas'),
188 'onUpdateState': UI
.updateState
,
189 'onXvpInit': UI
.updateXvpButton
,
190 'onClipboard': UI
.clipReceive
,
191 'onFBUComplete': UI
.initialResize
,
192 'onFBResize': UI
.updateViewDrag
,
193 'onDesktopName': UI
.updateDocumentTitle
});
196 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
201 addMouseHandlers: function() {
202 // Setup interface handlers that can't be inline
203 $D("noVNC_view_drag_button").onclick
= UI
.toggleViewDrag
;
204 $D("noVNC_mouse_button0").onclick = function () { UI
.setMouseButton(1); };
205 $D("noVNC_mouse_button1").onclick = function () { UI
.setMouseButton(2); };
206 $D("noVNC_mouse_button2").onclick = function () { UI
.setMouseButton(4); };
207 $D("noVNC_mouse_button4").onclick = function () { UI
.setMouseButton(0); };
208 $D("showKeyboard").onclick
= UI
.showKeyboard
;
210 $D("keyboardinput").oninput
= UI
.keyInput
;
211 $D("keyboardinput").onblur
= UI
.hideKeyboard
;
212 $D("keyboardinput").onsubmit = function () { return false; };
214 $D("toggleExtraKeysButton").onclick
= UI
.toggleExtraKeys
;
215 $D("toggleCtrlButton").onclick
= UI
.toggleCtrl
;
216 $D("toggleAltButton").onclick
= UI
.toggleAlt
;
217 $D("sendTabButton").onclick
= UI
.sendTab
;
218 $D("sendEscButton").onclick
= UI
.sendEsc
;
220 $D("sendCtrlAltDelButton").onclick
= UI
.sendCtrlAltDel
;
221 $D("xvpShutdownButton").onclick
= UI
.xvpShutdown
;
222 $D("xvpRebootButton").onclick
= UI
.xvpReboot
;
223 $D("xvpResetButton").onclick
= UI
.xvpReset
;
224 $D("noVNC_status").onclick
= UI
.popupStatus
;
225 $D("noVNC_popup_status").onclick
= UI
.closePopup
;
226 $D("xvpButton").onclick
= UI
.toggleXvpPanel
;
227 $D("clipboardButton").onclick
= UI
.toggleClipboardPanel
;
228 $D("fullscreenButton").onclick
= UI
.toggleFullscreen
;
229 $D("settingsButton").onclick
= UI
.toggleSettingsPanel
;
230 $D("connectButton").onclick
= UI
.toggleConnectPanel
;
231 $D("disconnectButton").onclick
= UI
.disconnect
;
232 $D("descriptionButton").onclick
= UI
.toggleConnectPanel
;
234 $D("noVNC_clipboard_text").onfocus
= UI
.displayBlur
;
235 $D("noVNC_clipboard_text").onblur
= UI
.displayFocus
;
236 $D("noVNC_clipboard_text").onchange
= UI
.clipSend
;
237 $D("noVNC_clipboard_clear_button").onclick
= UI
.clipClear
;
239 $D("noVNC_settings_menu").onmouseover
= UI
.displayBlur
;
240 $D("noVNC_settings_menu").onmouseover
= UI
.displayFocus
;
241 $D("noVNC_apply").onclick
= UI
.settingsApply
;
243 $D("noVNC_connect_button").onclick
= UI
.connect
;
245 $D("noVNC_resize").onchange
= UI
.enableDisableViewClip
;
248 updateState: function(rfb
, state
, oldstate
, msg
) {
249 UI
.rfb_state
= state
;
254 klass
= "noVNC_status_error";
257 klass
= "noVNC_status_normal";
260 $D('noVNC_logo').style
.display
= "block";
261 $D('noVNC_screen').style
.display
= "none";
264 klass
= "noVNC_status_normal";
267 UI
.toggleConnectPanel();
269 $D('noVNC_connect_button').value
= "Send Password";
270 $D('noVNC_connect_button').onclick
= UI
.setPassword
;
271 $D('noVNC_password').focus();
273 klass
= "noVNC_status_warn";
276 klass
= "noVNC_status_warn";
280 if (typeof(msg
) !== 'undefined') {
281 $D('noVNC-control-bar').setAttribute("class", klass
);
282 $D('noVNC_status').innerHTML
= msg
;
285 UI
.updateVisualState();
288 // Disable/enable controls depending on connection state
289 updateVisualState: function() {
290 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
292 //Util.Debug(">> updateVisualState");
293 $D('noVNC_encrypt').disabled
= connected
;
294 $D('noVNC_true_color').disabled
= connected
;
295 if (Util
.browserSupportsCursorURIs()) {
296 $D('noVNC_cursor').disabled
= connected
;
298 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
299 $D('noVNC_cursor').disabled
= true;
302 UI
.enableDisableViewClip();
303 $D('noVNC_resize').disabled
= connected
;
304 $D('noVNC_shared').disabled
= connected
;
305 $D('noVNC_view_only').disabled
= connected
;
306 $D('noVNC_path').disabled
= connected
;
307 $D('noVNC_repeaterID').disabled
= connected
;
311 UI
.setMouseButton(1);
312 $D('clipboardButton').style
.display
= "inline";
313 $D('showKeyboard').style
.display
= "inline";
314 $D('noVNC_extra_keys').style
.display
= "";
315 $D('sendCtrlAltDelButton').style
.display
= "inline";
318 $D('clipboardButton').style
.display
= "none";
319 $D('showKeyboard').style
.display
= "none";
320 $D('noVNC_extra_keys').style
.display
= "none";
321 $D('sendCtrlAltDelButton').style
.display
= "none";
322 UI
.updateXvpButton(0);
325 // State change disables viewport dragging.
326 // It is enabled (toggled) by direct click on the button
327 UI
.updateViewDrag(false);
329 switch (UI
.rfb_state
) {
333 $D('connectButton').style
.display
= "";
334 $D('disconnectButton').style
.display
= "none";
335 UI
.connSettingsOpen
= false;
336 UI
.toggleConnectPanel();
339 $D('connectButton').style
.display
= "";
340 $D('disconnectButton').style
.display
= "none";
343 $D('connectButton').style
.display
= "none";
344 $D('disconnectButton').style
.display
= "";
348 //Util.Debug("<< updateVisualState");
351 popupStatus: function(text
) {
352 var psp
= $D('noVNC_popup_status');
354 clearTimeout(UI
.popupStatusTimeout
);
356 if (typeof text
=== 'string') {
357 psp
.innerHTML
= text
;
359 psp
.innerHTML
= $D('noVNC_status').innerHTML
;
361 psp
.style
.display
= "block";
362 psp
.style
.left
= window
.innerWidth
/2 -
363 parseInt(window
.getComputedStyle(psp
).width
)/2 -30 + "px";
365 // Show the popup for a maximum of 1.5 seconds
366 UI
.popupStatusTimeout
= setTimeout(function() {
371 closePopup: function() {
372 clearTimeout(UI
.popupStatusTimeout
);
373 $D('noVNC_popup_status').style
.display
= "none";
376 // Initial page load read/initialization of settings
377 initSetting: function(name
, defVal
) {
378 // Check Query string followed by cookie
379 var val
= WebUtil
.getConfigVar(name
);
381 val
= WebUtil
.readSetting(name
, defVal
);
383 UI
.updateSetting(name
, val
);
387 // Update cookie and form control setting. If value is not set, then
388 // updates from control to current cookie setting.
389 updateSetting: function(name
, value
) {
391 // Save the cookie for this session
392 if (typeof value
!== 'undefined') {
393 WebUtil
.writeSetting(name
, value
);
396 // Update the settings control
397 value
= UI
.getSetting(name
);
399 var ctrl
= $D('noVNC_' + name
);
400 if (ctrl
.type
=== 'checkbox') {
401 ctrl
.checked
= value
;
403 } else if (typeof ctrl
.options
!== 'undefined') {
404 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
405 if (ctrl
.options
[i
].value
=== value
) {
406 ctrl
.selectedIndex
= i
;
411 /*Weird IE9 error leads to 'null' appearring
412 in textboxes instead of ''.*/
413 if (value
=== null) {
420 // Save control setting to cookie
421 saveSetting: function(name
) {
422 var val
, ctrl
= $D('noVNC_' + name
);
423 if (ctrl
.type
=== 'checkbox') {
425 } else if (typeof ctrl
.options
!== 'undefined') {
426 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
430 WebUtil
.writeSetting(name
, val
);
431 //Util.Debug("Setting saved '" + name + "=" + val + "'");
435 // Force a setting to be a certain value
436 forceSetting: function(name
, val
) {
437 UI
.updateSetting(name
, val
);
441 // Read form control compatible setting from cookie
442 getSetting: function(name
) {
443 var ctrl
= $D('noVNC_' + name
);
444 var val
= WebUtil
.readSetting(name
);
445 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
446 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
456 openSettingsMenu: function() {
457 // Close the description panel
458 $D('noVNC_description').style
.display
= "none";
459 // Close clipboard panel if open
460 if (UI
.clipboardOpen
=== true) {
461 UI
.toggleClipboardPanel();
463 // Close connection settings if open
464 if (UI
.connSettingsOpen
=== true) {
465 UI
.toggleConnectPanel();
467 // Close XVP panel if open
468 if (UI
.xvpOpen
=== true) {
471 $D('noVNC_settings').style
.display
= "block";
472 $D('settingsButton').className
= "noVNC_status_button_selected";
473 UI
.settingsOpen
= true;
476 // Close menu (without applying settings)
477 closeSettingsMenu: function() {
478 $D('noVNC_settings').style
.display
= "none";
479 $D('settingsButton').className
= "noVNC_status_button";
480 UI
.settingsOpen
= false;
483 // Toggle the settings menu:
484 // On open, settings are refreshed from saved cookies.
485 // On close, settings are applied
486 toggleSettingsPanel: function() {
487 // Close the description panel
488 $D('noVNC_description').style
.display
= "none";
489 if (UI
.settingsOpen
) {
491 UI
.closeSettingsMenu();
493 UI
.updateSetting('encrypt');
494 UI
.updateSetting('true_color');
495 if (Util
.browserSupportsCursorURIs()) {
496 UI
.updateSetting('cursor');
498 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
499 $D('noVNC_cursor').disabled
= true;
501 UI
.updateSetting('clip');
502 UI
.updateSetting('resize');
503 UI
.updateSetting('shared');
504 UI
.updateSetting('view_only');
505 UI
.updateSetting('path');
506 UI
.updateSetting('repeaterID');
507 UI
.updateSetting('stylesheet');
508 UI
.updateSetting('logging');
510 UI
.openSettingsMenu();
514 // Show the XVP panel
515 toggleXvpPanel: function() {
516 // Close the description panel
517 $D('noVNC_description').style
.display
= "none";
518 // Close settings if open
519 if (UI
.settingsOpen
=== true) {
521 UI
.closeSettingsMenu();
523 // Close connection settings if open
524 if (UI
.connSettingsOpen
=== true) {
525 UI
.toggleConnectPanel();
527 // Close clipboard panel if open
528 if (UI
.clipboardOpen
=== true) {
529 UI
.toggleClipboardPanel();
532 if (UI
.xvpOpen
=== true) {
533 $D('noVNC_xvp').style
.display
= "none";
534 $D('xvpButton').className
= "noVNC_status_button";
537 $D('noVNC_xvp').style
.display
= "block";
538 $D('xvpButton').className
= "noVNC_status_button_selected";
543 // Disable/enable XVP button
544 updateXvpButton: function(ver
) {
546 $D('xvpButton').style
.display
= 'inline';
548 $D('xvpButton').style
.display
= 'none';
549 // Close XVP panel if open
550 if (UI
.xvpOpen
=== true) {
556 // Show the clipboard panel
557 toggleClipboardPanel: function() {
558 // Close the description panel
559 $D('noVNC_description').style
.display
= "none";
560 // Close settings if open
561 if (UI
.settingsOpen
=== true) {
563 UI
.closeSettingsMenu();
565 // Close connection settings if open
566 if (UI
.connSettingsOpen
=== true) {
567 UI
.toggleConnectPanel();
569 // Close XVP panel if open
570 if (UI
.xvpOpen
=== true) {
573 // Toggle Clipboard Panel
574 if (UI
.clipboardOpen
=== true) {
575 $D('noVNC_clipboard').style
.display
= "none";
576 $D('clipboardButton').className
= "noVNC_status_button";
577 UI
.clipboardOpen
= false;
579 $D('noVNC_clipboard').style
.display
= "block";
580 $D('clipboardButton').className
= "noVNC_status_button_selected";
581 UI
.clipboardOpen
= true;
585 // Show the connection settings panel/menu
586 toggleConnectPanel: function() {
587 // Close the description panel
588 $D('noVNC_description').style
.display
= "none";
589 // Close connection settings if open
590 if (UI
.settingsOpen
=== true) {
592 UI
.closeSettingsMenu();
593 $D('connectButton').className
= "noVNC_status_button";
595 // Close clipboard panel if open
596 if (UI
.clipboardOpen
=== true) {
597 UI
.toggleClipboardPanel();
599 // Close XVP panel if open
600 if (UI
.xvpOpen
=== true) {
604 // Toggle Connection Panel
605 if (UI
.connSettingsOpen
=== true) {
606 $D('noVNC_controls').style
.display
= "none";
607 $D('connectButton').className
= "noVNC_status_button";
608 UI
.connSettingsOpen
= false;
609 UI
.saveSetting('host');
610 UI
.saveSetting('port');
611 UI
.saveSetting('token');
612 //UI.saveSetting('password');
614 $D('noVNC_controls').style
.display
= "block";
615 $D('connectButton').className
= "noVNC_status_button_selected";
616 UI
.connSettingsOpen
= true;
617 $D('noVNC_host').focus();
621 connect: function() {
622 UI
.closeSettingsMenu();
623 UI
.toggleConnectPanel();
625 var host
= $D('noVNC_host').value
;
626 var port
= $D('noVNC_port').value
;
627 var password
= $D('noVNC_password').value
;
628 var token
= $D('noVNC_token').value
;
629 var path
= $D('noVNC_path').value
;
631 //if token is in path then ignore the new token variable
633 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
636 if ((!host
) || (!port
)) {
637 throw new Error("Must set host and port");
640 if (!UI
.initRFB()) return;
642 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
643 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
644 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
645 UI
.rfb
.set_shared(UI
.getSetting('shared'));
646 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
647 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
649 UI
.rfb
.connect(host
, port
, password
, path
);
652 setTimeout(UI
.setBarPosition
, 100);
653 $D('noVNC_logo').style
.display
= "none";
654 $D('noVNC_screen').style
.display
= "inline";
657 disconnect: function() {
658 UI
.closeSettingsMenu();
661 // Restore the callback used for initial resize
662 UI
.rfb
.set_onFBUComplete(UI
.initialResize
);
664 $D('noVNC_logo').style
.display
= "block";
665 $D('noVNC_screen').style
.display
= "none";
667 // Don't display the connection settings until we're actually disconnected
670 toggleFullscreen: function() {
671 if (document
.fullscreenElement
|| // alternative standard method
672 document
.mozFullScreenElement
|| // currently working methods
673 document
.webkitFullscreenElement
||
674 document
.msFullscreenElement
) {
675 if (document
.exitFullscreen
) {
676 document
.exitFullscreen();
677 } else if (document
.mozCancelFullScreen
) {
678 document
.mozCancelFullScreen();
679 } else if (document
.webkitExitFullscreen
) {
680 document
.webkitExitFullscreen();
681 } else if (document
.msExitFullscreen
) {
682 document
.msExitFullscreen();
685 if (document
.documentElement
.requestFullscreen
) {
686 document
.documentElement
.requestFullscreen();
687 } else if (document
.documentElement
.mozRequestFullScreen
) {
688 document
.documentElement
.mozRequestFullScreen();
689 } else if (document
.documentElement
.webkitRequestFullscreen
) {
690 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
691 } else if (document
.body
.msRequestFullscreen
) {
692 document
.body
.msRequestFullscreen();
695 UI
.enableDisableViewClip();
696 UI
.updateFullscreenButton();
699 updateFullscreenButton: function() {
700 if (document
.fullscreenElement
|| // alternative standard method
701 document
.mozFullScreenElement
|| // currently working methods
702 document
.webkitFullscreenElement
||
703 document
.msFullscreenElement
) {
704 $D('fullscreenButton').className
= "noVNC_status_button_selected";
706 $D('fullscreenButton').className
= "noVNC_status_button";
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");
742 setPassword: function() {
743 UI
.rfb
.sendPassword($D('noVNC_password').value
);
744 //Reset connect button.
745 $D('noVNC_connect_button').value
= "Connect";
746 $D('noVNC_connect_button').onclick
= UI
.connect
;
747 //Hide connection panel.
748 UI
.toggleConnectPanel();
752 xvpShutdown: function() {
753 UI
.rfb
.xvpShutdown();
756 xvpReboot: function() {
760 xvpReset: function() {
764 // Display the desktop name in the document title
765 updateDocumentTitle: function(rfb
, name
) {
766 document
.title
= name
+ " - noVNC";
769 clipReceive: function(rfb
, text
) {
770 Util
.Debug(">> UI.clipReceive: " + text
.substr(0,40) + "...");
771 $D('noVNC_clipboard_text').value
= text
;
772 Util
.Debug("<< UI.clipReceive");
775 displayBlur: function() {
778 UI
.rfb
.get_keyboard().set_focused(false);
779 UI
.rfb
.get_mouse().set_focused(false);
782 displayFocus: function() {
785 UI
.rfb
.get_keyboard().set_focused(true);
786 UI
.rfb
.get_mouse().set_focused(true);
789 clipClear: function() {
790 $D('noVNC_clipboard_text').value
= "";
791 UI
.rfb
.clipboardPasteFrom("");
794 clipSend: function() {
795 var text
= $D('noVNC_clipboard_text').value
;
796 Util
.Debug(">> UI.clipSend: " + text
.substr(0,40) + "...");
797 UI
.rfb
.clipboardPasteFrom(text
);
798 Util
.Debug("<< UI.clipSend");
802 // Apply remote resizing or local scaling
803 applyResizeMode: function() {
806 var screen
= UI
.screenSize();
808 if (screen
&& UI
.rfb_state
=== 'normal' && UI
.rfb
.get_display()) {
810 var display
= UI
.rfb
.get_display();
811 var resizeMode
= UI
.getSetting('resize');
813 if (resizeMode
=== 'remote') {
815 // Request changing the resolution of the remote display to
816 // the size of the local browser viewport.
818 // In order to not send multiple requests before the browser-resize
819 // is finished we wait 0.5 seconds before sending the request.
820 clearTimeout(UI
.resizeTimeout
);
821 UI
.resizeTimeout
= setTimeout(function(){
823 // Limit the viewport to the size of the browser window
824 display
.set_maxWidth(screen
.w
);
825 display
.set_maxHeight(screen
.h
);
827 Util
.Debug('Attempting setDesktopSize(' +
828 screen
.w
+ ', ' + screen
.h
+ ')');
830 // Request a remote size covering the viewport
831 UI
.rfb
.setDesktopSize(screen
.w
, screen
.h
);
834 } else if (resizeMode
=== 'scale' || resizeMode
=== 'downscale') {
835 var downscaleOnly
= resizeMode
=== 'downscale';
836 var scaleRatio
= display
.autoscale(screen
.w
, screen
.h
, downscaleOnly
);
837 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
838 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
843 // The screen is always the same size as the available viewport
844 // in the browser window minus the height of the control bar
845 screenSize: function() {
846 var screen
= $D('noVNC_screen');
848 // Hide the scrollbars until the size is calculated
849 screen
.style
.overflow
= "hidden";
851 var pos
= Util
.getPosition(screen
);
855 screen
.style
.overflow
= "visible";
857 if (isNaN(w
) || isNaN(h
)) {
864 // Normally we only apply the current resize mode after a window resize
865 // event. This means that when a new connection is opened, there is no
866 // resize mode active.
867 // We have to wait until the first FBU because this is where the client
868 // will find the supported encodings of the server. Some calls later in
869 // the chain is dependant on knowing the server-capabilities.
870 initialResize: function(rfb
, fbu
) {
871 UI
.applyResizeMode();
872 // After doing this once, we remove the callback.
873 UI
.rfb
.set_onFBUComplete(function() { });
876 // Set and configure viewport clipping
877 setViewClip: function(clip
) {
880 display
= UI
.rfb
.get_display();
882 UI
.forceSetting('clip', clip
);
886 var cur_clip
= display
.get_viewport();
888 if (typeof(clip
) !== 'boolean') {
889 // Use current setting
890 clip
= UI
.getSetting('clip');
893 if (clip
&& !cur_clip
) {
895 UI
.updateSetting('clip', true);
896 } else if (!clip
&& cur_clip
) {
898 UI
.updateSetting('clip', false);
899 display
.set_viewport(false);
900 // Disable max dimensions
901 display
.set_maxWidth(0);
902 display
.set_maxHeight(0);
903 display
.viewportChangeSize();
905 if (UI
.getSetting('clip')) {
906 // If clipping, update clipping settings
907 display
.set_viewport(true);
909 var size
= UI
.screenSize();
911 display
.set_maxWidth(size
.w
);
912 display
.set_maxHeight(size
.h
);
914 // Hide potential scrollbars that can skew the position
915 $D('noVNC_screen').style
.overflow
= "hidden";
917 // The x position marks the left margin of the canvas,
918 // remove the margin from both sides to keep it centered
919 var new_w
= size
.w
- (2 * Util
.getPosition($D('noVNC_canvas')).x
);
921 $D('noVNC_screen').style
.overflow
= "visible";
923 display
.viewportChangeSize(new_w
, size
.h
);
928 // Handle special cases where clipping is forced on/off or locked
929 enableDisableViewClip: function() {
930 var resizeElem
= $D('noVNC_resize');
931 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
934 // Safari auto-hides the scrollbars which makes them
935 // impossible to use in most cases
936 UI
.setViewClip(true);
937 $D('noVNC_clip').disabled
= true;
938 } else if (resizeElem
.value
=== 'downscale' || resizeElem
.value
=== 'scale') {
939 // Disable clipping if we are scaling
940 UI
.setViewClip(false);
941 $D('noVNC_clip').disabled
= true;
942 } else if (document
.msFullscreenElement
) {
943 // The browser is IE and we are in fullscreen mode.
944 // - We need to force clipping while in fullscreen since
945 // scrollbars doesn't work.
946 UI
.popupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
947 UI
.rememberedClipSetting
= UI
.getSetting('clip');
948 UI
.setViewClip(true);
949 $D('noVNC_clip').disabled
= true;
950 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
951 // Restore view clip to what it was before fullscreen on IE
952 UI
.setViewClip(UI
.rememberedClipSetting
);
953 $D('noVNC_clip').disabled
= connected
|| UI
.isTouchDevice
;
955 $D('noVNC_clip').disabled
= connected
|| UI
.isTouchDevice
;
956 if (UI
.isTouchDevice
) {
957 UI
.setViewClip(true);
962 // Update the viewport drag state
963 updateViewDrag: function(drag
) {
966 var viewDragButton
= $D('noVNC_view_drag_button');
968 // Check if viewport drag is possible. It is only possible
969 // if the remote display is clipping the client display.
970 if (UI
.rfb_state
=== 'normal' &&
971 UI
.rfb
.get_display().get_viewport() &&
972 UI
.rfb
.get_display().clippingDisplay()) {
974 viewDragButton
.style
.display
= "inline";
975 viewDragButton
.disabled
= false;
978 // The size of the remote display is the same or smaller
979 // than the client display. Make sure viewport drag isn't
980 // active when it can't be used.
981 if (UI
.rfb
.get_viewportDrag
) {
982 viewDragButton
.className
= "noVNC_status_button";
983 UI
.rfb
.set_viewportDrag(false);
986 // The button is disabled instead of hidden on touch devices
987 if (UI
.rfb_state
=== 'normal' && UI
.isTouchDevice
) {
988 viewDragButton
.style
.display
= "inline";
989 viewDragButton
.disabled
= true;
991 viewDragButton
.style
.display
= "none";
996 if (typeof(drag
) !== "undefined" &&
997 typeof(drag
) !== "object") {
999 viewDragButton
.className
= "noVNC_status_button_selected";
1000 UI
.rfb
.set_viewportDrag(true);
1002 viewDragButton
.className
= "noVNC_status_button";
1003 UI
.rfb
.set_viewportDrag(false);
1008 toggleViewDrag: function() {
1009 if (!UI
.rfb
) return;
1011 var viewDragButton
= $D('noVNC_view_drag_button');
1012 if (UI
.rfb
.get_viewportDrag()) {
1013 viewDragButton
.className
= "noVNC_status_button";
1014 UI
.rfb
.set_viewportDrag(false);
1016 viewDragButton
.className
= "noVNC_status_button_selected";
1017 UI
.rfb
.set_viewportDrag(true);
1021 setMouseButton: function(num
) {
1022 if (typeof num
=== 'undefined') {
1023 // Disable mouse buttons
1027 UI
.rfb
.get_mouse().set_touchButton(num
);
1030 var blist
= [0, 1,2,4];
1031 for (var b
= 0; b
< blist
.length
; b
++) {
1032 var button
= $D('noVNC_mouse_button' + blist
[b
]);
1033 if (blist
[b
] === num
) {
1034 button
.style
.display
= "";
1036 button
.style
.display
= "none";
1041 // On touch devices, show the OS keyboard
1042 showKeyboard: function() {
1043 var kbi
= $D('keyboardinput');
1044 var skb
= $D('showKeyboard');
1045 var l
= kbi
.value
.length
;
1046 if(UI
.keyboardVisible
=== false) {
1048 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
1049 catch (err
) {} // setSelectionRange is undefined in Google Chrome
1050 UI
.keyboardVisible
= true;
1051 skb
.className
= "noVNC_status_button_selected";
1052 } else if(UI
.keyboardVisible
=== true) {
1054 skb
.className
= "noVNC_status_button";
1055 UI
.keyboardVisible
= false;
1059 hideKeyboard: function() {
1060 $D('showKeyboard').className
= "noVNC_status_button";
1061 //Weird bug in iOS if you change keyboardVisible
1062 //here it does not actually occur so next time
1063 //you click keyboard icon it doesnt work.
1064 UI
.hideKeyboardTimeout
= setTimeout(function() {
1065 UI
.keyboardVisible
= false;
1069 keepKeyboard: function() {
1070 clearTimeout(UI
.hideKeyboardTimeout
);
1071 if(UI
.keyboardVisible
=== true) {
1072 $D('keyboardinput').focus();
1073 $D('showKeyboard').className
= "noVNC_status_button_selected";
1074 } else if(UI
.keyboardVisible
=== false) {
1075 $D('keyboardinput').blur();
1076 $D('showKeyboard').className
= "noVNC_status_button";
1080 keyboardinputReset: function() {
1081 var kbi
= $D('keyboardinput');
1082 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1083 UI
.lastKeyboardinput
= kbi
.value
;
1086 // When normal keyboard events are left uncought, use the input events from
1087 // the keyboardinput element instead and generate the corresponding key events.
1088 // This code is required since some browsers on Android are inconsistent in
1089 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1090 keyInput: function(event
) {
1092 if (!UI
.rfb
) return;
1094 var newValue
= event
.target
.value
;
1096 if (!UI
.lastKeyboardinput
) {
1097 UI
.keyboardinputReset();
1099 var oldValue
= UI
.lastKeyboardinput
;
1103 // Try to check caret position since whitespace at the end
1104 // will not be considered by value.length in some browsers
1105 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1107 // selectionStart is undefined in Google Chrome
1108 newLen
= newValue
.length
;
1110 var oldLen
= oldValue
.length
;
1113 var inputs
= newLen
- oldLen
;
1115 backspaces
= -inputs
;
1120 // Compare the old string with the new to account for
1121 // text-corrections or other input that modify existing text
1123 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1124 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1125 inputs
= newLen
- i
;
1126 backspaces
= oldLen
- i
;
1131 // Send the key events
1132 for (i
= 0; i
< backspaces
; i
++) {
1133 UI
.rfb
.sendKey(XK_BackSpace
);
1135 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1136 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1139 // Control the text content length in the keyboardinput element
1140 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1141 UI
.keyboardinputReset();
1142 } else if (newLen
< 1) {
1143 // There always have to be some text in the keyboardinput
1144 // element with which backspace can interact.
1145 UI
.keyboardinputReset();
1146 // This sometimes causes the keyboard to disappear for a second
1147 // but it is required for the android keyboard to recognize that
1148 // text has been added to the field
1149 event
.target
.blur();
1150 // This has to be ran outside of the input handler in order to work
1151 setTimeout(function() { UI
.keepKeyboard(); }, 0);
1153 UI
.lastKeyboardinput
= newValue
;
1157 toggleExtraKeys: function() {
1159 if(UI
.extraKeysVisible
=== false) {
1160 $D('toggleCtrlButton').style
.display
= "inline";
1161 $D('toggleAltButton').style
.display
= "inline";
1162 $D('sendTabButton').style
.display
= "inline";
1163 $D('sendEscButton').style
.display
= "inline";
1164 $D('toggleExtraKeysButton').className
= "noVNC_status_button_selected";
1165 UI
.extraKeysVisible
= true;
1166 } else if(UI
.extraKeysVisible
=== true) {
1167 $D('toggleCtrlButton').style
.display
= "";
1168 $D('toggleAltButton').style
.display
= "";
1169 $D('sendTabButton').style
.display
= "";
1170 $D('sendEscButton').style
.display
= "";
1171 $D('toggleExtraKeysButton').className
= "noVNC_status_button";
1172 UI
.extraKeysVisible
= false;
1176 sendEsc: function() {
1178 UI
.rfb
.sendKey(XK_Escape
);
1181 sendTab: function() {
1183 UI
.rfb
.sendKey(XK_Tab
);
1186 toggleCtrl: function() {
1188 if(UI
.ctrlOn
=== false) {
1189 UI
.rfb
.sendKey(XK_Control_L
, true);
1190 $D('toggleCtrlButton').className
= "noVNC_status_button_selected";
1192 } else if(UI
.ctrlOn
=== true) {
1193 UI
.rfb
.sendKey(XK_Control_L
, false);
1194 $D('toggleCtrlButton').className
= "noVNC_status_button";
1199 toggleAlt: function() {
1201 if(UI
.altOn
=== false) {
1202 UI
.rfb
.sendKey(XK_Alt_L
, true);
1203 $D('toggleAltButton').className
= "noVNC_status_button_selected";
1205 } else if(UI
.altOn
=== true) {
1206 UI
.rfb
.sendKey(XK_Alt_L
, false);
1207 $D('toggleAltButton').className
= "noVNC_status_button";
1212 sendCtrlAltDel: function() {
1213 UI
.rfb
.sendCtrlAltDel();
1216 //Helper to add options to dropdown.
1217 addOption: function(selectbox
, text
, value
) {
1218 var optn
= document
.createElement("OPTION");
1221 selectbox
.options
.add(optn
);
1224 setBarPosition: function() {
1225 $D('noVNC-control-bar').style
.top
= (window
.pageYOffset
) + 'px';
1226 $D('noVNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
1228 var vncwidth
= $D('noVNC_container').style
.offsetWidth
;
1229 $D('noVNC-control-bar').style
.width
= vncwidth
+ 'px';