mirror of
https://github.com/love2d/love-android.git
synced 2026-08-21 05:00:22 +02:00
updated to latest mobile-common (0cf55b7da58e), removed libtiff, libpng, libmng, devil, jasper, jpeg-8d, added libturbo-jpeg
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user