Skip to content

Instantly share code, notes, and snippets.

@odysseywestra
Last active April 13, 2019 05:17
Show Gist options
  • Save odysseywestra/9ece83f32b833ffa5414e6008fc6b2a7 to your computer and use it in GitHub Desktop.
Save odysseywestra/9ece83f32b833ffa5414e6008fc6b2a7 to your computer and use it in GitHub Desktop.
Add-On script for AntergrosPrime and to be used with Configurable Button https://store.kde.org/p/1297839.
#! /bin/sh
#:
#: AntergosPrime/Configurable Button GPU Switch Script
#:
#: Small script to automate the process of switching gpu in KDE. This
#: is ment to be used with AntergrosPrime, and a widget called
#: Configurable button.
#:
#: This script has three functions:
#: 1. Activate "prime-select" to switch to either nvida or intel GPUs
#: 2. Logout the user, or restart the laptop to complete the process.
#: 3. Keep track of which GPU is in use for the Configurable Button.
#:
#: Usage: ./gpu_switch.sh [Option]
#:
#: Options:
#: --help Show this message and exit.
#: --intel Selects intel and complets changes.
#: --nvidia Selects nvidia and complets changes.
#: --status Checks status of which GPU it's using.
#:
#: By default, the script will log the user out when changing to intel. It will
#: reboot if changing to nvidia. Change variables in the script if you want a
#: result. Also it's recomended to have the status part of the Configurable buttons
#: to refresh every few seconds incase you cancel it when it ask for your
#: sudo password.
#:
#: License: CC-0/Public-Domain/WTFPL (http://www.wtfpl.net/) license
#:
### User Configuration
## Logout or Shutdown?
# For my case intel is okay by logging out and in. On the other hand
# my Nvidia card breaks my display server so I need to reboot the Laptop.
# Complete change to intel by logging out. If false, the PC will reboot.
logout_intel=true
# Complete change to nvidia by logging out. If false, the PC will reboot.
logout_nvidia=false
# No need to make any changes below unless you know what you are doing.
#######################################################################
## Activate Prime-Select
check=false
select_intel(){
notify-send "GPU Switch" "WARNING: Close all programs and save before proceeding."
kdesu --noignorebutton -n prime-select intel
check=true
gpu_status
if [ $status = 1 ] ; then
notify-send "GPU Switch" "ACTION CANCELED: Staying on Nvidia"
exit 0
else
if [ $logout_intel = true ]; then
logout=true
else
logout=false
fi
complete_change
fi
}
select_nvidia(){
notify-send "GPU Switch" "WARNING: Close all programs and save files before proceeding."
kdesu --noignorebutton -n prime-select nvidia
check=true
gpu_status
if [ $status = 0 ]; then
notify-send "GPU Switch" "ACTION CANCELED: Staying on Intel"
exit 0
else
if [ $logout_nvidia = true ]; then
logout=true
else
logout=false
fi
complete_change
fi
}
## Logout/Shutdown to complet change
complete_change(){
notify-send "GPU Swtich" "Completing Changes"
if [ $logout = true ]; then
qdbus org.kde.ksmserver /KSMServer logout 0 3 3
else
reboot
fi
}
gpu_status(){
status=$(type -p nvidia-smi | grep -c nvidia)
if [ $status = 1 ]; then
echo "Running Nvidia"
if [ $check = false ]; then
exit 1
fi
else
echo "Running Intel"
if [ $check = false ]; then
exit 0
fi
fi
}
# Error Message
run_help(){
grep '^#:' $0 | cut -d ':' -f 2-50
exit 0
}
## Commandline Processing
case "$1" in
--help)
run_help
;;
--intel)
select_intel
;;
--nvidia)
select_nvidia
;;
--status)
gpu_status
;;
*)
run_help
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment