[HowTo] Scripts for using Xbox Controller in RetrOSMC

Ok tested this out using retrosmc alpha 0.006 and the October update of OSMC on a RasPi 2.
What this Topic is aimed at doing:

  1. Install Xboxdrv.
  2. Create Configs for using wireless Xbox controllers with an Xbox receiver for use with both RetroPie and OSMC.
  3. Load Xboxdrv on startup with OSMC config.
  4. Edit the transition scripts from OSMC to RetroPie to kill the OSMC Xboxdrv process and recreate for 2 controllers with RetroPie Config.
  5. Enable simple config swapping on the fly (helps with ergonomics using NES games, but same concept can be used to load any config).
  6. Edit the transition scripts from RetroPie to OSMC to kill the RetroPie Xboxdrv process and recreate for 1 controller with OSMC Config.

Instructions as follows:

1. Install xboxdrv

sudo apt-get install xboxdrv

Ok that was easy

2. Create Configs
We are going to use 2 config files - an OSMC file i’ve called xbmc.ini and an alt config file for button swapping I’ve randomly named buttonswap.ini

Both of these I’ve referenced in the scripts as being in /home/osmc. You can change the destination, but please remember to point the scripts to the new folder.

sudo nano /home/osmc/xbmc.ini

copy/paste the following code

[xboxdrv]

extra-devices = false
extra-events = false
deadzone = 6000
device-name = "Microsoft Xbox 360 Controller"
[ui-buttonmap]

A = KEY_ENTER
B = KEY_BACKSPACE
X = KEY_X
Y = KEY_ESC

du = KEY_UP
dd = KEY_DOWN
dl = KEY_LEFT
dr = KEY_RIGHT

start = KEY_SPACE
back = KEY_L
white = KEY_C
black = KEY_I

TR = BTN_RIGHT # Left stick buttons
TL = BTN_LEFT # Right stick buttons

RT = KEY_EQUAL # Right trigger
LT = KEY_MINUS # Left trigger
[ui-axismap]

x1=REL_X:10
y1=REL_Y:10

LT^resp:127:255=ABS_Z
RT^resp:127:255=ABS_RZ
# EOF #

exit and save

make it executable

sudo chmod +x /home/osmc/xbmc.ini

Now for the second file

sudo nano /home/osmc/buttonswap.ini

copy/paste

# X and B swapped for comfort
# This is simply the default xboxdrv configuration, replicated as INI file with a X and B swapped
[xboxdrv]
[xboxdrv-daemon]
[autofire]
[axis-sensitivity]
[axismap]
[buttonmap]
[calibration]
[evdev-absmap]
[evdev-keymap]
[modifier]
[relative-axis]
[xboxdrv]
ui-clear = true
[ui-axismap]
X1 = ABS_X
Y1 = ABS_Y
X2 = ABS_RX
Y2 = ABS_RY
LT = ABS_BRAKE
RT = ABS_GAS
DPAD_X = ABS_HAT0X
DPAD_Y = ABS_HAT0Y
[ui-buttonmap]
start = BTN_START
guide = BTN_MODE
back = BTN_SELECT
A = BTN_A
B = BTN_X
X = BTN_B
Y = BTN_Y
LB = BTN_TL
RB = BTN_TR
TL = BTN_THUMBL
TR = BTN_THUMBR
# EOF #

Save and exit
Make it executable as before

sudo chmod +x /home/osmc/buttonswap.ini

3. Load Xboxdrv on startup with OSMC config

sudo nano /etc/rc.local

copy/paste

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/home/osmc/start.sh
sudo modprobe snd_bcm2835 & sleep 2
sudo modprobe uinput
sudo modprobe joydev
exit 0

Now create the start.sh script

sudo nano /home/osmc/start.sh

copy/paste

#!/bin/bash
sudo rmmod xpad
sudo /usr/bin/xboxdrv --config /home/osmc/xbmc.ini --id=0 --led=2 --silent --autofire RT=25 --autofire LT=25 --autofire du=200 --autofire dd=200 --autofire dl=500 --autofire dr=500 &  sleep 2

Make it executable

sudo chmod +x /home/osmc/start.sh

OK at this point when you restart the Pi you will be able to control the OSMC interface (or any other KODI skin) with the Xbox controller. So if this is all you need stop here.

4. and 5. Edit the transition scripts to RetroPie and enable swapable buttons

Create Xboxdrv init script for xpad emulation

sudo nano /home/osmc/switchtoxpad.sh

copy/paste

#!/bin/bash
sudo kill -9 `ps -ef | grep xboxdrv | grep -v grep | awk '{print $2}'`
sudo xboxdrv --wid 0 --led 3 --alt-config /home/osmc/buttonswap.ini & sleep 2
sudo xboxdrv --wid 1 --led 4 --alt-config /home/osmc/buttonswap.ini & sleep 2
exit

Make it executable

sudo chmod +x /home/osmc/switchtoxpad.sh

Now edit the transistion script as follows

sudo nano /home/osmc/RetroPie/scripts/retropie.sh

Insert the following line

sudo su osmc -c "sh /home/osmc/switchtoxpad.sh &" &

like so

#!/bin/bash

# This scripts starts the emulationstation watchdog deamon and
# emulationstation itself while stopping KODI afterwards.
# Script by mcobit

#clear the virtaul terminal 7 screen

sudo openvt -c 7 -s -f clear

# display a short message (user most likely won't see it as emulationstation is starting very fast

sudo openvt -c 7 -s -f echo "Running emulationstation from KODI"

# start the watchdog script

sudo su osmc -c "sh /home/osmc/RetroPie/scripts/retropie_watchdog.sh &" &

#run switch to xpad emulation for xboxdrv

sudo su osmc -c "sh /home/osmc/switchtoxpad.sh &" &

# start chroot.sh script on virtual terminal 7 and detach it

sudo su osmc -c "nohup openvt -c 7 -f -s /home/osmc/RetroPie/scripts/chroot.sh >/dev/null 2>&1 &" &

# clear the screen again

sudo openvt -c 7 -s -f clear

# wait a bit to make sure emulationstation is running detached from kodi

sleep 2

# stop kodi to free input devices for emulationstation

sudo su -c "systemctl stop mediacenter &" &

exit

Save and exit

6. Edit the transition script back to OSMC
Create Xboxdrv init script for kodi keyboard emulation

sudo nano /home/osmc/switchtoxboxdrv.sh

copy/paste

#!/bin/bash
sudo pkill xboxdrv #kill Xboxdrv Process
sudo /usr/bin/xboxdrv --config /home/osmc/xbmc.ini --wid 0 --led 2 --silent --autofire RT=25 --autofire LT=25 --autofire du=200 --autofire dd=200 --autofire dl=500 --autofire dr=500 #restart Xboxdrv with Kodi config
exit

Save and exit

Make it executable

sudo chmod +x /home/osmc/switchtoxboxdrv.sh

Lastly edit the transistion script

sudo nano /home/osmc/RetroPie/scripts/retropie_watchdog.sh

add the line

 sudo su osmc -c "sh /home/osmc/switchtoxboxdrv.sh &" &

like so

#!/bin/bash

# Script by mcobit

# play the transition video and wait a bit to give emulationstation time to start up

/home/osmc/RetroPie/scripts/video.sh in &

sleep 8

# check for emulationstation running

while [ true ]; do
        VAR1="$(pgrep emulatio)"

# if emulationstation is quit, clear the screen of virtual terminal 7 and show a message

                if [ ! "$VAR1" ]; then
                    sudo openvt -c 7 -s -f clear
# play the transition video and restart kodi
                        sudo su osmc -c "sh /home/osmc/switchtoxboxdrv.sh &" &
                        /home/osmc/RetroPie/scripts/video.sh out &
                        sleep 4
                        sudo su -c "sudo systemctl restart mediacenter &" &

# exit script

                        exit
                else

# if emulationstation is still running, wait 2 seconds and check again (could probably be longer, but doesn't seem to impact performance by much)

                        sleep 2
fi
done
exit

Now reboot and try it out.

When you enter Kodi the first time LED 1 will light up on only one controller (if you have 2 controllers the other’s LEDs will keep flashing).
On switching to Retropie, “Controller 1” will switch to LED 2 (to indicate you are now controlling Retropie).
If there is another controller this will now be active and assigned LED 3.
When you go back to Kodi, the first controller will again indicate LED 1 and the second controller will remain active on LED 3 for Retropie only.

Press the Center Xbox button to swap the B and X buttons at anytime whilst in Retropie.

2 Likes