]>
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) 2015 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 connSettingsOpen
: false,
30 popupStatusTimeout
: null,
32 keyboardVisible
: false,
33 hideKeyboardTimeout
: null,
34 lastKeyboardinput
: null,
35 defaultKeyboardinputLen
: 100,
36 extraKeysVisible
: false,
40 rememberedClipSetting
: null,
42 // Setup rfb object, load settings from browser storage, then call
43 // UI.init to setup the UI/menus
44 load: function (callback
) {
45 WebUtil
.initSettings(UI
.start
, callback
);
48 // Render default UI and initialize settings menu
49 start: function(callback
) {
50 UI
.isTouchDevice
= 'ontouchstart' in document
.documentElement
;
52 // Stylesheet selection dropdown
53 var sheet
= WebUtil
.selectStylesheet();
54 var sheets
= WebUtil
.getStylesheets();
56 for (i
= 0; i
< sheets
.length
; i
+= 1) {
57 UI
.addOption($D('noVNC_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
60 // Logging selection dropdown
61 var llevels
= ['error', 'warn', 'info', 'debug'];
62 for (i
= 0; i
< llevels
.length
; i
+= 1) {
63 UI
.addOption($D('noVNC_logging'),llevels
[i
], llevels
[i
]);
66 // Settings with immediate effects
67 UI
.initSetting('logging', 'warn');
68 WebUtil
.init_logging(UI
.getSetting('logging'));
70 UI
.initSetting('stylesheet', 'default');
71 WebUtil
.selectStylesheet(null);
72 // call twice to get around webkit bug
73 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
75 // if port == 80 (or 443) then it won't be present and should be
77 var port
= window
.location
.port
;
79 if (window
.location
.protocol
.substring(0,5) == 'https') {
82 else if (window
.location
.protocol
.substring(0,4) == 'http') {
87 /* Populate the controls if defaults are provided in the URL */
88 UI
.initSetting('host', window
.location
.hostname
);
89 UI
.initSetting('port', port
);
90 UI
.initSetting('password', '');
91 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
92 UI
.initSetting('true_color', true);
93 UI
.initSetting('cursor', !UI
.isTouchDevice
);
94 UI
.initSetting('resize', 'off');
95 UI
.initSetting('shared', true);
96 UI
.initSetting('view_only', false);
97 UI
.initSetting('path', 'websockify');
98 UI
.initSetting('repeaterID', '');
99 UI
.initSetting('token', '');
101 var autoconnect
= WebUtil
.getConfigVar('autoconnect', false);
102 if (autoconnect
=== 'true' || autoconnect
== '1') {
109 UI
.updateVisualState();
111 $D('noVNC_host').focus();
113 // Show mouse selector buttons on touch screen devices
114 if (UI
.isTouchDevice
) {
115 // Show mobile buttons
116 $D('noVNC_mobile_buttons').style
.display
= "inline";
118 // Remove the address bar
119 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
120 UI
.forceSetting('clip', true);
122 UI
.initSetting('clip', false);
128 Util
.addEvent(window
, 'resize', function () {
135 var isSafari
= (navigator
.userAgent
.indexOf('Safari') != -1 &&
136 navigator
.userAgent
.indexOf('Chrome') == -1);
138 // Only show the button if fullscreen is properly supported
139 // * Safari doesn't support alphanumerical input while in fullscreen
141 (document
.documentElement
.requestFullscreen
||
142 document
.documentElement
.mozRequestFullScreen
||
143 document
.documentElement
.webkitRequestFullscreen
||
144 document
.body
.msRequestFullscreen
)) {
145 $D('fullscreenButton').style
.display
= "inline";
146 Util
.addEvent(window
, 'fullscreenchange', UI
.updateFullscreenButton
);
147 Util
.addEvent(window
, 'mozfullscreenchange', UI
.updateFullscreenButton
);
148 Util
.addEvent(window
, 'webkitfullscreenchange', UI
.updateFullscreenButton
);
149 Util
.addEvent(window
, 'msfullscreenchange', UI
.updateFullscreenButton
);
152 Util
.addEvent(window
, 'load', UI
.keyboardinputReset
);
154 Util
.addEvent(window
, 'beforeunload', function () {
155 if (UI
.rfb
&& UI
.rfb_state
=== 'normal') {
156 return "You are currently connected.";
160 // Show description by default when hosted at for kanaka.github.com
161 if (location
.host
=== "kanaka.github.io") {
162 // Open the description dialog
163 $D('noVNC_description').style
.display
= "block";
165 // Show the connect panel on first load unless autoconnecting
166 if (autoconnect
=== UI
.connSettingsOpen
) {
167 UI
.toggleConnectPanel();
171 // Add mouse event click/focus/blur event handlers to the UI
172 UI
.addMouseHandlers();
174 if (typeof callback
=== "function") {
179 initRFB: function () {
181 UI
.rfb
= new RFB({'target': $D('noVNC_canvas'),
182 'onUpdateState': UI
.updateState
,
183 'onXvpInit': UI
.updateXvpVisualState
,
184 'onClipboard': UI
.clipReceive
,
185 'onFBUComplete': UI
.FBUComplete
,
186 'onFBResize': UI
.updateViewDrag
,
187 'onDesktopName': UI
.updateDocumentTitle
});
190 UI
.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc
);
195 addMouseHandlers: function() {
196 // Setup interface handlers that can't be inline
197 $D("noVNC_view_drag_button").onclick
= UI
.toggleViewDrag
;
198 $D("noVNC_mouse_button0").onclick = function () { UI
.setMouseButton(1); };
199 $D("noVNC_mouse_button1").onclick = function () { UI
.setMouseButton(2); };
200 $D("noVNC_mouse_button2").onclick = function () { UI
.setMouseButton(4); };
201 $D("noVNC_mouse_button4").onclick = function () { UI
.setMouseButton(0); };
202 $D("showKeyboard").onclick
= UI
.showKeyboard
;
204 $D("keyboardinput").oninput
= UI
.keyInput
;
205 $D("keyboardinput").onblur
= UI
.keyInputBlur
;
206 $D("keyboardinput").onsubmit = function () { return false; };
208 $D("showExtraKeysButton").onclick
= UI
.showExtraKeys
;
209 $D("toggleCtrlButton").onclick
= UI
.toggleCtrl
;
210 $D("toggleAltButton").onclick
= UI
.toggleAlt
;
211 $D("sendTabButton").onclick
= UI
.sendTab
;
212 $D("sendEscButton").onclick
= UI
.sendEsc
;
214 $D("sendCtrlAltDelButton").onclick
= UI
.sendCtrlAltDel
;
215 $D("xvpShutdownButton").onclick
= UI
.xvpShutdown
;
216 $D("xvpRebootButton").onclick
= UI
.xvpReboot
;
217 $D("xvpResetButton").onclick
= UI
.xvpReset
;
218 $D("noVNC_status").onclick
= UI
.togglePopupStatus
;
219 $D("noVNC_popup_status").onclick
= UI
.togglePopupStatus
;
220 $D("xvpButton").onclick
= UI
.toggleXvpPanel
;
221 $D("clipboardButton").onclick
= UI
.toggleClipboardPanel
;
222 $D("fullscreenButton").onclick
= UI
.toggleFullscreen
;
223 $D("settingsButton").onclick
= UI
.toggleSettingsPanel
;
224 $D("connectButton").onclick
= UI
.toggleConnectPanel
;
225 $D("disconnectButton").onclick
= UI
.disconnect
;
226 $D("descriptionButton").onclick
= UI
.toggleConnectPanel
;
228 $D("noVNC_clipboard_text").onfocus
= UI
.displayBlur
;
229 $D("noVNC_clipboard_text").onblur
= UI
.displayFocus
;
230 $D("noVNC_clipboard_text").onchange
= UI
.clipSend
;
231 $D("noVNC_clipboard_clear_button").onclick
= UI
.clipClear
;
233 $D("noVNC_settings_menu").onmouseover
= UI
.displayBlur
;
234 $D("noVNC_settings_menu").onmouseover
= UI
.displayFocus
;
235 $D("noVNC_apply").onclick
= UI
.settingsApply
;
237 $D("noVNC_connect_button").onclick
= UI
.connect
;
239 $D("noVNC_resize").onchange
= UI
.enableDisableViewClip
;
242 onresize: function (callback
) {
245 var size
= UI
.getCanvasLimit();
247 if (size
&& UI
.rfb_state
=== 'normal' && UI
.rfb
.get_display()) {
248 var display
= UI
.rfb
.get_display();
249 var scaleType
= UI
.getSetting('resize');
250 if (scaleType
=== 'remote') {
251 // use remote resizing
253 // When the local window has been resized, wait until the size remains
254 // the same for 0.5 seconds before sending the request for changing
255 // the resolution of the session
256 clearTimeout(UI
.resizeTimeout
);
257 UI
.resizeTimeout
= setTimeout(function(){
258 display
.set_maxWidth(size
.w
);
259 display
.set_maxHeight(size
.h
);
260 Util
.Debug('Attempting setDesktopSize(' +
261 size
.w
+ ', ' + size
.h
+ ')');
262 UI
.rfb
.setDesktopSize(size
.w
, size
.h
);
264 } else if (scaleType
=== 'scale' || scaleType
=== 'downscale') {
267 var downscaleOnly
= scaleType
=== 'downscale';
268 var scaleRatio
= display
.autoscale(size
.w
, size
.h
, downscaleOnly
);
269 UI
.rfb
.get_mouse().set_scale(scaleRatio
);
270 Util
.Debug('Scaling by ' + UI
.rfb
.get_mouse().get_scale());
275 getCanvasLimit: function () {
276 var container
= $D('noVNC_container');
278 // Hide the scrollbars until the size is calculated
279 container
.style
.overflow
= "hidden";
281 var pos
= Util
.getPosition(container
);
285 container
.style
.overflow
= "visible";
287 if (isNaN(w
) || isNaN(h
)) {
294 // Read form control compatible setting from cookie
295 getSetting: function(name
) {
296 var ctrl
= $D('noVNC_' + name
);
297 var val
= WebUtil
.readSetting(name
);
298 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
299 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
308 // Update cookie and form control setting. If value is not set, then
309 // updates from control to current cookie setting.
310 updateSetting: function(name
, value
) {
312 // Save the cookie for this session
313 if (typeof value
!== 'undefined') {
314 WebUtil
.writeSetting(name
, value
);
317 // Update the settings control
318 value
= UI
.getSetting(name
);
320 var ctrl
= $D('noVNC_' + name
);
321 if (ctrl
.type
=== 'checkbox') {
322 ctrl
.checked
= value
;
324 } else if (typeof ctrl
.options
!== 'undefined') {
325 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
326 if (ctrl
.options
[i
].value
=== value
) {
327 ctrl
.selectedIndex
= i
;
332 /*Weird IE9 error leads to 'null' appearring
333 in textboxes instead of ''.*/
334 if (value
=== null) {
341 // Save control setting to cookie
342 saveSetting: function(name
) {
343 var val
, ctrl
= $D('noVNC_' + name
);
344 if (ctrl
.type
=== 'checkbox') {
346 } else if (typeof ctrl
.options
!== 'undefined') {
347 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
351 WebUtil
.writeSetting(name
, val
);
352 //Util.Debug("Setting saved '" + name + "=" + val + "'");
356 // Initial page load read/initialization of settings
357 initSetting: function(name
, defVal
) {
358 // Check Query string followed by cookie
359 var val
= WebUtil
.getConfigVar(name
);
361 val
= WebUtil
.readSetting(name
, defVal
);
363 UI
.updateSetting(name
, val
);
367 // Force a setting to be a certain value
368 forceSetting: function(name
, val
) {
369 UI
.updateSetting(name
, val
);
374 // Show the popup status
375 togglePopupStatus: function(text
) {
376 var psp
= $D('noVNC_popup_status');
378 var closePopup = function() { psp
.style
.display
= "none"; };
380 if (window
.getComputedStyle(psp
).display
=== 'none') {
381 if (typeof text
=== 'string') {
382 psp
.innerHTML
= text
;
384 psp
.innerHTML
= $D('noVNC_status').innerHTML
;
386 psp
.style
.display
= "block";
387 psp
.style
.left
= window
.innerWidth
/2 -
388 parseInt(window
.getComputedStyle(psp
).width
)/2 -30 + "px";
390 // Show the popup for a maximum of 1.5 seconds
391 UI
.popupStatusTimeout
= setTimeout(function() { closePopup(); }, 1500);
393 clearTimeout(UI
.popupStatusTimeout
);
398 // Show the XVP panel
399 toggleXvpPanel: function() {
400 // Close the description panel
401 $D('noVNC_description').style
.display
= "none";
402 // Close settings if open
403 if (UI
.settingsOpen
=== true) {
405 UI
.closeSettingsMenu();
407 // Close connection settings if open
408 if (UI
.connSettingsOpen
=== true) {
409 UI
.toggleConnectPanel();
411 // Close clipboard panel if open
412 if (UI
.clipboardOpen
=== true) {
413 UI
.toggleClipboardPanel();
416 if (UI
.xvpOpen
=== true) {
417 $D('noVNC_xvp').style
.display
= "none";
418 $D('xvpButton').className
= "noVNC_status_button";
421 $D('noVNC_xvp').style
.display
= "block";
422 $D('xvpButton').className
= "noVNC_status_button_selected";
427 // Show the clipboard panel
428 toggleClipboardPanel: function() {
429 // Close the description panel
430 $D('noVNC_description').style
.display
= "none";
431 // Close settings if open
432 if (UI
.settingsOpen
=== true) {
434 UI
.closeSettingsMenu();
436 // Close connection settings if open
437 if (UI
.connSettingsOpen
=== true) {
438 UI
.toggleConnectPanel();
440 // Close XVP panel if open
441 if (UI
.xvpOpen
=== true) {
444 // Toggle Clipboard Panel
445 if (UI
.clipboardOpen
=== true) {
446 $D('noVNC_clipboard').style
.display
= "none";
447 $D('clipboardButton').className
= "noVNC_status_button";
448 UI
.clipboardOpen
= false;
450 $D('noVNC_clipboard').style
.display
= "block";
451 $D('clipboardButton').className
= "noVNC_status_button_selected";
452 UI
.clipboardOpen
= true;
456 // Toggle fullscreen mode
457 toggleFullscreen: function() {
458 if (document
.fullscreenElement
|| // alternative standard method
459 document
.mozFullScreenElement
|| // currently working methods
460 document
.webkitFullscreenElement
||
461 document
.msFullscreenElement
) {
462 if (document
.exitFullscreen
) {
463 document
.exitFullscreen();
464 } else if (document
.mozCancelFullScreen
) {
465 document
.mozCancelFullScreen();
466 } else if (document
.webkitExitFullscreen
) {
467 document
.webkitExitFullscreen();
468 } else if (document
.msExitFullscreen
) {
469 document
.msExitFullscreen();
472 if (document
.documentElement
.requestFullscreen
) {
473 document
.documentElement
.requestFullscreen();
474 } else if (document
.documentElement
.mozRequestFullScreen
) {
475 document
.documentElement
.mozRequestFullScreen();
476 } else if (document
.documentElement
.webkitRequestFullscreen
) {
477 document
.documentElement
.webkitRequestFullscreen(Element
.ALLOW_KEYBOARD_INPUT
);
478 } else if (document
.body
.msRequestFullscreen
) {
479 document
.body
.msRequestFullscreen();
482 UI
.enableDisableViewClip();
483 UI
.updateFullscreenButton();
486 updateFullscreenButton: function() {
487 if (document
.fullscreenElement
|| // alternative standard method
488 document
.mozFullScreenElement
|| // currently working methods
489 document
.webkitFullscreenElement
||
490 document
.msFullscreenElement
) {
491 $D('fullscreenButton').className
= "noVNC_status_button_selected";
493 $D('fullscreenButton').className
= "noVNC_status_button";
497 // Show the connection settings panel/menu
498 toggleConnectPanel: function() {
499 // Close the description panel
500 $D('noVNC_description').style
.display
= "none";
501 // Close connection settings if open
502 if (UI
.settingsOpen
=== true) {
504 UI
.closeSettingsMenu();
505 $D('connectButton').className
= "noVNC_status_button";
507 // Close clipboard panel if open
508 if (UI
.clipboardOpen
=== true) {
509 UI
.toggleClipboardPanel();
511 // Close XVP panel if open
512 if (UI
.xvpOpen
=== true) {
516 // Toggle Connection Panel
517 if (UI
.connSettingsOpen
=== true) {
518 $D('noVNC_controls').style
.display
= "none";
519 $D('connectButton').className
= "noVNC_status_button";
520 UI
.connSettingsOpen
= false;
521 UI
.saveSetting('host');
522 UI
.saveSetting('port');
523 UI
.saveSetting('token');
524 //UI.saveSetting('password');
526 $D('noVNC_controls').style
.display
= "block";
527 $D('connectButton').className
= "noVNC_status_button_selected";
528 UI
.connSettingsOpen
= true;
529 $D('noVNC_host').focus();
533 // Toggle the settings menu:
534 // On open, settings are refreshed from saved cookies.
535 // On close, settings are applied
536 toggleSettingsPanel: function() {
537 // Close the description panel
538 $D('noVNC_description').style
.display
= "none";
539 if (UI
.settingsOpen
) {
541 UI
.closeSettingsMenu();
543 UI
.updateSetting('encrypt');
544 UI
.updateSetting('true_color');
545 if (Util
.browserSupportsCursorURIs()) {
546 UI
.updateSetting('cursor');
548 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
549 $D('noVNC_cursor').disabled
= true;
551 UI
.updateSetting('clip');
552 UI
.updateSetting('resize');
553 UI
.updateSetting('shared');
554 UI
.updateSetting('view_only');
555 UI
.updateSetting('path');
556 UI
.updateSetting('repeaterID');
557 UI
.updateSetting('stylesheet');
558 UI
.updateSetting('logging');
560 UI
.openSettingsMenu();
565 openSettingsMenu: function() {
566 // Close the description panel
567 $D('noVNC_description').style
.display
= "none";
568 // Close clipboard panel if open
569 if (UI
.clipboardOpen
=== true) {
570 UI
.toggleClipboardPanel();
572 // Close connection settings if open
573 if (UI
.connSettingsOpen
=== true) {
574 UI
.toggleConnectPanel();
576 // Close XVP panel if open
577 if (UI
.xvpOpen
=== true) {
580 $D('noVNC_settings').style
.display
= "block";
581 $D('settingsButton').className
= "noVNC_status_button_selected";
582 UI
.settingsOpen
= true;
585 // Close menu (without applying settings)
586 closeSettingsMenu: function() {
587 $D('noVNC_settings').style
.display
= "none";
588 $D('settingsButton').className
= "noVNC_status_button";
589 UI
.settingsOpen
= false;
592 // Save/apply settings when 'Apply' button is pressed
593 settingsApply: function() {
594 //Util.Debug(">> settingsApply");
595 UI
.saveSetting('encrypt');
596 UI
.saveSetting('true_color');
597 if (Util
.browserSupportsCursorURIs()) {
598 UI
.saveSetting('cursor');
601 UI
.saveSetting('resize');
603 if (UI
.getSetting('resize') === 'downscale' || UI
.getSetting('resize') === 'scale') {
604 UI
.forceSetting('clip', false);
607 UI
.saveSetting('clip');
608 UI
.saveSetting('shared');
609 UI
.saveSetting('view_only');
610 UI
.saveSetting('path');
611 UI
.saveSetting('repeaterID');
612 UI
.saveSetting('stylesheet');
613 UI
.saveSetting('logging');
615 // Settings with immediate (non-connected related) effect
616 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
617 WebUtil
.init_logging(UI
.getSetting('logging'));
620 //Util.Debug("<< settingsApply");
625 setPassword: function() {
626 UI
.rfb
.sendPassword($D('noVNC_password').value
);
627 //Reset connect button.
628 $D('noVNC_connect_button').value
= "Connect";
629 $D('noVNC_connect_button').onclick
= UI
.connect
;
630 //Hide connection panel.
631 UI
.toggleConnectPanel();
635 sendCtrlAltDel: function() {
636 UI
.rfb
.sendCtrlAltDel();
639 xvpShutdown: function() {
640 UI
.rfb
.xvpShutdown();
643 xvpReboot: function() {
647 xvpReset: function() {
651 setMouseButton: function(num
) {
652 if (typeof num
=== 'undefined') {
653 // Disable mouse buttons
657 UI
.rfb
.get_mouse().set_touchButton(num
);
660 var blist
= [0, 1,2,4];
661 for (var b
= 0; b
< blist
.length
; b
++) {
662 var button
= $D('noVNC_mouse_button' + blist
[b
]);
663 if (blist
[b
] === num
) {
664 button
.style
.display
= "";
666 button
.style
.display
= "none";
671 updateState: function(rfb
, state
, oldstate
, msg
) {
672 UI
.rfb_state
= state
;
677 klass
= "noVNC_status_error";
680 klass
= "noVNC_status_normal";
683 $D('noVNC_logo').style
.display
= "block";
684 $D('noVNC_container').style
.display
= "none";
687 klass
= "noVNC_status_normal";
690 UI
.toggleConnectPanel();
692 $D('noVNC_connect_button').value
= "Send Password";
693 $D('noVNC_connect_button').onclick
= UI
.setPassword
;
694 $D('noVNC_password').focus();
696 klass
= "noVNC_status_warn";
699 klass
= "noVNC_status_warn";
703 if (typeof(msg
) !== 'undefined') {
704 $D('noVNC-control-bar').setAttribute("class", klass
);
705 $D('noVNC_status').innerHTML
= msg
;
708 UI
.updateVisualState();
711 // Disable/enable controls depending on connection state
712 updateVisualState: function() {
713 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
715 //Util.Debug(">> updateVisualState");
716 $D('noVNC_encrypt').disabled
= connected
;
717 $D('noVNC_true_color').disabled
= connected
;
718 if (Util
.browserSupportsCursorURIs()) {
719 $D('noVNC_cursor').disabled
= connected
;
721 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
722 $D('noVNC_cursor').disabled
= true;
725 UI
.enableDisableViewClip();
726 $D('noVNC_resize').disabled
= connected
;
727 $D('noVNC_shared').disabled
= connected
;
728 $D('noVNC_view_only').disabled
= connected
;
729 $D('noVNC_path').disabled
= connected
;
730 $D('noVNC_repeaterID').disabled
= connected
;
734 UI
.setMouseButton(1);
735 $D('clipboardButton').style
.display
= "inline";
736 $D('showKeyboard').style
.display
= "inline";
737 $D('noVNC_extra_keys').style
.display
= "";
738 $D('sendCtrlAltDelButton').style
.display
= "inline";
741 $D('clipboardButton').style
.display
= "none";
742 $D('showKeyboard').style
.display
= "none";
743 $D('noVNC_extra_keys').style
.display
= "none";
744 $D('sendCtrlAltDelButton').style
.display
= "none";
745 UI
.updateXvpVisualState(0);
748 // State change disables viewport dragging.
749 // It is enabled (toggled) by direct click on the button
750 UI
.updateViewDrag(false);
752 switch (UI
.rfb_state
) {
756 $D('connectButton').style
.display
= "";
757 $D('disconnectButton').style
.display
= "none";
758 UI
.connSettingsOpen
= false;
759 UI
.toggleConnectPanel();
762 $D('connectButton').style
.display
= "";
763 $D('disconnectButton').style
.display
= "none";
766 $D('connectButton').style
.display
= "none";
767 $D('disconnectButton').style
.display
= "";
771 //Util.Debug("<< updateVisualState");
774 // Disable/enable XVP button
775 updateXvpVisualState: function(ver
) {
777 $D('xvpButton').style
.display
= 'inline';
779 $D('xvpButton').style
.display
= 'none';
780 // Close XVP panel if open
781 if (UI
.xvpOpen
=== true) {
787 // This resize can not be done until we know from the first Frame Buffer Update
788 // if it is supported or not.
789 // The resize is needed to make sure the server desktop size is updated to the
790 // corresponding size of the current local window when reconnecting to an
792 FBUComplete: function(rfb
, fbu
) {
794 UI
.rfb
.set_onFBUComplete(function() { });
797 // Display the desktop name in the document title
798 updateDocumentTitle: function(rfb
, name
) {
799 document
.title
= name
+ " - noVNC";
802 clipReceive: function(rfb
, text
) {
803 Util
.Debug(">> UI.clipReceive: " + text
.substr(0,40) + "...");
804 $D('noVNC_clipboard_text').value
= text
;
805 Util
.Debug("<< UI.clipReceive");
808 connect: function() {
809 UI
.closeSettingsMenu();
810 UI
.toggleConnectPanel();
812 var host
= $D('noVNC_host').value
;
813 var port
= $D('noVNC_port').value
;
814 var password
= $D('noVNC_password').value
;
815 var token
= $D('noVNC_token').value
;
816 var path
= $D('noVNC_path').value
;
818 //if token is in path then ignore the new token variable
820 path
= WebUtil
.injectParamIfMissing(path
, "token", token
);
823 if ((!host
) || (!port
)) {
824 throw new Error("Must set host and port");
827 if (!UI
.initRFB()) return;
829 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
830 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
831 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
832 UI
.rfb
.set_shared(UI
.getSetting('shared'));
833 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
834 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
836 UI
.rfb
.connect(host
, port
, password
, path
);
839 setTimeout(UI
.setBarPosition
, 100);
840 $D('noVNC_logo').style
.display
= "none";
841 $D('noVNC_container').style
.display
= "inline";
844 disconnect: function() {
845 UI
.closeSettingsMenu();
848 // Restore the callback used for initial resize
849 UI
.rfb
.set_onFBUComplete(UI
.FBUComplete
);
851 $D('noVNC_logo').style
.display
= "block";
852 $D('noVNC_container').style
.display
= "none";
854 // Don't display the connection settings until we're actually disconnected
857 displayBlur: function() {
860 UI
.rfb
.get_keyboard().set_focused(false);
861 UI
.rfb
.get_mouse().set_focused(false);
864 displayFocus: function() {
867 UI
.rfb
.get_keyboard().set_focused(true);
868 UI
.rfb
.get_mouse().set_focused(true);
871 clipClear: function() {
872 $D('noVNC_clipboard_text').value
= "";
873 UI
.rfb
.clipboardPasteFrom("");
876 clipSend: function() {
877 var text
= $D('noVNC_clipboard_text').value
;
878 Util
.Debug(">> UI.clipSend: " + text
.substr(0,40) + "...");
879 UI
.rfb
.clipboardPasteFrom(text
);
880 Util
.Debug("<< UI.clipSend");
883 // Set and configure viewport clipping
884 setViewClip: function(clip
) {
887 display
= UI
.rfb
.get_display();
889 UI
.forceSetting('clip', clip
);
893 var cur_clip
= display
.get_viewport();
895 if (typeof(clip
) !== 'boolean') {
896 // Use current setting
897 clip
= UI
.getSetting('clip');
900 if (clip
&& !cur_clip
) {
902 UI
.updateSetting('clip', true);
903 } else if (!clip
&& cur_clip
) {
905 UI
.updateSetting('clip', false);
906 display
.set_viewport(false);
907 // Disable max dimensions
908 display
.set_maxWidth(0);
909 display
.set_maxHeight(0);
910 display
.viewportChangeSize();
912 if (UI
.getSetting('clip')) {
913 // If clipping, update clipping settings
914 display
.set_viewport(true);
916 var size
= UI
.getCanvasLimit();
918 display
.set_maxWidth(size
.w
);
919 display
.set_maxHeight(size
.h
);
921 // Hide potential scrollbars that can skew the position
922 $D('noVNC_container').style
.overflow
= "hidden";
924 // The x position marks the left margin of the canvas,
925 // remove the margin from both sides to keep it centered
926 var new_w
= size
.w
- (2 * Util
.getPosition($D('noVNC_canvas')).x
);
928 $D('noVNC_container').style
.overflow
= "visible";
930 display
.viewportChangeSize(new_w
, size
.h
);
935 // Handle special cases where clipping is forced on/off or locked
936 enableDisableViewClip: function () {
937 var resizeElem
= $D('noVNC_resize');
938 var connected
= UI
.rfb
&& UI
.rfb_state
=== 'normal';
940 if (resizeElem
.value
=== 'downscale' || resizeElem
.value
=== 'scale') {
941 // Disable clipping if we are scaling
942 UI
.setViewClip(false);
943 $D('noVNC_clip').disabled
= true;
944 } else if (document
.msFullscreenElement
) {
945 // The browser is IE and we are in fullscreen mode.
946 // - We need to force clipping while in fullscreen since
947 // scrollbars doesn't work.
948 UI
.togglePopupStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
949 UI
.rememberedClipSetting
= UI
.getSetting('clip');
950 UI
.setViewClip(true);
951 $D('noVNC_clip').disabled
= true;
952 } else if (document
.body
.msRequestFullscreen
&& UI
.rememberedClip
!== null) {
953 // Restore view clip to what it was before fullscreen on IE
954 UI
.setViewClip(UI
.rememberedClipSetting
);
955 $D('noVNC_clip').disabled
= connected
|| UI
.isTouchDevice
;
957 $D('noVNC_clip').disabled
= connected
|| UI
.isTouchDevice
;
958 if (UI
.isTouchDevice
) {
959 UI
.setViewClip(true);
964 // Update the viewport drag/move button
965 updateViewDrag: function(drag
) {
968 var vmb
= $D('noVNC_view_drag_button');
970 // Check if viewport drag is possible
971 if (UI
.rfb_state
=== 'normal' &&
972 UI
.rfb
.get_display().get_viewport() &&
973 UI
.rfb
.get_display().clippingDisplay()) {
975 // Show and enable the drag button
976 vmb
.style
.display
= "inline";
977 vmb
.disabled
= false;
980 // The VNC content is the same size as
981 // or smaller than the display
983 if (UI
.rfb
.get_viewportDrag
) {
984 // Turn off viewport drag when it's
985 // active since it can't be used here
986 vmb
.className
= "noVNC_status_button";
987 UI
.rfb
.set_viewportDrag(false);
990 // Disable or hide the drag button
991 if (UI
.rfb_state
=== 'normal' && UI
.isTouchDevice
) {
992 vmb
.style
.display
= "inline";
995 vmb
.style
.display
= "none";
1000 if (typeof(drag
) !== "undefined" &&
1001 typeof(drag
) !== "object") {
1003 vmb
.className
= "noVNC_status_button_selected";
1004 UI
.rfb
.set_viewportDrag(true);
1006 vmb
.className
= "noVNC_status_button";
1007 UI
.rfb
.set_viewportDrag(false);
1012 toggleViewDrag: function() {
1013 if (!UI
.rfb
) return;
1015 var vmb
= $D('noVNC_view_drag_button');
1016 if (UI
.rfb
.get_viewportDrag()) {
1017 vmb
.className
= "noVNC_status_button";
1018 UI
.rfb
.set_viewportDrag(false);
1020 vmb
.className
= "noVNC_status_button_selected";
1021 UI
.rfb
.set_viewportDrag(true);
1025 // On touch devices, show the OS keyboard
1026 showKeyboard: function() {
1027 var kbi
= $D('keyboardinput');
1028 var skb
= $D('showKeyboard');
1029 var l
= kbi
.value
.length
;
1030 if(UI
.keyboardVisible
=== false) {
1032 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
1033 catch (err
) {} // setSelectionRange is undefined in Google Chrome
1034 UI
.keyboardVisible
= true;
1035 skb
.className
= "noVNC_status_button_selected";
1036 } else if(UI
.keyboardVisible
=== true) {
1038 skb
.className
= "noVNC_status_button";
1039 UI
.keyboardVisible
= false;
1043 keepKeyboard: function() {
1044 clearTimeout(UI
.hideKeyboardTimeout
);
1045 if(UI
.keyboardVisible
=== true) {
1046 $D('keyboardinput').focus();
1047 $D('showKeyboard').className
= "noVNC_status_button_selected";
1048 } else if(UI
.keyboardVisible
=== false) {
1049 $D('keyboardinput').blur();
1050 $D('showKeyboard').className
= "noVNC_status_button";
1054 keyboardinputReset: function() {
1055 var kbi
= $D('keyboardinput');
1056 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
1057 UI
.lastKeyboardinput
= kbi
.value
;
1060 // When normal keyboard events are left uncought, use the input events from
1061 // the keyboardinput element instead and generate the corresponding key events.
1062 // This code is required since some browsers on Android are inconsistent in
1063 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1064 keyInput: function(event
) {
1066 if (!UI
.rfb
) return;
1068 var newValue
= event
.target
.value
;
1070 if (!UI
.lastKeyboardinput
) {
1071 UI
.keyboardinputReset();
1073 var oldValue
= UI
.lastKeyboardinput
;
1077 // Try to check caret position since whitespace at the end
1078 // will not be considered by value.length in some browsers
1079 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
1081 // selectionStart is undefined in Google Chrome
1082 newLen
= newValue
.length
;
1084 var oldLen
= oldValue
.length
;
1087 var inputs
= newLen
- oldLen
;
1089 backspaces
= -inputs
;
1094 // Compare the old string with the new to account for
1095 // text-corrections or other input that modify existing text
1097 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
1098 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
1099 inputs
= newLen
- i
;
1100 backspaces
= oldLen
- i
;
1105 // Send the key events
1106 for (i
= 0; i
< backspaces
; i
++) {
1107 UI
.rfb
.sendKey(XK_BackSpace
);
1109 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
1110 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
1113 // Control the text content length in the keyboardinput element
1114 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
1115 UI
.keyboardinputReset();
1116 } else if (newLen
< 1) {
1117 // There always have to be some text in the keyboardinput
1118 // element with which backspace can interact.
1119 UI
.keyboardinputReset();
1120 // This sometimes causes the keyboard to disappear for a second
1121 // but it is required for the android keyboard to recognize that
1122 // text has been added to the field
1123 event
.target
.blur();
1124 // This has to be ran outside of the input handler in order to work
1125 setTimeout(function() { UI
.keepKeyboard(); }, 0);
1127 UI
.lastKeyboardinput
= newValue
;
1131 keyInputBlur: function() {
1132 $D('showKeyboard').className
= "noVNC_status_button";
1133 //Weird bug in iOS if you change keyboardVisible
1134 //here it does not actually occur so next time
1135 //you click keyboard icon it doesnt work.
1136 UI
.hideKeyboardTimeout
= setTimeout(function() { UI
.setKeyboard(); },100);
1139 showExtraKeys: function() {
1141 if(UI
.extraKeysVisible
=== false) {
1142 $D('toggleCtrlButton').style
.display
= "inline";
1143 $D('toggleAltButton').style
.display
= "inline";
1144 $D('sendTabButton').style
.display
= "inline";
1145 $D('sendEscButton').style
.display
= "inline";
1146 $D('showExtraKeysButton').className
= "noVNC_status_button_selected";
1147 UI
.extraKeysVisible
= true;
1148 } else if(UI
.extraKeysVisible
=== true) {
1149 $D('toggleCtrlButton').style
.display
= "";
1150 $D('toggleAltButton').style
.display
= "";
1151 $D('sendTabButton').style
.display
= "";
1152 $D('sendEscButton').style
.display
= "";
1153 $D('showExtraKeysButton').className
= "noVNC_status_button";
1154 UI
.extraKeysVisible
= false;
1158 toggleCtrl: function() {
1160 if(UI
.ctrlOn
=== false) {
1161 UI
.rfb
.sendKey(XK_Control_L
, true);
1162 $D('toggleCtrlButton').className
= "noVNC_status_button_selected";
1164 } else if(UI
.ctrlOn
=== true) {
1165 UI
.rfb
.sendKey(XK_Control_L
, false);
1166 $D('toggleCtrlButton').className
= "noVNC_status_button";
1171 toggleAlt: function() {
1173 if(UI
.altOn
=== false) {
1174 UI
.rfb
.sendKey(XK_Alt_L
, true);
1175 $D('toggleAltButton').className
= "noVNC_status_button_selected";
1177 } else if(UI
.altOn
=== true) {
1178 UI
.rfb
.sendKey(XK_Alt_L
, false);
1179 $D('toggleAltButton').className
= "noVNC_status_button";
1184 sendTab: function() {
1186 UI
.rfb
.sendKey(XK_Tab
);
1189 sendEsc: function() {
1191 UI
.rfb
.sendKey(XK_Escape
);
1194 setKeyboard: function() {
1195 UI
.keyboardVisible
= false;
1198 //Helper to add options to dropdown.
1199 addOption: function(selectbox
, text
, value
) {
1200 var optn
= document
.createElement("OPTION");
1203 selectbox
.options
.add(optn
);
1206 setBarPosition: function() {
1207 $D('noVNC-control-bar').style
.top
= (window
.pageYOffset
) + 'px';
1208 $D('noVNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
1210 var vncwidth
= $D('noVNC_screen').style
.offsetWidth
;
1211 $D('noVNC-control-bar').style
.width
= vncwidth
+ 'px';