1

I would like to know if it's possible to setup special trigger events for devices when plugged in and/or unplugged from the system on the fly without needing to configure it every time the device is plugged in or removed. A trigger that watches

/dev/input/event

so it knows when the device is connected or not connected and it either starts or kills the daemon for that device while in use.

Here's what I'm aiming to use trigger events for;

I have a gamepad controller I like to use for playing games, and everytime I go to use it, I always have to mess around with the configuration for it everytime I plug it in to make it work for specific configuration for the controller. But also in some case events where the controller randomly disconnects and reconnects it resets and ignores the configuration I set for it, meaning I got to re-configure it again. So my aim is to have it re-run my configurations by calling it from a script when it detects the device is plugged in to pre-set the config file for it. Would there be something in

/etc/udev/rules.d

I could create to make it do that as a rule? The configuration file for my gamepad is stored in

~/.config/ds4drv.conf

and can also be called in

/etc/ds4drv.conf

I'd like to be able to make it check and apply that configuration file every time it detects the gamepad is plugged in or unplugged. The config has option for running a daemon which can lock a PID file inside of

/tmp/ds4drv.pid

when it's running. Make it pre-check if the daemon process is currently running, if not, start it and apply the configuration from the config file upon detecting the gamepad device. It can sometimes mess with Steam games when playing and make me have to re-check the box to enable controller, which can be a lot of faffing about when you just want it to continue where it left off if the controller loses connection for a moment before reconnecting and not reading from the config file.

2

udev is your friend. you can add a file called 99-gamepad.rules to /etc/udev/rules.d/

this file should contain a line similar to the following one

DRIVER=="usb",ATTRS{idProduct}=="c52b",RUN+="/path/to/your_script.sh"

You can find the ATTRS using udevadm:

udevadm info -a -p /sys/bus/usb/devices/3-1.5

replace /sys/bus/usb/devices/3-1.5 with the path to your usb device in sys, alternatively, you can use the path in /dev/. I usually just try some paths until I find something that matches my device.

if you know the path to the device in /dev then you can use the following line:

udevadm info -a -p $(udevadm info -q path -n /dev/input/js0)

Here is the man page of udev explaining how to write a rule.

Also have a look at the udev page of the ArchWiki.

  • This is kinda helpful to me in a way, I would've hoped to of avoid having to write out complicated scripts that actually works. Just to note when gamepad is plugged in it's automatically assigned to /dev/input/js0 or /dev/input/js1 and /dev/input/event15 or /dev/input/event16 – user91679 Nov 28 '15 at 12:32
  • There's a problem I run into with having the PID file. It doesn't remove itself if the device has been disconnected, not too sure how I can make it check the device isn't there anymore and delete the PID lockfile. – user91679 Nov 28 '15 at 14:31
  • sounds like the daemon keeps running after you unplug the device. You could add a second line to your rules file like this: ACTION=="remove",DRIVER=="usb",ATTRS{idProduct}=="c52b",RUN+="/your/daemon stop". This should stop the daemon. – Thawn Nov 28 '15 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy