Zed Lopez

Ratpoison Titlechanged Hook

A tiny patch I wrote was just committed to the ratpoison git repository. It adds a hook called “titlechanged” that allows you to configure actions to be taken when the current window’s title is changed.

My patch included a one-line description of the hook in the manual. It’s accurate enough, but by itself it remains one of those things that would leave you thinking “OK, so I can do this, but why would I want to?”

So this entry is my explanation of why you might want to.

One of the points of ratpoison is that it just draws windows, with no titlebar or other window decoration (save for an optional border.) Most of the time, I find this to be a feature — the titlebar rarely adds anything useful, so it’s usually wasted space. But I sometimes found it to be a bug when using a web browser, when the window title is whatever’s the contents of the current tab’s title tag, info that isn’t necessarily duplicated elsewhere and occasionally has value.

At some point, I toyed with a greasemonkey script that would reproduce the title in the body of the html in some distinctive style, but it was a pain. It took a while, but eventually I struck upon my current course and realized just how easy it would be to add the necessary support to ratpoison. (I programmed in C for five years in my first job out of college, but that was a long time ago, and I wouldn’t claim to be a C programmer today. But the ratpoison source is so clean and clear, the patch was straightforward.) And it’s a relief to know I’ll never want to cobble together an application-specific workaround again.

How I’m using it relies on inotail and dzen (compiled from source for xft support, which the Ubuntu Lucid package doesn’t have.)

In my .xsession, I have:

[ -f /tmp/left ] && rm /tmp/left
[ -f /tmp/right ] && rm /tmp/right
touch /tmp/left
touch /tmp/right
killall dzen2
inotail -f /tmp/left|dzen2 -x 0 -w 400 -ta l -fg “#1F1F1F” -bg “#CFCFD7” -fn “Consolas-11” &
inotail -f /tmp/right|dzen2 -x 400 -w 1280 -ta r -fg “#1F1F1F” -bg “#CFCFD7” -fn “Consolas-11” &
/usr/local/bin/status.pl &
ratpoison

In my .ratpoisonrc, I have:

set padding 0 16 0 0
addhook switchwin exec ratpoison -c “info %t” > /tmp/right
addhook switchframe exec ratpoison -c “info %t” > /tmp/right
addhook deletewindow exec ratpoison -c “info %t” > /tmp/right
addhook titlechanged exec ratpoison -c “info %t” > /tmp/right

“set padding” leaves 16 pixels at the top of the screen for the dzen2 bar, suitable for 1 line of Consolas-11. The hooks update /tmp/right whenever a potentially title-changing event occurs. (I was tempted to fire the titlechanged hooks in ratpoison as a side effect of the other hooks, but no other hook in ratpoison chains in that fashion, and I chose not to.)

Meanwhile, /usr/local/bin/status.pl writes to /tmp/left to update the left 400 pixels with the time and current CPU temp.

#!/usr/bin/perl
my $font=“Consolas-11”;
my $fg=“#1F1F1F”;
my $bg=“#CFCFD7”;

while (1) {
open(KID_TO_WRITE, “>/tmp/left”) or die “couldn’t open /tmp/left: $!”;
my ($sec, $min, $hour) = localtime(time);
my $time = sprintf “%02d:%02d”, $hour, $min;

my @sensors = split "\

“, `sensors`;
my ($systemp, $core0, $core1);
for (@sensors) {
/^Core 0/ && do { /(\d+\.\d+)/; $core0 = sprintf “%2.0f”, $1; };
/^Core 1/ && do { /(\d+\.\d+)/; $core1 = sprintf “%2.0f”, $1; };
}
print KID_TO_WRITE "$time ", (join “/”, $core0, $core1), "\
“;
sleep 15;
close KID_TO_WRITE;
}

I’m not using dual monitors at the moment. This setup would need some revision for that case; I’d probably want different dzen bars per screen (and I’d assign something to a switchscreen hook in ratpoison.) And the static width of the dzen bars wouldn’t do the right thing with screen rotation, something I’ll address at some point.