aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-09-08 22:14:04 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-09-08 22:14:04 -0700
commit70fbf7d8d5879c2349cb5c5d6ae2278dcf3d5a51 (patch)
tree7f03cc3e997e5cb64dd89bb6dfc10b92e152a69a /src
parent971e59fe7306042f90c85fe0dff04d666b1f01ab (diff)
proxy input events
Diffstat (limited to 'src')
-rw-r--r--src/postamble.js4
-rw-r--r--src/proxyClient.js28
-rw-r--r--src/proxyWorker.js51
3 files changed, 82 insertions, 1 deletions
diff --git a/src/postamble.js b/src/postamble.js
index df844121..d3212767 100644
--- a/src/postamble.js
+++ b/src/postamble.js
@@ -10,8 +10,8 @@ ExitStatus.prototype = new Error();
ExitStatus.prototype.constructor = ExitStatus;
var initialStackTop;
-
var preloadStartTime = null;
+var calledMain = false;
Module['callMain'] = Module.callMain = function callMain(args) {
assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on __ATMAIN__)');
@@ -70,6 +70,8 @@ Module['callMain'] = Module.callMain = function callMain(args) {
} else {
throw e;
}
+ } finally {
+ calledMain = true;
}
}
diff --git a/src/proxyClient.js b/src/proxyClient.js
index bbf3e278..41f6a501 100644
--- a/src/proxyClient.js
+++ b/src/proxyClient.js
@@ -41,3 +41,31 @@ worker.onmessage = function(event) {
}
};
+function cloneEvent(event) {
+ var ret = {};
+ for (var x in event) {
+ if (x == x.toUpperCase()) continue;
+ var prop = event[x];
+ if (typeof prop === 'number' || typeof prop === 'string') ret[x] = prop;
+ }
+ return ret;
+};
+
+['keydown', 'keyup', 'keypress', 'blur', 'visibilitychange'].forEach(function(event) {
+ document.addEventListener(event, function(event) {
+ worker.postMessage({ target: 'document', event: cloneEvent(event) });
+ });
+});
+
+['unload'].forEach(function(event) {
+ window.addEventListener(event, function(event) {
+ worker.postMessage({ target: 'window', event: cloneEvent(event) });
+ });
+});
+
+['mousedown', 'mouseup', 'mousemove', 'DOMMouseScroll', 'mousewheel', 'mouseout'].forEach(function(event) {
+ Module.canvas.addEventListener(event, function(event) {
+ worker.postMessage({ target: 'canvas', event: cloneEvent(event) });
+ }, true);
+});
+
diff --git a/src/proxyWorker.js b/src/proxyWorker.js
index 5cdc4455..8d641b0d 100644
--- a/src/proxyWorker.js
+++ b/src/proxyWorker.js
@@ -6,6 +6,16 @@ function EventListener() {
if (!this.listeners[event]) this.listeners[event] = [];
this.listeners[event].push(func);
};
+
+ this.fireEvent = function(event) {
+ event.preventDefault = function(){};
+
+ if (event.type in this.listeners) {
+ this.listeners[event.type].forEach(function(listener) {
+ listener(event);
+ });
+ }
+ };
};
var window = this;
@@ -75,3 +85,44 @@ Module.printErr = function(x) {
postMessage({ target: 'stderr', content: x });
};
+// buffer messages until the program starts to run
+
+var messageBuffer = null;
+
+function messageResender() {
+ if (calledMain) {
+ assert(messageBuffer && messageBuffer.length > 0);
+ messageBuffer.forEach(function(message) {
+ onmessage(message);
+ });
+ messageBuffer = null;
+ } else {
+ setTimeout(messageResender, 100);
+ }
+}
+
+onmessage = function(message) {
+ if (!calledMain) {
+ if (!messageBuffer) {
+ messageBuffer = [];
+ setTimeout(messageResender, 100);
+ }
+ messageBuffer.push(message);
+ }
+ switch (message.data.target) {
+ case 'document': {
+ document.fireEvent(message.data.event);
+ break;
+ }
+ case 'window': {
+ window.fireEvent(message.data.event);
+ break;
+ }
+ case 'canvas': {
+ Module.canvas.fireEvent(message.data.event);
+ break;
+ }
+ default: throw 'wha? ' + message.data.target;
+ }
+};
+