flak rss random

logging the foreground process in X11

Inspired by a similar New Old Thing post, Logging the foreground process as it changes, I thought I’d try my hand at a similar tool for X11. (No doubt such a thing already exists, didn’t look.) I’m not interested in the process that’s running on the CPU, which is closer to top’s job, but the name of the window I’m looking at.

The basic structure is pretty similar to the Windows sample program. We ask the X11 server for focus change events and print them out. We also have to ask for window creation events so we can track new windows as they show up. Here’s the source: windowlistener.c

A few other things to note. I spend most of my time in an xterm, so just knowing I switched between three different xterms doesn’t tell me much. Fortunately, it’s pretty easy to give each xterm a title that changes to reflect the current directory or file being edited. OpenBSD’s sample .kshrc file (/etc/ksh.kshrc) sets up aliases for common commands like cd and ssh that reset the xterm title. vim can do the same by adding set title to .vimrc, and then you’ll want to create an alias that resets the title after exiting vim.

Exercises: Add timestamps. Add a timeout and poll the current window title, so that title changes without focus changes (editing a new file) are detected.

Posted 13 Dec 2013 06:32 by tedu Updated: 24 Dec 2023 17:16
Tagged: c programming x11