How do i enable XInput in Raspberry Pi?

 Enabling XInput on the Raspberry Pi allows it to properly recognize modern game controllers (like Xbox and PlayStation controllers) as "XInput devices," which ensures they work correctly with many games and emulators that expect that standard.



Here's a complete guide on how to do it.


What is XInput?

  • XInput: A Microsoft API for PC gaming that became the standard with the Xbox 360 controller. Games designed for XInput expect buttons and triggers to be laid out in a specific way.

  • The Alternative (DInput): The older DirectInput API, used by many PlayStation and generic controllers.

Many Linux games and emulators (like RetroPie) can work with both, but some work much better with XInput. Enabling it ensures your controller is seen as an Xbox-compatible gamepad.

Method 1: For Wired Xbox 360 & Xbox One Controllers (The xpad driver)

This is the most common scenario. The Linux kernel already includes the xpad driver for these controllers, but it might not be loaded by default or might be blocked by another driver.

  1. Connect your controller via USB.

  2. Check if it's already working. Run lsusb in the terminal. You should see a line for "Xbox" or "Microsoft Corp." If it works in games already, you're done!

  3. If it's not working, check what driver is being used. Run:

    bash
    lsmod | grep xpad

    If you see no output, the xpad module is not loaded. If you see output, it is loaded.

  4. Check for conflicting drivers. The xtrig driver can sometimes interfere. Check with:

    bash
    lsmod | grep xtrig

    If this returns a result, you need to blacklist it.

  5. Enable the xpad driver.

    • Open the modules file for editing:

      bash
      sudo nano /etc/modules
    • Add a new line with just xpad on it. If it's already there, you're good.

    • Press Ctrl+X, then Y, then Enter to save and exit.

  6. Blacklist the conflicting driver (if necessary).

    • If xtrig was loaded, create a new blacklist file:

      bash
      sudo nano /etc/modprobe.d/blacklist-xtrig.conf
    • Add the following line:

      text
      blacklist xtrig
    • Save and exit the file (Ctrl+XYEnter).

  7. Reboot your Raspberry Pi.

    bash
    sudo reboot

After the reboot, your wired Xbox controller should be recognized as an XInput device. You can verify by running lsmod | grep xpad again or checking in a game.


Method 2: For Other Controllers & Advanced Mapping (using xboxdrv)

xboxdrv is a more powerful, userspace driver that can make any controller (PlayStation, Nintendo Switch Pro, generic USB) appear as an XInput device. It's highly recommended for RetroPie and other emulation setups.

  1. Install xboxdrv:

    bash
    sudo apt update
    sudo apt install xboxdrv
  2. Test it temporarily. Run the following command and then plug in your controller:

    bash
    sudo xboxdrv --silent --detach-kernel-driver
    • --silent: Suppresses the on-screen log.

    • --detach-kernel-driver: Detaches the default kernel driver (like hid-generic) to let xboxdrv take over.

    • Now test your controller in a game or with jstest (install with sudo apt install joystick). It should now be seen as an Xbox controller.

  3. Make it permanent with a service.

    • Create a systemd service file:

      bash
      sudo nano /etc/systemd/system/xboxdrv.service
    • Paste the following configuration. This is a common setup that works for many controllers:

      ini
      [Unit]
      Description=Xbox Drv service
      After=multi-user.target
      
      [Service]
      Type=simple
      ExecStart=/usr/bin/xboxdrv --silent --detach-kernel-driver --dbus disabled
      
      [Install]
      WantedBy=multi-user.target
    • Save and exit the file.

  4. Enable and start the service:

    bash
    sudo systemctl enable xboxdrv.service
    sudo systemctl start xboxdrv.service
  5. Reboot to test.

    bash
    sudo reboot

Your controller should now work as an XInput device every time you boot.


Method 3: For Bluetooth Controllers (e.g., PS4, PS5, Xbox One S)

For Bluetooth controllers, you need to pair them first, and then they will often be picked up correctly by the system.

  1. Install necessary tools:

    bash
    sudo apt install pi-bluetooth bluez
  2. Pair your controller.

    • Put your controller into pairing mode.

      • PS4: Hold the PS Button and Share button until the light bar flashes.

      • PS5: Hold the PS Button and Create button until the light blinks.

      • Xbox One S: Press the Pairing button on the top.

    • Use bluetoothctl to scan and pair:

      bash
      bluetoothctl
      # Inside the bluetoothctl shell:
      agent on
      scan on
      # Wait for your controller to appear (e.g., "Wireless Controller").
      trust [MAC-ADDRESS]
      pair [MAC-ADDRESS]
      connect [MAC-ADDRESS]
      quit
  3. The controller should now be connected. By default, recent versions of the kernel and BlueZ will often use the hid-sony or hid-generic driver, which may already emulate XInput. If it doesn't work perfectly, you can use Method 2 (xboxdrv) to force it to be an XInput device, even over Bluetooth.

    You would run xboxdrv with the --detach-kernel-driver option and specify the device by its ID if you have multiple controllers. This is best done through the systemd service file created in Method 2.


Verification

To confirm your controller is being seen as an XInput device, you can use the jstest tool or check the /dev/input directory.

  1. Install joystick package:

    bash
    sudo apt install joystick
  2. Run jstest:

    bash
    jstest /dev/input/js0

    (If you have multiple controllers, it might be js1, etc.). Press buttons and see if the inputs are registered correctly. The button layout should correspond to the Xbox standard (A, B, X, Y, etc.).

By following the method appropriate for your controller, you should now have XInput successfully enabled on your Raspberry Pi.

评论

此博客中的热门博文

How To Connect Stm32 To PC?

Detailed Explanation of STM32 HAL Library Clock System

How to add a GPS sensor to ESP32 for Wokwi?