No joystick detected

Hello, was anyone able to get detection of joysticks on Linux?

Input.GetJoystickNames();

Returns always empty string.

Here is my code to test joysticks:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JoystickTest : MonoBehaviour {

    // Use this for initialization
    void Awake() {
        string[] names = Input.GetJoystickNames();
        Debug.Log("Connected Joysticks:");
        for(int i = 0; i < names.Length; i++) {
            Debug.Log("Joystick" + (i + 1) + " = " + names[i]);
        }
    }
   
    // Update is called once per frame, (if any joystick was connected during gameplay
    void Update() {
        Debug.Log(Input.GetJoystickNames().Length);
    }
}

Here is settings of input:

What joystick(s) are you using?

Although I’m very happy with the Linux editor I’m also struggling with joystick input. The script from the OP also returns empty strings for me. I would appreciate any pointers as how to get this working. Below are 2 screen shots from jstest.gtk. The first shows that the joystick is recognized in Ubuntu, the second shows the axes and buttons. Moving the stick and pressing buttons is recognized so the stick is working.

I’m using Ubuntu 16.04 LTS and Unity 2017.10xf3Linux Personal.

jstest uses the “old” joystick system, so there could be a difference between what you see there and in Unity (and other applications that use SDL for input).
For what it’s worth, this is one of the joysticks we specifically tested when we migrated to SDL for input handling.

I’ve had similar problems with joysticks under Linux (Logitech Extreme 3D Pro). I’ve been able to get a PS3 controller recognised and working, though - have you any other game controllers to hand to see if they’re recognised?

Thanks Tak and jamesremuscat for your responses.

I’ve tried another game controller (usb RS- Flugsimulator) that was also not recognised.

I have a dualboot system and tried the test game on Windows where both controllers are recognised and working.

Then switched to Ubuntu and neither are recognised.

Because Tak mentioned that the joystick I have is tested and should work the problem clearly is on my end. I would appreciate any pointers on how to proceed to get this working. I’m sure this would help other users as well.

[Edit 9 September 13:30]

I try to understand what Tak meant with the “old” joystick system and the migration to SDL.

The Archlinux wiki has some info on this, see Gamepad - ArchWiki

I’m quoting:

“Linux has 2 different input systems for Joysticks. The original ‘Joystick’ interface and the newer ‘evdev’ based one.”

and:

“While SDL1 defaults to ‘evdev’ interface you can force it to use the old ‘Joystick’ API by setting the environment variable SDL_JOYSTICK_DEVICE=/dev/input/js0. This can help many games such as X3. SDL2 supports only the new ‘evdev’ interface.”

I’m assuming that Tak refers to the original Joystick interface as the “old” joystick system and that the ‘evdev’ based system is the SDL newer one.

I then followed the instruction on how to test one’s configuration. In my case I opened a terminal and typed:

cd /dev/input/by-id
ls

This listed my joystick as:

usb-Thrustmaster_T.Flight_Hotas_X-event-joystick
usb-Thrustmaster_T.Flight_Hotas_X-joystick

To test I typed in the terminal:

cat usb-Thrustmaster_T.Flight_Hotas_X-event-joystick

Moving the joystick and clicking buttons produced ‘mojibake’ but showed correct responses.

To further test the joystick I installed sdl-jstest, see https://github.com/Grumbel/sdl-jstest

I downloaded the zip, unpacked it which produced a folder called "sdl-jstest-master" in my Downloads folder. Then in a terminal I needed to install the required packages and then compile the source code into an executable program:

sudo apt-get install cmake
sudo apt-get install libsdl1.2-dev
sudo apt-get install libsdl2-dev
sudo apt-get install libncurses5-dev
cd ~/Downloads/sdl-jstest-master
mkdir build
cd build
cmake …
make install

I could then run the program in the terminal by typing:

./sdl-jstest – list

This produced the following:

Found 1 joystick(s)

Joystick Name: ‘Thrustmaster T.Flight Hotas X’
Joystick Number: 0
Number of Axes: 5
Number of Buttons: 12
Number of Hats: 1
Number of Balls: 0

I then typed (the 0 is the Joystick Number obtained from the previous command):

./sdl2-jstest --test 0

Which showed:

All axes and buttons in this terminal window responded to joystick movement and button clicks.

My conclusion so far is that the stick is recognized within the SDL system.

Did it print the game controller info as well?
Or GameController: not a gamepad?

(Unity is currently using the GameController API exclusively)

./sdl2-jstest -l

This produces:

Found 1 joystick(s)

Joystick Name: ‘Thrustmaster T.Flight Hotas X’
Joystick GUID: 030000004f04000008b1000000010000
Joystick Number: 0
Number of Axes: 5
Number of Buttons: 12
Number of Hats: 1
Number of Balls: 0
GameController:
not a gamepad

I’m not sure if this is bad news?

OK - this means that Unity won’t see this as a gamepad by default.
However, there are a number of ways you can configure it to appear as such:
You can use Steam’s controller configurator, or this third-party one (and there are probably others).
You can also manually set SDL_GAMECONTROLLERCONFIG in your environment(, which is what these tools do in any case).

(For this joystick, it would probably look something like:
SDL_GAMECONTROLLERCONFIG=" 030000004f04000008b1000000010000,Thrustmaster T.Flight Hotas X,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,
rightx:a2,righty:a3,start:b9,x:b2,y:b3,")

Please read the edit before reading on.

Thanks for your patience Tak, alas no ‘joy’ as yet. In a terminal as a normal user:

SDL_GAMECONTROLLERCONFIG=“030000004f04000008b1000000010000,Thrustmaster Hotas X,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,”

export SDL_GAMECONTROLLERCONFIG

echo $SDL_GAMECONTROLLERCONFIG

Shows:

030000004f04000008b1000000010000,Thrustmaster Hotas X,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,

In Unity the joystick is not recognized. I’m unfamiliar with environment variables and maybe the above is not sufficient to get Unity to see the joystick?

[Edit 11 September 17:00]

After a reboot the stick is now recognized in Unity!

Joystick1 = Thrustmaster T.Flight Hotas X
UnityEngine.Debug:Log(Object)
JoystickTest:Awake() (at Assets/JoystickTest.cs:12)

I think I need to tweak some axes/buttons but it does seem to work. I will post the final result soon.

As a follow up, here is what works consistently.

To set permanent environment variables in latest Ubuntu versions (from 14.04 and above) add the variables to /etc/environment. To do that, in a terminal, type:

sudo -H gedit /etc/environment

This opens an editor window where you need to add (do not remove any existing lines) a line to set the environment variable. I added the following line:

SDL_GAMECONTROLLERCONFIG=“030000004f04000008b1000000010000,Thrustmaster T.Flight Hotas X,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,”

My file looks like this after adding the line:

PATH=“/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”
JAVA_HOME=“/usr/lib/jvm/java-8-oracle”
SDL_GAMECONTROLLERCONFIG=“030000004f04000008b1000000010000,Thrustmaster T.Flight Hotas X,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,”

Then reboot to get Unity to recognize the joystick consistently. The above config still needs tweaking as the 4th axis needs to be replaced by the 5th axis. To end this thread I will post a config that works for me (also for my own future reference).

Thanks, Tak!

Hi everyone and thanks to RudderDuck and Tak, this post helped me.

But I have 2 problems :

  1. I don’t know how to map 2 different joysticks. I have the feeling that SDL_GAMECONTROLLERCONFIG can hold only one. Does someone know how to do that ?

  2. Input.GetJoystickNames() makes my app crash. The editor or the binary both close without any warning. I use Ubuntu 16.04 and Unity 2017.2.0f3. The same app works fine on an android phone, on android Tv, and mac (both editor and binary). Do you have the same problem ?

Thanks in advance
Olivier

It should be able to hold as many as you want I believe. Separate each by a comma like this.

Yes. I have seen this issue on Linux for many Unity versions. If you have the joysticks plugged in at start, it won’t crash usually, but if you unplug or plug one in, it will. It’s a threading problem apparently from the crash dump. I was bewildered when doing some routine testing with my plugin Rewired that it was crashing and it turned out to be that I was displaying the Input.GetJoystickNames results on screen just as a reference for what Unity’s input system was seeing. Since Rewired has its own native input implementation on Linux, as long as it’s enabled and you’re not using “Unity” as the input source in Rewired, it doesn’t suffer from this crash.

No it doesn’t work.

export SDL_GAMECONTROLLERCONFIG = “12555000…,stick1,…,…,…”,“32556685…,stick2,…” just maps the whole expression to the first joystick, same with : export SDL_GAMECONTROLLERCONFIG = “12555000…,stick1,…,…,…,32556685…,stick2,…”
And with a semicolon instead.

The example you’re talking about is extracted from the “gamecontrollerdb.txt”. I tried this too. I put the file in the app directory and then what ? Load with
SDL_GameControllerAddMappingsFromFile(“gamecontrollerdb.txt”); ? I tried to put this in a c# script but of course a reference to something is missing. It seems it’s beyond my skills. Google didn’t help.

As for the Input.GetJoystickNames() crashes, it’s eratic. Sometimes, it works as you describe it, sometimes it just crashes, even with joysticks already plugged.

Thank you anyway.

Can you show us the config file?

As far as I know each line needs to end with a comma and a newline. Putting more than 1 controller in 1 line could be the problem?

PATH=“/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”
SDL_GAMECONTROLLERCONFIG=“030000006f0e00001e01000011010000,Rock Candy Gamepad for PS3,platform:Linux,a:b1,b:b2,x:b0,y:b3,back:b8,start:b9,guide:b12,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,
030000000d0f0000c100000011010000,HORI CO.LTD. HORIPAD S,platform:Linux,a:b1,b:b2,x:b0,y:b3,back:b8,start:b9,guide:b12,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,”

Only the rock candy is mapped. If I switch the order, only the horipad is mapped.

And I just realize that changing a: b: etc values has no consequence, nor for sdl test or unity input manager (I can’t catch the joystick’s hat in unity). What did I not understand ?

According to this, it should work when newline delimited:

Here’s another example:
https://boilingsteam.com/making-third-party-gamepads-work-with-steam-games/

The file just contains the mappings. It should work when added to the SDL_GAMECONTROLLERCONFIG environment variable.

Perhaps you should ask for help on the SDL2 forums.

I have a controller that I currently do not use. If it helps I can try and see if I can add this as a second controller and see if it works?

Hey RudderDuck,

sorry for that delay, I was really really busy. If it’s not a problem for you to test a 2 controllers environment variable, it would be fine. I’m curious to see how a working file looks like.
Thank you