X11 app in C
I’ve been using dwm for a while now and once I got dwmblocks setup I’ve been making some little scripts to do various tasks you’d expect to do with a typical status bar in a full desktop environment. For instance clicking the volume display and changing it. With dwmblocks you can actually key on left click, right click, middle click and scrolling up and down so for most of my scripts I just key on those, like for volume and brightness I just use the scroll wheel to change the value. I’ve been doing fine with these simple functions but one thing I do actually like using is just being able to look at a calendar. On my work computer, a mac, I can click the date and have a little window pop down just showing the date. So my goal was to write something similar that I could integrate into my status bar.
First attempt
First thing that came to mind was cal. This is a linux util command that when run without arguments spits out a month calendar with today’s date highlighted.
Like this:
Great that’s what I want but I want it in a little window that I can look at then tell it to go away. A good way is to use a notification, I do it in my scripts all the time. I could just pipe cal into a notification and that should do the job.
notify-send "$(cal)"

Good start but I’ve had mixed results for one thing sometimes the columns don’t line up right.
Making a project more complicated for educational purposes
I’ve wanted to learn more C anyways so I thought this was the perfect project to do that since it’s actually useful to me. So how the hell do I make a gui box from C? Well in the linux world I know of two frameworks for this Qt and GTK. I looked into these briefly but they honestly seemed a bit big for what I’m trying to accomplish here. These are just wrappers around X11 anyway so why not just use the X11 interface. The C library is called Xlib and it’s what dwm uses so I was able to reference some things in it. There’s actually a lot to getting a window open with X11 so I started with this simple ‘hello world’ I found here.
So with this I was able to add in some code to run cal and draw it’s contents onto the window. I learned some lessons about memory management in C with this task and need to use gdb to track it down. So I got exactly what I originally wanted, a program that shows the current month. This is good but a few features will make it fully functional.
Features
For this I took the code that dwm uses to have keybinds defined in a config.h file and spliced that into my code so I can configure and change keybinds easily. I added the ability to move between months with h and l. H and L move a whole year (doubt that’d be useful but I put it in anyway). I also added a keybind to quit q. I can always close with the dwm command but having another way isn’t a bad thing.
I’d like to add some sort of highlight on the current day of the month like cal does when you run it from the command line but that can wait for another time.
Conclusion
It’s not perfect but I’m really happy with the results. I learned a TON from doing this and I got a useful program that I use daily. That’s the best I could have hoped for. The code is hosted on my github and it’s free and open source software.
Tags: tech