Advertisement
metalx1000

Control Shell Script Menu with Game Controller Joystick

Jul 6th, 2024
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.51 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2024  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. #uses the first game controller to select items from menu
  20.  
  21. let i="0"
  22. list=("continue" "reboot" "poweroff" "thunar")
  23.  
  24. function next() {
  25.     [[ $1 ]] && let i+=1 || let i-=1
  26.     [[ $i == ${#list[@]} ]] && let i=0
  27.     [[ $i -lt 0 ]] && let i=${#list[@]}-1
  28.     selected
  29. }
  30.  
  31. function selected() {
  32.     clear
  33.     echo "What would you like to do?"
  34.     echo ${list[$i]}
  35. }
  36.  
  37. function select_item() {
  38.     [[ ${list[$i]} == "continue" ]] && exit
  39.     eval ${list[$i]}
  40.     exit
  41. }
  42.  
  43. selected
  44.  
  45. jstest --event /dev/input/js0 | while read l; do
  46.     [[ "$l" == *"number 1, value 1"* ]] && select_item
  47.     [[ "$l" == *"number 2, value 1"* ]] && select_item
  48.     [[ "$l" == *"number 5, value -32767"* ]] && next
  49.     [[ "$l" == *"number 5, value 32767"* ]] && next 1
  50. done
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement