Skip to content

Commit

Permalink
Only use SDL sensor function on recent SDL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mmmaisel committed Apr 17, 2022
1 parent 0197929 commit cc49b8f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/gamecontroller/gamecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions src/gamecontroller/gamecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/inputdaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ 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);
}

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))
Expand Down Expand Up @@ -814,6 +816,7 @@ void InputDaemon::firstInputPass(QQueue<SDL_Event> *sdlEventQueue)
break;
}

#if SDL_VERSION_ATLEAST(2,0,14)
case SDL_CONTROLLERSENSORUPDATE: {
InputDevice *joy = trackcontrollers.value(event.caxis.which);

Expand All @@ -824,6 +827,7 @@ void InputDaemon::firstInputPass(QQueue<SDL_Event> *sdlEventQueue)
}
break;
}
#endif

case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP: {
Expand Down Expand Up @@ -960,11 +964,13 @@ void InputDaemon::modifyUnplugEvents(QQueue<SDL_Event> *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);
Expand Down Expand Up @@ -1115,6 +1121,7 @@ void InputDaemon::secondInputPass(QQueue<SDL_Event> *sdlEventQueue)
break;
}

#if SDL_VERSION_ATLEAST(2,0,14)
case SDL_CONTROLLERSENSORUPDATE: {
InputDevice *joy = trackcontrollers.value(event.csensor.which);

Expand All @@ -1140,6 +1147,7 @@ void InputDaemon::secondInputPass(QQueue<SDL_Event> *sdlEventQueue)

break;
}
#endif

case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP: {
Expand Down
4 changes: 2 additions & 2 deletions src/inputdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand Down
8 changes: 4 additions & 4 deletions src/inputdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions src/joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions src/sdleventreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cc49b8f

Please sign in to comment.