Fixed bug 4789 - Linux accelerometers no longer available as joysticks
authorSam Lantinga <slouken@libsdl.org>
Fri, 06 Sep 2019 08:42:54 -0700
changeset 13062211e1b06ec47
parent 13061 38e874cfc3d5
child 13063 33bcd45f0447
Fixed bug 4789 - Linux accelerometers no longer available as joysticks

Daniel Drake

A long time ago, it was possible to play neverball on Linux using the accelerometer found in HP laptops.

The kernel exposes the accelerometer as a joystick (/dev/input/jsX) as well as an evdev device (/dev/input/eventX). I guess it worked fine when SDL was using the js interface, but then stopped working here: http://hg.libsdl.org/SDL/rev/fdaeea9e7567

Looking at current code which uses udev to discover joysticks, it looks for the udev tag ID_INPUT_JOYSTICK.

However udev's internal input_id logic specifically tags accelerometers as ID_INPUT_ACCELEROMETER and nothing else.

This looks like a good fit for SDL_HINT_ACCELEROMETER_AS_JOYSTICK.
src/core/linux/SDL_udev.c
     1.1 --- a/src/core/linux/SDL_udev.c	Thu Sep 05 20:47:20 2019 +0300
     1.2 +++ b/src/core/linux/SDL_udev.c	Fri Sep 06 08:42:54 2019 -0700
     1.3 @@ -34,6 +34,7 @@
     1.4  #include "SDL_assert.h"
     1.5  #include "SDL_loadso.h"
     1.6  #include "SDL_timer.h"
     1.7 +#include "SDL_hints.h"
     1.8  #include "../unix/SDL_poll.h"
     1.9  
    1.10  static const char *SDL_UDEV_LIBS[] = { "libudev.so.1", "libudev.so.0" };
    1.11 @@ -420,6 +421,12 @@
    1.12          if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
    1.13              devclass |= SDL_UDEV_DEVICE_JOYSTICK;
    1.14          }
    1.15 +
    1.16 +        val = _this->syms.udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER");
    1.17 +        if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE) &&
    1.18 +            val != NULL && SDL_strcmp(val, "1") == 0 ) {
    1.19 +            devclass |= SDL_UDEV_DEVICE_JOYSTICK;
    1.20 +	}
    1.21          
    1.22          val = _this->syms.udev_device_get_property_value(dev, "ID_INPUT_MOUSE");
    1.23          if (val != NULL && SDL_strcmp(val, "1") == 0 ) {