1
0
Fork 1
mirror of https://github.com/Zerodya/hyprfreeze.git synced 2024-09-19 15:33:20 +02:00

Add notifications and more

- Added notifications function
- Added --silent and --notif-timeout flags
- Checks if hyprfreeze is trying to pause itself for every flag
- Rewrote and organized some of the code better
This commit is contained in:
Nicola 2023-10-20 18:27:42 +02:00 committed by GitHub
parent f2b216e0da
commit 02e9f00fde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,18 +2,20 @@
function printHelp() {
cat <<EOF
Usage: hyprfreeze (-a | -p [pid] | -n [name] | -r) [--info --dry-run]
Usage: hyprfreeze (-a | -p <pid> | -n <name> | -r) [options]
Hyprfreeze is an utility to suspend a game process (and other programs) in Hyprland.
Utility to suspend a game process (and other programs) in Hyprland.
Options:
-h, --help show help message
-a, --active pause/resume active window
-p, --pid pause/resume by process id
-n, --name pause/resume by process name/command
-r, --prop pause/resume by clicking on window
-a, --active suspend/resume active window
-p, --pid suspend/resume by process id
-n, --name suspend/resume by process name/command
-r, --prop suspend/resume by clicking on window
-s, --silent don't send notification
-t, --notif-timeout notification timeout in milliseconds (default 3000)
--info show information about the process
--dry-run doesn't actually pause/resume a process, useful with --info
--dry-run doesn't actually suspend/resume a process, useful with --info
EOF
}
@ -24,31 +26,26 @@ function toggleFreeze() {
# Get pids of process tree
PIDS=$(pstree -p $PID | grep -oP '\(\K[^\)]+')
# Pause or resume processes
if [[ "$(ps -o state= $PID)" == T ]]; then
kill -CONT $PIDS 2>/dev/null &&
echo "Resumed $(ps -p $PID -o comm= 2>/dev/null) ($PID)" || exit 1
else
kill -STOP $PIDS 2>/dev/null &&
echo "Stopped $(ps -p $PID -o comm= 2>/dev/null) ($PID)" || exit 1
fi
}
function freezeActive() {
PID=$(hyprctl activewindow -j | jq '.pid')
# Prevent pausing itself
script_pid=$$
if echo "$(pstree -p $PID | grep -oP '\(\K[^\)]+')" | grep -q "$script_pid"; then
# Prevent suspending itself
local pid_of_script=$$
if echo $PIDS | grep -q "$pid_of_script"; then
echo "You are trying to suspend the hyprfreeze process."
echo "This option is meant to be used via keybind to pause the active window, running it in terminal would suspend hyprfreeze itself."
exit 1
fi
toggleFreeze
# Suspend or resume processes
if [[ "$(ps -o state= $PID)" == T ]]; then
kill -CONT $PIDS 2>/dev/null || exit 1
else
kill -STOP $PIDS 2>/dev/null || exit 1
fi
}
function freezePid() {
function getPidByActive() {
PID=$(hyprctl activewindow -j | jq '.pid')
}
function getPidByPid() {
# Check if process pid exists
if ! ps -p $1 &>/dev/null; then
echo "Process ID $1 not found"
@ -56,10 +53,9 @@ function freezePid() {
fi
PID=$1
toggleFreeze
}
function freezeName() {
function getPidByName() {
# Check if process name exists
if ! pidof -x "$1" >/dev/null; then
echo "Process name $1 not found"
@ -68,12 +64,10 @@ function freezeName() {
# Get last process if there are multiple
PID=$(pidof $1 | awk '{print $NF}')
toggleFreeze
}
function freezeProp() {
function getPidByProp() {
PID=$(hyprprop | jq '.pid')
toggleFreeze
}
function printInfo() {
@ -88,19 +82,23 @@ function printInfo() {
\n$(tput bold)Process state$(tput sgr0) = $(ps -o state= -p $PID 2>/dev/null)"
}
function args() {
# Check if no options were passed
if [ -z "$1" ]; then
printHelp
exit 1
fi
function sendNotification() {
local title=$([[ "$(ps -o state= $PID)" == T ]] &&
echo "Suspended $(ps -p $PID -o comm= 2>/dev/null)" ||
echo "Resumed $(ps -p $PID -o comm= 2>/dev/null)")
# Track valid flags
local valid_flag_count=0
local message=$(echo "PID $PID")
notify-send "${title}" "${message}" -t "$NOTIF_TIMEOUT" -a Hyprfreeze
}
function args() {
# Track required flags
local required_flag_count=0
# Parse options
local options="hap:n:r"
local long_options="help,active,pid:,name:,prop,info,dry-run"
local options="hap:n:rst:"
local long_options="help,active,pid:,name:,prop,silent,notif-timeout:,info,dry-run"
local parsed_args=$(getopt -o $options --long $long_options -n "$(basename "$0")" -- "$@")
eval set -- "$parsed_args"
while true; do
@ -110,23 +108,30 @@ function args() {
exit 0
;;
-a | --active)
((valid_flag_count++))
((required_flag_count++))
FLAG_ACTIVE=true
;;
-p | --pid)
((valid_flag_count++))
((required_flag_count++))
shift
FLAG_PID="$1"
;;
-n | --name)
((valid_flag_count++))
((required_flag_count++))
shift
NAME_FLAG="$1"
;;
-r | --prop)
((valid_flag_count++))
((required_flag_count++))
FLAG_PROP=true
;;
-s | --silent)
SILENT=1
;;
-t | --notif-timeout)
shift
NOTIF_TIMEOUT="$1"
;;
--info)
INFO=1
;;
@ -145,29 +150,36 @@ function args() {
shift
done
# Check if more than one valid flag is provided, or if none was provided
if [ $valid_flag_count -ne 1 ]; then
# Check if more than one required flag is provided, or if none was provided
if [ $required_flag_count -ne 1 ]; then
printHelp
exit 1
fi
}
function main() {
# Handle the chosen valid flag
# Handle the chosen required flag
if [ "$FLAG_ACTIVE" = true ]; then
freezeActive
getPidByActive
toggleFreeze
elif [ -n "$FLAG_PID" ]; then
freezePid "$FLAG_PID"
getPidByPid "$FLAG_PID"
toggleFreeze
elif [ -n "$NAME_FLAG" ]; then
freezeName "$NAME_FLAG"
getPidByName "$NAME_FLAG"
toggleFreeze
elif [ "$FLAG_PROP" = true ]; then
freezeProp
getPidByProp
toggleFreeze
fi
# Run info function after PID is obtained
# Run these functions after PID is obtained
if [ $INFO -eq 1 ]; then printInfo; fi
if [ $SILENT -ne 1 ]; then sendNotification; fi
}
SILENT=0
NOTIF_TIMEOUT=3000
INFO=0
DRYRUN=0