aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library_sdl.js28
-rw-r--r--src/shell.html5
-rwxr-xr-xtests/runner.py28
-rw-r--r--tests/sdl_key.c47
4 files changed, 94 insertions, 14 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 65dbd28c..c26b50fa 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -92,10 +92,10 @@ mergeInto(LibraryManager.library, {
keyboardState: null,
keyCodes: { // DOM code ==> SDL code
- 38: 273, // up arrow
- 40: 274, // down arrow
- 37: 276, // left arrow
- 39: 275, // right arrow
+ 38: 1073741906, // up arrow
+ 40: 1073741905, // down arrow
+ 37: 1073741904, // left arrow
+ 39: 1073741903, // right arrow
17: 305, // control (right, or left)
18: 308, // alt
109: 45, // minus
@@ -113,16 +113,19 @@ mergeInto(LibraryManager.library, {
['i32', 'Rmask'], ['i32', 'Gmask'], ['i32', 'Bmask'], ['i32', 'Amask'] // Docs say i8, ./include/SDL_video.h says i32...
]),
KeyboardEvent: Runtime.generateStructInfo([
- ['i8', 'type'],
- ['i8', 'which'],
+ ['i32', 'type'],
+ ['i32', 'windowID'],
['i8', 'state'],
+ ['i8', 'repeat'],
+ ['i8', 'padding2'],
+ ['i8', 'padding3'],
['i32', 'keysym']
]),
keysym: Runtime.generateStructInfo([
- ['i8', 'scancode'],
+ ['i32', 'scancode'],
['i32', 'sym'],
- ['i32', 'mod'],
- ['i16', 'unicode']
+ ['i16', 'mod'],
+ ['i32', 'unicode']
]),
AudioSpec: Runtime.generateStructInfo([
['i32', 'freq'],
@@ -235,7 +238,6 @@ mergeInto(LibraryManager.library, {
receiveEvent: function(event) {
switch(event.type) {
case 'keydown': case 'keyup':
- //print('zz receive Event: ' + event.keyCode);
SDL.events.push(event);
{{{ makeSetValue('SDL.keyboardState', 'SDL.keyCodes[event.keyCode] || event.keyCode', 'event.type == "keydown"', 'i8') }}};
}
@@ -257,10 +259,10 @@ mergeInto(LibraryManager.library, {
if (key >= 65 && key <= 90) {
key = String.fromCharCode(key).toLowerCase().charCodeAt(0);
}
- //print('zz passing over Event: ' + key);
- {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'down ? 2 : 3', 'i8') }}}
- {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.which', '1', 'i8') }}}
+ {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'down ? 0x300 : 0x301', 'i32') }}}
+ //{{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.which', '1', 'i32') }}}
{{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.state', 'down ? 1 : 0', 'i8') }}}
+ {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.repeat', '0', 'i8') }}} // TODO
{{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.scancode', 'key', 'i8') }}}
{{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.sym', 'key', 'i32') }}}
diff --git a/src/shell.html b/src/shell.html
index 2f83f9f9..79b7e9b9 100644
--- a/src/shell.html
+++ b/src/shell.html
@@ -15,12 +15,15 @@
var Module = {
print: (function() {
var element = document.getElementById('output');
+ var printBuffer = [];
return function(text) {
text = text.replace(/&/g, "&amp;");
text = text.replace(/</g, "&lt;");
text = text.replace(/>/g, "&gt;");
text = text.replace('\n', '<br>', 'g');
- element.innerHTML += text + '<br>';
+ if (printBuffer.length > 10) printBuffer.shift();
+ printBuffer.push(text);
+ element.innerHTML = printBuffer.join('<br>');
};
})(),
canvas: document.getElementById('canvas'),
diff --git a/tests/runner.py b/tests/runner.py
index 66934895..7691d41b 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -6381,6 +6381,34 @@ f.close()
Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_font.c'), '-o', 'page.html']).communicate()
self.run_browser('page.html', '', '/report_result?80')
+ def test_sdl_key(self):
+ open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''
+ Module.postRun = function() {
+ function doOne() {
+ _one();
+ setTimeout(doOne, 1000/60);
+ }
+ setTimeout(doOne, 1000/60);
+ }
+
+ function simulateKeyEvent(c) {
+ var event = document.createEvent("KeyboardEvent");
+ event.initKeyEvent("keydown", true, true, window,
+ 0, 0, 0, 0,
+ c, c);
+ dispatchEvent(event);
+ var event2 = document.createEvent("KeyboardEvent");
+ event2.initKeyEvent("keyup", true, true, window,
+ 0, 0, 0, 0,
+ c, c);
+ dispatchEvent(event2);
+ }
+ ''')
+ open(os.path.join(self.get_dir(), 'sdl_key.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_key.c')).read()))
+
+ Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_key.c'), '-o', 'page.html', '--pre-js', 'pre.js']).communicate()
+ self.run_browser('page.html', '', '/report_result?30030')
+
def test_worker(self):
# Test running in a web worker
output = Popen(['python', EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'], stdout=PIPE, stderr=PIPE).communicate()
diff --git a/tests/sdl_key.c b/tests/sdl_key.c
new file mode 100644
index 00000000..633e975f
--- /dev/null
+++ b/tests/sdl_key.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_ttf.h>
+#include <emscripten.h>
+
+int result = 1;
+
+void one() {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_KEYDOWN:
+ break;
+ case SDL_KEYUP:
+ switch (event.key.keysym.sym) {
+ case SDLK_RIGHT: printf("right\n"); result *= 2; break;
+ case SDLK_LEFT: printf("left\n"); result *= 3; break;
+ case SDLK_DOWN: printf("down\n"); result *= 5; break;
+ case SDLK_UP: printf("up\n"); result *= 7; break;
+ case SDLK_SPACE: printf("space\n"); result *= 11; break;
+ case SDLK_a: printf("a\n"); result *= 13; break;
+ default: { REPORT_RESULT(); emscripten_run_script("throw 'done'"); }
+ }
+ break;
+ default: /* Report an unhandled event */
+ printf("I don't know what this event is!\n");
+ }
+ }
+}
+
+int main(int argc, char **argv) {
+ SDL_Init(SDL_INIT_VIDEO);
+ SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
+
+ emscripten_run_script("simulateKeyEvent(38)");
+ emscripten_run_script("simulateKeyEvent(40)");
+ emscripten_run_script("simulateKeyEvent(37)");
+ emscripten_run_script("simulateKeyEvent(39)");
+ emscripten_run_script("simulateKeyEvent(32)");
+ emscripten_run_script("simulateKeyEvent(97)");
+ emscripten_run_script("simulateKeyEvent(100)"); // trigger the end
+
+ if (argc == 1337) one(); // keep it alive
+
+ return 0;
+}
+