#!/bin/sh
# Stores the current calibration settings for the given joystick
# (materialized by its device). The calibration settings are stored
# using the joystick's name and serial number if available, and its
# vendor and product codes if it's a USB device. If none of these can
# be determined, the settings are stored against the device name.
if [ -z "$1" ]; then
echo "Usage: $0 {device}"
echo "Stores the device's calibration for future use."
exit 1
fi
if [ ! -x /sbin/udevadm ]; then
echo Storing joystick configuration requires udev! >&2
exit 1
fi
ident=$(mktemp)
/sbin/udevadm info -a -n $1 | @@PREFIX@@/share/joystick/ident > $ident
. $ident
rm $ident
STORE=/var/lib/joystick/joystick.state
if [ ! -d $(dirname $STORE) ]; then
mkdir -p $(dirname $STORE)
if [ $? -gt 0 ]; then
echo Unable to create directory $(dirname $STORE)! >&2
exit 1
fi
fi
# Filter the existing file
if [ -f $STORE ]; then
if [ -z "$NAME" ] && [ -z "$VENDOR" ]; then
echo "No product name or vendor available, calibration will be stored for the"
echo "given device name ($DEVICE) only!"
@@PREFIX@@/share/joystick/filter kernel="$DEVICE" < $STORE > $STORE.new
else
@@PREFIX@@/share/joystick/filter name="$NAME" serial="$SERIAL" vendor="$VENDOR" product="$PRODUCT" < $STORE > $STORE.new
fi
fi
# Append the new calibration information
if [ -f $STORE.new ] && [ ! -z "$(cat $STORE.new)" ]; then
echo >> $STORE.new
fi
if [ -z "$NAME" ] && [ -z "$VENDOR" ]; then
echo "DEVICE=\"$DEVICE\"" >> $STORE.new
fi
if [ ! -z "$NAME" ]; then
echo "NAME=\"$NAME\"" >> $STORE.new
fi
if [ ! -z "$SERIAL" ]; then
echo "SERIAL=\"$SERIAL\"" >> $STORE.new
fi
if [ ! -z "$VENDOR" ]; then
echo "VENDOR=\"$VENDOR\"" >> $STORE.new
fi
if [ ! -z "$PRODUCT" ]; then
echo "PRODUCT=\"$PRODUCT\"" >> $STORE.new
fi
# First store the axes mapping, then the calibration
# as they must be restored in the same order
jscal -q $1 | cut -d' ' -f-3 >> $STORE.new
jscal -p $1 | cut -d' ' -f-3 >> $STORE.new
if [ -f $STORE ]; then
mv $STORE $STORE.old
fi
mv $STORE.new $STORE
rm -f $STORE.old