The user wants me to build a minimalistic window manager for X11 in C, likely meaning they need code along with an explanation. I need to keep it concise but useful, with an oververbose level of 2. I'll have to manage events like MapRequest and ConfigureRequest, while also implementing a simple event loop using Xlib. The ultimate goal is to allow basic window manipulation, either floating or tiled. I’ll provide code comments for clarity without being overly complex.Planning window manager functionality
I need to make sure I handle grabbing SubstructureRedirectMask because if another window manager is running, that could lead to failure. Maybe I should handle ICCCM and set WM_DELETE_WINDOW, though it’s not strictly necessary for a minimal WM. I plan to use XOpenDisplay to connect and obtain the root window, then use XSelectInput to select the necessary events. For managing windows, I'll create a struct for window attributes, but I may not need it strictly—it depends on how I handle moving and resizing.Designing window management behavior
To handle ConfigureRequest, I have a couple of options: either let windows configure themselves by calling XConfigureWindow with the appropriate parameters, or I can approve the requested configurations. We should also consider managing existing windows at startup; many WMs use XQueryTree for this, but for simplicity, I could choose to manage only new windows.
For a minimalistic floating window manager, I'd like to include keybindings like Alt+Shift+Q to quit, drag-movement with Alt + Button1, resizing with Alt + Button3, and a click-to-focus method with no decorations or tiling.Implementing the event loop
I need to create an event loop that manages several events. For MapRequest, I’ll call XMapWindow, set borders, and perhaps raise the window while using XSetInputFocus.
For ConfigureRequest, I just need to call XConfigureWindow. When handling ButtonPress, I must check if the event belongs to a client window, and if so, set it as active, storing initial pointer and geometry values while grabbing the pointer.
During MotionNotify, if the pointer is grabbed, I’ll adjust the window’s position and size. Upon ButtonRelease, I’ll ungrab the pointer and stop any move or resize actions. Handling DestroyNotify may require maintaining a client list or not, and I’ll also want to consider handling EnterNotify but could skip that for simplicity. Lastly, I should manage WM replacements by catching specific error notifications.Creating the minimal window manager
I'm designing a minimal window manager and need to ensure we're avoiding plagiarism from the known "tinywm.c" example. I’ll implement features like click-to-focus, Alt+Button1 for moving windows, and Alt+Button3 for resizing. My compile command will be gcc -std=c99 -Wall -Wextra -o tinywm tinywm.c -lX11, and I should include a disclaimer about X11 programming.
I will also manage events such as button presses and motion, set up error handling for XSelectInput, and provide a full code example with brief explanations of each feature I implement. Lastly, I’ll give instructions for installing any necessary libraries on Debian systems.Addressing edge cases in configure requests
I need to handle edge cases for ConfigureRequest, particularly regarding event.value_mask bits. I’ll create an XWindowChanges structure and ensure I call XConfigureWindow with the relevant bits only.
It’s fine to be careful about which bits I supply. I might also need to use XSync as needed. I should define the macro #define _POSIX_C_SOURCE 200809L for compilation.
I know the Alt key is usually Mod1 on many keyboards, so I’m relying on that convention. Now, it’s time to write the code! Below is a tiny, self‑contained floating window manager for X11 written in C using Xlib.
Features:
Click to focus and raise windows
Alt + left mouse drag ⇒ move window
Alt + right mouse drag ⇒ resize window
Alt + Shift + Q ⇒ quit window manager
No decorations, no EWMH/ICCCM, no multi‑monitor logic
This is meant for learning and experimentation, not for daily use.