This commit is contained in:
Quad 2021-05-22 15:08:41 +02:00
parent 8c942bc6b0
commit 067570aab5
2 changed files with 94 additions and 3 deletions

2
scripts/gpd-sudo-prompt Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
zenity --password --title="sudo Authentication"

View File

@ -5,6 +5,9 @@ command_name=$(basename $0)
rapl_path="/sys/class/powercap/intel-rapl:0"
pl1_path="${rapl_path}/constraint_0_power_limit_uw"
pl2_path="${rapl_path}/constraint_1_power_limit_uw"
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
gui_name="GPD TDP Manager"
gui_options=""
# Set up help text
help_text="${command_name}: GDP Win 3 TDP management script
@ -46,6 +49,11 @@ Arguments:
--detail, -d Prints PL2 as well as PL1
--same, -s PL2 will be set to the same as PL1 rather than 2W higher
--help Prints this help text"
gui_help_text="${command_name} gui
No help is available for the GUI.
If it does not work, ensure the \"zenity\" command is installed and available"
print_help () {
case $1 in
@ -55,6 +63,9 @@ print_help () {
"set")
echo "$set_help_text"
;;
"set")
echo "$gui_help_text"
;;
*)
echo "$help_text"
;;
@ -93,6 +104,38 @@ is_detailed () {
fi
}
is_help () {
if [[ "$@" == *"--help"* ]] || [[ "$@" == *"-h"* ]]; then
exit 0
else
exit 1
fi
}
is_number () {
if ! [[ $1 =~ ^-?[0-9]+$ ]]; then
exit 1
else
exit 0
fi
}
gui_msg () {
zenity --info --title="$gui_name" $gui_options --text="$1" --no-wrap
}
gui_read () {
zenity --entry --title="$gui_name" $gui_options --text="$1:"
}
gui_ask () {
if $(zenity --question --title="$gui_name" $gui_options --text="$1" --no-wrap); then
exit 0
else
exit 1
fi
}
set_pl () {
if [ $1 -eq 1 ] || [ $1 -eq 2 ] || [ ! -z $2 ]; then
if [ $1 -eq 1 ]; then
@ -129,7 +172,7 @@ check_tdp () {
echo "PL2 is ${pl2}W" # Placeholder
fi
elif [ "$1" == "--help" ]; then
elif ( is_help $@ ); then
print_help "check"
else
@ -142,10 +185,10 @@ set_tdp () {
if [ -z "$1" ]; then
echo "Please specify wattage"
exit
elif [ "$1" == "--help" ]; then
elif ( is_help $@ ); then
print_help "set"
exit
elif ! [[ $1 =~ ^-?[0-9]+$ ]]; then
elif ! (is_number $1); then
echo "TDP is not a number or argument unknown!)"
print_unknown
exit
@ -174,6 +217,48 @@ set_tdp () {
fi
}
# Basic GUI using Zenity
gui_handler() {
if [ -z $(which zenity 2>/dev/null) ]; then
echo "Zenity is not available, GUI will not work until it is installed"
elif (is_help $@); then
print_help "gui"
else
export SUDO_ASKPASS="${script_dir}/gpd-sudo-prompt"
while : ; do
# Contains some ugly hacks to "widen" the entries for the small GPD screen, might tweak later
gui_action=$(zenity --list --title="${gui_name}" --width=400 --height=300 $gui_options --text="Chose action to perform:" --hide-column=1 --column="" --column="Action"\
check "`printf "\n Check current TDP\n "`" \
set "`printf "\n Set new TDP (requires sudo password)\n "`" \
exit "`printf "\n Exit TDP Manager\n "`")
case $gui_action in
"check")
gui_msg "$($command_name check --detail)"
;;
"set")
gui_tdp=$(gui_read "Please enter TDP")
if ! [ -z $gui_tdp ]; then
if $(gui_ask "Should PL2 be set to the same wattage?\nIf unsure, answer No."); then
gui_msg "$(tdp set $gui_tdp --same --detail)"
else
gui_msg "$(tdp set $gui_tdp --detail)"
fi
gui_tdp=""
fi
;;
*)
break
;;
esac
done
fi
}
# Command handler
case $1 in
@ -185,6 +270,10 @@ case $1 in
set_tdp "${@:2}"
;;
"gui" | "ui") # Pass to GUI handler
gui_handler "${@:2}"
;;
"help")
print_help
;;