/dev/posts/

SDL2 gamepad calibration

Published:

Updated:

Many recent games do not provide an option to map the keys/axes of the gamepad to specific actions. They assume that the gamepad is XBox compatible: if it is not the game is completely unusable. SDL2 provides a way to calibrate a gamepad 🎮 in order to map its keys/axes to the “standard” XBox ones.

Use calibration information

Get the gamecontrollerdb.txt file which contains calibration information of various gamepads and set its content in the SDL_GAMECONTROLLERCONFIG enrvironment variable.

Simple sdl2 script to do this:

#!/bin/sh
# Expects the calibration information in ~/.gamecontrollerdb.txt
export SDL_GAMECONTROLLERCONFIG="$(cat ~/.gamecontrollerdb.txt)"
exec "$@"

used as:

sdl2 ./myprogram

Adding calibration

If this does not work, the gamepad is not in the file and has to be calibrates:

hg clone http://hg.libsdl.org/SDL
cd SDL/tests
# I am lazy, I just compile what I need:
gcc controllermap.c -lSDL2 -I/usr/include/SDL2 -o controllermap
/controllermap 0 # 0 is the index of the gamepad tu calibrate

Once calibrated, a new line for the (gamepad, OS) pair is printed in stdout:

030000004f04000023b3000000010000,Thrustmaster Dual Trigger 3-in-1,platform:Linux,x:b0,a:b1,b:b2,y:b3,back:b8,start:b9,dpleft:h0.8,dpdown:h0.0,dpdown:h0.4,dpright:h0.0,dpright:h0.2,dpup:h0.0,dpup:h0.1,leftshoulder:h0.0,leftshoulder:b4,lefttrigger:b6,rightshoulder:b5,righttrigger:b7,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a5

You need to add this line to SDL_GAMECONTROLLERCONFIG either manually or by addind it to gamecontrollerdb.txt and using the script. If it is working, you can send it upstream.

Extra informations