]> git.proxmox.com Git - mirror_novnc.git/blame - include/ui.js
Stop using keyboardinputReset at the start of keyInput
[mirror_novnc.git] / include / ui.js
CommitLineData
15046f00
JM
1/*
2 * noVNC: HTML5 VNC client
d58f8b51 3 * Copyright (C) 2012 Joel Martin
fdedbafb 4 * Copyright (C) 2015 Samuel Mannehed for Cendio AB
1d728ace 5 * Licensed under MPL 2.0 (see LICENSE.txt)
15046f00
JM
6 *
7 * See README.md for usage and integration instructions.
8 */
43cf7bd8 9
bbbf42bb
SR
10/* jslint white: false, browser: true */
11/* global window, $D, Util, WebUtil, RFB, Display */
12
13var UI;
14
15(function () {
16 "use strict";
17
f8b399d7 18 var resizeTimeout;
19
bbbf42bb
SR
20 // Load supporting scripts
21 window.onscriptsload = function () { UI.load(); };
bbbf42bb
SR
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"]);
25
bd6874e0 26 UI = {
bbbf42bb
SR
27
28 rfb_state : 'loaded',
29 settingsOpen : false,
30 connSettingsOpen : false,
31 popupStatusOpen : false,
32 clipboardOpen: false,
33 keyboardVisible: false,
34 hideKeyboardTimeout: null,
35 lastKeyboardinput: null,
36 defaultKeyboardinputLen: 100,
37 extraKeysVisible: false,
38 ctrlOn: false,
39 altOn: false,
40 isTouchDevice: false,
41
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);
46 },
47
48 // Render default UI and initialize settings menu
49 start: function(callback) {
50 UI.isTouchDevice = 'ontouchstart' in document.documentElement;
51
52 // Stylesheet selection dropdown
53 var sheet = WebUtil.selectStylesheet();
54 var sheets = WebUtil.getStylesheets();
55 var i;
56 for (i = 0; i < sheets.length; i += 1) {
57 UI.addOption($D('noVNC_stylesheet'),sheets[i].title, sheets[i].title);
58 }
59
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]);
64 }
65
66 // Settings with immediate effects
67 UI.initSetting('logging', 'warn');
68 WebUtil.init_logging(UI.getSetting('logging'));
69
70 UI.initSetting('stylesheet', 'default');
71 WebUtil.selectStylesheet(null);
72 // call twice to get around webkit bug
73 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
74
75 // if port == 80 (or 443) then it won't be present and should be
76 // set manually
77 var port = window.location.port;
78 if (!port) {
79 if (window.location.protocol.substring(0,5) == 'https') {
80 port = 443;
81 }
82 else if (window.location.protocol.substring(0,4) == 'http') {
83 port = 80;
84 }
85 }
86
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);
8b46c0de 94 UI.initSetting('resize', 'off');
bbbf42bb
SR
95 UI.initSetting('shared', true);
96 UI.initSetting('view_only', false);
97 UI.initSetting('path', 'websockify');
98 UI.initSetting('repeaterID', '');
99
bbbf42bb
SR
100 var autoconnect = WebUtil.getQueryVar('autoconnect', false);
101 if (autoconnect === 'true' || autoconnect == '1') {
102 autoconnect = true;
103 UI.connect();
104 } else {
105 autoconnect = false;
106 }
107
108 UI.updateVisualState();
109
fdedbafb 110 $D('noVNC_host').focus();
111
bbbf42bb
SR
112 // Show mouse selector buttons on touch screen devices
113 if (UI.isTouchDevice) {
114 // Show mobile buttons
115 $D('noVNC_mobile_buttons').style.display = "inline";
116 UI.setMouseButton();
117 // Remove the address bar
118 setTimeout(function() { window.scrollTo(0, 1); }, 100);
119 UI.forceSetting('clip', true);
bbbf42bb
SR
120 } else {
121 UI.initSetting('clip', false);
122 }
123
bbbf42bb 124 UI.setViewClip();
fdedbafb 125 UI.setBarPosition();
f8b399d7 126
127 Util.addEvent(window, 'resize', function () {
fdedbafb 128 UI.onresize();
f8b399d7 129 UI.setViewClip();
fdedbafb 130 UI.updateViewDragButton();
131 UI.setBarPosition();
f8b399d7 132 } );
bbbf42bb 133
5b7598ac 134 Util.addEvent(window, 'load', UI.keyboardinputReset);
135
bbbf42bb 136 Util.addEvent(window, 'beforeunload', function () {
58ded70d 137 if (UI.rfb && UI.rfb_state === 'normal') {
bbbf42bb
SR
138 return "You are currently connected.";
139 }
140 } );
141
142 // Show description by default when hosted at for kanaka.github.com
143 if (location.host === "kanaka.github.io") {
144 // Open the description dialog
145 $D('noVNC_description').style.display = "block";
146 } else {
147 // Show the connect panel on first load unless autoconnecting
148 if (autoconnect === UI.connSettingsOpen) {
149 UI.toggleConnectPanel();
150 }
151 }
152
153 // Add mouse event click/focus/blur event handlers to the UI
154 UI.addMouseHandlers();
155
156 if (typeof callback === "function") {
157 callback(UI.rfb);
158 }
159 },
160
e543525f 161 initRFB: function () {
d9fc1c7b
SR
162 try {
163 UI.rfb = new RFB({'target': $D('noVNC_canvas'),
164 'onUpdateState': UI.updateState,
165 'onXvpInit': UI.updateXvpVisualState,
166 'onClipboard': UI.clipReceive,
167 'onFBUComplete': UI.FBUComplete,
168 'onFBResize': UI.updateViewDragButton,
169 'onDesktopName': UI.updateDocumentTitle});
170 return true;
171 } catch (exc) {
172 UI.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
173 return false;
174 }
e543525f
SR
175 },
176
bbbf42bb
SR
177 addMouseHandlers: function() {
178 // Setup interface handlers that can't be inline
179 $D("noVNC_view_drag_button").onclick = UI.setViewDrag;
180 $D("noVNC_mouse_button0").onclick = function () { UI.setMouseButton(1); };
181 $D("noVNC_mouse_button1").onclick = function () { UI.setMouseButton(2); };
182 $D("noVNC_mouse_button2").onclick = function () { UI.setMouseButton(4); };
183 $D("noVNC_mouse_button4").onclick = function () { UI.setMouseButton(0); };
184 $D("showKeyboard").onclick = UI.showKeyboard;
185
186 $D("keyboardinput").oninput = UI.keyInput;
187 $D("keyboardinput").onblur = UI.keyInputBlur;
48d26b2d 188 $D("keyboardinput").onsubmit = function () { return false; };
bbbf42bb
SR
189
190 $D("showExtraKeysButton").onclick = UI.showExtraKeys;
191 $D("toggleCtrlButton").onclick = UI.toggleCtrl;
192 $D("toggleAltButton").onclick = UI.toggleAlt;
193 $D("sendTabButton").onclick = UI.sendTab;
194 $D("sendEscButton").onclick = UI.sendEsc;
195
196 $D("sendCtrlAltDelButton").onclick = UI.sendCtrlAltDel;
197 $D("xvpShutdownButton").onclick = UI.xvpShutdown;
198 $D("xvpRebootButton").onclick = UI.xvpReboot;
199 $D("xvpResetButton").onclick = UI.xvpReset;
200 $D("noVNC_status").onclick = UI.togglePopupStatusPanel;
201 $D("noVNC_popup_status_panel").onclick = UI.togglePopupStatusPanel;
202 $D("xvpButton").onclick = UI.toggleXvpPanel;
203 $D("clipboardButton").onclick = UI.toggleClipboardPanel;
204 $D("settingsButton").onclick = UI.toggleSettingsPanel;
205 $D("connectButton").onclick = UI.toggleConnectPanel;
206 $D("disconnectButton").onclick = UI.disconnect;
207 $D("descriptionButton").onclick = UI.toggleConnectPanel;
208
209 $D("noVNC_clipboard_text").onfocus = UI.displayBlur;
210 $D("noVNC_clipboard_text").onblur = UI.displayFocus;
211 $D("noVNC_clipboard_text").onchange = UI.clipSend;
212 $D("noVNC_clipboard_clear_button").onclick = UI.clipClear;
213
214 $D("noVNC_settings_menu").onmouseover = UI.displayBlur;
215 $D("noVNC_settings_menu").onmouseover = UI.displayFocus;
216 $D("noVNC_apply").onclick = UI.settingsApply;
217
218 $D("noVNC_connect_button").onclick = UI.connect;
72747869
SR
219
220 $D("noVNC_resize").onchange = function () {
58ded70d 221 var connected = UI.rfb && UI.rfb_state === 'normal';
72747869
SR
222 UI.enableDisableClip(connected);
223 };
bbbf42bb
SR
224 },
225
fdedbafb 226 onresize: function (callback) {
58ded70d
SR
227 if (!UI.rfb) return;
228
fdedbafb 229 var size = UI.getCanvasLimit();
230
231 if (size && UI.rfb_state === 'normal' && UI.rfb.get_display()) {
232 var display = UI.rfb.get_display();
233 var scaleType = UI.getSetting('resize');
234 if (scaleType === 'remote') {
235 // use remote resizing
236
237 // When the local window has been resized, wait until the size remains
238 // the same for 0.5 seconds before sending the request for changing
239 // the resolution of the session
240 clearTimeout(resizeTimeout);
241 resizeTimeout = setTimeout(function(){
242 display.set_maxWidth(size.w);
243 display.set_maxHeight(size.h);
244 Util.Debug('Attempting setDesktopSize(' +
245 size.w + ', ' + size.h + ')');
246 UI.rfb.setDesktopSize(size.w, size.h);
247 }, 500);
248 } else if (scaleType === 'scale' || scaleType === 'downscale') {
249 // use local scaling
250
251 var downscaleOnly = scaleType === 'downscale';
252 var scaleRatio = display.autoscale(size.w, size.h, downscaleOnly);
253 UI.rfb.get_mouse().set_scale(scaleRatio);
254 Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
255 }
256 }
257 },
258
259 getCanvasLimit: function () {
260 var container = $D('noVNC_container');
261
262 // Hide the scrollbars until the size is calculated
263 container.style.overflow = "hidden";
264
16b3ef77 265 var pos = Util.getPosition(container);
266 var w = pos.width;
267 var h = pos.height;
fdedbafb 268
269 container.style.overflow = "visible";
270
271 if (isNaN(w) || isNaN(h)) {
272 return false;
273 } else {
274 return {w: w, h: h};
275 }
276 },
277
bbbf42bb
SR
278 // Read form control compatible setting from cookie
279 getSetting: function(name) {
280 var ctrl = $D('noVNC_' + name);
281 var val = WebUtil.readSetting(name);
f8b399d7 282 if (typeof val !== 'undefined' && val !== null && ctrl.type === 'checkbox') {
bbbf42bb
SR
283 if (val.toString().toLowerCase() in {'0':1, 'no':1, 'false':1}) {
284 val = false;
285 } else {
286 val = true;
287 }
288 }
289 return val;
290 },
291
292 // Update cookie and form control setting. If value is not set, then
293 // updates from control to current cookie setting.
294 updateSetting: function(name, value) {
295
296 // Save the cookie for this session
297 if (typeof value !== 'undefined') {
298 WebUtil.writeSetting(name, value);
299 }
300
301 // Update the settings control
302 value = UI.getSetting(name);
303
304 var ctrl = $D('noVNC_' + name);
305 if (ctrl.type === 'checkbox') {
306 ctrl.checked = value;
307
308 } else if (typeof ctrl.options !== 'undefined') {
309 for (var i = 0; i < ctrl.options.length; i += 1) {
310 if (ctrl.options[i].value === value) {
311 ctrl.selectedIndex = i;
312 break;
313 }
314 }
315 } else {
316 /*Weird IE9 error leads to 'null' appearring
317 in textboxes instead of ''.*/
318 if (value === null) {
319 value = "";
320 }
321 ctrl.value = value;
322 }
323 },
324
325 // Save control setting to cookie
326 saveSetting: function(name) {
327 var val, ctrl = $D('noVNC_' + name);
328 if (ctrl.type === 'checkbox') {
329 val = ctrl.checked;
330 } else if (typeof ctrl.options !== 'undefined') {
331 val = ctrl.options[ctrl.selectedIndex].value;
332 } else {
333 val = ctrl.value;
334 }
335 WebUtil.writeSetting(name, val);
336 //Util.Debug("Setting saved '" + name + "=" + val + "'");
337 return val;
338 },
339
340 // Initial page load read/initialization of settings
341 initSetting: function(name, defVal) {
342 // Check Query string followed by cookie
343 var val = WebUtil.getQueryVar(name);
344 if (val === null) {
345 val = WebUtil.readSetting(name, defVal);
346 }
347 UI.updateSetting(name, val);
348 return val;
349 },
350
351 // Force a setting to be a certain value
352 forceSetting: function(name, val) {
353 UI.updateSetting(name, val);
354 return val;
355 },
356
357
358 // Show the popup status panel
359 togglePopupStatusPanel: function() {
360 var psp = $D('noVNC_popup_status_panel');
361 if (UI.popupStatusOpen === true) {
362 psp.style.display = "none";
363 UI.popupStatusOpen = false;
364 } else {
365 psp.innerHTML = $D('noVNC_status').innerHTML;
366 psp.style.display = "block";
367 psp.style.left = window.innerWidth/2 -
368 parseInt(window.getComputedStyle(psp, false).width)/2 -30 + "px";
369 UI.popupStatusOpen = true;
370 }
371 },
372
373 // Show the XVP panel
374 toggleXvpPanel: function() {
375 // Close the description panel
376 $D('noVNC_description').style.display = "none";
377 // Close settings if open
378 if (UI.settingsOpen === true) {
379 UI.settingsApply();
380 UI.closeSettingsMenu();
381 }
382 // Close connection settings if open
383 if (UI.connSettingsOpen === true) {
384 UI.toggleConnectPanel();
385 }
386 // Close popup status panel if open
387 if (UI.popupStatusOpen === true) {
388 UI.togglePopupStatusPanel();
389 }
390 // Close clipboard panel if open
391 if (UI.clipboardOpen === true) {
392 UI.toggleClipboardPanel();
393 }
394 // Toggle XVP panel
395 if (UI.xvpOpen === true) {
396 $D('noVNC_xvp').style.display = "none";
397 $D('xvpButton').className = "noVNC_status_button";
398 UI.xvpOpen = false;
399 } else {
400 $D('noVNC_xvp').style.display = "block";
401 $D('xvpButton').className = "noVNC_status_button_selected";
402 UI.xvpOpen = true;
403 }
404 },
405
406 // Show the clipboard panel
407 toggleClipboardPanel: function() {
408 // Close the description panel
409 $D('noVNC_description').style.display = "none";
410 // Close settings if open
411 if (UI.settingsOpen === true) {
412 UI.settingsApply();
413 UI.closeSettingsMenu();
414 }
415 // Close connection settings if open
416 if (UI.connSettingsOpen === true) {
417 UI.toggleConnectPanel();
418 }
419 // Close popup status panel if open
420 if (UI.popupStatusOpen === true) {
421 UI.togglePopupStatusPanel();
422 }
423 // Close XVP panel if open
424 if (UI.xvpOpen === true) {
425 UI.toggleXvpPanel();
426 }
427 // Toggle Clipboard Panel
428 if (UI.clipboardOpen === true) {
429 $D('noVNC_clipboard').style.display = "none";
430 $D('clipboardButton').className = "noVNC_status_button";
431 UI.clipboardOpen = false;
432 } else {
433 $D('noVNC_clipboard').style.display = "block";
434 $D('clipboardButton').className = "noVNC_status_button_selected";
435 UI.clipboardOpen = true;
436 }
437 },
438
439 // Show the connection settings panel/menu
440 toggleConnectPanel: function() {
441 // Close the description panel
442 $D('noVNC_description').style.display = "none";
443 // Close connection settings if open
444 if (UI.settingsOpen === true) {
445 UI.settingsApply();
446 UI.closeSettingsMenu();
447 $D('connectButton').className = "noVNC_status_button";
448 }
449 // Close clipboard panel if open
450 if (UI.clipboardOpen === true) {
451 UI.toggleClipboardPanel();
452 }
453 // Close popup status panel if open
454 if (UI.popupStatusOpen === true) {
455 UI.togglePopupStatusPanel();
456 }
457 // Close XVP panel if open
458 if (UI.xvpOpen === true) {
459 UI.toggleXvpPanel();
460 }
461
462 // Toggle Connection Panel
463 if (UI.connSettingsOpen === true) {
464 $D('noVNC_controls').style.display = "none";
465 $D('connectButton').className = "noVNC_status_button";
466 UI.connSettingsOpen = false;
467 UI.saveSetting('host');
468 UI.saveSetting('port');
469 //UI.saveSetting('password');
470 } else {
471 $D('noVNC_controls').style.display = "block";
472 $D('connectButton').className = "noVNC_status_button_selected";
473 UI.connSettingsOpen = true;
474 $D('noVNC_host').focus();
475 }
476 },
477
478 // Toggle the settings menu:
479 // On open, settings are refreshed from saved cookies.
480 // On close, settings are applied
481 toggleSettingsPanel: function() {
482 // Close the description panel
483 $D('noVNC_description').style.display = "none";
484 if (UI.settingsOpen) {
485 UI.settingsApply();
486 UI.closeSettingsMenu();
487 } else {
488 UI.updateSetting('encrypt');
489 UI.updateSetting('true_color');
58ded70d 490 if (Util.browserSupportsCursorURIs()) {
bbbf42bb
SR
491 UI.updateSetting('cursor');
492 } else {
493 UI.updateSetting('cursor', !UI.isTouchDevice);
494 $D('noVNC_cursor').disabled = true;
495 }
496 UI.updateSetting('clip');
f8b399d7 497 UI.updateSetting('resize');
bbbf42bb
SR
498 UI.updateSetting('shared');
499 UI.updateSetting('view_only');
500 UI.updateSetting('path');
501 UI.updateSetting('repeaterID');
502 UI.updateSetting('stylesheet');
503 UI.updateSetting('logging');
504
505 UI.openSettingsMenu();
506 }
507 },
508
509 // Open menu
510 openSettingsMenu: function() {
511 // Close the description panel
512 $D('noVNC_description').style.display = "none";
513 // Close clipboard panel if open
514 if (UI.clipboardOpen === true) {
515 UI.toggleClipboardPanel();
516 }
517 // Close connection settings if open
518 if (UI.connSettingsOpen === true) {
519 UI.toggleConnectPanel();
520 }
521 // Close popup status panel if open
522 if (UI.popupStatusOpen === true) {
523 UI.togglePopupStatusPanel();
524 }
525 // Close XVP panel if open
526 if (UI.xvpOpen === true) {
527 UI.toggleXvpPanel();
528 }
529 $D('noVNC_settings').style.display = "block";
530 $D('settingsButton').className = "noVNC_status_button_selected";
531 UI.settingsOpen = true;
532 },
533
534 // Close menu (without applying settings)
535 closeSettingsMenu: function() {
536 $D('noVNC_settings').style.display = "none";
537 $D('settingsButton').className = "noVNC_status_button";
538 UI.settingsOpen = false;
539 },
540
541 // Save/apply settings when 'Apply' button is pressed
542 settingsApply: function() {
543 //Util.Debug(">> settingsApply");
544 UI.saveSetting('encrypt');
545 UI.saveSetting('true_color');
58ded70d 546 if (Util.browserSupportsCursorURIs()) {
bbbf42bb
SR
547 UI.saveSetting('cursor');
548 }
72747869 549
f8b399d7 550 UI.saveSetting('resize');
72747869
SR
551
552 if (UI.getSetting('resize') === 'downscale' || UI.getSetting('resize') === 'scale') {
553 UI.forceSetting('clip', false);
554 }
555
556 UI.saveSetting('clip');
bbbf42bb
SR
557 UI.saveSetting('shared');
558 UI.saveSetting('view_only');
559 UI.saveSetting('path');
560 UI.saveSetting('repeaterID');
561 UI.saveSetting('stylesheet');
562 UI.saveSetting('logging');
563
564 // Settings with immediate (non-connected related) effect
565 WebUtil.selectStylesheet(UI.getSetting('stylesheet'));
566 WebUtil.init_logging(UI.getSetting('logging'));
567 UI.setViewClip();
58ded70d 568 UI.setViewDrag(UI.rfb && UI.rfb.get_viewportDrag());
bbbf42bb
SR
569 //Util.Debug("<< settingsApply");
570 },
571
572
573
574 setPassword: function() {
575 UI.rfb.sendPassword($D('noVNC_password').value);
576 //Reset connect button.
577 $D('noVNC_connect_button').value = "Connect";
578 $D('noVNC_connect_button').onclick = UI.Connect;
579 //Hide connection panel.
f8ddfc73 580 UI.toggleConnectPanel();
bbbf42bb
SR
581 return false;
582 },
da6dd893 583
bbbf42bb
SR
584 sendCtrlAltDel: function() {
585 UI.rfb.sendCtrlAltDel();
586 },
53fc7392 587
bbbf42bb
SR
588 xvpShutdown: function() {
589 UI.rfb.xvpShutdown();
590 },
01a9eee9 591
bbbf42bb
SR
592 xvpReboot: function() {
593 UI.rfb.xvpReboot();
594 },
53fc7392 595
bbbf42bb
SR
596 xvpReset: function() {
597 UI.rfb.xvpReset();
598 },
01a9eee9 599
bbbf42bb
SR
600 setMouseButton: function(num) {
601 if (typeof num === 'undefined') {
602 // Disable mouse buttons
603 num = -1;
da6dd893 604 }
bbbf42bb
SR
605 if (UI.rfb) {
606 UI.rfb.get_mouse().set_touchButton(num);
607 }
608
609 var blist = [0, 1,2,4];
610 for (var b = 0; b < blist.length; b++) {
611 var button = $D('noVNC_mouse_button' + blist[b]);
612 if (blist[b] === num) {
613 button.style.display = "";
614 } else {
615 button.style.display = "none";
616 }
617 }
618 },
619
620 updateState: function(rfb, state, oldstate, msg) {
621 UI.rfb_state = state;
622 var klass;
623 switch (state) {
624 case 'failed':
625 case 'fatal':
626 klass = "noVNC_status_error";
627 break;
628 case 'normal':
629 klass = "noVNC_status_normal";
630 break;
631 case 'disconnected':
632 $D('noVNC_logo').style.display = "block";
fdedbafb 633 $D('noVNC_container').style.display = "none";
bbbf42bb
SR
634 /* falls through */
635 case 'loaded':
636 klass = "noVNC_status_normal";
637 break;
638 case 'password':
639 UI.toggleConnectPanel();
640
641 $D('noVNC_connect_button').value = "Send Password";
642 $D('noVNC_connect_button').onclick = UI.setPassword;
643 $D('noVNC_password').focus();
644
645 klass = "noVNC_status_warn";
646 break;
647 default:
648 klass = "noVNC_status_warn";
649 break;
650 }
651
652 if (typeof(msg) !== 'undefined') {
653 $D('noVNC-control-bar').setAttribute("class", klass);
654 $D('noVNC_status').innerHTML = msg;
655 }
656
657 UI.updateVisualState();
658 },
659
660 // Disable/enable controls depending on connection state
661 updateVisualState: function() {
58ded70d 662 var connected = UI.rfb && UI.rfb_state === 'normal';
bbbf42bb
SR
663
664 //Util.Debug(">> updateVisualState");
665 $D('noVNC_encrypt').disabled = connected;
666 $D('noVNC_true_color').disabled = connected;
58ded70d 667 if (Util.browserSupportsCursorURIs()) {
bbbf42bb
SR
668 $D('noVNC_cursor').disabled = connected;
669 } else {
670 UI.updateSetting('cursor', !UI.isTouchDevice);
671 $D('noVNC_cursor').disabled = true;
672 }
72747869
SR
673
674 UI.enableDisableClip(connected);
f8b399d7 675 $D('noVNC_resize').disabled = connected;
bbbf42bb
SR
676 $D('noVNC_shared').disabled = connected;
677 $D('noVNC_view_only').disabled = connected;
678 $D('noVNC_path').disabled = connected;
679 $D('noVNC_repeaterID').disabled = connected;
680
681 if (connected) {
682 UI.setViewClip();
683 UI.setMouseButton(1);
684 $D('clipboardButton').style.display = "inline";
685 $D('showKeyboard').style.display = "inline";
686 $D('noVNC_extra_keys').style.display = "";
687 $D('sendCtrlAltDelButton').style.display = "inline";
688 } else {
689 UI.setMouseButton();
690 $D('clipboardButton').style.display = "none";
691 $D('showKeyboard').style.display = "none";
692 $D('noVNC_extra_keys').style.display = "none";
693 $D('sendCtrlAltDelButton').style.display = "none";
694 UI.updateXvpVisualState(0);
695 }
696
697 // State change disables viewport dragging.
698 // It is enabled (toggled) by direct click on the button
699 UI.setViewDrag(false);
700
701 switch (UI.rfb_state) {
702 case 'fatal':
703 case 'failed':
bbbf42bb 704 case 'disconnected':
e543525f
SR
705 $D('connectButton').style.display = "";
706 $D('disconnectButton').style.display = "none";
707 UI.connSettingsOpen = false;
708 UI.toggleConnectPanel();
709 break;
710 case 'loaded':
bbbf42bb
SR
711 $D('connectButton').style.display = "";
712 $D('disconnectButton').style.display = "none";
713 break;
714 default:
715 $D('connectButton').style.display = "none";
716 $D('disconnectButton').style.display = "";
717 break;
718 }
719
720 //Util.Debug("<< updateVisualState");
721 },
722
723 // Disable/enable XVP button
724 updateXvpVisualState: function(ver) {
725 if (ver >= 1) {
726 $D('xvpButton').style.display = 'inline';
727 } else {
728 $D('xvpButton').style.display = 'none';
729 // Close XVP panel if open
730 if (UI.xvpOpen === true) {
731 UI.toggleXvpPanel();
732 }
733 }
734 },
735
72747869
SR
736 enableDisableClip: function (connected) {
737 var resizeElem = $D('noVNC_resize');
738 if (resizeElem.value === 'downscale' || resizeElem.value === 'scale') {
739 UI.forceSetting('clip', false);
740 $D('noVNC_clip').disabled = true;
741 } else {
742 $D('noVNC_clip').disabled = connected || UI.isTouchDevice;
743 if (UI.isTouchDevice) {
744 UI.forceSetting('clip', true);
745 }
746 }
747 },
748
f8b399d7 749 // This resize can not be done until we know from the first Frame Buffer Update
750 // if it is supported or not.
751 // The resize is needed to make sure the server desktop size is updated to the
752 // corresponding size of the current local window when reconnecting to an
753 // existing session.
754 FBUComplete: function(rfb, fbu) {
755 UI.onresize();
756 UI.rfb.set_onFBUComplete(function() { });
757 },
758
bbbf42bb
SR
759 // Display the desktop name in the document title
760 updateDocumentTitle: function(rfb, name) {
761 document.title = name + " - noVNC";
762 },
763
764 clipReceive: function(rfb, text) {
765 Util.Debug(">> UI.clipReceive: " + text.substr(0,40) + "...");
766 $D('noVNC_clipboard_text').value = text;
767 Util.Debug("<< UI.clipReceive");
768 },
769
770 connect: function() {
771 UI.closeSettingsMenu();
c506a481 772 UI.toggleConnectPanel();
f00b1e37 773
bbbf42bb
SR
774 var host = $D('noVNC_host').value;
775 var port = $D('noVNC_port').value;
776 var password = $D('noVNC_password').value;
777 var path = $D('noVNC_path').value;
778 if ((!host) || (!port)) {
779 throw new Error("Must set host and port");
780 }
53fc7392 781
d9fc1c7b 782 if (!UI.initRFB()) return;
58ded70d 783
bbbf42bb
SR
784 UI.rfb.set_encrypt(UI.getSetting('encrypt'));
785 UI.rfb.set_true_color(UI.getSetting('true_color'));
786 UI.rfb.set_local_cursor(UI.getSetting('cursor'));
787 UI.rfb.set_shared(UI.getSetting('shared'));
788 UI.rfb.set_view_only(UI.getSetting('view_only'));
789 UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
53fc7392 790
bbbf42bb 791 UI.rfb.connect(host, port, password, path);
8e0f0088 792
bbbf42bb
SR
793 //Close dialog.
794 setTimeout(UI.setBarPosition, 100);
795 $D('noVNC_logo').style.display = "none";
fdedbafb 796 $D('noVNC_container').style.display = "inline";
bbbf42bb 797 },
5299db1a 798
bbbf42bb
SR
799 disconnect: function() {
800 UI.closeSettingsMenu();
801 UI.rfb.disconnect();
8e0f0088 802
f8b399d7 803 // Restore the callback used for initial resize
804 UI.rfb.set_onFBUComplete(UI.FBUComplete);
805
bbbf42bb 806 $D('noVNC_logo').style.display = "block";
fdedbafb 807 $D('noVNC_container').style.display = "none";
808
e543525f 809 // Don't display the connection settings until we're actually disconnected
bbbf42bb
SR
810 },
811
812 displayBlur: function() {
58ded70d
SR
813 if (!UI.rfb) return;
814
bbbf42bb
SR
815 UI.rfb.get_keyboard().set_focused(false);
816 UI.rfb.get_mouse().set_focused(false);
817 },
818
819 displayFocus: function() {
58ded70d
SR
820 if (!UI.rfb) return;
821
bbbf42bb
SR
822 UI.rfb.get_keyboard().set_focused(true);
823 UI.rfb.get_mouse().set_focused(true);
824 },
825
826 clipClear: function() {
827 $D('noVNC_clipboard_text').value = "";
828 UI.rfb.clipboardPasteFrom("");
829 },
830
831 clipSend: function() {
832 var text = $D('noVNC_clipboard_text').value;
833 Util.Debug(">> UI.clipSend: " + text.substr(0,40) + "...");
834 UI.rfb.clipboardPasteFrom(text);
835 Util.Debug("<< UI.clipSend");
836 },
837
838 // Enable/disable and configure viewport clipping
839 setViewClip: function(clip) {
840 var display;
841 if (UI.rfb) {
842 display = UI.rfb.get_display();
843 } else {
844 return;
845 }
8e0f0088 846
bbbf42bb
SR
847 var cur_clip = display.get_viewport();
848
849 if (typeof(clip) !== 'boolean') {
850 // Use current setting
851 clip = UI.getSetting('clip');
852 }
8e0f0088 853
bbbf42bb
SR
854 if (clip && !cur_clip) {
855 // Turn clipping on
856 UI.updateSetting('clip', true);
857 } else if (!clip && cur_clip) {
858 // Turn clipping off
859 UI.updateSetting('clip', false);
860 display.set_viewport(false);
fdedbafb 861 display.set_maxWidth(0);
862 display.set_maxHeight(0);
f8b399d7 863 display.viewportChangeSize();
bbbf42bb
SR
864 }
865 if (UI.getSetting('clip')) {
866 // If clipping, update clipping settings
bbbf42bb 867 display.set_viewport(true);
fdedbafb 868
869 var size = UI.getCanvasLimit();
870 if (size) {
871 display.set_maxWidth(size.w);
872 display.set_maxHeight(size.h);
873
874 // Hide potential scrollbars that can skew the position
875 $D('noVNC_container').style.overflow = "hidden";
876
877 // The x position marks the left margin of the canvas,
878 // remove the margin from both sides to keep it centered
879 var new_w = size.w - (2 * Util.getPosition($D('noVNC_canvas')).x);
880
881 $D('noVNC_container').style.overflow = "visible";
882
883 display.viewportChangeSize(new_w, size.h);
884 }
bbbf42bb
SR
885 }
886 },
887
888 // Toggle/set/unset the viewport drag/move button
889 setViewDrag: function(drag) {
58ded70d 890 if (!UI.rfb) return;
bbbf42bb 891
f8b399d7 892 UI.updateViewDragButton();
8e0f0088 893
bbbf42bb
SR
894 if (typeof(drag) === "undefined" ||
895 typeof(drag) === "object") {
896 // If not specified, then toggle
897 drag = !UI.rfb.get_viewportDrag();
898 }
f8b399d7 899 var vmb = $D('noVNC_view_drag_button');
bbbf42bb
SR
900 if (drag) {
901 vmb.className = "noVNC_status_button_selected";
902 UI.rfb.set_viewportDrag(true);
903 } else {
904 vmb.className = "noVNC_status_button";
905 UI.rfb.set_viewportDrag(false);
906 }
907 },
908
f8b399d7 909 updateViewDragButton: function() {
910 var vmb = $D('noVNC_view_drag_button');
911 if (UI.rfb_state === 'normal' &&
912 UI.rfb.get_display().get_viewport() &&
fdedbafb 913 UI.rfb.get_display().clippingDisplay()) {
f8b399d7 914 vmb.style.display = "inline";
915 } else {
916 vmb.style.display = "none";
917 }
918 },
919
bbbf42bb
SR
920 // On touch devices, show the OS keyboard
921 showKeyboard: function() {
922 var kbi = $D('keyboardinput');
923 var skb = $D('showKeyboard');
924 var l = kbi.value.length;
925 if(UI.keyboardVisible === false) {
926 kbi.focus();
927 try { kbi.setSelectionRange(l, l); } // Move the caret to the end
928 catch (err) {} // setSelectionRange is undefined in Google Chrome
929 UI.keyboardVisible = true;
930 skb.className = "noVNC_status_button_selected";
931 } else if(UI.keyboardVisible === true) {
932 kbi.blur();
933 skb.className = "noVNC_status_button";
934 UI.keyboardVisible = false;
935 }
936 },
937
938 keepKeyboard: function() {
939 clearTimeout(UI.hideKeyboardTimeout);
940 if(UI.keyboardVisible === true) {
941 $D('keyboardinput').focus();
942 $D('showKeyboard').className = "noVNC_status_button_selected";
943 } else if(UI.keyboardVisible === false) {
944 $D('keyboardinput').blur();
945 $D('showKeyboard').className = "noVNC_status_button";
946 }
947 },
948
949 keyboardinputReset: function() {
950 var kbi = $D('keyboardinput');
951 kbi.value = new Array(UI.defaultKeyboardinputLen).join("_");
952 UI.lastKeyboardinput = kbi.value;
953 },
954
955 // When normal keyboard events are left uncought, use the input events from
956 // the keyboardinput element instead and generate the corresponding key events.
957 // This code is required since some browsers on Android are inconsistent in
958 // sending keyCodes in the normal keyboard events when using on screen keyboards.
959 keyInput: function(event) {
3b8ec46f 960
58ded70d 961 if (!UI.rfb) return;
3b8ec46f 962
bbbf42bb 963 var newValue = event.target.value;
cb3e4deb 964 var oldValue = UI.lastKeyboardinput;
bbbf42bb
SR
965
966 var newLen;
967 try {
968 // Try to check caret position since whitespace at the end
969 // will not be considered by value.length in some browsers
970 newLen = Math.max(event.target.selectionStart, newValue.length);
971 } catch (err) {
972 // selectionStart is undefined in Google Chrome
973 newLen = newValue.length;
974 }
975 var oldLen = oldValue.length;
976
977 var backspaces;
978 var inputs = newLen - oldLen;
979 if (inputs < 0) {
980 backspaces = -inputs;
981 } else {
982 backspaces = 0;
983 }
8e0f0088 984
bbbf42bb
SR
985 // Compare the old string with the new to account for
986 // text-corrections or other input that modify existing text
987 var i;
988 for (i = 0; i < Math.min(oldLen, newLen); i++) {
989 if (newValue.charAt(i) != oldValue.charAt(i)) {
990 inputs = newLen - i;
991 backspaces = oldLen - i;
992 break;
993 }
994 }
995
996 // Send the key events
997 for (i = 0; i < backspaces; i++) {
998 UI.rfb.sendKey(XK_BackSpace);
999 }
1000 for (i = newLen - inputs; i < newLen; i++) {
1001 UI.rfb.sendKey(newValue.charCodeAt(i));
1002 }
1003
1004 // Control the text content length in the keyboardinput element
1005 if (newLen > 2 * UI.defaultKeyboardinputLen) {
1006 UI.keyboardinputReset();
1007 } else if (newLen < 1) {
1008 // There always have to be some text in the keyboardinput
1009 // element with which backspace can interact.
1010 UI.keyboardinputReset();
1011 // This sometimes causes the keyboard to disappear for a second
1012 // but it is required for the android keyboard to recognize that
1013 // text has been added to the field
1014 event.target.blur();
1015 // This has to be ran outside of the input handler in order to work
1016 setTimeout(function() { UI.keepKeyboard(); }, 0);
1017 } else {
1018 UI.lastKeyboardinput = newValue;
1019 }
1020 },
1021
1022 keyInputBlur: function() {
1023 $D('showKeyboard').className = "noVNC_status_button";
1024 //Weird bug in iOS if you change keyboardVisible
1025 //here it does not actually occur so next time
1026 //you click keyboard icon it doesnt work.
1027 UI.hideKeyboardTimeout = setTimeout(function() { UI.setKeyboard(); },100);
1028 },
1029
1030 showExtraKeys: function() {
1031 UI.keepKeyboard();
1032 if(UI.extraKeysVisible === false) {
1033 $D('toggleCtrlButton').style.display = "inline";
1034 $D('toggleAltButton').style.display = "inline";
1035 $D('sendTabButton').style.display = "inline";
1036 $D('sendEscButton').style.display = "inline";
1037 $D('showExtraKeysButton').className = "noVNC_status_button_selected";
1038 UI.extraKeysVisible = true;
1039 } else if(UI.extraKeysVisible === true) {
1040 $D('toggleCtrlButton').style.display = "";
1041 $D('toggleAltButton').style.display = "";
1042 $D('sendTabButton').style.display = "";
1043 $D('sendEscButton').style.display = "";
1044 $D('showExtraKeysButton').className = "noVNC_status_button";
1045 UI.extraKeysVisible = false;
1046 }
1047 },
1048
1049 toggleCtrl: function() {
1050 UI.keepKeyboard();
1051 if(UI.ctrlOn === false) {
1052 UI.rfb.sendKey(XK_Control_L, true);
1053 $D('toggleCtrlButton').className = "noVNC_status_button_selected";
1054 UI.ctrlOn = true;
1055 } else if(UI.ctrlOn === true) {
1056 UI.rfb.sendKey(XK_Control_L, false);
1057 $D('toggleCtrlButton').className = "noVNC_status_button";
1058 UI.ctrlOn = false;
1059 }
1060 },
1061
1062 toggleAlt: function() {
1063 UI.keepKeyboard();
1064 if(UI.altOn === false) {
1065 UI.rfb.sendKey(XK_Alt_L, true);
1066 $D('toggleAltButton').className = "noVNC_status_button_selected";
1067 UI.altOn = true;
1068 } else if(UI.altOn === true) {
1069 UI.rfb.sendKey(XK_Alt_L, false);
1070 $D('toggleAltButton').className = "noVNC_status_button";
1071 UI.altOn = false;
1072 }
1073 },
1074
1075 sendTab: function() {
1076 UI.keepKeyboard();
1077 UI.rfb.sendKey(XK_Tab);
1078 },
1079
1080 sendEsc: function() {
1081 UI.keepKeyboard();
1082 UI.rfb.sendKey(XK_Escape);
1083 },
1084
1085 setKeyboard: function() {
1086 UI.keyboardVisible = false;
1087 },
1088
bbbf42bb
SR
1089 //Helper to add options to dropdown.
1090 addOption: function(selectbox, text, value) {
1091 var optn = document.createElement("OPTION");
1092 optn.text = text;
1093 optn.value = value;
1094 selectbox.options.add(optn);
1095 },
1096
1097 setBarPosition: function() {
1098 $D('noVNC-control-bar').style.top = (window.pageYOffset) + 'px';
1099 $D('noVNC_mobile_buttons').style.left = (window.pageXOffset) + 'px';
1100
1101 var vncwidth = $D('noVNC_screen').style.offsetWidth;
1102 $D('noVNC-control-bar').style.width = vncwidth + 'px';
1103 }
1104
1105 };
1106})();