How do I set dwm's status bar

2022-08-09T14:54:02Z

I like to keep under the eyes a few information when I work : the track played by mpd and the time.

I used a simple script calling xsetroot in a while loop :

while true; do
	mpd_track=$(mpc current)
	time=$(date "+%F  %H:%M")
	xsetroot -name "$mpd_track | $time"
	sleep 1
done

However, calling mpc every second seemed useless.

Worse : when I switch to another song, the display wasn't updated after 1 second.

No big deal, indeed. I used to display the volume too, and when changing volume while scrolling on the bar, there was an annoying delay before the right information was shown.

Recently I read about signals, and it seemed perfect. Now the script to set my dwm status bar waits for USR1 signals to call xsetroot :

set_status()
{
	xsetroot -name " $(status) "
}
trap set_status USR1

To make sure the mpd song is displayed directly, I have this loop : (damn I love mpc --wait)

(while true; do mpc --wait current >/dev/null; reload; done) &

And at last, I call a stupid loop doing nothing except waiting for signals :

while true; do sleep .1; done

The complete script :

#!/bin/sh
# set dwm's status when USR1 signal is received
#	pkill -USR1 statusloop
#	kill -USR1 $(cat /tmp/statusloop.lock)
LOCKFILE=/tmp/statusloop.lock
set_status()
{
	xsetroot -name " $(status) "
}
reload()
{
	kill -USR1 $(cat ${LOCKFILE})
}
if [ -e ${LOCKFILE} ]; then
	echo "already running, killing"
	kill -9 $(cat ${LOCKFILE})
fi
trap set_status USR1
# make sure the lockfile is removed when we exit
trap "rm -f ${LOCKFILE}; exit" INT QUIT TERM
echo $$ > ${LOCKFILE}
set_status
# update clock every minutes
(while true; do sleep 60; reload; done) &
# mpd changes
(while true; do mpc --wait current >/dev/null; reload; done) &
# global loop
while true; do sleep .1; done