dotfiles/bin/hyprsecscreen
2023-11-10 19:07:43 +01:00

126 lines
2.6 KiB
Bash
Executable file

#!/bin/bash
colored_star_message() {
local color="$1"
shift
printf '\033[1;'"${color}"'m*\033[0m \033[1m%s\033[0m\n' "$*"
}
ko() {
>&2 colored_star_message 31 "$*"
}
ok() {
colored_star_message 32 "$*"
}
get_vscreen_name() {
hyprctl monitors | awk '($1 == "Monitor") { print $2 }' | grep '^HEADLESS-[0-9]*$'
}
vscreen_exists() {
[ -n "$(get_vscreen_name)" ]
}
vnc_running() {
wayvncctl output-list > /dev/null 2>&1
}
get_mainmon_refrate() {
hyprctl monitors | grep -A 1 '^Monitor .* (ID 0):$' | sed 1d | xargs | sed 's/^.*@//g' | cut -d ' ' -f 1
}
create() {
# si il n'y a pas déjà d'écran
# créer un écran virtuel
# configurer l'écran
# donner le nom de l'écran
# si il n'y a pas de serveur VNC
# screener un serveur VNC
local \
vscreen_name \
resolution=1920x1080 \
position=0x0 \
listen_address=127.0.0.1
set -e
if ! vscreen_exists; then
hyprctl output create headless
vscreen_name="$(get_vscreen_name)"
printf '%s \033[1m%s\033[0m\n' "New virtual screen:" "$vscreen_name" &
hyprctl keyword monitor \
"${vscreen_name}," \
"${resolution}@$(get_mainmon_refrate)," \
"${position}," \
1
else
vscreen_name="$(get_vscreen_name)"
fi
if ! vnc_running; then
screen -S "Second screen (${vscreen_name})" -d -m \
wayvnc \
-v \
-o "$vscreen_name" \
"$listen_address"
fi
}
destroy() {
# si il y a un serveur VNC
# tuer le serveur VNC
# si il y a un écran virtuel
# supprimer l'écran virtuel
if vnc_running; then
wayvncctl wayvnc-exit
fi
if vscreen_exists; then
hyprctl output remove "$(get_vscreen_name)"
fi
}
status() {
# si il y a un serveur VNC
# afficher que le serveur VNC est en cours
# si il y a un écran virtuel
# afficher le nom de l'écran virtuel
local exit_status=0
if vnc_running; then
ok "The VNC server is running."
else
ko "The VNC server is not running."
exit_status="$(("$exit_status" + 1))"
fi
if vscreen_exists; then
ok "Monitor name: $(get_vscreen_name)"
else
ko "The virtual monitor does not exist!"
exit_status="$(("$exit_status" + 1))"
fi
return "$exit_status"
}
if [ -n "$1" ]; then
verb="$1"
shift
fi
case "$verb" in
create|destroy|status)
"$verb"
;;
"")
status
;;
*)
ko "skill issue"
exit 3
;;
esac