I really got inspired to do some silly coding solely for practice and education, thanks to this wonderful tutorial.
Since I am a cool guy an use arch btw, I am going to completely ignore everything Windows related from there and do the equivalent in X11. You can always track my progress at code.htmlbyhand.online.
First of all, I created a project folder, similar to how Casey does it in the video:
mkdir ~/Code/playground/
cd ~/Code/playground/
mkdir ./code ./build
cd ./code
Now I need to get any idea what the hell X11 is and how do I use its header files. I quickly find libX11.html and download it as pdf for easy access and reference.
There I discover I basically only need to include Xlib.h everywhere
#include <X11/Xlib.h>
Then we need to get an active display (I have no additional displays, so easy for me), create a root window on that display and draw it.
#include <stdio.h>
#include <unistd.h>
#include <X11/Xlib.h>
int main()
{
Display *display = XOpenDisplay(NULL);
Window main_window = XDefaultRootWindow(display);
Window window = XCreateSimpleWindow(display, main_window, 0, 0, 250, 250, 1, 0, 0x00aade87);
XMapWindow(display, window);
XFlush(display);
for(;;) { sleep(1); }
return 0;
}
The code is still quite easy and reads itself! XOpenDisplay requests a display structure from the X server, and since I got no preference, I can pass NULL to it so X11 uses defaults. XDefaultRootWindow creates a root window connected to the display X server gives us (my only display). Then I create an actual displayable window-son of the main-window-dad by calling XCreateSimpleWindow. It receives a display pointer, a root window pointer, x, y, width and height and some other misc parameters you can read about somewhere. Then, XMapWindow is basically “display my window” and XFlush is sending all requests making sure everything we want from X11 is forced. Some help is taken directly from hereket.
Honestly, this is it! Now I need to optimize the build-run pipeline. Of course, I will just do whatever Casey did in the video but specific for my local environment.
gcc main.c -g -o ../build/main -lX11
This is ./build (never forget chmod u+x to make an executable).
./../build/main
And this is ./run, also chmod’ed!
Those will allow me build and run my code quickly from vim itself by :! ./build and :!./run
This is it! We are so ready!! Behold, the silliest window in the world:
