From 8211b8f93ff366c76ac9bbf650fee78afe7da7e0 Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Fri, 18 May 2012 19:42:46 +0200 Subject: [PATCH] Add love.joystick.reload, remove love.joystick.open and friends. (issue #392) Reload originates from love-experiments, as noted in the ticket. --- src/modules/joystick/sdl/Joystick.cpp | 25 +++++++++++++- src/modules/joystick/sdl/Joystick.h | 1 + src/modules/joystick/sdl/wrap_Joystick.cpp | 38 ++++++++-------------- src/modules/joystick/sdl/wrap_Joystick.h | 4 +-- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/modules/joystick/sdl/Joystick.cpp b/src/modules/joystick/sdl/Joystick.cpp index 9209571c3..57d15e419 100644 --- a/src/modules/joystick/sdl/Joystick.cpp +++ b/src/modules/joystick/sdl/Joystick.cpp @@ -34,7 +34,7 @@ namespace sdl { // Init the SDL joystick system. if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) - throw Exception(SDL_GetError()); + throw love::Exception("%s", SDL_GetError()); // Start joystick event watching. SDL_JoystickEventState(SDL_ENABLE); @@ -61,6 +61,29 @@ namespace sdl SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } + void Joystick::reload() + { + // Closes any open joysticks. + for (int i = 0; i != getNumJoysticks(); i++) + { + if (isOpen(i)) + close(i); + } + + free(joysticks); + + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) + throw love::Exception("%s", SDL_GetError()); + + int numjoysticks = this->getNumJoysticks(); + this->joysticks = (SDL_Joystick **)calloc(numjoysticks, sizeof(SDL_Joystick*)); + + for (int i = 0;iopen(i); + } + const char * Joystick::getName() const { return "love.joystick.sdl"; diff --git a/src/modules/joystick/sdl/Joystick.h b/src/modules/joystick/sdl/Joystick.h index 069bcd74d..a7fa873cb 100644 --- a/src/modules/joystick/sdl/Joystick.h +++ b/src/modules/joystick/sdl/Joystick.h @@ -45,6 +45,7 @@ namespace sdl // Implements Module. const char * getName() const; + void reload(); bool checkIndex(int index); int getNumJoysticks(); const char * getName(int index); diff --git a/src/modules/joystick/sdl/wrap_Joystick.cpp b/src/modules/joystick/sdl/wrap_Joystick.cpp index 0ae653fff..1131365f4 100644 --- a/src/modules/joystick/sdl/wrap_Joystick.cpp +++ b/src/modules/joystick/sdl/wrap_Joystick.cpp @@ -28,6 +28,19 @@ namespace sdl { static Joystick * instance = 0; + int w_reload(lua_State * L) + { + try + { + instance->reload(); + } + catch (love::Exception & e) + { + return luaL_error(L, "%s", e.what()); + } + return 0; + } + int w_getNumJoysticks(lua_State * L) { lua_pushinteger(L, instance->getNumJoysticks()); @@ -41,20 +54,6 @@ namespace sdl return 1; } - int w_open(lua_State * L) - { - int index = luaL_checkint(L, 1)-1; - luax_pushboolean(L, instance->open(index)); - return 1; - } - - int w_isOpen(lua_State * L) - { - int index = luaL_checkint(L, 1)-1; - luax_pushboolean(L, instance->isOpen(index)); - return 1; - } - int w_getNumAxes(lua_State * L) { int index = luaL_checkint(L, 1)-1; @@ -133,19 +132,11 @@ namespace sdl return 1; } - int w_close(lua_State * L) - { - int index = luaL_checkint(L, 1)-1; - instance->close(index); - return 0; - } - // List of functions to wrap. static const luaL_Reg functions[] = { + { "reload", w_reload }, { "getNumJoysticks", w_getNumJoysticks }, { "getName", w_getName }, - { "open", w_open }, - { "isOpen", w_isOpen }, { "getNumAxes", w_getNumAxes }, { "getNumBalls", w_getNumBalls }, { "getNumButtons", w_getNumButtons }, @@ -157,7 +148,6 @@ namespace sdl { "isDown", w_isDown }, { "getHat", w_getHat }, - { "close", w_close }, { 0, 0 } }; diff --git a/src/modules/joystick/sdl/wrap_Joystick.h b/src/modules/joystick/sdl/wrap_Joystick.h index e5970a3ae..cdccbaf5c 100644 --- a/src/modules/joystick/sdl/wrap_Joystick.h +++ b/src/modules/joystick/sdl/wrap_Joystick.h @@ -31,10 +31,9 @@ namespace joystick { namespace sdl { + int w_reload(lua_State * L); int w_getNumJoysticks(lua_State * L); int w_getName(lua_State * L); - int w_open(lua_State * L); - int w_isOpen(lua_State * L); int w_getNumAxes(lua_State * L); int w_getNumBalls(lua_State * L); int w_getNumButtons(lua_State * L); @@ -44,7 +43,6 @@ namespace sdl int w_getBall(lua_State * L); int w_isDown(lua_State * L); int w_getHat(lua_State * L); - int w_close(lua_State * L); extern "C" LOVE_EXPORT int luaopen_love_joystick(lua_State * L); } // sdl