]>
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) 2013 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 */
20 // Load supporting scripts
21 window
.onscriptsload = function () { UI
.load(); };
22 Util
.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
23 "keysymdef.js", "keyboard.js", "input.js", "display.js",
24 "jsunzip.js", "rfb.js", "keysym.js"]);
30 connSettingsOpen
: false,
31 popupStatusOpen
: false,
33 keyboardVisible
: false,
34 hideKeyboardTimeout
: null,
35 lastKeyboardinput
: null,
36 defaultKeyboardinputLen
: 100,
37 extraKeysVisible
: false,
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 onresize: function (callback
) {
49 if (UI
.getSetting('resize')) {
50 var innerW
= window
.innerWidth
;
51 var innerH
= window
.innerHeight
;
52 var controlbarH
= $D('noVNC-control-bar').offsetHeight
;
53 // For some unknown reason the container is higher than the canvas,
54 // 5px higher in Firefox and 4px higher in Chrome
56 if (innerW
!== undefined && innerH
!== undefined)
57 UI
.rfb
.setDesktopSize(innerW
, innerH
- controlbarH
- padding
);
61 // Render default UI and initialize settings menu
62 start: function(callback
) {
63 UI
.isTouchDevice
= 'ontouchstart' in document
.documentElement
;
65 // Stylesheet selection dropdown
66 var sheet
= WebUtil
.selectStylesheet();
67 var sheets
= WebUtil
.getStylesheets();
69 for (i
= 0; i
< sheets
.length
; i
+= 1) {
70 UI
.addOption($D('noVNC_stylesheet'),sheets
[i
].title
, sheets
[i
].title
);
73 // Logging selection dropdown
74 var llevels
= ['error', 'warn', 'info', 'debug'];
75 for (i
= 0; i
< llevels
.length
; i
+= 1) {
76 UI
.addOption($D('noVNC_logging'),llevels
[i
], llevels
[i
]);
79 // Settings with immediate effects
80 UI
.initSetting('logging', 'warn');
81 WebUtil
.init_logging(UI
.getSetting('logging'));
83 UI
.initSetting('stylesheet', 'default');
84 WebUtil
.selectStylesheet(null);
85 // call twice to get around webkit bug
86 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
88 // if port == 80 (or 443) then it won't be present and should be
90 var port
= window
.location
.port
;
92 if (window
.location
.protocol
.substring(0,5) == 'https') {
95 else if (window
.location
.protocol
.substring(0,4) == 'http') {
100 /* Populate the controls if defaults are provided in the URL */
101 UI
.initSetting('host', window
.location
.hostname
);
102 UI
.initSetting('port', port
);
103 UI
.initSetting('password', '');
104 UI
.initSetting('encrypt', (window
.location
.protocol
=== "https:"));
105 UI
.initSetting('true_color', true);
106 UI
.initSetting('cursor', !UI
.isTouchDevice
);
107 UI
.initSetting('resize', false);
108 UI
.initSetting('shared', true);
109 UI
.initSetting('view_only', false);
110 UI
.initSetting('path', 'websockify');
111 UI
.initSetting('repeaterID', '');
113 UI
.rfb
= new RFB({'target': $D('noVNC_canvas'),
114 'onUpdateState': UI
.updateState
,
115 'onXvpInit': UI
.updateXvpVisualState
,
116 'onClipboard': UI
.clipReceive
,
117 'onFBUComplete': UI
.FBUComplete
,
118 'onFBResize': UI
.updateViewDragButton
,
119 'onDesktopName': UI
.updateDocumentTitle
});
121 var autoconnect
= WebUtil
.getQueryVar('autoconnect', false);
122 if (autoconnect
=== 'true' || autoconnect
== '1') {
129 UI
.updateVisualState();
131 // Show mouse selector buttons on touch screen devices
132 if (UI
.isTouchDevice
) {
133 // Show mobile buttons
134 $D('noVNC_mobile_buttons').style
.display
= "inline";
136 // Remove the address bar
137 setTimeout(function() { window
.scrollTo(0, 1); }, 100);
138 UI
.forceSetting('clip', true);
140 UI
.initSetting('clip', false);
143 //iOS Safari does not support CSS position:fixed.
144 //This detects iOS devices and enables javascript workaround.
145 if ((navigator
.userAgent
.match(/iPhone/i)) ||
146 (navigator
.userAgent
.match(/iPod/i)) ||
147 (navigator
.userAgent
.match(/iPad/i))) {
153 $D('noVNC_host').focus();
157 Util
.addEvent(window
, 'resize', function () {
159 // When the window has been resized, wait until the size remains
160 // the same for 0.5 seconds before sending the request for changing
161 // the resolution of the session
162 clearTimeout(resizeTimeout
);
163 resizeTimeout
= setTimeout(function(){
168 Util
.addEvent(window
, 'load', UI
.keyboardinputReset
);
170 Util
.addEvent(window
, 'beforeunload', function () {
171 if (UI
.rfb_state
=== 'normal') {
172 return "You are currently connected.";
176 // Show description by default when hosted at for kanaka.github.com
177 if (location
.host
=== "kanaka.github.io") {
178 // Open the description dialog
179 $D('noVNC_description').style
.display
= "block";
181 // Show the connect panel on first load unless autoconnecting
182 if (autoconnect
=== UI
.connSettingsOpen
) {
183 UI
.toggleConnectPanel();
187 // Add mouse event click/focus/blur event handlers to the UI
188 UI
.addMouseHandlers();
190 if (typeof callback
=== "function") {
195 addMouseHandlers: function() {
196 // Setup interface handlers that can't be inline
197 $D("noVNC_view_drag_button").onclick
= UI
.setViewDrag
;
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
;
207 $D("showExtraKeysButton").onclick
= UI
.showExtraKeys
;
208 $D("toggleCtrlButton").onclick
= UI
.toggleCtrl
;
209 $D("toggleAltButton").onclick
= UI
.toggleAlt
;
210 $D("sendTabButton").onclick
= UI
.sendTab
;
211 $D("sendEscButton").onclick
= UI
.sendEsc
;
213 $D("sendCtrlAltDelButton").onclick
= UI
.sendCtrlAltDel
;
214 $D("xvpShutdownButton").onclick
= UI
.xvpShutdown
;
215 $D("xvpRebootButton").onclick
= UI
.xvpReboot
;
216 $D("xvpResetButton").onclick
= UI
.xvpReset
;
217 $D("noVNC_status").onclick
= UI
.togglePopupStatusPanel
;
218 $D("noVNC_popup_status_panel").onclick
= UI
.togglePopupStatusPanel
;
219 $D("xvpButton").onclick
= UI
.toggleXvpPanel
;
220 $D("clipboardButton").onclick
= UI
.toggleClipboardPanel
;
221 $D("settingsButton").onclick
= UI
.toggleSettingsPanel
;
222 $D("connectButton").onclick
= UI
.toggleConnectPanel
;
223 $D("disconnectButton").onclick
= UI
.disconnect
;
224 $D("descriptionButton").onclick
= UI
.toggleConnectPanel
;
226 $D("noVNC_clipboard_text").onfocus
= UI
.displayBlur
;
227 $D("noVNC_clipboard_text").onblur
= UI
.displayFocus
;
228 $D("noVNC_clipboard_text").onchange
= UI
.clipSend
;
229 $D("noVNC_clipboard_clear_button").onclick
= UI
.clipClear
;
231 $D("noVNC_settings_menu").onmouseover
= UI
.displayBlur
;
232 $D("noVNC_settings_menu").onmouseover
= UI
.displayFocus
;
233 $D("noVNC_apply").onclick
= UI
.settingsApply
;
235 $D("noVNC_connect_button").onclick
= UI
.connect
;
238 // Read form control compatible setting from cookie
239 getSetting: function(name
) {
240 var ctrl
= $D('noVNC_' + name
);
241 var val
= WebUtil
.readSetting(name
);
242 if (typeof val
!== 'undefined' && val
!== null && ctrl
.type
=== 'checkbox') {
243 if (val
.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
252 // Update cookie and form control setting. If value is not set, then
253 // updates from control to current cookie setting.
254 updateSetting: function(name
, value
) {
256 // Save the cookie for this session
257 if (typeof value
!== 'undefined') {
258 WebUtil
.writeSetting(name
, value
);
261 // Update the settings control
262 value
= UI
.getSetting(name
);
264 var ctrl
= $D('noVNC_' + name
);
265 if (ctrl
.type
=== 'checkbox') {
266 ctrl
.checked
= value
;
268 } else if (typeof ctrl
.options
!== 'undefined') {
269 for (var i
= 0; i
< ctrl
.options
.length
; i
+= 1) {
270 if (ctrl
.options
[i
].value
=== value
) {
271 ctrl
.selectedIndex
= i
;
276 /*Weird IE9 error leads to 'null' appearring
277 in textboxes instead of ''.*/
278 if (value
=== null) {
285 // Save control setting to cookie
286 saveSetting: function(name
) {
287 var val
, ctrl
= $D('noVNC_' + name
);
288 if (ctrl
.type
=== 'checkbox') {
290 } else if (typeof ctrl
.options
!== 'undefined') {
291 val
= ctrl
.options
[ctrl
.selectedIndex
].value
;
295 WebUtil
.writeSetting(name
, val
);
296 //Util.Debug("Setting saved '" + name + "=" + val + "'");
300 // Initial page load read/initialization of settings
301 initSetting: function(name
, defVal
) {
302 // Check Query string followed by cookie
303 var val
= WebUtil
.getQueryVar(name
);
305 val
= WebUtil
.readSetting(name
, defVal
);
307 UI
.updateSetting(name
, val
);
311 // Force a setting to be a certain value
312 forceSetting: function(name
, val
) {
313 UI
.updateSetting(name
, val
);
318 // Show the popup status panel
319 togglePopupStatusPanel: function() {
320 var psp
= $D('noVNC_popup_status_panel');
321 if (UI
.popupStatusOpen
=== true) {
322 psp
.style
.display
= "none";
323 UI
.popupStatusOpen
= false;
325 psp
.innerHTML
= $D('noVNC_status').innerHTML
;
326 psp
.style
.display
= "block";
327 psp
.style
.left
= window
.innerWidth
/2 -
328 parseInt(window
.getComputedStyle(psp
, false).width
)/2 -30 + "px";
329 UI
.popupStatusOpen
= true;
333 // Show the XVP panel
334 toggleXvpPanel: function() {
335 // Close the description panel
336 $D('noVNC_description').style
.display
= "none";
337 // Close settings if open
338 if (UI
.settingsOpen
=== true) {
340 UI
.closeSettingsMenu();
342 // Close connection settings if open
343 if (UI
.connSettingsOpen
=== true) {
344 UI
.toggleConnectPanel();
346 // Close popup status panel if open
347 if (UI
.popupStatusOpen
=== true) {
348 UI
.togglePopupStatusPanel();
350 // Close clipboard panel if open
351 if (UI
.clipboardOpen
=== true) {
352 UI
.toggleClipboardPanel();
355 if (UI
.xvpOpen
=== true) {
356 $D('noVNC_xvp').style
.display
= "none";
357 $D('xvpButton').className
= "noVNC_status_button";
360 $D('noVNC_xvp').style
.display
= "block";
361 $D('xvpButton').className
= "noVNC_status_button_selected";
366 // Show the clipboard panel
367 toggleClipboardPanel: function() {
368 // Close the description panel
369 $D('noVNC_description').style
.display
= "none";
370 // Close settings if open
371 if (UI
.settingsOpen
=== true) {
373 UI
.closeSettingsMenu();
375 // Close connection settings if open
376 if (UI
.connSettingsOpen
=== true) {
377 UI
.toggleConnectPanel();
379 // Close popup status panel if open
380 if (UI
.popupStatusOpen
=== true) {
381 UI
.togglePopupStatusPanel();
383 // Close XVP panel if open
384 if (UI
.xvpOpen
=== true) {
387 // Toggle Clipboard Panel
388 if (UI
.clipboardOpen
=== true) {
389 $D('noVNC_clipboard').style
.display
= "none";
390 $D('clipboardButton').className
= "noVNC_status_button";
391 UI
.clipboardOpen
= false;
393 $D('noVNC_clipboard').style
.display
= "block";
394 $D('clipboardButton').className
= "noVNC_status_button_selected";
395 UI
.clipboardOpen
= true;
399 // Show the connection settings panel/menu
400 toggleConnectPanel: function() {
401 // Close the description panel
402 $D('noVNC_description').style
.display
= "none";
403 // Close connection settings if open
404 if (UI
.settingsOpen
=== true) {
406 UI
.closeSettingsMenu();
407 $D('connectButton').className
= "noVNC_status_button";
409 // Close clipboard panel if open
410 if (UI
.clipboardOpen
=== true) {
411 UI
.toggleClipboardPanel();
413 // Close popup status panel if open
414 if (UI
.popupStatusOpen
=== true) {
415 UI
.togglePopupStatusPanel();
417 // Close XVP panel if open
418 if (UI
.xvpOpen
=== true) {
422 // Toggle Connection Panel
423 if (UI
.connSettingsOpen
=== true) {
424 $D('noVNC_controls').style
.display
= "none";
425 $D('connectButton').className
= "noVNC_status_button";
426 UI
.connSettingsOpen
= false;
427 UI
.saveSetting('host');
428 UI
.saveSetting('port');
429 //UI.saveSetting('password');
431 $D('noVNC_controls').style
.display
= "block";
432 $D('connectButton').className
= "noVNC_status_button_selected";
433 UI
.connSettingsOpen
= true;
434 $D('noVNC_host').focus();
438 // Toggle the settings menu:
439 // On open, settings are refreshed from saved cookies.
440 // On close, settings are applied
441 toggleSettingsPanel: function() {
442 // Close the description panel
443 $D('noVNC_description').style
.display
= "none";
444 if (UI
.settingsOpen
) {
446 UI
.closeSettingsMenu();
448 UI
.updateSetting('encrypt');
449 UI
.updateSetting('true_color');
450 if (UI
.rfb
.get_display().get_cursor_uri()) {
451 UI
.updateSetting('cursor');
453 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
454 $D('noVNC_cursor').disabled
= true;
456 UI
.updateSetting('clip');
457 UI
.updateSetting('resize');
458 UI
.updateSetting('shared');
459 UI
.updateSetting('view_only');
460 UI
.updateSetting('path');
461 UI
.updateSetting('repeaterID');
462 UI
.updateSetting('stylesheet');
463 UI
.updateSetting('logging');
465 UI
.openSettingsMenu();
470 openSettingsMenu: function() {
471 // Close the description panel
472 $D('noVNC_description').style
.display
= "none";
473 // Close clipboard panel if open
474 if (UI
.clipboardOpen
=== true) {
475 UI
.toggleClipboardPanel();
477 // Close connection settings if open
478 if (UI
.connSettingsOpen
=== true) {
479 UI
.toggleConnectPanel();
481 // Close popup status panel if open
482 if (UI
.popupStatusOpen
=== true) {
483 UI
.togglePopupStatusPanel();
485 // Close XVP panel if open
486 if (UI
.xvpOpen
=== true) {
489 $D('noVNC_settings').style
.display
= "block";
490 $D('settingsButton').className
= "noVNC_status_button_selected";
491 UI
.settingsOpen
= true;
494 // Close menu (without applying settings)
495 closeSettingsMenu: function() {
496 $D('noVNC_settings').style
.display
= "none";
497 $D('settingsButton').className
= "noVNC_status_button";
498 UI
.settingsOpen
= false;
501 // Save/apply settings when 'Apply' button is pressed
502 settingsApply: function() {
503 //Util.Debug(">> settingsApply");
504 UI
.saveSetting('encrypt');
505 UI
.saveSetting('true_color');
506 if (UI
.rfb
.get_display().get_cursor_uri()) {
507 UI
.saveSetting('cursor');
509 UI
.saveSetting('clip');
510 UI
.saveSetting('resize');
511 UI
.saveSetting('shared');
512 UI
.saveSetting('view_only');
513 UI
.saveSetting('path');
514 UI
.saveSetting('repeaterID');
515 UI
.saveSetting('stylesheet');
516 UI
.saveSetting('logging');
518 // Settings with immediate (non-connected related) effect
519 WebUtil
.selectStylesheet(UI
.getSetting('stylesheet'));
520 WebUtil
.init_logging(UI
.getSetting('logging'));
522 UI
.setViewDrag(UI
.rfb
.get_viewportDrag());
523 //Util.Debug("<< settingsApply");
528 setPassword: function() {
529 UI
.rfb
.sendPassword($D('noVNC_password').value
);
530 //Reset connect button.
531 $D('noVNC_connect_button').value
= "Connect";
532 $D('noVNC_connect_button').onclick
= UI
.Connect
;
533 //Hide connection panel.
534 UI
.toggleConnectPanel();
538 sendCtrlAltDel: function() {
539 UI
.rfb
.sendCtrlAltDel();
542 xvpShutdown: function() {
543 UI
.rfb
.xvpShutdown();
546 xvpReboot: function() {
550 xvpReset: function() {
554 setMouseButton: function(num
) {
555 if (typeof num
=== 'undefined') {
556 // Disable mouse buttons
560 UI
.rfb
.get_mouse().set_touchButton(num
);
563 var blist
= [0, 1,2,4];
564 for (var b
= 0; b
< blist
.length
; b
++) {
565 var button
= $D('noVNC_mouse_button' + blist
[b
]);
566 if (blist
[b
] === num
) {
567 button
.style
.display
= "";
569 button
.style
.display
= "none";
574 updateState: function(rfb
, state
, oldstate
, msg
) {
575 UI
.rfb_state
= state
;
580 klass
= "noVNC_status_error";
583 klass
= "noVNC_status_normal";
586 $D('noVNC_logo').style
.display
= "block";
589 klass
= "noVNC_status_normal";
592 UI
.toggleConnectPanel();
594 $D('noVNC_connect_button').value
= "Send Password";
595 $D('noVNC_connect_button').onclick
= UI
.setPassword
;
596 $D('noVNC_password').focus();
598 klass
= "noVNC_status_warn";
601 klass
= "noVNC_status_warn";
605 if (typeof(msg
) !== 'undefined') {
606 $D('noVNC-control-bar').setAttribute("class", klass
);
607 $D('noVNC_status').innerHTML
= msg
;
610 UI
.updateVisualState();
613 // Disable/enable controls depending on connection state
614 updateVisualState: function() {
615 var connected
= UI
.rfb_state
=== 'normal' ? true : false;
617 //Util.Debug(">> updateVisualState");
618 $D('noVNC_encrypt').disabled
= connected
;
619 $D('noVNC_true_color').disabled
= connected
;
620 if (UI
.rfb
&& UI
.rfb
.get_display() &&
621 UI
.rfb
.get_display().get_cursor_uri()) {
622 $D('noVNC_cursor').disabled
= connected
;
624 UI
.updateSetting('cursor', !UI
.isTouchDevice
);
625 $D('noVNC_cursor').disabled
= true;
627 $D('noVNC_clip').disabled
= connected
|| UI
.isTouchDevice
;
628 $D('noVNC_resize').disabled
= connected
;
629 $D('noVNC_shared').disabled
= connected
;
630 $D('noVNC_view_only').disabled
= connected
;
631 $D('noVNC_path').disabled
= connected
;
632 $D('noVNC_repeaterID').disabled
= connected
;
636 UI
.setMouseButton(1);
637 $D('clipboardButton').style
.display
= "inline";
638 $D('showKeyboard').style
.display
= "inline";
639 $D('noVNC_extra_keys').style
.display
= "";
640 $D('sendCtrlAltDelButton').style
.display
= "inline";
643 $D('clipboardButton').style
.display
= "none";
644 $D('showKeyboard').style
.display
= "none";
645 $D('noVNC_extra_keys').style
.display
= "none";
646 $D('sendCtrlAltDelButton').style
.display
= "none";
647 UI
.updateXvpVisualState(0);
650 // State change disables viewport dragging.
651 // It is enabled (toggled) by direct click on the button
652 UI
.setViewDrag(false);
654 switch (UI
.rfb_state
) {
659 $D('connectButton').style
.display
= "";
660 $D('disconnectButton').style
.display
= "none";
663 $D('connectButton').style
.display
= "none";
664 $D('disconnectButton').style
.display
= "";
668 //Util.Debug("<< updateVisualState");
671 // Disable/enable XVP button
672 updateXvpVisualState: function(ver
) {
674 $D('xvpButton').style
.display
= 'inline';
676 $D('xvpButton').style
.display
= 'none';
677 // Close XVP panel if open
678 if (UI
.xvpOpen
=== true) {
684 // This resize can not be done until we know from the first Frame Buffer Update
685 // if it is supported or not.
686 // The resize is needed to make sure the server desktop size is updated to the
687 // corresponding size of the current local window when reconnecting to an
689 FBUComplete: function(rfb
, fbu
) {
691 UI
.rfb
.set_onFBUComplete(function() { });
694 // Display the desktop name in the document title
695 updateDocumentTitle: function(rfb
, name
) {
696 document
.title
= name
+ " - noVNC";
699 clipReceive: function(rfb
, text
) {
700 Util
.Debug(">> UI.clipReceive: " + text
.substr(0,40) + "...");
701 $D('noVNC_clipboard_text').value
= text
;
702 Util
.Debug("<< UI.clipReceive");
705 connect: function() {
706 UI
.closeSettingsMenu();
707 UI
.toggleConnectPanel();
709 var host
= $D('noVNC_host').value
;
710 var port
= $D('noVNC_port').value
;
711 var password
= $D('noVNC_password').value
;
712 var path
= $D('noVNC_path').value
;
713 if ((!host
) || (!port
)) {
714 throw new Error("Must set host and port");
717 UI
.rfb
.set_encrypt(UI
.getSetting('encrypt'));
718 UI
.rfb
.set_true_color(UI
.getSetting('true_color'));
719 UI
.rfb
.set_local_cursor(UI
.getSetting('cursor'));
720 UI
.rfb
.set_shared(UI
.getSetting('shared'));
721 UI
.rfb
.set_view_only(UI
.getSetting('view_only'));
722 UI
.rfb
.set_repeaterID(UI
.getSetting('repeaterID'));
724 UI
.rfb
.connect(host
, port
, password
, path
);
727 setTimeout(UI
.setBarPosition
, 100);
728 $D('noVNC_logo').style
.display
= "none";
731 disconnect: function() {
732 UI
.closeSettingsMenu();
735 // Restore the callback used for initial resize
736 UI
.rfb
.set_onFBUComplete(UI
.FBUComplete
);
738 $D('noVNC_logo').style
.display
= "block";
739 UI
.connSettingsOpen
= false;
740 UI
.toggleConnectPanel();
743 displayBlur: function() {
744 UI
.rfb
.get_keyboard().set_focused(false);
745 UI
.rfb
.get_mouse().set_focused(false);
748 displayFocus: function() {
749 UI
.rfb
.get_keyboard().set_focused(true);
750 UI
.rfb
.get_mouse().set_focused(true);
753 clipClear: function() {
754 $D('noVNC_clipboard_text').value
= "";
755 UI
.rfb
.clipboardPasteFrom("");
758 clipSend: function() {
759 var text
= $D('noVNC_clipboard_text').value
;
760 Util
.Debug(">> UI.clipSend: " + text
.substr(0,40) + "...");
761 UI
.rfb
.clipboardPasteFrom(text
);
762 Util
.Debug("<< UI.clipSend");
765 // Enable/disable and configure viewport clipping
766 setViewClip: function(clip
) {
769 display
= UI
.rfb
.get_display();
774 var cur_clip
= display
.get_viewport();
776 if (typeof(clip
) !== 'boolean') {
777 // Use current setting
778 clip
= UI
.getSetting('clip');
781 if (clip
&& !cur_clip
) {
783 UI
.updateSetting('clip', true);
784 } else if (!clip
&& cur_clip
) {
786 UI
.updateSetting('clip', false);
787 display
.set_viewport(false);
788 $D('noVNC_canvas').style
.position
= 'static';
789 display
.viewportChangeSize();
791 if (UI
.getSetting('clip')) {
792 // If clipping, update clipping settings
793 $D('noVNC_canvas').style
.position
= 'absolute';
794 var pos
= Util
.getPosition($D('noVNC_canvas'));
795 var new_w
= window
.innerWidth
- pos
.x
;
796 var new_h
= window
.innerHeight
- pos
.y
;
797 display
.set_viewport(true);
798 display
.viewportChangeSize(new_w
, new_h
);
802 // Toggle/set/unset the viewport drag/move button
803 setViewDrag: function(drag
) {
804 if (!UI
.rfb
) { return; }
806 UI
.updateViewDragButton();
808 if (typeof(drag
) === "undefined" ||
809 typeof(drag
) === "object") {
810 // If not specified, then toggle
811 drag
= !UI
.rfb
.get_viewportDrag();
813 var vmb
= $D('noVNC_view_drag_button');
815 vmb
.className
= "noVNC_status_button_selected";
816 UI
.rfb
.set_viewportDrag(true);
818 vmb
.className
= "noVNC_status_button";
819 UI
.rfb
.set_viewportDrag(false);
823 updateViewDragButton: function() {
824 var vmb
= $D('noVNC_view_drag_button');
825 if (UI
.rfb_state
=== 'normal' &&
826 UI
.rfb
.get_display().get_viewport() &&
827 UI
.rfb
.get_display().fbuClip()) {
828 vmb
.style
.display
= "inline";
830 vmb
.style
.display
= "none";
834 // On touch devices, show the OS keyboard
835 showKeyboard: function() {
836 var kbi
= $D('keyboardinput');
837 var skb
= $D('showKeyboard');
838 var l
= kbi
.value
.length
;
839 if(UI
.keyboardVisible
=== false) {
841 try { kbi
.setSelectionRange(l
, l
); } // Move the caret to the end
842 catch (err
) {} // setSelectionRange is undefined in Google Chrome
843 UI
.keyboardVisible
= true;
844 skb
.className
= "noVNC_status_button_selected";
845 } else if(UI
.keyboardVisible
=== true) {
847 skb
.className
= "noVNC_status_button";
848 UI
.keyboardVisible
= false;
852 keepKeyboard: function() {
853 clearTimeout(UI
.hideKeyboardTimeout
);
854 if(UI
.keyboardVisible
=== true) {
855 $D('keyboardinput').focus();
856 $D('showKeyboard').className
= "noVNC_status_button_selected";
857 } else if(UI
.keyboardVisible
=== false) {
858 $D('keyboardinput').blur();
859 $D('showKeyboard').className
= "noVNC_status_button";
863 keyboardinputReset: function() {
864 var kbi
= $D('keyboardinput');
865 kbi
.value
= new Array(UI
.defaultKeyboardinputLen
).join("_");
866 UI
.lastKeyboardinput
= kbi
.value
;
869 // When normal keyboard events are left uncought, use the input events from
870 // the keyboardinput element instead and generate the corresponding key events.
871 // This code is required since some browsers on Android are inconsistent in
872 // sending keyCodes in the normal keyboard events when using on screen keyboards.
873 keyInput: function(event
) {
874 var newValue
= event
.target
.value
;
875 var oldValue
= UI
.lastKeyboardinput
;
879 // Try to check caret position since whitespace at the end
880 // will not be considered by value.length in some browsers
881 newLen
= Math
.max(event
.target
.selectionStart
, newValue
.length
);
883 // selectionStart is undefined in Google Chrome
884 newLen
= newValue
.length
;
886 var oldLen
= oldValue
.length
;
889 var inputs
= newLen
- oldLen
;
891 backspaces
= -inputs
;
896 // Compare the old string with the new to account for
897 // text-corrections or other input that modify existing text
899 for (i
= 0; i
< Math
.min(oldLen
, newLen
); i
++) {
900 if (newValue
.charAt(i
) != oldValue
.charAt(i
)) {
902 backspaces
= oldLen
- i
;
907 // Send the key events
908 for (i
= 0; i
< backspaces
; i
++) {
909 UI
.rfb
.sendKey(XK_BackSpace
);
911 for (i
= newLen
- inputs
; i
< newLen
; i
++) {
912 UI
.rfb
.sendKey(newValue
.charCodeAt(i
));
915 // Control the text content length in the keyboardinput element
916 if (newLen
> 2 * UI
.defaultKeyboardinputLen
) {
917 UI
.keyboardinputReset();
918 } else if (newLen
< 1) {
919 // There always have to be some text in the keyboardinput
920 // element with which backspace can interact.
921 UI
.keyboardinputReset();
922 // This sometimes causes the keyboard to disappear for a second
923 // but it is required for the android keyboard to recognize that
924 // text has been added to the field
926 // This has to be ran outside of the input handler in order to work
927 setTimeout(function() { UI
.keepKeyboard(); }, 0);
929 UI
.lastKeyboardinput
= newValue
;
933 keyInputBlur: function() {
934 $D('showKeyboard').className
= "noVNC_status_button";
935 //Weird bug in iOS if you change keyboardVisible
936 //here it does not actually occur so next time
937 //you click keyboard icon it doesnt work.
938 UI
.hideKeyboardTimeout
= setTimeout(function() { UI
.setKeyboard(); },100);
941 showExtraKeys: function() {
943 if(UI
.extraKeysVisible
=== false) {
944 $D('toggleCtrlButton').style
.display
= "inline";
945 $D('toggleAltButton').style
.display
= "inline";
946 $D('sendTabButton').style
.display
= "inline";
947 $D('sendEscButton').style
.display
= "inline";
948 $D('showExtraKeysButton').className
= "noVNC_status_button_selected";
949 UI
.extraKeysVisible
= true;
950 } else if(UI
.extraKeysVisible
=== true) {
951 $D('toggleCtrlButton').style
.display
= "";
952 $D('toggleAltButton').style
.display
= "";
953 $D('sendTabButton').style
.display
= "";
954 $D('sendEscButton').style
.display
= "";
955 $D('showExtraKeysButton').className
= "noVNC_status_button";
956 UI
.extraKeysVisible
= false;
960 toggleCtrl: function() {
962 if(UI
.ctrlOn
=== false) {
963 UI
.rfb
.sendKey(XK_Control_L
, true);
964 $D('toggleCtrlButton').className
= "noVNC_status_button_selected";
966 } else if(UI
.ctrlOn
=== true) {
967 UI
.rfb
.sendKey(XK_Control_L
, false);
968 $D('toggleCtrlButton').className
= "noVNC_status_button";
973 toggleAlt: function() {
975 if(UI
.altOn
=== false) {
976 UI
.rfb
.sendKey(XK_Alt_L
, true);
977 $D('toggleAltButton').className
= "noVNC_status_button_selected";
979 } else if(UI
.altOn
=== true) {
980 UI
.rfb
.sendKey(XK_Alt_L
, false);
981 $D('toggleAltButton').className
= "noVNC_status_button";
986 sendTab: function() {
988 UI
.rfb
.sendKey(XK_Tab
);
991 sendEsc: function() {
993 UI
.rfb
.sendKey(XK_Escape
);
996 setKeyboard: function() {
997 UI
.keyboardVisible
= false;
1000 // iOS < Version 5 does not support position fixed. Javascript workaround:
1001 setOnscroll: function() {
1002 window
.onscroll = function() {
1003 UI
.setBarPosition();
1007 setResize: function () {
1008 window
.onResize = function() {
1009 UI
.setBarPosition();
1013 //Helper to add options to dropdown.
1014 addOption: function(selectbox
, text
, value
) {
1015 var optn
= document
.createElement("OPTION");
1018 selectbox
.options
.add(optn
);
1021 setBarPosition: function() {
1022 $D('noVNC-control-bar').style
.top
= (window
.pageYOffset
) + 'px';
1023 $D('noVNC_mobile_buttons').style
.left
= (window
.pageXOffset
) + 'px';
1025 var vncwidth
= $D('noVNC_screen').style
.offsetWidth
;
1026 $D('noVNC-control-bar').style
.width
= vncwidth
+ 'px';