]> git.proxmox.com Git - mirror_novnc.git/blame - include/util.js
Support local scaling
[mirror_novnc.git] / include / util.js
CommitLineData
61dd52c9 1/*
15046f00 2 * noVNC: HTML5 VNC client
d58f8b51 3 * Copyright (C) 2012 Joel Martin
1d728ace 4 * Licensed under MPL 2.0 (see LICENSE.txt)
15046f00
JM
5 *
6 * See README.md for usage and integration instructions.
61dd52c9
JM
7 */
8
d21cd6c1
SR
9/* jshint white: false, nonstandard: true */
10/*global window, console, document, navigator, ActiveXObject, INCLUDE_URI */
61dd52c9 11
15046f00 12// Globals defined here
a59f1cd2 13var Util = {};
15046f00 14
81e5adaf 15
56ec48be
JM
16/*
17 * Make arrays quack
18 */
19
f9fd0313
SR
20var addFunc = function (cl, name, func) {
21 if (!cl.prototype[name]) {
22 Object.defineProperty(cl.prototype, name, { enumerable: false, value: func });
23 }
24};
25
26addFunc(Array, 'push8', function (num) {
d21cd6c1 27 "use strict";
56ec48be 28 this.push(num & 0xFF);
f9fd0313 29});
56ec48be 30
f9fd0313 31addFunc(Array, 'push16', function (num) {
d21cd6c1 32 "use strict";
56ec48be 33 this.push((num >> 8) & 0xFF,
d21cd6c1 34 num & 0xFF);
f9fd0313
SR
35});
36
37addFunc(Array, 'push32', function (num) {
d21cd6c1 38 "use strict";
56ec48be
JM
39 this.push((num >> 24) & 0xFF,
40 (num >> 16) & 0xFF,
41 (num >> 8) & 0xFF,
d21cd6c1 42 num & 0xFF);
f9fd0313 43});
56ec48be 44
32f135d7
JM
45// IE does not support map (even in IE9)
46//This prototype is provided by the Mozilla foundation and
47//is distributed under the MIT license.
48//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
f9fd0313
SR
49addFunc(Array, 'map', function (fun /*, thisp*/) {
50 "use strict";
51 var len = this.length;
52 if (typeof fun != "function") {
53 throw new TypeError();
54 }
55
56 var res = new Array(len);
57 var thisp = arguments[1];
58 for (var i = 0; i < len; i++) {
59 if (i in this) {
60 res[i] = fun.call(thisp, this[i], i, this);
d21cd6c1 61 }
f9fd0313 62 }
d21cd6c1 63
f9fd0313
SR
64 return res;
65});
32f135d7 66
0fe30338 67// IE <9 does not support indexOf
68//This prototype is provided by the Mozilla foundation and
69//is distributed under the MIT license.
70//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
f9fd0313
SR
71addFunc(Array, 'indexOf', function (elt /*, from*/) {
72 "use strict";
73 var len = this.length >>> 0;
74
75 var from = Number(arguments[1]) || 0;
76 from = (from < 0) ? Math.ceil(from) : Math.floor(from);
77 if (from < 0) {
78 from += len;
79 }
80
81 for (; from < len; from++) {
82 if (from in this &&
83 this[from] === elt) {
84 return from;
d21cd6c1 85 }
f9fd0313
SR
86 }
87 return -1;
88});
0fe30338 89
95eb681b
SR
90// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
91if (!Object.keys) {
d21cd6c1
SR
92 Object.keys = (function () {
93 'use strict';
94 var hasOwnProperty = Object.prototype.hasOwnProperty,
95 hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
96 dontEnums = [
97 'toString',
98 'toLocaleString',
99 'valueOf',
100 'hasOwnProperty',
101 'isPrototypeOf',
102 'propertyIsEnumerable',
103 'constructor'
104 ],
105 dontEnumsLength = dontEnums.length;
106
107 return function (obj) {
108 if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
109 throw new TypeError('Object.keys called on non-object');
110 }
95eb681b 111
d21cd6c1 112 var result = [], prop, i;
95eb681b 113
d21cd6c1
SR
114 for (prop in obj) {
115 if (hasOwnProperty.call(obj, prop)) {
116 result.push(prop);
117 }
118 }
95eb681b 119
d21cd6c1
SR
120 if (hasDontEnumBug) {
121 for (i = 0; i < dontEnumsLength; i++) {
122 if (hasOwnProperty.call(obj, dontEnums[i])) {
123 result.push(dontEnums[i]);
124 }
125 }
126 }
127 return result;
128 };
129 })();
130}
95eb681b 131
d21cd6c1
SR
132// PhantomJS 1.x doesn't support bind,
133// so leave this in until PhantomJS 2.0 is released
134//This prototype is provided by the Mozilla foundation and
135//is distributed under the MIT license.
136//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
f9fd0313
SR
137addFunc(Function, 'bind', function (oThis) {
138 if (typeof this !== "function") {
139 // closest thing possible to the ECMAScript 5
140 // internal IsCallable function
141 throw new TypeError("Function.prototype.bind - " +
142 "what is trying to be bound is not callable");
143 }
144
145 var aArgs = Array.prototype.slice.call(arguments, 1),
146 fToBind = this,
147 fNOP = function () {},
148 fBound = function () {
149 return fToBind.apply(this instanceof fNOP && oThis ? this
150 : oThis,
151 aArgs.concat(Array.prototype.slice.call(arguments)));
152 };
d21cd6c1 153
f9fd0313
SR
154 fNOP.prototype = this.prototype;
155 fBound.prototype = new fNOP();
d21cd6c1 156
f9fd0313
SR
157 return fBound;
158});
0fe30338 159
d21cd6c1 160//
34d8b844
JM
161// requestAnimationFrame shim with setTimeout fallback
162//
163
d21cd6c1
SR
164window.requestAnimFrame = (function () {
165 "use strict";
166 return window.requestAnimationFrame ||
167 window.webkitRequestAnimationFrame ||
168 window.mozRequestAnimationFrame ||
169 window.oRequestAnimationFrame ||
170 window.msRequestAnimationFrame ||
171 function (callback) {
34d8b844
JM
172 window.setTimeout(callback, 1000 / 60);
173 };
174})();
175
d21cd6c1 176/*
15046f00
JM
177 * ------------------------------------------------------
178 * Namespaced in Util
179 * ------------------------------------------------------
180 */
181
8db09746
JM
182/*
183 * Logging/debug routines
184 */
185
c1eba48f 186Util._log_level = 'warn';
8db09746 187Util.init_logging = function (level) {
d21cd6c1 188 "use strict";
c1eba48f 189 if (typeof level === 'undefined') {
c1eba48f
JM
190 level = Util._log_level;
191 } else {
192 Util._log_level = level;
193 }
8db09746
JM
194 if (typeof window.console === "undefined") {
195 if (typeof window.opera !== "undefined") {
196 window.console = {
197 'log' : window.opera.postError,
198 'warn' : window.opera.postError,
d21cd6c1
SR
199 'error': window.opera.postError
200 };
8db09746
JM
201 } else {
202 window.console = {
d21cd6c1
SR
203 'log' : function (m) {},
204 'warn' : function (m) {},
205 'error': function (m) {}
206 };
8db09746
JM
207 }
208 }
209
210 Util.Debug = Util.Info = Util.Warn = Util.Error = function (msg) {};
d21cd6c1 211 /* jshint -W086 */
8db09746 212 switch (level) {
d21cd6c1
SR
213 case 'debug':
214 Util.Debug = function (msg) { console.log(msg); };
215 case 'info':
216 Util.Info = function (msg) { console.log(msg); };
217 case 'warn':
218 Util.Warn = function (msg) { console.warn(msg); };
219 case 'error':
220 Util.Error = function (msg) { console.error(msg); };
8db09746
JM
221 case 'none':
222 break;
223 default:
d21cd6c1 224 throw new Error("invalid logging type '" + level + "'");
8db09746 225 }
d21cd6c1 226 /* jshint +W086 */
8db09746 227};
c1eba48f 228Util.get_logging = function () {
8d5d2c82 229 return Util._log_level;
d3796c14 230};
8db09746 231// Initialize logging level
c1eba48f 232Util.init_logging();
8db09746 233
d21cd6c1
SR
234Util.make_property = function (proto, name, mode, type) {
235 "use strict";
a8edf9d8 236
d21cd6c1
SR
237 var getter;
238 if (type === 'arr') {
239 getter = function (idx) {
240 if (typeof idx !== 'undefined') {
241 return this['_' + name][idx];
242 } else {
243 return this['_' + name];
244 }
245 };
246 } else {
247 getter = function () {
248 return this['_' + name];
249 };
250 }
5210330a 251
d21cd6c1
SR
252 var make_setter = function (process_val) {
253 if (process_val) {
254 return function (val, idx) {
255 if (typeof idx !== 'undefined') {
256 this['_' + name][idx] = process_val(val);
257 } else {
258 this['_' + name] = process_val(val);
259 }
260 };
5210330a 261 } else {
d21cd6c1
SR
262 return function (val, idx) {
263 if (typeof idx !== 'undefined') {
264 this['_' + name][idx] = val;
265 } else {
266 this['_' + name] = val;
267 }
268 };
5210330a
JM
269 }
270 };
271
d21cd6c1
SR
272 var setter;
273 if (type === 'bool') {
274 setter = make_setter(function (val) {
275 if (!val || (val in {'0': 1, 'no': 1, 'false': 1})) {
276 return false;
d890e864 277 } else {
d21cd6c1 278 return true;
d890e864 279 }
d21cd6c1
SR
280 });
281 } else if (type === 'int') {
282 setter = make_setter(function (val) { return parseInt(val, 10); });
283 } else if (type === 'float') {
284 setter = make_setter(parseFloat);
285 } else if (type === 'str') {
286 setter = make_setter(String);
287 } else if (type === 'func') {
288 setter = make_setter(function (val) {
5210330a 289 if (!val) {
d21cd6c1
SR
290 return function () {};
291 } else {
292 return val;
5210330a 293 }
d21cd6c1
SR
294 });
295 } else if (type === 'arr' || type === 'dom' || type == 'raw') {
296 setter = make_setter();
297 } else {
298 throw new Error('Unknown property type ' + type); // some sanity checking
299 }
5210330a 300
d21cd6c1
SR
301 // set the getter
302 if (typeof proto['get_' + name] === 'undefined') {
303 proto['get_' + name] = getter;
125d8bbb 304 }
d890e864 305
d21cd6c1
SR
306 // set the setter if needed
307 if (typeof proto['set_' + name] === 'undefined') {
308 if (mode === 'rw') {
309 proto['set_' + name] = setter;
310 } else if (mode === 'wo') {
311 proto['set_' + name] = function (val, idx) {
312 if (typeof this['_' + name] !== 'undefined') {
313 throw new Error(name + " can only be set once");
314 }
315 setter.call(this, val, idx);
316 };
317 }
125d8bbb 318 }
ff36b127 319
d21cd6c1
SR
320 // make a special setter that we can use in set defaults
321 proto['_raw_set_' + name] = function (val, idx) {
322 setter.call(this, val, idx);
323 //delete this['_init_set_' + name]; // remove it after use
324 };
325};
326
327Util.make_properties = function (constructor, arr) {
328 "use strict";
329 for (var i = 0; i < arr.length; i++) {
330 Util.make_property(constructor.prototype, arr[i][0], arr[i][1], arr[i][2]);
ff36b127 331 }
125d8bbb
JM
332};
333
d21cd6c1
SR
334Util.set_defaults = function (obj, conf, defaults) {
335 var defaults_keys = Object.keys(defaults);
336 var conf_keys = Object.keys(conf);
337 var keys_obj = {};
5210330a 338 var i;
d21cd6c1
SR
339 for (i = 0; i < defaults_keys.length; i++) { keys_obj[defaults_keys[i]] = 1; }
340 for (i = 0; i < conf_keys.length; i++) { keys_obj[conf_keys[i]] = 1; }
341 var keys = Object.keys(keys_obj);
342
343 for (i = 0; i < keys.length; i++) {
344 var setter = obj['_raw_set_' + keys[i]];
7caa9c20
MA
345 if (!setter) {
346 Util.Warn('Invalid property ' + keys[i]);
347 continue;
348 }
d21cd6c1 349
cfc02e5e 350 if (keys[i] in conf) {
d21cd6c1
SR
351 setter.call(obj, conf[keys[i]]);
352 } else {
353 setter.call(obj, defaults[keys[i]]);
354 }
5210330a 355 }
ff4bfcb7 356};
125d8bbb 357
b7996b04 358/*
359 * Decode from UTF-8
360 */
d21cd6c1
SR
361Util.decodeUTF8 = function (utf8string) {
362 "use strict";
b7996b04 363 return decodeURIComponent(escape(utf8string));
d21cd6c1 364};
b7996b04 365
366
a8edf9d8 367
15046f00
JM
368/*
369 * Cross-browser routines
370 */
371
6f4b1e40
JM
372
373// Dynamically load scripts without using document.write()
374// Reference: http://unixpapa.com/js/dyna.html
375//
376// Handles the case where load_scripts is invoked from a script that
377// itself is loaded via load_scripts. Once all scripts are loaded the
378// window.onscriptsloaded handler is called (if set).
d21cd6c1 379Util.get_include_uri = function () {
6f4b1e40 380 return (typeof INCLUDE_URI !== "undefined") ? INCLUDE_URI : "include/";
d21cd6c1 381};
73ee4fa7 382Util._loading_scripts = [];
6f4b1e40 383Util._pending_scripts = [];
d21cd6c1
SR
384Util.load_scripts = function (files) {
385 "use strict";
73ee4fa7
JM
386 var head = document.getElementsByTagName('head')[0], script,
387 ls = Util._loading_scripts, ps = Util._pending_scripts;
d21cd6c1
SR
388
389 var loadFunc = function (e) {
390 while (ls.length > 0 && (ls[0].readyState === 'loaded' ||
391 ls[0].readyState === 'complete')) {
392 // For IE, append the script to trigger execution
393 var s = ls.shift();
394 //console.log("loaded script: " + s.src);
395 head.appendChild(s);
396 }
397 if (!this.readyState ||
398 (Util.Engine.presto && this.readyState === 'loaded') ||
399 this.readyState === 'complete') {
400 if (ps.indexOf(this) >= 0) {
401 this.onload = this.onreadystatechange = null;
402 //console.log("completed script: " + this.src);
403 ps.splice(ps.indexOf(this), 1);
404
405 // Call window.onscriptsload after last script loads
406 if (ps.length === 0 && window.onscriptsload) {
407 window.onscriptsload();
408 }
409 }
410 }
411 };
412
413 for (var f = 0; f < files.length; f++) {
73ee4fa7
JM
414 script = document.createElement('script');
415 script.type = 'text/javascript';
416 script.src = Util.get_include_uri() + files[f];
417 //console.log("loading script: " + script.src);
d21cd6c1 418 script.onload = script.onreadystatechange = loadFunc;
73ee4fa7
JM
419 // In-order script execution tricks
420 if (Util.Engine.trident) {
421 // For IE wait until readyState is 'loaded' before
422 // appending it which will trigger execution
423 // http://wiki.whatwg.org/wiki/Dynamic_Script_Execution_Order
424 ls.push(script);
425 } else {
426 // For webkit and firefox set async=false and append now
427 // https://developer.mozilla.org/en-US/docs/HTML/Element/script
428 script.async = false;
429 head.appendChild(script);
430 }
8e2d7496 431 ps.push(script);
6f4b1e40 432 }
d21cd6c1 433};
6f4b1e40 434
c77938ef 435
455f8f3f 436Util.getPosition = function(obj) {
d21cd6c1 437 "use strict";
72747869
SR
438 // NB(sross): the Mozilla developer reference seems to indicate that
439 // getBoundingClientRect includes border and padding, so the canvas
440 // style should NOT include either.
455f8f3f 441 var objPosition = obj.getBoundingClientRect();
72747869
SR
442 return {'x': objPosition.left + window.pageXOffset, 'y': objPosition.top + window.pageYOffset,
443 'width': objPosition.width, 'height': objPosition.height};
455f8f3f 444};
c77938ef 445
15046f00
JM
446
447// Get mouse event position in DOM element
125d8bbb 448Util.getEventPosition = function (e, obj, scale) {
d21cd6c1 449 "use strict";
15046f00
JM
450 var evt, docX, docY, pos;
451 //if (!e) evt = window.event;
452 evt = (e ? e : window.event);
ad3f7624 453 evt = (evt.changedTouches ? evt.changedTouches[0] : evt.touches ? evt.touches[0] : evt);
15046f00
JM
454 if (evt.pageX || evt.pageY) {
455 docX = evt.pageX;
456 docY = evt.pageY;
457 } else if (evt.clientX || evt.clientY) {
458 docX = evt.clientX + document.body.scrollLeft +
459 document.documentElement.scrollLeft;
460 docY = evt.clientY + document.body.scrollTop +
461 document.documentElement.scrollTop;
462 }
463 pos = Util.getPosition(obj);
125d8bbb
JM
464 if (typeof scale === "undefined") {
465 scale = 1;
466 }
ca9a9964
PD
467 var realx = docX - pos.x;
468 var realy = docY - pos.y;
72747869
SR
469 var x = Math.max(Math.min(realx, pos.width - 1), 0);
470 var y = Math.max(Math.min(realy, pos.height - 1), 0);
ca9a9964 471 return {'x': x / scale, 'y': y / scale, 'realx': realx / scale, 'realy': realy / scale};
15046f00
JM
472};
473
474
475// Event registration. Based on: http://www.scottandrew.com/weblog/articles/cbs-events
d21cd6c1
SR
476Util.addEvent = function (obj, evType, fn) {
477 "use strict";
478 if (obj.attachEvent) {
479 var r = obj.attachEvent("on" + evType, fn);
15046f00 480 return r;
d21cd6c1
SR
481 } else if (obj.addEventListener) {
482 obj.addEventListener(evType, fn, false);
d93d3e09 483 return true;
15046f00 484 } else {
d21cd6c1 485 throw new Error("Handler could not be attached");
15046f00
JM
486 }
487};
488
d21cd6c1
SR
489Util.removeEvent = function (obj, evType, fn) {
490 "use strict";
491 if (obj.detachEvent) {
492 var r = obj.detachEvent("on" + evType, fn);
15046f00 493 return r;
d21cd6c1 494 } else if (obj.removeEventListener) {
d93d3e09
JM
495 obj.removeEventListener(evType, fn, false);
496 return true;
15046f00 497 } else {
d21cd6c1 498 throw new Error("Handler could not be removed");
15046f00
JM
499 }
500};
501
d21cd6c1
SR
502Util.stopEvent = function (e) {
503 "use strict";
15046f00
JM
504 if (e.stopPropagation) { e.stopPropagation(); }
505 else { e.cancelBubble = true; }
506
507 if (e.preventDefault) { e.preventDefault(); }
508 else { e.returnValue = false; }
509};
510
511
512// Set browser engine versions. Based on mootools.
513Util.Features = {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)};
514
d21cd6c1
SR
515(function () {
516 "use strict";
517 // 'presto': (function () { return (!window.opera) ? false : true; }()),
518 var detectPresto = function () {
519 return !!window.opera;
520 };
521
522 // 'trident': (function () { return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4);
523 var detectTrident = function () {
524 if (!window.ActiveXObject) {
525 return false;
526 } else {
527 if (window.XMLHttpRequest) {
528 return (document.querySelectorAll) ? 6 : 5;
529 } else {
530 return 4;
531 }
532 }
533 };
534
535 // 'webkit': (function () { try { return (navigator.taintEnabled) ? false : ((Util.Features.xpath) ? ((Util.Features.query) ? 525 : 420) : 419); } catch (e) { return false; } }()),
536 var detectInitialWebkit = function () {
537 try {
538 if (navigator.taintEnabled) {
539 return false;
540 } else {
541 if (Util.Features.xpath) {
542 return (Util.Features.query) ? 525 : 420;
543 } else {
544 return 419;
545 }
546 }
547 } catch (e) {
548 return false;
549 }
550 };
551
552 var detectActualWebkit = function (initial_ver) {
553 var re = /WebKit\/([0-9\.]*) /;
554 var str_ver = (navigator.userAgent.match(re) || ['', initial_ver])[1];
555 return parseFloat(str_ver, 10);
556 };
557
558 // 'gecko': (function () { return (!document.getBoxObjectFor && window.mozInnerScreenX == null) ? false : ((document.getElementsByClassName) ? 19ssName) ? 19 : 18 : 18); }())
559 var detectGecko = function () {
560 /* jshint -W041 */
561 if (!document.getBoxObjectFor && window.mozInnerScreenX == null) {
562 return false;
563 } else {
564 return (document.getElementsByClassName) ? 19 : 18;
565 }
566 /* jshint +W041 */
567 };
568
569 Util.Engine = {
570 // Version detection break in Opera 11.60 (errors on arguments.callee.caller reference)
571 //'presto': (function() {
572 // return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925)); }()),
573 'presto': detectPresto(),
574 'trident': detectTrident(),
575 'webkit': detectInitialWebkit(),
576 'gecko': detectGecko(),
577 };
578
579 if (Util.Engine.webkit) {
580 // Extract actual webkit version if available
581 Util.Engine.webkit = detectActualWebkit(Util.Engine.webkit);
582 }
583})();
15046f00 584
d21cd6c1
SR
585Util.Flash = (function () {
586 "use strict";
15046f00
JM
587 var v, version;
588 try {
589 v = navigator.plugins['Shockwave Flash'].description;
d21cd6c1 590 } catch (err1) {
15046f00
JM
591 try {
592 v = new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
d21cd6c1 593 } catch (err2) {
15046f00
JM
594 v = '0 r0';
595 }
596 }
597 version = v.match(/\d+/g);
598 return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0};
d21cd6c1 599}());