From 6d6cef59aa669717085f2a97152857ceec1ae68f Mon Sep 17 00:00:00 2001 From: Max Maisel Date: Sun, 17 Apr 2022 13:00:56 +0200 Subject: [PATCH] Only use SDL sensor function on recent SDL Sensors are supported in SDL since version 2.0.14. Do no use sensor functions in prior version because it will not compile on older Linux distributions otherwise. Additionally, convert hasSensor functions from int to bool. --- src/gamecontroller/gamecontroller.cpp | 12 ++++++++++-- src/gamecontroller/gamecontroller.h | 4 ++-- src/inputdaemon.cpp | 8 ++++++++ src/inputdevice.cpp | 4 ++-- src/inputdevice.h | 8 ++++---- src/joystick.cpp | 8 ++++---- src/joystick.h | 4 ++-- src/sdleventreader.cpp | 4 ++++ 8 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/gamecontroller/gamecontroller.cpp b/src/gamecontroller/gamecontroller.cpp index a572fced9..f0e26a0b8 100644 --- a/src/gamecontroller/gamecontroller.cpp +++ b/src/gamecontroller/gamecontroller.cpp @@ -172,14 +172,22 @@ int GameController::getNumberRawAxes() return SDL_CONTROLLER_AXIS_MAX; } -int GameController::hasRawAccelerometer() +bool GameController::hasRawAccelerometer() { +#if SDL_VERSION_ATLEAST(2,0,14) return SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL); +#else + return false; +#endif } -int GameController::hasRawGyroscope() +bool GameController::hasRawGyroscope() { +#if SDL_VERSION_ATLEAST(2,0,14) return SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO); +#else + return false; +#endif } int GameController::getNumberRawHats() { return 0; } diff --git a/src/gamecontroller/gamecontroller.h b/src/gamecontroller/gamecontroller.h index 09b2bcf07..82ec8e0e5 100644 --- a/src/gamecontroller/gamecontroller.h +++ b/src/gamecontroller/gamecontroller.h @@ -59,8 +59,8 @@ class GameController : public InputDevice virtual int getNumberRawButtons() override; virtual int getNumberRawAxes() override; virtual int getNumberRawHats() override; - virtual int hasRawAccelerometer() override; - virtual int hasRawGyroscope() override; + virtual bool hasRawAccelerometer() override; + virtual bool hasRawGyroscope() override; void setCounterUniques(int counter) override; QString getBindStringForAxis(int index, bool trueIndex = true); diff --git a/src/inputdaemon.cpp b/src/inputdaemon.cpp index d8f13b3c7..8514b9b39 100644 --- a/src/inputdaemon.cpp +++ b/src/inputdaemon.cpp @@ -192,6 +192,7 @@ void InputDaemon::refreshJoysticks() SDL_Joystick *sdlStick = SDL_GameControllerGetJoystick(controller); SDL_JoystickID tempJoystickID = SDL_JoystickInstanceID(sdlStick); +#if SDL_VERSION_ATLEAST(2,0,14) if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO)) { SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE); } @@ -199,6 +200,7 @@ void InputDaemon::refreshJoysticks() if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL)) { SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE); } +#endif // Check if device has already been grabbed. if (!m_joysticks->contains(tempJoystickID)) @@ -814,6 +816,7 @@ void InputDaemon::firstInputPass(QQueue *sdlEventQueue) break; } +#if SDL_VERSION_ATLEAST(2,0,14) case SDL_CONTROLLERSENSORUPDATE: { InputDevice *joy = trackcontrollers.value(event.caxis.which); @@ -824,6 +827,7 @@ void InputDaemon::firstInputPass(QQueue *sdlEventQueue) } break; } +#endif case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP: { @@ -960,11 +964,13 @@ void InputDaemon::modifyUnplugEvents(QQueue *sdlEventQueue) break; } +#if SDL_VERSION_ATLEAST(2,0,14) case SDL_CONTROLLERSENSORUPDATE: { // XXX: implement more tempQueue.enqueue(event); break; } +#endif case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP: { tempQueue.enqueue(event); @@ -1115,6 +1121,7 @@ void InputDaemon::secondInputPass(QQueue *sdlEventQueue) break; } +#if SDL_VERSION_ATLEAST(2,0,14) case SDL_CONTROLLERSENSORUPDATE: { InputDevice *joy = trackcontrollers.value(event.csensor.which); @@ -1140,6 +1147,7 @@ void InputDaemon::secondInputPass(QQueue *sdlEventQueue) break; } +#endif case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP: { diff --git a/src/inputdevice.cpp b/src/inputdevice.cpp index 4f533bbe6..56ae6af3a 100644 --- a/src/inputdevice.cpp +++ b/src/inputdevice.cpp @@ -567,9 +567,9 @@ int InputDevice::getNumberAxes() { return getActiveSetJoystick()->getNumberAxes( int InputDevice::getNumberHats() { return getActiveSetJoystick()->getNumberHats(); } -int InputDevice::hasAccelerometer() { return getActiveSetJoystick()->hasAccelerometer(); } +bool InputDevice::hasAccelerometer() { return getActiveSetJoystick()->hasAccelerometer(); } -int InputDevice::hasGyroscope() { return getActiveSetJoystick()->hasGyroscope(); } +bool InputDevice::hasGyroscope() { return getActiveSetJoystick()->hasGyroscope(); } int InputDevice::getNumberSticks() { return getActiveSetJoystick()->getNumberSticks(); } diff --git a/src/inputdevice.h b/src/inputdevice.h index c5537e63c..bd62ef5f6 100644 --- a/src/inputdevice.h +++ b/src/inputdevice.h @@ -40,8 +40,8 @@ class InputDevice : public QObject virtual int getNumberButtons(); virtual int getNumberAxes(); virtual int getNumberHats(); - virtual int hasAccelerometer(); - virtual int hasGyroscope(); + virtual bool hasAccelerometer(); + virtual bool hasGyroscope(); virtual int getNumberSticks(); virtual int getNumberVDPads(); @@ -95,8 +95,8 @@ class InputDevice : public QObject virtual int getNumberRawButtons() = 0; virtual int getNumberRawAxes() = 0; virtual int getNumberRawHats() = 0; - virtual int hasRawAccelerometer() = 0; - virtual int hasRawGyroscope() = 0; + virtual bool hasRawAccelerometer() = 0; + virtual bool hasRawGyroscope() = 0; int getDeviceKeyPressTime(); // unsigned diff --git a/src/joystick.cpp b/src/joystick.cpp index 5488c46e9..16015563d 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -149,14 +149,14 @@ int Joystick::getNumberRawHats() return numhats; } -int Joystick::hasRawAccelerometer() +bool Joystick::hasRawAccelerometer() { - return 0; + return false; } -int Joystick::hasRawGyroscope() +bool Joystick::hasRawGyroscope() { - return 0; + return false; } void Joystick::setCounterUniques(int counter) { counterUniques = counter; } diff --git a/src/joystick.h b/src/joystick.h index 3b9d6d8b4..0ce67fa03 100644 --- a/src/joystick.h +++ b/src/joystick.h @@ -46,8 +46,8 @@ class Joystick : public InputDevice virtual int getNumberRawButtons() override; virtual int getNumberRawAxes() override; virtual int getNumberRawHats() override; - virtual int hasRawAccelerometer() override; - virtual int hasRawGyroscope() override; + virtual bool hasRawAccelerometer() override; + virtual bool hasRawGyroscope() override; void setCounterUniques(int counter) override; diff --git a/src/sdleventreader.cpp b/src/sdleventreader.cpp index f2a598b5f..63b9fc523 100644 --- a/src/sdleventreader.cpp +++ b/src/sdleventreader.cpp @@ -61,7 +61,11 @@ SDLEventReader::~SDLEventReader() void SDLEventReader::initSDL() { // SDL_INIT_GAMECONTROLLER should automatically initialize SDL_INIT_JOYSTICK // but it doesn't seem to be the case with v2.0.4 +#if SDL_VERSION_ATLEAST(2,0,14) SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK | SDL_INIT_SENSOR); +#else + SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK); +#endif SDL_JoystickEventState(SDL_ENABLE); sdlIsOpen = true;