diff options
-rw-r--r-- | src/library_sdl.js | 96 | ||||
-rwxr-xr-x | tests/runner.py | 13 | ||||
-rw-r--r-- | tests/sdl_key.c | 43 |
3 files changed, 107 insertions, 45 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 1c8ac52d..a131f424 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -47,6 +47,7 @@ var LibrarySDL = { startTime: null, buttonState: 0, + modState: 0, DOMButtons: [0, 0, 0], DOMEventToSDLEvent: {}, @@ -373,7 +374,8 @@ var LibrarySDL = { if (event['movementX'] == 0 && event['movementY'] == 0) { // ignore a mousemove event if it doesn't contain any movement info // (without pointer lock, we infer movement from pageX/pageY, so this check is unnecessary) - return false; + event.preventDefault(); + return; } } // fall through @@ -396,15 +398,20 @@ var LibrarySDL = { } else if (event.type == 'mousedown') { SDL.DOMButtons[event.button] = 1; } else if (event.type == 'mouseup') { - if (!SDL.DOMButtons[event.button]) return false; // ignore extra ups, can happen if we leave the canvas while pressing down, then return, - // since we add a mouseup in that case + // ignore extra ups, can happen if we leave the canvas while pressing down, then return, + // since we add a mouseup in that case + if (!SDL.DOMButtons[event.button]) { + event.preventDefault(); + return; + } + SDL.DOMButtons[event.button] = 0; } if (event.type == 'keypress' && !SDL.textInput) { break; } - + SDL.events.push(event); break; case 'mouseout': @@ -438,7 +445,7 @@ var LibrarySDL = { // Force-run a main event loop, since otherwise this event will never be caught! Browser.mainLoop.runner(); } - return true; + return; case 'resize': SDL.events.push(event); break; @@ -447,7 +454,11 @@ var LibrarySDL = { Module.printErr('SDL event queue full, dropping events'); SDL.events = SDL.events.slice(0, 10000); } - return false; + // manually triggered resize event doesn't have a preventDefault member + if (event.preventDefault) { + event.preventDefault(); + } + return; }, makeCEvent: function(event, ptr) { @@ -473,15 +484,6 @@ var LibrarySDL = { } else { scan = SDL.scanCodes[key] || key; } - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', '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', 'scan', 'i32') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.sym', 'key', 'i32') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.mod', '0', 'i32') }}} - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.unicode', 'key', 'i32') }}} var code = SDL.keyCodes[event.keyCode] || event.keyCode; {{{ makeSetValue('SDL.keyboardState', 'code', 'down', 'i8') }}}; @@ -491,6 +493,19 @@ var LibrarySDL = { delete SDL.keyboardMap[code]; } + // TODO: lmeta, rmeta, numlock, capslock, KMOD_MODE, KMOD_RESERVED + SDL.modState = ({{{ makeGetValue('SDL.keyboardState', '1248', 'i8') }}} ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL + ({{{ makeGetValue('SDL.keyboardState', '1249', 'i8') }}} ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT + ({{{ makeGetValue('SDL.keyboardState', '1250', 'i8') }}} ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT + + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', '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', 'scan', 'i32') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.sym', 'key', 'i32') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.mod', 'SDL.modState', 'i32') }}} + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.unicode', 'key', 'i32') }}} + break; } case 'keypress': { @@ -614,13 +629,13 @@ var LibrarySDL = { SDL.startTime = Date.now(); // capture all key events. we just keep down and up, but also capture press to prevent default actions if (!Module['doNotCaptureKeyboard']) { - document.onkeydown = SDL.receiveEvent; - document.onkeyup = SDL.receiveEvent; - document.onkeypress = SDL.receiveEvent; - document.onblur = SDL.receiveEvent; + document.addEventListener("keydown", SDL.receiveEvent); + document.addEventListener("keyup", SDL.receiveEvent); + document.addEventListener("keypress", SDL.receiveEvent); + document.addEventListener("blur", SDL.receiveEvent); document.addEventListener("visibilitychange", SDL.receiveEvent); } - window.onunload = SDL.receiveEvent; + window.addEventListener("unload", SDL.receiveEvent); SDL.keyboardState = _malloc(0x10000); // Our SDL needs 512, but 64K is safe for older SDLs _memset(SDL.keyboardState, 0, 0x10000); // Initialize this structure carefully for closure @@ -888,12 +903,16 @@ var LibrarySDL = { SDL_GetKeyState: function() { return _SDL_GetKeyboardState(); }, + + SDL_GetKeyName: function(key) { + if (!SDL.keyName) { + SDL.keyName = allocate(intArrayFromString('unknown key'), 'i8', ALLOC_NORMAL); + } + return SDL.keyName; + }, SDL_GetModState: function() { - // TODO: numlock, capslock, etc. - return (SDL.keyboardState[16] ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT - (SDL.keyboardState[17] ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL - (SDL.keyboardState[18] ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT + return SDL.modState; }, SDL_GetMouseState: function(x, y) { @@ -1103,6 +1122,20 @@ var LibrarySDL = { return (a&0xff)+((b&0xff)<<8)+((g&0xff)<<16)+((r&0xff)<<24) }, + SDL_GetAppState: function() { + var state = 0; + + if (Browser.pointerLock) { + state |= 0x01; // SDL_APPMOUSEFOCUS + } + if (document.hasFocus()) { + state |= 0x02; // SDL_APPINPUTFOCUS + } + state |= 0x04; // SDL_APPACTIVE + + return state; + }, + SDL_WM_GrabInput: function() {}, SDL_WM_ToggleFullScreen: function(surf) { @@ -1755,6 +1788,10 @@ var LibrarySDL = { return -1; }, + SDL_SetGammaRamp: function (redTable, greenTable, blueTable) { + return -1; + }, + // Misc SDL_InitSubSystem: function(flags) { return 0 }, @@ -1792,7 +1829,7 @@ var LibrarySDL = { SDL_FreeRW: function() { throw 'SDL_FreeRW: TODO' }, SDL_CondBroadcast: function() { throw 'SDL_CondBroadcast: TODO' }, SDL_CondWaitTimeout: function() { throw 'SDL_CondWaitTimeout: TODO' }, - SDL_WM_ToggleFullScreen: function() { throw 'SDL_WM_ToggleFullScreen: TODO' }, + SDL_WM_IconifyWindow: function() { throw 'SDL_WM_IconifyWindow TODO' }, Mix_SetPostMix: function() { throw 'Mix_SetPostMix: TODO' }, Mix_QuerySpec: function() { throw 'Mix_QuerySpec: TODO' }, @@ -1802,6 +1839,15 @@ var LibrarySDL = { Mix_Linked_Version: function() { throw 'Mix_Linked_Version: TODO' }, SDL_CreateRGBSurfaceFrom: function() { throw 'SDL_CreateRGBSurfaceFrom: TODO' }, SDL_SaveBMP_RW: function() { throw 'SDL_SaveBMP_RW: TODO' }, + + SDL_HasRDTSC: function() { return 0; }, + SDL_HasMMX: function() { return 0; }, + SDL_HasMMXExt: function() { return 0; }, + SDL_Has3DNow: function() { return 0; }, + SDL_Has3DNowExt: function() { return 0; }, + SDL_HasSSE: function() { return 0; }, + SDL_HasSSE2: function() { return 0; }, + SDL_HasAltiVec: function() { return 0; } }; autoAddDeps(LibrarySDL, '$SDL'); diff --git a/tests/runner.py b/tests/runner.py index 58332aed..5c5e3152 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -12112,23 +12112,26 @@ elif 'browser' in str(sys.argv): setTimeout(doOne, 1000/60); } - function simulateKeyEvent(c) { + function keydown(c) { var event = document.createEvent("KeyboardEvent"); event.initKeyEvent("keydown", true, true, window, 0, 0, 0, 0, c, c); document.dispatchEvent(event); - var event2 = document.createEvent("KeyboardEvent"); - event2.initKeyEvent("keyup", true, true, window, + } + + function keyup(c) { + var event = document.createEvent("KeyboardEvent"); + event.initKeyEvent("keyup", true, true, window, 0, 0, 0, 0, c, c); - document.dispatchEvent(event2); + document.dispatchEvent(event); } ''') 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', '-s', '''EXPORTED_FUNCTIONS=['_main', '_one']''']).communicate() - self.run_browser('page.html', '', '/report_result?510510') + self.run_browser('page.html', '', '/report_result?223092870') def test_sdl_text(self): open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' diff --git a/tests/sdl_key.c b/tests/sdl_key.c index 19b0a3d6..7a304fc1 100644 --- a/tests/sdl_key.c +++ b/tests/sdl_key.c @@ -12,16 +12,30 @@ void one() { case SDL_KEYDOWN: break; case SDL_KEYUP: + // don't handle the modifier key events + if (event.key.keysym.sym == SDLK_LCTRL || + event.key.keysym.sym == SDLK_LSHIFT || + event.key.keysym.sym == SDLK_LALT) { + return; + } + if ((event.key.keysym.mod & KMOD_LCTRL) || (event.key.keysym.mod & KMOD_RCTRL)) { + result *= 2; + } + if ((event.key.keysym.mod & KMOD_LSHIFT) || (event.key.keysym.mod & KMOD_RSHIFT)) { + result *= 3; + } + if ((event.key.keysym.mod & KMOD_LALT) || (event.key.keysym.mod & KMOD_RALT)) { + result *= 5; + } 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; + case SDLK_RIGHT: printf("right\n"); result *= 7; break; + case SDLK_LEFT: printf("left\n"); result *= 11; break; + case SDLK_DOWN: printf("down\n"); result *= 13; break; + case SDLK_UP: printf("up\n"); result *= 17; break; + case SDLK_a: printf("a\n"); result *= 19; break; default: { if (event.key.keysym.scancode == SDL_SCANCODE_B) { - printf("b scancode\n"); result *= 17; break; + printf("b scancode\n"); result *= 23; break; } printf("unknown key: sym %d scancode %d\n", event.key.keysym.sym, event.key.keysym.scancode); REPORT_RESULT(); @@ -40,14 +54,13 @@ 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)"); // up - emscripten_run_script("simulateKeyEvent(40)"); // down - emscripten_run_script("simulateKeyEvent(37)"); // left - emscripten_run_script("simulateKeyEvent(39)"); // right - emscripten_run_script("simulateKeyEvent(32)"); // space - emscripten_run_script("simulateKeyEvent(65)"); // a - emscripten_run_script("simulateKeyEvent(66)"); // b - emscripten_run_script("simulateKeyEvent(100)"); // trigger the end + emscripten_run_script("keydown(1250);keydown(38);keyup(38);keyup(1250);"); // alt, up + emscripten_run_script("keydown(1248);keydown(1249);keydown(40);keyup(40);keyup(1249);keyup(1248);"); // ctrl, shift, down + emscripten_run_script("keydown(37);keyup(37);"); // left + emscripten_run_script("keydown(39);keyup(39);"); // right + emscripten_run_script("keydown(65);keyup(65);"); // a + emscripten_run_script("keydown(66);keyup(66);"); // b + emscripten_run_script("keydown(100);keyup(100);"); // trigger the end if (argc == 1337) one(); // keep it alive |