Last active
May 3, 2023 19:25
-
-
Save WinterSnowfall/729a8287e0f2f465df637813d481113a to your computer and use it in GitHub Desktop.
xboxdrv-based Xbox controller emulation for DualShock (2, 3) controllers - tested on Ubuntu & derivatives
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# xboxdrv must be installed | |
#sudo apt-get install xboxdrv | |
# use evtest to test and determine controller/event inputs if in doubt (for the mappings below) | |
#sudo apt-get install evtest | |
# use fftest (part of joystick) to test rumble settings, if supported by the controller and kernel | |
#sudo apt-get install joystick | |
# adjust as needed for non-standard xboxdrv installations (using the full command path here) | |
XBOXDRVPATH=$(which xboxdrv) | |
# set to true if using a build of xboxdrv that has the 60s delay bug (default is false) | |
INITDELAY=false | |
echo ">>> Emulation of Xbox controllers for connected DualShock (2,3) controllers <<<" | |
# bash output styles and colors | |
DEFAULT="\033[0m" | |
BOLD="\033[1m" | |
RED="\033[1;31m" | |
GREEN="\033[1;32m" | |
YELLOW="\033[1;33m" | |
xboxdrv_processes=$(ps -ef | grep xboxdrv | grep -v grep | wc -l) | |
case "$1" in | |
start) | |
if [ $xboxdrv_processes -eq 0 ] | |
then | |
detected_controllers=$(ls -l /dev/input/by-path | grep event-joystick | wc -l) | |
if [ $detected_controllers -ne 0 ] | |
then | |
# need to use sudo further down so might as well prompt for password here | |
sudo echo "Detected $detected_controllers controller(s)..." | |
controller=0 | |
for event in $(ls -l /dev/input/by-path | grep event-joystick | awk "{print $7}" | cut -d "/" -f 2) | |
do | |
controller=$((++controller)) | |
echo -n "Emulating Xbox controller for controller$controller / $event... " | |
# mappings are compatible with DualShock, DualShock 2 and DualShock 3 controllers and clones (adjust as needed for other type of controllers) | |
# add --force-feedback below if you want to try and enable vibration (won't work most of the time) - note that some games may crash with force feedback enabled, so use with caution! | |
sudo -b nohup "$XBOXDRVPATH" --quiet --evdev /dev/input/$event --evdev-absmap ABS_X=x1,ABS_Y=y1,ABS_Z=x2,ABS_RZ=y2,ABS_HAT0X=dpad_x,ABS_HAT0Y=dpad_y --axismap -Y1=Y1,-Y2=Y2 --evdev-keymap BTN_WEST=y,BTN_EAST=b,BTN_SOUTH=a,BTN_NORTH=x,BTN_THUMBL=tl,BTN_THUMBR=tr,BTN_TL=lb,BTN_TR=rb,BTN_TL2=lt,BTN_TR2=rt,BTN_SELECT=back,BTN_MODE=guide,BTN_START=start --mimic-xpad 1>/dev/null 2>&1 | |
echo -e $GREEN"DONE"$DEFAULT | |
done | |
if $INITDELAY | |
then | |
echo "The Xbox controller(s) will initialize over the next ~60s:" | |
echo -ne $RED"0%" | |
for i in {0..60..2} | |
do | |
if [ $i -eq 30 ] | |
then | |
echo -ne $YELLOW"50%" | |
else | |
echo -n "." | |
fi | |
sleep 2 | |
done | |
echo -e $GREEN"100%"$DEFAULT | |
sleep 1 | |
else | |
sleep 1 | |
fi | |
echo "Xbox controller emulation is now active." | |
else | |
echo -e $RED"No controllers detected."$DEFAULT" Retry running the script after unplugging/replugging all controller devices." | |
exit 1 | |
fi | |
else | |
echo "Xbox controller emulation is already running." | |
exit 2 | |
fi | |
;; | |
stop) | |
if [ $xboxdrv_processes -gt 0 ] | |
then | |
# need to use sudo below so might as well prompt for password here | |
sudo echo -n "Terminating Xbox controller emulation... " | |
sudo killall xboxdrv | |
echo -e $GREEN"DONE"$DEFAULT | |
else | |
echo "Xbox controller emulation is not running." | |
exit 3 | |
fi | |
;; | |
*) | |
echo -e "Invalid option! Use either "$BOLD"start"$DEFAULT" or "$BOLD"stop"$DEFAULT"." | |
exit 4 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment