aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/Logout.sh
blob: c9d1b274cd74358e5f09d61c19d9923710ab1de1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Logout helper for wlogout and keybind callers.
LOG_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/hypr"
LOG_FILE="${LOG_DIR}/hypr-logout.log"
if ! mkdir -p "$LOG_DIR" >/dev/null 2>&1; then
    LOG_FILE="${XDG_RUNTIME_DIR:-/tmp}/hypr-logout.log"
fi

log_msg() {
    printf "[%s] %s\n" "$(date +"%F %T")" "$1" >>"$LOG_FILE"
}

run_logged() {
    local label="$1"
    shift
    log_msg "RUN ${label}: $*"
    "$@" >>"$LOG_FILE" 2>&1
    local rc=$?
    log_msg "RC ${label}: ${rc}"
    return "$rc"
}
SESSION_USER="${USER:-$(id -un)}"
SESSION_HYPRLAND_PIDS="$(pgrep -xu "$SESSION_USER" -x Hyprland || true)"
SESSION_DM_SERVICE=""
IS_SDDM_SESSION=0

session_hyprland_running() {
    local pid
    if [ -n "$SESSION_HYPRLAND_PIDS" ]; then
        for pid in $SESSION_HYPRLAND_PIDS; do
            if kill -0 "$pid" >/dev/null 2>&1; then
                return 0
            fi
        done
        return 1
    fi
    pgrep -xu "$SESSION_USER" -x Hyprland >/dev/null 2>&1
}
logout_completed() {
    # Give the session up to 2 seconds to terminate after a successful command.
    for _ in {1..20}; do
        session_hyprland_running || return 0
        sleep 0.1
    done
    return 1
}
stop_proc() {
    local name="$1"
    pkill -u "$SESSION_USER" -x -TERM "$name" >/dev/null 2>&1 || true

    # Wait up to 1 second for graceful shutdown.
    for _ in {1..10}; do
        pgrep -xu "$SESSION_USER" -x "$name" >/dev/null 2>&1 || return 0
        sleep 0.1
    done
    pkill -u "$SESSION_USER" -x -KILL "$name" >/dev/null 2>&1 || true
}

ensure_hyprland_env() {
    local runtime_dir="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
    if [ -z "${WAYLAND_DISPLAY:-}" ] || [ ! -S "$runtime_dir/$WAYLAND_DISPLAY" ]; then
        for socket in "$runtime_dir"/wayland-[0-9]*; do
            [ -S "$socket" ] || continue
            case "$(basename "$socket")" in
                *awww*) continue ;;
            esac
            export WAYLAND_DISPLAY="$(basename "$socket")"
            break
        done
    fi

    if [ -z "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]; then
        for sig_dir in "$runtime_dir"/hypr/*/; do
            [ -S "${sig_dir}.socket.sock" ] || continue
            export HYPRLAND_INSTANCE_SIGNATURE="$(basename "$sig_dir")"
            break
        done
    fi
}

ensure_hyprland_env

# Close wlogout if it is still visible.
stop_proc "wlogout"
HYPRCTL_BIN="$(command -v hyprctl || true)"
HYPRSHUTDOWN_BIN="$(command -v hyprshutdown || true)"
UWSM_BIN="$(command -v uwsm || true)"
LOGINCTL_BIN="$(command -v loginctl || true)"
if [ -n "$LOGINCTL_BIN" ] && [ -n "${XDG_SESSION_ID:-}" ]; then
    SESSION_DM_SERVICE="$("$LOGINCTL_BIN" show-session "$XDG_SESSION_ID" -p Service --value 2>/dev/null || true)"
    if [ "$SESSION_DM_SERVICE" = "sddm" ] || [ "$SESSION_DM_SERVICE" = "sddm-autologin" ]; then
        IS_SDDM_SESSION=1
    fi
fi

# Primary exit path: tell Hyprland to exit cleanly
if [ -n "$HYPRCTL_BIN" ]; then
    if run_logged "hyprctl-dispatch-exit" "$HYPRCTL_BIN" dispatch exit; then
        if logout_completed; then
            exit 0
        fi
        log_msg "hyprctl dispatch exit returned but Hyprland is still running"
    fi

    if run_logged "hyprctl-dispatch-lua-exit" "$HYPRCTL_BIN" dispatch 'hl.dsp.exit()'; then
        if logout_completed; then
            exit 0
        fi
        log_msg "hyprctl dispatch 'hl.dsp.exit()' returned but Hyprland is still running"
    fi

    if run_logged "hyprctl-dispatch-exit-1" "$HYPRCTL_BIN" dispatch exit 1; then
        if logout_completed; then
            exit 0
        fi
        log_msg "hyprctl dispatch exit 1 returned but Hyprland is still running"
    fi

    if run_logged "hyprctl-dispatch-exit-0" "$HYPRCTL_BIN" dispatch exit 0; then
        if logout_completed; then
            exit 0
        fi
        log_msg "hyprctl dispatch exit 0 returned but Hyprland is still running"
    fi

    if run_logged "hyprctl-dispatch-exit-x" "$HYPRCTL_BIN" dispatch exit x; then
        if logout_completed; then
            exit 0
        fi
        log_msg "hyprctl dispatch exit x returned but Hyprland is still running"
    fi
fi

# Secondary path: hyprshutdown with kill timeout if hyprctl dispatch did not exit
if [ -n "$HYPRSHUTDOWN_BIN" ]; then
    if [ -n "$TIMEOUT_BIN" ]; then
        SHUTDOWN_CMD=("$TIMEOUT_BIN" "-k" "1s" "2s" "$HYPRSHUTDOWN_BIN" "--no-fork")
    else
        SHUTDOWN_CMD=("$HYPRSHUTDOWN_BIN" "--no-fork")
    fi
    if run_logged "hyprshutdown-no-fork" "${SHUTDOWN_CMD[@]}"; then
        if logout_completed; then
            exit 0
        fi
        log_msg "hyprshutdown returned success but Hyprland is still running"
    fi
fi

# systemd session fallback.
if [ -n "$LOGINCTL_BIN" ] && [ -n "${XDG_SESSION_ID:-}" ]; then
    if [ "$IS_SDDM_SESSION" -eq 1 ]; then
        log_msg "Skipping loginctl terminate-session for SDDM-managed session (${SESSION_DM_SERVICE})"
    elif run_logged "loginctl-terminate-session" "$LOGINCTL_BIN" terminate-session "$XDG_SESSION_ID"; then
        if logout_completed; then
            exit 0
        fi
        log_msg "loginctl terminate-session returned success but Hyprland is still running"
    fi
fi

# Fallback: ask Hyprland to spawn hyprshutdown via a normal dispatch exec call.
if [ -n "$HYPRCTL_BIN" ] && [ -n "$HYPRSHUTDOWN_BIN" ]; then
    if run_logged \
        "hyprctl-dispatch-exec-hyprshutdown" \
        "$HYPRCTL_BIN" dispatch exec "hyprshutdown --no-fork"; then
        if logout_completed; then
            exit 0
        fi
        log_msg "hyprctl dispatched hyprshutdown but Hyprland is still running"
    fi
fi

# UWSM-managed session fallback (common on NixOS).
if [ -n "$UWSM_BIN" ] && [ "$IS_SDDM_SESSION" -eq 0 ]; then
    if run_logged "uwsm-stop" "$UWSM_BIN" stop; then
        if logout_completed; then
            exit 0
        fi
        log_msg "uwsm stop returned success but Hyprland is still running"
    fi
elif [ -n "$UWSM_BIN" ] && [ "$IS_SDDM_SESSION" -eq 1 ]; then
    log_msg "Skipping uwsm stop on SDDM-managed session to avoid delayed logout"
fi

# Final process-level fallback.
if run_logged "pkill-hyprland-term" pkill -u "$SESSION_USER" -x -TERM Hyprland; then
    if logout_completed; then
        exit 0
    fi
    log_msg "SIGTERM sent to Hyprland but process is still running"
fi
if run_logged "pkill-hyprland-kill" pkill -u "$SESSION_USER" -x -KILL Hyprland; then
    exit 0
fi

log_msg "Logout failed: no method succeeded"

exit 1
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage