]> git.proxmox.com Git - mirror_novnc.git/blame - app/ui.js
Don't modify mouse or keyboard in view_only mode
[mirror_novnc.git] / app / ui.js
CommitLineData
15046f00
JM
1/*
2 * noVNC: HTML5 VNC client
d58f8b51 3 * Copyright (C) 2012 Joel Martin
777cb7a0 4 * Copyright (C) 2016 Samuel Mannehed for Cendio AB
6cba147d 5 * Copyright (C) 2016 Pierre Ossman for Cendio AB
1d728ace 6 * Licensed under MPL 2.0 (see LICENSE.txt)
15046f00
JM
7 *
8 * See README.md for usage and integration instructions.
9 */
43cf7bd8 10
bbbf42bb 11/* jslint white: false, browser: true */
ae510306
SR
12/* global window, document.getElementById, Util, WebUtil, RFB, Display */
13
14/* [module]
15 * import Util from "../core/util";
bd5340c7 16 * import KeyTable from "../core/input/keysym";
ae510306
SR
17 * import RFB from "../core/rfb";
18 * import Display from "../core/display";
19 * import WebUtil from "./webutil";
20 */
bbbf42bb
SR
21
22var UI;
23
24(function () {
25 "use strict";
26
ae510306 27 /* [begin skip-as-module] */
bbbf42bb 28 // Load supporting scripts
72bdd06e 29 WebUtil.load_scripts(
bd5340c7
SR
30 {'core': ["base64.js", "websock.js", "des.js", "input/keysymdef.js",
31 "input/xtscancodes.js", "input/util.js", "input/devices.js",
32 "display.js", "inflator.js", "rfb.js", "input/keysym.js"]});
ae510306 33
bbbf42bb 34 window.onscriptsload = function () { UI.load(); };
ae510306 35 /* [end skip-as-module] */
bbbf42bb 36
bd6874e0 37 UI = {
bbbf42bb 38
3bb12056
SM
39 connected: false,
40 desktopName: "",
529c64e1 41
045d9224 42 resizeTimeout: null,
ca5c74ad 43 statusTimeout: null,
529c64e1 44 hideKeyboardTimeout: null,
3f93a385
SM
45 idleControlbarTimeout: null,
46 closeControlbarTimeout: null,
529c64e1 47
04b399e2
SM
48 controlbarGrabbed: false,
49 controlbarDrag: false,
50 controlbarMouseDownClientY: 0,
51 controlbarMouseDownOffsetY: 0,
bbbf42bb 52 keyboardVisible: false,
529c64e1 53
bbbf42bb 54 isTouchDevice: false,
f620259b 55 isSafari: false,
a6357e82 56 rememberedClipSetting: null,
529c64e1 57 lastKeyboardinput: null,
58 defaultKeyboardinputLen: 100,
59
bbbf42bb
SR
60 // Setup rfb object, load settings from browser storage, then call
61 // UI.init to setup the UI/menus
0bd2cbac 62 load: function(callback) {
bbbf42bb
SR
63 WebUtil.initSettings(UI.start, callback);
64 },
65
66 // Render default UI and initialize settings menu
67 start: function(callback) {
0f6af1e3 68
69 // Setup global variables first
bbbf42bb 70 UI.isTouchDevice = 'ontouchstart' in document.documentElement;
0f6af1e3 71 UI.isSafari = (navigator.userAgent.indexOf('Safari') !== -1 &&
72 navigator.userAgent.indexOf('Chrome') === -1);
73
74 UI.initSettings();
75
6244e383 76 // Adapt the interface for touch screen devices
0f6af1e3 77 if (UI.isTouchDevice) {
6244e383 78 document.documentElement.classList.add("noVNC_touch");
0f6af1e3 79 // Remove the address bar
80 setTimeout(function() { window.scrollTo(0, 1); }, 100);
81 UI.forceSetting('clip', true);
82 } else {
83 UI.initSetting('clip', false);
84 }
85
86 // Setup and initialize event handlers
87 UI.setupWindowEvents();
88 UI.setupFullscreen();
89 UI.addControlbarHandlers();
90 UI.addTouchSpecificHandlers();
ebbec43a 91 UI.addExtraKeysHandlers();
0f6af1e3 92 UI.addXvpHandlers();
93 UI.addConnectionControlHandlers();
94 UI.addClipboardHandlers();
95 UI.addSettingsHandlers();
bbbf42bb 96
f9fff037 97 // Show the connect panel on first load unless autoconnecting
ed8cbe4e
PO
98 if (!autoconnect) {
99 UI.openConnectPanel();
100 }
0f6af1e3 101
f0d9ab96 102 UI.updateViewClip();
0f6af1e3 103
104 UI.updateVisualState();
105
106 document.getElementById('noVNC_setting_host').focus();
107
108 var autoconnect = WebUtil.getConfigVar('autoconnect', false);
109 if (autoconnect === 'true' || autoconnect == '1') {
110 autoconnect = true;
111 UI.connect();
112 } else {
113 autoconnect = false;
114 }
115
116 if (typeof callback === "function") {
117 callback(UI.rfb);
118 }
119 },
120
121 initSettings: function() {
bbbf42bb
SR
122 // Stylesheet selection dropdown
123 var sheet = WebUtil.selectStylesheet();
124 var sheets = WebUtil.getStylesheets();
125 var i;
126 for (i = 0; i < sheets.length; i += 1) {
ae510306 127 UI.addOption(document.getElementById('noVNC_setting_stylesheet'),sheets[i].title, sheets[i].title);
bbbf42bb
SR
128 }
129
130 // Logging selection dropdown
131 var llevels = ['error', 'warn', 'info', 'debug'];
132 for (i = 0; i < llevels.length; i += 1) {
ae510306 133 UI.addOption(document.getElementById('noVNC_setting_logging'),llevels[i], llevels[i]);
bbbf42bb
SR
134 }
135
136 // Settings with immediate effects
137 UI.initSetting('logging', 'warn');
138 WebUtil.init_logging(UI.getSetting('logging'));
139
140 UI.initSetting('stylesheet', 'default');
141 WebUtil.selectStylesheet(null);
142 // call twice to get around webkit bug
143 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
144
145 // if port == 80 (or 443) then it won't be present and should be
146 // set manually
147 var port = window.location.port;
148 if (!port) {
149 if (window.location.protocol.substring(0,5) == 'https') {
150 port = 443;
151 }
152 else if (window.location.protocol.substring(0,4) == 'http') {
153 port = 80;
154 }
155 }
156
157 /* Populate the controls if defaults are provided in the URL */
158 UI.initSetting('host', window.location.hostname);
159 UI.initSetting('port', port);
160 UI.initSetting('password', '');
161 UI.initSetting('encrypt', (window.location.protocol === "https:"));
162 UI.initSetting('true_color', true);
163 UI.initSetting('cursor', !UI.isTouchDevice);
8b46c0de 164 UI.initSetting('resize', 'off');
bbbf42bb
SR
165 UI.initSetting('shared', true);
166 UI.initSetting('view_only', false);
167 UI.initSetting('path', 'websockify');
168 UI.initSetting('repeaterID', '');
c55f05f6 169 UI.initSetting('token', '');
0f6af1e3 170 },
bbbf42bb 171
0f6af1e3 172 setupWindowEvents: function() {
173 window.addEventListener( 'resize', function () {
777cb7a0 174 UI.applyResizeMode();
f0d9ab96 175 UI.updateViewClip();
31ddaa1c 176 UI.updateViewDrag();
f8b399d7 177 } );
ca5c74ad 178
179 document.getElementById("noVNC_status")
180 .addEventListener('click', UI.hideStatus);
bbbf42bb
SR
181 },
182
0f6af1e3 183 setupFullscreen: function() {
184 // Only show the button if fullscreen is properly supported
185 // * Safari doesn't support alphanumerical input while in fullscreen
186 if (!UI.isSafari &&
187 (document.documentElement.requestFullscreen ||
188 document.documentElement.mozRequestFullScreen ||
189 document.documentElement.webkitRequestFullscreen ||
190 document.body.msRequestFullscreen)) {
e40978c7
PO
191 document.getElementById('noVNC_fullscreen_button')
192 .classList.remove("noVNC_hidden");
0f6af1e3 193 UI.addFullscreenHandlers();
d9fc1c7b 194 }
e543525f
SR
195 },
196
0f6af1e3 197 addControlbarHandlers: function() {
728b5d9e
PO
198 document.getElementById("noVNC_control_bar")
199 .addEventListener('mousemove', UI.activateControlbar);
200 document.getElementById("noVNC_control_bar")
201 .addEventListener('mouseup', UI.activateControlbar);
202 document.getElementById("noVNC_control_bar")
203 .addEventListener('mousedown', UI.activateControlbar);
204 document.getElementById("noVNC_control_bar")
205 .addEventListener('keypress', UI.activateControlbar);
206
3f93a385
SM
207 document.getElementById("noVNC_control_bar")
208 .addEventListener('mousedown', UI.keepControlbar);
209 document.getElementById("noVNC_control_bar")
210 .addEventListener('keypress', UI.keepControlbar);
211
d7f79071 212 document.getElementById("noVNC_view_drag_button")
213 .addEventListener('click', UI.toggleViewDrag);
04b399e2
SM
214
215 document.getElementById("noVNC_control_bar_handle")
216 .addEventListener('mousedown', UI.controlbarHandleMouseDown);
217 document.getElementById("noVNC_control_bar_handle")
218 .addEventListener('mouseup', UI.controlbarHandleMouseUp);
219 document.getElementById("noVNC_control_bar_handle")
220 .addEventListener('mousemove', UI.dragControlbarHandle);
221 // resize events aren't available for elements
222 window.addEventListener('resize', UI.updateControlbarHandle);
0f6af1e3 223 },
224
225 addTouchSpecificHandlers: function() {
d7f79071 226 document.getElementById("noVNC_mouse_button0")
227 .addEventListener('click', function () { UI.setMouseButton(1); });
228 document.getElementById("noVNC_mouse_button1")
229 .addEventListener('click', function () { UI.setMouseButton(2); });
230 document.getElementById("noVNC_mouse_button2")
231 .addEventListener('click', function () { UI.setMouseButton(4); });
232 document.getElementById("noVNC_mouse_button4")
233 .addEventListener('click', function () { UI.setMouseButton(0); });
234 document.getElementById("noVNC_keyboard_button")
4b30f9ce 235 .addEventListener('click', UI.toggleVirtualKeyboard);
d7f79071 236
237 document.getElementById("noVNC_keyboardinput")
238 .addEventListener('input', UI.keyInput);
239 document.getElementById("noVNC_keyboardinput")
4b30f9ce 240 .addEventListener('blur', UI.onblurVirtualKeyboard);
d7f79071 241 document.getElementById("noVNC_keyboardinput")
242 .addEventListener('submit', function () { return false; });
243
728b5d9e
PO
244 document.getElementById("noVNC_control_bar")
245 .addEventListener('touchstart', UI.activateControlbar);
246 document.getElementById("noVNC_control_bar")
247 .addEventListener('touchmove', UI.activateControlbar);
248 document.getElementById("noVNC_control_bar")
249 .addEventListener('touchend', UI.activateControlbar);
250 document.getElementById("noVNC_control_bar")
251 .addEventListener('input', UI.activateControlbar);
252
3f93a385
SM
253 document.getElementById("noVNC_control_bar")
254 .addEventListener('touchstart', UI.keepControlbar);
255 document.getElementById("noVNC_control_bar")
256 .addEventListener('input', UI.keepControlbar);
257
04b399e2
SM
258 document.getElementById("noVNC_control_bar_handle")
259 .addEventListener('touchstart', UI.controlbarHandleMouseDown);
260 document.getElementById("noVNC_control_bar_handle")
261 .addEventListener('touchend', UI.controlbarHandleMouseUp);
262 document.getElementById("noVNC_control_bar_handle")
263 .addEventListener('touchmove', UI.dragControlbarHandle);
264
0f6af1e3 265 window.addEventListener('load', UI.keyboardinputReset);
ebbec43a 266 },
0f6af1e3 267
ebbec43a 268 addExtraKeysHandlers: function() {
a49d9298 269 document.getElementById("noVNC_toggle_extra_keys_button")
d7f79071 270 .addEventListener('click', UI.toggleExtraKeys);
a49d9298 271 document.getElementById("noVNC_toggle_ctrl_button")
d7f79071 272 .addEventListener('click', UI.toggleCtrl);
a49d9298 273 document.getElementById("noVNC_toggle_alt_button")
d7f79071 274 .addEventListener('click', UI.toggleAlt);
a49d9298 275 document.getElementById("noVNC_send_tab_button")
d7f79071 276 .addEventListener('click', UI.sendTab);
a49d9298 277 document.getElementById("noVNC_send_esc_button")
d7f79071 278 .addEventListener('click', UI.sendEsc);
ca25d2ae
PO
279 document.getElementById("noVNC_send_ctrl_alt_del_button")
280 .addEventListener('click', UI.sendCtrlAltDel);
0f6af1e3 281 },
d7f79071 282
0f6af1e3 283 addXvpHandlers: function() {
a49d9298 284 document.getElementById("noVNC_xvp_shutdown_button")
d7f79071 285 .addEventListener('click', function() { UI.rfb.xvpShutdown(); });
a49d9298 286 document.getElementById("noVNC_xvp_reboot_button")
d7f79071 287 .addEventListener('click', function() { UI.rfb.xvpReboot(); });
a49d9298 288 document.getElementById("noVNC_xvp_reset_button")
d7f79071 289 .addEventListener('click', function() { UI.rfb.xvpReset(); });
a49d9298 290 document.getElementById("noVNC_xvp_button")
d7f79071 291 .addEventListener('click', UI.toggleXvpPanel);
0f6af1e3 292 },
293
294 addConnectionControlHandlers: function() {
a49d9298 295 document.getElementById("noVNC_connect_controls_button")
d7f79071 296 .addEventListener('click', UI.toggleConnectPanel);
297 document.getElementById("noVNC_disconnect_button")
298 .addEventListener('click', UI.disconnect);
0f6af1e3 299 document.getElementById("noVNC_connect_button")
300 .addEventListener('click', UI.connect);
8a7ec6ea
SM
301
302 document.getElementById("noVNC_password_button")
303 .addEventListener('click', UI.setPassword);
0f6af1e3 304 },
d7f79071 305
0f6af1e3 306 addClipboardHandlers: function() {
307 document.getElementById("noVNC_clipboard_button")
308 .addEventListener('click', UI.toggleClipboardPanel);
d7f79071 309 document.getElementById("noVNC_clipboard_text")
310 .addEventListener('focus', UI.displayBlur);
311 document.getElementById("noVNC_clipboard_text")
312 .addEventListener('blur', UI.displayFocus);
313 document.getElementById("noVNC_clipboard_text")
314 .addEventListener('change', UI.clipboardSend);
315 document.getElementById("noVNC_clipboard_clear_button")
316 .addEventListener('click', UI.clipboardClear);
0f6af1e3 317 },
d7f79071 318
0f6af1e3 319 addSettingsHandlers: function() {
320 document.getElementById("noVNC_settings_button")
321 .addEventListener('click', UI.toggleSettingsPanel);
d7f79071 322 document.getElementById("noVNC_settings_apply")
323 .addEventListener('click', UI.settingsApply);
324
d7f79071 325 document.getElementById("noVNC_setting_resize")
326 .addEventListener('change', UI.enableDisableViewClip);
bbbf42bb
SR
327 },
328
0f6af1e3 329 addFullscreenHandlers: function() {
330 document.getElementById("noVNC_fullscreen_button")
331 .addEventListener('click', UI.toggleFullscreen);
332
333 window.addEventListener('fullscreenchange', UI.updateFullscreenButton);
334 window.addEventListener('mozfullscreenchange', UI.updateFullscreenButton);
335 window.addEventListener('webkitfullscreenchange', UI.updateFullscreenButton);
336 window.addEventListener('msfullscreenchange', UI.updateFullscreenButton);
337 },
338
339 initRFB: function() {
340 try {
341 UI.rfb = new RFB({'target': document.getElementById('noVNC_canvas'),
a7127fee 342 'onNotification': UI.notification,
0f6af1e3 343 'onUpdateState': UI.updateState,
3bb12056 344 'onDisconnected': UI.disconnectFinished,
7d714b15 345 'onPasswordRequired': UI.passwordRequired,
0f6af1e3 346 'onXvpInit': UI.updateXvpButton,
347 'onClipboard': UI.clipboardReceive,
63bf2ba5 348 'onBell': UI.bell,
0f6af1e3 349 'onFBUComplete': UI.initialResize,
350 'onFBResize': UI.updateViewDrag,
3bb12056 351 'onDesktopName': UI.updateDesktopName});
0f6af1e3 352 return true;
353 } catch (exc) {
301dc0e2
SM
354 var msg = 'Unable to create RFB client -- ' + exc;
355 Util.Error(msg);
356 UI.showStatus(msg, 'error');
0f6af1e3 357 return false;
358 }
359 },
360
95dd6001 361/* ------^-------
362 * /INIT
363 * ==============
364 * VISUAL
365 * ------v------*/
58ded70d 366
3bb12056
SM
367 updateState: function(rfb, state, oldstate) {
368 switch (state) {
369 case 'connecting':
370 UI.showStatus("Connecting");
371 break;
372 case 'connected':
373 UI.connected = true;
374 if (rfb && rfb.get_encrypt()) {
375 UI.showStatus("Connected (encrypted) to " +
376 UI.desktopName);
377 } else {
378 UI.showStatus("Connected (unencrypted) to " +
379 UI.desktopName);
380 }
381 break;
382 case 'disconnecting':
383 UI.showStatus("Disconnecting");
384 break;
385 case 'disconnected':
386 UI.connected = false;
387 UI.showStatus("Disconnected");
388 break;
389 default:
390 UI.showStatus("Invalid state", 'error');
391 break;
fdedbafb 392 }
29475d77 393
394 UI.updateVisualState();
fdedbafb 395 },
396
29475d77 397 // Disable/enable controls depending on connection state
398 updateVisualState: function() {
29475d77 399 //Util.Debug(">> updateVisualState");
3bb12056
SM
400 document.getElementById('noVNC_setting_encrypt').disabled = UI.connected;
401 document.getElementById('noVNC_setting_true_color').disabled = UI.connected;
29475d77 402 if (Util.browserSupportsCursorURIs()) {
3bb12056 403 document.getElementById('noVNC_setting_cursor').disabled = UI.connected;
29475d77 404 } else {
405 UI.updateSetting('cursor', !UI.isTouchDevice);
ae510306 406 document.getElementById('noVNC_setting_cursor').disabled = true;
29475d77 407 }
fdedbafb 408
29475d77 409 UI.enableDisableViewClip();
3bb12056
SM
410 document.getElementById('noVNC_setting_resize').disabled = UI.connected;
411 document.getElementById('noVNC_setting_shared').disabled = UI.connected;
412 document.getElementById('noVNC_setting_view_only').disabled = UI.connected;
413 document.getElementById('noVNC_setting_path').disabled = UI.connected;
414 document.getElementById('noVNC_setting_repeaterID').disabled = UI.connected;
fdedbafb 415
3bb12056 416 if (UI.connected) {
6244e383 417 document.documentElement.classList.add("noVNC_connected");
f0d9ab96 418 UI.updateViewClip();
29475d77 419 UI.setMouseButton(1);
3f93a385
SM
420
421 // Hide the controlbar after 2 seconds
422 UI.closeControlbarTimeout = setTimeout(UI.closeControlbar, 2000);
29475d77 423 } else {
6244e383 424 document.documentElement.classList.remove("noVNC_connected");
9e45354e 425 UI.updateXvpButton(0);
7520ba52 426 UI.keepControlbar();
29475d77 427 }
fdedbafb 428
29475d77 429 // State change disables viewport dragging.
430 // It is enabled (toggled) by direct click on the button
f0d9ab96 431 UI.setViewDrag(false);
29475d77 432
8a7ec6ea
SM
433 // State change also closes the password dialog
434 document.getElementById('noVNC_password_dlg')
435 .classList.remove('noVNC_open');
436
29475d77 437 //Util.Debug("<< updateVisualState");
438 },
439
8d7708c8 440 showStatus: function(text, status_type, time) {
ca5c74ad 441 var statusElem = document.getElementById('noVNC_status');
4e471b5b 442
ca5c74ad 443 clearTimeout(UI.statusTimeout);
4e471b5b 444
8d7708c8
SM
445 if (typeof status_type === 'undefined') {
446 status_type = 'normal';
447 }
448
449 statusElem.classList.remove("noVNC_status_normal",
450 "noVNC_status_warn",
451 "noVNC_status_error");
452
453 switch (status_type) {
454 case 'warning':
455 case 'warn':
456 statusElem.classList.add("noVNC_status_warn");
457 break;
458 case 'error':
459 statusElem.classList.add("noVNC_status_error");
460 break;
461 case 'normal':
462 case 'info':
463 default:
464 statusElem.classList.add("noVNC_status_normal");
465 break;
466 }
467
ca5c74ad 468 statusElem.innerHTML = text;
469 statusElem.classList.add("noVNC_open");
470
471 // If no time was specified, show the status for 1.5 seconds
472 if (typeof time === 'undefined') {
473 time = 1500;
fdedbafb 474 }
4e471b5b 475
74a4a2b4
SM
476 // Error messages do not timeout
477 if (status_type !== 'error') {
ca5c74ad 478 UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
479 }
fdedbafb 480 },
481
ca5c74ad 482 hideStatus: function() {
483 clearTimeout(UI.statusTimeout);
484 document.getElementById('noVNC_status').classList.remove("noVNC_open");
4e471b5b 485 },
486
a7127fee
SM
487 notification: function (rfb, msg, level, options) {
488 UI.showStatus(msg, level);
489 },
490
3f93a385
SM
491 activateControlbar: function(event) {
492 clearTimeout(UI.idleControlbarTimeout);
728b5d9e
PO
493 // We manipulate the anchor instead of the actual control
494 // bar in order to avoid creating new a stacking group
495 document.getElementById('noVNC_control_bar_anchor')
496 .classList.remove("noVNC_idle");
3f93a385 497 UI.idleControlbarTimeout = window.setTimeout(UI.idleControlbar, 2000);
728b5d9e
PO
498 },
499
500 idleControlbar: function() {
501 document.getElementById('noVNC_control_bar_anchor')
502 .classList.add("noVNC_idle");
503 },
504
3f93a385
SM
505 keepControlbar: function() {
506 clearTimeout(UI.closeControlbarTimeout);
507 },
508
38323d4d
PO
509 openControlbar: function() {
510 document.getElementById('noVNC_control_bar')
511 .classList.add("noVNC_open");
512 },
513
514 closeControlbar: function() {
515 UI.closeAllPanels();
516 document.getElementById('noVNC_control_bar')
517 .classList.remove("noVNC_open");
518 },
519
520 toggleControlbar: function() {
521 if (document.getElementById('noVNC_control_bar')
522 .classList.contains("noVNC_open")) {
523 UI.closeControlbar();
524 } else {
525 UI.openControlbar();
526 }
527 },
528
04b399e2
SM
529 dragControlbarHandle: function (e) {
530 if (!UI.controlbarGrabbed) return;
531
532 var ptr = Util.getPointerEvent(e);
533
534 if (!UI.controlbarDrag) {
535 // The goal is to trigger on a certain physical width, the
536 // devicePixelRatio brings us a bit closer but is not optimal.
537 var dragThreshold = 10 * (window.devicePixelRatio || 1);
538 var dragDistance = Math.abs(ptr.clientY - UI.controlbarMouseDownClientY);
539
540 if (dragDistance < dragThreshold) return;
541
542 UI.controlbarDrag = true;
543 }
544
545 var eventY = ptr.clientY - UI.controlbarMouseDownOffsetY;
546
547 UI.moveControlbarHandle(eventY);
548
549 e.preventDefault();
550 e.stopPropagation();
551 },
552
553 // Move the handle but don't allow any position outside the bounds
554 moveControlbarHandle: function (posY) {
555 var handle = document.getElementById("noVNC_control_bar_handle");
556 var handleHeight = Util.getPosition(handle).height;
557 var controlbar = document.getElementById("noVNC_control_bar");
558 var controlbarBounds = Util.getPosition(controlbar);
559 var controlbarTop = controlbarBounds.y;
560 var controlbarBottom = controlbarBounds.y + controlbarBounds.height;
561 var margin = 10;
562
563 var viewportY = posY;
564
565 // Refuse coordinates outside the control bar
566 if (viewportY < controlbarTop + margin) {
567 viewportY = controlbarTop + margin;
568 } else if (viewportY > controlbarBottom - handleHeight - margin) {
569 viewportY = controlbarBottom - handleHeight - margin;
570 }
571
572 // Corner case: control bar too small for stable position
573 if (controlbarBounds.height < (handleHeight + margin * 2)) {
574 viewportY = controlbarTop + (controlbarBounds.height - handleHeight) / 2;
575 }
576
577 var relativeY = viewportY - controlbarTop;
578 handle.style.transform = "translateY(" + relativeY + "px)";
579 },
580
581 updateControlbarHandle: function () {
582 var handle = document.getElementById("noVNC_control_bar_handle");
583 var pos = Util.getPosition(handle);
584 UI.moveControlbarHandle(pos.y);
585 },
586
587 controlbarHandleMouseUp: function(e) {
1c1cc1d0 588 if ((e.type == "mouseup") && (e.button != 0)) return;
04b399e2
SM
589
590 // mouseup and mousedown on the same place toggles the controlbar
591 if (UI.controlbarGrabbed && !UI.controlbarDrag) {
592 UI.toggleControlbar();
593 e.preventDefault();
594 e.stopPropagation();
595 }
596 UI.controlbarGrabbed = false;
597 },
598
599 controlbarHandleMouseDown: function(e) {
1c1cc1d0 600 if ((e.type == "mousedown") && (e.button != 0)) return;
04b399e2
SM
601
602 var ptr = Util.getPointerEvent(e);
603
604 var handle = document.getElementById("noVNC_control_bar_handle");
605 var bounds = handle.getBoundingClientRect();
606
607 WebUtil.setCapture(handle);
608 UI.controlbarGrabbed = true;
609 UI.controlbarDrag = false;
610
611 UI.controlbarMouseDownClientY = ptr.clientY;
612 UI.controlbarMouseDownOffsetY = ptr.clientY - bounds.top;
613 e.preventDefault();
614 e.stopPropagation();
615 },
616
95dd6001 617/* ------^-------
618 * /VISUAL
619 * ==============
620 * SETTINGS
621 * ------v------*/
622
45c70c9e 623 // Initial page load read/initialization of settings
624 initSetting: function(name, defVal) {
625 // Check Query string followed by cookie
626 var val = WebUtil.getConfigVar(name);
627 if (val === null) {
628 val = WebUtil.readSetting(name, defVal);
bbbf42bb 629 }
45c70c9e 630 UI.updateSetting(name, val);
bbbf42bb
SR
631 return val;
632 },
633
634 // Update cookie and form control setting. If value is not set, then
635 // updates from control to current cookie setting.
636 updateSetting: function(name, value) {
637
638 // Save the cookie for this session
639 if (typeof value !== 'undefined') {
640 WebUtil.writeSetting(name, value);
641 }
642
643 // Update the settings control
644 value = UI.getSetting(name);
645
ae510306 646 var ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
647 if (ctrl.type === 'checkbox') {
648 ctrl.checked = value;
649
650 } else if (typeof ctrl.options !== 'undefined') {
651 for (var i = 0; i < ctrl.options.length; i += 1) {
652 if (ctrl.options[i].value === value) {
653 ctrl.selectedIndex = i;
654 break;
655 }
656 }
657 } else {
658 /*Weird IE9 error leads to 'null' appearring
659 in textboxes instead of ''.*/
660 if (value === null) {
661 value = "";
662 }
663 ctrl.value = value;
664 }
665 },
666
667 // Save control setting to cookie
668 saveSetting: function(name) {
ae510306 669 var val, ctrl = document.getElementById('noVNC_setting_' + name);
bbbf42bb
SR
670 if (ctrl.type === 'checkbox') {
671 val = ctrl.checked;
672 } else if (typeof ctrl.options !== 'undefined') {
673 val = ctrl.options[ctrl.selectedIndex].value;
674 } else {
675 val = ctrl.value;
676 }
677 WebUtil.writeSetting(name, val);
678 //Util.Debug("Setting saved '" + name + "=" + val + "'");
679 return val;
680 },
681
bbbf42bb
SR
682 // Force a setting to be a certain value
683 forceSetting: function(name, val) {
684 UI.updateSetting(name, val);
685 return val;
686 },
687
45c70c9e 688 // Read form control compatible setting from cookie
689 getSetting: function(name) {
ae510306 690 var ctrl = document.getElementById('noVNC_setting_' + name);
45c70c9e 691 var val = WebUtil.readSetting(name);
692 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
693 if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
694 val = false;
4f19e5c6 695 } else {
45c70c9e 696 val = true;
4f19e5c6 697 }
bbbf42bb 698 }
bbbf42bb 699 return val;
bbbf42bb
SR
700 },
701
95dd6001 702 // Save/apply settings when 'Apply' button is pressed
703 settingsApply: function() {
704 //Util.Debug(">> settingsApply");
705 UI.saveSetting('encrypt');
706 UI.saveSetting('true_color');
707 if (Util.browserSupportsCursorURIs()) {
708 UI.saveSetting('cursor');
bbbf42bb 709 }
bbbf42bb 710
95dd6001 711 UI.saveSetting('resize');
bbbf42bb 712
95dd6001 713 if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
714 UI.forceSetting('clip', false);
7d1dc09a 715 }
7d1dc09a 716
95dd6001 717 UI.saveSetting('clip');
718 UI.saveSetting('shared');
719 UI.saveSetting('view_only');
720 UI.saveSetting('path');
721 UI.saveSetting('repeaterID');
722 UI.saveSetting('stylesheet');
723 UI.saveSetting('logging');
724
725 // Settings with immediate (non-connected related) effect
726 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
727 WebUtil.init_logging(UI.getSetting('logging'));
f0d9ab96 728 UI.updateViewClip();
95dd6001 729 UI.updateViewDrag();
730 //Util.Debug("<< settingsApply");
7d1dc09a 731 },
732
ed8cbe4e
PO
733/* ------^-------
734 * /SETTINGS
735 * ==============
736 * PANELS
737 * ------v------*/
738
739 closeAllPanels: function() {
740 UI.closeSettingsPanel();
741 UI.closeXvpPanel();
742 UI.closeClipboardPanel();
743 UI.closeConnectPanel();
fb7c3b3b 744 UI.closeExtraKeys();
ed8cbe4e
PO
745 },
746
747/* ------^-------
748 * /PANELS
749 * ==============
750 * SETTINGS (panel)
751 * ------v------*/
752
753 openSettingsPanel: function() {
754 UI.closeAllPanels();
38323d4d 755 UI.openControlbar();
ed8cbe4e
PO
756
757 UI.updateSetting('encrypt');
758 UI.updateSetting('true_color');
759 if (Util.browserSupportsCursorURIs()) {
760 UI.updateSetting('cursor');
761 } else {
762 UI.updateSetting('cursor', !UI.isTouchDevice);
763 document.getElementById('noVNC_setting_cursor').disabled = true;
bbbf42bb 764 }
ed8cbe4e
PO
765 UI.updateSetting('clip');
766 UI.updateSetting('resize');
767 UI.updateSetting('shared');
768 UI.updateSetting('view_only');
769 UI.updateSetting('path');
770 UI.updateSetting('repeaterID');
771 UI.updateSetting('stylesheet');
772 UI.updateSetting('logging');
773
e40978c7
PO
774 document.getElementById('noVNC_settings')
775 .classList.add("noVNC_open");
776 document.getElementById('noVNC_settings_button')
d9e86214 777 .classList.add("noVNC_selected");
45c70c9e 778 },
bbbf42bb 779
ed8cbe4e 780 closeSettingsPanel: function() {
e40978c7
PO
781 document.getElementById('noVNC_settings')
782 .classList.remove("noVNC_open");
783 document.getElementById('noVNC_settings_button')
d9e86214 784 .classList.remove("noVNC_selected");
bbbf42bb
SR
785 },
786
787 // Toggle the settings menu:
788 // On open, settings are refreshed from saved cookies.
789 // On close, settings are applied
790 toggleSettingsPanel: function() {
ed8cbe4e
PO
791 if (document.getElementById('noVNC_settings')
792 .classList.contains("noVNC_open")) {
bbbf42bb 793 UI.settingsApply();
ed8cbe4e 794 UI.closeSettingsPanel();
bbbf42bb 795 } else {
ed8cbe4e 796 UI.openSettingsPanel();
bbbf42bb
SR
797 }
798 },
799
95dd6001 800/* ------^-------
801 * /SETTINGS
802 * ==============
803 * XVP
804 * ------v------*/
805
ed8cbe4e
PO
806 openXvpPanel: function() {
807 UI.closeAllPanels();
38323d4d 808 UI.openControlbar();
ed8cbe4e
PO
809
810 document.getElementById('noVNC_xvp')
811 .classList.add("noVNC_open");
812 document.getElementById('noVNC_xvp_button')
813 .classList.add("noVNC_selected");
814 },
815
816 closeXvpPanel: function() {
817 document.getElementById('noVNC_xvp')
818 .classList.remove("noVNC_open");
819 document.getElementById('noVNC_xvp_button')
820 .classList.remove("noVNC_selected");
821 },
822
bbbf42bb 823 toggleXvpPanel: function() {
ed8cbe4e
PO
824 if (document.getElementById('noVNC_xvp')
825 .classList.contains("noVNC_open")) {
826 UI.closeXvpPanel();
bbbf42bb 827 } else {
ed8cbe4e 828 UI.openXvpPanel();
bbbf42bb 829 }
bbbf42bb
SR
830 },
831
832 // Disable/enable XVP button
9e45354e 833 updateXvpButton: function(ver) {
bbbf42bb 834 if (ver >= 1) {
a49d9298 835 document.getElementById('noVNC_xvp_button')
e40978c7 836 .classList.remove("noVNC_hidden");
bbbf42bb 837 } else {
a49d9298 838 document.getElementById('noVNC_xvp_button')
e40978c7 839 .classList.add("noVNC_hidden");
bbbf42bb 840 // Close XVP panel if open
ed8cbe4e 841 UI.closeXvpPanel();
bbbf42bb
SR
842 }
843 },
844
95dd6001 845/* ------^-------
846 * /XVP
847 * ==============
848 * CLIPBOARD
849 * ------v------*/
f8b399d7 850
ed8cbe4e
PO
851 openClipboardPanel: function() {
852 UI.closeAllPanels();
38323d4d 853 UI.openControlbar();
ed8cbe4e
PO
854
855 document.getElementById('noVNC_clipboard')
856 .classList.add("noVNC_open");
857 document.getElementById('noVNC_clipboard_button')
858 .classList.add("noVNC_selected");
859 },
860
861 closeClipboardPanel: function() {
862 document.getElementById('noVNC_clipboard')
863 .classList.remove("noVNC_open");
864 document.getElementById('noVNC_clipboard_button')
865 .classList.remove("noVNC_selected");
866 },
867
bbbf42bb 868 toggleClipboardPanel: function() {
ed8cbe4e
PO
869 if (document.getElementById('noVNC_clipboard')
870 .classList.contains("noVNC_open")) {
871 UI.closeClipboardPanel();
bbbf42bb 872 } else {
ed8cbe4e 873 UI.openClipboardPanel();
bbbf42bb 874 }
bbbf42bb
SR
875 },
876
4d26f58e 877 clipboardReceive: function(rfb, text) {
878 Util.Debug(">> UI.clipboardReceive: " + text.substr(0,40) + "...");
ae510306 879 document.getElementById('noVNC_clipboard_text').value = text;
4d26f58e 880 Util.Debug("<< UI.clipboardReceive");
95dd6001 881 },
882
4d26f58e 883 clipboardClear: function() {
ae510306 884 document.getElementById('noVNC_clipboard_text').value = "";
95dd6001 885 UI.rfb.clipboardPasteFrom("");
886 },
887
4d26f58e 888 clipboardSend: function() {
ae510306 889 var text = document.getElementById('noVNC_clipboard_text').value;
4d26f58e 890 Util.Debug(">> UI.clipboardSend: " + text.substr(0,40) + "...");
95dd6001 891 UI.rfb.clipboardPasteFrom(text);
4d26f58e 892 Util.Debug("<< UI.clipboardSend");
95dd6001 893 },
894
895/* ------^-------
896 * /CLIPBOARD
897 * ==============
898 * CONNECTION
899 * ------v------*/
900
ed8cbe4e
PO
901 openConnectPanel: function() {
902 UI.closeAllPanels();
38323d4d 903 UI.openControlbar();
ab81ddf5 904
ed8cbe4e
PO
905 document.getElementById('noVNC_connect_controls')
906 .classList.add("noVNC_open");
907 document.getElementById('noVNC_connect_controls_button')
908 .classList.add("noVNC_selected");
909
910 document.getElementById('noVNC_setting_host').focus();
911 },
912
913 closeConnectPanel: function() {
914 document.getElementById('noVNC_connect_controls')
915 .classList.remove("noVNC_open");
916 document.getElementById('noVNC_connect_controls_button')
917 .classList.remove("noVNC_selected");
918
919 UI.saveSetting('host');
920 UI.saveSetting('port');
921 UI.saveSetting('token');
922 //UI.saveSetting('password');
923 },
924
925 toggleConnectPanel: function() {
926 if (document.getElementById('noVNC_connect_controls')
927 .classList.contains("noVNC_open")) {
928 UI.closeConnectPanel();
ab81ddf5 929 } else {
ed8cbe4e 930 UI.openConnectPanel();
ab81ddf5 931 }
bbbf42bb
SR
932 },
933
934 connect: function() {
ae510306
SR
935 var host = document.getElementById('noVNC_setting_host').value;
936 var port = document.getElementById('noVNC_setting_port').value;
937 var password = document.getElementById('noVNC_setting_password').value;
938 var token = document.getElementById('noVNC_setting_token').value;
939 var path = document.getElementById('noVNC_setting_path').value;
c55f05f6
MXPN
940
941 //if token is in path then ignore the new token variable
942 if (token) {
943 path = WebUtil.injectParamIfMissing(path, "token", token);
944 }
945
bbbf42bb 946 if ((!host) || (!port)) {
3bb12056
SM
947 UI.showStatus("Must set host and port", 'error');
948 return;
bbbf42bb 949 }
53fc7392 950
d9fc1c7b 951 if (!UI.initRFB()) return;
58ded70d 952
4102b71c
SM
953 UI.closeAllPanels();
954
bbbf42bb
SR
955 UI.rfb.set_encrypt(UI.getSetting('encrypt'));
956 UI.rfb.set_true_color(UI.getSetting('true_color'));
957 UI.rfb.set_local_cursor(UI.getSetting('cursor'));
958 UI.rfb.set_shared(UI.getSetting('shared'));
959 UI.rfb.set_view_only(UI.getSetting('view_only'));
960 UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
53fc7392 961
bbbf42bb 962 UI.rfb.connect(host, port, password, path);
bbbf42bb 963 },
5299db1a 964
bbbf42bb 965 disconnect: function() {
ed8cbe4e 966 UI.closeAllPanels();
bbbf42bb 967 UI.rfb.disconnect();
8e0f0088 968
f8b399d7 969 // Restore the callback used for initial resize
ab81ddf5 970 UI.rfb.set_onFBUComplete(UI.initialResize);
f8b399d7 971
e543525f 972 // Don't display the connection settings until we're actually disconnected
bbbf42bb
SR
973 },
974
3bb12056
SM
975 disconnectFinished: function (rfb, reason) {
976 if (typeof reason !== 'undefined') {
977 UI.showStatus(reason, 'error');
978 }
979 UI.openConnectPanel();
980 },
981
7d714b15
SM
982/* ------^-------
983 * /CONNECTION
984 * ==============
985 * PASSWORD
986 * ------v------*/
987
988 passwordRequired: function(rfb, msg) {
989
990 document.getElementById('noVNC_password_dlg')
991 .classList.add('noVNC_open');
992
993 setTimeout(function () {
994 document.getElementById('noVNC_password_input').focus();
995 }, 100);
996
997 if (typeof msg === 'undefined') {
998 msg = "Password is required";
999 }
3bb12056 1000 UI.showStatus(msg, "warning");
7d714b15
SM
1001 },
1002
95dd6001 1003 setPassword: function() {
8a7ec6ea
SM
1004 UI.rfb.sendPassword(document.getElementById('noVNC_password_input').value);
1005 document.getElementById('noVNC_password_dlg')
1006 .classList.remove('noVNC_open');
95dd6001 1007 return false;
1008 },
58ded70d 1009
95dd6001 1010/* ------^-------
7d714b15 1011 * /PASSWORD
95dd6001 1012 * ==============
1013 * FULLSCREEN
1014 * ------v------*/
1015
7d1dc09a 1016 toggleFullscreen: function() {
1017 if (document.fullscreenElement || // alternative standard method
1018 document.mozFullScreenElement || // currently working methods
1019 document.webkitFullscreenElement ||
a6357e82 1020 document.msFullscreenElement) {
7d1dc09a 1021 if (document.exitFullscreen) {
1022 document.exitFullscreen();
1023 } else if (document.mozCancelFullScreen) {
1024 document.mozCancelFullScreen();
1025 } else if (document.webkitExitFullscreen) {
1026 document.webkitExitFullscreen();
1027 } else if (document.msExitFullscreen) {
1028 document.msExitFullscreen();
1029 }
1030 } else {
1031 if (document.documentElement.requestFullscreen) {
1032 document.documentElement.requestFullscreen();
1033 } else if (document.documentElement.mozRequestFullScreen) {
1034 document.documentElement.mozRequestFullScreen();
1035 } else if (document.documentElement.webkitRequestFullscreen) {
1036 document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
1037 } else if (document.body.msRequestFullscreen) {
1038 document.body.msRequestFullscreen();
1039 }
1040 }
a6357e82 1041 UI.enableDisableViewClip();
7d1dc09a 1042 UI.updateFullscreenButton();
bbbf42bb
SR
1043 },
1044
7d1dc09a 1045 updateFullscreenButton: function() {
1046 if (document.fullscreenElement || // alternative standard method
1047 document.mozFullScreenElement || // currently working methods
1048 document.webkitFullscreenElement ||
1049 document.msFullscreenElement ) {
a49d9298 1050 document.getElementById('noVNC_fullscreen_button')
d9e86214 1051 .classList.add("noVNC_selected");
7d1dc09a 1052 } else {
a49d9298 1053 document.getElementById('noVNC_fullscreen_button')
d9e86214 1054 .classList.remove("noVNC_selected");
7d1dc09a 1055 }
1056 },
1057
95dd6001 1058/* ------^-------
1059 * /FULLSCREEN
1060 * ==============
1061 * RESIZE
1062 * ------v------*/
777cb7a0 1063
1064 // Apply remote resizing or local scaling
0bd2cbac 1065 applyResizeMode: function() {
58ded70d
SR
1066 if (!UI.rfb) return;
1067
777cb7a0 1068 var screen = UI.screenSize();
1069
3bb12056 1070 if (screen && UI.connected && UI.rfb.get_display()) {
777cb7a0 1071
1072 var display = UI.rfb.get_display();
1073 var resizeMode = UI.getSetting('resize');
1074
1075 if (resizeMode === 'remote') {
1076
1077 // Request changing the resolution of the remote display to
1078 // the size of the local browser viewport.
1079
1080 // In order to not send multiple requests before the browser-resize
1081 // is finished we wait 0.5 seconds before sending the request.
1082 clearTimeout(UI.resizeTimeout);
1083 UI.resizeTimeout = setTimeout(function(){
1084
1085 // Limit the viewport to the size of the browser window
1086 display.set_maxWidth(screen.w);
1087 display.set_maxHeight(screen.h);
1088
777cb7a0 1089 // Request a remote size covering the viewport
7ae53db9
SM
1090 if (UI.rfb.requestDesktopSize(screen.w, screen.h)) {
1091 Util.Debug('Requested new desktop size: ' +
1092 screen.w + 'x' + screen.h);
1093 }
777cb7a0 1094 }, 500);
1095
1096 } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
1097 var downscaleOnly = resizeMode === 'downscale';
1098 var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
ceb847b0
SM
1099
1100 if (!UI.rfb.get_view_only()) {
1101 UI.rfb.get_mouse().set_scale(scaleRatio);
1102 Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
1103 }
777cb7a0 1104 }
1105 }
bbbf42bb
SR
1106 },
1107
777cb7a0 1108 // The screen is always the same size as the available viewport
1109 // in the browser window minus the height of the control bar
0bd2cbac 1110 screenSize: function() {
ae510306 1111 var screen = document.getElementById('noVNC_screen');
777cb7a0 1112
1113 // Hide the scrollbars until the size is calculated
1114 screen.style.overflow = "hidden";
1115
1116 var pos = Util.getPosition(screen);
1117 var w = pos.width;
1118 var h = pos.height;
1119
1120 screen.style.overflow = "visible";
1121
1122 if (isNaN(w) || isNaN(h)) {
1123 return false;
1124 } else {
1125 return {w: w, h: h};
1126 }
bbbf42bb
SR
1127 },
1128
777cb7a0 1129 // Normally we only apply the current resize mode after a window resize
1130 // event. This means that when a new connection is opened, there is no
1131 // resize mode active.
1132 // We have to wait until the first FBU because this is where the client
1133 // will find the supported encodings of the server. Some calls later in
1134 // the chain is dependant on knowing the server-capabilities.
1135 initialResize: function(rfb, fbu) {
1136 UI.applyResizeMode();
1137 // After doing this once, we remove the callback.
1138 UI.rfb.set_onFBUComplete(function() { });
bbbf42bb
SR
1139 },
1140
95dd6001 1141/* ------^-------
1142 * /RESIZE
1143 * ==============
1144 * CLIPPING
1145 * ------v------*/
1146
30bfff81 1147 // Set and configure viewport clipping
bbbf42bb 1148 setViewClip: function(clip) {
f0d9ab96 1149 UI.updateSetting('clip', clip);
1150 UI.updateViewClip();
1151 },
1152
1153 // Update parameters that depend on the clip setting
1154 updateViewClip: function() {
1c1cc1d0 1155 if (!UI.rfb) return;
8e0f0088 1156
f0d9ab96 1157 var display = UI.rfb.get_display();
bbbf42bb 1158 var cur_clip = display.get_viewport();
f0d9ab96 1159 var new_clip = UI.getSetting('clip');
bbbf42bb 1160
f0d9ab96 1161 if (cur_clip !== new_clip) {
1162 display.set_viewport(new_clip);
bbbf42bb 1163 }
8e0f0088 1164
f0d9ab96 1165 var size = UI.screenSize();
fdedbafb 1166
f0d9ab96 1167 if (new_clip && size) {
1168 // When clipping is enabled, the screen is limited to
1169 // the size of the browser window.
1170 display.set_maxWidth(size.w);
1171 display.set_maxHeight(size.h);
fdedbafb 1172
f0d9ab96 1173 var screen = document.getElementById('noVNC_screen');
1174 var canvas = document.getElementById('noVNC_canvas');
fdedbafb 1175
f0d9ab96 1176 // Hide potential scrollbars that can skew the position
1177 screen.style.overflow = "hidden";
fdedbafb 1178
f0d9ab96 1179 // The x position marks the left margin of the canvas,
1180 // remove the margin from both sides to keep it centered.
1181 var new_w = size.w - (2 * Util.getPosition(canvas).x);
fdedbafb 1182
f0d9ab96 1183 screen.style.overflow = "visible";
1184
1185 display.viewportChangeSize(new_w, size.h);
1186 } else {
1187 // Disable max dimensions
1188 display.set_maxWidth(0);
1189 display.set_maxHeight(0);
1190 display.viewportChangeSize();
bbbf42bb
SR
1191 }
1192 },
1193
30bfff81 1194 // Handle special cases where clipping is forced on/off or locked
0bd2cbac 1195 enableDisableViewClip: function() {
ae510306 1196 var resizeSetting = document.getElementById('noVNC_setting_resize');
a6357e82 1197
f620259b 1198 if (UI.isSafari) {
1199 // Safari auto-hides the scrollbars which makes them
1200 // impossible to use in most cases
1201 UI.setViewClip(true);
ae510306 1202 document.getElementById('noVNC_setting_clip').disabled = true;
682fd02b 1203 } else if (resizeSetting.value === 'downscale' || resizeSetting.value === 'scale') {
a6357e82 1204 // Disable clipping if we are scaling
1205 UI.setViewClip(false);
ae510306 1206 document.getElementById('noVNC_setting_clip').disabled = true;
a6357e82 1207 } else if (document.msFullscreenElement) {
1208 // The browser is IE and we are in fullscreen mode.
1209 // - We need to force clipping while in fullscreen since
1210 // scrollbars doesn't work.
ca5c74ad 1211 UI.showStatus("Forcing clipping mode since scrollbars aren't supported by IE in fullscreen");
a6357e82 1212 UI.rememberedClipSetting = UI.getSetting('clip');
1213 UI.setViewClip(true);
ae510306 1214 document.getElementById('noVNC_setting_clip').disabled = true;
a6357e82 1215 } else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) {
1216 // Restore view clip to what it was before fullscreen on IE
1217 UI.setViewClip(UI.rememberedClipSetting);
3bb12056
SM
1218 document.getElementById('noVNC_setting_clip').disabled =
1219 UI.connected || UI.isTouchDevice;
30bfff81 1220 } else {
3bb12056
SM
1221 document.getElementById('noVNC_setting_clip').disabled =
1222 UI.connected || UI.isTouchDevice;
30bfff81 1223 if (UI.isTouchDevice) {
a6357e82 1224 UI.setViewClip(true);
30bfff81 1225 }
1226 }
1227 },
1228
95dd6001 1229/* ------^-------
1230 * /CLIPPING
1231 * ==============
1232 * VIEWDRAG
1233 * ------v------*/
1234
f0d9ab96 1235 toggleViewDrag: function() {
58ded70d 1236 if (!UI.rfb) return;
bbbf42bb 1237
f0d9ab96 1238 var drag = UI.rfb.get_viewportDrag();
1239 UI.setViewDrag(!drag);
1240 },
1241
1242 // Set the view drag mode which moves the viewport on mouse drags
1243 setViewDrag: function(drag) {
1244 if (!UI.rfb) return;
1245
1246 UI.rfb.set_viewportDrag(drag);
1247
1248 UI.updateViewDrag();
1249 },
1250
1251 updateViewDrag: function() {
1252 var clipping = false;
bbbf42bb 1253
3bb12056 1254 if (!UI.connected) return;
6244e383 1255
e00698fe 1256 // Check if viewport drag is possible. It is only possible
1257 // if the remote display is clipping the client display.
6244e383 1258 if (UI.rfb.get_display().get_viewport() &&
fdedbafb 1259 UI.rfb.get_display().clippingDisplay()) {
f0d9ab96 1260 clipping = true;
1261 }
31ddaa1c 1262
f0d9ab96 1263 var viewDragButton = document.getElementById('noVNC_view_drag_button');
29a0e6a8 1264
6244e383
PO
1265 if (!clipping &&
1266 UI.rfb.get_viewportDrag()) {
1267 // The size of the remote display is the same or smaller
1268 // than the client display. Make sure viewport drag isn't
1269 // active when it can't be used.
1270 UI.rfb.set_viewportDrag(false);
1271 }
1272
1273 if (UI.rfb.get_viewportDrag()) {
1274 viewDragButton.classList.add("noVNC_selected");
31ddaa1c 1275 } else {
6244e383
PO
1276 viewDragButton.classList.remove("noVNC_selected");
1277 }
31ddaa1c 1278
6244e383
PO
1279 // Different behaviour for touch vs non-touch
1280 // The button is disabled instead of hidden on touch devices
1281 if (UI.isTouchDevice) {
1282 viewDragButton.classList.remove("noVNC_hidden");
1283
1284 if (clipping) {
1285 viewDragButton.disabled = false;
31ddaa1c 1286 } else {
6244e383 1287 viewDragButton.disabled = true;
31ddaa1c 1288 }
6244e383
PO
1289 } else {
1290 viewDragButton.disabled = false;
29a0e6a8 1291
6244e383 1292 if (clipping) {
e40978c7 1293 viewDragButton.classList.remove("noVNC_hidden");
f0d9ab96 1294 } else {
6244e383 1295 viewDragButton.classList.add("noVNC_hidden");
f0d9ab96 1296 }
f8b399d7 1297 }
1298 },
1299
95dd6001 1300/* ------^-------
1301 * /VIEWDRAG
1302 * ==============
1303 * KEYBOARD
1304 * ------v------*/
fdf21468 1305
4b30f9ce 1306 showVirtualKeyboard: function() {
1c1cc1d0 1307 if (!UI.isTouchDevice) return;
4b30f9ce
SM
1308
1309 var input = document.getElementById('noVNC_keyboardinput');
1310
1c1cc1d0 1311 if (document.activeElement == input) return;
4b30f9ce
SM
1312
1313 UI.keyboardVisible = true;
1314 document.getElementById('noVNC_keyboard_button')
1315 .classList.add("noVNC_selected");
1316 input.focus();
1317
1318 try {
1319 var l = input.value.length;
1320 // Move the caret to the end
1321 input.setSelectionRange(l, l);
1322 } catch (err) {} // setSelectionRange is undefined in Google Chrome
1323 },
1324
1325 hideVirtualKeyboard: function() {
1c1cc1d0 1326 if (!UI.isTouchDevice) return;
4b30f9ce
SM
1327
1328 var input = document.getElementById('noVNC_keyboardinput');
1329
1c1cc1d0 1330 if (document.activeElement != input) return;
4b30f9ce
SM
1331
1332 input.blur();
1333 },
1334
1335 toggleVirtualKeyboard: function () {
1336 if (UI.keyboardVisible) {
1337 UI.hideVirtualKeyboard();
1338 } else {
1339 UI.showVirtualKeyboard();
bbbf42bb
SR
1340 }
1341 },
1342
4b30f9ce 1343 onblurVirtualKeyboard: function() {
fdf21468 1344 //Weird bug in iOS if you change keyboardVisible
1345 //here it does not actually occur so next time
1346 //you click keyboard icon it doesnt work.
1347 UI.hideKeyboardTimeout = setTimeout(function() {
1348 UI.keyboardVisible = false;
4b30f9ce
SM
1349 document.getElementById('noVNC_keyboard_button')
1350 .classList.remove("noVNC_selected");
fdf21468 1351 },100);
1352 },
1353
bbbf42bb
SR
1354 keepKeyboard: function() {
1355 clearTimeout(UI.hideKeyboardTimeout);
1356 if(UI.keyboardVisible === true) {
4b30f9ce 1357 UI.showVirtualKeyboard();
bbbf42bb 1358 } else if(UI.keyboardVisible === false) {
4b30f9ce 1359 UI.hideVirtualKeyboard();
bbbf42bb
SR
1360 }
1361 },
1362
1363 keyboardinputReset: function() {
ae510306 1364 var kbi = document.getElementById('noVNC_keyboardinput');
bbbf42bb
SR
1365 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
1366 UI.lastKeyboardinput = kbi.value;
1367 },
1368
1369 // When normal keyboard events are left uncought, use the input events from
1370 // the keyboardinput element instead and generate the corresponding key events.
1371 // This code is required since some browsers on Android are inconsistent in
1372 // sending keyCodes in the normal keyboard events when using on screen keyboards.
1373 keyInput: function(event) {
3b8ec46f 1374
58ded70d 1375 if (!UI.rfb) return;
3b8ec46f 1376
bbbf42bb 1377 var newValue = event.target.value;
1138bdd4 1378
1379 if (!UI.lastKeyboardinput) {
1380 UI.keyboardinputReset();
1381 }
cb3e4deb 1382 var oldValue = UI.lastKeyboardinput;
bbbf42bb
SR
1383
1384 var newLen;
1385 try {
1386 // Try to check caret position since whitespace at the end
1387 // will not be considered by value.length in some browsers
1388 newLen = Math.max(event.target.selectionStart, newValue.length);
1389 } catch (err) {
1390 // selectionStart is undefined in Google Chrome
1391 newLen = newValue.length;
1392 }
1393 var oldLen = oldValue.length;
1394
1395 var backspaces;
1396 var inputs = newLen - oldLen;
1397 if (inputs < 0) {
1398 backspaces = -inputs;
1399 } else {
1400 backspaces = 0;
1401 }
8e0f0088 1402
bbbf42bb
SR
1403 // Compare the old string with the new to account for
1404 // text-corrections or other input that modify existing text
1405 var i;
1406 for (i = 0; i < Math.min(oldLen, newLen); i++) {
1407 if (newValue.charAt(i) != oldValue.charAt(i)) {
1408 inputs = newLen - i;
1409 backspaces = oldLen - i;
1410 break;
1411 }
1412 }
1413
1414 // Send the key events
1415 for (i = 0; i < backspaces; i++) {
ae510306 1416 UI.rfb.sendKey(KeyTable.XK_BackSpace);
bbbf42bb
SR
1417 }
1418 for (i = newLen - inputs; i < newLen; i++) {
1419 UI.rfb.sendKey(newValue.charCodeAt(i));
1420 }
1421
1422 // Control the text content length in the keyboardinput element
1423 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1424 UI.keyboardinputReset();
1425 } else if (newLen < 1) {
1426 // There always have to be some text in the keyboardinput
1427 // element with which backspace can interact.
1428 UI.keyboardinputReset();
1429 // This sometimes causes the keyboard to disappear for a second
1430 // but it is required for the android keyboard to recognize that
1431 // text has been added to the field
1432 event.target.blur();
1433 // This has to be ran outside of the input handler in order to work
67685d07 1434 setTimeout(UI.keepKeyboard, 0);
bbbf42bb
SR
1435 } else {
1436 UI.lastKeyboardinput = newValue;
1437 }
1438 },
1439
4b30f9ce
SM
1440/* ------^-------
1441 * /KEYBOARD
1442 * ==============
1443 * EXTRA KEYS
1444 * ------v------*/
1445
ed8cbe4e 1446 openExtraKeys: function() {
fb7c3b3b 1447 UI.closeAllPanels();
38323d4d 1448 UI.openControlbar();
fb7c3b3b 1449
ed8cbe4e
PO
1450 document.getElementById('noVNC_modifiers')
1451 .classList.add("noVNC_open");
1452 document.getElementById('noVNC_toggle_extra_keys_button')
1453 .classList.add("noVNC_selected");
1454 },
1455
1456 closeExtraKeys: function() {
1457 document.getElementById('noVNC_modifiers')
1458 .classList.remove("noVNC_open");
1459 document.getElementById('noVNC_toggle_extra_keys_button')
1460 .classList.remove("noVNC_selected");
1461 },
1462
cd611a53 1463 toggleExtraKeys: function() {
bbbf42bb 1464 UI.keepKeyboard();
ed8cbe4e
PO
1465 if(document.getElementById('noVNC_modifiers')
1466 .classList.contains("noVNC_open")) {
1467 UI.closeExtraKeys();
1468 } else {
1469 UI.openExtraKeys();
bbbf42bb
SR
1470 }
1471 },
1472
fdf21468 1473 sendEsc: function() {
1474 UI.keepKeyboard();
ae510306 1475 UI.rfb.sendKey(KeyTable.XK_Escape);
fdf21468 1476 },
1477
1478 sendTab: function() {
1479 UI.keepKeyboard();
ae510306 1480 UI.rfb.sendKey(KeyTable.XK_Tab);
fdf21468 1481 },
1482
bbbf42bb
SR
1483 toggleCtrl: function() {
1484 UI.keepKeyboard();
b0c6d3c6 1485 var btn = document.getElementById('noVNC_toggle_ctrl_button');
1486 if (btn.classList.contains("noVNC_selected")) {
ae510306 1487 UI.rfb.sendKey(KeyTable.XK_Control_L, false);
b0c6d3c6 1488 btn.classList.remove("noVNC_selected");
1489 } else {
1490 UI.rfb.sendKey(KeyTable.XK_Control_L, true);
1491 btn.classList.add("noVNC_selected");
bbbf42bb
SR
1492 }
1493 },
1494
1495 toggleAlt: function() {
1496 UI.keepKeyboard();
b0c6d3c6 1497 var btn = document.getElementById('noVNC_toggle_alt_button');
1498 if (btn.classList.contains("noVNC_selected")) {
ae510306 1499 UI.rfb.sendKey(KeyTable.XK_Alt_L, false);
b0c6d3c6 1500 btn.classList.remove("noVNC_selected");
1501 } else {
1502 UI.rfb.sendKey(KeyTable.XK_Alt_L, true);
1503 btn.classList.add("noVNC_selected");
bbbf42bb
SR
1504 }
1505 },
1506
fdf21468 1507 sendCtrlAltDel: function() {
ca25d2ae 1508 UI.keepKeyboard();
fdf21468 1509 UI.rfb.sendCtrlAltDel();
bbbf42bb
SR
1510 },
1511
95dd6001 1512/* ------^-------
4b30f9ce 1513 * /EXTRA KEYS
95dd6001 1514 * ==============
1515 * MISC
1516 * ------v------*/
1517
1518 setMouseButton: function(num) {
ceb847b0 1519 if (UI.rfb && !UI.rfb.get_view_only()) {
95dd6001 1520 UI.rfb.get_mouse().set_touchButton(num);
1521 }
1522
1523 var blist = [0, 1,2,4];
1524 for (var b = 0; b < blist.length; b++) {
ae510306 1525 var button = document.getElementById('noVNC_mouse_button' + blist[b]);
95dd6001 1526 if (blist[b] === num) {
e40978c7 1527 button.classList.remove("noVNC_hidden");
95dd6001 1528 } else {
e40978c7 1529 button.classList.add("noVNC_hidden");
95dd6001 1530 }
1531 }
1532 },
1533
1534 displayBlur: function() {
ceb847b0
SM
1535 if (UI.rfb && !UI.rfb.get_view_only()) {
1536 UI.rfb.get_keyboard().set_focused(false);
1537 UI.rfb.get_mouse().set_focused(false);
1538 }
95dd6001 1539 },
1540
1541 displayFocus: function() {
ceb847b0
SM
1542 if (UI.rfb && !UI.rfb.get_view_only()) {
1543 UI.rfb.get_keyboard().set_focused(true);
1544 UI.rfb.get_mouse().set_focused(true);
1545 }
bbbf42bb
SR
1546 },
1547
3bb12056
SM
1548 updateDesktopName: function(rfb, name) {
1549 UI.desktopName = name;
1550 // Display the desktop name in the document title
95dd6001 1551 document.title = name + " - noVNC";
bbbf42bb
SR
1552 },
1553
63bf2ba5
PO
1554 bell: function(rfb) {
1555 if (WebUtil.getConfigVar('bell', 'on') === 'on') {
1556 document.getElementById('noVNC_bell').play();
1557 }
1558 },
1559
bbbf42bb
SR
1560 //Helper to add options to dropdown.
1561 addOption: function(selectbox, text, value) {
1562 var optn = document.createElement("OPTION");
1563 optn.text = text;
1564 optn.value = value;
1565 selectbox.options.add(optn);
1566 },
1567
95dd6001 1568/* ------^-------
1569 * /MISC
1570 * ==============
1571 */
bbbf42bb 1572 };
ae510306
SR
1573
1574 /* [module] UI.load(); */
bbbf42bb 1575})();
ae510306
SR
1576
1577/* [module] export default UI; */