[HowTo] Scripts for using Xbox Controller in RetrOSMC

Continuing the discussion from [HowTo] Retrosmc - Retrogaming on OSMC:

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

It works!!!

Except… the controller does not seem to work in any emulators. Could this be because it was used in each emulator prior to this config?

I was running osmc with retrosmc .06 prior and just using the wireless 360 controller to play the emus. It worked, although I could not get it to detect additional controllers. The only setup I did at that time was to install xboxdrv and retropie/retrosmc did the rest and auto detected the controller. (only 1)

After this guide i have the controller working in both osmc and retrosmc. It detects both of my controllers in retrosmc and the primary one works the menu in emulation station… But… as soon as i open any game in any emu, the controller no longer works. I no buttons, no nothing. Any advice?

I’ve only checked up until the menu myself. Will install some games and test out the emulators and feedback.

2 Likes

First time around OSMC remained functional but RetroPie didn’t seem to like it. That said I had followed a different method originally to get the controller working in the first place so it was probably that.

Reformatted to an earlier image i’d made before getting the controller working the first time around and I’m now in the same place as drsdroid. Controller works perfectly in OSMC and RetroPie menus but not in any of the emulators.

Thanks Yggdrasil for all your work! :smiley:

Got it working.

  1. Connect only one Controller.

  2. Select Retropie from Main Emulator Menu

  3. Select Configure Retroarch keyboard/joystick.

  4. Select 1. Configure joystick/controller

  5. Map the keys

  6. Exit Retropie and then come back in

  7. Done

When you reconnect the second controller it should work as well.

Yeh I tried it earlier and it doesn’t seem to recognise any inputs from the controller. In the RGUI input settings it wont recognise inputs as user 1 but has no problem recognising inputs from the controller for user 2 binding.

Well that’s strange. what controllers and receiver are you using. Is this a fresh install? What version of Retrosmc are you using?

any luck?

I did as you suggested but unfortunately got the same results. I’m going to run an update on retrosmc later tonight and see if that has any effect. Otherwise perhaps it has something to do with the --mimic-xpad ? a single wireless 360 controller worked for me in retrosmc before this, so perhaps there is no need for me to mimic xpad and i can just use the rest of that line in the script?

I’ll try the update tonight and if that doesn’t work i’ll try modifying the script… either way i’ll report back later.

so just to give this the best test possible… i completely redid my full osmc install and then installed retrosmc, then followed your method exactly.

Same results.

Now i have gone back and changed

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

into

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
exit

and SUCCESS… although it now sees the controller as "XBOX GAMEPAD (USERSPACE DRIVER) it still configures exactly the same (still have the same weird issue with the left and right bottom bumpers showing up as +5 and -5 axis, but i literally always have that in retrosmc with my 360 controller). I have now tested it and it is working with the NES emulator (haven’t loaded other roms on just yet)

Now attempting this with multiple controllers…

copied the same line again and changed the wid and led… success… 2 working controllers in osmc!!!

The glory… oh the glory!

Awesome

The triggers are configured as axis to enable analogue control (for racing games), but you can change that in the xboxdrv options.

So obviously it was not creating the process correctly with xpad mimic. Happy you found the problem. Changed the original scripts so hopefully it will work out the box now…

Great yggdrasil will give it a test soon

Just wondering is it possible to have the analog sticks autofire like the dpad while scrolling down movie list. Previously I had to flick the analog stick down each time to move down list. Be great if could hold stick down and it scrolls down through movies.

Cheers for great work

you can map the sticks to the d-pad…if you use the xbmc.ini above though it should mimic a mouse

Thanks for reply yggdrasil could you provide sample code to map the right analog stick to be same as dpad. Be much appreciated.

Cheers again

I tested with SNES emulator and with Quake 3 and it is not working. I’m using the latest version of OSMC, Retrosmc and script from this page.

Tested also NES emulator but it is not working. I also tested with both configuration but I get same results: works fine in Kodi and EmulationStation but stops working in emulators.

so i’m assuming i need to add trigger-as-button=true in the [xboxdrv] section of the config file pulled up for retropie. Additionally I need to remove the references to the triggers in ui-axis map section and then add references in the ui-button map section? That’s what i’m getting from my understanding of the xboxdrv reference website anyway.

So I did that and added
LT = BTN_LT
RT = BTN_RT

to the buttonswap.ini but the seem to screw up the whole process. Any tips?

i’m a newb… forget all that. I was just putting the expression in the wrong place.

For anyone looking the change triggers to buttons with this current config, all you need to do is add --trigger-as-button to the commands in switchtoxpad.sh like so:

sudo xboxdrv --wid 0 --led 2 --trigger-as-button --alt-config /home/osmc/buttonswap.ini & sleep 2