.dotfiles/.config/hypr/handler.sh

69 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env nix-shell
#!nix-shell -i sh -p socat jq
# Config
ws_media="[08]"
ws_chat="[09]"
ws_media_at="[ 0, 0 ]"
# Debug code
debug_enabled="false"
if [ "$1" == "--debug" ]; then
debug_enabled="true"
fi
debug() {
if [ "$debug_enabled" == "true" ]; then
echo "$1" >&2
fi
}
# Main code
all_clients="[]"
_handler_update_all_clients() {
all_clients=$(hyprctl clients -j)
}
_handler_check_ws_media_empty() {
# split at ">>" and get the second part and take first part of that
event=$(echo "$1" | cut -d'>' -f1)
win_id=$(echo "$1" | cut -d'>' -f3 | cut -d',' -f1)
win_client=$(echo "$all_clients" | jq ".[] | select(.address == \"0x$win_id\")")
if [ "$event" == "closewindow" ]; then
event_on_media="$(echo "$win_client" | jq ".workspace.name == \"$ws_media\"")"
fi
if [ "$event" == "movewindow" ]; then
event_on_media="$(echo "$win_client" | jq ".at == $ws_media_at")"
fi
has_zero_windows=$(hyprctl workspaces -j | jq ".[] | select(.name == \"$ws_media\") | .windows == 0")
debug "event: $event"
debug "win_id: $win_id"
debug "client: $win_client"
debug "event_on_media: $event_on_media"
debug "has_zero_windows: $has_zero_windows"
if [ "$has_zero_windows" == "true" ] && [ "$event_on_media" == "true" ]; then
hyprctl dispatch workspace "$ws_chat"
fi
}
handle() {
debug "----- $1 -----"
case $1 in
openwindow*) _handler_update_all_clients "$1" ;;
movewindow*) _handler_check_ws_media_empty "$1" ;;
closewindow*) _handler_check_ws_media_empty "$1" ;;
esac
}
socat -U - "UNIX-CONNECT:/tmp/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock" | while read -r line; do handle "$line"; done