aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-08-30 14:49:12 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-08-30 14:49:12 -0700
commit249e75b46f07ebca6cce1b648b5d34bc4a75792c (patch)
treeef2889dc90b6347d7e9d58f6c46f678e302af5ec
parent2a193932e89d6371913d6fdcbb55a072508a8140 (diff)
support for SDL_QUIT event
-rw-r--r--src/library_browser.js8
-rw-r--r--src/library_sdl.js11
-rwxr-xr-xtests/runner.py21
-rw-r--r--tests/sdl_quit.c33
4 files changed, 61 insertions, 12 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index e6a8b7c6..51c0cf78 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -384,7 +384,7 @@ mergeInto(LibraryManager.library, {
Module['noExitRuntime'] = true;
var jsFunc = FUNCTION_TABLE[func];
- var wrapper = function() {
+ Browser.mainLoop.runner = function() {
if (Browser.mainLoop.queue.length > 0) {
var start = Date.now();
var blocker = Browser.mainLoop.queue.shift();
@@ -402,7 +402,7 @@ mergeInto(LibraryManager.library, {
}
console.log('main loop blocker "' + blocker.name + '" took ' + (Date.now() - start) + ' ms'); //, left: ' + Browser.mainLoop.remainingBlockers);
Browser.mainLoop.updateStatus();
- setTimeout(wrapper, 0);
+ setTimeout(Browser.mainLoop.runner, 0);
return;
}
if (Browser.mainLoop.shouldPause) {
@@ -436,11 +436,11 @@ mergeInto(LibraryManager.library, {
}
if (fps && fps > 0) {
Browser.mainLoop.scheduler = function() {
- setTimeout(wrapper, 1000/fps); // doing this each time means that on exception, we stop
+ setTimeout(Browser.mainLoop.runner, 1000/fps); // doing this each time means that on exception, we stop
}
} else {
Browser.mainLoop.scheduler = function() {
- Browser.requestAnimationFrame(wrapper);
+ Browser.requestAnimationFrame(Browser.mainLoop.runner);
}
}
Browser.mainLoop.scheduler();
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 48c18c8b..8cb8db72 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -394,6 +394,11 @@ var LibrarySDL = {
}
}
break;
+ case 'unload':
+ SDL.events.push(event);
+ // Force-run a main event loop, since otherwise this event will never be caught!
+ Browser.mainLoop.runner();
+ return true;
}
return false;
},
@@ -488,6 +493,10 @@ var LibrarySDL = {
SDL.mouseY = y;
break;
}
+ case 'unload': {
+ {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
+ break;
+ }
default: throw 'Unhandled SDL event: ' + event.type;
}
},
@@ -557,6 +566,7 @@ var LibrarySDL = {
document.onkeydown = SDL.receiveEvent;
document.onkeyup = SDL.receiveEvent;
document.onkeypress = SDL.receiveEvent;
+ window.onunload = SDL.receiveEvent;
SDL.keyboardState = _malloc(0x10000);
_memset(SDL.keyboardState, 0, 0x10000);
// Initialize this structure carefully for closure
@@ -565,6 +575,7 @@ var LibrarySDL = {
SDL.DOMEventToSDLEvent['mousedown'] = 0x401 /* SDL_MOUSEBUTTONDOWN */;
SDL.DOMEventToSDLEvent['mouseup'] = 0x402 /* SDL_MOUSEBUTTONUP */;
SDL.DOMEventToSDLEvent['mousemove'] = 0x400 /* SDL_MOUSEMOTION */;
+ SDL.DOMEventToSDLEvent['unload'] = 0x100 /* SDL_QUIT */;
return 0; // success
},
diff --git a/tests/runner.py b/tests/runner.py
index 72eb2e7c..ec2f290f 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -7649,15 +7649,17 @@ elif 'browser' in str(sys.argv):
print '(moving on..)'
def with_report_result(self, code):
- return code.replace('REPORT_RESULT();', '''
- char output[1000];
- sprintf(output,
- "xhr = new XMLHttpRequest();"
- "xhr.open('GET', 'http://localhost:8888/report_result?%d');"
- "xhr.send();", result);
- emscripten_run_script(output);
+ return '''
+ #define REPORT_RESULT_INTERNAL(sync) \
+ char output[1000]; \
+ sprintf(output, \
+ "xhr = new XMLHttpRequest();" \
+ "xhr.open('GET', 'http://localhost:8888/report_result?%d'%s);" \
+ "xhr.send();", result, sync ? ", false" : ""); \
+ emscripten_run_script(output); \
emscripten_run_script("setTimeout(function() { window.close() }, 1000)");
-''')
+ #define REPORT_RESULT() REPORT_RESULT_INTERNAL(0)
+''' + code
def reftest(self, expected):
basename = os.path.basename(expected)
@@ -8157,6 +8159,9 @@ elif 'browser' in str(sys.argv):
shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) # preloaded *after* run
self.btest('emscripten_fs_api_browser.cpp', '1')
+ def test_sdl_quit(self):
+ self.btest('sdl_quit.c', '1')
+
def test_gc(self):
self.btest('browser_gc.cpp', '1')
diff --git a/tests/sdl_quit.c b/tests/sdl_quit.c
new file mode 100644
index 00000000..1a07526f
--- /dev/null
+++ b/tests/sdl_quit.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_ttf.h>
+#include <assert.h>
+#include <emscripten.h>
+
+int result = 0;
+
+void one() {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_QUIT: {
+ if (!result) { // prevent infinite recursion since REPORT_RESULT does window.close too.
+ result = 1;
+ REPORT_RESULT_INTERNAL(1);
+ }
+ }
+ }
+ }
+}
+
+void main_2();
+
+int main() {
+ SDL_Init(SDL_INIT_VIDEO);
+ SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
+
+ emscripten_set_main_loop(one, 0);
+
+ emscripten_run_script("setTimeout(function() { window.close() }, 2000)");
+}
+