TheAlkaris

DualShock 4 Check Script

Jan 29th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.81 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. NC='\033[31;0m'
  4. RED='\033[0;31;1m'
  5. PUR='\033[0;35;1m'
  6. YEL='\033[0;33;1m'
  7. GRA='\033[0;37;1m'
  8.  
  9. if [ "$(id -u)" != "0" ]; then
  10.    printf "\n\n${RED}== /!\ DO NOT USE ROOT! /!\ ==\n\n\${NC}" 2>&1
  11.    exit 1
  12. fi
  13.  
  14. ## DS4 Check Script
  15. ## Sony DualShock 4 controller script for Linux
  16. ##
  17. ## /!\ THIS SCRIPT IS EXPERIMENTAL AND MAY REQUIRE TWEAKING TO GET THE RESULT YOU WANT /!\
  18. ##
  19. ## make sure you have ds4drv and xboxdrv installed on your system in order to use this.
  20. ## this script is to attempt to detect DualShock 4 controller when plugged into the system
  21. ## and automatically apply a config in ds4drv.conf, you can change LED bar color to any defined in the conf
  22. ## there are RGB color limitations to the LightBar on the DS4.
  23. ##
  24. ## List of tested valid LED colors for DualShock 4 written by me:
  25. ##  - https://github.com/chrippa/ds4drv/wiki/Light-Bar-Colors
  26. ##
  27. ## first it'll attempt to lock the ds4drv to its own PID so there is only 1 instance running at all times.
  28. ## if a PID file already exists it will exit, otherwise it'll create and lock to one. Check your $HOME/.cache
  29. ## directory for the log file labeled ds4drv.log. A secondary log may be contained within systems own Syslog
  30. ## detailing hidraw and udev events.
  31. ##
  32. ## if controller gets disconnected, it should hopefully unlock the PID file and remove it.
  33. ## if you do not have a ds4drv.conf you can get a template from here:
  34. ##    https://github.com/chrippa/ds4drv/blob/master/ds4drv.conf
  35.  
  36. pidfile=/tmp/ds4drv.pid
  37.  
  38. # check if process is already running
  39. for pid in $(pidof -x $HOME/scripts/ds4check.sh $pidfile); do
  40.     if [ $pid != $$ ]; then
  41.       echo "[$(date "+%Y-%m-%d - %I:%M:%S %p")] : ds4check.sh : Process is already running with PID $pid\n" >> $HOME/.cache/ds4drv.log
  42.       printf "\n${PUR}[$(date "+%Y-%m-%d - %I:%M:%S %p")]${NC} : ds4check.sh : ${RED}Process is already running with PID $pid\n\n${NC}" 2>&1
  43.       exit 1
  44. # if not running then run and apply config
  45.       else  ( ds4drv --hidraw --config $HOME/.config/ds4drv.conf & disown );
  46.      
  47.       exit 0
  48.     fi
  49. done
  50.  
  51. ##     === /!\/!\ DO NOT RUN AS ROOT /!\/!\ ===
  52. ## if you are having permission issues running this, you will need to create a udev rule for normal users.
  53. ## read here on how to create udev rule: https://github.com/chrippa/ds4drv
  54.  
  55.  
  56. ## just ignore this stuff below, it wasn't working out so well, but I may fix it eventually.
  57. ##
  58. # remove PID file on exit... hopefully
  59. #if ( set -o noclobber; echo "locked" > "$pidfile") 2> /dev/null; then
  60. #   trap 'rm -f "$pidfile"; exit $?' INT TERM EXIT
  61. #   echo "Locking succeeded" 2> $HOME/.cache/ds4drv.log;
  62. #    rm -f "$pidfile"
  63. #else
  64. #   echo "Lock failed - exit" 2> $HOME/.cache/ds4drv.log;
  65. #   exit 1
  66. #fi
  67.  
  68. #trap "srm -rv -- '$pidfile'" EXIT >> $HOME/.cache/ds4drv.log
  69.  
  70. echo " "
  71. echo "DualShock 4 Check Script"
  72. echo " "
Add Comment
Please, Sign In to add comment