Sxhkd output in Polybar

Hi guys, if some of you want to get the sxhkd output in polybar modules, you will have to first start sxhkd with the -s option to use a fifo and then grab infos from it.

mkfifo /run/user/1000/sxhkd.fifo

sxhkd -s /run/user/1000/sxhkd.fifo

Then create a listener script:

#!/bin/bash

# Listen on sxhkd events to:
# - indicate when in a chord chain,
# the last hotkey and the corresponding command.
# export XDG_RUNTIME_DIR="/run/user/1000/"
# export SXHKD_FIFO="$XDG_RUNTIME_DIR"/sxhkd.fifo

cmd="$XDG_RUNTIME_DIR/cmd.txt"
keys="$XDG_RUNTIME_DIR/keys.txt"

while read line <$SXHKD_FIFO
do
    if [[ "$line" == 'EEnd chain' ]]; then
        polybar-msg action "#sxhkd-ipc.hook.1" >/dev/null 2>&1
    elif [[ "$line" == 'BBegin chain' ]]; then
        polybar-msg action "#sxhkd-ipc.hook.0" >/dev/null 2>&1
    elif [[ "$line" =~ 'H' ]]; then
        echo "$line" > "$keys"
        polybar-msg action "#hotkeys-ipc.hook.1" >/dev/null 2>&1
    elif [[ "$line" =~ 'C' ]]; then
        echo "$line" > "$cmd"
        polybar-msg action "#command-ipc.hook.1" >/dev/null 2>&1
    fi
done

Ok now another one for sxhkd: (sxhkd-start)

#!/bin/bash

declare -a proc=( "sxhkd" "sxhkd-listener" )

# Terminate existing sxhkd scripts instances
if [[ -n $(pidof sxhkd) ]]; then
    for i in "${proc[@]}"; do
        kill -15 "$(ps -ef \
        | grep "$i" \
        | awk 'NR==1 {print $2}')" >/dev/null 2>&1
        # Wait until the processes has been shut down
        while pidof -x "$i" >/dev/null; do
            sleep 0.2
            break
        done
    done
fi

[[ -e $SXHKD_FIFO ]] && rm -f "$SXHKD_FIFO"
sleep 0.5

if [[ -n $(pidof bspwm) ]]; then
    # Launch sxhkd, with the -s option for the listener script.
    mkfifo "$SXHKD_FIFO"
    sxhkd -t 5 \
    -s "$SXHKD_FIFO" \
    -c "$XDG_CONFIG_HOME"/sxhkd/sxhkdrc &

    sxhkd-listener &
    notify-send 'hotkeys daemon ready'
else
    exit 127
fi

exit

In your polybar config.ini: enable-ipc = true
Now the polybar modules:

[module/sxhkd-ipc]
; Indicate if sxhkd has began a chain and serve as a launcher.
type = custom/ipc
format = <output>
format-padding = 1
hook-0 = echo "  %{F#FF4747}%{F-}"
hook-1 = echo "  %{F#A6E074}%{F-}"
initial = 2
click-left = sxhkd-start &

[module/hotkeys-ipc]
type = custom/ipc
format = <output>
format-padding = 1
hook-0 = echo ""
hook-1 = echo "  %{F#FF4747} $(cat "$XDG_RUNTIME_DIR"/keys.txt | sed 's/H//')%{F-}"
initial = 1

[module/command-ipc]
type = custom/ipc
format = <output>
format-padding = 1
hook-0 = echo ""
hook-1 = echo "  %{F#A6E074} $(cat "$XDG_RUNTIME_DIR"/cmd.txt | sed 's/^[[a-z]]//;s/C//')%{F-}"
initial = 1

The first symbol indicates if you’re in a chord chain, then the keybord shortcut and finaly the corresponding command.

Hope you like it! :wink:

6 Likes