Commits

Alex Szpakowski committed 606212e

Moved the love.joystick Lua wrapper code from modules/joystick/sdl/ to modules/joystick/.

  • Participants
  • Parent commits 40ec801

Comments (0)

Files changed (10)

File CMakeLists.txt

 	src/modules/joystick/Joystick.cpp
 	src/modules/joystick/Joystick.h
 	src/modules/joystick/JoystickModule.h
+	src/modules/joystick/wrap_Joystick.cpp
+	src/modules/joystick/wrap_Joystick.h
+	src/modules/joystick/wrap_JoystickModule.cpp
+	src/modules/joystick/wrap_JoystickModule.h
 )
 
 set(LOVE_SRC_MODULE_JOYSTICK_SDL
 	src/modules/joystick/sdl/Joystick.h
 	src/modules/joystick/sdl/JoystickModule.cpp
 	src/modules/joystick/sdl/JoystickModule.h
-	src/modules/joystick/sdl/wrap_Joystick.cpp
-	src/modules/joystick/sdl/wrap_Joystick.h
-	src/modules/joystick/sdl/wrap_JoystickModule.cpp
-	src/modules/joystick/sdl/wrap_JoystickModule.h
 )
 
 set(LOVE_SRC_MODULE_JOYSTICK

File platform/macosx/love-framework.xcodeproj/project.pbxproj

 				FAB0078E1740C12D00A9664D /* Joystick.h */,
 				4C606AEC342457600C3F0741 /* JoystickModule.h */,
 				687216AD6FA406C838284B91 /* sdl */,
+				FAB007951740C87D00A9664D /* wrap_Joystick.cpp */,
+				FAB007961740C87D00A9664D /* wrap_Joystick.h */,
+				139411436818381E493F00F5 /* wrap_JoystickModule.cpp */,
+				2D7B7DEC4FC87878332E41B3 /* wrap_JoystickModule.h */,
 			);
 			path = joystick;
 			sourceTree = "<group>";
 				FAB007921740C28900A9664D /* Joystick.h */,
 				55B425307C0C1C4B3EFC3A5F /* JoystickModule.cpp */,
 				439E46D768A266780E894800 /* JoystickModule.h */,
-				FAB007951740C87D00A9664D /* wrap_Joystick.cpp */,
-				FAB007961740C87D00A9664D /* wrap_Joystick.h */,
-				139411436818381E493F00F5 /* wrap_JoystickModule.cpp */,
-				2D7B7DEC4FC87878332E41B3 /* wrap_JoystickModule.h */,
 			);
 			path = sdl;
 			sourceTree = "<group>";

File src/modules/joystick/sdl/wrap_Joystick.cpp

-/**
- * Copyright (c) 2006-2014 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
-
-// LOVE
-#include "wrap_Joystick.h"
-#include "wrap_JoystickModule.h"
-
-#include <vector>
-
-namespace love
-{
-namespace joystick
-{
-namespace sdl
-{
-
-Joystick *luax_checkjoystick(lua_State *L, int idx)
-{
-	return luax_checktype<Joystick>(L, idx, "Joystick", JOYSTICK_JOYSTICK_T);
-}
-
-int w_Joystick_isConnected(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	luax_pushboolean(L, j->isConnected());
-	return 1;
-}
-
-int w_Joystick_getName(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	lua_pushstring(L, j->getName());
-	return 1;
-}
-
-int w_Joystick_getID(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-
-	// IDs are 1-based in Lua.
-	lua_pushinteger(L, j->getID() + 1);
-
-	int instanceid = j->getInstanceID();
-	if (instanceid >= 0)
-		lua_pushinteger(L, instanceid + 1);
-	else
-		lua_pushnil(L);
-
-	return 2;
-}
-
-int w_Joystick_getGUID(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	luax_pushstring(L, j->getGUID());
-	return 1;
-}
-
-int w_Joystick_getAxisCount(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	lua_pushinteger(L, j->getAxisCount());
-	return 1;
-}
-
-int w_Joystick_getButtonCount(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	lua_pushinteger(L, j->getButtonCount());
-	return 1;
-}
-
-int w_Joystick_getHatCount(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	lua_pushinteger(L, j->getHatCount());
-	return 1;
-}
-
-int w_Joystick_getAxis(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	int axisindex = luaL_checkint(L, 2) - 1;
-	lua_pushnumber(L, j->getAxis(axisindex));
-	return 1;
-}
-
-int w_Joystick_getAxes(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	std::vector<float> axes = j->getAxes();
-
-	for (size_t i = 0; i < axes.size(); i++)
-		lua_pushnumber(L, axes[i]);
-
-	return (int) axes.size();
-}
-
-int w_Joystick_getHat(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	int hatindex = luaL_checkint(L, 2) - 1;
-
-	Joystick::Hat h = j->getHat(hatindex);
-
-	const char *direction = "";
-	love::joystick::Joystick::getConstant(h, direction);
-
-	lua_pushstring(L, direction);
-	return 1;
-}
-
-int w_Joystick_isDown(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-
-	luaL_checkinteger(L, 2);
-
-	std::vector<int> buttons;
-	for (int i = 2; i <= lua_gettop(L); i++)
-		buttons.push_back(luaL_checkint(L, i) - 1);
-
-	luax_pushboolean(L, j->isDown(buttons));
-	return 1;
-}
-
-int w_Joystick_isGamepad(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	luax_pushboolean(L, j->isGamepad());
-	return 1;
-}
-
-int w_Joystick_getGamepadAxis(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-
-	const char *str = luaL_checkstring(L, 2);
-	Joystick::GamepadAxis axis;
-
-	if (!joystick::Joystick::getConstant(str, axis))
-		return luaL_error(L, "Invalid gamepad axis: %s", str);
-
-	lua_pushnumber(L, j->getGamepadAxis(axis));
-	return 1;
-}
-
-int w_Joystick_isGamepadDown(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-
-	std::vector<Joystick::GamepadButton> buttons;
-	buttons.reserve(lua_gettop(L) - 1);
-
-	luaL_checkstring(L, 2);
-
-	for (int i = 2; i <= lua_gettop(L); i++)
-	{
-		const char *str = luaL_checkstring(L, i);
-		Joystick::GamepadButton button;
-
-		if (!joystick::Joystick::getConstant(str, button))
-			return luaL_error(L, "Invalid gamepad button: %s", str);
-
-		buttons.push_back(button);
-	}
-
-	luax_pushboolean(L, j->isGamepadDown(buttons));
-	return 1;
-}
-
-int w_Joystick_isVibrationSupported(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	luax_pushboolean(L, j->isVibrationSupported());
-	return 1;
-}
-
-int w_Joystick_setVibration(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	bool success = false;
-
-	if (lua_isnoneornil(L, 2))
-	{
-		// Disable joystick vibration if no argument is given.
-		success = j->setVibration();
-	}
-	else
-	{
-		float left = (float) luaL_checknumber(L, 2);
-		float right = (float) luaL_optnumber(L, 3, left);
-		float duration = (float) luaL_optnumber(L, 4, -1.0); // -1 is infinite.
-		success = j->setVibration(left, right, duration);
-	}
-
-	luax_pushboolean(L, success);
-	return 1;
-}
-
-int w_Joystick_getVibration(lua_State *L)
-{
-	Joystick *j = luax_checkjoystick(L, 1);
-	float left, right;
-	j->getVibration(left, right);
-	lua_pushnumber(L, left);
-	lua_pushnumber(L, right);
-	return 2;
-}
-
-// List of functions to wrap.
-static const luaL_Reg functions[] =
-{
-	{ "isConnected", w_Joystick_isConnected },
-	{ "getName", w_Joystick_getName },
-	{ "getID", w_Joystick_getID },
-	{ "getGUID", w_Joystick_getGUID },
-	{ "getAxisCount", w_Joystick_getAxisCount },
-	{ "getButtonCount", w_Joystick_getButtonCount },
-	{ "getHatCount", w_Joystick_getHatCount },
-	{ "getAxis", w_Joystick_getAxis },
-	{ "getAxes", w_Joystick_getAxes },
-	{ "getHat", w_Joystick_getHat },
-	{ "isDown", w_Joystick_isDown },
-
-	{ "isGamepad", w_Joystick_isGamepad },
-	{ "getGamepadAxis", w_Joystick_getGamepadAxis },
-	{ "isGamepadDown", w_Joystick_isGamepadDown },
-
-	{ "isVibrationSupported", w_Joystick_isVibrationSupported },
-	{ "setVibration", w_Joystick_setVibration },
-	{ "getVibration", w_Joystick_getVibration },
-
-	// From wrap_JoystickModule.
-	{ "getConnectedIndex", w_getIndex },
-	{ "getGamepadMapping", w_getGamepadMapping },
-	{ "saveGamepadMapping", w_saveGamepadMapping },
-
-	{ 0, 0 },
-};
-
-extern "C" int luaopen_joystick(lua_State *L)
-{
-	return luax_register_type(L, "Joystick", functions);
-}
-
-} // sdl
-} // joystick
-} // love

File src/modules/joystick/sdl/wrap_Joystick.h

-/**
- * Copyright (c) 2006-2014 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
-
-#ifndef LOVE_JOYSTICK_SDL_WRAP_JOYSTICK_H
-#define LOVE_JOYSTICK_SDL_WRAP_JOYSTICK_H
-
-// LOVE
-#include "common/config.h"
-#include "Joystick.h"
-#include "common/runtime.h"
-
-namespace love
-{
-namespace joystick
-{
-namespace sdl
-{
-
-Joystick *luax_checkjoystick(lua_State *L, int idx);
-int w_Joystick_isConnected(lua_State *L);
-int w_Joystick_getName(lua_State *L);
-int w_Joystick_getID(lua_State *L);
-int w_Joystick_getGUID(lua_State *L);
-int w_Joystick_getAxisCount(lua_State *L);
-int w_Joystick_getButtonCount(lua_State *L);
-int w_Joystick_getHatCount(lua_State *L);
-int w_Joystick_getAxis(lua_State *L);
-int w_Joystick_getAxes(lua_State *L);
-int w_Joystick_getHat(lua_State *L);
-int w_Joystick_isDown(lua_State *L);
-int w_Joystick_isGamepad(lua_State *L);
-int w_Joystick_getGamepadAxis(lua_State *L);
-int w_Joystick_isGamepadDown(lua_State *L);
-int w_Joystick_isVibrationSupported(lua_State *L);
-int w_Joystick_setVibration(lua_State *L);
-int w_Joystick_getVibration(lua_State *L);
-extern "C" int luaopen_joystick(lua_State *L);
-
-} // sdl
-} // joystick
-} // love
-
-#endif // LOVE_JOYSTICK_SDL_WRAP_JOYSTICK_H

File src/modules/joystick/sdl/wrap_JoystickModule.cpp

-/**
- * Copyright (c) 2006-2014 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
-
-#include "wrap_JoystickModule.h"
-#include "wrap_Joystick.h"
-
-#include "filesystem/wrap_Filesystem.h"
-
-namespace love
-{
-namespace joystick
-{
-namespace sdl
-{
-
-static JoystickModule *instance = nullptr;
-
-int w_getJoysticks(lua_State *L)
-{
-	int stickcount = instance->getJoystickCount();
-	lua_createtable(L, stickcount, 0);
-
-	for (int i = 0; i < stickcount; i++)
-	{
-		love::joystick::Joystick *stick = instance->getJoystick(i);
-		stick->retain();
-		luax_pushtype(L, "Joystick", JOYSTICK_JOYSTICK_T, stick);
-		lua_rawseti(L, -2, i + 1);
-	}
-
-	return 1;
-}
-
-int w_getIndex(lua_State *L)
-{
-	love::joystick::Joystick *j = luax_checkjoystick(L, 1);
-	int index = instance->getIndex(j);
-	if (index >= 0)
-		lua_pushinteger(L, index + 1);
-	else
-		lua_pushnil(L);
-	return 1;
-}
-
-int w_getJoystickCount(lua_State *L)
-{
-	lua_pushinteger(L, instance->getJoystickCount());
-	return 1;
-}
-
-int w_setGamepadMapping(lua_State *L)
-{
-	// Only accept a GUID string. We don't accept a Joystick object because
-	// the gamepad mapping applies to all joysticks with the same GUID (e.g. all
-	// Xbox 360 controllers on the system), rather than individual objects.
-	const char *guid = luaL_checkstring(L, 1);
-
-	const char *gpbindstr = luaL_checkstring(L, 2);
-	Joystick::GamepadInput gpinput;
-
-	if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.axis))
-		gpinput.type = Joystick::INPUT_TYPE_AXIS;
-	else if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.button))
-		gpinput.type = Joystick::INPUT_TYPE_BUTTON;
-	else
-		return luaL_error(L, "Invalid gamepad axis/button: %s", gpbindstr);
-
-	const char *jinputtypestr = luaL_checkstring(L, 3);
-	Joystick::JoystickInput jinput;
-
-	if (!love::joystick::Joystick::getConstant(jinputtypestr, jinput.type))
-		return luaL_error(L, "Invalid joystick input type: %s", jinputtypestr);
-
-	const char *hatstr;
-	switch (jinput.type)
-	{
-	case Joystick::INPUT_TYPE_AXIS:
-		jinput.axis = luaL_checkint(L, 4) - 1;
-		break;
-	case Joystick::INPUT_TYPE_BUTTON:
-		jinput.button = luaL_checkint(L, 4) - 1;
-		break;
-	case Joystick::INPUT_TYPE_HAT:
-		// Hats need both a hat index and a hat value.
-		jinput.hat.index = luaL_checkint(L, 4) - 1;
-		hatstr = luaL_checkstring(L, 5);
-		if (!love::joystick::Joystick::getConstant(hatstr, jinput.hat.value))
-			return luaL_error(L, "Invalid joystick hat: %s", hatstr);
-		break;
-	default:
-		return luaL_error(L, "Invalid joystick input type: %s", jinputtypestr);
-	}
-
-	bool success = false;
-	luax_catchexcept(L, [&](){ success = instance->setGamepadMapping(guid, gpinput, jinput); });
-
-	luax_pushboolean(L, success);
-	return 1;
-}
-
-int w_getGamepadMapping(lua_State *L)
-{
-	std::string guid;
-
-	// Accept either a GUID string or a Joystick object. This way we can re-use
-	// the function for Joystick:getGamepadMapping.
-	if (lua_type(L, 1) == LUA_TSTRING)
-		guid = luax_checkstring(L, 1);
-	else
-	{
-		love::joystick::Joystick *stick = luax_checkjoystick(L, 1);
-		guid = stick->getGUID();
-	}
-
-	const char *gpbindstr = luaL_checkstring(L, 2);
-	Joystick::GamepadInput gpinput;
-
-	if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.axis))
-		gpinput.type = Joystick::INPUT_TYPE_AXIS;
-	else if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.button))
-		gpinput.type = Joystick::INPUT_TYPE_BUTTON;
-	else
-		return luaL_error(L, "Invalid gamepad axis/button: %s", gpbindstr);
-
-	Joystick::JoystickInput jinput;
-	jinput.type = Joystick::INPUT_TYPE_MAX_ENUM;
-
-	luax_catchexcept(L, [&](){ jinput = instance->getGamepadMapping(guid, gpinput); });
-
-	if (jinput.type == Joystick::INPUT_TYPE_MAX_ENUM)
-		return 0;
-
-	const char *inputtypestr;
-	if (!love::joystick::Joystick::getConstant(jinput.type, inputtypestr))
-		return luaL_error(L, "Unknown joystick input type.");
-
-	lua_pushstring(L, inputtypestr);
-
-	const char *hatstr;
-	switch (jinput.type)
-	{
-	case Joystick::INPUT_TYPE_AXIS:
-		lua_pushinteger(L, jinput.axis + 1);
-		return 2;
-	case Joystick::INPUT_TYPE_BUTTON:
-		lua_pushinteger(L, jinput.button + 1);
-		return 2;
-	case Joystick::INPUT_TYPE_HAT:
-		lua_pushinteger(L, jinput.hat.index + 1);
-		if (love::joystick::Joystick::getConstant(jinput.hat.value, hatstr))
-		{
-			lua_pushstring(L, hatstr);
-			return 3;
-		}
-		else
-			return luaL_error(L, "Unknown joystick hat.");
-	default:
-		break; // ?
-	}
-
-	return 1;
-}
-
-int w_loadGamepadMappings(lua_State *L)
-{
-	lua_pushvalue(L, 1);
-	luax_convobj(L, -1, "filesystem", "isFile");
-	bool isfile = luax_toboolean(L, -1);
-	lua_pop(L, 1);
-
-	std::string mappings;
-
-	if (isfile)
-	{
-		love::filesystem::FileData *fd = love::filesystem::luax_getfiledata(L, 1);
-		mappings = std::string((const char *) fd->getData(), fd->getSize());
-		fd->release();
-
-	}
-	else
-		mappings = luax_checkstring(L, 1);
-
-	luax_catchexcept(L, [&](){ instance->loadGamepadMappings(mappings); });
-	return 0;
-}
-
-int w_saveGamepadMapping(lua_State *L)
-{
-	std::string guid, mapping;
-
-	// Accept either a GUID string or a Joystick object. This way we can re-use
-	// the function for Joystick:getGamepadMapping.
-	if (lua_type(L, 1) == LUA_TSTRING)
-		guid = luax_checkstring(L, 1);
-	else
-	{
-		love::joystick::Joystick *stick = luax_checkjoystick(L, 1);
-		guid = stick->getGUID();
-	}
-
-	luax_catchexcept(L, [&](){ mapping = instance->saveGamepadMapping(guid); });
-
-	luax_pushstring(L, mapping);
-	return 1;
-}
-
-// List of functions to wrap.
-static const luaL_Reg functions[] =
-{
-	{ "getJoysticks", w_getJoysticks },
-	{ "getJoystickCount", w_getJoystickCount },
-	{ "setGamepadMapping", w_setGamepadMapping },
-	{ "getGamepadMapping", w_getGamepadMapping },
-	{ "loadGamepadMappings", w_loadGamepadMappings },
-	{ "saveGamepadMapping", w_saveGamepadMapping },
-	{ 0, 0 }
-};
-
-static const lua_CFunction types[] =
-{
-	luaopen_joystick,
-	0,
-};
-
-extern "C" int luaopen_love_joystick(lua_State *L)
-{
-	if (instance == nullptr)
-	{
-		luax_catchexcept(L, [&](){ instance = new JoystickModule(); });
-	}
-	else
-		instance->retain();
-
-	WrappedModule w;
-	w.module = instance;
-	w.name = "joystick";
-	w.flags = MODULE_T;
-	w.functions = functions;
-	w.types = types;
-
-	return luax_register_module(L, w);
-}
-
-} // sdl
-} // joystick
-} // love

File src/modules/joystick/sdl/wrap_JoystickModule.h

-/**
- * Copyright (c) 2006-2014 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
-
-#ifndef LOVE_JOYSTICK_SDL_WRAP_JOYSTICK_MODULE_H
-#define LOVE_JOYSTICK_SDL_WRAP_JOYSTICK_MODULE_H
-
-// LOVE
-#include "common/config.h"
-#include "common/runtime.h"
-#include "JoystickModule.h"
-
-namespace love
-{
-namespace joystick
-{
-namespace sdl
-{
-
-int w_getJoysticks(lua_State *L);
-int w_getIndex(lua_State *L);
-int w_getJoystickCount(lua_State *L);
-int w_setGamepadMapping(lua_State *L);
-int w_getGamepadMapping(lua_State *L);
-int w_loadGamepadMappings(lua_State *L);
-int w_saveGamepadMapping(lua_State *L);
-extern "C" LOVE_EXPORT int luaopen_love_joystick(lua_State *L);
-
-} // sdl
-} // joystick
-} // love
-
-#endif // LOVE_JOYSTICK_SDL_WRAP_JOYSTICK_MODULE_H

File src/modules/joystick/wrap_Joystick.cpp

+/**
+ * Copyright (c) 2006-2014 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+// LOVE
+#include "wrap_Joystick.h"
+#include "wrap_JoystickModule.h"
+
+#include <vector>
+
+namespace love
+{
+namespace joystick
+{
+
+Joystick *luax_checkjoystick(lua_State *L, int idx)
+{
+	return luax_checktype<Joystick>(L, idx, "Joystick", JOYSTICK_JOYSTICK_T);
+}
+
+int w_Joystick_isConnected(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	luax_pushboolean(L, j->isConnected());
+	return 1;
+}
+
+int w_Joystick_getName(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	lua_pushstring(L, j->getName());
+	return 1;
+}
+
+int w_Joystick_getID(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+
+	// IDs are 1-based in Lua.
+	lua_pushinteger(L, j->getID() + 1);
+
+	int instanceid = j->getInstanceID();
+	if (instanceid >= 0)
+		lua_pushinteger(L, instanceid + 1);
+	else
+		lua_pushnil(L);
+
+	return 2;
+}
+
+int w_Joystick_getGUID(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	luax_pushstring(L, j->getGUID());
+	return 1;
+}
+
+int w_Joystick_getAxisCount(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	lua_pushinteger(L, j->getAxisCount());
+	return 1;
+}
+
+int w_Joystick_getButtonCount(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	lua_pushinteger(L, j->getButtonCount());
+	return 1;
+}
+
+int w_Joystick_getHatCount(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	lua_pushinteger(L, j->getHatCount());
+	return 1;
+}
+
+int w_Joystick_getAxis(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	int axisindex = luaL_checkint(L, 2) - 1;
+	lua_pushnumber(L, j->getAxis(axisindex));
+	return 1;
+}
+
+int w_Joystick_getAxes(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	std::vector<float> axes = j->getAxes();
+
+	for (size_t i = 0; i < axes.size(); i++)
+		lua_pushnumber(L, axes[i]);
+
+	return (int) axes.size();
+}
+
+int w_Joystick_getHat(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	int hatindex = luaL_checkint(L, 2) - 1;
+
+	Joystick::Hat h = j->getHat(hatindex);
+
+	const char *direction = "";
+	love::joystick::Joystick::getConstant(h, direction);
+
+	lua_pushstring(L, direction);
+	return 1;
+}
+
+int w_Joystick_isDown(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+
+	luaL_checkinteger(L, 2);
+
+	std::vector<int> buttons;
+	for (int i = 2; i <= lua_gettop(L); i++)
+		buttons.push_back(luaL_checkint(L, i) - 1);
+
+	luax_pushboolean(L, j->isDown(buttons));
+	return 1;
+}
+
+int w_Joystick_isGamepad(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	luax_pushboolean(L, j->isGamepad());
+	return 1;
+}
+
+int w_Joystick_getGamepadAxis(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+
+	const char *str = luaL_checkstring(L, 2);
+	Joystick::GamepadAxis axis;
+
+	if (!joystick::Joystick::getConstant(str, axis))
+		return luaL_error(L, "Invalid gamepad axis: %s", str);
+
+	lua_pushnumber(L, j->getGamepadAxis(axis));
+	return 1;
+}
+
+int w_Joystick_isGamepadDown(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+
+	std::vector<Joystick::GamepadButton> buttons;
+	buttons.reserve(lua_gettop(L) - 1);
+
+	luaL_checkstring(L, 2);
+
+	for (int i = 2; i <= lua_gettop(L); i++)
+	{
+		const char *str = luaL_checkstring(L, i);
+		Joystick::GamepadButton button;
+
+		if (!joystick::Joystick::getConstant(str, button))
+			return luaL_error(L, "Invalid gamepad button: %s", str);
+
+		buttons.push_back(button);
+	}
+
+	luax_pushboolean(L, j->isGamepadDown(buttons));
+	return 1;
+}
+
+int w_Joystick_isVibrationSupported(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	luax_pushboolean(L, j->isVibrationSupported());
+	return 1;
+}
+
+int w_Joystick_setVibration(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	bool success = false;
+
+	if (lua_isnoneornil(L, 2))
+	{
+		// Disable joystick vibration if no argument is given.
+		success = j->setVibration();
+	}
+	else
+	{
+		float left = (float) luaL_checknumber(L, 2);
+		float right = (float) luaL_optnumber(L, 3, left);
+		float duration = (float) luaL_optnumber(L, 4, -1.0); // -1 is infinite.
+		success = j->setVibration(left, right, duration);
+	}
+
+	luax_pushboolean(L, success);
+	return 1;
+}
+
+int w_Joystick_getVibration(lua_State *L)
+{
+	Joystick *j = luax_checkjoystick(L, 1);
+	float left, right;
+	j->getVibration(left, right);
+	lua_pushnumber(L, left);
+	lua_pushnumber(L, right);
+	return 2;
+}
+
+// List of functions to wrap.
+static const luaL_Reg functions[] =
+{
+	{ "isConnected", w_Joystick_isConnected },
+	{ "getName", w_Joystick_getName },
+	{ "getID", w_Joystick_getID },
+	{ "getGUID", w_Joystick_getGUID },
+	{ "getAxisCount", w_Joystick_getAxisCount },
+	{ "getButtonCount", w_Joystick_getButtonCount },
+	{ "getHatCount", w_Joystick_getHatCount },
+	{ "getAxis", w_Joystick_getAxis },
+	{ "getAxes", w_Joystick_getAxes },
+	{ "getHat", w_Joystick_getHat },
+	{ "isDown", w_Joystick_isDown },
+
+	{ "isGamepad", w_Joystick_isGamepad },
+	{ "getGamepadAxis", w_Joystick_getGamepadAxis },
+	{ "isGamepadDown", w_Joystick_isGamepadDown },
+
+	{ "isVibrationSupported", w_Joystick_isVibrationSupported },
+	{ "setVibration", w_Joystick_setVibration },
+	{ "getVibration", w_Joystick_getVibration },
+
+	// From wrap_JoystickModule.
+	{ "getConnectedIndex", w_getIndex },
+	{ "getGamepadMapping", w_getGamepadMapping },
+	{ "saveGamepadMapping", w_saveGamepadMapping },
+
+	{ 0, 0 },
+};
+
+extern "C" int luaopen_joystick(lua_State *L)
+{
+	return luax_register_type(L, "Joystick", functions);
+}
+
+} // joystick
+} // love

File src/modules/joystick/wrap_Joystick.h

+/**
+ * Copyright (c) 2006-2014 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#ifndef LOVE_JOYSTICK_WRAP_JOYSTICK_H
+#define LOVE_JOYSTICK_WRAP_JOYSTICK_H
+
+// LOVE
+#include "common/config.h"
+#include "Joystick.h"
+#include "common/runtime.h"
+
+namespace love
+{
+namespace joystick
+{
+
+Joystick *luax_checkjoystick(lua_State *L, int idx);
+int w_Joystick_isConnected(lua_State *L);
+int w_Joystick_getName(lua_State *L);
+int w_Joystick_getID(lua_State *L);
+int w_Joystick_getGUID(lua_State *L);
+int w_Joystick_getAxisCount(lua_State *L);
+int w_Joystick_getButtonCount(lua_State *L);
+int w_Joystick_getHatCount(lua_State *L);
+int w_Joystick_getAxis(lua_State *L);
+int w_Joystick_getAxes(lua_State *L);
+int w_Joystick_getHat(lua_State *L);
+int w_Joystick_isDown(lua_State *L);
+int w_Joystick_isGamepad(lua_State *L);
+int w_Joystick_getGamepadAxis(lua_State *L);
+int w_Joystick_isGamepadDown(lua_State *L);
+int w_Joystick_isVibrationSupported(lua_State *L);
+int w_Joystick_setVibration(lua_State *L);
+int w_Joystick_getVibration(lua_State *L);
+extern "C" int luaopen_joystick(lua_State *L);
+
+} // joystick
+} // love
+
+#endif // LOVE_JOYSTICK_SDL_WRAP_JOYSTICK_H

File src/modules/joystick/wrap_JoystickModule.cpp

+/**
+ * Copyright (c) 2006-2014 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#include "wrap_JoystickModule.h"
+#include "wrap_Joystick.h"
+
+#include "filesystem/wrap_Filesystem.h"
+
+#include "sdl/JoystickModule.h"
+
+namespace love
+{
+namespace joystick
+{
+
+static JoystickModule *instance = nullptr;
+
+int w_getJoysticks(lua_State *L)
+{
+	int stickcount = instance->getJoystickCount();
+	lua_createtable(L, stickcount, 0);
+
+	for (int i = 0; i < stickcount; i++)
+	{
+		love::joystick::Joystick *stick = instance->getJoystick(i);
+		stick->retain();
+		luax_pushtype(L, "Joystick", JOYSTICK_JOYSTICK_T, stick);
+		lua_rawseti(L, -2, i + 1);
+	}
+
+	return 1;
+}
+
+int w_getIndex(lua_State *L)
+{
+	love::joystick::Joystick *j = luax_checkjoystick(L, 1);
+	int index = instance->getIndex(j);
+	if (index >= 0)
+		lua_pushinteger(L, index + 1);
+	else
+		lua_pushnil(L);
+	return 1;
+}
+
+int w_getJoystickCount(lua_State *L)
+{
+	lua_pushinteger(L, instance->getJoystickCount());
+	return 1;
+}
+
+int w_setGamepadMapping(lua_State *L)
+{
+	// Only accept a GUID string. We don't accept a Joystick object because
+	// the gamepad mapping applies to all joysticks with the same GUID (e.g. all
+	// Xbox 360 controllers on the system), rather than individual objects.
+	const char *guid = luaL_checkstring(L, 1);
+
+	const char *gpbindstr = luaL_checkstring(L, 2);
+	Joystick::GamepadInput gpinput;
+
+	if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.axis))
+		gpinput.type = Joystick::INPUT_TYPE_AXIS;
+	else if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.button))
+		gpinput.type = Joystick::INPUT_TYPE_BUTTON;
+	else
+		return luaL_error(L, "Invalid gamepad axis/button: %s", gpbindstr);
+
+	const char *jinputtypestr = luaL_checkstring(L, 3);
+	Joystick::JoystickInput jinput;
+
+	if (!love::joystick::Joystick::getConstant(jinputtypestr, jinput.type))
+		return luaL_error(L, "Invalid joystick input type: %s", jinputtypestr);
+
+	const char *hatstr;
+	switch (jinput.type)
+	{
+	case Joystick::INPUT_TYPE_AXIS:
+		jinput.axis = luaL_checkint(L, 4) - 1;
+		break;
+	case Joystick::INPUT_TYPE_BUTTON:
+		jinput.button = luaL_checkint(L, 4) - 1;
+		break;
+	case Joystick::INPUT_TYPE_HAT:
+		// Hats need both a hat index and a hat value.
+		jinput.hat.index = luaL_checkint(L, 4) - 1;
+		hatstr = luaL_checkstring(L, 5);
+		if (!love::joystick::Joystick::getConstant(hatstr, jinput.hat.value))
+			return luaL_error(L, "Invalid joystick hat: %s", hatstr);
+		break;
+	default:
+		return luaL_error(L, "Invalid joystick input type: %s", jinputtypestr);
+	}
+
+	bool success = false;
+	luax_catchexcept(L, [&](){ success = instance->setGamepadMapping(guid, gpinput, jinput); });
+
+	luax_pushboolean(L, success);
+	return 1;
+}
+
+int w_getGamepadMapping(lua_State *L)
+{
+	std::string guid;
+
+	// Accept either a GUID string or a Joystick object. This way we can re-use
+	// the function for Joystick:getGamepadMapping.
+	if (lua_type(L, 1) == LUA_TSTRING)
+		guid = luax_checkstring(L, 1);
+	else
+	{
+		love::joystick::Joystick *stick = luax_checkjoystick(L, 1);
+		guid = stick->getGUID();
+	}
+
+	const char *gpbindstr = luaL_checkstring(L, 2);
+	Joystick::GamepadInput gpinput;
+
+	if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.axis))
+		gpinput.type = Joystick::INPUT_TYPE_AXIS;
+	else if (love::joystick::Joystick::getConstant(gpbindstr, gpinput.button))
+		gpinput.type = Joystick::INPUT_TYPE_BUTTON;
+	else
+		return luaL_error(L, "Invalid gamepad axis/button: %s", gpbindstr);
+
+	Joystick::JoystickInput jinput;
+	jinput.type = Joystick::INPUT_TYPE_MAX_ENUM;
+
+	luax_catchexcept(L, [&](){ jinput = instance->getGamepadMapping(guid, gpinput); });
+
+	if (jinput.type == Joystick::INPUT_TYPE_MAX_ENUM)
+		return 0;
+
+	const char *inputtypestr;
+	if (!love::joystick::Joystick::getConstant(jinput.type, inputtypestr))
+		return luaL_error(L, "Unknown joystick input type.");
+
+	lua_pushstring(L, inputtypestr);
+
+	const char *hatstr;
+	switch (jinput.type)
+	{
+	case Joystick::INPUT_TYPE_AXIS:
+		lua_pushinteger(L, jinput.axis + 1);
+		return 2;
+	case Joystick::INPUT_TYPE_BUTTON:
+		lua_pushinteger(L, jinput.button + 1);
+		return 2;
+	case Joystick::INPUT_TYPE_HAT:
+		lua_pushinteger(L, jinput.hat.index + 1);
+		if (love::joystick::Joystick::getConstant(jinput.hat.value, hatstr))
+		{
+			lua_pushstring(L, hatstr);
+			return 3;
+		}
+		else
+			return luaL_error(L, "Unknown joystick hat.");
+	default:
+		break; // ?
+	}
+
+	return 1;
+}
+
+int w_loadGamepadMappings(lua_State *L)
+{
+	lua_pushvalue(L, 1);
+	luax_convobj(L, -1, "filesystem", "isFile");
+	bool isfile = luax_toboolean(L, -1);
+	lua_pop(L, 1);
+
+	std::string mappings;
+
+	if (isfile)
+	{
+		love::filesystem::FileData *fd = love::filesystem::luax_getfiledata(L, 1);
+		mappings = std::string((const char *) fd->getData(), fd->getSize());
+		fd->release();
+
+	}
+	else
+		mappings = luax_checkstring(L, 1);
+
+	luax_catchexcept(L, [&](){ instance->loadGamepadMappings(mappings); });
+	return 0;
+}
+
+int w_saveGamepadMapping(lua_State *L)
+{
+	std::string guid, mapping;
+
+	// Accept either a GUID string or a Joystick object. This way we can re-use
+	// the function for Joystick:getGamepadMapping.
+	if (lua_type(L, 1) == LUA_TSTRING)
+		guid = luax_checkstring(L, 1);
+	else
+	{
+		love::joystick::Joystick *stick = luax_checkjoystick(L, 1);
+		guid = stick->getGUID();
+	}
+
+	luax_catchexcept(L, [&](){ mapping = instance->saveGamepadMapping(guid); });
+
+	luax_pushstring(L, mapping);
+	return 1;
+}
+
+// List of functions to wrap.
+static const luaL_Reg functions[] =
+{
+	{ "getJoysticks", w_getJoysticks },
+	{ "getJoystickCount", w_getJoystickCount },
+	{ "setGamepadMapping", w_setGamepadMapping },
+	{ "getGamepadMapping", w_getGamepadMapping },
+	{ "loadGamepadMappings", w_loadGamepadMappings },
+	{ "saveGamepadMapping", w_saveGamepadMapping },
+	{ 0, 0 }
+};
+
+static const lua_CFunction types[] =
+{
+	luaopen_joystick,
+	0,
+};
+
+extern "C" int luaopen_love_joystick(lua_State *L)
+{
+	if (instance == nullptr)
+	{
+		luax_catchexcept(L, [&](){ instance = new sdl::JoystickModule(); });
+	}
+	else
+		instance->retain();
+
+	WrappedModule w;
+	w.module = instance;
+	w.name = "joystick";
+	w.flags = MODULE_T;
+	w.functions = functions;
+	w.types = types;
+
+	return luax_register_module(L, w);
+}
+
+} // joystick
+} // love

File src/modules/joystick/wrap_JoystickModule.h

+/**
+ * Copyright (c) 2006-2014 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#ifndef LOVE_JOYSTICK_WRAP_JOYSTICK_MODULE_H
+#define LOVE_JOYSTICK_WRAP_JOYSTICK_MODULE_H
+
+// LOVE
+#include "common/config.h"
+#include "common/runtime.h"
+#include "JoystickModule.h"
+
+namespace love
+{
+namespace joystick
+{
+
+int w_getJoysticks(lua_State *L);
+int w_getIndex(lua_State *L);
+int w_getJoystickCount(lua_State *L);
+int w_setGamepadMapping(lua_State *L);
+int w_getGamepadMapping(lua_State *L);
+int w_loadGamepadMappings(lua_State *L);
+int w_saveGamepadMapping(lua_State *L);
+extern "C" LOVE_EXPORT int luaopen_love_joystick(lua_State *L);
+
+} // joystick
+} // love
+
+#endif // LOVE_JOYSTICK_WRAP_JOYSTICK_MODULE_H