Producing minimal X11 reparenting window manager code
I'm creating a full minimal C program using Xlib that reparents new client windows into frames with title bars and borders, handles core X11 events (MapRequest, ConfigureRequest, UnmapNotify, DestroyNotify, Expose), tracks client-to-frame mapping with XSaveContext, sets SubstructureRedirectMask on root, and gracefully handles another WM running by checking errors.
/*
* Minimalistic reparenting window manager for X11
* Build with: gcc -std=c99 -Wall -O2 -o miniwm miniwm.c -lX11
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static Display *dpy;
static Window root;
static int scr;
static unsigned border_width = 2;
static unsigned title_height = 20;
static XContext client_ctx; /* map client -> frame */
static int
wm_error_handler(Display *d, XErrorEvent *e)
{
if (e->error_code == BadAccess &&
e->request_code == X_ChangeWindowAttributes &&
e->resourceid == root) {
fprintf(stderr, "Another window manager is already running.\n");
exit(1);
}
fprintf(stderr, "X error: req=%d, code=%d, res=%lu\n",
e->request_code, e->error_code, e->resourceid);
return 0;
}
static void
draw_frame(Window frame, Window client)
{
/* Simple title bar fill */
XWindowAttributes a;
if (XGetWindowAttributes(dpy, frame, &a) == 0)
return;
XSetForeground(dpy, DefaultGC(dpy, scr), BlackPixel(dpy, scr));
XFillRectangle(dpy, frame, DefaultGC(dpy, scr), 0, 0, a.width, title_height);
/* Optionally draw window title */
char *name = NULL;
if (XFetchName(dpy, client, &name) && name) {
XSetForeground(dpy, DefaultGC(dpy, scr), WhitePixel(dpy, scr));
XDrawString(dpy, frame, DefaultGC(dpy, scr), 4, title_height - 6,
name, strlen(name));
XFree(name);
}
}
/* Create a frame for the given client window and reparent */
static void
frame_window(Window client)
{
XWindowAttributes attr;
if (!XGetWindowAttributes(dpy, client, &attr) || attr.override_redirect)
return; /* do not manage override-redirect */
/* Determine frame geometry */
int fx = attr.x - (int)border_width;
int fy = attr.y - (int)(border_width + title_height);
unsigned fw = attr.width + 2 * border_width;
unsigned fh = attr.height + 2 * border_width + title_height;
/* Create frame */
Window frame = XCreateSimpleWindow(
dpy, root, fx, fy, fw, fh,
0, BlackPixel(dpy, scr), WhitePixel(dpy, scr));
/* Listen for expose (to draw title) and button events */
XSelectInput(dpy, frame, ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | SubstructureRedirectMask | SubstructureNotifyMask);
/* Save mapping from client to frame */
XSaveContext(dpy, client, client_ctx, (XPointer)frame);
/* Reparent client inside frame */
XAddToSaveSet(dpy, client);
XReparentWindow(dpy, client, frame, border_width, border_width + title_height);
/* Map frame and client */
XMapWindow(dpy, frame);
XMapWindow(dpy, client);
draw_frame(frame, client);
}
/* Destroy frame corresponding to client */
static void
unframe_window(Window client)
{
Window frame;
if (XFindContext(dpy, client, client_ctx, (XPointer *)&frame) == 0) {
XDeleteContext(dpy, client, client_ctx);
XUnmapWindow(dpy, frame);
XDestroyWindow(dpy, frame);
}
}
/* Handle configure requests, adjusting frame and client */
static void
handle_configure_request(XConfigureRequestEvent *ev)
{
Window frame;
XWindowChanges wc;
if (XFindContext(dpy, ev->window, client_ctx, (XPointer *)&frame) == 0) {
/* The request is from a managed client; resize/move frame and client */
wc.x = ev->x - (int)border_width;
wc.y = ev->y - (int)(border_width + title_height);
wc.width = ev->width + 2 * border_width;
wc.height = ev->height + 2 * border_width + title_height;
wc.border_width = 0;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
XConfigureWindow(dpy, frame, ev->value_mask, &wc);
/* Also resize/move the client relative to the frame */
wc.x = border_width;
wc.y = border_width + title_height;
wc.width = ev->width;
wc.height = ev->height;
wc.border_width = 0;
XConfigureWindow(dpy, ev->window,
CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
} else {
/* Not managed, just pass it through */
wc.x = ev->x;
wc.y = ev->y;
wc.width = ev->width;
wc.height = ev->height;
wc.border_width = ev->border_width;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
}
}
/* Optional: simple moving of frames by dragging title bar */
static int moving = 0;
static int drag_x, drag_y;
static Window drag_frame = None;
static void
handle_button_press(XButtonEvent *ev)
{
Window client;
if (XFindContext(dpy, ev->window, client_ctx, (XPointer *)&client) != 0)
return;
/* If click is within title bar area, start moving */
if ((int)ev->y < (int)title_height) {
moving = 1;
drag_frame = ev->window;
drag_x = ev->x_root;
drag_y = ev->y_root;
}
}
static void
handle_button_release(XButtonEvent *ev)
{
moving = 0;
drag_frame = None;
}
static void
handle_motion_notify(XMotionEvent *ev)
{
if (!moving || drag_frame == None)
return;
/* Move the frame according to pointer delta */
int dx = ev->x_root - drag_x;
int dy = ev->y_root - drag_y;
XWindowAttributes a;
if (XGetWindowAttributes(dpy, drag_frame, &a)) {
XMoveWindow(dpy, drag_frame, a.x + dx, a.y + dy);
}
drag_x = ev->x_root;
drag_y = ev->y_root;
}
int
main(void)
{
XEvent ev;
dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr, "Cannot open display.\n");
return 1;
}
scr = DefaultScreen(dpy);
root = RootWindow(dpy, scr);
client_ctx = XUniqueContext();
XSetErrorHandler(wm_error_handler);
/* Select for substructure redirect on the root */
XSelectInput(dpy, root, SubstructureRedirectMask | SubstructureNotifyMask);
/* Sync to trigger potential BadAccess if another WM is running */
XSync(dpy, False);
/* Manage existing top-level windows */
unsigned int nwins;
Window dummy1, dummy2, *wins = NULL;
if (XQueryTree(dpy, root, &dummy1, &dummy2, &wins, &nwins) && wins) {
for (unsigned int i = 0; i < nwins; ++i) {
XWindowAttributes a;
if (XGetWindowAttributes(dpy, wins[i], &a) && a.map_state == IsViewable) {
frame_window(wins[i]);
}
}
XFree(wins);
}
/* Event loop */
for (;;) {
XNextEvent(dpy, &ev);
switch (ev.type) {
case MapRequest:
frame_window(ev.xmaprequest.window);
break;
case ConfigureRequest:
handle_configure_request(&ev.xconfigurerequest);
break;
case DestroyNotify:
unframe_window(ev.xdestroywindow.window);
break;
case UnmapNotify:
unframe_window(ev.xunmap.window);
break;
case Expose:
/* Redraw title bar on frame exposes */
if (ev.xexpose.window != root) {
Window client;
if (XFindContext(dpy, ev.xexpose.window, client_ctx, (XPointer *)&client) == 0)
draw_frame(ev.xexpose.window, client);
}
break;
case ButtonPress:
handle_button_press(&ev.xbutton);
break;
case ButtonRelease:
handle_button_release(&ev.xbutton);
break;
case MotionNotify:
handle_motion_notify(&ev.xmotion);
break;
default:
break;
}
}
XCloseDisplay(dpy);
return 0;
}
This program installs itself as the window manager by selecting SubstructureRedirectMask on the root window. When a new top-level client maps (MapRequest), it creates a frame with a simple title bar and border, reparents the client into it, and maps both. It tracks frames with an XContext, handles configure requests, destroys frames on unmap/destroy, and draws a basic title on expose. A very basic move-on-drag of the title bar is included.