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