mirror of
https://github.com/love2d/love-android.git
synced 2026-08-19 20:20:25 +02:00
updated to latest mobile-common (0cf55b7da58e), removed libtiff, libpng, libmng, devil, jasper, jpeg-8d, added libturbo-jpeg
This commit is contained in:
@@ -36,6 +36,9 @@ public:
|
||||
|
||||
virtual ~JoystickModule() {}
|
||||
|
||||
// Implements Module.
|
||||
virtual ModuleType getModuleType() const { return M_JOYSTICK; }
|
||||
|
||||
/**
|
||||
* Adds a connected Joystick device and opens it for use.
|
||||
* Returns NULL if the Joystick could not be added.
|
||||
@@ -85,6 +88,20 @@ public:
|
||||
**/
|
||||
virtual Joystick::JoystickInput getGamepadMapping(const std::string &pguid, Joystick::GamepadInput gpinput) = 0;
|
||||
|
||||
/**
|
||||
* Loads a newline-separated list of virtual Gamepad mapping strings for
|
||||
* multiple joysticks at a time. The mapping strings must have been
|
||||
* generated with saveGamepadMappings, via Steam, or some other tool which
|
||||
* generates SDL GameController mappings.
|
||||
**/
|
||||
virtual void loadGamepadMappings(const std::string &mappings) = 0;
|
||||
|
||||
/**
|
||||
* Gets a newline-separated list of virtual Gamepad mapping strings for
|
||||
* all used or modified Joysticks which are identified as Gamepads.
|
||||
**/
|
||||
virtual std::string saveGamepadMappings() = 0;
|
||||
|
||||
}; // JoystickModule
|
||||
|
||||
} // joystick
|
||||
|
||||
@@ -191,12 +191,14 @@ bool Joystick::isDown(const std::vector<int> &buttonlist) const
|
||||
if (!isConnected())
|
||||
return false;
|
||||
|
||||
int num = getButtonCount();
|
||||
int numbuttons = getButtonCount();
|
||||
|
||||
for (size_t i = 0; i < buttonlist.size(); i++)
|
||||
for (int button : buttonlist)
|
||||
{
|
||||
int button = buttonlist[i];
|
||||
if (button >= 0 && button < num && SDL_JoystickGetButton(joyhandle, button) == 1)
|
||||
if (button < 0 || button >= numbuttons)
|
||||
continue;
|
||||
|
||||
if (SDL_JoystickGetButton(joyhandle, button) == 1)
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -244,9 +246,9 @@ bool Joystick::isGamepadDown(const std::vector<GamepadButton> &blist) const
|
||||
|
||||
SDL_GameControllerButton sdlbutton;
|
||||
|
||||
for (size_t i = 0; i < blist.size(); i++)
|
||||
for (GamepadButton button : blist)
|
||||
{
|
||||
if (!getConstant(blist[i], sdlbutton))
|
||||
if (!getConstant(button, sdlbutton))
|
||||
continue;
|
||||
|
||||
if (SDL_GameControllerGetButton(controller, sdlbutton) == 1)
|
||||
|
||||
@@ -57,10 +57,10 @@ JoystickModule::JoystickModule()
|
||||
JoystickModule::~JoystickModule()
|
||||
{
|
||||
// Close any open Joysticks.
|
||||
for (auto it = joysticks.begin(); it != joysticks.end(); ++it)
|
||||
for (auto stick : joysticks)
|
||||
{
|
||||
(*it)->close();
|
||||
(*it)->release();
|
||||
stick->close();
|
||||
stick->release();
|
||||
}
|
||||
|
||||
if (SDL_WasInit(SDL_INIT_HAPTIC) != 0)
|
||||
@@ -101,10 +101,10 @@ int JoystickModule::getJoystickCount() const
|
||||
|
||||
love::joystick::Joystick *JoystickModule::getJoystickFromID(int instanceid)
|
||||
{
|
||||
for (size_t i = 0; i < activeSticks.size(); i++)
|
||||
for (auto stick : activeSticks)
|
||||
{
|
||||
if (instanceid == activeSticks[i]->getInstanceID())
|
||||
return activeSticks[i];
|
||||
if (stick->getInstanceID() == instanceid)
|
||||
return stick;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
@@ -119,12 +119,12 @@ love::joystick::Joystick *JoystickModule::addJoystick(int deviceindex)
|
||||
joystick::Joystick *joystick = 0;
|
||||
bool reused = false;
|
||||
|
||||
for (auto it = joysticks.begin(); it != joysticks.end(); ++it)
|
||||
for (auto stick : joysticks)
|
||||
{
|
||||
// Try to re-use a disconnected Joystick with the same GUID.
|
||||
if (!(*it)->isConnected() && (*it)->getGUID() == guidstr)
|
||||
if (!stick->isConnected() && stick->getGUID() == guidstr)
|
||||
{
|
||||
joystick = *it;
|
||||
joystick = stick;
|
||||
reused = true;
|
||||
break;
|
||||
}
|
||||
@@ -144,9 +144,9 @@ love::joystick::Joystick *JoystickModule::addJoystick(int deviceindex)
|
||||
|
||||
// Make sure multiple instances of the same physical joystick aren't added
|
||||
// to the active list.
|
||||
for (auto it = activeSticks.begin(); it != activeSticks.end(); ++it)
|
||||
for (auto activestick : activeSticks)
|
||||
{
|
||||
if (joystick->getHandle() == (*it)->getHandle())
|
||||
if (joystick->getHandle() == activestick->getHandle())
|
||||
{
|
||||
joystick->close();
|
||||
|
||||
@@ -157,10 +157,13 @@ love::joystick::Joystick *JoystickModule::addJoystick(int deviceindex)
|
||||
joystick->release();
|
||||
}
|
||||
|
||||
return *it;
|
||||
return activestick;
|
||||
}
|
||||
}
|
||||
|
||||
if (joystick->isGamepad())
|
||||
recentGamepadGUIDs[joystick->getGUID()] = true;
|
||||
|
||||
activeSticks.push_back(joystick);
|
||||
return joystick;
|
||||
}
|
||||
@@ -257,6 +260,9 @@ bool JoystickModule::setGamepadMapping(const std::string &guid, Joystick::Gamepa
|
||||
// 1 == added, 0 == updated, -1 == error.
|
||||
int status = SDL_GameControllerAddMapping(mapstr.c_str());
|
||||
|
||||
if (status != -1)
|
||||
recentGamepadGUIDs[guid] = true;
|
||||
|
||||
// FIXME: massive hack until missing APIs are added to SDL 2:
|
||||
// https://bugzilla.libsdl.org/show_bug.cgi?id=1975
|
||||
if (status == 1)
|
||||
@@ -427,22 +433,23 @@ void JoystickModule::checkGamepads(const std::string &guid) const
|
||||
if (guid.compare(getDeviceGUID(d_index)) != 0)
|
||||
continue;
|
||||
|
||||
for (auto it = activeSticks.begin(); it != activeSticks.end(); ++it)
|
||||
for (auto stick : activeSticks)
|
||||
{
|
||||
if ((*it)->isGamepad() || guid.compare((*it)->getGUID()) != 0)
|
||||
if (stick->isGamepad() || guid.compare(stick->getGUID()) != 0)
|
||||
continue;
|
||||
|
||||
// Big hack time: open the index as a game controller and compare
|
||||
// the underlying joystick handle to the active stick's.
|
||||
SDL_GameController *ctrl = SDL_GameControllerOpen(d_index);
|
||||
if (ctrl == nullptr)
|
||||
SDL_GameController *controller = SDL_GameControllerOpen(d_index);
|
||||
if (controller == nullptr)
|
||||
continue;
|
||||
|
||||
SDL_Joystick *stick = SDL_GameControllerGetJoystick(ctrl);
|
||||
if (stick == (SDL_Joystick *) (*it)->getHandle())
|
||||
(*it)->openGamepad(d_index);
|
||||
SDL_Joystick *sdlstick = SDL_GameControllerGetJoystick(controller);
|
||||
if (sdlstick == (SDL_Joystick *) stick->getHandle())
|
||||
stick->openGamepad(d_index);
|
||||
|
||||
SDL_GameControllerClose(ctrl);
|
||||
// GameController objects are reference-counted in SDL.
|
||||
SDL_GameControllerClose(controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -462,6 +469,80 @@ std::string JoystickModule::getDeviceGUID(int deviceindex) const
|
||||
return std::string(guidstr);
|
||||
}
|
||||
|
||||
void JoystickModule::loadGamepadMappings(const std::string &mappings)
|
||||
{
|
||||
// TODO: We should use SDL_GameControllerAddMappingsFromRW. We're
|
||||
// duplicating its functionality for now because it was added after
|
||||
// SDL 2.0.0's release, and we want runtime compat with 2.0.0 on Linux...
|
||||
|
||||
std::stringstream ss(mappings);
|
||||
std::string mapping;
|
||||
bool success = false;
|
||||
|
||||
// The mappings string contains newline-separated mappings.
|
||||
while (std::getline(ss, mapping))
|
||||
{
|
||||
if (mapping.empty())
|
||||
continue;
|
||||
|
||||
// Strip out and compare any "platform:XYZ," in the mapping.
|
||||
size_t pstartpos = mapping.find("platform:");
|
||||
if (pstartpos != std::string::npos)
|
||||
{
|
||||
pstartpos += strlen("platform:");
|
||||
|
||||
size_t pendpos = mapping.find_first_of(',', pstartpos);
|
||||
std::string platform = mapping.substr(pstartpos, pendpos - pstartpos);
|
||||
|
||||
if (platform.compare(SDL_GetPlatform()) != 0)
|
||||
continue;
|
||||
|
||||
pstartpos -= strlen("platform:");
|
||||
mapping.erase(pstartpos, pendpos - pstartpos + 1);
|
||||
}
|
||||
|
||||
if (SDL_GameControllerAddMapping(mapping.c_str()) != -1)
|
||||
{
|
||||
success = true;
|
||||
std::string guid = mapping.substr(0, mapping.find_first_of(','));
|
||||
recentGamepadGUIDs[guid] = true;
|
||||
|
||||
// FIXME: massive hack until missing APIs are added to SDL 2:
|
||||
// https://bugzilla.libsdl.org/show_bug.cgi?id=1975
|
||||
checkGamepads(guid);
|
||||
}
|
||||
}
|
||||
|
||||
if (!success)
|
||||
throw love::Exception("Invalid gamepad mappings.");
|
||||
}
|
||||
|
||||
std::string JoystickModule::saveGamepadMappings()
|
||||
{
|
||||
std::string mappings;
|
||||
|
||||
for (const auto &g : recentGamepadGUIDs)
|
||||
{
|
||||
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(g.first.c_str());
|
||||
|
||||
char *sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
|
||||
if (sdlmapping == nullptr)
|
||||
continue;
|
||||
|
||||
std::string mapping = sdlmapping;
|
||||
SDL_free(sdlmapping);
|
||||
|
||||
if (mapping.find_last_of(',') != mapping.length() - 1)
|
||||
mapping += ",";
|
||||
|
||||
// Matches SDL_GameControllerAddMappingsFromRW.
|
||||
mapping += "platform:" + std::string(SDL_GetPlatform()) + ",\n";
|
||||
mappings += mapping;
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
|
||||
} // sdl
|
||||
} // joystick
|
||||
} // love
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
namespace love
|
||||
{
|
||||
@@ -56,6 +57,8 @@ public:
|
||||
|
||||
bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput);
|
||||
Joystick::JoystickInput getGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput);
|
||||
void loadGamepadMappings(const std::string &mappings);
|
||||
std::string saveGamepadMappings();
|
||||
|
||||
private:
|
||||
|
||||
@@ -74,6 +77,10 @@ private:
|
||||
// Persistent list of all Joysticks which have been connected at some point.
|
||||
std::list<Joystick *> joysticks;
|
||||
|
||||
// Persistent map indicating GUIDs for Gamepads which have been connected or
|
||||
// modified at some point.
|
||||
std::map<std::string, bool> recentGamepadGUIDs;
|
||||
|
||||
}; // JoystickModule
|
||||
|
||||
} // sdl
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
/**
|
||||
* 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 = "";
|
||||
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 },
|
||||
|
||||
{ 0, 0 },
|
||||
};
|
||||
|
||||
extern "C" int luaopen_joystick(lua_State *L)
|
||||
{
|
||||
return luax_register_type(L, "Joystick", functions);
|
||||
}
|
||||
|
||||
} // joystick
|
||||
} // love
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 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
|
||||
@@ -0,0 +1,261 @@
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
|
||||
#define instance() (Module::getInstance<JoystickModule>(Module::M_JOYSTICK))
|
||||
|
||||
int w_getJoysticks(lua_State *L)
|
||||
{
|
||||
int stickcount = instance()->getJoystickCount();
|
||||
lua_createtable(L, stickcount, 0);
|
||||
|
||||
for (int i = 0; i < stickcount; i++)
|
||||
{
|
||||
Joystick *stick = instance()->getJoystick(i);
|
||||
luax_pushtype(L, "Joystick", JOYSTICK_JOYSTICK_T, stick);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getIndex(lua_State *L)
|
||||
{
|
||||
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 (Joystick::getConstant(gpbindstr, gpinput.axis))
|
||||
gpinput.type = Joystick::INPUT_TYPE_AXIS;
|
||||
else if (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 (!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 (!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
|
||||
{
|
||||
Joystick *stick = luax_checkjoystick(L, 1);
|
||||
guid = stick->getGUID();
|
||||
}
|
||||
|
||||
const char *gpbindstr = luaL_checkstring(L, 2);
|
||||
Joystick::GamepadInput gpinput;
|
||||
|
||||
if (Joystick::getConstant(gpbindstr, gpinput.axis))
|
||||
gpinput.type = Joystick::INPUT_TYPE_AXIS;
|
||||
else if (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 (!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 (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_saveGamepadMappings(lua_State *L)
|
||||
{
|
||||
lua_settop(L, 1);
|
||||
std::string mappings = instance()->saveGamepadMappings();
|
||||
|
||||
// Optionally write the mappings string to a file.
|
||||
if (!lua_isnoneornil(L, 1))
|
||||
{
|
||||
luax_pushstring(L, mappings);
|
||||
int idxs[] = {1, 2};
|
||||
luax_convobj(L, idxs, 2, "filesystem", "write");
|
||||
lua_pop(L, 1); // Pop the return value.
|
||||
}
|
||||
|
||||
// Return the actual string even if we also wrote it to a file.
|
||||
luax_pushstring(L, mappings);
|
||||
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 },
|
||||
{ "saveGamepadMappings", w_saveGamepadMappings },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
static const lua_CFunction types[] =
|
||||
{
|
||||
luaopen_joystick,
|
||||
0,
|
||||
};
|
||||
|
||||
extern "C" int luaopen_love_joystick(lua_State *L)
|
||||
{
|
||||
JoystickModule *instance = instance();
|
||||
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
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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_saveGamepadMappings(lua_State *L);
|
||||
extern "C" LOVE_EXPORT int luaopen_love_joystick(lua_State *L);
|
||||
|
||||
} // joystick
|
||||
} // love
|
||||
|
||||
#endif // LOVE_JOYSTICK_WRAP_JOYSTICK_MODULE_H
|
||||
Reference in New Issue
Block a user