Xlib taskbar in D

Posted 2020-08-17

Continuing my "X in D" series, here I'll talk about my custom taskbar in D with xlib.

Core D Development Statistics

In the community

Community announcements

See more at the announce forum.

Taskbar in D

Here's the code: https://github.com/adamdruppe/taskbar

Be advised that the paths in the makefile assume my computer, but it is simple to change if you want to compile it. Moreover, other constants in the code are specialized for me - the colors, position, etc., so if you actually want to use this, don't be afraid to edit things!

Anyway, I've been using this myself for about 13 years now as part of my custom Linux user environment (together with my 100% custom terminal emulator, my hacked version of the Blackbox window manager - which I wouldn't mind porting to D some day - and many other things). I wanted something that was easy for me to customize, shows my windows with their icons, and lets me quickly interact with them.

It started in C

As the README file says, it started out as a fork of a little program in C that I got off the internet. Then, I did some pretty heavy modifications to make it fit what I wanted, and eventually ported it over to D and added more features like a notification area/systray (along with simpledisplay.d speaking the other side of the protocol), more icon image support, and slight tweaks to the ui like being able to reorder/pin windows with a middle click.

You can see this heritage and evolution in the code: the code still looks like C. And I consider that one of D's strengths: porting code from C to D is reasonably easy and your existing knowledge carries on, but then you can start adding in more and more D parts as you like. (And you don't need -betterC for this btw, but it is there if you want it.)

I import simpledisplay.d here but don't use a lot of its classes; I'm using it mostly because it is a nice self-contained set of translated headers for Xlib. While more is available, since it was already written in C, I kept writing with a C style here.

D can access the whole system

In X, there's some special programs that aren't really that special, like taskbars and window managers. They are special in that they serve a particular role to the user, but they aren't really that special because they just call the same APIs and speak similar protocols to every other program on the system.

The taskbar here wants to get a list of open windows and monitor changes in their state, and it does this through the same low-level Xlib calls as programs call to set this.

And the D language is with you all the way. It doesn't always help - you need to bring in function prototypes and struct definitions yourself*, but this is generally easy - you can see one at the bottom of taskbar.d to bring in another function. But this is why I imported simpledisplay - it already translated a good chunk of the X headers.

But even when D doesn't help directly, it never gets in the way. You can access these functions easily enough and then build whatever you want on top of it, and this ported taskbar is a nice example to prove it.

* there are projects to automatically translate headers from C but you still need to at least run those.