Sunday, February 14, 2010

Joystick Utility for Linux

So, here's the scenario on my Ubuntu Linux system. Every time I unplug a controller and either plug it back in or swap to another one I have to re-calibrate it. Which you have to do since most games and emulators expect the controller to be connected at "/dev/input/js0" so if you switch from a flight sim to a driving game you could leave them both plugged in and use root privileges to temporarily shift whichever one is connected at js1 or js2 (or wherever) to js0, but you're still going to go through recalibration before you start playing. Not that the calibration takes too long, but I'd rather be playing than calibrating, and I wanted a simple way to SAVE my calibrations so I don't have to do the same thing over and over and over.

Now, some of you might be thinking "there's a calibration utility already!" Um, yes, there is. It's called "Joystick Calibrator." And it doesn't work very well (if at all). Breeze through some Linux gaming forums and more often than not you'll see posts advising those having problems with joysticks to uninstall "Joystick Calibrator" because it can interfere (apparently) with other joystick stuff. What does seem to flawlessly work on Linux are "jscal" and "jstest" - but they are command-line only within a terminal, and don't SAVE your configurations!

Here's my solution: a simple bash script utility with a zenity UI that lets you recall calibrations for all your controllers! It will even simplify the task of shifting any connected controller to the golden "js0" spot!

Just copy the stuff below into a text file, save it and then set its permissions to allow you to "execute" it as a program. As the comments in it say, you'll have to plug in each controller in turn, run "jscal -c" on it, then "jscal -p" to get the calibration settings, copy and paste those INTO this bash script, and (of course) edit it so the options refer to whatever controllers you use. If you don't have "zenity" installed, it should be available in your distro's repositories.

(in case you're wondering, the "jscal -s" calibrations below are for a Gravis Eliminator flight stick, a Thrustmaster Pro Digital NASCAR driving wheel, and a Thrustmaster F2 Ferrari wheel).

#!/bin/bash
#
# Before running this utility you need to populate the data!
# From a terminal run: jscal -c /dev/input/js0
# Follow on-screen instructions to calibrate controller
# From terminal type: jscal -p /dev/input/js0
# Copy the line it returns and paste it where you see the "jscal -s" lines below
# Do this for every controller you plan to use


zenity --info --text "GAME CONTROLLER PRESET\nThis utility lets you select a calibration for a game controller."

joy=$(zenity --list --text "Game Controller Input" --width "320" --height "240" --radiolist --column "Pick" --column "/dev/input/" TRUE "js0" FALSE "js1" FALSE "js2" FALSE "js3" FALSE "js4"); echo $joy

if [ $joy != "js0" ]; then
zenity --question --text "Shift controller to js0?"
q=$?
if [ $q = "0" ]; then
gksudo ln -s /dev/input/$joy /dev/input/js0
fi
fi

# Enter the Names of your Controllers where it says ">>PUT NAME HERE<<"
# Add or remove entries as needed in format:
# TRUE|FALSE # "Controller Name"

cal=$(zenity --list --text "Game Controller Preset" --width "425" --height "275" --radiolist --column "Pick" --column "#" --column "Controller" TRUE 1 ">>PUT NAME 1 HERE<<" FALSE 2 ">>PUT NAME 2 HERE<<" FALSE 3 ">>PUT NAME 3 HERE<<"); echo $ans

# For each option above have an IF entry below

if [ $cal = "1" ]; then
# CONTROLLER 1
jscal -s 6,1,0,125,125,16776704,14509582,1,1,128,128,15338701,11930101,1,1,87,87,6170742,4098126,1,0,120,120,5368545,5592235,1,0,0,0,536854528,536854528,1,0,0,0,536854528,536854528 /dev/input/$joy
fi
if [ $cal = "2" ]; then
# CONTROLLER 2
jscal -s 3,1,0,124,124,9256113,6100620,1,0,254,254,2113601,-2147483648,1,0,255,255,4793344,-2147483648 /dev/input/$joy

fi
if [ $cal = "3" ]; then
# CONTROLLER 3
jscal -s 5,1,0,-144,-128,2033540,1525155,1,0,255,255,2105312,-2147483648,1,0,255,255,2105312,-2147483648,1,0,0,0,536854528,536854528,1,0,0,0,536854528,536854528 /dev/input/$joy
fi

1 comment:

SuperCrackMunkey said...

http://ubuntuforums.org/showpost.php?p=4240646&postcount=3 < This also works :)