diff options
author | Michael Bishop <mbtyke@yahoo.com> | 2014-01-12 12:46:35 -0500 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2014-06-10 02:00:06 +0300 |
commit | b71f0def47d8ffbbaffcdaccba20713db5f0f5d1 (patch) | |
tree | fdb6f91389860dbbe1dbb17e3e66edf5a8e0526d /src/library_sdl.js | |
parent | fee203ed899368e0bd0d6bd6c88c72b0ca8d74e8 (diff) |
Added an additional call: `SDL_SetEventHandler()` to SDL.
This is not in the official API but it is needed for Mozilla
Persona, which makes sure an event handler is on the stack when
`navigator.id.request()` is called.
Using the standard SDL api, this can never be the case because an SDL
app retrieves events from SDL’s event queue independently of when
those events are added to the queue (through event handlers that SDL
itself registers).
With this additional call, apps can receive events directly in
response to an actual event handler which will still be on the stack
when the event is handled by the SDL-based application.
Diffstat (limited to 'src/library_sdl.js')
-rw-r--r-- | src/library_sdl.js | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 7145a7ba..53adf8eb 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -84,6 +84,9 @@ var LibrarySDL = { TOUCH_DEFAULT_ID: 0, // Our default deviceID for touch events (we get nothing from the browser) + eventHandler: null, + eventHandlerContext: null, + keyCodes: { // DOM code ==> SDL code. See https://developer.mozilla.org/en/Document_Object_Model_%28DOM%29/KeyboardEvent and SDL_keycode.h // For keys that don't have unicode value, we map DOM codes with the corresponding scan codes + 1024 (using "| 1 << 10") 16: 225 | 1<<10, // shift @@ -699,6 +702,9 @@ var LibrarySDL = { Module.printErr('SDL event queue full, dropping events'); SDL.events = SDL.events.slice(0, 10000); } + // If we have a handler installed, this will push the events to the app + // instead of the app polling for them. + SDL.flushEventsToHandler(); return; }, @@ -749,6 +755,30 @@ var LibrarySDL = { } } }, + flushEventsToHandler: function() { + if (!SDL.eventHandler) { + return; + } + + // All SDLEvents take the same amount of memory + var sdlEventPtr = allocate({{{ C_STRUCTS.SDL_KeyboardEvent.__size__ }}}, "i8", ALLOC_STACK); + + while (SDL.pollEvent(sdlEventPtr)) { + Runtime.dynCall('iii', SDL.eventHandler, [SDL.eventHandlerContext, sdlEventPtr]); + } + }, + pollEvent: function(ptr) { + if (SDL.initFlags & 0x200 && SDL.joystickEventState) { + // If SDL_INIT_JOYSTICK was supplied AND the joystick system is configured + // to automatically query for events, query for joystick events. + SDL.queryJoysticks(); + } + if (SDL.events.length === 0) return 0; + if (ptr) { + SDL.makeCEvent(SDL.events.shift(), ptr); + } + return 1; + }, makeCEvent: function(event, ptr) { if (typeof event === 'number') { @@ -1709,16 +1739,7 @@ var LibrarySDL = { }, SDL_PollEvent: function(ptr) { - if (SDL.initFlags & 0x200 && SDL.joystickEventState) { - // If SDL_INIT_JOYSTICK was supplied AND the joystick system is configured - // to automatically query for events, query for joystick events. - SDL.queryJoysticks(); - } - if (SDL.events.length === 0) return 0; - if (ptr) { - SDL.makeCEvent(SDL.events.shift(), ptr); - } - return 1; + return SDL.pollEvent(ptr); }, SDL_PushEvent: function(ptr) { @@ -1758,6 +1779,11 @@ var LibrarySDL = { SDL.handleEvent(event); }); }, + + SDL_SetEventHandler: function(_handler, _userdata){ + SDL.eventHandler = _handler; + SDL.eventHandlerContext = _userdata; + }, SDL_SetColors: function(surf, colors, firstColor, nColors) { var surfData = SDL.surfaces[surf]; |