diff options
Diffstat (limited to 'src/library_browser.js')
-rw-r--r-- | src/library_browser.js | 72 |
1 files changed, 56 insertions, 16 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 5f9c4d06..a690286b 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -6,7 +6,8 @@ mergeInto(LibraryManager.library, { $Browser__postset: 'Module["requestFullScreen"] = function(lockPointer, resizeCanvas) { Browser.requestFullScreen(lockPointer, resizeCanvas) };\n' + // exports 'Module["requestAnimationFrame"] = function(func) { Browser.requestAnimationFrame(func) };\n' + 'Module["pauseMainLoop"] = function() { Browser.mainLoop.pause() };\n' + - 'Module["resumeMainLoop"] = function() { Browser.mainLoop.resume() };\n', + 'Module["resumeMainLoop"] = function() { Browser.mainLoop.resume() };\n' + + 'Module["getUserMedia"] = function() { Browser.getUserMedia() }', $Browser: { mainLoop: { scheduler: null, @@ -184,7 +185,7 @@ mergeInto(LibraryManager.library, { }; audio.src = url; // workaround for chrome bug 124926 - we do not always get oncanplaythrough or onerror - setTimeout(function() { + Browser.safeSetTimeout(function() { finish(audio); // try to use it even though it is not necessarily ready to play }, 10000); } else { @@ -201,7 +202,8 @@ mergeInto(LibraryManager.library, { canvas['webkitRequestPointerLock']; canvas.exitPointerLock = document['exitPointerLock'] || document['mozExitPointerLock'] || - document['webkitExitPointerLock']; + document['webkitExitPointerLock'] || + function(){}; // no-op if function does not exist canvas.exitPointerLock = canvas.exitPointerLock.bind(document); function pointerLockChange() { @@ -345,7 +347,7 @@ mergeInto(LibraryManager.library, { canvas.requestFullScreen = canvas['requestFullScreen'] || canvas['mozRequestFullScreen'] || (canvas['webkitRequestFullScreen'] ? function() { canvas['webkitRequestFullScreen'](Element['ALLOW_KEYBOARD_INPUT']) } : null); - canvas.requestFullScreen(); + canvas.requestFullScreen(); }, requestAnimationFrame: function(func) { @@ -360,6 +362,38 @@ mergeInto(LibraryManager.library, { window.requestAnimationFrame(func); }, + // generic abort-aware wrapper for an async callback + safeCallback: function(func) { + return function() { + if (!ABORT) return func.apply(null, arguments); + }; + }, + + // abort-aware versions + safeRequestAnimationFrame: function(func) { + Browser.requestAnimationFrame(function() { + if (!ABORT) func(); + }); + }, + safeSetTimeout: function(func, timeout) { + setTimeout(function() { + if (!ABORT) func(); + }, timeout); + }, + safeSetInterval: function(func, timeout) { + setInterval(function() { + if (!ABORT) func(); + }, timeout); + }, + + getUserMedia: function(func) { + if(!window.getUserMedia) { + window.getUserMedia = navigator['getUserMedia'] || + navigator['mozGetUserMedia']; + } + window.getUserMedia(func); + }, + getMovementX: function(event) { return event['movementX'] || event['mozMovementX'] || @@ -474,7 +508,7 @@ mergeInto(LibraryManager.library, { {{{ makeSetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'flags', 'i32') }}} Browser.updateResizeListeners(); }, - + setWindowedCanvasSize: function() { var canvas = Module['canvas']; canvas.width = this.windowedWidth; @@ -484,7 +518,7 @@ mergeInto(LibraryManager.library, { {{{ makeSetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'flags', 'i32') }}} Browser.updateResizeListeners(); } - + }, emscripten_async_wget: function(url, file, onload, onerror) { @@ -521,11 +555,11 @@ mergeInto(LibraryManager.library, { var _request = Pointer_stringify(request); var _param = Pointer_stringify(param); var index = _file.lastIndexOf('/'); - + var http = new XMLHttpRequest(); http.open(_request, _url, true); http.responseType = 'arraybuffer'; - + // LOAD http.onload = function(e) { if (http.status == 200) { @@ -535,20 +569,20 @@ mergeInto(LibraryManager.library, { if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]); } }; - + // ERROR http.onerror = function(e) { if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]); }; - + // PROGRESS http.onprogress = function(e) { var percentComplete = (e.position / e.totalSize)*100; if (onprogress) Runtime.dynCall('vii', onprogress, [arg, percentComplete]); }; - + // Useful because the browser can limit the number of redirection - try { + try { if (http.channel instanceof Ci.nsIHttpChannel) http.channel.redirectionLimit = 0; } catch (ex) { /* whatever */ } @@ -563,7 +597,7 @@ mergeInto(LibraryManager.library, { http.send(null); } }, - + emscripten_async_prepare: function(file, onload, onerror) { var _file = Pointer_stringify(file); var data = FS.analyzePath(_file); @@ -611,7 +645,7 @@ mergeInto(LibraryManager.library, { Module['noExitRuntime'] = true; // TODO: cache these to avoid generating garbage - setTimeout(function() { + Browser.safeSetTimeout(function() { _emscripten_run_script(script); }, millis); }, @@ -620,6 +654,7 @@ mergeInto(LibraryManager.library, { Module['noExitRuntime'] = true; Browser.mainLoop.runner = function() { + if (ABORT) return; if (Browser.mainLoop.queue.length > 0) { var start = Date.now(); var blocker = Browser.mainLoop.queue.shift(); @@ -722,12 +757,17 @@ mergeInto(LibraryManager.library, { } if (millis >= 0) { - setTimeout(wrapper, millis); + Browser.safeSetTimeout(wrapper, millis); } else { - Browser.requestAnimationFrame(wrapper); + Browser.safeRequestAnimationFrame(wrapper); } }, + emscripten_exit_with_live_runtime: function() { + Module['noExitRuntime'] = true; + throw 'SimulateInfiniteLoop'; + }, + emscripten_hide_mouse: function() { var styleSheet = document.styleSheets[0]; var rules = styleSheet.cssRules; |