xfce/polybar-panel.sh 4.0 K raw
1
#!/bin/sh
2
# Requires: xfconf-query (xfce4-settings), polybar
3
# Swaps xfce4-panel out of the XFCE session for polybar, and can put the
4
# panel back.
5
#
6
# xfce4-panel is not an autostart .desktop entry — xfce4-session launches it
7
# as one of its own session clients, so it has to be replaced there. This is
8
# a script rather than a checked-in xfconf XML file for the same reason as
9
# rofi-keybinds.sh: xfconf rewrites its config atomically, by rename, which
10
# would replace a symlink from this repo with a regular file.
11
#
12
# Usage: polybar-panel.sh            apply (idempotent, safe to re-run)
13
#        polybar-panel.sh --remove   put xfce4-panel back
14
#        polybar-panel.sh --list     show which session clients run what
15
#
16
# Applying also swaps the running session live; no logout needed.
17
set -u
18
19
CHANNEL=xfce4-session
20
BACKUP="${XDG_DATA_HOME:-$HOME/.local/share}/polybar-panel.backup"
21
22
# xfwm4's compositor shadows a dock as a rectangle the size of its window,
23
# ignoring what the window actually paints. polybar's bar spans the screen and
24
# is transparent everywhere except the islands, so that shadow shows up as a
25
# band hanging across the empty middle. Turning it off is the only knob for it;
26
# true is xfwm4's default, which --remove puts back.
27
dock_shadow() {
28
    command -v xfconf-query >/dev/null 2>&1 || return 0
29
    xfconf-query -c xfwm4 -p /general/show_dock_shadow -s "$1" 2>/dev/null ||
30
        xfconf-query -c xfwm4 -p /general/show_dock_shadow -n -t bool -s "$1"
31
}
32
33
# Where the polybar launcher lives. The repo layout is the default; point
34
# POLYBAR_LAUNCH somewhere else to use an installed copy instead.
35
LAUNCH=${POLYBAR_LAUNCH:-$(CDPATH= cd -- "$(dirname -- "$0")/../polybar" 2>/dev/null && pwd)/launch.sh}
36
[ -f "$LAUNCH" ] || LAUNCH="$HOME/.config/polybar/launch.sh"
37
[ -x "$LAUNCH" ] || { echo "polybar-panel: no executable launcher at $LAUNCH" >&2; exit 1; }
38
39
command -v xfconf-query >/dev/null 2>&1 ||
40
    { echo "polybar-panel: xfconf-query not found — is this an XFCE session?" >&2; exit 1; }
41
command -v polybar >/dev/null 2>&1 ||
42
    { echo "polybar-panel: polybar is not installed" >&2; exit 1; }
43
44
# Every session client command in every stored session, as "prop<TAB>[cmd,args]".
45
clients() {
46
    xfconf-query -c "$CHANNEL" -lv |
47
        sed -n 's|^\(/sessions/[^/]*/Client[0-9]*_Command\) *\(\[.*\]\)$|\1\t\2|p'
48
}
49
50
# Set an array property from a "[a,b,c]" literal.
51
set_array() {
52
    prop=$1
53
    args=""
54
    IFS=,
55
    for word in $(printf '%s' "$2" | sed 's/^\[//; s/\]$//'); do
56
        args="$args -t string -s $word"
57
    done
58
    unset IFS
59
    # shellcheck disable=SC2086
60
    xfconf-query -c "$CHANNEL" -p "$prop" $args --force-array
61
}
62
63
case "${1:-}" in
64
--list)
65
    clients | while IFS='	' read -r prop cmd; do
66
        printf '%-45s %s\n' "$prop" "$cmd"
67
    done
68
    ;;
69
--remove)
70
    [ -f "$BACKUP" ] || { echo "polybar-panel: nothing to restore ($BACKUP not found)" >&2; exit 1; }
71
    while IFS='	' read -r prop cmd; do
72
        [ -n "${prop:-}" ] || continue
73
        set_array "$prop" "$cmd"
74
        echo "restored $prop -> $cmd"
75
    done <"$BACKUP"
76
    rm -f "$BACKUP"
77
    dock_shadow true
78
    polybar-msg cmd quit >/dev/null 2>&1 || pkill -u "$(id -u)" -x polybar
79
    [ -n "${DISPLAY:-}" ] && command -v xfce4-panel >/dev/null 2>&1 &&
80
        (xfce4-panel >/dev/null 2>&1 &)
81
    ;;
82
"")
83
    found=0
84
    clients | while IFS='	' read -r prop cmd; do
85
        case "$cmd" in
86
        "[xfce4-panel"*)
87
            printf '%s\t%s\n' "$prop" "$cmd" >>"$BACKUP"
88
            set_array "$prop" "[$LAUNCH]"
89
            echo "$prop -> $LAUNCH"
90
            ;;
91
        esac
92
    done
93
    clients | grep -q "$LAUNCH" && found=1
94
    [ "$found" = 1 ] || { echo "polybar-panel: no xfce4-panel session client found" >&2; exit 1; }
95
    dock_shadow false
96
    if [ -n "${DISPLAY:-}" ]; then
97
        pgrep -u "$(id -u)" -x xfce4-panel >/dev/null 2>&1 &&
98
            xfce4-panel --quit >/dev/null 2>&1
99
        "$LAUNCH" >/dev/null 2>&1 &
100
        echo "polybar started; xfce4-panel stopped"
101
    fi
102
    ;;
103
*)
104
    echo "usage: polybar-panel.sh [--remove|--list]" >&2
105
    exit 1
106
    ;;
107
esac