Getting screenshotting to work in Dwm: an interesting experience

A few days ago I decided to finally fix screenshotting in dwm. After about an hour I finally got it working, sort of. #100DaysToOffload

a picture of a git commit

Yesterday I had a lot of time on my hands so I decided I should fix one of the things I've not gotten working on dwm yet: screenshotting.

If you ever have used a tilling window manger before, most people like to use the utility scrot to screenshot your desktop. Since it works in the terminal, you can just bind the command to a keybinding like mod + PrtSc.

In dwm I was going to make a binding for scrot. To make a binding for a command in dwm you have to do these two things:

Make a varriable of the command in the /* commands */ section. "Make", "sure", "every", "argument", "is", "like", "this".

Here is an example of a command:

static const char *termcmd[] = { "tabbed", "-r", "2", "st", "-w", "''",  NULL };

Then, after that you would make it a key in the static Key keys[] = { section. here is an example:

      { MODKEY|ShiftMask,                           XK_Return,  spawn,          {.v = termcmd} },

So if you press mod + shift + return it will run the termcmd.

So all I had to do was do all that but with my scrot command. Sounds easy, right? Wrong.

When I typed in my scrot command, I noticed that the %b in my scrot command was highlited differently. And when I pressed PrtSc scrot did not take a picture.

After a bit of digging I thought about how in C, the language Dwm is configured in, %b is an escape character. So Dwm will interpret the %b part as an escape character, thus breaking it.

Workaround: use an external script

I decided that I should make a script that runs the command. and link it as a keybinding. That way dwm can justrun the scriptwich will run scrot without any problems.

This is what I put into the config:

{ 0,                               XK_Print, spawn,               SHCMD("~/dwm-config/screenshot.sh") },

This is argueably the worst workaround I have done. Notice that it links to inside of the ~/dwm-config directory. If ayone were to have the config stuff in a different place, screenshotting will magically stop working.

How will I solve it?

To be honest I probably won't work on it anytime soon because I'm just really anoyed by it. There are two ways to have it work in the longterm:

  • find out if there is a way to right %b without it messing anything up,
  • Document it well enought that people know about thisproblem and hope for the best.

If anyone wants to help me or knows the answer to my problem, please DM me on mastodon.