Using Polybar with dwm
While I love dwm’s many features and the realistic option to change the source code to your needs, I was not as fond of the bar. It was simply lacking the customization options i was looking for, or would make simple features too complex to have. To solve this, I made Polybar work with dwm’s tags.
I have tried many window managers while using GNU/Linux. I liked all of them at first, but quickly found them to be limiting. After I became unhappy with Qtile, I was ready to switch again. Having heard of it often, I decided to try suckless’s dwm.
I quickly fell in love with it. Anything I wanted to do had already been done by someone else, I simply had to install the patch. Any errors I had were understandable and could be solved by simply looking at the relevant code.
The only thing I wanted to change, which I could not find information on, was using a different bar. I have to admit that I used to frequent r/unixporn and got into GNU/Linux for the looks. It was however through this, that I discovered all the benefits it brought. I still love a good looking system though and could not be charmed by dwm’s default look.
To solve this I decided to install polybar and spin up an instance. What this gives you however, is not satisfactory. The default bar will still be there, toggling the bar will obviously not be an option and most importantly: your tag information will be missing.
Step One: Replacing the default bar
You will first have to get dwm’s status bar out of the way to make space for polybar. Simply turning it off in config.h is not an option, as this will also remove the spacing dwm leaves for the bar. In some window managers this might mean game-over, but dwm allows us to simply edit the source code.
You will only need to remove all references to
barwin in dwm.c. Look for calls like this one:
Be sure to also remove all unnecessary code like in
updatebars(void), which is solely responsible for drawing the contents of the bar window.
Recompiling dwm should now result in a bar-free experience, while still preserving the space.
Step Two: Getting dwm to share its tag state
dwm does by default not provide enough information for any module to render some kind of tag status. You might try polybar’s xworkspaces module, but it will not work, as dwm does not share enough information. Even if it would however, xworkspaces is meant to display only one selected tag. This is meant for manual tilers like i3wm, but is not satisfactory for dwm because of its dynamic nature. Selecting multiple tags at once will only show one tag as active. To solve these problems we will simply make dwm share the current configuration and the state all tags are in.
For this we will store the information for each monitor in the X-server properties of the root window. This is where dwm would set such values anyway and the xprop utility makes it easy to read those values later.
To set the values for each monitor I use this function. It takes an input string and sets it as the state for the specified monitor.
For more information on this see the Xlib Programming Manual.
The string I set for the value is generated by the following code.
The state of each tag is represented by a single char. I fittingly hooked dwm’s original
drawbar function to execute this, after stripping it from it’s previous content.
With this, dwm’s state is now automatically kept updated.
Step Three: Polybar module
To display this information in polybar I use a customs script as a module.
As you can see, with custom modules polybar simply displays the last line echoed by the script. Reading the value for each tag is easy using xprop, and we can format the output to our needs.
Finally, to piece all of this together, we need to start polybar for each of our monitors.
To do this we use another script, which reads in all connected monitors and launches an instance of polybar for each of them.
What is notable is that we piece together the command needed to execute the tag-script and pass it on as an environment variable. We do this because we need to specify which monitor’s state the script should display. Simply passing on the number of the monitor will unfortunately not work, as polybar cannot concatenate config strings with environment variables.
The block that we need to include in our polybar config therefore looks like this:
With this, we now have a solution to use dwm with polybar. Admittedly this is hacky, and will break in some conditions relating to switching primary monitors, but works great for my daily usage.
If you have any questions feel free to contact me by email at firstname.lastname@example.org.
This was one of the first major customization projects I did on my machine using GNU/Linux. I can obviously see the shortcomings of this solution, but found that using GNU/Linux for me is about this type of bodging and hacking. It both makes you learn about your systems internals and enables you to use your computer as you wish, rather than being dependent on technology fitting your needs.
You may find that your unhappy with this solution, or want to improve certain parts. You may also despise the corruption of the suckless’ ideals related to what I did. But that is ultimately your choice and chance to find what fits your needs and make it.