Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

Static /dev/input entry for bluetooth controllers



  • It was bugging that depending on what USB devices were installed my bluetooth PS3 controllers would get a different /dev/input/event<n> assignment. The joystick selection script helps but didn't seem to be the right way to handle the problem and didn't work very well with some of the emulators.

    What I created instead was a way to use a udev rule to create a static ps3_controller_event1 link to whatever /dev/input/event<n> was assigned. This way I can pick exactly what controller I want to use and don't have to worry about the order. This technique also works with other controller types.

    I did a pile of research and didn't find any existing solutions. The closest I found were a couple of entries that said it wasn't possible. The following are my instruction on how to do this.

    First create a new udev rule using the following,

    sudo nano /etc/udev/rules.d/99-xxx_map_controllers.rules
    

    containing the following,

    ATTRS{name}=="PLAYSTATION(R)3 Controller", PROGRAM="/bin/ps3_dev_namer %k", SYMLINK+="%c"
    

    The first part of this looks for a udev assignment that matches ATTRS{name}=="PLAYSTATION(R)3 Controller". This occurs whenever your connect a PS3 controller. If you aren’t using ps3 controllers you can modify this to search for your controller identity by using the “udevadm info -a -n /dev/input/event1” command and finding the corresponding string for your controller.

    The second part defines the actions that should be taken when a PS3 controller is attached. In this case the PROGRAM="/bin/ps3_dev_namer %k” section calls the /dev/ps3_dev_namer script I created. This script iterates through the potential /dev/input/ps3_controller_event<n> devices and returns the first unassigned device. So on the first call it will return /dev/input/ps3_controller_event1. The SYMLINK+="%c" section creates a “/dev/input/ps3_controller_event1” entry that links to whatever /dev/input/event<n> was assigned to the first PS3 controller that was connected. Connecting the next PS3 controller will create a “/dev/input/ps3_controller_event2” entry that points to its appropriate event. This means that you can use the /dev/input/ps3_controller_event<n> entries you can use in your emulator config files.

    #!/bin/bash
    # /bin/ps3_dev_namer script to provide a static name for ps3 controllers 
    # Created by Jonathan Booth ©2018 free for use providing this line remains
    # Called by udev rule to create static device entries that link to the dynamically /dev/input/event<n>
    # This  is useful when the device order changes but you need a static way to refer to the same device 
    # I use this is retropie to create a static mapping for my ps3 controller events and joysticks
    #
    # To use this script create “/etc/udev/rules.d/99-xxx_map_controllers.rules” with the following line
    # less the leading ‘#” character
    # ATTRS{name}=="PLAYSTATION(R)3 Controller", PROGRAM="/bin/device_namer %k", SYMLINK+="%c"
    
    BASENAME="/dev/input/ps3_controller"	# Change if you have a different controller
    COUNT=1
    
    if [[ $1 == js* ]]			# if we have a js<n> device		
    then
            TYPE="_js"			# Append “_js”
    elif [[ $1 == event* ]]		# else if we have an event<n> device
    then
            TYPE="_event"		# Append “_event”
    fi
    
    while [ $COUNT -le 100 ]                # Main loop with backup plan to exit after 100 trys. 
    do
            NAME="$BASENAME$COUNT$TYPE"     # Build filename to test for
    
            if [ -e $NAME ]                 	# if the file exists
            then
                    ((COUNT+=1))            	# increment count and try the next possible entry
            else                            		# file does not exist
                    echo $NAME | cut -c 6-  # output the name of the first available link less the "\dev" prefix
                    break                   	# break the loop and exit
            fi
    done
    

    I was pretty happy when I got this working as the combination of a keyboard, trackpad, trackball, 2 arcade controllers, 2 wii controllers, 2 old style usb controllers, and 2 PS3 controllers was driving me nuts every time I changed the order. Now I can create rules to create static named /dev/input devices that point to whatever the assigned /dev/input/event was.

    I hope this will be of use to others and I hope people will update this thread on how they use it.



Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.