Script to "remotely" control computer with Xbox controller.

Freepoorman

Active Member
Joined
Mar 8, 2024
Messages
263
Reaction score
164
Credits
2,248
I do some gaming with my Xubuntu system, so I have a Xbox360 controller plugged in most of the time. Sometimes I just want to watch YouTube videos or movies without having to readjust my posture or walk up to my laptop to access the keyboard/mouse.

I've been using this script I created to control my laptop with my controller. It needs xboxdrv to be installed with
Bash:
sudo apt install xboxdrv

The script provides a menu to either run xboxdrv with custom or default configurations, or to load/unload the xpad module.

Here is a summary of the workflow and the custom button mappings:

    • Main Menu Controls:
      • 1:
        • If xpad is loaded: Deactivate xpad and run xboxdrv with custom configuration.
        • If xpad is not loaded: Run xboxdrv with custom configuration.
      • 2:
        • If xpad is loaded: Deactivate xpad and run xboxdrv with default configuration.
        • If xpad is not loaded: Run xboxdrv with default configuration.
      • 3:
        • If xpad is not loaded: Load xpad and exit the script.
      • Esc: Exit the terminal.
    • Custom Configuration for xboxdrv:
      • Start Button: Maps to Ctrl + Esc.
      • Back Button: Maps to Alt + Tab.
      • D-Pad Up: Maps to Up Arrow.
      • D-Pad Down: Maps to Down Arrow.
      • D-Pad Right: Maps to Right Arrow.
      • D-Pad Left: Maps to Left Arrow.
      • Left Bumper (LB): Maps to Left Ctrl.
      • Left Trigger (LT): Maps to Right Mouse Button.
      • Right Bumper (RB): Maps to Left Shift.
      • Right Trigger (RT): Maps to Left Mouse Button.
      • Y Button: Maps to Tab.
      • B Button: Maps to Esc.
      • A Button: Maps to Enter.
      • X Button: Maps to Space.
      • Right Stick Click (TR): Maps to Delete.
      • Left Stick: Maps to scroll wheel (horizontal and vertical).
      • Right Stick: Maps to mouse pointer movement.
    • Default Configuration for xboxdrv:
      • The default configuration is not explicitly defined in the script, so it will use xboxdrv's default settings.

Here is the script, and it runs with sudo (
Bash:
sudo script.sh
):


Bash:
#!/bin/bash

# Function to handle errors with more context
handle_error() {
    echo "An error occurred at line ${BASH_LINENO[0]}:"
    echo "Command: $BASH_COMMAND"
    echo "Error: $1"
    read -p "Press any key to exit." -n 1 -s
    exit 1
}

# Trap errors
trap 'handle_error "$BASH_COMMAND"' ERR

# Function to check if a module is loaded
is_module_loaded() {
    local module="$1"
    if lsmod | grep -q "^$module "; then
        return 0
    else
        return 1
    fi
}

# Function to load a module
load_module() {
    local module="$1"
    modprobe "$module" || handle_error "Failed to load $module"
    echo "$module successfully loaded."
}

# Function to unload a module
unload_module() {
    local module="$1"
    rmmod "$module" || handle_error "Failed to remove $module"
    echo "$module successfully removed."
}

# Check if xpad is loaded
if is_module_loaded "xpad"; then
    echo -e "xpad is loaded.\nPress 1 to deactivate xpad and run xboxdrv with custom configuration,\n2 to deactivate xpad and run xboxdrv with default configuration,\nor Esc to exit the terminal."
   
    # Read a single character from the user
    read -rsn1 input
   
    if [ "$input" = "1" ]; then
        echo "Disabling xpad and running xboxdrv with custom configuration..."
        unload_module "xpad"
        custom_config=true
    elif [ "$input" = "2" ]; then
        echo "Disabling xpad and running xboxdrv with default configuration..."
        unload_module "xpad"
        custom_config=false
    else
        echo "Exiting terminal."
        exit 0
    fi
else
    echo -e "xpad is not loaded.\nPress 1 to run xboxdrv with custom configuration,\n2 to run xboxdrv with default configuration,\n3 to load xpad,\nor Esc to exit the terminal."
   
    # Read a single character from the user
    read -rsn1 input
   
    if [ "$input" = "1" ]; then
        echo "Running xboxdrv with custom configuration..."
        custom_config=true
    elif [ "$input" = "2" ]; then
        echo "Running xboxdrv with default configuration..."
        custom_config=false
    elif [ "$input" = "3" ]; then
        echo "Activating xpad..."
        load_module "xpad"
        exit 0
    else
        echo "Exiting terminal."
        exit 0
    fi
fi

# Run xboxdrv with the appropriate configuration
if [ "$custom_config" = true ]; then
    xboxdrv --silent \
      --ui-buttonmap start=KEY_LEFTCTRL+KEY_ESC,back=KEY_LEFTALT+KEY_TAB \
      --ui-buttonmap du=KEY_UP,dd=KEY_DOWN,dr=KEY_RIGHT,dl=KEY_LEFT \
      --ui-buttonmap lb=KEY_LEFTCTRL,lt=BTN_RIGHT,rb=KEY_LEFTSHIFT,rt=BTN_LEFT,y=KEY_TAB,b=KEY_ESC,a=KEY_ENTER,x=KEY_SPACE \
      --ui-buttonmap tr=KEY_DELETE \
      --ui-axismap X1=void,Y1=void \
      --axismap X2^deadzone:4000,Y2^deadzone:4000 \
      --ui-axismap X2=REL_X:10,Y2=REL_Y:10 \
      --ui-axismap Y1=REL_WHEEL:1:100,Y1=REL_WHEEL:-1:100 \
      --ui-axismap X1=REL_HWHEEL:-10,X1=REL_HWHEEL:10 || handle_error "Failed to start xboxdrv with custom configuration"
else
    xboxdrv || handle_error "Failed to start xboxdrv with default configuration"
fi

echo "xboxdrv started successfully."


Feel free to use, edit, and share it as you wish.
 
Last edited:


Members online


Latest posts

Top