refactor(hypr): improve monitoring scripts
Some checks failed
Check Formatting of Files / Check-Formatting (push) Failing after 1m7s

This commit is contained in:
Price Hiller 2024-06-27 23:36:47 -05:00
parent 8faf39d2b1
commit f1b9df39eb
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
5 changed files with 129 additions and 91 deletions

View File

@ -1,4 +1,4 @@
monitor = ,preferred,auto,auto
monitor=eDP-1,preferred,0x0,1.5
bindl=,switch:off:Lid Switch,exec,brightnessctl s 50%
bindl=,switch:on:Lid Switch,exec,brightnessctl s 0%
bindl=,switch:off:Lid Switch,exec,systemd-run --user ~/.config/hypr/scripts/laptop-lid.bash
bindl=,switch:on:Lid Switch,exec,systemd-run --user ~/.config/hypr/scripts/laptop-lid.bash

View File

@ -4,7 +4,7 @@ exec = systemd-run --user --unit=udiskie udiskie --tray || systemctl --user rest
exec = systemd-run --user --unit=blueman-applet blueman-applet || systemctl --user restart blueman-applet
exec = systemd-run --user --unit=slimbookbattery slimbookbattery --minimize || systemctl --user restart slimbookbattery
exec = systemd-run --user --unit=clight-gui clight-gui --tray || systemctl --user restart clight-gui
exec = systemd-run --user --unit=events-monitor ~/.config/hypr/scripts/launchers/events-monitor.bash || systemctl --user restart events-monitor
exec = systemd-run --user --unit=monitor-ssid ~/.config/hypr/scripts/monitor-ssid.bash || systemctl --user restart monitor-ssid
exec = systemd-run --user --unit=xwaylandvideobridge xwaylandvideobridge || systemctl --user restart xwaylandvideobridge
exec = systemd-run --user --unit=polkit-agent /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 || systemctl --user restart polkit-agent
exec-once = systemctl --user import-environment WAYLAND_DISPLAY XDG_CURRENT_DESKTOP PATH

View File

@ -0,0 +1,76 @@
#!/usr/bin/env bash
set -eEuo pipefail
log() {
local syslog_id="laptop-lid"
local title="Laptop Clamshell"
local level="INFO"
local args="${*}"
if (($# > 1)); then
local level="${1}"
shift
fi
local msg="${*}"
case "${level^^}" in
"TRACE")
systemd-cat -t "$syslog_id" -p "debug" <<<"$msg"
logger --stderr -p "user.debug" "$syslog_id" "$msg"
;;
"INFO")
systemd-cat -t "$syslog_id" -p "notice" <<<"$msg"
notify-send "$title" "$msg" -a "$title"
;;
"ERROR")
systemd-cat -t "$syslog_id" -p "error" <<<"$msg"
notify-send "$title" "$msg" -a "$title" -u "critical"
;;
*)
systemd-cat -t "$syslog_id" -p "error" <<-__EOS__
Invalid log level passed!
Received input as: '${args}'
__EOS__
return 1
;;
esac
}
handle-laptop-lid() {
local laptop_lid_state
local laptop_mon="${1:-"eDP-1"}"
local laptop_lid_acpi_path="${2:-"/proc/acpi/button/lid/LID0/state"}"
if [[ ! -r "$laptop_lid_acpi_path" ]]; then
log "ERROR" "Unable to read laptop state from ACPI path: '${laptop_lid_acpi_path}'"
return 1
fi
log "TRACE" "Checking monitor laptop lid state at: '${laptop_lid_acpi_path}'"
laptop_lid_state="$(</proc/acpi/button/lid/LID0/state)"
laptop_lid_state="${laptop_lid_state##* }"
laptop_lid_state="${laptop_lid_state^^}"
log "TRACE" "Laptop lid state: '${laptop_lid_state}'"
case "$laptop_lid_state" in
"OPEN")
if ! hyprctl monitors -j | jq -er '.[] | select(.name=="eDP-1")' >/dev/null; then
log "TRACE" "Laptop lid is open, attempting to enable it..."
if hyprctl keyword monitor "${laptop_mon},enable" >/dev/null; then
log "Laptop screen enabled"
else
log "ERROR" "Received an error when enabling the laptop screen!"
fi
fi
;;
"CLOSED")
if hyprctl monitors -j | jq -er '.[] | select(.name=="eDP-1")' >/dev/null; then
log "TRACE" "Laptop lid is shut, attempting to disable it..."
if hyprctl keyword monitor "${laptop_mon},disable" >/dev/null; then
log "Laptop screen disabled"
else
log "ERROR" "Received an error when disabling the laptop screen in clamshell mode!"
fi
fi
;;
esac
}
handle-laptop-lid "${@}"

View File

@ -1,87 +0,0 @@
#!/usr/bin/env bash
log() {
local monitor="${1}"
local msg="${2}"
printf "%s: %s\n" "${monitor}" "${msg}"
}
monitor-dir() {
local event="${1}"
local directory="${2}"
local message_title="${3}"
inotifywait -m -e "${event}" --format '%w%f' "${directory}" | while read -r NEWFILE; do
notify-send "${message_title}" "${NEWFILE}" -a "Device Monitor"
done
}
monitor-ssid() {
local notify_title="Wifi State Changed"
local notify_app="Wifi State"
local previous_ssid=""
local ssid
while :; do
ssid="$(iwctl station wlan0 show | grep 'Connected network' | awk '{print $3}')"
ssid="$(printf "%s" "${ssid}" | xargs)"
if [[ "${ssid}" != "${previous_ssid}" ]]; then
if [[ -z "${ssid// /}" ]]; then
log "SSID" "Wifi Disconnected"
notify-send "${notify_title}" "Wifi Disconnected" -a "${notify_app}"
else
log "SSID" "Wifi Connected to ${ssid}"
notify-send "${notify_title}" "Wifi Connected to ${ssid}" -a "${notify_app}"
fi
previous_ssid="${ssid}"
fi
sleep 1
done
}
monitor-laptop-lid() {
local laptop_lid_state
local laptop_lid_last_state
while :; do
sleep 1
laptop_lid_state="$(</proc/acpi/button/lid/LID0/state)"
laptop_lid_state="${laptop_lid_state##* }"
laptop_lid_state="${laptop_lid_state^^}"
if [[ "${laptop_lid_state}" == "${laptop_lid_last_state}" ]]; then
continue
fi
case "${laptop_lid_state}" in
"OPEN")
if hyprctl monitors -j | jq -er '.[] | select(.name=="eDP-1") | .name' >/dev/null; then
printf "Laptop screen was opened, attempting to enable it...\n"
if hyprctl dispatch dpms on eDP-1; then
laptop_lid_last_state="${laptop_lid_state}"
local msg="Laptop screen successfully enabled"
log "Laptop Clamshell" "${msg}"
notify-send "Laptop Clamshell" "${msg}" -a "Laptop Clamshell"
else
local msg="Received an error when enabling the laptop screen!\n"
log "Laptop Clamshell" "${msg}"
notify-send "Laptop Clamshell Error" "${msg}" -a "Laptop Clamshell"
fi
fi
;;
"CLOSED")
if hyprctl monitors -j | jq -er '.[] | select(.name=="eDP-1") | .name' >/dev/null; then
printf "Laptop screen was shut, attempting to disable it...\n"
if hyprctl dispatch dpms off eDP-1; then
laptop_lid_last_state="${laptop_lid_state}"
local msg="Laptop screen successfully disabled"
log "Laptop Clamshell" "${msg}"
notify-send "Laptop Clamshell" "${msg}" -a "Laptop Clamshell"
else
local msg="Received an error when disabling the laptop screen in clamshell mode!\n"
log "Laptop Clamshell" "${msg}"
notify-send "Laptop Clamshell Error" "${msg}" -a "Laptop Clamshell"
fi
fi
;;
esac
done
}
monitor-ssid &
wait

View File

@ -0,0 +1,49 @@
#!/usr/bin/env bash
log() {
local title="Wifi State Changed"
local app="Wifi State"
local level="INFO"
if (($# > 1)); then
local level="${1}"
fi
local msg="${*}"
case "${level^^}" in
"TRACE")
printf "[TRACE] %s: %s\n" "$title" "$msg"
;;
"INFO")
printf "[INFO] %s: %s\n" "$title" "$msg"
notify-send "$title" "$msg" -a "$app"
;;
"ERROR")
printf "[ERROR] %s: %s\n" "$title" "$msg"
notify-send "$title" "$msg" -a "$app" -u "critical"
;;
*)
printf "INVALID LOG LEVEL PASSED!\n" >&2
return 1
;;
esac
printf "Laptop Clamshell: %s\n""$msg"
}
monitor-ssid() {
local previous_ssid=""
local ssid
while :; do
ssid="$(iwctl station wlan0 show | grep 'Connected network' | awk '{print $3}')"
ssid="$(printf "%s" "$ssid" | xargs)"
if [[ "$ssid" != "$previous_ssid" ]]; then
if [[ -z "${ssid// /}" ]]; then
log "Wifi Disconnected"
else
log "Wifi Connected to ${ssid}"
fi
previous_ssid="$ssid"
fi
sleep 1
done
}
monitor-ssid