Using an USB joystick or gamepad in XBMC on Ubuntu

Now I have those cheap USB gamepads, XBMC running, the ROM collection browser addon and MAME all set up. Awesome! But why can't I navigate XBMC with my joystick to select games and adjust the volume etc?! Here's how to solve that problem...

1. Prequisites

First of all you need joystick support and some tools. Go ahead and install the joystick package in Ubuntu with "sudo apt-get install joystick". That gives you joystick tools like jstest and jscal which we'll need later.
Run "cat /proc/bus/input/devices" which shows you a list of all input devices your system has. Locate the entry describing your joystick and write down or copy the exact string after "NAME=...", including all spaces etc.! We'll need that later. The last string of the line "Handlers=..." should read jsX with X corresponding to the number of the joystick.
Now start the tool jstest with the joystick you want to use, like so: "jstest /dev/input/js0". It will show you the states of all of the buttons and axes of js0 and you can see them change when you use the joystick. Find out which indexes correspond to which axis or button.

2. XBMC keymap files

XBMC uses keymap xml files to connect input device "keys" to XBMC functions. We need to create a keymap file for our joystick. Create a new keymap file in your users keymap folder using an editor of you choice and name it after your joysticks name, e.g. "nano ~/.xbmc/userdata/keymaps/joystick.SOMENAME.xml".
The file starts with <keymap>, then <global> and then you start a joystick entry:

<keymap>
   <global>
      <joystick name="NAME_OF_YOUR_JOYSTICK">

Then follows the button/axis configuration. For a button add an entry:
<button id="BUTTON_ID">NAME_OF_XBMC_ACTION</button>
for an axis add:
<axis id="AXIS_ID" limit="VALUE_LIMIT">NAME_OF_XBMC_ACTION</axis>
and for a D-PAD or hat add:
<had id="HAT_ID" position="DIRECTION">"NAME_OF_XBMC_ACTION</hat>

BUTTON_ID, AXIS_ID and HAT_ID correspond to the index you got from jstest above +1, as jstests indices start as 0, but XBMC seems to start from 1...
VALUE_LIMIT is the limit that needs to be reached so XBMC activates that function. The default value of an axis is usually 0, so use "+1" and "-1" for the different directions of the axis.
DIRECTION is the direction of the D-PAD or hat and takes strings like "left", "right", "up" or "down".
NAME_OF_XBMC_ACTION is the function that should be executed in XBMC. A list of possible actions can be found here. An example for that section could be:

         <button id="3">Select</button>
         <axis id="1" limit="-1">AnalogSeekBack</axis>
         <hat id="1" direction="left">Left</hat>

 Finish the file with closing all sections again:

      </joystick>
   </global>
</keymap>

Save the file (here's my keymap file as reference) and start XBMC. You gamepad should work now. Enjoy.

Comments

pauloallex24 said…
Hi. I've used this tutorial because one of my controllers are a DragonRise but I'm not able to use it on XBMC.
I've the files on ~/.xbmc/userdata/keymaps/ and /usr/share/xbmc/system/keymaps/
I'm using Raspbian with XBMC (started from terminal).
There is my keymap xml files and a log file: https://www.dropbox.com/s/x7z32sgydo2hbut/xbmc_keymaps_log.zip
Any Ideia?
Bim said…
Exectly HOW is is not working? No reaction whatsoever? Only not working in XBMC?
Are other joysticks working? Does "/dev/input/js0" exist? If not, did you install jystick support with "apt-get install joystick"? Have you added your user to the input group (See here: http://lektiondestages.blogspot.de/2013/05/raspberry-pi-setup-configuration-stuff.html)? Does the gampepad work with "jstest /dev/input/js0"?

Good luck.
Bim said…
Also the name in your kymap file seems strange. Looks like is has too many blanks... Check the name.
pauloallex24 said…
Seems that XBMC for RPi are compiled without joystick enable so I will need to try another solution such joy2key but I'm not getting it work to.
You have XBMC with joystick working on RPi?
pauloallex24 said…
I've the js0 working good. It works with emulators without any problem and with jstest.
Bim said…
Na, sorry. I'm using XBMC a PC... Some poeple seem to be compiling it themselves with with joystick support. Maybe you could try that.
jerome said…
Hello. I'm trying to make a Joystick work on Openelen-Rpi.Can you please take a look and give some ideas:
http://openelec.tv/forum/124-raspberry-pi/66341-joystick-not-detected-logitech-wingman-cordless#82595
Bruce Dixon said…
I've been trying to use a USB joystick in XMBC on Ubuntu for several days but I'm finding some difficulties on it. Good thing, you share this post and now I what to do when using the Joystick Controller for our CCTV Camera.