c
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static Display *dpy;
static int screen;
static Window root;
static Atom WM_PROTOCOLS, WM_DELETE_WINDOW;
static bool ismoving = false;
static bool isresizing = false;
static Window selwin = None;
static int selx, sely;
static int xerror(Display *d, XErrorEvent *ee) {
if (ee->error_code == BadAccess) {
fputs("mini-wm: another window manager is running or cannot open display\n", stderr);
exit(EXIT_FAILURE);
}
return 0;
}
static void die(const char *msg) {
fputs(msg, stderr);
exit(EXIT_FAILURE);
}
int main(void) {
if (!(dpy = XOpenDisplay(NULL)))
die("mini-wm: cannot open display\n");
XSetErrorHandler(xerror);
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
WM_PROTOCOLS = XInternAtom(dpy, "WM_PROTOCOLS", False);
WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWindowAttributes attr = { .event_mask = SubstructureRedirectMask | SubstructureNotifyMask |
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
EnterWindowMask | LeaveWindowMask | FocusChangeMask |
ExposureMask | PointerMotionMask };
XChangeWindowAttributes(dpy, root, CWEventMask, &attr);
XSync(dpy, False);
XEvent ev;
while (1) {
XNextEvent(dpy, &ev);
switch (ev.type) {
case MapRequest: {
Window w = ev.xmaprequest.window;
XWindowAttributes wa;
XGetWindowAttributes(dpy, w, &wa);
if (!wa.override_redirect) {
XSetWMProtocols(dpy, w, &WM_DELETE_WINDOW, 1);
XSelectInput(dpy, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
// Center window
int scrw = DisplayWidth(dpy, screen);
int scrh = DisplayHeight(dpy, screen);
int x = (scrw - wa.width) / 2;
int y = (scrh - wa.height) / 2;
if (x < 0) x = 0;
if (y < 0) y = 0;
XMoveWindow(dpy, w, x, y);
}
XMapWindow(dpy, w);
XRaiseWindow(dpy, w);
break;
}
case EnterNotify:
if (ev.xcrossing.mode != NotifyGrab &&
ev.xcrossing.detail != NotifyInferior) {
XSetInputFocus(dpy, ev.xcrossing.window, RevertToParent, CurrentTime);
XRaiseWindow(dpy, ev.xcrossing.window);
}
break;
case ButtonPress: {
if (ev.xbutton.subwindow == None)
break;
selwin = ev.xbutton.subwindow;
XWindowAttributes wa;
XGetWindowAttributes(dpy, selwin, &wa);
if (wa.override_redirect)
break;
XRaiseWindow(dpy, selwin);
XSetInputFocus(dpy, selwin, RevertToParent, CurrentTime);
if (!(ev.xbutton.state & Mod1Mask))
break;
int relx, rely, dummy;
XTranslateCoordinates(dpy, root, selwin,
ev.xbutton.x_root, ev.xbutton.y_root,
&relx, &rely, &dummy);
if (ev.xbutton.button == Button1) {
// Move
ismoving = true;
selx = relx;
sely = rely;
} else if (ev.xbutton.button == Button3) {
// Resize (bottom-right)
isresizing = true;
selx = wa.x;
sely = wa.y;
} else {
break;
}
XGrabPointer(dpy, selwin, True, ButtonMotionMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
break;
}
case MotionNotify:
if (!ismoving && !isresizing)
break;
if (ismoving) {
int newx = ev.xmotion.x_root - selx;
int newy = ev.xmotion.y_root - sely;
XMoveWindow(dpy, selwin, newx, newy);
} else if (isresizing) {
int neww = ev.xmotion.x_root - selx;
int newh = ev.xmotion.y_root - sely;
if (neww >= 50 && newh >= 50)
XResizeWindow(dpy, selwin, neww, newh);
}
break;
case ButtonRelease:
if (ismoving || isresizing) {
XUngrabPointer(dpy, CurrentTime);
ismoving = false;
isresizing = false;
}
break;
case ConfigureRequest: {
XConfigureRequestEvent *cre = &ev.xconfigurerequest;
XWindowChanges wc = {
.x = cre->x,
.y = cre->y,
.width = cre->width,
.height = cre->height,
.border_width = 0,
.sibling = cre->sibling,
.stack_mode = cre->stack_mode
};
unsigned int mask = cre->value_mask | CWBorderWidth;
XConfigureWindow(dpy, cre->window, mask, &wc);
break;
}
case ClientMessage:
if (ev.xclient.message_type == WM_PROTOCOLS &&
(Atom)ev.xclient.data.l[0] == WM_DELETE_WINDOW) {
XDestroyWindow(dpy, ev.xclient.window);
}
break;
case DestroyNotify:
case UnmapNotify:
case MapNotify:
/* Handled implicitly */
break;
default:
break;
}
}
return EXIT_SUCCESS;
}
This is a fully functional, ~200 line minimal X11 window manager inspired by tinywm/dwm principles.