You are not logged in.

#1 2011-01-31 17:31:14

drelyn86
Member
From: Indianapolis, IN
Registered: 2009-04-10
Posts: 80

Watch for joystick unplug/re-plug with BASH

I'm having a little trouble with what I think should be a very simple BASH script. It could be a logic error, but I really don't know.

First, here is the problem I am trying to solve:
On this particular PC, I am using Boxee as the main interface with openbox-session. I currently do not have a suitable wireless input to control this, so I have a basic 6-button Microsoft Sidewinder gamepad plugged in. I'm using rejoystick to map the buttons to the proper keys to control Boxee (and also the volume via xbindkeys). However, if the gamepad ever gets unplugged, and I plug it back in, the bindings no longer work. To get them working again, I have to sigkill rejoystick (for some reason, sigterm doesn't do it), and re-execute "rejoystick -d".

This script seems to work when I run it in a console. To test it, I have "rejoystick -d" running, I unplug the gamepad, and I plug it back in, and the button mappings are working.
However, when I let the script run in the background for a few minutes, and I perform the same test, the button mappings do not work.

~/.scripts/rejoystick.sh

#!/bin/sh
if ls /dev/input/js0 # run an initial check to see if js0 is plugged in and set the PLUGGED variable
then
    PLUGGED=true
else
    PLUGGED=false
fi

while true
do
    sleep 2
    if ls /dev/input/js0
    then
        if $PLUGGED # this is more like, if it was plugged in during the previous cycle of the loop
        then
            PLUGGED=true
        else
            killall --signal 9 rejoystick & sleep 2 && rejoystick -d
            PLUGGED=true
        fi
    else
        PLUGGED=false
    fi
done

Notice that "killall --signal 9 rejoystick & sleep 2 && rejoystick -d" only gets ran once the joystick has been unplugged and gets plugged back in

Here are the last few lines of my ~/.config/openbox/autostart.sh:

~/.scripts/display.sh & # this script is a simple "xset -dmps"
xfce4-volumed & # for volume change notification
xbindkeys & # I have F7 and F8 bound to "amixer -c 0 set Master 1-" and "amixer -c 0 set Master 1+"
~/.fehbg &
~/.scripts/rejoystick.sh & # described above
~/.scripts/boxee.sh & disown # simple script to re-open Boxee if someone exits or crashes it

So maybe there's something wrong with the script, or the way I'm running it in the background, or maybe there's just another way to solve the problem... I must be doing something wrong.

Thanks in advance.

Last edited by drelyn86 (2011-01-31 17:35:05)

Offline

Board footer

Powered by FluxBB