Search Unity

No joystick detected

Discussion in 'Linux' started by CoWaygere, Jun 10, 2017.

  1. CoWaygere

    CoWaygere

    Joined:
    Jun 2, 2017
    Posts:
    1
    Hello, was anyone able to get detection of joysticks on Linux?

    Code (CSharp):
    1. Input.GetJoystickNames();
    Returns always empty string.

    Here is my code to test joysticks:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class JoystickTest : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Awake() {
    9.         string[] names = Input.GetJoystickNames();
    10.         Debug.Log("Connected Joysticks:");
    11.         for(int i = 0; i < names.Length; i++) {
    12.             Debug.Log("Joystick" + (i + 1) + " = " + names[i]);
    13.         }
    14.     }
    15.    
    16.     // Update is called once per frame, (if any joystick was connected during gameplay
    17.     void Update() {
    18.         Debug.Log(Input.GetJoystickNames().Length);
    19.     }
    20. }
    21.  
    Here is settings of input:

     
  2. Tak

    Tak

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    What joystick(s) are you using?
     
  3. RudderDuck

    RudderDuck

    Joined:
    Jul 21, 2015
    Posts:
    7
    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.



     
  4. Tak

    Tak

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    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.
     
  5. jamesremuscat

    jamesremuscat

    Joined:
    May 31, 2017
    Posts:
    1
    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?
     
  6. RudderDuck

    RudderDuck

    Joined:
    Jul 21, 2015
    Posts:
    7
    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 https://wiki.archlinux.org/index.php/Gamepad

    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.


     
    Last edited: Sep 9, 2017
  7. Tak

    Tak

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    Did it print the game controller info as well?
    Or GameController: not a gamepad?

    (Unity is currently using the GameController API exclusively)
     
  8. RudderDuck

    RudderDuck

    Joined:
    Jul 21, 2015
    Posts:
    7
    ./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?
     
  9. Tak

    Tak

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    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,"
    )
     
  10. RudderDuck

    RudderDuck

    Joined:
    Jul 21, 2015
    Posts:
    7
    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.
     
    Last edited: Sep 11, 2017
  11. RudderDuck

    RudderDuck

    Joined:
    Jul 21, 2015
    Posts:
    7
    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!
     
    Last edited: Sep 11, 2017
  12. olipoh1

    olipoh1

    Joined:
    Jul 1, 2016
    Posts:
    5
    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
     
  13. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,632
    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.
     
    Last edited: Nov 2, 2017
  14. olipoh1

    olipoh1

    Joined:
    Jul 1, 2016
    Posts:
    5
    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.
     
  15. RudderDuck

    RudderDuck

    Joined:
    Jul 21, 2015
    Posts:
    7
    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?
     
  16. olipoh1

    olipoh1

    Joined:
    Jul 1, 2016
    Posts:
    5
    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.
     
  17. olipoh1

    olipoh1

    Joined:
    Jul 1, 2016
    Posts:
    5
    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 ?
     
  18. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,632
    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.
     
    Last edited: Nov 3, 2017
  19. RudderDuck

    RudderDuck

    Joined:
    Jul 21, 2015
    Posts:
    7
    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?
     
  20. olipoh1

    olipoh1

    Joined:
    Jul 1, 2016
    Posts:
    5
    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
     
  21. nerdnils

    nerdnils

    Joined:
    Dec 7, 2017
    Posts:
    36
    And in 2017 unity still has problems detecting joysticks and gamepads which work in EVERY other game…
    [edit] 2018 [/edit]
     
    Last edited: Jan 1, 2018
  22. Klaus-Eiperle

    Klaus-Eiperle

    Joined:
    Mar 10, 2012
    Posts:
    41
    is there still no useful way to get joystick input on Linux?
     
  23. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,632
    I suggest you try Rewired. It comes with a native input library for Linux that works with most joysticks.
     
  24. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,551
    You can try the Rewired free trial version to see how well it works for you. Then you can do the evaluation before your purchase the product. Rewired has worked very well with all of the flight controllers and some joysticks I have tried with Linux. It nice to have same experience on Windows/MAC/Linux when it comes to controllers thanks to Rewired.

    I am using Ubuntu Linux which was recommended by Unity.
     
    guavaman likes this.
  25. Klaus-Eiperle

    Klaus-Eiperle

    Joined:
    Mar 10, 2012
    Posts:
    41
    Thank you very much for your reply. I have to buy third-party software to end this nightmare? I thought this should be implemented in Unity, as most Linux games require an input device. :-(
     
  26. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,551
    Back in 2014 I was blocked on input on multiple platforms for HOTAS support using Unity's input. But started using Rewired so at least I am not longer blocked on input. That is how I got out my nightmare of input support.

    You can try it the free trial of Rewired so at least you have a benchmark if it works or not with your device.

    When are you shipping your product? That is probably the most important question on whether you are going to wait for Unity to solve the issue or purchase a third party product. Have you checked the Unity roadmap to see when the new input system is coming out?
     
  27. Klaus-Eiperle

    Klaus-Eiperle

    Joined:
    Mar 10, 2012
    Posts:
    41
    My 'neXt - CGM rc Flight Simulator' has been sold for 7 years now. When I switched to a newer Unity3D engine, Linux joystick support was broken because of SDL2. Now I am looking for a simple solution. Presumably, I look at Rewired.
     
  28. Klaus-Eiperle

    Klaus-Eiperle

    Joined:
    Mar 10, 2012
    Posts:
    41
    I like to post here my solution for this problem. I don't like to add any 3rd party libraries to my build. So, we made a little program for Linux which installs the needed SDL2 libraries, configures the joystick and saves the controller map. The user has to execute this only once. Then a second program loads the controller map and starts the simulator. Everything is working very well. :)
     
  29. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,551
    Do you only run on a specific distro and version of Linux? Or do you run on various Linux distros? Do you install just libraries in the same directory as your application? Curious if you just install locally with your application or use RPM to install? I guess a pre-req is the end user has admin rights?
     
  30. Klaus-Eiperle

    Klaus-Eiperle

    Joined:
    Mar 10, 2012
    Posts:
    41
    Unity3D needs Ubuntu 12.4 or later. That was their official statement. And our joystick solution is working on Ubuntu 14.4 or higher because since these version LIBSDL2 is possible. So, this is no limitation for me. The needed libraries were installed on the system. The user needs the admin password. The configuration file will be saved into my data folder.