Skip to content

Instantly share code, notes, and snippets.

@reasonset
Created April 22, 2018 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reasonset/c142833d214a7d94578c6b6e374ae188 to your computer and use it in GitHub Desktop.
Save reasonset/c142833d214a7d94578c6b6e374ae188 to your computer and use it in GitHub Desktop.
Turn on/off device with xinput and zenity.
#!/usr/bin/zsh
setopt EXTENDED_GLOB
device="$(zenity --title="Select Device" --width=600 --height=500 --list --column="DEVICES" "${(f)$(xinput --list | ruby -e 'print ARGF.each.map {|i| i =~ /[^A-Za-z]*([A-za-z1-9 ]+[a-zA-Z0-9])\s*id=(\d+)/; "#$2 #$1\n" }.join.chomp')}"| perl -ne '/\d+/; print $&;')"
if (( $? == 0 )) && [[ -n $device ]]
then
action="$(zenity --list --column="ACTION" "turn on" "turn off")"
if (( $? == 0 )) && [[ -n $action ]]
then
if [[ $action == "turn off" ]]
then
backtime="$(zenity --title="Back time" --entry --text="Back after sec")"
if [[ "$backtime" == [0-9]## ]]
then
xinput set-int-prop $device "Device Enabled" 8 0
sleep $backtime
xinput set-int-prop $device "Device Enabled" 8 1
else
zenity --question --width=200 --text="Really turn off device $device?" && xinput set-int-prop $device "Device Enabled" 8 0
fi
else
xinput set-int-prop $device "Device Enabled" 8 1
fi
else
exit 1
fi
else
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment