diff options
author | Ehsan Akhgari <ehsan.akhgari@gmail.com> | 2013-02-27 20:36:14 -0500 |
---|---|---|
committer | Ehsan Akhgari <ehsan.akhgari@gmail.com> | 2013-02-27 20:36:14 -0500 |
commit | e1a0116a871bf96243472d79135eb624dc9c82c3 (patch) | |
tree | 7f14659e4855acc42e9752a73606efaed27ba382 | |
parent | 42da4e19f9d72026be6642c8b54f646c6d828dde (diff) |
Implement SDL_TEXTINPUT
-rw-r--r-- | src/library_sdl.js | 31 | ||||
-rwxr-xr-x | tests/runner.py | 22 | ||||
-rw-r--r-- | tests/sdl_text.c | 40 |
3 files changed, 90 insertions, 3 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 3b412daa..67891f63 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -44,6 +44,8 @@ var LibrarySDL = { ctrlKey: false, altKey: false, + textInput: false, + startTime: null, mouseX: 0, mouseY: 0, @@ -173,6 +175,11 @@ var LibrarySDL = { ['i16', 'mod'], ['i32', 'unicode'] ]), + TextInputEvent: Runtime.generateStructInfo([ + ['i32', 'type'], + ['i32', 'windowID'], + ['b256', 'text'], + ]), MouseMotionEvent: Runtime.generateStructInfo([ ['i32', 'type'], ['i32', 'windowID'], @@ -373,7 +380,7 @@ var LibrarySDL = { } } // fall through - case 'keydown': case 'keyup': case 'mousedown': case 'mouseup': case 'DOMMouseScroll': case 'mousewheel': + case 'keydown': case 'keyup': case 'keypress': case 'mousedown': case 'mouseup': case 'DOMMouseScroll': case 'mousewheel': if (event.type == 'DOMMouseScroll' || event.type == 'mousewheel') { var button = (event.type == 'DOMMouseScroll' ? event.detail : -event.wheelDelta) > 0 ? 4 : 3; var event2 = { @@ -397,6 +404,10 @@ var LibrarySDL = { SDL.DOMButtons[event.button] = 0; } + if (event.type == 'keypress' && !SDL.textInput) { + break; + } + SDL.events.push(event); if (SDL.events.length >= 10000) { Module.printErr('SDL event queue full, dropping earliest event'); @@ -476,6 +487,15 @@ var LibrarySDL = { break; } + case 'keypress': { + {{{ makeSetValue('ptr', 'SDL.structs.TextInputEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}} + // Not filling in windowID for now + var cStr = intArrayFromString(String.fromCharCode(event.charCode)); + for (var i = 0; i < cStr.length; ++i) { + {{{ makeSetValue('ptr', 'SDL.structs.TextInputEvent.text + i', 'cStr[i]', 'i8') }}}; + } + break; + } case 'mousedown': case 'mouseup': if (event.type == 'mousedown') { // SDL_BUTTON(x) is defined as (1 << ((x)-1)). SDL buttons are 1-3, @@ -618,6 +638,7 @@ var LibrarySDL = { // Initialize this structure carefully for closure SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */; SDL.DOMEventToSDLEvent['keyup'] = 0x301 /* SDL_KEYUP */; + SDL.DOMEventToSDLEvent['keypress'] = 0x303 /* SDL_TEXTINPUT */; SDL.DOMEventToSDLEvent['mousedown'] = 0x401 /* SDL_MOUSEBUTTONDOWN */; SDL.DOMEventToSDLEvent['mouseup'] = 0x402 /* SDL_MOUSEBUTTONUP */; SDL.DOMEventToSDLEvent['mousemove'] = 0x400 /* SDL_MOUSEMOTION */; @@ -1175,8 +1196,12 @@ var LibrarySDL = { SDL_CondWait: function() {}, SDL_DestroyCond: function() {}, - SDL_StartTextInput: function() {}, // TODO - SDL_StopTextInput: function() {}, // TODO + SDL_StartTextInput: function() { + SDL.textInput = true; + }, + SDL_StopTextInput: function() { + SDL.textInput = false; + }, // SDL Mixer diff --git a/tests/runner.py b/tests/runner.py index 50568762..d1d65d6f 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -10370,6 +10370,28 @@ elif 'browser' in str(sys.argv): 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') + def test_sdl_text(self): + open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' + Module.postRun = function() { + function doOne() { + Module._one(); + setTimeout(doOne, 1000/60); + } + setTimeout(doOne, 1000/60); + } + + function simulateKeyEvent(charCode) { + var event = document.createEvent("KeyboardEvent"); + event.initKeyEvent("keypress", true, true, window, + 0, 0, 0, 0, 0, charCode); + document.body.dispatchEvent(event); + } + ''') + open(os.path.join(self.get_dir(), 'sdl_text.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_text.c')).read())) + + Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_text.c'), '-o', 'page.html', '--pre-js', 'pre.js', '-s', '''EXPORTED_FUNCTIONS=['_main', '_one']''']).communicate() + self.run_browser('page.html', '', '/report_result?1') + def test_sdl_mouse(self): open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' function simulateMouseEvent(x, y, button) { diff --git a/tests/sdl_text.c b/tests/sdl_text.c new file mode 100644 index 00000000..6bcb0d61 --- /dev/null +++ b/tests/sdl_text.c @@ -0,0 +1,40 @@ +#include <SDL/SDL.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> +#include <emscripten.h> + +int result = 0; + +void one() { + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_TEXTEDITING: assert(0); break; + case SDL_TEXTINPUT: + printf("Received %s\n", event.text.text); + if (!strcmp("a", event.text.text)) { + result = 1; + } else if (!strcmp("A", event.text.text)) { + 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() { + SDL_Init(SDL_INIT_VIDEO); + SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE); + SDL_StartTextInput(); + + emscripten_run_script("simulateKeyEvent('a'.charCodeAt(0))"); // a + emscripten_run_script("simulateKeyEvent('A'.charCodeAt(0))"); // A + + one(); + + return 0; +} |