aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/WaybarStartup.sh
blob: ac2696000f0e5851b20c49b1d96d220401f0e38a (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
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Dedicated startup helper for Waybar.
# Handles both systemd user service setups and direct Waybar launch.

runtime_dir="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
export XDG_RUNTIME_DIR="$runtime_dir"

is_waybar_running() {
    pgrep -x "waybar" >/dev/null 2>&1 || pgrep -x '\.waybar-wrapped' >/dev/null 2>&1
}

wait_for_wayland() {
    # If WAYLAND_DISPLAY is already valid, use it.
    if [ -n "${WAYLAND_DISPLAY:-}" ] && [ -S "$runtime_dir/$WAYLAND_DISPLAY" ]; then
        return 0
    fi

    # Otherwise wait briefly for an available Wayland socket.
    for _ in $(seq 1 120); do
        if [ -n "${WAYLAND_DISPLAY:-}" ] && [ -S "$runtime_dir/$WAYLAND_DISPLAY" ]; then
            return 0
        fi

        for socket in "$runtime_dir"/wayland-[0-9]*; do
            [ -S "$socket" ] || continue
            case "$(basename "$socket")" in
                *awww*) continue ;;
            esac
            export WAYLAND_DISPLAY="$(basename "$socket")"
            return 0
        done
        sleep 0.1
    done

    return 1
}

start_waybar_direct() {
    if command -v waybar >/dev/null 2>&1; then
        waybar >/dev/null 2>&1 &
        return 0
    fi

    if command -v .waybar-wrapped >/dev/null 2>&1; then
        .waybar-wrapped >/dev/null 2>&1 &
        return 0
    fi

    return 1
}

start_waybar_via_systemd() {
    [ -x "$(command -v systemctl)" ] || return 1

    local load_state
    load_state="$(systemctl --user show waybar.service --property=LoadState --value 2>/dev/null || true)"
    [ -n "$load_state" ] && [ "$load_state" != "not-found" ] || return 1

    systemctl --user start waybar.service >/dev/null 2>&1 || return 1
    sleep 0.4
    is_waybar_running
}

main() {
    # Allow key startup services to settle before launching Waybar.
    sleep 1
    wait_for_wayland || true

    is_waybar_running && exit 0

    if start_waybar_via_systemd; then
        exit 0
    fi

    if is_waybar_running; then
        exit 0
    fi

    start_waybar_direct || exit 1
}

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