Handmade Hero » Forums » Code » Lightweight game controller library for linux
DanB91
Dan
21 posts / 1 project

None

#5713 Lightweight game controller library for linux
2 years, 10 months ago Edited by Dan on Jan. 25, 2016, 4:26 a.m.

I am starting to build a game from scratch using X11/GLX for Linux.
I know on Windows, you typically have Xinput*.dll to work with (a la Handmade hero).

I was looking for something similar for Linux and I found this: https://github.com/ThemsAllTook/libstem_gamepad/

It would be a grand total of 4 readable header/implementation files (and I may even concatenate them into one file), which is nice. Doesn't look like it has support for rumble, but looking through evdev API, it doesn't look TOO terrible to put in later.

Has anyone used this library or recommend another Linux library for controllers?

I understand SDL, of course, has a module for controllers, but it doesn't look like its too easy to rip out from the rest of the library.

None
mmozeiko
Mārtiņš Možeiko
1831 posts / 1 project
#5714 Lightweight game controller library for linux
2 years, 10 months ago Edited by Mārtiņš Možeiko on Jan. 25, 2016, 7:16 a.m.

SDL uses pretty much the same mechanism - reading events from /dev/input.
glfw also: https://github.com/glfw/glfw/blob/master/src/linux_joystick.c

Here is documentation how to read those events from /dev/input/js* special devices: https://www.kernel.org/doc/Documentation/input/joystick-api.txt It's pretty simple API.

And here's documentation for force-feedback: https://www.kernel.org/doc/Documentation/input/ff.txt Unfortunately force-feedback will require access to /dev/input/event* It doesn't work on js* devices. On most distributions envent* devices by default doesn't have permissions for regular users. Only root has access. So you'll need to ask users to configure udev to allow writing to event* devices. Here's more info on this:
https://forums.libsdl.org/viewtopic.php?t=9772

But the very awesome stuff in SDL is the controller database. It contains info to map buttons from different controllers to approximately same control schema. I suggest you to use information from it. You need only one file which is easy to parse: http://hg.libsdl.org/SDL/file/default/include/SDL_gamecontroller.h
There exists additional community made databases for even more controllers: https://github.com/gabomdq/SDL_Ga.../blob/master/gamecontrollerdb.txt
DanB91
Dan
21 posts / 1 project

None

#5716 Lightweight game controller library for linux
2 years, 10 months ago

Thanks very much for the evdev documentation! I couldn't find it for some reason :/ I was literally looking at the source code for the library and input.h to figure out what was going on.

I saw that DB, which is awesome. I tested out my PS4 and XBone controller using sdl2-jstest which corresponds to exactly that file (and evdev, by extension, I believe).

That sucks about needing root for the evdev files. I don't remember having to manually configure udev files, Perhaps when I installed some package on my machine it autoconfigured udev for controller use. In fact, I can tell which eventXX are controllers because they are the only files you don't need root for.

None
mmozeiko
Mārtiņš Možeiko
1831 posts / 1 project
#5717 Lightweight game controller library for linux
2 years, 10 months ago Edited by Mārtiņš Možeiko on Jan. 25, 2016, 8:24 p.m.

Yeah, that might be the case. Either some package or your distribution configures permission for event* files. On my machine event* files have 0660 permission.

You can check /etc/udev/rules.d/ folder. It should contain file with following contents:
1
SUBSYSTEM=="input", MODE="0666", ENV{ID_INPUT_JOYSTICK}="1"


As for evdev documentation - yes, finding good documentation for Linux is not easy. But those kernel documentation .txt files are pretty useful. Once you know about them you can find a lot of interesting stuff there: https://github.com/torvalds/linux/tree/master/Documentation